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