blob: 2b99470a4604b2acc709741acbb6fe6bc3071ff8 [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
Davide Pesavento7e780642018-11-24 15:51:34 -050028#include "ndn-cxx/net/face-uri.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070029
Davide Pesavento7e780642018-11-24 15:51:34 -050030#include "tests/boost-test.hpp"
31#include "tests/unit/net/collect-netifs.hpp"
32#include "tests/unit/net/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{
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500125 FaceUri uri("udp://hostname:6363");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700126 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
127 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
128
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700129 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
130
131 BOOST_CHECK(uri.parse("udp://hostname:80"));
132 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
133 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
134 BOOST_CHECK_EQUAL(uri.getPort(), "80");
135 BOOST_CHECK_EQUAL(uri.getPath(), "");
136
137 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
138 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
139 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
140 BOOST_CHECK_EQUAL(uri.getPort(), "20");
141 BOOST_CHECK_EQUAL(uri.getPath(), "");
142
143 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
144 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
145 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
146 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
147 BOOST_CHECK_EQUAL(uri.getPath(), "");
148
149 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
150 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
151 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
152 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
153 BOOST_CHECK_EQUAL(uri.getPath(), "");
154
155 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
156
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500157 namespace ip = boost::asio::ip;
158
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700159 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500160 uri = FaceUri(endpoint4);
161 BOOST_CHECK_EQUAL(uri.toString(), "udp4://192.0.2.1:7777");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700162
163 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500164 uri = FaceUri(endpoint6);
165 BOOST_CHECK_EQUAL(uri.toString(), "udp6://[2001:db8::1]:7777");
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700166
167 BOOST_CHECK(uri.parse("udp6://[fe80::1%25eth1]:6363"));
168 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1");
169
170 BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]:6363"));
171 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1");
172
173 BOOST_CHECK(uri.parse("udp6://[fe80::1%1]:6363"));
174 BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]"));
Davide Pesaventob984e322018-01-24 19:40:07 -0500175
176 BOOST_CHECK(uri.parse("udp6://[ff01::114%eth#1]"));
177 BOOST_CHECK(uri.parse("udp6://[ff01::114%eth.1,2]"));
178 BOOST_CHECK(uri.parse("udp6://[ff01::114%a+b-c=0]"));
179 BOOST_CHECK(uri.parse("udp6://[ff01::114%[foo]]"));
180 BOOST_CHECK(uri.parse("udp6://[ff01::114%]]"));
181 BOOST_CHECK(uri.parse("udp6://[ff01::114%%]"));
182 BOOST_CHECK(!uri.parse("udp6://[ff01::114%]"));
183 BOOST_CHECK(!uri.parse("udp6://[ff01::114%foo bar]"));
184 BOOST_CHECK(!uri.parse("udp6://[ff01::114%foo/bar]"));
185 BOOST_CHECK(!uri.parse("udp6://[ff01::114%eth0:1]"));
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700186}
187
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500188BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700189{
190 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true);
191 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true);
192 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true);
193
194 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true);
195 BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false);
196 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false);
197 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false);
198 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true);
199 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700200 BOOST_CHECK_EQUAL(FaceUri("udp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700201 BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false);
202 BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false);
203 BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false);
204 BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
Eric Newberry71aecae2017-05-29 00:22:39 -0700205 BOOST_CHECK_EQUAL(FaceUri("udp4://[2001:db8::1]:6363").isCanonical(), false);
206 BOOST_CHECK_EQUAL(FaceUri("udp6://192.0.2.1:6363").isCanonical(), false);
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700207
208 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
209 if (!networkInterfaces.empty()) {
210 const auto& netif = networkInterfaces.front();
211 auto name = netif->getName();
212 auto index = to_string(netif->getIndex());
213
214 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
215 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
216 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]").isCanonical(), false);
217 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
218 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800219}
220
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500221BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800222BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
223{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100224 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700225
226 // IPv4 unicast
227 addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363");
228 addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363");
229 addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363");
230 addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363");
231 addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695");
232 addTest("udp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500233 addTest("udp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700234 addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500235 addTest("udp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700236 addTest("udp4://invalid.invalid", false, "");
237
238 // IPv4 multicast
239 addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
240 addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
241 addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
242
Eric Newberry71aecae2017-05-29 00:22:39 -0700243 // IPv6 used with udp4 protocol - not canonical
244 addTest("udp4://[2001:db8::1]:6363", false, "");
245
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800246 runTests();
247}
248
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500249BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800250BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
251{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100252 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800253
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700254 // IPv6 unicast
255 addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500256 addTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700257 addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
258 addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500259 addTest("udp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500260 addTest("udp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700261 addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500262 addTest("udp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700263 addTest("udp6://invalid.invalid", false, "");
264 addTest("udp://invalid.invalid", false, "");
265
266 // IPv6 multicast
267 addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
268 addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
269
Eric Newberry71aecae2017-05-29 00:22:39 -0700270 // IPv4 used with udp6 protocol - not canonical
271 addTest("udp6://192.0.2.1:6363", false, "");
272
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700273 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
274 if (!networkInterfaces.empty()) {
275 const auto& netif = networkInterfaces.front();
276 auto name = netif->getName();
277 auto index = to_string(netif->getIndex());
278
279 addTest("udp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
280 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
281
282 addTest("udp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
283 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
284 }
285
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700286 runTests();
287}
288
289BOOST_AUTO_TEST_CASE(ParseTcp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700290{
291 FaceUri uri;
292
293 BOOST_CHECK(uri.parse("tcp://random.host.name"));
294 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
295 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
296 BOOST_CHECK_EQUAL(uri.getPort(), "");
297 BOOST_CHECK_EQUAL(uri.getPath(), "");
298
299 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
300 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
301
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500302 namespace ip = boost::asio::ip;
303
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700304 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500305 uri = FaceUri(endpoint4);
306 BOOST_CHECK_EQUAL(uri.toString(), "tcp4://192.0.2.1:7777");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700307
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500308 uri = FaceUri(endpoint4, "wsclient");
309 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://192.0.2.1:7777");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700310
311 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500312 uri = FaceUri(endpoint6);
313 BOOST_CHECK_EQUAL(uri.toString(), "tcp6://[2001:db8::1]:7777");
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700314
315 BOOST_CHECK(uri.parse("tcp6://[fe80::1%25eth1]:6363"));
316 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1");
317
318 BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]:6363"));
319 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1");
320
321 BOOST_CHECK(uri.parse("tcp6://[fe80::1%1]:6363"));
322 BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]"));
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700323}
324
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500325BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700326{
327 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
328 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
329 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
330
331 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
332 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
333 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
334 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
335 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
336 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700337 BOOST_CHECK_EQUAL(FaceUri("tcp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700338 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
339 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
340 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
341 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700342 BOOST_CHECK_EQUAL(FaceUri("tcp4://[2001:db8::1]:6363").isCanonical(), false);
343 BOOST_CHECK_EQUAL(FaceUri("tcp6://192.0.2.1:6363").isCanonical(), false);
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700344
345 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
346 if (!networkInterfaces.empty()) {
347 const auto& netif = networkInterfaces.front();
348 auto name = netif->getName();
349 auto index = to_string(netif->getIndex());
350
351 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
352 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
353 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]").isCanonical(), false);
354 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
355 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800356}
357
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500358BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800359BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
360{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100361 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700362
363 // IPv4 unicast
364 addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
365 addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
366 addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
367 addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
368 addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
369 addTest("tcp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500370 addTest("tcp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700371 addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500372 addTest("tcp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700373 addTest("tcp4://invalid.invalid", false, "");
374
375 // IPv4 multicast
376 addTest("tcp4://224.0.23.170:56363", false, "");
377 addTest("tcp4://224.0.23.170", false, "");
378 addTest("tcp4://all-routers.mcast.net:56363", false, "");
379
Eric Newberry71aecae2017-05-29 00:22:39 -0700380 // IPv6 used with tcp4 protocol - not canonical
381 addTest("tcp4://[2001:db8::1]:6363", false, "");
382
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700383 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
384 if (!networkInterfaces.empty()) {
385 const auto& netif = networkInterfaces.front();
386 auto name = netif->getName();
387 auto index = to_string(netif->getIndex());
388
389 addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
390 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
391
392 addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
393 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
394 }
395
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800396 runTests();
397}
398
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500399BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800400BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
401{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100402 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800403
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700404 // IPv6 unicast
405 addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500406 addTest("tcp6://[2001:db8::1]", true, "tcp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700407 addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
408 addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500409 addTest("tcp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500410 addTest("tcp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700411 addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500412 addTest("tcp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700413 addTest("tcp6://invalid.invalid", false, "");
414 addTest("tcp://invalid.invalid", false, "");
415
416 // IPv6 multicast
417 addTest("tcp6://[ff02::2]:56363", false, "");
418 addTest("tcp6://[ff02::2]", false, "");
419
Eric Newberry71aecae2017-05-29 00:22:39 -0700420 // IPv4 used with tcp6 protocol - not canonical
421 addTest("tcp6://192.0.2.1:6363", false, "");
422
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700423 runTests();
424}
425
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500426BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700427BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700428{
429 FaceUri uri;
430
431 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
432 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
433 BOOST_CHECK_EQUAL(uri.getHost(), "");
434 BOOST_CHECK_EQUAL(uri.getPort(), "");
435 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
436
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500437 // This is not a valid unix:// URI, but the parse would treat "var" as host
438 BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); // Bug #3896
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700439
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500440#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
441 using boost::asio::local::stream_protocol;
442 stream_protocol::endpoint endpoint("/var/run/example.sock");
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500443 uri = FaceUri(endpoint);
444 BOOST_CHECK_EQUAL(uri.toString(), "unix:///var/run/example.sock");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500445#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700446}
447
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700448BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700449{
450 FaceUri uri;
451
452 BOOST_CHECK(uri.parse("fd://6"));
453 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
454 BOOST_CHECK_EQUAL(uri.getHost(), "6");
455 BOOST_CHECK_EQUAL(uri.getPort(), "");
456 BOOST_CHECK_EQUAL(uri.getPath(), "");
457
458 int fd = 21;
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500459 uri = FaceUri::fromFd(fd);
460 BOOST_CHECK_EQUAL(uri.toString(), "fd://21");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700461}
462
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700463BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700464{
465 FaceUri uri;
466
467 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
468 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
469 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
470 BOOST_CHECK_EQUAL(uri.getPort(), "");
471 BOOST_CHECK_EQUAL(uri.getPath(), "");
472
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700473 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700474
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500475 auto address = ethernet::Address::fromString("33:33:01:01:01:01");
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500476 uri = FaceUri(address);
477 BOOST_CHECK_EQUAL(uri.toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700478}
479
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700480BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
481{
482 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
483
484 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
485 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
486 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
487 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
488
489 addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
490 addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
491 addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
492 addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
493
494 runTests();
495}
496
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000497BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700498BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700499{
500 FaceUri uri;
501
502 BOOST_CHECK(uri.parse("dev://eth0"));
503 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
504 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
505 BOOST_CHECK_EQUAL(uri.getPort(), "");
506 BOOST_CHECK_EQUAL(uri.getPath(), "");
507
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000508 BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896
509
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700510 std::string ifname = "en1";
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500511 uri = FaceUri::fromDev(ifname);
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000512 BOOST_CHECK_EQUAL(uri.toString(), "dev://en1");
513}
514
515BOOST_AUTO_TEST_CASE(IsCanonicalDev)
516{
517 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), true);
518
519 BOOST_CHECK_EQUAL(FaceUri("dev://eth0").isCanonical(), true);
520 BOOST_CHECK_EQUAL(FaceUri("dev://").isCanonical(), false);
521 BOOST_CHECK_EQUAL(FaceUri("dev://eth0:8888").isCanonical(), false);
522 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/").isCanonical(), false);
523 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/A").isCanonical(), false);
524}
525
526BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture)
527{
528 addTest("dev://eth0", true, "dev://eth0");
529 addTest("dev://", false, "");
530 addTest("dev://eth0:8888", false, "");
531 addTest("dev://eth0/", true, "dev://eth0");
532 addTest("dev://eth0/A", false, "");
533
534 runTests();
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700535}
536
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700537BOOST_AUTO_TEST_CASE(ParseUdpDev)
538{
539 FaceUri uri;
540
541 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
542 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
543 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
544 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
545 BOOST_CHECK_EQUAL(uri.getPath(), "");
546
547 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
548 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
549 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
550 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
551 BOOST_CHECK_EQUAL(uri.getPath(), "");
552
553 BOOST_CHECK(uri.parse("abc+efg://eth0"));
554 BOOST_CHECK(!uri.parse("abc+://eth0"));
555 BOOST_CHECK(!uri.parse("+abc://eth0"));
556
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500557 namespace ip = boost::asio::ip;
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700558
559 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500560 uri = FaceUri::fromUdpDev(endpoint4, "en1");
561 BOOST_CHECK_EQUAL(uri.toString(), "udp4+dev://en1:7777");
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700562
563 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -0500564 uri = FaceUri::fromUdpDev(endpoint6, "en2");
565 BOOST_CHECK_EQUAL(uri.toString(), "udp6+dev://en2:7777");
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700566}
567
568BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
569{
570 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
571 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
572 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
573 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
574
575 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
576 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
577 addTest("udp+dev://en1:7777", false, "");
578 addTest("udp6+dev://en2", false, "");
579}
580
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700581BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
582{
583 boost::asio::io_service io;
584
585 // unsupported scheme
586 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
587 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500588 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700589
590 // cannot resolve
591 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
592 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500593 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700594
595 // already canonical
596 FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
597 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500598 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700599
600 // need DNS resolution
601 FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
602 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500603 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700604
605 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100606
607 // avoid "test case [...] did not check any assertions" message from Boost.Test
608 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700609}
610
611BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
612{
613 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
614 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
615 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
616 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700617
618 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
619 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
620 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
621 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700622
623 addTest("internal://", false, "");
624 addTest("null://", false, "");
625 addTest("unix:///var/run/nfd.sock", false, "");
626 addTest("fd://0", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700627
628 runTests();
629}
630
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700631BOOST_AUTO_TEST_CASE(Bug1635)
632{
633 FaceUri uri;
634
635 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
636 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
637 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
638 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
639 BOOST_CHECK_EQUAL(uri.getPath(), "");
640 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
641}
642
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100643BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
Junxiao Shi25467942017-06-30 02:53:14 +0000644BOOST_AUTO_TEST_SUITE_END() // Net
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700645
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800646} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700647} // namespace ndn