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