mgmt refactoring: Refactor InternalFace

InternalFace is now exclusively for internal use by NFD's forwarding
pipelines.  A separate InternalClientFace has been introduced, intended
to be used by the NFD internal applications, including FIB, Face, RIB
manager, and others.

Change-Id: I4a06b9d05b1613a456c6267582091924557d73be
Refs: #2107
diff --git a/tests/daemon/face/internal-client-face.t.cpp b/tests/daemon/face/internal-client-face.t.cpp
new file mode 100644
index 0000000..0b2295c
--- /dev/null
+++ b/tests/daemon/face/internal-client-face.t.cpp
@@ -0,0 +1,134 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  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 NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "face/internal-client-face.hpp"
+#include "face/internal-face.hpp"
+#include "tests/test-common.hpp"
+#include "tests/identity-management-fixture.hpp"
+
+namespace nfd {
+namespace tests {
+
+class InternalClientFaceFixture : public UnitTestTimeFixture
+                                , public IdentityManagementFixture
+{
+public:
+  InternalClientFaceFixture()
+    : m_internalFace(make_shared<InternalFace>())
+    , m_face(makeInternalClientFace(m_internalFace, m_keyChain))
+  {
+  }
+
+protected:
+  shared_ptr<InternalFace> m_internalFace;
+  shared_ptr<InternalClientFace> m_face;
+};
+
+BOOST_FIXTURE_TEST_SUITE(FaceInternalClientFace, InternalClientFaceFixture)
+
+BOOST_AUTO_TEST_CASE(ExpressInterest)
+{
+  bool didReceiveDataBack = false;
+  bool didTimeoutCallbackFire = false;
+  Data receivedData;
+
+  auto expressInterest = [&] (shared_ptr<Interest> interest) {
+    didReceiveDataBack = false;
+    didTimeoutCallbackFire = false;
+    m_face->expressInterest(interest->setInterestLifetime(time::milliseconds(100)),
+                            [&] (const Interest& interest, Data& data) {
+                              didReceiveDataBack = true;
+                              receivedData = data;
+                            },
+                            bind([&] { didTimeoutCallbackFire = true; }));
+    advanceClocks(time::milliseconds(1));
+  };
+
+  expressInterest(makeInterest("/test/timeout"));
+  advanceClocks(time::milliseconds(1), 200); // wait for time out
+  BOOST_CHECK(didTimeoutCallbackFire);
+
+  auto dataToSend = makeData("/test/data")->setContent(Block("\x81\x01\0x01", 3));
+  expressInterest(makeInterest("/test/data"));
+  m_internalFace->sendData(dataToSend); // send data to satisfy the expressed interest
+  advanceClocks(time::milliseconds(1));
+  BOOST_CHECK(didReceiveDataBack);
+  BOOST_CHECK(receivedData.wireEncode() == dataToSend.wireEncode());
+}
+
+BOOST_AUTO_TEST_CASE(InterestFilter)
+{
+  bool didOnInterestCallbackFire = false;
+  Block receivedBlock, expectedBlock;
+
+  auto testSendInterest = [&] (shared_ptr<Interest> interest) {
+    didOnInterestCallbackFire = false;
+    expectedBlock = interest->wireEncode();
+    m_internalFace->sendInterest(*interest);
+  };
+
+  testSendInterest(makeInterest("/test/filter")); // no filter is set now
+  BOOST_CHECK(!didOnInterestCallbackFire);
+
+  // set filter
+  auto filter = m_face->setInterestFilter("/test/filter",
+                                          bind([&](const Interest& interset) {
+                                              didOnInterestCallbackFire = true;
+                                              receivedBlock = interset.wireEncode();
+                                            }, _2));
+  advanceClocks(time::milliseconds(1));
+
+  testSendInterest(makeInterest("/test/filter"));
+  BOOST_CHECK(didOnInterestCallbackFire);
+  BOOST_CHECK(receivedBlock == expectedBlock);
+
+  // unset filter
+  didOnInterestCallbackFire = false;
+  m_face->unsetInterestFilter(filter);
+  advanceClocks(time::milliseconds(1));
+  testSendInterest(makeInterest("/test/filter"));
+  BOOST_CHECK(!didOnInterestCallbackFire);
+}
+
+BOOST_AUTO_TEST_CASE(PutData)
+{
+  bool didInternalFaceReceiveData = false;
+  Data receivedData;
+  m_internalFace->onReceiveData.connect([&] (const Data& data) {
+      didInternalFaceReceiveData = true;
+      receivedData = data;
+    });
+
+  auto dataToPut = makeData("/test/put/data");
+  m_face->put(*dataToPut);
+  advanceClocks(time::milliseconds(1));
+  BOOST_CHECK(didInternalFaceReceiveData);
+  BOOST_CHECK(receivedData.wireEncode() == dataToPut->wireEncode());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/daemon/face/internal-face.t.cpp b/tests/daemon/face/internal-face.t.cpp
new file mode 100644
index 0000000..44f5e7b
--- /dev/null
+++ b/tests/daemon/face/internal-face.t.cpp
@@ -0,0 +1,143 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  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 NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "face/internal-face.hpp"
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace tests {
+
+class InternalFaceFixture : protected BaseFixture
+{
+public:
+  InternalFaceFixture()
+  {
+    m_face.onReceiveInterest.connect([this] (const Interest& interest) {
+        inInterests.push_back(interest);
+      });
+    m_face.onSendInterest.connect([this] (const Interest& interest) {
+        outInterests.push_back(interest);
+      });
+    m_face.onReceiveData.connect([this] (const Data& data) {
+        inData.push_back(data);
+      });
+    m_face.onSendData.connect([this] (const Data& data) {
+        outData.push_back(data);
+      });
+  }
+
+protected:
+  InternalFace m_face;
+  std::vector<Interest> inInterests;
+  std::vector<Interest> outInterests;
+  std::vector<Data> inData;
+  std::vector<Data> outData;
+};
+
+BOOST_FIXTURE_TEST_SUITE(FaceInternalFace, InternalFaceFixture)
+
+BOOST_AUTO_TEST_CASE(SendInterest)
+{
+  BOOST_CHECK(outInterests.empty());
+
+  auto interest = makeInterest("/test/send/interest");
+  Block expectedBlock = interest->wireEncode(); // assign a value to nonce
+  m_face.sendInterest(*interest);
+
+  BOOST_CHECK_EQUAL(outInterests.size(), 1);
+  BOOST_CHECK(outInterests[0].wireEncode() == expectedBlock);
+}
+
+BOOST_AUTO_TEST_CASE(SendData)
+{
+  BOOST_CHECK(outData.empty());
+
+  auto data = makeData("/test/send/data");
+  m_face.sendData(*data);
+
+  BOOST_CHECK_EQUAL(outData.size(), 1);
+  BOOST_CHECK(outData[0].wireEncode() == data->wireEncode());
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveInterest)
+{
+  BOOST_CHECK(inInterests.empty());
+
+  auto interest = makeInterest("/test/receive/interest");
+  Block expectedBlock = interest->wireEncode(); // assign a value to nonce
+  m_face.receiveInterest(*interest);
+
+  BOOST_CHECK_EQUAL(inInterests.size(), 1);
+  BOOST_CHECK(inInterests[0].wireEncode() == expectedBlock);
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveData)
+{
+  BOOST_CHECK(inData.empty());
+
+  auto data = makeData("/test/send/data");
+  m_face.receiveData(*data);
+
+  BOOST_CHECK_EQUAL(inData.size(), 1);
+  BOOST_CHECK(inData[0].wireEncode() == data->wireEncode());
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveBlock)
+{
+  BOOST_CHECK(inInterests.empty());
+  BOOST_CHECK(inData.empty());
+
+  Block interestBlock = makeInterest("test/receive/interest")->wireEncode();
+  m_face.receive(interestBlock);
+  BOOST_CHECK_EQUAL(inInterests.size(), 1);
+  BOOST_CHECK(inInterests[0].wireEncode() == interestBlock);
+
+  Block dataBlock = makeData("test/receive/data")->wireEncode();
+  m_face.receive(dataBlock);
+  BOOST_CHECK_EQUAL(inData.size(), 1);
+  BOOST_CHECK(inData[0].wireEncode() == dataBlock);
+}
+
+BOOST_AUTO_TEST_CASE(ExtractPacketFromBlock)
+{
+  {
+    Block block = makeInterest("/test/interest")->wireEncode();
+    const Block& payload = ndn::nfd::LocalControlHeader::getPayload(block);
+    auto interest = m_face.extractPacketFromBlock<Interest>(block, payload);
+    BOOST_CHECK(interest->wireEncode() == block);
+  }
+
+  {
+    Block block = makeData("/test/data")->wireEncode();
+    const Block& payload = ndn::nfd::LocalControlHeader::getPayload(block);
+    auto data = m_face.extractPacketFromBlock<Data>(block, payload);
+    BOOST_CHECK(data->wireEncode() == block);
+  }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/daemon/mgmt/internal-face.t.cpp b/tests/daemon/mgmt/internal-face.t.cpp
deleted file mode 100644
index 29b602c..0000000
--- a/tests/daemon/mgmt/internal-face.t.cpp
+++ /dev/null
@@ -1,238 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2015,  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 NFD (Named Data Networking Forwarding Daemon).
- * See AUTHORS.md for complete list of NFD authors and contributors.
- *
- * NFD is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NFD 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "mgmt/internal-face.hpp"
-#include "tests/daemon/face/dummy-face.hpp"
-
-#include "tests/test-common.hpp"
-
-namespace nfd {
-namespace tests {
-
-class InternalFaceFixture : protected BaseFixture
-{
-public:
-
-  InternalFaceFixture()
-    : m_onInterestFired(false),
-      m_noOnInterestFired(false)
-  {
-
-  }
-
-  void
-  validateOnInterestCallback(const Name& name, const Interest& interest)
-  {
-    m_onInterestFired = true;
-  }
-
-  void
-  validateNoOnInterestCallback(const Name& name, const Interest& interest)
-  {
-    m_noOnInterestFired = true;
-  }
-
-  void
-  addFace(shared_ptr<Face> face)
-  {
-    m_faces.push_back(face);
-  }
-
-  bool
-  didOnInterestFire()
-  {
-    return m_onInterestFired;
-  }
-
-  bool
-  didNoOnInterestFire()
-  {
-    return m_noOnInterestFired;
-  }
-
-  void
-  resetOnInterestFired()
-  {
-    m_onInterestFired = false;
-  }
-
-  void
-  resetNoOnInterestFired()
-  {
-    m_noOnInterestFired = false;
-  }
-
-protected:
-  ndn::KeyChain m_keyChain;
-
-private:
-  std::vector<shared_ptr<Face> > m_faces;
-  bool m_onInterestFired;
-  bool m_noOnInterestFired;
-};
-
-BOOST_FIXTURE_TEST_SUITE(MgmtInternalFace, InternalFaceFixture)
-
-void
-validatePutData(bool& called, const Name& expectedName, const Data& data)
-{
-  called = true;
-  BOOST_CHECK_EQUAL(expectedName, data.getName());
-}
-
-BOOST_AUTO_TEST_CASE(PutData)
-{
-  addFace(make_shared<DummyFace>());
-
-  shared_ptr<InternalFace> face(new InternalFace);
-
-  bool didPutData = false;
-  Name dataName("/hello");
-  face->onReceiveData.connect(bind(&validatePutData, ref(didPutData), dataName, _1));
-
-  Data testData(dataName);
-  m_keyChain.sign(testData);
-  face->put(testData);
-
-  BOOST_REQUIRE(didPutData);
-
-  BOOST_CHECK_THROW(face->close(), InternalFace::Error);
-}
-
-BOOST_AUTO_TEST_CASE(SendInterestHitEnd)
-{
-  addFace(make_shared<DummyFace>());
-
-  shared_ptr<InternalFace> face(new InternalFace);
-
-  face->setInterestFilter("/localhost/nfd/fib",
-                          bind(&InternalFaceFixture::validateOnInterestCallback,
-                               this, _1, _2));
-
-  // generate command whose name is canonically
-  // ordered after /localhost/nfd/fib so that
-  // we hit the end of the std::map
-
-  Name commandName("/localhost/nfd/fib/end");
-  shared_ptr<Interest> command = makeInterest(commandName);
-  face->sendInterest(*command);
-  g_io.run_one();
-
-  BOOST_REQUIRE(didOnInterestFire());
-  BOOST_REQUIRE(didNoOnInterestFire() == false);
-}
-
-BOOST_AUTO_TEST_CASE(SendInterestHitBegin)
-{
-  addFace(make_shared<DummyFace>());
-
-  shared_ptr<InternalFace> face(new InternalFace);
-
-  face->setInterestFilter("/localhost/nfd/fib",
-                          bind(&InternalFaceFixture::validateNoOnInterestCallback,
-                               this, _1, _2));
-
-  // generate command whose name is canonically
-  // ordered before /localhost/nfd/fib so that
-  // we hit the beginning of the std::map
-
-  Name commandName("/localhost/nfd");
-  shared_ptr<Interest> command = makeInterest(commandName);
-  face->sendInterest(*command);
-  g_io.run_one();
-
-  BOOST_REQUIRE(didNoOnInterestFire() == false);
-}
-
-BOOST_AUTO_TEST_CASE(SendInterestHitExact)
-{
-  addFace(make_shared<DummyFace>());
-
-  shared_ptr<InternalFace> face(new InternalFace);
-
-  face->setInterestFilter("/localhost/nfd/eib",
-                          bind(&InternalFaceFixture::validateNoOnInterestCallback,
-                               this, _1, _2));
-
-  face->setInterestFilter("/localhost/nfd/fib",
-                          bind(&InternalFaceFixture::validateOnInterestCallback,
-                           this, _1, _2));
-
-  face->setInterestFilter("/localhost/nfd/gib",
-                          bind(&InternalFaceFixture::validateNoOnInterestCallback,
-                               this, _1, _2));
-
-  // generate command whose name exactly matches
-  // /localhost/nfd/fib
-
-  Name commandName("/localhost/nfd/fib");
-  shared_ptr<Interest> command = makeInterest(commandName);
-  face->sendInterest(*command);
-  g_io.run_one();
-
-  BOOST_REQUIRE(didOnInterestFire());
-  BOOST_REQUIRE(didNoOnInterestFire() == false);
-}
-
-BOOST_AUTO_TEST_CASE(SendInterestHitPrevious)
-{
-  addFace(make_shared<DummyFace>());
-
-  shared_ptr<InternalFace> face(new InternalFace);
-
-  face->setInterestFilter("/localhost/nfd/fib",
-                          bind(&InternalFaceFixture::validateOnInterestCallback,
-                               this, _1, _2));
-
-  face->setInterestFilter("/localhost/nfd/fib/zzzzzzzzzzzzz/",
-                          bind(&InternalFaceFixture::validateNoOnInterestCallback,
-                               this, _1, _2));
-
-  // generate command whose name exactly matches
-  // an Interest filter
-
-  Name commandName("/localhost/nfd/fib/previous");
-  shared_ptr<Interest> command = makeInterest(commandName);
-  face->sendInterest(*command);
-  g_io.run_one();
-
-  BOOST_REQUIRE(didOnInterestFire());
-  BOOST_REQUIRE(didNoOnInterestFire() == false);
-}
-
-BOOST_AUTO_TEST_CASE(InterestGone)
-{
-  shared_ptr<InternalFace> face = make_shared<InternalFace>();
-  shared_ptr<Interest> interest = makeInterest("ndn:/localhost/nfd/gone");
-  face->sendInterest(*interest);
-
-  interest.reset();
-  BOOST_CHECK_NO_THROW(g_io.poll());
-}
-
-BOOST_AUTO_TEST_SUITE_END()
-
-} // namespace tests
-} // namespace nfd