fw: /localhost scope control for incoming Interest

refs #1230

Change-Id: I86fd071ef144caa506b26a723f14232e2af5e2de
diff --git a/tests/fw/forwarder.cpp b/tests/fw/forwarder.cpp
index 27653bc..433c36b 100644
--- a/tests/fw/forwarder.cpp
+++ b/tests/fw/forwarder.cpp
@@ -13,6 +13,7 @@
 
 class ForwarderTestFace : public Face {
 public:
+  explicit
   ForwarderTestFace(boost::asio::io_service& ioService)
     : m_ioService(ioService)
   {
@@ -122,6 +123,105 @@
   BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId());
 }
 
+
+class ForwarderTestLocalFace : public DummyFace {
+public:
+  explicit
+  ForwarderTestLocalFace(bool isLocal)
+    : m_isLocal(isLocal)
+  {
+  }
+
+  virtual bool
+  isLocal() const
+  {
+    return m_isLocal;
+  }
+
+private:
+  bool m_isLocal;
+};
+
+class ScopeLocalhostTestForwarder : public Forwarder
+{
+public:
+  explicit
+  ScopeLocalhostTestForwarder(boost::asio::io_service& ioService)
+    : Forwarder(ioService)
+  {
+  }
+
+  virtual void
+  onDataUnsolicited(Face& inFace, const Data& data)
+  {
+    ++m_onDataUnsolicited_count;
+  }
+
+protected:
+  virtual void
+  dispatchToStrategy(const Face& inFace,
+                     const Interest& interest,
+                     shared_ptr<fib::Entry> fibEntry,
+                     shared_ptr<pit::Entry> pitEntry)
+  {
+    ++m_dispatchToStrategy_count;
+  }
+
+public:
+  int m_dispatchToStrategy_count;
+  int m_onDataUnsolicited_count;
+};
+
+BOOST_AUTO_TEST_CASE(ScopeLocalhost)
+{
+  boost::asio::io_service io;
+  ScopeLocalhostTestForwarder forwarder(io);
+  shared_ptr<ForwarderTestLocalFace> face1 = make_shared<ForwarderTestLocalFace>(true);
+  shared_ptr<ForwarderTestLocalFace> face2 = make_shared<ForwarderTestLocalFace>(false);
+  forwarder.addFace(face1);
+  forwarder.addFace(face2);
+  
+  // local face, /localhost: OK
+  forwarder.m_dispatchToStrategy_count = 0;
+  forwarder.onIncomingInterest(*face1, Interest(Name("/localhost/A1")));
+  BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
+  
+  // non-local face, /localhost: violate
+  forwarder.m_dispatchToStrategy_count = 0;
+  forwarder.onIncomingInterest(*face2, Interest(Name("/localhost/A2")));
+  BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
+  
+  // local face, non-/localhost: OK
+  forwarder.m_dispatchToStrategy_count = 0;
+  forwarder.onIncomingInterest(*face1, Interest(Name("/A3")));
+  BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
+  
+  // non-local face, non-/localhost: OK
+  forwarder.m_dispatchToStrategy_count = 0;
+  forwarder.onIncomingInterest(*face2, Interest(Name("/A4")));
+  BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
+  
+  // local face, /localhost: OK
+  forwarder.m_onDataUnsolicited_count = 0;
+  forwarder.onIncomingData(*face1, Data(Name("/localhost/B1")));
+  BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
+
+  // non-local face, /localhost: OK
+  forwarder.m_onDataUnsolicited_count = 0;
+  forwarder.onIncomingData(*face2, Data(Name("/localhost/B2")));
+  BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
+  
+  // local face, non-/localhost: OK
+  forwarder.m_onDataUnsolicited_count = 0;
+  forwarder.onIncomingData(*face1, Data(Name("/B3")));
+  BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
+
+  // non-local face, non-/localhost: OK
+  forwarder.m_onDataUnsolicited_count = 0;
+  forwarder.onIncomingData(*face2, Data(Name("/B4")));
+  BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace nfd