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