net: support link-local IPv6 addresses in FaceUri
Change-Id: Ia986847e60b0a21a94bf2e4ce99d4a5a688a2006
Refs: #1428
diff --git a/tests/unit-tests/net/address-converter.t.cpp b/tests/unit-tests/net/address-converter.t.cpp
new file mode 100644
index 0000000..712bf6d
--- /dev/null
+++ b/tests/unit-tests/net/address-converter.t.cpp
@@ -0,0 +1,159 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "net/address-converter.hpp"
+
+#include "boost-test.hpp"
+#include "collect-netifs.hpp"
+
+namespace ndn {
+namespace ip {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(Net)
+BOOST_AUTO_TEST_SUITE(TestAddressConverter)
+
+#define CHECK_IPV6_ADDRESS(address, string, scope) do { \
+ auto addrV6 = boost::asio::ip::address_v6::from_string(string); \
+ addrV6.scope_id(scope); \
+ BOOST_CHECK_EQUAL(address, addrV6); \
+} while (false)
+
+BOOST_AUTO_TEST_CASE(ScopeNameFromId)
+{
+ const auto& networkInterfaces = net::tests::collectNetworkInterfaces();
+ if (!networkInterfaces.empty()) {
+ const auto& netif = networkInterfaces.front();
+ auto index = netif->getIndex();
+ auto name = netif->getName();
+
+ BOOST_CHECK_EQUAL(scopeNameFromId(index).value(), name);
+ }
+
+ BOOST_CHECK(!scopeNameFromId(std::numeric_limits<unsigned int>::max()));
+}
+
+BOOST_AUTO_TEST_CASE(AddressFromString)
+{
+ boost::asio::ip::address addr;
+ boost::system::error_code ec;
+
+ // empty string
+ BOOST_CHECK_THROW(addressFromString(""), boost::system::system_error);
+ BOOST_CHECK_EQUAL(addressFromString("", ec), addr);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
+
+ // IPv4 address
+ BOOST_CHECK_EQUAL(addressFromString("192.168.0.1", ec),
+ boost::asio::ip::address::from_string("192.168.0.1"));
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
+
+ BOOST_CHECK_THROW(addressFromString("192.168.0"), boost::system::system_error);
+ BOOST_CHECK_EQUAL(addressFromString("192.168.0", ec), addr);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
+
+ BOOST_CHECK_THROW(addressFromString("192.168.0.1%"), boost::system::system_error);
+ BOOST_CHECK_EQUAL(addressFromString("192.168.0.1%", ec), addr);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
+
+ // regular IPv6 address
+ BOOST_CHECK_EQUAL(addressFromString("2001:db8::1", ec),
+ boost::asio::ip::address::from_string("2001:db8::1"));
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
+
+ BOOST_CHECK_THROW(addressFromString("2001:db8:::"), boost::system::system_error);
+ BOOST_CHECK_EQUAL(addressFromString("2001:db8:::", ec), addr);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
+
+ // link-local IPv6 address
+ const auto& networkInterfaces = net::tests::collectNetworkInterfaces();
+ if (!networkInterfaces.empty()) {
+ const auto& netif = networkInterfaces.front();
+ CHECK_IPV6_ADDRESS(addressFromString("fe80::1%" + netif->getName(), ec).to_v6(),
+ "fe80::1", netif->getIndex());
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
+ }
+}
+
+BOOST_AUTO_TEST_CASE(AddressV6FromString)
+{
+ boost::asio::ip::address_v6 addr;
+ boost::system::error_code ec;
+
+ // empty string
+ BOOST_CHECK_THROW(addressV6FromString(""), boost::system::system_error);
+ BOOST_CHECK_EQUAL(addressV6FromString("", ec), addr);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
+
+ // IPv4 address
+ BOOST_CHECK_THROW(addressV6FromString("192.168.0.1"), boost::system::system_error);
+ BOOST_CHECK_EQUAL(addressV6FromString("192.168.0.1", ec), addr);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
+
+ // regular IPv6 addresses
+ BOOST_CHECK_EQUAL(addressV6FromString("2001:db8::1", ec),
+ boost::asio::ip::address_v6::from_string("2001:db8::1", ec));
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
+
+ BOOST_CHECK_THROW(addressV6FromString("2001:db8:::"), boost::system::system_error);
+ BOOST_CHECK_EQUAL(addressV6FromString("2001:db8:::", ec), addr);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::invalid_argument);
+
+
+ const auto& networkInterfaces = net::tests::collectNetworkInterfaces();
+ if (!networkInterfaces.empty()) {
+ const auto& netif = networkInterfaces.front();
+ auto index = netif->getIndex();
+
+ CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%" + netif->getName(), ec), "fe80::1", index);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
+
+ CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%" + to_string(index), ec), "fe80::1", index);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
+ }
+
+ int invalidIndex = 0;
+ for (const auto& netif : networkInterfaces) {
+ invalidIndex += netif->getIndex();
+ }
+
+ // an invalid interface name will lead to a default scope id (i.e. 0) which means no scope
+ CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%NotAnInterface", ec), "fe80::1", 0);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
+
+ // supplying an interface index in the string won't trigger any checks on its validity
+ CHECK_IPV6_ADDRESS(addressV6FromString("fe80::1%" + to_string(invalidIndex), ec),
+ "fe80::1", invalidIndex);
+ BOOST_CHECK_EQUAL(ec, boost::system::errc::success);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestAddressConverter
+BOOST_AUTO_TEST_SUITE_END() // Net
+
+} // namespace tests
+} // namespace ip
+} // namespace ndn
diff --git a/tests/unit-tests/net/collect-netifs.cpp b/tests/unit-tests/net/collect-netifs.cpp
new file mode 100644
index 0000000..51bd71e
--- /dev/null
+++ b/tests/unit-tests/net/collect-netifs.cpp
@@ -0,0 +1,64 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "collect-netifs.hpp"
+#include "net/network-monitor.hpp"
+
+#include <boost/asio/io_service.hpp>
+
+namespace ndn {
+namespace net {
+namespace tests {
+
+std::vector<shared_ptr<const NetworkInterface>>
+collectNetworkInterfaces(bool allowCached)
+{
+ static std::vector<shared_ptr<const NetworkInterface>> cached;
+ // cached.empty() indicates there's no cached list of netifs.
+ // Although it could also mean a system without any network interface, this situation is rare
+ // because the loopback interface is present on almost all systems.
+
+ if (!allowCached || cached.empty()) {
+ boost::asio::io_service io;
+ NetworkMonitor netmon(io);
+ if ((netmon.getCapabilities() & NetworkMonitor::CAP_ENUM) == 0) {
+ BOOST_THROW_EXCEPTION(NetworkMonitor::Error("NetworkMonitor::CAP_ENUM is unavailable"));
+ }
+
+ netmon.onEnumerationCompleted.connect([&io] { io.stop(); });
+ io.run();
+ io.reset();
+
+ cached = netmon.listNetworkInterfaces();
+ }
+
+ return cached;
+}
+
+} // namespace tests
+} // namespace net
+} // namespace ndn
diff --git a/tests/unit-tests/net/collect-netifs.hpp b/tests/unit-tests/net/collect-netifs.hpp
new file mode 100644
index 0000000..c7e4e8f
--- /dev/null
+++ b/tests/unit-tests/net/collect-netifs.hpp
@@ -0,0 +1,52 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NFD_TESTS_UNIT_TESTS_NET_COLLECT_NETIFS_HPP
+#define NFD_TESTS_UNIT_TESTS_NET_COLLECT_NETIFS_HPP
+
+#include "common.hpp"
+#include "net/network-interface.hpp"
+
+#include <vector>
+
+namespace ndn {
+namespace net {
+namespace tests {
+
+/** \brief Collect information about network interfaces
+ * \param allowCached if true, previously collected information can be returned
+ * \note This function is blocking if \p allowCached is false or no previous information exists
+ * \throw ndn::net::NetworkMonitor::Error NetworkMonitor::CAP_ENUM is unavailable
+ */
+std::vector<shared_ptr<const NetworkInterface>>
+collectNetworkInterfaces(bool allowCached = true);
+
+} // namespace tests
+} // namespace net
+} // namespace ndn
+
+#endif // NFD_TESTS_UNIT_TESTS_NET_COLLECT_NETIFS_HPP
diff --git a/tests/unit-tests/net/face-uri.t.cpp b/tests/unit-tests/net/face-uri.t.cpp
index 27313f7..1dd40c8 100644
--- a/tests/unit-tests/net/face-uri.t.cpp
+++ b/tests/unit-tests/net/face-uri.t.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
@@ -28,6 +28,7 @@
#include "net/face-uri.hpp"
#include "boost-test.hpp"
+#include "collect-netifs.hpp"
#include "network-configuration-detector.hpp"
namespace ndn {
@@ -163,6 +164,15 @@
ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777");
+
+ BOOST_CHECK(uri.parse("udp6://[fe80::1%25eth1]:6363"));
+ BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1");
+
+ BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]:6363"));
+ BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1");
+
+ BOOST_CHECK(uri.parse("udp6://[fe80::1%1]:6363"));
+ BOOST_CHECK(uri.parse("udp6://[fe80::1%eth1]"));
}
BOOST_FIXTURE_TEST_CASE(IsCanonicalUdp, CanonizeFixture)
@@ -184,6 +194,18 @@
BOOST_CHECK_EQUAL(FaceUri("udp4://224.0.23.170:56363").isCanonical(), true);
BOOST_CHECK_EQUAL(FaceUri("udp4://[2001:db8::1]:6363").isCanonical(), false);
BOOST_CHECK_EQUAL(FaceUri("udp6://192.0.2.1:6363").isCanonical(), false);
+
+ const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
+ if (!networkInterfaces.empty()) {
+ const auto& netif = networkInterfaces.front();
+ auto name = netif->getName();
+ auto index = to_string(netif->getIndex());
+
+ BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
+ BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
+ BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1%" + name + "]").isCanonical(), false);
+ BOOST_CHECK_EQUAL(FaceUri("udp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
+ }
}
BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeUdpV4, 1)
@@ -238,6 +260,19 @@
// IPv4 used with udp6 protocol - not canonical
addTest("udp6://192.0.2.1:6363", false, "");
+ const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
+ if (!networkInterfaces.empty()) {
+ const auto& netif = networkInterfaces.front();
+ auto name = netif->getName();
+ auto index = to_string(netif->getIndex());
+
+ addTest("udp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
+ "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
+
+ addTest("udp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
+ "udp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
+ }
+
runTests();
}
@@ -266,6 +301,15 @@
ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777);
BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6));
BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777");
+
+ BOOST_CHECK(uri.parse("tcp6://[fe80::1%25eth1]:6363"));
+ BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%25eth1");
+
+ BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]:6363"));
+ BOOST_CHECK_EQUAL(uri.getHost(), "fe80::1%eth1");
+
+ BOOST_CHECK(uri.parse("tcp6://[fe80::1%1]:6363"));
+ BOOST_CHECK(uri.parse("tcp6://[fe80::1%eth1]"));
}
BOOST_FIXTURE_TEST_CASE(IsCanonicalTcp, CanonizeFixture)
@@ -287,6 +331,18 @@
BOOST_CHECK_EQUAL(FaceUri("tcp4://224.0.23.170:56363").isCanonical(), false);
BOOST_CHECK_EQUAL(FaceUri("tcp4://[2001:db8::1]:6363").isCanonical(), false);
BOOST_CHECK_EQUAL(FaceUri("tcp6://192.0.2.1:6363").isCanonical(), false);
+
+ const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
+ if (!networkInterfaces.empty()) {
+ const auto& netif = networkInterfaces.front();
+ auto name = netif->getName();
+ auto index = to_string(netif->getIndex());
+
+ BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]:6363").isCanonical(), true);
+ BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + index + "]:6363").isCanonical(), false);
+ BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1%" + name + "]").isCanonical(), false);
+ BOOST_CHECK_EQUAL(FaceUri("tcp6://[fe80::1068:dddb:fe26:fe3f%25en0]:6363").isCanonical(), false);
+ }
}
BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanonizeTcpV4, 1)
@@ -314,6 +370,19 @@
// IPv6 used with tcp4 protocol - not canonical
addTest("tcp4://[2001:db8::1]:6363", false, "");
+ const auto& networkInterfaces = ndn::net::tests::collectNetworkInterfaces();
+ if (!networkInterfaces.empty()) {
+ const auto& netif = networkInterfaces.front();
+ auto name = netif->getName();
+ auto index = to_string(netif->getIndex());
+
+ addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%25" + name + "]:6363", true,
+ "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
+
+ addTest("tcp6://[fe80::1068:dddb:fe26:fe3f%" + index + "]:6363", true,
+ "tcp6://[fe80::1068:dddb:fe26:fe3f%" + name + "]:6363");
+ }
+
runTests();
}