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