blob: 402f9c684bb300e386d577374380eb3dbb444b72 [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 Pesaventof56cf632024-01-27 22:22:06 -05003 * Copyright (c) 2014-2024, 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 Pesaventof56cf632024-01-27 22:22:06 -050031#include <boost/mp11/list.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 Pesaventof56cf632024-01-27 22:22:06 -050038using AddressFamilies = boost::mp11::mp_list_c<AddressFamily, AddressFamily::V4, AddressFamily::V6>;
Davide Pesavento096f86a2018-11-10 19:51:45 -050039
40BOOST_AUTO_TEST_CASE_TEMPLATE(Uri, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070041{
Davide Pesavento279af1c2022-08-29 20:18:32 -040042 websocket::Endpoint ep(IpAddressTypeFromFamily<F::value>::loopback(), 20070);
Davide Pesavento096f86a2018-11-10 19:51:45 -050043 auto channel = this->makeChannel(ep.address(), ep.port());
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020044 BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep, "ws"));
Weiwei Liu280d7dd2016-03-02 23:19:26 -070045}
46
Davide Pesavento096f86a2018-11-10 19:51:45 -050047BOOST_AUTO_TEST_CASE_TEMPLATE(Listen, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070048{
Davide Pesavento279af1c2022-08-29 20:18:32 -040049 auto channel = this->makeChannel(IpAddressTypeFromFamily<F::value>());
Weiwei Liu280d7dd2016-03-02 23:19:26 -070050 BOOST_CHECK_EQUAL(channel->isListening(), false);
51
52 channel->listen(nullptr);
53 BOOST_CHECK_EQUAL(channel->isListening(), true);
54
55 // listen() is idempotent
56 BOOST_CHECK_NO_THROW(channel->listen(nullptr));
57 BOOST_CHECK_EQUAL(channel->isListening(), true);
58}
59
Davide Pesavento096f86a2018-11-10 19:51:45 -050060BOOST_AUTO_TEST_CASE_TEMPLATE(MultipleAccepts, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070061{
Davide Pesavento096f86a2018-11-10 19:51:45 -050062 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020063 SKIP_IF_IP_UNAVAILABLE(address);
64 this->listen(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070065
66 BOOST_CHECK_EQUAL(listenerChannel->isListening(), true);
67 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
68
69 websocket::Client client1;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020070 this->clientConnect(client1);
71
Weiwei Liu280d7dd2016-03-02 23:19:26 -070072 BOOST_CHECK_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen
Davide Pesavento00335782018-02-10 22:31:33 -050073 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070074 BOOST_CHECK_EQUAL(listenerChannel->size(), 1);
75
76 websocket::Client client2;
77 websocket::Client client3;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020078 this->clientConnect(client2);
79 this->clientConnect(client3);
80
Weiwei Liu280d7dd2016-03-02 23:19:26 -070081 BOOST_CHECK_EQUAL(limitedIo.run(4, // 2 listenerOnFaceCreated, 2 clientHandleOpen
Davide Pesavento00335782018-02-10 22:31:33 -050082 2_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070083 BOOST_CHECK_EQUAL(listenerChannel->size(), 3);
84
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040085 // check face persistency and channel association
Weiwei Liu280d7dd2016-03-02 23:19:26 -070086 for (const auto& face : listenerFaces) {
87 BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040088 BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel);
Weiwei Liu280d7dd2016-03-02 23:19:26 -070089 }
90}
91
Davide Pesavento096f86a2018-11-10 19:51:45 -050092BOOST_AUTO_TEST_CASE_TEMPLATE(Send, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -070093{
Davide Pesavento096f86a2018-11-10 19:51:45 -050094 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +020095 SKIP_IF_IP_UNAVAILABLE(address);
96 this->initialize(address);
Davide Pesavento096f86a2018-11-10 19:51:45 -050097 auto transport = listenerFaces.at(0)->getTransport();
Weiwei Liu280d7dd2016-03-02 23:19:26 -070098
99 Block pkt1 = ndn::encoding::makeStringBlock(300, "hello");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400100 transport->send(pkt1);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700101 BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage
Davide Pesavento00335782018-02-10 22:31:33 -0500102 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700103
104 Block pkt2 = ndn::encoding::makeStringBlock(301, "world!");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400105 transport->send(pkt2);
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 BOOST_REQUIRE_EQUAL(clientReceivedMessages.size(), 2);
110 BOOST_CHECK_EQUAL_COLLECTIONS(
111 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()),
112 reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()) + clientReceivedMessages[0].size(),
113 pkt1.begin(), pkt1.end());
114 BOOST_CHECK_EQUAL_COLLECTIONS(
115 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()),
116 reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()) + clientReceivedMessages[1].size(),
117 pkt2.begin(), pkt2.end());
118}
119
Davide Pesavento096f86a2018-11-10 19:51:45 -0500120BOOST_AUTO_TEST_CASE_TEMPLATE(Receive, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700121{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500122 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200123 SKIP_IF_IP_UNAVAILABLE(address);
124 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700125
126 // use network-layer packets here, otherwise GenericLinkService
127 // won't recognize the packet type and will discard it
128 auto interest1 = makeInterest("ndn:/TpnzGvW9R");
129 auto interest2 = makeInterest("ndn:/QWiIMfj5sL");
130
Davide Pesavento096f86a2018-11-10 19:51:45 -0500131 this->clientSendInterest(*interest1);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700132 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
Davide Pesavento00335782018-02-10 22:31:33 -0500133 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700134
Davide Pesavento096f86a2018-11-10 19:51:45 -0500135 this->clientSendInterest(*interest2);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700136 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest
Davide Pesavento00335782018-02-10 22:31:33 -0500137 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700138
139 BOOST_REQUIRE_EQUAL(faceReceivedInterests.size(), 2);
140 BOOST_CHECK_EQUAL(faceReceivedInterests[0].getName(), interest1->getName());
141 BOOST_CHECK_EQUAL(faceReceivedInterests[1].getName(), interest2->getName());
142}
143
Davide Pesavento096f86a2018-11-10 19:51:45 -0500144BOOST_AUTO_TEST_CASE_TEMPLATE(FaceClosure, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700145{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500146 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200147 SKIP_IF_IP_UNAVAILABLE(address);
148 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700149
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400150 BOOST_CHECK_EQUAL(listenerFaces.at(0)->getChannel().lock(), listenerChannel);
151
Davide Pesavento096f86a2018-11-10 19:51:45 -0500152 listenerFaces.at(0)->close();
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700153 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
154}
155
Davide Pesavento096f86a2018-11-10 19:51:45 -0500156BOOST_AUTO_TEST_CASE_TEMPLATE(RemoteClose, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700157{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500158 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200159 SKIP_IF_IP_UNAVAILABLE(address);
160 this->initialize(address);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700161
162 client.close(clientHandle, websocketpp::close::status::going_away, "");
163 BOOST_CHECK_EQUAL(limitedIo.run(1, // faceClosedSignal
Davide Pesavento00335782018-02-10 22:31:33 -0500164 1_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700165 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
166}
167
Davide Pesavento096f86a2018-11-10 19:51:45 -0500168BOOST_AUTO_TEST_CASE_TEMPLATE(SetPingInterval, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700169{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500170 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200171 SKIP_IF_IP_UNAVAILABLE(address);
Davide Pesavento412c9822021-07-02 00:21:05 -0400172 const auto pingInterval = 1500_ms;
Davide Pesavento00335782018-02-10 22:31:33 -0500173 this->initialize(address, pingInterval);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700174
Davide Pesavento412c9822021-07-02 00:21:05 -0400175 BOOST_CHECK_EQUAL(limitedIo.run(5, // clientHandlePing
176 5 * pingInterval + 1_s), LimitedIo::EXCEED_OPS);
177 BOOST_CHECK_EQUAL(measuredPingIntervals.size(), 4);
178
179 auto avgPingInterval = std::accumulate(measuredPingIntervals.begin(), measuredPingIntervals.end(), 0_ns);
180 avgPingInterval /= measuredPingIntervals.size();
181 BOOST_CHECK_LE(avgPingInterval, pingInterval * 1.1);
182 BOOST_CHECK_GE(avgPingInterval, pingInterval * 0.9);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700183}
184
Davide Pesavento096f86a2018-11-10 19:51:45 -0500185BOOST_AUTO_TEST_CASE_TEMPLATE(SetPongTimeOut, F, AddressFamilies)
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700186{
Davide Pesavento096f86a2018-11-10 19:51:45 -0500187 auto address = getTestIp(F::value, AddressScope::Loopback);
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200188 SKIP_IF_IP_UNAVAILABLE(address);
Davide Pesavento00335782018-02-10 22:31:33 -0500189 this->initialize(address, 600_ms, 300_ms);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500190 this->clientShouldPong = false;
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200191
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700192 BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, faceClosedSignal
Davide Pesavento00335782018-02-10 22:31:33 -0500193 2_s), LimitedIo::EXCEED_OPS);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700194 BOOST_CHECK_EQUAL(listenerChannel->size(), 0);
195
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400196 auto transport = static_cast<face::WebSocketTransport*>(listenerFaces.at(0)->getTransport());
197 BOOST_CHECK(transport->getState() == face::TransportState::FAILED ||
198 transport->getState() == face::TransportState::CLOSED);
Davide Pesavento00335782018-02-10 22:31:33 -0500199 BOOST_CHECK_GE(transport->getCounters().nOutPings, 1);
200 BOOST_CHECK_LE(transport->getCounters().nOutPings, 2);
Weiwei Liu280d7dd2016-03-02 23:19:26 -0700201 BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 0);
202}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700203
204BOOST_AUTO_TEST_SUITE_END() // TestWebSocketChannel
205BOOST_AUTO_TEST_SUITE_END() // Face
206
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400207} // namespace nfd::tests