blob: ce4d17ef51c2331a829c8c65d5f719ebf14af841 [file] [log] [blame]
Junxiao Shicde37ad2015-12-24 01:02:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi84d62cb2017-07-12 16:15:18 +00002/*
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
Junxiao Shi64d99f22017-01-21 23:06:36 +000028#include "face-system-fixture.hpp"
Davide Pesaventob15276f2017-07-15 16:27:13 -040029#include "factory-test-common.hpp"
Junxiao Shi84d62cb2017-07-12 16:15:18 +000030#include "test-netif-ip.hpp"
Davide Pesaventob15276f2017-07-15 16:27:13 -040031
Junxiao Shic31080d2017-01-24 15:10:12 +000032#include <boost/algorithm/string/replace.hpp>
Davide Pesaventob15276f2017-07-15 16:27:13 -040033#include <boost/range/algorithm/count_if.hpp>
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040034#include <ndn-cxx/net/address-converter.hpp>
Junxiao Shicde37ad2015-12-24 01:02:05 -070035
36namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000037namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070038namespace tests {
39
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040040class UdpFactoryFixture : public FaceSystemFactoryFixture<UdpFactory>
41{
42protected:
43 shared_ptr<UdpChannel>
44 createChannel(const std::string& localIp, const std::string& localPort)
45 {
46 udp::Endpoint endpoint(ndn::ip::addressFromString(localIp),
47 boost::lexical_cast<uint16_t>(localPort));
48 return factory.createChannel(endpoint, time::minutes(5));
49 }
50};
Junxiao Shi0ba6d642017-07-17 00:53:22 +000051
Junxiao Shicde37ad2015-12-24 01:02:05 -070052BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi0ba6d642017-07-17 00:53:22 +000053BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, UdpFactoryFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -070054
55using nfd::Face;
56
Junxiao Shi0ba6d642017-07-17 00:53:22 +000057BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi64d99f22017-01-21 23:06:36 +000058
59BOOST_AUTO_TEST_CASE(Channels)
60{
61 const std::string CONFIG = R"CONFIG(
62 face_system
63 {
64 udp
65 {
66 port 7001
67 enable_v4 yes
68 enable_v6 yes
69 idle_timeout 30
70 mcast no
71 }
72 }
73 )CONFIG";
74
75 parseConfig(CONFIG, true);
76 parseConfig(CONFIG, false);
77
Junxiao Shi64d99f22017-01-21 23:06:36 +000078 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"});
79}
80
81BOOST_AUTO_TEST_CASE(ChannelV4)
82{
83 const std::string CONFIG = R"CONFIG(
84 face_system
85 {
86 udp
87 {
88 port 7001
89 enable_v4 yes
90 enable_v6 no
91 mcast no
92 }
93 }
94 )CONFIG";
95
96 parseConfig(CONFIG, true);
97 parseConfig(CONFIG, false);
98
Junxiao Shi64d99f22017-01-21 23:06:36 +000099 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"});
100}
101
102BOOST_AUTO_TEST_CASE(ChannelV6)
103{
104 const std::string CONFIG = R"CONFIG(
105 face_system
106 {
107 udp
108 {
109 port 7001
110 enable_v4 no
111 enable_v6 yes
112 mcast no
113 }
114 }
115 )CONFIG";
116
117 parseConfig(CONFIG, true);
118 parseConfig(CONFIG, false);
119
Junxiao Shi64d99f22017-01-21 23:06:36 +0000120 checkChannelListEqual(factory, {"udp6://[::]:7001"});
121}
122
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000123class UdpMcastConfigFixture : public UdpFactoryFixture
Junxiao Shi64d99f22017-01-21 23:06:36 +0000124{
125protected:
126 UdpMcastConfigFixture()
127 {
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000128 for (const auto& netif : collectNetworkInterfaces()) {
129 if (netif->isUp() && netif->canMulticast() && hasAddressFamily<AddressFamily::V4>(*netif)) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000130 netifs.push_back(netif);
131 }
132 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000133
134 this->copyRealNetifsToNetmon();
Junxiao Shi64d99f22017-01-21 23:06:36 +0000135 }
136
137 std::vector<const Face*>
Teng Liangfe4fce32017-03-29 04:49:38 +0000138 listUdpMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
Junxiao Shi64d99f22017-01-21 23:06:36 +0000139 {
Teng Liangfe4fce32017-03-29 04:49:38 +0000140 return this->listFacesByScheme("udp4", linkType);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000141 }
142
143 size_t
Teng Liangfe4fce32017-03-29 04:49:38 +0000144 countUdpMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
Junxiao Shi64d99f22017-01-21 23:06:36 +0000145 {
Teng Liangfe4fce32017-03-29 04:49:38 +0000146 return this->listUdpMcastFaces(linkType).size();
Junxiao Shi64d99f22017-01-21 23:06:36 +0000147 }
148
Junxiao Shic31080d2017-01-24 15:10:12 +0000149 /** \brief determine whether a UDP multicast face is created on \p netif
150 */
151 static bool
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000152 isFaceOnNetif(const Face& face, const shared_ptr<const ndn::net::NetworkInterface>& netif)
Junxiao Shic31080d2017-01-24 15:10:12 +0000153 {
154 auto ip = boost::asio::ip::address_v4::from_string(face.getLocalUri().getHost());
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000155 return std::any_of(netif->getNetworkAddresses().begin(), netif->getNetworkAddresses().end(),
156 [ip] (const ndn::net::NetworkAddress& a) { return a.getIp() == ip; });
Junxiao Shic31080d2017-01-24 15:10:12 +0000157 }
158
Junxiao Shi64d99f22017-01-21 23:06:36 +0000159protected:
160 /** \brief MulticastUdpTransport-capable network interfaces
161 */
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000162 std::vector<shared_ptr<const ndn::net::NetworkInterface>> netifs;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000163};
164
165#define SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(n) \
166 do { \
167 if (this->netifs.size() < (n)) { \
168 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
169 " or more MulticastUdpTransport-capable network interfaces"); \
170 return; \
171 } \
172 } while (false)
173
174BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpMcastConfigFixture)
175{
176#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000177 // need superuser privilege for creating multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000178 SKIP_IF_NOT_SUPERUSER();
179#endif // __linux__
180
181 const std::string CONFIG_WITH_MCAST = R"CONFIG(
182 face_system
183 {
184 udp
185 {
186 mcast yes
187 }
188 }
189 )CONFIG";
190 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
191 face_system
192 {
193 udp
194 {
195 mcast no
196 }
197 }
198 )CONFIG";
199
200 parseConfig(CONFIG_WITHOUT_MCAST, false);
201 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), 0);
202
203 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
204
205 parseConfig(CONFIG_WITH_MCAST, false);
206 g_io.poll();
207 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), netifs.size());
208
209 parseConfig(CONFIG_WITHOUT_MCAST, false);
210 g_io.poll();
211 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), 0);
212}
213
Teng Liangfe4fce32017-03-29 04:49:38 +0000214BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpMcastConfigFixture)
215{
216#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000217 // need superuser privilege for creating multicast faces on Linux
Teng Liangfe4fce32017-03-29 04:49:38 +0000218 SKIP_IF_NOT_SUPERUSER();
219#endif // __linux__
220 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
221
222 const std::string CONFIG = R"CONFIG(
223 face_system
224 {
225 udp
226 {
227 mcast_ad_hoc yes
228 }
229 }
230 )CONFIG";
231
232 parseConfig(CONFIG, false);
233 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(ndn::nfd::LINK_TYPE_AD_HOC), netifs.size());
234}
235
Junxiao Shi64d99f22017-01-21 23:06:36 +0000236BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpoint, UdpMcastConfigFixture)
237{
238#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000239 // need superuser privilege for creating multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000240 SKIP_IF_NOT_SUPERUSER();
241#endif // __linux__
242 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
243
244 const std::string CONFIG1 = R"CONFIG(
245 face_system
246 {
247 udp
248 {
249 mcast_group 239.66.30.1
250 mcast_port 7011
251 }
252 }
253 )CONFIG";
254 const std::string CONFIG2 = R"CONFIG(
255 face_system
256 {
257 udp
258 {
259 mcast_group 239.66.30.2
260 mcast_port 7012
261 }
262 }
263 )CONFIG";
264
265 parseConfig(CONFIG1, false);
266 auto udpMcastFaces = this->listUdpMcastFaces();
267 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifs.size());
268 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(),
269 FaceUri("udp4://239.66.30.1:7011"));
270
271 parseConfig(CONFIG2, false);
272 g_io.poll();
273 udpMcastFaces = this->listUdpMcastFaces();
274 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifs.size());
275 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(),
276 FaceUri("udp4://239.66.30.2:7012"));
277}
278
Junxiao Shic31080d2017-01-24 15:10:12 +0000279BOOST_FIXTURE_TEST_CASE(Whitelist, UdpMcastConfigFixture)
280{
281#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000282 // need superuser privilege for creating multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000283 SKIP_IF_NOT_SUPERUSER();
284#endif // __linux__
285 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
286
287 std::string CONFIG = R"CONFIG(
288 face_system
289 {
290 udp
291 {
292 whitelist
293 {
294 ifname %ifname
295 }
296 }
297 }
298 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000299 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000300
301 parseConfig(CONFIG, false);
302 auto udpMcastFaces = this->listUdpMcastFaces();
303 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1);
304 BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.front()));
305}
306
307BOOST_FIXTURE_TEST_CASE(Blacklist, UdpMcastConfigFixture)
308{
309#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000310 // need superuser privilege for creating multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000311 SKIP_IF_NOT_SUPERUSER();
312#endif // __linux__
313 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
314
315 std::string CONFIG = R"CONFIG(
316 face_system
317 {
318 udp
319 {
320 blacklist
321 {
322 ifname %ifname
323 }
324 }
325 }
326 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000327 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000328
329 parseConfig(CONFIG, false);
330 auto udpMcastFaces = this->listUdpMcastFaces();
331 BOOST_CHECK_EQUAL(udpMcastFaces.size(), netifs.size() - 1);
Davide Pesaventob15276f2017-07-15 16:27:13 -0400332 BOOST_CHECK_EQUAL(boost::count_if(udpMcastFaces, [this] (const Face* face) {
Junxiao Shic31080d2017-01-24 15:10:12 +0000333 return isFaceOnNetif(*face, netifs.front());
334 }), 0);
335}
336
337BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpMcastConfigFixture)
338{
339#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000340 // need superuser privilege for creating multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000341 SKIP_IF_NOT_SUPERUSER();
342#endif // __linux__
343 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2);
344
345 std::string CONFIG1 = R"CONFIG(
346 face_system
347 {
348 udp
349 {
350 whitelist
351 {
352 ifname %ifname
353 }
354 }
355 }
356 )CONFIG";
357 std::string CONFIG2 = CONFIG1;
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000358 boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName());
359 boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000360
361 parseConfig(CONFIG1, false);
362 auto udpMcastFaces = this->listUdpMcastFaces();
363 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1);
364 BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.front()));
365
366 parseConfig(CONFIG2, false);
367 g_io.poll();
368 udpMcastFaces = this->listUdpMcastFaces();
369 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1);
370 BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.back()));
371}
372
Junxiao Shi64d99f22017-01-21 23:06:36 +0000373BOOST_AUTO_TEST_CASE(Omitted)
374{
375 const std::string CONFIG = R"CONFIG(
376 face_system
377 {
378 }
379 )CONFIG";
380
381 parseConfig(CONFIG, true);
382 parseConfig(CONFIG, false);
383
Junxiao Shi64d99f22017-01-21 23:06:36 +0000384 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
385 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
386}
387
388BOOST_AUTO_TEST_CASE(BadIdleTimeout)
389{
390 const std::string CONFIG = R"CONFIG(
391 face_system
392 {
393 udp
394 {
395 idle_timeout hello
396 }
397 }
398 )CONFIG";
399
400 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
401 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
402}
403
404BOOST_AUTO_TEST_CASE(BadMcast)
405{
406 const std::string CONFIG = R"CONFIG(
407 face_system
408 {
409 udp
410 {
411 mcast hello
412 }
413 }
414 )CONFIG";
415
416 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
417 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
418}
419
420BOOST_AUTO_TEST_CASE(BadMcastGroup)
421{
422 const std::string CONFIG = R"CONFIG(
423 face_system
424 {
425 udp
426 {
427 mcast_group hello
428 }
429 }
430 )CONFIG";
431
432 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
433 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
434}
435
436BOOST_AUTO_TEST_CASE(BadMcastGroupV4Unicast)
437{
438 const std::string CONFIG = R"CONFIG(
439 face_system
440 {
441 udp
442 {
443 mcast_group 10.0.0.1
444 }
445 }
446 )CONFIG";
447
448 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
449 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
450}
451
452BOOST_AUTO_TEST_CASE(BadMcastGroupV6)
453{
454 const std::string CONFIG = R"CONFIG(
455 face_system
456 {
457 udp
458 {
459 mcast_group ff00::1
460 }
461 }
462 )CONFIG";
463
464 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
465 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
466}
467
468BOOST_AUTO_TEST_CASE(AllDisabled)
469{
470 const std::string CONFIG = R"CONFIG(
471 face_system
472 {
473 udp
474 {
475 enable_v4 no
476 enable_v6 no
477 mcast no
478 }
479 }
480 )CONFIG";
481
482 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
483 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
484}
485
486BOOST_AUTO_TEST_CASE(UnknownOption)
487{
488 const std::string CONFIG = R"CONFIG(
489 face_system
490 {
491 udp
492 {
493 hello
494 }
495 }
496 )CONFIG";
497
498 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
499 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
500}
501
502BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
503
Junxiao Shicde37ad2015-12-24 01:02:05 -0700504BOOST_AUTO_TEST_CASE(GetChannels)
505{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400506 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700507
Davide Pesaventob15276f2017-07-15 16:27:13 -0400508 std::set<std::string> expected;
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400509 expected.insert(createChannel("127.0.0.1", "20070")->getUri().toString());
510 expected.insert(createChannel("127.0.0.1", "20071")->getUri().toString());
511 expected.insert(createChannel("::1", "20071")->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400512 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700513}
514
Weiwei Liu72cee942016-02-04 16:49:19 -0700515BOOST_AUTO_TEST_CASE(CreateChannel)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700516{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400517 auto channel1 = createChannel("127.0.0.1", "20070");
518 auto channel1a = createChannel("127.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700519 BOOST_CHECK_EQUAL(channel1, channel1a);
520 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
521
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400522 auto channel2 = createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700523 BOOST_CHECK_NE(channel1, channel2);
524
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400525 auto channel3 = createChannel("::1", "20071");
Weiwei Liu72cee942016-02-04 16:49:19 -0700526 BOOST_CHECK_NE(channel2, channel3);
527 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700528
Weiwei Liu72cee942016-02-04 16:49:19 -0700529 // createChannel with multicast address
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400530 BOOST_CHECK_EXCEPTION(createChannel("224.0.0.1", "20070"), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700531 [] (const UdpFactory::Error& e) {
532 return strcmp(e.what(),
533 "createChannel is only for unicast channels. The provided endpoint "
534 "is multicast. Use createMulticastFace to create a multicast face") == 0;
535 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700536
Weiwei Liu72cee942016-02-04 16:49:19 -0700537 // createChannel with a local endpoint that has already been allocated for a UDP multicast face
538 auto multicastFace = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20072");
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400539 BOOST_CHECK_EXCEPTION(createChannel("127.0.0.1", "20072"), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700540 [] (const UdpFactory::Error& e) {
541 return strcmp(e.what(),
542 "Cannot create the requested UDP unicast channel, local "
543 "endpoint is already allocated for a UDP multicast face") == 0;
544 });
545}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700546
Weiwei Liu72cee942016-02-04 16:49:19 -0700547BOOST_AUTO_TEST_CASE(CreateMulticastFace)
548{
Weiwei Liu72cee942016-02-04 16:49:19 -0700549 auto multicastFace1 = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070");
550 auto multicastFace1a = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700551 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
552
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200553 // createMulticastFace with a local endpoint that is already used by a channel
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400554 auto channel = createChannel("127.0.0.1", "20071");
Weiwei Liu72cee942016-02-04 16:49:19 -0700555 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20071"), UdpFactory::Error,
556 [] (const UdpFactory::Error& e) {
557 return strcmp(e.what(),
558 "Cannot create the requested UDP multicast face, local "
559 "endpoint is already allocated for a UDP unicast channel") == 0;
560 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700561
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200562 // createMulticastFace with a local endpoint that is already
563 // used by a multicast face on a different multicast group
Weiwei Liu72cee942016-02-04 16:49:19 -0700564 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.42", "20070"), UdpFactory::Error,
565 [] (const UdpFactory::Error& e) {
566 return strcmp(e.what(),
567 "Cannot create the requested UDP multicast face, local "
568 "endpoint is already allocated for a UDP multicast face "
569 "on a different multicast group") == 0;
570 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700571
Weiwei Liu72cee942016-02-04 16:49:19 -0700572 // createMulticastFace with an IPv4 unicast address
573 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "192.168.10.15", "20072"), UdpFactory::Error,
574 [] (const UdpFactory::Error& e) {
575 return strcmp(e.what(),
576 "Cannot create the requested UDP multicast face, "
577 "the multicast group given as input is not a multicast address") == 0;
578 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700579
Weiwei Liu72cee942016-02-04 16:49:19 -0700580 // createMulticastFace with an IPv6 multicast address
581 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("::1", "ff01::114", "20073"), UdpFactory::Error,
582 [] (const UdpFactory::Error& e) {
583 return strcmp(e.what(),
584 "IPv6 multicast is not supported yet. Please provide an IPv4 "
585 "address") == 0;
586 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700587
Weiwei Liu72cee942016-02-04 16:49:19 -0700588 // createMulticastFace with different local and remote port numbers
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200589 udp::Endpoint localEndpoint(boost::asio::ip::address_v4::loopback(), 20074);
590 udp::Endpoint multicastEndpoint(boost::asio::ip::address::from_string("224.0.0.1"), 20075);
Weiwei Liu72cee942016-02-04 16:49:19 -0700591 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(localEndpoint, multicastEndpoint), UdpFactory::Error,
592 [] (const UdpFactory::Error& e) {
593 return strcmp(e.what(),
594 "Cannot create the requested UDP multicast face, "
595 "both endpoints should have the same port number. ") == 0;
596 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700597}
598
Davide Pesaventob15276f2017-07-15 16:27:13 -0400599BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700600{
Eric Newberry42602412016-08-27 09:33:18 -0700601 createFace(factory,
602 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000603 {},
Eric Newberry42602412016-08-27 09:33:18 -0700604 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700605 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400606 false,
Eric Newberry42602412016-08-27 09:33:18 -0700607 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700608
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400609 createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700610
Eric Newberry42602412016-08-27 09:33:18 -0700611 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400612 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000613 {},
Eric Newberry42602412016-08-27 09:33:18 -0700614 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700615 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400616 false,
Eric Newberry42602412016-08-27 09:33:18 -0700617 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Eric Newberry78e32b02017-04-01 14:34:44 +0000618
Eric Newberry42602412016-08-27 09:33:18 -0700619 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400620 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000621 {},
Eric Newberry42602412016-08-27 09:33:18 -0700622 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700623 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400624 false,
Eric Newberry42602412016-08-27 09:33:18 -0700625 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700626
Eric Newberry42602412016-08-27 09:33:18 -0700627 createFace(factory,
628 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000629 {},
Eric Newberry42602412016-08-27 09:33:18 -0700630 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700631 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400632 false,
633 {CreateFaceExpectedResult::SUCCESS, 0, ""});
634
635
636 createFace(factory,
637 FaceUri("udp4://127.0.0.1:20073"),
638 {},
639 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
640 false,
641 true,
Eric Newberry42602412016-08-27 09:33:18 -0700642 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700643}
644
Davide Pesaventob15276f2017-07-15 16:27:13 -0400645BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700646{
Davide Pesavento4b89a6e2017-10-07 15:29:50 -0400647 createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700648
Eric Newberry42602412016-08-27 09:33:18 -0700649 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400650 FaceUri("udp4://127.0.0.1:20072"),
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400651 FaceUri("udp4://127.0.0.1:20071"),
Davide Pesavento46afec42017-05-28 14:28:47 -0400652 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
653 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400654 false,
Davide Pesavento46afec42017-05-28 14:28:47 -0400655 {CreateFaceExpectedResult::FAILURE, 406,
656 "Unicast UDP faces cannot be created with a LocalUri"});
657
658 createFace(factory,
659 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000660 {},
Eric Newberry42602412016-08-27 09:33:18 -0700661 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700662 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400663 false,
Eric Newberry42602412016-08-27 09:33:18 -0700664 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400665 "Outgoing UDP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +0000666
667 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400668 FaceUri("udp4://233.252.0.1:23252"),
669 {},
Eric Newberry78e32b02017-04-01 14:34:44 +0000670 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
671 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400672 false,
Eric Newberry78e32b02017-04-01 14:34:44 +0000673 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400674 "Cannot create multicast UDP faces"});
675
676 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400677 FaceUri("udp4://127.0.0.1:20072"),
678 {},
679 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
680 true,
Eric Newberry2642cd22017-07-13 21:34:53 -0400681 false,
Davide Pesavento46afec42017-05-28 14:28:47 -0400682 {CreateFaceExpectedResult::FAILURE, 406,
683 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700684}
685
Junxiao Shicde37ad2015-12-24 01:02:05 -0700686BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
687BOOST_AUTO_TEST_SUITE_END() // Face
688
689} // namespace tests
Junxiao Shi64d99f22017-01-21 23:06:36 +0000690} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700691} // namespace nfd