blob: 11d708348ca156cf10ba6d7e66bfa7ff9645dc8c [file] [log] [blame]
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi3ffe66d2014-11-06 15:37:59 -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
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi3ffe66d2014-11-06 15:37:59 -070024 */
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080025
26#include "core/face-uri.hpp"
27
28#include "tests/test-common.hpp"
29
30namespace nfd {
31namespace tests {
32
33BOOST_FIXTURE_TEST_SUITE(CoreFaceUri, BaseFixture)
34
Junxiao Shie38b1c42014-04-01 23:45:36 -070035BOOST_AUTO_TEST_CASE(Internal)
36{
37 FaceUri uri;
38
39 BOOST_CHECK(uri.parse("internal://"));
40 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
41 BOOST_CHECK_EQUAL(uri.getHost(), "");
42 BOOST_CHECK_EQUAL(uri.getPort(), "");
43 BOOST_CHECK_EQUAL(uri.getPath(), "");
44
45 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
46 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
47}
48
49BOOST_AUTO_TEST_CASE(Udp)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080050{
51 BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
52 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
53 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
54
55 FaceUri uri;
56 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
Junxiao Shie38b1c42014-04-01 23:45:36 -070057
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080058 BOOST_CHECK(uri.parse("udp://hostname:80"));
59 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070060 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080061 BOOST_CHECK_EQUAL(uri.getPort(), "80");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070062 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080063
64 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
65 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070066 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080067 BOOST_CHECK_EQUAL(uri.getPort(), "20");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070068 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080069
70 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
71 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070072 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080073 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070074 BOOST_CHECK_EQUAL(uri.getPath(), "");
Junxiao Shie38b1c42014-04-01 23:45:36 -070075
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080076 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
77 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070078 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080079 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070080 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080081
Junxiao Shie38b1c42014-04-01 23:45:36 -070082 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
83
84 using namespace boost::asio;
85 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
86 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
87 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777");
88
89 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
90 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
91 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
92}
93
94BOOST_AUTO_TEST_CASE(Tcp)
95{
96 FaceUri uri;
97
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080098 BOOST_CHECK(uri.parse("tcp://random.host.name"));
99 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700100 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800101 BOOST_CHECK_EQUAL(uri.getPort(), "");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700102 BOOST_CHECK_EQUAL(uri.getPath(), "");
103
Junxiao Shie38b1c42014-04-01 23:45:36 -0700104 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
105 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
106
107 using namespace boost::asio;
108 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
109 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
110 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
111
112 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
113 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
114 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
115}
116
117BOOST_AUTO_TEST_CASE(Unix)
118{
119 FaceUri uri;
120
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700121 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
122 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
123 BOOST_CHECK_EQUAL(uri.getHost(), "");
124 BOOST_CHECK_EQUAL(uri.getPort(), "");
125 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800126
Junxiao Shie38b1c42014-04-01 23:45:36 -0700127 //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false);
128 // This is not a valid unix:/ URI, but the parse would treat "var" as host
129
130#ifdef HAVE_UNIX_SOCKETS
131 using namespace boost::asio;
132 local::stream_protocol::endpoint endpoint("/var/run/example.sock");
133 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
134 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
135#endif // HAVE_UNIX_SOCKETS
136}
137
138BOOST_AUTO_TEST_CASE(Fd)
139{
140 FaceUri uri;
141
142 BOOST_CHECK(uri.parse("fd://6"));
143 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
144 BOOST_CHECK_EQUAL(uri.getHost(), "6");
145 BOOST_CHECK_EQUAL(uri.getPort(), "");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700146 BOOST_CHECK_EQUAL(uri.getPath(), "");
Junxiao Shie38b1c42014-04-01 23:45:36 -0700147
148 int fd = 21;
149 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
150 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
151}
152
153BOOST_AUTO_TEST_CASE(Ether)
154{
155 FaceUri uri;
156
Chengyu Fan514ed5e2014-04-17 13:07:30 -0600157 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
Junxiao Shie38b1c42014-04-01 23:45:36 -0700158 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
159 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
160 BOOST_CHECK_EQUAL(uri.getPort(), "");
161 BOOST_CHECK_EQUAL(uri.getPath(), "");
162
163 BOOST_CHECK_EQUAL(uri.parse("ether://08:00:27:zz:dd:01"), false);
164
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700165#ifdef HAVE_LIBPCAP
Junxiao Shie38b1c42014-04-01 23:45:36 -0700166 ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01");
167 BOOST_REQUIRE_NO_THROW(FaceUri(address));
Chengyu Fan514ed5e2014-04-17 13:07:30 -0600168 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700169#endif // HAVE_LIBPCAP
Junxiao Shie38b1c42014-04-01 23:45:36 -0700170}
171
172BOOST_AUTO_TEST_CASE(Dev)
173{
174 FaceUri uri;
175
176 BOOST_CHECK(uri.parse("dev://eth0"));
177 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
178 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
179 BOOST_CHECK_EQUAL(uri.getPort(), "");
180 BOOST_CHECK_EQUAL(uri.getPath(), "");
181
182 std::string ifname = "en1";
183 BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname));
184 BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800185}
186
Junxiao Shi5cc1e342014-05-29 06:32:16 -0700187BOOST_AUTO_TEST_CASE(Bug1635)
188{
189 FaceUri uri;
190
191 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
192 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
193 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
194 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
195 BOOST_CHECK_EQUAL(uri.getPath(), "");
196 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
197}
198
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800199BOOST_AUTO_TEST_SUITE_END()
200
201} // namespace tests
202} // namespace nfd