blob: 40485ca6fbb5fadc70dba351c222e3aeaa37b281 [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);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700382 BOOST_CHECK_EQUAL(face2->getCounters().getNOutBytes(), 0);
383 BOOST_CHECK_EQUAL(face2->getCounters().getNInBytes(), 0);
Junxiao Shi79494162014-04-02 18:25:11 -0700384 // face1 is not created yet
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700385
Giulio Grassi624f6c62014-02-18 19:42:14 +0100386 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
387 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
388 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700389
Giulio Grassi624f6c62014-02-18 19:42:14 +0100390 Interest interest1("ndn:/TpnzGvW9R");
391 Data data1 ("ndn:/KfczhUqVix");
392 data1.setContent(0, 0);
393 Interest interest2("ndn:/QWiIMfj5sL");
394 Data data2 ("ndn:/XNBV796f");
395 data2.setContent(0, 0);
396 Interest interest3("ndn:/QWiIhjgkj5sL");
397 Data data3 ("ndn:/XNBV794f");
398 data3.setContent(0, 0);
399
400
401 ndn::SignatureSha256WithRsa fakeSignature;
402 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
403 reinterpret_cast<const uint8_t*>(0),
404 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700405
Giulio Grassi624f6c62014-02-18 19:42:14 +0100406 // set fake signature on data1 and data2
407 data1.setSignature(fakeSignature);
408 data2.setSignature(fakeSignature);
409 data3.setSignature(fakeSignature);
410
Junxiao Shi79494162014-04-02 18:25:11 -0700411 face2->sendInterest(interest2);
412 face2->sendData (data2 );
413 face2->sendData (data2 );
414 face2->sendData (data2 );
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700415 size_t nBytesSent2 = interest2.wireEncode().size() + data2.wireEncode().size() * 3;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100416
Junxiao Shi79494162014-04-02 18:25:11 -0700417 BOOST_CHECK_MESSAGE(limitedIo.run(5,//4 send + 1 listen return
Alexander Afanasyev6f5ff632014-03-07 16:40:10 +0000418 time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700419 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100420
Junxiao Shi79494162014-04-02 18:25:11 -0700421 BOOST_REQUIRE(static_cast<bool>(face1));
422 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp4://127.0.0.1:20071");
423 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp4://127.0.0.1:20070");
424 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700425
Junxiao Shi79494162014-04-02 18:25:11 -0700426 face1->sendInterest(interest1);
427 face1->sendInterest(interest1);
428 face1->sendInterest(interest1);
429 face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700430
Junxiao Shi79494162014-04-02 18:25:11 -0700431 BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700432 "UdpChannel error: cannot send or receive Interest/Data packets");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100433
Junxiao Shi79494162014-04-02 18:25:11 -0700434 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
435 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
436 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3);
437 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700438
Junxiao Shi79494162014-04-02 18:25:11 -0700439 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
440 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
441 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
442 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700443
444
445
Giulio Grassi624f6c62014-02-18 19:42:14 +0100446 //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700447
Junxiao Shi79494162014-04-02 18:25:11 -0700448 face2->sendData (data3 );
449 face2->sendInterest(interest3);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700450 nBytesSent2 += data3.wireEncode().size() + interest3.wireEncode().size();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100451
Junxiao Shi79494162014-04-02 18:25:11 -0700452 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700453 "UdpChannel error: cannot send or receive Interest/Data packets");
454
Junxiao Shi79494162014-04-02 18:25:11 -0700455 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
456 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700457
Junxiao Shi79494162014-04-02 18:25:11 -0700458 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
459 BOOST_CHECK_EQUAL(face1_receivedDatas [3].getName(), data3.getName());
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000460
Junxiao Shi79494162014-04-02 18:25:11 -0700461 const FaceCounters& counters1 = face1->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700462 BOOST_CHECK_EQUAL(counters1.getNInInterests() , 2);
463 BOOST_CHECK_EQUAL(counters1.getNInDatas() , 4);
464 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3);
465 BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700466 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesSent2);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000467
Junxiao Shi79494162014-04-02 18:25:11 -0700468 const FaceCounters& counters2 = face2->getCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -0700469 BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3);
470 BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1);
471 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 2);
472 BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 4);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700473 BOOST_CHECK_EQUAL(counters2.getNOutBytes(), nBytesSent2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000474}
Giulio Grassi624f6c62014-02-18 19:42:14 +0100475
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000476BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture)
477{
478 UdpFactory factory;
479
480 factory.createChannel("::1", "20071");
481
482 factory.createFace(FaceUri("udp://[::1]:20070"),
483 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
484 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
485
486
Junxiao Shi79494162014-04-02 18:25:11 -0700487 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000488 "UdpChannel error: cannot connect or cannot accept connection");
489
Junxiao Shi79494162014-04-02 18:25:11 -0700490 BOOST_REQUIRE(static_cast<bool>(face2));
491 BOOST_CHECK_EQUAL(face2->getRemoteUri().toString(), "udp6://[::1]:20070");
492 BOOST_CHECK_EQUAL(face2->getLocalUri().toString(), "udp6://[::1]:20071");
493 BOOST_CHECK_EQUAL(face2->isLocal(), false);
494 // face1 is not created yet
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000495
496 shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
497 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
498 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
499
500 Interest interest1("ndn:/TpnzGvW9R");
501 Data data1 ("ndn:/KfczhUqVix");
502 data1.setContent(0, 0);
503 Interest interest2("ndn:/QWiIMfj5sL");
504 Data data2 ("ndn:/XNBV796f");
505 data2.setContent(0, 0);
506 Interest interest3("ndn:/QWiIhjgkj5sL");
507 Data data3 ("ndn:/XNBV794f");
508 data3.setContent(0, 0);
509
510
511 ndn::SignatureSha256WithRsa fakeSignature;
512 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
513 reinterpret_cast<const uint8_t*>(0),
514 0));
515
516 // set fake signature on data1 and data2
517 data1.setSignature(fakeSignature);
518 data2.setSignature(fakeSignature);
519 data3.setSignature(fakeSignature);
520
Junxiao Shi79494162014-04-02 18:25:11 -0700521 face2->sendInterest(interest2);
522 face2->sendData (data2 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000523
Junxiao Shi79494162014-04-02 18:25:11 -0700524 BOOST_CHECK_MESSAGE(limitedIo.run(3,//2 send + 1 listen return
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000525 time::seconds(1)) == LimitedIo::EXCEED_OPS,
526 "UdpChannel error: cannot send or receive Interest/Data packets");
527
Junxiao Shi79494162014-04-02 18:25:11 -0700528 BOOST_REQUIRE(static_cast<bool>(face1));
529 BOOST_CHECK_EQUAL(face1->getRemoteUri().toString(), "udp6://[::1]:20071");
530 BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "udp6://[::1]:20070");
531 BOOST_CHECK_EQUAL(face1->isLocal(), false);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000532
Junxiao Shi79494162014-04-02 18:25:11 -0700533 face1->sendInterest(interest1);
534 face1->sendData (data1 );
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000535
Junxiao Shi79494162014-04-02 18:25:11 -0700536 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000537 "UdpChannel error: cannot send or receive Interest/Data packets");
538
539
Junxiao Shi79494162014-04-02 18:25:11 -0700540 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
541 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
542 BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
543 BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000544
Junxiao Shi79494162014-04-02 18:25:11 -0700545 BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
546 BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
547 BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
548 BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000549
550
551
552 //checking if the connection accepting mechanism works properly.
553
Junxiao Shi79494162014-04-02 18:25:11 -0700554 face2->sendData (data3 );
555 face2->sendInterest(interest3);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000556
Junxiao Shi79494162014-04-02 18:25:11 -0700557 BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000558 "UdpChannel error: cannot send or receive Interest/Data packets");
559
Junxiao Shi79494162014-04-02 18:25:11 -0700560 BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
561 BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000562
Junxiao Shi79494162014-04-02 18:25:11 -0700563 BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
564 BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Giulio Grassi624f6c62014-02-18 19:42:14 +0100565}
566
567BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
568{
569 Interest interest1("ndn:/TpnzGvW9R");
570 Interest interest2("ndn:/QWiIMfj5sL");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100571
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700572 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100573
Giulio Grassi624f6c62014-02-18 19:42:14 +0100574 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
575 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700576
Giulio Grassi624f6c62014-02-18 19:42:14 +0100577 channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
578 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700579
Giulio Grassi624f6c62014-02-18 19:42:14 +0100580 channel2->connect("127.0.0.1", "20070",
581 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
582 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700583
584
Junxiao Shi79494162014-04-02 18:25:11 -0700585 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700586 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100587
Junxiao Shi79494162014-04-02 18:25:11 -0700588 BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700589
Giulio Grassi624f6c62014-02-18 19:42:14 +0100590 shared_ptr<UdpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
591 channel3->connect("127.0.0.1", "20070",
592 bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
593 bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700594
Giulio Grassi624f6c62014-02-18 19:42:14 +0100595 shared_ptr<UdpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
596
597 BOOST_CHECK_NE(channel3, channel4);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100598
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700599 scheduler::schedule(time::milliseconds(500),
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700600 bind(&UdpChannel::connect, channel4, "127.0.0.1", "20070",
601 // does not work without static_cast
602 static_cast<UdpChannel::FaceCreatedCallback>(
603 bind(&EndToEndFixture::channel_onFaceCreated, this, _1)),
604 static_cast<UdpChannel::ConnectFailedCallback>(
605 bind(&EndToEndFixture::channel_onConnectFailed, this, _1))));
606
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700607 scheduler::schedule(time::milliseconds(400), bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700608
Junxiao Shi79494162014-04-02 18:25:11 -0700609 BOOST_CHECK_MESSAGE(limitedIo.run(2,// 2 connects
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700610 time::seconds(4)) == LimitedIo::EXCEED_OPS,
611 "UdpChannel error: cannot connect or cannot accept multiple connections");
612
Junxiao Shi79494162014-04-02 18:25:11 -0700613 BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700614
615
Junxiao Shi79494162014-04-02 18:25:11 -0700616 face2->sendInterest(interest1);
617 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700618 "UdpChannel error: cannot send or receive Interest/Data packets");
619
Junxiao Shi79494162014-04-02 18:25:11 -0700620 BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700621
Junxiao Shi79494162014-04-02 18:25:11 -0700622 face3->sendInterest(interest2);
623 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(1)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700624 "UdpChannel error: cannot send or receive Interest/Data packets");
625
Junxiao Shi79494162014-04-02 18:25:11 -0700626 //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
627 BOOST_CHECK_EQUAL(faces.size(), 5);
Davide Pesavento126249b2014-03-13 02:42:21 +0100628 BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
629 bind(&EndToEndFixture::channel_onConnectFailedOk, this, _1)),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100630 UdpChannel::Error);
631}
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700632
Giulio Grassi624f6c62014-02-18 19:42:14 +0100633//Test commented because it required to be run in a machine that can resolve ipv6 query
634//BOOST_FIXTURE_TEST_CASE(EndToEndIpv6, EndToEndFixture)
635//{
636// UdpFactory factory = UdpFactory();
637// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700638//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100639// EventId abortEvent =
640// scheduler.scheduleEvent(time::seconds(1),
641// bind(&EndToEndFixture::abortTestCase, this,
642// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700643//
Junxiao Shi79494162014-04-02 18:25:11 -0700644// limitedIoRemaining = 1;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700645//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100646// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
647// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700648//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100649// channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
650// bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700651//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100652// channel2->connect("::1", "20070",
653// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
654// bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
655// g_io.run();
656// g_io.reset();
657// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700658//
Junxiao Shi79494162014-04-02 18:25:11 -0700659// BOOST_REQUIRE(static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700660//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100661// abortEvent =
662// scheduler.scheduleEvent(time::seconds(1),
663// bind(&EndToEndFixture::abortTestCase, this,
664// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700665//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100666// Interest interest1("ndn:/TpnzGvW9R");
667// Data data1 ("ndn:/KfczhUqVix");
668// data1.setContent(0, 0);
669// Interest interest2("ndn:/QWiIMfj5sL");
670// Data data2 ("ndn:/XNBV796f");
671// data2.setContent(0, 0);
672// Interest interest3("ndn:/QWiIhjgkj5sL");
673// Data data3 ("ndn:/XNBV794f");
674// data3.setContent(0, 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700675//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100676// ndn::SignatureSha256WithRsa fakeSignature;
677// fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700678//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100679// // set fake signature on data1 and data2
680// data1.setSignature(fakeSignature);
681// data2.setSignature(fakeSignature);
682// data3.setSignature(fakeSignature);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700683//
Junxiao Shi79494162014-04-02 18:25:11 -0700684// face2->sendInterest(interest2);
685// face2->sendData (data2 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700686//
Junxiao Shi79494162014-04-02 18:25:11 -0700687// limitedIoRemaining = 3; //2 send + 1 listen return
Giulio Grassi624f6c62014-02-18 19:42:14 +0100688// g_io.run();
689// g_io.reset();
690// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700691//
Junxiao Shi79494162014-04-02 18:25:11 -0700692// BOOST_REQUIRE(static_cast<bool>(face1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700693//
Junxiao Shi79494162014-04-02 18:25:11 -0700694// face1->sendInterest(interest1);
695// face1->sendData (data1 );
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700696//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100697// abortEvent =
698// scheduler.scheduleEvent(time::seconds(1),
699// bind(&EndToEndFixture::abortTestCase, this,
700// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700701// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100702// g_io.run();
703// g_io.reset();
704// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700705//
Junxiao Shi79494162014-04-02 18:25:11 -0700706// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1);
707// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1);
708// BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1);
709// BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700710//
Junxiao Shi79494162014-04-02 18:25:11 -0700711// BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2.getName());
712// BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2.getName());
713// BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1.getName());
714// BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700715//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100716// //checking if the connection accepting mechanism works properly.
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700717//
Junxiao Shi79494162014-04-02 18:25:11 -0700718// face2->sendData (data3 );
719// face2->sendInterest(interest3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700720//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100721// abortEvent =
722// scheduler.scheduleEvent(time::seconds(1),
723// bind(&EndToEndFixture::abortTestCase, this,
724// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700725// limitedIoRemaining = 2;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100726// g_io.run();
727// g_io.reset();
728// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700729//
Junxiao Shi79494162014-04-02 18:25:11 -0700730// BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 2);
731// BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 2);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700732//
Junxiao Shi79494162014-04-02 18:25:11 -0700733// BOOST_CHECK_EQUAL(face1_receivedInterests[1].getName(), interest3.getName());
734// BOOST_CHECK_EQUAL(face1_receivedDatas [1].getName(), data3.getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700735//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100736//}
737
738
739//Test commented because it required to be run in a machine that can resolve ipv6 query
740//BOOST_FIXTURE_TEST_CASE(MultipleAcceptsIpv6, EndToEndFixture)
741//{
742// Interest interest1("ndn:/TpnzGvW9R");
743// Interest interest2("ndn:/QWiIMfj5sL");
744// Interest interest3("ndn:/QWiIhjgkj5sL");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700745//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100746// UdpFactory factory = UdpFactory();
747// Scheduler scheduler(g_io); // to limit the amount of time the test may take
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700748//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100749// EventId abortEvent =
750// scheduler.scheduleEvent(time::seconds(4),
751// bind(&EndToEndFixture::abortTestCase, this,
752// "UdpChannel error: cannot connect or cannot accept connection"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700753//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100754// shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070");
755// shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700756//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100757// channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
758// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700759//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100760// channel2->connect("::1", "20070",
761// bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
762// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700763//
Junxiao Shi79494162014-04-02 18:25:11 -0700764// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100765// g_io.run();
766// g_io.reset();
767// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700768//
Junxiao Shi79494162014-04-02 18:25:11 -0700769// BOOST_CHECK_EQUAL(faces.size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700770//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100771// shared_ptr<UdpChannel> channel3 = factory.createChannel("::1", "20072");
772// channel3->connect("::1", "20070",
773// bind(&EndToEndFixture::channel3_onFaceCreated, this, _1),
774// bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700775//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100776// shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700777//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100778// BOOST_CHECK_NE(channel3, channel4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700779//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100780// scheduler
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700781// .scheduleEvent(time::milliseconds(500),
Giulio Grassi624f6c62014-02-18 19:42:14 +0100782// bind(&UdpChannel::connect, channel4,
783// "::1", "20070",
784// static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
785// channel_onFaceCreated, this, _1)),
786// static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
787// channel_onConnectFailed, this, _1))));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700788//
Junxiao Shi79494162014-04-02 18:25:11 -0700789// limitedIoRemaining = 2; // 2 connects
Giulio Grassi624f6c62014-02-18 19:42:14 +0100790// abortEvent =
791// scheduler.scheduleEvent(time::seconds(4),
792// bind(&EndToEndFixture::abortTestCase, this,
793// "UdpChannel error: cannot connect or cannot accept multiple connections"));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700794//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100795// scheduler.scheduleEvent(time::seconds(0.4),
796// bind(&EndToEndFixture::checkFaceList, this, 2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700797//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100798// g_io.run();
799// g_io.reset();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700800//
Junxiao Shi79494162014-04-02 18:25:11 -0700801// BOOST_CHECK_EQUAL(faces.size(), 3);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700802//
803//
Junxiao Shi79494162014-04-02 18:25:11 -0700804// face2->sendInterest(interest1);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100805// abortEvent =
806// scheduler.scheduleEvent(time::seconds(1),
807// bind(&EndToEndFixture::abortTestCase, this,
808// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700809// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100810// g_io.run();
811// g_io.reset();
812// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700813//
Junxiao Shi79494162014-04-02 18:25:11 -0700814// BOOST_CHECK_EQUAL(faces.size(), 4);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700815//
Junxiao Shi79494162014-04-02 18:25:11 -0700816// face3->sendInterest(interest2);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100817// abortEvent =
818// scheduler.scheduleEvent(time::seconds(1),
819// bind(&EndToEndFixture::abortTestCase, this,
820// "UdpChannel error: cannot send or receive Interest/Data packets"));
Junxiao Shi79494162014-04-02 18:25:11 -0700821// limitedIoRemaining = 1;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100822// g_io.run();
823// g_io.reset();
824// scheduler.cancelEvent(abortEvent);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700825//
Junxiao Shi79494162014-04-02 18:25:11 -0700826// //channel1 should have created 2 faces, one when face2 sent an interest, one when face3 sent an interest
827// BOOST_CHECK_EQUAL(faces.size(), 5);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100828// BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,
829// this,
830// _1),
831// bind(&EndToEndFixture::channel_onConnectFailedOk,
832// this,
833// _1)),
834// UdpChannel::Error);
835//}
836
837
838BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture)
839{
840 UdpFactory factory = UdpFactory();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100841
Giulio Grassi624f6c62014-02-18 19:42:14 +0100842 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
843 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700844
Giulio Grassi624f6c62014-02-18 19:42:14 +0100845 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
846 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700847
Giulio Grassi624f6c62014-02-18 19:42:14 +0100848 channel2->connect("127.0.0.1", "20070",
849 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
850 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700851
Junxiao Shi79494162014-04-02 18:25:11 -0700852 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700853 "UdpChannel error: cannot connect or cannot accept connection");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100854
855 BOOST_CHECK_EQUAL(channel2->size(), 1);
856
Junxiao Shi79494162014-04-02 18:25:11 -0700857 BOOST_CHECK(static_cast<bool>(face2));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100858
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700859 // Face::close must be invoked during io run to be counted as an op
Junxiao Shi79494162014-04-02 18:25:11 -0700860 scheduler::schedule(time::milliseconds(100), bind(&Face::close, face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700861
Junxiao Shi79494162014-04-02 18:25:11 -0700862 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700863 "FaceClosing error: cannot properly close faces");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100864
Junxiao Shi79494162014-04-02 18:25:11 -0700865 BOOST_CHECK(!static_cast<bool>(face2));
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700866
Giulio Grassi624f6c62014-02-18 19:42:14 +0100867 BOOST_CHECK_EQUAL(channel2->size(), 0);
868}
Junxiao Shi79494162014-04-02 18:25:11 -0700869
Giulio Grassi69871f02014-03-09 16:14:44 +0100870BOOST_FIXTURE_TEST_CASE(ClosingIdleFace, EndToEndFixture)
871{
872 Interest interest1("ndn:/TpnzGvW9R");
873 Interest interest2("ndn:/QWiIMfj5sL");
Davide Pesavento126249b2014-03-13 02:42:21 +0100874
Giulio Grassi69871f02014-03-09 16:14:44 +0100875 UdpFactory factory;
Davide Pesavento126249b2014-03-13 02:42:21 +0100876
877 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070",
Giulio Grassi69871f02014-03-09 16:14:44 +0100878 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100879 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071",
Giulio Grassi69871f02014-03-09 16:14:44 +0100880 time::seconds(2));
Davide Pesavento126249b2014-03-13 02:42:21 +0100881
Giulio Grassi69871f02014-03-09 16:14:44 +0100882 channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1),
Davide Pesavento126249b2014-03-13 02:42:21 +0100883 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
Giulio Grassi69871f02014-03-09 16:14:44 +0100884
885 channel2->connect("127.0.0.1", "20070",
886 bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
887 bind(&EndToEndFixture::channel2_onConnectFailed, this, _1));
Junxiao Shi79494162014-04-02 18:25:11 -0700888
889 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100890 "UdpChannel error: cannot connect or cannot accept connection");
891
Junxiao Shi79494162014-04-02 18:25:11 -0700892 face2->sendInterest(interest1);
893 BOOST_CHECK(!face2->isOnDemand());
Giulio Grassi69871f02014-03-09 16:14:44 +0100894
Junxiao Shi79494162014-04-02 18:25:11 -0700895 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100896 time::seconds(1)) == LimitedIo::EXCEED_OPS,
897 "UdpChannel error: cannot send or receive Interest/Data packets");
Junxiao Shi79494162014-04-02 18:25:11 -0700898
899 BOOST_CHECK_EQUAL(faces.size(), 2);
900 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(2)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100901 "Idle face should be still open because has been used recently");
Junxiao Shi79494162014-04-02 18:25:11 -0700902 BOOST_CHECK_EQUAL(faces.size(), 2);
903 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_OPS,
Giulio Grassi69871f02014-03-09 16:14:44 +0100904 "Closing idle face error: face should be closed by now");
Junxiao Shi79494162014-04-02 18:25:11 -0700905
Giulio Grassi69871f02014-03-09 16:14:44 +0100906 //the face on listen should be closed now
907 BOOST_CHECK_EQUAL(channel1->size(), 0);
Junxiao Shi79494162014-04-02 18:25:11 -0700908 //checking that face2 has not been closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100909 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700910 BOOST_REQUIRE(static_cast<bool>(face2));
911
912 face2->sendInterest(interest2);
913 BOOST_CHECK_MESSAGE(limitedIo.run(2,//1 send + 1 listen return
Giulio Grassi69871f02014-03-09 16:14:44 +0100914 time::seconds(1)) == LimitedIo::EXCEED_OPS,
915 "UdpChannel error: cannot send or receive Interest/Data packets");
916 //channel1 should have created a new face by now
917 BOOST_CHECK_EQUAL(channel1->size(), 1);
918 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700919 BOOST_REQUIRE(static_cast<bool>(face1));
920 BOOST_CHECK(face1->isOnDemand());
921
Giulio Grassi69871f02014-03-09 16:14:44 +0100922 channel1->connect("127.0.0.1", "20071",
923 bind(&EndToEndFixture::channel1_onFaceCreatedNoCheck, this, _1),
924 bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
925
Junxiao Shi79494162014-04-02 18:25:11 -0700926 BOOST_CHECK_MESSAGE(limitedIo.run(1,//1 connect
Giulio Grassi69871f02014-03-09 16:14:44 +0100927 time::seconds(1)) == LimitedIo::EXCEED_OPS,
928 "UdpChannel error: cannot connect");
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700929
Junxiao Shi79494162014-04-02 18:25:11 -0700930 BOOST_CHECK(!face1->isOnDemand());
931
932 //the connect should have set face1 as permanent face,
Giulio Grassi69871f02014-03-09 16:14:44 +0100933 //but it shouln't have created any additional faces
934 BOOST_CHECK_EQUAL(channel1->size(), 1);
935 BOOST_CHECK_EQUAL(channel2->size(), 1);
Junxiao Shi79494162014-04-02 18:25:11 -0700936 BOOST_CHECK_MESSAGE(limitedIo.run(1, time::seconds(4)) == LimitedIo::EXCEED_TIME,
Giulio Grassi69871f02014-03-09 16:14:44 +0100937 "Idle face should be still open because it's permanent now");
938 //both faces are permanent, nothing should have changed
939 BOOST_CHECK_EQUAL(channel1->size(), 1);
940 BOOST_CHECK_EQUAL(channel2->size(), 1);
941}
942
Giulio Grassi624f6c62014-02-18 19:42:14 +0100943BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700944
Giulio Grassi624f6c62014-02-18 19:42:14 +0100945} // namespace tests
946} // namespace nfd