blob: e1e07a989e77b21b62f0eef1e9f59fa163249c73 [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,
8 * Beijing Institute of Technology
9 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080024
25#include "core/face-uri.hpp"
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070026#ifdef HAVE_LIBPCAP
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070027#include "core/ethernet.hpp"
Alexander Afanasyev885a85b2014-04-12 21:01:13 -070028#endif // HAVE_LIBPCAP
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080029
30#include "tests/test-common.hpp"
31
32namespace nfd {
33namespace tests {
34
35BOOST_FIXTURE_TEST_SUITE(CoreFaceUri, BaseFixture)
36
Junxiao Shie38b1c42014-04-01 23:45:36 -070037BOOST_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)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080052{
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);
Junxiao Shie38b1c42014-04-01 23:45:36 -070059
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080060 BOOST_CHECK(uri.parse("udp://hostname:80"));
61 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070062 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080063 BOOST_CHECK_EQUAL(uri.getPort(), "80");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070064 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080065
66 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
67 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070068 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080069 BOOST_CHECK_EQUAL(uri.getPort(), "20");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070070 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080071
72 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
73 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070074 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080075 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070076 BOOST_CHECK_EQUAL(uri.getPath(), "");
Junxiao Shie38b1c42014-04-01 23:45:36 -070077
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080078 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
79 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070080 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080081 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070082 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080083
Junxiao Shie38b1c42014-04-01 23:45:36 -070084 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
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800100 BOOST_CHECK(uri.parse("tcp://random.host.name"));
101 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700102 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800103 BOOST_CHECK_EQUAL(uri.getPort(), "");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700104 BOOST_CHECK_EQUAL(uri.getPath(), "");
105
Junxiao Shie38b1c42014-04-01 23:45:36 -0700106 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 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
115 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
116 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
117}
118
119BOOST_AUTO_TEST_CASE(Unix)
120{
121 FaceUri uri;
122
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700123 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
124 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
125 BOOST_CHECK_EQUAL(uri.getHost(), "");
126 BOOST_CHECK_EQUAL(uri.getPort(), "");
127 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800128
Junxiao Shie38b1c42014-04-01 23:45:36 -0700129 //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false);
130 // This is not a valid unix:/ URI, but the parse would treat "var" as host
131
132#ifdef HAVE_UNIX_SOCKETS
133 using namespace boost::asio;
134 local::stream_protocol::endpoint endpoint("/var/run/example.sock");
135 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
136 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
137#endif // HAVE_UNIX_SOCKETS
138}
139
140BOOST_AUTO_TEST_CASE(Fd)
141{
142 FaceUri uri;
143
144 BOOST_CHECK(uri.parse("fd://6"));
145 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
146 BOOST_CHECK_EQUAL(uri.getHost(), "6");
147 BOOST_CHECK_EQUAL(uri.getPort(), "");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700148 BOOST_CHECK_EQUAL(uri.getPath(), "");
Junxiao Shie38b1c42014-04-01 23:45:36 -0700149
150 int fd = 21;
151 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
152 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
153}
154
155BOOST_AUTO_TEST_CASE(Ether)
156{
157 FaceUri uri;
158
Chengyu Fan514ed5e2014-04-17 13:07:30 -0600159 BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]"));
Junxiao Shie38b1c42014-04-01 23:45:36 -0700160 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
161 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
162 BOOST_CHECK_EQUAL(uri.getPort(), "");
163 BOOST_CHECK_EQUAL(uri.getPath(), "");
164
165 BOOST_CHECK_EQUAL(uri.parse("ether://08:00:27:zz:dd:01"), false);
166
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700167#ifdef HAVE_LIBPCAP
Junxiao Shie38b1c42014-04-01 23:45:36 -0700168 ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01");
169 BOOST_REQUIRE_NO_THROW(FaceUri(address));
Chengyu Fan514ed5e2014-04-17 13:07:30 -0600170 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]");
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700171#endif // HAVE_LIBPCAP
Junxiao Shie38b1c42014-04-01 23:45:36 -0700172}
173
174BOOST_AUTO_TEST_CASE(Dev)
175{
176 FaceUri uri;
177
178 BOOST_CHECK(uri.parse("dev://eth0"));
179 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
180 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
181 BOOST_CHECK_EQUAL(uri.getPort(), "");
182 BOOST_CHECK_EQUAL(uri.getPath(), "");
183
184 std::string ifname = "en1";
185 BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname));
186 BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800187}
188
189BOOST_AUTO_TEST_SUITE_END()
190
191} // namespace tests
192} // namespace nfd