blob: 74c67845ba0c68d4ed4977b9244beff6af9f77d7 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoe1081cd2016-12-19 23:58:15 -05003 * Copyright (c) 2014-2016, 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
404BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700405{
406 FaceUri uri;
407
408 BOOST_CHECK(uri.parse("dev://eth0"));
409 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
410 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
411 BOOST_CHECK_EQUAL(uri.getPort(), "");
412 BOOST_CHECK_EQUAL(uri.getPath(), "");
413
414 std::string ifname = "en1";
415 BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname));
416 BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1");
417}
418
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700419BOOST_AUTO_TEST_CASE(ParseUdpDev)
420{
421 FaceUri uri;
422
423 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
424 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
425 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
426 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
427 BOOST_CHECK_EQUAL(uri.getPath(), "");
428
429 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
430 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
431 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
432 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
433 BOOST_CHECK_EQUAL(uri.getPath(), "");
434
435 BOOST_CHECK(uri.parse("abc+efg://eth0"));
436 BOOST_CHECK(!uri.parse("abc+://eth0"));
437 BOOST_CHECK(!uri.parse("+abc://eth0"));
438
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500439 namespace ip = boost::asio::ip;
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700440
441 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
442 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1"));
443 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777");
444
445 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
446 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2"));
447 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777");
448}
449
450BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
451{
452 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
453 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
454 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
455 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
456
457 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
458 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
459 addTest("udp+dev://en1:7777", false, "");
460 addTest("udp6+dev://en2", false, "");
461}
462
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700463BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
464{
465 boost::asio::io_service io;
466
467 // unsupported scheme
468 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
469 FaceUri::CanonizeFailureCallback(),
470 io, time::milliseconds(1));
471
472 // cannot resolve
473 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
474 FaceUri::CanonizeFailureCallback(),
475 io, time::milliseconds(1));
476
477 // already canonical
478 FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
479 FaceUri::CanonizeFailureCallback(),
480 io, time::milliseconds(1));
481
482 // need DNS resolution
483 FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
484 FaceUri::CanonizeFailureCallback(),
485 io, time::milliseconds(1));
486
487 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100488
489 // avoid "test case [...] did not check any assertions" message from Boost.Test
490 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700491}
492
493BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
494{
495 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
496 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
497 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
498 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
499 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), false);
500
501 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
502 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
503 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
504 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
505 BOOST_CHECK_EQUAL(FaceUri("dev://eth1").isCanonical(), false);
506
507 addTest("internal://", false, "");
508 addTest("null://", false, "");
509 addTest("unix:///var/run/nfd.sock", false, "");
510 addTest("fd://0", false, "");
511 addTest("dev://eth1", false, "");
512
513 runTests();
514}
515
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700516BOOST_AUTO_TEST_CASE(Bug1635)
517{
518 FaceUri uri;
519
520 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
521 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
522 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
523 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
524 BOOST_CHECK_EQUAL(uri.getPath(), "");
525 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
526}
527
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100528BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
529BOOST_AUTO_TEST_SUITE_END() // Util
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700530
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800531} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700532} // namespace util
533} // namespace ndn