blob: 5c1a60d1114a65185c30d3ab45810fa3202466d4 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi84d62cb2017-07-12 16:15:18 +00002/*
Davide Pesavento8fd15e62017-04-06 19:58:54 -04003 * Copyright (c) 2014-2017, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * 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
Junxiao Shicde37ad2015-12-24 01:02:05 -070026#include "face/websocket-channel.hpp"
Weiwei Liu280d7dd2016-03-02 23:19:26 -070027#include "face/websocket-transport.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070028
Davide Pesavento9a00fab2016-09-27 11:22:46 +020029#include "channel-fixture.hpp"
Junxiao Shi84d62cb2017-07-12 16:15:18 +000030#include "test-netif-ip.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070031
32namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040033namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070034namespace tests {
Eric Newberrya98bf932015-09-21 00:58:47 -070035
Weiwei Liu280d7dd2016-03-02 23:19:26 -070036namespace ip = boost::asio::ip;
Junxiao Shicde37ad2015-12-24 01:02:05 -070037
Davide Pesavento9a00fab2016-09-27 11:22:46 +020038class WebSocketChannelFixture : public ChannelFixture<WebSocketChannel, websocket::Endpoint>
Weiwei Liu280d7dd2016-03-02 23:19:26 -070039{
40protected:
Davide Pesavento8fd15e62017-04-06 19:58:54 -040041 unique_ptr<WebSocketChannel>
Davide Pesavento9a00fab2016-09-27 11:22:46 +020042 makeChannel(const ip::address& addr, uint16_t port = 0) final
Weiwei Liu280d7dd2016-03-02 23:19:26 -070043 {
44 if (port == 0)
Davide Pesavento9a00fab2016-09-27 11:22:46 +020045 port = getNextPort();
Weiwei Liu280d7dd2016-03-02 23:19:26 -070046
47 return make_unique<WebSocketChannel>(websocket::Endpoint(addr, port));
48 }
49
50 void
51 listen(const ip::address& addr,
52 const time::milliseconds& pingInterval = time::seconds(10),
53 const time::milliseconds& pongTimeout = time::seconds(1))
54 {
55 listenerEp = websocket::Endpoint(addr, 20030);
56 listenerChannel = makeChannel(addr, 20030);
57 listenerChannel->setPingInterval(pingInterval);
58 listenerChannel->setPongTimeout(pongTimeout);
59 listenerChannel->listen(bind(&WebSocketChannelFixture::listenerOnFaceCreated, this, _1));
60 }
61
62 void
63 clientConnect(websocket::Client& client)
64 {
65 client.clear_access_channels(websocketpp::log::alevel::all);
66 client.clear_error_channels(websocketpp::log::elevel::all);
67
68 client.init_asio(&g_io);
69 client.set_open_handler(bind(&WebSocketChannelFixture::clientHandleOpen, this, _1));
70 client.set_message_handler(bind(&WebSocketChannelFixture::clientHandleMessage, this, _1, _2));
71 client.set_ping_handler(bind(&WebSocketChannelFixture::clientHandlePing, this, _1, _2));
72
73 std::string uri = "ws://" + listenerEp.address().to_string() + ":" + to_string(listenerEp.port());
74 websocketpp::lib::error_code ec;
Davide Pesavento9a00fab2016-09-27 11:22:46 +020075 auto con = client.get_connection(uri, ec);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070076 BOOST_REQUIRE_EQUAL(ec, websocketpp::lib::error_code());
77
78 client.connect(con);
79 }
80
81 void
82 initialize(const ip::address& addr,
83 const time::milliseconds& pingInterval = time::seconds(10),
84 const time::milliseconds& pongTimeout = time::seconds(1))
85 {
86 listen(addr, pingInterval, pongTimeout);
87 clientConnect(client);
88 BOOST_REQUIRE_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen
89 time::seconds(1)), LimitedIo::EXCEED_OPS);
90 BOOST_REQUIRE_EQUAL(listenerChannel->size(), 1);
91 }
92
93 void
94 clientSendInterest(const Interest& interest)
95 {
96 const Block& payload = interest.wireEncode();
97 client.send(clientHandle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
98 }
99
100private:
101 void
102 listenerOnFaceCreated(const shared_ptr<Face>& newFace)
103 {
104 BOOST_REQUIRE(newFace != nullptr);
105 newFace->afterReceiveInterest.connect(bind(&WebSocketChannelFixture::faceAfterReceiveInterest, this, _1));
106 connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
107 listenerFaces.push_back(newFace);
108 limitedIo.afterOp();
109 }
110
111 void
112 faceAfterReceiveInterest(const Interest& interest)
113 {
114 faceReceivedInterests.push_back(interest);
115 limitedIo.afterOp();
116 }
117
118 void
119 clientHandleOpen(websocketpp::connection_hdl hdl)
120 {
121 clientHandle = hdl;
122 limitedIo.afterOp();
123 }
124
125 void
126 clientHandleMessage(websocketpp::connection_hdl, websocket::Client::message_ptr msg)
127 {
128 clientReceivedMessages.push_back(msg->get_payload());
129 limitedIo.afterOp();
130 }
131
132 bool
133 clientHandlePing(websocketpp::connection_hdl, std::string)
134 {
135 auto now = time::steady_clock::now();
136 if (m_prevPingRecvTime != time::steady_clock::TimePoint()) {
137 measuredPingInterval = now - m_prevPingRecvTime;
138 }
139 m_prevPingRecvTime = now;
140
141 limitedIo.afterOp();
142 return clientShouldPong;
143 }
144
145protected:
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700146 std::vector<Interest> faceReceivedInterests;
147
148 websocket::Client client;
149 websocketpp::connection_hdl clientHandle;
150 std::vector<std::string> clientReceivedMessages;
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200151
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700152 time::steady_clock::Duration measuredPingInterval;
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200153 bool clientShouldPong = true; // set clientShouldPong false to disable the pong response,
154 // which will cause timeout in listenerChannel
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700155
156private:
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700157 time::steady_clock::TimePoint m_prevPingRecvTime;
158};
159
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200160BOOST_AUTO_TEST_SUITE(Face)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700161BOOST_FIXTURE_TEST_SUITE(TestWebSocketChannel, WebSocketChannelFixture)
162
163BOOST_AUTO_TEST_CASE(Uri)
164{
165 websocket::Endpoint ep(ip::address_v4::loopback(), 20070);
166 auto channel = makeChannel(ep.address(), ep.port());
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200167 BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep, "ws"));
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700168}
169
170BOOST_AUTO_TEST_CASE(Listen)
171{
172 auto channel = makeChannel(ip::address_v4());
173 BOOST_CHECK_EQUAL(channel->isListening(), false);
174
175 channel->listen(nullptr);
176 BOOST_CHECK_EQUAL(channel->isListening(), true);
177
178 // listen() is idempotent
179 BOOST_CHECK_NO_THROW(channel->listen(nullptr));
180 BOOST_CHECK_EQUAL(channel->isListening(), true);
181}
182
183BOOST_AUTO_TEST_CASE(MultipleAccepts)
184{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400185 auto address = getTestIp(AddressFamily::V4, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200186 SKIP_IF_IP_UNAVAILABLE(address);
187 this->listen(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700188
189 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
190 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
191
192 websocket::Client client1;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200193 this->clientConnect(client1);
194
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700195 BOOST_CHECK_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen
196 time::seconds(1)), LimitedIo::EXCEED_OPS);
197 BOOST_CHECK_EQUAL(listenerChannel->size(), 1);
198
199 websocket::Client client2;
200 websocket::Client client3;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200201 this->clientConnect(client2);
202 this->clientConnect(client3);
203
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700204 BOOST_CHECK_EQUAL(limitedIo.run(4, // 2 listenerOnFaceCreated, 2 clientHandleOpen
Eric Newberry9f3973e2017-05-26 22:14:44 -0700205 time::seconds(2)), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700206 BOOST_CHECK_EQUAL(listenerChannel->size(), 3);
207
208 // check face persistency
209 for (const auto& face : listenerFaces) {
210 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
211 }
212}
213
214BOOST_AUTO_TEST_CASE(Send)
215{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400216 auto address = getTestIp(AddressFamily::V4, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200217 SKIP_IF_IP_UNAVAILABLE(address);
218 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700219 auto transport = listenerFaces.front()->getTransport();
220
221 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
222 transport->send(face::Transport::Packet(Block(pkt1)));
223 BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
224 time::seconds(1)), LimitedIo::EXCEED_OPS);
225
226 Block pkt2 = ndn::encoding::makeStringBlock(301, "world!");
227 transport->send(face::Transport::Packet(Block(pkt2)));
228 BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
229 time::seconds(1)), LimitedIo::EXCEED_OPS);
230
231 BOOST_REQUIRE_EQUAL(clientReceivedMessages.size(), 2);
232 BOOST_CHECK_EQUAL_COLLECTIONS(
233 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()),
234 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()) + clientReceivedMessages[0].size(),
235 pkt1.begin(), pkt1.end());
236 BOOST_CHECK_EQUAL_COLLECTIONS(
237 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()),
238 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()) + clientReceivedMessages[1].size(),
239 pkt2.begin(), pkt2.end());
240}
241
242BOOST_AUTO_TEST_CASE(Receive)
243{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400244 auto address = getTestIp(AddressFamily::V4, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200245 SKIP_IF_IP_UNAVAILABLE(address);
246 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700247
248 // use network-layer packets here, otherwise GenericLinkService
249 // won't recognize the packet type and will discard it
250 auto interest1 = makeInterest("ndn:/TpnzGvW9R");
251 auto interest2 = makeInterest("ndn:/QWiIMfj5sL");
252
253 clientSendInterest(*interest1);
254 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
255 time::seconds(1)), LimitedIo::EXCEED_OPS);
256
257 clientSendInterest(*interest2);
258 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
259 time::seconds(1)), LimitedIo::EXCEED_OPS);
260
261 BOOST_REQUIRE_EQUAL(faceReceivedInterests.size(), 2);
262 BOOST_CHECK_EQUAL(faceReceivedInterests[0].getName(), interest1->getName());
263 BOOST_CHECK_EQUAL(faceReceivedInterests[1].getName(), interest2->getName());
264}
265
266BOOST_AUTO_TEST_CASE(FaceClosure)
267{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400268 auto address = getTestIp(AddressFamily::V4, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200269 SKIP_IF_IP_UNAVAILABLE(address);
270 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700271
272 listenerFaces.front()->close();
273 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
274}
275
276BOOST_AUTO_TEST_CASE(RemoteClose)
277{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400278 auto address = getTestIp(AddressFamily::V4, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200279 SKIP_IF_IP_UNAVAILABLE(address);
280 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700281
282 client.close(clientHandle, websocketpp::close::status::going_away, "");
283 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceClosedSignal
284 time::seconds(1)), LimitedIo::EXCEED_OPS);
285 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
286}
287
288BOOST_AUTO_TEST_CASE(SetPingInterval)
289{
Junxiao Shicfe81d52017-09-15 03:26:08 +0000290 const time::milliseconds pingInterval(800);
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400291 auto address = getTestIp(AddressFamily::V4, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200292 SKIP_IF_IP_UNAVAILABLE(address);
293 this->initialize(address, pingInterval, time::milliseconds(1000));
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700294
295 BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing
Junxiao Shicfe81d52017-09-15 03:26:08 +0000296 pingInterval * 3), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700297 BOOST_CHECK_LE(measuredPingInterval, pingInterval * 1.1);
298 BOOST_CHECK_GE(measuredPingInterval, pingInterval * 0.9);
299}
300
301BOOST_AUTO_TEST_CASE(SetPongTimeOut)
302{
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400303 auto address = getTestIp(AddressFamily::V4, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200304 SKIP_IF_IP_UNAVAILABLE(address);
305 this->initialize(address, time::milliseconds(500), time::milliseconds(300));
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700306 clientShouldPong = false;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200307
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700308 BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, faceClosedSignal
309 time::seconds(2)), LimitedIo::EXCEED_OPS);
310 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
311
312 auto transport = static_cast<face::WebSocketTransport*>(listenerFaces.front()->getTransport());
313 BOOST_CHECK(transport->getState() == face::TransportState::FAILED ||
314 transport->getState() == face::TransportState::CLOSED);
315 BOOST_CHECK_EQUAL(transport->getCounters().nOutPings, 1);
316 BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 0);
317}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700318
319BOOST_AUTO_TEST_SUITE_END() // TestWebSocketChannel
320BOOST_AUTO_TEST_SUITE_END() // Face
321
322} // namespace tests
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400323} // namespace face
Eric Newberrya98bf932015-09-21 00:58:47 -0700324} // namespace nfd