blob: 0c10276cac38ce3a576e3d371b66028be651411a [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shicde37ad2015-12-24 01:02:05 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "face/udp-factory.hpp"
27
Junxiao Shi64d99f22017-01-21 23:06:36 +000028#include "face-system-fixture.hpp"
Davide Pesaventob15276f2017-07-15 16:27:13 -040029#include "factory-test-common.hpp"
Davide Pesaventob15276f2017-07-15 16:27:13 -040030
Junxiao Shic31080d2017-01-24 15:10:12 +000031#include <boost/algorithm/string/replace.hpp>
Junxiao Shicde37ad2015-12-24 01:02:05 -070032
33namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000034namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070035namespace tests {
36
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040037class UdpFactoryFixture : public FaceSystemFactoryFixture<UdpFactory>
38{
39protected:
40 shared_ptr<UdpChannel>
Davide Pesaventobb734df2017-10-24 18:05:36 -040041 createChannel(const std::string& localIp, uint16_t localPort)
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040042 {
Davide Pesavento9c33b902018-05-20 01:30:29 -040043 udp::Endpoint endpoint(boost::asio::ip::address::from_string(localIp), localPort);
Davide Pesavento494a9552018-02-04 22:16:05 -050044 return factory.createChannel(endpoint, 5_min);
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040045 }
46};
Junxiao Shi0ba6d642017-07-17 00:53:22 +000047
Davide Pesaventobb734df2017-10-24 18:05:36 -040048class UdpFactoryMcastFixture : public UdpFactoryFixture
49{
50protected:
51 UdpFactoryMcastFixture()
52 {
53 for (const auto& netif : collectNetworkInterfaces()) {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050054 // same filtering logic as UdpFactory::applyMcastConfigToNetif()
55 if (netif->isUp() && !netif->isLoopback() && netif->canMulticast()) {
56 bool hasValidIpAddress = false;
57 if (hasAddressFamily(*netif, ndn::net::AddressFamily::V4)) {
58 hasValidIpAddress = true;
59 netifsV4.push_back(netif);
60 }
61 if (hasAddressFamily(*netif, ndn::net::AddressFamily::V6)) {
62 hasValidIpAddress = true;
63 netifsV6.push_back(netif);
64 }
65 if (hasValidIpAddress) {
66 netifs.push_back(netif);
67 }
Davide Pesaventobb734df2017-10-24 18:05:36 -040068 }
69 }
Davide Pesaventobb734df2017-10-24 18:05:36 -040070 this->copyRealNetifsToNetmon();
71 }
72
73 shared_ptr<Face>
74 createMulticastFace(const std::string& localIp, const std::string& mcastIp, uint16_t mcastPort)
75 {
Davide Pesavento9c33b902018-05-20 01:30:29 -040076 auto localAddress = boost::asio::ip::address::from_string(localIp);
77 udp::Endpoint mcastEndpoint(boost::asio::ip::address::from_string(mcastIp), mcastPort);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050078
79 if (localAddress.is_v4()) {
80 BOOST_ASSERT(!netifsV4.empty());
81 return factory.createMulticastFace(netifsV4.front(), localAddress, mcastEndpoint);
82 }
83 else {
84 BOOST_ASSERT(!netifsV6.empty());
85 return factory.createMulticastFace(netifsV6.front(), localAddress, mcastEndpoint);
86 }
87 }
88
89 /** \brief returns a non-loopback IP address suitable for the creation of a UDP multicast face
90 */
91 boost::asio::ip::address
92 findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily af) const
93 {
94 const auto& netifList = af == ndn::net::AddressFamily::V4 ? netifsV4 : netifsV6;
95 for (const auto& netif : netifList) {
96 for (const auto& a : netif->getNetworkAddresses()) {
97 if (a.getFamily() == af && !a.getIp().is_loopback())
98 return a.getIp();
99 }
100 }
101 return {};
Davide Pesaventobb734df2017-10-24 18:05:36 -0400102 }
103
104 std::vector<const Face*>
105 listUdp4McastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
106 {
107 return this->listFacesByScheme("udp4", linkType);
108 }
109
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500110 std::vector<const Face*>
111 listUdp6McastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const
112 {
113 return this->listFacesByScheme("udp6", linkType);
114 }
115
116 /** \brief determine whether \p netif has at least one IP address of the given family
Davide Pesaventobb734df2017-10-24 18:05:36 -0400117 */
118 static bool
119 hasAddressFamily(const NetworkInterface& netif, ndn::net::AddressFamily af)
120 {
121 return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(),
122 [af] (const NetworkAddress& a) { return a.getFamily() == af; });
123 }
124
125 /** \brief determine whether a UDP multicast face is created on \p netif
126 */
127 static bool
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500128 isFaceOnNetif(const Face& face, const NetworkInterface& netif)
Davide Pesaventobb734df2017-10-24 18:05:36 -0400129 {
Davide Pesavento9c33b902018-05-20 01:30:29 -0400130 auto ip = boost::asio::ip::address::from_string(face.getLocalUri().getHost());
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500131 return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(),
Davide Pesaventobb734df2017-10-24 18:05:36 -0400132 [ip] (const NetworkAddress& a) { return a.getIp() == ip; });
133 }
134
135protected:
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500136 /** \brief MulticastUdpTransport-capable network interfaces (IPv4 + IPv6)
137 *
138 * This should be used in test cases that do not depend on a specific address family
Davide Pesaventobb734df2017-10-24 18:05:36 -0400139 */
140 std::vector<shared_ptr<const NetworkInterface>> netifs;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500141
142 /** \brief MulticastUdpTransport-capable network interfaces (IPv4 only)
143 */
144 std::vector<shared_ptr<const NetworkInterface>> netifsV4;
145
146 /** \brief MulticastUdpTransport-capable network interfaces (IPv6 only)
147 */
148 std::vector<shared_ptr<const NetworkInterface>> netifsV6;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400149};
150
151#define SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(n) \
152 do { \
153 if (this->netifs.size() < (n)) { \
154 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
155 " or more MulticastUdpTransport-capable network interfaces"); \
156 return; \
157 } \
158 } while (false)
159
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500160#define SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(n) \
161 do { \
162 if (this->netifsV4.size() < (n)) { \
163 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
164 " or more IPv4 MulticastUdpTransport-capable network interfaces"); \
165 return; \
166 } \
167 } while (false)
168
169#define SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(n) \
170 do { \
171 if (this->netifsV6.size() < (n)) { \
172 BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \
173 " or more IPv6 MulticastUdpTransport-capable network interfaces"); \
174 return; \
175 } \
176 } while (false)
177
Junxiao Shicde37ad2015-12-24 01:02:05 -0700178BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000179BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, UdpFactoryFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700180
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000181BOOST_AUTO_TEST_SUITE(ProcessConfig)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000182
Davide Pesavento494a9552018-02-04 22:16:05 -0500183using nfd::Face;
184
185BOOST_AUTO_TEST_CASE(Defaults)
186{
187 const std::string CONFIG = R"CONFIG(
188 face_system
189 {
190 udp
191 }
192 )CONFIG";
193
194 parseConfig(CONFIG, true);
195 parseConfig(CONFIG, false);
196
197 checkChannelListEqual(factory, {"udp4://0.0.0.0:6363", "udp6://[::]:6363"});
198 auto channels = factory.getChannels();
199 BOOST_CHECK(std::all_of(channels.begin(), channels.end(),
Davide Pesaventod27841b2018-11-13 00:22:24 -0500200 [] (const auto& ch) { return ch->isListening(); }));
Davide Pesavento494a9552018-02-04 22:16:05 -0500201}
202
203BOOST_AUTO_TEST_CASE(DisableListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000204{
205 const std::string CONFIG = R"CONFIG(
206 face_system
207 {
208 udp
209 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500210 listen no
Junxiao Shi64d99f22017-01-21 23:06:36 +0000211 port 7001
Junxiao Shi64d99f22017-01-21 23:06:36 +0000212 mcast no
213 }
214 }
215 )CONFIG";
216
217 parseConfig(CONFIG, true);
218 parseConfig(CONFIG, false);
219
Junxiao Shi64d99f22017-01-21 23:06:36 +0000220 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"});
Davide Pesavento494a9552018-02-04 22:16:05 -0500221 auto channels = factory.getChannels();
222 BOOST_CHECK(std::none_of(channels.begin(), channels.end(),
Davide Pesaventod27841b2018-11-13 00:22:24 -0500223 [] (const auto& ch) { return ch->isListening(); }));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000224}
225
Davide Pesavento494a9552018-02-04 22:16:05 -0500226BOOST_AUTO_TEST_CASE(DisableV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000227{
228 const std::string CONFIG = R"CONFIG(
229 face_system
230 {
231 udp
232 {
233 port 7001
234 enable_v4 no
235 enable_v6 yes
236 mcast no
237 }
238 }
239 )CONFIG";
240
241 parseConfig(CONFIG, true);
242 parseConfig(CONFIG, false);
243
Junxiao Shi64d99f22017-01-21 23:06:36 +0000244 checkChannelListEqual(factory, {"udp6://[::]:7001"});
245}
246
Davide Pesavento494a9552018-02-04 22:16:05 -0500247BOOST_AUTO_TEST_CASE(DisableV6)
248{
249 const std::string CONFIG = R"CONFIG(
250 face_system
251 {
252 udp
253 {
254 port 7001
255 enable_v4 yes
256 enable_v6 no
257 mcast no
258 }
259 }
260 )CONFIG";
261
262 parseConfig(CONFIG, true);
263 parseConfig(CONFIG, false);
264
265 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"});
266}
267
Davide Pesaventobb734df2017-10-24 18:05:36 -0400268BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000269{
Junxiao Shi64d99f22017-01-21 23:06:36 +0000270 const std::string CONFIG_WITH_MCAST = R"CONFIG(
271 face_system
272 {
273 udp
274 {
275 mcast yes
276 }
277 }
278 )CONFIG";
279 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
280 face_system
281 {
282 udp
283 {
284 mcast no
285 }
286 }
287 )CONFIG";
288
289 parseConfig(CONFIG_WITHOUT_MCAST, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400290 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500291 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000292
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500293#ifdef __linux__
294 // need superuser privileges to create multicast faces on Linux
295 SKIP_IF_NOT_SUPERUSER();
296#endif // __linux__
Junxiao Shi64d99f22017-01-21 23:06:36 +0000297
298 parseConfig(CONFIG_WITH_MCAST, false);
299 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500300 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), netifsV4.size());
301 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), netifsV6.size());
Junxiao Shi64d99f22017-01-21 23:06:36 +0000302
303 parseConfig(CONFIG_WITHOUT_MCAST, false);
304 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400305 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500306 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000307}
308
Davide Pesaventobb734df2017-10-24 18:05:36 -0400309BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpFactoryMcastFixture)
Teng Liangfe4fce32017-03-29 04:49:38 +0000310{
311#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500312 // need superuser privileges to create multicast faces on Linux
Teng Liangfe4fce32017-03-29 04:49:38 +0000313 SKIP_IF_NOT_SUPERUSER();
314#endif // __linux__
315 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
316
317 const std::string CONFIG = R"CONFIG(
318 face_system
319 {
320 udp
321 {
322 mcast_ad_hoc yes
323 }
324 }
325 )CONFIG";
326
327 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500328 BOOST_CHECK_EQUAL(this->listUdp4McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV4.size());
329 BOOST_CHECK_EQUAL(this->listUdp6McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV6.size());
Teng Liangfe4fce32017-03-29 04:49:38 +0000330}
331
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500332BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV4, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000333{
334#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500335 // need superuser privileges to create multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000336 SKIP_IF_NOT_SUPERUSER();
337#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500338 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000339
340 const std::string CONFIG1 = R"CONFIG(
341 face_system
342 {
343 udp
344 {
345 mcast_group 239.66.30.1
346 mcast_port 7011
347 }
348 }
349 )CONFIG";
350 const std::string CONFIG2 = R"CONFIG(
351 face_system
352 {
353 udp
354 {
355 mcast_group 239.66.30.2
356 mcast_port 7012
357 }
358 }
359 )CONFIG";
360
361 parseConfig(CONFIG1, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400362 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500363 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
364 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.1:7011"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000365
366 parseConfig(CONFIG2, false);
367 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400368 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500369 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
370 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.2:7012"));
371}
372
373BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV6, UdpFactoryMcastFixture)
374{
375#ifdef __linux__
376 // need superuser privileges to create multicast faces on Linux
377 SKIP_IF_NOT_SUPERUSER();
378#endif // __linux__
379 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
380
381 const std::string CONFIG1 = R"CONFIG(
382 face_system
383 {
384 udp
385 {
386 mcast_group_v6 ff02::1101
387 mcast_port_v6 7011
388 }
389 }
390 )CONFIG";
391 const std::string CONFIG2 = R"CONFIG(
392 face_system
393 {
394 udp
395 {
396 mcast_group_v6 ff02::1102
397 mcast_port_v6 7012
398 }
399 }
400 )CONFIG";
401
402 parseConfig(CONFIG1, false);
403 auto udpMcastFaces = this->listUdp6McastFaces();
404 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
405 auto expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1101");
406 expectedAddr.scope_id(netifsV6.front()->getIndex());
407 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7011)));
408
409 parseConfig(CONFIG2, false);
410 g_io.poll();
411 udpMcastFaces = this->listUdp6McastFaces();
412 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
413 expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1102");
414 expectedAddr.scope_id(netifsV6.front()->getIndex());
415 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7012)));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000416}
417
Davide Pesaventobb734df2017-10-24 18:05:36 -0400418BOOST_FIXTURE_TEST_CASE(Whitelist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000419{
420#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500421 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000422 SKIP_IF_NOT_SUPERUSER();
423#endif // __linux__
424 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
425
426 std::string CONFIG = R"CONFIG(
427 face_system
428 {
429 udp
430 {
431 whitelist
432 {
433 ifname %ifname
434 }
435 }
436 }
437 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000438 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000439
440 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500441
Davide Pesaventobb734df2017-10-24 18:05:36 -0400442 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500443 BOOST_CHECK_LE(udpMcastFaces.size(), 1);
444 auto udpMcastFacesV6 = this->listUdp6McastFaces();
445 BOOST_CHECK_LE(udpMcastFacesV6.size(), 1);
446 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
447 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
448 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
449 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000450}
451
Davide Pesaventobb734df2017-10-24 18:05:36 -0400452BOOST_FIXTURE_TEST_CASE(Blacklist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000453{
454#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500455 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000456 SKIP_IF_NOT_SUPERUSER();
457#endif // __linux__
458 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
459
460 std::string CONFIG = R"CONFIG(
461 face_system
462 {
463 udp
464 {
465 blacklist
466 {
467 ifname %ifname
468 }
469 }
470 }
471 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000472 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000473
474 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500475
Davide Pesaventobb734df2017-10-24 18:05:36 -0400476 auto udpMcastFaces = this->listUdp4McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500477 if (!netifsV4.empty())
478 BOOST_CHECK_GE(udpMcastFaces.size(), netifsV4.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500479 auto udpMcastFacesV6 = this->listUdp6McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500480 if (!netifsV6.empty())
481 BOOST_CHECK_GE(udpMcastFacesV6.size(), netifsV6.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500482 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
483 BOOST_CHECK_LT(udpMcastFaces.size(), netifsV4.size() + netifsV6.size());
484 BOOST_CHECK(std::none_of(udpMcastFaces.begin(), udpMcastFaces.end(),
485 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000486}
487
Davide Pesaventobb734df2017-10-24 18:05:36 -0400488BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000489{
490#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500491 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000492 SKIP_IF_NOT_SUPERUSER();
493#endif // __linux__
494 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2);
495
496 std::string CONFIG1 = R"CONFIG(
497 face_system
498 {
499 udp
500 {
501 whitelist
502 {
503 ifname %ifname
504 }
505 }
506 }
507 )CONFIG";
508 std::string CONFIG2 = CONFIG1;
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000509 boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName());
510 boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000511
512 parseConfig(CONFIG1, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500513
Davide Pesaventobb734df2017-10-24 18:05:36 -0400514 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500515 auto udpMcastFacesV6 = this->listUdp6McastFaces();
516 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
517 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
518 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
519 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000520
521 parseConfig(CONFIG2, false);
522 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500523
Davide Pesaventobb734df2017-10-24 18:05:36 -0400524 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500525 udpMcastFacesV6 = this->listUdp6McastFaces();
526 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
527 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
528 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
529 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.back()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000530}
531
Junxiao Shi64d99f22017-01-21 23:06:36 +0000532BOOST_AUTO_TEST_CASE(Omitted)
533{
534 const std::string CONFIG = R"CONFIG(
535 face_system
536 {
537 }
538 )CONFIG";
539
540 parseConfig(CONFIG, true);
541 parseConfig(CONFIG, false);
542
Junxiao Shi64d99f22017-01-21 23:06:36 +0000543 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
544 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500545 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp6", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000546}
547
Davide Pesaventobb734df2017-10-24 18:05:36 -0400548BOOST_AUTO_TEST_CASE(AllDisabled)
549{
550 const std::string CONFIG = R"CONFIG(
551 face_system
552 {
553 udp
554 {
555 enable_v4 no
556 enable_v6 no
557 mcast no
558 }
559 }
560 )CONFIG";
561
562 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
563 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
564}
565
Davide Pesavento494a9552018-02-04 22:16:05 -0500566BOOST_AUTO_TEST_CASE(BadListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000567{
568 const std::string CONFIG = R"CONFIG(
569 face_system
570 {
571 udp
572 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500573 listen hello
574 }
575 }
576 )CONFIG";
577
578 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
579 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
580}
581
582BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(BadPort, 2) // Bug #4489
583BOOST_AUTO_TEST_CASE(BadPort)
584{
585 // not a number
586 const std::string CONFIG1 = R"CONFIG(
587 face_system
588 {
589 udp
590 {
591 port hello
592 }
593 }
594 )CONFIG";
595
596 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
597 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
598
599 // negative number
600 const std::string CONFIG2 = R"CONFIG(
601 face_system
602 {
603 udp
604 {
605 port -1
606 }
607 }
608 )CONFIG";
609
610 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
611 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
612
613 // out of range
614 const std::string CONFIG3 = R"CONFIG(
615 face_system
616 {
617 udp
618 {
619 port 65536
620 }
621 }
622 )CONFIG";
623
624 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
625 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
626}
627
628BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(BadIdleTimeout, 2) // Bug #4489
629BOOST_AUTO_TEST_CASE(BadIdleTimeout)
630{
631 // not a number
632 const std::string CONFIG1 = R"CONFIG(
633 face_system
634 {
635 udp
636 {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000637 idle_timeout hello
638 }
639 }
640 )CONFIG";
641
Davide Pesavento494a9552018-02-04 22:16:05 -0500642 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
643 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
644
645 // negative number
646 const std::string CONFIG2 = R"CONFIG(
647 face_system
648 {
649 udp
650 {
651 idle_timeout -15
652 }
653 }
654 )CONFIG";
655
656 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
657 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000658}
659
660BOOST_AUTO_TEST_CASE(BadMcast)
661{
662 const std::string CONFIG = R"CONFIG(
663 face_system
664 {
665 udp
666 {
667 mcast hello
668 }
669 }
670 )CONFIG";
671
672 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
673 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
674}
675
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500676BOOST_AUTO_TEST_CASE(BadMcastGroupV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000677{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400678 // not an address
679 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000680 face_system
681 {
682 udp
683 {
684 mcast_group hello
685 }
686 }
687 )CONFIG";
688
Davide Pesaventobb734df2017-10-24 18:05:36 -0400689 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
690 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000691
Davide Pesaventobb734df2017-10-24 18:05:36 -0400692 // non-multicast address
693 const std::string CONFIG2 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000694 face_system
695 {
696 udp
697 {
698 mcast_group 10.0.0.1
699 }
700 }
701 )CONFIG";
702
Davide Pesaventobb734df2017-10-24 18:05:36 -0400703 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
704 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000705
Davide Pesaventobb734df2017-10-24 18:05:36 -0400706 // wrong address family
707 const std::string CONFIG3 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000708 face_system
709 {
710 udp
711 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400712 mcast_group ff02::1234
Junxiao Shi64d99f22017-01-21 23:06:36 +0000713 }
714 }
715 )CONFIG";
716
Davide Pesaventobb734df2017-10-24 18:05:36 -0400717 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
718 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000719}
720
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500721BOOST_AUTO_TEST_CASE(BadMcastGroupV6)
722{
723 // not an address
724 const std::string CONFIG1 = R"CONFIG(
725 face_system
726 {
727 udp
728 {
729 mcast_group_v6 foo
730 }
731 }
732 )CONFIG";
733
734 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
735 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
736
737 // non-multicast address
738 const std::string CONFIG2 = R"CONFIG(
739 face_system
740 {
741 udp
742 {
743 mcast_group_v6 fe80::1234
744 }
745 }
746 )CONFIG";
747
748 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
749 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
750
751 // wrong address family
752 const std::string CONFIG3 = R"CONFIG(
753 face_system
754 {
755 udp
756 {
757 mcast_group_v6 224.0.23.170
758 }
759 }
760 )CONFIG";
761
762 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
763 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
764}
765
766BOOST_AUTO_TEST_CASE(BadMcastPortV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000767{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400768 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000769 face_system
770 {
771 udp
772 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400773 mcast_port hey
Junxiao Shi64d99f22017-01-21 23:06:36 +0000774 }
775 }
776 )CONFIG";
777
Davide Pesaventobb734df2017-10-24 18:05:36 -0400778 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
779 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
780
781 const std::string CONFIG2 = R"CONFIG(
782 face_system
783 {
784 udp
785 {
786 mcast_port 99999
787 }
788 }
789 )CONFIG";
790
791 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
792 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000793}
794
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500795BOOST_AUTO_TEST_CASE(BadMcastPortV6)
796{
797 const std::string CONFIG1 = R"CONFIG(
798 face_system
799 {
800 udp
801 {
802 mcast_port_v6 bar
803 }
804 }
805 )CONFIG";
806
807 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
808 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
809
810 const std::string CONFIG2 = R"CONFIG(
811 face_system
812 {
813 udp
814 {
815 mcast_port_v6 99999
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
Junxiao Shi64d99f22017-01-21 23:06:36 +0000824BOOST_AUTO_TEST_CASE(UnknownOption)
825{
826 const std::string CONFIG = R"CONFIG(
827 face_system
828 {
829 udp
830 {
831 hello
832 }
833 }
834 )CONFIG";
835
836 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
837 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
838}
839
840BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
841
Junxiao Shicde37ad2015-12-24 01:02:05 -0700842BOOST_AUTO_TEST_CASE(GetChannels)
843{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400844 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700845
Davide Pesaventob15276f2017-07-15 16:27:13 -0400846 std::set<std::string> expected;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400847 expected.insert(createChannel("127.0.0.1", 20070)->getUri().toString());
848 expected.insert(createChannel("127.0.0.1", 20071)->getUri().toString());
849 expected.insert(createChannel("::1", 20071)->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400850 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700851}
852
Davide Pesaventobb734df2017-10-24 18:05:36 -0400853BOOST_FIXTURE_TEST_CASE(CreateChannel, UdpFactoryMcastFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700854{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400855 auto channel1 = createChannel("127.0.0.1", 20070);
856 auto channel1a = createChannel("127.0.0.1", 20070);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700857 BOOST_CHECK_EQUAL(channel1, channel1a);
858 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
859
Davide Pesaventobb734df2017-10-24 18:05:36 -0400860 auto channel2 = createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700861 BOOST_CHECK_NE(channel1, channel2);
862
Davide Pesaventobb734df2017-10-24 18:05:36 -0400863 auto channel3 = createChannel("::1", 20071);
Weiwei Liu72cee942016-02-04 16:49:19 -0700864 BOOST_CHECK_NE(channel2, channel3);
865 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700866
Davide Pesaventobb734df2017-10-24 18:05:36 -0400867#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500868 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400869 SKIP_IF_NOT_SUPERUSER();
870#endif // __linux__
Davide Pesaventobb734df2017-10-24 18:05:36 -0400871
Davide Pesavento19779d82019-02-14 13:40:04 -0500872 // createChannel with a local endpoint that has already been allocated to a UDP multicast face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500873 if (!netifsV4.empty()) {
874 auto mcastFace = createMulticastFace("127.0.0.1", "224.0.0.254", 20072);
875 BOOST_CHECK_EXCEPTION(createChannel("127.0.0.1", 20072), UdpFactory::Error,
876 [] (const UdpFactory::Error& e) {
877 return strcmp(e.what(),
878 "Cannot create UDP channel on 127.0.0.1:20072, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500879 "endpoint already allocated to a UDP multicast face") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500880 });
881 }
882 if (!netifsV6.empty()) {
883 auto mcastFace = createMulticastFace("::1", "ff02::114", 20072);
884 BOOST_CHECK_EXCEPTION(createChannel("::1", 20072), UdpFactory::Error,
885 [] (const UdpFactory::Error& e) {
886 return strcmp(e.what(),
887 "Cannot create UDP channel on [::1]:20072, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500888 "endpoint already allocated to a UDP multicast face") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500889 });
890 }
Weiwei Liu72cee942016-02-04 16:49:19 -0700891}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700892
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500893BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV4, UdpFactoryMcastFixture)
Weiwei Liu72cee942016-02-04 16:49:19 -0700894{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400895#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500896 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400897 SKIP_IF_NOT_SUPERUSER();
898#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500899 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400900
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500901 auto multicastFace1 = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
902 auto multicastFace1a = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
903 auto multicastFace2 = createMulticastFace("127.0.0.1", "224.0.0.254", 20030);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700904 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500905 BOOST_CHECK_NE(multicastFace1, multicastFace2);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700906
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500907 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V4);
908 if (!address.is_unspecified()) {
909 auto multicastFace3 = createMulticastFace(address.to_string(), "224.0.0.254", 20070);
910 BOOST_CHECK_NE(multicastFace1, multicastFace3);
911 BOOST_CHECK_NE(multicastFace2, multicastFace3);
912 }
913
914 // create with a local endpoint already used by a channel
Davide Pesaventobb734df2017-10-24 18:05:36 -0400915 auto channel = createChannel("127.0.0.1", 20071);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500916 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.254", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700917 [] (const UdpFactory::Error& e) {
918 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500919 "Cannot create UDP multicast face on 127.0.0.1:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500920 "endpoint already allocated to a UDP channel") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700921 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700922
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500923 // create with a local endpoint already used by a multicast face on a different multicast group
Davide Pesaventobb734df2017-10-24 18:05:36 -0400924 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.42", 20070), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700925 [] (const UdpFactory::Error& e) {
926 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500927 "Cannot create UDP multicast face on 127.0.0.1:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500928 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700929 });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500930}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700931
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500932BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV6, UdpFactoryMcastFixture)
933{
934#ifdef __linux__
935 // need superuser privileges to create multicast faces on Linux
936 SKIP_IF_NOT_SUPERUSER();
937#endif // __linux__
938 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
939
940 auto multicastFace1 = createMulticastFace("::1", "ff02::114", 20070);
941 auto multicastFace1a = createMulticastFace("::1", "ff02::114", 20070);
942 auto multicastFace2 = createMulticastFace("::1", "ff02::114", 20030);
943 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
944 BOOST_CHECK_NE(multicastFace1, multicastFace2);
945
946 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V6);
947 if (!address.is_unspecified()) {
948 auto multicastFace3 = createMulticastFace(address.to_string(), "ff02::114", 20070);
949 BOOST_CHECK_NE(multicastFace1, multicastFace3);
950 BOOST_CHECK_NE(multicastFace2, multicastFace3);
951 }
952
953 // create with a local endpoint already used by a channel
954 auto channel = createChannel("::1", 20071);
955 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::114", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700956 [] (const UdpFactory::Error& e) {
957 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500958 "Cannot create UDP multicast face on [::1]:20071, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500959 "endpoint already allocated to a UDP channel") == 0;
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500960 });
961
962 // create with a local endpoint already used by a multicast face on a different multicast group
963 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::42", 20070), UdpFactory::Error,
964 [] (const UdpFactory::Error& e) {
965 return strcmp(e.what(),
966 "Cannot create UDP multicast face on [::1]:20070, "
Davide Pesavento19779d82019-02-14 13:40:04 -0500967 "endpoint already allocated to a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700968 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700969}
970
Davide Pesaventob15276f2017-07-15 16:27:13 -0400971BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700972{
Eric Newberry42602412016-08-27 09:33:18 -0700973 createFace(factory,
974 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000975 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700976 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700977 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700978
Davide Pesaventobb734df2017-10-24 18:05:36 -0400979 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700980
Eric Newberry42602412016-08-27 09:33:18 -0700981 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400982 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000983 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700984 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700985 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Eric Newberry78e32b02017-04-01 14:34:44 +0000986
Eric Newberry42602412016-08-27 09:33:18 -0700987 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400988 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000989 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700990 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700991 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700992
Eric Newberry42602412016-08-27 09:33:18 -0700993 createFace(factory,
994 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000995 {},
Eric Newberry812d6152018-06-06 15:06:01 -0700996 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, false},
Eric Newberry2642cd22017-07-13 21:34:53 -0400997 {CreateFaceExpectedResult::SUCCESS, 0, ""});
998
Eric Newberry2642cd22017-07-13 21:34:53 -0400999 createFace(factory,
1000 FaceUri("udp4://127.0.0.1:20073"),
1001 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001002 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, true, false},
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001003 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1004
1005 createFace(factory,
1006 FaceUri("udp4://127.0.0.1:20073"),
1007 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001008 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, {}, false, false, true},
1009 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1010
1011 createFace(factory,
1012 FaceUri("udp4://127.0.0.1:20074"),
1013 {},
1014 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, 1000, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001015 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001016}
1017
Davide Pesaventob15276f2017-07-15 16:27:13 -04001018BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -07001019{
Davide Pesaventobb734df2017-10-24 18:05:36 -04001020 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -07001021
Eric Newberry42602412016-08-27 09:33:18 -07001022 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001023 FaceUri("udp4://127.0.0.1:20072"),
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001024 FaceUri("udp4://127.0.0.1:20071"),
Eric Newberry812d6152018-06-06 15:06:01 -07001025 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001026 {CreateFaceExpectedResult::FAILURE, 406,
1027 "Unicast UDP faces cannot be created with a LocalUri"});
1028
1029 createFace(factory,
1030 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001031 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001032 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001033 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001034 "Outgoing UDP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +00001035
1036 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001037 FaceUri("udp4://233.252.0.1:23252"),
1038 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001039 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, false, false, false},
Eric Newberry78e32b02017-04-01 14:34:44 +00001040 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001041 "Cannot create multicast UDP faces"});
1042
1043 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001044 FaceUri("udp4://127.0.0.1:20072"),
1045 {},
Eric Newberry812d6152018-06-06 15:06:01 -07001046 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, {}, true, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001047 {CreateFaceExpectedResult::FAILURE, 406,
1048 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001049}
1050
Junxiao Shicde37ad2015-12-24 01:02:05 -07001051BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
1052BOOST_AUTO_TEST_SUITE_END() // Face
1053
1054} // namespace tests
Junxiao Shi64d99f22017-01-21 23:06:36 +00001055} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -07001056} // namespace nfd