blob: fd8365668d0162d6000bdcb36901227cc439f741 [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 Pesaventod91fe6d2023-10-04 21:40:02 -04003 * Copyright (c) 2014-2023, 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;
Davide Pesavento279af1c2022-08-29 20:18:32 -040037using ndn::net::NetworkInterface;
Junxiao Shicde37ad2015-12-24 01:02:05 -070038
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040039class UdpFactoryFixture : public FaceSystemFactoryFixture<UdpFactory>
40{
41protected:
42 shared_ptr<UdpChannel>
Davide Pesaventobb734df2017-10-24 18:05:36 -040043 createChannel(const std::string& localIp, uint16_t localPort)
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040044 {
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040045 udp::Endpoint endpoint(boost::asio::ip::make_address(localIp), localPort);
Davide Pesavento494a9552018-02-04 22:16:05 -050046 return factory.createChannel(endpoint, 5_min);
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040047 }
48};
Junxiao Shi0ba6d642017-07-17 00:53:22 +000049
Davide Pesaventobb734df2017-10-24 18:05:36 -040050class UdpFactoryMcastFixture : public UdpFactoryFixture
51{
52protected:
53 UdpFactoryMcastFixture()
54 {
55 for (const auto& netif : collectNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050056 // same filtering logic as UdpFactory::applyMcastConfigToNetif()
57 if (netif->isUp() && !netif->isLoopback() && netif->canMulticast()) {
58 bool hasValidIpAddress = false;
59 if (hasAddressFamily(*netif, ndn::net::AddressFamily::V4)) {
60 hasValidIpAddress = true;
61 netifsV4.push_back(netif);
62 }
63 if (hasAddressFamily(*netif, ndn::net::AddressFamily::V6)) {
64 hasValidIpAddress = true;
65 netifsV6.push_back(netif);
66 }
67 if (hasValidIpAddress) {
68 netifs.push_back(netif);
69 }
Davide Pesaventobb734df2017-10-24 18:05:36 -040070 }
71 }
Davide Pesaventod7083a52023-10-19 17:51:16 -040072 copyRealNetifsToNetmon();
Davide Pesaventobb734df2017-10-24 18:05:36 -040073 }
74
75 shared_ptr<Face>
76 createMulticastFace(const std::string& localIp, const std::string& mcastIp, uint16_t mcastPort)
77 {
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040078 auto localAddress = boost::asio::ip::make_address(localIp);
79 udp::Endpoint mcastEndpoint(boost::asio::ip::make_address(mcastIp), mcastPort);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050080
81 if (localAddress.is_v4()) {
82 BOOST_ASSERT(!netifsV4.empty());
Davide Pesavento279af1c2022-08-29 20:18:32 -040083 return factory.createMulticastFace(*netifsV4.front(), localAddress, mcastEndpoint);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050084 }
85 else {
86 BOOST_ASSERT(!netifsV6.empty());
Davide Pesavento279af1c2022-08-29 20:18:32 -040087 return factory.createMulticastFace(*netifsV6.front(), localAddress, mcastEndpoint);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050088 }
89 }
90
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040091 /** \brief Returns a non-loopback IP address suitable for the creation of a UDP multicast face.
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050092 */
93 boost::asio::ip::address
94 findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily af) const
95 {
96 const auto& netifList = af == ndn::net::AddressFamily::V4 ? netifsV4 : netifsV6;
97 for (const auto& netif : netifList) {
98 for (const auto& a : netif->getNetworkAddresses()) {
99 if (a.getFamily() == af && !a.getIp().is_loopback())
100 return a.getIp();
101 }
102 }
103 return {};
Davide Pesaventobb734df2017-10-24 18:05:36 -0400104 }
105
106 std::vector<const Face*>
107 listUdp4McastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
108 {
Davide Pesaventod7083a52023-10-19 17:51:16 -0400109 return listFacesByScheme("udp4", linkType);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400110 }
111
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500112 std::vector<const Face*>
113 listUdp6McastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
114 {
Davide Pesaventod7083a52023-10-19 17:51:16 -0400115 return listFacesByScheme("udp6", linkType);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500116 }
117
Davide Pesaventod7083a52023-10-19 17:51:16 -0400118 /**
119 * \brief Determine whether \p netif has at least one IP address of the given family.
Davide Pesaventobb734df2017-10-24 18:05:36 -0400120 */
121 static bool
122 hasAddressFamily(const NetworkInterface& netif, ndn::net::AddressFamily af)
123 {
124 return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(),
Davide Pesavento279af1c2022-08-29 20:18:32 -0400125 [af] (const auto& a) { return a.getFamily() == af; });
Davide Pesaventobb734df2017-10-24 18:05:36 -0400126 }
127
Davide Pesaventod7083a52023-10-19 17:51:16 -0400128 /**
129 * \brief Determine whether a UDP multicast face is created on \p netif.
Davide Pesaventobb734df2017-10-24 18:05:36 -0400130 */
131 static bool
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500132 isFaceOnNetif(const Face& face, const NetworkInterface& netif)
Davide Pesaventobb734df2017-10-24 18:05:36 -0400133 {
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400134 auto ip = boost::asio::ip::make_address(face.getLocalUri().getHost());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500135 return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(),
Davide Pesavento279af1c2022-08-29 20:18:32 -0400136 [ip] (const auto& a) { return a.getIp() == ip; });
Davide Pesaventobb734df2017-10-24 18:05:36 -0400137 }
138
139protected:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400140 /** \brief MulticastUdpTransport-capable network interfaces (IPv4 + IPv6).
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500141 *
142 * This should be used in test cases that do not depend on a specific address family
Davide Pesaventobb734df2017-10-24 18:05:36 -0400143 */
144 std::vector<shared_ptr<const NetworkInterface>> netifs;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500145
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400146 /** \brief MulticastUdpTransport-capable network interfaces (IPv4 only).
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500147 */
148 std::vector<shared_ptr<const NetworkInterface>> netifsV4;
149
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400150 /** \brief MulticastUdpTransport-capable network interfaces (IPv6 only).
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500151 */
152 std::vector<shared_ptr<const NetworkInterface>> netifsV6;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400153};
154
155#define SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(n) \
156 do { \
157 if (this->netifs.size() < (n)) { \
158 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
159 " or more MulticastUdpTransport-capable network interfaces"); \
160 return; \
161 } \
162 } while (false)
163
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500164#define SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(n) \
165 do { \
166 if (this->netifsV4.size() < (n)) { \
167 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
168 " or more IPv4 MulticastUdpTransport-capable network interfaces"); \
169 return; \
170 } \
171 } while (false)
172
173#define SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(n) \
174 do { \
175 if (this->netifsV6.size() < (n)) { \
176 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
177 " or more IPv6 MulticastUdpTransport-capable network interfaces"); \
178 return; \
179 } \
180 } while (false)
181
Junxiao Shicde37ad2015-12-24 01:02:05 -0700182BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000183BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, UdpFactoryFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700184
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000185BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000186
Davide Pesavento494a9552018-02-04 22:16:05 -0500187using nfd::Face;
188
189BOOST_AUTO_TEST_CASE(Defaults)
190{
191 const std::string CONFIG = R"CONFIG(
192 face_system
193 {
194 udp
195 }
196 )CONFIG";
197
198 parseConfig(CONFIG, true);
199 parseConfig(CONFIG, false);
200
201 checkChannelListEqual(factory, {"udp4://0.0.0.0:6363", "udp6://[::]:6363"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700202 for (const auto& ch : factory.getChannels()) {
203 BOOST_CHECK(ch->isListening());
204 BOOST_CHECK_EQUAL(ch->getDefaultMtu(), ndn::MAX_NDN_PACKET_SIZE);
205 }
Davide Pesavento494a9552018-02-04 22:16:05 -0500206}
207
208BOOST_AUTO_TEST_CASE(DisableListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000209{
210 const std::string CONFIG = R"CONFIG(
211 face_system
212 {
213 udp
214 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500215 listen no
Junxiao Shi64d99f22017-01-21 23:06:36 +0000216 port 7001
Junxiao Shi64d99f22017-01-21 23:06:36 +0000217 mcast no
218 }
219 }
220 )CONFIG";
221
222 parseConfig(CONFIG, true);
223 parseConfig(CONFIG, false);
224
Junxiao Shi64d99f22017-01-21 23:06:36 +0000225 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700226 for (const auto& ch : factory.getChannels()) {
227 BOOST_CHECK(!ch->isListening());
228 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000229}
230
Davide Pesavento494a9552018-02-04 22:16:05 -0500231BOOST_AUTO_TEST_CASE(DisableV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000232{
233 const std::string CONFIG = R"CONFIG(
234 face_system
235 {
236 udp
237 {
238 port 7001
239 enable_v4 no
240 enable_v6 yes
Junxiao Shia6286a92021-02-23 06:43:52 -0700241 unicast_mtu 1452
Junxiao Shi64d99f22017-01-21 23:06:36 +0000242 mcast no
243 }
244 }
245 )CONFIG";
246
247 parseConfig(CONFIG, true);
248 parseConfig(CONFIG, false);
249
Junxiao Shi64d99f22017-01-21 23:06:36 +0000250 checkChannelListEqual(factory, {"udp6://[::]:7001"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700251 for (const auto& ch : factory.getChannels()) {
252 BOOST_CHECK_EQUAL(ch->getDefaultMtu(), 1452);
253 }
Junxiao Shi64d99f22017-01-21 23:06:36 +0000254}
255
Davide Pesavento494a9552018-02-04 22:16:05 -0500256BOOST_AUTO_TEST_CASE(DisableV6)
257{
258 const std::string CONFIG = R"CONFIG(
259 face_system
260 {
261 udp
262 {
263 port 7001
264 enable_v4 yes
265 enable_v6 no
Junxiao Shia6286a92021-02-23 06:43:52 -0700266 unicast_mtu 1452
Davide Pesavento494a9552018-02-04 22:16:05 -0500267 mcast no
268 }
269 }
270 )CONFIG";
271
272 parseConfig(CONFIG, true);
273 parseConfig(CONFIG, false);
274
275 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"});
Junxiao Shia6286a92021-02-23 06:43:52 -0700276 for (const auto& ch : factory.getChannels()) {
277 BOOST_CHECK_EQUAL(ch->getDefaultMtu(), 1452);
278 }
Davide Pesavento494a9552018-02-04 22:16:05 -0500279}
280
Davide Pesaventobb734df2017-10-24 18:05:36 -0400281BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000282{
Junxiao Shi64d99f22017-01-21 23:06:36 +0000283 const std::string CONFIG_WITH_MCAST = R"CONFIG(
284 face_system
285 {
286 udp
287 {
288 mcast yes
289 }
290 }
291 )CONFIG";
292 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
293 face_system
294 {
295 udp
296 {
297 mcast no
298 }
299 }
300 )CONFIG";
301
302 parseConfig(CONFIG_WITHOUT_MCAST, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400303 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500304 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000305
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500306#ifdef __linux__
307 // need superuser privileges to create multicast faces on Linux
308 SKIP_IF_NOT_SUPERUSER();
309#endif // __linux__
Junxiao Shi64d99f22017-01-21 23:06:36 +0000310
311 parseConfig(CONFIG_WITH_MCAST, false);
312 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500313 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), netifsV4.size());
314 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), netifsV6.size());
Junxiao Shi64d99f22017-01-21 23:06:36 +0000315
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -0400316 BOOST_REQUIRE_EQUAL(factory.getChannels().size(), 2);
317 for (const auto& face : this->listUdp4McastFaces()) {
318 BOOST_REQUIRE(face->getChannel().lock());
319 BOOST_CHECK_EQUAL(face->getChannel().lock()->getUri().getScheme(), "udp4");
320 }
321
322 for (const auto& face : this->listUdp6McastFaces()) {
323 BOOST_REQUIRE(face->getChannel().lock());
324 BOOST_CHECK_EQUAL(face->getChannel().lock()->getUri().getScheme(), "udp6");
325 }
326
Junxiao Shi64d99f22017-01-21 23:06:36 +0000327 parseConfig(CONFIG_WITHOUT_MCAST, false);
328 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400329 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500330 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000331}
332
Davide Pesaventobb734df2017-10-24 18:05:36 -0400333BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpFactoryMcastFixture)
Teng Liangfe4fce32017-03-29 04:49:38 +0000334{
335#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500336 // need superuser privileges to create multicast faces on Linux
Teng Liangfe4fce32017-03-29 04:49:38 +0000337 SKIP_IF_NOT_SUPERUSER();
338#endif // __linux__
339 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
340
341 const std::string CONFIG = R"CONFIG(
342 face_system
343 {
344 udp
345 {
346 mcast_ad_hoc yes
347 }
348 }
349 )CONFIG";
350
351 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500352 BOOST_CHECK_EQUAL(this->listUdp4McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV4.size());
353 BOOST_CHECK_EQUAL(this->listUdp6McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV6.size());
Teng Liangfe4fce32017-03-29 04:49:38 +0000354}
355
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500356BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV4, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000357{
358#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500359 // need superuser privileges to create multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000360 SKIP_IF_NOT_SUPERUSER();
361#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500362 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000363
364 const std::string CONFIG1 = R"CONFIG(
365 face_system
366 {
367 udp
368 {
369 mcast_group 239.66.30.1
370 mcast_port 7011
371 }
372 }
373 )CONFIG";
374 const std::string CONFIG2 = R"CONFIG(
375 face_system
376 {
377 udp
378 {
379 mcast_group 239.66.30.2
380 mcast_port 7012
381 }
382 }
383 )CONFIG";
384
385 parseConfig(CONFIG1, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400386 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500387 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
388 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.1:7011"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000389
390 parseConfig(CONFIG2, false);
391 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400392 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500393 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
394 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.2:7012"));
395}
396
397BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV6, UdpFactoryMcastFixture)
398{
399#ifdef __linux__
400 // need superuser privileges to create multicast faces on Linux
401 SKIP_IF_NOT_SUPERUSER();
402#endif // __linux__
403 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
404
405 const std::string CONFIG1 = R"CONFIG(
406 face_system
407 {
408 udp
409 {
410 mcast_group_v6 ff02::1101
411 mcast_port_v6 7011
412 }
413 }
414 )CONFIG";
415 const std::string CONFIG2 = R"CONFIG(
416 face_system
417 {
418 udp
419 {
420 mcast_group_v6 ff02::1102
421 mcast_port_v6 7012
422 }
423 }
424 )CONFIG";
425
426 parseConfig(CONFIG1, false);
427 auto udpMcastFaces = this->listUdp6McastFaces();
428 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400429 auto uri = udpMcastFaces.front()->getRemoteUri();
430 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
431 // check the address ignoring the scope id
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400432 auto addr = boost::asio::ip::make_address_v6(uri.getHost());
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400433 addr.scope_id(0);
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400434 BOOST_CHECK_EQUAL(addr, boost::asio::ip::make_address_v6("ff02::1101"));
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400435 BOOST_CHECK_EQUAL(uri.getPort(), "7011");
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500436
437 parseConfig(CONFIG2, false);
438 g_io.poll();
439 udpMcastFaces = this->listUdp6McastFaces();
440 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400441 uri = udpMcastFaces.front()->getRemoteUri();
442 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
443 // check the address ignoring the scope id
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400444 addr = boost::asio::ip::make_address_v6(uri.getHost());
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400445 addr.scope_id(0);
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400446 BOOST_CHECK_EQUAL(addr, boost::asio::ip::make_address_v6("ff02::1102"));
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400447 BOOST_CHECK_EQUAL(uri.getPort(), "7012");
Junxiao Shi64d99f22017-01-21 23:06:36 +0000448}
449
Davide Pesaventobb734df2017-10-24 18:05:36 -0400450BOOST_FIXTURE_TEST_CASE(Whitelist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000451{
452#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500453 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000454 SKIP_IF_NOT_SUPERUSER();
455#endif // __linux__
456 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
457
458 std::string CONFIG = R"CONFIG(
459 face_system
460 {
461 udp
462 {
463 whitelist
464 {
465 ifname %ifname
466 }
467 }
468 }
469 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000470 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000471
472 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500473
Davide Pesaventobb734df2017-10-24 18:05:36 -0400474 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500475 BOOST_CHECK_LE(udpMcastFaces.size(), 1);
476 auto udpMcastFacesV6 = this->listUdp6McastFaces();
477 BOOST_CHECK_LE(udpMcastFacesV6.size(), 1);
478 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
479 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
480 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
481 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000482}
483
Davide Pesaventobb734df2017-10-24 18:05:36 -0400484BOOST_FIXTURE_TEST_CASE(Blacklist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000485{
486#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500487 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000488 SKIP_IF_NOT_SUPERUSER();
489#endif // __linux__
490 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
491
492 std::string CONFIG = R"CONFIG(
493 face_system
494 {
495 udp
496 {
497 blacklist
498 {
499 ifname %ifname
500 }
501 }
502 }
503 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000504 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000505
506 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500507
Davide Pesaventobb734df2017-10-24 18:05:36 -0400508 auto udpMcastFaces = this->listUdp4McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500509 if (!netifsV4.empty())
510 BOOST_CHECK_GE(udpMcastFaces.size(), netifsV4.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500511 auto udpMcastFacesV6 = this->listUdp6McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500512 if (!netifsV6.empty())
513 BOOST_CHECK_GE(udpMcastFacesV6.size(), netifsV6.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500514 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
515 BOOST_CHECK_LT(udpMcastFaces.size(), netifsV4.size() + netifsV6.size());
516 BOOST_CHECK(std::none_of(udpMcastFaces.begin(), udpMcastFaces.end(),
517 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000518}
519
Davide Pesaventobb734df2017-10-24 18:05:36 -0400520BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000521{
522#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500523 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000524 SKIP_IF_NOT_SUPERUSER();
525#endif // __linux__
526 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2);
527
528 std::string CONFIG1 = R"CONFIG(
529 face_system
530 {
531 udp
532 {
533 whitelist
534 {
535 ifname %ifname
536 }
537 }
538 }
539 )CONFIG";
540 std::string CONFIG2 = CONFIG1;
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000541 boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName());
542 boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000543
544 parseConfig(CONFIG1, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500545
Davide Pesaventobb734df2017-10-24 18:05:36 -0400546 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500547 auto udpMcastFacesV6 = this->listUdp6McastFaces();
548 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
549 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
550 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
551 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000552
553 parseConfig(CONFIG2, false);
554 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500555
Davide Pesaventobb734df2017-10-24 18:05:36 -0400556 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500557 udpMcastFacesV6 = this->listUdp6McastFaces();
558 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
559 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
560 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
561 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.back()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000562}
563
Junxiao Shi64d99f22017-01-21 23:06:36 +0000564BOOST_AUTO_TEST_CASE(Omitted)
565{
566 const std::string CONFIG = R"CONFIG(
567 face_system
568 {
569 }
570 )CONFIG";
571
572 parseConfig(CONFIG, true);
573 parseConfig(CONFIG, false);
574
Junxiao Shi64d99f22017-01-21 23:06:36 +0000575 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
576 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500577 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp6", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000578}
579
Davide Pesaventobb734df2017-10-24 18:05:36 -0400580BOOST_AUTO_TEST_CASE(AllDisabled)
581{
582 const std::string CONFIG = R"CONFIG(
583 face_system
584 {
585 udp
586 {
587 enable_v4 no
588 enable_v6 no
589 mcast no
590 }
591 }
592 )CONFIG";
593
594 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
595 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
596}
597
Davide Pesavento494a9552018-02-04 22:16:05 -0500598BOOST_AUTO_TEST_CASE(BadListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000599{
600 const std::string CONFIG = R"CONFIG(
601 face_system
602 {
603 udp
604 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500605 listen hello
606 }
607 }
608 )CONFIG";
609
610 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
611 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
612}
613
Davide Pesavento494a9552018-02-04 22:16:05 -0500614BOOST_AUTO_TEST_CASE(BadPort)
615{
616 // not a number
617 const std::string CONFIG1 = R"CONFIG(
618 face_system
619 {
620 udp
621 {
622 port hello
623 }
624 }
625 )CONFIG";
626
627 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
628 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
629
630 // negative number
631 const std::string CONFIG2 = R"CONFIG(
632 face_system
633 {
634 udp
635 {
636 port -1
637 }
638 }
639 )CONFIG";
640
641 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
642 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
643
644 // out of range
645 const std::string CONFIG3 = R"CONFIG(
646 face_system
647 {
648 udp
649 {
650 port 65536
651 }
652 }
653 )CONFIG";
654
655 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
656 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
657}
658
Davide Pesavento494a9552018-02-04 22:16:05 -0500659BOOST_AUTO_TEST_CASE(BadIdleTimeout)
660{
661 // not a number
662 const std::string CONFIG1 = R"CONFIG(
663 face_system
664 {
665 udp
666 {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000667 idle_timeout hello
668 }
669 }
670 )CONFIG";
671
Davide Pesavento494a9552018-02-04 22:16:05 -0500672 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
673 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
674
675 // negative number
676 const std::string CONFIG2 = R"CONFIG(
677 face_system
678 {
679 udp
680 {
681 idle_timeout -15
682 }
683 }
684 )CONFIG";
685
686 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
687 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000688}
689
Junxiao Shia6286a92021-02-23 06:43:52 -0700690BOOST_AUTO_TEST_CASE(BadMtu)
691{
692 // not a number
693 const std::string CONFIG1 = R"CONFIG(
694 face_system
695 {
696 udp
697 {
698 unicast_mtu hello
699 }
700 }
701 )CONFIG";
702
703 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
704 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
705
706 // underflow
707 const std::string CONFIG2 = R"CONFIG(
708 face_system
709 {
710 udp
711 {
712 unicast_mtu 63
713 }
714 }
715 )CONFIG";
716
717 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
718 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
719
720 // underflow
721 const std::string CONFIG3 = R"CONFIG(
722 face_system
723 {
724 udp
725 {
726 unicast_mtu 8801
727 }
728 }
729 )CONFIG";
730
731 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
732 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
733}
734
Junxiao Shi64d99f22017-01-21 23:06:36 +0000735BOOST_AUTO_TEST_CASE(BadMcast)
736{
737 const std::string CONFIG = R"CONFIG(
738 face_system
739 {
740 udp
741 {
742 mcast hello
743 }
744 }
745 )CONFIG";
746
747 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
748 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
749}
750
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500751BOOST_AUTO_TEST_CASE(BadMcastGroupV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000752{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400753 // not an address
754 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000755 face_system
756 {
757 udp
758 {
759 mcast_group hello
760 }
761 }
762 )CONFIG";
763
Davide Pesaventobb734df2017-10-24 18:05:36 -0400764 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
765 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000766
Davide Pesaventobb734df2017-10-24 18:05:36 -0400767 // non-multicast address
768 const std::string CONFIG2 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000769 face_system
770 {
771 udp
772 {
773 mcast_group 10.0.0.1
774 }
775 }
776 )CONFIG";
777
Davide Pesaventobb734df2017-10-24 18:05:36 -0400778 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
779 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000780
Davide Pesaventobb734df2017-10-24 18:05:36 -0400781 // wrong address family
782 const std::string CONFIG3 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000783 face_system
784 {
785 udp
786 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400787 mcast_group ff02::1234
Junxiao Shi64d99f22017-01-21 23:06:36 +0000788 }
789 }
790 )CONFIG";
791
Davide Pesaventobb734df2017-10-24 18:05:36 -0400792 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
793 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000794}
795
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500796BOOST_AUTO_TEST_CASE(BadMcastGroupV6)
797{
798 // not an address
799 const std::string CONFIG1 = R"CONFIG(
800 face_system
801 {
802 udp
803 {
804 mcast_group_v6 foo
805 }
806 }
807 )CONFIG";
808
809 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
810 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
811
812 // non-multicast address
813 const std::string CONFIG2 = R"CONFIG(
814 face_system
815 {
816 udp
817 {
818 mcast_group_v6 fe80::1234
819 }
820 }
821 )CONFIG";
822
823 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
824 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
825
826 // wrong address family
827 const std::string CONFIG3 = R"CONFIG(
828 face_system
829 {
830 udp
831 {
832 mcast_group_v6 224.0.23.170
833 }
834 }
835 )CONFIG";
836
837 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
838 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
839}
840
841BOOST_AUTO_TEST_CASE(BadMcastPortV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000842{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400843 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000844 face_system
845 {
846 udp
847 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400848 mcast_port hey
Junxiao Shi64d99f22017-01-21 23:06:36 +0000849 }
850 }
851 )CONFIG";
852
Davide Pesaventobb734df2017-10-24 18:05:36 -0400853 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
854 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
855
856 const std::string CONFIG2 = R"CONFIG(
857 face_system
858 {
859 udp
860 {
861 mcast_port 99999
862 }
863 }
864 )CONFIG";
865
866 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
867 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000868}
869
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500870BOOST_AUTO_TEST_CASE(BadMcastPortV6)
871{
872 const std::string CONFIG1 = R"CONFIG(
873 face_system
874 {
875 udp
876 {
877 mcast_port_v6 bar
878 }
879 }
880 )CONFIG";
881
882 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
883 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
884
885 const std::string CONFIG2 = R"CONFIG(
886 face_system
887 {
888 udp
889 {
890 mcast_port_v6 99999
891 }
892 }
893 )CONFIG";
894
895 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
896 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
897}
898
Junxiao Shi64d99f22017-01-21 23:06:36 +0000899BOOST_AUTO_TEST_CASE(UnknownOption)
900{
901 const std::string CONFIG = R"CONFIG(
902 face_system
903 {
904 udp
905 {
906 hello
907 }
908 }
909 )CONFIG";
910
911 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
912 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
913}
914
915BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
916
Junxiao Shicde37ad2015-12-24 01:02:05 -0700917BOOST_AUTO_TEST_CASE(GetChannels)
918{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400919 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700920
Davide Pesaventob15276f2017-07-15 16:27:13 -0400921 std::set<std::string> expected;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400922 expected.insert(createChannel("127.0.0.1", 20070)->getUri().toString());
923 expected.insert(createChannel("127.0.0.1", 20071)->getUri().toString());
924 expected.insert(createChannel("::1", 20071)->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400925 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700926}
927
Davide Pesaventobb734df2017-10-24 18:05:36 -0400928BOOST_FIXTURE_TEST_CASE(CreateChannel, UdpFactoryMcastFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700929{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400930 auto channel1 = createChannel("127.0.0.1", 20070);
931 auto channel1a = createChannel("127.0.0.1", 20070);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700932 BOOST_CHECK_EQUAL(channel1, channel1a);
933 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
934
Davide Pesaventobb734df2017-10-24 18:05:36 -0400935 auto channel2 = createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700936 BOOST_CHECK_NE(channel1, channel2);
937
Davide Pesaventobb734df2017-10-24 18:05:36 -0400938 auto channel3 = createChannel("::1", 20071);
Weiwei Liu72cee942016-02-04 16:49:19 -0700939 BOOST_CHECK_NE(channel2, channel3);
940 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700941
Davide Pesaventobb734df2017-10-24 18:05:36 -0400942#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500943 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400944 SKIP_IF_NOT_SUPERUSER();
945#endif // __linux__
Davide Pesaventobb734df2017-10-24 18:05:36 -0400946
Davide Pesavento19779d82019-02-14 13:40:04 -0500947 // createChannel with a local endpoint that has already been allocated to a UDP multicast face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500948 if (!netifsV4.empty()) {
949 auto mcastFace = createMulticastFace("127.0.0.1", "224.0.0.254", 20072);
950 BOOST_CHECK_EXCEPTION(createChannel("127.0.0.1", 20072), UdpFactory::Error,
951 [] (const UdpFactory::Error& e) {
952 return strcmp(e.what(),
953 "Cannot create UDP channel on 127.0.0.1:20072, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500954 "endpoint already allocated to a UDP multicast face") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500955 });
956 }
957 if (!netifsV6.empty()) {
958 auto mcastFace = createMulticastFace("::1", "ff02::114", 20072);
959 BOOST_CHECK_EXCEPTION(createChannel("::1", 20072), UdpFactory::Error,
960 [] (const UdpFactory::Error& e) {
961 return strcmp(e.what(),
962 "Cannot create UDP channel on [::1]:20072, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500963 "endpoint already allocated to a UDP multicast face") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500964 });
965 }
Weiwei Liu72cee942016-02-04 16:49:19 -0700966}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700967
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500968BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV4, UdpFactoryMcastFixture)
Weiwei Liu72cee942016-02-04 16:49:19 -0700969{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400970#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500971 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400972 SKIP_IF_NOT_SUPERUSER();
973#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500974 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400975
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500976 auto multicastFace1 = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
977 auto multicastFace1a = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
978 auto multicastFace2 = createMulticastFace("127.0.0.1", "224.0.0.254", 20030);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700979 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500980 BOOST_CHECK_NE(multicastFace1, multicastFace2);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700981
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500982 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V4);
983 if (!address.is_unspecified()) {
984 auto multicastFace3 = createMulticastFace(address.to_string(), "224.0.0.254", 20070);
985 BOOST_CHECK_NE(multicastFace1, multicastFace3);
986 BOOST_CHECK_NE(multicastFace2, multicastFace3);
987 }
988
989 // create with a local endpoint already used by a channel
Davide Pesaventobb734df2017-10-24 18:05:36 -0400990 auto channel = createChannel("127.0.0.1", 20071);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500991 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.254", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700992 [] (const UdpFactory::Error& e) {
993 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500994 "Cannot create UDP multicast face on 127.0.0.1:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500995 "endpoint already allocated to a UDP channel") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700996 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700997
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500998 // create with a local endpoint already used by a multicast face on a different multicast group
Davide Pesaventobb734df2017-10-24 18:05:36 -0400999 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.42", 20070), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -07001000 [] (const UdpFactory::Error& e) {
1001 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001002 "Cannot create UDP multicast face on 127.0.0.1:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -05001003 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -07001004 });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001005}
Junxiao Shicde37ad2015-12-24 01:02:05 -07001006
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001007BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV6, UdpFactoryMcastFixture)
1008{
1009#ifdef __linux__
1010 // need superuser privileges to create multicast faces on Linux
1011 SKIP_IF_NOT_SUPERUSER();
1012#endif // __linux__
1013 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
1014
1015 auto multicastFace1 = createMulticastFace("::1", "ff02::114", 20070);
1016 auto multicastFace1a = createMulticastFace("::1", "ff02::114", 20070);
1017 auto multicastFace2 = createMulticastFace("::1", "ff02::114", 20030);
1018 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
1019 BOOST_CHECK_NE(multicastFace1, multicastFace2);
1020
1021 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V6);
1022 if (!address.is_unspecified()) {
1023 auto multicastFace3 = createMulticastFace(address.to_string(), "ff02::114", 20070);
1024 BOOST_CHECK_NE(multicastFace1, multicastFace3);
1025 BOOST_CHECK_NE(multicastFace2, multicastFace3);
1026 }
1027
1028 // create with a local endpoint already used by a channel
1029 auto channel = createChannel("::1", 20071);
1030 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::114", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -07001031 [] (const UdpFactory::Error& e) {
1032 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001033 "Cannot create UDP multicast face on [::1]:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -05001034 "endpoint already allocated to a UDP channel") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05001035 });
1036
1037 // create with a local endpoint already used by a multicast face on a different multicast group
1038 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::42", 20070), UdpFactory::Error,
1039 [] (const UdpFactory::Error& e) {
1040 return strcmp(e.what(),
1041 "Cannot create UDP multicast face on [::1]:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -05001042 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -07001043 });
Junxiao Shicde37ad2015-12-24 01:02:05 -07001044}
1045
Davide Pesaventob15276f2017-07-15 16:27:13 -04001046BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -07001047{
Eric Newberry42602412016-08-27 09:33:18 -07001048 createFace(factory,
1049 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001050 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001051 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001052 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001053
Davide Pesaventobb734df2017-10-24 18:05:36 -04001054 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -07001055
Eric Newberry42602412016-08-27 09:33:18 -07001056 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001057 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001058 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001059 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001060 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Eric Newberry78e32b02017-04-01 14:34:44 +00001061
Eric Newberry42602412016-08-27 09:33:18 -07001062 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001063 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001064 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001065 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001066 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001067
Eric Newberry42602412016-08-27 09:33:18 -07001068 createFace(factory,
1069 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001070 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001071 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry2642cd22017-07-13 21:34:53 -04001072 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1073
Eric Newberry2642cd22017-07-13 21:34:53 -04001074 createFace(factory,
1075 FaceUri("udp4://127.0.0.1:20073"),
1076 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001077 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, true, false},
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001078 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1079
1080 createFace(factory,
1081 FaceUri("udp4://127.0.0.1:20073"),
1082 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001083 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, true},
1084 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1085
1086 createFace(factory,
1087 FaceUri("udp4://127.0.0.1:20074"),
1088 {},
1089 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, 1000, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001090 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001091}
1092
Davide Pesaventob15276f2017-07-15 16:27:13 -04001093BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -07001094{
Davide Pesaventobb734df2017-10-24 18:05:36 -04001095 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -07001096
Eric Newberry42602412016-08-27 09:33:18 -07001097 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001098 FaceUri("udp4://127.0.0.1:20072"),
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001099 FaceUri("udp4://127.0.0.1:20071"),
Eric Newberry812d6152018-06-06 15:06:01 -07001100 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001101 {CreateFaceExpectedResult::FAILURE, 406,
1102 "Unicast UDP faces cannot be created with a LocalUri"});
1103
1104 createFace(factory,
1105 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001106 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001107 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001108 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001109 "Outgoing UDP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +00001110
1111 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001112 FaceUri("udp4://233.252.0.1:23252"),
1113 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001114 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry78e32b02017-04-01 14:34:44 +00001115 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001116 "Cannot create multicast UDP faces"});
1117
1118 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001119 FaceUri("udp4://127.0.0.1:20072"),
1120 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001121 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, true, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001122 {CreateFaceExpectedResult::FAILURE, 406,
1123 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001124}
1125
Junxiao Shicde37ad2015-12-24 01:02:05 -07001126BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
1127BOOST_AUTO_TEST_SUITE_END() // Face
1128
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04001129} // namespace nfd::tests