blob: 6cf52d971e24050595c32951e6c33d04d532a8cd [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");
210 addTest("udp4://invalid.invalid", false, "");
211
212 // IPv4 multicast
213 addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
214 addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
215 addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
216
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800217 runTests();
218}
219
220BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
221{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100222 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800223
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700224 // IPv6 unicast
225 addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
226 addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
227 addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
228 addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
229 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
252 using namespace boost::asio;
253 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
254 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
255 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
256
257 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient"));
258 BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777");
259
260 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
261 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
262 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
263}
264
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800265BOOST_FIXTURE_TEST_CASE(CheckCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700266{
267 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
268 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
269 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
270
271 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
272 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
273 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
274 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
275 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
276 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
277 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
278 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
279 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
280 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800281}
282
283BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
284{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100285 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700286
287 // IPv4 unicast
288 addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
289 addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
290 addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
291 addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
292 addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
293 addTest("tcp4://192.0.2.666:6363", false, "");
294 addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
295 addTest("tcp4://invalid.invalid", false, "");
296
297 // IPv4 multicast
298 addTest("tcp4://224.0.23.170:56363", false, "");
299 addTest("tcp4://224.0.23.170", false, "");
300 addTest("tcp4://all-routers.mcast.net:56363", false, "");
301
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800302 runTests();
303}
304
305BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
306{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100307 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800308
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700309 // IPv6 unicast
310 addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
311 addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
312 addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363");
313 addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
314 addTest("tcp6://invalid.invalid", false, "");
315 addTest("tcp://invalid.invalid", false, "");
316
317 // IPv6 multicast
318 addTest("tcp6://[ff02::2]:56363", false, "");
319 addTest("tcp6://[ff02::2]", false, "");
320
321 runTests();
322}
323
324BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700325{
326 FaceUri uri;
327
328 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
329 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
330 BOOST_CHECK_EQUAL(uri.getHost(), "");
331 BOOST_CHECK_EQUAL(uri.getPort(), "");
332 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
333
334 //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false);
335 // This is not a valid unix:/ URI, but the parse would treat "var" as host
336
337#ifdef HAVE_UNIX_SOCKETS
338 using namespace boost::asio;
339 local::stream_protocol::endpoint endpoint("/var/run/example.sock");
340 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
341 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
342#endif // HAVE_UNIX_SOCKETS
343}
344
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700345BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700346{
347 FaceUri uri;
348
349 BOOST_CHECK(uri.parse("fd://6"));
350 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
351 BOOST_CHECK_EQUAL(uri.getHost(), "6");
352 BOOST_CHECK_EQUAL(uri.getPort(), "");
353 BOOST_CHECK_EQUAL(uri.getPath(), "");
354
355 int fd = 21;
356 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
357 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
358}
359
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700360BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700361{
362 FaceUri uri;
363
364 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
365 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
366 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
367 BOOST_CHECK_EQUAL(uri.getPort(), "");
368 BOOST_CHECK_EQUAL(uri.getPath(), "");
369
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700370 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700371
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700372 ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01");
373 BOOST_REQUIRE_NO_THROW(FaceUri(address));
374 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700375}
376
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700377BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
378{
379 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
380
381 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
382 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
383 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
384 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
385
386 addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
387 addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
388 addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
389 addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
390
391 runTests();
392}
393
394BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700395{
396 FaceUri uri;
397
398 BOOST_CHECK(uri.parse("dev://eth0"));
399 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
400 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
401 BOOST_CHECK_EQUAL(uri.getPort(), "");
402 BOOST_CHECK_EQUAL(uri.getPath(), "");
403
404 std::string ifname = "en1";
405 BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname));
406 BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1");
407}
408
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700409BOOST_AUTO_TEST_CASE(ParseUdpDev)
410{
411 FaceUri uri;
412
413 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
414 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
415 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
416 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
417 BOOST_CHECK_EQUAL(uri.getPath(), "");
418
419 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
420 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
421 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
422 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
423 BOOST_CHECK_EQUAL(uri.getPath(), "");
424
425 BOOST_CHECK(uri.parse("abc+efg://eth0"));
426 BOOST_CHECK(!uri.parse("abc+://eth0"));
427 BOOST_CHECK(!uri.parse("+abc://eth0"));
428
429 using namespace boost::asio;
430
431 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
432 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1"));
433 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777");
434
435 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
436 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2"));
437 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777");
438}
439
440BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
441{
442 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
443 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
444 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
445 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
446
447 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
448 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
449 addTest("udp+dev://en1:7777", false, "");
450 addTest("udp6+dev://en2", false, "");
451}
452
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700453BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
454{
455 boost::asio::io_service io;
456
457 // unsupported scheme
458 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
459 FaceUri::CanonizeFailureCallback(),
460 io, time::milliseconds(1));
461
462 // cannot resolve
463 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
464 FaceUri::CanonizeFailureCallback(),
465 io, time::milliseconds(1));
466
467 // already canonical
468 FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
469 FaceUri::CanonizeFailureCallback(),
470 io, time::milliseconds(1));
471
472 // need DNS resolution
473 FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
474 FaceUri::CanonizeFailureCallback(),
475 io, time::milliseconds(1));
476
477 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100478
479 // avoid "test case [...] did not check any assertions" message from Boost.Test
480 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700481}
482
483BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
484{
485 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
486 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
487 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
488 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
489 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), false);
490
491 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
492 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
493 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
494 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
495 BOOST_CHECK_EQUAL(FaceUri("dev://eth1").isCanonical(), false);
496
497 addTest("internal://", false, "");
498 addTest("null://", false, "");
499 addTest("unix:///var/run/nfd.sock", false, "");
500 addTest("fd://0", false, "");
501 addTest("dev://eth1", false, "");
502
503 runTests();
504}
505
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700506BOOST_AUTO_TEST_CASE(Bug1635)
507{
508 FaceUri uri;
509
510 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
511 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
512 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
513 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
514 BOOST_CHECK_EQUAL(uri.getPath(), "");
515 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
516}
517
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100518BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
519BOOST_AUTO_TEST_SUITE_END() // Util
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700520
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800521} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700522} // namespace util
523} // namespace ndn