blob: 6fd8e700e1fbeb407e08dc599261fe03c9a61cfc [file] [log] [blame]
Davide Pesavento22fba352017-10-17 15:53:51 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2017, Regents of the University of California,
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
32#include "dummy-receive-link-service.hpp"
33#include "tests/limited-io.hpp"
34
35namespace nfd {
36namespace face {
37namespace tests {
38
39using namespace nfd::tests;
40namespace ip = boost::asio::ip;
41
42/** \brief a fixture that accepts a single WebSocket connection from a client
43 */
44class WebSocketTransportFixture : public BaseFixture
45{
46protected:
47 WebSocketTransportFixture()
48 : transport(nullptr)
49 , serverReceivedPackets(nullptr)
50 , clientShouldPong(true)
51 {
52 }
53
54 /** \brief initialize server and start listening
55 */
56 void
57 serverListen(const ip::tcp::endpoint& ep,
58 const time::milliseconds& pongTimeout = time::seconds(1))
59 {
60 server.clear_access_channels(websocketpp::log::alevel::all);
61 server.clear_error_channels(websocketpp::log::elevel::all);
62
63 server.init_asio(&g_io);
64 server.set_open_handler(bind(&WebSocketTransportFixture::serverHandleOpen, this, _1));
65 server.set_close_handler(bind(&WebSocketTransportFixture::serverHandleClose, this));
66 server.set_message_handler(bind(&WebSocketTransportFixture::serverHandleMessage, this, _2));
67 server.set_pong_handler(bind(&WebSocketTransportFixture::serverHandlePong, this));
68 server.set_pong_timeout_handler(bind(&WebSocketTransportFixture::serverHandlePongTimeout, this));
69 server.set_pong_timeout(pongTimeout.count());
70
71 server.set_reuse_addr(true);
72
73 server.listen(ep);
74 server.start_accept();
75 }
76
77 /** \brief initialize client and connect to server
78 */
79 void
80 clientConnect(const std::string& uri)
81 {
82 client.clear_access_channels(websocketpp::log::alevel::all);
83 client.clear_error_channels(websocketpp::log::elevel::all);
84
85 client.init_asio(&g_io);
86 client.set_open_handler(bind(&WebSocketTransportFixture::clientHandleOpen, this, _1));
87 client.set_message_handler(bind(&WebSocketTransportFixture::clientHandleMessage, this, _2));
88 client.set_ping_handler(bind(&WebSocketTransportFixture::clientHandlePing, this));
89
90 websocketpp::lib::error_code ec;
91 auto con = client.get_connection(uri, ec);
92 BOOST_REQUIRE_EQUAL(ec, websocketpp::lib::error_code());
93
94 client.connect(con);
95 }
96
97 /** \brief initialize both server and client, and have each other connected, create Transport
98 */
99 void
100 initialize(ip::address address,
101 time::milliseconds pingInterval = time::seconds(10),
102 time::milliseconds pongTimeout = time::seconds(1))
103 {
104 ip::tcp::endpoint ep(address, 20070);
105 serverListen(ep, pongTimeout);
106 clientConnect(FaceUri(ep, "ws").toString());
107
108 BOOST_REQUIRE_EQUAL(limitedIo.run(2, // serverHandleOpen, clientHandleOpen
109 time::seconds(1)), LimitedIo::EXCEED_OPS);
110
111 face = make_unique<Face>(
112 make_unique<DummyReceiveLinkService>(),
113 make_unique<WebSocketTransport>(serverHdl, ref(server), pingInterval));
114 transport = static_cast<WebSocketTransport*>(face->getTransport());
115 serverReceivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
116
117 BOOST_REQUIRE_EQUAL(transport->getState(), TransportState::UP);
118 }
119
120private:
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
199protected:
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
213private:
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