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