blob: 8c295ba72c75d6a6cb772941b3b1be69ca0ed1f6 [file] [log] [blame]
Yukai Tu2d6d5632015-10-26 11:06:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "face/websocket-transport.hpp"
27#include "face/lp-face.hpp"
28#include "dummy-receive-link-service.hpp"
29#include "transport-properties.hpp"
30
31#include "tests/test-common.hpp"
32#include "tests/limited-io.hpp"
33
34namespace nfd {
35namespace face {
36namespace tests {
37
38using namespace nfd::tests;
39namespace ip = boost::asio::ip;
40
41BOOST_AUTO_TEST_SUITE(Face)
42
43/** \brief a fixture that accepts a single WebSocket connection from a client
44 */
45class SingleWebSocketFixture : public BaseFixture
46{
47public:
48 SingleWebSocketFixture()
49 : transport(nullptr)
50 , serverReceivedPackets(nullptr)
51 , clientShouldPong(true)
52 {
53 }
54
55 /** \brief initialize server and start listening
56 */
57 void
58 serverListen(const ip::tcp::endpoint& ep,
59 const time::milliseconds& pongTimeout = time::milliseconds(1000))
60 {
61 server.clear_access_channels(websocketpp::log::alevel::all);
62 server.clear_error_channels(websocketpp::log::elevel::all);
63
64 server.init_asio(&g_io);
65 server.set_open_handler(bind(&SingleWebSocketFixture::serverHandleOpen, this, _1));
66 server.set_close_handler(bind(&SingleWebSocketFixture::serverHandleClose, this));
67 server.set_message_handler(bind(&SingleWebSocketFixture::serverHandleMessage, this, _2));
68 server.set_pong_handler(bind(&SingleWebSocketFixture::serverHandlePong, this));
69 server.set_pong_timeout_handler(bind(&SingleWebSocketFixture::serverHandlePongTimeout, this));
70 server.set_pong_timeout(pongTimeout.count());
71
72 server.set_reuse_addr(true);
73
74 server.listen(ep);
75 server.start_accept();
76 }
77
78 /** \brief initialize client and connect to server
79 */
80 void
81 clientConnect(const std::string& uri)
82 {
83 client.clear_access_channels(websocketpp::log::alevel::all);
84 client.clear_error_channels(websocketpp::log::elevel::all);
85
86 client.init_asio(&g_io);
87 client.set_open_handler(bind(&SingleWebSocketFixture::clientHandleOpen, this, _1));
88 client.set_message_handler(bind(&SingleWebSocketFixture::clientHandleMessage, this, _2));
89 client.set_ping_handler(bind(&SingleWebSocketFixture::clientHandlePing, this));
90
91 websocketpp::lib::error_code ec;
92 websocket::Client::connection_ptr con = client.get_connection("ws://127.0.0.1:20070", ec);
93 BOOST_REQUIRE(!ec);
94
95 client.connect(con);
96 }
97
98 void
99 makeFace(const time::milliseconds& pingInterval = time::milliseconds(10000))
100 {
101 face = make_unique<LpFace>(
102 make_unique<DummyReceiveLinkService>(),
103 make_unique<WebSocketTransport>(serverHdl, ref(server), pingInterval));
104 transport = static_cast<WebSocketTransport*>(face->getTransport());
105 serverReceivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
106 }
107
108 /** \brief initialize both server and client, and have each other connected, create Transport
109 */
110 void
111 endToEndInitialize(const ip::tcp::endpoint& ep,
112 const time::milliseconds& pingInterval = time::milliseconds(10000),
113 const time::milliseconds& pongTimeout = time::milliseconds(1000))
114 {
115 this->serverListen(ep, pongTimeout);
116 std::string uri = "ws://" + ep.address().to_string() + ":" + to_string(ep.port());
117 this->clientConnect(uri);
118 BOOST_REQUIRE_EQUAL(limitedIo.run(2, // serverHandleOpen, clientHandleOpen
119 time::seconds(1)), LimitedIo::EXCEED_OPS);
120 this->makeFace(pingInterval);
121 }
122
123private:
124 void
125 serverHandleOpen(websocketpp::connection_hdl hdl)
126 {
127 serverHdl = hdl;
128 limitedIo.afterOp();
129 }
130
131 void
132 serverHandleClose()
133 {
134 if (transport == nullptr) {
135 return;
136 }
137
138 transport->close();
139 limitedIo.afterOp();
140 }
141
142 void
143 serverHandleMessage(websocket::Server::message_ptr msg)
144 {
145 if (transport == nullptr) {
146 return;
147 }
148
149 transport->receiveMessage(msg->get_payload());
150 limitedIo.afterOp();
151 }
152
153 void
154 serverHandlePong()
155 {
156 if (transport == nullptr) {
157 return;
158 }
159
160 transport->handlePong();
161 limitedIo.afterOp();
162 }
163
164 void
165 serverHandlePongTimeout()
166 {
167 if (transport == nullptr) {
168 return;
169 }
170
171 transport->handlePongTimeout();
172 limitedIo.afterOp();
173 }
174
175 void
176 clientHandleOpen(websocketpp::connection_hdl hdl)
177 {
178 clientHdl = hdl;
179 limitedIo.afterOp();
180 }
181
182 void
183 clientHandleMessage(websocket::Client::message_ptr msg)
184 {
185 clientReceivedMessages.push_back(msg->get_payload());
186 limitedIo.afterOp();
187 }
188
189 bool
190 clientHandlePing()
191 {
192 limitedIo.afterOp();
193 return clientShouldPong;
194 }
195
196public:
197 LimitedIo limitedIo;
198
199 websocket::Server server;
200 websocketpp::connection_hdl serverHdl;
201 unique_ptr<LpFace> face;
202 WebSocketTransport* transport;
203 std::vector<Transport::Packet>* serverReceivedPackets;
204
205 websocket::Client client;
206 websocketpp::connection_hdl clientHdl;
207 bool clientShouldPong;
208 std::vector<std::string> clientReceivedMessages;
209};
210
211BOOST_FIXTURE_TEST_SUITE(TestWebSocketTransport, SingleWebSocketFixture)
212
213BOOST_AUTO_TEST_CASE(StaticProperties)
214{
215 ip::tcp::endpoint ep(ip::address_v4::loopback(), 20070);
216 this->endToEndInitialize(ep);
217 checkStaticPropertiesInitialized(*transport);
218
219 BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("ws://127.0.0.1:20070"));
220 BOOST_CHECK_EQUAL(transport->getRemoteUri().getScheme(), "wsclient");
221 BOOST_CHECK_EQUAL(transport->getRemoteUri().getHost(), "127.0.0.1");
222 BOOST_CHECK_EQUAL(transport->getRemoteUri().getPath(), "");
223 BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_LOCAL);
224 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
225 BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
226 BOOST_CHECK_EQUAL(transport->getMtu(), MTU_UNLIMITED);
227}
228
229BOOST_AUTO_TEST_CASE(PingPong)
230{
231 ip::tcp::endpoint ep(ip::address_v4::loopback(), 20070);
232 this->endToEndInitialize(ep, time::milliseconds(500), time::milliseconds(300));
233
234 BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, serverHandlePong
235 time::milliseconds(1500)), LimitedIo::EXCEED_OPS);
236 BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
Junxiao Shi57df2882015-11-11 06:12:35 -0700237 BOOST_CHECK_EQUAL(transport->getCounters().nOutPings, 1);
238 BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 1);
Yukai Tu2d6d5632015-10-26 11:06:02 -0700239
240 this->clientShouldPong = false;
241 BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, serverHandlePongTimeout
242 time::milliseconds(2000)), LimitedIo::EXCEED_OPS);
243 BOOST_CHECK_MESSAGE(transport->getState() == TransportState::FAILED ||
244 transport->getState() == TransportState::CLOSED,
245 "expect FAILED or CLOSED state, actual state=" << transport->getState());
Junxiao Shi57df2882015-11-11 06:12:35 -0700246 BOOST_CHECK_EQUAL(transport->getCounters().nOutPings, 2);
247 BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 1);
Yukai Tu2d6d5632015-10-26 11:06:02 -0700248}
249
250BOOST_AUTO_TEST_CASE(Send)
251{
252 ip::tcp::endpoint ep(ip::address_v4::loopback(), 20070);
253 this->endToEndInitialize(ep);
254
255 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
256 transport->send(Transport::Packet(Block(pkt1)));
257 BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
258 time::milliseconds(1000)), LimitedIo::EXCEED_OPS);
259
260 Block pkt2 = ndn::encoding::makeStringBlock(301, "world!");
261 transport->send(Transport::Packet(Block(pkt2)));
262 BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
263 time::milliseconds(1000)), LimitedIo::EXCEED_OPS);
264
265 BOOST_REQUIRE_EQUAL(clientReceivedMessages.size(), 2);
266 BOOST_CHECK_EQUAL_COLLECTIONS(
267 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()),
268 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()) + clientReceivedMessages[0].size(),
269 pkt1.begin(), pkt1.end());
270 BOOST_CHECK_EQUAL_COLLECTIONS(
271 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()),
272 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()) + clientReceivedMessages[1].size(),
273 pkt2.begin(), pkt2.end());
274}
275
276BOOST_AUTO_TEST_CASE(Receive)
277{
278 ip::tcp::endpoint ep(ip::address_v4::loopback(), 20070);
279 this->endToEndInitialize(ep);
280
281 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
282 client.send(clientHdl, pkt1.wire(), pkt1.size(), websocketpp::frame::opcode::binary);
283 BOOST_CHECK_EQUAL(limitedIo.run(1, // serverHandleMessage
284 time::milliseconds(1000)), LimitedIo::EXCEED_OPS);
285
286 Block pkt2 = ndn::encoding::makeStringBlock(301, "world!");
287 client.send(clientHdl, pkt2.wire(), pkt2.size(), websocketpp::frame::opcode::binary);
288 BOOST_CHECK_EQUAL(limitedIo.run(1, // serverHandleMessage
289 time::milliseconds(1000)), LimitedIo::EXCEED_OPS);
290
291 BOOST_REQUIRE_EQUAL(serverReceivedPackets->size(), 2);
292 BOOST_CHECK(serverReceivedPackets->at(0).packet == pkt1);
293 BOOST_CHECK(serverReceivedPackets->at(1).packet == pkt2);
294 BOOST_CHECK_EQUAL(serverReceivedPackets->at(0).remoteEndpoint, serverReceivedPackets->at(1).remoteEndpoint);
295}
296
297BOOST_AUTO_TEST_CASE(ReceiveMalformed)
298{
299 ip::tcp::endpoint ep(ip::address_v4::loopback(), 20070);
300 this->endToEndInitialize(ep);
301
302 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
303 client.send(clientHdl, pkt1.wire(), pkt1.size() - 1, // truncated
304 websocketpp::frame::opcode::binary);
305 BOOST_CHECK_EQUAL(limitedIo.run(1, // serverHandleMessage
306 time::milliseconds(1000)), LimitedIo::EXCEED_OPS);
307
308 // bad packet is dropped
309 BOOST_CHECK_EQUAL(transport->getState(), TransportState::UP);
310 BOOST_CHECK_EQUAL(serverReceivedPackets->size(), 0);
311
312 Block pkt2 = ndn::encoding::makeStringBlock(301, "world!");
313 client.send(clientHdl, pkt2.wire(), pkt2.size(), websocketpp::frame::opcode::binary);
314 BOOST_CHECK_EQUAL(limitedIo.run(1, // serverHandleMessage
315 time::milliseconds(1000)), LimitedIo::EXCEED_OPS);
316
317 // next valid packet is still received normally
318 BOOST_REQUIRE_EQUAL(serverReceivedPackets->size(), 1);
319 BOOST_CHECK(serverReceivedPackets->at(0).packet == pkt2);
320}
321
322BOOST_AUTO_TEST_SUITE_END() // TestWebSocketTransport
323BOOST_AUTO_TEST_SUITE_END() // Face
324
325} // namespace tests
326} // namespace face
327} // namespace nfd