Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 26 | #include "face/websocket-channel.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 | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame^] | 29 | #include "channel-fixture.hpp" |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 30 | #include "test-ip.hpp" |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 33 | namespace tests { |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 34 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 35 | namespace ip = boost::asio::ip; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame^] | 37 | class WebSocketChannelFixture : public ChannelFixture<WebSocketChannel, websocket::Endpoint> |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 38 | { |
| 39 | protected: |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame^] | 40 | virtual unique_ptr<WebSocketChannel> |
| 41 | makeChannel(const ip::address& addr, uint16_t port = 0) final |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 42 | { |
| 43 | if (port == 0) |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame^] | 44 | port = getNextPort(); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 45 | |
| 46 | return make_unique<WebSocketChannel>(websocket::Endpoint(addr, port)); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | listen(const ip::address& addr, |
| 51 | const time::milliseconds& pingInterval = time::seconds(10), |
| 52 | const time::milliseconds& pongTimeout = time::seconds(1)) |
| 53 | { |
| 54 | listenerEp = websocket::Endpoint(addr, 20030); |
| 55 | listenerChannel = makeChannel(addr, 20030); |
| 56 | listenerChannel->setPingInterval(pingInterval); |
| 57 | listenerChannel->setPongTimeout(pongTimeout); |
| 58 | listenerChannel->listen(bind(&WebSocketChannelFixture::listenerOnFaceCreated, this, _1)); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | clientConnect(websocket::Client& client) |
| 63 | { |
| 64 | client.clear_access_channels(websocketpp::log::alevel::all); |
| 65 | client.clear_error_channels(websocketpp::log::elevel::all); |
| 66 | |
| 67 | client.init_asio(&g_io); |
| 68 | client.set_open_handler(bind(&WebSocketChannelFixture::clientHandleOpen, this, _1)); |
| 69 | client.set_message_handler(bind(&WebSocketChannelFixture::clientHandleMessage, this, _1, _2)); |
| 70 | client.set_ping_handler(bind(&WebSocketChannelFixture::clientHandlePing, this, _1, _2)); |
| 71 | |
| 72 | std::string uri = "ws://" + listenerEp.address().to_string() + ":" + to_string(listenerEp.port()); |
| 73 | websocketpp::lib::error_code ec; |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame^] | 74 | auto con = client.get_connection(uri, ec); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 75 | BOOST_REQUIRE_EQUAL(ec, websocketpp::lib::error_code()); |
| 76 | |
| 77 | client.connect(con); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | initialize(const ip::address& addr, |
| 82 | const time::milliseconds& pingInterval = time::seconds(10), |
| 83 | const time::milliseconds& pongTimeout = time::seconds(1)) |
| 84 | { |
| 85 | listen(addr, pingInterval, pongTimeout); |
| 86 | clientConnect(client); |
| 87 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen |
| 88 | time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 89 | BOOST_REQUIRE_EQUAL(listenerChannel->size(), 1); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | clientSendInterest(const Interest& interest) |
| 94 | { |
| 95 | const Block& payload = interest.wireEncode(); |
| 96 | client.send(clientHandle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | void |
| 101 | listenerOnFaceCreated(const shared_ptr<Face>& newFace) |
| 102 | { |
| 103 | BOOST_REQUIRE(newFace != nullptr); |
| 104 | newFace->afterReceiveInterest.connect(bind(&WebSocketChannelFixture::faceAfterReceiveInterest, this, _1)); |
| 105 | connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); }); |
| 106 | listenerFaces.push_back(newFace); |
| 107 | limitedIo.afterOp(); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | faceAfterReceiveInterest(const Interest& interest) |
| 112 | { |
| 113 | faceReceivedInterests.push_back(interest); |
| 114 | limitedIo.afterOp(); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | clientHandleOpen(websocketpp::connection_hdl hdl) |
| 119 | { |
| 120 | clientHandle = hdl; |
| 121 | limitedIo.afterOp(); |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | clientHandleMessage(websocketpp::connection_hdl, websocket::Client::message_ptr msg) |
| 126 | { |
| 127 | clientReceivedMessages.push_back(msg->get_payload()); |
| 128 | limitedIo.afterOp(); |
| 129 | } |
| 130 | |
| 131 | bool |
| 132 | clientHandlePing(websocketpp::connection_hdl, std::string) |
| 133 | { |
| 134 | auto now = time::steady_clock::now(); |
| 135 | if (m_prevPingRecvTime != time::steady_clock::TimePoint()) { |
| 136 | measuredPingInterval = now - m_prevPingRecvTime; |
| 137 | } |
| 138 | m_prevPingRecvTime = now; |
| 139 | |
| 140 | limitedIo.afterOp(); |
| 141 | return clientShouldPong; |
| 142 | } |
| 143 | |
| 144 | protected: |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 145 | std::vector<Interest> faceReceivedInterests; |
| 146 | |
| 147 | websocket::Client client; |
| 148 | websocketpp::connection_hdl clientHandle; |
| 149 | std::vector<std::string> clientReceivedMessages; |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame^] | 150 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 151 | time::steady_clock::Duration measuredPingInterval; |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame^] | 152 | bool clientShouldPong = true; // set clientShouldPong false to disable the pong response, |
| 153 | // which will cause timeout in listenerChannel |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 154 | |
| 155 | private: |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 156 | time::steady_clock::TimePoint m_prevPingRecvTime; |
| 157 | }; |
| 158 | |
Davide Pesavento | 9a00fab | 2016-09-27 11:22:46 +0200 | [diff] [blame^] | 159 | BOOST_AUTO_TEST_SUITE(Face) |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 160 | BOOST_FIXTURE_TEST_SUITE(TestWebSocketChannel, WebSocketChannelFixture) |
| 161 | |
| 162 | BOOST_AUTO_TEST_CASE(Uri) |
| 163 | { |
| 164 | websocket::Endpoint ep(ip::address_v4::loopback(), 20070); |
| 165 | auto channel = makeChannel(ep.address(), ep.port()); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(channel->getUri(), FaceUri(ep, "ws")); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | BOOST_AUTO_TEST_CASE(Listen) |
| 170 | { |
| 171 | auto channel = makeChannel(ip::address_v4()); |
| 172 | BOOST_CHECK_EQUAL(channel->isListening(), false); |
| 173 | |
| 174 | channel->listen(nullptr); |
| 175 | BOOST_CHECK_EQUAL(channel->isListening(), true); |
| 176 | |
| 177 | // listen() is idempotent |
| 178 | BOOST_CHECK_NO_THROW(channel->listen(nullptr)); |
| 179 | BOOST_CHECK_EQUAL(channel->isListening(), true); |
| 180 | } |
| 181 | |
| 182 | BOOST_AUTO_TEST_CASE(MultipleAccepts) |
| 183 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 184 | auto address = getTestIp<ip::address_v4>(LoopbackAddress::Yes); |
| 185 | SKIP_IF_IP_UNAVAILABLE(address); |
| 186 | this->listen(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 187 | |
| 188 | BOOST_CHECK_EQUAL(listenerChannel->isListening(), true); |
| 189 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 190 | |
| 191 | websocket::Client client1; |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 192 | this->clientConnect(client1); |
| 193 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 194 | BOOST_CHECK_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen |
| 195 | time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 196 | BOOST_CHECK_EQUAL(listenerChannel->size(), 1); |
| 197 | |
| 198 | websocket::Client client2; |
| 199 | websocket::Client client3; |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 200 | this->clientConnect(client2); |
| 201 | this->clientConnect(client3); |
| 202 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 203 | BOOST_CHECK_EQUAL(limitedIo.run(4, // 2 listenerOnFaceCreated, 2 clientHandleOpen |
| 204 | time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 205 | BOOST_CHECK_EQUAL(listenerChannel->size(), 3); |
| 206 | |
| 207 | // check face persistency |
| 208 | for (const auto& face : listenerFaces) { |
| 209 | BOOST_CHECK_EQUAL(face->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | BOOST_AUTO_TEST_CASE(Send) |
| 214 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 215 | auto address = getTestIp<ip::address_v4>(LoopbackAddress::Yes); |
| 216 | SKIP_IF_IP_UNAVAILABLE(address); |
| 217 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 218 | auto transport = listenerFaces.front()->getTransport(); |
| 219 | |
| 220 | Block pkt1 = ndn::encoding::makeStringBlock(300, "hello"); |
| 221 | transport->send(face::Transport::Packet(Block(pkt1))); |
| 222 | BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage |
| 223 | time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 224 | |
| 225 | Block pkt2 = ndn::encoding::makeStringBlock(301, "world!"); |
| 226 | transport->send(face::Transport::Packet(Block(pkt2))); |
| 227 | BOOST_CHECK_EQUAL(limitedIo.run(1, // clientHandleMessage |
| 228 | time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 229 | |
| 230 | BOOST_REQUIRE_EQUAL(clientReceivedMessages.size(), 2); |
| 231 | BOOST_CHECK_EQUAL_COLLECTIONS( |
| 232 | reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()), |
| 233 | reinterpret_cast<const uint8_t*>(clientReceivedMessages[0].data()) + clientReceivedMessages[0].size(), |
| 234 | pkt1.begin(), pkt1.end()); |
| 235 | BOOST_CHECK_EQUAL_COLLECTIONS( |
| 236 | reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()), |
| 237 | reinterpret_cast<const uint8_t*>(clientReceivedMessages[1].data()) + clientReceivedMessages[1].size(), |
| 238 | pkt2.begin(), pkt2.end()); |
| 239 | } |
| 240 | |
| 241 | BOOST_AUTO_TEST_CASE(Receive) |
| 242 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 243 | auto address = getTestIp<ip::address_v4>(LoopbackAddress::Yes); |
| 244 | SKIP_IF_IP_UNAVAILABLE(address); |
| 245 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 246 | |
| 247 | // use network-layer packets here, otherwise GenericLinkService |
| 248 | // won't recognize the packet type and will discard it |
| 249 | auto interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 250 | auto interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 251 | |
| 252 | clientSendInterest(*interest1); |
| 253 | BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest |
| 254 | time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 255 | |
| 256 | clientSendInterest(*interest2); |
| 257 | BOOST_CHECK_EQUAL(limitedIo.run(1, // faceAfterReceiveInterest |
| 258 | time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 259 | |
| 260 | BOOST_REQUIRE_EQUAL(faceReceivedInterests.size(), 2); |
| 261 | BOOST_CHECK_EQUAL(faceReceivedInterests[0].getName(), interest1->getName()); |
| 262 | BOOST_CHECK_EQUAL(faceReceivedInterests[1].getName(), interest2->getName()); |
| 263 | } |
| 264 | |
| 265 | BOOST_AUTO_TEST_CASE(FaceClosure) |
| 266 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 267 | auto address = getTestIp<ip::address_v4>(LoopbackAddress::Yes); |
| 268 | SKIP_IF_IP_UNAVAILABLE(address); |
| 269 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 270 | |
| 271 | listenerFaces.front()->close(); |
| 272 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 273 | } |
| 274 | |
| 275 | BOOST_AUTO_TEST_CASE(RemoteClose) |
| 276 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 277 | auto address = getTestIp<ip::address_v4>(LoopbackAddress::Yes); |
| 278 | SKIP_IF_IP_UNAVAILABLE(address); |
| 279 | this->initialize(address); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 280 | |
| 281 | client.close(clientHandle, websocketpp::close::status::going_away, ""); |
| 282 | BOOST_CHECK_EQUAL(limitedIo.run(1, // faceClosedSignal |
| 283 | time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 284 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 285 | } |
| 286 | |
| 287 | BOOST_AUTO_TEST_CASE(SetPingInterval) |
| 288 | { |
| 289 | auto pingInterval = time::milliseconds(300); |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 290 | auto address = getTestIp<ip::address_v4>(LoopbackAddress::Yes); |
| 291 | SKIP_IF_IP_UNAVAILABLE(address); |
| 292 | this->initialize(address, pingInterval, time::milliseconds(1000)); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 293 | |
| 294 | BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing |
| 295 | time::seconds(1)), LimitedIo::EXCEED_OPS); |
| 296 | BOOST_CHECK_LE(measuredPingInterval, pingInterval * 1.1); |
| 297 | BOOST_CHECK_GE(measuredPingInterval, pingInterval * 0.9); |
| 298 | } |
| 299 | |
| 300 | BOOST_AUTO_TEST_CASE(SetPongTimeOut) |
| 301 | { |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 302 | auto address = getTestIp<ip::address_v4>(LoopbackAddress::Yes); |
| 303 | SKIP_IF_IP_UNAVAILABLE(address); |
| 304 | this->initialize(address, time::milliseconds(500), time::milliseconds(300)); |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 305 | clientShouldPong = false; |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 306 | |
Weiwei Liu | 280d7dd | 2016-03-02 23:19:26 -0700 | [diff] [blame] | 307 | BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing, faceClosedSignal |
| 308 | time::seconds(2)), LimitedIo::EXCEED_OPS); |
| 309 | BOOST_CHECK_EQUAL(listenerChannel->size(), 0); |
| 310 | |
| 311 | auto transport = static_cast<face::WebSocketTransport*>(listenerFaces.front()->getTransport()); |
| 312 | BOOST_CHECK(transport->getState() == face::TransportState::FAILED || |
| 313 | transport->getState() == face::TransportState::CLOSED); |
| 314 | BOOST_CHECK_EQUAL(transport->getCounters().nOutPings, 1); |
| 315 | BOOST_CHECK_EQUAL(transport->getCounters().nInPongs, 0); |
| 316 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 317 | |
| 318 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocketChannel |
| 319 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 320 | |
| 321 | } // namespace tests |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 322 | } // namespace nfd |