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