tests: Add partial unit test coverage for Face class

Change-Id: Ica156cae150b8fbb7cb5161765960f248e3eef70
Refs: #1947
diff --git a/src/transport/transport.hpp b/src/transport/transport.hpp
index 2fac216..0073eeb 100644
--- a/src/transport/transport.hpp
+++ b/src/transport/transport.hpp
@@ -29,7 +29,7 @@
 
 namespace ndn {
 
-class Transport
+class Transport : noncopyable
 {
 public:
   class Error : public std::runtime_error
diff --git a/tests/integrated/test-faces.cpp b/tests/integrated/test-faces.cpp
index e7ce988..9550286 100644
--- a/tests/integrated/test-faces.cpp
+++ b/tests/integrated/test-faces.cpp
@@ -26,7 +26,7 @@
 #include "boost-test.hpp"
 
 namespace ndn {
-
+namespace tests {
 
 class FacesFixture
 {
@@ -491,6 +491,8 @@
   BOOST_CHECK_EQUAL(nInInterests, 2);
   BOOST_CHECK_EQUAL(nTimeouts, 4);
 }
+
 BOOST_AUTO_TEST_SUITE_END()
 
+} // tests
 } // namespace ndn
diff --git a/tests/unit-tests/dummy-client-face.hpp b/tests/unit-tests/dummy-client-face.hpp
index b744882..82d9c7c 100644
--- a/tests/unit-tests/dummy-client-face.hpp
+++ b/tests/unit-tests/dummy-client-face.hpp
@@ -24,6 +24,9 @@
 
 #include "face.hpp"
 #include "transport/transport.hpp"
+#include "management/nfd-controller.hpp"
+#include "management/nfd-control-response.hpp"
+#include "util/event-emitter.hpp"
 
 namespace ndn {
 namespace tests {
@@ -57,10 +60,12 @@
   send(const Block& wire)
   {
     if (wire.type() == tlv::Interest) {
-      m_sentInterests->push_back(Interest(wire));
+      shared_ptr<Interest> interest = make_shared<Interest>(wire);
+      (*m_onInterest)(*interest, this);
     }
     else if (wire.type() == tlv::Data) {
-      m_sentDatas->push_back(Data(wire));
+      shared_ptr<Data> data = make_shared<Data>(wire);
+      (*m_onData)(*data, this);
     }
   }
 
@@ -70,12 +75,45 @@
     this->send(payload);
   }
 
-public:
-  std::vector<Interest>* m_sentInterests;
-  std::vector<Data>*     m_sentDatas;
+  boost::asio::io_service&
+  getIoService()
+  {
+    return *m_ioService;
+  }
+
+private:
+  friend class DummyClientFace;
+  util::EventEmitter<Interest, DummyClientTransport*>* m_onInterest;
+  util::EventEmitter<Data, DummyClientTransport*>* m_onData;
 };
 
 
+/** \brief Callback to connect
+ */
+inline void
+replyNfdRibCommands(const Interest& interest, DummyClientTransport* transport)
+{
+  static const Name localhostRegistration("/localhost/nfd/rib");
+  if (localhostRegistration.isPrefixOf(interest.getName())) {
+    shared_ptr<Data> okResponse = make_shared<Data>(interest.getName());
+    nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
+    params.setFaceId(1);
+    params.setOrigin(0);
+    if (interest.getName().get(3) == name::Component("register")) {
+      params.setCost(0);
+    }
+    nfd::ControlResponse resp;
+    resp.setCode(200);
+    resp.setBody(params.wireEncode());
+    okResponse->setContent(resp.wireEncode());
+    KeyChain keyChain;
+    keyChain.signWithSha256(*okResponse);
+
+    transport->getIoService().post(bind(&DummyClientTransport::receive, transport,
+                                        okResponse->wireEncode()));
+  }
+}
+
 /** \brief a client-side face for unit testing
  */
 class DummyClientFace : public ndn::Face
@@ -86,16 +124,20 @@
     : Face(transport)
     , m_transport(transport)
   {
-    m_transport->m_sentInterests = &m_sentInterests;
-    m_transport->m_sentDatas     = &m_sentDatas;
+    m_transport->m_onInterest = &onInterest;
+    m_transport->m_onData     = &onData;
+
+    enablePacketLogging();
   }
 
   DummyClientFace(shared_ptr<DummyClientTransport> transport, boost::asio::io_service& ioService)
     : Face(transport, ioService)
     , m_transport(transport)
   {
-    m_transport->m_sentInterests = &m_sentInterests;
-    m_transport->m_sentDatas     = &m_sentDatas;
+    m_transport->m_onInterest = &onInterest;
+    m_transport->m_onData     = &onData;
+
+    enablePacketLogging();
   }
 
   /** \brief cause the Face to receive a packet
@@ -107,6 +149,26 @@
     m_transport->receive(packet.wireEncode());
   }
 
+  void
+  enablePacketLogging()
+  {
+    // @todo Replace with C++11 lambdas
+
+    onInterest += bind(static_cast<void(std::vector<Interest>::*)(const Interest&)>(
+                         &std::vector<Interest>::push_back),
+                       &m_sentInterests, _1);
+
+    onData += bind(static_cast<void(std::vector<Data>::*)(const Data&)>(
+                     &std::vector<Data>::push_back),
+                   &m_sentDatas, _1);
+  }
+
+  void
+  enableRegistrationReply()
+  {
+    onInterest += &replyNfdRibCommands;
+  }
+
 public:
   /** \brief sent Interests
    *  \note After .expressInterest, .processEvents must be called before
@@ -119,6 +181,19 @@
    */
   std::vector<Data>     m_sentDatas;
 
+public:
+  /** \brief Event to be called whenever an Interest is received
+   *  \note After .expressInterest, .processEvents must be called before
+   *        the Interest would show up here.
+   */
+  util::EventEmitter<Interest, DummyClientTransport*> onInterest;
+
+  /** \brief Event to be called whenever a Data packet is received
+   *  \note After .put, .processEvents must be called before
+   *        the Interest would show up here.
+   */
+  util::EventEmitter<Data, DummyClientTransport*> onData;
+
 private:
   shared_ptr<DummyClientTransport> m_transport;
 };
@@ -135,6 +210,7 @@
   return make_shared<DummyClientFace>(make_shared<DummyClientTransport>(), ref(ioService));
 }
 
+
 } // namespace tests
 } // namespace ndn
 
diff --git a/tests/unit-tests/test-faces.cpp b/tests/unit-tests/test-faces.cpp
new file mode 100644
index 0000000..f5b45f1
--- /dev/null
+++ b/tests/unit-tests/test-faces.cpp
@@ -0,0 +1,381 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * 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 "face.hpp"
+#include "util/scheduler.hpp"
+#include "security/key-chain.hpp"
+
+#include "boost-test.hpp"
+
+#include "dummy-client-face.hpp"
+
+namespace ndn {
+namespace tests {
+
+class FacesFixture
+{
+public:
+  FacesFixture()
+    : face(makeDummyClientFace(io))
+    , nData(0)
+    , nTimeouts(0)
+    , nInInterests(0)
+    , nInInterests2(0)
+    , nRegSuccesses(0)
+    , nRegFailures(0)
+    , nUnregSuccesses(0)
+    , nUnregFailures(0)
+  {
+  }
+
+  void
+  onData()
+  {
+    ++nData;
+  }
+
+  void
+  onTimeout()
+  {
+    ++nTimeouts;
+  }
+
+  void
+  onInterest(Face& face,
+             const Name&, const Interest&)
+  {
+    ++nInInterests;
+  }
+
+  void
+  onInterest2(Face& face,
+              const Name&, const Interest&)
+  {
+    ++nInInterests2;
+  }
+
+  void
+  onInterestRegex(Face& face,
+                  const InterestFilter&, const Interest&)
+  {
+    ++nInInterests;
+  }
+
+  void
+  onInterestRegexError(Face& face,
+                       const Name&, const Interest&)
+  {
+    BOOST_FAIL("InterestFilter::Error should have been triggered");
+  }
+
+  void
+  onRegSucceeded()
+  {
+    ++nRegSuccesses;
+  }
+
+  void
+  onRegFailed()
+  {
+    ++nRegFailures;
+  }
+
+  void
+  onUnregSucceeded()
+  {
+    ++nUnregSuccesses;
+  }
+
+  void
+  onUnregFailed()
+  {
+    ++nUnregFailures;
+  }
+
+  shared_ptr<Data>
+  makeData(const Name& name)
+  {
+    shared_ptr<Data> data = make_shared<Data>("/Hello/World/!");
+    static KeyChain keyChain;
+    keyChain.signWithSha256(*data);
+    return data;
+  }
+
+  boost::asio::io_service io;
+  shared_ptr<DummyClientFace> face;
+
+  uint32_t nData;
+  uint32_t nTimeouts;
+
+  uint32_t nInInterests;
+  uint32_t nInInterests2;
+  uint32_t nRegSuccesses;
+  uint32_t nRegFailures;
+  uint32_t nUnregSuccesses;
+  uint32_t nUnregFailures;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestFaces, FacesFixture)
+
+BOOST_AUTO_TEST_CASE(ExpressInterestData)
+{
+  face->enableRegistrationReply();
+
+  face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
+                        bind(&FacesFixture::onData, this),
+                        bind(&FacesFixture::onTimeout, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(-100)));
+
+  face->receive(*makeData("/Hello/World/!"));
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(200)));
+
+  BOOST_CHECK_EQUAL(nData, 1);
+  BOOST_CHECK_EQUAL(nTimeouts, 0);
+}
+
+BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
+{
+  face->enableRegistrationReply();
+
+  face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
+                        bind(&FacesFixture::onData, this),
+                        bind(&FacesFixture::onTimeout, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(200)));
+
+  BOOST_CHECK_EQUAL(nData, 0);
+  BOOST_CHECK_EQUAL(nTimeouts, 1);
+}
+
+BOOST_AUTO_TEST_CASE(SetFilter)
+{
+  face->enableRegistrationReply();
+
+  face->setInterestFilter("/Hello/World",
+                          bind(&FacesFixture::onInterest, this, ref(*face), _1, _2),
+                          RegisterPrefixSuccessCallback(),
+                          bind(&FacesFixture::onRegFailed, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(-100)));
+
+  face->receive(Interest("/Hello/World/!"));
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(-100)));
+
+  BOOST_CHECK_EQUAL(nRegFailures, 0);
+  BOOST_CHECK_EQUAL(nInInterests, 1);
+}
+
+BOOST_AUTO_TEST_CASE(SetFilterFail)
+{
+  // don't enable registration reply
+
+  face->setInterestFilter("/Hello/World",
+                          bind(&FacesFixture::onInterest, this, ref(*face), _1, _2),
+                          RegisterPrefixSuccessCallback(),
+                          bind(&FacesFixture::onRegFailed, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(11000)));
+
+  BOOST_CHECK_EQUAL(nRegFailures, 1);
+}
+
+BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
+{
+  face->enableRegistrationReply();
+
+  const RegisteredPrefixId* regPrefixId =
+    face->setInterestFilter(InterestFilter("/Hello/World"),
+                            bind(&FacesFixture::onInterest, this,
+                                 ref(*face), _1, _2),
+                            RegisterPrefixSuccessCallback(),
+                            bind(&FacesFixture::onRegFailed, this));
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(100)));
+
+  face->receive(Interest("/Hello/World/!"));
+  BOOST_CHECK_EQUAL(nInInterests, 1);
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(100)));
+
+  face->receive(Interest("/Hello/World/!"));
+  BOOST_CHECK_EQUAL(nInInterests, 2);
+
+  face->unsetInterestFilter(regPrefixId);
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(100)));
+
+  face->receive(Interest("/Hello/World/!"));
+  BOOST_CHECK_EQUAL(nInInterests, 2);
+
+  BOOST_CHECK_NO_THROW(face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0)));
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(100)));
+
+  BOOST_CHECK_NO_THROW(face->unsetInterestFilter(static_cast<const InterestFilterId*>(0)));
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(100)));
+}
+
+BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
+{
+  face->enableRegistrationReply();
+
+  const RegisteredPrefixId* regPrefixId =
+    face->registerPrefix("/Hello/World",
+                         bind(&FacesFixture::onRegSucceeded, this),
+                         bind(&FacesFixture::onRegFailed, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(100)));
+  BOOST_CHECK_EQUAL(nRegFailures, 0);
+  BOOST_CHECK_EQUAL(nRegSuccesses, 1);
+
+  face->unregisterPrefix(regPrefixId,
+                         bind(&FacesFixture::onUnregSucceeded, this),
+                         bind(&FacesFixture::onUnregFailed, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(100)));
+  BOOST_CHECK_EQUAL(nUnregFailures, 0);
+  BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
+
+}
+
+BOOST_AUTO_TEST_CASE(SeTwoSimilarFilters)
+{
+  face->enableRegistrationReply();
+
+  face->setInterestFilter("/Hello/World",
+                          bind(&FacesFixture::onInterest, this, ref(*face), _1, _2),
+                          RegisterPrefixSuccessCallback(),
+                          bind(&FacesFixture::onRegFailed, this));
+
+  face->setInterestFilter("/Hello",
+                          bind(&FacesFixture::onInterest2, this, ref(*face), _1, _2),
+                          RegisterPrefixSuccessCallback(),
+                          bind(&FacesFixture::onRegFailed, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(-100)));
+
+  face->receive(Interest("/Hello/World/!"));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(-100)));
+
+  BOOST_CHECK_EQUAL(nRegFailures, 0);
+  BOOST_CHECK_EQUAL(nInInterests, 1);
+  BOOST_CHECK_EQUAL(nInInterests2, 1);
+}
+
+BOOST_AUTO_TEST_CASE(SetTwoDifferentFilters)
+{
+  face->enableRegistrationReply();
+
+  face->setInterestFilter("/Hello/World",
+                          bind(&FacesFixture::onInterest, this, ref(*face), _1, _2),
+                          RegisterPrefixSuccessCallback(),
+                          bind(&FacesFixture::onRegFailed, this));
+
+  face->setInterestFilter("/Los/Angeles/Lakers",
+                          bind(&FacesFixture::onInterest2, this, ref(*face), _1, _2),
+                          RegisterPrefixSuccessCallback(),
+                          bind(&FacesFixture::onRegFailed, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(-100)));
+
+  face->receive(Interest("/Hello/World/!"));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(-100)));
+
+  BOOST_CHECK_EQUAL(nRegFailures, 0);
+  BOOST_CHECK_EQUAL(nInInterests, 1);
+  BOOST_CHECK_EQUAL(nInInterests2, 0);
+}
+
+BOOST_AUTO_TEST_CASE(SetRegexFilterError)
+{
+  face->enableRegistrationReply();
+
+  face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
+                          bind(&FacesFixture::onInterestRegexError, this,
+                               ref(*face), _1, _2),
+                          RegisterPrefixSuccessCallback(),
+                          bind(&FacesFixture::onRegFailed, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(-100)));
+
+  BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
+}
+
+BOOST_AUTO_TEST_CASE(SetRegexFilter)
+{
+  face->enableRegistrationReply();
+
+  face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
+                          bind(&FacesFixture::onInterestRegex, this,
+                               ref(*face), _1, _2),
+                          RegisterPrefixSuccessCallback(),
+                          bind(&FacesFixture::onRegFailed, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(-100)));
+
+  face->receive(Interest("/Hello/World/a"));     // shouldn't match
+  BOOST_CHECK_EQUAL(nInInterests, 0);
+
+  face->receive(Interest("/Hello/World/a/b"));   // should match
+  BOOST_CHECK_EQUAL(nInInterests, 1);
+
+  face->receive(Interest("/Hello/World/a/b/c")); // should match
+  BOOST_CHECK_EQUAL(nInInterests, 2);
+
+  face->receive(Interest("/Hello/World/a/b/d")); // should not match
+  BOOST_CHECK_EQUAL(nInInterests, 2);
+}
+
+
+BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
+{
+  face->enableRegistrationReply();
+
+  face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
+                          bind(&FacesFixture::onInterestRegex, this,
+                               ref(*face), _1, _2));
+
+  face->registerPrefix("/Hello/World",
+                       bind(&FacesFixture::onRegSucceeded, this),
+                       bind(&FacesFixture::onRegFailed, this));
+
+  BOOST_REQUIRE_NO_THROW(face->processEvents(time::milliseconds(100)));
+  BOOST_CHECK_EQUAL(nRegFailures, 0);
+  BOOST_CHECK_EQUAL(nRegSuccesses, 1);
+
+  face->receive(Interest("/Hello/World/a")); // shouldn't match
+  BOOST_CHECK_EQUAL(nInInterests, 0);
+
+  face->receive(Interest("/Hello/World/a/b")); // should match
+  BOOST_CHECK_EQUAL(nInInterests, 1);
+
+  face->receive(Interest("/Hello/World/a/b/c")); // should match
+  BOOST_CHECK_EQUAL(nInInterests, 2);
+
+  face->receive(Interest("/Hello/World/a/b/d")); // should not match
+  BOOST_CHECK_EQUAL(nInInterests, 2);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // tests
+} // namespace ndn