blob: e2788f50647eaaf3acb0275dce93c5027988465a [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 Shia6286a92021-02-23 06:43:52 -07003 * Copyright (c) 2014-2021, 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"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700198 for (const auto& ch : factory.getChannels()) {
199 BOOST_CHECK(ch->isListening());
200 BOOST_CHECK_EQUAL(ch->getDefaultMtu(), ndn::MAX_NDN_PACKET_SIZE);
201 }
Davide Pesavento494a9552018-02-04 22:16:05 -0500202}
203
204BOOST_AUTO_TEST_CASE(DisableListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000205{
206 const std::string CONFIG = R"CONFIG(
207 face_system
208 {
209 udp
210 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500211 listen no
Junxiao Shi64d99f22017-01-21 23:06:36 +0000212 port 7001
Junxiao Shi64d99f22017-01-21 23:06:36 +0000213 mcast no
214 }
215 }
216 )CONFIG";
217
218 parseConfig(CONFIG, true);
219 parseConfig(CONFIG, false);
220
Junxiao Shi64d99f22017-01-21 23:06:36 +0000221 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700222 for (const auto& ch : factory.getChannels()) {
223 BOOST_CHECK(!ch->isListening());
224 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000225}
226
Davide Pesavento494a9552018-02-04 22:16:05 -0500227BOOST_AUTO_TEST_CASE(DisableV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000228{
229 const std::string CONFIG = R"CONFIG(
230 face_system
231 {
232 udp
233 {
234 port 7001
235 enable_v4 no
236 enable_v6 yes
Junxiao Shia6286a92021-02-23 06:43:52 -0700237 unicast_mtu 1452
Junxiao Shi64d99f22017-01-21 23:06:36 +0000238 mcast no
239 }
240 }
241 )CONFIG";
242
243 parseConfig(CONFIG, true);
244 parseConfig(CONFIG, false);
245
Junxiao Shi64d99f22017-01-21 23:06:36 +0000246 checkChannelListEqual(factory, {"udp6://[::]:7001"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700247 for (const auto& ch : factory.getChannels()) {
248 BOOST_CHECK_EQUAL(ch->getDefaultMtu(), 1452);
249 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000250}
251
Davide Pesavento494a9552018-02-04 22:16:05 -0500252BOOST_AUTO_TEST_CASE(DisableV6)
253{
254 const std::string CONFIG = R"CONFIG(
255 face_system
256 {
257 udp
258 {
259 port 7001
260 enable_v4 yes
261 enable_v6 no
Junxiao Shia6286a92021-02-23 06:43:52 -0700262 unicast_mtu 1452
Davide Pesavento494a9552018-02-04 22:16:05 -0500263 mcast no
264 }
265 }
266 )CONFIG";
267
268 parseConfig(CONFIG, true);
269 parseConfig(CONFIG, false);
270
271 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700272 for (const auto& ch : factory.getChannels()) {
273 BOOST_CHECK_EQUAL(ch->getDefaultMtu(), 1452);
274 }
Davide Pesavento494a9552018-02-04 22:16:05 -0500275}
276
Davide Pesaventobb734df2017-10-24 18:05:36 -0400277BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000278{
Junxiao Shi64d99f22017-01-21 23:06:36 +0000279 const std::string CONFIG_WITH_MCAST = R"CONFIG(
280 face_system
281 {
282 udp
283 {
284 mcast yes
285 }
286 }
287 )CONFIG";
288 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
289 face_system
290 {
291 udp
292 {
293 mcast no
294 }
295 }
296 )CONFIG";
297
298 parseConfig(CONFIG_WITHOUT_MCAST, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400299 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500300 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000301
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500302#ifdef __linux__
303 // need superuser privileges to create multicast faces on Linux
304 SKIP_IF_NOT_SUPERUSER();
305#endif // __linux__
Junxiao Shi64d99f22017-01-21 23:06:36 +0000306
307 parseConfig(CONFIG_WITH_MCAST, false);
308 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500309 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), netifsV4.size());
310 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), netifsV6.size());
Junxiao Shi64d99f22017-01-21 23:06:36 +0000311
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400312 BOOST_REQUIRE_EQUAL(factory.getChannels().size(), 2);
313 for (const auto& face : this->listUdp4McastFaces()) {
314 BOOST_REQUIRE(face->getChannel().lock());
315 BOOST_CHECK_EQUAL(face->getChannel().lock()->getUri().getScheme(), "udp4");
316 }
317
318 for (const auto& face : this->listUdp6McastFaces()) {
319 BOOST_REQUIRE(face->getChannel().lock());
320 BOOST_CHECK_EQUAL(face->getChannel().lock()->getUri().getScheme(), "udp6");
321 }
322
Junxiao Shi64d99f22017-01-21 23:06:36 +0000323 parseConfig(CONFIG_WITHOUT_MCAST, false);
324 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400325 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500326 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000327}
328
Davide Pesaventobb734df2017-10-24 18:05:36 -0400329BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpFactoryMcastFixture)
Teng Liangfe4fce32017-03-29 04:49:38 +0000330{
331#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500332 // need superuser privileges to create multicast faces on Linux
Teng Liangfe4fce32017-03-29 04:49:38 +0000333 SKIP_IF_NOT_SUPERUSER();
334#endif // __linux__
335 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
336
337 const std::string CONFIG = R"CONFIG(
338 face_system
339 {
340 udp
341 {
342 mcast_ad_hoc yes
343 }
344 }
345 )CONFIG";
346
347 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500348 BOOST_CHECK_EQUAL(this->listUdp4McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV4.size());
349 BOOST_CHECK_EQUAL(this->listUdp6McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV6.size());
Teng Liangfe4fce32017-03-29 04:49:38 +0000350}
351
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500352BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV4, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000353{
354#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500355 // need superuser privileges to create multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000356 SKIP_IF_NOT_SUPERUSER();
357#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500358 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000359
360 const std::string CONFIG1 = R"CONFIG(
361 face_system
362 {
363 udp
364 {
365 mcast_group 239.66.30.1
366 mcast_port 7011
367 }
368 }
369 )CONFIG";
370 const std::string CONFIG2 = R"CONFIG(
371 face_system
372 {
373 udp
374 {
375 mcast_group 239.66.30.2
376 mcast_port 7012
377 }
378 }
379 )CONFIG";
380
381 parseConfig(CONFIG1, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400382 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500383 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
384 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.1:7011"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000385
386 parseConfig(CONFIG2, false);
387 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400388 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500389 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
390 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.2:7012"));
391}
392
393BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV6, UdpFactoryMcastFixture)
394{
395#ifdef __linux__
396 // need superuser privileges to create multicast faces on Linux
397 SKIP_IF_NOT_SUPERUSER();
398#endif // __linux__
399 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
400
401 const std::string CONFIG1 = R"CONFIG(
402 face_system
403 {
404 udp
405 {
406 mcast_group_v6 ff02::1101
407 mcast_port_v6 7011
408 }
409 }
410 )CONFIG";
411 const std::string CONFIG2 = R"CONFIG(
412 face_system
413 {
414 udp
415 {
416 mcast_group_v6 ff02::1102
417 mcast_port_v6 7012
418 }
419 }
420 )CONFIG";
421
422 parseConfig(CONFIG1, false);
423 auto udpMcastFaces = this->listUdp6McastFaces();
424 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
425 auto expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1101");
426 expectedAddr.scope_id(netifsV6.front()->getIndex());
427 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7011)));
428
429 parseConfig(CONFIG2, false);
430 g_io.poll();
431 udpMcastFaces = this->listUdp6McastFaces();
432 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
433 expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1102");
434 expectedAddr.scope_id(netifsV6.front()->getIndex());
435 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7012)));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000436}
437
Davide Pesaventobb734df2017-10-24 18:05:36 -0400438BOOST_FIXTURE_TEST_CASE(Whitelist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000439{
440#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500441 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000442 SKIP_IF_NOT_SUPERUSER();
443#endif // __linux__
444 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
445
446 std::string CONFIG = R"CONFIG(
447 face_system
448 {
449 udp
450 {
451 whitelist
452 {
453 ifname %ifname
454 }
455 }
456 }
457 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000458 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000459
460 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500461
Davide Pesaventobb734df2017-10-24 18:05:36 -0400462 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500463 BOOST_CHECK_LE(udpMcastFaces.size(), 1);
464 auto udpMcastFacesV6 = this->listUdp6McastFaces();
465 BOOST_CHECK_LE(udpMcastFacesV6.size(), 1);
466 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
467 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
468 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
469 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000470}
471
Davide Pesaventobb734df2017-10-24 18:05:36 -0400472BOOST_FIXTURE_TEST_CASE(Blacklist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000473{
474#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500475 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000476 SKIP_IF_NOT_SUPERUSER();
477#endif // __linux__
478 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
479
480 std::string CONFIG = R"CONFIG(
481 face_system
482 {
483 udp
484 {
485 blacklist
486 {
487 ifname %ifname
488 }
489 }
490 }
491 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000492 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000493
494 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500495
Davide Pesaventobb734df2017-10-24 18:05:36 -0400496 auto udpMcastFaces = this->listUdp4McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500497 if (!netifsV4.empty())
498 BOOST_CHECK_GE(udpMcastFaces.size(), netifsV4.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500499 auto udpMcastFacesV6 = this->listUdp6McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500500 if (!netifsV6.empty())
501 BOOST_CHECK_GE(udpMcastFacesV6.size(), netifsV6.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500502 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
503 BOOST_CHECK_LT(udpMcastFaces.size(), netifsV4.size() + netifsV6.size());
504 BOOST_CHECK(std::none_of(udpMcastFaces.begin(), udpMcastFaces.end(),
505 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000506}
507
Davide Pesaventobb734df2017-10-24 18:05:36 -0400508BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000509{
510#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500511 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000512 SKIP_IF_NOT_SUPERUSER();
513#endif // __linux__
514 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2);
515
516 std::string CONFIG1 = R"CONFIG(
517 face_system
518 {
519 udp
520 {
521 whitelist
522 {
523 ifname %ifname
524 }
525 }
526 }
527 )CONFIG";
528 std::string CONFIG2 = CONFIG1;
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000529 boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName());
530 boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000531
532 parseConfig(CONFIG1, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500533
Davide Pesaventobb734df2017-10-24 18:05:36 -0400534 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500535 auto udpMcastFacesV6 = this->listUdp6McastFaces();
536 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
537 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
538 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
539 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000540
541 parseConfig(CONFIG2, false);
542 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500543
Davide Pesaventobb734df2017-10-24 18:05:36 -0400544 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500545 udpMcastFacesV6 = this->listUdp6McastFaces();
546 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
547 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
548 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
549 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.back()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000550}
551
Junxiao Shi64d99f22017-01-21 23:06:36 +0000552BOOST_AUTO_TEST_CASE(Omitted)
553{
554 const std::string CONFIG = R"CONFIG(
555 face_system
556 {
557 }
558 )CONFIG";
559
560 parseConfig(CONFIG, true);
561 parseConfig(CONFIG, false);
562
Junxiao Shi64d99f22017-01-21 23:06:36 +0000563 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
564 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500565 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp6", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000566}
567
Davide Pesaventobb734df2017-10-24 18:05:36 -0400568BOOST_AUTO_TEST_CASE(AllDisabled)
569{
570 const std::string CONFIG = R"CONFIG(
571 face_system
572 {
573 udp
574 {
575 enable_v4 no
576 enable_v6 no
577 mcast no
578 }
579 }
580 )CONFIG";
581
582 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
583 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
584}
585
Davide Pesavento494a9552018-02-04 22:16:05 -0500586BOOST_AUTO_TEST_CASE(BadListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000587{
588 const std::string CONFIG = R"CONFIG(
589 face_system
590 {
591 udp
592 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500593 listen hello
594 }
595 }
596 )CONFIG";
597
598 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
599 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
600}
601
Davide Pesavento494a9552018-02-04 22:16:05 -0500602BOOST_AUTO_TEST_CASE(BadPort)
603{
604 // not a number
605 const std::string CONFIG1 = R"CONFIG(
606 face_system
607 {
608 udp
609 {
610 port hello
611 }
612 }
613 )CONFIG";
614
615 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
616 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
617
618 // negative number
619 const std::string CONFIG2 = R"CONFIG(
620 face_system
621 {
622 udp
623 {
624 port -1
625 }
626 }
627 )CONFIG";
628
629 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
630 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
631
632 // out of range
633 const std::string CONFIG3 = R"CONFIG(
634 face_system
635 {
636 udp
637 {
638 port 65536
639 }
640 }
641 )CONFIG";
642
643 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
644 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
645}
646
Davide Pesavento494a9552018-02-04 22:16:05 -0500647BOOST_AUTO_TEST_CASE(BadIdleTimeout)
648{
649 // not a number
650 const std::string CONFIG1 = R"CONFIG(
651 face_system
652 {
653 udp
654 {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000655 idle_timeout hello
656 }
657 }
658 )CONFIG";
659
Davide Pesavento494a9552018-02-04 22:16:05 -0500660 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
661 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
662
663 // negative number
664 const std::string CONFIG2 = R"CONFIG(
665 face_system
666 {
667 udp
668 {
669 idle_timeout -15
670 }
671 }
672 )CONFIG";
673
674 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
675 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000676}
677
Junxiao Shia6286a92021-02-23 06:43:52 -0700678BOOST_AUTO_TEST_CASE(BadMtu)
679{
680 // not a number
681 const std::string CONFIG1 = R"CONFIG(
682 face_system
683 {
684 udp
685 {
686 unicast_mtu hello
687 }
688 }
689 )CONFIG";
690
691 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
692 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
693
694 // underflow
695 const std::string CONFIG2 = R"CONFIG(
696 face_system
697 {
698 udp
699 {
700 unicast_mtu 63
701 }
702 }
703 )CONFIG";
704
705 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
706 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
707
708 // underflow
709 const std::string CONFIG3 = R"CONFIG(
710 face_system
711 {
712 udp
713 {
714 unicast_mtu 8801
715 }
716 }
717 )CONFIG";
718
719 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
720 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
721}
722
Junxiao Shi64d99f22017-01-21 23:06:36 +0000723BOOST_AUTO_TEST_CASE(BadMcast)
724{
725 const std::string CONFIG = R"CONFIG(
726 face_system
727 {
728 udp
729 {
730 mcast hello
731 }
732 }
733 )CONFIG";
734
735 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
736 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
737}
738
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500739BOOST_AUTO_TEST_CASE(BadMcastGroupV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000740{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400741 // not an address
742 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000743 face_system
744 {
745 udp
746 {
747 mcast_group hello
748 }
749 }
750 )CONFIG";
751
Davide Pesaventobb734df2017-10-24 18:05:36 -0400752 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
753 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000754
Davide Pesaventobb734df2017-10-24 18:05:36 -0400755 // non-multicast address
756 const std::string CONFIG2 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000757 face_system
758 {
759 udp
760 {
761 mcast_group 10.0.0.1
762 }
763 }
764 )CONFIG";
765
Davide Pesaventobb734df2017-10-24 18:05:36 -0400766 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
767 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000768
Davide Pesaventobb734df2017-10-24 18:05:36 -0400769 // wrong address family
770 const std::string CONFIG3 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000771 face_system
772 {
773 udp
774 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400775 mcast_group ff02::1234
Junxiao Shi64d99f22017-01-21 23:06:36 +0000776 }
777 }
778 )CONFIG";
779
Davide Pesaventobb734df2017-10-24 18:05:36 -0400780 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
781 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000782}
783
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500784BOOST_AUTO_TEST_CASE(BadMcastGroupV6)
785{
786 // not an address
787 const std::string CONFIG1 = R"CONFIG(
788 face_system
789 {
790 udp
791 {
792 mcast_group_v6 foo
793 }
794 }
795 )CONFIG";
796
797 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
798 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
799
800 // non-multicast address
801 const std::string CONFIG2 = R"CONFIG(
802 face_system
803 {
804 udp
805 {
806 mcast_group_v6 fe80::1234
807 }
808 }
809 )CONFIG";
810
811 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
812 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
813
814 // wrong address family
815 const std::string CONFIG3 = R"CONFIG(
816 face_system
817 {
818 udp
819 {
820 mcast_group_v6 224.0.23.170
821 }
822 }
823 )CONFIG";
824
825 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
826 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
827}
828
829BOOST_AUTO_TEST_CASE(BadMcastPortV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000830{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400831 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000832 face_system
833 {
834 udp
835 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400836 mcast_port hey
Junxiao Shi64d99f22017-01-21 23:06:36 +0000837 }
838 }
839 )CONFIG";
840
Davide Pesaventobb734df2017-10-24 18:05:36 -0400841 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
842 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
843
844 const std::string CONFIG2 = R"CONFIG(
845 face_system
846 {
847 udp
848 {
849 mcast_port 99999
850 }
851 }
852 )CONFIG";
853
854 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
855 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000856}
857
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500858BOOST_AUTO_TEST_CASE(BadMcastPortV6)
859{
860 const std::string CONFIG1 = R"CONFIG(
861 face_system
862 {
863 udp
864 {
865 mcast_port_v6 bar
866 }
867 }
868 )CONFIG";
869
870 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
871 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
872
873 const std::string CONFIG2 = R"CONFIG(
874 face_system
875 {
876 udp
877 {
878 mcast_port_v6 99999
879 }
880 }
881 )CONFIG";
882
883 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
884 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
885}
886
Junxiao Shi64d99f22017-01-21 23:06:36 +0000887BOOST_AUTO_TEST_CASE(UnknownOption)
888{
889 const std::string CONFIG = R"CONFIG(
890 face_system
891 {
892 udp
893 {
894 hello
895 }
896 }
897 )CONFIG";
898
899 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
900 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
901}
902
903BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
904
Junxiao Shicde37ad2015-12-24 01:02:05 -0700905BOOST_AUTO_TEST_CASE(GetChannels)
906{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400907 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700908
Davide Pesaventob15276f2017-07-15 16:27:13 -0400909 std::set<std::string> expected;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400910 expected.insert(createChannel("127.0.0.1", 20070)->getUri().toString());
911 expected.insert(createChannel("127.0.0.1", 20071)->getUri().toString());
912 expected.insert(createChannel("::1", 20071)->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400913 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700914}
915
Davide Pesaventobb734df2017-10-24 18:05:36 -0400916BOOST_FIXTURE_TEST_CASE(CreateChannel, UdpFactoryMcastFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700917{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400918 auto channel1 = createChannel("127.0.0.1", 20070);
919 auto channel1a = createChannel("127.0.0.1", 20070);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700920 BOOST_CHECK_EQUAL(channel1, channel1a);
921 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
922
Davide Pesaventobb734df2017-10-24 18:05:36 -0400923 auto channel2 = createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700924 BOOST_CHECK_NE(channel1, channel2);
925
Davide Pesaventobb734df2017-10-24 18:05:36 -0400926 auto channel3 = createChannel("::1", 20071);
Weiwei Liu72cee942016-02-04 16:49:19 -0700927 BOOST_CHECK_NE(channel2, channel3);
928 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700929
Davide Pesaventobb734df2017-10-24 18:05:36 -0400930#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500931 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400932 SKIP_IF_NOT_SUPERUSER();
933#endif // __linux__
Davide Pesaventobb734df2017-10-24 18:05:36 -0400934
Davide Pesavento19779d82019-02-14 13:40:04 -0500935 // createChannel with a local endpoint that has already been allocated to a UDP multicast face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500936 if (!netifsV4.empty()) {
937 auto mcastFace = createMulticastFace("127.0.0.1", "224.0.0.254", 20072);
938 BOOST_CHECK_EXCEPTION(createChannel("127.0.0.1", 20072), UdpFactory::Error,
939 [] (const UdpFactory::Error& e) {
940 return strcmp(e.what(),
941 "Cannot create UDP channel on 127.0.0.1:20072, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500942 "endpoint already allocated to a UDP multicast face") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500943 });
944 }
945 if (!netifsV6.empty()) {
946 auto mcastFace = createMulticastFace("::1", "ff02::114", 20072);
947 BOOST_CHECK_EXCEPTION(createChannel("::1", 20072), UdpFactory::Error,
948 [] (const UdpFactory::Error& e) {
949 return strcmp(e.what(),
950 "Cannot create UDP channel on [::1]:20072, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500951 "endpoint already allocated to a UDP multicast face") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500952 });
953 }
Weiwei Liu72cee942016-02-04 16:49:19 -0700954}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700955
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500956BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV4, UdpFactoryMcastFixture)
Weiwei Liu72cee942016-02-04 16:49:19 -0700957{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400958#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500959 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400960 SKIP_IF_NOT_SUPERUSER();
961#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500962 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400963
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500964 auto multicastFace1 = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
965 auto multicastFace1a = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
966 auto multicastFace2 = createMulticastFace("127.0.0.1", "224.0.0.254", 20030);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700967 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500968 BOOST_CHECK_NE(multicastFace1, multicastFace2);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700969
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500970 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V4);
971 if (!address.is_unspecified()) {
972 auto multicastFace3 = createMulticastFace(address.to_string(), "224.0.0.254", 20070);
973 BOOST_CHECK_NE(multicastFace1, multicastFace3);
974 BOOST_CHECK_NE(multicastFace2, multicastFace3);
975 }
976
977 // create with a local endpoint already used by a channel
Davide Pesaventobb734df2017-10-24 18:05:36 -0400978 auto channel = createChannel("127.0.0.1", 20071);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500979 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.254", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700980 [] (const UdpFactory::Error& e) {
981 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500982 "Cannot create UDP multicast face on 127.0.0.1:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500983 "endpoint already allocated to a UDP channel") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700984 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700985
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500986 // create with a local endpoint already used by a multicast face on a different multicast group
Davide Pesaventobb734df2017-10-24 18:05:36 -0400987 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.42", 20070), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700988 [] (const UdpFactory::Error& e) {
989 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500990 "Cannot create UDP multicast face on 127.0.0.1:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500991 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700992 });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500993}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700994
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500995BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV6, UdpFactoryMcastFixture)
996{
997#ifdef __linux__
998 // need superuser privileges to create multicast faces on Linux
999 SKIP_IF_NOT_SUPERUSER();
1000#endif // __linux__
1001 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
1002
1003 auto multicastFace1 = createMulticastFace("::1", "ff02::114", 20070);
1004 auto multicastFace1a = createMulticastFace("::1", "ff02::114", 20070);
1005 auto multicastFace2 = createMulticastFace("::1", "ff02::114", 20030);
1006 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
1007 BOOST_CHECK_NE(multicastFace1, multicastFace2);
1008
1009 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V6);
1010 if (!address.is_unspecified()) {
1011 auto multicastFace3 = createMulticastFace(address.to_string(), "ff02::114", 20070);
1012 BOOST_CHECK_NE(multicastFace1, multicastFace3);
1013 BOOST_CHECK_NE(multicastFace2, multicastFace3);
1014 }
1015
1016 // create with a local endpoint already used by a channel
1017 auto channel = createChannel("::1", 20071);
1018 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::114", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -07001019 [] (const UdpFactory::Error& e) {
1020 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001021 "Cannot create UDP multicast face on [::1]:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -05001022 "endpoint already allocated to a UDP channel") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001023 });
1024
1025 // create with a local endpoint already used by a multicast face on a different multicast group
1026 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::42", 20070), UdpFactory::Error,
1027 [] (const UdpFactory::Error& e) {
1028 return strcmp(e.what(),
1029 "Cannot create UDP multicast face on [::1]:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -05001030 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -07001031 });
Junxiao Shicde37ad2015-12-24 01:02:05 -07001032}
1033
Davide Pesaventob15276f2017-07-15 16:27:13 -04001034BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -07001035{
Eric Newberry42602412016-08-27 09:33:18 -07001036 createFace(factory,
1037 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001038 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001039 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001040 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001041
Davide Pesaventobb734df2017-10-24 18:05:36 -04001042 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -07001043
Eric Newberry42602412016-08-27 09:33:18 -07001044 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001045 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001046 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001047 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001048 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Eric Newberry78e32b02017-04-01 14:34:44 +00001049
Eric Newberry42602412016-08-27 09:33:18 -07001050 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001051 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001052 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001053 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001054 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001055
Eric Newberry42602412016-08-27 09:33:18 -07001056 createFace(factory,
1057 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001058 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001059 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry2642cd22017-07-13 21:34:53 -04001060 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1061
Eric Newberry2642cd22017-07-13 21:34:53 -04001062 createFace(factory,
1063 FaceUri("udp4://127.0.0.1:20073"),
1064 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001065 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, true, false},
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001066 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1067
1068 createFace(factory,
1069 FaceUri("udp4://127.0.0.1:20073"),
1070 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001071 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, true},
1072 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1073
1074 createFace(factory,
1075 FaceUri("udp4://127.0.0.1:20074"),
1076 {},
1077 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, 1000, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001078 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001079}
1080
Davide Pesaventob15276f2017-07-15 16:27:13 -04001081BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -07001082{
Davide Pesaventobb734df2017-10-24 18:05:36 -04001083 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -07001084
Eric Newberry42602412016-08-27 09:33:18 -07001085 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001086 FaceUri("udp4://127.0.0.1:20072"),
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001087 FaceUri("udp4://127.0.0.1:20071"),
Eric Newberry812d6152018-06-06 15:06:01 -07001088 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001089 {CreateFaceExpectedResult::FAILURE, 406,
1090 "Unicast UDP faces cannot be created with a LocalUri"});
1091
1092 createFace(factory,
1093 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001094 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001095 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001096 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001097 "Outgoing UDP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +00001098
1099 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001100 FaceUri("udp4://233.252.0.1:23252"),
1101 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001102 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry78e32b02017-04-01 14:34:44 +00001103 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001104 "Cannot create multicast UDP faces"});
1105
1106 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001107 FaceUri("udp4://127.0.0.1:20072"),
1108 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001109 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, true, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001110 {CreateFaceExpectedResult::FAILURE, 406,
1111 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001112}
1113
Junxiao Shicde37ad2015-12-24 01:02:05 -07001114BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
1115BOOST_AUTO_TEST_SUITE_END() // Face
1116
1117} // namespace tests
Junxiao Shi64d99f22017-01-21 23:06:36 +00001118} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -07001119} // namespace nfd