blob: 15d1a572a24d6d8b17359442052283e473ac81ee [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
Eric Newberrya98bf932015-09-21 00:58:47 -070034
Davide Pesavento9a00fab2016-09-27 11:22:46 +020035BOOST_AUTO_TEST_SUITE(Face)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070036BOOST_FIXTURE_TEST_SUITE(TestWebSocketChannel, WebSocketChannelFixture)
37
Davide Pesavento096f86a2018-11-10 19:51:45 -050038using AddressFamilies = boost::mpl::vector<
39 std::integral_constant<AddressFamily, AddressFamily::V4>,
40 std::integral_constant<AddressFamily, AddressFamily::V6>>;
41
42BOOST_AUTO_TEST_CASE_TEMPLATE(Uri, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070043{
Davide Pesavento279af1c2022-08-29 20:18:32 -040044 websocket::Endpoint ep(IpAddressTypeFromFamily<F::value>::loopback(), 20070);
Davide Pesavento096f86a2018-11-10 19:51:45 -050045 auto channel = this->makeChannel(ep.address(), ep.port());
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020046 BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep, "ws"));
Weiwei Liu280d7dd2016-03-02 23:19:26 -070047}
48
Davide Pesavento096f86a2018-11-10 19:51:45 -050049BOOST_AUTO_TEST_CASE_TEMPLATE(Listen, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070050{
Davide Pesavento279af1c2022-08-29 20:18:32 -040051 auto channel = this->makeChannel(IpAddressTypeFromFamily<F::value>());
Weiwei Liu280d7dd2016-03-02 23:19:26 -070052 BOOST_CHECK_EQUAL(channel->isListening(), false);
53
54 channel->listen(nullptr);
55 BOOST_CHECK_EQUAL(channel->isListening(), true);
56
57 // listen() is idempotent
58 BOOST_CHECK_NO_THROW(channel->listen(nullptr));
59 BOOST_CHECK_EQUAL(channel->isListening(), true);
60}
61
Davide Pesavento096f86a2018-11-10 19:51:45 -050062BOOST_AUTO_TEST_CASE_TEMPLATE(MultipleAccepts, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070063{
Davide Pesavento096f86a2018-11-10 19:51:45 -050064 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020065 SKIP_IF_IP_UNAVAILABLE(address);
66 this->listen(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070067
68 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
69 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
70
71 websocket::Client client1;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020072 this->clientConnect(client1);
73
Weiwei Liu280d7dd2016-03-02 23:19:26 -070074 BOOST_CHECK_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen
Davide Pesavento00335782018-02-10 22:31:33 -050075 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070076 BOOST_CHECK_EQUAL(listenerChannel->size(), 1);
77
78 websocket::Client client2;
79 websocket::Client client3;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020080 this->clientConnect(client2);
81 this->clientConnect(client3);
82
Weiwei Liu280d7dd2016-03-02 23:19:26 -070083 BOOST_CHECK_EQUAL(limitedIo.run(4, // 2 listenerOnFaceCreated, 2 clientHandleOpen
Davide Pesavento00335782018-02-10 22:31:33 -050084 2_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070085 BOOST_CHECK_EQUAL(listenerChannel->size(), 3);
86
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040087 // check face persistency and channel association
Weiwei Liu280d7dd2016-03-02 23:19:26 -070088 for (const auto& face : listenerFaces) {
89 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040090 BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070091 }
92}
93
Davide Pesavento096f86a2018-11-10 19:51:45 -050094BOOST_AUTO_TEST_CASE_TEMPLATE(Send, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070095{
Davide Pesavento096f86a2018-11-10 19:51:45 -050096 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020097 SKIP_IF_IP_UNAVAILABLE(address);
98 this->initialize(address);
Davide Pesavento096f86a2018-11-10 19:51:45 -050099 auto transport = listenerFaces.at(0)->getTransport();
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700100
101 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400102 transport->send(pkt1);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700103 BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
Davide Pesavento00335782018-02-10 22:31:33 -0500104 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700105
106 Block pkt2 = ndn::encoding::makeStringBlock(301, "world!");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400107 transport->send(pkt2);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700108 BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
Davide Pesavento00335782018-02-10 22:31:33 -0500109 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700110
111 BOOST_REQUIRE_EQUAL(clientReceivedMessages.size(), 2);
112 BOOST_CHECK_EQUAL_COLLECTIONS(
113 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()),
114 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()) + clientReceivedMessages[0].size(),
115 pkt1.begin(), pkt1.end());
116 BOOST_CHECK_EQUAL_COLLECTIONS(
117 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()),
118 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()) + clientReceivedMessages[1].size(),
119 pkt2.begin(), pkt2.end());
120}
121
Davide Pesavento096f86a2018-11-10 19:51:45 -0500122BOOST_AUTO_TEST_CASE_TEMPLATE(Receive, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700123{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500124 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200125 SKIP_IF_IP_UNAVAILABLE(address);
126 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700127
128 // use network-layer packets here, otherwise GenericLinkService
129 // won't recognize the packet type and will discard it
130 auto interest1 = makeInterest("ndn:/TpnzGvW9R");
131 auto interest2 = makeInterest("ndn:/QWiIMfj5sL");
132
Davide Pesavento096f86a2018-11-10 19:51:45 -0500133 this->clientSendInterest(*interest1);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700134 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
Davide Pesavento00335782018-02-10 22:31:33 -0500135 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700136
Davide Pesavento096f86a2018-11-10 19:51:45 -0500137 this->clientSendInterest(*interest2);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700138 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
Davide Pesavento00335782018-02-10 22:31:33 -0500139 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700140
141 BOOST_REQUIRE_EQUAL(faceReceivedInterests.size(), 2);
142 BOOST_CHECK_EQUAL(faceReceivedInterests[0].getName(), interest1->getName());
143 BOOST_CHECK_EQUAL(faceReceivedInterests[1].getName(), interest2->getName());
144}
145
Davide Pesavento096f86a2018-11-10 19:51:45 -0500146BOOST_AUTO_TEST_CASE_TEMPLATE(FaceClosure, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700147{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500148 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200149 SKIP_IF_IP_UNAVAILABLE(address);
150 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700151
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400152 BOOST_CHECK_EQUAL(listenerFaces.at(0)->getChannel().lock(), listenerChannel);
153
Davide Pesavento096f86a2018-11-10 19:51:45 -0500154 listenerFaces.at(0)->close();
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700155 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
156}
157
Davide Pesavento096f86a2018-11-10 19:51:45 -0500158BOOST_AUTO_TEST_CASE_TEMPLATE(RemoteClose, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700159{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500160 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200161 SKIP_IF_IP_UNAVAILABLE(address);
162 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700163
164 client.close(clientHandle, websocketpp::close::status::going_away, "");
165 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceClosedSignal
Davide Pesavento00335782018-02-10 22:31:33 -0500166 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700167 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
168}
169
Davide Pesavento096f86a2018-11-10 19:51:45 -0500170BOOST_AUTO_TEST_CASE_TEMPLATE(SetPingInterval, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700171{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500172 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200173 SKIP_IF_IP_UNAVAILABLE(address);
Davide Pesavento412c9822021-07-02 00:21:05 -0400174 const auto pingInterval = 1500_ms;
Davide Pesavento00335782018-02-10 22:31:33 -0500175 this->initialize(address, pingInterval);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700176
Davide Pesavento412c9822021-07-02 00:21:05 -0400177 BOOST_CHECK_EQUAL(limitedIo.run(5, // clientHandlePing
178 5 * pingInterval + 1_s), LimitedIo::EXCEED_OPS);
179 BOOST_CHECK_EQUAL(measuredPingIntervals.size(), 4);
180
181 auto avgPingInterval = std::accumulate(measuredPingIntervals.begin(), measuredPingIntervals.end(), 0_ns);
182 avgPingInterval /= measuredPingIntervals.size();
183 BOOST_CHECK_LE(avgPingInterval, pingInterval * 1.1);
184 BOOST_CHECK_GE(avgPingInterval, pingInterval * 0.9);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700185}
186
Davide Pesavento096f86a2018-11-10 19:51:45 -0500187BOOST_AUTO_TEST_CASE_TEMPLATE(SetPongTimeOut, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700188{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500189 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200190 SKIP_IF_IP_UNAVAILABLE(address);
Davide Pesavento00335782018-02-10 22:31:33 -0500191 this->initialize(address, 600_ms, 300_ms);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500192 this->clientShouldPong = false;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200193
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700194 BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, faceClosedSignal
Davide Pesavento00335782018-02-10 22:31:33 -0500195 2_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700196 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
197
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400198 auto transport = static_cast<face::WebSocketTransport*>(listenerFaces.at(0)->getTransport());
199 BOOST_CHECK(transport->getState() == face::TransportState::FAILED ||
200 transport->getState() == face::TransportState::CLOSED);
Davide Pesavento00335782018-02-10 22:31:33 -0500201 BOOST_CHECK_GE(transport->getCounters().nOutPings, 1);
202 BOOST_CHECK_LE(transport->getCounters().nOutPings, 2);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700203 BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 0);
204}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700205
206BOOST_AUTO_TEST_SUITE_END() // TestWebSocketChannel
207BOOST_AUTO_TEST_SUITE_END() // Face
208
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400209} // namespace nfd::tests