Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame^] | 2 | /* |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | * The University of Memphis. |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 28 | #include "net/face-uri.hpp" |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 29 | |
| 30 | #include "boost-test.hpp" |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame^] | 31 | #include "collect-netifs.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 32 | #include "network-configuration-detector.hpp" |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 35 | namespace tests { |
| 36 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Net) |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(TestFaceUri) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 40 | class CanonizeFixture : noncopyable |
| 41 | { |
| 42 | protected: |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 43 | void |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 44 | addTest(const std::string& request, bool shouldSucceed, const std::string& expectedUri) |
| 45 | { |
| 46 | ++m_nPending; |
| 47 | auto tc = make_shared<CanonizeTestCase>(request, shouldSucceed, expectedUri); |
| 48 | |
| 49 | FaceUri uri(request); |
| 50 | uri.canonize(bind(&CanonizeFixture::onCanonizeSuccess, this, tc, _1), |
| 51 | bind(&CanonizeFixture::onCanonizeFailure, this, tc, _1), |
| 52 | m_io, time::seconds(10)); |
| 53 | } |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 54 | |
| 55 | void |
| 56 | runTests() |
| 57 | { |
| 58 | m_io.run(); |
| 59 | BOOST_CHECK_EQUAL(m_nPending, 0); |
| 60 | } |
| 61 | |
| 62 | private: |
Davide Pesavento | 50df562 | 2015-03-19 22:05:05 +0100 | [diff] [blame] | 63 | class CanonizeTestCase |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 64 | { |
| 65 | public: |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 66 | CanonizeTestCase(const std::string& request, bool shouldSucceed, const std::string& expectedUri) |
| 67 | : m_expectedUri(expectedUri) |
Davide Pesavento | 50df562 | 2015-03-19 22:05:05 +0100 | [diff] [blame] | 68 | , m_message(request + " should " + (shouldSucceed ? "succeed" : "fail")) |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 69 | , m_shouldSucceed(shouldSucceed) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 70 | , m_isCompleted(false) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | public: |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 75 | std::string m_expectedUri; |
Davide Pesavento | 50df562 | 2015-03-19 22:05:05 +0100 | [diff] [blame] | 76 | std::string m_message; |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 77 | bool m_shouldSucceed; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 78 | bool m_isCompleted; |
| 79 | }; |
| 80 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 81 | void |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 82 | onCanonizeSuccess(const shared_ptr<CanonizeTestCase>& tc, const FaceUri& canonicalUri) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 83 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(tc->m_isCompleted, false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 85 | tc->m_isCompleted = true; |
| 86 | --m_nPending; |
| 87 | |
Davide Pesavento | 50df562 | 2015-03-19 22:05:05 +0100 | [diff] [blame] | 88 | BOOST_CHECK_MESSAGE(tc->m_shouldSucceed, tc->m_message); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 89 | if (tc->m_shouldSucceed) { |
| 90 | BOOST_CHECK_EQUAL(canonicalUri.toString(), tc->m_expectedUri); |
| 91 | } |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 94 | void |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 95 | onCanonizeFailure(const shared_ptr<CanonizeTestCase>& tc, const std::string& reason) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 96 | { |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(tc->m_isCompleted, false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 98 | tc->m_isCompleted = true; |
| 99 | --m_nPending; |
| 100 | |
Davide Pesavento | 50df562 | 2015-03-19 22:05:05 +0100 | [diff] [blame] | 101 | BOOST_CHECK_MESSAGE(!tc->m_shouldSucceed, tc->m_message); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private: |
| 105 | boost::asio::io_service m_io; |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 106 | ssize_t m_nPending = 0; |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(ParseInternal) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 110 | { |
| 111 | FaceUri uri; |
| 112 | |
| 113 | BOOST_CHECK(uri.parse("internal://")); |
| 114 | BOOST_CHECK_EQUAL(uri.getScheme(), "internal"); |
| 115 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 116 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 117 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 118 | |
| 119 | BOOST_CHECK_EQUAL(uri.parse("internal:"), false); |
| 120 | BOOST_CHECK_EQUAL(uri.parse("internal:/"), false); |
| 121 | } |
| 122 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(ParseUdp) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 124 | { |
| 125 | BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363")); |
| 126 | BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error); |
| 127 | BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error); |
| 128 | |
| 129 | FaceUri uri; |
| 130 | BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false); |
| 131 | |
| 132 | BOOST_CHECK(uri.parse("udp://hostname:80")); |
| 133 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp"); |
| 134 | BOOST_CHECK_EQUAL(uri.getHost(), "hostname"); |
| 135 | BOOST_CHECK_EQUAL(uri.getPort(), "80"); |
| 136 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 137 | |
| 138 | BOOST_CHECK(uri.parse("udp4://192.0.2.1:20")); |
| 139 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp4"); |
| 140 | BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1"); |
| 141 | BOOST_CHECK_EQUAL(uri.getPort(), "20"); |
| 142 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 143 | |
| 144 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363")); |
| 145 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
| 146 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1"); |
| 147 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
| 148 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 149 | |
| 150 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363")); |
| 151 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
| 152 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3"); |
| 153 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
| 154 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 155 | |
| 156 | BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false); |
| 157 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 158 | namespace ip = boost::asio::ip; |
| 159 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 160 | ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 161 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 162 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777"); |
| 163 | |
| 164 | ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 165 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 166 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777"); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame^] | 167 | |
| 168 | BOOST_CHECK(uri.parse("udp6://[fe80::1%25eth1]:6363")); |
| 169 | BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1"); |
| 170 | |
| 171 | BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]:6363")); |
| 172 | BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1"); |
| 173 | |
| 174 | BOOST_CHECK(uri.parse("udp6://[fe80::1%1]:6363")); |
| 175 | BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]")); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 178 | BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 179 | { |
| 180 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp"), true); |
| 181 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp4"), true); |
| 182 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("udp6"), true); |
| 183 | |
| 184 | BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363").isCanonical(), true); |
| 185 | BOOST_CHECK_EQUAL(FaceUri("udp://192.0.2.1:6363").isCanonical(), false); |
| 186 | BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1").isCanonical(), false); |
| 187 | BOOST_CHECK_EQUAL(FaceUri("udp4://192.0.2.1:6363/").isCanonical(), false); |
| 188 | BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::1]:6363").isCanonical(), true); |
| 189 | BOOST_CHECK_EQUAL(FaceUri("udp6://[2001:db8::01]:6363").isCanonical(), false); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(FaceUri("udp://[2001:db8::1]:6363").isCanonical(), false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 191 | BOOST_CHECK_EQUAL(FaceUri("udp://example.net:6363").isCanonical(), false); |
| 192 | BOOST_CHECK_EQUAL(FaceUri("udp4://example.net:6363").isCanonical(), false); |
| 193 | BOOST_CHECK_EQUAL(FaceUri("udp6://example.net:6363").isCanonical(), false); |
| 194 | BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(FaceUri("udp4://[2001:db8::1]:6363").isCanonical(), false); |
| 196 | BOOST_CHECK_EQUAL(FaceUri("udp6://192.0.2.1:6363").isCanonical(), false); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame^] | 197 | |
| 198 | const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces(); |
| 199 | if (!networkInterfaces.empty()) { |
| 200 | const auto& netif = networkInterfaces.front(); |
| 201 | auto name = netif->getName(); |
| 202 | auto index = to_string(netif->getIndex()); |
| 203 | |
| 204 | BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]:6363").isCanonical(), true); |
| 205 | BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + index + "]:6363").isCanonical(), false); |
| 206 | BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]").isCanonical(), false); |
| 207 | BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false); |
| 208 | } |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 211 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1) |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 212 | BOOST_FIXTURE_TEST_CASE(CanonizeUdpV4, CanonizeFixture) |
| 213 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 214 | SKIP_IF_IPV4_UNAVAILABLE(); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 215 | |
| 216 | // IPv4 unicast |
| 217 | addTest("udp4://192.0.2.1:6363", true, "udp4://192.0.2.1:6363"); |
| 218 | addTest("udp://192.0.2.2:6363", true, "udp4://192.0.2.2:6363"); |
| 219 | addTest("udp4://192.0.2.3", true, "udp4://192.0.2.3:6363"); |
| 220 | addTest("udp4://192.0.2.4:6363/", true, "udp4://192.0.2.4:6363"); |
| 221 | addTest("udp4://192.0.2.5:9695", true, "udp4://192.0.2.5:9695"); |
| 222 | addTest("udp4://192.0.2.666:6363", false, ""); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 223 | addTest("udp4://192.0.2.7:99999", false, ""); // Bug #3897 |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 224 | addTest("udp4://google-public-dns-a.google.com", true, "udp4://8.8.8.8:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 225 | addTest("udp4://google-public-dns-a.google.com:70000", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 226 | addTest("udp4://invalid.invalid", false, ""); |
| 227 | |
| 228 | // IPv4 multicast |
| 229 | addTest("udp4://224.0.23.170:56363", true, "udp4://224.0.23.170:56363"); |
| 230 | addTest("udp4://224.0.23.170", true, "udp4://224.0.23.170:56363"); |
| 231 | addTest("udp4://all-routers.mcast.net:56363", true, "udp4://224.0.0.2:56363"); |
| 232 | |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 233 | // IPv6 used with udp4 protocol - not canonical |
| 234 | addTest("udp4://[2001:db8::1]:6363", false, ""); |
| 235 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 236 | runTests(); |
| 237 | } |
| 238 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 239 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV6, 1) |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 240 | BOOST_FIXTURE_TEST_CASE(CanonizeUdpV6, CanonizeFixture) |
| 241 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 242 | SKIP_IF_IPV6_UNAVAILABLE(); |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 243 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 244 | // IPv6 unicast |
| 245 | addTest("udp6://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 246 | addTest("udp6://[2001:db8::1]", true, "udp6://[2001:db8::1]:6363"); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 247 | addTest("udp://[2001:db8::1]:6363", true, "udp6://[2001:db8::1]:6363"); |
| 248 | addTest("udp6://[2001:db8::01]:6363", true, "udp6://[2001:db8::1]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 249 | addTest("udp6://[2001::db8::1]:6363", false, ""); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 250 | addTest("udp6://[2001:db8::1]:99999", false, ""); // Bug #3897 |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 251 | addTest("udp6://google-public-dns-a.google.com", true, "udp6://[2001:4860:4860::8888]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 252 | addTest("udp6://google-public-dns-a.google.com:70000", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 253 | addTest("udp6://invalid.invalid", false, ""); |
| 254 | addTest("udp://invalid.invalid", false, ""); |
| 255 | |
| 256 | // IPv6 multicast |
| 257 | addTest("udp6://[ff02::2]:56363", true, "udp6://[ff02::2]:56363"); |
| 258 | addTest("udp6://[ff02::2]", true, "udp6://[ff02::2]:56363"); |
| 259 | |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 260 | // IPv4 used with udp6 protocol - not canonical |
| 261 | addTest("udp6://192.0.2.1:6363", false, ""); |
| 262 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame^] | 263 | const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces(); |
| 264 | if (!networkInterfaces.empty()) { |
| 265 | const auto& netif = networkInterfaces.front(); |
| 266 | auto name = netif->getName(); |
| 267 | auto index = to_string(netif->getIndex()); |
| 268 | |
| 269 | addTest("udp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true, |
| 270 | "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363"); |
| 271 | |
| 272 | addTest("udp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true, |
| 273 | "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363"); |
| 274 | } |
| 275 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 276 | runTests(); |
| 277 | } |
| 278 | |
| 279 | BOOST_AUTO_TEST_CASE(ParseTcp) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 280 | { |
| 281 | FaceUri uri; |
| 282 | |
| 283 | BOOST_CHECK(uri.parse("tcp://random.host.name")); |
| 284 | BOOST_CHECK_EQUAL(uri.getScheme(), "tcp"); |
| 285 | BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name"); |
| 286 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 287 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 288 | |
| 289 | BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false); |
| 290 | BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false); |
| 291 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 292 | namespace ip = boost::asio::ip; |
| 293 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 294 | ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 295 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 296 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777"); |
| 297 | |
| 298 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4, "wsclient")); |
| 299 | BOOST_CHECK_EQUAL(FaceUri(endpoint4, "wsclient").toString(), "wsclient://192.0.2.1:7777"); |
| 300 | |
| 301 | ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 302 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 303 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777"); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame^] | 304 | |
| 305 | BOOST_CHECK(uri.parse("tcp6://[fe80::1%25eth1]:6363")); |
| 306 | BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1"); |
| 307 | |
| 308 | BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]:6363")); |
| 309 | BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1"); |
| 310 | |
| 311 | BOOST_CHECK(uri.parse("tcp6://[fe80::1%1]:6363")); |
| 312 | BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]")); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 315 | BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 316 | { |
| 317 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp"), true); |
| 318 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp4"), true); |
| 319 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("tcp6"), true); |
| 320 | |
| 321 | BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363").isCanonical(), true); |
| 322 | BOOST_CHECK_EQUAL(FaceUri("tcp://192.0.2.1:6363").isCanonical(), false); |
| 323 | BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1").isCanonical(), false); |
| 324 | BOOST_CHECK_EQUAL(FaceUri("tcp4://192.0.2.1:6363/").isCanonical(), false); |
| 325 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::1]:6363").isCanonical(), true); |
| 326 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[2001:db8::01]:6363").isCanonical(), false); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 327 | BOOST_CHECK_EQUAL(FaceUri("tcp://[2001:db8::1]:6363").isCanonical(), false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 328 | BOOST_CHECK_EQUAL(FaceUri("tcp://example.net:6363").isCanonical(), false); |
| 329 | BOOST_CHECK_EQUAL(FaceUri("tcp4://example.net:6363").isCanonical(), false); |
| 330 | BOOST_CHECK_EQUAL(FaceUri("tcp6://example.net:6363").isCanonical(), false); |
| 331 | BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false); |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 332 | BOOST_CHECK_EQUAL(FaceUri("tcp4://[2001:db8::1]:6363").isCanonical(), false); |
| 333 | BOOST_CHECK_EQUAL(FaceUri("tcp6://192.0.2.1:6363").isCanonical(), false); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame^] | 334 | |
| 335 | const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces(); |
| 336 | if (!networkInterfaces.empty()) { |
| 337 | const auto& netif = networkInterfaces.front(); |
| 338 | auto name = netif->getName(); |
| 339 | auto index = to_string(netif->getIndex()); |
| 340 | |
| 341 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]:6363").isCanonical(), true); |
| 342 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + index + "]:6363").isCanonical(), false); |
| 343 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]").isCanonical(), false); |
| 344 | BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false); |
| 345 | } |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 348 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1) |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 349 | BOOST_FIXTURE_TEST_CASE(CanonizeTcpV4, CanonizeFixture) |
| 350 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 351 | SKIP_IF_IPV4_UNAVAILABLE(); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 352 | |
| 353 | // IPv4 unicast |
| 354 | addTest("tcp4://192.0.2.1:6363", true, "tcp4://192.0.2.1:6363"); |
| 355 | addTest("tcp://192.0.2.2:6363", true, "tcp4://192.0.2.2:6363"); |
| 356 | addTest("tcp4://192.0.2.3", true, "tcp4://192.0.2.3:6363"); |
| 357 | addTest("tcp4://192.0.2.4:6363/", true, "tcp4://192.0.2.4:6363"); |
| 358 | addTest("tcp4://192.0.2.5:9695", true, "tcp4://192.0.2.5:9695"); |
| 359 | addTest("tcp4://192.0.2.666:6363", false, ""); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 360 | addTest("tcp4://192.0.2.7:99999", false, ""); // Bug #3897 |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 361 | addTest("tcp4://google-public-dns-a.google.com", true, "tcp4://8.8.8.8:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 362 | addTest("tcp4://google-public-dns-a.google.com:70000", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 363 | addTest("tcp4://invalid.invalid", false, ""); |
| 364 | |
| 365 | // IPv4 multicast |
| 366 | addTest("tcp4://224.0.23.170:56363", false, ""); |
| 367 | addTest("tcp4://224.0.23.170", false, ""); |
| 368 | addTest("tcp4://all-routers.mcast.net:56363", false, ""); |
| 369 | |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 370 | // IPv6 used with tcp4 protocol - not canonical |
| 371 | addTest("tcp4://[2001:db8::1]:6363", false, ""); |
| 372 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame^] | 373 | const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces(); |
| 374 | if (!networkInterfaces.empty()) { |
| 375 | const auto& netif = networkInterfaces.front(); |
| 376 | auto name = netif->getName(); |
| 377 | auto index = to_string(netif->getIndex()); |
| 378 | |
| 379 | addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true, |
| 380 | "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363"); |
| 381 | |
| 382 | addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true, |
| 383 | "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363"); |
| 384 | } |
| 385 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 386 | runTests(); |
| 387 | } |
| 388 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 389 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV6, 1) |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 390 | BOOST_FIXTURE_TEST_CASE(CanonizeTcpV6, CanonizeFixture) |
| 391 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 392 | SKIP_IF_IPV6_UNAVAILABLE(); |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 393 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 394 | // IPv6 unicast |
| 395 | addTest("tcp6://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 396 | addTest("tcp6://[2001:db8::1]", true, "tcp6://[2001:db8::1]:6363"); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 397 | addTest("tcp://[2001:db8::1]:6363", true, "tcp6://[2001:db8::1]:6363"); |
| 398 | addTest("tcp6://[2001:db8::01]:6363", true, "tcp6://[2001:db8::1]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 399 | addTest("tcp6://[2001::db8::1]:6363", false, ""); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 400 | addTest("tcp6://[2001:db8::1]:99999", false, ""); // Bug #3897 |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 401 | addTest("tcp6://google-public-dns-a.google.com", true, "tcp6://[2001:4860:4860::8888]:6363"); |
Davide Pesavento | f78cb70 | 2016-12-11 14:42:40 -0500 | [diff] [blame] | 402 | addTest("tcp6://google-public-dns-a.google.com:70000", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 403 | addTest("tcp6://invalid.invalid", false, ""); |
| 404 | addTest("tcp://invalid.invalid", false, ""); |
| 405 | |
| 406 | // IPv6 multicast |
| 407 | addTest("tcp6://[ff02::2]:56363", false, ""); |
| 408 | addTest("tcp6://[ff02::2]", false, ""); |
| 409 | |
Eric Newberry | 71aecae | 2017-05-29 00:22:39 -0700 | [diff] [blame] | 410 | // IPv4 used with tcp6 protocol - not canonical |
| 411 | addTest("tcp6://192.0.2.1:6363", false, ""); |
| 412 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 413 | runTests(); |
| 414 | } |
| 415 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 416 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseUnix, 1) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 417 | BOOST_AUTO_TEST_CASE(ParseUnix) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 418 | { |
| 419 | FaceUri uri; |
| 420 | |
| 421 | BOOST_CHECK(uri.parse("unix:///var/run/example.sock")); |
| 422 | BOOST_CHECK_EQUAL(uri.getScheme(), "unix"); |
| 423 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 424 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 425 | BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock"); |
| 426 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 427 | // This is not a valid unix:// URI, but the parse would treat "var" as host |
| 428 | BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); // Bug #3896 |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 429 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 430 | #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS |
| 431 | using boost::asio::local::stream_protocol; |
| 432 | stream_protocol::endpoint endpoint("/var/run/example.sock"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 433 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint)); |
| 434 | BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock"); |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 435 | #endif // BOOST_ASIO_HAS_LOCAL_SOCKETS |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 438 | BOOST_AUTO_TEST_CASE(ParseFd) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 439 | { |
| 440 | FaceUri uri; |
| 441 | |
| 442 | BOOST_CHECK(uri.parse("fd://6")); |
| 443 | BOOST_CHECK_EQUAL(uri.getScheme(), "fd"); |
| 444 | BOOST_CHECK_EQUAL(uri.getHost(), "6"); |
| 445 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 446 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 447 | |
| 448 | int fd = 21; |
| 449 | BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd)); |
| 450 | BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21"); |
| 451 | } |
| 452 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 453 | BOOST_AUTO_TEST_CASE(ParseEther) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 454 | { |
| 455 | FaceUri uri; |
| 456 | |
| 457 | BOOST_CHECK(uri.parse("ether://[08:00:27:01:dd:01]")); |
| 458 | BOOST_CHECK_EQUAL(uri.getScheme(), "ether"); |
| 459 | BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01"); |
| 460 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 461 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 462 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 463 | BOOST_CHECK_EQUAL(uri.parse("ether://[08:00:27:zz:dd:01]"), false); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 464 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 465 | auto address = ethernet::Address::fromString("33:33:01:01:01:01"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 466 | BOOST_REQUIRE_NO_THROW(FaceUri(address)); |
| 467 | BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://[33:33:01:01:01:01]"); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 470 | BOOST_FIXTURE_TEST_CASE(CanonizeEther, CanonizeFixture) |
| 471 | { |
| 472 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("ether"), true); |
| 473 | |
| 474 | BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]").isCanonical(), true); |
| 475 | BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:1:1:1]").isCanonical(), false); |
| 476 | BOOST_CHECK_EQUAL(FaceUri("ether://[08:00:27:01:01:01]/").isCanonical(), false); |
| 477 | BOOST_CHECK_EQUAL(FaceUri("ether://[33:33:01:01:01:01]").isCanonical(), true); |
| 478 | |
| 479 | addTest("ether://[08:00:27:01:01:01]", true, "ether://[08:00:27:01:01:01]"); |
| 480 | addTest("ether://[08:00:27:1:1:1]", true, "ether://[08:00:27:01:01:01]"); |
| 481 | addTest("ether://[08:00:27:01:01:01]/", true, "ether://[08:00:27:01:01:01]"); |
| 482 | addTest("ether://[33:33:01:01:01:01]", true, "ether://[33:33:01:01:01:01]"); |
| 483 | |
| 484 | runTests(); |
| 485 | } |
| 486 | |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 487 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ParseDev, 1) |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 488 | BOOST_AUTO_TEST_CASE(ParseDev) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 489 | { |
| 490 | FaceUri uri; |
| 491 | |
| 492 | BOOST_CHECK(uri.parse("dev://eth0")); |
| 493 | BOOST_CHECK_EQUAL(uri.getScheme(), "dev"); |
| 494 | BOOST_CHECK_EQUAL(uri.getHost(), "eth0"); |
| 495 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 496 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 497 | |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 498 | BOOST_CHECK_EQUAL(uri.parse("dev://eth0:8888"), false); // Bug #3896 |
| 499 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 500 | std::string ifname = "en1"; |
Junxiao Shi | d5c2f0c | 2017-04-04 09:52:11 +0000 | [diff] [blame] | 501 | BOOST_REQUIRE_NO_THROW(uri = FaceUri::fromDev(ifname)); |
| 502 | BOOST_CHECK_EQUAL(uri.toString(), "dev://en1"); |
| 503 | } |
| 504 | |
| 505 | BOOST_AUTO_TEST_CASE(IsCanonicalDev) |
| 506 | { |
| 507 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("dev"), true); |
| 508 | |
| 509 | BOOST_CHECK_EQUAL(FaceUri("dev://eth0").isCanonical(), true); |
| 510 | BOOST_CHECK_EQUAL(FaceUri("dev://").isCanonical(), false); |
| 511 | BOOST_CHECK_EQUAL(FaceUri("dev://eth0:8888").isCanonical(), false); |
| 512 | BOOST_CHECK_EQUAL(FaceUri("dev://eth0/").isCanonical(), false); |
| 513 | BOOST_CHECK_EQUAL(FaceUri("dev://eth0/A").isCanonical(), false); |
| 514 | } |
| 515 | |
| 516 | BOOST_FIXTURE_TEST_CASE(CanonizeDev, CanonizeFixture) |
| 517 | { |
| 518 | addTest("dev://eth0", true, "dev://eth0"); |
| 519 | addTest("dev://", false, ""); |
| 520 | addTest("dev://eth0:8888", false, ""); |
| 521 | addTest("dev://eth0/", true, "dev://eth0"); |
| 522 | addTest("dev://eth0/A", false, ""); |
| 523 | |
| 524 | runTests(); |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 527 | BOOST_AUTO_TEST_CASE(ParseUdpDev) |
| 528 | { |
| 529 | FaceUri uri; |
| 530 | |
| 531 | BOOST_CHECK(uri.parse("udp4+dev://eth0:7777")); |
| 532 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp4+dev"); |
| 533 | BOOST_CHECK_EQUAL(uri.getHost(), "eth0"); |
| 534 | BOOST_CHECK_EQUAL(uri.getPort(), "7777"); |
| 535 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 536 | |
| 537 | BOOST_CHECK(uri.parse("udp6+dev://eth1:7777")); |
| 538 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6+dev"); |
| 539 | BOOST_CHECK_EQUAL(uri.getHost(), "eth1"); |
| 540 | BOOST_CHECK_EQUAL(uri.getPort(), "7777"); |
| 541 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 542 | |
| 543 | BOOST_CHECK(uri.parse("abc+efg://eth0")); |
| 544 | BOOST_CHECK(!uri.parse("abc+://eth0")); |
| 545 | BOOST_CHECK(!uri.parse("+abc://eth0")); |
| 546 | |
Davide Pesavento | e1081cd | 2016-12-19 23:58:15 -0500 | [diff] [blame] | 547 | namespace ip = boost::asio::ip; |
Weiwei Liu | d7f4fda | 2016-10-19 22:38:39 -0700 | [diff] [blame] | 548 | |
| 549 | ip::udp::endpoint endpoint4(ip::udp::v4(), 7777); |
| 550 | BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint4, "en1")); |
| 551 | BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint4, "en1").toString(), "udp4+dev://en1:7777"); |
| 552 | |
| 553 | ip::udp::endpoint endpoint6(ip::udp::v6(), 7777); |
| 554 | BOOST_REQUIRE_NO_THROW(FaceUri::fromUdpDev(endpoint6, "en2")); |
| 555 | BOOST_CHECK_EQUAL(FaceUri::fromUdpDev(endpoint6, "en2").toString(), "udp6+dev://en2:7777"); |
| 556 | } |
| 557 | |
| 558 | BOOST_FIXTURE_TEST_CASE(CanonizeUdpDev, CanonizeFixture) |
| 559 | { |
| 560 | BOOST_CHECK_EQUAL(FaceUri("udp4+dev://eth0:7777").isCanonical(), true); |
| 561 | BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1:7777").isCanonical(), true); |
| 562 | BOOST_CHECK_EQUAL(FaceUri("udp+dev://eth1:7777").isCanonical(), false); |
| 563 | BOOST_CHECK_EQUAL(FaceUri("udp6+dev://eth1").isCanonical(), false); |
| 564 | |
| 565 | addTest("udp4+dev://en0:7777", true, "udp4+dev://en0:7777"); |
| 566 | addTest("udp6+dev://en0:7777", true, "udp6+dev://en0:7777"); |
| 567 | addTest("udp+dev://en1:7777", false, ""); |
| 568 | addTest("udp6+dev://en2", false, ""); |
| 569 | } |
| 570 | |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 571 | BOOST_AUTO_TEST_CASE(CanonizeEmptyCallback) |
| 572 | { |
| 573 | boost::asio::io_service io; |
| 574 | |
| 575 | // unsupported scheme |
| 576 | FaceUri("null://").canonize(FaceUri::CanonizeSuccessCallback(), |
| 577 | FaceUri::CanonizeFailureCallback(), |
| 578 | io, time::milliseconds(1)); |
| 579 | |
| 580 | // cannot resolve |
| 581 | FaceUri("udp://192.0.2.333").canonize(FaceUri::CanonizeSuccessCallback(), |
| 582 | FaceUri::CanonizeFailureCallback(), |
| 583 | io, time::milliseconds(1)); |
| 584 | |
| 585 | // already canonical |
| 586 | FaceUri("udp4://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(), |
| 587 | FaceUri::CanonizeFailureCallback(), |
| 588 | io, time::milliseconds(1)); |
| 589 | |
| 590 | // need DNS resolution |
| 591 | FaceUri("udp://192.0.2.1:6363").canonize(FaceUri::CanonizeSuccessCallback(), |
| 592 | FaceUri::CanonizeFailureCallback(), |
| 593 | io, time::milliseconds(1)); |
| 594 | |
| 595 | io.run(); // should not crash |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 596 | |
| 597 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 598 | BOOST_CHECK(true); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | BOOST_FIXTURE_TEST_CASE(CanonizeUnsupported, CanonizeFixture) |
| 602 | { |
| 603 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("internal"), false); |
| 604 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("null"), false); |
| 605 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("unix"), false); |
| 606 | BOOST_CHECK_EQUAL(FaceUri::canCanonize("fd"), false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 607 | |
| 608 | BOOST_CHECK_EQUAL(FaceUri("internal://").isCanonical(), false); |
| 609 | BOOST_CHECK_EQUAL(FaceUri("null://").isCanonical(), false); |
| 610 | BOOST_CHECK_EQUAL(FaceUri("unix:///var/run/nfd.sock").isCanonical(), false); |
| 611 | BOOST_CHECK_EQUAL(FaceUri("fd://0").isCanonical(), false); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 612 | |
| 613 | addTest("internal://", false, ""); |
| 614 | addTest("null://", false, ""); |
| 615 | addTest("unix:///var/run/nfd.sock", false, ""); |
| 616 | addTest("fd://0", false, ""); |
Junxiao Shi | 4083c8d | 2014-10-12 16:43:16 -0700 | [diff] [blame] | 617 | |
| 618 | runTests(); |
| 619 | } |
| 620 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 621 | BOOST_AUTO_TEST_CASE(Bug1635) |
| 622 | { |
| 623 | FaceUri uri; |
| 624 | |
| 625 | BOOST_CHECK(uri.parse("wsclient://[::ffff:76.90.11.239]:56366")); |
| 626 | BOOST_CHECK_EQUAL(uri.getScheme(), "wsclient"); |
| 627 | BOOST_CHECK_EQUAL(uri.getHost(), "76.90.11.239"); |
| 628 | BOOST_CHECK_EQUAL(uri.getPort(), "56366"); |
| 629 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 630 | BOOST_CHECK_EQUAL(uri.toString(), "wsclient://76.90.11.239:56366"); |
| 631 | } |
| 632 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 633 | BOOST_AUTO_TEST_SUITE_END() // TestFaceUri |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 634 | BOOST_AUTO_TEST_SUITE_END() // Net |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 635 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 636 | } // namespace tests |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 637 | } // namespace ndn |