Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 26 | #include "websocket-channel-fixture.hpp" |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 27 | #include "face/websocket-transport.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 29 | #include "test-ip.hpp" |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 30 | |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 31 | #include <boost/mp11/list.hpp> |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(Face) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(TestWebSocketChannel, WebSocketChannelFixture) |
| 37 | |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 38 | using AddressFamilies = boost::mp11::mp_list_c<AddressFamily, AddressFamily::V4, AddressFamily::V6>; |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 39 | |
| 40 | BOOST_AUTO_TEST_CASE_TEMPLATE(Uri, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 41 | { |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 42 | websocket::Endpoint ep(IpAddressTypeFromFamily<F::value>::loopback(), 20070); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 43 | auto channel = this->makeChannel(ep.address(), ep.port()); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 44 | BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep, "ws")); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 47 | BOOST_AUTO_TEST_CASE_TEMPLATE(Listen, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 48 | { |
Davide Pesavento | 279af1c | 2022-08-29 20:18:32 -0400 | [diff] [blame] | 49 | auto channel = this->makeChannel(IpAddressTypeFromFamily<F::value>()); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 50 | 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 Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 60 | BOOST_AUTO_TEST_CASE_TEMPLATE(MultipleAccepts, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 61 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 62 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 63 | SKIP_IF_IP_UNAVAILABLE(address); |
| 64 | this->listen(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 65 | |
| 66 | BOOST_CHECK_EQUAL(listenerChannel->isListening(), true); |
| 67 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 68 | |
| 69 | websocket::Client client1; |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 70 | this->clientConnect(client1); |
| 71 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 73 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(listenerChannel->size(), 1); |
| 75 | |
| 76 | websocket::Client client2; |
| 77 | websocket::Client client3; |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 78 | this->clientConnect(client2); |
| 79 | this->clientConnect(client3); |
| 80 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(limitedIo.run(4, // 2 listenerOnFaceCreated, 2 clientHandleOpen |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 82 | 2_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(listenerChannel->size(), 3); |
| 84 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 85 | // check face persistency and channel association |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 86 | for (const auto& face : listenerFaces) { |
| 87 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 92 | BOOST_AUTO_TEST_CASE_TEMPLATE(Send, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 93 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 94 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 95 | SKIP_IF_IP_UNAVAILABLE(address); |
| 96 | this->initialize(address); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 97 | auto transport = listenerFaces.at(0)->getTransport(); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 98 | |
| 99 | Block pkt1 = ndn::encoding::makeStringBlock(300, "hello"); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 100 | transport->send(pkt1); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 102 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 103 | |
| 104 | Block pkt2 = ndn::encoding::makeStringBlock(301, "world!"); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 105 | transport->send(pkt2); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 107 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 108 | |
| 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 Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 120 | BOOST_AUTO_TEST_CASE_TEMPLATE(Receive, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 121 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 122 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 123 | SKIP_IF_IP_UNAVAILABLE(address); |
| 124 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 125 | |
| 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 Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 131 | this->clientSendInterest(*interest1); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 133 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 134 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 135 | this->clientSendInterest(*interest2); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 137 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 138 | |
| 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 Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 144 | BOOST_AUTO_TEST_CASE_TEMPLATE(FaceClosure, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 145 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 146 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 147 | SKIP_IF_IP_UNAVAILABLE(address); |
| 148 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 150 | BOOST_CHECK_EQUAL(listenerFaces.at(0)->getChannel().lock(), listenerChannel); |
| 151 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 152 | listenerFaces.at(0)->close(); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 153 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 154 | } |
| 155 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 156 | BOOST_AUTO_TEST_CASE_TEMPLATE(RemoteClose, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 157 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 158 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 159 | SKIP_IF_IP_UNAVAILABLE(address); |
| 160 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 161 | |
| 162 | client.close(clientHandle, websocketpp::close::status::going_away, ""); |
| 163 | BOOST_CHECK_EQUAL(limitedIo.run(1, // faceClosedSignal |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 164 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 166 | } |
| 167 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 168 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetPingInterval, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 169 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 170 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 171 | SKIP_IF_IP_UNAVAILABLE(address); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 172 | const auto pingInterval = 1500_ms; |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 173 | this->initialize(address, pingInterval); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 174 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 175 | 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 Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 185 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetPongTimeOut, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 186 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 187 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 188 | SKIP_IF_IP_UNAVAILABLE(address); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 189 | this->initialize(address, 600_ms, 300_ms); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 190 | this->clientShouldPong = false; |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 191 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 192 | BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, faceClosedSignal |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 193 | 2_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 194 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 195 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 196 | 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 Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 199 | BOOST_CHECK_GE(transport->getCounters().nOutPings, 1); |
| 200 | BOOST_CHECK_LE(transport->getCounters().nOutPings, 2); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 0); |
| 202 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 203 | |
| 204 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocketChannel |
| 205 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 206 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 207 | } // namespace nfd::tests |