blob: 68691766cdded0fee9571f29224182126d08b6be [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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
Davide Pesavento6ad890a2015-03-09 03:43:17 +010026#include "face/udp-channel.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010027#include "face/udp-factory.hpp"
Junxiao Shi7e2413b2014-03-02 11:15:09 -070028
Davide Pesavento6ad890a2015-03-09 03:43:17 +010029#include "core/network-interface.hpp"
Junxiao Shi7e2413b2014-03-02 11:15:09 -070030#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070031#include "tests/limited-io.hpp"
Chengyu Fan4381fb62015-01-14 11:37:04 -070032#include "face-history.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010033
34namespace nfd {
35namespace tests {
36
Yukai Tu0a49d342015-09-13 12:54:22 +080037BOOST_AUTO_TEST_SUITE(Face)
38BOOST_FIXTURE_TEST_SUITE(TestUdp, BaseFixture)
39
40using nfd::Face;
Giulio Grassi624f6c62014-02-18 19:42:14 +010041
Steve DiBenedettoef04f272014-06-04 14:28:31 -060042BOOST_AUTO_TEST_CASE(GetChannels)
43{
44 UdpFactory factory;
45 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
46
47 std::vector<shared_ptr<const Channel> > expectedChannels;
48
49 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
50 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
51 expectedChannels.push_back(factory.createChannel("::1", "20071"));
52
53 std::list<shared_ptr<const Channel> > channels = factory.getChannels();
54 for (std::list<shared_ptr<const Channel> >::const_iterator i = channels.begin();
55 i != channels.end(); ++i)
56 {
57 std::vector<shared_ptr<const Channel> >::iterator pos =
58 std::find(expectedChannels.begin(), expectedChannels.end(), *i);
59
60 BOOST_REQUIRE(pos != expectedChannels.end());
61 expectedChannels.erase(pos);
62 }
63
64 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
65}
66
Giulio Grassi624f6c62014-02-18 19:42:14 +010067class FactoryErrorCheck : protected BaseFixture
68{
69public:
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070070 bool
71 isTheSameMulticastEndpoint(const UdpFactory::Error& e) {
Giulio Grassi624f6c62014-02-18 19:42:14 +010072 return strcmp(e.what(),
73 "Cannot create the requested UDP unicast channel, local "
74 "endpoint is already allocated for a UDP multicast face") == 0;
75 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070076
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070077 bool
78 isNotMulticastAddress(const UdpFactory::Error& e) {
Giulio Grassi624f6c62014-02-18 19:42:14 +010079 return strcmp(e.what(),
80 "Cannot create the requested UDP multicast face, "
81 "the multicast group given as input is not a multicast address") == 0;
82 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070083
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070084 bool
85 isTheSameUnicastEndpoint(const UdpFactory::Error& e) {
Giulio Grassi624f6c62014-02-18 19:42:14 +010086 return strcmp(e.what(),
87 "Cannot create the requested UDP multicast face, local "
88 "endpoint is already allocated for a UDP unicast channel") == 0;
89 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -070090
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -070091 bool
92 isLocalEndpointOnDifferentGroup(const UdpFactory::Error& e) {
Giulio Grassi624f6c62014-02-18 19:42:14 +010093 return strcmp(e.what(),
94 "Cannot create the requested UDP multicast face, local "
95 "endpoint is already allocated for a UDP multicast face "
96 "on a different multicast group") == 0;
97 }
98};
Junxiao Shi7e2413b2014-03-02 11:15:09 -070099
Giulio Grassi624f6c62014-02-18 19:42:14 +0100100BOOST_FIXTURE_TEST_CASE(ChannelMapUdp, FactoryErrorCheck)
101{
102 using boost::asio::ip::udp;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700103
Giulio Grassi624f6c62014-02-18 19:42:14 +0100104 UdpFactory factory = UdpFactory();
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700105
Giulio Grassi624f6c62014-02-18 19:42:14 +0100106 //to instantiate multicast face on a specific ip address, change interfaceIp
107 std::string interfaceIp = "0.0.0.0";
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700108
Giulio Grassi624f6c62014-02-18 19:42:14 +0100109 shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
110 shared_ptr<UdpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
111 BOOST_CHECK_EQUAL(channel1, channel1a);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700112 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700113
Giulio Grassi624f6c62014-02-18 19:42:14 +0100114 shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
115 BOOST_CHECK_NE(channel1, channel2);
116
117 shared_ptr<UdpChannel> channel3 = factory.createChannel(interfaceIp, "20070");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700118
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700119 shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20071");
120 BOOST_CHECK_NE(channel2, channel4);
121 BOOST_CHECK_EQUAL(channel4->getUri().toString(), "udp6://[::1]:20071");
122
Giulio Grassi624f6c62014-02-18 19:42:14 +0100123 //same endpoint of a unicast channel
Davide Pesavento94279412015-02-27 01:29:32 +0100124 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp, "224.0.0.1", "20070"),
125 UdpFactory::Error, isTheSameUnicastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700126
Davide Pesavento94279412015-02-27 01:29:32 +0100127 auto multicastFace1 = factory.createMulticastFace(interfaceIp, "224.0.0.1", "20072");
128 auto multicastFace1a = factory.createMulticastFace(interfaceIp, "224.0.0.1", "20072");
Giulio Grassi624f6c62014-02-18 19:42:14 +0100129 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Davide Pesavento94279412015-02-27 01:29:32 +0100130 BOOST_CHECK_EQUAL(multicastFace1->isLocal(), false);
Yukai Tu0a49d342015-09-13 12:54:22 +0800131 BOOST_CHECK_EQUAL(multicastFace1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
Davide Pesavento94279412015-02-27 01:29:32 +0100132 BOOST_CHECK_EQUAL(multicastFace1->isMultiAccess(), true);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700133
Giulio Grassi624f6c62014-02-18 19:42:14 +0100134 //same endpoint of a multicast face
135 BOOST_CHECK_EXCEPTION(factory.createChannel(interfaceIp, "20072"),
Davide Pesavento94279412015-02-27 01:29:32 +0100136 UdpFactory::Error, isTheSameMulticastEndpoint);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700137
Giulio Grassi624f6c62014-02-18 19:42:14 +0100138 //same multicast endpoint, different group
Davide Pesavento94279412015-02-27 01:29:32 +0100139 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp, "224.0.0.42", "20072"),
140 UdpFactory::Error, isLocalEndpointOnDifferentGroup);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100141
Davide Pesavento94279412015-02-27 01:29:32 +0100142 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp, "192.168.10.15", "20025"),
143 UdpFactory::Error, isNotMulticastAddress);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100144
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700145
Giulio Grassi624f6c62014-02-18 19:42:14 +0100146// //Test commented because it required to be run in a machine that can resolve ipv6 query
147// shared_ptr<UdpChannel> channel1v6 = factory.createChannel(//"::1",
148// "fe80::5e96:9dff:fe7d:9c8d%en1",
149// //"fe80::aa54:b2ff:fe08:27b8%wlan0",
150// "20070");
151//
152// //the creation of multicastFace2 works properly. It has been disable because it needs an IP address of
153// //an available network interface (different from the first one used)
154// shared_ptr<MulticastUdpFace> multicastFace2 = factory.createMulticastFace("192.168.1.17",
155// "224.0.0.1",
156// "20073");
157// BOOST_CHECK_NE(multicastFace1, multicastFace2);
158//
159//
160// //ipv6 - work in progress
161// shared_ptr<MulticastUdpFace> multicastFace3 = factory.createMulticastFace("fe80::5e96:9dff:fe7d:9c8d%en1",
162// "FF01:0:0:0:0:0:0:2",
163// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700164//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100165// shared_ptr<MulticastUdpFace> multicastFace4 = factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
166// "FF01:0:0:0:0:0:0:2",
167// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700168//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100169// BOOST_CHECK_EQUAL(multicastFace3, multicastFace4);
170//
171// shared_ptr<MulticastUdpFace> multicastFace5 = factory.createMulticastFace("::1",
172// "FF01:0:0:0:0:0:0:2",
173// "20073");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700174//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100175// BOOST_CHECK_NE(multicastFace3, multicastFace5);
176//
177// //same local ipv6 endpoint for a different multicast group
178// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
179// "FE01:0:0:0:0:0:0:2",
180// "20073"),
181// UdpFactory::Error);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700182//
Giulio Grassi624f6c62014-02-18 19:42:14 +0100183// //same local ipv6 (expect for th port number) endpoint for a different multicast group
184// BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0",
185// "FE01:0:0:0:0:0:0:2",
186// "20075"),
187// UdpFactory::Error);
188//
189// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
190// "FE12:0:0:0:0:0:0:2",
191// "20075"),
192// UdpFactory::Error);
193//
194// //not a multicast ipv6
195// BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff",
196// "A112:0:0:0:0:0:0:2",
197// "20075"),
198// UdpFactory::Error);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100199}
200
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700201class FaceCreateFixture : protected BaseFixture
202{
203public:
204 void
Alexander Afanasyev86bc91a2014-08-28 22:29:16 -0700205 checkError(const std::string& errorActual, const std::string& errorExpected)
206 {
207 BOOST_CHECK_EQUAL(errorActual, errorExpected);
208 }
209
210 void
211 failIfError(const std::string& errorActual)
212 {
213 BOOST_FAIL("No error expected, but got: [" << errorActual << "]");
214 }
215};
216
217BOOST_FIXTURE_TEST_CASE(FaceCreate, FaceCreateFixture)
218{
219 UdpFactory factory = UdpFactory();
220
Chengyu Fan4381fb62015-01-14 11:37:04 -0700221 factory.createFace(FaceUri("udp4://127.0.0.1:6363"),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800222 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
223 bind([]{}),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700224 bind(&FaceCreateFixture::checkError, this, _1,
225 "No channels available to connect to 127.0.0.1:6363"));
Giulio Grassi624f6c62014-02-18 19:42:14 +0100226
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000227 factory.createChannel("127.0.0.1", "20071");
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700228
Giulio Grassi624f6c62014-02-18 19:42:14 +0100229 factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800230 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
231 bind([]{}),
Chengyu Fan4381fb62015-01-14 11:37:04 -0700232 bind(&FaceCreateFixture::failIfError, this, _1));
Yukai Tu375dcb02015-08-01 13:04:43 +0800233 //test the upgrade
234 factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
235 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
236 bind([]{}),
237 bind(&FaceCreateFixture::failIfError, this, _1));
238
239 factory.createFace(FaceUri("udp4://127.0.0.1:20072"),
240 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
241 bind([]{}),
242 bind(&FaceCreateFixture::failIfError, this, _1));
Chengyu Fan4381fb62015-01-14 11:37:04 -0700243}
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700244
Yukai Tu375dcb02015-08-01 13:04:43 +0800245BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800246{
247 UdpFactory factory;
248
249 factory.createChannel("127.0.0.1", "20070");
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800250
251 BOOST_CHECK_THROW(factory.createFace(FaceUri("udp4://127.0.0.1:20070"),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800252 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
253 bind([]{}),
254 bind([]{})),
255 ProtocolFactory::Error);
256}
257
Chengyu Fan4381fb62015-01-14 11:37:04 -0700258class EndToEndIpv4
259{
260public:
261 static const std::string
262 getScheme()
263 {
264 return "udp4";
265 }
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700266
Chengyu Fan4381fb62015-01-14 11:37:04 -0700267 static const std::string
268 getLocalIp()
269 {
270 return "127.0.0.1";
271 }
Giulio Grassi624f6c62014-02-18 19:42:14 +0100272
Chengyu Fan4381fb62015-01-14 11:37:04 -0700273 static const std::string
274 getPort1()
275 {
276 return "20071";
277 }
278
279 static const std::string
280 getPort2()
281 {
282 return "20072";
283 }
284
285 static const std::string
286 getPort3()
287 {
288 return "20073";
289 }
290
291 static const FaceUri
292 getFaceUri1()
293 {
294 return FaceUri("udp4://127.0.0.1:20071");
295 }
296
297 static const FaceUri
298 getFaceUri2()
299 {
300 return FaceUri("udp4://127.0.0.1:20072");
301 }
302
303 static const FaceUri
304 getFaceUri3()
305 {
306 return FaceUri("udp4://127.0.0.1:20073");
307 }
308};
309
310class EndToEndIpv6
311{
312public:
313 static const std::string
314 getScheme()
315 {
316 return "udp6";
317 }
318
319 static const std::string
320 getLocalIp()
321 {
322 return "::1";
323 }
324
325 static const std::string
326 getPort1()
327 {
328 return "20071";
329 }
330
331 static const std::string
332 getPort2()
333 {
334 return "20072";
335 }
336
337 static const std::string
338 getPort3()
339 {
340 return "20073";
341 }
342
343 static const FaceUri
344 getFaceUri1()
345 {
346 return FaceUri("udp6://[::1]:20071");
347 }
348
349 static const FaceUri
350 getFaceUri2()
351 {
352 return FaceUri("udp6://[::1]:20072");
353 }
354
355 static const FaceUri
356 getFaceUri3()
357 {
358 return FaceUri("udp6://[::1]:20073");
359 }
360};
361
362typedef boost::mpl::list<EndToEndIpv4, EndToEndIpv6> EndToEndAddresses;
363
364// end to end communication
365BOOST_AUTO_TEST_CASE_TEMPLATE(EndToEnd, A, EndToEndAddresses)
366{
367 LimitedIo limitedIo;
368 UdpFactory factory;
369
370 shared_ptr<UdpChannel> channel1 = factory.createChannel(A::getLocalIp(), A::getPort1());
371
372 // face1 (on channel1) connects to face2 (on channel2, to be created)
373 shared_ptr<Face> face1;
374 unique_ptr<FaceHistory> history1;
375 factory.createFace(A::getFaceUri2(),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800376 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Chengyu Fan4381fb62015-01-14 11:37:04 -0700377 [&] (shared_ptr<Face> newFace) {
378 face1 = newFace;
379 history1.reset(new FaceHistory(*face1, limitedIo));
380 limitedIo.afterOp();
381 },
382 [] (const std::string& reason) { BOOST_ERROR(reason); });
383
384 limitedIo.run(1, time::seconds(1));
385 BOOST_REQUIRE(face1 != nullptr);
386 BOOST_CHECK_EQUAL(face1->getRemoteUri(), A::getFaceUri2());
387 BOOST_CHECK_EQUAL(face1->getLocalUri(), A::getFaceUri1());
388 BOOST_CHECK_EQUAL(face1->isLocal(), false); // UdpFace is never local
389 BOOST_CHECK_EQUAL(face1->getCounters().getNInBytes(), 0);
390 BOOST_CHECK_EQUAL(face1->getCounters().getNOutBytes(), 0);
391
392 // channel2 creation must be after face1 creation,
393 // otherwise channel2's endpoint would be prohibited
394 shared_ptr<UdpChannel> channel2 = factory.createChannel(A::getLocalIp(), A::getPort2());
395 shared_ptr<Face> face2;
396 unique_ptr<FaceHistory> history2;
397 channel2->listen([&] (shared_ptr<Face> newFace) {
398 BOOST_CHECK(face2 == nullptr);
399 face2 = newFace;
400 history2.reset(new FaceHistory(*face2, limitedIo));
401 limitedIo.afterOp();
402 },
403 [] (const std::string& reason) { BOOST_ERROR(reason); });
404
405 limitedIo.run(1, time::seconds(1));
406 BOOST_CHECK(face2 == nullptr); // face2 shouldn't be created until face1 sends something
407
408 shared_ptr<Interest> interest1 = makeInterest("/I1");
409 shared_ptr<Interest> interest2 = makeInterest("/I2");
410 shared_ptr<Data> data1 = makeData("/D1");
411 shared_ptr<Data> data2 = makeData("/D2");
412
413 // face1 sends to channel2, creates face2
414 face1->sendInterest(*interest1);
415 face1->sendData(*data1);
416 face1->sendData(*data1);
417 face1->sendData(*data1);
418 size_t nBytesSent1 = interest1->wireEncode().size() + 3 * data1->wireEncode().size();
419
420 limitedIo.run(5, time::seconds(1)); // 1 accept, 4 receives
421
422 BOOST_REQUIRE(face2 != nullptr);
423 BOOST_CHECK_EQUAL(face2->getRemoteUri(), A::getFaceUri1());
424 BOOST_CHECK_EQUAL(face2->getLocalUri(), A::getFaceUri2());
425 BOOST_CHECK_EQUAL(face2->isLocal(), false); // UdpFace is never local
426 BOOST_CHECK_EQUAL(face2->getCounters().getNInBytes(), nBytesSent1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700427 BOOST_CHECK_EQUAL(face2->getCounters().getNOutBytes(), 0);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700428
Chengyu Fan4381fb62015-01-14 11:37:04 -0700429 BOOST_REQUIRE_EQUAL(history2->receivedInterests.size(), 1);
430 BOOST_CHECK_EQUAL(history2->receivedInterests.front().getName(), interest1->getName());
431 BOOST_REQUIRE_EQUAL(history2->receivedData.size(), 3);
432 BOOST_CHECK_EQUAL(history2->receivedData.front().getName(), data1->getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700433
Chengyu Fan4381fb62015-01-14 11:37:04 -0700434 // face2 sends to face1
435 face2->sendInterest(*interest2);
436 face2->sendInterest(*interest2);
437 face2->sendInterest(*interest2);
438 face2->sendData(*data2);
439 size_t nBytesSent2 = 3 * interest2->wireEncode().size() + data2->wireEncode().size();
Giulio Grassi624f6c62014-02-18 19:42:14 +0100440
Chengyu Fan4381fb62015-01-14 11:37:04 -0700441 limitedIo.run(4, time::seconds(1)); // 4 receives
Giulio Grassi624f6c62014-02-18 19:42:14 +0100442
Chengyu Fan4381fb62015-01-14 11:37:04 -0700443 BOOST_REQUIRE_EQUAL(history1->receivedInterests.size(), 3);
444 BOOST_CHECK_EQUAL(history1->receivedInterests.front().getName(), interest2->getName());
445 BOOST_REQUIRE_EQUAL(history1->receivedData.size(), 1);
446 BOOST_CHECK_EQUAL(history1->receivedData.front().getName(), data2->getName());
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700447
Chengyu Fan4381fb62015-01-14 11:37:04 -0700448 // counters
Junxiao Shi79494162014-04-02 18:25:11 -0700449 const FaceCounters& counters1 = face1->getCounters();
Chengyu Fan4381fb62015-01-14 11:37:04 -0700450 BOOST_CHECK_EQUAL(counters1.getNInInterests(), 3);
451 BOOST_CHECK_EQUAL(counters1.getNInDatas(), 1);
452 BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 1);
453 BOOST_CHECK_EQUAL(counters1.getNOutDatas(), 3);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700454 BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesSent2);
Chengyu Fan4381fb62015-01-14 11:37:04 -0700455 BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent1);
Alexander Afanasyev7e698e62014-03-07 16:48:35 +0000456
Junxiao Shi79494162014-04-02 18:25:11 -0700457 const FaceCounters& counters2 = face2->getCounters();
Chengyu Fan4381fb62015-01-14 11:37:04 -0700458 BOOST_CHECK_EQUAL(counters2.getNInInterests(), 1);
459 BOOST_CHECK_EQUAL(counters2.getNInDatas(), 3);
460 BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 3);
461 BOOST_CHECK_EQUAL(counters2.getNOutDatas(), 1);
462 BOOST_CHECK_EQUAL(counters2.getNInBytes(), nBytesSent1);
Junxiao Shi5dd26c32014-07-20 23:15:14 -0700463 BOOST_CHECK_EQUAL(counters2.getNOutBytes(), nBytesSent2);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000464}
Giulio Grassi624f6c62014-02-18 19:42:14 +0100465
Chengyu Fan4381fb62015-01-14 11:37:04 -0700466// channel accepting multiple incoming connections
467BOOST_AUTO_TEST_CASE_TEMPLATE(MultipleAccepts, A, EndToEndAddresses)
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000468{
Chengyu Fan4381fb62015-01-14 11:37:04 -0700469 LimitedIo limitedIo;
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000470 UdpFactory factory;
471
Chengyu Fan4381fb62015-01-14 11:37:04 -0700472 // channel1 is listening
473 shared_ptr<UdpChannel> channel1 = factory.createChannel(A::getLocalIp(), A::getPort1());
474 std::vector<shared_ptr<Face>> faces1;
475 channel1->listen([&] (shared_ptr<Face> newFace) {
476 faces1.push_back(newFace);
477 limitedIo.afterOp();
478 },
479 [] (const std::string& reason) { BOOST_ERROR(reason); });
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000480
Chengyu Fan4381fb62015-01-14 11:37:04 -0700481 // face2 (on channel2) connects to channel1
482 shared_ptr<UdpChannel> channel2 = factory.createChannel(A::getLocalIp(), A::getPort2());
483 BOOST_CHECK_NE(channel1, channel2);
484 shared_ptr<Face> face2;
485 boost::asio::ip::address ipAddress2 = boost::asio::ip::address::from_string(A::getLocalIp());
486 udp::Endpoint endpoint2(ipAddress2, boost::lexical_cast<uint16_t>(A::getPort1()));
487 channel2->connect(endpoint2,
Yukai Tu375dcb02015-08-01 13:04:43 +0800488 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Chengyu Fan4381fb62015-01-14 11:37:04 -0700489 [&] (shared_ptr<Face> newFace) {
490 face2 = newFace;
491 limitedIo.afterOp();
492 },
493 [] (const std::string& reason) { BOOST_ERROR(reason); });
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000494
Chengyu Fan4381fb62015-01-14 11:37:04 -0700495 limitedIo.run(1, time::seconds(1)); // 1 create (on channel2)
496 BOOST_REQUIRE(face2 != nullptr);
497 BOOST_CHECK_EQUAL(faces1.size(), 0); // channel1 won't create face until face2 sends something
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000498
Chengyu Fan4381fb62015-01-14 11:37:04 -0700499 // face3 (on channel3) connects to channel1
500 shared_ptr<UdpChannel> channel3 = factory.createChannel(A::getLocalIp(), A::getPort3());
501 BOOST_CHECK_NE(channel1, channel3);
502 shared_ptr<Face> face3;
503 boost::asio::ip::address ipAddress3 = boost::asio::ip::address::from_string(A::getLocalIp());
504 udp::Endpoint endpoint3(ipAddress3, boost::lexical_cast<uint16_t>(A::getPort1()));
505 channel3->connect(endpoint3,
Yukai Tu375dcb02015-08-01 13:04:43 +0800506 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Chengyu Fan4381fb62015-01-14 11:37:04 -0700507 [&] (shared_ptr<Face> newFace) {
508 face3 = newFace;
509 limitedIo.afterOp();
510 },
511 [] (const std::string& reason) { BOOST_ERROR(reason); });
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000512
Chengyu Fan4381fb62015-01-14 11:37:04 -0700513 limitedIo.run(1, time::seconds(1)); // 1 create (on channel3)
514 BOOST_REQUIRE(face3 != nullptr);
515 BOOST_CHECK_EQUAL(faces1.size(), 0); // channel1 won't create face until face3 sends something
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000516
Chengyu Fan4381fb62015-01-14 11:37:04 -0700517 // face2 sends to channel1
518 shared_ptr<Interest> interest2 = makeInterest("/I2");
519 face2->sendInterest(*interest2);
520 limitedIo.run(1, time::milliseconds(100)); // 1 accept (on channel1)
521 BOOST_REQUIRE_EQUAL(faces1.size(), 1);
522 BOOST_CHECK_EQUAL(channel1->size(), 1);
523 BOOST_CHECK_EQUAL(faces1.at(0)->getRemoteUri(), A::getFaceUri2());
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000524
Chengyu Fan4381fb62015-01-14 11:37:04 -0700525 // face3 sends to channel1
526 shared_ptr<Data> data3 = makeData("/D3");
527 face3->sendData(*data3);
528 limitedIo.run(1, time::milliseconds(100)); // 1 accept (on channel1)
529 BOOST_REQUIRE_EQUAL(faces1.size(), 2);
530 BOOST_CHECK_EQUAL(channel1->size(), 2);
531 BOOST_CHECK_EQUAL(faces1.at(1)->getRemoteUri(), A::getFaceUri3());
Giulio Grassi624f6c62014-02-18 19:42:14 +0100532}
533
Chengyu Fan4381fb62015-01-14 11:37:04 -0700534// manually close a face
535BOOST_AUTO_TEST_CASE_TEMPLATE(ManualClose, A, EndToEndAddresses)
Giulio Grassi624f6c62014-02-18 19:42:14 +0100536{
Chengyu Fan4381fb62015-01-14 11:37:04 -0700537 LimitedIo limitedIo;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700538 UdpFactory factory;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100539
Chengyu Fan4381fb62015-01-14 11:37:04 -0700540 shared_ptr<UdpChannel> channel1 = factory.createChannel(A::getLocalIp(), A::getPort1());
541 shared_ptr<Face> face1;
542 unique_ptr<FaceHistory> history1;
543 factory.createFace(A::getFaceUri2(),
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800544 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Chengyu Fan4381fb62015-01-14 11:37:04 -0700545 [&] (shared_ptr<Face> newFace) {
546 face1 = newFace;
547 history1.reset(new FaceHistory(*face1, limitedIo));
548 limitedIo.afterOp();
549 },
550 [] (const std::string& reason) { BOOST_ERROR(reason); });
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700551
Chengyu Fan4381fb62015-01-14 11:37:04 -0700552 limitedIo.run(1, time::milliseconds(100));
553 BOOST_REQUIRE(face1 != nullptr);
554 BOOST_CHECK_EQUAL(channel1->size(), 1);
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700555
Chengyu Fan4381fb62015-01-14 11:37:04 -0700556 face1->close();
557 getGlobalIoService().poll();
558 BOOST_CHECK_EQUAL(history1->failures.size(), 1);
Giulio Grassi69871f02014-03-09 16:14:44 +0100559 BOOST_CHECK_EQUAL(channel1->size(), 0);
Chengyu Fan4381fb62015-01-14 11:37:04 -0700560}
Junxiao Shi79494162014-04-02 18:25:11 -0700561
Chengyu Fan4381fb62015-01-14 11:37:04 -0700562// automatically close an idle incoming face
563BOOST_AUTO_TEST_CASE_TEMPLATE(IdleClose, A, EndToEndAddresses)
564{
565 LimitedIo limitedIo;
566 UdpFactory factory;
567
568 // channel1 is listening
569 shared_ptr<UdpChannel> channel1 = factory.createChannel(A::getLocalIp(), A::getPort1(),
570 time::seconds(2));
571 shared_ptr<Face> face1;
572 unique_ptr<FaceHistory> history1;
573 channel1->listen([&] (shared_ptr<Face> newFace) {
574 BOOST_CHECK(face1 == nullptr);
575 face1 = newFace;
576 history1.reset(new FaceHistory(*face1, limitedIo));
577 limitedIo.afterOp();
578 },
579 [] (const std::string& reason) { BOOST_ERROR(reason); });
580
581 // face2 (on channel2) connects to channel1
582 shared_ptr<UdpChannel> channel2 = factory.createChannel(A::getLocalIp(), A::getPort2(),
583 time::seconds(2));
584 shared_ptr<Face> face2;
585 unique_ptr<FaceHistory> history2;
586 boost::asio::ip::address ipAddress = boost::asio::ip::address::from_string(A::getLocalIp());
587 udp::Endpoint endpoint(ipAddress, boost::lexical_cast<uint16_t>(A::getPort1()));
588 channel2->connect(endpoint,
Yukai Tu375dcb02015-08-01 13:04:43 +0800589 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Chengyu Fan4381fb62015-01-14 11:37:04 -0700590 [&] (shared_ptr<Face> newFace) {
591 face2 = newFace;
592 history2.reset(new FaceHistory(*face2, limitedIo));
593 limitedIo.afterOp();
594 },
595 [] (const std::string& reason) { BOOST_ERROR(reason); });
596
597 limitedIo.run(1, time::milliseconds(100)); // 1 create (on channel2)
598 BOOST_REQUIRE(face2 != nullptr);
Yukai Tu731f0d72015-07-04 11:14:44 +0800599 BOOST_CHECK_EQUAL(face2->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
Davide Pesavento94279412015-02-27 01:29:32 +0100600 BOOST_CHECK_EQUAL(face2->isMultiAccess(), false);
Chengyu Fan4381fb62015-01-14 11:37:04 -0700601
602 // face2 sends to channel1, creates face1
603 shared_ptr<Interest> interest2 = makeInterest("/I2");
604 face2->sendInterest(*interest2);
605
606 limitedIo.run(2, time::seconds(1)); // 1 accept (on channel1), 1 receive (on face1)
Giulio Grassi69871f02014-03-09 16:14:44 +0100607 BOOST_CHECK_EQUAL(channel1->size(), 1);
Chengyu Fan4381fb62015-01-14 11:37:04 -0700608 BOOST_REQUIRE(face1 != nullptr);
Yukai Tu731f0d72015-07-04 11:14:44 +0800609 BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
Davide Pesavento94279412015-02-27 01:29:32 +0100610 BOOST_CHECK_EQUAL(face1->isMultiAccess(), false);
Junxiao Shi79494162014-04-02 18:25:11 -0700611
Chengyu Fan4381fb62015-01-14 11:37:04 -0700612 limitedIo.defer(time::seconds(1));
613 BOOST_CHECK_EQUAL(history1->failures.size(), 0); // face1 is not idle
614 BOOST_CHECK_EQUAL(history2->failures.size(), 0); // face2 is outgoing face and never closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100615
Chengyu Fan4381fb62015-01-14 11:37:04 -0700616 limitedIo.defer(time::seconds(4));
617 BOOST_CHECK_EQUAL(history1->failures.size(), 1); // face1 is idle and automatically closed
618 BOOST_CHECK_EQUAL(channel1->size(), 0);
619 BOOST_CHECK_EQUAL(history2->failures.size(), 0); // face2 is outgoing face and never closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100620}
621
Alexander Afanasyev70aaf8a2014-12-13 00:44:22 -0800622class FakeNetworkInterfaceFixture : public BaseFixture
623{
624public:
625 FakeNetworkInterfaceFixture()
626 {
627 using namespace boost::asio::ip;
628
629 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
630
631 fakeInterfaces->push_back(
632 NetworkInterfaceInfo {0, "eth0",
633 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
634 {address_v4::from_string("0.0.0.0")},
635 {address_v6::from_string("::")},
636 address_v4(),
637 IFF_UP});
638 fakeInterfaces->push_back(
639 NetworkInterfaceInfo {1, "eth0",
640 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
641 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
642 {},
643 address_v4::from_string("192.168.2.255"),
644 0});
645 fakeInterfaces->push_back(
646 NetworkInterfaceInfo {2, "eth1",
647 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
648 {address_v4::from_string("198.51.100.1")},
649 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
650 address_v4::from_string("198.51.100.255"),
651 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
652
653 setDebugNetworkInterfaces(fakeInterfaces);
654 }
655
656 ~FakeNetworkInterfaceFixture()
657 {
658 setDebugNetworkInterfaces(nullptr);
659 }
660};
661
662BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
663{
664 using namespace boost::asio::ip;
665
666 UdpFactory factory;
667 factory.prohibitEndpoint(udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
668 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
669 BOOST_CHECK((factory.m_prohibitedEndpoints ==
670 std::set<udp::Endpoint> {
671 udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
672 }));
673
674 factory.m_prohibitedEndpoints.clear();
675 factory.prohibitEndpoint(udp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
676 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
677 BOOST_CHECK((factory.m_prohibitedEndpoints ==
678 std::set<udp::Endpoint> {
679 udp::Endpoint(address_v6::from_string("2001:db8::1"), 2048),
680 }));
681
682 factory.m_prohibitedEndpoints.clear();
683 factory.prohibitEndpoint(udp::Endpoint(address_v4(), 1024));
684 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 6);
685 BOOST_CHECK((factory.m_prohibitedEndpoints ==
686 std::set<udp::Endpoint> {
687 udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
688 udp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
689 udp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
690 udp::Endpoint(address_v4::from_string("198.51.100.255"), 1024),
691 udp::Endpoint(address_v4::from_string("255.255.255.255"), 1024),
692 udp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
693 }));
694
695 factory.m_prohibitedEndpoints.clear();
696 factory.prohibitEndpoint(udp::Endpoint(address_v6(), 2048));
697 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
698 BOOST_CHECK((factory.m_prohibitedEndpoints ==
699 std::set<udp::Endpoint> {
700 udp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
701 udp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
702 udp::Endpoint(address_v6::from_string("::"), 2048),
703 }));
704}
705
Yukai Tu0a49d342015-09-13 12:54:22 +0800706BOOST_AUTO_TEST_SUITE_END() // TestUdp
707BOOST_AUTO_TEST_SUITE_END() // Face
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700708
Giulio Grassi624f6c62014-02-18 19:42:14 +0100709} // namespace tests
710} // namespace nfd