Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "util/face-uri.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace util { |
| 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(UtilTestFaceUri) |
| 30 | |
| 31 | BOOST_AUTO_TEST_CASE(Internal) |
| 32 | { |
| 33 | FaceUri uri; |
| 34 | |
| 35 | BOOST_CHECK(uri.parse("internal://")); |
| 36 | BOOST_CHECK_EQUAL(uri.getScheme(), "internal"); |
| 37 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 38 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 39 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 40 | |
| 41 | BOOST_CHECK_EQUAL(uri.parse("internal:"), false); |
| 42 | BOOST_CHECK_EQUAL(uri.parse("internal:/"), false); |
| 43 | } |
| 44 | |
| 45 | BOOST_AUTO_TEST_CASE(Udp) |
| 46 | { |
| 47 | BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363")); |
| 48 | BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error); |
| 49 | BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error); |
| 50 | |
| 51 | FaceUri uri; |
| 52 | BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false); |
| 53 | |
| 54 | BOOST_CHECK(uri.parse("udp://hostname:80")); |
| 55 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp"); |
| 56 | BOOST_CHECK_EQUAL(uri.getHost(), "hostname"); |
| 57 | BOOST_CHECK_EQUAL(uri.getPort(), "80"); |
| 58 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 59 | |
| 60 | BOOST_CHECK(uri.parse("udp4://192.0.2.1:20")); |
| 61 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp4"); |
| 62 | BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1"); |
| 63 | BOOST_CHECK_EQUAL(uri.getPort(), "20"); |
| 64 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 65 | |
| 66 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363")); |
| 67 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
| 68 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1"); |
| 69 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
| 70 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 71 | |
| 72 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363")); |
| 73 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
| 74 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3"); |
| 75 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
| 76 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 77 | |
| 78 | BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false); |
| 79 | |
| 80 | using namespace boost::asio; |
| 81 | ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 82 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 83 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777"); |
| 84 | |
| 85 | ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 86 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 87 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777"); |
| 88 | } |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE(Tcp) |
| 91 | { |
| 92 | FaceUri uri; |
| 93 | |
| 94 | BOOST_CHECK(uri.parse("tcp://random.host.name")); |
| 95 | BOOST_CHECK_EQUAL(uri.getScheme(), "tcp"); |
| 96 | BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name"); |
| 97 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 98 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 99 | |
| 100 | BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false); |
| 101 | BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false); |
| 102 | |
| 103 | using namespace boost::asio; |
| 104 | ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 105 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 106 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777"); |
| 107 | |
| 108 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient")); |
| 109 | BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777"); |
| 110 | |
| 111 | ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 112 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 113 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777"); |
| 114 | } |
| 115 | |
| 116 | BOOST_AUTO_TEST_CASE(Unix) |
| 117 | { |
| 118 | FaceUri uri; |
| 119 | |
| 120 | BOOST_CHECK(uri.parse("unix:///var/run/example.sock")); |
| 121 | BOOST_CHECK_EQUAL(uri.getScheme(), "unix"); |
| 122 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 123 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 124 | BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock"); |
| 125 | |
| 126 | //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); |
| 127 | // This is not a valid unix:/ URI, but the parse would treat "var" as host |
| 128 | |
| 129 | #ifdef HAVE_UNIX_SOCKETS |
| 130 | using namespace boost::asio; |
| 131 | local::stream_protocol::endpoint endpoint("/var/run/example.sock"); |
| 132 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint)); |
| 133 | BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock"); |
| 134 | #endif // HAVE_UNIX_SOCKETS |
| 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE(Fd) |
| 138 | { |
| 139 | FaceUri uri; |
| 140 | |
| 141 | BOOST_CHECK(uri.parse("fd://6")); |
| 142 | BOOST_CHECK_EQUAL(uri.getScheme(), "fd"); |
| 143 | BOOST_CHECK_EQUAL(uri.getHost(), "6"); |
| 144 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 145 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 146 | |
| 147 | int fd = 21; |
| 148 | BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd)); |
| 149 | BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21"); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_CASE(Ether) |
| 153 | { |
| 154 | FaceUri uri; |
| 155 | |
| 156 | BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]")); |
| 157 | BOOST_CHECK_EQUAL(uri.getScheme(), "ether"); |
| 158 | BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01"); |
| 159 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 160 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 161 | |
| 162 | BOOST_CHECK_EQUAL(uri.parse("ether://08:00:27:zz:dd:01"), false); |
| 163 | |
| 164 | #ifdef HAVE_LIBPCAP |
| 165 | ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01"); |
| 166 | BOOST_REQUIRE_NO_THROW(FaceUri(address)); |
| 167 | BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]"); |
| 168 | #endif // HAVE_LIBPCAP |
| 169 | } |
| 170 | |
| 171 | BOOST_AUTO_TEST_CASE(Dev) |
| 172 | { |
| 173 | FaceUri uri; |
| 174 | |
| 175 | BOOST_CHECK(uri.parse("dev://eth0")); |
| 176 | BOOST_CHECK_EQUAL(uri.getScheme(), "dev"); |
| 177 | BOOST_CHECK_EQUAL(uri.getHost(), "eth0"); |
| 178 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 179 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 180 | |
| 181 | std::string ifname = "en1"; |
| 182 | BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname)); |
| 183 | BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1"); |
| 184 | } |
| 185 | |
| 186 | BOOST_AUTO_TEST_CASE(Bug1635) |
| 187 | { |
| 188 | FaceUri uri; |
| 189 | |
| 190 | BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366")); |
| 191 | BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient"); |
| 192 | BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239"); |
| 193 | BOOST_CHECK_EQUAL(uri.getPort(), "56366"); |
| 194 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 195 | BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366"); |
| 196 | } |
| 197 | |
| 198 | BOOST_AUTO_TEST_SUITE_END() |
| 199 | |
| 200 | } // namespace util |
| 201 | } // namespace ndn |