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 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 28 | #include "face-system-fixture.hpp" |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame^] | 29 | #include "factory-test-common.hpp" |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 30 | #include "test-netif-ip.hpp" |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame^] | 31 | |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 32 | #include <boost/algorithm/string/replace.hpp> |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame^] | 33 | #include <boost/range/algorithm/count_if.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); |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame^] | 321 | BOOST_CHECK_EQUAL(boost::count_if(udpMcastFaces, [this] (const Face* face) { |
Junxiao Shi | c31080d | 2017-01-24 15:10:12 +0000 | [diff] [blame] | 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 | { |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame^] | 495 | BOOST_CHECK_EQUAL(factory.getChannels().empty(), true); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 496 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame^] | 497 | std::set<std::string> expected; |
| 498 | expected.insert(factory.createChannel("127.0.0.1", "20070")->getUri().toString()); |
| 499 | expected.insert(factory.createChannel("127.0.0.1", "20071")->getUri().toString()); |
| 500 | expected.insert(factory.createChannel("::1", "20071")->getUri().toString()); |
| 501 | checkChannelListEqual(factory, expected); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 504 | BOOST_AUTO_TEST_CASE(CreateChannel) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 505 | { |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 506 | auto channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 507 | auto channel1a = factory.createChannel("127.0.0.1", "20070"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 508 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 509 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070"); |
| 510 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 511 | auto channel2 = factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 512 | BOOST_CHECK_NE(channel1, channel2); |
| 513 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 514 | auto channel3 = factory.createChannel("::1", "20071"); |
| 515 | BOOST_CHECK_NE(channel2, channel3); |
| 516 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "udp6://[::1]:20071"); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 517 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 518 | // createChannel with multicast address |
| 519 | BOOST_CHECK_EXCEPTION(factory.createChannel("224.0.0.1", "20070"), UdpFactory::Error, |
| 520 | [] (const UdpFactory::Error& e) { |
| 521 | return strcmp(e.what(), |
| 522 | "createChannel is only for unicast channels. The provided endpoint " |
| 523 | "is multicast. Use createMulticastFace to create a multicast face") == 0; |
| 524 | }); |
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 a local endpoint that has already been allocated for a UDP multicast face |
| 527 | auto multicastFace = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20072"); |
| 528 | BOOST_CHECK_EXCEPTION(factory.createChannel("127.0.0.1", "20072"), UdpFactory::Error, |
| 529 | [] (const UdpFactory::Error& e) { |
| 530 | return strcmp(e.what(), |
| 531 | "Cannot create the requested UDP unicast channel, local " |
| 532 | "endpoint is already allocated for a UDP multicast face") == 0; |
| 533 | }); |
| 534 | } |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 535 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 536 | BOOST_AUTO_TEST_CASE(CreateMulticastFace) |
| 537 | { |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 538 | auto multicastFace1 = factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20070"); |
| 539 | 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] | 540 | BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a); |
| 541 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 542 | // createMulticastFace with a local endpoint that is already used by a channel |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 543 | auto channel = factory.createChannel("127.0.0.1", "20071"); |
| 544 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.1", "20071"), UdpFactory::Error, |
| 545 | [] (const UdpFactory::Error& e) { |
| 546 | return strcmp(e.what(), |
| 547 | "Cannot create the requested UDP multicast face, local " |
| 548 | "endpoint is already allocated for a UDP unicast channel") == 0; |
| 549 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 550 | |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 551 | // createMulticastFace with a local endpoint that is already |
| 552 | // used by a multicast face on a different multicast group |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 553 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "224.0.0.42", "20070"), UdpFactory::Error, |
| 554 | [] (const UdpFactory::Error& e) { |
| 555 | return strcmp(e.what(), |
| 556 | "Cannot create the requested UDP multicast face, local " |
| 557 | "endpoint is already allocated for a UDP multicast face " |
| 558 | "on a different multicast group") == 0; |
| 559 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 560 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 561 | // createMulticastFace with an IPv4 unicast address |
| 562 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("127.0.0.1", "192.168.10.15", "20072"), UdpFactory::Error, |
| 563 | [] (const UdpFactory::Error& e) { |
| 564 | return strcmp(e.what(), |
| 565 | "Cannot create the requested UDP multicast face, " |
| 566 | "the multicast group given as input is not a multicast address") == 0; |
| 567 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 568 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 569 | // createMulticastFace with an IPv6 multicast address |
| 570 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace("::1", "ff01::114", "20073"), UdpFactory::Error, |
| 571 | [] (const UdpFactory::Error& e) { |
| 572 | return strcmp(e.what(), |
| 573 | "IPv6 multicast is not supported yet. Please provide an IPv4 " |
| 574 | "address") == 0; |
| 575 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 576 | |
Weiwei Liu | 72cee94 | 2016-02-04 16:49:19 -0700 | [diff] [blame] | 577 | // createMulticastFace with different local and remote port numbers |
Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 578 | udp::Endpoint localEndpoint(boost::asio::ip::address_v4::loopback(), 20074); |
| 579 | 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] | 580 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace(localEndpoint, multicastEndpoint), UdpFactory::Error, |
| 581 | [] (const UdpFactory::Error& e) { |
| 582 | return strcmp(e.what(), |
| 583 | "Cannot create the requested UDP multicast face, " |
| 584 | "both endpoints should have the same port number. ") == 0; |
| 585 | }); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame^] | 588 | BOOST_AUTO_TEST_CASE(CreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 589 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 590 | createFace(factory, |
| 591 | FaceUri("udp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 592 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 593 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 594 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 595 | {CreateFaceExpectedResult::FAILURE, 504, "No channels available to connect"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 596 | |
| 597 | factory.createChannel("127.0.0.1", "20071"); |
| 598 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 599 | createFace(factory, |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 600 | FaceUri("udp4://127.0.0.1:6363"), |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 601 | {}, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 602 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 603 | false, |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 604 | {CreateFaceExpectedResult::SUCCESS, 0, ""}); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 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_PERMANENT, |
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, ""}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 612 | |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 613 | createFace(factory, |
| 614 | FaceUri("udp4://127.0.0.1:20072"), |
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 | } |
| 620 | |
Davide Pesavento | b15276f | 2017-07-15 16:27:13 -0400 | [diff] [blame^] | 621 | BOOST_AUTO_TEST_CASE(UnsupportedCreateFace) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 622 | { |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 623 | factory.createChannel("127.0.0.1", "20071"); |
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, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 626 | FaceUri("udp4://127.0.0.1:20072"), |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 627 | FaceUri("udp4://127.0.0.1:20071"), |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 628 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 629 | false, |
| 630 | {CreateFaceExpectedResult::FAILURE, 406, |
| 631 | "Unicast UDP faces cannot be created with a LocalUri"}); |
| 632 | |
| 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_ON_DEMAND, |
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::FAILURE, 406, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 639 | "Outgoing UDP faces do not support on-demand persistency"}); |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 640 | |
| 641 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 642 | FaceUri("udp4://233.252.0.1:23252"), |
| 643 | {}, |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 644 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 645 | false, |
| 646 | {CreateFaceExpectedResult::FAILURE, 406, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 647 | "Cannot create multicast UDP faces"}); |
| 648 | |
| 649 | createFace(factory, |
Davide Pesavento | 46afec4 | 2017-05-28 14:28:47 -0400 | [diff] [blame] | 650 | FaceUri("udp4://127.0.0.1:20072"), |
| 651 | {}, |
| 652 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 653 | true, |
| 654 | {CreateFaceExpectedResult::FAILURE, 406, |
| 655 | "Local fields can only be enabled on faces with local scope"}); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 658 | BOOST_AUTO_TEST_SUITE_END() // TestUdpFactory |
| 659 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 660 | |
| 661 | } // namespace tests |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 662 | } // namespace face |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 663 | } // namespace nfd |