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