net: support link-local IPv6 addresses in FaceUri

Change-Id: Ia986847e60b0a21a94bf2e4ce99d4a5a688a2006
Refs: #1428
diff --git a/src/net/address-converter.cpp b/src/net/address-converter.cpp
new file mode 100644
index 0000000..b6f2de2
--- /dev/null
+++ b/src/net/address-converter.cpp
@@ -0,0 +1,151 @@
+/* -*- 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 "address-converter.hpp"
+
+#if BOOST_VERSION < 105800
+#include <boost/algorithm/string.hpp>
+#include <boost/lexical_cast.hpp>
+#include <vector>
+#endif // BOOST_VERSION < 105800
+
+#include <net/if.h> // for if_nametoindex and if_indextoname
+
+namespace ndn {
+namespace ip {
+
+optional<std::string>
+scopeNameFromId(unsigned int scopeId)
+{
+  char buffer[IFNAMSIZ];
+  auto scopeName = if_indextoname(scopeId, buffer);
+  if (scopeName != nullptr) {
+    return std::string(scopeName);
+  }
+  return nullopt;
+}
+
+#if BOOST_VERSION < 105800
+static unsigned int
+scopeIdFromString(const std::string& scope)
+{
+  auto id = if_nametoindex(scope.c_str());
+  if (id != 0) {
+    return id;
+  }
+
+  // cannot find a corresponding index, assume it's not a name but an interface index
+  try {
+    return boost::lexical_cast<unsigned int>(scope);
+  }
+  catch (const boost::bad_lexical_cast&) {
+    return 0;
+  }
+}
+
+struct ParsedAddress
+{
+  boost::asio::ip::address addr;
+  std::string scope;
+};
+
+static ParsedAddress
+parseAddressFromString(const std::string& address, boost::system::error_code& ec)
+{
+  std::vector<std::string> parseResult;
+  boost::algorithm::split(parseResult, address, boost::is_any_of("%"));
+  auto addr = boost::asio::ip::address::from_string(parseResult[0], ec);
+
+  switch (parseResult.size()) {
+  case 1:
+    // regular address
+    return {addr, ""};
+  case 2:
+    // the presence of % in either an IPv4 address or a regular IPv6 address is invalid
+    if (!ec && addr.is_v6() && addr.to_v6().is_link_local()) {
+      return {addr, parseResult[1]};
+    }
+    // Fallthrough, if the presence of % is invalid set ec to invalid_argument
+  default:
+    ec = boost::asio::error::invalid_argument;
+    return {};
+  }
+}
+#endif // BOOST_VERSION < 105800
+
+boost::asio::ip::address
+addressFromString(const std::string& address, boost::system::error_code& ec)
+{
+  // boost < 1.58 cannot recognize scope-id in link-local IPv6 address
+#if BOOST_VERSION < 105800
+  auto parsedAddress = parseAddressFromString(address, ec);
+  if (ec || parsedAddress.addr.is_v4()) {
+    return parsedAddress.addr;
+  }
+  auto addr = parsedAddress.addr.to_v6();
+  addr.scope_id(scopeIdFromString(parsedAddress.scope));
+  return addr;
+#else
+  return boost::asio::ip::address::from_string(address, ec);
+#endif // BOOST_VERSION < 105800
+}
+
+boost::asio::ip::address
+addressFromString(const std::string& address)
+{
+  boost::system::error_code ec;
+  auto addr = addressFromString(address, ec);
+  if (ec) {
+    BOOST_THROW_EXCEPTION(boost::system::system_error(ec));
+  }
+  return addr;
+}
+
+boost::asio::ip::address_v6
+addressV6FromString(const std::string& address, boost::system::error_code& ec)
+{
+  auto addr = addressFromString(address, ec);
+  if (ec || addr.is_v4()) {
+    ec = boost::asio::error::invalid_argument;
+    return {};
+  }
+  return addr.to_v6();
+}
+
+boost::asio::ip::address_v6
+addressV6FromString(const std::string& address)
+{
+  boost::system::error_code ec;
+  auto addr = addressV6FromString(address, ec);
+  if (ec) {
+    BOOST_THROW_EXCEPTION(boost::system::system_error(ec));
+  }
+  return addr;
+}
+
+} // namespace ip
+} // namespace ndn
diff --git a/src/net/address-converter.hpp b/src/net/address-converter.hpp
new file mode 100644
index 0000000..b58cfff
--- /dev/null
+++ b/src/net/address-converter.hpp
@@ -0,0 +1,96 @@
+/* -*- 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 NDN_NET_ADDRESS_CONVERTER_HPP
+#define NDN_NET_ADDRESS_CONVERTER_HPP
+
+#include "../common.hpp"
+
+#include <boost/asio/ip/address.hpp>
+#include <boost/system/error_code.hpp>
+
+namespace ndn {
+namespace ip {
+
+/**
+ * \brief Convert scope ID of IPv6 address into interface name
+ *
+ * \return interface name, or ndn::nullopt if \p scopeId cannot be converted
+ */
+optional<std::string>
+scopeNameFromId(unsigned int scopeId);
+
+/**
+ * \brief parse and convert the input string into an IP address
+ *
+ * \param str the string to parse
+ *
+ * \return the converted IP address
+ * \throw boost::system::system_error in case of failure
+ */
+boost::asio::ip::address
+addressFromString(const std::string& str);
+
+/**
+ * \brief parse and convert the input string into an IP address
+ *
+ * \param str the string to parse
+ * \param ec the error code of failure in conversion
+ *
+ * \return the converted IP address, or a default-constructed
+ *         `boost::asio::ip::address` in case of failure
+ */
+boost::asio::ip::address
+addressFromString(const std::string& str, boost::system::error_code& ec);
+
+/**
+ * \brief parse and convert the input string into an IPv6 address
+ *
+ * \param str the string to parse
+ *
+ * \return the converted IPv6 address
+ * \throw boost::system::system_error in case of failure
+ */
+boost::asio::ip::address_v6
+addressV6FromString(const std::string& str);
+
+/**
+ * \brief parse and convert the input string into an IPv6 address
+ *
+ * \param str the string to parse
+ * \param ec the error code of failure in conversion
+ *
+ * \return the converted IPv6 address, or a default-constructed
+ *         `boost::asio::ip::address_v6` in case of failure
+ */
+boost::asio::ip::address_v6
+addressV6FromString(const std::string& str, boost::system::error_code& ec);
+
+} // namespace ip
+} // namespace ndn
+
+#endif // NDN_NET_ADDRESS_CONVERTER_HPP
diff --git a/src/net/face-uri.cpp b/src/net/face-uri.cpp
index 11e4fe5..3e9f358 100644
--- a/src/net/face-uri.cpp
+++ b/src/net/face-uri.cpp
@@ -26,8 +26,12 @@
  */
 
 #include "face-uri.hpp"
-#include "dns.hpp"
 
+#include "address-converter.hpp"
+#include "dns.hpp"
+#include "util/string-helper.hpp"
+
+#include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/mpl/vector.hpp>
 #include <boost/mpl/for_each.hpp>
@@ -74,6 +78,9 @@
   const std::string& authority = protocolMatch[3];
   m_path = protocolMatch[4];
 
+  // pattern for IPv6 link local address enclosed in [ ], with optional port number
+  static const boost::regex v6LinkLocalExp("^\\[([a-fA-F0-9:]+)%([a-zA-Z0-9]+)\\]"
+                                           "(?:\\:(\\d+))?$");
   // pattern for IPv6 address enclosed in [ ], with optional port number
   static const boost::regex v6Exp("^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
   // pattern for Ethernet address in standard hex-digits-and-colons notation
@@ -88,6 +95,13 @@
   }
   else {
     boost::smatch match;
+    if (boost::regex_match(authority, match, v6LinkLocalExp)) {
+      m_isV6 = true;
+      m_host = match[1] + "%" + match[2];
+      m_port = match[3];
+      return true;
+    }
+
     m_isV6 = boost::regex_match(authority, match, v6Exp);
     if (m_isV6 ||
         boost::regex_match(authority, match, etherExp) ||
@@ -256,18 +270,43 @@
     }
 
     boost::system::error_code ec;
-    boost::asio::ip::address addr;
-    if (faceUri.getScheme() == m_v4Scheme) {
-      addr = boost::asio::ip::address_v4::from_string(faceUri.getHost(), ec);
-    }
-    else if (faceUri.getScheme() == m_v6Scheme) {
-      addr = boost::asio::ip::address_v6::from_string(faceUri.getHost(), ec);
-    }
-    else {
+    auto addr = ip::addressFromString(unescapeHost(faceUri.getHost()), ec);
+    if (ec) {
       return false;
     }
 
-    return !ec && addr.to_string() == faceUri.getHost() && checkAddress(addr).first;
+    bool hasCorrectScheme = (faceUri.getScheme() == m_v4Scheme && addr.is_v4()) ||
+                            (faceUri.getScheme() == m_v6Scheme && addr.is_v6());
+    if (!hasCorrectScheme) {
+      return false;
+    }
+
+    auto checkAddressWithUri = [] (const boost::asio::ip::address& addr,
+                                   const FaceUri& faceUri) -> bool {
+      if (addr.is_v4() || !addr.to_v6().is_link_local()) {
+        return addr.to_string() == faceUri.getHost();
+      }
+
+      std::vector<std::string> addrFields, faceUriFields;
+      std::string addrString = addr.to_string();
+      std::string faceUriString = faceUri.getHost();
+
+      boost::algorithm::split(addrFields, addrString, boost::is_any_of("%"));
+      boost::algorithm::split(faceUriFields, faceUriString, boost::is_any_of("%"));
+      if (addrFields.size() != 2 || faceUriFields.size() != 2) {
+        return false;
+      }
+
+      if (faceUriFields[1].size() > 2 && faceUriFields[1].compare(0, 2, "25") == 0) {
+        // %25... is accepted, but not a canonical form
+        return false;
+      }
+
+      return addrFields[0] == faceUriFields[0] &&
+             addrFields[1] == faceUriFields[1];
+    };
+
+    return checkAddressWithUri(addr, faceUri) && checkAddress(addr).first;
   }
 
   void
@@ -284,7 +323,7 @@
     // make a copy because caller may modify faceUri
     auto uri = make_shared<FaceUri>(faceUri);
     boost::system::error_code ec;
-    auto ipAddress = boost::asio::ip::address::from_string(faceUri.getHost(), ec);
+    auto ipAddress = ip::addressFromString(unescapeHost(faceUri.getHost()), ec);
     if (!ec) {
       // No need to resolve IP address if host is already an IP
       if ((faceUri.getScheme() == m_v4Scheme && !ipAddress.is_v4()) ||
@@ -307,7 +346,7 @@
         addressSelector = dns::AnyAddress();
       }
 
-      dns::asyncResolve(faceUri.getHost(),
+      dns::asyncResolve(unescapeHost(faceUri.getHost()),
         bind(&IpHostCanonizeProvider<Protocol>::onDnsSuccess, this, uri, onSuccess, onFailure, _1),
         bind(&IpHostCanonizeProvider<Protocol>::onDnsFailure, this, uri, onFailure, _1),
         io, addressSelector, timeout);
@@ -377,6 +416,16 @@
     return {true, ""};
   }
 
+  static std::string
+  unescapeHost(std::string host)
+  {
+    auto escapePos = host.find("%25");
+    if (escapePos != std::string::npos && escapePos < host.size() - 3) {
+      host = unescape(host);
+    }
+    return host;
+  }
+
 private:
   std::string m_baseScheme;
   std::string m_v4Scheme;
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();
 }