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