Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 2 | /* |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 4 | * 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 Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 28 | #include "face-system-fixture.hpp" |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 29 | #include "factory-test-common.hpp" |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 30 | |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 31 | #include <boost/algorithm/string/replace.hpp> |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 32 | #include <ndn-cxx/net/address-converter.hpp> |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 33 | |
| 34 | namespace nfd { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 35 | namespace face { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 36 | namespace tests { |
| 37 | |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 38 | class UdpFactoryFixture : public FaceSystemFactoryFixture<UdpFactory> |
| 39 | { |
| 40 | protected: |
| 41 | shared_ptr<UdpChannel> |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 42 | createChannel(const std::string& localIp, uint16_t localPort) |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 43 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 44 | udp::Endpoint endpoint(ndn::ip::addressFromString(localIp), localPort); |
Davide Pesavento | 4b89a6e | 2017-10-07 15:29:50 -0400 | [diff] [blame] | 45 | return factory.createChannel(endpoint, time::minutes(5)); |
| 46 | } |
| 47 | }; |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 48 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 49 | class UdpFactoryMcastFixture : public UdpFactoryFixture |
| 50 | { |
| 51 | protected: |
| 52 | UdpFactoryMcastFixture() |
| 53 | { |
| 54 | for (const auto& netif : collectNetworkInterfaces()) { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 55 | // 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 Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 69 | } |
| 70 | } |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 71 | this->copyRealNetifsToNetmon(); |
| 72 | } |
| 73 | |
| 74 | shared_ptr<Face> |
| 75 | createMulticastFace(const std::string& localIp, const std::string& mcastIp, uint16_t mcastPort) |
| 76 | { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 77 | auto localAddress = ndn::ip::addressFromString(localIp); |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 78 | udp::Endpoint mcastEndpoint(ndn::ip::addressFromString(mcastIp), mcastPort); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 79 | |
| 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 Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 103 | } |
| 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 Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 111 | 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 Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 118 | */ |
| 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 Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 129 | isFaceOnNetif(const Face& face, const NetworkInterface& netif) |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 130 | { |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 131 | auto ip = ndn::ip::addressFromString(face.getLocalUri().getHost()); |
| 132 | return std::any_of(netif.getNetworkAddresses().begin(), netif.getNetworkAddresses().end(), |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 133 | [ip] (const NetworkAddress& a) { return a.getIp() == ip; }); |
| 134 | } |
| 135 | |
| 136 | protected: |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 137 | /** \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 Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 140 | */ |
| 141 | std::vector<shared_ptr<const NetworkInterface>> netifs; |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 142 | |
| 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 Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 150 | }; |
| 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 Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 161 | #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 Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 179 | BOOST_AUTO_TEST_SUITE(Face) |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 180 | BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, UdpFactoryFixture) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 181 | |
| 182 | using nfd::Face; |
| 183 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 184 | BOOST_AUTO_TEST_SUITE(ProcessConfig) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 185 | |
| 186 | BOOST_AUTO_TEST_CASE(Channels) |
| 187 | { |
| 188 | const std::string CONFIG = R"CONFIG( |
| 189 | face_system |
| 190 | { |
| 191 | udp |
| 192 | { |
| 193 | port 7001 |
| 194 | enable_v4 yes |
| 195 | enable_v6 yes |
| 196 | idle_timeout 30 |
| 197 | mcast no |
| 198 | } |
| 199 | } |
| 200 | )CONFIG"; |
| 201 | |
| 202 | parseConfig(CONFIG, true); |
| 203 | parseConfig(CONFIG, false); |
| 204 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 205 | checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"}); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_CASE(ChannelV4) |
| 209 | { |
| 210 | const std::string CONFIG = R"CONFIG( |
| 211 | face_system |
| 212 | { |
| 213 | udp |
| 214 | { |
| 215 | port 7001 |
| 216 | enable_v4 yes |
| 217 | enable_v6 no |
| 218 | mcast no |
| 219 | } |
| 220 | } |
| 221 | )CONFIG"; |
| 222 | |
| 223 | parseConfig(CONFIG, true); |
| 224 | parseConfig(CONFIG, false); |
| 225 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 226 | checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"}); |
| 227 | } |
| 228 | |
| 229 | BOOST_AUTO_TEST_CASE(ChannelV6) |
| 230 | { |
| 231 | const std::string CONFIG = R"CONFIG( |
| 232 | face_system |
| 233 | { |
| 234 | udp |
| 235 | { |
| 236 | port 7001 |
| 237 | enable_v4 no |
| 238 | enable_v6 yes |
| 239 | mcast no |
| 240 | } |
| 241 | } |
| 242 | )CONFIG"; |
| 243 | |
| 244 | parseConfig(CONFIG, true); |
| 245 | parseConfig(CONFIG, false); |
| 246 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 247 | checkChannelListEqual(factory, {"udp6://[::]:7001"}); |
| 248 | } |
| 249 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 250 | BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpFactoryMcastFixture) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 251 | { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 252 | const std::string CONFIG_WITH_MCAST = R"CONFIG( |
| 253 | face_system |
| 254 | { |
| 255 | udp |
| 256 | { |
| 257 | mcast yes |
| 258 | } |
| 259 | } |
| 260 | )CONFIG"; |
| 261 | const std::string CONFIG_WITHOUT_MCAST = R"CONFIG( |
| 262 | face_system |
| 263 | { |
| 264 | udp |
| 265 | { |
| 266 | mcast no |
| 267 | } |
| 268 | } |
| 269 | )CONFIG"; |
| 270 | |
| 271 | parseConfig(CONFIG_WITHOUT_MCAST, false); |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 272 | BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 273 | BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 274 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 275 | #ifdef __linux__ |
| 276 | // need superuser privileges to create multicast faces on Linux |
| 277 | SKIP_IF_NOT_SUPERUSER(); |
| 278 | #endif // __linux__ |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 279 | |
| 280 | parseConfig(CONFIG_WITH_MCAST, false); |
| 281 | g_io.poll(); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), netifsV4.size()); |
| 283 | BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), netifsV6.size()); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 284 | |
| 285 | parseConfig(CONFIG_WITHOUT_MCAST, false); |
| 286 | g_io.poll(); |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 287 | BOOST_CHECK_EQUAL(this->listUdp4McastFaces().size(), 0); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 288 | BOOST_CHECK_EQUAL(this->listUdp6McastFaces().size(), 0); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 291 | BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpFactoryMcastFixture) |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 292 | { |
| 293 | #ifdef __linux__ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 294 | // need superuser privileges to create multicast faces on Linux |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 295 | SKIP_IF_NOT_SUPERUSER(); |
| 296 | #endif // __linux__ |
| 297 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1); |
| 298 | |
| 299 | const std::string CONFIG = R"CONFIG( |
| 300 | face_system |
| 301 | { |
| 302 | udp |
| 303 | { |
| 304 | mcast_ad_hoc yes |
| 305 | } |
| 306 | } |
| 307 | )CONFIG"; |
| 308 | |
| 309 | parseConfig(CONFIG, false); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 310 | BOOST_CHECK_EQUAL(this->listUdp4McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV4.size()); |
| 311 | BOOST_CHECK_EQUAL(this->listUdp6McastFaces(ndn::nfd::LINK_TYPE_AD_HOC).size(), netifsV6.size()); |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 314 | BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV4, UdpFactoryMcastFixture) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 315 | { |
| 316 | #ifdef __linux__ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 317 | // need superuser privileges to create multicast faces on Linux |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 318 | SKIP_IF_NOT_SUPERUSER(); |
| 319 | #endif // __linux__ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 320 | SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 321 | |
| 322 | const std::string CONFIG1 = R"CONFIG( |
| 323 | face_system |
| 324 | { |
| 325 | udp |
| 326 | { |
| 327 | mcast_group 239.66.30.1 |
| 328 | mcast_port 7011 |
| 329 | } |
| 330 | } |
| 331 | )CONFIG"; |
| 332 | const std::string CONFIG2 = R"CONFIG( |
| 333 | face_system |
| 334 | { |
| 335 | udp |
| 336 | { |
| 337 | mcast_group 239.66.30.2 |
| 338 | mcast_port 7012 |
| 339 | } |
| 340 | } |
| 341 | )CONFIG"; |
| 342 | |
| 343 | parseConfig(CONFIG1, false); |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 344 | auto udpMcastFaces = this->listUdp4McastFaces(); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 345 | BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size()); |
| 346 | BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.1:7011")); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 347 | |
| 348 | parseConfig(CONFIG2, false); |
| 349 | g_io.poll(); |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 350 | udpMcastFaces = this->listUdp4McastFaces(); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 351 | BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV4.size()); |
| 352 | BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri("udp4://239.66.30.2:7012")); |
| 353 | } |
| 354 | |
| 355 | BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpointV6, UdpFactoryMcastFixture) |
| 356 | { |
| 357 | #ifdef __linux__ |
| 358 | // need superuser privileges to create multicast faces on Linux |
| 359 | SKIP_IF_NOT_SUPERUSER(); |
| 360 | #endif // __linux__ |
| 361 | SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1); |
| 362 | |
| 363 | const std::string CONFIG1 = R"CONFIG( |
| 364 | face_system |
| 365 | { |
| 366 | udp |
| 367 | { |
| 368 | mcast_group_v6 ff02::1101 |
| 369 | mcast_port_v6 7011 |
| 370 | } |
| 371 | } |
| 372 | )CONFIG"; |
| 373 | const std::string CONFIG2 = R"CONFIG( |
| 374 | face_system |
| 375 | { |
| 376 | udp |
| 377 | { |
| 378 | mcast_group_v6 ff02::1102 |
| 379 | mcast_port_v6 7012 |
| 380 | } |
| 381 | } |
| 382 | )CONFIG"; |
| 383 | |
| 384 | parseConfig(CONFIG1, false); |
| 385 | auto udpMcastFaces = this->listUdp6McastFaces(); |
| 386 | BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size()); |
| 387 | auto expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1101"); |
| 388 | expectedAddr.scope_id(netifsV6.front()->getIndex()); |
| 389 | BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7011))); |
| 390 | |
| 391 | parseConfig(CONFIG2, false); |
| 392 | g_io.poll(); |
| 393 | udpMcastFaces = this->listUdp6McastFaces(); |
| 394 | BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifsV6.size()); |
| 395 | expectedAddr = boost::asio::ip::address_v6::from_string("ff02::1102"); |
| 396 | expectedAddr.scope_id(netifsV6.front()->getIndex()); |
| 397 | BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), FaceUri(udp::Endpoint(expectedAddr, 7012))); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 400 | BOOST_FIXTURE_TEST_CASE(Whitelist, UdpFactoryMcastFixture) |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 401 | { |
| 402 | #ifdef __linux__ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 403 | // need superuser privileges to create multicast faces on Linux |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 404 | SKIP_IF_NOT_SUPERUSER(); |
| 405 | #endif // __linux__ |
| 406 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1); |
| 407 | |
| 408 | std::string CONFIG = R"CONFIG( |
| 409 | face_system |
| 410 | { |
| 411 | udp |
| 412 | { |
| 413 | whitelist |
| 414 | { |
| 415 | ifname %ifname |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | )CONFIG"; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 420 | boost::replace_first(CONFIG, "%ifname", netifs.front()->getName()); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 421 | |
| 422 | parseConfig(CONFIG, false); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 423 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 424 | auto udpMcastFaces = this->listUdp4McastFaces(); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 425 | BOOST_CHECK_LE(udpMcastFaces.size(), 1); |
| 426 | auto udpMcastFacesV6 = this->listUdp6McastFaces(); |
| 427 | BOOST_CHECK_LE(udpMcastFacesV6.size(), 1); |
| 428 | udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end()); |
| 429 | BOOST_CHECK_GE(udpMcastFaces.size(), 1); |
| 430 | BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(), |
| 431 | [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); })); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 434 | BOOST_FIXTURE_TEST_CASE(Blacklist, UdpFactoryMcastFixture) |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 435 | { |
| 436 | #ifdef __linux__ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 437 | // need superuser privileges to create multicast faces on Linux |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 438 | SKIP_IF_NOT_SUPERUSER(); |
| 439 | #endif // __linux__ |
| 440 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1); |
| 441 | |
| 442 | std::string CONFIG = R"CONFIG( |
| 443 | face_system |
| 444 | { |
| 445 | udp |
| 446 | { |
| 447 | blacklist |
| 448 | { |
| 449 | ifname %ifname |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | )CONFIG"; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 454 | boost::replace_first(CONFIG, "%ifname", netifs.front()->getName()); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 455 | |
| 456 | parseConfig(CONFIG, false); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 457 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 458 | auto udpMcastFaces = this->listUdp4McastFaces(); |
Davide Pesavento | 97a0101 | 2018-01-22 19:36:28 -0500 | [diff] [blame] | 459 | if (!netifsV4.empty()) |
| 460 | BOOST_CHECK_GE(udpMcastFaces.size(), netifsV4.size() - 1); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 461 | auto udpMcastFacesV6 = this->listUdp6McastFaces(); |
Davide Pesavento | 97a0101 | 2018-01-22 19:36:28 -0500 | [diff] [blame] | 462 | if (!netifsV6.empty()) |
| 463 | BOOST_CHECK_GE(udpMcastFacesV6.size(), netifsV6.size() - 1); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 464 | udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end()); |
| 465 | BOOST_CHECK_LT(udpMcastFaces.size(), netifsV4.size() + netifsV6.size()); |
| 466 | BOOST_CHECK(std::none_of(udpMcastFaces.begin(), udpMcastFaces.end(), |
| 467 | [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); })); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 470 | BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpFactoryMcastFixture) |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 471 | { |
| 472 | #ifdef __linux__ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 473 | // need superuser privileges to create multicast faces on Linux |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 474 | SKIP_IF_NOT_SUPERUSER(); |
| 475 | #endif // __linux__ |
| 476 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2); |
| 477 | |
| 478 | std::string CONFIG1 = R"CONFIG( |
| 479 | face_system |
| 480 | { |
| 481 | udp |
| 482 | { |
| 483 | whitelist |
| 484 | { |
| 485 | ifname %ifname |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | )CONFIG"; |
| 490 | std::string CONFIG2 = CONFIG1; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 491 | boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName()); |
| 492 | boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName()); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 493 | |
| 494 | parseConfig(CONFIG1, false); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 495 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 496 | auto udpMcastFaces = this->listUdp4McastFaces(); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 497 | auto udpMcastFacesV6 = this->listUdp6McastFaces(); |
| 498 | udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end()); |
| 499 | BOOST_CHECK_GE(udpMcastFaces.size(), 1); |
| 500 | BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(), |
| 501 | [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.front()); })); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 502 | |
| 503 | parseConfig(CONFIG2, false); |
| 504 | g_io.poll(); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 505 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 506 | udpMcastFaces = this->listUdp4McastFaces(); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 507 | udpMcastFacesV6 = this->listUdp6McastFaces(); |
| 508 | udpMcastFaces.insert(udpMcastFaces.end(), udpMcastFacesV6.begin(), udpMcastFacesV6.end()); |
| 509 | BOOST_CHECK_GE(udpMcastFaces.size(), 1); |
| 510 | BOOST_CHECK(std::all_of(udpMcastFaces.begin(), udpMcastFaces.end(), |
| 511 | [this] (const Face* face) { return isFaceOnNetif(*face, *netifs.back()); })); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 514 | BOOST_AUTO_TEST_CASE(Omitted) |
| 515 | { |
| 516 | const std::string CONFIG = R"CONFIG( |
| 517 | face_system |
| 518 | { |
| 519 | } |
| 520 | )CONFIG"; |
| 521 | |
| 522 | parseConfig(CONFIG, true); |
| 523 | parseConfig(CONFIG, false); |
| 524 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 525 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 526 | BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 527 | BOOST_CHECK_EQUAL(this->listFacesByScheme("udp6", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 530 | BOOST_AUTO_TEST_CASE(AllDisabled) |
| 531 | { |
| 532 | const std::string CONFIG = R"CONFIG( |
| 533 | face_system |
| 534 | { |
| 535 | udp |
| 536 | { |
| 537 | enable_v4 no |
| 538 | enable_v6 no |
| 539 | mcast no |
| 540 | } |
| 541 | } |
| 542 | )CONFIG"; |
| 543 | |
| 544 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 545 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 546 | } |
| 547 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 548 | BOOST_AUTO_TEST_CASE(BadIdleTimeout) |
| 549 | { |
| 550 | const std::string CONFIG = R"CONFIG( |
| 551 | face_system |
| 552 | { |
| 553 | udp |
| 554 | { |
| 555 | idle_timeout hello |
| 556 | } |
| 557 | } |
| 558 | )CONFIG"; |
| 559 | |
| 560 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 561 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 562 | } |
| 563 | |
| 564 | BOOST_AUTO_TEST_CASE(BadMcast) |
| 565 | { |
| 566 | const std::string CONFIG = R"CONFIG( |
| 567 | face_system |
| 568 | { |
| 569 | udp |
| 570 | { |
| 571 | mcast hello |
| 572 | } |
| 573 | } |
| 574 | )CONFIG"; |
| 575 | |
| 576 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 577 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 578 | } |
| 579 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 580 | BOOST_AUTO_TEST_CASE(BadMcastGroupV4) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 581 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 582 | // not an address |
| 583 | const std::string CONFIG1 = R"CONFIG( |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 584 | face_system |
| 585 | { |
| 586 | udp |
| 587 | { |
| 588 | mcast_group hello |
| 589 | } |
| 590 | } |
| 591 | )CONFIG"; |
| 592 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 593 | BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error); |
| 594 | BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 595 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 596 | // non-multicast address |
| 597 | const std::string CONFIG2 = R"CONFIG( |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 598 | face_system |
| 599 | { |
| 600 | udp |
| 601 | { |
| 602 | mcast_group 10.0.0.1 |
| 603 | } |
| 604 | } |
| 605 | )CONFIG"; |
| 606 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 607 | BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error); |
| 608 | BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 609 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 610 | // wrong address family |
| 611 | const std::string CONFIG3 = R"CONFIG( |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 612 | face_system |
| 613 | { |
| 614 | udp |
| 615 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 616 | mcast_group ff02::1234 |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | )CONFIG"; |
| 620 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 621 | BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error); |
| 622 | BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 625 | BOOST_AUTO_TEST_CASE(BadMcastGroupV6) |
| 626 | { |
| 627 | // not an address |
| 628 | const std::string CONFIG1 = R"CONFIG( |
| 629 | face_system |
| 630 | { |
| 631 | udp |
| 632 | { |
| 633 | mcast_group_v6 foo |
| 634 | } |
| 635 | } |
| 636 | )CONFIG"; |
| 637 | |
| 638 | BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error); |
| 639 | BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error); |
| 640 | |
| 641 | // non-multicast address |
| 642 | const std::string CONFIG2 = R"CONFIG( |
| 643 | face_system |
| 644 | { |
| 645 | udp |
| 646 | { |
| 647 | mcast_group_v6 fe80::1234 |
| 648 | } |
| 649 | } |
| 650 | )CONFIG"; |
| 651 | |
| 652 | BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error); |
| 653 | BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error); |
| 654 | |
| 655 | // wrong address family |
| 656 | const std::string CONFIG3 = R"CONFIG( |
| 657 | face_system |
| 658 | { |
| 659 | udp |
| 660 | { |
| 661 | mcast_group_v6 224.0.23.170 |
| 662 | } |
| 663 | } |
| 664 | )CONFIG"; |
| 665 | |
| 666 | BOOST_CHECK_THROW(parseConfig(CONFIG3, true), ConfigFile::Error); |
| 667 | BOOST_CHECK_THROW(parseConfig(CONFIG3, false), ConfigFile::Error); |
| 668 | } |
| 669 | |
| 670 | BOOST_AUTO_TEST_CASE(BadMcastPortV4) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 671 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 672 | const std::string CONFIG1 = R"CONFIG( |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 673 | face_system |
| 674 | { |
| 675 | udp |
| 676 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 677 | mcast_port hey |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | )CONFIG"; |
| 681 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 682 | BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error); |
| 683 | BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error); |
| 684 | |
| 685 | const std::string CONFIG2 = R"CONFIG( |
| 686 | face_system |
| 687 | { |
| 688 | udp |
| 689 | { |
| 690 | mcast_port 99999 |
| 691 | } |
| 692 | } |
| 693 | )CONFIG"; |
| 694 | |
| 695 | BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error); |
| 696 | BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 699 | BOOST_AUTO_TEST_CASE(BadMcastPortV6) |
| 700 | { |
| 701 | const std::string CONFIG1 = R"CONFIG( |
| 702 | face_system |
| 703 | { |
| 704 | udp |
| 705 | { |
| 706 | mcast_port_v6 bar |
| 707 | } |
| 708 | } |
| 709 | )CONFIG"; |
| 710 | |
| 711 | BOOST_CHECK_THROW(parseConfig(CONFIG1, true), ConfigFile::Error); |
| 712 | BOOST_CHECK_THROW(parseConfig(CONFIG1, false), ConfigFile::Error); |
| 713 | |
| 714 | const std::string CONFIG2 = R"CONFIG( |
| 715 | face_system |
| 716 | { |
| 717 | udp |
| 718 | { |
| 719 | mcast_port_v6 99999 |
| 720 | } |
| 721 | } |
| 722 | )CONFIG"; |
| 723 | |
| 724 | BOOST_CHECK_THROW(parseConfig(CONFIG2, true), ConfigFile::Error); |
| 725 | BOOST_CHECK_THROW(parseConfig(CONFIG2, false), ConfigFile::Error); |
| 726 | } |
| 727 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 728 | BOOST_AUTO_TEST_CASE(UnknownOption) |
| 729 | { |
| 730 | const std::string CONFIG = R"CONFIG( |
| 731 | face_system |
| 732 | { |
| 733 | udp |
| 734 | { |
| 735 | hello |
| 736 | } |
| 737 | } |
| 738 | )CONFIG"; |
| 739 | |
| 740 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 741 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 742 | } |
| 743 | |
| 744 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
| 745 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 746 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 747 | { |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 748 | BOOST_CHECK_EQUAL(factory.getChannels().empty(), true); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 749 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 750 | std::set<std::string> expected; |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 751 | expected.insert(createChannel("127.0.0.1", 20070)->getUri().toString()); |
| 752 | expected.insert(createChannel("127.0.0.1", 20071)->getUri().toString()); |
| 753 | expected.insert(createChannel("::1", 20071)->getUri().toString()); |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 754 | checkChannelListEqual(factory, expected); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 757 | BOOST_FIXTURE_TEST_CASE(CreateChannel, UdpFactoryMcastFixture) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 758 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 759 | auto channel1 = createChannel("127.0.0.1", 20070); |
| 760 | auto channel1a = createChannel("127.0.0.1", 20070); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 761 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 762 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070"); |
| 763 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 764 | auto channel2 = createChannel("127.0.0.1", 20071); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 765 | BOOST_CHECK_NE(channel1, channel2); |
| 766 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 767 | auto channel3 = createChannel("::1", 20071); |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 768 | BOOST_CHECK_NE(channel2, channel3); |
| 769 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 770 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 771 | #ifdef __linux__ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 772 | // need superuser privileges to create multicast faces on Linux |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 773 | SKIP_IF_NOT_SUPERUSER(); |
| 774 | #endif // __linux__ |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 775 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 776 | // createChannel with a local endpoint that has already been allocated for a UDP multicast face |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 777 | if (!netifsV4.empty()) { |
| 778 | auto mcastFace = createMulticastFace("127.0.0.1", "224.0.0.254", 20072); |
| 779 | BOOST_CHECK_EXCEPTION(createChannel("127.0.0.1", 20072), UdpFactory::Error, |
| 780 | [] (const UdpFactory::Error& e) { |
| 781 | return strcmp(e.what(), |
| 782 | "Cannot create UDP channel on 127.0.0.1:20072, " |
| 783 | "endpoint already allocated for a UDP multicast face") == 0; |
| 784 | }); |
| 785 | } |
| 786 | if (!netifsV6.empty()) { |
| 787 | auto mcastFace = createMulticastFace("::1", "ff02::114", 20072); |
| 788 | BOOST_CHECK_EXCEPTION(createChannel("::1", 20072), UdpFactory::Error, |
| 789 | [] (const UdpFactory::Error& e) { |
| 790 | return strcmp(e.what(), |
| 791 | "Cannot create UDP channel on [::1]:20072, " |
| 792 | "endpoint already allocated for a UDP multicast face") == 0; |
| 793 | }); |
| 794 | } |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 795 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 796 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 797 | BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV4, UdpFactoryMcastFixture) |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 798 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 799 | #ifdef __linux__ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 800 | // need superuser privileges to create multicast faces on Linux |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 801 | SKIP_IF_NOT_SUPERUSER(); |
| 802 | #endif // __linux__ |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 803 | SKIP_IF_UDP_MCAST_V4_NETIF_COUNT_LT(1); |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 804 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 805 | auto multicastFace1 = createMulticastFace("127.0.0.1", "224.0.0.254", 20070); |
| 806 | auto multicastFace1a = createMulticastFace("127.0.0.1", "224.0.0.254", 20070); |
| 807 | auto multicastFace2 = createMulticastFace("127.0.0.1", "224.0.0.254", 20030); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 808 | BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 809 | BOOST_CHECK_NE(multicastFace1, multicastFace2); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 810 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 811 | auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V4); |
| 812 | if (!address.is_unspecified()) { |
| 813 | auto multicastFace3 = createMulticastFace(address.to_string(), "224.0.0.254", 20070); |
| 814 | BOOST_CHECK_NE(multicastFace1, multicastFace3); |
| 815 | BOOST_CHECK_NE(multicastFace2, multicastFace3); |
| 816 | } |
| 817 | |
| 818 | // create with a local endpoint already used by a channel |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 819 | auto channel = createChannel("127.0.0.1", 20071); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 820 | BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.254", 20071), UdpFactory::Error, |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 821 | [] (const UdpFactory::Error& e) { |
| 822 | return strcmp(e.what(), |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 823 | "Cannot create UDP multicast face on 127.0.0.1:20071, " |
| 824 | "endpoint already allocated for a UDP channel") == 0; |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 825 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 826 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 827 | // create with a local endpoint already used by a multicast face on a different multicast group |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 828 | BOOST_CHECK_EXCEPTION(createMulticastFace("127.0.0.1", "224.0.0.42", 20070), UdpFactory::Error, |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 829 | [] (const UdpFactory::Error& e) { |
| 830 | return strcmp(e.what(), |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 831 | "Cannot create UDP multicast face on 127.0.0.1:20070, " |
| 832 | "endpoint already allocated for a different UDP multicast face") == 0; |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 833 | }); |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 834 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 835 | |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 836 | BOOST_FIXTURE_TEST_CASE(CreateMulticastFaceV6, UdpFactoryMcastFixture) |
| 837 | { |
| 838 | #ifdef __linux__ |
| 839 | // need superuser privileges to create multicast faces on Linux |
| 840 | SKIP_IF_NOT_SUPERUSER(); |
| 841 | #endif // __linux__ |
| 842 | SKIP_IF_UDP_MCAST_V6_NETIF_COUNT_LT(1); |
| 843 | |
| 844 | auto multicastFace1 = createMulticastFace("::1", "ff02::114", 20070); |
| 845 | auto multicastFace1a = createMulticastFace("::1", "ff02::114", 20070); |
| 846 | auto multicastFace2 = createMulticastFace("::1", "ff02::114", 20030); |
| 847 | BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a); |
| 848 | BOOST_CHECK_NE(multicastFace1, multicastFace2); |
| 849 | |
| 850 | auto address = findNonLoopbackAddressForMulticastFace(ndn::net::AddressFamily::V6); |
| 851 | if (!address.is_unspecified()) { |
| 852 | auto multicastFace3 = createMulticastFace(address.to_string(), "ff02::114", 20070); |
| 853 | BOOST_CHECK_NE(multicastFace1, multicastFace3); |
| 854 | BOOST_CHECK_NE(multicastFace2, multicastFace3); |
| 855 | } |
| 856 | |
| 857 | // create with a local endpoint already used by a channel |
| 858 | auto channel = createChannel("::1", 20071); |
| 859 | BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::114", 20071), UdpFactory::Error, |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 860 | [] (const UdpFactory::Error& e) { |
| 861 | return strcmp(e.what(), |
Md Ashiqur Rahman | 8ce0903 | 2018-01-14 22:43:13 -0500 | [diff] [blame] | 862 | "Cannot create UDP multicast face on [::1]:20071, " |
| 863 | "endpoint already allocated for a UDP channel") == 0; |
| 864 | }); |
| 865 | |
| 866 | // create with a local endpoint already used by a multicast face on a different multicast group |
| 867 | BOOST_CHECK_EXCEPTION(createMulticastFace("::1", "ff02::42", 20070), UdpFactory::Error, |
| 868 | [] (const UdpFactory::Error& e) { |
| 869 | return strcmp(e.what(), |
| 870 | "Cannot create UDP multicast face on [::1]:20070, " |
| 871 | "endpoint already allocated for a different UDP multicast face") == 0; |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 872 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 875 | BOOST_AUTO_TEST_CASE(CreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 876 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 877 | createFace(factory, |
| 878 | FaceUri("udp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 879 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 880 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 881 | {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 882 | |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 883 | createChannel("127.0.0.1", 20071); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 884 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 885 | createFace(factory, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 886 | FaceUri("udp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 887 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 888 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 889 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 890 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 891 | createFace(factory, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 892 | FaceUri("udp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 893 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 894 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 895 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 896 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 897 | createFace(factory, |
| 898 | FaceUri("udp4://127.0.0.1:20072"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 899 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 900 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, false}, |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 901 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
| 902 | |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 903 | createFace(factory, |
| 904 | FaceUri("udp4://127.0.0.1:20073"), |
| 905 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 906 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, true, false}, |
| 907 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
| 908 | |
| 909 | createFace(factory, |
| 910 | FaceUri("udp4://127.0.0.1:20073"), |
| 911 | {}, |
| 912 | {ndn::nfd::FACE_PERSISTENCY_PERMANENT, {}, {}, false, false, true}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 913 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 914 | } |
| 915 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame] | 916 | BOOST_AUTO_TEST_CASE(UnsupportedCreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 917 | { |
Davide Pesavento | bb734df | 2017-10-24 18:05:36 -0400 | [diff] [blame] | 918 | createChannel("127.0.0.1", 20071); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 919 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 920 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 921 | FaceUri("udp4://127.0.0.1:20072"), |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 922 | FaceUri("udp4://127.0.0.1:20071"), |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 923 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false}, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 924 | {CreateFaceExpectedResult::FAILURE, 406, |
| 925 | "Unicast UDP faces cannot be created with a LocalUri"}); |
| 926 | |
| 927 | createFace(factory, |
| 928 | FaceUri("udp4://127.0.0.1:20072"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 929 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 930 | {ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, {}, {}, false, false, false}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 931 | {CreateFaceExpectedResult::FAILURE, 406, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 932 | "Outgoing UDP faces do not support on-demand persistency"}); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 933 | |
| 934 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 935 | FaceUri("udp4://233.252.0.1:23252"), |
| 936 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 937 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false}, |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 938 | {CreateFaceExpectedResult::FAILURE, 406, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 939 | "Cannot create multicast UDP faces"}); |
| 940 | |
| 941 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 942 | FaceUri("udp4://127.0.0.1:20072"), |
| 943 | {}, |
Eric Newberry | 0c3e57b | 2018-01-25 20:54:46 -0700 | [diff] [blame] | 944 | {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, true, false, false}, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 945 | {CreateFaceExpectedResult::FAILURE, 406, |
| 946 | "Local fields can only be enabled on faces with local scope"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 949 | BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory |
| 950 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 951 | |
| 952 | } // namespace tests |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 953 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 954 | } // namespace nfd |