blob: b228095cbbaf669ab275a14448dc87d510ca0f6e [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shie3ef6ee2014-10-05 14:40:54 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Junxiao Shi77dcadd2014-10-05 14:40:54 -070010 *
11 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
12 *
13 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
14 * terms of the GNU Lesser General Public License as published by the Free Software
15 * Foundation, either version 3 of the License, or (at your option) any later version.
16 *
17 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 *
21 * You should have received copies of the GNU General Public License and GNU Lesser
22 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
23 * <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
26 */
27
28#include "util/face-uri.hpp"
29
30#include "boost-test.hpp"
31
32namespace ndn {
33namespace util {
34
35BOOST_AUTO_TEST_SUITE(UtilTestFaceUri)
36
37BOOST_AUTO_TEST_CASE(Internal)
38{
39 FaceUri uri;
40
41 BOOST_CHECK(uri.parse("internal://"));
42 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
43 BOOST_CHECK_EQUAL(uri.getHost(), "");
44 BOOST_CHECK_EQUAL(uri.getPort(), "");
45 BOOST_CHECK_EQUAL(uri.getPath(), "");
46
47 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
48 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
49}
50
51BOOST_AUTO_TEST_CASE(Udp)
52{
53 BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
54 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
55 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
56
57 FaceUri uri;
58 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
59
60 BOOST_CHECK(uri.parse("udp://hostname:80"));
61 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
62 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
63 BOOST_CHECK_EQUAL(uri.getPort(), "80");
64 BOOST_CHECK_EQUAL(uri.getPath(), "");
65
66 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
67 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
68 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
69 BOOST_CHECK_EQUAL(uri.getPort(), "20");
70 BOOST_CHECK_EQUAL(uri.getPath(), "");
71
72 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
73 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
74 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
75 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
76 BOOST_CHECK_EQUAL(uri.getPath(), "");
77
78 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
79 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
80 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
81 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
82 BOOST_CHECK_EQUAL(uri.getPath(), "");
83
84 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
85
86 using namespace boost::asio;
87 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
88 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
89 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777");
90
91 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
92 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
93 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
94}
95
96BOOST_AUTO_TEST_CASE(Tcp)
97{
98 FaceUri uri;
99
100 BOOST_CHECK(uri.parse("tcp://random.host.name"));
101 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
102 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
103 BOOST_CHECK_EQUAL(uri.getPort(), "");
104 BOOST_CHECK_EQUAL(uri.getPath(), "");
105
106 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
107 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
108
109 using namespace boost::asio;
110 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
111 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
112 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
113
114 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient"));
115 BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777");
116
117 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
118 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
119 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
120}
121
122BOOST_AUTO_TEST_CASE(Unix)
123{
124 FaceUri uri;
125
126 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
127 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
128 BOOST_CHECK_EQUAL(uri.getHost(), "");
129 BOOST_CHECK_EQUAL(uri.getPort(), "");
130 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
131
132 //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false);
133 // This is not a valid unix:/ URI, but the parse would treat "var" as host
134
135#ifdef HAVE_UNIX_SOCKETS
136 using namespace boost::asio;
137 local::stream_protocol::endpoint endpoint("/var/run/example.sock");
138 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
139 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
140#endif // HAVE_UNIX_SOCKETS
141}
142
143BOOST_AUTO_TEST_CASE(Fd)
144{
145 FaceUri uri;
146
147 BOOST_CHECK(uri.parse("fd://6"));
148 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
149 BOOST_CHECK_EQUAL(uri.getHost(), "6");
150 BOOST_CHECK_EQUAL(uri.getPort(), "");
151 BOOST_CHECK_EQUAL(uri.getPath(), "");
152
153 int fd = 21;
154 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
155 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
156}
157
158BOOST_AUTO_TEST_CASE(Ether)
159{
160 FaceUri uri;
161
162 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
163 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
164 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
165 BOOST_CHECK_EQUAL(uri.getPort(), "");
166 BOOST_CHECK_EQUAL(uri.getPath(), "");
167
168 BOOST_CHECK_EQUAL(uri.parse("ether://08:00:27:zz:dd:01"), false);
169
170#ifdef HAVE_LIBPCAP
171 ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01");
172 BOOST_REQUIRE_NO_THROW(FaceUri(address));
173 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
174#endif // HAVE_LIBPCAP
175}
176
177BOOST_AUTO_TEST_CASE(Dev)
178{
179 FaceUri uri;
180
181 BOOST_CHECK(uri.parse("dev://eth0"));
182 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
183 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
184 BOOST_CHECK_EQUAL(uri.getPort(), "");
185 BOOST_CHECK_EQUAL(uri.getPath(), "");
186
187 std::string ifname = "en1";
188 BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname));
189 BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1");
190}
191
192BOOST_AUTO_TEST_CASE(Bug1635)
193{
194 FaceUri uri;
195
196 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
197 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
198 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
199 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
200 BOOST_CHECK_EQUAL(uri.getPath(), "");
201 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
202}
203
204BOOST_AUTO_TEST_SUITE_END()
205
206} // namespace util
207} // namespace ndn