blob: 0631efa73d9ec8add66ce4a85e57c74777b7738a [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 Pesaventob3a23ca2019-05-04 20:40:21 -04003 * Copyright (c) 2014-2019, 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
Davide Pesavento096f86a2018-11-10 19:51:45 -050026#include "websocket-channel-fixture.hpp"
Weiwei Liu280d7dd2016-03-02 23:19:26 -070027#include "face/websocket-transport.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070028
Davide Pesavento22fba352017-10-17 15:53:51 -040029#include "test-ip.hpp"
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040030
Davide Pesavento096f86a2018-11-10 19:51:45 -050031#include <boost/mpl/vector.hpp>
Eric Newberrya98bf932015-09-21 00:58:47 -070032
33namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040034namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070035namespace tests {
Eric Newberrya98bf932015-09-21 00:58:47 -070036
Davide Pesavento9a00fab2016-09-27 11:22:46 +020037BOOST_AUTO_TEST_SUITE(Face)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070038BOOST_FIXTURE_TEST_SUITE(TestWebSocketChannel, WebSocketChannelFixture)
39
Davide Pesavento096f86a2018-11-10 19:51:45 -050040using AddressFamilies = boost::mpl::vector<
41 std::integral_constant<AddressFamily, AddressFamily::V4>,
42 std::integral_constant<AddressFamily, AddressFamily::V6>>;
43
44BOOST_AUTO_TEST_CASE_TEMPLATE(Uri, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070045{
Davide Pesavento096f86a2018-11-10 19:51:45 -050046 using Address = typename IpAddressFromFamily<F::value>::type;
47 websocket::Endpoint ep(Address::loopback(), 20070);
48 auto channel = this->makeChannel(ep.address(), ep.port());
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020049 BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep, "ws"));
Weiwei Liu280d7dd2016-03-02 23:19:26 -070050}
51
Davide Pesavento096f86a2018-11-10 19:51:45 -050052BOOST_AUTO_TEST_CASE_TEMPLATE(Listen, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070053{
Davide Pesavento096f86a2018-11-10 19:51:45 -050054 using Address = typename IpAddressFromFamily<F::value>::type;
55 auto channel = this->makeChannel(Address());
Weiwei Liu280d7dd2016-03-02 23:19:26 -070056 BOOST_CHECK_EQUAL(channel->isListening(), false);
57
58 channel->listen(nullptr);
59 BOOST_CHECK_EQUAL(channel->isListening(), true);
60
61 // listen() is idempotent
62 BOOST_CHECK_NO_THROW(channel->listen(nullptr));
63 BOOST_CHECK_EQUAL(channel->isListening(), true);
64}
65
Davide Pesavento096f86a2018-11-10 19:51:45 -050066BOOST_AUTO_TEST_CASE_TEMPLATE(MultipleAccepts, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070067{
Davide Pesavento096f86a2018-11-10 19:51:45 -050068 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020069 SKIP_IF_IP_UNAVAILABLE(address);
70 this->listen(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070071
72 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
73 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
74
75 websocket::Client client1;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020076 this->clientConnect(client1);
77
Weiwei Liu280d7dd2016-03-02 23:19:26 -070078 BOOST_CHECK_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen
Davide Pesavento00335782018-02-10 22:31:33 -050079 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070080 BOOST_CHECK_EQUAL(listenerChannel->size(), 1);
81
82 websocket::Client client2;
83 websocket::Client client3;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020084 this->clientConnect(client2);
85 this->clientConnect(client3);
86
Weiwei Liu280d7dd2016-03-02 23:19:26 -070087 BOOST_CHECK_EQUAL(limitedIo.run(4, // 2 listenerOnFaceCreated, 2 clientHandleOpen
Davide Pesavento00335782018-02-10 22:31:33 -050088 2_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070089 BOOST_CHECK_EQUAL(listenerChannel->size(), 3);
90
91 // check face persistency
92 for (const auto& face : listenerFaces) {
93 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
94 }
95}
96
Davide Pesavento096f86a2018-11-10 19:51:45 -050097BOOST_AUTO_TEST_CASE_TEMPLATE(Send, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070098{
Davide Pesavento096f86a2018-11-10 19:51:45 -050099 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200100 SKIP_IF_IP_UNAVAILABLE(address);
101 this->initialize(address);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500102 auto transport = listenerFaces.at(0)->getTransport();
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700103
104 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400105 transport->send(pkt1);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700106 BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
Davide Pesavento00335782018-02-10 22:31:33 -0500107 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700108
109 Block pkt2 = ndn::encoding::makeStringBlock(301, "world!");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400110 transport->send(pkt2);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700111 BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
Davide Pesavento00335782018-02-10 22:31:33 -0500112 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700113
114 BOOST_REQUIRE_EQUAL(clientReceivedMessages.size(), 2);
115 BOOST_CHECK_EQUAL_COLLECTIONS(
116 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()),
117 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()) + clientReceivedMessages[0].size(),
118 pkt1.begin(), pkt1.end());
119 BOOST_CHECK_EQUAL_COLLECTIONS(
120 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()),
121 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()) + clientReceivedMessages[1].size(),
122 pkt2.begin(), pkt2.end());
123}
124
Davide Pesavento096f86a2018-11-10 19:51:45 -0500125BOOST_AUTO_TEST_CASE_TEMPLATE(Receive, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700126{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500127 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200128 SKIP_IF_IP_UNAVAILABLE(address);
129 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700130
131 // use network-layer packets here, otherwise GenericLinkService
132 // won't recognize the packet type and will discard it
133 auto interest1 = makeInterest("ndn:/TpnzGvW9R");
134 auto interest2 = makeInterest("ndn:/QWiIMfj5sL");
135
Davide Pesavento096f86a2018-11-10 19:51:45 -0500136 this->clientSendInterest(*interest1);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700137 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
Davide Pesavento00335782018-02-10 22:31:33 -0500138 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700139
Davide Pesavento096f86a2018-11-10 19:51:45 -0500140 this->clientSendInterest(*interest2);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700141 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
Davide Pesavento00335782018-02-10 22:31:33 -0500142 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700143
144 BOOST_REQUIRE_EQUAL(faceReceivedInterests.size(), 2);
145 BOOST_CHECK_EQUAL(faceReceivedInterests[0].getName(), interest1->getName());
146 BOOST_CHECK_EQUAL(faceReceivedInterests[1].getName(), interest2->getName());
147}
148
Davide Pesavento096f86a2018-11-10 19:51:45 -0500149BOOST_AUTO_TEST_CASE_TEMPLATE(FaceClosure, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700150{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500151 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200152 SKIP_IF_IP_UNAVAILABLE(address);
153 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700154
Davide Pesavento096f86a2018-11-10 19:51:45 -0500155 listenerFaces.at(0)->close();
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700156 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
157}
158
Davide Pesavento096f86a2018-11-10 19:51:45 -0500159BOOST_AUTO_TEST_CASE_TEMPLATE(RemoteClose, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700160{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500161 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200162 SKIP_IF_IP_UNAVAILABLE(address);
163 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700164
165 client.close(clientHandle, websocketpp::close::status::going_away, "");
166 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceClosedSignal
Davide Pesavento00335782018-02-10 22:31:33 -0500167 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700168 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
169}
170
Davide Pesavento096f86a2018-11-10 19:51:45 -0500171BOOST_AUTO_TEST_CASE_TEMPLATE(SetPingInterval, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700172{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500173 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200174 SKIP_IF_IP_UNAVAILABLE(address);
Davide Pesavento00335782018-02-10 22:31:33 -0500175 const auto pingInterval = 1200_ms;
176 this->initialize(address, pingInterval);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700177
178 BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing
Davide Pesavento00335782018-02-10 22:31:33 -0500179 pingInterval * 3), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700180 BOOST_CHECK_LE(measuredPingInterval, pingInterval * 1.1);
181 BOOST_CHECK_GE(measuredPingInterval, pingInterval * 0.9);
182}
183
Davide Pesavento096f86a2018-11-10 19:51:45 -0500184BOOST_AUTO_TEST_CASE_TEMPLATE(SetPongTimeOut, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700185{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500186 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200187 SKIP_IF_IP_UNAVAILABLE(address);
Davide Pesavento00335782018-02-10 22:31:33 -0500188 this->initialize(address, 600_ms, 300_ms);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500189 this->clientShouldPong = false;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200190
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700191 BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, faceClosedSignal
Davide Pesavento00335782018-02-10 22:31:33 -0500192 2_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700193 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
194
Davide Pesavento096f86a2018-11-10 19:51:45 -0500195 auto transport = static_cast<WebSocketTransport*>(listenerFaces.at(0)->getTransport());
Davide Pesavento00335782018-02-10 22:31:33 -0500196 BOOST_CHECK(transport->getState() == TransportState::FAILED ||
197 transport->getState() == TransportState::CLOSED);
198 BOOST_CHECK_GE(transport->getCounters().nOutPings, 1);
199 BOOST_CHECK_LE(transport->getCounters().nOutPings, 2);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700200 BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 0);
201}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700202
203BOOST_AUTO_TEST_SUITE_END() // TestWebSocketChannel
204BOOST_AUTO_TEST_SUITE_END() // Face
205
206} // namespace tests
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400207} // namespace face
Eric Newberrya98bf932015-09-21 00:58:47 -0700208} // namespace nfd