blob: b860e2f24f3d5a6408dd61ebcb9b706082f3fb03 [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
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080039BOOST_AUTO_TEST_SUITE(UtilFaceUri)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070040
Junxiao Shi4083c8d2014-10-12 16:43:16 -070041class CanonizeFixture : noncopyable
42{
43protected:
44 CanonizeFixture()
45 : m_nPending(0)
46 {
47 }
48
49 void
50 addTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri);
51
52 void
53 runTests()
54 {
55 m_io.run();
56 BOOST_CHECK_EQUAL(m_nPending, 0);
57 }
58
59private:
Davide Pesavento50df5622015-03-19 22:05:05 +010060 class CanonizeTestCase
Junxiao Shi4083c8d2014-10-12 16:43:16 -070061 {
62 public:
63 CanonizeTestCase(const std::string& request,
64 bool shouldSucceed, const std::string& expectedUri)
Davide Pesavento50df5622015-03-19 22:05:05 +010065 : m_shouldSucceed(shouldSucceed)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070066 , m_expectedUri(expectedUri)
Davide Pesavento50df5622015-03-19 22:05:05 +010067 , m_message(request + " should " + (shouldSucceed ? "succeed" : "fail"))
Junxiao Shi4083c8d2014-10-12 16:43:16 -070068 , m_isCompleted(false)
69 {
70 }
71
72 public:
Junxiao Shi4083c8d2014-10-12 16:43:16 -070073 bool m_shouldSucceed;
74 std::string m_expectedUri;
Davide Pesavento50df5622015-03-19 22:05:05 +010075 std::string m_message;
Junxiao Shi4083c8d2014-10-12 16:43:16 -070076 bool m_isCompleted;
77 };
78
79 // tc is a shared_ptr passed by value, because this function can take ownership
80 void
81 onCanonizeSuccess(shared_ptr<CanonizeTestCase> tc, const FaceUri& canonicalUri)
82 {
83 BOOST_CHECK(!tc->m_isCompleted);
84 tc->m_isCompleted = true;
85 --m_nPending;
86
Davide Pesavento50df5622015-03-19 22:05:05 +010087 BOOST_CHECK_MESSAGE(tc->m_shouldSucceed, tc->m_message);
Junxiao Shi4083c8d2014-10-12 16:43:16 -070088 BOOST_CHECK_EQUAL(tc->m_expectedUri, canonicalUri.toString());
89 }
90
91 // tc is a shared_ptr passed by value, because this function can take ownership
92 void
93 onCanonizeFailure(shared_ptr<CanonizeTestCase> tc, const std::string& reason)
94 {
95 BOOST_CHECK(!tc->m_isCompleted);
96 tc->m_isCompleted = true;
97 --m_nPending;
98
Davide Pesavento50df5622015-03-19 22:05:05 +010099 BOOST_CHECK_MESSAGE(!tc->m_shouldSucceed, tc->m_message);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700100 }
101
102private:
103 boost::asio::io_service m_io;
104 ssize_t m_nPending;
105};
106
107void
108CanonizeFixture::addTest(const std::string& request,
109 bool shouldSucceed, const std::string& expectedUri)
110{
111 ++m_nPending;
Davide Pesavento50df5622015-03-19 22:05:05 +0100112 auto tc = make_shared<CanonizeTestCase>(request, shouldSucceed, expectedUri);
113
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700114 FaceUri uri(request);
115 uri.canonize(bind(&CanonizeFixture::onCanonizeSuccess, this, tc, _1),
116 bind(&CanonizeFixture::onCanonizeFailure, this, tc, _1),
Davide Pesavento50df5622015-03-19 22:05:05 +0100117 m_io, time::seconds(10));
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700118}
119
120BOOST_AUTO_TEST_CASE(ParseInternal)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700121{
122 FaceUri uri;
123
124 BOOST_CHECK(uri.parse("internal://"));
125 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
126 BOOST_CHECK_EQUAL(uri.getHost(), "");
127 BOOST_CHECK_EQUAL(uri.getPort(), "");
128 BOOST_CHECK_EQUAL(uri.getPath(), "");
129
130 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
131 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
132}
133
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700134BOOST_AUTO_TEST_CASE(ParseUdp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700135{
136 BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
137 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
138 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
139
140 FaceUri uri;
141 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
142
143 BOOST_CHECK(uri.parse("udp://hostname:80"));
144 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
145 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
146 BOOST_CHECK_EQUAL(uri.getPort(), "80");
147 BOOST_CHECK_EQUAL(uri.getPath(), "");
148
149 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
150 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
151 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
152 BOOST_CHECK_EQUAL(uri.getPort(), "20");
153 BOOST_CHECK_EQUAL(uri.getPath(), "");
154
155 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
156 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
157 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
158 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
159 BOOST_CHECK_EQUAL(uri.getPath(), "");
160
161 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
162 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
163 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
164 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
165 BOOST_CHECK_EQUAL(uri.getPath(), "");
166
167 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
168
169 using namespace boost::asio;
170 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
171 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
172 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777");
173
174 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
175 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
176 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
177}
178
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800179BOOST_FIXTURE_TEST_CASE(CheckCanonicalUdp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700180{
181 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true);
182 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true);
183 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true);
184
185 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true);
186 BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false);
187 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false);
188 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false);
189 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true);
190 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false);
191 BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false);
192 BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false);
193 BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false);
194 BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800195}
196
197BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
198{
199 if (!NetworkConfigurationDetector::hasIpv4()) {
200 BOOST_TEST_MESSAGE("Platform does not support IPv4, skipping the test case");
201 return;
202 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700203
204 // IPv4 unicast
205 addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363");
206 addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363");
207 addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363");
208 addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363");
209 addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695");
210 addTest("udp4://192.0.2.666:6363", false, "");
211 addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363");
212 addTest("udp4://invalid.invalid", false, "");
213
214 // IPv4 multicast
215 addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
216 addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
217 addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
218
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800219 runTests();
220}
221
222BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
223{
224 if (!NetworkConfigurationDetector::hasIpv6()) {
225 BOOST_TEST_MESSAGE("Platform does not support IPv6, skipping the test case");
226 return;
227 }
228
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700229 // IPv6 unicast
230 addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
231 addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
232 addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
233 addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
234 addTest("udp6://invalid.invalid", false, "");
235 addTest("udp://invalid.invalid", false, "");
236
237 // IPv6 multicast
238 addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
239 addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
240
241 runTests();
242}
243
244BOOST_AUTO_TEST_CASE(ParseTcp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700245{
246 FaceUri uri;
247
248 BOOST_CHECK(uri.parse("tcp://random.host.name"));
249 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
250 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
251 BOOST_CHECK_EQUAL(uri.getPort(), "");
252 BOOST_CHECK_EQUAL(uri.getPath(), "");
253
254 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
255 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
256
257 using namespace boost::asio;
258 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
259 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
260 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
261
262 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient"));
263 BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777");
264
265 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
266 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
267 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
268}
269
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800270BOOST_FIXTURE_TEST_CASE(CheckCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700271{
272 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
273 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
274 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
275
276 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
277 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
278 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
279 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
280 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
281 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
282 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
283 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
284 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
285 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800286}
287
288BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
289{
290 if (!NetworkConfigurationDetector::hasIpv4()) {
291 BOOST_TEST_MESSAGE("Platform does not support IPv4, skipping the test case");
292 return;
293 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700294
295 // IPv4 unicast
296 addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
297 addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
298 addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
299 addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
300 addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
301 addTest("tcp4://192.0.2.666:6363", false, "");
302 addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
303 addTest("tcp4://invalid.invalid", false, "");
304
305 // IPv4 multicast
306 addTest("tcp4://224.0.23.170:56363", false, "");
307 addTest("tcp4://224.0.23.170", false, "");
308 addTest("tcp4://all-routers.mcast.net:56363", false, "");
309
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800310 runTests();
311}
312
313BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
314{
315 if (!NetworkConfigurationDetector::hasIpv6()) {
316 BOOST_TEST_MESSAGE("Platform does not support IPv6, skipping the test case");
317 return;
318 }
319
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700320 // IPv6 unicast
321 addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
322 addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
323 addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363");
324 addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
325 addTest("tcp6://invalid.invalid", false, "");
326 addTest("tcp://invalid.invalid", false, "");
327
328 // IPv6 multicast
329 addTest("tcp6://[ff02::2]:56363", false, "");
330 addTest("tcp6://[ff02::2]", false, "");
331
332 runTests();
333}
334
335BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700336{
337 FaceUri uri;
338
339 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
340 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
341 BOOST_CHECK_EQUAL(uri.getHost(), "");
342 BOOST_CHECK_EQUAL(uri.getPort(), "");
343 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
344
345 //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false);
346 // This is not a valid unix:/ URI, but the parse would treat "var" as host
347
348#ifdef HAVE_UNIX_SOCKETS
349 using namespace boost::asio;
350 local::stream_protocol::endpoint endpoint("/var/run/example.sock");
351 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
352 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
353#endif // HAVE_UNIX_SOCKETS
354}
355
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700356BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700357{
358 FaceUri uri;
359
360 BOOST_CHECK(uri.parse("fd://6"));
361 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
362 BOOST_CHECK_EQUAL(uri.getHost(), "6");
363 BOOST_CHECK_EQUAL(uri.getPort(), "");
364 BOOST_CHECK_EQUAL(uri.getPath(), "");
365
366 int fd = 21;
367 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
368 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
369}
370
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700371BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700372{
373 FaceUri uri;
374
375 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
376 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
377 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
378 BOOST_CHECK_EQUAL(uri.getPort(), "");
379 BOOST_CHECK_EQUAL(uri.getPath(), "");
380
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700381 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700382
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700383 ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01");
384 BOOST_REQUIRE_NO_THROW(FaceUri(address));
385 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700386}
387
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700388BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
389{
390 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
391
392 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
393 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
394 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
395 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
396
397 addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
398 addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
399 addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
400 addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
401
402 runTests();
403}
404
405BOOST_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
415 std::string ifname = "en1";
416 BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname));
417 BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1");
418}
419
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700420BOOST_AUTO_TEST_CASE(ParseUdpDev)
421{
422 FaceUri uri;
423
424 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
425 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
426 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
427 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
428 BOOST_CHECK_EQUAL(uri.getPath(), "");
429
430 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
431 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
432 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
433 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
434 BOOST_CHECK_EQUAL(uri.getPath(), "");
435
436 BOOST_CHECK(uri.parse("abc+efg://eth0"));
437 BOOST_CHECK(!uri.parse("abc+://eth0"));
438 BOOST_CHECK(!uri.parse("+abc://eth0"));
439
440 using namespace boost::asio;
441
442 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
443 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1"));
444 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777");
445
446 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
447 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2"));
448 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777");
449}
450
451BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
452{
453 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
454 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
455 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
456 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
457
458 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
459 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
460 addTest("udp+dev://en1:7777", false, "");
461 addTest("udp6+dev://en2", false, "");
462}
463
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700464BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
465{
466 boost::asio::io_service io;
467
468 // unsupported scheme
469 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
470 FaceUri::CanonizeFailureCallback(),
471 io, time::milliseconds(1));
472
473 // cannot resolve
474 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
475 FaceUri::CanonizeFailureCallback(),
476 io, time::milliseconds(1));
477
478 // already canonical
479 FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
480 FaceUri::CanonizeFailureCallback(),
481 io, time::milliseconds(1));
482
483 // need DNS resolution
484 FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
485 FaceUri::CanonizeFailureCallback(),
486 io, time::milliseconds(1));
487
488 io.run(); // should not crash
489}
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
526BOOST_AUTO_TEST_SUITE_END()
527
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800528} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700529} // namespace util
530} // namespace ndn