blob: fa93b6a141b61cba95ced100ad1d2fa5018480b0 [file] [log] [blame]
Steve DiBenedettoef04f272014-06-04 14:28:31 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, 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#include "face/websocket-factory.hpp"
27#include "tests/test-common.hpp"
Wentao Shang93ef6c92014-06-19 11:59:17 -040028#include "tests/limited-io.hpp"
29
30#include <websocketpp/config/asio_no_tls_client.hpp>
31#include <websocketpp/client.hpp>
32
33typedef websocketpp::client<websocketpp::config::asio_client> Client;
Steve DiBenedettoef04f272014-06-04 14:28:31 -060034
35namespace nfd {
36namespace tests {
37
38BOOST_FIXTURE_TEST_SUITE(FaceWebSocket, BaseFixture)
39
40BOOST_AUTO_TEST_CASE(GetChannels)
41{
42 WebSocketFactory factory("19596");
43 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
44
45 std::vector<shared_ptr<const Channel> > expectedChannels;
46
47 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
48 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
49 expectedChannels.push_back(factory.createChannel("::1", "20071"));
50
51 std::list<shared_ptr<const Channel> > channels = factory.getChannels();
52 for (std::list<shared_ptr<const Channel> >::const_iterator i = channels.begin();
53 i != channels.end(); ++i)
54 {
55 std::vector<shared_ptr<const Channel> >::iterator pos =
56 std::find(expectedChannels.begin(), expectedChannels.end(), *i);
57
58 BOOST_REQUIRE(pos != expectedChannels.end());
59 expectedChannels.erase(pos);
60 }
61
62 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
63}
64
Wentao Shang93ef6c92014-06-19 11:59:17 -040065class EndToEndFixture : protected BaseFixture
66{
67public:
68 void
69 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
70 {
71 BOOST_CHECK(!static_cast<bool>(face1));
72 face1 = newFace;
73 face1->onReceiveInterest +=
74 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
75 face1->onReceiveData +=
76 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
77 face1->onFail +=
78 bind(&EndToEndFixture::face1_onFail, this);
79
80 limitedIo.afterOp();
81 }
82
83 void
84 face1_onReceiveInterest(const Interest& interest)
85 {
86 face1_receivedInterests.push_back(interest);
Wentao Shang93ef6c92014-06-19 11:59:17 -040087 limitedIo.afterOp();
88 }
89
90 void
91 face1_onReceiveData(const Data& data)
92 {
93 face1_receivedDatas.push_back(data);
Wentao Shang93ef6c92014-06-19 11:59:17 -040094 limitedIo.afterOp();
95 }
96
97 void
98 face1_onFail()
99 {
100 face1.reset();
101 limitedIo.afterOp();
102 }
103
104 void
105 client1_onOpen(websocketpp::connection_hdl hdl)
106 {
107 handle = hdl;
108 limitedIo.afterOp();
109 }
110
111 void
112 client1_onClose(websocketpp::connection_hdl hdl)
113 {
114 limitedIo.afterOp();
115 }
116
117 void
118 client1_onFail(websocketpp::connection_hdl hdl)
119 {
120 limitedIo.afterOp();
121 }
122
Wentao Shang98733142014-09-17 12:13:57 -0700123 bool
124 client1_onPing(websocketpp::connection_hdl hdl, std::string msg)
125 {
126 limitedIo.afterOp();
127 // Return false to suppress the pong response,
128 // which will cause timeout in the websocket channel
129 return false;
130 }
131
Wentao Shang93ef6c92014-06-19 11:59:17 -0400132 void
133 client1_sendInterest(const Interest& interest)
134 {
135 const Block& payload = interest.wireEncode();
136 client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
137 }
138
139 void
140 client1_sendData(const Data& data)
141 {
142 const Block& payload = data.wireEncode();
143 client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
144 }
145
146 void
147 client1_onMessage(websocketpp::connection_hdl hdl,
148 websocketpp::config::asio_client::message_type::ptr msg)
149 {
150 bool isOk = true;
151 Block element;
152 const std::string& payload = msg->get_payload();
153 isOk = Block::fromBuffer(reinterpret_cast<const uint8_t*>(payload.c_str()),
154 payload.size(), element);
155 if (isOk)
156 {
157 try {
158 if (element.type() == tlv::Interest)
159 {
160 shared_ptr<Interest> i = make_shared<Interest>();
161 i->wireDecode(element);
162 client1_onReceiveInterest(*i);
163 }
164 else if (element.type() == tlv::Data)
165 {
166 shared_ptr<Data> d = make_shared<Data>();
167 d->wireDecode(element);
168 client1_onReceiveData(*d);
169 }
170 }
171 catch (tlv::Error&) {
172 // Do something?
173 }
174 }
175 limitedIo.afterOp();
176 }
177
178 void
179 client1_onReceiveInterest(const Interest& interest)
180 {
181 client1_receivedInterests.push_back(interest);
182 limitedIo.afterOp();
183 }
184
185 void
186 client1_onReceiveData(const Data& data)
187 {
188 client1_receivedDatas.push_back(data);
189 limitedIo.afterOp();
190 }
191
192public:
193 LimitedIo limitedIo;
194
195 shared_ptr<Face> face1;
196 std::vector<Interest> face1_receivedInterests;
197 std::vector<Data> face1_receivedDatas;
198 Client client1;
199 websocketpp::connection_hdl handle;
200 std::vector<Interest> client1_receivedInterests;
201 std::vector<Data> client1_receivedDatas;
Wentao Shang93ef6c92014-06-19 11:59:17 -0400202};
203
204BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
205{
206 WebSocketFactory factory1("9696");
207
208 shared_ptr<WebSocketChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
Wentao Shang98733142014-09-17 12:13:57 -0700209 channel1->setPingInterval(time::milliseconds(3000));
210 channel1->setPongTimeout(time::milliseconds(1000));
Wentao Shang93ef6c92014-06-19 11:59:17 -0400211
212 BOOST_CHECK_EQUAL(channel1->isListening(), false);
213
214 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1));
215
216 BOOST_CHECK_EQUAL(channel1->isListening(), true);
217
Wentao Shang93ef6c92014-06-19 11:59:17 -0400218 // Clear all logging info from websocketpp library
219 client1.clear_access_channels(websocketpp::log::alevel::all);
220
221 client1.init_asio(&getGlobalIoService());
222 client1.set_open_handler(bind(&EndToEndFixture::client1_onOpen, this, _1));
223 client1.set_close_handler(bind(&EndToEndFixture::client1_onClose, this, _1));
224 client1.set_fail_handler(bind(&EndToEndFixture::client1_onFail, this, _1));
225 client1.set_message_handler(bind(&EndToEndFixture::client1_onMessage, this, _1, _2));
Wentao Shang98733142014-09-17 12:13:57 -0700226 client1.set_ping_handler(bind(&EndToEndFixture::client1_onPing, this, _1, _2));
Wentao Shang93ef6c92014-06-19 11:59:17 -0400227
228 websocketpp::lib::error_code ec;
229 Client::connection_ptr con = client1.get_connection("ws://127.0.0.1:20070", ec);
230 client1.connect(con);
231
232 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
233 "WebSocketChannel error: cannot connect or cannot accept connection");
234
Wentao Shang98733142014-09-17 12:13:57 -0700235 BOOST_CHECK_EQUAL(channel1->size(), 1);
236
Wentao Shang93ef6c92014-06-19 11:59:17 -0400237 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "ws://127.0.0.1:20070");
Wentao Shang98733142014-09-17 12:13:57 -0700238 BOOST_CHECK_EQUAL(face1->isOnDemand(), true);
Wentao Shang93ef6c92014-06-19 11:59:17 -0400239
240 //BOOST_CHECK_EQUAL(face1->isLocal(), true);
241
242 //BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), false);
243
244 shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
245 shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
246 shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
247 shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
248
Wentao Shang6990e4c2014-11-05 11:17:35 -0800249 std::string bigName("ndn:/");
250 bigName.append(9000, 'a');
251 shared_ptr<Interest> bigInterest = makeInterest(bigName);
252
Wentao Shang93ef6c92014-06-19 11:59:17 -0400253 client1_sendInterest(*interest1);
254 client1_sendInterest(*interest1);
255 client1_sendInterest(*interest1);
Wentao Shang6990e4c2014-11-05 11:17:35 -0800256 client1_sendInterest(*bigInterest); // This one should be ignored by face1
Wentao Shang93ef6c92014-06-19 11:59:17 -0400257 face1->sendData (*data1);
258 face1->sendInterest (*interest2);
259 client1_sendData (*data2);
260 client1_sendData (*data2);
261 client1_sendData (*data2);
Wentao Shange612e2f2014-08-04 10:24:59 -0400262 size_t nBytesSent = data1->wireEncode().size() + interest2->wireEncode().size();
263 size_t nBytesReceived = interest1->wireEncode().size() * 3 + data2->wireEncode().size() * 3;
Wentao Shang93ef6c92014-06-19 11:59:17 -0400264
265 BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
266 "WebSocketChannel error: cannot send or receive Interest/Data packets");
267
268 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 3);
269 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
270 BOOST_REQUIRE_EQUAL(client1_receivedInterests.size(), 1);
271 BOOST_REQUIRE_EQUAL(client1_receivedDatas .size(), 1);
272
273 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest1->getName());
274 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
275 BOOST_CHECK_EQUAL(client1_receivedInterests[0].getName(), interest2->getName());
276 BOOST_CHECK_EQUAL(client1_receivedDatas [0].getName(), data1->getName());
277
278 const FaceCounters& counters1 = face1->getCounters();
279 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 3);
280 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
281 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 1);
282 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Wentao Shange612e2f2014-08-04 10:24:59 -0400283 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesReceived);
284 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent);
Wentao Shang98733142014-09-17 12:13:57 -0700285
286 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(8));
287 BOOST_CHECK_EQUAL(channel1->size(), 0);
Wentao Shang93ef6c92014-06-19 11:59:17 -0400288}
289
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600290BOOST_AUTO_TEST_SUITE_END()
291
292} // namespace tests
293} // namespace nfd