blob: 87ee8f0603198b57da0ebe19cb9645f3be1ce678 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedettoef04f272014-06-04 14:28:31 -06003 * 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
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
26#include "face/udp-factory.hpp"
Junxiao Shi7e2413b2014-03-02 11:15:09 -070027
28#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070029#include "tests/limited-io.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010030
31namespace nfd {
32namespace tests {
33
34BOOST_FIXTURE_TEST_SUITE(FaceUdp, BaseFixture)
35
Steve DiBenedettoef04f272014-06-04 14:28:31 -060036BOOST_AUTO_TEST_CASE(GetChannels)
37{
38 UdpFactory factory;
39 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
40
41 std::vector<shared_ptr<const Channel> > expectedChannels;
42
43 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
44 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
45 expectedChannels.push_back(factory.createChannel("::1", "20071"));
46
47 std::list<shared_ptr<const Channel> > channels = factory.getChannels();
48 for (std::list<shared_ptr<const Channel> >::const_iterator i = channels.begin();
49 i != channels.end(); ++i)
50 {
51 std::vector<shared_ptr<const Channel> >::iterator pos =
52 std::find(expectedChannels.begin(), expectedChannels.end(), *i);
53
54 BOOST_REQUIRE(pos != expectedChannels.end());
55 expectedChannels.erase(pos);
56 }
57
58 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
59}
60
Giulio Grassi624f6c62014-02-18 19:42:14 +010061class FactoryErrorCheck : protected BaseFixture
62{
63public:
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070064 bool
65 isTheSameMulticastEndpoint(const UdpFactory::Error& e) {
Giulio Grassi624f6c62014-02-18 19:42:14 +010066 return strcmp(e.what(),
67 "Cannot create the requested UDP unicast channel, local "
68 "endpoint is already allocated for a UDP multicast face") == 0;
69 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070070
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070071 bool
72 isNotMulticastAddress(const UdpFactory::Error& e) {
Giulio Grassi624f6c62014-02-18 19:42:14 +010073 return strcmp(e.what(),
74 "Cannot create the requested UDP multicast face, "
75 "the multicast group given as input is not a multicast address") == 0;
76 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070077
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070078 bool
79 isTheSameUnicastEndpoint(const UdpFactory::Error& e) {
Giulio Grassi624f6c62014-02-18 19:42:14 +010080 return strcmp(e.what(),
81 "Cannot create the requested UDP multicast face, local "
82 "endpoint is already allocated for a UDP unicast channel") == 0;
83 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070084
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070085 bool
86 isLocalEndpointOnDifferentGroup(const UdpFactory::Error& e) {
Giulio Grassi624f6c62014-02-18 19:42:14 +010087 return strcmp(e.what(),
88 "Cannot create the requested UDP multicast face, local "
89 "endpoint is already allocated for a UDP multicast face "
90 "on a different multicast group") == 0;
91 }
92};
Junxiao Shi7e2413b2014-03-02 11:15:09 -070093
Giulio Grassi624f6c62014-02-18 19:42:14 +010094BOOST_FIXTURE_TEST_CASE(ChannelMapUdp, FactoryErrorCheck)
95{
96 using boost::asio::ip::udp;
Junxiao Shi7e2413b2014-03-02 11:15:09 -070097
Giulio Grassi624f6c62014-02-18 19:42:14 +010098 UdpFactory factory = UdpFactory();
Junxiao Shi7e2413b2014-03-02 11:15:09 -070099
Giulio Grassi624f6c62014-02-18 19:42:14 +0100100 //to instantiate multicast face on a specific ip address, change interfaceIp
101 std::string interfaceIp = "0.0.0.0";
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700102
Giulio Grassi624f6c62014-02-18 19:42:14 +0100103 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
104 shared_ptr<UdpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
105 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700106 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700107
Giulio Grassi624f6c62014-02-18 19:42:14 +0100108 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
109 BOOST_CHECK_NE(channel1, channel2);
110
111 shared_ptr<UdpChannel> channel3 = factory.createChannel(interfaceIp, "20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700112
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700113 shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20071");
114 BOOST_CHECK_NE(channel2, channel4);
115 BOOST_CHECK_EQUAL(channel4->getUri().toString(), "udp6://[::1]:20071");
116
Giulio Grassi624f6c62014-02-18 19:42:14 +0100117 //same endpoint of a unicast channel
118 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
119 "224.0.0.1",
120 "20070"),
121 UdpFactory::Error,
122 isTheSameUnicastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700123
124
Giulio Grassi624f6c62014-02-18 19:42:14 +0100125 shared_ptr<MulticastUdpFace> multicastFace1 = factory.createMulticastFace(interfaceIp,
126 "224.0.0.1",
127 "20072");
128 shared_ptr<MulticastUdpFace> multicastFace1a = factory.createMulticastFace(interfaceIp,
129 "224.0.0.1",
130 "20072");
131 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700132
133
Giulio Grassi624f6c62014-02-18 19:42:14 +0100134 //same endpoint of a multicast face
135 BOOST_CHECK_EXCEPTION(factory.createChannel(interfaceIp, "20072"),
136 UdpFactory::Error,
137 isTheSameMulticastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700138
Giulio Grassi624f6c62014-02-18 19:42:14 +0100139 //same multicast endpoint, different group
140 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
141 "224.0.0.42",
142 "20072"),
143 UdpFactory::Error,
144 isLocalEndpointOnDifferentGroup);
145
146 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
147 "192.168.10.15",
148 "20025"),
149 UdpFactory::Error,
150 isNotMulticastAddress);
151
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700152
Giulio Grassi624f6c62014-02-18 19:42:14 +0100153// //Test commented because it required to be run in a machine that can resolve ipv6 query
154// shared_ptr<UdpChannel> channel1v6 = factory.createChannel(//"::1",
155// "fe80::5e96:9dff:fe7d:9c8d%en1",
156// //"fe80::aa54:b2ff:fe08:27b8%wlan0",
157// "20070");
158//
159// //the creation of multicastFace2 works properly. It has been disable because it needs an IP address of
160// //an available network interface (different from the first one used)
161// shared_ptr<MulticastUdpFace> multicastFace2 = factory.createMulticastFace("192.168.1.17",
162// "224.0.0.1",
163// "20073");
164// BOOST_CHECK_NE(multicastFace1, multicastFace2);
165//
166//
167// //ipv6 - work in progress
168// shared_ptr<MulticastUdpFace> multicastFace3 = factory.createMulticastFace("fe80::5e96:9dff:fe7d:9c8d%en1",
169// "FF01:0:0:0:0:0:0:2",
170// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700171//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100172// shared_ptr<MulticastUdpFace> multicastFace4 = factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
173// "FF01:0:0:0:0:0:0:2",
174// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700175//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100176// BOOST_CHECK_EQUAL(multicastFace3, multicastFace4);
177//
178// shared_ptr<MulticastUdpFace> multicastFace5 = factory.createMulticastFace("::1",
179// "FF01:0:0:0:0:0:0:2",
180// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700181//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100182// BOOST_CHECK_NE(multicastFace3, multicastFace5);
183//
184// //same local ipv6 endpoint for a different multicast group
185// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
186// "FE01:0:0:0:0:0:0:2",
187// "20073"),
188// UdpFactory::Error);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700189//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100190// //same local ipv6 (expect for th port number) endpoint for a different multicast group
191// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
192// "FE01:0:0:0:0:0:0:2",
193// "20075"),
194// UdpFactory::Error);
195//
196// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
197// "FE12:0:0:0:0:0:0:2",
198// "20075"),
199// UdpFactory::Error);
200//
201// //not a multicast ipv6
202// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
203// "A112:0:0:0:0:0:0:2",
204// "20075"),
205// UdpFactory::Error);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100206}
207
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700208class FaceCreateFixture : protected BaseFixture
209{
210public:
211 void
212 ignore()
213 {
214 }
215
216 void
217 checkError(const std::string& errorActual, const std::string& errorExpected)
218 {
219 BOOST_CHECK_EQUAL(errorActual, errorExpected);
220 }
221
222 void
223 failIfError(const std::string& errorActual)
224 {
225 BOOST_FAIL("No error expected, but got: [" << errorActual << "]");
226 }
227};
228
229BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture)
230{
231 UdpFactory factory = UdpFactory();
232
233 factory.createFace(FaceUri("udp4://127.0.0.1"),
234 bind(&FaceCreateFixture::ignore, this),
235 bind(&FaceCreateFixture::failIfError, this, _1));
236
237 factory.createFace(FaceUri("udp4://127.0.0.1/"),
238 bind(&FaceCreateFixture::ignore, this),
239 bind(&FaceCreateFixture::failIfError, this, _1));
240
241 factory.createFace(FaceUri("udp4://127.0.0.1/path"),
242 bind(&FaceCreateFixture::ignore, this),
243 bind(&FaceCreateFixture::checkError, this, _1, "Invalid URI"));
244
245}
246
Giulio Grassi624f6c62014-02-18 19:42:14 +0100247class EndToEndFixture : protected BaseFixture
248{
249public:
250 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700251 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100252 {
Junxiao Shi79494162014-04-02 18:25:11 -0700253 BOOST_CHECK(!static_cast<bool>(face1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100254 channel1_onFaceCreatedNoCheck(newFace);
255 }
Junxiao Shi79494162014-04-02 18:25:11 -0700256
Giulio Grassi69871f02014-03-09 16:14:44 +0100257 void
258 channel1_onFaceCreatedNoCheck(const shared_ptr<Face>& newFace)
259 {
Junxiao Shi79494162014-04-02 18:25:11 -0700260 face1 = newFace;
261 face1->onReceiveInterest +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100262 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700263 face1->onReceiveData +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100264 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700265 face1->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100266 bind(&EndToEndFixture::face1_onFail, this);
267 BOOST_CHECK_MESSAGE(true, "channel 1 face created");
268
Junxiao Shi79494162014-04-02 18:25:11 -0700269 faces.push_back(face1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100270
Junxiao Shi79494162014-04-02 18:25:11 -0700271 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100272 }
273
274 void
275 channel1_onConnectFailed(const std::string& reason)
276 {
277 BOOST_CHECK_MESSAGE(false, reason);
278
Junxiao Shi79494162014-04-02 18:25:11 -0700279 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100280 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700281
Giulio Grassi624f6c62014-02-18 19:42:14 +0100282 void
283 face1_onReceiveInterest(const Interest& interest)
284 {
Junxiao Shi79494162014-04-02 18:25:11 -0700285 face1_receivedInterests.push_back(interest);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100286
Junxiao Shi79494162014-04-02 18:25:11 -0700287 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100288 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700289
Giulio Grassi624f6c62014-02-18 19:42:14 +0100290 void
291 face1_onReceiveData(const Data& data)
292 {
Junxiao Shi79494162014-04-02 18:25:11 -0700293 face1_receivedDatas.push_back(data);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100294
Junxiao Shi79494162014-04-02 18:25:11 -0700295 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100296 }
297
298 void
299 face1_onFail()
300 {
Junxiao Shi79494162014-04-02 18:25:11 -0700301 face1.reset();
302 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100303 }
304
305 void
306 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
307 {
Junxiao Shi79494162014-04-02 18:25:11 -0700308 BOOST_CHECK(!static_cast<bool>(face2));
309 face2 = newFace;
310 face2->onReceiveInterest +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100311 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700312 face2->onReceiveData +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100313 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700314 face2->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100315 bind(&EndToEndFixture::face2_onFail, this);
316
Junxiao Shi79494162014-04-02 18:25:11 -0700317 faces.push_back(face2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100318
319 BOOST_CHECK_MESSAGE(true, "channel 2 face created");
Junxiao Shi79494162014-04-02 18:25:11 -0700320 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100321 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700322
Giulio Grassi624f6c62014-02-18 19:42:14 +0100323 void
324 channel2_onConnectFailed(const std::string& reason)
325 {
326 BOOST_CHECK_MESSAGE(false, reason);
327
Junxiao Shi79494162014-04-02 18:25:11 -0700328 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100329 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700330
Giulio Grassi624f6c62014-02-18 19:42:14 +0100331 void
332 face2_onReceiveInterest(const Interest& interest)
333 {
Junxiao Shi79494162014-04-02 18:25:11 -0700334 face2_receivedInterests.push_back(interest);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100335
Junxiao Shi79494162014-04-02 18:25:11 -0700336 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100337 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700338
Giulio Grassi624f6c62014-02-18 19:42:14 +0100339 void
340 face2_onReceiveData(const Data& data)
341 {
Junxiao Shi79494162014-04-02 18:25:11 -0700342 face2_receivedDatas.push_back(data);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100343
Junxiao Shi79494162014-04-02 18:25:11 -0700344 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100345 }
346
347 void
348 face2_onFail()
349 {
Junxiao Shi79494162014-04-02 18:25:11 -0700350 face2.reset();
351 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100352 }
353
354 void
355 channel3_onFaceCreated(const shared_ptr<Face>& newFace)
356 {
Junxiao Shi79494162014-04-02 18:25:11 -0700357 BOOST_CHECK(!static_cast<bool>(face1));
358 face3 = newFace;
359 faces.push_back(newFace);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700360
Junxiao Shi79494162014-04-02 18:25:11 -0700361 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100362 }
363
Giulio Grassi624f6c62014-02-18 19:42:14 +0100364 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700365 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100366 {
Junxiao Shi79494162014-04-02 18:25:11 -0700367 faces.push_back(newFace);
368 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100369 }
370
371 void
372 channel_onConnectFailed(const std::string& reason)
373 {
374 BOOST_CHECK_MESSAGE(false, reason);
375
Junxiao Shi79494162014-04-02 18:25:11 -0700376 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100377 }
378
379 void
380 channel_onConnectFailedOk(const std::string& reason)
381 {
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200382 // it's ok, it was supposed to fail
Junxiao Shi79494162014-04-02 18:25:11 -0700383 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100384 }
385
386 void
387 checkFaceList(size_t shouldBe)
388 {
Junxiao Shi79494162014-04-02 18:25:11 -0700389 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100390 }
Giulio Grassi624f6c62014-02-18 19:42:14 +0100391
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200392 void
393 connect(const shared_ptr<UdpChannel>& channel,
394 const std::string& remoteHost,
395 const std::string& remotePort)
396 {
397 channel->connect(remoteHost, remotePort,
398 bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
399 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
400 }
401
Giulio Grassi624f6c62014-02-18 19:42:14 +0100402public:
Junxiao Shi79494162014-04-02 18:25:11 -0700403 LimitedIo limitedIo;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100404
Junxiao Shi79494162014-04-02 18:25:11 -0700405 shared_ptr<Face> face1;
406 std::vector<Interest> face1_receivedInterests;
407 std::vector<Data> face1_receivedDatas;
408 shared_ptr<Face> face2;
409 std::vector<Interest> face2_receivedInterests;
410 std::vector<Data> face2_receivedDatas;
411 shared_ptr<Face> face3;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100412
Junxiao Shi79494162014-04-02 18:25:11 -0700413 std::list< shared_ptr<Face> > faces;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100414};
415
416
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000417BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100418{
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700419 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100420
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000421 factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700422
Giulio Grassi624f6c62014-02-18 19:42:14 +0100423 factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
424 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
425 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700426
427
Junxiao Shi79494162014-04-02 18:25:11 -0700428 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700429 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100430
Junxiao Shi79494162014-04-02 18:25:11 -0700431 BOOST_REQUIRE(static_cast<bool>(face2));
432 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp4://127.0.0.1:20070");
433 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp4://127.0.0.1:20071");
434 BOOST_CHECK_EQUAL(face2->isLocal(), false);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700435 BOOST_CHECK_EQUAL(face2->getCounters().getNOutBytes(), 0);
436 BOOST_CHECK_EQUAL(face2->getCounters().getNInBytes(), 0);
Junxiao Shi79494162014-04-02 18:25:11 -0700437 // face1 is not created yet
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700438
Giulio Grassi624f6c62014-02-18 19:42:14 +0100439 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
440 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
441 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700442
Giulio Grassi624f6c62014-02-18 19:42:14 +0100443 Interest interest1("ndn:/TpnzGvW9R");
444 Data data1 ("ndn:/KfczhUqVix");
445 data1.setContent(0, 0);
446 Interest interest2("ndn:/QWiIMfj5sL");
447 Data data2 ("ndn:/XNBV796f");
448 data2.setContent(0, 0);
449 Interest interest3("ndn:/QWiIhjgkj5sL");
450 Data data3 ("ndn:/XNBV794f");
451 data3.setContent(0, 0);
452
453
454 ndn::SignatureSha256WithRsa fakeSignature;
455 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
456 reinterpret_cast<const uint8_t*>(0),
457 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700458
Giulio Grassi624f6c62014-02-18 19:42:14 +0100459 // set fake signature on data1 and data2
460 data1.setSignature(fakeSignature);
461 data2.setSignature(fakeSignature);
462 data3.setSignature(fakeSignature);
463
Junxiao Shi79494162014-04-02 18:25:11 -0700464 face2->sendInterest(interest2);
465 face2->sendData (data2 );
466 face2->sendData (data2 );
467 face2->sendData (data2 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700468 size_t nBytesSent2 = interest2.wireEncode().size() + data2.wireEncode().size() * 3;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100469
Junxiao Shi79494162014-04-02 18:25:11 -0700470 BOOST_CHECK_MESSAGE(limitedIo.run(5,//4 send + 1 listen return
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000471 time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700472 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100473
Junxiao Shi79494162014-04-02 18:25:11 -0700474 BOOST_REQUIRE(static_cast<bool>(face1));
475 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp4://127.0.0.1:20071");
476 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp4://127.0.0.1:20070");
477 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700478
Junxiao Shi79494162014-04-02 18:25:11 -0700479 face1->sendInterest(interest1);
480 face1->sendInterest(interest1);
481 face1->sendInterest(interest1);
482 face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700483
Junxiao Shi79494162014-04-02 18:25:11 -0700484 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700485 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100486
Junxiao Shi79494162014-04-02 18:25:11 -0700487 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
488 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
489 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
490 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700491
Junxiao Shi79494162014-04-02 18:25:11 -0700492 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
493 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
494 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
495 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700496
497
498
Giulio Grassi624f6c62014-02-18 19:42:14 +0100499 //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700500
Junxiao Shi79494162014-04-02 18:25:11 -0700501 face2->sendData (data3 );
502 face2->sendInterest(interest3);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700503 nBytesSent2 += data3.wireEncode().size() + interest3.wireEncode().size();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100504
Junxiao Shi79494162014-04-02 18:25:11 -0700505 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700506 "UdpChannel error: cannot send or receive Interest/Data packets");
507
Junxiao Shi79494162014-04-02 18:25:11 -0700508 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
509 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700510
Junxiao Shi79494162014-04-02 18:25:11 -0700511 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
512 BOOST_CHECK_EQUAL(face1_receivedDatas [3].getName(), data3.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000513
Junxiao Shi79494162014-04-02 18:25:11 -0700514 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700515 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 2);
516 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 4);
517 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
518 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700519 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesSent2);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000520
Junxiao Shi79494162014-04-02 18:25:11 -0700521 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700522 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
523 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
524 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 2);
525 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 4);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700526 BOOST_CHECK_EQUAL(counters2.getNOutBytes(), nBytesSent2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000527}
Giulio Grassi624f6c62014-02-18 19:42:14 +0100528
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000529BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
530{
531 UdpFactory factory;
532
533 factory.createChannel("::1", "20071");
534
535 factory.createFace(FaceUri("udp://[::1]:20070"),
536 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
537 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
538
539
Junxiao Shi79494162014-04-02 18:25:11 -0700540 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000541 "UdpChannel error: cannot connect or cannot accept connection");
542
Junxiao Shi79494162014-04-02 18:25:11 -0700543 BOOST_REQUIRE(static_cast<bool>(face2));
544 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp6://[::1]:20070");
545 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp6://[::1]:20071");
546 BOOST_CHECK_EQUAL(face2->isLocal(), false);
547 // face1 is not created yet
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000548
549 shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
550 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
551 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
552
553 Interest interest1("ndn:/TpnzGvW9R");
554 Data data1 ("ndn:/KfczhUqVix");
555 data1.setContent(0, 0);
556 Interest interest2("ndn:/QWiIMfj5sL");
557 Data data2 ("ndn:/XNBV796f");
558 data2.setContent(0, 0);
559 Interest interest3("ndn:/QWiIhjgkj5sL");
560 Data data3 ("ndn:/XNBV794f");
561 data3.setContent(0, 0);
562
563
564 ndn::SignatureSha256WithRsa fakeSignature;
565 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
566 reinterpret_cast<const uint8_t*>(0),
567 0));
568
569 // set fake signature on data1 and data2
570 data1.setSignature(fakeSignature);
571 data2.setSignature(fakeSignature);
572 data3.setSignature(fakeSignature);
573
Junxiao Shi79494162014-04-02 18:25:11 -0700574 face2->sendInterest(interest2);
575 face2->sendData (data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000576
Junxiao Shi79494162014-04-02 18:25:11 -0700577 BOOST_CHECK_MESSAGE(limitedIo.run(3,//2 send + 1 listen return
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000578 time::seconds(1)) == LimitedIo::EXCEED_OPS,
579 "UdpChannel error: cannot send or receive Interest/Data packets");
580
Junxiao Shi79494162014-04-02 18:25:11 -0700581 BOOST_REQUIRE(static_cast<bool>(face1));
582 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp6://[::1]:20071");
583 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp6://[::1]:20070");
584 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000585
Junxiao Shi79494162014-04-02 18:25:11 -0700586 face1->sendInterest(interest1);
587 face1->sendData (data1 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000588
Junxiao Shi79494162014-04-02 18:25:11 -0700589 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000590 "UdpChannel error: cannot send or receive Interest/Data packets");
591
592
Junxiao Shi79494162014-04-02 18:25:11 -0700593 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
594 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
595 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
596 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000597
Junxiao Shi79494162014-04-02 18:25:11 -0700598 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
599 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
600 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
601 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000602
603
604
605 //checking if the connection accepting mechanism works properly.
606
Junxiao Shi79494162014-04-02 18:25:11 -0700607 face2->sendData (data3 );
608 face2->sendInterest(interest3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000609
Junxiao Shi79494162014-04-02 18:25:11 -0700610 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000611 "UdpChannel error: cannot send or receive Interest/Data packets");
612
Junxiao Shi79494162014-04-02 18:25:11 -0700613 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
614 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000615
Junxiao Shi79494162014-04-02 18:25:11 -0700616 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
617 BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Giulio Grassi624f6c62014-02-18 19:42:14 +0100618}
619
620BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
621{
622 Interest interest1("ndn:/TpnzGvW9R");
623 Interest interest2("ndn:/QWiIMfj5sL");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100624
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700625 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100626
Giulio Grassi624f6c62014-02-18 19:42:14 +0100627 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
628 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700629
Giulio Grassi624f6c62014-02-18 19:42:14 +0100630 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
631 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700632
Giulio Grassi624f6c62014-02-18 19:42:14 +0100633 channel2->connect("127.0.0.1", "20070",
634 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
635 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700636
637
Junxiao Shi79494162014-04-02 18:25:11 -0700638 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700639 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100640
Junxiao Shi79494162014-04-02 18:25:11 -0700641 BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700642
Giulio Grassi624f6c62014-02-18 19:42:14 +0100643 shared_ptr<UdpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
644 channel3->connect("127.0.0.1", "20070",
645 bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
646 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700647
Giulio Grassi624f6c62014-02-18 19:42:14 +0100648 shared_ptr<UdpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
649
650 BOOST_CHECK_NE(channel3, channel4);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100651
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700652 scheduler::schedule(time::milliseconds(500),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200653 bind(&EndToEndFixture::connect, this, channel4, "127.0.0.1", "20070"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700654
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700655 scheduler::schedule(time::milliseconds(400), bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700656
Junxiao Shi79494162014-04-02 18:25:11 -0700657 BOOST_CHECK_MESSAGE(limitedIo.run(2,// 2 connects
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700658 time::seconds(4)) == LimitedIo::EXCEED_OPS,
659 "UdpChannel error: cannot connect or cannot accept multiple connections");
660
Junxiao Shi79494162014-04-02 18:25:11 -0700661 BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700662
663
Junxiao Shi79494162014-04-02 18:25:11 -0700664 face2->sendInterest(interest1);
665 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700666 "UdpChannel error: cannot send or receive Interest/Data packets");
667
Junxiao Shi79494162014-04-02 18:25:11 -0700668 BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700669
Junxiao Shi79494162014-04-02 18:25:11 -0700670 face3->sendInterest(interest2);
671 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700672 "UdpChannel error: cannot send or receive Interest/Data packets");
673
Junxiao Shi79494162014-04-02 18:25:11 -0700674 //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
675 BOOST_CHECK_EQUAL(faces.size(), 5);
Davide Pesavento126249b2014-03-13 02:42:21 +0100676 BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
677 bind(&EndToEndFixture::channel_onConnectFailedOk, this, _1)),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100678 UdpChannel::Error);
679}
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700680
Giulio Grassi624f6c62014-02-18 19:42:14 +0100681//Test commented because it required to be run in a machine that can resolve ipv6 query
682//BOOST_FIXTURE_TEST_CASE(EndToEndIpv6, EndToEndFixture)
683//{
684// UdpFactory factory = UdpFactory();
685// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700686//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100687// EventId abortEvent =
688// scheduler.scheduleEvent(time::seconds(1),
689// bind(&EndToEndFixture::abortTestCase, this,
690// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700691//
Junxiao Shi79494162014-04-02 18:25:11 -0700692// limitedIoRemaining = 1;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700693//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100694// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
695// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700696//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100697// channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
698// bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700699//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100700// channel2->connect("::1", "20070",
701// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
702// bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
703// g_io.run();
704// g_io.reset();
705// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700706//
Junxiao Shi79494162014-04-02 18:25:11 -0700707// BOOST_REQUIRE(static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700708//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100709// abortEvent =
710// scheduler.scheduleEvent(time::seconds(1),
711// bind(&EndToEndFixture::abortTestCase, this,
712// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700713//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100714// Interest interest1("ndn:/TpnzGvW9R");
715// Data data1 ("ndn:/KfczhUqVix");
716// data1.setContent(0, 0);
717// Interest interest2("ndn:/QWiIMfj5sL");
718// Data data2 ("ndn:/XNBV796f");
719// data2.setContent(0, 0);
720// Interest interest3("ndn:/QWiIhjgkj5sL");
721// Data data3 ("ndn:/XNBV794f");
722// data3.setContent(0, 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700723//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100724// ndn::SignatureSha256WithRsa fakeSignature;
725// fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700726//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100727// // set fake signature on data1 and data2
728// data1.setSignature(fakeSignature);
729// data2.setSignature(fakeSignature);
730// data3.setSignature(fakeSignature);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700731//
Junxiao Shi79494162014-04-02 18:25:11 -0700732// face2->sendInterest(interest2);
733// face2->sendData (data2 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700734//
Junxiao Shi79494162014-04-02 18:25:11 -0700735// limitedIoRemaining = 3; //2 send + 1 listen return
Giulio Grassi624f6c62014-02-18 19:42:14 +0100736// g_io.run();
737// g_io.reset();
738// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700739//
Junxiao Shi79494162014-04-02 18:25:11 -0700740// BOOST_REQUIRE(static_cast<bool>(face1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700741//
Junxiao Shi79494162014-04-02 18:25:11 -0700742// face1->sendInterest(interest1);
743// face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700744//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100745// abortEvent =
746// scheduler.scheduleEvent(time::seconds(1),
747// bind(&EndToEndFixture::abortTestCase, this,
748// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700749// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100750// g_io.run();
751// g_io.reset();
752// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700753//
Junxiao Shi79494162014-04-02 18:25:11 -0700754// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
755// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
756// BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
757// BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700758//
Junxiao Shi79494162014-04-02 18:25:11 -0700759// BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
760// BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
761// BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
762// BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700763//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100764// //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700765//
Junxiao Shi79494162014-04-02 18:25:11 -0700766// face2->sendData (data3 );
767// face2->sendInterest(interest3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700768//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100769// abortEvent =
770// scheduler.scheduleEvent(time::seconds(1),
771// bind(&EndToEndFixture::abortTestCase, this,
772// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700773// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100774// g_io.run();
775// g_io.reset();
776// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700777//
Junxiao Shi79494162014-04-02 18:25:11 -0700778// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
779// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700780//
Junxiao Shi79494162014-04-02 18:25:11 -0700781// BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
782// BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700783//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100784//}
785
786
787//Test commented because it required to be run in a machine that can resolve ipv6 query
788//BOOST_FIXTURE_TEST_CASE(MultipleAcceptsIpv6, EndToEndFixture)
789//{
790// Interest interest1("ndn:/TpnzGvW9R");
791// Interest interest2("ndn:/QWiIMfj5sL");
792// Interest interest3("ndn:/QWiIhjgkj5sL");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700793//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100794// UdpFactory factory = UdpFactory();
795// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700796//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100797// EventId abortEvent =
798// scheduler.scheduleEvent(time::seconds(4),
799// bind(&EndToEndFixture::abortTestCase, this,
800// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700801//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100802// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
803// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700804//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100805// channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
806// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700807//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100808// channel2->connect("::1", "20070",
809// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
810// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700811//
Junxiao Shi79494162014-04-02 18:25:11 -0700812// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100813// g_io.run();
814// g_io.reset();
815// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700816//
Junxiao Shi79494162014-04-02 18:25:11 -0700817// BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700818//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100819// shared_ptr<UdpChannel> channel3 = factory.createChannel("::1", "20072");
820// channel3->connect("::1", "20070",
821// bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
822// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700823//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100824// shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700825//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100826// BOOST_CHECK_NE(channel3, channel4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700827//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100828// scheduler
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700829// .scheduleEvent(time::milliseconds(500),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100830// bind(&UdpChannel::connect, channel4,
831// "::1", "20070",
832// static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
833// channel_onFaceCreated, this, _1)),
834// static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
835// channel_onConnectFailed, this, _1))));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700836//
Junxiao Shi79494162014-04-02 18:25:11 -0700837// limitedIoRemaining = 2; // 2 connects
Giulio Grassi624f6c62014-02-18 19:42:14 +0100838// abortEvent =
839// scheduler.scheduleEvent(time::seconds(4),
840// bind(&EndToEndFixture::abortTestCase, this,
841// "UdpChannel error: cannot connect or cannot accept multiple connections"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700842//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100843// scheduler.scheduleEvent(time::seconds(0.4),
844// bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700845//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100846// g_io.run();
847// g_io.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700848//
Junxiao Shi79494162014-04-02 18:25:11 -0700849// BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700850//
851//
Junxiao Shi79494162014-04-02 18:25:11 -0700852// face2->sendInterest(interest1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100853// abortEvent =
854// scheduler.scheduleEvent(time::seconds(1),
855// bind(&EndToEndFixture::abortTestCase, this,
856// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700857// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100858// g_io.run();
859// g_io.reset();
860// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700861//
Junxiao Shi79494162014-04-02 18:25:11 -0700862// BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700863//
Junxiao Shi79494162014-04-02 18:25:11 -0700864// face3->sendInterest(interest2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100865// abortEvent =
866// scheduler.scheduleEvent(time::seconds(1),
867// bind(&EndToEndFixture::abortTestCase, this,
868// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700869// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100870// g_io.run();
871// g_io.reset();
872// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700873//
Junxiao Shi79494162014-04-02 18:25:11 -0700874// //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
875// BOOST_CHECK_EQUAL(faces.size(), 5);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100876// BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,
877// this,
878// _1),
879// bind(&EndToEndFixture::channel_onConnectFailedOk,
880// this,
881// _1)),
882// UdpChannel::Error);
883//}
884
885
886BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
887{
888 UdpFactory factory = UdpFactory();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100889
Giulio Grassi624f6c62014-02-18 19:42:14 +0100890 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
891 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700892
Giulio Grassi624f6c62014-02-18 19:42:14 +0100893 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
894 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700895
Giulio Grassi624f6c62014-02-18 19:42:14 +0100896 channel2->connect("127.0.0.1", "20070",
897 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
898 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700899
Junxiao Shi79494162014-04-02 18:25:11 -0700900 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700901 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100902
903 BOOST_CHECK_EQUAL(channel2->size(), 1);
904
Junxiao Shi79494162014-04-02 18:25:11 -0700905 BOOST_CHECK(static_cast<bool>(face2));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100906
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700907 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700908 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700909
Junxiao Shi79494162014-04-02 18:25:11 -0700910 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700911 "FaceClosing error: cannot properly close faces");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100912
Junxiao Shi79494162014-04-02 18:25:11 -0700913 BOOST_CHECK(!static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700914
Giulio Grassi624f6c62014-02-18 19:42:14 +0100915 BOOST_CHECK_EQUAL(channel2->size(), 0);
916}
Junxiao Shi79494162014-04-02 18:25:11 -0700917
Giulio Grassi69871f02014-03-09 16:14:44 +0100918BOOST_FIXTURE_TEST_CASE(ClosingIdleFace, EndToEndFixture)
919{
920 Interest interest1("ndn:/TpnzGvW9R");
921 Interest interest2("ndn:/QWiIMfj5sL");
Davide Pesavento126249b2014-03-13 02:42:21 +0100922
Giulio Grassi69871f02014-03-09 16:14:44 +0100923 UdpFactory factory;
Davide Pesavento126249b2014-03-13 02:42:21 +0100924
925 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070",
Giulio Grassi69871f02014-03-09 16:14:44 +0100926 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100927 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071",
Giulio Grassi69871f02014-03-09 16:14:44 +0100928 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100929
Giulio Grassi69871f02014-03-09 16:14:44 +0100930 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
Davide Pesavento126249b2014-03-13 02:42:21 +0100931 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100932
933 channel2->connect("127.0.0.1", "20070",
934 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
935 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi79494162014-04-02 18:25:11 -0700936
937 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100938 "UdpChannel error: cannot connect or cannot accept connection");
939
Junxiao Shi79494162014-04-02 18:25:11 -0700940 face2->sendInterest(interest1);
941 BOOST_CHECK(!face2->isOnDemand());
Giulio Grassi69871f02014-03-09 16:14:44 +0100942
Junxiao Shi79494162014-04-02 18:25:11 -0700943 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100944 time::seconds(1)) == LimitedIo::EXCEED_OPS,
945 "UdpChannel error: cannot send or receive Interest/Data packets");
Junxiao Shi79494162014-04-02 18:25:11 -0700946
947 BOOST_CHECK_EQUAL(faces.size(), 2);
948 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(2)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100949 "Idle face should be still open because has been used recently");
Junxiao Shi79494162014-04-02 18:25:11 -0700950 BOOST_CHECK_EQUAL(faces.size(), 2);
951 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100952 "Closing idle face error: face should be closed by now");
Junxiao Shi79494162014-04-02 18:25:11 -0700953
Giulio Grassi69871f02014-03-09 16:14:44 +0100954 //the face on listen should be closed now
955 BOOST_CHECK_EQUAL(channel1->size(), 0);
Junxiao Shi79494162014-04-02 18:25:11 -0700956 //checking that face2 has not been closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100957 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700958 BOOST_REQUIRE(static_cast<bool>(face2));
959
960 face2->sendInterest(interest2);
961 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100962 time::seconds(1)) == LimitedIo::EXCEED_OPS,
963 "UdpChannel error: cannot send or receive Interest/Data packets");
964 //channel1 should have created a new face by now
965 BOOST_CHECK_EQUAL(channel1->size(), 1);
966 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700967 BOOST_REQUIRE(static_cast<bool>(face1));
968 BOOST_CHECK(face1->isOnDemand());
969
Giulio Grassi69871f02014-03-09 16:14:44 +0100970 channel1->connect("127.0.0.1", "20071",
971 bind(&EndToEndFixture::channel1_onFaceCreatedNoCheck, this, _1),
972 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
973
Junxiao Shi79494162014-04-02 18:25:11 -0700974 BOOST_CHECK_MESSAGE(limitedIo.run(1,//1 connect
Giulio Grassi69871f02014-03-09 16:14:44 +0100975 time::seconds(1)) == LimitedIo::EXCEED_OPS,
976 "UdpChannel error: cannot connect");
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700977
Junxiao Shi79494162014-04-02 18:25:11 -0700978 BOOST_CHECK(!face1->isOnDemand());
979
980 //the connect should have set face1 as permanent face,
Giulio Grassi69871f02014-03-09 16:14:44 +0100981 //but it shouln't have created any additional faces
982 BOOST_CHECK_EQUAL(channel1->size(), 1);
983 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700984 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100985 "Idle face should be still open because it's permanent now");
986 //both faces are permanent, nothing should have changed
987 BOOST_CHECK_EQUAL(channel1->size(), 1);
988 BOOST_CHECK_EQUAL(channel2->size(), 1);
989}
990
Giulio Grassi624f6c62014-02-18 19:42:14 +0100991BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700992
Giulio Grassi624f6c62014-02-18 19:42:14 +0100993} // namespace tests
994} // namespace nfd