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, |
| 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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 24 | |
| 25 | #include "core/face-uri.hpp" |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 26 | #ifdef HAVE_LIBPCAP |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 27 | #include "core/ethernet.hpp" |
Alexander Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 28 | #endif // HAVE_LIBPCAP |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 29 | |
| 30 | #include "tests/test-common.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace tests { |
| 34 | |
| 35 | BOOST_FIXTURE_TEST_SUITE(CoreFaceUri, BaseFixture) |
| 36 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 37 | BOOST_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 | |
| 51 | BOOST_AUTO_TEST_CASE(Udp) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 52 | { |
| 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 Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 60 | BOOST_CHECK(uri.parse("udp://hostname:80")); |
| 61 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(uri.getHost(), "hostname"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(uri.getPort(), "80"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 65 | |
| 66 | BOOST_CHECK(uri.parse("udp4://192.0.2.1:20")); |
| 67 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp4"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(uri.getPort(), "20"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 71 | |
| 72 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363")); |
| 73 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 77 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 78 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363")); |
| 79 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 80 | 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] | 81 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 83 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 84 | 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 | |
| 96 | BOOST_AUTO_TEST_CASE(Tcp) |
| 97 | { |
| 98 | FaceUri uri; |
| 99 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 100 | BOOST_CHECK(uri.parse("tcp://random.host.name")); |
| 101 | BOOST_CHECK_EQUAL(uri.getScheme(), "tcp"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 105 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 106 | 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 | |
| 119 | BOOST_AUTO_TEST_CASE(Unix) |
| 120 | { |
| 121 | FaceUri uri; |
| 122 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 123 | 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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 128 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 129 | //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 | |
| 140 | BOOST_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 Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 148 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 149 | |
| 150 | int fd = 21; |
| 151 | BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd)); |
| 152 | BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21"); |
| 153 | } |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(Ether) |
| 156 | { |
| 157 | FaceUri uri; |
| 158 | |
Chengyu Fan | 514ed5e | 2014-04-17 13:07:30 -0600 | [diff] [blame] | 159 | BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]")); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 160 | 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 Afanasyev | 885a85b | 2014-04-12 21:01:13 -0700 | [diff] [blame] | 167 | #ifdef HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 168 | ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01"); |
| 169 | BOOST_REQUIRE_NO_THROW(FaceUri(address)); |
Chengyu Fan | 514ed5e | 2014-04-17 13:07:30 -0600 | [diff] [blame] | 170 | 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] | 171 | #endif // HAVE_LIBPCAP |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | BOOST_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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | BOOST_AUTO_TEST_SUITE_END() |
| 190 | |
| 191 | } // namespace tests |
| 192 | } // namespace nfd |