blob: 1f90f67b153bd8d131ec9a323ef3fbeb03857d72 [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]"));
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700176}
177
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500178BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700179{
180 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true);
181 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true);
182 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true);
183
184 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true);
185 BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false);
186 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false);
187 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false);
188 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true);
189 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700190 BOOST_CHECK_EQUAL(FaceUri("udp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700191 BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false);
192 BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false);
193 BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false);
194 BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
Eric Newberry71aecae2017-05-29 00:22:39 -0700195 BOOST_CHECK_EQUAL(FaceUri("udp4://[2001:db8::1]:6363").isCanonical(), false);
196 BOOST_CHECK_EQUAL(FaceUri("udp6://192.0.2.1:6363").isCanonical(), false);
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700197
198 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
199 if (!networkInterfaces.empty()) {
200 const auto& netif = networkInterfaces.front();
201 auto name = netif->getName();
202 auto index = to_string(netif->getIndex());
203
204 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
205 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
206 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]").isCanonical(), false);
207 BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
208 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800209}
210
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500211BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800212BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
213{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100214 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700215
216 // IPv4 unicast
217 addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363");
218 addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363");
219 addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363");
220 addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363");
221 addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695");
222 addTest("udp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500223 addTest("udp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700224 addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500225 addTest("udp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700226 addTest("udp4://invalid.invalid", false, "");
227
228 // IPv4 multicast
229 addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
230 addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
231 addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
232
Eric Newberry71aecae2017-05-29 00:22:39 -0700233 // IPv6 used with udp4 protocol - not canonical
234 addTest("udp4://[2001:db8::1]:6363", false, "");
235
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800236 runTests();
237}
238
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500239BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800240BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
241{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100242 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800243
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700244 // IPv6 unicast
245 addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500246 addTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700247 addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
248 addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500249 addTest("udp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500250 addTest("udp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700251 addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500252 addTest("udp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700253 addTest("udp6://invalid.invalid", false, "");
254 addTest("udp://invalid.invalid", false, "");
255
256 // IPv6 multicast
257 addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
258 addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
259
Eric Newberry71aecae2017-05-29 00:22:39 -0700260 // IPv4 used with udp6 protocol - not canonical
261 addTest("udp6://192.0.2.1:6363", false, "");
262
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700263 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
264 if (!networkInterfaces.empty()) {
265 const auto& netif = networkInterfaces.front();
266 auto name = netif->getName();
267 auto index = to_string(netif->getIndex());
268
269 addTest("udp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
270 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
271
272 addTest("udp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
273 "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
274 }
275
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700276 runTests();
277}
278
279BOOST_AUTO_TEST_CASE(ParseTcp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700280{
281 FaceUri uri;
282
283 BOOST_CHECK(uri.parse("tcp://random.host.name"));
284 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
285 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
286 BOOST_CHECK_EQUAL(uri.getPort(), "");
287 BOOST_CHECK_EQUAL(uri.getPath(), "");
288
289 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
290 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
291
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500292 namespace ip = boost::asio::ip;
293
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700294 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
295 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
296 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
297
298 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient"));
299 BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777");
300
301 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
302 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
303 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700304
305 BOOST_CHECK(uri.parse("tcp6://[fe80::1%25eth1]:6363"));
306 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1");
307
308 BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]:6363"));
309 BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1");
310
311 BOOST_CHECK(uri.parse("tcp6://[fe80::1%1]:6363"));
312 BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]"));
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700313}
314
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500315BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700316{
317 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
318 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
319 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
320
321 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
322 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
323 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
324 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
325 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
326 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700327 BOOST_CHECK_EQUAL(FaceUri("tcp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700328 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
329 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
330 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
331 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700332 BOOST_CHECK_EQUAL(FaceUri("tcp4://[2001:db8::1]:6363").isCanonical(), false);
333 BOOST_CHECK_EQUAL(FaceUri("tcp6://192.0.2.1:6363").isCanonical(), false);
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700334
335 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
336 if (!networkInterfaces.empty()) {
337 const auto& netif = networkInterfaces.front();
338 auto name = netif->getName();
339 auto index = to_string(netif->getIndex());
340
341 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
342 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
343 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]").isCanonical(), false);
344 BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
345 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800346}
347
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500348BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800349BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
350{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100351 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700352
353 // IPv4 unicast
354 addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
355 addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
356 addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
357 addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
358 addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
359 addTest("tcp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500360 addTest("tcp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700361 addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500362 addTest("tcp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700363 addTest("tcp4://invalid.invalid", false, "");
364
365 // IPv4 multicast
366 addTest("tcp4://224.0.23.170:56363", false, "");
367 addTest("tcp4://224.0.23.170", false, "");
368 addTest("tcp4://all-routers.mcast.net:56363", false, "");
369
Eric Newberry71aecae2017-05-29 00:22:39 -0700370 // IPv6 used with tcp4 protocol - not canonical
371 addTest("tcp4://[2001:db8::1]:6363", false, "");
372
Yanbiao Liac8b4ca2017-07-10 02:27:50 -0700373 const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
374 if (!networkInterfaces.empty()) {
375 const auto& netif = networkInterfaces.front();
376 auto name = netif->getName();
377 auto index = to_string(netif->getIndex());
378
379 addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
380 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
381
382 addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
383 "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
384 }
385
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800386 runTests();
387}
388
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500389BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800390BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
391{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100392 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800393
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700394 // IPv6 unicast
395 addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500396 addTest("tcp6://[2001:db8::1]", true, "tcp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700397 addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
398 addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500399 addTest("tcp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500400 addTest("tcp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700401 addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500402 addTest("tcp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700403 addTest("tcp6://invalid.invalid", false, "");
404 addTest("tcp://invalid.invalid", false, "");
405
406 // IPv6 multicast
407 addTest("tcp6://[ff02::2]:56363", false, "");
408 addTest("tcp6://[ff02::2]", false, "");
409
Eric Newberry71aecae2017-05-29 00:22:39 -0700410 // IPv4 used with tcp6 protocol - not canonical
411 addTest("tcp6://192.0.2.1:6363", false, "");
412
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700413 runTests();
414}
415
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500416BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700417BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700418{
419 FaceUri uri;
420
421 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
422 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
423 BOOST_CHECK_EQUAL(uri.getHost(), "");
424 BOOST_CHECK_EQUAL(uri.getPort(), "");
425 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
426
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500427 // This is not a valid unix:// URI, but the parse would treat "var" as host
428 BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); // Bug #3896
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700429
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500430#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
431 using boost::asio::local::stream_protocol;
432 stream_protocol::endpoint endpoint("/var/run/example.sock");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700433 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
434 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500435#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700436}
437
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700438BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700439{
440 FaceUri uri;
441
442 BOOST_CHECK(uri.parse("fd://6"));
443 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
444 BOOST_CHECK_EQUAL(uri.getHost(), "6");
445 BOOST_CHECK_EQUAL(uri.getPort(), "");
446 BOOST_CHECK_EQUAL(uri.getPath(), "");
447
448 int fd = 21;
449 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
450 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
451}
452
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700453BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700454{
455 FaceUri uri;
456
457 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
458 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
459 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
460 BOOST_CHECK_EQUAL(uri.getPort(), "");
461 BOOST_CHECK_EQUAL(uri.getPath(), "");
462
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700463 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700464
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500465 auto address = ethernet::Address::fromString("33:33:01:01:01:01");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700466 BOOST_REQUIRE_NO_THROW(FaceUri(address));
467 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700468}
469
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700470BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
471{
472 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
473
474 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
475 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
476 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
477 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
478
479 addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
480 addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
481 addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
482 addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
483
484 runTests();
485}
486
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000487BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700488BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700489{
490 FaceUri uri;
491
492 BOOST_CHECK(uri.parse("dev://eth0"));
493 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
494 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
495 BOOST_CHECK_EQUAL(uri.getPort(), "");
496 BOOST_CHECK_EQUAL(uri.getPath(), "");
497
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000498 BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896
499
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700500 std::string ifname = "en1";
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000501 BOOST_REQUIRE_NO_THROW(uri = FaceUri::fromDev(ifname));
502 BOOST_CHECK_EQUAL(uri.toString(), "dev://en1");
503}
504
505BOOST_AUTO_TEST_CASE(IsCanonicalDev)
506{
507 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), true);
508
509 BOOST_CHECK_EQUAL(FaceUri("dev://eth0").isCanonical(), true);
510 BOOST_CHECK_EQUAL(FaceUri("dev://").isCanonical(), false);
511 BOOST_CHECK_EQUAL(FaceUri("dev://eth0:8888").isCanonical(), false);
512 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/").isCanonical(), false);
513 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/A").isCanonical(), false);
514}
515
516BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture)
517{
518 addTest("dev://eth0", true, "dev://eth0");
519 addTest("dev://", false, "");
520 addTest("dev://eth0:8888", false, "");
521 addTest("dev://eth0/", true, "dev://eth0");
522 addTest("dev://eth0/A", false, "");
523
524 runTests();
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700525}
526
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700527BOOST_AUTO_TEST_CASE(ParseUdpDev)
528{
529 FaceUri uri;
530
531 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
532 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
533 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
534 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
535 BOOST_CHECK_EQUAL(uri.getPath(), "");
536
537 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
538 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
539 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
540 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
541 BOOST_CHECK_EQUAL(uri.getPath(), "");
542
543 BOOST_CHECK(uri.parse("abc+efg://eth0"));
544 BOOST_CHECK(!uri.parse("abc+://eth0"));
545 BOOST_CHECK(!uri.parse("+abc://eth0"));
546
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500547 namespace ip = boost::asio::ip;
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700548
549 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
550 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1"));
551 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777");
552
553 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
554 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2"));
555 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777");
556}
557
558BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
559{
560 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
561 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
562 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
563 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
564
565 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
566 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
567 addTest("udp+dev://en1:7777", false, "");
568 addTest("udp6+dev://en2", false, "");
569}
570
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700571BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
572{
573 boost::asio::io_service io;
574
575 // unsupported scheme
576 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
577 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500578 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700579
580 // cannot resolve
581 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
582 FaceUri::CanonizeFailureCallback(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500583 io, 1_ms);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700584
585 // already canonical
586 FaceUri("udp4://192.0.2.1:6363").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 // need DNS resolution
591 FaceUri("udp://192.0.2.1:6363").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 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100596
597 // avoid "test case [...] did not check any assertions" message from Boost.Test
598 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700599}
600
601BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
602{
603 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
604 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
605 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
606 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700607
608 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
609 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
610 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
611 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700612
613 addTest("internal://", false, "");
614 addTest("null://", false, "");
615 addTest("unix:///var/run/nfd.sock", false, "");
616 addTest("fd://0", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700617
618 runTests();
619}
620
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700621BOOST_AUTO_TEST_CASE(Bug1635)
622{
623 FaceUri uri;
624
625 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
626 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
627 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
628 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
629 BOOST_CHECK_EQUAL(uri.getPath(), "");
630 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
631}
632
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100633BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
Junxiao Shi25467942017-06-30 02:53:14 +0000634BOOST_AUTO_TEST_SUITE_END() // Net
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700635
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800636} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700637} // namespace ndn