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