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