blob: 503ec74640ca7796f83a612be222784a63e4efc2 [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 {
382 //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
392public:
Junxiao Shi79494162014-04-02 18:25:11 -0700393 LimitedIo limitedIo;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100394
Junxiao Shi79494162014-04-02 18:25:11 -0700395 shared_ptr<Face> face1;
396 std::vector<Interest> face1_receivedInterests;
397 std::vector<Data> face1_receivedDatas;
398 shared_ptr<Face> face2;
399 std::vector<Interest> face2_receivedInterests;
400 std::vector<Data> face2_receivedDatas;
401 shared_ptr<Face> face3;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100402
Junxiao Shi79494162014-04-02 18:25:11 -0700403 std::list< shared_ptr<Face> > faces;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100404};
405
406
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000407BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100408{
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700409 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100410
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000411 factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700412
Giulio Grassi624f6c62014-02-18 19:42:14 +0100413 factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
414 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
415 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700416
417
Junxiao Shi79494162014-04-02 18:25:11 -0700418 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700419 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100420
Junxiao Shi79494162014-04-02 18:25:11 -0700421 BOOST_REQUIRE(static_cast<bool>(face2));
422 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp4://127.0.0.1:20070");
423 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp4://127.0.0.1:20071");
424 BOOST_CHECK_EQUAL(face2->isLocal(), false);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700425 BOOST_CHECK_EQUAL(face2->getCounters().getNOutBytes(), 0);
426 BOOST_CHECK_EQUAL(face2->getCounters().getNInBytes(), 0);
Junxiao Shi79494162014-04-02 18:25:11 -0700427 // face1 is not created yet
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700428
Giulio Grassi624f6c62014-02-18 19:42:14 +0100429 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
430 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
431 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700432
Giulio Grassi624f6c62014-02-18 19:42:14 +0100433 Interest interest1("ndn:/TpnzGvW9R");
434 Data data1 ("ndn:/KfczhUqVix");
435 data1.setContent(0, 0);
436 Interest interest2("ndn:/QWiIMfj5sL");
437 Data data2 ("ndn:/XNBV796f");
438 data2.setContent(0, 0);
439 Interest interest3("ndn:/QWiIhjgkj5sL");
440 Data data3 ("ndn:/XNBV794f");
441 data3.setContent(0, 0);
442
443
444 ndn::SignatureSha256WithRsa fakeSignature;
445 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
446 reinterpret_cast<const uint8_t*>(0),
447 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700448
Giulio Grassi624f6c62014-02-18 19:42:14 +0100449 // set fake signature on data1 and data2
450 data1.setSignature(fakeSignature);
451 data2.setSignature(fakeSignature);
452 data3.setSignature(fakeSignature);
453
Junxiao Shi79494162014-04-02 18:25:11 -0700454 face2->sendInterest(interest2);
455 face2->sendData (data2 );
456 face2->sendData (data2 );
457 face2->sendData (data2 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700458 size_t nBytesSent2 = interest2.wireEncode().size() + data2.wireEncode().size() * 3;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100459
Junxiao Shi79494162014-04-02 18:25:11 -0700460 BOOST_CHECK_MESSAGE(limitedIo.run(5,//4 send + 1 listen return
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000461 time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700462 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100463
Junxiao Shi79494162014-04-02 18:25:11 -0700464 BOOST_REQUIRE(static_cast<bool>(face1));
465 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp4://127.0.0.1:20071");
466 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp4://127.0.0.1:20070");
467 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700468
Junxiao Shi79494162014-04-02 18:25:11 -0700469 face1->sendInterest(interest1);
470 face1->sendInterest(interest1);
471 face1->sendInterest(interest1);
472 face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700473
Junxiao Shi79494162014-04-02 18:25:11 -0700474 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700475 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100476
Junxiao Shi79494162014-04-02 18:25:11 -0700477 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
478 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
479 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
480 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700481
Junxiao Shi79494162014-04-02 18:25:11 -0700482 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
483 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
484 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
485 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700486
487
488
Giulio Grassi624f6c62014-02-18 19:42:14 +0100489 //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700490
Junxiao Shi79494162014-04-02 18:25:11 -0700491 face2->sendData (data3 );
492 face2->sendInterest(interest3);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700493 nBytesSent2 += data3.wireEncode().size() + interest3.wireEncode().size();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100494
Junxiao Shi79494162014-04-02 18:25:11 -0700495 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700496 "UdpChannel error: cannot send or receive Interest/Data packets");
497
Junxiao Shi79494162014-04-02 18:25:11 -0700498 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
499 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700500
Junxiao Shi79494162014-04-02 18:25:11 -0700501 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
502 BOOST_CHECK_EQUAL(face1_receivedDatas [3].getName(), data3.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000503
Junxiao Shi79494162014-04-02 18:25:11 -0700504 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700505 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 2);
506 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 4);
507 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
508 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700509 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesSent2);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000510
Junxiao Shi79494162014-04-02 18:25:11 -0700511 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700512 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
513 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
514 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 2);
515 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 4);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700516 BOOST_CHECK_EQUAL(counters2.getNOutBytes(), nBytesSent2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000517}
Giulio Grassi624f6c62014-02-18 19:42:14 +0100518
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000519BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
520{
521 UdpFactory factory;
522
523 factory.createChannel("::1", "20071");
524
525 factory.createFace(FaceUri("udp://[::1]:20070"),
526 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
527 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
528
529
Junxiao Shi79494162014-04-02 18:25:11 -0700530 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000531 "UdpChannel error: cannot connect or cannot accept connection");
532
Junxiao Shi79494162014-04-02 18:25:11 -0700533 BOOST_REQUIRE(static_cast<bool>(face2));
534 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp6://[::1]:20070");
535 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp6://[::1]:20071");
536 BOOST_CHECK_EQUAL(face2->isLocal(), false);
537 // face1 is not created yet
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000538
539 shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
540 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
541 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
542
543 Interest interest1("ndn:/TpnzGvW9R");
544 Data data1 ("ndn:/KfczhUqVix");
545 data1.setContent(0, 0);
546 Interest interest2("ndn:/QWiIMfj5sL");
547 Data data2 ("ndn:/XNBV796f");
548 data2.setContent(0, 0);
549 Interest interest3("ndn:/QWiIhjgkj5sL");
550 Data data3 ("ndn:/XNBV794f");
551 data3.setContent(0, 0);
552
553
554 ndn::SignatureSha256WithRsa fakeSignature;
555 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
556 reinterpret_cast<const uint8_t*>(0),
557 0));
558
559 // set fake signature on data1 and data2
560 data1.setSignature(fakeSignature);
561 data2.setSignature(fakeSignature);
562 data3.setSignature(fakeSignature);
563
Junxiao Shi79494162014-04-02 18:25:11 -0700564 face2->sendInterest(interest2);
565 face2->sendData (data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000566
Junxiao Shi79494162014-04-02 18:25:11 -0700567 BOOST_CHECK_MESSAGE(limitedIo.run(3,//2 send + 1 listen return
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000568 time::seconds(1)) == LimitedIo::EXCEED_OPS,
569 "UdpChannel error: cannot send or receive Interest/Data packets");
570
Junxiao Shi79494162014-04-02 18:25:11 -0700571 BOOST_REQUIRE(static_cast<bool>(face1));
572 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp6://[::1]:20071");
573 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp6://[::1]:20070");
574 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000575
Junxiao Shi79494162014-04-02 18:25:11 -0700576 face1->sendInterest(interest1);
577 face1->sendData (data1 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000578
Junxiao Shi79494162014-04-02 18:25:11 -0700579 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000580 "UdpChannel error: cannot send or receive Interest/Data packets");
581
582
Junxiao Shi79494162014-04-02 18:25:11 -0700583 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
584 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
585 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
586 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000587
Junxiao Shi79494162014-04-02 18:25:11 -0700588 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
589 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
590 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
591 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000592
593
594
595 //checking if the connection accepting mechanism works properly.
596
Junxiao Shi79494162014-04-02 18:25:11 -0700597 face2->sendData (data3 );
598 face2->sendInterest(interest3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000599
Junxiao Shi79494162014-04-02 18:25:11 -0700600 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000601 "UdpChannel error: cannot send or receive Interest/Data packets");
602
Junxiao Shi79494162014-04-02 18:25:11 -0700603 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
604 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000605
Junxiao Shi79494162014-04-02 18:25:11 -0700606 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
607 BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Giulio Grassi624f6c62014-02-18 19:42:14 +0100608}
609
610BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
611{
612 Interest interest1("ndn:/TpnzGvW9R");
613 Interest interest2("ndn:/QWiIMfj5sL");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100614
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700615 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100616
Giulio Grassi624f6c62014-02-18 19:42:14 +0100617 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
618 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700619
Giulio Grassi624f6c62014-02-18 19:42:14 +0100620 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
621 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700622
Giulio Grassi624f6c62014-02-18 19:42:14 +0100623 channel2->connect("127.0.0.1", "20070",
624 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
625 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700626
627
Junxiao Shi79494162014-04-02 18:25:11 -0700628 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700629 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100630
Junxiao Shi79494162014-04-02 18:25:11 -0700631 BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700632
Giulio Grassi624f6c62014-02-18 19:42:14 +0100633 shared_ptr<UdpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
634 channel3->connect("127.0.0.1", "20070",
635 bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
636 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700637
Giulio Grassi624f6c62014-02-18 19:42:14 +0100638 shared_ptr<UdpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
639
640 BOOST_CHECK_NE(channel3, channel4);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100641
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700642 scheduler::schedule(time::milliseconds(500),
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700643 bind(&UdpChannel::connect, channel4, "127.0.0.1", "20070",
644 // does not work without static_cast
645 static_cast<UdpChannel::FaceCreatedCallback>(
646 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
647 static_cast<UdpChannel::ConnectFailedCallback>(
648 bind(&EndToEndFixture::channel_onConnectFailed, this, _1))));
649
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700650 scheduler::schedule(time::milliseconds(400), bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700651
Junxiao Shi79494162014-04-02 18:25:11 -0700652 BOOST_CHECK_MESSAGE(limitedIo.run(2,// 2 connects
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700653 time::seconds(4)) == LimitedIo::EXCEED_OPS,
654 "UdpChannel error: cannot connect or cannot accept multiple connections");
655
Junxiao Shi79494162014-04-02 18:25:11 -0700656 BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700657
658
Junxiao Shi79494162014-04-02 18:25:11 -0700659 face2->sendInterest(interest1);
660 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700661 "UdpChannel error: cannot send or receive Interest/Data packets");
662
Junxiao Shi79494162014-04-02 18:25:11 -0700663 BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700664
Junxiao Shi79494162014-04-02 18:25:11 -0700665 face3->sendInterest(interest2);
666 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700667 "UdpChannel error: cannot send or receive Interest/Data packets");
668
Junxiao Shi79494162014-04-02 18:25:11 -0700669 //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
670 BOOST_CHECK_EQUAL(faces.size(), 5);
Davide Pesavento126249b2014-03-13 02:42:21 +0100671 BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
672 bind(&EndToEndFixture::channel_onConnectFailedOk, this, _1)),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100673 UdpChannel::Error);
674}
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700675
Giulio Grassi624f6c62014-02-18 19:42:14 +0100676//Test commented because it required to be run in a machine that can resolve ipv6 query
677//BOOST_FIXTURE_TEST_CASE(EndToEndIpv6, EndToEndFixture)
678//{
679// UdpFactory factory = UdpFactory();
680// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700681//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100682// EventId abortEvent =
683// scheduler.scheduleEvent(time::seconds(1),
684// bind(&EndToEndFixture::abortTestCase, this,
685// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700686//
Junxiao Shi79494162014-04-02 18:25:11 -0700687// limitedIoRemaining = 1;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700688//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100689// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
690// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700691//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100692// channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
693// bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700694//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100695// channel2->connect("::1", "20070",
696// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
697// bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
698// g_io.run();
699// g_io.reset();
700// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700701//
Junxiao Shi79494162014-04-02 18:25:11 -0700702// BOOST_REQUIRE(static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700703//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100704// abortEvent =
705// scheduler.scheduleEvent(time::seconds(1),
706// bind(&EndToEndFixture::abortTestCase, this,
707// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700708//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100709// Interest interest1("ndn:/TpnzGvW9R");
710// Data data1 ("ndn:/KfczhUqVix");
711// data1.setContent(0, 0);
712// Interest interest2("ndn:/QWiIMfj5sL");
713// Data data2 ("ndn:/XNBV796f");
714// data2.setContent(0, 0);
715// Interest interest3("ndn:/QWiIhjgkj5sL");
716// Data data3 ("ndn:/XNBV794f");
717// data3.setContent(0, 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700718//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100719// ndn::SignatureSha256WithRsa fakeSignature;
720// fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700721//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100722// // set fake signature on data1 and data2
723// data1.setSignature(fakeSignature);
724// data2.setSignature(fakeSignature);
725// data3.setSignature(fakeSignature);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700726//
Junxiao Shi79494162014-04-02 18:25:11 -0700727// face2->sendInterest(interest2);
728// face2->sendData (data2 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700729//
Junxiao Shi79494162014-04-02 18:25:11 -0700730// limitedIoRemaining = 3; //2 send + 1 listen return
Giulio Grassi624f6c62014-02-18 19:42:14 +0100731// g_io.run();
732// g_io.reset();
733// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700734//
Junxiao Shi79494162014-04-02 18:25:11 -0700735// BOOST_REQUIRE(static_cast<bool>(face1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700736//
Junxiao Shi79494162014-04-02 18:25:11 -0700737// face1->sendInterest(interest1);
738// face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700739//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100740// abortEvent =
741// scheduler.scheduleEvent(time::seconds(1),
742// bind(&EndToEndFixture::abortTestCase, this,
743// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700744// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100745// g_io.run();
746// g_io.reset();
747// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700748//
Junxiao Shi79494162014-04-02 18:25:11 -0700749// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
750// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
751// BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
752// BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700753//
Junxiao Shi79494162014-04-02 18:25:11 -0700754// BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
755// BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
756// BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
757// BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700758//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100759// //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700760//
Junxiao Shi79494162014-04-02 18:25:11 -0700761// face2->sendData (data3 );
762// face2->sendInterest(interest3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700763//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100764// abortEvent =
765// scheduler.scheduleEvent(time::seconds(1),
766// bind(&EndToEndFixture::abortTestCase, this,
767// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700768// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100769// g_io.run();
770// g_io.reset();
771// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700772//
Junxiao Shi79494162014-04-02 18:25:11 -0700773// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
774// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700775//
Junxiao Shi79494162014-04-02 18:25:11 -0700776// BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
777// BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700778//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100779//}
780
781
782//Test commented because it required to be run in a machine that can resolve ipv6 query
783//BOOST_FIXTURE_TEST_CASE(MultipleAcceptsIpv6, EndToEndFixture)
784//{
785// Interest interest1("ndn:/TpnzGvW9R");
786// Interest interest2("ndn:/QWiIMfj5sL");
787// Interest interest3("ndn:/QWiIhjgkj5sL");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700788//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100789// UdpFactory factory = UdpFactory();
790// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700791//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100792// EventId abortEvent =
793// scheduler.scheduleEvent(time::seconds(4),
794// bind(&EndToEndFixture::abortTestCase, this,
795// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700796//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100797// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
798// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700799//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100800// channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
801// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700802//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100803// channel2->connect("::1", "20070",
804// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
805// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700806//
Junxiao Shi79494162014-04-02 18:25:11 -0700807// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100808// g_io.run();
809// g_io.reset();
810// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700811//
Junxiao Shi79494162014-04-02 18:25:11 -0700812// BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700813//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100814// shared_ptr<UdpChannel> channel3 = factory.createChannel("::1", "20072");
815// channel3->connect("::1", "20070",
816// bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
817// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700818//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100819// shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700820//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100821// BOOST_CHECK_NE(channel3, channel4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700822//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100823// scheduler
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700824// .scheduleEvent(time::milliseconds(500),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100825// bind(&UdpChannel::connect, channel4,
826// "::1", "20070",
827// static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
828// channel_onFaceCreated, this, _1)),
829// static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
830// channel_onConnectFailed, this, _1))));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700831//
Junxiao Shi79494162014-04-02 18:25:11 -0700832// limitedIoRemaining = 2; // 2 connects
Giulio Grassi624f6c62014-02-18 19:42:14 +0100833// abortEvent =
834// scheduler.scheduleEvent(time::seconds(4),
835// bind(&EndToEndFixture::abortTestCase, this,
836// "UdpChannel error: cannot connect or cannot accept multiple connections"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700837//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100838// scheduler.scheduleEvent(time::seconds(0.4),
839// bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700840//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100841// g_io.run();
842// g_io.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700843//
Junxiao Shi79494162014-04-02 18:25:11 -0700844// BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700845//
846//
Junxiao Shi79494162014-04-02 18:25:11 -0700847// face2->sendInterest(interest1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100848// abortEvent =
849// scheduler.scheduleEvent(time::seconds(1),
850// bind(&EndToEndFixture::abortTestCase, this,
851// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700852// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100853// g_io.run();
854// g_io.reset();
855// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700856//
Junxiao Shi79494162014-04-02 18:25:11 -0700857// BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700858//
Junxiao Shi79494162014-04-02 18:25:11 -0700859// face3->sendInterest(interest2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100860// abortEvent =
861// scheduler.scheduleEvent(time::seconds(1),
862// bind(&EndToEndFixture::abortTestCase, this,
863// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700864// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100865// g_io.run();
866// g_io.reset();
867// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700868//
Junxiao Shi79494162014-04-02 18:25:11 -0700869// //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
870// BOOST_CHECK_EQUAL(faces.size(), 5);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100871// BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,
872// this,
873// _1),
874// bind(&EndToEndFixture::channel_onConnectFailedOk,
875// this,
876// _1)),
877// UdpChannel::Error);
878//}
879
880
881BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
882{
883 UdpFactory factory = UdpFactory();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100884
Giulio Grassi624f6c62014-02-18 19:42:14 +0100885 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
886 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700887
Giulio Grassi624f6c62014-02-18 19:42:14 +0100888 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
889 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700890
Giulio Grassi624f6c62014-02-18 19:42:14 +0100891 channel2->connect("127.0.0.1", "20070",
892 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
893 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700894
Junxiao Shi79494162014-04-02 18:25:11 -0700895 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700896 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100897
898 BOOST_CHECK_EQUAL(channel2->size(), 1);
899
Junxiao Shi79494162014-04-02 18:25:11 -0700900 BOOST_CHECK(static_cast<bool>(face2));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100901
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700902 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700903 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700904
Junxiao Shi79494162014-04-02 18:25:11 -0700905 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700906 "FaceClosing error: cannot properly close faces");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100907
Junxiao Shi79494162014-04-02 18:25:11 -0700908 BOOST_CHECK(!static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700909
Giulio Grassi624f6c62014-02-18 19:42:14 +0100910 BOOST_CHECK_EQUAL(channel2->size(), 0);
911}
Junxiao Shi79494162014-04-02 18:25:11 -0700912
Giulio Grassi69871f02014-03-09 16:14:44 +0100913BOOST_FIXTURE_TEST_CASE(ClosingIdleFace, EndToEndFixture)
914{
915 Interest interest1("ndn:/TpnzGvW9R");
916 Interest interest2("ndn:/QWiIMfj5sL");
Davide Pesavento126249b2014-03-13 02:42:21 +0100917
Giulio Grassi69871f02014-03-09 16:14:44 +0100918 UdpFactory factory;
Davide Pesavento126249b2014-03-13 02:42:21 +0100919
920 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070",
Giulio Grassi69871f02014-03-09 16:14:44 +0100921 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100922 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071",
Giulio Grassi69871f02014-03-09 16:14:44 +0100923 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100924
Giulio Grassi69871f02014-03-09 16:14:44 +0100925 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
Davide Pesavento126249b2014-03-13 02:42:21 +0100926 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100927
928 channel2->connect("127.0.0.1", "20070",
929 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
930 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi79494162014-04-02 18:25:11 -0700931
932 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100933 "UdpChannel error: cannot connect or cannot accept connection");
934
Junxiao Shi79494162014-04-02 18:25:11 -0700935 face2->sendInterest(interest1);
936 BOOST_CHECK(!face2->isOnDemand());
Giulio Grassi69871f02014-03-09 16:14:44 +0100937
Junxiao Shi79494162014-04-02 18:25:11 -0700938 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100939 time::seconds(1)) == LimitedIo::EXCEED_OPS,
940 "UdpChannel error: cannot send or receive Interest/Data packets");
Junxiao Shi79494162014-04-02 18:25:11 -0700941
942 BOOST_CHECK_EQUAL(faces.size(), 2);
943 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(2)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100944 "Idle face should be still open because has been used recently");
Junxiao Shi79494162014-04-02 18:25:11 -0700945 BOOST_CHECK_EQUAL(faces.size(), 2);
946 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100947 "Closing idle face error: face should be closed by now");
Junxiao Shi79494162014-04-02 18:25:11 -0700948
Giulio Grassi69871f02014-03-09 16:14:44 +0100949 //the face on listen should be closed now
950 BOOST_CHECK_EQUAL(channel1->size(), 0);
Junxiao Shi79494162014-04-02 18:25:11 -0700951 //checking that face2 has not been closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100952 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700953 BOOST_REQUIRE(static_cast<bool>(face2));
954
955 face2->sendInterest(interest2);
956 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100957 time::seconds(1)) == LimitedIo::EXCEED_OPS,
958 "UdpChannel error: cannot send or receive Interest/Data packets");
959 //channel1 should have created a new face by now
960 BOOST_CHECK_EQUAL(channel1->size(), 1);
961 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700962 BOOST_REQUIRE(static_cast<bool>(face1));
963 BOOST_CHECK(face1->isOnDemand());
964
Giulio Grassi69871f02014-03-09 16:14:44 +0100965 channel1->connect("127.0.0.1", "20071",
966 bind(&EndToEndFixture::channel1_onFaceCreatedNoCheck, this, _1),
967 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
968
Junxiao Shi79494162014-04-02 18:25:11 -0700969 BOOST_CHECK_MESSAGE(limitedIo.run(1,//1 connect
Giulio Grassi69871f02014-03-09 16:14:44 +0100970 time::seconds(1)) == LimitedIo::EXCEED_OPS,
971 "UdpChannel error: cannot connect");
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700972
Junxiao Shi79494162014-04-02 18:25:11 -0700973 BOOST_CHECK(!face1->isOnDemand());
974
975 //the connect should have set face1 as permanent face,
Giulio Grassi69871f02014-03-09 16:14:44 +0100976 //but it shouln't have created any additional faces
977 BOOST_CHECK_EQUAL(channel1->size(), 1);
978 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700979 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100980 "Idle face should be still open because it's permanent now");
981 //both faces are permanent, nothing should have changed
982 BOOST_CHECK_EQUAL(channel1->size(), 1);
983 BOOST_CHECK_EQUAL(channel2->size(), 1);
984}
985
Giulio Grassi624f6c62014-02-18 19:42:14 +0100986BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700987
Giulio Grassi624f6c62014-02-18 19:42:14 +0100988} // namespace tests
989} // namespace nfd