blob: c3060fd7b74088eeccc5a66e954713472d6ee228 [file] [log] [blame]
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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,
Junxiao Shi5cc1e342014-05-29 06:32:16 -07008 * 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/>.
24 **/
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080025
26#include "core/face-uri.hpp"
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070027#ifdef HAVE_LIBPCAP
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070028#include "core/ethernet.hpp"
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070029#endif // HAVE_LIBPCAP
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080030
31#include "tests/test-common.hpp"
32
33namespace nfd {
34namespace tests {
35
36BOOST_FIXTURE_TEST_SUITE(CoreFaceUri, BaseFixture)
37
Junxiao Shie38b1c42014-04-01 23:45:36 -070038BOOST_AUTO_TEST_CASE(Internal)
39{
40 FaceUri uri;
41
42 BOOST_CHECK(uri.parse("internal://"));
43 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
44 BOOST_CHECK_EQUAL(uri.getHost(), "");
45 BOOST_CHECK_EQUAL(uri.getPort(), "");
46 BOOST_CHECK_EQUAL(uri.getPath(), "");
47
48 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
49 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
50}
51
52BOOST_AUTO_TEST_CASE(Udp)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080053{
54 BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
55 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
56 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
57
58 FaceUri uri;
59 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
Junxiao Shie38b1c42014-04-01 23:45:36 -070060
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080061 BOOST_CHECK(uri.parse("udp://hostname:80"));
62 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070063 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080064 BOOST_CHECK_EQUAL(uri.getPort(), "80");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070065 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080066
67 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
68 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070069 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080070 BOOST_CHECK_EQUAL(uri.getPort(), "20");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070071 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080072
73 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
74 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070075 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080076 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070077 BOOST_CHECK_EQUAL(uri.getPath(), "");
Junxiao Shie38b1c42014-04-01 23:45:36 -070078
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080079 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
80 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070081 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080082 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070083 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080084
Junxiao Shie38b1c42014-04-01 23:45:36 -070085 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
86
87 using namespace boost::asio;
88 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
89 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
90 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777");
91
92 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
93 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
94 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
95}
96
97BOOST_AUTO_TEST_CASE(Tcp)
98{
99 FaceUri uri;
100
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800101 BOOST_CHECK(uri.parse("tcp://random.host.name"));
102 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700103 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800104 BOOST_CHECK_EQUAL(uri.getPort(), "");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700105 BOOST_CHECK_EQUAL(uri.getPath(), "");
106
Junxiao Shie38b1c42014-04-01 23:45:36 -0700107 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
108 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
109
110 using namespace boost::asio;
111 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
112 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
113 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
114
115 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
116 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
117 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
118}
119
120BOOST_AUTO_TEST_CASE(Unix)
121{
122 FaceUri uri;
123
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700124 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
125 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
126 BOOST_CHECK_EQUAL(uri.getHost(), "");
127 BOOST_CHECK_EQUAL(uri.getPort(), "");
128 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800129
Junxiao Shie38b1c42014-04-01 23:45:36 -0700130 //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false);
131 // This is not a valid unix:/ URI, but the parse would treat "var" as host
132
133#ifdef HAVE_UNIX_SOCKETS
134 using namespace boost::asio;
135 local::stream_protocol::endpoint endpoint("/var/run/example.sock");
136 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
137 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
138#endif // HAVE_UNIX_SOCKETS
139}
140
141BOOST_AUTO_TEST_CASE(Fd)
142{
143 FaceUri uri;
144
145 BOOST_CHECK(uri.parse("fd://6"));
146 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
147 BOOST_CHECK_EQUAL(uri.getHost(), "6");
148 BOOST_CHECK_EQUAL(uri.getPort(), "");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700149 BOOST_CHECK_EQUAL(uri.getPath(), "");
Junxiao Shie38b1c42014-04-01 23:45:36 -0700150
151 int fd = 21;
152 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
153 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
154}
155
156BOOST_AUTO_TEST_CASE(Ether)
157{
158 FaceUri uri;
159
Chengyu Fan514ed5e2014-04-17 13:07:30 -0600160 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
Junxiao Shie38b1c42014-04-01 23:45:36 -0700161 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
162 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
163 BOOST_CHECK_EQUAL(uri.getPort(), "");
164 BOOST_CHECK_EQUAL(uri.getPath(), "");
165
166 BOOST_CHECK_EQUAL(uri.parse("ether://08:00:27:zz:dd:01"), false);
167
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700168#ifdef HAVE_LIBPCAP
Junxiao Shie38b1c42014-04-01 23:45:36 -0700169 ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01");
170 BOOST_REQUIRE_NO_THROW(FaceUri(address));
Chengyu Fan514ed5e2014-04-17 13:07:30 -0600171 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700172#endif // HAVE_LIBPCAP
Junxiao Shie38b1c42014-04-01 23:45:36 -0700173}
174
175BOOST_AUTO_TEST_CASE(Dev)
176{
177 FaceUri uri;
178
179 BOOST_CHECK(uri.parse("dev://eth0"));
180 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
181 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
182 BOOST_CHECK_EQUAL(uri.getPort(), "");
183 BOOST_CHECK_EQUAL(uri.getPath(), "");
184
185 std::string ifname = "en1";
186 BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname));
187 BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800188}
189
Junxiao Shi5cc1e342014-05-29 06:32:16 -0700190BOOST_AUTO_TEST_CASE(Bug1635)
191{
192 FaceUri uri;
193
194 BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366"));
195 BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient");
196 BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239");
197 BOOST_CHECK_EQUAL(uri.getPort(), "56366");
198 BOOST_CHECK_EQUAL(uri.getPath(), "");
199 BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366");
200}
201
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800202BOOST_AUTO_TEST_SUITE_END()
203
204} // namespace tests
205} // namespace nfd