blob: a095a99373fd6571d53b74cbbeeea07a91bd3448 [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
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 Shi84d62cb2017-07-12 16:15:18 +000031#include "test-netif-ip.hpp"
Junxiao Shic31080d2017-01-24 15:10:12 +000032#include <boost/algorithm/string/replace.hpp>
33#include <boost/range/algorithm.hpp>
Junxiao Shicde37ad2015-12-24 01:02:05 -070034
35namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000036namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070037namespace tests {
38
Junxiao Shi0ba6d642017-07-17 00:53:22 +000039using UdpFactoryFixture = FaceSystemFactoryFixture<UdpFactory>;
40
Junxiao Shicde37ad2015-12-24 01:02:05 -070041BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi0ba6d642017-07-17 00:53:22 +000042BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, UdpFactoryFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -070043
44using nfd::Face;
45
Junxiao Shi0ba6d642017-07-17 00:53:22 +000046BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi64d99f22017-01-21 23:06:36 +000047
48BOOST_AUTO_TEST_CASE(Channels)
49{
50 const std::string CONFIG = R"CONFIG(
51 face_system
52 {
53 udp
54 {
55 port 7001
56 enable_v4 yes
57 enable_v6 yes
58 idle_timeout 30
59 mcast no
60 }
61 }
62 )CONFIG";
63
64 parseConfig(CONFIG, true);
65 parseConfig(CONFIG, false);
66
Junxiao Shi64d99f22017-01-21 23:06:36 +000067 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
Junxiao Shi64d99f22017-01-21 23:06:36 +000088 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"});
89}
90
91BOOST_AUTO_TEST_CASE(ChannelV6)
92{
93 const std::string CONFIG = R"CONFIG(
94 face_system
95 {
96 udp
97 {
98 port 7001
99 enable_v4 no
100 enable_v6 yes
101 mcast no
102 }
103 }
104 )CONFIG";
105
106 parseConfig(CONFIG, true);
107 parseConfig(CONFIG, false);
108
Junxiao Shi64d99f22017-01-21 23:06:36 +0000109 checkChannelListEqual(factory, {"udp6://[::]:7001"});
110}
111
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000112class UdpMcastConfigFixture : public UdpFactoryFixture
Junxiao Shi64d99f22017-01-21 23:06:36 +0000113{
114protected:
115 UdpMcastConfigFixture()
116 {
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000117 for (const auto& netif : collectNetworkInterfaces()) {
118 if (netif->isUp() && netif->canMulticast() && hasAddressFamily<AddressFamily::V4>(*netif)) {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000119 netifs.push_back(netif);
120 }
121 }
Junxiao Shi79a92082017-08-08 02:40:59 +0000122
123 this->copyRealNetifsToNetmon();
Junxiao Shi64d99f22017-01-21 23:06:36 +0000124 }
125
126 std::vector<const Face*>
Teng Liangfe4fce32017-03-29 04:49:38 +0000127 listUdpMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
Junxiao Shi64d99f22017-01-21 23:06:36 +0000128 {
Teng Liangfe4fce32017-03-29 04:49:38 +0000129 return this->listFacesByScheme("udp4", linkType);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000130 }
131
132 size_t
Teng Liangfe4fce32017-03-29 04:49:38 +0000133 countUdpMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
Junxiao Shi64d99f22017-01-21 23:06:36 +0000134 {
Teng Liangfe4fce32017-03-29 04:49:38 +0000135 return this->listUdpMcastFaces(linkType).size();
Junxiao Shi64d99f22017-01-21 23:06:36 +0000136 }
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
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000141 isFaceOnNetif(const Face& face, const shared_ptr<const ndn::net::NetworkInterface>& netif)
Junxiao Shic31080d2017-01-24 15:10:12 +0000142 {
143 auto ip = boost::asio::ip::address_v4::from_string(face.getLocalUri().getHost());
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000144 return std::any_of(netif->getNetworkAddresses().begin(), netif->getNetworkAddresses().end(),
145 [ip] (const ndn::net::NetworkAddress& a) { return a.getIp() == ip; });
Junxiao Shic31080d2017-01-24 15:10:12 +0000146 }
147
Junxiao Shi64d99f22017-01-21 23:06:36 +0000148protected:
149 /** \brief MulticastUdpTransport-capable network interfaces
150 */
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000151 std::vector<shared_ptr<const ndn::net::NetworkInterface>> netifs;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000152};
153
154#define SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(n) \
155 do { \
156 if (this->netifs.size() < (n)) { \
157 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
158 " or more MulticastUdpTransport-capable network interfaces"); \
159 return; \
160 } \
161 } while (false)
162
163BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpMcastConfigFixture)
164{
165#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000166 // need superuser privilege for creating multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000167 SKIP_IF_NOT_SUPERUSER();
168#endif // __linux__
169
170 const std::string CONFIG_WITH_MCAST = R"CONFIG(
171 face_system
172 {
173 udp
174 {
175 mcast yes
176 }
177 }
178 )CONFIG";
179 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
180 face_system
181 {
182 udp
183 {
184 mcast no
185 }
186 }
187 )CONFIG";
188
189 parseConfig(CONFIG_WITHOUT_MCAST, false);
190 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), 0);
191
192 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
193
194 parseConfig(CONFIG_WITH_MCAST, false);
195 g_io.poll();
196 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), netifs.size());
197
198 parseConfig(CONFIG_WITHOUT_MCAST, false);
199 g_io.poll();
200 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), 0);
201}
202
Teng Liangfe4fce32017-03-29 04:49:38 +0000203BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpMcastConfigFixture)
204{
205#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000206 // need superuser privilege for creating multicast faces on Linux
Teng Liangfe4fce32017-03-29 04:49:38 +0000207 SKIP_IF_NOT_SUPERUSER();
208#endif // __linux__
209 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
210
211 const std::string CONFIG = R"CONFIG(
212 face_system
213 {
214 udp
215 {
216 mcast_ad_hoc yes
217 }
218 }
219 )CONFIG";
220
221 parseConfig(CONFIG, false);
222 BOOST_CHECK_EQUAL(this->countUdpMcastFaces(ndn::nfd::LINK_TYPE_AD_HOC), netifs.size());
223}
224
Junxiao Shi64d99f22017-01-21 23:06:36 +0000225BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpoint, UdpMcastConfigFixture)
226{
227#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000228 // need superuser privilege for creating multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000229 SKIP_IF_NOT_SUPERUSER();
230#endif // __linux__
231 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
232
233 const std::string CONFIG1 = R"CONFIG(
234 face_system
235 {
236 udp
237 {
238 mcast_group 239.66.30.1
239 mcast_port 7011
240 }
241 }
242 )CONFIG";
243 const std::string CONFIG2 = R"CONFIG(
244 face_system
245 {
246 udp
247 {
248 mcast_group 239.66.30.2
249 mcast_port 7012
250 }
251 }
252 )CONFIG";
253
254 parseConfig(CONFIG1, false);
255 auto udpMcastFaces = this->listUdpMcastFaces();
256 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifs.size());
257 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(),
258 FaceUri("udp4://239.66.30.1:7011"));
259
260 parseConfig(CONFIG2, false);
261 g_io.poll();
262 udpMcastFaces = this->listUdpMcastFaces();
263 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifs.size());
264 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(),
265 FaceUri("udp4://239.66.30.2:7012"));
266}
267
Junxiao Shic31080d2017-01-24 15:10:12 +0000268BOOST_FIXTURE_TEST_CASE(Whitelist, UdpMcastConfigFixture)
269{
270#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000271 // need superuser privilege for creating multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000272 SKIP_IF_NOT_SUPERUSER();
273#endif // __linux__
274 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
275
276 std::string CONFIG = R"CONFIG(
277 face_system
278 {
279 udp
280 {
281 whitelist
282 {
283 ifname %ifname
284 }
285 }
286 }
287 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000288 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000289
290 parseConfig(CONFIG, false);
291 auto udpMcastFaces = this->listUdpMcastFaces();
292 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1);
293 BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.front()));
294}
295
296BOOST_FIXTURE_TEST_CASE(Blacklist, UdpMcastConfigFixture)
297{
298#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000299 // need superuser privilege for creating multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000300 SKIP_IF_NOT_SUPERUSER();
301#endif // __linux__
302 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
303
304 std::string CONFIG = R"CONFIG(
305 face_system
306 {
307 udp
308 {
309 blacklist
310 {
311 ifname %ifname
312 }
313 }
314 }
315 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000316 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000317
318 parseConfig(CONFIG, false);
319 auto udpMcastFaces = this->listUdpMcastFaces();
320 BOOST_CHECK_EQUAL(udpMcastFaces.size(), netifs.size() - 1);
321 BOOST_CHECK_EQUAL(boost::count_if(udpMcastFaces, [=] (const Face* face) {
322 return isFaceOnNetif(*face, netifs.front());
323 }), 0);
324}
325
326BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpMcastConfigFixture)
327{
328#ifdef __linux__
Junxiao Shibbace1d2017-08-06 20:03:37 +0000329 // need superuser privilege for creating multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000330 SKIP_IF_NOT_SUPERUSER();
331#endif // __linux__
332 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2);
333
334 std::string CONFIG1 = R"CONFIG(
335 face_system
336 {
337 udp
338 {
339 whitelist
340 {
341 ifname %ifname
342 }
343 }
344 }
345 )CONFIG";
346 std::string CONFIG2 = CONFIG1;
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000347 boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName());
348 boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000349
350 parseConfig(CONFIG1, false);
351 auto udpMcastFaces = this->listUdpMcastFaces();
352 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1);
353 BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.front()));
354
355 parseConfig(CONFIG2, false);
356 g_io.poll();
357 udpMcastFaces = this->listUdpMcastFaces();
358 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1);
359 BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.back()));
360}
361
Junxiao Shi64d99f22017-01-21 23:06:36 +0000362BOOST_AUTO_TEST_CASE(Omitted)
363{
364 const std::string CONFIG = R"CONFIG(
365 face_system
366 {
367 }
368 )CONFIG";
369
370 parseConfig(CONFIG, true);
371 parseConfig(CONFIG, false);
372
Junxiao Shi64d99f22017-01-21 23:06:36 +0000373 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
374 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
375}
376
377BOOST_AUTO_TEST_CASE(BadIdleTimeout)
378{
379 const std::string CONFIG = R"CONFIG(
380 face_system
381 {
382 udp
383 {
384 idle_timeout hello
385 }
386 }
387 )CONFIG";
388
389 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
390 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
391}
392
393BOOST_AUTO_TEST_CASE(BadMcast)
394{
395 const std::string CONFIG = R"CONFIG(
396 face_system
397 {
398 udp
399 {
400 mcast hello
401 }
402 }
403 )CONFIG";
404
405 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
406 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
407}
408
409BOOST_AUTO_TEST_CASE(BadMcastGroup)
410{
411 const std::string CONFIG = R"CONFIG(
412 face_system
413 {
414 udp
415 {
416 mcast_group hello
417 }
418 }
419 )CONFIG";
420
421 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
422 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
423}
424
425BOOST_AUTO_TEST_CASE(BadMcastGroupV4Unicast)
426{
427 const std::string CONFIG = R"CONFIG(
428 face_system
429 {
430 udp
431 {
432 mcast_group 10.0.0.1
433 }
434 }
435 )CONFIG";
436
437 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
438 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
439}
440
441BOOST_AUTO_TEST_CASE(BadMcastGroupV6)
442{
443 const std::string CONFIG = R"CONFIG(
444 face_system
445 {
446 udp
447 {
448 mcast_group ff00::1
449 }
450 }
451 )CONFIG";
452
453 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
454 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
455}
456
457BOOST_AUTO_TEST_CASE(AllDisabled)
458{
459 const std::string CONFIG = R"CONFIG(
460 face_system
461 {
462 udp
463 {
464 enable_v4 no
465 enable_v6 no
466 mcast no
467 }
468 }
469 )CONFIG";
470
471 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
472 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
473}
474
475BOOST_AUTO_TEST_CASE(UnknownOption)
476{
477 const std::string CONFIG = R"CONFIG(
478 face_system
479 {
480 udp
481 {
482 hello
483 }
484 }
485 )CONFIG";
486
487 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
488 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
489}
490
491BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
492
Junxiao Shicde37ad2015-12-24 01:02:05 -0700493BOOST_AUTO_TEST_CASE(GetChannels)
494{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700495 BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
496
497 std::vector<shared_ptr<const Channel>> expectedChannels;
498 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
499 expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
500 expectedChannels.push_back(factory.createChannel("::1", "20071"));
501
502 for (const auto& i : factory.getChannels()) {
503 auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i);
504 BOOST_REQUIRE(pos != expectedChannels.end());
505 expectedChannels.erase(pos);
506 }
507
508 BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
509}
510
Weiwei Liu72cee942016-02-04 16:49:19 -0700511BOOST_AUTO_TEST_CASE(CreateChannel)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700512{
Weiwei Liu72cee942016-02-04 16:49:19 -0700513 auto channel1 = factory.createChannel("127.0.0.1", "20070");
514 auto channel1a = factory.createChannel("127.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700515 BOOST_CHECK_EQUAL(channel1, channel1a);
516 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
517
Weiwei Liu72cee942016-02-04 16:49:19 -0700518 auto channel2 = factory.createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700519 BOOST_CHECK_NE(channel1, channel2);
520
Weiwei Liu72cee942016-02-04 16:49:19 -0700521 auto channel3 = factory.createChannel("::1", "20071");
522 BOOST_CHECK_NE(channel2, channel3);
523 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700524
Weiwei Liu72cee942016-02-04 16:49:19 -0700525 // createChannel with multicast address
526 BOOST_CHECK_EXCEPTION(factory.createChannel("224.0.0.1", "20070"), UdpFactory::Error,
527 [] (const UdpFactory::Error& e) {
528 return strcmp(e.what(),
529 "createChannel is only for unicast channels. The provided endpoint "
530 "is multicast. Use createMulticastFace to create a multicast face") == 0;
531 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700532
Weiwei Liu72cee942016-02-04 16:49:19 -0700533 // createChannel with a local endpoint that has already been allocated for a UDP multicast face
534 auto multicastFace = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20072");
535 BOOST_CHECK_EXCEPTION(factory.createChannel("127.0.0.1", "20072"), UdpFactory::Error,
536 [] (const UdpFactory::Error& e) {
537 return strcmp(e.what(),
538 "Cannot create the requested UDP unicast channel, local "
539 "endpoint is already allocated for a UDP multicast face") == 0;
540 });
541}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700542
Weiwei Liu72cee942016-02-04 16:49:19 -0700543BOOST_AUTO_TEST_CASE(CreateMulticastFace)
544{
Weiwei Liu72cee942016-02-04 16:49:19 -0700545 auto multicastFace1 = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070");
546 auto multicastFace1a = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700547 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
548
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200549 // createMulticastFace with a local endpoint that is already used by a channel
Weiwei Liu72cee942016-02-04 16:49:19 -0700550 auto channel = factory.createChannel("127.0.0.1", "20071");
551 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20071"), UdpFactory::Error,
552 [] (const UdpFactory::Error& e) {
553 return strcmp(e.what(),
554 "Cannot create the requested UDP multicast face, local "
555 "endpoint is already allocated for a UDP unicast channel") == 0;
556 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700557
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200558 // createMulticastFace with a local endpoint that is already
559 // used by a multicast face on a different multicast group
Weiwei Liu72cee942016-02-04 16:49:19 -0700560 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.42", "20070"), UdpFactory::Error,
561 [] (const UdpFactory::Error& e) {
562 return strcmp(e.what(),
563 "Cannot create the requested UDP multicast face, local "
564 "endpoint is already allocated for a UDP multicast face "
565 "on a different multicast group") == 0;
566 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700567
Weiwei Liu72cee942016-02-04 16:49:19 -0700568 // createMulticastFace with an IPv4 unicast address
569 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "192.168.10.15", "20072"), UdpFactory::Error,
570 [] (const UdpFactory::Error& e) {
571 return strcmp(e.what(),
572 "Cannot create the requested UDP multicast face, "
573 "the multicast group given as input is not a multicast address") == 0;
574 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700575
Weiwei Liu72cee942016-02-04 16:49:19 -0700576 // createMulticastFace with an IPv6 multicast address
577 BOOST_CHECK_EXCEPTION(factory.createMulticastFace("::1", "ff01::114", "20073"), UdpFactory::Error,
578 [] (const UdpFactory::Error& e) {
579 return strcmp(e.what(),
580 "IPv6 multicast is not supported yet. Please provide an IPv4 "
581 "address") == 0;
582 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700583
Weiwei Liu72cee942016-02-04 16:49:19 -0700584 // createMulticastFace with different local and remote port numbers
Davide Pesaventoeee53aa2016-04-11 17:20:21 +0200585 udp::Endpoint localEndpoint(boost::asio::ip::address_v4::loopback(), 20074);
586 udp::Endpoint multicastEndpoint(boost::asio::ip::address::from_string("224.0.0.1"), 20075);
Weiwei Liu72cee942016-02-04 16:49:19 -0700587 BOOST_CHECK_EXCEPTION(factory.createMulticastFace(localEndpoint, multicastEndpoint), UdpFactory::Error,
588 [] (const UdpFactory::Error& e) {
589 return strcmp(e.what(),
590 "Cannot create the requested UDP multicast face, "
591 "both endpoints should have the same port number. ") == 0;
592 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700593}
594
Eric Newberry42602412016-08-27 09:33:18 -0700595BOOST_AUTO_TEST_CASE(FaceCreate)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700596{
Eric Newberry42602412016-08-27 09:33:18 -0700597 createFace(factory,
598 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000599 {},
Eric Newberry42602412016-08-27 09:33:18 -0700600 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700601 false,
Eric Newberry42602412016-08-27 09:33:18 -0700602 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700603
604 factory.createChannel("127.0.0.1", "20071");
605
Eric Newberry42602412016-08-27 09:33:18 -0700606 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400607 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000608 {},
Eric Newberry42602412016-08-27 09:33:18 -0700609 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700610 false,
Eric Newberry42602412016-08-27 09:33:18 -0700611 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Eric Newberry78e32b02017-04-01 14:34:44 +0000612
Eric Newberry42602412016-08-27 09:33:18 -0700613 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400614 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000615 {},
Eric Newberry42602412016-08-27 09:33:18 -0700616 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700617 false,
Eric Newberry42602412016-08-27 09:33:18 -0700618 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700619
Eric Newberry42602412016-08-27 09:33:18 -0700620 createFace(factory,
621 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000622 {},
Eric Newberry42602412016-08-27 09:33:18 -0700623 ndn::nfd::FACE_PERSISTENCY_PERMANENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700624 false,
Eric Newberry42602412016-08-27 09:33:18 -0700625 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700626}
627
628BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
629{
Eric Newberry78e32b02017-04-01 14:34:44 +0000630 factory.createChannel("127.0.0.1", "20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700631
Eric Newberry42602412016-08-27 09:33:18 -0700632 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400633 FaceUri("udp4://127.0.0.1:20072"),
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400634 FaceUri("udp4://127.0.0.1:20071"),
Davide Pesavento46afec42017-05-28 14:28:47 -0400635 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
636 false,
637 {CreateFaceExpectedResult::FAILURE, 406,
638 "Unicast UDP faces cannot be created with a LocalUri"});
639
640 createFace(factory,
641 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000642 {},
Eric Newberry42602412016-08-27 09:33:18 -0700643 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
Eric Newberryf40551a2016-09-05 15:41:16 -0700644 false,
Eric Newberry42602412016-08-27 09:33:18 -0700645 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400646 "Outgoing UDP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +0000647
648 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400649 FaceUri("udp4://233.252.0.1:23252"),
650 {},
Eric Newberry78e32b02017-04-01 14:34:44 +0000651 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
652 false,
653 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -0400654 "Cannot create multicast UDP faces"});
655
656 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -0400657 FaceUri("udp4://127.0.0.1:20072"),
658 {},
659 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
660 true,
661 {CreateFaceExpectedResult::FAILURE, 406,
662 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700663}
664
Junxiao Shicde37ad2015-12-24 01:02:05 -0700665BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
666BOOST_AUTO_TEST_SUITE_END() // Face
667
668} // namespace tests
Junxiao Shi64d99f22017-01-21 23:06:36 +0000669} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -0700670} // namespace nfd