blob: 265bf97f3b6a9c984ac66350ffc79f1d795ad0ed [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi38b24c72017-01-05 02:59:31 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shicde37ad2015-12-24 01:02:05 -07004 * 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.
10 *
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/>.
24 */
25
26#include "face/udp-factory.hpp"
27
Eric Newberry42602412016-08-27 09:33:18 -070028#include "factory-test-common.hpp"
Junxiao Shi64d99f22017-01-21 23:06:36 +000029#include "face-system-fixture.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030#include "tests/limited-io.hpp"
Junxiao Shic31080d2017-01-24 15:10:12 +000031#include <boost/algorithm/string/replace.hpp>
32#include <boost/range/algorithm.hpp>
Junxiao Shicde37ad2015-12-24 01:02:05 -070033
34namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000035namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070036namespace tests {
37
Junxiao Shi64d99f22017-01-21 23:06:36 +000038using namespace nfd::tests;
39
Junxiao Shicde37ad2015-12-24 01:02:05 -070040BOOST_AUTO_TEST_SUITE(Face)
41BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, BaseFixture)
42
43using nfd::Face;
44
Junxiao Shi64d99f22017-01-21 23:06:36 +000045BOOST_FIXTURE_TEST_SUITE(ProcessConfig, FaceSystemFixture)
46
47BOOST_AUTO_TEST_CASE(Channels)
48{
49 const std::string CONFIG = R"CONFIG(
50 face_system
51 {
52 udp
53 {
54 port 7001
55 enable_v4 yes
56 enable_v6 yes
57 idle_timeout 30
58 mcast no
59 }
60 }
61 )CONFIG";
62
63 parseConfig(CONFIG, true);
64 parseConfig(CONFIG, false);
65
66 auto& factory = this->getFactoryById<UdpFactory>("udp");
67 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"});
68}
69
70BOOST_AUTO_TEST_CASE(ChannelV4)
71{
72 const std::string CONFIG = R"CONFIG(
73 face_system
74 {
75 udp
76 {
77 port 7001
78 enable_v4 yes
79 enable_v6 no
80 mcast no
81 }
82 }
83 )CONFIG";
84
85 parseConfig(CONFIG, true);
86 parseConfig(CONFIG, false);
87
88 auto& factory = this->getFactoryById<UdpFactory>("udp");
89 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"});
90}
91
92BOOST_AUTO_TEST_CASE(ChannelV6)
93{
94 const std::string CONFIG = R"CONFIG(
95 face_system
96 {
97 udp
98 {
99 port 7001
100 enable_v4 no
101 enable_v6 yes
102 mcast no
103 }
104 }
105 )CONFIG";
106
107 parseConfig(CONFIG, true);
108 parseConfig(CONFIG, false);
109
110 auto& factory = this->getFactoryById<UdpFactory>("udp");
111 checkChannelListEqual(factory, {"udp6://[::]:7001"});
112}
113
114class UdpMcastConfigFixture : public FaceSystemFixture
115{
116protected:
117 UdpMcastConfigFixture()
118 {
119 for (const auto& netif : listNetworkInterfaces()) {
120 if (netif.isUp() && netif.isMulticastCapable() && !netif.ipv4Addresses.empty()) {
121 netifs.push_back(netif);
122 }
123 }
124 }
125
126 std::vector<const Face*>
127 listUdpMcastFaces() const
128 {
129 return this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS);
130 }
131
132 size_t
133 countUdpMcastFaces() const
134 {
135 return this->listUdpMcastFaces().size();
136 }
137
Junxiao Shic31080d2017-01-24 15:10:12 +0000138 /** \brief determine whether a UDP multicast face is created on \p netif
139 */
140 static bool
141 isFaceOnNetif(const Face& face, const NetworkInterfaceInfo& netif)
142 {
143 auto ip = boost::asio::ip::address_v4::from_string(face.getLocalUri().getHost());
144 return boost::count(netif.ipv4Addresses, ip) > 0;
145 }
146
Junxiao Shi64d99f22017-01-21 23:06:36 +0000147protected:
148 /** \brief MulticastUdpTransport-capable network interfaces
149 */
150 std::vector<NetworkInterfaceInfo> netifs;
151};
152
153#define SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(n) \
154 do { \
155 if (this->netifs.size() < (n)) { \
156 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
157 " or more MulticastUdpTransport-capable network interfaces"); \
158 return; \
159 } \
160 } while (false)
161
162BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpMcastConfigFixture)
163{
164#ifdef __linux__
165 // need superuser privilege for creating multicast faces on linux
166 SKIP_IF_NOT_SUPERUSER();
167#endif // __linux__
168
169 const std::string CONFIG_WITH_MCAST = R"CONFIG(
170 face_system
171 {
172 udp
173 {
174 mcast yes
175 }
176 }
177 )CONFIG";
178 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
179 face_system
180 {
181 udp
182 {
183 mcast no
184 }
185 }
186 )CONFIG";
187
188 parseConfig(CONFIG_WITHOUT_MCAST, false);
189 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), 0);
190
191 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
192
193 parseConfig(CONFIG_WITH_MCAST, false);
194 g_io.poll();
195 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), netifs.size());
196
197 parseConfig(CONFIG_WITHOUT_MCAST, false);
198 g_io.poll();
199 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), 0);
200}
201
202BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpoint, UdpMcastConfigFixture)
203{
204#ifdef __linux__
205 // need superuser privilege for creating multicast faces on linux
206 SKIP_IF_NOT_SUPERUSER();
207#endif // __linux__
208 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
209
210 const std::string CONFIG1 = R"CONFIG(
211 face_system
212 {
213 udp
214 {
215 mcast_group 239.66.30.1
216 mcast_port 7011
217 }
218 }
219 )CONFIG";
220 const std::string CONFIG2 = R"CONFIG(
221 face_system
222 {
223 udp
224 {
225 mcast_group 239.66.30.2
226 mcast_port 7012
227 }
228 }
229 )CONFIG";
230
231 parseConfig(CONFIG1, false);
232 auto udpMcastFaces = this->listUdpMcastFaces();
233 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifs.size());
234 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(),
235 FaceUri("udp4://239.66.30.1:7011"));
236
237 parseConfig(CONFIG2, false);
238 g_io.poll();
239 udpMcastFaces = this->listUdpMcastFaces();
240 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifs.size());
241 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(),
242 FaceUri("udp4://239.66.30.2:7012"));
243}
244
Junxiao Shic31080d2017-01-24 15:10:12 +0000245BOOST_FIXTURE_TEST_CASE(Whitelist, UdpMcastConfigFixture)
246{
247#ifdef __linux__
248 // need superuser privilege for creating multicast faces on linux
249 SKIP_IF_NOT_SUPERUSER();
250#endif // __linux__
251 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
252
253 std::string CONFIG = R"CONFIG(
254 face_system
255 {
256 udp
257 {
258 whitelist
259 {
260 ifname %ifname
261 }
262 }
263 }
264 )CONFIG";
265 boost::replace_first(CONFIG, "%ifname", netifs.front().name);
266
267 parseConfig(CONFIG, false);
268 auto udpMcastFaces = this->listUdpMcastFaces();
269 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1);
270 BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.front()));
271}
272
273BOOST_FIXTURE_TEST_CASE(Blacklist, UdpMcastConfigFixture)
274{
275#ifdef __linux__
276 // need superuser privilege for creating multicast faces on linux
277 SKIP_IF_NOT_SUPERUSER();
278#endif // __linux__
279 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
280
281 std::string CONFIG = R"CONFIG(
282 face_system
283 {
284 udp
285 {
286 blacklist
287 {
288 ifname %ifname
289 }
290 }
291 }
292 )CONFIG";
293 boost::replace_first(CONFIG, "%ifname", netifs.front().name);
294
295 parseConfig(CONFIG, false);
296 auto udpMcastFaces = this->listUdpMcastFaces();
297 BOOST_CHECK_EQUAL(udpMcastFaces.size(), netifs.size() - 1);
298 BOOST_CHECK_EQUAL(boost::count_if(udpMcastFaces, [=] (const Face* face) {
299 return isFaceOnNetif(*face, netifs.front());
300 }), 0);
301}
302
303BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpMcastConfigFixture)
304{
305#ifdef __linux__
306 // need superuser privilege for creating multicast faces on linux
307 SKIP_IF_NOT_SUPERUSER();
308#endif // __linux__
309 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2);
310
311 std::string CONFIG1 = R"CONFIG(
312 face_system
313 {
314 udp
315 {
316 whitelist
317 {
318 ifname %ifname
319 }
320 }
321 }
322 )CONFIG";
323 std::string CONFIG2 = CONFIG1;
324 boost::replace_first(CONFIG1, "%ifname", netifs.front().name);
325 boost::replace_first(CONFIG2, "%ifname", netifs.back().name);
326
327 parseConfig(CONFIG1, false);
328 auto udpMcastFaces = this->listUdpMcastFaces();
329 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1);
330 BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.front()));
331
332 parseConfig(CONFIG2, false);
333 g_io.poll();
334 udpMcastFaces = this->listUdpMcastFaces();
335 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1);
336 BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.back()));
337}
338
Junxiao Shi64d99f22017-01-21 23:06:36 +0000339BOOST_AUTO_TEST_CASE(Omitted)
340{
341 const std::string CONFIG = R"CONFIG(
342 face_system
343 {
344 }
345 )CONFIG";
346
347 parseConfig(CONFIG, true);
348 parseConfig(CONFIG, false);
349
350 auto& factory = this->getFactoryById<UdpFactory>("udp");
351 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
352 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
353}
354
355BOOST_AUTO_TEST_CASE(BadIdleTimeout)
356{
357 const std::string CONFIG = R"CONFIG(
358 face_system
359 {
360 udp
361 {
362 idle_timeout hello
363 }
364 }
365 )CONFIG";
366
367 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
368 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
369}
370
371BOOST_AUTO_TEST_CASE(BadMcast)
372{
373 const std::string CONFIG = R"CONFIG(
374 face_system
375 {
376 udp
377 {
378 mcast hello
379 }
380 }
381 )CONFIG";
382
383 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
384 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
385}
386
387BOOST_AUTO_TEST_CASE(BadMcastGroup)
388{
389 const std::string CONFIG = R"CONFIG(
390 face_system
391 {
392 udp
393 {
394 mcast_group hello
395 }
396 }
397 )CONFIG";
398
399 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
400 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
401}
402
403BOOST_AUTO_TEST_CASE(BadMcastGroupV4Unicast)
404{
405 const std::string CONFIG = R"CONFIG(
406 face_system
407 {
408 udp
409 {
410 mcast_group 10.0.0.1
411 }
412 }
413 )CONFIG";
414
415 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
416 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
417}
418
419BOOST_AUTO_TEST_CASE(BadMcastGroupV6)
420{
421 const std::string CONFIG = R"CONFIG(
422 face_system
423 {
424 udp
425 {
426 mcast_group ff00::1
427 }
428 }
429 )CONFIG";
430
431 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
432 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
433}
434
435BOOST_AUTO_TEST_CASE(AllDisabled)
436{
437 const std::string CONFIG = R"CONFIG(
438 face_system
439 {
440 udp
441 {
442 enable_v4 no
443 enable_v6 no
444 mcast no
445 }
446 }
447 )CONFIG";
448
449 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
450 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
451}
452
453BOOST_AUTO_TEST_CASE(UnknownOption)
454{
455 const std::string CONFIG = R"CONFIG(
456 face_system
457 {
458 udp
459 {
460 hello
461 }
462 }
463 )CONFIG";
464
465 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
466 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
467}
468
469BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
470
Junxiao Shicde37ad2015-12-24 01:02:05 -0700471BOOST_AUTO_TEST_CASE(GetChannels)
472{
473 UdpFactory factory;
474 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
475
476 std::vector<shared_ptr<const Channel>> expectedChannels;
477 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
478 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
479 expectedChannels.push_back(factory.createChannel("::1", "20071"));
480
481 for (const auto& i : factory.getChannels()) {
482 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i);
483 BOOST_REQUIRE(pos != expectedChannels.end());
484 expectedChannels.erase(pos);
485 }
486
487 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
488}
489
Weiwei Liu72cee942016-02-04 16:49:19 -0700490BOOST_AUTO_TEST_CASE(CreateChannel)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700491{
Weiwei Liu72cee942016-02-04 16:49:19 -0700492 UdpFactory factory;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700493
Weiwei Liu72cee942016-02-04 16:49:19 -0700494 auto channel1 = factory.createChannel("127.0.0.1", "20070");
495 auto channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700496 BOOST_CHECK_EQUAL(channel1, channel1a);
497 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
498
Weiwei Liu72cee942016-02-04 16:49:19 -0700499 auto channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700500 BOOST_CHECK_NE(channel1, channel2);
501
Weiwei Liu72cee942016-02-04 16:49:19 -0700502 auto channel3 = factory.createChannel("::1", "20071");
503 BOOST_CHECK_NE(channel2, channel3);
504 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700505
Weiwei Liu72cee942016-02-04 16:49:19 -0700506 // createChannel with multicast address
507 BOOST_CHECK_EXCEPTION(factory.createChannel("224.0.0.1", "20070"), UdpFactory::Error,
508 [] (const UdpFactory::Error& e) {
509 return strcmp(e.what(),
510 "createChannel is only for unicast channels. The provided endpoint "
511 "is multicast. Use createMulticastFace to create a multicast face") == 0;
512 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700513
Weiwei Liu72cee942016-02-04 16:49:19 -0700514 // createChannel with a local endpoint that has already been allocated for a UDP multicast face
515 auto multicastFace = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20072");
516 BOOST_CHECK_EXCEPTION(factory.createChannel("127.0.0.1", "20072"), UdpFactory::Error,
517 [] (const UdpFactory::Error& e) {
518 return strcmp(e.what(),
519 "Cannot create the requested UDP unicast channel, local "
520 "endpoint is already allocated for a UDP multicast face") == 0;
521 });
522}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700523
Weiwei Liu72cee942016-02-04 16:49:19 -0700524BOOST_AUTO_TEST_CASE(CreateMulticastFace)
525{
Weiwei Liu72cee942016-02-04 16:49:19 -0700526 UdpFactory factory;
527
528 auto multicastFace1 = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070");
529 auto multicastFace1a = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700530 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
531
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200532 // createMulticastFace with a local endpoint that is already used by a channel
Weiwei Liu72cee942016-02-04 16:49:19 -0700533 auto channel = factory.createChannel("127.0.0.1", "20071");
534 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20071"), UdpFactory::Error,
535 [] (const UdpFactory::Error& e) {
536 return strcmp(e.what(),
537 "Cannot create the requested UDP multicast face, local "
538 "endpoint is already allocated for a UDP unicast channel") == 0;
539 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700540
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200541 // createMulticastFace with a local endpoint that is already
542 // used by a multicast face on a different multicast group
Weiwei Liu72cee942016-02-04 16:49:19 -0700543 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.42", "20070"), UdpFactory::Error,
544 [] (const UdpFactory::Error& e) {
545 return strcmp(e.what(),
546 "Cannot create the requested UDP multicast face, local "
547 "endpoint is already allocated for a UDP multicast face "
548 "on a different multicast group") == 0;
549 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700550
Weiwei Liu72cee942016-02-04 16:49:19 -0700551 // createMulticastFace with an IPv4 unicast address
552 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "192.168.10.15", "20072"), UdpFactory::Error,
553 [] (const UdpFactory::Error& e) {
554 return strcmp(e.what(),
555 "Cannot create the requested UDP multicast face, "
556 "the multicast group given as input is not a multicast address") == 0;
557 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700558
Weiwei Liu72cee942016-02-04 16:49:19 -0700559 // createMulticastFace with an IPv6 multicast address
560 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("::1", "ff01::114", "20073"), UdpFactory::Error,
561 [] (const UdpFactory::Error& e) {
562 return strcmp(e.what(),
563 "IPv6 multicast is not supported yet. Please provide an IPv4 "
564 "address") == 0;
565 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700566
Weiwei Liu72cee942016-02-04 16:49:19 -0700567 // createMulticastFace with different local and remote port numbers
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200568 udp::Endpoint localEndpoint(boost::asio::ip::address_v4::loopback(), 20074);
569 udp::Endpoint multicastEndpoint(boost::asio::ip::address::from_string("224.0.0.1"), 20075);
Weiwei Liu72cee942016-02-04 16:49:19 -0700570 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(localEndpoint, multicastEndpoint), UdpFactory::Error,
571 [] (const UdpFactory::Error& e) {
572 return strcmp(e.what(),
573 "Cannot create the requested UDP multicast face, "
574 "both endpoints should have the same port number. ") == 0;
575 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700576}
577
Eric Newberry42602412016-08-27 09:33:18 -0700578BOOST_AUTO_TEST_CASE(FaceCreate)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700579{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000580 UdpFactory factory;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700581
Eric Newberry42602412016-08-27 09:33:18 -0700582 createFace(factory,
583 FaceUri("udp4://127.0.0.1:6363"),
584 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700585 false,
Eric Newberry42602412016-08-27 09:33:18 -0700586 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700587
588 factory.createChannel("127.0.0.1", "20071");
589
Eric Newberry42602412016-08-27 09:33:18 -0700590 createFace(factory,
591 FaceUri("udp4://127.0.0.1:20070"),
592 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700593 false,
Eric Newberry42602412016-08-27 09:33:18 -0700594 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700595 //test the upgrade
Eric Newberry42602412016-08-27 09:33:18 -0700596 createFace(factory,
597 FaceUri("udp4://127.0.0.1:20070"),
598 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700599 false,
Eric Newberry42602412016-08-27 09:33:18 -0700600 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700601
Eric Newberry42602412016-08-27 09:33:18 -0700602 createFace(factory,
603 FaceUri("udp4://127.0.0.1:20072"),
604 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700605 false,
Eric Newberry42602412016-08-27 09:33:18 -0700606 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700607}
608
609BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
610{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000611 UdpFactory factory;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700612
613 factory.createChannel("127.0.0.1", "20070");
614
Eric Newberry42602412016-08-27 09:33:18 -0700615 createFace(factory,
616 FaceUri("udp4://127.0.0.1:20070"),
617 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700618 false,
Eric Newberry42602412016-08-27 09:33:18 -0700619 {CreateFaceExpectedResult::FAILURE, 406,
620 "Outgoing unicast UDP faces do not support on-demand persistency"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700621}
622
623class FakeNetworkInterfaceFixture : public BaseFixture
624{
625public:
626 FakeNetworkInterfaceFixture()
627 {
628 using namespace boost::asio::ip;
629
630 auto fakeInterfaces = make_shared<std::vector<NetworkInterfaceInfo>>();
631
632 fakeInterfaces->push_back(
633 NetworkInterfaceInfo {0, "eth0",
634 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
635 {address_v4::from_string("0.0.0.0")},
636 {address_v6::from_string("::")},
637 address_v4(),
638 IFF_UP});
639 fakeInterfaces->push_back(
640 NetworkInterfaceInfo {1, "eth0",
641 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
642 {address_v4::from_string("192.168.2.1"), address_v4::from_string("192.168.2.2")},
643 {},
644 address_v4::from_string("192.168.2.255"),
645 0});
646 fakeInterfaces->push_back(
647 NetworkInterfaceInfo {2, "eth1",
648 ethernet::Address::fromString("3e:15:c2:8b:65:00"),
649 {address_v4::from_string("198.51.100.1")},
650 {address_v6::from_string("2001:db8::2"), address_v6::from_string("2001:db8::3")},
651 address_v4::from_string("198.51.100.255"),
652 IFF_MULTICAST | IFF_BROADCAST | IFF_UP});
653
654 setDebugNetworkInterfaces(fakeInterfaces);
655 }
656
657 ~FakeNetworkInterfaceFixture()
658 {
659 setDebugNetworkInterfaces(nullptr);
660 }
661};
662
663BOOST_FIXTURE_TEST_CASE(Bug2292, FakeNetworkInterfaceFixture)
664{
665 using namespace boost::asio::ip;
666
667 UdpFactory factory;
668 factory.prohibitEndpoint(udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024));
669 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
670 BOOST_CHECK((factory.m_prohibitedEndpoints ==
671 std::set<udp::Endpoint> {
672 udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
673 }));
674
675 factory.m_prohibitedEndpoints.clear();
676 factory.prohibitEndpoint(udp::Endpoint(address_v6::from_string("2001:db8::1"), 2048));
677 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 1);
678 BOOST_CHECK((factory.m_prohibitedEndpoints ==
679 std::set<udp::Endpoint> {
680 udp::Endpoint(address_v6::from_string("2001:db8::1"), 2048),
681 }));
682
683 factory.m_prohibitedEndpoints.clear();
684 factory.prohibitEndpoint(udp::Endpoint(address_v4(), 1024));
685 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 6);
686 BOOST_CHECK((factory.m_prohibitedEndpoints ==
687 std::set<udp::Endpoint> {
688 udp::Endpoint(address_v4::from_string("192.168.2.1"), 1024),
689 udp::Endpoint(address_v4::from_string("192.168.2.2"), 1024),
690 udp::Endpoint(address_v4::from_string("198.51.100.1"), 1024),
691 udp::Endpoint(address_v4::from_string("198.51.100.255"), 1024),
692 udp::Endpoint(address_v4::from_string("255.255.255.255"), 1024),
693 udp::Endpoint(address_v4::from_string("0.0.0.0"), 1024)
694 }));
695
696 factory.m_prohibitedEndpoints.clear();
697 factory.prohibitEndpoint(udp::Endpoint(address_v6(), 2048));
698 BOOST_REQUIRE_EQUAL(factory.m_prohibitedEndpoints.size(), 3);
699 BOOST_CHECK((factory.m_prohibitedEndpoints ==
700 std::set<udp::Endpoint> {
701 udp::Endpoint(address_v6::from_string("2001:db8::2"), 2048),
702 udp::Endpoint(address_v6::from_string("2001:db8::3"), 2048),
703 udp::Endpoint(address_v6::from_string("::"), 2048),
704 }));
705}
706
707BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
708BOOST_AUTO_TEST_SUITE_END() // Face
709
710} // namespace tests
Junxiao Shi64d99f22017-01-21 23:06:36 +0000711} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700712} // namespace nfd