blob: 559a5b0276c4d67cfedf45453f65e22b8c8b9566 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shid5c2f0c2017-04-04 09:52:11 +00003 * Copyright (c) 2013-2017 Regents of the University of California,
4 * 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
28#include "util/face-uri.hpp"
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050029#include "util/ethernet.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070030
31#include "boost-test.hpp"
Alexander Afanasyev1286e022015-01-26 10:42:29 -080032#include "../network-configuration-detector.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070033
34namespace ndn {
35namespace util {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080036namespace tests {
37
38using ndn::tests::NetworkConfigurationDetector;
Junxiao Shi77dcadd2014-10-05 14:40:54 -070039
Davide Pesaventoeee3e822016-11-26 19:19:34 +010040BOOST_AUTO_TEST_SUITE(Util)
41BOOST_AUTO_TEST_SUITE(TestFaceUri)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070042
Junxiao Shi4083c8d2014-10-12 16:43:16 -070043class CanonizeFixture : noncopyable
44{
45protected:
Junxiao Shi4083c8d2014-10-12 16:43:16 -070046 void
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050047 addTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri)
48 {
49 ++m_nPending;
50 auto tc = make_shared<CanonizeTestCase>(request, shouldSucceed, expectedUri);
51
52 FaceUri uri(request);
53 uri.canonize(bind(&CanonizeFixture::onCanonizeSuccess, this, tc, _1),
54 bind(&CanonizeFixture::onCanonizeFailure, this, tc, _1),
55 m_io, time::seconds(10));
56 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -070057
58 void
59 runTests()
60 {
61 m_io.run();
62 BOOST_CHECK_EQUAL(m_nPending, 0);
63 }
64
65private:
Davide Pesavento50df5622015-03-19 22:05:05 +010066 class CanonizeTestCase
Junxiao Shi4083c8d2014-10-12 16:43:16 -070067 {
68 public:
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050069 CanonizeTestCase(const std::string& request, bool shouldSucceed, const std::string& expectedUri)
70 : m_expectedUri(expectedUri)
Davide Pesavento50df5622015-03-19 22:05:05 +010071 , m_message(request + " should " + (shouldSucceed ? "succeed" : "fail"))
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050072 , m_shouldSucceed(shouldSucceed)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070073 , m_isCompleted(false)
74 {
75 }
76
77 public:
Junxiao Shi4083c8d2014-10-12 16:43:16 -070078 std::string m_expectedUri;
Davide Pesavento50df5622015-03-19 22:05:05 +010079 std::string m_message;
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050080 bool m_shouldSucceed;
Junxiao Shi4083c8d2014-10-12 16:43:16 -070081 bool m_isCompleted;
82 };
83
Junxiao Shi4083c8d2014-10-12 16:43:16 -070084 void
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050085 onCanonizeSuccess(const shared_ptr<CanonizeTestCase>& tc, const FaceUri& canonicalUri)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070086 {
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050087 BOOST_CHECK_EQUAL(tc->m_isCompleted, false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -070088 tc->m_isCompleted = true;
89 --m_nPending;
90
Davide Pesavento50df5622015-03-19 22:05:05 +010091 BOOST_CHECK_MESSAGE(tc->m_shouldSucceed, tc->m_message);
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050092 if (tc->m_shouldSucceed) {
93 BOOST_CHECK_EQUAL(canonicalUri.toString(), tc->m_expectedUri);
94 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -070095 }
96
Junxiao Shi4083c8d2014-10-12 16:43:16 -070097 void
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050098 onCanonizeFailure(const shared_ptr<CanonizeTestCase>& tc, const std::string& reason)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070099 {
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500100 BOOST_CHECK_EQUAL(tc->m_isCompleted, false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700101 tc->m_isCompleted = true;
102 --m_nPending;
103
Davide Pesavento50df5622015-03-19 22:05:05 +0100104 BOOST_CHECK_MESSAGE(!tc->m_shouldSucceed, tc->m_message);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700105 }
106
107private:
108 boost::asio::io_service m_io;
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500109 ssize_t m_nPending = 0;
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700110};
111
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700112BOOST_AUTO_TEST_CASE(ParseInternal)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700113{
114 FaceUri uri;
115
116 BOOST_CHECK(uri.parse("internal://"));
117 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
118 BOOST_CHECK_EQUAL(uri.getHost(), "");
119 BOOST_CHECK_EQUAL(uri.getPort(), "");
120 BOOST_CHECK_EQUAL(uri.getPath(), "");
121
122 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
123 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
124}
125
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700126BOOST_AUTO_TEST_CASE(ParseUdp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700127{
128 BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
129 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
130 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
131
132 FaceUri uri;
133 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
134
135 BOOST_CHECK(uri.parse("udp://hostname:80"));
136 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
137 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
138 BOOST_CHECK_EQUAL(uri.getPort(), "80");
139 BOOST_CHECK_EQUAL(uri.getPath(), "");
140
141 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
142 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
143 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
144 BOOST_CHECK_EQUAL(uri.getPort(), "20");
145 BOOST_CHECK_EQUAL(uri.getPath(), "");
146
147 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
148 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
149 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
150 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
151 BOOST_CHECK_EQUAL(uri.getPath(), "");
152
153 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
154 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
155 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
156 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
157 BOOST_CHECK_EQUAL(uri.getPath(), "");
158
159 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
160
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500161 namespace ip = boost::asio::ip;
162
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700163 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
164 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
165 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777");
166
167 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
168 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
169 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
170}
171
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500172BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700173{
174 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true);
175 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true);
176 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true);
177
178 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true);
179 BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false);
180 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false);
181 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false);
182 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true);
183 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700184 BOOST_CHECK_EQUAL(FaceUri("udp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700185 BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false);
186 BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false);
187 BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false);
188 BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
Eric Newberry71aecae2017-05-29 00:22:39 -0700189 BOOST_CHECK_EQUAL(FaceUri("udp4://[2001:db8::1]:6363").isCanonical(), false);
190 BOOST_CHECK_EQUAL(FaceUri("udp6://192.0.2.1:6363").isCanonical(), false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800191}
192
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500193BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800194BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
195{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100196 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700197
198 // IPv4 unicast
199 addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363");
200 addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363");
201 addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363");
202 addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363");
203 addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695");
204 addTest("udp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500205 addTest("udp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700206 addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500207 addTest("udp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700208 addTest("udp4://invalid.invalid", false, "");
209
210 // IPv4 multicast
211 addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
212 addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
213 addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
214
Eric Newberry71aecae2017-05-29 00:22:39 -0700215 // IPv6 used with udp4 protocol - not canonical
216 addTest("udp4://[2001:db8::1]:6363", false, "");
217
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800218 runTests();
219}
220
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500221BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800222BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
223{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100224 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800225
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700226 // IPv6 unicast
227 addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500228 addTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700229 addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
230 addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500231 addTest("udp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500232 addTest("udp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700233 addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500234 addTest("udp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700235 addTest("udp6://invalid.invalid", false, "");
236 addTest("udp://invalid.invalid", false, "");
237
238 // IPv6 multicast
239 addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
240 addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
241
Eric Newberry71aecae2017-05-29 00:22:39 -0700242 // IPv4 used with udp6 protocol - not canonical
243 addTest("udp6://192.0.2.1:6363", false, "");
244
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700245 runTests();
246}
247
248BOOST_AUTO_TEST_CASE(ParseTcp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700249{
250 FaceUri uri;
251
252 BOOST_CHECK(uri.parse("tcp://random.host.name"));
253 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
254 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
255 BOOST_CHECK_EQUAL(uri.getPort(), "");
256 BOOST_CHECK_EQUAL(uri.getPath(), "");
257
258 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
259 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
260
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500261 namespace ip = boost::asio::ip;
262
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700263 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
264 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
265 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
266
267 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient"));
268 BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777");
269
270 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
271 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
272 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
273}
274
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500275BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700276{
277 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
278 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
279 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
280
281 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
282 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
283 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
284 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
285 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
286 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700287 BOOST_CHECK_EQUAL(FaceUri("tcp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700288 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
289 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
290 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
291 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700292 BOOST_CHECK_EQUAL(FaceUri("tcp4://[2001:db8::1]:6363").isCanonical(), false);
293 BOOST_CHECK_EQUAL(FaceUri("tcp6://192.0.2.1:6363").isCanonical(), false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800294}
295
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500296BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800297BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
298{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100299 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700300
301 // IPv4 unicast
302 addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
303 addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
304 addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
305 addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
306 addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
307 addTest("tcp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500308 addTest("tcp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700309 addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500310 addTest("tcp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700311 addTest("tcp4://invalid.invalid", false, "");
312
313 // IPv4 multicast
314 addTest("tcp4://224.0.23.170:56363", false, "");
315 addTest("tcp4://224.0.23.170", false, "");
316 addTest("tcp4://all-routers.mcast.net:56363", false, "");
317
Eric Newberry71aecae2017-05-29 00:22:39 -0700318 // IPv6 used with tcp4 protocol - not canonical
319 addTest("tcp4://[2001:db8::1]:6363", false, "");
320
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800321 runTests();
322}
323
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500324BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800325BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
326{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100327 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800328
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700329 // IPv6 unicast
330 addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500331 addTest("tcp6://[2001:db8::1]", true, "tcp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700332 addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
333 addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500334 addTest("tcp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500335 addTest("tcp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700336 addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500337 addTest("tcp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700338 addTest("tcp6://invalid.invalid", false, "");
339 addTest("tcp://invalid.invalid", false, "");
340
341 // IPv6 multicast
342 addTest("tcp6://[ff02::2]:56363", false, "");
343 addTest("tcp6://[ff02::2]", false, "");
344
Eric Newberry71aecae2017-05-29 00:22:39 -0700345 // IPv4 used with tcp6 protocol - not canonical
346 addTest("tcp6://192.0.2.1:6363", false, "");
347
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700348 runTests();
349}
350
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500351BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700352BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700353{
354 FaceUri uri;
355
356 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
357 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
358 BOOST_CHECK_EQUAL(uri.getHost(), "");
359 BOOST_CHECK_EQUAL(uri.getPort(), "");
360 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
361
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500362 // This is not a valid unix:// URI, but the parse would treat "var" as host
363 BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); // Bug #3896
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700364
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500365#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
366 using boost::asio::local::stream_protocol;
367 stream_protocol::endpoint endpoint("/var/run/example.sock");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700368 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
369 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500370#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700371}
372
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700373BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700374{
375 FaceUri uri;
376
377 BOOST_CHECK(uri.parse("fd://6"));
378 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
379 BOOST_CHECK_EQUAL(uri.getHost(), "6");
380 BOOST_CHECK_EQUAL(uri.getPort(), "");
381 BOOST_CHECK_EQUAL(uri.getPath(), "");
382
383 int fd = 21;
384 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
385 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
386}
387
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700388BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700389{
390 FaceUri uri;
391
392 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
393 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
394 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
395 BOOST_CHECK_EQUAL(uri.getPort(), "");
396 BOOST_CHECK_EQUAL(uri.getPath(), "");
397
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700398 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700399
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500400 auto address = ethernet::Address::fromString("33:33:01:01:01:01");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700401 BOOST_REQUIRE_NO_THROW(FaceUri(address));
402 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700403}
404
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700405BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
406{
407 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
408
409 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
410 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
411 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
412 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
413
414 addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
415 addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
416 addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
417 addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
418
419 runTests();
420}
421
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000422BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700423BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700424{
425 FaceUri uri;
426
427 BOOST_CHECK(uri.parse("dev://eth0"));
428 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
429 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
430 BOOST_CHECK_EQUAL(uri.getPort(), "");
431 BOOST_CHECK_EQUAL(uri.getPath(), "");
432
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000433 BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896
434
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700435 std::string ifname = "en1";
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000436 BOOST_REQUIRE_NO_THROW(uri = FaceUri::fromDev(ifname));
437 BOOST_CHECK_EQUAL(uri.toString(), "dev://en1");
438}
439
440BOOST_AUTO_TEST_CASE(IsCanonicalDev)
441{
442 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), true);
443
444 BOOST_CHECK_EQUAL(FaceUri("dev://eth0").isCanonical(), true);
445 BOOST_CHECK_EQUAL(FaceUri("dev://").isCanonical(), false);
446 BOOST_CHECK_EQUAL(FaceUri("dev://eth0:8888").isCanonical(), false);
447 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/").isCanonical(), false);
448 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/A").isCanonical(), false);
449}
450
451BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture)
452{
453 addTest("dev://eth0", true, "dev://eth0");
454 addTest("dev://", false, "");
455 addTest("dev://eth0:8888", false, "");
456 addTest("dev://eth0/", true, "dev://eth0");
457 addTest("dev://eth0/A", false, "");
458
459 runTests();
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700460}
461
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700462BOOST_AUTO_TEST_CASE(ParseUdpDev)
463{
464 FaceUri uri;
465
466 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
467 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
468 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
469 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
470 BOOST_CHECK_EQUAL(uri.getPath(), "");
471
472 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
473 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
474 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
475 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
476 BOOST_CHECK_EQUAL(uri.getPath(), "");
477
478 BOOST_CHECK(uri.parse("abc+efg://eth0"));
479 BOOST_CHECK(!uri.parse("abc+://eth0"));
480 BOOST_CHECK(!uri.parse("+abc://eth0"));
481
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500482 namespace ip = boost::asio::ip;
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700483
484 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
485 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1"));
486 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777");
487
488 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
489 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2"));
490 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777");
491}
492
493BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
494{
495 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
496 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
497 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
498 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
499
500 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
501 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
502 addTest("udp+dev://en1:7777", false, "");
503 addTest("udp6+dev://en2", false, "");
504}
505
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700506BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
507{
508 boost::asio::io_service io;
509
510 // unsupported scheme
511 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
512 FaceUri::CanonizeFailureCallback(),
513 io, time::milliseconds(1));
514
515 // cannot resolve
516 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
517 FaceUri::CanonizeFailureCallback(),
518 io, time::milliseconds(1));
519
520 // already canonical
521 FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
522 FaceUri::CanonizeFailureCallback(),
523 io, time::milliseconds(1));
524
525 // need DNS resolution
526 FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
527 FaceUri::CanonizeFailureCallback(),
528 io, time::milliseconds(1));
529
530 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100531
532 // avoid "test case [...] did not check any assertions" message from Boost.Test
533 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700534}
535
536BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
537{
538 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
539 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
540 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
541 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700542
543 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
544 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
545 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
546 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700547
548 addTest("internal://", false, "");
549 addTest("null://", false, "");
550 addTest("unix:///var/run/nfd.sock", false, "");
551 addTest("fd://0", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700552
553 runTests();
554}
555
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700556BOOST_AUTO_TEST_CASE(Bug1635)
557{
558 FaceUri uri;
559
560 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
561 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
562 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
563 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
564 BOOST_CHECK_EQUAL(uri.getPath(), "");
565 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
566}
567
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100568BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
569BOOST_AUTO_TEST_SUITE_END() // Util
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700570
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800571} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700572} // namespace util
573} // namespace ndn