blob: 19cf24c74ec14af605c697ad4f4813965c78996c [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 Pesavento2d7c5852022-07-18 16:02:52 -04003 * Copyright (c) 2013-2022 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
35namespace ndn {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080036namespace tests {
37
Junxiao Shi25467942017-06-30 02:53:14 +000038BOOST_AUTO_TEST_SUITE(Net)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010039BOOST_AUTO_TEST_SUITE(TestFaceUri)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070040
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050041class CanonizeFixture
Junxiao Shi4083c8d2014-10-12 16:43:16 -070042{
43protected:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050044 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 Shi4083c8d2014-10-12 16:43:16 -070065 void
Davide Pesavento2d7c5852022-07-18 16:02:52 -040066 runTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri = "")
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050067 {
Davide Pesavento2d7c5852022-07-18 16:02:52 -040068 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 Pesaventoe1081cd2016-12-19 23:58:15 -050086
Davide Pesavento2d7c5852022-07-18 16:02:52 -040087 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 Shi4083c8d2014-10-12 16:43:16 -070094 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -070095 }
96
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050097protected:
98 shared_ptr<const net::NetworkInterface> m_netif;
99
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700100private:
101 boost::asio::io_service m_io;
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700102};
103
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700104BOOST_AUTO_TEST_CASE(ParseInternal)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700105{
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 Shi4083c8d2014-10-12 16:43:16 -0700118BOOST_AUTO_TEST_CASE(ParseUdp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700119{
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500120 FaceUri uri("udp://hostname:6363");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700121 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
122 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
123
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700124 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 Pesaventoe1081cd2016-12-19 23:58:15 -0500152 namespace ip = boost::asio::ip;
153
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700154 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500155 uri = FaceUri(endpoint4);
156 BOOST_CHECK_EQUAL(uri.toString(), "udp4://192.0.2.1:7777");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700157
158 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500159 uri = FaceUri(endpoint6);
160 BOOST_CHECK_EQUAL(uri.toString(), "udp6://[2001:db8::1]:7777");
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700161
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 Pesaventob984e322018-01-24 19:40:07 -0500170
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 Shi77dcadd2014-10-05 14:40:54 -0700181}
182
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500183BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700184{
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 Newberry71aecae2017-05-29 00:22:39 -0700195 BOOST_CHECK_EQUAL(FaceUri("udp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700196 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 Newberry71aecae2017-05-29 00:22:39 -0700200 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 Liac8b4ca2017-07-10 02:27:50 -0700202
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500203 if (m_netif) {
204 auto name = m_netif->getName();
205 auto index = to_string(m_netif->getIndex());
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700206
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 Afanasyev1286e022015-01-26 10:42:29 -0800212}
213
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500214BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800215BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
216{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100217 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700218
219 // IPv4 unicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400220 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 Shi4083c8d2014-10-12 16:43:16 -0700231
232 // IPv4 multicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400233 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 Shi4083c8d2014-10-12 16:43:16 -0700237
Eric Newberry71aecae2017-05-29 00:22:39 -0700238 // IPv6 used with udp4 protocol - not canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400239 runTest("udp4://[2001:db8::1]:6363", false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800240}
241
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500242BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800243BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
244{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100245 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800246
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700247 // IPv6 unicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400248 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 Shi4083c8d2014-10-12 16:43:16 -0700257
258 // IPv6 multicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400259 runTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
260 runTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700261
Eric Newberry71aecae2017-05-29 00:22:39 -0700262 // IPv4 used with udp6 protocol - not canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400263 runTest("udp6://192.0.2.1:6363", false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700264
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500265 if (m_netif) {
266 auto name = m_netif->getName();
267 auto index = to_string(m_netif->getIndex());
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700268
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400269 runTest("udp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700270 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
271
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400272 runTest("udp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700273 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
274 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700275}
276
277BOOST_AUTO_TEST_CASE(ParseTcp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700278{
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 Pesaventoe1081cd2016-12-19 23:58:15 -0500290 namespace ip = boost::asio::ip;
291
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700292 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500293 uri = FaceUri(endpoint4);
294 BOOST_CHECK_EQUAL(uri.toString(), "tcp4://192.0.2.1:7777");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700295
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500296 uri = FaceUri(endpoint4, "wsclient");
297 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://192.0.2.1:7777");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700298
299 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500300 uri = FaceUri(endpoint6);
301 BOOST_CHECK_EQUAL(uri.toString(), "tcp6://[2001:db8::1]:7777");
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700302
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 Shi77dcadd2014-10-05 14:40:54 -0700311}
312
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500313BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700314{
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 Newberry71aecae2017-05-29 00:22:39 -0700325 BOOST_CHECK_EQUAL(FaceUri("tcp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700326 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 Newberry71aecae2017-05-29 00:22:39 -0700330 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 Liac8b4ca2017-07-10 02:27:50 -0700332
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500333 if (m_netif) {
334 auto name = m_netif->getName();
335 auto index = to_string(m_netif->getIndex());
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700336
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 Afanasyev1286e022015-01-26 10:42:29 -0800342}
343
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500344BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800345BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
346{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100347 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700348
349 // IPv4 unicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400350 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 Shi4083c8d2014-10-12 16:43:16 -0700361
362 // IPv4 multicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400363 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 Shi4083c8d2014-10-12 16:43:16 -0700367
Eric Newberry71aecae2017-05-29 00:22:39 -0700368 // IPv6 used with tcp4 protocol - not canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400369 runTest("tcp4://[2001:db8::1]:6363", false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700370
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500371 if (m_netif) {
372 auto name = m_netif->getName();
373 auto index = to_string(m_netif->getIndex());
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700374
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400375 runTest("tcp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700376 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
377
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400378 runTest("tcp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700379 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
380 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800381}
382
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500383BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800384BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
385{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100386 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800387
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700388 // IPv6 unicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400389 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 Shi4083c8d2014-10-12 16:43:16 -0700398
399 // IPv6 multicast
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400400 runTest("tcp6://[ff02::2]:56363", false);
401 runTest("tcp6://[ff02::2]", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700402
Eric Newberry71aecae2017-05-29 00:22:39 -0700403 // IPv4 used with tcp6 protocol - not canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400404 runTest("tcp6://192.0.2.1:6363", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700405}
406
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500407BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700408BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700409{
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 Pesaventoe1081cd2016-12-19 23:58:15 -0500418 // 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 Shi77dcadd2014-10-05 14:40:54 -0700420
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500421#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
422 using boost::asio::local::stream_protocol;
423 stream_protocol::endpoint endpoint("/var/run/example.sock");
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500424 uri = FaceUri(endpoint);
425 BOOST_CHECK_EQUAL(uri.toString(), "unix:///var/run/example.sock");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500426#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700427}
428
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700429BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700430{
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 Pesavento8a8c01b2018-03-11 00:07:52 -0500440 uri = FaceUri::fromFd(fd);
441 BOOST_CHECK_EQUAL(uri.toString(), "fd://21");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700442}
443
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700444BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700445{
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 Shi4083c8d2014-10-12 16:43:16 -0700454 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700455
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500456 auto address = ethernet::Address::fromString("33:33:01:01:01:01");
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500457 uri = FaceUri(address);
458 BOOST_CHECK_EQUAL(uri.toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700459}
460
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700461BOOST_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 Pesavento2d7c5852022-07-18 16:02:52 -0400470 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 Shi4083c8d2014-10-12 16:43:16 -0700474}
475
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000476BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700477BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700478{
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 Shid5c2f0c2017-04-04 09:52:11 +0000487 BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896
488
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700489 std::string ifname = "en1";
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500490 uri = FaceUri::fromDev(ifname);
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000491 BOOST_CHECK_EQUAL(uri.toString(), "dev://en1");
492}
493
494BOOST_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
505BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture)
506{
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400507 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 Shi77dcadd2014-10-05 14:40:54 -0700512}
513
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700514BOOST_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 Pesaventoe1081cd2016-12-19 23:58:15 -0500534 namespace ip = boost::asio::ip;
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700535
536 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500537 uri = FaceUri::fromUdpDev(endpoint4, "en1");
538 BOOST_CHECK_EQUAL(uri.toString(), "udp4+dev://en1:7777");
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700539
540 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500541 uri = FaceUri::fromUdpDev(endpoint6, "en2");
542 BOOST_CHECK_EQUAL(uri.toString(), "udp6+dev://en2:7777");
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700543}
544
545BOOST_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 Pesavento2d7c5852022-07-18 16:02:52 -0400552 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 Liud7f4fda2016-10-19 22:38:39 -0700556}
557
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700558BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
559{
560 boost::asio::io_service io;
561
562 // unsupported scheme
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400563 FaceUri("null://").canonize(nullptr, nullptr, io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700564
565 // cannot resolve
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400566 FaceUri("udp://192.0.2.333").canonize(nullptr, nullptr, io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700567
568 // already canonical
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400569 FaceUri("udp4://192.0.2.1:6363").canonize(nullptr, nullptr, io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700570
571 // need DNS resolution
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400572 FaceUri("udp://192.0.2.1:6363").canonize(nullptr, nullptr, io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700573
574 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100575
576 // avoid "test case [...] did not check any assertions" message from Boost.Test
577 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700578}
579
580BOOST_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 Shi4083c8d2014-10-12 16:43:16 -0700586
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 Shi4083c8d2014-10-12 16:43:16 -0700591
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400592 runTest("internal://", false);
593 runTest("null://", false);
594 runTest("unix:///var/run/nfd.sock", false);
595 runTest("fd://0", false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700596}
597
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700598BOOST_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 Pesaventoeee3e822016-11-26 19:19:34 +0100610BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
Junxiao Shi25467942017-06-30 02:53:14 +0000611BOOST_AUTO_TEST_SUITE_END() // Net
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700612
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800613} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700614} // namespace ndn