blob: 5d9613e753f4008653d851e138ddea6c46a0ea14 [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/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
34
35using face::UdpChannel;
36using face::UdpFactory;
Junxiao Shicde37ad2015-12-24 01:02:05 -070037
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040038class UdpFactoryFixture : public FaceSystemFactoryFixture<UdpFactory>
39{
40protected:
41 shared_ptr<UdpChannel>
Davide Pesaventobb734df2017-10-24 18:05:36 -040042 createChannel(const std::string& localIp, uint16_t localPort)
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040043 {
Davide Pesavento9c33b902018-05-20 01:30:29 -040044 udp::Endpoint endpoint(boost::asio::ip::address::from_string(localIp), localPort);
Davide Pesavento494a9552018-02-04 22:16:05 -050045 return factory.createChannel(endpoint, 5_min);
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040046 }
47};
Junxiao Shi0ba6d642017-07-17 00:53:22 +000048
Davide Pesaventobb734df2017-10-24 18:05:36 -040049class UdpFactoryMcastFixture : public UdpFactoryFixture
50{
51protected:
52 UdpFactoryMcastFixture()
53 {
54 for (const auto& netif : collectNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050055 // same filtering logic as UdpFactory::applyMcastConfigToNetif()
56 if (netif->isUp() && !netif->isLoopback() && netif->canMulticast()) {
57 bool hasValidIpAddress = false;
58 if (hasAddressFamily(*netif, ndn::net::AddressFamily::V4)) {
59 hasValidIpAddress = true;
60 netifsV4.push_back(netif);
61 }
62 if (hasAddressFamily(*netif, ndn::net::AddressFamily::V6)) {
63 hasValidIpAddress = true;
64 netifsV6.push_back(netif);
65 }
66 if (hasValidIpAddress) {
67 netifs.push_back(netif);
68 }
Davide Pesaventobb734df2017-10-24 18:05:36 -040069 }
70 }
Davide Pesaventobb734df2017-10-24 18:05:36 -040071 this->copyRealNetifsToNetmon();
72 }
73
74 shared_ptr<Face>
75 createMulticastFace(const std::string& localIp, const std::string& mcastIp, uint16_t mcastPort)
76 {
Davide Pesavento9c33b902018-05-20 01:30:29 -040077 auto localAddress = boost::asio::ip::address::from_string(localIp);
78 udp::Endpoint mcastEndpoint(boost::asio::ip::address::from_string(mcastIp), mcastPort);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050079
80 if (localAddress.is_v4()) {
81 BOOST_ASSERT(!netifsV4.empty());
82 return factory.createMulticastFace(netifsV4.front(), localAddress, mcastEndpoint);
83 }
84 else {
85 BOOST_ASSERT(!netifsV6.empty());
86 return factory.createMulticastFace(netifsV6.front(), localAddress, mcastEndpoint);
87 }
88 }
89
90 /** \brief returns a non-loopback IP address suitable for the creation of a UDP multicast face
91 */
92 boost::asio::ip::address
93 findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily af) const
94 {
95 const auto& netifList = af == ndn::net::AddressFamily::V4 ? netifsV4 : netifsV6;
96 for (const auto& netif : netifList) {
97 for (const auto& a : netif->getNetworkAddresses()) {
98 if (a.getFamily() == af && !a.getIp().is_loopback())
99 return a.getIp();
100 }
101 }
102 return {};
Davide Pesaventobb734df2017-10-24 18:05:36 -0400103 }
104
105 std::vector<const Face*>
106 listUdp4McastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
107 {
108 return this->listFacesByScheme("udp4", linkType);
109 }
110
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500111 std::vector<const Face*>
112 listUdp6McastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
113 {
114 return this->listFacesByScheme("udp6", linkType);
115 }
116
117 /** \brief determine whether \p netif has at least one IP address of the given family
Davide Pesaventobb734df2017-10-24 18:05:36 -0400118 */
119 static bool
120 hasAddressFamily(const NetworkInterface& netif, ndn::net::AddressFamily af)
121 {
122 return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(),
123 [af] (const NetworkAddress& a) { return a.getFamily() == af; });
124 }
125
126 /** \brief determine whether a UDP multicast face is created on \p netif
127 */
128 static bool
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500129 isFaceOnNetif(const Face& face, const NetworkInterface& netif)
Davide Pesaventobb734df2017-10-24 18:05:36 -0400130 {
Davide Pesavento9c33b902018-05-20 01:30:29 -0400131 auto ip = boost::asio::ip::address::from_string(face.getLocalUri().getHost());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500132 return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(),
Davide Pesaventobb734df2017-10-24 18:05:36 -0400133 [ip] (const NetworkAddress& a) { return a.getIp() == ip; });
134 }
135
136protected:
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500137 /** \brief MulticastUdpTransport-capable network interfaces (IPv4 + IPv6)
138 *
139 * This should be used in test cases that do not depend on a specific address family
Davide Pesaventobb734df2017-10-24 18:05:36 -0400140 */
141 std::vector<shared_ptr<const NetworkInterface>> netifs;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500142
143 /** \brief MulticastUdpTransport-capable network interfaces (IPv4 only)
144 */
145 std::vector<shared_ptr<const NetworkInterface>> netifsV4;
146
147 /** \brief MulticastUdpTransport-capable network interfaces (IPv6 only)
148 */
149 std::vector<shared_ptr<const NetworkInterface>> netifsV6;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400150};
151
152#define SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(n) \
153 do { \
154 if (this->netifs.size() < (n)) { \
155 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
156 " or more MulticastUdpTransport-capable network interfaces"); \
157 return; \
158 } \
159 } while (false)
160
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500161#define SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(n) \
162 do { \
163 if (this->netifsV4.size() < (n)) { \
164 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
165 " or more IPv4 MulticastUdpTransport-capable network interfaces"); \
166 return; \
167 } \
168 } while (false)
169
170#define SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(n) \
171 do { \
172 if (this->netifsV6.size() < (n)) { \
173 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
174 " or more IPv6 MulticastUdpTransport-capable network interfaces"); \
175 return; \
176 } \
177 } while (false)
178
Junxiao Shicde37ad2015-12-24 01:02:05 -0700179BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000180BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, UdpFactoryFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700181
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000182BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000183
Davide Pesavento494a9552018-02-04 22:16:05 -0500184using nfd::Face;
185
186BOOST_AUTO_TEST_CASE(Defaults)
187{
188 const std::string CONFIG = R"CONFIG(
189 face_system
190 {
191 udp
192 }
193 )CONFIG";
194
195 parseConfig(CONFIG, true);
196 parseConfig(CONFIG, false);
197
198 checkChannelListEqual(factory, {"udp4://0.0.0.0:6363", "udp6://[::]:6363"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700199 for (const auto& ch : factory.getChannels()) {
200 BOOST_CHECK(ch->isListening());
201 BOOST_CHECK_EQUAL(ch->getDefaultMtu(), ndn::MAX_NDN_PACKET_SIZE);
202 }
Davide Pesavento494a9552018-02-04 22:16:05 -0500203}
204
205BOOST_AUTO_TEST_CASE(DisableListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000206{
207 const std::string CONFIG = R"CONFIG(
208 face_system
209 {
210 udp
211 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500212 listen no
Junxiao Shi64d99f22017-01-21 23:06:36 +0000213 port 7001
Junxiao Shi64d99f22017-01-21 23:06:36 +0000214 mcast no
215 }
216 }
217 )CONFIG";
218
219 parseConfig(CONFIG, true);
220 parseConfig(CONFIG, false);
221
Junxiao Shi64d99f22017-01-21 23:06:36 +0000222 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700223 for (const auto& ch : factory.getChannels()) {
224 BOOST_CHECK(!ch->isListening());
225 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000226}
227
Davide Pesavento494a9552018-02-04 22:16:05 -0500228BOOST_AUTO_TEST_CASE(DisableV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000229{
230 const std::string CONFIG = R"CONFIG(
231 face_system
232 {
233 udp
234 {
235 port 7001
236 enable_v4 no
237 enable_v6 yes
Junxiao Shia6286a92021-02-23 06:43:52 -0700238 unicast_mtu 1452
Junxiao Shi64d99f22017-01-21 23:06:36 +0000239 mcast no
240 }
241 }
242 )CONFIG";
243
244 parseConfig(CONFIG, true);
245 parseConfig(CONFIG, false);
246
Junxiao Shi64d99f22017-01-21 23:06:36 +0000247 checkChannelListEqual(factory, {"udp6://[::]:7001"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700248 for (const auto& ch : factory.getChannels()) {
249 BOOST_CHECK_EQUAL(ch->getDefaultMtu(), 1452);
250 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000251}
252
Davide Pesavento494a9552018-02-04 22:16:05 -0500253BOOST_AUTO_TEST_CASE(DisableV6)
254{
255 const std::string CONFIG = R"CONFIG(
256 face_system
257 {
258 udp
259 {
260 port 7001
261 enable_v4 yes
262 enable_v6 no
Junxiao Shia6286a92021-02-23 06:43:52 -0700263 unicast_mtu 1452
Davide Pesavento494a9552018-02-04 22:16:05 -0500264 mcast no
265 }
266 }
267 )CONFIG";
268
269 parseConfig(CONFIG, true);
270 parseConfig(CONFIG, false);
271
272 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700273 for (const auto& ch : factory.getChannels()) {
274 BOOST_CHECK_EQUAL(ch->getDefaultMtu(), 1452);
275 }
Davide Pesavento494a9552018-02-04 22:16:05 -0500276}
277
Davide Pesaventobb734df2017-10-24 18:05:36 -0400278BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000279{
Junxiao Shi64d99f22017-01-21 23:06:36 +0000280 const std::string CONFIG_WITH_MCAST = R"CONFIG(
281 face_system
282 {
283 udp
284 {
285 mcast yes
286 }
287 }
288 )CONFIG";
289 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
290 face_system
291 {
292 udp
293 {
294 mcast no
295 }
296 }
297 )CONFIG";
298
299 parseConfig(CONFIG_WITHOUT_MCAST, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400300 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500301 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000302
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500303#ifdef __linux__
304 // need superuser privileges to create multicast faces on Linux
305 SKIP_IF_NOT_SUPERUSER();
306#endif // __linux__
Junxiao Shi64d99f22017-01-21 23:06:36 +0000307
308 parseConfig(CONFIG_WITH_MCAST, false);
309 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500310 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), netifsV4.size());
311 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), netifsV6.size());
Junxiao Shi64d99f22017-01-21 23:06:36 +0000312
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400313 BOOST_REQUIRE_EQUAL(factory.getChannels().size(), 2);
314 for (const auto& face : this->listUdp4McastFaces()) {
315 BOOST_REQUIRE(face->getChannel().lock());
316 BOOST_CHECK_EQUAL(face->getChannel().lock()->getUri().getScheme(), "udp4");
317 }
318
319 for (const auto& face : this->listUdp6McastFaces()) {
320 BOOST_REQUIRE(face->getChannel().lock());
321 BOOST_CHECK_EQUAL(face->getChannel().lock()->getUri().getScheme(), "udp6");
322 }
323
Junxiao Shi64d99f22017-01-21 23:06:36 +0000324 parseConfig(CONFIG_WITHOUT_MCAST, false);
325 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400326 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500327 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000328}
329
Davide Pesaventobb734df2017-10-24 18:05:36 -0400330BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpFactoryMcastFixture)
Teng Liangfe4fce32017-03-29 04:49:38 +0000331{
332#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500333 // need superuser privileges to create multicast faces on Linux
Teng Liangfe4fce32017-03-29 04:49:38 +0000334 SKIP_IF_NOT_SUPERUSER();
335#endif // __linux__
336 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
337
338 const std::string CONFIG = R"CONFIG(
339 face_system
340 {
341 udp
342 {
343 mcast_ad_hoc yes
344 }
345 }
346 )CONFIG";
347
348 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500349 BOOST_CHECK_EQUAL(this->listUdp4McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV4.size());
350 BOOST_CHECK_EQUAL(this->listUdp6McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV6.size());
Teng Liangfe4fce32017-03-29 04:49:38 +0000351}
352
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500353BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV4, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000354{
355#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500356 // need superuser privileges to create multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000357 SKIP_IF_NOT_SUPERUSER();
358#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500359 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000360
361 const std::string CONFIG1 = R"CONFIG(
362 face_system
363 {
364 udp
365 {
366 mcast_group 239.66.30.1
367 mcast_port 7011
368 }
369 }
370 )CONFIG";
371 const std::string CONFIG2 = R"CONFIG(
372 face_system
373 {
374 udp
375 {
376 mcast_group 239.66.30.2
377 mcast_port 7012
378 }
379 }
380 )CONFIG";
381
382 parseConfig(CONFIG1, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400383 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500384 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
385 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.1:7011"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000386
387 parseConfig(CONFIG2, false);
388 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400389 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500390 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
391 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.2:7012"));
392}
393
394BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV6, UdpFactoryMcastFixture)
395{
396#ifdef __linux__
397 // need superuser privileges to create multicast faces on Linux
398 SKIP_IF_NOT_SUPERUSER();
399#endif // __linux__
400 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
401
402 const std::string CONFIG1 = R"CONFIG(
403 face_system
404 {
405 udp
406 {
407 mcast_group_v6 ff02::1101
408 mcast_port_v6 7011
409 }
410 }
411 )CONFIG";
412 const std::string CONFIG2 = R"CONFIG(
413 face_system
414 {
415 udp
416 {
417 mcast_group_v6 ff02::1102
418 mcast_port_v6 7012
419 }
420 }
421 )CONFIG";
422
423 parseConfig(CONFIG1, false);
424 auto udpMcastFaces = this->listUdp6McastFaces();
425 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400426 auto uri = udpMcastFaces.front()->getRemoteUri();
427 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
428 // check the address ignoring the scope id
429 auto addr = boost::asio::ip::address_v6::from_string(uri.getHost());
430 addr.scope_id(0);
431 BOOST_CHECK_EQUAL(addr, boost::asio::ip::address_v6::from_string("ff02::1101"));
432 BOOST_CHECK_EQUAL(uri.getPort(), "7011");
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500433
434 parseConfig(CONFIG2, false);
435 g_io.poll();
436 udpMcastFaces = this->listUdp6McastFaces();
437 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400438 uri = udpMcastFaces.front()->getRemoteUri();
439 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
440 // check the address ignoring the scope id
441 addr = boost::asio::ip::address_v6::from_string(uri.getHost());
442 addr.scope_id(0);
443 BOOST_CHECK_EQUAL(addr, boost::asio::ip::address_v6::from_string("ff02::1102"));
444 BOOST_CHECK_EQUAL(uri.getPort(), "7012");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000445}
446
Davide Pesaventobb734df2017-10-24 18:05:36 -0400447BOOST_FIXTURE_TEST_CASE(Whitelist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000448{
449#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500450 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000451 SKIP_IF_NOT_SUPERUSER();
452#endif // __linux__
453 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
454
455 std::string CONFIG = R"CONFIG(
456 face_system
457 {
458 udp
459 {
460 whitelist
461 {
462 ifname %ifname
463 }
464 }
465 }
466 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000467 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000468
469 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500470
Davide Pesaventobb734df2017-10-24 18:05:36 -0400471 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500472 BOOST_CHECK_LE(udpMcastFaces.size(), 1);
473 auto udpMcastFacesV6 = this->listUdp6McastFaces();
474 BOOST_CHECK_LE(udpMcastFacesV6.size(), 1);
475 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
476 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
477 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
478 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000479}
480
Davide Pesaventobb734df2017-10-24 18:05:36 -0400481BOOST_FIXTURE_TEST_CASE(Blacklist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000482{
483#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500484 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000485 SKIP_IF_NOT_SUPERUSER();
486#endif // __linux__
487 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
488
489 std::string CONFIG = R"CONFIG(
490 face_system
491 {
492 udp
493 {
494 blacklist
495 {
496 ifname %ifname
497 }
498 }
499 }
500 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000501 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000502
503 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500504
Davide Pesaventobb734df2017-10-24 18:05:36 -0400505 auto udpMcastFaces = this->listUdp4McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500506 if (!netifsV4.empty())
507 BOOST_CHECK_GE(udpMcastFaces.size(), netifsV4.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500508 auto udpMcastFacesV6 = this->listUdp6McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500509 if (!netifsV6.empty())
510 BOOST_CHECK_GE(udpMcastFacesV6.size(), netifsV6.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500511 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
512 BOOST_CHECK_LT(udpMcastFaces.size(), netifsV4.size() + netifsV6.size());
513 BOOST_CHECK(std::none_of(udpMcastFaces.begin(), udpMcastFaces.end(),
514 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000515}
516
Davide Pesaventobb734df2017-10-24 18:05:36 -0400517BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000518{
519#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500520 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000521 SKIP_IF_NOT_SUPERUSER();
522#endif // __linux__
523 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2);
524
525 std::string CONFIG1 = R"CONFIG(
526 face_system
527 {
528 udp
529 {
530 whitelist
531 {
532 ifname %ifname
533 }
534 }
535 }
536 )CONFIG";
537 std::string CONFIG2 = CONFIG1;
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000538 boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName());
539 boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000540
541 parseConfig(CONFIG1, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500542
Davide Pesaventobb734df2017-10-24 18:05:36 -0400543 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500544 auto udpMcastFacesV6 = this->listUdp6McastFaces();
545 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
546 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
547 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
548 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000549
550 parseConfig(CONFIG2, false);
551 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500552
Davide Pesaventobb734df2017-10-24 18:05:36 -0400553 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500554 udpMcastFacesV6 = this->listUdp6McastFaces();
555 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
556 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
557 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
558 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.back()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000559}
560
Junxiao Shi64d99f22017-01-21 23:06:36 +0000561BOOST_AUTO_TEST_CASE(Omitted)
562{
563 const std::string CONFIG = R"CONFIG(
564 face_system
565 {
566 }
567 )CONFIG";
568
569 parseConfig(CONFIG, true);
570 parseConfig(CONFIG, false);
571
Junxiao Shi64d99f22017-01-21 23:06:36 +0000572 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
573 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500574 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp6", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000575}
576
Davide Pesaventobb734df2017-10-24 18:05:36 -0400577BOOST_AUTO_TEST_CASE(AllDisabled)
578{
579 const std::string CONFIG = R"CONFIG(
580 face_system
581 {
582 udp
583 {
584 enable_v4 no
585 enable_v6 no
586 mcast no
587 }
588 }
589 )CONFIG";
590
591 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
592 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
593}
594
Davide Pesavento494a9552018-02-04 22:16:05 -0500595BOOST_AUTO_TEST_CASE(BadListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000596{
597 const std::string CONFIG = R"CONFIG(
598 face_system
599 {
600 udp
601 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500602 listen hello
603 }
604 }
605 )CONFIG";
606
607 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
608 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
609}
610
Davide Pesavento494a9552018-02-04 22:16:05 -0500611BOOST_AUTO_TEST_CASE(BadPort)
612{
613 // not a number
614 const std::string CONFIG1 = R"CONFIG(
615 face_system
616 {
617 udp
618 {
619 port hello
620 }
621 }
622 )CONFIG";
623
624 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
625 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
626
627 // negative number
628 const std::string CONFIG2 = R"CONFIG(
629 face_system
630 {
631 udp
632 {
633 port -1
634 }
635 }
636 )CONFIG";
637
638 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
639 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
640
641 // out of range
642 const std::string CONFIG3 = R"CONFIG(
643 face_system
644 {
645 udp
646 {
647 port 65536
648 }
649 }
650 )CONFIG";
651
652 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
653 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
654}
655
Davide Pesavento494a9552018-02-04 22:16:05 -0500656BOOST_AUTO_TEST_CASE(BadIdleTimeout)
657{
658 // not a number
659 const std::string CONFIG1 = R"CONFIG(
660 face_system
661 {
662 udp
663 {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000664 idle_timeout hello
665 }
666 }
667 )CONFIG";
668
Davide Pesavento494a9552018-02-04 22:16:05 -0500669 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
670 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
671
672 // negative number
673 const std::string CONFIG2 = R"CONFIG(
674 face_system
675 {
676 udp
677 {
678 idle_timeout -15
679 }
680 }
681 )CONFIG";
682
683 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
684 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000685}
686
Junxiao Shia6286a92021-02-23 06:43:52 -0700687BOOST_AUTO_TEST_CASE(BadMtu)
688{
689 // not a number
690 const std::string CONFIG1 = R"CONFIG(
691 face_system
692 {
693 udp
694 {
695 unicast_mtu hello
696 }
697 }
698 )CONFIG";
699
700 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
701 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
702
703 // underflow
704 const std::string CONFIG2 = R"CONFIG(
705 face_system
706 {
707 udp
708 {
709 unicast_mtu 63
710 }
711 }
712 )CONFIG";
713
714 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
715 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
716
717 // underflow
718 const std::string CONFIG3 = R"CONFIG(
719 face_system
720 {
721 udp
722 {
723 unicast_mtu 8801
724 }
725 }
726 )CONFIG";
727
728 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
729 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
730}
731
Junxiao Shi64d99f22017-01-21 23:06:36 +0000732BOOST_AUTO_TEST_CASE(BadMcast)
733{
734 const std::string CONFIG = R"CONFIG(
735 face_system
736 {
737 udp
738 {
739 mcast hello
740 }
741 }
742 )CONFIG";
743
744 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
745 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
746}
747
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500748BOOST_AUTO_TEST_CASE(BadMcastGroupV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000749{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400750 // not an address
751 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000752 face_system
753 {
754 udp
755 {
756 mcast_group hello
757 }
758 }
759 )CONFIG";
760
Davide Pesaventobb734df2017-10-24 18:05:36 -0400761 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
762 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000763
Davide Pesaventobb734df2017-10-24 18:05:36 -0400764 // non-multicast address
765 const std::string CONFIG2 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000766 face_system
767 {
768 udp
769 {
770 mcast_group 10.0.0.1
771 }
772 }
773 )CONFIG";
774
Davide Pesaventobb734df2017-10-24 18:05:36 -0400775 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
776 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000777
Davide Pesaventobb734df2017-10-24 18:05:36 -0400778 // wrong address family
779 const std::string CONFIG3 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000780 face_system
781 {
782 udp
783 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400784 mcast_group ff02::1234
Junxiao Shi64d99f22017-01-21 23:06:36 +0000785 }
786 }
787 )CONFIG";
788
Davide Pesaventobb734df2017-10-24 18:05:36 -0400789 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
790 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000791}
792
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500793BOOST_AUTO_TEST_CASE(BadMcastGroupV6)
794{
795 // not an address
796 const std::string CONFIG1 = R"CONFIG(
797 face_system
798 {
799 udp
800 {
801 mcast_group_v6 foo
802 }
803 }
804 )CONFIG";
805
806 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
807 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
808
809 // non-multicast address
810 const std::string CONFIG2 = R"CONFIG(
811 face_system
812 {
813 udp
814 {
815 mcast_group_v6 fe80::1234
816 }
817 }
818 )CONFIG";
819
820 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
821 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
822
823 // wrong address family
824 const std::string CONFIG3 = R"CONFIG(
825 face_system
826 {
827 udp
828 {
829 mcast_group_v6 224.0.23.170
830 }
831 }
832 )CONFIG";
833
834 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
835 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
836}
837
838BOOST_AUTO_TEST_CASE(BadMcastPortV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000839{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400840 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000841 face_system
842 {
843 udp
844 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400845 mcast_port hey
Junxiao Shi64d99f22017-01-21 23:06:36 +0000846 }
847 }
848 )CONFIG";
849
Davide Pesaventobb734df2017-10-24 18:05:36 -0400850 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
851 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
852
853 const std::string CONFIG2 = R"CONFIG(
854 face_system
855 {
856 udp
857 {
858 mcast_port 99999
859 }
860 }
861 )CONFIG";
862
863 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
864 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000865}
866
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500867BOOST_AUTO_TEST_CASE(BadMcastPortV6)
868{
869 const std::string CONFIG1 = R"CONFIG(
870 face_system
871 {
872 udp
873 {
874 mcast_port_v6 bar
875 }
876 }
877 )CONFIG";
878
879 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
880 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
881
882 const std::string CONFIG2 = R"CONFIG(
883 face_system
884 {
885 udp
886 {
887 mcast_port_v6 99999
888 }
889 }
890 )CONFIG";
891
892 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
893 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
894}
895
Junxiao Shi64d99f22017-01-21 23:06:36 +0000896BOOST_AUTO_TEST_CASE(UnknownOption)
897{
898 const std::string CONFIG = R"CONFIG(
899 face_system
900 {
901 udp
902 {
903 hello
904 }
905 }
906 )CONFIG";
907
908 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
909 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
910}
911
912BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
913
Junxiao Shicde37ad2015-12-24 01:02:05 -0700914BOOST_AUTO_TEST_CASE(GetChannels)
915{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400916 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700917
Davide Pesaventob15276f2017-07-15 16:27:13 -0400918 std::set<std::string> expected;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400919 expected.insert(createChannel("127.0.0.1", 20070)->getUri().toString());
920 expected.insert(createChannel("127.0.0.1", 20071)->getUri().toString());
921 expected.insert(createChannel("::1", 20071)->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400922 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700923}
924
Davide Pesaventobb734df2017-10-24 18:05:36 -0400925BOOST_FIXTURE_TEST_CASE(CreateChannel, UdpFactoryMcastFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700926{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400927 auto channel1 = createChannel("127.0.0.1", 20070);
928 auto channel1a = createChannel("127.0.0.1", 20070);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700929 BOOST_CHECK_EQUAL(channel1, channel1a);
930 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
931
Davide Pesaventobb734df2017-10-24 18:05:36 -0400932 auto channel2 = createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700933 BOOST_CHECK_NE(channel1, channel2);
934
Davide Pesaventobb734df2017-10-24 18:05:36 -0400935 auto channel3 = createChannel("::1", 20071);
Weiwei Liu72cee942016-02-04 16:49:19 -0700936 BOOST_CHECK_NE(channel2, channel3);
937 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700938
Davide Pesaventobb734df2017-10-24 18:05:36 -0400939#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500940 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400941 SKIP_IF_NOT_SUPERUSER();
942#endif // __linux__
Davide Pesaventobb734df2017-10-24 18:05:36 -0400943
Davide Pesavento19779d82019-02-14 13:40:04 -0500944 // createChannel with a local endpoint that has already been allocated to a UDP multicast face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500945 if (!netifsV4.empty()) {
946 auto mcastFace = createMulticastFace("127.0.0.1", "224.0.0.254", 20072);
947 BOOST_CHECK_EXCEPTION(createChannel("127.0.0.1", 20072), UdpFactory::Error,
948 [] (const UdpFactory::Error& e) {
949 return strcmp(e.what(),
950 "Cannot create UDP channel on 127.0.0.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 }
954 if (!netifsV6.empty()) {
955 auto mcastFace = createMulticastFace("::1", "ff02::114", 20072);
956 BOOST_CHECK_EXCEPTION(createChannel("::1", 20072), UdpFactory::Error,
957 [] (const UdpFactory::Error& e) {
958 return strcmp(e.what(),
959 "Cannot create UDP channel on [::1]:20072, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500960 "endpoint already allocated to a UDP multicast face") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500961 });
962 }
Weiwei Liu72cee942016-02-04 16:49:19 -0700963}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700964
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500965BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV4, UdpFactoryMcastFixture)
Weiwei Liu72cee942016-02-04 16:49:19 -0700966{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400967#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500968 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400969 SKIP_IF_NOT_SUPERUSER();
970#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500971 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400972
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500973 auto multicastFace1 = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
974 auto multicastFace1a = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
975 auto multicastFace2 = createMulticastFace("127.0.0.1", "224.0.0.254", 20030);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700976 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500977 BOOST_CHECK_NE(multicastFace1, multicastFace2);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700978
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500979 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V4);
980 if (!address.is_unspecified()) {
981 auto multicastFace3 = createMulticastFace(address.to_string(), "224.0.0.254", 20070);
982 BOOST_CHECK_NE(multicastFace1, multicastFace3);
983 BOOST_CHECK_NE(multicastFace2, multicastFace3);
984 }
985
986 // create with a local endpoint already used by a channel
Davide Pesaventobb734df2017-10-24 18:05:36 -0400987 auto channel = createChannel("127.0.0.1", 20071);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500988 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.254", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700989 [] (const UdpFactory::Error& e) {
990 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500991 "Cannot create UDP multicast face on 127.0.0.1:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500992 "endpoint already allocated to a UDP channel") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700993 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700994
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500995 // create with a local endpoint already used by a multicast face on a different multicast group
Davide Pesaventobb734df2017-10-24 18:05:36 -0400996 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.42", 20070), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700997 [] (const UdpFactory::Error& e) {
998 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500999 "Cannot create UDP multicast face on 127.0.0.1:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -05001000 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -07001001 });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001002}
Junxiao Shicde37ad2015-12-24 01:02:05 -07001003
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001004BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV6, UdpFactoryMcastFixture)
1005{
1006#ifdef __linux__
1007 // need superuser privileges to create multicast faces on Linux
1008 SKIP_IF_NOT_SUPERUSER();
1009#endif // __linux__
1010 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
1011
1012 auto multicastFace1 = createMulticastFace("::1", "ff02::114", 20070);
1013 auto multicastFace1a = createMulticastFace("::1", "ff02::114", 20070);
1014 auto multicastFace2 = createMulticastFace("::1", "ff02::114", 20030);
1015 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
1016 BOOST_CHECK_NE(multicastFace1, multicastFace2);
1017
1018 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V6);
1019 if (!address.is_unspecified()) {
1020 auto multicastFace3 = createMulticastFace(address.to_string(), "ff02::114", 20070);
1021 BOOST_CHECK_NE(multicastFace1, multicastFace3);
1022 BOOST_CHECK_NE(multicastFace2, multicastFace3);
1023 }
1024
1025 // create with a local endpoint already used by a channel
1026 auto channel = createChannel("::1", 20071);
1027 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::114", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -07001028 [] (const UdpFactory::Error& e) {
1029 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001030 "Cannot create UDP multicast face on [::1]:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -05001031 "endpoint already allocated to a UDP channel") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001032 });
1033
1034 // create with a local endpoint already used by a multicast face on a different multicast group
1035 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::42", 20070), UdpFactory::Error,
1036 [] (const UdpFactory::Error& e) {
1037 return strcmp(e.what(),
1038 "Cannot create UDP multicast face on [::1]:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -05001039 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -07001040 });
Junxiao Shicde37ad2015-12-24 01:02:05 -07001041}
1042
Davide Pesaventob15276f2017-07-15 16:27:13 -04001043BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -07001044{
Eric Newberry42602412016-08-27 09:33:18 -07001045 createFace(factory,
1046 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001047 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001048 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001049 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001050
Davide Pesaventobb734df2017-10-24 18:05:36 -04001051 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -07001052
Eric Newberry42602412016-08-27 09:33:18 -07001053 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001054 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001055 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001056 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001057 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Eric Newberry78e32b02017-04-01 14:34:44 +00001058
Eric Newberry42602412016-08-27 09:33:18 -07001059 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001060 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001061 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001062 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001063 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001064
Eric Newberry42602412016-08-27 09:33:18 -07001065 createFace(factory,
1066 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001067 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001068 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry2642cd22017-07-13 21:34:53 -04001069 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1070
Eric Newberry2642cd22017-07-13 21:34:53 -04001071 createFace(factory,
1072 FaceUri("udp4://127.0.0.1:20073"),
1073 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001074 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, true, false},
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001075 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1076
1077 createFace(factory,
1078 FaceUri("udp4://127.0.0.1:20073"),
1079 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001080 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, true},
1081 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1082
1083 createFace(factory,
1084 FaceUri("udp4://127.0.0.1:20074"),
1085 {},
1086 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, 1000, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001087 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001088}
1089
Davide Pesaventob15276f2017-07-15 16:27:13 -04001090BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -07001091{
Davide Pesaventobb734df2017-10-24 18:05:36 -04001092 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -07001093
Eric Newberry42602412016-08-27 09:33:18 -07001094 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001095 FaceUri("udp4://127.0.0.1:20072"),
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001096 FaceUri("udp4://127.0.0.1:20071"),
Eric Newberry812d6152018-06-06 15:06:01 -07001097 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001098 {CreateFaceExpectedResult::FAILURE, 406,
1099 "Unicast UDP faces cannot be created with a LocalUri"});
1100
1101 createFace(factory,
1102 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001103 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001104 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001105 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001106 "Outgoing UDP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +00001107
1108 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001109 FaceUri("udp4://233.252.0.1:23252"),
1110 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001111 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry78e32b02017-04-01 14:34:44 +00001112 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001113 "Cannot create multicast UDP faces"});
1114
1115 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001116 FaceUri("udp4://127.0.0.1:20072"),
1117 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001118 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, true, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001119 {CreateFaceExpectedResult::FAILURE, 406,
1120 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001121}
1122
Junxiao Shicde37ad2015-12-24 01:02:05 -07001123BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
1124BOOST_AUTO_TEST_SUITE_END() // Face
1125
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04001126} // namespace nfd::tests