blob: 8b8ee427c68a5a383d54534d7f7e6f3426fd8798 [file] [log] [blame]
Davide Pesavento22fba352017-10-17 15:53:51 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, Regents of the University of California,
Davide Pesavento22fba352017-10-17 15:53:51 -04004 * 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 Pesaventocf7db2f2019-03-24 23:17:28 -040032#include "tests/test-common.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060033#include "tests/daemon/limited-io.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040034#include "dummy-receive-link-service.hpp"
Davide Pesavento22fba352017-10-17 15:53:51 -040035
36namespace nfd {
37namespace face {
38namespace tests {
39
40using namespace nfd::tests;
41namespace ip = boost::asio::ip;
42
43/** \brief a fixture that accepts a single WebSocket connection from a client
44 */
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040045class WebSocketTransportFixture : public GlobalIoFixture
Davide Pesavento22fba352017-10-17 15:53:51 -040046{
47protected:
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,
59 const time::milliseconds& pongTimeout = time::seconds(1))
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,
102 time::milliseconds pingInterval = time::seconds(10),
103 time::milliseconds pongTimeout = time::seconds(1))
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
110 time::seconds(1)), LimitedIo::EXCEED_OPS);
111
112 face = make_unique<Face>(
113 make_unique<DummyReceiveLinkService>(),
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400114 make_unique<WebSocketTransport>(serverHdl, std::ref(server), pingInterval));
Davide Pesavento22fba352017-10-17 15:53:51 -0400115 transport = static_cast<WebSocketTransport*>(face->getTransport());
116 serverReceivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
117
118 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
119 }
120
121private:
122 void
123 serverHandleOpen(websocketpp::connection_hdl hdl)
124 {
125 websocketpp::lib::error_code ec;
126 auto con = server.get_con_from_hdl(hdl, ec);
127 BOOST_REQUIRE_EQUAL(ec, websocketpp::lib::error_code());
128 BOOST_REQUIRE(con);
129 remoteEp = con->get_socket().remote_endpoint();
130
131 serverHdl = hdl;
132 limitedIo.afterOp();
133 }
134
135 void
136 serverHandleClose()
137 {
138 if (transport == nullptr) {
139 return;
140 }
141
142 transport->close();
143 limitedIo.afterOp();
144 }
145
146 void
147 serverHandleMessage(websocket::Server::message_ptr msg)
148 {
149 if (transport == nullptr) {
150 return;
151 }
152
153 transport->receiveMessage(msg->get_payload());
154 limitedIo.afterOp();
155 }
156
157 void
158 serverHandlePong()
159 {
160 if (transport == nullptr) {
161 return;
162 }
163
164 transport->handlePong();
165 limitedIo.afterOp();
166 }
167
168 void
169 serverHandlePongTimeout()
170 {
171 if (transport == nullptr) {
172 return;
173 }
174
175 transport->handlePongTimeout();
176 limitedIo.afterOp();
177 }
178
179 void
180 clientHandleOpen(websocketpp::connection_hdl hdl)
181 {
182 clientHdl = hdl;
183 limitedIo.afterOp();
184 }
185
186 void
187 clientHandleMessage(websocket::Client::message_ptr msg)
188 {
189 clientReceivedMessages.push_back(msg->get_payload());
190 limitedIo.afterOp();
191 }
192
193 bool
194 clientHandlePing()
195 {
196 limitedIo.afterOp();
197 return clientShouldPong;
198 }
199
200protected:
201 LimitedIo limitedIo;
202
203 websocket::Server server;
204 websocketpp::connection_hdl serverHdl;
205 ip::tcp::endpoint remoteEp;
206 WebSocketTransport* transport;
207 std::vector<Transport::Packet>* serverReceivedPackets;
208
209 websocket::Client client;
210 websocketpp::connection_hdl clientHdl;
211 bool clientShouldPong;
212 std::vector<std::string> clientReceivedMessages;
213
214private:
215 unique_ptr<Face> face;
216};
217
218} // namespace tests
219} // namespace face
220} // namespace nfd
221
222#endif // NFD_TESTS_DAEMON_FACE_WEBSOCKET_TRANSPORT_FIXTURE_HPP