blob: 80d5a894eba46d54e0422cfc796b9fafef7bedf2 [file] [log] [blame]
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "core/face-uri.hpp"
Junxiao Shie38b1c42014-04-01 23:45:36 -07008#ifdef HAVE_PCAP
9#include "face/ethernet.hpp"
10#endif // HAVE_PCAP
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080011
12#include "tests/test-common.hpp"
13
14namespace nfd {
15namespace tests {
16
17BOOST_FIXTURE_TEST_SUITE(CoreFaceUri, BaseFixture)
18
Junxiao Shie38b1c42014-04-01 23:45:36 -070019BOOST_AUTO_TEST_CASE(Internal)
20{
21 FaceUri uri;
22
23 BOOST_CHECK(uri.parse("internal://"));
24 BOOST_CHECK_EQUAL(uri.getScheme(), "internal");
25 BOOST_CHECK_EQUAL(uri.getHost(), "");
26 BOOST_CHECK_EQUAL(uri.getPort(), "");
27 BOOST_CHECK_EQUAL(uri.getPath(), "");
28
29 BOOST_CHECK_EQUAL(uri.parse("internal:"), false);
30 BOOST_CHECK_EQUAL(uri.parse("internal:/"), false);
31}
32
33BOOST_AUTO_TEST_CASE(Udp)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080034{
35 BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
36 BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
37 BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
38
39 FaceUri uri;
40 BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
Junxiao Shie38b1c42014-04-01 23:45:36 -070041
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080042 BOOST_CHECK(uri.parse("udp://hostname:80"));
43 BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070044 BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080045 BOOST_CHECK_EQUAL(uri.getPort(), "80");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070046 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080047
48 BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
49 BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070050 BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080051 BOOST_CHECK_EQUAL(uri.getPort(), "20");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070052 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080053
54 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
55 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070056 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080057 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070058 BOOST_CHECK_EQUAL(uri.getPath(), "");
Junxiao Shie38b1c42014-04-01 23:45:36 -070059
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080060 BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
61 BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070062 BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080063 BOOST_CHECK_EQUAL(uri.getPort(), "6363");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070064 BOOST_CHECK_EQUAL(uri.getPath(), "");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080065
Junxiao Shie38b1c42014-04-01 23:45:36 -070066 BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);
67
68 using namespace boost::asio;
69 ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
70 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
71 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777");
72
73 ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
74 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
75 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
76}
77
78BOOST_AUTO_TEST_CASE(Tcp)
79{
80 FaceUri uri;
81
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080082 BOOST_CHECK(uri.parse("tcp://random.host.name"));
83 BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070084 BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080085 BOOST_CHECK_EQUAL(uri.getPort(), "");
Junxiao Shi61e3cc52014-03-03 20:40:28 -070086 BOOST_CHECK_EQUAL(uri.getPath(), "");
87
Junxiao Shie38b1c42014-04-01 23:45:36 -070088 BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
89 BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
90
91 using namespace boost::asio;
92 ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777);
93 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4));
94 BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777");
95
96 ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
97 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
98 BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
99}
100
101BOOST_AUTO_TEST_CASE(Unix)
102{
103 FaceUri uri;
104
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700105 BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
106 BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
107 BOOST_CHECK_EQUAL(uri.getHost(), "");
108 BOOST_CHECK_EQUAL(uri.getPort(), "");
109 BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800110
Junxiao Shie38b1c42014-04-01 23:45:36 -0700111 //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false);
112 // This is not a valid unix:/ URI, but the parse would treat "var" as host
113
114#ifdef HAVE_UNIX_SOCKETS
115 using namespace boost::asio;
116 local::stream_protocol::endpoint endpoint("/var/run/example.sock");
117 BOOST_REQUIRE_NO_THROW(FaceUri(endpoint));
118 BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock");
119#endif // HAVE_UNIX_SOCKETS
120}
121
122BOOST_AUTO_TEST_CASE(Fd)
123{
124 FaceUri uri;
125
126 BOOST_CHECK(uri.parse("fd://6"));
127 BOOST_CHECK_EQUAL(uri.getScheme(), "fd");
128 BOOST_CHECK_EQUAL(uri.getHost(), "6");
129 BOOST_CHECK_EQUAL(uri.getPort(), "");
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700130 BOOST_CHECK_EQUAL(uri.getPath(), "");
Junxiao Shie38b1c42014-04-01 23:45:36 -0700131
132 int fd = 21;
133 BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd));
134 BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21");
135}
136
137BOOST_AUTO_TEST_CASE(Ether)
138{
139 FaceUri uri;
140
141 BOOST_CHECK(uri.parse("ether://08:00:27:01:dd:01"));
142 BOOST_CHECK_EQUAL(uri.getScheme(), "ether");
143 BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01");
144 BOOST_CHECK_EQUAL(uri.getPort(), "");
145 BOOST_CHECK_EQUAL(uri.getPath(), "");
146
147 BOOST_CHECK_EQUAL(uri.parse("ether://08:00:27:zz:dd:01"), false);
148
149#ifdef HAVE_PCAP
150 ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01");
151 BOOST_REQUIRE_NO_THROW(FaceUri(address));
152 BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://33:33:01:01:01:01");
153#endif // HAVE_PCAP
154}
155
156BOOST_AUTO_TEST_CASE(Dev)
157{
158 FaceUri uri;
159
160 BOOST_CHECK(uri.parse("dev://eth0"));
161 BOOST_CHECK_EQUAL(uri.getScheme(), "dev");
162 BOOST_CHECK_EQUAL(uri.getHost(), "eth0");
163 BOOST_CHECK_EQUAL(uri.getPort(), "");
164 BOOST_CHECK_EQUAL(uri.getPath(), "");
165
166 std::string ifname = "en1";
167 BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname));
168 BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1");
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800169}
170
171BOOST_AUTO_TEST_SUITE_END()
172
173} // namespace tests
174} // namespace nfd