blob: b63c2de1b4fc2d8fdba0f22468910d8087698a28 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Weiwei Liud7f4fda2016-10-19 22:38:39 -07003 * Copyright (c) 2013-2016 Regents of the University of California,
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08004 * 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"
29
30#include "boost-test.hpp"
Alexander Afanasyev1286e022015-01-26 10:42:29 -080031#include "../network-configuration-detector.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070032
33namespace ndn {
34namespace util {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080035namespace tests {
36
37using ndn::tests::NetworkConfigurationDetector;
Junxiao Shi77dcadd2014-10-05 14:40:54 -070038
Davide Pesaventoeee3e822016-11-26 19:19:34 +010039BOOST_AUTO_TEST_SUITE(Util)
40BOOST_AUTO_TEST_SUITE(TestFaceUri)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070041
Junxiao Shi4083c8d2014-10-12 16:43:16 -070042class CanonizeFixture : noncopyable
43{
44protected:
45 CanonizeFixture()
46 : m_nPending(0)
47 {
48 }
49
50 void
51 addTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri);
52
53 void
54 runTests()
55 {
56 m_io.run();
57 BOOST_CHECK_EQUAL(m_nPending, 0);
58 }
59
60private:
Davide Pesavento50df5622015-03-19 22:05:05 +010061 class CanonizeTestCase
Junxiao Shi4083c8d2014-10-12 16:43:16 -070062 {
63 public:
64 CanonizeTestCase(const std::string& request,
65 bool shouldSucceed, const std::string& expectedUri)
Davide Pesavento50df5622015-03-19 22:05:05 +010066 : m_shouldSucceed(shouldSucceed)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070067 , m_expectedUri(expectedUri)
Davide Pesavento50df5622015-03-19 22:05:05 +010068 , m_message(request + " should " + (shouldSucceed ? "succeed" : "fail"))
Junxiao Shi4083c8d2014-10-12 16:43:16 -070069 , m_isCompleted(false)
70 {
71 }
72
73 public:
Junxiao Shi4083c8d2014-10-12 16:43:16 -070074 bool m_shouldSucceed;
75 std::string m_expectedUri;
Davide Pesavento50df5622015-03-19 22:05:05 +010076 std::string m_message;
Junxiao Shi4083c8d2014-10-12 16:43:16 -070077 bool m_isCompleted;
78 };
79
80 // tc is a shared_ptr passed by value, because this function can take ownership
81 void
82 onCanonizeSuccess(shared_ptr<CanonizeTestCase> tc, const FaceUri& canonicalUri)
83 {
84 BOOST_CHECK(!tc->m_isCompleted);
85 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);
Junxiao Shi4083c8d2014-10-12 16:43:16 -070089 BOOST_CHECK_EQUAL(tc->m_expectedUri, canonicalUri.toString());
90 }
91
92 // tc is a shared_ptr passed by value, because this function can take ownership
93 void
94 onCanonizeFailure(shared_ptr<CanonizeTestCase> tc, const std::string& reason)
95 {
96 BOOST_CHECK(!tc->m_isCompleted);
97 tc->m_isCompleted = true;
98 --m_nPending;
99
Davide Pesavento50df5622015-03-19 22:05:05 +0100100 BOOST_CHECK_MESSAGE(!tc->m_shouldSucceed, tc->m_message);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700101 }
102
103private:
104 boost::asio::io_service m_io;
105 ssize_t m_nPending;
106};
107
108void
109CanonizeFixture::addTest(const std::string& request,
110 bool shouldSucceed, const std::string& expectedUri)
111{
112 ++m_nPending;
Davide Pesavento50df5622015-03-19 22:05:05 +0100113 auto tc = make_shared<CanonizeTestCase>(request, shouldSucceed, expectedUri);
114
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700115 FaceUri uri(request);
116 uri.canonize(bind(&CanonizeFixture::onCanonizeSuccess, this, tc, _1),
117 bind(&CanonizeFixture::onCanonizeFailure, this, tc, _1),
Davide Pesavento50df5622015-03-19 22:05:05 +0100118 m_io, time::seconds(10));
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700119}
120
121BOOST_AUTO_TEST_CASE(ParseInternal)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700122{
123 FaceUri uri;
124
125 BOOST_CHECK(uri.parse("internal://"));
126 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
127 BOOST_CHECK_EQUAL(uri.getHost(), "");
128 BOOST_CHECK_EQUAL(uri.getPort(), "");
129 BOOST_CHECK_EQUAL(uri.getPath(), "");
130
131 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
132 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
133}
134
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700135BOOST_AUTO_TEST_CASE(ParseUdp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700136{
137 BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
138 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
139 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
140
141 FaceUri uri;
142 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
143
144 BOOST_CHECK(uri.parse("udp://hostname:80"));
145 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
146 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
147 BOOST_CHECK_EQUAL(uri.getPort(), "80");
148 BOOST_CHECK_EQUAL(uri.getPath(), "");
149
150 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
151 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
152 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
153 BOOST_CHECK_EQUAL(uri.getPort(), "20");
154 BOOST_CHECK_EQUAL(uri.getPath(), "");
155
156 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
157 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
158 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
159 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
160 BOOST_CHECK_EQUAL(uri.getPath(), "");
161
162 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
163 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
164 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
165 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
166 BOOST_CHECK_EQUAL(uri.getPath(), "");
167
168 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
169
170 using namespace boost::asio;
171 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
172 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
173 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777");
174
175 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
176 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
177 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
178}
179
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800180BOOST_FIXTURE_TEST_CASE(CheckCanonicalUdp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700181{
182 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true);
183 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true);
184 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true);
185
186 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true);
187 BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false);
188 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false);
189 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false);
190 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true);
191 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false);
192 BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false);
193 BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false);
194 BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false);
195 BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800196}
197
198BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
199{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100200 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700201
202 // IPv4 unicast
203 addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363");
204 addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363");
205 addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363");
206 addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363");
207 addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695");
208 addTest("udp4://192.0.2.666:6363", false, "");
209 addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500210 addTest("udp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700211 addTest("udp4://invalid.invalid", false, "");
212
213 // IPv4 multicast
214 addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
215 addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
216 addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
217
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800218 runTests();
219}
220
221BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
222{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100223 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800224
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700225 // IPv6 unicast
226 addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500227 addTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700228 addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
229 addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500230 addTest("udp6://[2001::db8::1]:6363", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700231 addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500232 addTest("udp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700233 addTest("udp6://invalid.invalid", false, "");
234 addTest("udp://invalid.invalid", false, "");
235
236 // IPv6 multicast
237 addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
238 addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
239
240 runTests();
241}
242
243BOOST_AUTO_TEST_CASE(ParseTcp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700244{
245 FaceUri uri;
246
247 BOOST_CHECK(uri.parse("tcp://random.host.name"));
248 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
249 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
250 BOOST_CHECK_EQUAL(uri.getPort(), "");
251 BOOST_CHECK_EQUAL(uri.getPath(), "");
252
253 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
254 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
255
256 using namespace boost::asio;
257 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
258 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
259 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
260
261 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient"));
262 BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777");
263
264 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
265 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
266 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
267}
268
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800269BOOST_FIXTURE_TEST_CASE(CheckCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700270{
271 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
272 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
273 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
274
275 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
276 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
277 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
278 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
279 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
280 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
281 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
282 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
283 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
284 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800285}
286
287BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
288{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100289 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700290
291 // IPv4 unicast
292 addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
293 addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
294 addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
295 addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
296 addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
297 addTest("tcp4://192.0.2.666:6363", false, "");
298 addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500299 addTest("tcp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700300 addTest("tcp4://invalid.invalid", false, "");
301
302 // IPv4 multicast
303 addTest("tcp4://224.0.23.170:56363", false, "");
304 addTest("tcp4://224.0.23.170", false, "");
305 addTest("tcp4://all-routers.mcast.net:56363", false, "");
306
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800307 runTests();
308}
309
310BOOST_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, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700320 addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500321 addTest("tcp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700322 addTest("tcp6://invalid.invalid", false, "");
323 addTest("tcp://invalid.invalid", false, "");
324
325 // IPv6 multicast
326 addTest("tcp6://[ff02::2]:56363", false, "");
327 addTest("tcp6://[ff02::2]", false, "");
328
329 runTests();
330}
331
332BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700333{
334 FaceUri uri;
335
336 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
337 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
338 BOOST_CHECK_EQUAL(uri.getHost(), "");
339 BOOST_CHECK_EQUAL(uri.getPort(), "");
340 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
341
342 //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false);
343 // This is not a valid unix:/ URI, but the parse would treat "var" as host
344
345#ifdef HAVE_UNIX_SOCKETS
346 using namespace boost::asio;
347 local::stream_protocol::endpoint endpoint("/var/run/example.sock");
348 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
349 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
350#endif // HAVE_UNIX_SOCKETS
351}
352
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700353BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700354{
355 FaceUri uri;
356
357 BOOST_CHECK(uri.parse("fd://6"));
358 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
359 BOOST_CHECK_EQUAL(uri.getHost(), "6");
360 BOOST_CHECK_EQUAL(uri.getPort(), "");
361 BOOST_CHECK_EQUAL(uri.getPath(), "");
362
363 int fd = 21;
364 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
365 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
366}
367
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700368BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700369{
370 FaceUri uri;
371
372 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
373 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
374 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
375 BOOST_CHECK_EQUAL(uri.getPort(), "");
376 BOOST_CHECK_EQUAL(uri.getPath(), "");
377
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700378 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700379
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700380 ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01");
381 BOOST_REQUIRE_NO_THROW(FaceUri(address));
382 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700383}
384
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700385BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
386{
387 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
388
389 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
390 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
391 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
392 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
393
394 addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
395 addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
396 addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
397 addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
398
399 runTests();
400}
401
402BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700403{
404 FaceUri uri;
405
406 BOOST_CHECK(uri.parse("dev://eth0"));
407 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
408 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
409 BOOST_CHECK_EQUAL(uri.getPort(), "");
410 BOOST_CHECK_EQUAL(uri.getPath(), "");
411
412 std::string ifname = "en1";
413 BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname));
414 BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1");
415}
416
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700417BOOST_AUTO_TEST_CASE(ParseUdpDev)
418{
419 FaceUri uri;
420
421 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
422 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
423 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
424 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
425 BOOST_CHECK_EQUAL(uri.getPath(), "");
426
427 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
428 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
429 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
430 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
431 BOOST_CHECK_EQUAL(uri.getPath(), "");
432
433 BOOST_CHECK(uri.parse("abc+efg://eth0"));
434 BOOST_CHECK(!uri.parse("abc+://eth0"));
435 BOOST_CHECK(!uri.parse("+abc://eth0"));
436
437 using namespace boost::asio;
438
439 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
440 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1"));
441 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777");
442
443 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
444 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2"));
445 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777");
446}
447
448BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
449{
450 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
451 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
452 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
453 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
454
455 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
456 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
457 addTest("udp+dev://en1:7777", false, "");
458 addTest("udp6+dev://en2", false, "");
459}
460
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700461BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
462{
463 boost::asio::io_service io;
464
465 // unsupported scheme
466 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
467 FaceUri::CanonizeFailureCallback(),
468 io, time::milliseconds(1));
469
470 // cannot resolve
471 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
472 FaceUri::CanonizeFailureCallback(),
473 io, time::milliseconds(1));
474
475 // already canonical
476 FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
477 FaceUri::CanonizeFailureCallback(),
478 io, time::milliseconds(1));
479
480 // need DNS resolution
481 FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
482 FaceUri::CanonizeFailureCallback(),
483 io, time::milliseconds(1));
484
485 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100486
487 // avoid "test case [...] did not check any assertions" message from Boost.Test
488 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700489}
490
491BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
492{
493 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
494 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
495 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
496 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
497 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), false);
498
499 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
500 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
501 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
502 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
503 BOOST_CHECK_EQUAL(FaceUri("dev://eth1").isCanonical(), false);
504
505 addTest("internal://", false, "");
506 addTest("null://", false, "");
507 addTest("unix:///var/run/nfd.sock", false, "");
508 addTest("fd://0", false, "");
509 addTest("dev://eth1", false, "");
510
511 runTests();
512}
513
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700514BOOST_AUTO_TEST_CASE(Bug1635)
515{
516 FaceUri uri;
517
518 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
519 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
520 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
521 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
522 BOOST_CHECK_EQUAL(uri.getPath(), "");
523 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
524}
525
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100526BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
527BOOST_AUTO_TEST_SUITE_END() // Util
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700528
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800529} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700530} // namespace util
531} // namespace ndn