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 | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, 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 | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 31 | #include <boost/mpl/vector.hpp> |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 34 | namespace face { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 35 | namespace tests { |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Face) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestWebSocketChannel, WebSocketChannelFixture) |
| 39 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 40 | using AddressFamilies = boost::mpl::vector< |
| 41 | std::integral_constant<AddressFamily, AddressFamily::V4>, |
| 42 | std::integral_constant<AddressFamily, AddressFamily::V6>>; |
| 43 | |
| 44 | BOOST_AUTO_TEST_CASE_TEMPLATE(Uri, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 45 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 46 | using Address = typename IpAddressFromFamily<F::value>::type; |
| 47 | websocket::Endpoint ep(Address::loopback(), 20070); |
| 48 | auto channel = this->makeChannel(ep.address(), ep.port()); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep, "ws")); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 52 | BOOST_AUTO_TEST_CASE_TEMPLATE(Listen, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 53 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 54 | using Address = typename IpAddressFromFamily<F::value>::type; |
| 55 | auto channel = this->makeChannel(Address()); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 56 | 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 Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 66 | BOOST_AUTO_TEST_CASE_TEMPLATE(MultipleAccepts, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 67 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 68 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 69 | SKIP_IF_IP_UNAVAILABLE(address); |
| 70 | this->listen(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 71 | |
| 72 | BOOST_CHECK_EQUAL(listenerChannel->isListening(), true); |
| 73 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 74 | |
| 75 | websocket::Client client1; |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 76 | this->clientConnect(client1); |
| 77 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 79 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(listenerChannel->size(), 1); |
| 81 | |
| 82 | websocket::Client client2; |
| 83 | websocket::Client client3; |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 84 | this->clientConnect(client2); |
| 85 | this->clientConnect(client3); |
| 86 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL(limitedIo.run(4, // 2 listenerOnFaceCreated, 2 clientHandleOpen |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 88 | 2_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(listenerChannel->size(), 3); |
| 90 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 91 | // check face persistency and channel association |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 92 | for (const auto& face : listenerFaces) { |
| 93 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(face->getChannel().lock(), listenerChannel); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE_TEMPLATE(Send, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 99 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 100 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 101 | SKIP_IF_IP_UNAVAILABLE(address); |
| 102 | this->initialize(address); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 103 | auto transport = listenerFaces.at(0)->getTransport(); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 104 | |
| 105 | Block pkt1 = ndn::encoding::makeStringBlock(300, "hello"); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 106 | transport->send(pkt1); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 108 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 109 | |
| 110 | Block pkt2 = ndn::encoding::makeStringBlock(301, "world!"); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 111 | transport->send(pkt2); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 112 | BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 113 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 114 | |
| 115 | BOOST_REQUIRE_EQUAL(clientReceivedMessages.size(), 2); |
| 116 | BOOST_CHECK_EQUAL_COLLECTIONS( |
| 117 | reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()), |
| 118 | reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()) + clientReceivedMessages[0].size(), |
| 119 | pkt1.begin(), pkt1.end()); |
| 120 | BOOST_CHECK_EQUAL_COLLECTIONS( |
| 121 | reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()), |
| 122 | reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()) + clientReceivedMessages[1].size(), |
| 123 | pkt2.begin(), pkt2.end()); |
| 124 | } |
| 125 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE_TEMPLATE(Receive, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 127 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 128 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 129 | SKIP_IF_IP_UNAVAILABLE(address); |
| 130 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 131 | |
| 132 | // use network-layer packets here, otherwise GenericLinkService |
| 133 | // won't recognize the packet type and will discard it |
| 134 | auto interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 135 | auto interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 136 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 137 | this->clientSendInterest(*interest1); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 139 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 140 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 141 | this->clientSendInterest(*interest2); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 143 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 144 | |
| 145 | BOOST_REQUIRE_EQUAL(faceReceivedInterests.size(), 2); |
| 146 | BOOST_CHECK_EQUAL(faceReceivedInterests[0].getName(), interest1->getName()); |
| 147 | BOOST_CHECK_EQUAL(faceReceivedInterests[1].getName(), interest2->getName()); |
| 148 | } |
| 149 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE_TEMPLATE(FaceClosure, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 151 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 152 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 153 | SKIP_IF_IP_UNAVAILABLE(address); |
| 154 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 155 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(listenerFaces.at(0)->getChannel().lock(), listenerChannel); |
| 157 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 158 | listenerFaces.at(0)->close(); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 160 | } |
| 161 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 162 | BOOST_AUTO_TEST_CASE_TEMPLATE(RemoteClose, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 163 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 164 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 165 | SKIP_IF_IP_UNAVAILABLE(address); |
| 166 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 167 | |
| 168 | client.close(clientHandle, websocketpp::close::status::going_away, ""); |
| 169 | BOOST_CHECK_EQUAL(limitedIo.run(1, // faceClosedSignal |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 170 | 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 172 | } |
| 173 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 174 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetPingInterval, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 175 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 176 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 177 | SKIP_IF_IP_UNAVAILABLE(address); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 178 | const auto pingInterval = 1500_ms; |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 179 | this->initialize(address, pingInterval); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 180 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 181 | BOOST_CHECK_EQUAL(limitedIo.run(5, // clientHandlePing |
| 182 | 5 * pingInterval + 1_s), LimitedIo::EXCEED_OPS); |
| 183 | BOOST_CHECK_EQUAL(measuredPingIntervals.size(), 4); |
| 184 | |
| 185 | auto avgPingInterval = std::accumulate(measuredPingIntervals.begin(), measuredPingIntervals.end(), 0_ns); |
| 186 | avgPingInterval /= measuredPingIntervals.size(); |
| 187 | BOOST_CHECK_LE(avgPingInterval, pingInterval * 1.1); |
| 188 | BOOST_CHECK_GE(avgPingInterval, pingInterval * 0.9); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 191 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetPongTimeOut, F, AddressFamilies) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 192 | { |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 193 | auto address = getTestIp(F::value, AddressScope::Loopback); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 194 | SKIP_IF_IP_UNAVAILABLE(address); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 195 | this->initialize(address, 600_ms, 300_ms); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 196 | this->clientShouldPong = false; |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 197 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 198 | BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, faceClosedSignal |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 199 | 2_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 200 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 201 | |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 202 | auto transport = static_cast<WebSocketTransport*>(listenerFaces.at(0)->getTransport()); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 203 | BOOST_CHECK(transport->getState() == TransportState::FAILED || |
| 204 | transport->getState() == TransportState::CLOSED); |
| 205 | BOOST_CHECK_GE(transport->getCounters().nOutPings, 1); |
| 206 | BOOST_CHECK_LE(transport->getCounters().nOutPings, 2); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 0); |
| 208 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 209 | |
| 210 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocketChannel |
| 211 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 212 | |
| 213 | } // namespace tests |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 214 | } // namespace face |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 215 | } // namespace nfd |