blob: c09fe628a0714077ac1e8de23a7bdf308143288d [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/*
Eric Newberrycb27d912020-04-08 19:38:18 -07003 * Copyright (c) 2014-2020, 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"
Davide Pesaventob15276f2017-07-15 16:27:13 -040030
Junxiao Shic31080d2017-01-24 15:10:12 +000031#include <boost/algorithm/string/replace.hpp>
Junxiao Shicde37ad2015-12-24 01:02:05 -070032
33namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000034namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070035namespace tests {
36
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040037class UdpFactoryFixture : public FaceSystemFactoryFixture<UdpFactory>
38{
39protected:
40 shared_ptr<UdpChannel>
Davide Pesaventobb734df2017-10-24 18:05:36 -040041 createChannel(const std::string& localIp, uint16_t localPort)
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040042 {
Davide Pesavento9c33b902018-05-20 01:30:29 -040043 udp::Endpoint endpoint(boost::asio::ip::address::from_string(localIp), localPort);
Davide Pesavento494a9552018-02-04 22:16:05 -050044 return factory.createChannel(endpoint, 5_min);
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040045 }
46};
Junxiao Shi0ba6d642017-07-17 00:53:22 +000047
Davide Pesaventobb734df2017-10-24 18:05:36 -040048class UdpFactoryMcastFixture : public UdpFactoryFixture
49{
50protected:
51 UdpFactoryMcastFixture()
52 {
53 for (const auto& netif : collectNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050054 // same filtering logic as UdpFactory::applyMcastConfigToNetif()
55 if (netif->isUp() && !netif->isLoopback() && netif->canMulticast()) {
56 bool hasValidIpAddress = false;
57 if (hasAddressFamily(*netif, ndn::net::AddressFamily::V4)) {
58 hasValidIpAddress = true;
59 netifsV4.push_back(netif);
60 }
61 if (hasAddressFamily(*netif, ndn::net::AddressFamily::V6)) {
62 hasValidIpAddress = true;
63 netifsV6.push_back(netif);
64 }
65 if (hasValidIpAddress) {
66 netifs.push_back(netif);
67 }
Davide Pesaventobb734df2017-10-24 18:05:36 -040068 }
69 }
Davide Pesaventobb734df2017-10-24 18:05:36 -040070 this->copyRealNetifsToNetmon();
71 }
72
73 shared_ptr<Face>
74 createMulticastFace(const std::string& localIp, const std::string& mcastIp, uint16_t mcastPort)
75 {
Davide Pesavento9c33b902018-05-20 01:30:29 -040076 auto localAddress = boost::asio::ip::address::from_string(localIp);
77 udp::Endpoint mcastEndpoint(boost::asio::ip::address::from_string(mcastIp), mcastPort);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050078
79 if (localAddress.is_v4()) {
80 BOOST_ASSERT(!netifsV4.empty());
81 return factory.createMulticastFace(netifsV4.front(), localAddress, mcastEndpoint);
82 }
83 else {
84 BOOST_ASSERT(!netifsV6.empty());
85 return factory.createMulticastFace(netifsV6.front(), localAddress, mcastEndpoint);
86 }
87 }
88
89 /** \brief returns a non-loopback IP address suitable for the creation of a UDP multicast face
90 */
91 boost::asio::ip::address
92 findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily af) const
93 {
94 const auto& netifList = af == ndn::net::AddressFamily::V4 ? netifsV4 : netifsV6;
95 for (const auto& netif : netifList) {
96 for (const auto& a : netif->getNetworkAddresses()) {
97 if (a.getFamily() == af && !a.getIp().is_loopback())
98 return a.getIp();
99 }
100 }
101 return {};
Davide Pesaventobb734df2017-10-24 18:05:36 -0400102 }
103
104 std::vector<const Face*>
105 listUdp4McastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
106 {
107 return this->listFacesByScheme("udp4", linkType);
108 }
109
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500110 std::vector<const Face*>
111 listUdp6McastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
112 {
113 return this->listFacesByScheme("udp6", linkType);
114 }
115
116 /** \brief determine whether \p netif has at least one IP address of the given family
Davide Pesaventobb734df2017-10-24 18:05:36 -0400117 */
118 static bool
119 hasAddressFamily(const NetworkInterface& netif, ndn::net::AddressFamily af)
120 {
121 return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(),
122 [af] (const NetworkAddress& a) { return a.getFamily() == af; });
123 }
124
125 /** \brief determine whether a UDP multicast face is created on \p netif
126 */
127 static bool
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500128 isFaceOnNetif(const Face& face, const NetworkInterface& netif)
Davide Pesaventobb734df2017-10-24 18:05:36 -0400129 {
Davide Pesavento9c33b902018-05-20 01:30:29 -0400130 auto ip = boost::asio::ip::address::from_string(face.getLocalUri().getHost());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500131 return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(),
Davide Pesaventobb734df2017-10-24 18:05:36 -0400132 [ip] (const NetworkAddress& a) { return a.getIp() == ip; });
133 }
134
135protected:
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500136 /** \brief MulticastUdpTransport-capable network interfaces (IPv4 + IPv6)
137 *
138 * This should be used in test cases that do not depend on a specific address family
Davide Pesaventobb734df2017-10-24 18:05:36 -0400139 */
140 std::vector<shared_ptr<const NetworkInterface>> netifs;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500141
142 /** \brief MulticastUdpTransport-capable network interfaces (IPv4 only)
143 */
144 std::vector<shared_ptr<const NetworkInterface>> netifsV4;
145
146 /** \brief MulticastUdpTransport-capable network interfaces (IPv6 only)
147 */
148 std::vector<shared_ptr<const NetworkInterface>> netifsV6;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400149};
150
151#define SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(n) \
152 do { \
153 if (this->netifs.size() < (n)) { \
154 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
155 " or more MulticastUdpTransport-capable network interfaces"); \
156 return; \
157 } \
158 } while (false)
159
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500160#define SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(n) \
161 do { \
162 if (this->netifsV4.size() < (n)) { \
163 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
164 " or more IPv4 MulticastUdpTransport-capable network interfaces"); \
165 return; \
166 } \
167 } while (false)
168
169#define SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(n) \
170 do { \
171 if (this->netifsV6.size() < (n)) { \
172 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
173 " or more IPv6 MulticastUdpTransport-capable network interfaces"); \
174 return; \
175 } \
176 } while (false)
177
Junxiao Shicde37ad2015-12-24 01:02:05 -0700178BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000179BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, UdpFactoryFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700180
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000181BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000182
Davide Pesavento494a9552018-02-04 22:16:05 -0500183using nfd::Face;
184
185BOOST_AUTO_TEST_CASE(Defaults)
186{
187 const std::string CONFIG = R"CONFIG(
188 face_system
189 {
190 udp
191 }
192 )CONFIG";
193
194 parseConfig(CONFIG, true);
195 parseConfig(CONFIG, false);
196
197 checkChannelListEqual(factory, {"udp4://0.0.0.0:6363", "udp6://[::]:6363"});
198 auto channels = factory.getChannels();
199 BOOST_CHECK(std::all_of(channels.begin(), channels.end(),
Davide Pesaventod27841b2018-11-13 00:22:24 -0500200 [] (const auto& ch) { return ch->isListening(); }));
Davide Pesavento494a9552018-02-04 22:16:05 -0500201}
202
203BOOST_AUTO_TEST_CASE(DisableListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000204{
205 const std::string CONFIG = R"CONFIG(
206 face_system
207 {
208 udp
209 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500210 listen no
Junxiao Shi64d99f22017-01-21 23:06:36 +0000211 port 7001
Junxiao Shi64d99f22017-01-21 23:06:36 +0000212 mcast no
213 }
214 }
215 )CONFIG";
216
217 parseConfig(CONFIG, true);
218 parseConfig(CONFIG, false);
219
Junxiao Shi64d99f22017-01-21 23:06:36 +0000220 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"});
Davide Pesavento494a9552018-02-04 22:16:05 -0500221 auto channels = factory.getChannels();
222 BOOST_CHECK(std::none_of(channels.begin(), channels.end(),
Davide Pesaventod27841b2018-11-13 00:22:24 -0500223 [] (const auto& ch) { return ch->isListening(); }));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000224}
225
Davide Pesavento494a9552018-02-04 22:16:05 -0500226BOOST_AUTO_TEST_CASE(DisableV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000227{
228 const std::string CONFIG = R"CONFIG(
229 face_system
230 {
231 udp
232 {
233 port 7001
234 enable_v4 no
235 enable_v6 yes
236 mcast no
237 }
238 }
239 )CONFIG";
240
241 parseConfig(CONFIG, true);
242 parseConfig(CONFIG, false);
243
Junxiao Shi64d99f22017-01-21 23:06:36 +0000244 checkChannelListEqual(factory, {"udp6://[::]:7001"});
245}
246
Davide Pesavento494a9552018-02-04 22:16:05 -0500247BOOST_AUTO_TEST_CASE(DisableV6)
248{
249 const std::string CONFIG = R"CONFIG(
250 face_system
251 {
252 udp
253 {
254 port 7001
255 enable_v4 yes
256 enable_v6 no
257 mcast no
258 }
259 }
260 )CONFIG";
261
262 parseConfig(CONFIG, true);
263 parseConfig(CONFIG, false);
264
265 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"});
266}
267
Davide Pesaventobb734df2017-10-24 18:05:36 -0400268BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000269{
Junxiao Shi64d99f22017-01-21 23:06:36 +0000270 const std::string CONFIG_WITH_MCAST = R"CONFIG(
271 face_system
272 {
273 udp
274 {
275 mcast yes
276 }
277 }
278 )CONFIG";
279 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
280 face_system
281 {
282 udp
283 {
284 mcast no
285 }
286 }
287 )CONFIG";
288
289 parseConfig(CONFIG_WITHOUT_MCAST, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400290 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500291 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000292
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500293#ifdef __linux__
294 // need superuser privileges to create multicast faces on Linux
295 SKIP_IF_NOT_SUPERUSER();
296#endif // __linux__
Junxiao Shi64d99f22017-01-21 23:06:36 +0000297
298 parseConfig(CONFIG_WITH_MCAST, false);
299 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500300 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), netifsV4.size());
301 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), netifsV6.size());
Junxiao Shi64d99f22017-01-21 23:06:36 +0000302
303 parseConfig(CONFIG_WITHOUT_MCAST, false);
304 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400305 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500306 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000307}
308
Davide Pesaventobb734df2017-10-24 18:05:36 -0400309BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpFactoryMcastFixture)
Teng Liangfe4fce32017-03-29 04:49:38 +0000310{
311#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500312 // need superuser privileges to create multicast faces on Linux
Teng Liangfe4fce32017-03-29 04:49:38 +0000313 SKIP_IF_NOT_SUPERUSER();
314#endif // __linux__
315 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
316
317 const std::string CONFIG = R"CONFIG(
318 face_system
319 {
320 udp
321 {
322 mcast_ad_hoc yes
323 }
324 }
325 )CONFIG";
326
327 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500328 BOOST_CHECK_EQUAL(this->listUdp4McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV4.size());
329 BOOST_CHECK_EQUAL(this->listUdp6McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV6.size());
Teng Liangfe4fce32017-03-29 04:49:38 +0000330}
331
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500332BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV4, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000333{
334#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500335 // need superuser privileges to create multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000336 SKIP_IF_NOT_SUPERUSER();
337#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500338 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000339
340 const std::string CONFIG1 = R"CONFIG(
341 face_system
342 {
343 udp
344 {
345 mcast_group 239.66.30.1
346 mcast_port 7011
347 }
348 }
349 )CONFIG";
350 const std::string CONFIG2 = R"CONFIG(
351 face_system
352 {
353 udp
354 {
355 mcast_group 239.66.30.2
356 mcast_port 7012
357 }
358 }
359 )CONFIG";
360
361 parseConfig(CONFIG1, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400362 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500363 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
364 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.1:7011"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000365
366 parseConfig(CONFIG2, false);
367 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400368 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500369 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
370 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.2:7012"));
371}
372
373BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV6, UdpFactoryMcastFixture)
374{
375#ifdef __linux__
376 // need superuser privileges to create multicast faces on Linux
377 SKIP_IF_NOT_SUPERUSER();
378#endif // __linux__
379 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
380
381 const std::string CONFIG1 = R"CONFIG(
382 face_system
383 {
384 udp
385 {
386 mcast_group_v6 ff02::1101
387 mcast_port_v6 7011
388 }
389 }
390 )CONFIG";
391 const std::string CONFIG2 = R"CONFIG(
392 face_system
393 {
394 udp
395 {
396 mcast_group_v6 ff02::1102
397 mcast_port_v6 7012
398 }
399 }
400 )CONFIG";
401
402 parseConfig(CONFIG1, false);
403 auto udpMcastFaces = this->listUdp6McastFaces();
404 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
405 auto expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1101");
406 expectedAddr.scope_id(netifsV6.front()->getIndex());
407 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7011)));
408
409 parseConfig(CONFIG2, false);
410 g_io.poll();
411 udpMcastFaces = this->listUdp6McastFaces();
412 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
413 expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1102");
414 expectedAddr.scope_id(netifsV6.front()->getIndex());
415 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7012)));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000416}
417
Davide Pesaventobb734df2017-10-24 18:05:36 -0400418BOOST_FIXTURE_TEST_CASE(Whitelist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000419{
420#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500421 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000422 SKIP_IF_NOT_SUPERUSER();
423#endif // __linux__
424 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
425
426 std::string CONFIG = R"CONFIG(
427 face_system
428 {
429 udp
430 {
431 whitelist
432 {
433 ifname %ifname
434 }
435 }
436 }
437 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000438 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000439
440 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500441
Davide Pesaventobb734df2017-10-24 18:05:36 -0400442 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500443 BOOST_CHECK_LE(udpMcastFaces.size(), 1);
444 auto udpMcastFacesV6 = this->listUdp6McastFaces();
445 BOOST_CHECK_LE(udpMcastFacesV6.size(), 1);
446 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
447 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
448 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
449 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000450}
451
Davide Pesaventobb734df2017-10-24 18:05:36 -0400452BOOST_FIXTURE_TEST_CASE(Blacklist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000453{
454#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500455 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000456 SKIP_IF_NOT_SUPERUSER();
457#endif // __linux__
458 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
459
460 std::string CONFIG = R"CONFIG(
461 face_system
462 {
463 udp
464 {
465 blacklist
466 {
467 ifname %ifname
468 }
469 }
470 }
471 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000472 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000473
474 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500475
Davide Pesaventobb734df2017-10-24 18:05:36 -0400476 auto udpMcastFaces = this->listUdp4McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500477 if (!netifsV4.empty())
478 BOOST_CHECK_GE(udpMcastFaces.size(), netifsV4.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500479 auto udpMcastFacesV6 = this->listUdp6McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500480 if (!netifsV6.empty())
481 BOOST_CHECK_GE(udpMcastFacesV6.size(), netifsV6.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500482 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
483 BOOST_CHECK_LT(udpMcastFaces.size(), netifsV4.size() + netifsV6.size());
484 BOOST_CHECK(std::none_of(udpMcastFaces.begin(), udpMcastFaces.end(),
485 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000486}
487
Davide Pesaventobb734df2017-10-24 18:05:36 -0400488BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000489{
490#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500491 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000492 SKIP_IF_NOT_SUPERUSER();
493#endif // __linux__
494 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2);
495
496 std::string CONFIG1 = R"CONFIG(
497 face_system
498 {
499 udp
500 {
501 whitelist
502 {
503 ifname %ifname
504 }
505 }
506 }
507 )CONFIG";
508 std::string CONFIG2 = CONFIG1;
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000509 boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName());
510 boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000511
512 parseConfig(CONFIG1, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500513
Davide Pesaventobb734df2017-10-24 18:05:36 -0400514 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500515 auto udpMcastFacesV6 = this->listUdp6McastFaces();
516 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
517 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
518 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
519 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000520
521 parseConfig(CONFIG2, false);
522 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500523
Davide Pesaventobb734df2017-10-24 18:05:36 -0400524 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500525 udpMcastFacesV6 = this->listUdp6McastFaces();
526 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
527 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
528 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
529 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.back()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000530}
531
Junxiao Shi64d99f22017-01-21 23:06:36 +0000532BOOST_AUTO_TEST_CASE(Omitted)
533{
534 const std::string CONFIG = R"CONFIG(
535 face_system
536 {
537 }
538 )CONFIG";
539
540 parseConfig(CONFIG, true);
541 parseConfig(CONFIG, false);
542
Junxiao Shi64d99f22017-01-21 23:06:36 +0000543 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
544 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500545 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp6", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000546}
547
Davide Pesaventobb734df2017-10-24 18:05:36 -0400548BOOST_AUTO_TEST_CASE(AllDisabled)
549{
550 const std::string CONFIG = R"CONFIG(
551 face_system
552 {
553 udp
554 {
555 enable_v4 no
556 enable_v6 no
557 mcast no
558 }
559 }
560 )CONFIG";
561
562 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
563 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
564}
565
Davide Pesavento494a9552018-02-04 22:16:05 -0500566BOOST_AUTO_TEST_CASE(BadListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000567{
568 const std::string CONFIG = R"CONFIG(
569 face_system
570 {
571 udp
572 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500573 listen hello
574 }
575 }
576 )CONFIG";
577
578 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
579 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
580}
581
Davide Pesavento494a9552018-02-04 22:16:05 -0500582BOOST_AUTO_TEST_CASE(BadPort)
583{
584 // not a number
585 const std::string CONFIG1 = R"CONFIG(
586 face_system
587 {
588 udp
589 {
590 port hello
591 }
592 }
593 )CONFIG";
594
595 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
596 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
597
598 // negative number
599 const std::string CONFIG2 = R"CONFIG(
600 face_system
601 {
602 udp
603 {
604 port -1
605 }
606 }
607 )CONFIG";
608
609 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
610 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
611
612 // out of range
613 const std::string CONFIG3 = R"CONFIG(
614 face_system
615 {
616 udp
617 {
618 port 65536
619 }
620 }
621 )CONFIG";
622
623 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
624 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
625}
626
Davide Pesavento494a9552018-02-04 22:16:05 -0500627BOOST_AUTO_TEST_CASE(BadIdleTimeout)
628{
629 // not a number
630 const std::string CONFIG1 = R"CONFIG(
631 face_system
632 {
633 udp
634 {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000635 idle_timeout hello
636 }
637 }
638 )CONFIG";
639
Davide Pesavento494a9552018-02-04 22:16:05 -0500640 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
641 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
642
643 // negative number
644 const std::string CONFIG2 = R"CONFIG(
645 face_system
646 {
647 udp
648 {
649 idle_timeout -15
650 }
651 }
652 )CONFIG";
653
654 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
655 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000656}
657
658BOOST_AUTO_TEST_CASE(BadMcast)
659{
660 const std::string CONFIG = R"CONFIG(
661 face_system
662 {
663 udp
664 {
665 mcast hello
666 }
667 }
668 )CONFIG";
669
670 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
671 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
672}
673
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500674BOOST_AUTO_TEST_CASE(BadMcastGroupV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000675{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400676 // not an address
677 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000678 face_system
679 {
680 udp
681 {
682 mcast_group hello
683 }
684 }
685 )CONFIG";
686
Davide Pesaventobb734df2017-10-24 18:05:36 -0400687 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
688 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000689
Davide Pesaventobb734df2017-10-24 18:05:36 -0400690 // non-multicast address
691 const std::string CONFIG2 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000692 face_system
693 {
694 udp
695 {
696 mcast_group 10.0.0.1
697 }
698 }
699 )CONFIG";
700
Davide Pesaventobb734df2017-10-24 18:05:36 -0400701 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
702 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000703
Davide Pesaventobb734df2017-10-24 18:05:36 -0400704 // wrong address family
705 const std::string CONFIG3 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000706 face_system
707 {
708 udp
709 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400710 mcast_group ff02::1234
Junxiao Shi64d99f22017-01-21 23:06:36 +0000711 }
712 }
713 )CONFIG";
714
Davide Pesaventobb734df2017-10-24 18:05:36 -0400715 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
716 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000717}
718
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500719BOOST_AUTO_TEST_CASE(BadMcastGroupV6)
720{
721 // not an address
722 const std::string CONFIG1 = R"CONFIG(
723 face_system
724 {
725 udp
726 {
727 mcast_group_v6 foo
728 }
729 }
730 )CONFIG";
731
732 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
733 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
734
735 // non-multicast address
736 const std::string CONFIG2 = R"CONFIG(
737 face_system
738 {
739 udp
740 {
741 mcast_group_v6 fe80::1234
742 }
743 }
744 )CONFIG";
745
746 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
747 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
748
749 // wrong address family
750 const std::string CONFIG3 = R"CONFIG(
751 face_system
752 {
753 udp
754 {
755 mcast_group_v6 224.0.23.170
756 }
757 }
758 )CONFIG";
759
760 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
761 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
762}
763
764BOOST_AUTO_TEST_CASE(BadMcastPortV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000765{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400766 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000767 face_system
768 {
769 udp
770 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400771 mcast_port hey
Junxiao Shi64d99f22017-01-21 23:06:36 +0000772 }
773 }
774 )CONFIG";
775
Davide Pesaventobb734df2017-10-24 18:05:36 -0400776 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
777 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
778
779 const std::string CONFIG2 = R"CONFIG(
780 face_system
781 {
782 udp
783 {
784 mcast_port 99999
785 }
786 }
787 )CONFIG";
788
789 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
790 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000791}
792
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500793BOOST_AUTO_TEST_CASE(BadMcastPortV6)
794{
795 const std::string CONFIG1 = R"CONFIG(
796 face_system
797 {
798 udp
799 {
800 mcast_port_v6 bar
801 }
802 }
803 )CONFIG";
804
805 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
806 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
807
808 const std::string CONFIG2 = R"CONFIG(
809 face_system
810 {
811 udp
812 {
813 mcast_port_v6 99999
814 }
815 }
816 )CONFIG";
817
818 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
819 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
820}
821
Junxiao Shi64d99f22017-01-21 23:06:36 +0000822BOOST_AUTO_TEST_CASE(UnknownOption)
823{
824 const std::string CONFIG = R"CONFIG(
825 face_system
826 {
827 udp
828 {
829 hello
830 }
831 }
832 )CONFIG";
833
834 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
835 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
836}
837
838BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
839
Junxiao Shicde37ad2015-12-24 01:02:05 -0700840BOOST_AUTO_TEST_CASE(GetChannels)
841{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400842 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700843
Davide Pesaventob15276f2017-07-15 16:27:13 -0400844 std::set<std::string> expected;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400845 expected.insert(createChannel("127.0.0.1", 20070)->getUri().toString());
846 expected.insert(createChannel("127.0.0.1", 20071)->getUri().toString());
847 expected.insert(createChannel("::1", 20071)->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400848 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700849}
850
Davide Pesaventobb734df2017-10-24 18:05:36 -0400851BOOST_FIXTURE_TEST_CASE(CreateChannel, UdpFactoryMcastFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700852{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400853 auto channel1 = createChannel("127.0.0.1", 20070);
854 auto channel1a = createChannel("127.0.0.1", 20070);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700855 BOOST_CHECK_EQUAL(channel1, channel1a);
856 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
857
Davide Pesaventobb734df2017-10-24 18:05:36 -0400858 auto channel2 = createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700859 BOOST_CHECK_NE(channel1, channel2);
860
Davide Pesaventobb734df2017-10-24 18:05:36 -0400861 auto channel3 = createChannel("::1", 20071);
Weiwei Liu72cee942016-02-04 16:49:19 -0700862 BOOST_CHECK_NE(channel2, channel3);
863 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700864
Davide Pesaventobb734df2017-10-24 18:05:36 -0400865#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500866 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400867 SKIP_IF_NOT_SUPERUSER();
868#endif // __linux__
Davide Pesaventobb734df2017-10-24 18:05:36 -0400869
Davide Pesavento19779d82019-02-14 13:40:04 -0500870 // createChannel with a local endpoint that has already been allocated to a UDP multicast face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500871 if (!netifsV4.empty()) {
872 auto mcastFace = createMulticastFace("127.0.0.1", "224.0.0.254", 20072);
873 BOOST_CHECK_EXCEPTION(createChannel("127.0.0.1", 20072), UdpFactory::Error,
874 [] (const UdpFactory::Error& e) {
875 return strcmp(e.what(),
876 "Cannot create UDP channel on 127.0.0.1:20072, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500877 "endpoint already allocated to a UDP multicast face") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500878 });
879 }
880 if (!netifsV6.empty()) {
881 auto mcastFace = createMulticastFace("::1", "ff02::114", 20072);
882 BOOST_CHECK_EXCEPTION(createChannel("::1", 20072), UdpFactory::Error,
883 [] (const UdpFactory::Error& e) {
884 return strcmp(e.what(),
885 "Cannot create UDP channel on [::1]:20072, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500886 "endpoint already allocated to a UDP multicast face") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500887 });
888 }
Weiwei Liu72cee942016-02-04 16:49:19 -0700889}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700890
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500891BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV4, UdpFactoryMcastFixture)
Weiwei Liu72cee942016-02-04 16:49:19 -0700892{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400893#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500894 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400895 SKIP_IF_NOT_SUPERUSER();
896#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500897 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400898
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500899 auto multicastFace1 = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
900 auto multicastFace1a = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
901 auto multicastFace2 = createMulticastFace("127.0.0.1", "224.0.0.254", 20030);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700902 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500903 BOOST_CHECK_NE(multicastFace1, multicastFace2);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700904
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500905 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V4);
906 if (!address.is_unspecified()) {
907 auto multicastFace3 = createMulticastFace(address.to_string(), "224.0.0.254", 20070);
908 BOOST_CHECK_NE(multicastFace1, multicastFace3);
909 BOOST_CHECK_NE(multicastFace2, multicastFace3);
910 }
911
912 // create with a local endpoint already used by a channel
Davide Pesaventobb734df2017-10-24 18:05:36 -0400913 auto channel = createChannel("127.0.0.1", 20071);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500914 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.254", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700915 [] (const UdpFactory::Error& e) {
916 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500917 "Cannot create UDP multicast face on 127.0.0.1:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500918 "endpoint already allocated to a UDP channel") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700919 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700920
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500921 // create with a local endpoint already used by a multicast face on a different multicast group
Davide Pesaventobb734df2017-10-24 18:05:36 -0400922 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.42", 20070), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700923 [] (const UdpFactory::Error& e) {
924 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500925 "Cannot create UDP multicast face on 127.0.0.1:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500926 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700927 });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500928}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700929
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500930BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV6, UdpFactoryMcastFixture)
931{
932#ifdef __linux__
933 // need superuser privileges to create multicast faces on Linux
934 SKIP_IF_NOT_SUPERUSER();
935#endif // __linux__
936 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
937
938 auto multicastFace1 = createMulticastFace("::1", "ff02::114", 20070);
939 auto multicastFace1a = createMulticastFace("::1", "ff02::114", 20070);
940 auto multicastFace2 = createMulticastFace("::1", "ff02::114", 20030);
941 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
942 BOOST_CHECK_NE(multicastFace1, multicastFace2);
943
944 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V6);
945 if (!address.is_unspecified()) {
946 auto multicastFace3 = createMulticastFace(address.to_string(), "ff02::114", 20070);
947 BOOST_CHECK_NE(multicastFace1, multicastFace3);
948 BOOST_CHECK_NE(multicastFace2, multicastFace3);
949 }
950
951 // create with a local endpoint already used by a channel
952 auto channel = createChannel("::1", 20071);
953 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::114", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700954 [] (const UdpFactory::Error& e) {
955 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500956 "Cannot create UDP multicast face on [::1]:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500957 "endpoint already allocated to a UDP channel") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500958 });
959
960 // create with a local endpoint already used by a multicast face on a different multicast group
961 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::42", 20070), UdpFactory::Error,
962 [] (const UdpFactory::Error& e) {
963 return strcmp(e.what(),
964 "Cannot create UDP multicast face on [::1]:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500965 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700966 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700967}
968
Davide Pesaventob15276f2017-07-15 16:27:13 -0400969BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700970{
Eric Newberry42602412016-08-27 09:33:18 -0700971 createFace(factory,
972 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000973 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700974 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700975 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700976
Davide Pesaventobb734df2017-10-24 18:05:36 -0400977 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700978
Eric Newberry42602412016-08-27 09:33:18 -0700979 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400980 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000981 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700982 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700983 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Eric Newberry78e32b02017-04-01 14:34:44 +0000984
Eric Newberry42602412016-08-27 09:33:18 -0700985 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400986 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000987 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700988 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700989 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700990
Eric Newberry42602412016-08-27 09:33:18 -0700991 createFace(factory,
992 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000993 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700994 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry2642cd22017-07-13 21:34:53 -0400995 {CreateFaceExpectedResult::SUCCESS, 0, ""});
996
Eric Newberry2642cd22017-07-13 21:34:53 -0400997 createFace(factory,
998 FaceUri("udp4://127.0.0.1:20073"),
999 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001000 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, true, false},
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001001 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1002
1003 createFace(factory,
1004 FaceUri("udp4://127.0.0.1:20073"),
1005 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001006 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, true},
1007 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1008
1009 createFace(factory,
1010 FaceUri("udp4://127.0.0.1:20074"),
1011 {},
1012 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, 1000, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001013 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001014}
1015
Davide Pesaventob15276f2017-07-15 16:27:13 -04001016BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -07001017{
Davide Pesaventobb734df2017-10-24 18:05:36 -04001018 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -07001019
Eric Newberry42602412016-08-27 09:33:18 -07001020 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001021 FaceUri("udp4://127.0.0.1:20072"),
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001022 FaceUri("udp4://127.0.0.1:20071"),
Eric Newberry812d6152018-06-06 15:06:01 -07001023 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001024 {CreateFaceExpectedResult::FAILURE, 406,
1025 "Unicast UDP faces cannot be created with a LocalUri"});
1026
1027 createFace(factory,
1028 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001029 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001030 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001031 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001032 "Outgoing UDP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +00001033
1034 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001035 FaceUri("udp4://233.252.0.1:23252"),
1036 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001037 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry78e32b02017-04-01 14:34:44 +00001038 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001039 "Cannot create multicast UDP faces"});
1040
1041 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001042 FaceUri("udp4://127.0.0.1:20072"),
1043 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001044 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, true, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001045 {CreateFaceExpectedResult::FAILURE, 406,
1046 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001047}
1048
Junxiao Shicde37ad2015-12-24 01:02:05 -07001049BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
1050BOOST_AUTO_TEST_SUITE_END() // Face
1051
1052} // namespace tests
Junxiao Shi64d99f22017-01-21 23:06:36 +00001053} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -07001054} // namespace nfd