Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * 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 Shi | 5cc1e34 | 2014-05-29 06:32:16 -0700 | [diff] [blame] | 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 25 | |
| 26 | #include "core/face-uri.hpp" |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 27 | #ifdef HAVE_LIBPCAP |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 28 | #include "core/ethernet.hpp" |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 29 | #endif // HAVE_LIBPCAP |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 30 | |
| 31 | #include "tests/test-common.hpp" |
| 32 | |
| 33 | namespace nfd { |
| 34 | namespace tests { |
| 35 | |
| 36 | BOOST_FIXTURE_TEST_SUITE(CoreFaceUri, BaseFixture) |
| 37 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 38 | BOOST_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 | |
| 52 | BOOST_AUTO_TEST_CASE(Udp) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 53 | { |
| 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 Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 61 | BOOST_CHECK(uri.parse("udp://hostname:80")); |
| 62 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(uri.getHost(), "hostname"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(uri.getPort(), "80"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 66 | |
| 67 | BOOST_CHECK(uri.parse("udp4://192.0.2.1:20")); |
| 68 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp4"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(uri.getPort(), "20"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 72 | |
| 73 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363")); |
| 74 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 77 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 79 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363")); |
| 80 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 84 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 85 | 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 | |
| 97 | BOOST_AUTO_TEST_CASE(Tcp) |
| 98 | { |
| 99 | FaceUri uri; |
| 100 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 101 | BOOST_CHECK(uri.parse("tcp://random.host.name")); |
| 102 | BOOST_CHECK_EQUAL(uri.getScheme(), "tcp"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 106 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 107 | 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 | |
| 120 | BOOST_AUTO_TEST_CASE(Unix) |
| 121 | { |
| 122 | FaceUri uri; |
| 123 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 124 | 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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 129 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 130 | //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 | |
| 141 | BOOST_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 Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 149 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 150 | |
| 151 | int fd = 21; |
| 152 | BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd)); |
| 153 | BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21"); |
| 154 | } |
| 155 | |
| 156 | BOOST_AUTO_TEST_CASE(Ether) |
| 157 | { |
| 158 | FaceUri uri; |
| 159 | |
Chengyu Fan | 514ed5e | 2014-04-17 13:07:30 -0600 | [diff] [blame] | 160 | BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]")); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 161 | 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 Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 168 | #ifdef HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 169 | ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01"); |
| 170 | BOOST_REQUIRE_NO_THROW(FaceUri(address)); |
Chengyu Fan | 514ed5e | 2014-04-17 13:07:30 -0600 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]"); |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 172 | #endif // HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | BOOST_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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Junxiao Shi | 5cc1e34 | 2014-05-29 06:32:16 -0700 | [diff] [blame] | 190 | BOOST_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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 202 | BOOST_AUTO_TEST_SUITE_END() |
| 203 | |
| 204 | } // namespace tests |
| 205 | } // namespace nfd |