blob: 75767d58f2d9936792b462eb47e01cb077a8f519 [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/*
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -05003 * Copyright (c) 2014-2018, 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>
Davide Pesavento4b89a6e2017-10-07 15:29:50 -040032#include <ndn-cxx/net/address-converter.hpp>
Junxiao Shicde37ad2015-12-24 01:02:05 -070033
34namespace nfd {
Junxiao Shi64d99f22017-01-21 23:06:36 +000035namespace face {
Junxiao Shicde37ad2015-12-24 01:02:05 -070036namespace tests {
37
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 Pesaventobb734df2017-10-24 18:05:36 -040044 udp::Endpoint endpoint(ndn::ip::addressFromString(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 {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -050077 auto localAddress = ndn::ip::addressFromString(localIp);
Davide Pesaventobb734df2017-10-24 18:05:36 -040078 udp::Endpoint mcastEndpoint(ndn::ip::addressFromString(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 {
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500131 auto ip = ndn::ip::addressFromString(face.getLocalUri().getHost());
132 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"});
199 auto channels = factory.getChannels();
200 BOOST_CHECK(std::all_of(channels.begin(), channels.end(),
201 [] (const shared_ptr<const Channel>& ch) { return ch->isListening(); }));
202}
203
204BOOST_AUTO_TEST_CASE(DisableListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000205{
206 const std::string CONFIG = R"CONFIG(
207 face_system
208 {
209 udp
210 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500211 listen no
Junxiao Shi64d99f22017-01-21 23:06:36 +0000212 port 7001
Junxiao Shi64d99f22017-01-21 23:06:36 +0000213 mcast no
214 }
215 }
216 )CONFIG";
217
218 parseConfig(CONFIG, true);
219 parseConfig(CONFIG, false);
220
Junxiao Shi64d99f22017-01-21 23:06:36 +0000221 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"});
Davide Pesavento494a9552018-02-04 22:16:05 -0500222 auto channels = factory.getChannels();
223 BOOST_CHECK(std::none_of(channels.begin(), channels.end(),
224 [] (const shared_ptr<const Channel>& ch) { return ch->isListening(); }));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000225}
226
Davide Pesavento494a9552018-02-04 22:16:05 -0500227BOOST_AUTO_TEST_CASE(DisableV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000228{
229 const std::string CONFIG = R"CONFIG(
230 face_system
231 {
232 udp
233 {
234 port 7001
235 enable_v4 no
236 enable_v6 yes
237 mcast no
238 }
239 }
240 )CONFIG";
241
242 parseConfig(CONFIG, true);
243 parseConfig(CONFIG, false);
244
Junxiao Shi64d99f22017-01-21 23:06:36 +0000245 checkChannelListEqual(factory, {"udp6://[::]:7001"});
246}
247
Davide Pesavento494a9552018-02-04 22:16:05 -0500248BOOST_AUTO_TEST_CASE(DisableV6)
249{
250 const std::string CONFIG = R"CONFIG(
251 face_system
252 {
253 udp
254 {
255 port 7001
256 enable_v4 yes
257 enable_v6 no
258 mcast no
259 }
260 }
261 )CONFIG";
262
263 parseConfig(CONFIG, true);
264 parseConfig(CONFIG, false);
265
266 checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"});
267}
268
Davide Pesaventobb734df2017-10-24 18:05:36 -0400269BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000270{
Junxiao Shi64d99f22017-01-21 23:06:36 +0000271 const std::string CONFIG_WITH_MCAST = R"CONFIG(
272 face_system
273 {
274 udp
275 {
276 mcast yes
277 }
278 }
279 )CONFIG";
280 const std::string CONFIG_WITHOUT_MCAST = R"CONFIG(
281 face_system
282 {
283 udp
284 {
285 mcast no
286 }
287 }
288 )CONFIG";
289
290 parseConfig(CONFIG_WITHOUT_MCAST, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400291 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500292 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000293
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500294#ifdef __linux__
295 // need superuser privileges to create multicast faces on Linux
296 SKIP_IF_NOT_SUPERUSER();
297#endif // __linux__
Junxiao Shi64d99f22017-01-21 23:06:36 +0000298
299 parseConfig(CONFIG_WITH_MCAST, false);
300 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500301 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), netifsV4.size());
302 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), netifsV6.size());
Junxiao Shi64d99f22017-01-21 23:06:36 +0000303
304 parseConfig(CONFIG_WITHOUT_MCAST, false);
305 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400306 BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500307 BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000308}
309
Davide Pesaventobb734df2017-10-24 18:05:36 -0400310BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpFactoryMcastFixture)
Teng Liangfe4fce32017-03-29 04:49:38 +0000311{
312#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500313 // need superuser privileges to create multicast faces on Linux
Teng Liangfe4fce32017-03-29 04:49:38 +0000314 SKIP_IF_NOT_SUPERUSER();
315#endif // __linux__
316 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
317
318 const std::string CONFIG = R"CONFIG(
319 face_system
320 {
321 udp
322 {
323 mcast_ad_hoc yes
324 }
325 }
326 )CONFIG";
327
328 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500329 BOOST_CHECK_EQUAL(this->listUdp4McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV4.size());
330 BOOST_CHECK_EQUAL(this->listUdp6McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV6.size());
Teng Liangfe4fce32017-03-29 04:49:38 +0000331}
332
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500333BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV4, UdpFactoryMcastFixture)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000334{
335#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500336 // need superuser privileges to create multicast faces on Linux
Junxiao Shi64d99f22017-01-21 23:06:36 +0000337 SKIP_IF_NOT_SUPERUSER();
338#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500339 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000340
341 const std::string CONFIG1 = R"CONFIG(
342 face_system
343 {
344 udp
345 {
346 mcast_group 239.66.30.1
347 mcast_port 7011
348 }
349 }
350 )CONFIG";
351 const std::string CONFIG2 = R"CONFIG(
352 face_system
353 {
354 udp
355 {
356 mcast_group 239.66.30.2
357 mcast_port 7012
358 }
359 }
360 )CONFIG";
361
362 parseConfig(CONFIG1, false);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400363 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500364 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
365 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.1:7011"));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000366
367 parseConfig(CONFIG2, false);
368 g_io.poll();
Davide Pesaventobb734df2017-10-24 18:05:36 -0400369 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500370 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size());
371 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.2:7012"));
372}
373
374BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV6, UdpFactoryMcastFixture)
375{
376#ifdef __linux__
377 // need superuser privileges to create multicast faces on Linux
378 SKIP_IF_NOT_SUPERUSER();
379#endif // __linux__
380 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
381
382 const std::string CONFIG1 = R"CONFIG(
383 face_system
384 {
385 udp
386 {
387 mcast_group_v6 ff02::1101
388 mcast_port_v6 7011
389 }
390 }
391 )CONFIG";
392 const std::string CONFIG2 = R"CONFIG(
393 face_system
394 {
395 udp
396 {
397 mcast_group_v6 ff02::1102
398 mcast_port_v6 7012
399 }
400 }
401 )CONFIG";
402
403 parseConfig(CONFIG1, false);
404 auto udpMcastFaces = this->listUdp6McastFaces();
405 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
406 auto expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1101");
407 expectedAddr.scope_id(netifsV6.front()->getIndex());
408 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7011)));
409
410 parseConfig(CONFIG2, false);
411 g_io.poll();
412 udpMcastFaces = this->listUdp6McastFaces();
413 BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size());
414 expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1102");
415 expectedAddr.scope_id(netifsV6.front()->getIndex());
416 BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7012)));
Junxiao Shi64d99f22017-01-21 23:06:36 +0000417}
418
Davide Pesaventobb734df2017-10-24 18:05:36 -0400419BOOST_FIXTURE_TEST_CASE(Whitelist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000420{
421#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500422 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000423 SKIP_IF_NOT_SUPERUSER();
424#endif // __linux__
425 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
426
427 std::string CONFIG = R"CONFIG(
428 face_system
429 {
430 udp
431 {
432 whitelist
433 {
434 ifname %ifname
435 }
436 }
437 }
438 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000439 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000440
441 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500442
Davide Pesaventobb734df2017-10-24 18:05:36 -0400443 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500444 BOOST_CHECK_LE(udpMcastFaces.size(), 1);
445 auto udpMcastFacesV6 = this->listUdp6McastFaces();
446 BOOST_CHECK_LE(udpMcastFacesV6.size(), 1);
447 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
448 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
449 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
450 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000451}
452
Davide Pesaventobb734df2017-10-24 18:05:36 -0400453BOOST_FIXTURE_TEST_CASE(Blacklist, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000454{
455#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500456 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000457 SKIP_IF_NOT_SUPERUSER();
458#endif // __linux__
459 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1);
460
461 std::string CONFIG = R"CONFIG(
462 face_system
463 {
464 udp
465 {
466 blacklist
467 {
468 ifname %ifname
469 }
470 }
471 }
472 )CONFIG";
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000473 boost::replace_first(CONFIG, "%ifname", netifs.front()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000474
475 parseConfig(CONFIG, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500476
Davide Pesaventobb734df2017-10-24 18:05:36 -0400477 auto udpMcastFaces = this->listUdp4McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500478 if (!netifsV4.empty())
479 BOOST_CHECK_GE(udpMcastFaces.size(), netifsV4.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500480 auto udpMcastFacesV6 = this->listUdp6McastFaces();
Davide Pesavento97a01012018-01-22 19:36:28 -0500481 if (!netifsV6.empty())
482 BOOST_CHECK_GE(udpMcastFacesV6.size(), netifsV6.size() - 1);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500483 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
484 BOOST_CHECK_LT(udpMcastFaces.size(), netifsV4.size() + netifsV6.size());
485 BOOST_CHECK(std::none_of(udpMcastFaces.begin(), udpMcastFaces.end(),
486 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000487}
488
Davide Pesaventobb734df2017-10-24 18:05:36 -0400489BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpFactoryMcastFixture)
Junxiao Shic31080d2017-01-24 15:10:12 +0000490{
491#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500492 // need superuser privileges to create multicast faces on Linux
Junxiao Shic31080d2017-01-24 15:10:12 +0000493 SKIP_IF_NOT_SUPERUSER();
494#endif // __linux__
495 SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2);
496
497 std::string CONFIG1 = R"CONFIG(
498 face_system
499 {
500 udp
501 {
502 whitelist
503 {
504 ifname %ifname
505 }
506 }
507 }
508 )CONFIG";
509 std::string CONFIG2 = CONFIG1;
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000510 boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName());
511 boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName());
Junxiao Shic31080d2017-01-24 15:10:12 +0000512
513 parseConfig(CONFIG1, false);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500514
Davide Pesaventobb734df2017-10-24 18:05:36 -0400515 auto udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500516 auto udpMcastFacesV6 = this->listUdp6McastFaces();
517 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
518 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
519 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
520 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000521
522 parseConfig(CONFIG2, false);
523 g_io.poll();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500524
Davide Pesaventobb734df2017-10-24 18:05:36 -0400525 udpMcastFaces = this->listUdp4McastFaces();
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500526 udpMcastFacesV6 = this->listUdp6McastFaces();
527 udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end());
528 BOOST_CHECK_GE(udpMcastFaces.size(), 1);
529 BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(),
530 [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.back()); }));
Junxiao Shic31080d2017-01-24 15:10:12 +0000531}
532
Junxiao Shi64d99f22017-01-21 23:06:36 +0000533BOOST_AUTO_TEST_CASE(Omitted)
534{
535 const std::string CONFIG = R"CONFIG(
536 face_system
537 {
538 }
539 )CONFIG";
540
541 parseConfig(CONFIG, true);
542 parseConfig(CONFIG, false);
543
Junxiao Shi64d99f22017-01-21 23:06:36 +0000544 BOOST_CHECK_EQUAL(factory.getChannels().size(), 0);
545 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500546 BOOST_CHECK_EQUAL(this->listFacesByScheme("udp6", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000547}
548
Davide Pesaventobb734df2017-10-24 18:05:36 -0400549BOOST_AUTO_TEST_CASE(AllDisabled)
550{
551 const std::string CONFIG = R"CONFIG(
552 face_system
553 {
554 udp
555 {
556 enable_v4 no
557 enable_v6 no
558 mcast no
559 }
560 }
561 )CONFIG";
562
563 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
564 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
565}
566
Davide Pesavento494a9552018-02-04 22:16:05 -0500567BOOST_AUTO_TEST_CASE(BadListen)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000568{
569 const std::string CONFIG = R"CONFIG(
570 face_system
571 {
572 udp
573 {
Davide Pesavento494a9552018-02-04 22:16:05 -0500574 listen hello
575 }
576 }
577 )CONFIG";
578
579 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
580 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
581}
582
583BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(BadPort, 2) // Bug #4489
584BOOST_AUTO_TEST_CASE(BadPort)
585{
586 // not a number
587 const std::string CONFIG1 = R"CONFIG(
588 face_system
589 {
590 udp
591 {
592 port hello
593 }
594 }
595 )CONFIG";
596
597 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
598 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
599
600 // negative number
601 const std::string CONFIG2 = R"CONFIG(
602 face_system
603 {
604 udp
605 {
606 port -1
607 }
608 }
609 )CONFIG";
610
611 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
612 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
613
614 // out of range
615 const std::string CONFIG3 = R"CONFIG(
616 face_system
617 {
618 udp
619 {
620 port 65536
621 }
622 }
623 )CONFIG";
624
625 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
626 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
627}
628
629BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(BadIdleTimeout, 2) // Bug #4489
630BOOST_AUTO_TEST_CASE(BadIdleTimeout)
631{
632 // not a number
633 const std::string CONFIG1 = R"CONFIG(
634 face_system
635 {
636 udp
637 {
Junxiao Shi64d99f22017-01-21 23:06:36 +0000638 idle_timeout hello
639 }
640 }
641 )CONFIG";
642
Davide Pesavento494a9552018-02-04 22:16:05 -0500643 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
644 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
645
646 // negative number
647 const std::string CONFIG2 = R"CONFIG(
648 face_system
649 {
650 udp
651 {
652 idle_timeout -15
653 }
654 }
655 )CONFIG";
656
657 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
658 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000659}
660
661BOOST_AUTO_TEST_CASE(BadMcast)
662{
663 const std::string CONFIG = R"CONFIG(
664 face_system
665 {
666 udp
667 {
668 mcast hello
669 }
670 }
671 )CONFIG";
672
673 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
674 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
675}
676
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500677BOOST_AUTO_TEST_CASE(BadMcastGroupV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000678{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400679 // not an address
680 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000681 face_system
682 {
683 udp
684 {
685 mcast_group hello
686 }
687 }
688 )CONFIG";
689
Davide Pesaventobb734df2017-10-24 18:05:36 -0400690 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
691 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000692
Davide Pesaventobb734df2017-10-24 18:05:36 -0400693 // non-multicast address
694 const std::string CONFIG2 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000695 face_system
696 {
697 udp
698 {
699 mcast_group 10.0.0.1
700 }
701 }
702 )CONFIG";
703
Davide Pesaventobb734df2017-10-24 18:05:36 -0400704 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
705 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000706
Davide Pesaventobb734df2017-10-24 18:05:36 -0400707 // wrong address family
708 const std::string CONFIG3 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000709 face_system
710 {
711 udp
712 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400713 mcast_group ff02::1234
Junxiao Shi64d99f22017-01-21 23:06:36 +0000714 }
715 }
716 )CONFIG";
717
Davide Pesaventobb734df2017-10-24 18:05:36 -0400718 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
719 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000720}
721
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500722BOOST_AUTO_TEST_CASE(BadMcastGroupV6)
723{
724 // not an address
725 const std::string CONFIG1 = R"CONFIG(
726 face_system
727 {
728 udp
729 {
730 mcast_group_v6 foo
731 }
732 }
733 )CONFIG";
734
735 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
736 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
737
738 // non-multicast address
739 const std::string CONFIG2 = R"CONFIG(
740 face_system
741 {
742 udp
743 {
744 mcast_group_v6 fe80::1234
745 }
746 }
747 )CONFIG";
748
749 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
750 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
751
752 // wrong address family
753 const std::string CONFIG3 = R"CONFIG(
754 face_system
755 {
756 udp
757 {
758 mcast_group_v6 224.0.23.170
759 }
760 }
761 )CONFIG";
762
763 BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error);
764 BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error);
765}
766
767BOOST_AUTO_TEST_CASE(BadMcastPortV4)
Junxiao Shi64d99f22017-01-21 23:06:36 +0000768{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400769 const std::string CONFIG1 = R"CONFIG(
Junxiao Shi64d99f22017-01-21 23:06:36 +0000770 face_system
771 {
772 udp
773 {
Davide Pesaventobb734df2017-10-24 18:05:36 -0400774 mcast_port hey
Junxiao Shi64d99f22017-01-21 23:06:36 +0000775 }
776 }
777 )CONFIG";
778
Davide Pesaventobb734df2017-10-24 18:05:36 -0400779 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
780 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
781
782 const std::string CONFIG2 = R"CONFIG(
783 face_system
784 {
785 udp
786 {
787 mcast_port 99999
788 }
789 }
790 )CONFIG";
791
792 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
793 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
Junxiao Shi64d99f22017-01-21 23:06:36 +0000794}
795
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500796BOOST_AUTO_TEST_CASE(BadMcastPortV6)
797{
798 const std::string CONFIG1 = R"CONFIG(
799 face_system
800 {
801 udp
802 {
803 mcast_port_v6 bar
804 }
805 }
806 )CONFIG";
807
808 BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error);
809 BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error);
810
811 const std::string CONFIG2 = R"CONFIG(
812 face_system
813 {
814 udp
815 {
816 mcast_port_v6 99999
817 }
818 }
819 )CONFIG";
820
821 BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error);
822 BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error);
823}
824
Junxiao Shi64d99f22017-01-21 23:06:36 +0000825BOOST_AUTO_TEST_CASE(UnknownOption)
826{
827 const std::string CONFIG = R"CONFIG(
828 face_system
829 {
830 udp
831 {
832 hello
833 }
834 }
835 )CONFIG";
836
837 BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error);
838 BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error);
839}
840
841BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
842
Junxiao Shicde37ad2015-12-24 01:02:05 -0700843BOOST_AUTO_TEST_CASE(GetChannels)
844{
Davide Pesaventob15276f2017-07-15 16:27:13 -0400845 BOOST_CHECK_EQUAL(factory.getChannels().empty(), true);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700846
Davide Pesaventob15276f2017-07-15 16:27:13 -0400847 std::set<std::string> expected;
Davide Pesaventobb734df2017-10-24 18:05:36 -0400848 expected.insert(createChannel("127.0.0.1", 20070)->getUri().toString());
849 expected.insert(createChannel("127.0.0.1", 20071)->getUri().toString());
850 expected.insert(createChannel("::1", 20071)->getUri().toString());
Davide Pesaventob15276f2017-07-15 16:27:13 -0400851 checkChannelListEqual(factory, expected);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700852}
853
Davide Pesaventobb734df2017-10-24 18:05:36 -0400854BOOST_FIXTURE_TEST_CASE(CreateChannel, UdpFactoryMcastFixture)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700855{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400856 auto channel1 = createChannel("127.0.0.1", 20070);
857 auto channel1a = createChannel("127.0.0.1", 20070);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700858 BOOST_CHECK_EQUAL(channel1, channel1a);
859 BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
860
Davide Pesaventobb734df2017-10-24 18:05:36 -0400861 auto channel2 = createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700862 BOOST_CHECK_NE(channel1, channel2);
863
Davide Pesaventobb734df2017-10-24 18:05:36 -0400864 auto channel3 = createChannel("::1", 20071);
Weiwei Liu72cee942016-02-04 16:49:19 -0700865 BOOST_CHECK_NE(channel2, channel3);
866 BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700867
Davide Pesaventobb734df2017-10-24 18:05:36 -0400868#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500869 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400870 SKIP_IF_NOT_SUPERUSER();
871#endif // __linux__
Davide Pesaventobb734df2017-10-24 18:05:36 -0400872
Weiwei Liu72cee942016-02-04 16:49:19 -0700873 // createChannel with a local endpoint that has already been allocated for a UDP multicast face
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500874 if (!netifsV4.empty()) {
875 auto mcastFace = createMulticastFace("127.0.0.1", "224.0.0.254", 20072);
876 BOOST_CHECK_EXCEPTION(createChannel("127.0.0.1", 20072), UdpFactory::Error,
877 [] (const UdpFactory::Error& e) {
878 return strcmp(e.what(),
879 "Cannot create UDP channel on 127.0.0.1:20072, "
880 "endpoint already allocated for a UDP multicast face") == 0;
881 });
882 }
883 if (!netifsV6.empty()) {
884 auto mcastFace = createMulticastFace("::1", "ff02::114", 20072);
885 BOOST_CHECK_EXCEPTION(createChannel("::1", 20072), UdpFactory::Error,
886 [] (const UdpFactory::Error& e) {
887 return strcmp(e.what(),
888 "Cannot create UDP channel on [::1]:20072, "
889 "endpoint already allocated for a UDP multicast face") == 0;
890 });
891 }
Weiwei Liu72cee942016-02-04 16:49:19 -0700892}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700893
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500894BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV4, UdpFactoryMcastFixture)
Weiwei Liu72cee942016-02-04 16:49:19 -0700895{
Davide Pesaventobb734df2017-10-24 18:05:36 -0400896#ifdef __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500897 // need superuser privileges to create multicast faces on Linux
Davide Pesaventobb734df2017-10-24 18:05:36 -0400898 SKIP_IF_NOT_SUPERUSER();
899#endif // __linux__
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500900 SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1);
Davide Pesaventobb734df2017-10-24 18:05:36 -0400901
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500902 auto multicastFace1 = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
903 auto multicastFace1a = createMulticastFace("127.0.0.1", "224.0.0.254", 20070);
904 auto multicastFace2 = createMulticastFace("127.0.0.1", "224.0.0.254", 20030);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700905 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500906 BOOST_CHECK_NE(multicastFace1, multicastFace2);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700907
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500908 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V4);
909 if (!address.is_unspecified()) {
910 auto multicastFace3 = createMulticastFace(address.to_string(), "224.0.0.254", 20070);
911 BOOST_CHECK_NE(multicastFace1, multicastFace3);
912 BOOST_CHECK_NE(multicastFace2, multicastFace3);
913 }
914
915 // create with a local endpoint already used by a channel
Davide Pesaventobb734df2017-10-24 18:05:36 -0400916 auto channel = createChannel("127.0.0.1", 20071);
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500917 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.254", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700918 [] (const UdpFactory::Error& e) {
919 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500920 "Cannot create UDP multicast face on 127.0.0.1:20071, "
921 "endpoint already allocated for a UDP channel") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700922 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700923
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500924 // create with a local endpoint already used by a multicast face on a different multicast group
Davide Pesaventobb734df2017-10-24 18:05:36 -0400925 BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.42", 20070), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700926 [] (const UdpFactory::Error& e) {
927 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500928 "Cannot create UDP multicast face on 127.0.0.1:20070, "
929 "endpoint already allocated for a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700930 });
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500931}
Junxiao Shicde37ad2015-12-24 01:02:05 -0700932
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500933BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV6, UdpFactoryMcastFixture)
934{
935#ifdef __linux__
936 // need superuser privileges to create multicast faces on Linux
937 SKIP_IF_NOT_SUPERUSER();
938#endif // __linux__
939 SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1);
940
941 auto multicastFace1 = createMulticastFace("::1", "ff02::114", 20070);
942 auto multicastFace1a = createMulticastFace("::1", "ff02::114", 20070);
943 auto multicastFace2 = createMulticastFace("::1", "ff02::114", 20030);
944 BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a);
945 BOOST_CHECK_NE(multicastFace1, multicastFace2);
946
947 auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V6);
948 if (!address.is_unspecified()) {
949 auto multicastFace3 = createMulticastFace(address.to_string(), "ff02::114", 20070);
950 BOOST_CHECK_NE(multicastFace1, multicastFace3);
951 BOOST_CHECK_NE(multicastFace2, multicastFace3);
952 }
953
954 // create with a local endpoint already used by a channel
955 auto channel = createChannel("::1", 20071);
956 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::114", 20071), UdpFactory::Error,
Weiwei Liu72cee942016-02-04 16:49:19 -0700957 [] (const UdpFactory::Error& e) {
958 return strcmp(e.what(),
Md Ashiqur Rahman8ce09032018-01-14 22:43:13 -0500959 "Cannot create UDP multicast face on [::1]:20071, "
960 "endpoint already allocated for a UDP channel") == 0;
961 });
962
963 // create with a local endpoint already used by a multicast face on a different multicast group
964 BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::42", 20070), UdpFactory::Error,
965 [] (const UdpFactory::Error& e) {
966 return strcmp(e.what(),
967 "Cannot create UDP multicast face on [::1]:20070, "
968 "endpoint already allocated for a different UDP multicast face") == 0;
Weiwei Liu72cee942016-02-04 16:49:19 -0700969 });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700970}
971
Davide Pesaventob15276f2017-07-15 16:27:13 -0400972BOOST_AUTO_TEST_CASE(CreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700973{
Eric Newberry42602412016-08-27 09:33:18 -0700974 createFace(factory,
975 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000976 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700977 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700978 {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700979
Davide Pesaventobb734df2017-10-24 18:05:36 -0400980 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700981
Eric Newberry42602412016-08-27 09:33:18 -0700982 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400983 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000984 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700985 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700986 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Eric Newberry78e32b02017-04-01 14:34:44 +0000987
Eric Newberry42602412016-08-27 09:33:18 -0700988 createFace(factory,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400989 FaceUri("udp4://127.0.0.1:6363"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000990 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700991 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -0700992 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -0700993
Eric Newberry42602412016-08-27 09:33:18 -0700994 createFace(factory,
995 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +0000996 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -0700997 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false},
Eric Newberry2642cd22017-07-13 21:34:53 -0400998 {CreateFaceExpectedResult::SUCCESS, 0, ""});
999
Eric Newberry2642cd22017-07-13 21:34:53 -04001000 createFace(factory,
1001 FaceUri("udp4://127.0.0.1:20073"),
1002 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001003 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, true, false},
1004 {CreateFaceExpectedResult::SUCCESS, 0, ""});
1005
1006 createFace(factory,
1007 FaceUri("udp4://127.0.0.1:20073"),
1008 {},
1009 {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, true},
Eric Newberry42602412016-08-27 09:33:18 -07001010 {CreateFaceExpectedResult::SUCCESS, 0, ""});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001011}
1012
Davide Pesaventob15276f2017-07-15 16:27:13 -04001013BOOST_AUTO_TEST_CASE(UnsupportedCreateFace)
Junxiao Shicde37ad2015-12-24 01:02:05 -07001014{
Davide Pesaventobb734df2017-10-24 18:05:36 -04001015 createChannel("127.0.0.1", 20071);
Junxiao Shicde37ad2015-12-24 01:02:05 -07001016
Eric Newberry42602412016-08-27 09:33:18 -07001017 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001018 FaceUri("udp4://127.0.0.1:20072"),
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -04001019 FaceUri("udp4://127.0.0.1:20071"),
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001020 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001021 {CreateFaceExpectedResult::FAILURE, 406,
1022 "Unicast UDP faces cannot be created with a LocalUri"});
1023
1024 createFace(factory,
1025 FaceUri("udp4://127.0.0.1:20072"),
Eric Newberry78e32b02017-04-01 14:34:44 +00001026 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001027 {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, false, false, false},
Eric Newberry42602412016-08-27 09:33:18 -07001028 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001029 "Outgoing UDP faces do not support on-demand persistency"});
Eric Newberry78e32b02017-04-01 14:34:44 +00001030
1031 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001032 FaceUri("udp4://233.252.0.1:23252"),
1033 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001034 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
Eric Newberry78e32b02017-04-01 14:34:44 +00001035 {CreateFaceExpectedResult::FAILURE, 406,
Davide Pesavento46afec42017-05-28 14:28:47 -04001036 "Cannot create multicast UDP faces"});
1037
1038 createFace(factory,
Davide Pesavento46afec42017-05-28 14:28:47 -04001039 FaceUri("udp4://127.0.0.1:20072"),
1040 {},
Eric Newberry0c3e57b2018-01-25 20:54:46 -07001041 {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, true, false, false},
Davide Pesavento46afec42017-05-28 14:28:47 -04001042 {CreateFaceExpectedResult::FAILURE, 406,
1043 "Local fields can only be enabled on faces with local scope"});
Junxiao Shicde37ad2015-12-24 01:02:05 -07001044}
1045
Junxiao Shicde37ad2015-12-24 01:02:05 -07001046BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory
1047BOOST_AUTO_TEST_SUITE_END() // Face
1048
1049} // namespace tests
Junxiao Shi64d99f22017-01-21 23:06:36 +00001050} // namespace face
Junxiao Shicde37ad2015-12-24 01:02:05 -07001051} // namespace nfd