blob: 27313f79c855f66dfe49dd8e691c41b48c48675e [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shid5c2f0c2017-04-04 09:52:11 +00003 * Copyright (c) 2013-2017 Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Junxiao Shi77dcadd2014-10-05 14:40:54 -070010 *
11 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
12 *
13 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
14 * terms of the GNU Lesser General Public License as published by the Free Software
15 * Foundation, either version 3 of the License, or (at your option) any later version.
16 *
17 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 *
21 * You should have received copies of the GNU General Public License and GNU Lesser
22 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
23 * <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
26 */
27
Junxiao Shi25467942017-06-30 02:53:14 +000028#include "net/face-uri.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070029
30#include "boost-test.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000031#include "network-configuration-detector.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070032
33namespace ndn {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080034namespace tests {
35
Junxiao Shi25467942017-06-30 02:53:14 +000036BOOST_AUTO_TEST_SUITE(Net)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010037BOOST_AUTO_TEST_SUITE(TestFaceUri)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070038
Junxiao Shi4083c8d2014-10-12 16:43:16 -070039class CanonizeFixture : noncopyable
40{
41protected:
Junxiao Shi4083c8d2014-10-12 16:43:16 -070042 void
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050043 addTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri)
44 {
45 ++m_nPending;
46 auto tc = make_shared<CanonizeTestCase>(request, shouldSucceed, expectedUri);
47
48 FaceUri uri(request);
49 uri.canonize(bind(&CanonizeFixture::onCanonizeSuccess, this, tc, _1),
50 bind(&CanonizeFixture::onCanonizeFailure, this, tc, _1),
51 m_io, time::seconds(10));
52 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -070053
54 void
55 runTests()
56 {
57 m_io.run();
58 BOOST_CHECK_EQUAL(m_nPending, 0);
59 }
60
61private:
Davide Pesavento50df5622015-03-19 22:05:05 +010062 class CanonizeTestCase
Junxiao Shi4083c8d2014-10-12 16:43:16 -070063 {
64 public:
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050065 CanonizeTestCase(const std::string& request, bool shouldSucceed, const std::string& expectedUri)
66 : m_expectedUri(expectedUri)
Davide Pesavento50df5622015-03-19 22:05:05 +010067 , m_message(request + " should " + (shouldSucceed ? "succeed" : "fail"))
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050068 , m_shouldSucceed(shouldSucceed)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070069 , m_isCompleted(false)
70 {
71 }
72
73 public:
Junxiao Shi4083c8d2014-10-12 16:43:16 -070074 std::string m_expectedUri;
Davide Pesavento50df5622015-03-19 22:05:05 +010075 std::string m_message;
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050076 bool m_shouldSucceed;
Junxiao Shi4083c8d2014-10-12 16:43:16 -070077 bool m_isCompleted;
78 };
79
Junxiao Shi4083c8d2014-10-12 16:43:16 -070080 void
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050081 onCanonizeSuccess(const shared_ptr<CanonizeTestCase>& tc, const FaceUri& canonicalUri)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070082 {
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050083 BOOST_CHECK_EQUAL(tc->m_isCompleted, false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -070084 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);
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050088 if (tc->m_shouldSucceed) {
89 BOOST_CHECK_EQUAL(canonicalUri.toString(), tc->m_expectedUri);
90 }
Junxiao Shi4083c8d2014-10-12 16:43:16 -070091 }
92
Junxiao Shi4083c8d2014-10-12 16:43:16 -070093 void
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050094 onCanonizeFailure(const shared_ptr<CanonizeTestCase>& tc, const std::string& reason)
Junxiao Shi4083c8d2014-10-12 16:43:16 -070095 {
Davide Pesaventoe1081cd2016-12-19 23:58:15 -050096 BOOST_CHECK_EQUAL(tc->m_isCompleted, false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -070097 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;
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500105 ssize_t m_nPending = 0;
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700106};
107
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700108BOOST_AUTO_TEST_CASE(ParseInternal)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700109{
110 FaceUri uri;
111
112 BOOST_CHECK(uri.parse("internal://"));
113 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
114 BOOST_CHECK_EQUAL(uri.getHost(), "");
115 BOOST_CHECK_EQUAL(uri.getPort(), "");
116 BOOST_CHECK_EQUAL(uri.getPath(), "");
117
118 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
119 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
120}
121
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700122BOOST_AUTO_TEST_CASE(ParseUdp)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700123{
124 BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
125 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
126 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
127
128 FaceUri uri;
129 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
130
131 BOOST_CHECK(uri.parse("udp://hostname:80"));
132 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
133 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
134 BOOST_CHECK_EQUAL(uri.getPort(), "80");
135 BOOST_CHECK_EQUAL(uri.getPath(), "");
136
137 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
138 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
139 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
140 BOOST_CHECK_EQUAL(uri.getPort(), "20");
141 BOOST_CHECK_EQUAL(uri.getPath(), "");
142
143 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
144 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
145 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
146 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
147 BOOST_CHECK_EQUAL(uri.getPath(), "");
148
149 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
150 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
151 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
152 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
153 BOOST_CHECK_EQUAL(uri.getPath(), "");
154
155 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
156
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500157 namespace ip = boost::asio::ip;
158
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700159 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
160 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
161 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777");
162
163 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
164 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
165 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
166}
167
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500168BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700169{
170 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true);
171 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true);
172 BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true);
173
174 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true);
175 BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false);
176 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false);
177 BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false);
178 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true);
179 BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700180 BOOST_CHECK_EQUAL(FaceUri("udp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700181 BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false);
182 BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false);
183 BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false);
184 BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
Eric Newberry71aecae2017-05-29 00:22:39 -0700185 BOOST_CHECK_EQUAL(FaceUri("udp4://[2001:db8::1]:6363").isCanonical(), false);
186 BOOST_CHECK_EQUAL(FaceUri("udp6://192.0.2.1:6363").isCanonical(), false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800187}
188
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500189BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800190BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture)
191{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100192 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700193
194 // IPv4 unicast
195 addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363");
196 addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363");
197 addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363");
198 addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363");
199 addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695");
200 addTest("udp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500201 addTest("udp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700202 addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500203 addTest("udp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700204 addTest("udp4://invalid.invalid", false, "");
205
206 // IPv4 multicast
207 addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363");
208 addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363");
209 addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363");
210
Eric Newberry71aecae2017-05-29 00:22:39 -0700211 // IPv6 used with udp4 protocol - not canonical
212 addTest("udp4://[2001:db8::1]:6363", false, "");
213
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800214 runTests();
215}
216
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500217BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800218BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture)
219{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100220 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800221
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700222 // IPv6 unicast
223 addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500224 addTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700225 addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363");
226 addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500227 addTest("udp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500228 addTest("udp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700229 addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500230 addTest("udp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700231 addTest("udp6://invalid.invalid", false, "");
232 addTest("udp://invalid.invalid", false, "");
233
234 // IPv6 multicast
235 addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363");
236 addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363");
237
Eric Newberry71aecae2017-05-29 00:22:39 -0700238 // IPv4 used with udp6 protocol - not canonical
239 addTest("udp6://192.0.2.1:6363", false, "");
240
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700241 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
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500257 namespace ip = boost::asio::ip;
258
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700259 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
260 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
261 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
262
263 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient"));
264 BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777");
265
266 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
267 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
268 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
269}
270
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500271BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700272{
273 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true);
274 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true);
275 BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true);
276
277 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true);
278 BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false);
279 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false);
280 BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false);
281 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true);
282 BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700283 BOOST_CHECK_EQUAL(FaceUri("tcp://[2001:db8::1]:6363").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700284 BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false);
285 BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false);
286 BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false);
287 BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
Eric Newberry71aecae2017-05-29 00:22:39 -0700288 BOOST_CHECK_EQUAL(FaceUri("tcp4://[2001:db8::1]:6363").isCanonical(), false);
289 BOOST_CHECK_EQUAL(FaceUri("tcp6://192.0.2.1:6363").isCanonical(), false);
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800290}
291
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500292BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800293BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture)
294{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100295 SKIP_IF_IPV4_UNAVAILABLE();
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700296
297 // IPv4 unicast
298 addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363");
299 addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363");
300 addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363");
301 addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363");
302 addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695");
303 addTest("tcp4://192.0.2.666:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500304 addTest("tcp4://192.0.2.7:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700305 addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500306 addTest("tcp4://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700307 addTest("tcp4://invalid.invalid", false, "");
308
309 // IPv4 multicast
310 addTest("tcp4://224.0.23.170:56363", false, "");
311 addTest("tcp4://224.0.23.170", false, "");
312 addTest("tcp4://all-routers.mcast.net:56363", false, "");
313
Eric Newberry71aecae2017-05-29 00:22:39 -0700314 // IPv6 used with tcp4 protocol - not canonical
315 addTest("tcp4://[2001:db8::1]:6363", false, "");
316
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800317 runTests();
318}
319
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500320BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1)
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800321BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture)
322{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100323 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800324
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700325 // IPv6 unicast
326 addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500327 addTest("tcp6://[2001:db8::1]", true, "tcp6://[2001:db8::1]:6363");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700328 addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363");
329 addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500330 addTest("tcp6://[2001::db8::1]:6363", false, "");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500331 addTest("tcp6://[2001:db8::1]:99999", false, ""); // Bug #3897
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700332 addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363");
Davide Pesaventof78cb702016-12-11 14:42:40 -0500333 addTest("tcp6://google-public-dns-a.google.com:70000", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700334 addTest("tcp6://invalid.invalid", false, "");
335 addTest("tcp://invalid.invalid", false, "");
336
337 // IPv6 multicast
338 addTest("tcp6://[ff02::2]:56363", false, "");
339 addTest("tcp6://[ff02::2]", false, "");
340
Eric Newberry71aecae2017-05-29 00:22:39 -0700341 // IPv4 used with tcp6 protocol - not canonical
342 addTest("tcp6://192.0.2.1:6363", false, "");
343
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700344 runTests();
345}
346
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500347BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700348BOOST_AUTO_TEST_CASE(ParseUnix)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700349{
350 FaceUri uri;
351
352 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
353 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
354 BOOST_CHECK_EQUAL(uri.getHost(), "");
355 BOOST_CHECK_EQUAL(uri.getPort(), "");
356 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
357
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500358 // This is not a valid unix:// URI, but the parse would treat "var" as host
359 BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); // Bug #3896
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700360
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500361#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
362 using boost::asio::local::stream_protocol;
363 stream_protocol::endpoint endpoint("/var/run/example.sock");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700364 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
365 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500366#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700367}
368
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700369BOOST_AUTO_TEST_CASE(ParseFd)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700370{
371 FaceUri uri;
372
373 BOOST_CHECK(uri.parse("fd://6"));
374 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
375 BOOST_CHECK_EQUAL(uri.getHost(), "6");
376 BOOST_CHECK_EQUAL(uri.getPort(), "");
377 BOOST_CHECK_EQUAL(uri.getPath(), "");
378
379 int fd = 21;
380 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
381 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
382}
383
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700384BOOST_AUTO_TEST_CASE(ParseEther)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700385{
386 FaceUri uri;
387
388 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
389 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
390 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
391 BOOST_CHECK_EQUAL(uri.getPort(), "");
392 BOOST_CHECK_EQUAL(uri.getPath(), "");
393
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700394 BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false);
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700395
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500396 auto address = ethernet::Address::fromString("33:33:01:01:01:01");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700397 BOOST_REQUIRE_NO_THROW(FaceUri(address));
398 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700399}
400
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700401BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture)
402{
403 BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true);
404
405 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true);
406 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false);
407 BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false);
408 BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true);
409
410 addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]");
411 addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]");
412 addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]");
413 addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]");
414
415 runTests();
416}
417
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000418BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1)
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700419BOOST_AUTO_TEST_CASE(ParseDev)
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700420{
421 FaceUri uri;
422
423 BOOST_CHECK(uri.parse("dev://eth0"));
424 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
425 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
426 BOOST_CHECK_EQUAL(uri.getPort(), "");
427 BOOST_CHECK_EQUAL(uri.getPath(), "");
428
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000429 BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896
430
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700431 std::string ifname = "en1";
Junxiao Shid5c2f0c2017-04-04 09:52:11 +0000432 BOOST_REQUIRE_NO_THROW(uri = FaceUri::fromDev(ifname));
433 BOOST_CHECK_EQUAL(uri.toString(), "dev://en1");
434}
435
436BOOST_AUTO_TEST_CASE(IsCanonicalDev)
437{
438 BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), true);
439
440 BOOST_CHECK_EQUAL(FaceUri("dev://eth0").isCanonical(), true);
441 BOOST_CHECK_EQUAL(FaceUri("dev://").isCanonical(), false);
442 BOOST_CHECK_EQUAL(FaceUri("dev://eth0:8888").isCanonical(), false);
443 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/").isCanonical(), false);
444 BOOST_CHECK_EQUAL(FaceUri("dev://eth0/A").isCanonical(), false);
445}
446
447BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture)
448{
449 addTest("dev://eth0", true, "dev://eth0");
450 addTest("dev://", false, "");
451 addTest("dev://eth0:8888", false, "");
452 addTest("dev://eth0/", true, "dev://eth0");
453 addTest("dev://eth0/A", false, "");
454
455 runTests();
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700456}
457
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700458BOOST_AUTO_TEST_CASE(ParseUdpDev)
459{
460 FaceUri uri;
461
462 BOOST_CHECK(uri.parse("udp4+dev://eth0:7777"));
463 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev");
464 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
465 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
466 BOOST_CHECK_EQUAL(uri.getPath(), "");
467
468 BOOST_CHECK(uri.parse("udp6+dev://eth1:7777"));
469 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev");
470 BOOST_CHECK_EQUAL(uri.getHost(), "eth1");
471 BOOST_CHECK_EQUAL(uri.getPort(), "7777");
472 BOOST_CHECK_EQUAL(uri.getPath(), "");
473
474 BOOST_CHECK(uri.parse("abc+efg://eth0"));
475 BOOST_CHECK(!uri.parse("abc+://eth0"));
476 BOOST_CHECK(!uri.parse("+abc://eth0"));
477
Davide Pesaventoe1081cd2016-12-19 23:58:15 -0500478 namespace ip = boost::asio::ip;
Weiwei Liud7f4fda2016-10-19 22:38:39 -0700479
480 ip::udp::endpoint endpoint4(ip::udp::v4(), 7777);
481 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1"));
482 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777");
483
484 ip::udp::endpoint endpoint6(ip::udp::v6(), 7777);
485 BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2"));
486 BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777");
487}
488
489BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture)
490{
491 BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true);
492 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true);
493 BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false);
494 BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false);
495
496 addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777");
497 addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777");
498 addTest("udp+dev://en1:7777", false, "");
499 addTest("udp6+dev://en2", false, "");
500}
501
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700502BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback)
503{
504 boost::asio::io_service io;
505
506 // unsupported scheme
507 FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(),
508 FaceUri::CanonizeFailureCallback(),
509 io, time::milliseconds(1));
510
511 // cannot resolve
512 FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(),
513 FaceUri::CanonizeFailureCallback(),
514 io, time::milliseconds(1));
515
516 // already canonical
517 FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
518 FaceUri::CanonizeFailureCallback(),
519 io, time::milliseconds(1));
520
521 // need DNS resolution
522 FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(),
523 FaceUri::CanonizeFailureCallback(),
524 io, time::milliseconds(1));
525
526 io.run(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100527
528 // avoid "test case [...] did not check any assertions" message from Boost.Test
529 BOOST_CHECK(true);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700530}
531
532BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture)
533{
534 BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false);
535 BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false);
536 BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false);
537 BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700538
539 BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false);
540 BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false);
541 BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false);
542 BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false);
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700543
544 addTest("internal://", false, "");
545 addTest("null://", false, "");
546 addTest("unix:///var/run/nfd.sock", false, "");
547 addTest("fd://0", false, "");
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700548
549 runTests();
550}
551
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700552BOOST_AUTO_TEST_CASE(Bug1635)
553{
554 FaceUri uri;
555
556 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
557 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
558 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
559 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
560 BOOST_CHECK_EQUAL(uri.getPath(), "");
561 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
562}
563
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100564BOOST_AUTO_TEST_SUITE_END() // TestFaceUri
Junxiao Shi25467942017-06-30 02:53:14 +0000565BOOST_AUTO_TEST_SUITE_END() // Net
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700566
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800567} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700568} // namespace ndn