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