blob: 34e050ed68b342fa92fed629043799817b6ce9b7 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yanbiao Liac8b4ca2017-07-10 02:27:50 -07002/*
Davide Pesavento152ef442023-04-22 02:02:29 -04003 * Copyright (c) 2013-2023 Regents of the University of California,
Junxiao Shid5c2f0c2017-04-04 09:52:11 +00004 * 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 Shi77dcadd2014-10-05 14:40:54 -070010 *
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 Pesavento7e780642018-11-24 15:51:34 -050028#include "ndn-cxx/net/face-uri.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050029#include "ndn-cxx/net/network-interface.hpp"
30#include "ndn-cxx/net/network-monitor.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070031
Davide Pesavento7e780642018-11-24 15:51:34 -050032#include "tests/boost-test.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050033#include "tests/unit/net/network-configuration-detector.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070034
Davide Pesavento152ef442023-04-22 02:02:29 -040035#include <boost/concept_check.hpp>
36
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040037namespace ndn::tests {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080038
Davide Pesavento152ef442023-04-22 02:02:29 -040039BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceUri>));
40
Junxiao Shi25467942017-06-30 02:53:14 +000041BOOST_AUTO_TEST_SUITE(Net)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010042BOOST_AUTO_TEST_SUITE(TestFaceUri)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070043
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050044class CanonizeFixture
Junxiao Shi4083c8d2014-10-12 16:43:16 -070045{
46protected:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050047 CanonizeFixture()
48 {
49 static const auto netifs = [this] {
50 net::NetworkMonitor netmon(m_io);
51 if (netmon.getCapabilities() & net::NetworkMonitor::CAP_ENUM) {
52 netmon.onEnumerationCompleted.connect([this] { m_io.stop(); });
53 m_io.run();
54#if BOOST_VERSION >= 106600
55 m_io.restart();
56#else
57 m_io.reset();
58#endif
59 }
60 return netmon.listNetworkInterfaces();
61 }();
62
63 if (!netifs.empty()) {
64 m_netif = netifs.front();
65 }
66 }
67
Junxiao Shi4083c8d2014-10-12 16:43:16 -070068 void
Davide Pesavento2d7c5852022-07-18 16:02:52 -040069 runTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri = "")
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050070 {
Davide Pesavento2d7c5852022-07-18 16:02:52 -040071 BOOST_TEST_CONTEXT(std::quoted(request) << " should " << (shouldSucceed ? "succeed" : "fail")) {
72 bool didInvokeCb = false;
73 FaceUri uri(request);
74 uri.canonize(
75 [&] (const FaceUri& canonicalUri) {
76 BOOST_CHECK_EQUAL(didInvokeCb, false);
77 didInvokeCb = true;
78 BOOST_CHECK_EQUAL(shouldSucceed, true);
79 if (shouldSucceed) {
80 BOOST_CHECK_EQUAL(canonicalUri.toString(), expectedUri);
81 }
82 },
83 [&] (const std::string&) {
84 BOOST_CHECK_EQUAL(didInvokeCb, false);
85 didInvokeCb = true;
86 BOOST_CHECK_EQUAL(shouldSucceed, false);
87 },
88 m_io, 30_s);
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050089
Davide Pesavento2d7c5852022-07-18 16:02:52 -040090 m_io.run();
91 BOOST_CHECK_EQUAL(didInvokeCb, true);
92#if BOOST_VERSION >= 106600
93 m_io.restart();
94#else
95 m_io.reset();
96#endif
Junxiao Shi4083c8d2014-10-12 16:43:16 -070097 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -070098 }
99
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500100protected:
101 shared_ptr<const net::NetworkInterface> m_netif;
102
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700103private:
104 boost::asio::io_service m_io;
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700105};
106
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700107BOOST_AUTO_TEST_CASE(ParseInternal)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700108{
109 FaceUri uri;
110
111 BOOST_CHECK(uri.parse("internal://"));
112 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
113 BOOST_CHECK_EQUAL(uri.getHost(), "");
114 BOOST_CHECK_EQUAL(uri.getPort(), "");
115 BOOST_CHECK_EQUAL(uri.getPath(), "");
116
117 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
118 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
119}
120
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700121BOOST_AUTO_TEST_CASE(ParseUdp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700122{
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500123 FaceUri uri("udp://hostname:6363");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700124 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
125 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
126
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700127 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
128
129 BOOST_CHECK(uri.parse("udp://hostname:80"));
130 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
131 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
132 BOOST_CHECK_EQUAL(uri.getPort(), "80");
133 BOOST_CHECK_EQUAL(uri.getPath(), "");
134
135 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
136 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
137 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
138 BOOST_CHECK_EQUAL(uri.getPort(), "20");
139 BOOST_CHECK_EQUAL(uri.getPath(), "");
140
141 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
142 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
143 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
144 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
145 BOOST_CHECK_EQUAL(uri.getPath(), "");
146
147 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
148 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
149 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
150 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
151 BOOST_CHECK_EQUAL(uri.getPath(), "");
152
153 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
154
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500155 namespace ip = boost::asio::ip;
156
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700157 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500158 uri = FaceUri(endpoint4);
159 BOOST_CHECK_EQUAL(uri.toString(), "udp4://192.0.2.1:7777");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700160
161 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500162 uri = FaceUri(endpoint6);
163 BOOST_CHECK_EQUAL(uri.toString(), "udp6://[2001:db8::1]:7777");
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700164
165 BOOST_CHECK(uri.parse("udp6://[fe80::1%25eth1]:6363"));
166 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1");
167
168 BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]:6363"));
169 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1");
170
171 BOOST_CHECK(uri.parse("udp6://[fe80::1%1]:6363"));
172 BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]"));
Davide Pesaventob984e322018-01-24 19:40:07 -0500173
174 BOOST_CHECK(uri.parse("udp6://[ff01::114%eth#1]"));
175 BOOST_CHECK(uri.parse("udp6://[ff01::114%eth.1,2]"));
176 BOOST_CHECK(uri.parse("udp6://[ff01::114%a+b-c=0]"));
177 BOOST_CHECK(uri.parse("udp6://[ff01::114%[foo]]"));
178 BOOST_CHECK(uri.parse("udp6://[ff01::114%]]"));
179 BOOST_CHECK(uri.parse("udp6://[ff01::114%%]"));
180 BOOST_CHECK(!uri.parse("udp6://[ff01::114%]"));
181 BOOST_CHECK(!uri.parse("udp6://[ff01::114%foo bar]"));
182 BOOST_CHECK(!uri.parse("udp6://[ff01::114%foo/bar]"));
183 BOOST_CHECK(!uri.parse("udp6://[ff01::114%eth0:1]"));
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700184}
185
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500186BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700187{
188 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true);
189 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true);
190 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true);
191
192 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true);
193 BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false);
194 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false);
195 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false);
196 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true);
197 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700198 BOOST_CHECK_EQUAL(FaceUri("udp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700199 BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false);
200 BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false);
201 BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false);
202 BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
Eric Newberry71aecae2017-05-29 00:22:39 -0700203 BOOST_CHECK_EQUAL(FaceUri("udp4://[2001:db8::1]:6363").isCanonical(), false);
204 BOOST_CHECK_EQUAL(FaceUri("udp6://192.0.2.1:6363").isCanonical(), false);
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700205
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500206 if (m_netif) {
207 auto name = m_netif->getName();
208 auto index = to_string(m_netif->getIndex());
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700209
210 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
211 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
212 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]").isCanonical(), false);
213 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
214 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800215}
216
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500217BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800218BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
219{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100220 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700221
222 // IPv4 unicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400223 runTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363");
224 runTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363");
225 runTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363");
226 runTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363");
227 runTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695");
228 runTest("udp4://192.0.2.666:6363", false);
229 runTest("udp4://192.0.2.7:99999", false); // Bug #3897
230 runTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363");
231 runTest("udp4://google-public-dns-a.google.com:70000", false);
232 runTest("udp4://invalid.invalid.", false);
233 runTest("udp://invalid.invalid.", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700234
235 // IPv4 multicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400236 runTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
237 runTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
238 runTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
239 runTest("udp://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700240
Eric Newberry71aecae2017-05-29 00:22:39 -0700241 // IPv6 used with udp4 protocol - not canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400242 runTest("udp4://[2001:db8::1]:6363", false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800243}
244
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500245BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800246BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
247{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100248 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800249
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700250 // IPv6 unicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400251 runTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
252 runTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363");
253 runTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
254 runTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
255 runTest("udp6://[2001::db8::1]:6363", false);
256 runTest("udp6://[2001:db8::1]:99999", false); // Bug #3897
257 runTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
258 runTest("udp6://google-public-dns-a.google.com:70000", false);
259 runTest("udp6://invalid.invalid.", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700260
261 // IPv6 multicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400262 runTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
263 runTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700264
Eric Newberry71aecae2017-05-29 00:22:39 -0700265 // IPv4 used with udp6 protocol - not canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400266 runTest("udp6://192.0.2.1:6363", false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700267
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500268 if (m_netif) {
269 auto name = m_netif->getName();
270 auto index = to_string(m_netif->getIndex());
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700271
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400272 runTest("udp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700273 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
274
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400275 runTest("udp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700276 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
277 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700278}
279
280BOOST_AUTO_TEST_CASE(ParseTcp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700281{
282 FaceUri uri;
283
284 BOOST_CHECK(uri.parse("tcp://random.host.name"));
285 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
286 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
287 BOOST_CHECK_EQUAL(uri.getPort(), "");
288 BOOST_CHECK_EQUAL(uri.getPath(), "");
289
290 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
291 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
292
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500293 namespace ip = boost::asio::ip;
294
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700295 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500296 uri = FaceUri(endpoint4);
297 BOOST_CHECK_EQUAL(uri.toString(), "tcp4://192.0.2.1:7777");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700298
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500299 uri = FaceUri(endpoint4, "wsclient");
300 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://192.0.2.1:7777");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700301
302 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500303 uri = FaceUri(endpoint6);
304 BOOST_CHECK_EQUAL(uri.toString(), "tcp6://[2001:db8::1]:7777");
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700305
306 BOOST_CHECK(uri.parse("tcp6://[fe80::1%25eth1]:6363"));
307 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1");
308
309 BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]:6363"));
310 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1");
311
312 BOOST_CHECK(uri.parse("tcp6://[fe80::1%1]:6363"));
313 BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]"));
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700314}
315
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500316BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700317{
318 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
319 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
320 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
321
322 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
323 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
324 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
325 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
326 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
327 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700328 BOOST_CHECK_EQUAL(FaceUri("tcp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700329 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
330 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
331 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
332 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700333 BOOST_CHECK_EQUAL(FaceUri("tcp4://[2001:db8::1]:6363").isCanonical(), false);
334 BOOST_CHECK_EQUAL(FaceUri("tcp6://192.0.2.1:6363").isCanonical(), false);
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700335
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500336 if (m_netif) {
337 auto name = m_netif->getName();
338 auto index = to_string(m_netif->getIndex());
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700339
340 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
341 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
342 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]").isCanonical(), false);
343 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
344 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800345}
346
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500347BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800348BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
349{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100350 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700351
352 // IPv4 unicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400353 runTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
354 runTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
355 runTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
356 runTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
357 runTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
358 runTest("tcp4://192.0.2.666:6363", false);
359 runTest("tcp4://192.0.2.7:99999", false); // Bug #3897
360 runTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
361 runTest("tcp4://google-public-dns-a.google.com:70000", false);
362 runTest("tcp4://invalid.invalid.", false);
363 runTest("tcp://invalid.invalid.", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700364
365 // IPv4 multicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400366 runTest("tcp4://224.0.23.170:56363", false);
367 runTest("tcp4://224.0.23.170", false);
368 runTest("tcp4://all-routers.mcast.net:56363", false);
369 runTest("tcp://all-routers.mcast.net:56363", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700370
Eric Newberry71aecae2017-05-29 00:22:39 -0700371 // IPv6 used with tcp4 protocol - not canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400372 runTest("tcp4://[2001:db8::1]:6363", false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700373
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500374 if (m_netif) {
375 auto name = m_netif->getName();
376 auto index = to_string(m_netif->getIndex());
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700377
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400378 runTest("tcp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700379 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
380
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400381 runTest("tcp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700382 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
383 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800384}
385
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500386BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800387BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
388{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100389 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800390
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700391 // IPv6 unicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400392 runTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
393 runTest("tcp6://[2001:db8::1]", true, "tcp6://[2001:db8::1]:6363");
394 runTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
395 runTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363");
396 runTest("tcp6://[2001::db8::1]:6363", false);
397 runTest("tcp6://[2001:db8::1]:99999", false); // Bug #3897
398 runTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
399 runTest("tcp6://google-public-dns-a.google.com:70000", false);
400 runTest("tcp6://invalid.invalid.", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700401
402 // IPv6 multicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400403 runTest("tcp6://[ff02::2]:56363", false);
404 runTest("tcp6://[ff02::2]", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700405
Eric Newberry71aecae2017-05-29 00:22:39 -0700406 // IPv4 used with tcp6 protocol - not canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400407 runTest("tcp6://192.0.2.1:6363", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700408}
409
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500410BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700411BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700412{
413 FaceUri uri;
414
415 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
416 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
417 BOOST_CHECK_EQUAL(uri.getHost(), "");
418 BOOST_CHECK_EQUAL(uri.getPort(), "");
419 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
420
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500421 // This is not a valid unix:// URI, but the parse would treat "var" as host
422 BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); // Bug #3896
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700423
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500424#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
425 using boost::asio::local::stream_protocol;
426 stream_protocol::endpoint endpoint("/var/run/example.sock");
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500427 uri = FaceUri(endpoint);
428 BOOST_CHECK_EQUAL(uri.toString(), "unix:///var/run/example.sock");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500429#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700430}
431
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700432BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700433{
434 FaceUri uri;
435
436 BOOST_CHECK(uri.parse("fd://6"));
437 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
438 BOOST_CHECK_EQUAL(uri.getHost(), "6");
439 BOOST_CHECK_EQUAL(uri.getPort(), "");
440 BOOST_CHECK_EQUAL(uri.getPath(), "");
441
442 int fd = 21;
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500443 uri = FaceUri::fromFd(fd);
444 BOOST_CHECK_EQUAL(uri.toString(), "fd://21");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700445}
446
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700447BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700448{
449 FaceUri uri;
450
451 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
452 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
453 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
454 BOOST_CHECK_EQUAL(uri.getPort(), "");
455 BOOST_CHECK_EQUAL(uri.getPath(), "");
456
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700457 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700458
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500459 auto address = ethernet::Address::fromString("33:33:01:01:01:01");
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500460 uri = FaceUri(address);
461 BOOST_CHECK_EQUAL(uri.toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700462}
463
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700464BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
465{
466 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
467
468 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
469 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
470 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
471 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
472
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400473 runTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
474 runTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
475 runTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
476 runTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700477}
478
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000479BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700480BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700481{
482 FaceUri uri;
483
484 BOOST_CHECK(uri.parse("dev://eth0"));
485 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
486 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
487 BOOST_CHECK_EQUAL(uri.getPort(), "");
488 BOOST_CHECK_EQUAL(uri.getPath(), "");
489
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000490 BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896
491
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700492 std::string ifname = "en1";
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500493 uri = FaceUri::fromDev(ifname);
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000494 BOOST_CHECK_EQUAL(uri.toString(), "dev://en1");
495}
496
497BOOST_AUTO_TEST_CASE(IsCanonicalDev)
498{
499 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), true);
500
501 BOOST_CHECK_EQUAL(FaceUri("dev://eth0").isCanonical(), true);
502 BOOST_CHECK_EQUAL(FaceUri("dev://").isCanonical(), false);
503 BOOST_CHECK_EQUAL(FaceUri("dev://eth0:8888").isCanonical(), false);
504 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/").isCanonical(), false);
505 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/A").isCanonical(), false);
506}
507
508BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture)
509{
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400510 runTest("dev://eth0", true, "dev://eth0");
511 runTest("dev://", false);
512 runTest("dev://eth0:8888", false);
513 runTest("dev://eth0/", true, "dev://eth0");
514 runTest("dev://eth0/A", false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700515}
516
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700517BOOST_AUTO_TEST_CASE(ParseUdpDev)
518{
519 FaceUri uri;
520
521 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
522 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
523 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
524 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
525 BOOST_CHECK_EQUAL(uri.getPath(), "");
526
527 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
528 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
529 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
530 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
531 BOOST_CHECK_EQUAL(uri.getPath(), "");
532
533 BOOST_CHECK(uri.parse("abc+efg://eth0"));
534 BOOST_CHECK(!uri.parse("abc+://eth0"));
535 BOOST_CHECK(!uri.parse("+abc://eth0"));
536
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500537 namespace ip = boost::asio::ip;
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700538
539 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500540 uri = FaceUri::fromUdpDev(endpoint4, "en1");
541 BOOST_CHECK_EQUAL(uri.toString(), "udp4+dev://en1:7777");
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700542
543 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500544 uri = FaceUri::fromUdpDev(endpoint6, "en2");
545 BOOST_CHECK_EQUAL(uri.toString(), "udp6+dev://en2:7777");
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700546}
547
548BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
549{
550 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
551 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
552 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
553 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
554
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400555 runTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
556 runTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
557 runTest("udp+dev://en1:7777", false);
558 runTest("udp6+dev://en2", false);
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700559}
560
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700561BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
562{
563 boost::asio::io_service io;
564
565 // unsupported scheme
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400566 FaceUri("null://").canonize(nullptr, nullptr, io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700567
568 // cannot resolve
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400569 FaceUri("udp://192.0.2.333").canonize(nullptr, nullptr, io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700570
571 // already canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400572 FaceUri("udp4://192.0.2.1:6363").canonize(nullptr, nullptr, io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700573
574 // need DNS resolution
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400575 FaceUri("udp://192.0.2.1:6363").canonize(nullptr, nullptr, io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700576
577 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100578
579 // avoid "test case [...] did not check any assertions" message from Boost.Test
580 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700581}
582
583BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
584{
585 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
586 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
587 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
588 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700589
590 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
591 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
592 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
593 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700594
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400595 runTest("internal://", false);
596 runTest("null://", false);
597 runTest("unix:///var/run/nfd.sock", false);
598 runTest("fd://0", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700599}
600
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700601BOOST_AUTO_TEST_CASE(Bug1635)
602{
603 FaceUri uri;
604
605 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
606 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
607 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
608 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
609 BOOST_CHECK_EQUAL(uri.getPath(), "");
610 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
611}
612
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100613BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
Junxiao Shi25467942017-06-30 02:53:14 +0000614BOOST_AUTO_TEST_SUITE_END() // Net
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700615
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400616} // namespace ndn::tests