blob: d3da744b79976c005263103a561a50609d7469b1 [file] [log] [blame]
Steve DiBenedettoef04f272014-06-04 14:28:31 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060010 *
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 Pesavento6ad890a2015-03-09 03:43:17 +010026#include "face/websocket-channel.hpp"
27#include "face/websocket-face.hpp"
Steve DiBenedettoef04f272014-06-04 14:28:31 -060028#include "face/websocket-factory.hpp"
Davide Pesavento6ad890a2015-03-09 03:43:17 +010029
Steve DiBenedettoef04f272014-06-04 14:28:31 -060030#include "tests/test-common.hpp"
Wentao Shang93ef6c92014-06-19 11:59:17 -040031#include "tests/limited-io.hpp"
32
33#include <websocketpp/config/asio_no_tls_client.hpp>
34#include <websocketpp/client.hpp>
35
36typedef websocketpp::client<websocketpp::config::asio_client> Client;
Steve DiBenedettoef04f272014-06-04 14:28:31 -060037
38namespace nfd {
39namespace tests {
40
41BOOST_FIXTURE_TEST_SUITE(FaceWebSocket, BaseFixture)
42
43BOOST_AUTO_TEST_CASE(GetChannels)
44{
45 WebSocketFactory factory("19596");
46 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
47
48 std::vector<shared_ptr<const Channel> > expectedChannels;
49
50 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
51 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
52 expectedChannels.push_back(factory.createChannel("::1", "20071"));
53
54 std::list<shared_ptr<const Channel> > channels = factory.getChannels();
55 for (std::list<shared_ptr<const Channel> >::const_iterator i = channels.begin();
56 i != channels.end(); ++i)
57 {
58 std::vector<shared_ptr<const Channel> >::iterator pos =
59 std::find(expectedChannels.begin(), expectedChannels.end(), *i);
60
61 BOOST_REQUIRE(pos != expectedChannels.end());
62 expectedChannels.erase(pos);
63 }
64
65 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
66}
67
Yukai Tu7c90e6d2015-07-11 12:21:46 +080068BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
69{
70 WebSocketFactory factory("19596");
71
72 BOOST_CHECK_THROW(factory.createFace(FaceUri("ws://127.0.0.1:20070"),
73 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
74 bind([]{}),
75 bind([]{})),
76 ProtocolFactory::Error);
77
78 BOOST_CHECK_THROW(factory.createFace(FaceUri("ws://127.0.0.1:20070"),
79 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
80 bind([]{}),
81 bind([]{})),
82 ProtocolFactory::Error);
83
84 BOOST_CHECK_THROW(factory.createFace(FaceUri("ws://127.0.0.1:20070"),
85 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
86 bind([]{}),
87 bind([]{})),
88 ProtocolFactory::Error);
89}
90
Wentao Shang93ef6c92014-06-19 11:59:17 -040091class EndToEndFixture : protected BaseFixture
92{
93public:
94 void
95 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
96 {
97 BOOST_CHECK(!static_cast<bool>(face1));
98 face1 = newFace;
Junxiao Shic099ddb2014-12-25 20:53:20 -070099 face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1));
100 face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1));
101 face1->onFail.connect(bind(&EndToEndFixture::face1_onFail, this));
Wentao Shang93ef6c92014-06-19 11:59:17 -0400102
103 limitedIo.afterOp();
104 }
105
106 void
107 face1_onReceiveInterest(const Interest& interest)
108 {
109 face1_receivedInterests.push_back(interest);
Wentao Shang93ef6c92014-06-19 11:59:17 -0400110 limitedIo.afterOp();
111 }
112
113 void
114 face1_onReceiveData(const Data& data)
115 {
116 face1_receivedDatas.push_back(data);
Wentao Shang93ef6c92014-06-19 11:59:17 -0400117 limitedIo.afterOp();
118 }
119
120 void
121 face1_onFail()
122 {
123 face1.reset();
124 limitedIo.afterOp();
125 }
126
127 void
128 client1_onOpen(websocketpp::connection_hdl hdl)
129 {
130 handle = hdl;
131 limitedIo.afterOp();
132 }
133
134 void
135 client1_onClose(websocketpp::connection_hdl hdl)
136 {
137 limitedIo.afterOp();
138 }
139
140 void
141 client1_onFail(websocketpp::connection_hdl hdl)
142 {
143 limitedIo.afterOp();
144 }
145
Wentao Shang98733142014-09-17 12:13:57 -0700146 bool
147 client1_onPing(websocketpp::connection_hdl hdl, std::string msg)
148 {
149 limitedIo.afterOp();
150 // Return false to suppress the pong response,
151 // which will cause timeout in the websocket channel
152 return false;
153 }
154
Wentao Shang93ef6c92014-06-19 11:59:17 -0400155 void
156 client1_sendInterest(const Interest& interest)
157 {
158 const Block& payload = interest.wireEncode();
159 client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
160 }
161
162 void
163 client1_sendData(const Data& data)
164 {
165 const Block& payload = data.wireEncode();
166 client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
167 }
168
169 void
170 client1_onMessage(websocketpp::connection_hdl hdl,
171 websocketpp::config::asio_client::message_type::ptr msg)
172 {
Junxiao Shi78926c92015-02-28 22:56:06 -0700173 bool isOk = false;
Wentao Shang93ef6c92014-06-19 11:59:17 -0400174 Block element;
175 const std::string& payload = msg->get_payload();
Junxiao Shi78926c92015-02-28 22:56:06 -0700176 std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(payload.c_str()),
177 payload.size());
Wentao Shang93ef6c92014-06-19 11:59:17 -0400178 if (isOk)
179 {
180 try {
181 if (element.type() == tlv::Interest)
182 {
183 shared_ptr<Interest> i = make_shared<Interest>();
184 i->wireDecode(element);
185 client1_onReceiveInterest(*i);
186 }
187 else if (element.type() == tlv::Data)
188 {
189 shared_ptr<Data> d = make_shared<Data>();
190 d->wireDecode(element);
191 client1_onReceiveData(*d);
192 }
193 }
194 catch (tlv::Error&) {
195 // Do something?
196 }
197 }
198 limitedIo.afterOp();
199 }
200
201 void
202 client1_onReceiveInterest(const Interest& interest)
203 {
204 client1_receivedInterests.push_back(interest);
205 limitedIo.afterOp();
206 }
207
208 void
209 client1_onReceiveData(const Data& data)
210 {
211 client1_receivedDatas.push_back(data);
212 limitedIo.afterOp();
213 }
214
215public:
216 LimitedIo limitedIo;
217
218 shared_ptr<Face> face1;
219 std::vector<Interest> face1_receivedInterests;
220 std::vector<Data> face1_receivedDatas;
221 Client client1;
222 websocketpp::connection_hdl handle;
223 std::vector<Interest> client1_receivedInterests;
224 std::vector<Data> client1_receivedDatas;
Wentao Shang93ef6c92014-06-19 11:59:17 -0400225};
226
227BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
228{
229 WebSocketFactory factory1("9696");
230
231 shared_ptr<WebSocketChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
Wentao Shang98733142014-09-17 12:13:57 -0700232 channel1->setPingInterval(time::milliseconds(3000));
233 channel1->setPongTimeout(time::milliseconds(1000));
Wentao Shang93ef6c92014-06-19 11:59:17 -0400234
235 BOOST_CHECK_EQUAL(channel1->isListening(), false);
236
237 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1));
238
239 BOOST_CHECK_EQUAL(channel1->isListening(), true);
240
Wentao Shang93ef6c92014-06-19 11:59:17 -0400241 // Clear all logging info from websocketpp library
242 client1.clear_access_channels(websocketpp::log::alevel::all);
243
244 client1.init_asio(&getGlobalIoService());
245 client1.set_open_handler(bind(&EndToEndFixture::client1_onOpen, this, _1));
246 client1.set_close_handler(bind(&EndToEndFixture::client1_onClose, this, _1));
247 client1.set_fail_handler(bind(&EndToEndFixture::client1_onFail, this, _1));
248 client1.set_message_handler(bind(&EndToEndFixture::client1_onMessage, this, _1, _2));
Wentao Shang98733142014-09-17 12:13:57 -0700249 client1.set_ping_handler(bind(&EndToEndFixture::client1_onPing, this, _1, _2));
Wentao Shang93ef6c92014-06-19 11:59:17 -0400250
251 websocketpp::lib::error_code ec;
252 Client::connection_ptr con = client1.get_connection("ws://127.0.0.1:20070", ec);
253 client1.connect(con);
254
255 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
256 "WebSocketChannel error: cannot connect or cannot accept connection");
257
Wentao Shang98733142014-09-17 12:13:57 -0700258 BOOST_CHECK_EQUAL(channel1->size(), 1);
259
Davide Pesavento94279412015-02-27 01:29:32 +0100260 BOOST_REQUIRE(static_cast<bool>(face1));
261 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Yukai Tu731f0d72015-07-04 11:14:44 +0800262 BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Davide Pesavento94279412015-02-27 01:29:32 +0100263 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
264 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "ws://127.0.0.1:20070");
Wentao Shang93ef6c92014-06-19 11:59:17 -0400265
266 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
267 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
268 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
269 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
270
Wentao Shang6990e4c2014-11-05 11:17:35 -0800271 std::string bigName("ndn:/");
272 bigName.append(9000, 'a');
273 shared_ptr<Interest> bigInterest = makeInterest(bigName);
274
Wentao Shang93ef6c92014-06-19 11:59:17 -0400275 client1_sendInterest(*interest1);
276 client1_sendInterest(*interest1);
277 client1_sendInterest(*interest1);
Wentao Shang6990e4c2014-11-05 11:17:35 -0800278 client1_sendInterest(*bigInterest); // This one should be ignored by face1
Wentao Shang93ef6c92014-06-19 11:59:17 -0400279 face1->sendData (*data1);
280 face1->sendInterest (*interest2);
281 client1_sendData (*data2);
282 client1_sendData (*data2);
283 client1_sendData (*data2);
Wentao Shange612e2f2014-08-04 10:24:59 -0400284 size_t nBytesSent = data1->wireEncode().size() + interest2->wireEncode().size();
285 size_t nBytesReceived = interest1->wireEncode().size() * 3 + data2->wireEncode().size() * 3;
Wentao Shang93ef6c92014-06-19 11:59:17 -0400286
287 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
288 "WebSocketChannel error: cannot send or receive Interest/Data packets");
289
290 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 3);
291 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
292 BOOST_REQUIRE_EQUAL(client1_receivedInterests.size(), 1);
293 BOOST_REQUIRE_EQUAL(client1_receivedDatas .size(), 1);
294
295 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest1->getName());
296 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
297 BOOST_CHECK_EQUAL(client1_receivedInterests[0].getName(), interest2->getName());
298 BOOST_CHECK_EQUAL(client1_receivedDatas [0].getName(), data1->getName());
299
300 const FaceCounters& counters1 = face1->getCounters();
301 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 3);
302 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
303 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 1);
304 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Wentao Shange612e2f2014-08-04 10:24:59 -0400305 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesReceived);
306 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent);
Wentao Shang98733142014-09-17 12:13:57 -0700307
308 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(8));
309 BOOST_CHECK_EQUAL(channel1->size(), 0);
Wentao Shang93ef6c92014-06-19 11:59:17 -0400310}
311
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600312BOOST_AUTO_TEST_SUITE_END()
313
314} // namespace tests
315} // namespace nfd