Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | e3ef6ee | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 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 |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
| 28 | #include "util/face-uri.hpp" |
| 29 | |
| 30 | #include "boost-test.hpp" |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace util { |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(UtilTestFaceUri) |
| 36 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 37 | class CanonizeFixture : noncopyable |
| 38 | { |
| 39 | protected: |
| 40 | CanonizeFixture() |
| 41 | : m_nPending(0) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | addTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri); |
| 47 | |
| 48 | void |
| 49 | runTests() |
| 50 | { |
| 51 | m_io.run(); |
| 52 | BOOST_CHECK_EQUAL(m_nPending, 0); |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | struct CanonizeTestCase |
| 57 | { |
| 58 | public: |
| 59 | CanonizeTestCase(const std::string& request, |
| 60 | bool shouldSucceed, const std::string& expectedUri) |
| 61 | : m_request(request) |
| 62 | , m_shouldSucceed(shouldSucceed) |
| 63 | , m_expectedUri(expectedUri) |
| 64 | , m_isCompleted(false) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | public: |
| 69 | std::string m_request; |
| 70 | bool m_shouldSucceed; |
| 71 | std::string m_expectedUri; |
| 72 | bool m_isCompleted; |
| 73 | }; |
| 74 | |
| 75 | // tc is a shared_ptr passed by value, because this function can take ownership |
| 76 | void |
| 77 | onCanonizeSuccess(shared_ptr<CanonizeTestCase> tc, const FaceUri& canonicalUri) |
| 78 | { |
| 79 | BOOST_CHECK(!tc->m_isCompleted); |
| 80 | tc->m_isCompleted = true; |
| 81 | --m_nPending; |
| 82 | |
| 83 | BOOST_CHECK_MESSAGE(tc->m_shouldSucceed, tc->m_request + " should fail"); |
| 84 | BOOST_CHECK_EQUAL(tc->m_expectedUri, canonicalUri.toString()); |
| 85 | } |
| 86 | |
| 87 | // tc is a shared_ptr passed by value, because this function can take ownership |
| 88 | void |
| 89 | onCanonizeFailure(shared_ptr<CanonizeTestCase> tc, const std::string& reason) |
| 90 | { |
| 91 | BOOST_CHECK(!tc->m_isCompleted); |
| 92 | tc->m_isCompleted = true; |
| 93 | --m_nPending; |
| 94 | |
| 95 | BOOST_CHECK_MESSAGE(!tc->m_shouldSucceed, tc->m_request + " should succeed"); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | boost::asio::io_service m_io; |
| 100 | ssize_t m_nPending; |
| 101 | }; |
| 102 | |
| 103 | void |
| 104 | CanonizeFixture::addTest(const std::string& request, |
| 105 | bool shouldSucceed, const std::string& expectedUri) |
| 106 | { |
| 107 | ++m_nPending; |
| 108 | shared_ptr<CanonizeTestCase> tc = ndn::make_shared<CanonizeTestCase>( |
| 109 | request, shouldSucceed, expectedUri); |
| 110 | FaceUri uri(request); |
| 111 | uri.canonize(bind(&CanonizeFixture::onCanonizeSuccess, this, tc, _1), |
| 112 | bind(&CanonizeFixture::onCanonizeFailure, this, tc, _1), |
| 113 | m_io, time::seconds(4)); |
| 114 | } |
| 115 | |
| 116 | BOOST_AUTO_TEST_CASE(ParseInternal) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 117 | { |
| 118 | FaceUri uri; |
| 119 | |
| 120 | BOOST_CHECK(uri.parse("internal://")); |
| 121 | BOOST_CHECK_EQUAL(uri.getScheme(), "internal"); |
| 122 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 123 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 124 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 125 | |
| 126 | BOOST_CHECK_EQUAL(uri.parse("internal:"), false); |
| 127 | BOOST_CHECK_EQUAL(uri.parse("internal:/"), false); |
| 128 | } |
| 129 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 130 | BOOST_AUTO_TEST_CASE(ParseUdp) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 131 | { |
| 132 | BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363")); |
| 133 | BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error); |
| 134 | BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error); |
| 135 | |
| 136 | FaceUri uri; |
| 137 | BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false); |
| 138 | |
| 139 | BOOST_CHECK(uri.parse("udp://hostname:80")); |
| 140 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp"); |
| 141 | BOOST_CHECK_EQUAL(uri.getHost(), "hostname"); |
| 142 | BOOST_CHECK_EQUAL(uri.getPort(), "80"); |
| 143 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 144 | |
| 145 | BOOST_CHECK(uri.parse("udp4://192.0.2.1:20")); |
| 146 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp4"); |
| 147 | BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1"); |
| 148 | BOOST_CHECK_EQUAL(uri.getPort(), "20"); |
| 149 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 150 | |
| 151 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363")); |
| 152 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
| 153 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1"); |
| 154 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
| 155 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 156 | |
| 157 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363")); |
| 158 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
| 159 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3"); |
| 160 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
| 161 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 162 | |
| 163 | BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false); |
| 164 | |
| 165 | using namespace boost::asio; |
| 166 | ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 167 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 168 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777"); |
| 169 | |
| 170 | ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 171 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 172 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777"); |
| 173 | } |
| 174 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 175 | BOOST_FIXTURE_TEST_CASE(CanonizeUdp, CanonizeFixture) |
| 176 | { |
| 177 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true); |
| 178 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true); |
| 179 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true); |
| 180 | |
| 181 | BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true); |
| 182 | BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false); |
| 183 | BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false); |
| 184 | BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false); |
| 185 | BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true); |
| 186 | BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false); |
| 187 | BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false); |
| 188 | BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false); |
| 189 | BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false); |
| 190 | BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true); |
| 191 | |
| 192 | // IPv4 unicast |
| 193 | addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363"); |
| 194 | addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363"); |
| 195 | addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363"); |
| 196 | addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363"); |
| 197 | addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695"); |
| 198 | addTest("udp4://192.0.2.666:6363", false, ""); |
| 199 | addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363"); |
| 200 | addTest("udp4://invalid.invalid", false, ""); |
| 201 | |
| 202 | // IPv4 multicast |
| 203 | addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363"); |
| 204 | addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363"); |
| 205 | addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363"); |
| 206 | |
| 207 | // IPv6 unicast |
| 208 | addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363"); |
| 209 | addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363"); |
| 210 | addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363"); |
| 211 | addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363"); |
| 212 | addTest("udp6://invalid.invalid", false, ""); |
| 213 | addTest("udp://invalid.invalid", false, ""); |
| 214 | |
| 215 | // IPv6 multicast |
| 216 | addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363"); |
| 217 | addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363"); |
| 218 | |
| 219 | runTests(); |
| 220 | } |
| 221 | |
| 222 | BOOST_AUTO_TEST_CASE(ParseTcp) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 223 | { |
| 224 | FaceUri uri; |
| 225 | |
| 226 | BOOST_CHECK(uri.parse("tcp://random.host.name")); |
| 227 | BOOST_CHECK_EQUAL(uri.getScheme(), "tcp"); |
| 228 | BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name"); |
| 229 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 230 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 231 | |
| 232 | BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false); |
| 233 | BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false); |
| 234 | |
| 235 | using namespace boost::asio; |
| 236 | ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 237 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 238 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777"); |
| 239 | |
| 240 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient")); |
| 241 | BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777"); |
| 242 | |
| 243 | ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 244 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 245 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777"); |
| 246 | } |
| 247 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 248 | BOOST_FIXTURE_TEST_CASE(CanonizeTcp, CanonizeFixture) |
| 249 | { |
| 250 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true); |
| 251 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true); |
| 252 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true); |
| 253 | |
| 254 | BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true); |
| 255 | BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false); |
| 256 | BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false); |
| 257 | BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false); |
| 258 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true); |
| 259 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false); |
| 260 | BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false); |
| 261 | BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false); |
| 262 | BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false); |
| 263 | BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false); |
| 264 | |
| 265 | // IPv4 unicast |
| 266 | addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363"); |
| 267 | addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363"); |
| 268 | addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363"); |
| 269 | addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363"); |
| 270 | addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695"); |
| 271 | addTest("tcp4://192.0.2.666:6363", false, ""); |
| 272 | addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363"); |
| 273 | addTest("tcp4://invalid.invalid", false, ""); |
| 274 | |
| 275 | // IPv4 multicast |
| 276 | addTest("tcp4://224.0.23.170:56363", false, ""); |
| 277 | addTest("tcp4://224.0.23.170", false, ""); |
| 278 | addTest("tcp4://all-routers.mcast.net:56363", false, ""); |
| 279 | |
| 280 | // IPv6 unicast |
| 281 | addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363"); |
| 282 | addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363"); |
| 283 | addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363"); |
| 284 | addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363"); |
| 285 | addTest("tcp6://invalid.invalid", false, ""); |
| 286 | addTest("tcp://invalid.invalid", false, ""); |
| 287 | |
| 288 | // IPv6 multicast |
| 289 | addTest("tcp6://[ff02::2]:56363", false, ""); |
| 290 | addTest("tcp6://[ff02::2]", false, ""); |
| 291 | |
| 292 | runTests(); |
| 293 | } |
| 294 | |
| 295 | BOOST_AUTO_TEST_CASE(ParseUnix) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 296 | { |
| 297 | FaceUri uri; |
| 298 | |
| 299 | BOOST_CHECK(uri.parse("unix:///var/run/example.sock")); |
| 300 | BOOST_CHECK_EQUAL(uri.getScheme(), "unix"); |
| 301 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 302 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 303 | BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock"); |
| 304 | |
| 305 | //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); |
| 306 | // This is not a valid unix:/ URI, but the parse would treat "var" as host |
| 307 | |
| 308 | #ifdef HAVE_UNIX_SOCKETS |
| 309 | using namespace boost::asio; |
| 310 | local::stream_protocol::endpoint endpoint("/var/run/example.sock"); |
| 311 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint)); |
| 312 | BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock"); |
| 313 | #endif // HAVE_UNIX_SOCKETS |
| 314 | } |
| 315 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 316 | BOOST_AUTO_TEST_CASE(ParseFd) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 317 | { |
| 318 | FaceUri uri; |
| 319 | |
| 320 | BOOST_CHECK(uri.parse("fd://6")); |
| 321 | BOOST_CHECK_EQUAL(uri.getScheme(), "fd"); |
| 322 | BOOST_CHECK_EQUAL(uri.getHost(), "6"); |
| 323 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 324 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 325 | |
| 326 | int fd = 21; |
| 327 | BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd)); |
| 328 | BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21"); |
| 329 | } |
| 330 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 331 | BOOST_AUTO_TEST_CASE(ParseEther) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 332 | { |
| 333 | FaceUri uri; |
| 334 | |
| 335 | BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]")); |
| 336 | BOOST_CHECK_EQUAL(uri.getScheme(), "ether"); |
| 337 | BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01"); |
| 338 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 339 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 340 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 341 | BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 342 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 343 | ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01"); |
| 344 | BOOST_REQUIRE_NO_THROW(FaceUri(address)); |
| 345 | BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 348 | BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture) |
| 349 | { |
| 350 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true); |
| 351 | |
| 352 | BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true); |
| 353 | BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false); |
| 354 | BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false); |
| 355 | BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true); |
| 356 | |
| 357 | addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]"); |
| 358 | addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]"); |
| 359 | addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]"); |
| 360 | addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]"); |
| 361 | |
| 362 | runTests(); |
| 363 | } |
| 364 | |
| 365 | BOOST_AUTO_TEST_CASE(ParseDev) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 366 | { |
| 367 | FaceUri uri; |
| 368 | |
| 369 | BOOST_CHECK(uri.parse("dev://eth0")); |
| 370 | BOOST_CHECK_EQUAL(uri.getScheme(), "dev"); |
| 371 | BOOST_CHECK_EQUAL(uri.getHost(), "eth0"); |
| 372 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 373 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 374 | |
| 375 | std::string ifname = "en1"; |
| 376 | BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname)); |
| 377 | BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1"); |
| 378 | } |
| 379 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 380 | BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback) |
| 381 | { |
| 382 | boost::asio::io_service io; |
| 383 | |
| 384 | // unsupported scheme |
| 385 | FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(), |
| 386 | FaceUri::CanonizeFailureCallback(), |
| 387 | io, time::milliseconds(1)); |
| 388 | |
| 389 | // cannot resolve |
| 390 | FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(), |
| 391 | FaceUri::CanonizeFailureCallback(), |
| 392 | io, time::milliseconds(1)); |
| 393 | |
| 394 | // already canonical |
| 395 | FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(), |
| 396 | FaceUri::CanonizeFailureCallback(), |
| 397 | io, time::milliseconds(1)); |
| 398 | |
| 399 | // need DNS resolution |
| 400 | FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(), |
| 401 | FaceUri::CanonizeFailureCallback(), |
| 402 | io, time::milliseconds(1)); |
| 403 | |
| 404 | io.run(); // should not crash |
| 405 | } |
| 406 | |
| 407 | BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture) |
| 408 | { |
| 409 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false); |
| 410 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false); |
| 411 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false); |
| 412 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false); |
| 413 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), false); |
| 414 | |
| 415 | BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false); |
| 416 | BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false); |
| 417 | BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false); |
| 418 | BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false); |
| 419 | BOOST_CHECK_EQUAL(FaceUri("dev://eth1").isCanonical(), false); |
| 420 | |
| 421 | addTest("internal://", false, ""); |
| 422 | addTest("null://", false, ""); |
| 423 | addTest("unix:///var/run/nfd.sock", false, ""); |
| 424 | addTest("fd://0", false, ""); |
| 425 | addTest("dev://eth1", false, ""); |
| 426 | |
| 427 | runTests(); |
| 428 | } |
| 429 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 430 | BOOST_AUTO_TEST_CASE(Bug1635) |
| 431 | { |
| 432 | FaceUri uri; |
| 433 | |
| 434 | BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366")); |
| 435 | BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient"); |
| 436 | BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239"); |
| 437 | BOOST_CHECK_EQUAL(uri.getPort(), "56366"); |
| 438 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 439 | BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366"); |
| 440 | } |
| 441 | |
| 442 | BOOST_AUTO_TEST_SUITE_END() |
| 443 | |
| 444 | } // namespace util |
| 445 | } // namespace ndn |