blob: 73549df24069eb403082db468000b68a636c18a3 [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 Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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
Junxiao Shi25467942017-06-30 02:53:14 +000028#include "net/face-uri.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070029
30#include "boost-test.hpp"
Yanbiao Liac8b4ca2017-07-10 02:27:50 -070031#include "collect-netifs.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000032#include "network-configuration-detector.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070033
34namespace ndn {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080035namespace tests {
36
Junxiao Shi25467942017-06-30 02:53:14 +000037BOOST_AUTO_TEST_SUITE(Net)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010038BOOST_AUTO_TEST_SUITE(TestFaceUri)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070039
Junxiao Shi4083c8d2014-10-12 16:43:16 -070040class CanonizeFixture : noncopyable
41{
42protected:
Junxiao Shi4083c8d2014-10-12 16:43:16 -070043 void
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050044 addTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri)
45 {
46 ++m_nPending;
47 auto tc = make_shared<CanonizeTestCase>(request, shouldSucceed, expectedUri);
48
49 FaceUri uri(request);
50 uri.canonize(bind(&CanonizeFixture::onCanonizeSuccess, this, tc, _1),
51 bind(&CanonizeFixture::onCanonizeFailure, this, tc, _1),
Davide Pesavento0f830802018-01-16 23:58:58 -050052 m_io, 10_s);
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050053 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -070054
55 void
56 runTests()
57 {
58 m_io.run();
59 BOOST_CHECK_EQUAL(m_nPending, 0);
60 }
61
62private:
Davide Pesavento50df5622015-03-19 22:05:05 +010063 class CanonizeTestCase
Junxiao Shi4083c8d2014-10-12 16:43:16 -070064 {
65 public:
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050066 CanonizeTestCase(const std::string& request, bool shouldSucceed, const std::string& expectedUri)
67 : m_expectedUri(expectedUri)
Davide Pesavento50df5622015-03-19 22:05:05 +010068 , m_message(request + " should " + (shouldSucceed ? "succeed" : "fail"))
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050069 , m_shouldSucceed(shouldSucceed)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070070 , m_isCompleted(false)
71 {
72 }
73
74 public:
Junxiao Shi4083c8d2014-10-12 16:43:16 -070075 std::string m_expectedUri;
Davide Pesavento50df5622015-03-19 22:05:05 +010076 std::string m_message;
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050077 bool m_shouldSucceed;
Junxiao Shi4083c8d2014-10-12 16:43:16 -070078 bool m_isCompleted;
79 };
80
Junxiao Shi4083c8d2014-10-12 16:43:16 -070081 void
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050082 onCanonizeSuccess(const shared_ptr<CanonizeTestCase>& tc, const FaceUri& canonicalUri)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070083 {
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050084 BOOST_CHECK_EQUAL(tc->m_isCompleted, false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -070085 tc->m_isCompleted = true;
86 --m_nPending;
87
Davide Pesavento50df5622015-03-19 22:05:05 +010088 BOOST_CHECK_MESSAGE(tc->m_shouldSucceed, tc->m_message);
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050089 if (tc->m_shouldSucceed) {
90 BOOST_CHECK_EQUAL(canonicalUri.toString(), tc->m_expectedUri);
91 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -070092 }
93
Junxiao Shi4083c8d2014-10-12 16:43:16 -070094 void
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050095 onCanonizeFailure(const shared_ptr<CanonizeTestCase>& tc, const std::string& reason)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070096 {
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050097 BOOST_CHECK_EQUAL(tc->m_isCompleted, false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -070098 tc->m_isCompleted = true;
99 --m_nPending;
100
Davide Pesavento50df5622015-03-19 22:05:05 +0100101 BOOST_CHECK_MESSAGE(!tc->m_shouldSucceed, tc->m_message);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700102 }
103
104private:
105 boost::asio::io_service m_io;
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500106 ssize_t m_nPending = 0;
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700107};
108
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700109BOOST_AUTO_TEST_CASE(ParseInternal)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700110{
111 FaceUri uri;
112
113 BOOST_CHECK(uri.parse("internal://"));
114 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
115 BOOST_CHECK_EQUAL(uri.getHost(), "");
116 BOOST_CHECK_EQUAL(uri.getPort(), "");
117 BOOST_CHECK_EQUAL(uri.getPath(), "");
118
119 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
120 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
121}
122
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700123BOOST_AUTO_TEST_CASE(ParseUdp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700124{
125 BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
126 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
127 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
128
129 FaceUri uri;
130 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
131
132 BOOST_CHECK(uri.parse("udp://hostname:80"));
133 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
134 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
135 BOOST_CHECK_EQUAL(uri.getPort(), "80");
136 BOOST_CHECK_EQUAL(uri.getPath(), "");
137
138 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
139 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
140 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
141 BOOST_CHECK_EQUAL(uri.getPort(), "20");
142 BOOST_CHECK_EQUAL(uri.getPath(), "");
143
144 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
145 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
146 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
147 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
148 BOOST_CHECK_EQUAL(uri.getPath(), "");
149
150 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
151 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
152 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
153 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
154 BOOST_CHECK_EQUAL(uri.getPath(), "");
155
156 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
157
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500158 namespace ip = boost::asio::ip;
159
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700160 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
161 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
162 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777");
163
164 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
165 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
166 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700167
168 BOOST_CHECK(uri.parse("udp6://[fe80::1%25eth1]:6363"));
169 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1");
170
171 BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]:6363"));
172 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1");
173
174 BOOST_CHECK(uri.parse("udp6://[fe80::1%1]:6363"));
175 BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]"));
Davide Pesaventob984e322018-01-24 19:40:07 -0500176
177 BOOST_CHECK(uri.parse("udp6://[ff01::114%eth#1]"));
178 BOOST_CHECK(uri.parse("udp6://[ff01::114%eth.1,2]"));
179 BOOST_CHECK(uri.parse("udp6://[ff01::114%a+b-c=0]"));
180 BOOST_CHECK(uri.parse("udp6://[ff01::114%[foo]]"));
181 BOOST_CHECK(uri.parse("udp6://[ff01::114%]]"));
182 BOOST_CHECK(uri.parse("udp6://[ff01::114%%]"));
183 BOOST_CHECK(!uri.parse("udp6://[ff01::114%]"));
184 BOOST_CHECK(!uri.parse("udp6://[ff01::114%foo bar]"));
185 BOOST_CHECK(!uri.parse("udp6://[ff01::114%foo/bar]"));
186 BOOST_CHECK(!uri.parse("udp6://[ff01::114%eth0:1]"));
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700187}
188
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500189BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700190{
191 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true);
192 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true);
193 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true);
194
195 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true);
196 BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false);
197 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false);
198 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false);
199 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true);
200 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700201 BOOST_CHECK_EQUAL(FaceUri("udp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700202 BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false);
203 BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false);
204 BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false);
205 BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
Eric Newberry71aecae2017-05-29 00:22:39 -0700206 BOOST_CHECK_EQUAL(FaceUri("udp4://[2001:db8::1]:6363").isCanonical(), false);
207 BOOST_CHECK_EQUAL(FaceUri("udp6://192.0.2.1:6363").isCanonical(), false);
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700208
209 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
210 if (!networkInterfaces.empty()) {
211 const auto& netif = networkInterfaces.front();
212 auto name = netif->getName();
213 auto index = to_string(netif->getIndex());
214
215 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
216 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
217 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]").isCanonical(), false);
218 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
219 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800220}
221
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500222BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800223BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
224{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100225 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700226
227 // IPv4 unicast
228 addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363");
229 addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363");
230 addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363");
231 addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363");
232 addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695");
233 addTest("udp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500234 addTest("udp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700235 addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500236 addTest("udp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700237 addTest("udp4://invalid.invalid", false, "");
238
239 // IPv4 multicast
240 addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
241 addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
242 addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
243
Eric Newberry71aecae2017-05-29 00:22:39 -0700244 // IPv6 used with udp4 protocol - not canonical
245 addTest("udp4://[2001:db8::1]:6363", false, "");
246
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800247 runTests();
248}
249
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500250BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800251BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
252{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100253 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800254
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700255 // IPv6 unicast
256 addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500257 addTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700258 addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
259 addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500260 addTest("udp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500261 addTest("udp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700262 addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500263 addTest("udp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700264 addTest("udp6://invalid.invalid", false, "");
265 addTest("udp://invalid.invalid", false, "");
266
267 // IPv6 multicast
268 addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
269 addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
270
Eric Newberry71aecae2017-05-29 00:22:39 -0700271 // IPv4 used with udp6 protocol - not canonical
272 addTest("udp6://192.0.2.1:6363", false, "");
273
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700274 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
275 if (!networkInterfaces.empty()) {
276 const auto& netif = networkInterfaces.front();
277 auto name = netif->getName();
278 auto index = to_string(netif->getIndex());
279
280 addTest("udp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
281 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
282
283 addTest("udp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
284 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
285 }
286
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700287 runTests();
288}
289
290BOOST_AUTO_TEST_CASE(ParseTcp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700291{
292 FaceUri uri;
293
294 BOOST_CHECK(uri.parse("tcp://random.host.name"));
295 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
296 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
297 BOOST_CHECK_EQUAL(uri.getPort(), "");
298 BOOST_CHECK_EQUAL(uri.getPath(), "");
299
300 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
301 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
302
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500303 namespace ip = boost::asio::ip;
304
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700305 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
306 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
307 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
308
309 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient"));
310 BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777");
311
312 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
313 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
314 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700315
316 BOOST_CHECK(uri.parse("tcp6://[fe80::1%25eth1]:6363"));
317 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1");
318
319 BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]:6363"));
320 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1");
321
322 BOOST_CHECK(uri.parse("tcp6://[fe80::1%1]:6363"));
323 BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]"));
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700324}
325
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500326BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700327{
328 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
329 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
330 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
331
332 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
333 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
334 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
335 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
336 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
337 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700338 BOOST_CHECK_EQUAL(FaceUri("tcp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700339 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
340 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
341 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
342 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700343 BOOST_CHECK_EQUAL(FaceUri("tcp4://[2001:db8::1]:6363").isCanonical(), false);
344 BOOST_CHECK_EQUAL(FaceUri("tcp6://192.0.2.1:6363").isCanonical(), false);
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700345
346 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
347 if (!networkInterfaces.empty()) {
348 const auto& netif = networkInterfaces.front();
349 auto name = netif->getName();
350 auto index = to_string(netif->getIndex());
351
352 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
353 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
354 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]").isCanonical(), false);
355 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
356 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800357}
358
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500359BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800360BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
361{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100362 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700363
364 // IPv4 unicast
365 addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
366 addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
367 addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
368 addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
369 addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
370 addTest("tcp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500371 addTest("tcp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700372 addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500373 addTest("tcp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700374 addTest("tcp4://invalid.invalid", false, "");
375
376 // IPv4 multicast
377 addTest("tcp4://224.0.23.170:56363", false, "");
378 addTest("tcp4://224.0.23.170", false, "");
379 addTest("tcp4://all-routers.mcast.net:56363", false, "");
380
Eric Newberry71aecae2017-05-29 00:22:39 -0700381 // IPv6 used with tcp4 protocol - not canonical
382 addTest("tcp4://[2001:db8::1]:6363", false, "");
383
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700384 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
385 if (!networkInterfaces.empty()) {
386 const auto& netif = networkInterfaces.front();
387 auto name = netif->getName();
388 auto index = to_string(netif->getIndex());
389
390 addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
391 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
392
393 addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
394 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
395 }
396
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800397 runTests();
398}
399
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500400BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800401BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
402{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100403 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800404
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700405 // IPv6 unicast
406 addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500407 addTest("tcp6://[2001:db8::1]", true, "tcp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700408 addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
409 addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500410 addTest("tcp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500411 addTest("tcp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700412 addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500413 addTest("tcp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700414 addTest("tcp6://invalid.invalid", false, "");
415 addTest("tcp://invalid.invalid", false, "");
416
417 // IPv6 multicast
418 addTest("tcp6://[ff02::2]:56363", false, "");
419 addTest("tcp6://[ff02::2]", false, "");
420
Eric Newberry71aecae2017-05-29 00:22:39 -0700421 // IPv4 used with tcp6 protocol - not canonical
422 addTest("tcp6://192.0.2.1:6363", false, "");
423
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700424 runTests();
425}
426
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500427BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700428BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700429{
430 FaceUri uri;
431
432 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
433 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
434 BOOST_CHECK_EQUAL(uri.getHost(), "");
435 BOOST_CHECK_EQUAL(uri.getPort(), "");
436 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
437
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500438 // This is not a valid unix:// URI, but the parse would treat "var" as host
439 BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); // Bug #3896
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700440
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500441#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
442 using boost::asio::local::stream_protocol;
443 stream_protocol::endpoint endpoint("/var/run/example.sock");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700444 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
445 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500446#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700447}
448
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700449BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700450{
451 FaceUri uri;
452
453 BOOST_CHECK(uri.parse("fd://6"));
454 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
455 BOOST_CHECK_EQUAL(uri.getHost(), "6");
456 BOOST_CHECK_EQUAL(uri.getPort(), "");
457 BOOST_CHECK_EQUAL(uri.getPath(), "");
458
459 int fd = 21;
460 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
461 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
462}
463
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700464BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700465{
466 FaceUri uri;
467
468 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
469 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
470 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
471 BOOST_CHECK_EQUAL(uri.getPort(), "");
472 BOOST_CHECK_EQUAL(uri.getPath(), "");
473
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700474 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700475
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500476 auto address = ethernet::Address::fromString("33:33:01:01:01:01");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700477 BOOST_REQUIRE_NO_THROW(FaceUri(address));
478 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700479}
480
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700481BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
482{
483 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
484
485 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
486 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
487 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
488 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
489
490 addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
491 addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
492 addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
493 addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
494
495 runTests();
496}
497
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000498BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700499BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700500{
501 FaceUri uri;
502
503 BOOST_CHECK(uri.parse("dev://eth0"));
504 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
505 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
506 BOOST_CHECK_EQUAL(uri.getPort(), "");
507 BOOST_CHECK_EQUAL(uri.getPath(), "");
508
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000509 BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896
510
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700511 std::string ifname = "en1";
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000512 BOOST_REQUIRE_NO_THROW(uri = FaceUri::fromDev(ifname));
513 BOOST_CHECK_EQUAL(uri.toString(), "dev://en1");
514}
515
516BOOST_AUTO_TEST_CASE(IsCanonicalDev)
517{
518 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), true);
519
520 BOOST_CHECK_EQUAL(FaceUri("dev://eth0").isCanonical(), true);
521 BOOST_CHECK_EQUAL(FaceUri("dev://").isCanonical(), false);
522 BOOST_CHECK_EQUAL(FaceUri("dev://eth0:8888").isCanonical(), false);
523 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/").isCanonical(), false);
524 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/A").isCanonical(), false);
525}
526
527BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture)
528{
529 addTest("dev://eth0", true, "dev://eth0");
530 addTest("dev://", false, "");
531 addTest("dev://eth0:8888", false, "");
532 addTest("dev://eth0/", true, "dev://eth0");
533 addTest("dev://eth0/A", false, "");
534
535 runTests();
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700536}
537
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700538BOOST_AUTO_TEST_CASE(ParseUdpDev)
539{
540 FaceUri uri;
541
542 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
543 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
544 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
545 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
546 BOOST_CHECK_EQUAL(uri.getPath(), "");
547
548 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
549 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
550 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
551 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
552 BOOST_CHECK_EQUAL(uri.getPath(), "");
553
554 BOOST_CHECK(uri.parse("abc+efg://eth0"));
555 BOOST_CHECK(!uri.parse("abc+://eth0"));
556 BOOST_CHECK(!uri.parse("+abc://eth0"));
557
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500558 namespace ip = boost::asio::ip;
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700559
560 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
561 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1"));
562 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777");
563
564 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
565 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2"));
566 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777");
567}
568
569BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
570{
571 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
572 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
573 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
574 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
575
576 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
577 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
578 addTest("udp+dev://en1:7777", false, "");
579 addTest("udp6+dev://en2", false, "");
580}
581
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700582BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
583{
584 boost::asio::io_service io;
585
586 // unsupported scheme
587 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
588 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500589 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700590
591 // cannot resolve
592 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
593 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500594 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700595
596 // already canonical
597 FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
598 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500599 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700600
601 // need DNS resolution
602 FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
603 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500604 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700605
606 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100607
608 // avoid "test case [...] did not check any assertions" message from Boost.Test
609 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700610}
611
612BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
613{
614 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
615 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
616 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
617 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700618
619 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
620 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
621 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
622 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700623
624 addTest("internal://", false, "");
625 addTest("null://", false, "");
626 addTest("unix:///var/run/nfd.sock", false, "");
627 addTest("fd://0", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700628
629 runTests();
630}
631
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700632BOOST_AUTO_TEST_CASE(Bug1635)
633{
634 FaceUri uri;
635
636 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
637 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
638 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
639 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
640 BOOST_CHECK_EQUAL(uri.getPath(), "");
641 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
642}
643
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100644BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
Junxiao Shi25467942017-06-30 02:53:14 +0000645BOOST_AUTO_TEST_SUITE_END() // Net
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700646
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800647} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700648} // namespace ndn