Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [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 | |
| 26 | #ifndef NFD_TESTS_DAEMON_FACE_WEBSOCKET_TRANSPORT_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_WEBSOCKET_TRANSPORT_FIXTURE_HPP |
| 28 | |
| 29 | #include "face/websocket-transport.hpp" |
| 30 | #include "face/face.hpp" |
| 31 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 32 | #include "tests/test-common.hpp" |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 33 | #include "tests/daemon/limited-io.hpp" |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame^] | 34 | #include "tests/daemon/face/dummy-link-service.hpp" |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 35 | |
| 36 | namespace nfd { |
| 37 | namespace face { |
| 38 | namespace tests { |
| 39 | |
| 40 | using namespace nfd::tests; |
| 41 | namespace ip = boost::asio::ip; |
| 42 | |
| 43 | /** \brief a fixture that accepts a single WebSocket connection from a client |
| 44 | */ |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 45 | class WebSocketTransportFixture : public GlobalIoFixture |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 46 | { |
| 47 | protected: |
| 48 | WebSocketTransportFixture() |
| 49 | : transport(nullptr) |
| 50 | , serverReceivedPackets(nullptr) |
| 51 | , clientShouldPong(true) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | /** \brief initialize server and start listening |
| 56 | */ |
| 57 | void |
| 58 | serverListen(const ip::tcp::endpoint& ep, |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 59 | const time::milliseconds& pongTimeout = 1_s) |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 60 | { |
| 61 | server.clear_access_channels(websocketpp::log::alevel::all); |
| 62 | server.clear_error_channels(websocketpp::log::elevel::all); |
| 63 | |
| 64 | server.init_asio(&g_io); |
| 65 | server.set_open_handler(bind(&WebSocketTransportFixture::serverHandleOpen, this, _1)); |
| 66 | server.set_close_handler(bind(&WebSocketTransportFixture::serverHandleClose, this)); |
| 67 | server.set_message_handler(bind(&WebSocketTransportFixture::serverHandleMessage, this, _2)); |
| 68 | server.set_pong_handler(bind(&WebSocketTransportFixture::serverHandlePong, this)); |
| 69 | server.set_pong_timeout_handler(bind(&WebSocketTransportFixture::serverHandlePongTimeout, this)); |
| 70 | server.set_pong_timeout(pongTimeout.count()); |
| 71 | |
| 72 | server.set_reuse_addr(true); |
| 73 | |
| 74 | server.listen(ep); |
| 75 | server.start_accept(); |
| 76 | } |
| 77 | |
| 78 | /** \brief initialize client and connect to server |
| 79 | */ |
| 80 | void |
| 81 | clientConnect(const std::string& uri) |
| 82 | { |
| 83 | client.clear_access_channels(websocketpp::log::alevel::all); |
| 84 | client.clear_error_channels(websocketpp::log::elevel::all); |
| 85 | |
| 86 | client.init_asio(&g_io); |
| 87 | client.set_open_handler(bind(&WebSocketTransportFixture::clientHandleOpen, this, _1)); |
| 88 | client.set_message_handler(bind(&WebSocketTransportFixture::clientHandleMessage, this, _2)); |
| 89 | client.set_ping_handler(bind(&WebSocketTransportFixture::clientHandlePing, this)); |
| 90 | |
| 91 | websocketpp::lib::error_code ec; |
| 92 | auto con = client.get_connection(uri, ec); |
| 93 | BOOST_REQUIRE_EQUAL(ec, websocketpp::lib::error_code()); |
| 94 | |
| 95 | client.connect(con); |
| 96 | } |
| 97 | |
| 98 | /** \brief initialize both server and client, and have each other connected, create Transport |
| 99 | */ |
| 100 | void |
| 101 | initialize(ip::address address, |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 102 | time::milliseconds pingInterval = 10_s, |
| 103 | time::milliseconds pongTimeout = 1_s) |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 104 | { |
| 105 | ip::tcp::endpoint ep(address, 20070); |
| 106 | serverListen(ep, pongTimeout); |
| 107 | clientConnect(FaceUri(ep, "ws").toString()); |
| 108 | |
| 109 | BOOST_REQUIRE_EQUAL(limitedIo.run(2, // serverHandleOpen, clientHandleOpen |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 110 | 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 111 | |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame^] | 112 | face = make_unique<Face>(make_unique<DummyLinkService>(), |
| 113 | make_unique<WebSocketTransport>(serverHdl, std::ref(server), pingInterval)); |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 114 | transport = static_cast<WebSocketTransport*>(face->getTransport()); |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame^] | 115 | serverReceivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets; |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 116 | |
| 117 | BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP); |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | void |
| 122 | serverHandleOpen(websocketpp::connection_hdl hdl) |
| 123 | { |
| 124 | websocketpp::lib::error_code ec; |
| 125 | auto con = server.get_con_from_hdl(hdl, ec); |
| 126 | BOOST_REQUIRE_EQUAL(ec, websocketpp::lib::error_code()); |
| 127 | BOOST_REQUIRE(con); |
| 128 | remoteEp = con->get_socket().remote_endpoint(); |
| 129 | |
| 130 | serverHdl = hdl; |
| 131 | limitedIo.afterOp(); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | serverHandleClose() |
| 136 | { |
| 137 | if (transport == nullptr) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | transport->close(); |
| 142 | limitedIo.afterOp(); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | serverHandleMessage(websocket::Server::message_ptr msg) |
| 147 | { |
| 148 | if (transport == nullptr) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | transport->receiveMessage(msg->get_payload()); |
| 153 | limitedIo.afterOp(); |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | serverHandlePong() |
| 158 | { |
| 159 | if (transport == nullptr) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | transport->handlePong(); |
| 164 | limitedIo.afterOp(); |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | serverHandlePongTimeout() |
| 169 | { |
| 170 | if (transport == nullptr) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | transport->handlePongTimeout(); |
| 175 | limitedIo.afterOp(); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | clientHandleOpen(websocketpp::connection_hdl hdl) |
| 180 | { |
| 181 | clientHdl = hdl; |
| 182 | limitedIo.afterOp(); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | clientHandleMessage(websocket::Client::message_ptr msg) |
| 187 | { |
| 188 | clientReceivedMessages.push_back(msg->get_payload()); |
| 189 | limitedIo.afterOp(); |
| 190 | } |
| 191 | |
| 192 | bool |
| 193 | clientHandlePing() |
| 194 | { |
| 195 | limitedIo.afterOp(); |
| 196 | return clientShouldPong; |
| 197 | } |
| 198 | |
| 199 | protected: |
| 200 | LimitedIo limitedIo; |
| 201 | |
| 202 | websocket::Server server; |
| 203 | websocketpp::connection_hdl serverHdl; |
| 204 | ip::tcp::endpoint remoteEp; |
| 205 | WebSocketTransport* transport; |
| 206 | std::vector<Transport::Packet>* serverReceivedPackets; |
| 207 | |
| 208 | websocket::Client client; |
| 209 | websocketpp::connection_hdl clientHdl; |
| 210 | bool clientShouldPong; |
| 211 | std::vector<std::string> clientReceivedMessages; |
| 212 | |
| 213 | private: |
| 214 | unique_ptr<Face> face; |
| 215 | }; |
| 216 | |
| 217 | } // namespace tests |
| 218 | } // namespace face |
| 219 | } // namespace nfd |
| 220 | |
| 221 | #endif // NFD_TESTS_DAEMON_FACE_WEBSOCKET_TRANSPORT_FIXTURE_HPP |