Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California, |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [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 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 28 | #include "net/face-uri.hpp" |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 29 | |
| 30 | #include "boost-test.hpp" |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 31 | #include "collect-netifs.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 32 | #include "network-configuration-detector.hpp" |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 35 | namespace tests { |
| 36 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Net) |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(TestFaceUri) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 40 | class CanonizeFixture : noncopyable |
| 41 | { |
| 42 | protected: |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 43 | void |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 44 | addTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri) |
| 45 | { |
| 46 | ++m_nPending; |
| 47 | auto tc = make_shared<CanonizeTestCase>(request, shouldSucceed, expectedUri); |
| 48 | |
| 49 | FaceUri uri(request); |
| 50 | uri.canonize(bind(&CanonizeFixture::onCanonizeSuccess, this, tc, _1), |
| 51 | bind(&CanonizeFixture::onCanonizeFailure, this, tc, _1), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 52 | m_io, 10_s); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 53 | } |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 54 | |
| 55 | void |
| 56 | runTests() |
| 57 | { |
| 58 | m_io.run(); |
| 59 | BOOST_CHECK_EQUAL(m_nPending, 0); |
| 60 | } |
| 61 | |
| 62 | private: |
Davide Pesavento | 50df562 | 2015-03-19 22:05:05 +0100 | [diff] [blame] | 63 | class CanonizeTestCase |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 64 | { |
| 65 | public: |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 66 | CanonizeTestCase(const std::string& request, bool shouldSucceed, const std::string& expectedUri) |
| 67 | : m_expectedUri(expectedUri) |
Davide Pesavento | 50df562 | 2015-03-19 22:05:05 +0100 | [diff] [blame] | 68 | , m_message(request + " should " + (shouldSucceed ? "succeed" : "fail")) |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 69 | , m_shouldSucceed(shouldSucceed) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 70 | , m_isCompleted(false) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | public: |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 75 | std::string m_expectedUri; |
Davide Pesavento | 50df562 | 2015-03-19 22:05:05 +0100 | [diff] [blame] | 76 | std::string m_message; |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 77 | bool m_shouldSucceed; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 78 | bool m_isCompleted; |
| 79 | }; |
| 80 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 81 | void |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 82 | onCanonizeSuccess(const shared_ptr<CanonizeTestCase>& tc, const FaceUri& canonicalUri) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 83 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(tc->m_isCompleted, false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 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); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 89 | if (tc->m_shouldSucceed) { |
| 90 | BOOST_CHECK_EQUAL(canonicalUri.toString(), tc->m_expectedUri); |
| 91 | } |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 94 | void |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 95 | onCanonizeFailure(const shared_ptr<CanonizeTestCase>& tc, const std::string& reason) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 96 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(tc->m_isCompleted, false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 98 | tc->m_isCompleted = true; |
| 99 | --m_nPending; |
| 100 | |
Davide Pesavento | 50df562 | 2015-03-19 22:05:05 +0100 | [diff] [blame] | 101 | BOOST_CHECK_MESSAGE(!tc->m_shouldSucceed, tc->m_message); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private: |
| 105 | boost::asio::io_service m_io; |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 106 | ssize_t m_nPending = 0; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(ParseInternal) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 110 | { |
| 111 | FaceUri uri; |
| 112 | |
| 113 | BOOST_CHECK(uri.parse("internal://")); |
| 114 | BOOST_CHECK_EQUAL(uri.getScheme(), "internal"); |
| 115 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 116 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 117 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 118 | |
| 119 | BOOST_CHECK_EQUAL(uri.parse("internal:"), false); |
| 120 | BOOST_CHECK_EQUAL(uri.parse("internal:/"), false); |
| 121 | } |
| 122 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(ParseUdp) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 124 | { |
| 125 | BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363")); |
| 126 | BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error); |
| 127 | BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error); |
| 128 | |
| 129 | FaceUri uri; |
| 130 | BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false); |
| 131 | |
| 132 | BOOST_CHECK(uri.parse("udp://hostname:80")); |
| 133 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp"); |
| 134 | BOOST_CHECK_EQUAL(uri.getHost(), "hostname"); |
| 135 | BOOST_CHECK_EQUAL(uri.getPort(), "80"); |
| 136 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 137 | |
| 138 | BOOST_CHECK(uri.parse("udp4://192.0.2.1:20")); |
| 139 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp4"); |
| 140 | BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1"); |
| 141 | BOOST_CHECK_EQUAL(uri.getPort(), "20"); |
| 142 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 143 | |
| 144 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363")); |
| 145 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
| 146 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1"); |
| 147 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
| 148 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 149 | |
| 150 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363")); |
| 151 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
| 152 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3"); |
| 153 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
| 154 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 155 | |
| 156 | BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false); |
| 157 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 158 | namespace ip = boost::asio::ip; |
| 159 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 160 | ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 161 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 162 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777"); |
| 163 | |
| 164 | ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 165 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 166 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777"); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 167 | |
| 168 | BOOST_CHECK(uri.parse("udp6://[fe80::1%25eth1]:6363")); |
| 169 | BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1"); |
| 170 | |
| 171 | BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]:6363")); |
| 172 | BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1"); |
| 173 | |
| 174 | BOOST_CHECK(uri.parse("udp6://[fe80::1%1]:6363")); |
| 175 | BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]")); |
Davide Pesavento | b984e32 | 2018-01-24 19:40:07 -0500 | [diff] [blame^] | 176 | |
| 177 | BOOST_CHECK(uri.parse("udp6://[ff01::114%eth#1]")); |
| 178 | BOOST_CHECK(uri.parse("udp6://[ff01::114%eth.1,2]")); |
| 179 | BOOST_CHECK(uri.parse("udp6://[ff01::114%a+b-c=0]")); |
| 180 | BOOST_CHECK(uri.parse("udp6://[ff01::114%[foo]]")); |
| 181 | BOOST_CHECK(uri.parse("udp6://[ff01::114%]]")); |
| 182 | BOOST_CHECK(uri.parse("udp6://[ff01::114%%]")); |
| 183 | BOOST_CHECK(!uri.parse("udp6://[ff01::114%]")); |
| 184 | BOOST_CHECK(!uri.parse("udp6://[ff01::114%foo bar]")); |
| 185 | BOOST_CHECK(!uri.parse("udp6://[ff01::114%foo/bar]")); |
| 186 | BOOST_CHECK(!uri.parse("udp6://[ff01::114%eth0:1]")); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 189 | BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 190 | { |
| 191 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true); |
| 192 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true); |
| 193 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true); |
| 194 | |
| 195 | BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true); |
| 196 | BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false); |
| 197 | BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false); |
| 198 | BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false); |
| 199 | BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true); |
| 200 | BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(FaceUri("udp://[2001:db8::1]:6363").isCanonical(), false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 202 | BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false); |
| 203 | BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false); |
| 204 | BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false); |
| 205 | BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 206 | BOOST_CHECK_EQUAL(FaceUri("udp4://[2001:db8::1]:6363").isCanonical(), false); |
| 207 | BOOST_CHECK_EQUAL(FaceUri("udp6://192.0.2.1:6363").isCanonical(), false); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 208 | |
| 209 | const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces(); |
| 210 | if (!networkInterfaces.empty()) { |
| 211 | const auto& netif = networkInterfaces.front(); |
| 212 | auto name = netif->getName(); |
| 213 | auto index = to_string(netif->getIndex()); |
| 214 | |
| 215 | BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]:6363").isCanonical(), true); |
| 216 | BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + index + "]:6363").isCanonical(), false); |
| 217 | BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]").isCanonical(), false); |
| 218 | BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false); |
| 219 | } |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 222 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1) |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 223 | BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture) |
| 224 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 225 | SKIP_IF_IPV4_UNAVAILABLE(); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 226 | |
| 227 | // IPv4 unicast |
| 228 | addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363"); |
| 229 | addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363"); |
| 230 | addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363"); |
| 231 | addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363"); |
| 232 | addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695"); |
| 233 | addTest("udp4://192.0.2.666:6363", false, ""); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 234 | addTest("udp4://192.0.2.7:99999", false, ""); // Bug #3897 |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 235 | 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] | 236 | addTest("udp4://google-public-dns-a.google.com:70000", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 237 | addTest("udp4://invalid.invalid", false, ""); |
| 238 | |
| 239 | // IPv4 multicast |
| 240 | addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363"); |
| 241 | addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363"); |
| 242 | addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363"); |
| 243 | |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 244 | // IPv6 used with udp4 protocol - not canonical |
| 245 | addTest("udp4://[2001:db8::1]:6363", false, ""); |
| 246 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 247 | runTests(); |
| 248 | } |
| 249 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 250 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1) |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 251 | BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture) |
| 252 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 253 | SKIP_IF_IPV6_UNAVAILABLE(); |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 254 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 255 | // IPv6 unicast |
| 256 | addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 257 | addTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363"); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 258 | addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363"); |
| 259 | addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 260 | addTest("udp6://[2001::db8::1]:6363", false, ""); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 261 | addTest("udp6://[2001:db8::1]:99999", false, ""); // Bug #3897 |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 262 | 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] | 263 | addTest("udp6://google-public-dns-a.google.com:70000", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 264 | addTest("udp6://invalid.invalid", false, ""); |
| 265 | addTest("udp://invalid.invalid", false, ""); |
| 266 | |
| 267 | // IPv6 multicast |
| 268 | addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363"); |
| 269 | addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363"); |
| 270 | |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 271 | // IPv4 used with udp6 protocol - not canonical |
| 272 | addTest("udp6://192.0.2.1:6363", false, ""); |
| 273 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 274 | const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces(); |
| 275 | if (!networkInterfaces.empty()) { |
| 276 | const auto& netif = networkInterfaces.front(); |
| 277 | auto name = netif->getName(); |
| 278 | auto index = to_string(netif->getIndex()); |
| 279 | |
| 280 | addTest("udp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true, |
| 281 | "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363"); |
| 282 | |
| 283 | addTest("udp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true, |
| 284 | "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363"); |
| 285 | } |
| 286 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 287 | runTests(); |
| 288 | } |
| 289 | |
| 290 | BOOST_AUTO_TEST_CASE(ParseTcp) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 291 | { |
| 292 | FaceUri uri; |
| 293 | |
| 294 | BOOST_CHECK(uri.parse("tcp://random.host.name")); |
| 295 | BOOST_CHECK_EQUAL(uri.getScheme(), "tcp"); |
| 296 | BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name"); |
| 297 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 298 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 299 | |
| 300 | BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false); |
| 301 | BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false); |
| 302 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 303 | namespace ip = boost::asio::ip; |
| 304 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 305 | ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 306 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 307 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777"); |
| 308 | |
| 309 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient")); |
| 310 | BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777"); |
| 311 | |
| 312 | ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 313 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 314 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777"); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 315 | |
| 316 | BOOST_CHECK(uri.parse("tcp6://[fe80::1%25eth1]:6363")); |
| 317 | BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1"); |
| 318 | |
| 319 | BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]:6363")); |
| 320 | BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1"); |
| 321 | |
| 322 | BOOST_CHECK(uri.parse("tcp6://[fe80::1%1]:6363")); |
| 323 | BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]")); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 326 | BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 327 | { |
| 328 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true); |
| 329 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true); |
| 330 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true); |
| 331 | |
| 332 | BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true); |
| 333 | BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false); |
| 334 | BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false); |
| 335 | BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false); |
| 336 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true); |
| 337 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 338 | BOOST_CHECK_EQUAL(FaceUri("tcp://[2001:db8::1]:6363").isCanonical(), false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 339 | BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false); |
| 340 | BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false); |
| 341 | BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false); |
| 342 | BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 343 | BOOST_CHECK_EQUAL(FaceUri("tcp4://[2001:db8::1]:6363").isCanonical(), false); |
| 344 | BOOST_CHECK_EQUAL(FaceUri("tcp6://192.0.2.1:6363").isCanonical(), false); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 345 | |
| 346 | const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces(); |
| 347 | if (!networkInterfaces.empty()) { |
| 348 | const auto& netif = networkInterfaces.front(); |
| 349 | auto name = netif->getName(); |
| 350 | auto index = to_string(netif->getIndex()); |
| 351 | |
| 352 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]:6363").isCanonical(), true); |
| 353 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + index + "]:6363").isCanonical(), false); |
| 354 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]").isCanonical(), false); |
| 355 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false); |
| 356 | } |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 359 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1) |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 360 | BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture) |
| 361 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 362 | SKIP_IF_IPV4_UNAVAILABLE(); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 363 | |
| 364 | // IPv4 unicast |
| 365 | addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363"); |
| 366 | addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363"); |
| 367 | addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363"); |
| 368 | addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363"); |
| 369 | addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695"); |
| 370 | addTest("tcp4://192.0.2.666:6363", false, ""); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 371 | addTest("tcp4://192.0.2.7:99999", false, ""); // Bug #3897 |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 372 | 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] | 373 | addTest("tcp4://google-public-dns-a.google.com:70000", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 374 | addTest("tcp4://invalid.invalid", false, ""); |
| 375 | |
| 376 | // IPv4 multicast |
| 377 | addTest("tcp4://224.0.23.170:56363", false, ""); |
| 378 | addTest("tcp4://224.0.23.170", false, ""); |
| 379 | addTest("tcp4://all-routers.mcast.net:56363", false, ""); |
| 380 | |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 381 | // IPv6 used with tcp4 protocol - not canonical |
| 382 | addTest("tcp4://[2001:db8::1]:6363", false, ""); |
| 383 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 384 | const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces(); |
| 385 | if (!networkInterfaces.empty()) { |
| 386 | const auto& netif = networkInterfaces.front(); |
| 387 | auto name = netif->getName(); |
| 388 | auto index = to_string(netif->getIndex()); |
| 389 | |
| 390 | addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true, |
| 391 | "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363"); |
| 392 | |
| 393 | addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true, |
| 394 | "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363"); |
| 395 | } |
| 396 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 397 | runTests(); |
| 398 | } |
| 399 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 400 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1) |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 401 | BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture) |
| 402 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 403 | SKIP_IF_IPV6_UNAVAILABLE(); |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 404 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 405 | // IPv6 unicast |
| 406 | addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 407 | addTest("tcp6://[2001:db8::1]", true, "tcp6://[2001:db8::1]:6363"); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 408 | addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363"); |
| 409 | addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 410 | addTest("tcp6://[2001::db8::1]:6363", false, ""); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 411 | addTest("tcp6://[2001:db8::1]:99999", false, ""); // Bug #3897 |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 412 | 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] | 413 | addTest("tcp6://google-public-dns-a.google.com:70000", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 414 | addTest("tcp6://invalid.invalid", false, ""); |
| 415 | addTest("tcp://invalid.invalid", false, ""); |
| 416 | |
| 417 | // IPv6 multicast |
| 418 | addTest("tcp6://[ff02::2]:56363", false, ""); |
| 419 | addTest("tcp6://[ff02::2]", false, ""); |
| 420 | |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 421 | // IPv4 used with tcp6 protocol - not canonical |
| 422 | addTest("tcp6://192.0.2.1:6363", false, ""); |
| 423 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 424 | runTests(); |
| 425 | } |
| 426 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 427 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 428 | BOOST_AUTO_TEST_CASE(ParseUnix) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 429 | { |
| 430 | FaceUri uri; |
| 431 | |
| 432 | BOOST_CHECK(uri.parse("unix:///var/run/example.sock")); |
| 433 | BOOST_CHECK_EQUAL(uri.getScheme(), "unix"); |
| 434 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 435 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 436 | BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock"); |
| 437 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 438 | // This is not a valid unix:// URI, but the parse would treat "var" as host |
| 439 | 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] | 440 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 441 | #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS |
| 442 | using boost::asio::local::stream_protocol; |
| 443 | stream_protocol::endpoint endpoint("/var/run/example.sock"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 444 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint)); |
| 445 | BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock"); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 446 | #endif // BOOST_ASIO_HAS_LOCAL_SOCKETS |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 449 | BOOST_AUTO_TEST_CASE(ParseFd) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 450 | { |
| 451 | FaceUri uri; |
| 452 | |
| 453 | BOOST_CHECK(uri.parse("fd://6")); |
| 454 | BOOST_CHECK_EQUAL(uri.getScheme(), "fd"); |
| 455 | BOOST_CHECK_EQUAL(uri.getHost(), "6"); |
| 456 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 457 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 458 | |
| 459 | int fd = 21; |
| 460 | BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd)); |
| 461 | BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21"); |
| 462 | } |
| 463 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 464 | BOOST_AUTO_TEST_CASE(ParseEther) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 465 | { |
| 466 | FaceUri uri; |
| 467 | |
| 468 | BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]")); |
| 469 | BOOST_CHECK_EQUAL(uri.getScheme(), "ether"); |
| 470 | BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01"); |
| 471 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 472 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 473 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 474 | 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] | 475 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 476 | auto address = ethernet::Address::fromString("33:33:01:01:01:01"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 477 | BOOST_REQUIRE_NO_THROW(FaceUri(address)); |
| 478 | 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] | 479 | } |
| 480 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 481 | BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture) |
| 482 | { |
| 483 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true); |
| 484 | |
| 485 | BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true); |
| 486 | BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false); |
| 487 | BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false); |
| 488 | BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true); |
| 489 | |
| 490 | addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]"); |
| 491 | addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]"); |
| 492 | addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]"); |
| 493 | addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]"); |
| 494 | |
| 495 | runTests(); |
| 496 | } |
| 497 | |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 498 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 499 | BOOST_AUTO_TEST_CASE(ParseDev) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 500 | { |
| 501 | FaceUri uri; |
| 502 | |
| 503 | BOOST_CHECK(uri.parse("dev://eth0")); |
| 504 | BOOST_CHECK_EQUAL(uri.getScheme(), "dev"); |
| 505 | BOOST_CHECK_EQUAL(uri.getHost(), "eth0"); |
| 506 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 507 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 508 | |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 509 | BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896 |
| 510 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 511 | std::string ifname = "en1"; |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 512 | BOOST_REQUIRE_NO_THROW(uri = FaceUri::fromDev(ifname)); |
| 513 | BOOST_CHECK_EQUAL(uri.toString(), "dev://en1"); |
| 514 | } |
| 515 | |
| 516 | BOOST_AUTO_TEST_CASE(IsCanonicalDev) |
| 517 | { |
| 518 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), true); |
| 519 | |
| 520 | BOOST_CHECK_EQUAL(FaceUri("dev://eth0").isCanonical(), true); |
| 521 | BOOST_CHECK_EQUAL(FaceUri("dev://").isCanonical(), false); |
| 522 | BOOST_CHECK_EQUAL(FaceUri("dev://eth0:8888").isCanonical(), false); |
| 523 | BOOST_CHECK_EQUAL(FaceUri("dev://eth0/").isCanonical(), false); |
| 524 | BOOST_CHECK_EQUAL(FaceUri("dev://eth0/A").isCanonical(), false); |
| 525 | } |
| 526 | |
| 527 | BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture) |
| 528 | { |
| 529 | addTest("dev://eth0", true, "dev://eth0"); |
| 530 | addTest("dev://", false, ""); |
| 531 | addTest("dev://eth0:8888", false, ""); |
| 532 | addTest("dev://eth0/", true, "dev://eth0"); |
| 533 | addTest("dev://eth0/A", false, ""); |
| 534 | |
| 535 | runTests(); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 538 | BOOST_AUTO_TEST_CASE(ParseUdpDev) |
| 539 | { |
| 540 | FaceUri uri; |
| 541 | |
| 542 | BOOST_CHECK(uri.parse("udp4+dev://eth0:7777")); |
| 543 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev"); |
| 544 | BOOST_CHECK_EQUAL(uri.getHost(), "eth0"); |
| 545 | BOOST_CHECK_EQUAL(uri.getPort(), "7777"); |
| 546 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 547 | |
| 548 | BOOST_CHECK(uri.parse("udp6+dev://eth1:7777")); |
| 549 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev"); |
| 550 | BOOST_CHECK_EQUAL(uri.getHost(), "eth1"); |
| 551 | BOOST_CHECK_EQUAL(uri.getPort(), "7777"); |
| 552 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 553 | |
| 554 | BOOST_CHECK(uri.parse("abc+efg://eth0")); |
| 555 | BOOST_CHECK(!uri.parse("abc+://eth0")); |
| 556 | BOOST_CHECK(!uri.parse("+abc://eth0")); |
| 557 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 558 | namespace ip = boost::asio::ip; |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 559 | |
| 560 | ip::udp::endpoint endpoint4(ip::udp::v4(), 7777); |
| 561 | BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1")); |
| 562 | BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777"); |
| 563 | |
| 564 | ip::udp::endpoint endpoint6(ip::udp::v6(), 7777); |
| 565 | BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2")); |
| 566 | BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777"); |
| 567 | } |
| 568 | |
| 569 | BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture) |
| 570 | { |
| 571 | BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true); |
| 572 | BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true); |
| 573 | BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false); |
| 574 | BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false); |
| 575 | |
| 576 | addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777"); |
| 577 | addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777"); |
| 578 | addTest("udp+dev://en1:7777", false, ""); |
| 579 | addTest("udp6+dev://en2", false, ""); |
| 580 | } |
| 581 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 582 | BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback) |
| 583 | { |
| 584 | boost::asio::io_service io; |
| 585 | |
| 586 | // unsupported scheme |
| 587 | FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(), |
| 588 | FaceUri::CanonizeFailureCallback(), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 589 | io, 1_ms); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 590 | |
| 591 | // cannot resolve |
| 592 | FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(), |
| 593 | FaceUri::CanonizeFailureCallback(), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 594 | io, 1_ms); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 595 | |
| 596 | // already canonical |
| 597 | FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(), |
| 598 | FaceUri::CanonizeFailureCallback(), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 599 | io, 1_ms); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 600 | |
| 601 | // need DNS resolution |
| 602 | FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(), |
| 603 | FaceUri::CanonizeFailureCallback(), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 604 | io, 1_ms); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 605 | |
| 606 | io.run(); // should not crash |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 607 | |
| 608 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 609 | BOOST_CHECK(true); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture) |
| 613 | { |
| 614 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false); |
| 615 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false); |
| 616 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false); |
| 617 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 618 | |
| 619 | BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false); |
| 620 | BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false); |
| 621 | BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false); |
| 622 | BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 623 | |
| 624 | addTest("internal://", false, ""); |
| 625 | addTest("null://", false, ""); |
| 626 | addTest("unix:///var/run/nfd.sock", false, ""); |
| 627 | addTest("fd://0", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 628 | |
| 629 | runTests(); |
| 630 | } |
| 631 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 632 | BOOST_AUTO_TEST_CASE(Bug1635) |
| 633 | { |
| 634 | FaceUri uri; |
| 635 | |
| 636 | BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366")); |
| 637 | BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient"); |
| 638 | BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239"); |
| 639 | BOOST_CHECK_EQUAL(uri.getPort(), "56366"); |
| 640 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 641 | BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366"); |
| 642 | } |
| 643 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 644 | BOOST_AUTO_TEST_SUITE_END() // TestFaceUri |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 645 | BOOST_AUTO_TEST_SUITE_END() // Net |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 646 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 647 | } // namespace tests |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 648 | } // namespace ndn |