face: Configurable IP subnets for "local" TCP faces

Change-Id: Idaddfe4b2c304b552d2e132235f4c3d3e6c2ebcb
Refs: #4546
diff --git a/tests/daemon/face/factory-test-common.hpp b/tests/daemon/face/factory-test-common.hpp
index 795bdf3..15b3621 100644
--- a/tests/daemon/face/factory-test-common.hpp
+++ b/tests/daemon/face/factory-test-common.hpp
@@ -64,11 +64,15 @@
            const FaceUri& remoteUri,
            const ndn::optional<FaceUri>& localUri,
            const TestFaceParams& params,
-           const CreateFaceExpectedResult& expected)
+           const CreateFaceExpectedResult& expected,
+           const std::function<void(const Face&)>& extraChecks = nullptr)
 {
   factory.createFace({remoteUri, localUri, params},
-                     [expected] (const shared_ptr<Face>&) {
+                     [expected, extraChecks] (const shared_ptr<Face>& face) {
                        BOOST_CHECK_EQUAL(CreateFaceExpectedResult::SUCCESS, expected.result);
+                       if (extraChecks) {
+                         extraChecks(*face);
+                       }
                      },
                      [expected] (uint32_t actualStatus, const std::string& actualReason) {
                        BOOST_CHECK_EQUAL(CreateFaceExpectedResult::FAILURE, expected.result);
diff --git a/tests/daemon/face/tcp-channel-fixture.hpp b/tests/daemon/face/tcp-channel-fixture.hpp
index 732ad22..fa90735 100644
--- a/tests/daemon/face/tcp-channel-fixture.hpp
+++ b/tests/daemon/face/tcp-channel-fixture.hpp
@@ -27,6 +27,7 @@
 #define NFD_TESTS_DAEMON_FACE_TCP_CHANNEL_FIXTURE_HPP
 
 #include "face/tcp-channel.hpp"
+#include "core/network-predicate.hpp"
 
 #include "channel-fixture.hpp"
 
@@ -37,13 +38,19 @@
 class TcpChannelFixture : public ChannelFixture<TcpChannel, tcp::Endpoint>
 {
 protected:
+  TcpChannelFixture()
+  {
+    local.assign({{"subnet", "127.0.0.0/8"}, {"subnet", "::1/128"}}, {});
+  }
+
   unique_ptr<TcpChannel>
   makeChannel(const boost::asio::ip::address& addr, uint16_t port = 0) final
   {
     if (port == 0)
       port = getNextPort();
 
-    return make_unique<TcpChannel>(tcp::Endpoint(addr, port), false);
+    return make_unique<TcpChannel>(tcp::Endpoint(addr, port), false,
+                                   std::bind(&TcpChannelFixture::determineFaceScope, this, _1, _2));
   }
 
   void
@@ -61,8 +68,21 @@
     });
   }
 
+  ndn::nfd::FaceScope
+  determineFaceScope(const boost::asio::ip::address& localAddress,
+                     const boost::asio::ip::address& remoteAddress)
+  {
+    if (local(localAddress) && local(remoteAddress)) {
+      return ndn::nfd::FACE_SCOPE_LOCAL;
+    }
+    else {
+      return ndn::nfd::FACE_SCOPE_NON_LOCAL;
+    }
+  }
+
 protected:
   std::vector<shared_ptr<Face>> clientFaces;
+  IpAddressPredicate local;
 };
 
 } // namespace tests
diff --git a/tests/daemon/face/tcp-factory.t.cpp b/tests/daemon/face/tcp-factory.t.cpp
index 77b8066..08e4498 100644
--- a/tests/daemon/face/tcp-factory.t.cpp
+++ b/tests/daemon/face/tcp-factory.t.cpp
@@ -45,6 +45,9 @@
                            boost::lexical_cast<uint16_t>(localPort));
     return factory.createChannel(endpoint);
   }
+
+protected:
+  LimitedIo limitedIo;
 };
 
 BOOST_AUTO_TEST_SUITE(Face)
@@ -68,6 +71,11 @@
   auto channels = factory.getChannels();
   BOOST_CHECK(std::all_of(channels.begin(), channels.end(),
                           [] (const shared_ptr<const Channel>& ch) { return ch->isListening(); }));
+
+  BOOST_CHECK_EQUAL(factory.m_local.m_whitelist.size(), 2);
+  BOOST_CHECK_EQUAL(factory.m_local.m_whitelist.count("127.0.0.0/8"), 1);
+  BOOST_CHECK_EQUAL(factory.m_local.m_whitelist.count("::1/128"), 1);
+  BOOST_CHECK_EQUAL(factory.m_local.m_blacklist.size(), 0);
 }
 
 BOOST_AUTO_TEST_CASE(DisableListen)
@@ -132,6 +140,88 @@
   checkChannelListEqual(factory, {"tcp4://0.0.0.0:7001"});
 }
 
+BOOST_AUTO_TEST_CASE(ConfigureLocal)
+{
+  const std::string CONFIG = R"CONFIG(
+    face_system
+    {
+      tcp
+      {
+        local {
+          whitelist {
+            subnet 127.0.0.0/8
+          }
+
+          blacklist {
+            subnet ::1/128
+          }
+        }
+      }
+    }
+  )CONFIG";
+
+  parseConfig(CONFIG, true);
+  parseConfig(CONFIG, false);
+
+  BOOST_CHECK_EQUAL(factory.m_local.m_whitelist.size(), 1);
+  BOOST_CHECK_EQUAL(factory.m_local.m_whitelist.count("127.0.0.0/8"), 1);
+  BOOST_CHECK_EQUAL(factory.m_local.m_blacklist.size(), 1);
+  BOOST_CHECK_EQUAL(factory.m_local.m_blacklist.count("::1/128"), 1);
+
+  createFace(factory,
+             FaceUri("tcp4://127.0.0.1:6363"),
+             {},
+             {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
+             {CreateFaceExpectedResult::SUCCESS, 0, ""},
+             [] (const nfd::Face& face) {
+               BOOST_CHECK_EQUAL(face.getScope(), ndn::nfd::FACE_SCOPE_LOCAL);
+             });
+
+  limitedIo.run(1, 100_ms);
+}
+
+BOOST_AUTO_TEST_CASE(ConfigureNonLocal)
+{
+  const std::string CONFIG = R"CONFIG(
+    face_system
+    {
+      tcp
+      {
+        local {
+          whitelist {
+            *
+          }
+
+          blacklist {
+            subnet 127.0.0.0/8
+            subnet ::1/128
+          }
+        }
+      }
+    }
+  )CONFIG";
+
+  parseConfig(CONFIG, true);
+  parseConfig(CONFIG, false);
+
+  BOOST_CHECK_EQUAL(factory.m_local.m_whitelist.size(), 1);
+  BOOST_CHECK_EQUAL(factory.m_local.m_whitelist.count("*"), 1);
+  BOOST_CHECK_EQUAL(factory.m_local.m_blacklist.size(), 2);
+  BOOST_CHECK_EQUAL(factory.m_local.m_blacklist.count("127.0.0.0/8"), 1);
+  BOOST_CHECK_EQUAL(factory.m_local.m_blacklist.count("::1/128"), 1);
+
+  createFace(factory,
+             FaceUri("tcp4://127.0.0.1:6363"),
+             {},
+             {ndn::nfd::FACE_PERSISTENCY_PERSISTENT, {}, {}, false, false, false},
+             {CreateFaceExpectedResult::SUCCESS, 0, ""},
+             [] (const nfd::Face& face) {
+               BOOST_CHECK_EQUAL(face.getScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL);
+             });
+
+  limitedIo.run(1, 100_ms);
+}
+
 BOOST_AUTO_TEST_CASE(Omitted)
 {
   const std::string CONFIG = R"CONFIG(
@@ -357,7 +447,6 @@
   }
 
 public:
-  LimitedIo limitedIo;
   shared_ptr<nfd::Face> face;
 };
 
diff --git a/tests/daemon/face/tcp-transport-fixture.hpp b/tests/daemon/face/tcp-transport-fixture.hpp
index face839..0cfae44 100644
--- a/tests/daemon/face/tcp-transport-fixture.hpp
+++ b/tests/daemon/face/tcp-transport-fixture.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2017,  Regents of the University of California,
+ * Copyright (c) 2014-2018,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -88,8 +88,17 @@
     BOOST_REQUIRE_EQUAL(limitedIo.run(2, time::seconds(1)), LimitedIo::EXCEED_OPS);
 
     localEp = sock.local_endpoint();
+
+    ndn::nfd::FaceScope scope;
+    if (sock.local_endpoint().address().is_loopback() && sock.remote_endpoint().address().is_loopback()) {
+      scope = ndn::nfd::FACE_SCOPE_LOCAL;
+    }
+    else {
+      scope = ndn::nfd::FACE_SCOPE_NON_LOCAL;
+    }
+
     face = make_unique<Face>(make_unique<DummyReceiveLinkService>(),
-                             make_unique<TcpTransport>(std::move(sock), persistency));
+                             make_unique<TcpTransport>(std::move(sock), persistency, scope));
     transport = static_cast<TcpTransport*>(face->getTransport());
     receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
 
diff --git a/tests/daemon/face/tcp-transport.t.cpp b/tests/daemon/face/tcp-transport.t.cpp
index 96cba88..0b573f3 100644
--- a/tests/daemon/face/tcp-transport.t.cpp
+++ b/tests/daemon/face/tcp-transport.t.cpp
@@ -119,7 +119,7 @@
 {
 public:
   PermanentTcpTransportReconnectObserver(protocol::socket&& socket, LimitedIo& io)
-    : TcpTransport(std::move(socket), ndn::nfd::FACE_PERSISTENCY_PERMANENT)
+    : TcpTransport(std::move(socket), ndn::nfd::FACE_PERSISTENCY_PERMANENT, ndn::nfd::FACE_SCOPE_LOCAL)
     , m_io(io)
   {
   }