blob: f2d3417d2c53fe8e6174166dafdde12b74564955 [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:
64 bool isTheSameMulticastEndpoint(const UdpFactory::Error& e) {
65 return strcmp(e.what(),
66 "Cannot create the requested UDP unicast channel, local "
67 "endpoint is already allocated for a UDP multicast face") == 0;
68 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070069
Giulio Grassi624f6c62014-02-18 19:42:14 +010070 bool isNotMulticastAddress(const UdpFactory::Error& e) {
71 return strcmp(e.what(),
72 "Cannot create the requested UDP multicast face, "
73 "the multicast group given as input is not a multicast address") == 0;
74 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070075
Giulio Grassi624f6c62014-02-18 19:42:14 +010076 bool isTheSameUnicastEndpoint(const UdpFactory::Error& e) {
77 return strcmp(e.what(),
78 "Cannot create the requested UDP multicast face, local "
79 "endpoint is already allocated for a UDP unicast channel") == 0;
80 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070081
Giulio Grassi624f6c62014-02-18 19:42:14 +010082 bool isLocalEndpointOnDifferentGroup(const UdpFactory::Error& e) {
83 return strcmp(e.what(),
84 "Cannot create the requested UDP multicast face, local "
85 "endpoint is already allocated for a UDP multicast face "
86 "on a different multicast group") == 0;
87 }
88};
Junxiao Shi7e2413b2014-03-02 11:15:09 -070089
Giulio Grassi624f6c62014-02-18 19:42:14 +010090BOOST_FIXTURE_TEST_CASE(ChannelMapUdp, FactoryErrorCheck)
91{
92 using boost::asio::ip::udp;
Junxiao Shi7e2413b2014-03-02 11:15:09 -070093
Giulio Grassi624f6c62014-02-18 19:42:14 +010094 UdpFactory factory = UdpFactory();
Junxiao Shi7e2413b2014-03-02 11:15:09 -070095
Giulio Grassi624f6c62014-02-18 19:42:14 +010096 //to instantiate multicast face on a specific ip address, change interfaceIp
97 std::string interfaceIp = "0.0.0.0";
Junxiao Shi7e2413b2014-03-02 11:15:09 -070098
Giulio Grassi624f6c62014-02-18 19:42:14 +010099 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
100 shared_ptr<UdpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
101 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700102 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700103
Giulio Grassi624f6c62014-02-18 19:42:14 +0100104 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
105 BOOST_CHECK_NE(channel1, channel2);
106
107 shared_ptr<UdpChannel> channel3 = factory.createChannel(interfaceIp, "20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700108
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700109 shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20071");
110 BOOST_CHECK_NE(channel2, channel4);
111 BOOST_CHECK_EQUAL(channel4->getUri().toString(), "udp6://[::1]:20071");
112
Giulio Grassi624f6c62014-02-18 19:42:14 +0100113 //same endpoint of a unicast channel
114 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
115 "224.0.0.1",
116 "20070"),
117 UdpFactory::Error,
118 isTheSameUnicastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700119
120
Giulio Grassi624f6c62014-02-18 19:42:14 +0100121 shared_ptr<MulticastUdpFace> multicastFace1 = factory.createMulticastFace(interfaceIp,
122 "224.0.0.1",
123 "20072");
124 shared_ptr<MulticastUdpFace> multicastFace1a = factory.createMulticastFace(interfaceIp,
125 "224.0.0.1",
126 "20072");
127 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700128
129
Giulio Grassi624f6c62014-02-18 19:42:14 +0100130 //same endpoint of a multicast face
131 BOOST_CHECK_EXCEPTION(factory.createChannel(interfaceIp, "20072"),
132 UdpFactory::Error,
133 isTheSameMulticastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700134
Giulio Grassi624f6c62014-02-18 19:42:14 +0100135 //same multicast endpoint, different group
136 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
137 "224.0.0.42",
138 "20072"),
139 UdpFactory::Error,
140 isLocalEndpointOnDifferentGroup);
141
142 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
143 "192.168.10.15",
144 "20025"),
145 UdpFactory::Error,
146 isNotMulticastAddress);
147
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700148
Giulio Grassi624f6c62014-02-18 19:42:14 +0100149// //Test commented because it required to be run in a machine that can resolve ipv6 query
150// shared_ptr<UdpChannel> channel1v6 = factory.createChannel(//"::1",
151// "fe80::5e96:9dff:fe7d:9c8d%en1",
152// //"fe80::aa54:b2ff:fe08:27b8%wlan0",
153// "20070");
154//
155// //the creation of multicastFace2 works properly. It has been disable because it needs an IP address of
156// //an available network interface (different from the first one used)
157// shared_ptr<MulticastUdpFace> multicastFace2 = factory.createMulticastFace("192.168.1.17",
158// "224.0.0.1",
159// "20073");
160// BOOST_CHECK_NE(multicastFace1, multicastFace2);
161//
162//
163// //ipv6 - work in progress
164// shared_ptr<MulticastUdpFace> multicastFace3 = factory.createMulticastFace("fe80::5e96:9dff:fe7d:9c8d%en1",
165// "FF01:0:0:0:0:0:0:2",
166// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700167//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100168// shared_ptr<MulticastUdpFace> multicastFace4 = factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
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// BOOST_CHECK_EQUAL(multicastFace3, multicastFace4);
173//
174// shared_ptr<MulticastUdpFace> multicastFace5 = factory.createMulticastFace("::1",
175// "FF01:0:0:0:0:0:0:2",
176// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700177//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100178// BOOST_CHECK_NE(multicastFace3, multicastFace5);
179//
180// //same local ipv6 endpoint for a different multicast group
181// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
182// "FE01:0:0:0:0:0:0:2",
183// "20073"),
184// UdpFactory::Error);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700185//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100186// //same local ipv6 (expect for th port number) endpoint for a different multicast group
187// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
188// "FE01:0:0:0:0:0:0:2",
189// "20075"),
190// UdpFactory::Error);
191//
192// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
193// "FE12:0:0:0:0:0:0:2",
194// "20075"),
195// UdpFactory::Error);
196//
197// //not a multicast ipv6
198// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
199// "A112:0:0:0:0:0:0:2",
200// "20075"),
201// UdpFactory::Error);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100202}
203
204class EndToEndFixture : protected BaseFixture
205{
206public:
207 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700208 channel1_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100209 {
Junxiao Shi79494162014-04-02 18:25:11 -0700210 BOOST_CHECK(!static_cast<bool>(face1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100211 channel1_onFaceCreatedNoCheck(newFace);
212 }
Junxiao Shi79494162014-04-02 18:25:11 -0700213
Giulio Grassi69871f02014-03-09 16:14:44 +0100214 void
215 channel1_onFaceCreatedNoCheck(const shared_ptr<Face>& newFace)
216 {
Junxiao Shi79494162014-04-02 18:25:11 -0700217 face1 = newFace;
218 face1->onReceiveInterest +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100219 bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700220 face1->onReceiveData +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100221 bind(&EndToEndFixture::face1_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700222 face1->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100223 bind(&EndToEndFixture::face1_onFail, this);
224 BOOST_CHECK_MESSAGE(true, "channel 1 face created");
225
Junxiao Shi79494162014-04-02 18:25:11 -0700226 faces.push_back(face1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100227
Junxiao Shi79494162014-04-02 18:25:11 -0700228 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100229 }
230
231 void
232 channel1_onConnectFailed(const std::string& reason)
233 {
234 BOOST_CHECK_MESSAGE(false, reason);
235
Junxiao Shi79494162014-04-02 18:25:11 -0700236 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100237 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700238
Giulio Grassi624f6c62014-02-18 19:42:14 +0100239 void
240 face1_onReceiveInterest(const Interest& interest)
241 {
Junxiao Shi79494162014-04-02 18:25:11 -0700242 face1_receivedInterests.push_back(interest);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100243
Junxiao Shi79494162014-04-02 18:25:11 -0700244 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100245 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700246
Giulio Grassi624f6c62014-02-18 19:42:14 +0100247 void
248 face1_onReceiveData(const Data& data)
249 {
Junxiao Shi79494162014-04-02 18:25:11 -0700250 face1_receivedDatas.push_back(data);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100251
Junxiao Shi79494162014-04-02 18:25:11 -0700252 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100253 }
254
255 void
256 face1_onFail()
257 {
Junxiao Shi79494162014-04-02 18:25:11 -0700258 face1.reset();
259 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100260 }
261
262 void
263 channel2_onFaceCreated(const shared_ptr<Face>& newFace)
264 {
Junxiao Shi79494162014-04-02 18:25:11 -0700265 BOOST_CHECK(!static_cast<bool>(face2));
266 face2 = newFace;
267 face2->onReceiveInterest +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100268 bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700269 face2->onReceiveData +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100270 bind(&EndToEndFixture::face2_onReceiveData, this, _1);
Junxiao Shi79494162014-04-02 18:25:11 -0700271 face2->onFail +=
Giulio Grassi624f6c62014-02-18 19:42:14 +0100272 bind(&EndToEndFixture::face2_onFail, this);
273
Junxiao Shi79494162014-04-02 18:25:11 -0700274 faces.push_back(face2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100275
276 BOOST_CHECK_MESSAGE(true, "channel 2 face created");
Junxiao Shi79494162014-04-02 18:25:11 -0700277 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100278 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700279
Giulio Grassi624f6c62014-02-18 19:42:14 +0100280 void
281 channel2_onConnectFailed(const std::string& reason)
282 {
283 BOOST_CHECK_MESSAGE(false, reason);
284
Junxiao Shi79494162014-04-02 18:25:11 -0700285 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100286 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700287
Giulio Grassi624f6c62014-02-18 19:42:14 +0100288 void
289 face2_onReceiveInterest(const Interest& interest)
290 {
Junxiao Shi79494162014-04-02 18:25:11 -0700291 face2_receivedInterests.push_back(interest);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100292
Junxiao Shi79494162014-04-02 18:25:11 -0700293 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100294 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700295
Giulio Grassi624f6c62014-02-18 19:42:14 +0100296 void
297 face2_onReceiveData(const Data& data)
298 {
Junxiao Shi79494162014-04-02 18:25:11 -0700299 face2_receivedDatas.push_back(data);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100300
Junxiao Shi79494162014-04-02 18:25:11 -0700301 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100302 }
303
304 void
305 face2_onFail()
306 {
Junxiao Shi79494162014-04-02 18:25:11 -0700307 face2.reset();
308 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100309 }
310
311 void
312 channel3_onFaceCreated(const shared_ptr<Face>& newFace)
313 {
Junxiao Shi79494162014-04-02 18:25:11 -0700314 BOOST_CHECK(!static_cast<bool>(face1));
315 face3 = newFace;
316 faces.push_back(newFace);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700317
Junxiao Shi79494162014-04-02 18:25:11 -0700318 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100319 }
320
Giulio Grassi624f6c62014-02-18 19:42:14 +0100321 void
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700322 channel_onFaceCreated(const shared_ptr<Face>& newFace)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100323 {
Junxiao Shi79494162014-04-02 18:25:11 -0700324 faces.push_back(newFace);
325 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100326 }
327
328 void
329 channel_onConnectFailed(const std::string& reason)
330 {
331 BOOST_CHECK_MESSAGE(false, reason);
332
Junxiao Shi79494162014-04-02 18:25:11 -0700333 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100334 }
335
336 void
337 channel_onConnectFailedOk(const std::string& reason)
338 {
339 //it's ok, it was supposed to fail
Junxiao Shi79494162014-04-02 18:25:11 -0700340 limitedIo.afterOp();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100341 }
342
343 void
344 checkFaceList(size_t shouldBe)
345 {
Junxiao Shi79494162014-04-02 18:25:11 -0700346 BOOST_CHECK_EQUAL(faces.size(), shouldBe);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100347 }
Giulio Grassi624f6c62014-02-18 19:42:14 +0100348
349public:
Junxiao Shi79494162014-04-02 18:25:11 -0700350 LimitedIo limitedIo;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100351
Junxiao Shi79494162014-04-02 18:25:11 -0700352 shared_ptr<Face> face1;
353 std::vector<Interest> face1_receivedInterests;
354 std::vector<Data> face1_receivedDatas;
355 shared_ptr<Face> face2;
356 std::vector<Interest> face2_receivedInterests;
357 std::vector<Data> face2_receivedDatas;
358 shared_ptr<Face> face3;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100359
Junxiao Shi79494162014-04-02 18:25:11 -0700360 std::list< shared_ptr<Face> > faces;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100361};
362
363
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000364BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100365{
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700366 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100367
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000368 factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700369
Giulio Grassi624f6c62014-02-18 19:42:14 +0100370 factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
371 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
372 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700373
374
Junxiao Shi79494162014-04-02 18:25:11 -0700375 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700376 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100377
Junxiao Shi79494162014-04-02 18:25:11 -0700378 BOOST_REQUIRE(static_cast<bool>(face2));
379 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp4://127.0.0.1:20070");
380 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp4://127.0.0.1:20071");
381 BOOST_CHECK_EQUAL(face2->isLocal(), false);
382 // face1 is not created yet
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700383
Giulio Grassi624f6c62014-02-18 19:42:14 +0100384 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
385 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
386 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700387
Giulio Grassi624f6c62014-02-18 19:42:14 +0100388 Interest interest1("ndn:/TpnzGvW9R");
389 Data data1 ("ndn:/KfczhUqVix");
390 data1.setContent(0, 0);
391 Interest interest2("ndn:/QWiIMfj5sL");
392 Data data2 ("ndn:/XNBV796f");
393 data2.setContent(0, 0);
394 Interest interest3("ndn:/QWiIhjgkj5sL");
395 Data data3 ("ndn:/XNBV794f");
396 data3.setContent(0, 0);
397
398
399 ndn::SignatureSha256WithRsa fakeSignature;
400 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
401 reinterpret_cast<const uint8_t*>(0),
402 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700403
Giulio Grassi624f6c62014-02-18 19:42:14 +0100404 // set fake signature on data1 and data2
405 data1.setSignature(fakeSignature);
406 data2.setSignature(fakeSignature);
407 data3.setSignature(fakeSignature);
408
Junxiao Shi79494162014-04-02 18:25:11 -0700409 face2->sendInterest(interest2);
410 face2->sendData (data2 );
411 face2->sendData (data2 );
412 face2->sendData (data2 );
Giulio Grassi624f6c62014-02-18 19:42:14 +0100413
Junxiao Shi79494162014-04-02 18:25:11 -0700414 BOOST_CHECK_MESSAGE(limitedIo.run(5,//4 send + 1 listen return
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000415 time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700416 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100417
Junxiao Shi79494162014-04-02 18:25:11 -0700418 BOOST_REQUIRE(static_cast<bool>(face1));
419 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp4://127.0.0.1:20071");
420 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp4://127.0.0.1:20070");
421 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700422
Junxiao Shi79494162014-04-02 18:25:11 -0700423 face1->sendInterest(interest1);
424 face1->sendInterest(interest1);
425 face1->sendInterest(interest1);
426 face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700427
Junxiao Shi79494162014-04-02 18:25:11 -0700428 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700429 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100430
431
Junxiao Shi79494162014-04-02 18:25:11 -0700432 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
433 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
434 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
435 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700436
Junxiao Shi79494162014-04-02 18:25:11 -0700437 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
438 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
439 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
440 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700441
442
443
Giulio Grassi624f6c62014-02-18 19:42:14 +0100444 //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700445
Junxiao Shi79494162014-04-02 18:25:11 -0700446 face2->sendData (data3 );
447 face2->sendInterest(interest3);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100448
Junxiao Shi79494162014-04-02 18:25:11 -0700449 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700450 "UdpChannel error: cannot send or receive Interest/Data packets");
451
Junxiao Shi79494162014-04-02 18:25:11 -0700452 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
453 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700454
Junxiao Shi79494162014-04-02 18:25:11 -0700455 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
456 BOOST_CHECK_EQUAL(face1_receivedDatas [3].getName(), data3.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000457
Junxiao Shi79494162014-04-02 18:25:11 -0700458 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700459 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 2);
460 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 4);
461 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
462 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000463
Junxiao Shi79494162014-04-02 18:25:11 -0700464 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700465 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
466 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
467 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 2);
468 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 4);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000469}
Giulio Grassi624f6c62014-02-18 19:42:14 +0100470
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000471BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
472{
473 UdpFactory factory;
474
475 factory.createChannel("::1", "20071");
476
477 factory.createFace(FaceUri("udp://[::1]:20070"),
478 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
479 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
480
481
Junxiao Shi79494162014-04-02 18:25:11 -0700482 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000483 "UdpChannel error: cannot connect or cannot accept connection");
484
Junxiao Shi79494162014-04-02 18:25:11 -0700485 BOOST_REQUIRE(static_cast<bool>(face2));
486 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp6://[::1]:20070");
487 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp6://[::1]:20071");
488 BOOST_CHECK_EQUAL(face2->isLocal(), false);
489 // face1 is not created yet
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000490
491 shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
492 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
493 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
494
495 Interest interest1("ndn:/TpnzGvW9R");
496 Data data1 ("ndn:/KfczhUqVix");
497 data1.setContent(0, 0);
498 Interest interest2("ndn:/QWiIMfj5sL");
499 Data data2 ("ndn:/XNBV796f");
500 data2.setContent(0, 0);
501 Interest interest3("ndn:/QWiIhjgkj5sL");
502 Data data3 ("ndn:/XNBV794f");
503 data3.setContent(0, 0);
504
505
506 ndn::SignatureSha256WithRsa fakeSignature;
507 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
508 reinterpret_cast<const uint8_t*>(0),
509 0));
510
511 // set fake signature on data1 and data2
512 data1.setSignature(fakeSignature);
513 data2.setSignature(fakeSignature);
514 data3.setSignature(fakeSignature);
515
Junxiao Shi79494162014-04-02 18:25:11 -0700516 face2->sendInterest(interest2);
517 face2->sendData (data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000518
Junxiao Shi79494162014-04-02 18:25:11 -0700519 BOOST_CHECK_MESSAGE(limitedIo.run(3,//2 send + 1 listen return
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000520 time::seconds(1)) == LimitedIo::EXCEED_OPS,
521 "UdpChannel error: cannot send or receive Interest/Data packets");
522
Junxiao Shi79494162014-04-02 18:25:11 -0700523 BOOST_REQUIRE(static_cast<bool>(face1));
524 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp6://[::1]:20071");
525 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp6://[::1]:20070");
526 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000527
Junxiao Shi79494162014-04-02 18:25:11 -0700528 face1->sendInterest(interest1);
529 face1->sendData (data1 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000530
Junxiao Shi79494162014-04-02 18:25:11 -0700531 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000532 "UdpChannel error: cannot send or receive Interest/Data packets");
533
534
Junxiao Shi79494162014-04-02 18:25:11 -0700535 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
536 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
537 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
538 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000539
Junxiao Shi79494162014-04-02 18:25:11 -0700540 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
541 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
542 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
543 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000544
545
546
547 //checking if the connection accepting mechanism works properly.
548
Junxiao Shi79494162014-04-02 18:25:11 -0700549 face2->sendData (data3 );
550 face2->sendInterest(interest3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000551
Junxiao Shi79494162014-04-02 18:25:11 -0700552 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000553 "UdpChannel error: cannot send or receive Interest/Data packets");
554
Junxiao Shi79494162014-04-02 18:25:11 -0700555 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
556 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000557
Junxiao Shi79494162014-04-02 18:25:11 -0700558 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
559 BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Giulio Grassi624f6c62014-02-18 19:42:14 +0100560}
561
562BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
563{
564 Interest interest1("ndn:/TpnzGvW9R");
565 Interest interest2("ndn:/QWiIMfj5sL");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100566
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700567 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100568
Giulio Grassi624f6c62014-02-18 19:42:14 +0100569 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
570 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700571
Giulio Grassi624f6c62014-02-18 19:42:14 +0100572 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
573 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700574
Giulio Grassi624f6c62014-02-18 19:42:14 +0100575 channel2->connect("127.0.0.1", "20070",
576 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
577 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700578
579
Junxiao Shi79494162014-04-02 18:25:11 -0700580 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700581 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100582
Junxiao Shi79494162014-04-02 18:25:11 -0700583 BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700584
Giulio Grassi624f6c62014-02-18 19:42:14 +0100585 shared_ptr<UdpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
586 channel3->connect("127.0.0.1", "20070",
587 bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
588 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700589
Giulio Grassi624f6c62014-02-18 19:42:14 +0100590 shared_ptr<UdpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
591
592 BOOST_CHECK_NE(channel3, channel4);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100593
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700594 scheduler::schedule(time::milliseconds(500),
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700595 bind(&UdpChannel::connect, channel4, "127.0.0.1", "20070",
596 // does not work without static_cast
597 static_cast<UdpChannel::FaceCreatedCallback>(
598 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
599 static_cast<UdpChannel::ConnectFailedCallback>(
600 bind(&EndToEndFixture::channel_onConnectFailed, this, _1))));
601
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700602 scheduler::schedule(time::milliseconds(400), bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700603
Junxiao Shi79494162014-04-02 18:25:11 -0700604 BOOST_CHECK_MESSAGE(limitedIo.run(2,// 2 connects
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700605 time::seconds(4)) == LimitedIo::EXCEED_OPS,
606 "UdpChannel error: cannot connect or cannot accept multiple connections");
607
Junxiao Shi79494162014-04-02 18:25:11 -0700608 BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700609
610
Junxiao Shi79494162014-04-02 18:25:11 -0700611 face2->sendInterest(interest1);
612 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700613 "UdpChannel error: cannot send or receive Interest/Data packets");
614
Junxiao Shi79494162014-04-02 18:25:11 -0700615 BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700616
Junxiao Shi79494162014-04-02 18:25:11 -0700617 face3->sendInterest(interest2);
618 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700619 "UdpChannel error: cannot send or receive Interest/Data packets");
620
Junxiao Shi79494162014-04-02 18:25:11 -0700621 //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
622 BOOST_CHECK_EQUAL(faces.size(), 5);
Davide Pesavento126249b2014-03-13 02:42:21 +0100623 BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
624 bind(&EndToEndFixture::channel_onConnectFailedOk, this, _1)),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100625 UdpChannel::Error);
626}
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700627
Giulio Grassi624f6c62014-02-18 19:42:14 +0100628//Test commented because it required to be run in a machine that can resolve ipv6 query
629//BOOST_FIXTURE_TEST_CASE(EndToEndIpv6, EndToEndFixture)
630//{
631// UdpFactory factory = UdpFactory();
632// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700633//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100634// EventId abortEvent =
635// scheduler.scheduleEvent(time::seconds(1),
636// bind(&EndToEndFixture::abortTestCase, this,
637// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700638//
Junxiao Shi79494162014-04-02 18:25:11 -0700639// limitedIoRemaining = 1;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700640//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100641// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
642// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700643//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100644// channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
645// bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700646//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100647// channel2->connect("::1", "20070",
648// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
649// bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
650// g_io.run();
651// g_io.reset();
652// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700653//
Junxiao Shi79494162014-04-02 18:25:11 -0700654// BOOST_REQUIRE(static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700655//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100656// abortEvent =
657// scheduler.scheduleEvent(time::seconds(1),
658// bind(&EndToEndFixture::abortTestCase, this,
659// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700660//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100661// Interest interest1("ndn:/TpnzGvW9R");
662// Data data1 ("ndn:/KfczhUqVix");
663// data1.setContent(0, 0);
664// Interest interest2("ndn:/QWiIMfj5sL");
665// Data data2 ("ndn:/XNBV796f");
666// data2.setContent(0, 0);
667// Interest interest3("ndn:/QWiIhjgkj5sL");
668// Data data3 ("ndn:/XNBV794f");
669// data3.setContent(0, 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700670//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100671// ndn::SignatureSha256WithRsa fakeSignature;
672// fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700673//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100674// // set fake signature on data1 and data2
675// data1.setSignature(fakeSignature);
676// data2.setSignature(fakeSignature);
677// data3.setSignature(fakeSignature);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700678//
Junxiao Shi79494162014-04-02 18:25:11 -0700679// face2->sendInterest(interest2);
680// face2->sendData (data2 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700681//
Junxiao Shi79494162014-04-02 18:25:11 -0700682// limitedIoRemaining = 3; //2 send + 1 listen return
Giulio Grassi624f6c62014-02-18 19:42:14 +0100683// g_io.run();
684// g_io.reset();
685// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700686//
Junxiao Shi79494162014-04-02 18:25:11 -0700687// BOOST_REQUIRE(static_cast<bool>(face1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700688//
Junxiao Shi79494162014-04-02 18:25:11 -0700689// face1->sendInterest(interest1);
690// face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700691//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100692// abortEvent =
693// scheduler.scheduleEvent(time::seconds(1),
694// bind(&EndToEndFixture::abortTestCase, this,
695// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700696// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100697// g_io.run();
698// g_io.reset();
699// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700700//
Junxiao Shi79494162014-04-02 18:25:11 -0700701// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
702// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
703// BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
704// BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700705//
Junxiao Shi79494162014-04-02 18:25:11 -0700706// BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
707// BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
708// BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
709// BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700710//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100711// //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700712//
Junxiao Shi79494162014-04-02 18:25:11 -0700713// face2->sendData (data3 );
714// face2->sendInterest(interest3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700715//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100716// abortEvent =
717// scheduler.scheduleEvent(time::seconds(1),
718// bind(&EndToEndFixture::abortTestCase, this,
719// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700720// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100721// g_io.run();
722// g_io.reset();
723// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700724//
Junxiao Shi79494162014-04-02 18:25:11 -0700725// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
726// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700727//
Junxiao Shi79494162014-04-02 18:25:11 -0700728// BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
729// BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700730//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100731//}
732
733
734//Test commented because it required to be run in a machine that can resolve ipv6 query
735//BOOST_FIXTURE_TEST_CASE(MultipleAcceptsIpv6, EndToEndFixture)
736//{
737// Interest interest1("ndn:/TpnzGvW9R");
738// Interest interest2("ndn:/QWiIMfj5sL");
739// Interest interest3("ndn:/QWiIhjgkj5sL");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700740//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100741// UdpFactory factory = UdpFactory();
742// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700743//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100744// EventId abortEvent =
745// scheduler.scheduleEvent(time::seconds(4),
746// bind(&EndToEndFixture::abortTestCase, this,
747// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700748//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100749// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
750// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700751//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100752// channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
753// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700754//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100755// channel2->connect("::1", "20070",
756// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
757// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700758//
Junxiao Shi79494162014-04-02 18:25:11 -0700759// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100760// g_io.run();
761// g_io.reset();
762// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700763//
Junxiao Shi79494162014-04-02 18:25:11 -0700764// BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700765//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100766// shared_ptr<UdpChannel> channel3 = factory.createChannel("::1", "20072");
767// channel3->connect("::1", "20070",
768// bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
769// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700770//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100771// shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700772//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100773// BOOST_CHECK_NE(channel3, channel4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700774//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100775// scheduler
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700776// .scheduleEvent(time::milliseconds(500),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100777// bind(&UdpChannel::connect, channel4,
778// "::1", "20070",
779// static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
780// channel_onFaceCreated, this, _1)),
781// static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
782// channel_onConnectFailed, this, _1))));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700783//
Junxiao Shi79494162014-04-02 18:25:11 -0700784// limitedIoRemaining = 2; // 2 connects
Giulio Grassi624f6c62014-02-18 19:42:14 +0100785// abortEvent =
786// scheduler.scheduleEvent(time::seconds(4),
787// bind(&EndToEndFixture::abortTestCase, this,
788// "UdpChannel error: cannot connect or cannot accept multiple connections"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700789//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100790// scheduler.scheduleEvent(time::seconds(0.4),
791// bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700792//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100793// g_io.run();
794// g_io.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700795//
Junxiao Shi79494162014-04-02 18:25:11 -0700796// BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700797//
798//
Junxiao Shi79494162014-04-02 18:25:11 -0700799// face2->sendInterest(interest1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100800// abortEvent =
801// scheduler.scheduleEvent(time::seconds(1),
802// bind(&EndToEndFixture::abortTestCase, this,
803// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700804// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100805// g_io.run();
806// g_io.reset();
807// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700808//
Junxiao Shi79494162014-04-02 18:25:11 -0700809// BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700810//
Junxiao Shi79494162014-04-02 18:25:11 -0700811// face3->sendInterest(interest2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100812// abortEvent =
813// scheduler.scheduleEvent(time::seconds(1),
814// bind(&EndToEndFixture::abortTestCase, this,
815// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700816// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100817// g_io.run();
818// g_io.reset();
819// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700820//
Junxiao Shi79494162014-04-02 18:25:11 -0700821// //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
822// BOOST_CHECK_EQUAL(faces.size(), 5);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100823// BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,
824// this,
825// _1),
826// bind(&EndToEndFixture::channel_onConnectFailedOk,
827// this,
828// _1)),
829// UdpChannel::Error);
830//}
831
832
833BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
834{
835 UdpFactory factory = UdpFactory();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100836
Giulio Grassi624f6c62014-02-18 19:42:14 +0100837 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
838 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700839
Giulio Grassi624f6c62014-02-18 19:42:14 +0100840 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
841 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700842
Giulio Grassi624f6c62014-02-18 19:42:14 +0100843 channel2->connect("127.0.0.1", "20070",
844 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
845 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700846
Junxiao Shi79494162014-04-02 18:25:11 -0700847 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700848 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100849
850 BOOST_CHECK_EQUAL(channel2->size(), 1);
851
Junxiao Shi79494162014-04-02 18:25:11 -0700852 BOOST_CHECK(static_cast<bool>(face2));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100853
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700854 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700855 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700856
Junxiao Shi79494162014-04-02 18:25:11 -0700857 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700858 "FaceClosing error: cannot properly close faces");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100859
Junxiao Shi79494162014-04-02 18:25:11 -0700860 BOOST_CHECK(!static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700861
Giulio Grassi624f6c62014-02-18 19:42:14 +0100862 BOOST_CHECK_EQUAL(channel2->size(), 0);
863}
Junxiao Shi79494162014-04-02 18:25:11 -0700864
Giulio Grassi69871f02014-03-09 16:14:44 +0100865BOOST_FIXTURE_TEST_CASE(ClosingIdleFace, EndToEndFixture)
866{
867 Interest interest1("ndn:/TpnzGvW9R");
868 Interest interest2("ndn:/QWiIMfj5sL");
Davide Pesavento126249b2014-03-13 02:42:21 +0100869
Giulio Grassi69871f02014-03-09 16:14:44 +0100870 UdpFactory factory;
Davide Pesavento126249b2014-03-13 02:42:21 +0100871
872 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070",
Giulio Grassi69871f02014-03-09 16:14:44 +0100873 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100874 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071",
Giulio Grassi69871f02014-03-09 16:14:44 +0100875 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100876
Giulio Grassi69871f02014-03-09 16:14:44 +0100877 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
Davide Pesavento126249b2014-03-13 02:42:21 +0100878 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100879
880 channel2->connect("127.0.0.1", "20070",
881 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
882 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi79494162014-04-02 18:25:11 -0700883
884 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100885 "UdpChannel error: cannot connect or cannot accept connection");
886
Junxiao Shi79494162014-04-02 18:25:11 -0700887 face2->sendInterest(interest1);
888 BOOST_CHECK(!face2->isOnDemand());
Giulio Grassi69871f02014-03-09 16:14:44 +0100889
Junxiao Shi79494162014-04-02 18:25:11 -0700890 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100891 time::seconds(1)) == LimitedIo::EXCEED_OPS,
892 "UdpChannel error: cannot send or receive Interest/Data packets");
Junxiao Shi79494162014-04-02 18:25:11 -0700893
894 BOOST_CHECK_EQUAL(faces.size(), 2);
895 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(2)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100896 "Idle face should be still open because has been used recently");
Junxiao Shi79494162014-04-02 18:25:11 -0700897 BOOST_CHECK_EQUAL(faces.size(), 2);
898 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100899 "Closing idle face error: face should be closed by now");
Junxiao Shi79494162014-04-02 18:25:11 -0700900
Giulio Grassi69871f02014-03-09 16:14:44 +0100901 //the face on listen should be closed now
902 BOOST_CHECK_EQUAL(channel1->size(), 0);
Junxiao Shi79494162014-04-02 18:25:11 -0700903 //checking that face2 has not been closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100904 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700905 BOOST_REQUIRE(static_cast<bool>(face2));
906
907 face2->sendInterest(interest2);
908 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100909 time::seconds(1)) == LimitedIo::EXCEED_OPS,
910 "UdpChannel error: cannot send or receive Interest/Data packets");
911 //channel1 should have created a new face by now
912 BOOST_CHECK_EQUAL(channel1->size(), 1);
913 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700914 BOOST_REQUIRE(static_cast<bool>(face1));
915 BOOST_CHECK(face1->isOnDemand());
916
Giulio Grassi69871f02014-03-09 16:14:44 +0100917 channel1->connect("127.0.0.1", "20071",
918 bind(&EndToEndFixture::channel1_onFaceCreatedNoCheck, this, _1),
919 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
920
Junxiao Shi79494162014-04-02 18:25:11 -0700921 BOOST_CHECK_MESSAGE(limitedIo.run(1,//1 connect
Giulio Grassi69871f02014-03-09 16:14:44 +0100922 time::seconds(1)) == LimitedIo::EXCEED_OPS,
923 "UdpChannel error: cannot connect");
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700924
Junxiao Shi79494162014-04-02 18:25:11 -0700925 BOOST_CHECK(!face1->isOnDemand());
926
927 //the connect should have set face1 as permanent face,
Giulio Grassi69871f02014-03-09 16:14:44 +0100928 //but it shouln't have created any additional faces
929 BOOST_CHECK_EQUAL(channel1->size(), 1);
930 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700931 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100932 "Idle face should be still open because it's permanent now");
933 //both faces are permanent, nothing should have changed
934 BOOST_CHECK_EQUAL(channel1->size(), 1);
935 BOOST_CHECK_EQUAL(channel2->size(), 1);
936}
937
Giulio Grassi624f6c62014-02-18 19:42:14 +0100938BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700939
Giulio Grassi624f6c62014-02-18 19:42:14 +0100940} // namespace tests
941} // namespace nfd