blob: 0b9a61a81a5a3e6ab08c0c40226615233eb4bfaa [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);
184 BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false);
185 BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false);
186 BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false);
187 BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800188}
189
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500190BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800191BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
192{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100193 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700194
195 // IPv4 unicast
196 addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363");
197 addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363");
198 addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363");
199 addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363");
200 addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695");
201 addTest("udp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500202 addTest("udp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700203 addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500204 addTest("udp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700205 addTest("udp4://invalid.invalid", false, "");
206
207 // IPv4 multicast
208 addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
209 addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
210 addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
211
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800212 runTests();
213}
214
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500215BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800216BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
217{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100218 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800219
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700220 // IPv6 unicast
221 addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500222 addTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700223 addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
224 addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500225 addTest("udp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500226 addTest("udp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700227 addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500228 addTest("udp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700229 addTest("udp6://invalid.invalid", false, "");
230 addTest("udp://invalid.invalid", false, "");
231
232 // IPv6 multicast
233 addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
234 addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
235
236 runTests();
237}
238
239BOOST_AUTO_TEST_CASE(ParseTcp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700240{
241 FaceUri uri;
242
243 BOOST_CHECK(uri.parse("tcp://random.host.name"));
244 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
245 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
246 BOOST_CHECK_EQUAL(uri.getPort(), "");
247 BOOST_CHECK_EQUAL(uri.getPath(), "");
248
249 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
250 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
251
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500252 namespace ip = boost::asio::ip;
253
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700254 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
255 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
256 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
257
258 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient"));
259 BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777");
260
261 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
262 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
263 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
264}
265
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500266BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700267{
268 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
269 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
270 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
271
272 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
273 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
274 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
275 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
276 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
277 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
278 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
279 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
280 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
281 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800282}
283
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500284BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800285BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
286{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100287 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700288
289 // IPv4 unicast
290 addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
291 addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
292 addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
293 addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
294 addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
295 addTest("tcp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500296 addTest("tcp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700297 addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500298 addTest("tcp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700299 addTest("tcp4://invalid.invalid", false, "");
300
301 // IPv4 multicast
302 addTest("tcp4://224.0.23.170:56363", false, "");
303 addTest("tcp4://224.0.23.170", false, "");
304 addTest("tcp4://all-routers.mcast.net:56363", false, "");
305
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800306 runTests();
307}
308
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500309BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800310BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
311{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100312 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800313
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700314 // IPv6 unicast
315 addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500316 addTest("tcp6://[2001:db8::1]", true, "tcp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700317 addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
318 addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500319 addTest("tcp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500320 addTest("tcp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700321 addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500322 addTest("tcp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700323 addTest("tcp6://invalid.invalid", false, "");
324 addTest("tcp://invalid.invalid", false, "");
325
326 // IPv6 multicast
327 addTest("tcp6://[ff02::2]:56363", false, "");
328 addTest("tcp6://[ff02::2]", false, "");
329
330 runTests();
331}
332
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500333BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700334BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700335{
336 FaceUri uri;
337
338 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
339 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
340 BOOST_CHECK_EQUAL(uri.getHost(), "");
341 BOOST_CHECK_EQUAL(uri.getPort(), "");
342 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
343
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500344 // This is not a valid unix:// URI, but the parse would treat "var" as host
345 BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); // Bug #3896
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700346
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500347#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
348 using boost::asio::local::stream_protocol;
349 stream_protocol::endpoint endpoint("/var/run/example.sock");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700350 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
351 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500352#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700353}
354
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700355BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700356{
357 FaceUri uri;
358
359 BOOST_CHECK(uri.parse("fd://6"));
360 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
361 BOOST_CHECK_EQUAL(uri.getHost(), "6");
362 BOOST_CHECK_EQUAL(uri.getPort(), "");
363 BOOST_CHECK_EQUAL(uri.getPath(), "");
364
365 int fd = 21;
366 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
367 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
368}
369
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700370BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700371{
372 FaceUri uri;
373
374 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
375 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
376 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
377 BOOST_CHECK_EQUAL(uri.getPort(), "");
378 BOOST_CHECK_EQUAL(uri.getPath(), "");
379
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700380 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700381
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500382 auto address = ethernet::Address::fromString("33:33:01:01:01:01");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700383 BOOST_REQUIRE_NO_THROW(FaceUri(address));
384 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700385}
386
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700387BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
388{
389 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
390
391 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
392 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
393 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
394 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
395
396 addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
397 addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
398 addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
399 addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
400
401 runTests();
402}
403
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000404BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700405BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700406{
407 FaceUri uri;
408
409 BOOST_CHECK(uri.parse("dev://eth0"));
410 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
411 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
412 BOOST_CHECK_EQUAL(uri.getPort(), "");
413 BOOST_CHECK_EQUAL(uri.getPath(), "");
414
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000415 BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896
416
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700417 std::string ifname = "en1";
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000418 BOOST_REQUIRE_NO_THROW(uri = FaceUri::fromDev(ifname));
419 BOOST_CHECK_EQUAL(uri.toString(), "dev://en1");
420}
421
422BOOST_AUTO_TEST_CASE(IsCanonicalDev)
423{
424 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), true);
425
426 BOOST_CHECK_EQUAL(FaceUri("dev://eth0").isCanonical(), true);
427 BOOST_CHECK_EQUAL(FaceUri("dev://").isCanonical(), false);
428 BOOST_CHECK_EQUAL(FaceUri("dev://eth0:8888").isCanonical(), false);
429 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/").isCanonical(), false);
430 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/A").isCanonical(), false);
431}
432
433BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture)
434{
435 addTest("dev://eth0", true, "dev://eth0");
436 addTest("dev://", false, "");
437 addTest("dev://eth0:8888", false, "");
438 addTest("dev://eth0/", true, "dev://eth0");
439 addTest("dev://eth0/A", false, "");
440
441 runTests();
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700442}
443
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700444BOOST_AUTO_TEST_CASE(ParseUdpDev)
445{
446 FaceUri uri;
447
448 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
449 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
450 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
451 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
452 BOOST_CHECK_EQUAL(uri.getPath(), "");
453
454 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
455 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
456 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
457 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
458 BOOST_CHECK_EQUAL(uri.getPath(), "");
459
460 BOOST_CHECK(uri.parse("abc+efg://eth0"));
461 BOOST_CHECK(!uri.parse("abc+://eth0"));
462 BOOST_CHECK(!uri.parse("+abc://eth0"));
463
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500464 namespace ip = boost::asio::ip;
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700465
466 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
467 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1"));
468 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777");
469
470 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
471 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2"));
472 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777");
473}
474
475BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
476{
477 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
478 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
479 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
480 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
481
482 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
483 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
484 addTest("udp+dev://en1:7777", false, "");
485 addTest("udp6+dev://en2", false, "");
486}
487
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700488BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
489{
490 boost::asio::io_service io;
491
492 // unsupported scheme
493 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
494 FaceUri::CanonizeFailureCallback(),
495 io, time::milliseconds(1));
496
497 // cannot resolve
498 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
499 FaceUri::CanonizeFailureCallback(),
500 io, time::milliseconds(1));
501
502 // already canonical
503 FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
504 FaceUri::CanonizeFailureCallback(),
505 io, time::milliseconds(1));
506
507 // need DNS resolution
508 FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
509 FaceUri::CanonizeFailureCallback(),
510 io, time::milliseconds(1));
511
512 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100513
514 // avoid "test case [...] did not check any assertions" message from Boost.Test
515 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700516}
517
518BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
519{
520 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
521 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
522 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
523 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700524
525 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
526 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
527 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
528 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700529
530 addTest("internal://", false, "");
531 addTest("null://", false, "");
532 addTest("unix:///var/run/nfd.sock", false, "");
533 addTest("fd://0", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700534
535 runTests();
536}
537
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700538BOOST_AUTO_TEST_CASE(Bug1635)
539{
540 FaceUri uri;
541
542 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
543 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
544 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
545 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
546 BOOST_CHECK_EQUAL(uri.getPath(), "");
547 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
548}
549
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100550BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
551BOOST_AUTO_TEST_SUITE_END() // Util
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700552
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800553} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700554} // namespace util
555} // namespace ndn