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 | /* |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 28 | #include "factory-test-common.hpp" |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 29 | #include "face-system-fixture.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | #include "tests/limited-io.hpp" |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 31 | #include "test-netif-ip.hpp" |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 32 | #include <boost/algorithm/string/replace.hpp> |
| 33 | #include <boost/range/algorithm.hpp> |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 34 | |
| 35 | namespace nfd { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 36 | namespace face { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 37 | namespace tests { |
| 38 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 39 | using UdpFactoryFixture = FaceSystemFactoryFixture<UdpFactory>; |
| 40 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(Face) |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 42 | BOOST_FIXTURE_TEST_SUITE(TestUdpFactory, UdpFactoryFixture) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 43 | |
| 44 | using nfd::Face; |
| 45 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 46 | BOOST_AUTO_TEST_SUITE(ProcessConfig) |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 47 | |
| 48 | BOOST_AUTO_TEST_CASE(Channels) |
| 49 | { |
| 50 | const std::string CONFIG = R"CONFIG( |
| 51 | face_system |
| 52 | { |
| 53 | udp |
| 54 | { |
| 55 | port 7001 |
| 56 | enable_v4 yes |
| 57 | enable_v6 yes |
| 58 | idle_timeout 30 |
| 59 | mcast no |
| 60 | } |
| 61 | } |
| 62 | )CONFIG"; |
| 63 | |
| 64 | parseConfig(CONFIG, true); |
| 65 | parseConfig(CONFIG, false); |
| 66 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 67 | checkChannelListEqual(factory, {"udp4://0.0.0.0:7001", "udp6://[::]:7001"}); |
| 68 | } |
| 69 | |
| 70 | BOOST_AUTO_TEST_CASE(ChannelV4) |
| 71 | { |
| 72 | const std::string CONFIG = R"CONFIG( |
| 73 | face_system |
| 74 | { |
| 75 | udp |
| 76 | { |
| 77 | port 7001 |
| 78 | enable_v4 yes |
| 79 | enable_v6 no |
| 80 | mcast no |
| 81 | } |
| 82 | } |
| 83 | )CONFIG"; |
| 84 | |
| 85 | parseConfig(CONFIG, true); |
| 86 | parseConfig(CONFIG, false); |
| 87 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 88 | checkChannelListEqual(factory, {"udp4://0.0.0.0:7001"}); |
| 89 | } |
| 90 | |
| 91 | BOOST_AUTO_TEST_CASE(ChannelV6) |
| 92 | { |
| 93 | const std::string CONFIG = R"CONFIG( |
| 94 | face_system |
| 95 | { |
| 96 | udp |
| 97 | { |
| 98 | port 7001 |
| 99 | enable_v4 no |
| 100 | enable_v6 yes |
| 101 | mcast no |
| 102 | } |
| 103 | } |
| 104 | )CONFIG"; |
| 105 | |
| 106 | parseConfig(CONFIG, true); |
| 107 | parseConfig(CONFIG, false); |
| 108 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 109 | checkChannelListEqual(factory, {"udp6://[::]:7001"}); |
| 110 | } |
| 111 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 112 | class UdpMcastConfigFixture : public UdpFactoryFixture |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 113 | { |
| 114 | protected: |
| 115 | UdpMcastConfigFixture() |
| 116 | { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 117 | for (const auto& netif : collectNetworkInterfaces()) { |
| 118 | if (netif->isUp() && netif->canMulticast() && hasAddressFamily<AddressFamily::V4>(*netif)) { |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 119 | netifs.push_back(netif); |
| 120 | } |
| 121 | } |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 122 | |
| 123 | this->copyRealNetifsToNetmon(); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | std::vector<const Face*> |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 127 | listUdpMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 128 | { |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 129 | return this->listFacesByScheme("udp4", linkType); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | size_t |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 133 | countUdpMcastFaces(ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_MULTI_ACCESS) const |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 134 | { |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 135 | return this->listUdpMcastFaces(linkType).size(); |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 138 | /** \brief determine whether a UDP multicast face is created on \p netif |
| 139 | */ |
| 140 | static bool |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 141 | isFaceOnNetif(const Face& face, const shared_ptr<const ndn::net::NetworkInterface>& netif) |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 142 | { |
| 143 | auto ip = boost::asio::ip::address_v4::from_string(face.getLocalUri().getHost()); |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 144 | return std::any_of(netif->getNetworkAddresses().begin(), netif->getNetworkAddresses().end(), |
| 145 | [ip] (const ndn::net::NetworkAddress& a) { return a.getIp() == ip; }); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 148 | protected: |
| 149 | /** \brief MulticastUdpTransport-capable network interfaces |
| 150 | */ |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 151 | std::vector<shared_ptr<const ndn::net::NetworkInterface>> netifs; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | #define SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(n) \ |
| 155 | do { \ |
| 156 | if (this->netifs.size() < (n)) { \ |
| 157 | BOOST_WARN_MESSAGE(false, "skipping assertions that require " #n \ |
| 158 | " or more MulticastUdpTransport-capable network interfaces"); \ |
| 159 | return; \ |
| 160 | } \ |
| 161 | } while (false) |
| 162 | |
| 163 | BOOST_FIXTURE_TEST_CASE(EnableDisableMcast, UdpMcastConfigFixture) |
| 164 | { |
| 165 | #ifdef __linux__ |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 166 | // need superuser privilege for creating multicast faces on Linux |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 167 | SKIP_IF_NOT_SUPERUSER(); |
| 168 | #endif // __linux__ |
| 169 | |
| 170 | const std::string CONFIG_WITH_MCAST = R"CONFIG( |
| 171 | face_system |
| 172 | { |
| 173 | udp |
| 174 | { |
| 175 | mcast yes |
| 176 | } |
| 177 | } |
| 178 | )CONFIG"; |
| 179 | const std::string CONFIG_WITHOUT_MCAST = R"CONFIG( |
| 180 | face_system |
| 181 | { |
| 182 | udp |
| 183 | { |
| 184 | mcast no |
| 185 | } |
| 186 | } |
| 187 | )CONFIG"; |
| 188 | |
| 189 | parseConfig(CONFIG_WITHOUT_MCAST, false); |
| 190 | BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), 0); |
| 191 | |
| 192 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1); |
| 193 | |
| 194 | parseConfig(CONFIG_WITH_MCAST, false); |
| 195 | g_io.poll(); |
| 196 | BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), netifs.size()); |
| 197 | |
| 198 | parseConfig(CONFIG_WITHOUT_MCAST, false); |
| 199 | g_io.poll(); |
| 200 | BOOST_CHECK_EQUAL(this->countUdpMcastFaces(), 0); |
| 201 | } |
| 202 | |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 203 | BOOST_FIXTURE_TEST_CASE(McastAdHoc, UdpMcastConfigFixture) |
| 204 | { |
| 205 | #ifdef __linux__ |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 206 | // need superuser privilege for creating multicast faces on Linux |
Teng Liang | fe4fce3 | 2017-03-29 04:49:38 +0000 | [diff] [blame] | 207 | SKIP_IF_NOT_SUPERUSER(); |
| 208 | #endif // __linux__ |
| 209 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1); |
| 210 | |
| 211 | const std::string CONFIG = R"CONFIG( |
| 212 | face_system |
| 213 | { |
| 214 | udp |
| 215 | { |
| 216 | mcast_ad_hoc yes |
| 217 | } |
| 218 | } |
| 219 | )CONFIG"; |
| 220 | |
| 221 | parseConfig(CONFIG, false); |
| 222 | BOOST_CHECK_EQUAL(this->countUdpMcastFaces(ndn::nfd::LINK_TYPE_AD_HOC), netifs.size()); |
| 223 | } |
| 224 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 225 | BOOST_FIXTURE_TEST_CASE(ChangeMcastEndpoint, UdpMcastConfigFixture) |
| 226 | { |
| 227 | #ifdef __linux__ |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 228 | // need superuser privilege for creating multicast faces on Linux |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 229 | SKIP_IF_NOT_SUPERUSER(); |
| 230 | #endif // __linux__ |
| 231 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1); |
| 232 | |
| 233 | const std::string CONFIG1 = R"CONFIG( |
| 234 | face_system |
| 235 | { |
| 236 | udp |
| 237 | { |
| 238 | mcast_group 239.66.30.1 |
| 239 | mcast_port 7011 |
| 240 | } |
| 241 | } |
| 242 | )CONFIG"; |
| 243 | const std::string CONFIG2 = R"CONFIG( |
| 244 | face_system |
| 245 | { |
| 246 | udp |
| 247 | { |
| 248 | mcast_group 239.66.30.2 |
| 249 | mcast_port 7012 |
| 250 | } |
| 251 | } |
| 252 | )CONFIG"; |
| 253 | |
| 254 | parseConfig(CONFIG1, false); |
| 255 | auto udpMcastFaces = this->listUdpMcastFaces(); |
| 256 | BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifs.size()); |
| 257 | BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), |
| 258 | FaceUri("udp4://239.66.30.1:7011")); |
| 259 | |
| 260 | parseConfig(CONFIG2, false); |
| 261 | g_io.poll(); |
| 262 | udpMcastFaces = this->listUdpMcastFaces(); |
| 263 | BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), netifs.size()); |
| 264 | BOOST_CHECK_EQUAL(udpMcastFaces.front()->getRemoteUri(), |
| 265 | FaceUri("udp4://239.66.30.2:7012")); |
| 266 | } |
| 267 | |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 268 | BOOST_FIXTURE_TEST_CASE(Whitelist, UdpMcastConfigFixture) |
| 269 | { |
| 270 | #ifdef __linux__ |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 271 | // need superuser privilege for creating multicast faces on Linux |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 272 | SKIP_IF_NOT_SUPERUSER(); |
| 273 | #endif // __linux__ |
| 274 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1); |
| 275 | |
| 276 | std::string CONFIG = R"CONFIG( |
| 277 | face_system |
| 278 | { |
| 279 | udp |
| 280 | { |
| 281 | whitelist |
| 282 | { |
| 283 | ifname %ifname |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | )CONFIG"; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 288 | boost::replace_first(CONFIG, "%ifname", netifs.front()->getName()); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 289 | |
| 290 | parseConfig(CONFIG, false); |
| 291 | auto udpMcastFaces = this->listUdpMcastFaces(); |
| 292 | BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1); |
| 293 | BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.front())); |
| 294 | } |
| 295 | |
| 296 | BOOST_FIXTURE_TEST_CASE(Blacklist, UdpMcastConfigFixture) |
| 297 | { |
| 298 | #ifdef __linux__ |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 299 | // need superuser privilege for creating multicast faces on Linux |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 300 | SKIP_IF_NOT_SUPERUSER(); |
| 301 | #endif // __linux__ |
| 302 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(1); |
| 303 | |
| 304 | std::string CONFIG = R"CONFIG( |
| 305 | face_system |
| 306 | { |
| 307 | udp |
| 308 | { |
| 309 | blacklist |
| 310 | { |
| 311 | ifname %ifname |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | )CONFIG"; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 316 | boost::replace_first(CONFIG, "%ifname", netifs.front()->getName()); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 317 | |
| 318 | parseConfig(CONFIG, false); |
| 319 | auto udpMcastFaces = this->listUdpMcastFaces(); |
| 320 | BOOST_CHECK_EQUAL(udpMcastFaces.size(), netifs.size() - 1); |
| 321 | BOOST_CHECK_EQUAL(boost::count_if(udpMcastFaces, [=] (const Face* face) { |
| 322 | return isFaceOnNetif(*face, netifs.front()); |
| 323 | }), 0); |
| 324 | } |
| 325 | |
| 326 | BOOST_FIXTURE_TEST_CASE(ChangePredicate, UdpMcastConfigFixture) |
| 327 | { |
| 328 | #ifdef __linux__ |
Junxiao Shi | bbace1d | 2017-08-06 20:03:37 +0000 | [diff] [blame] | 329 | // need superuser privilege for creating multicast faces on Linux |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 330 | SKIP_IF_NOT_SUPERUSER(); |
| 331 | #endif // __linux__ |
| 332 | SKIP_IF_UDP_MCAST_NETIF_COUNT_LT(2); |
| 333 | |
| 334 | std::string CONFIG1 = R"CONFIG( |
| 335 | face_system |
| 336 | { |
| 337 | udp |
| 338 | { |
| 339 | whitelist |
| 340 | { |
| 341 | ifname %ifname |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | )CONFIG"; |
| 346 | std::string CONFIG2 = CONFIG1; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 347 | boost::replace_first(CONFIG1, "%ifname", netifs.front()->getName()); |
| 348 | boost::replace_first(CONFIG2, "%ifname", netifs.back()->getName()); |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 349 | |
| 350 | parseConfig(CONFIG1, false); |
| 351 | auto udpMcastFaces = this->listUdpMcastFaces(); |
| 352 | BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1); |
| 353 | BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.front())); |
| 354 | |
| 355 | parseConfig(CONFIG2, false); |
| 356 | g_io.poll(); |
| 357 | udpMcastFaces = this->listUdpMcastFaces(); |
| 358 | BOOST_REQUIRE_EQUAL(udpMcastFaces.size(), 1); |
| 359 | BOOST_CHECK(isFaceOnNetif(*udpMcastFaces.front(), netifs.back())); |
| 360 | } |
| 361 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 362 | BOOST_AUTO_TEST_CASE(Omitted) |
| 363 | { |
| 364 | const std::string CONFIG = R"CONFIG( |
| 365 | face_system |
| 366 | { |
| 367 | } |
| 368 | )CONFIG"; |
| 369 | |
| 370 | parseConfig(CONFIG, true); |
| 371 | parseConfig(CONFIG, false); |
| 372 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 373 | BOOST_CHECK_EQUAL(factory.getChannels().size(), 0); |
| 374 | BOOST_CHECK_EQUAL(this->listFacesByScheme("udp4", ndn::nfd::LINK_TYPE_MULTI_ACCESS).size(), 0); |
| 375 | } |
| 376 | |
| 377 | BOOST_AUTO_TEST_CASE(BadIdleTimeout) |
| 378 | { |
| 379 | const std::string CONFIG = R"CONFIG( |
| 380 | face_system |
| 381 | { |
| 382 | udp |
| 383 | { |
| 384 | idle_timeout hello |
| 385 | } |
| 386 | } |
| 387 | )CONFIG"; |
| 388 | |
| 389 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 390 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 391 | } |
| 392 | |
| 393 | BOOST_AUTO_TEST_CASE(BadMcast) |
| 394 | { |
| 395 | const std::string CONFIG = R"CONFIG( |
| 396 | face_system |
| 397 | { |
| 398 | udp |
| 399 | { |
| 400 | mcast hello |
| 401 | } |
| 402 | } |
| 403 | )CONFIG"; |
| 404 | |
| 405 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 406 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 407 | } |
| 408 | |
| 409 | BOOST_AUTO_TEST_CASE(BadMcastGroup) |
| 410 | { |
| 411 | const std::string CONFIG = R"CONFIG( |
| 412 | face_system |
| 413 | { |
| 414 | udp |
| 415 | { |
| 416 | mcast_group hello |
| 417 | } |
| 418 | } |
| 419 | )CONFIG"; |
| 420 | |
| 421 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 422 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 423 | } |
| 424 | |
| 425 | BOOST_AUTO_TEST_CASE(BadMcastGroupV4Unicast) |
| 426 | { |
| 427 | const std::string CONFIG = R"CONFIG( |
| 428 | face_system |
| 429 | { |
| 430 | udp |
| 431 | { |
| 432 | mcast_group 10.0.0.1 |
| 433 | } |
| 434 | } |
| 435 | )CONFIG"; |
| 436 | |
| 437 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 438 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 439 | } |
| 440 | |
| 441 | BOOST_AUTO_TEST_CASE(BadMcastGroupV6) |
| 442 | { |
| 443 | const std::string CONFIG = R"CONFIG( |
| 444 | face_system |
| 445 | { |
| 446 | udp |
| 447 | { |
| 448 | mcast_group ff00::1 |
| 449 | } |
| 450 | } |
| 451 | )CONFIG"; |
| 452 | |
| 453 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 454 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 455 | } |
| 456 | |
| 457 | BOOST_AUTO_TEST_CASE(AllDisabled) |
| 458 | { |
| 459 | const std::string CONFIG = R"CONFIG( |
| 460 | face_system |
| 461 | { |
| 462 | udp |
| 463 | { |
| 464 | enable_v4 no |
| 465 | enable_v6 no |
| 466 | mcast no |
| 467 | } |
| 468 | } |
| 469 | )CONFIG"; |
| 470 | |
| 471 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 472 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 473 | } |
| 474 | |
| 475 | BOOST_AUTO_TEST_CASE(UnknownOption) |
| 476 | { |
| 477 | const std::string CONFIG = R"CONFIG( |
| 478 | face_system |
| 479 | { |
| 480 | udp |
| 481 | { |
| 482 | hello |
| 483 | } |
| 484 | } |
| 485 | )CONFIG"; |
| 486 | |
| 487 | BOOST_CHECK_THROW(parseConfig(CONFIG, true), ConfigFile::Error); |
| 488 | BOOST_CHECK_THROW(parseConfig(CONFIG, false), ConfigFile::Error); |
| 489 | } |
| 490 | |
| 491 | BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
| 492 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 493 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 494 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 495 | BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true); |
| 496 | |
| 497 | std::vector<shared_ptr<const Channel>> expectedChannels; |
| 498 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070")); |
| 499 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071")); |
| 500 | expectedChannels.push_back(factory.createChannel("::1", "20071")); |
| 501 | |
| 502 | for (const auto& i : factory.getChannels()) { |
| 503 | auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i); |
| 504 | BOOST_REQUIRE(pos != expectedChannels.end()); |
| 505 | expectedChannels.erase(pos); |
| 506 | } |
| 507 | |
| 508 | BOOST_CHECK_EQUAL(expectedChannels.size(), 0); |
| 509 | } |
| 510 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 511 | BOOST_AUTO_TEST_CASE(CreateChannel) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 512 | { |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 513 | auto channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 514 | auto channel1a = factory.createChannel("127.0.0.1", "20070"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 515 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 516 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070"); |
| 517 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 518 | auto channel2 = factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 519 | BOOST_CHECK_NE(channel1, channel2); |
| 520 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 521 | auto channel3 = factory.createChannel("::1", "20071"); |
| 522 | BOOST_CHECK_NE(channel2, channel3); |
| 523 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 524 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 525 | // createChannel with multicast address |
| 526 | BOOST_CHECK_EXCEPTION(factory.createChannel("224.0.0.1", "20070"), UdpFactory::Error, |
| 527 | [] (const UdpFactory::Error& e) { |
| 528 | return strcmp(e.what(), |
| 529 | "createChannel is only for unicast channels. The provided endpoint " |
| 530 | "is multicast. Use createMulticastFace to create a multicast face") == 0; |
| 531 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 532 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 533 | // createChannel with a local endpoint that has already been allocated for a UDP multicast face |
| 534 | auto multicastFace = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20072"); |
| 535 | BOOST_CHECK_EXCEPTION(factory.createChannel("127.0.0.1", "20072"), UdpFactory::Error, |
| 536 | [] (const UdpFactory::Error& e) { |
| 537 | return strcmp(e.what(), |
| 538 | "Cannot create the requested UDP unicast channel, local " |
| 539 | "endpoint is already allocated for a UDP multicast face") == 0; |
| 540 | }); |
| 541 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 542 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 543 | BOOST_AUTO_TEST_CASE(CreateMulticastFace) |
| 544 | { |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 545 | auto multicastFace1 = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070"); |
| 546 | auto multicastFace1a = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 547 | BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a); |
| 548 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 549 | // createMulticastFace with a local endpoint that is already used by a channel |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 550 | auto channel = factory.createChannel("127.0.0.1", "20071"); |
| 551 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20071"), UdpFactory::Error, |
| 552 | [] (const UdpFactory::Error& e) { |
| 553 | return strcmp(e.what(), |
| 554 | "Cannot create the requested UDP multicast face, local " |
| 555 | "endpoint is already allocated for a UDP unicast channel") == 0; |
| 556 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 557 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 558 | // createMulticastFace with a local endpoint that is already |
| 559 | // used by a multicast face on a different multicast group |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 560 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.42", "20070"), UdpFactory::Error, |
| 561 | [] (const UdpFactory::Error& e) { |
| 562 | return strcmp(e.what(), |
| 563 | "Cannot create the requested UDP multicast face, local " |
| 564 | "endpoint is already allocated for a UDP multicast face " |
| 565 | "on a different multicast group") == 0; |
| 566 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 567 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 568 | // createMulticastFace with an IPv4 unicast address |
| 569 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "192.168.10.15", "20072"), UdpFactory::Error, |
| 570 | [] (const UdpFactory::Error& e) { |
| 571 | return strcmp(e.what(), |
| 572 | "Cannot create the requested UDP multicast face, " |
| 573 | "the multicast group given as input is not a multicast address") == 0; |
| 574 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 575 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 576 | // createMulticastFace with an IPv6 multicast address |
| 577 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("::1", "ff01::114", "20073"), UdpFactory::Error, |
| 578 | [] (const UdpFactory::Error& e) { |
| 579 | return strcmp(e.what(), |
| 580 | "IPv6 multicast is not supported yet. Please provide an IPv4 " |
| 581 | "address") == 0; |
| 582 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 583 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 584 | // createMulticastFace with different local and remote port numbers |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 585 | udp::Endpoint localEndpoint(boost::asio::ip::address_v4::loopback(), 20074); |
| 586 | udp::Endpoint multicastEndpoint(boost::asio::ip::address::from_string("224.0.0.1"), 20075); |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 587 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace(localEndpoint, multicastEndpoint), UdpFactory::Error, |
| 588 | [] (const UdpFactory::Error& e) { |
| 589 | return strcmp(e.what(), |
| 590 | "Cannot create the requested UDP multicast face, " |
| 591 | "both endpoints should have the same port number. ") == 0; |
| 592 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 595 | BOOST_AUTO_TEST_CASE(FaceCreate) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 596 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 597 | createFace(factory, |
| 598 | FaceUri("udp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 599 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 600 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 601 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 602 | {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 603 | |
| 604 | factory.createChannel("127.0.0.1", "20071"); |
| 605 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 606 | createFace(factory, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 607 | FaceUri("udp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 608 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 609 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 610 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 611 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 612 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 613 | createFace(factory, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 614 | FaceUri("udp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 615 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 616 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 617 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 618 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 619 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 620 | createFace(factory, |
| 621 | FaceUri("udp4://127.0.0.1:20072"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 622 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 623 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 624 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 625 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
| 629 | { |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 630 | factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 631 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 632 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 633 | FaceUri("udp4://127.0.0.1:20072"), |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 634 | FaceUri("udp4://127.0.0.1:20071"), |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 635 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 636 | false, |
| 637 | {CreateFaceExpectedResult::FAILURE, 406, |
| 638 | "Unicast UDP faces cannot be created with a LocalUri"}); |
| 639 | |
| 640 | createFace(factory, |
| 641 | FaceUri("udp4://127.0.0.1:20072"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 642 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 643 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 644 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 645 | {CreateFaceExpectedResult::FAILURE, 406, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 646 | "Outgoing UDP faces do not support on-demand persistency"}); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 647 | |
| 648 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 649 | FaceUri("udp4://233.252.0.1:23252"), |
| 650 | {}, |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 651 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 652 | false, |
| 653 | {CreateFaceExpectedResult::FAILURE, 406, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 654 | "Cannot create multicast UDP faces"}); |
| 655 | |
| 656 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 657 | FaceUri("udp4://127.0.0.1:20072"), |
| 658 | {}, |
| 659 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 660 | true, |
| 661 | {CreateFaceExpectedResult::FAILURE, 406, |
| 662 | "Local fields can only be enabled on faces with local scope"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 665 | BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory |
| 666 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 667 | |
| 668 | } // namespace tests |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 669 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 670 | } // namespace nfd |