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