rib: Unit tests for RibManager

Change-Id: I8096089f6cc8a6eea7be51278b29700c79e68d5c
Refs: #1501
diff --git a/rib/main.cpp b/rib/main.cpp
index 75771b7..b123b04 100644
--- a/rib/main.cpp
+++ b/rib/main.cpp
@@ -72,12 +72,17 @@
     }
   };
 
+  Nrd()
+    : m_face(getGlobalIoService())
+  {
+  }
+
   void
   initialize(const std::string& configFile)
   {
     initializeLogging(configFile);
 
-    m_ribManager = make_shared<RibManager>();
+    m_ribManager = make_shared<RibManager>(ndn::ref(m_face));
 
     ConfigFile config((IgnoreNfdAndLogSections()));
     m_ribManager->setConfigFile(config);
@@ -201,6 +206,7 @@
 
 private:
   shared_ptr<RibManager> m_ribManager;
+  ndn::Face m_face;
 };
 
 } // namespace rib
diff --git a/rib/rib-manager.cpp b/rib/rib-manager.cpp
index c5f8666..c8d6111 100644
--- a/rib/rib-manager.cpp
+++ b/rib/rib-manager.cpp
@@ -58,8 +58,8 @@
                      ),
   };
 
-RibManager::RibManager()
-  : m_face(getGlobalIoService())
+RibManager::RibManager(ndn::Face& face)
+  : m_face(face)
   , m_nfdController(m_face)
   , m_localhostValidator(m_face)
   , m_localhopValidator(m_face)
diff --git a/rib/rib-manager.hpp b/rib/rib-manager.hpp
index f664528..6566d1f 100644
--- a/rib/rib-manager.hpp
+++ b/rib/rib-manager.hpp
@@ -56,7 +56,8 @@
     }
   };
 
-  RibManager();
+  explicit
+  RibManager(ndn::Face& face);
 
   void
   registerWithNfd();
@@ -199,7 +200,7 @@
 
 private:
   Rib m_managedRib;
-  ndn::Face m_face;
+  ndn::Face& m_face;
   ndn::nfd::Controller m_nfdController;
   ndn::KeyChain m_keyChain;
   ndn::ValidatorConfig m_localhostValidator;
diff --git a/tests/rib/dummy-face.hpp b/tests/rib/dummy-face.hpp
new file mode 100644
index 0000000..4fde76f
--- /dev/null
+++ b/tests/rib/dummy-face.hpp
@@ -0,0 +1,120 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  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/>.
+ */
+
+#ifndef RIB_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP
+#define RIB_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP
+
+#include <ndn-cxx/face.hpp>
+#include <ndn-cxx/transport/transport.hpp>
+
+namespace ndn {
+
+class DummyTransport : public Transport
+{
+public:
+  void
+  receive(const Block& block)
+  {
+    m_receiveCallback(block);
+  }
+
+  virtual void
+  close()
+  {
+  }
+
+  virtual void
+  pause()
+  {
+  }
+
+  virtual void
+  resume()
+  {
+  }
+
+  virtual void
+  send(const Block& wire)
+  {
+    if (wire.type() == Tlv::Interest) {
+      m_sentInterests->push_back(Interest(wire));
+    }
+    else if (wire.type() == Tlv::Data) {
+      m_sentDatas->push_back(Data(wire));
+    }
+  }
+
+  virtual void
+  send(const Block& header, const Block& payload)
+  {
+    this->send(payload);
+  }
+
+public:
+  std::vector<Interest>* m_sentInterests;
+  std::vector<Data>*     m_sentDatas;
+};
+
+
+/** \brief a Face for unit testing
+ */
+class DummyFace : public Face
+{
+public:
+  explicit
+  DummyFace(shared_ptr<DummyTransport> transport)
+    : Face(transport)
+    , m_transport(transport)
+  {
+    m_transport->m_sentInterests = &m_sentInterests;
+    m_transport->m_sentDatas     = &m_sentDatas;
+  }
+
+  /** \brief cause the Face to receive a packet
+   */
+  template<typename Packet>
+  void
+  receive(const Packet& packet)
+  {
+    m_transport->receive(packet.wireEncode());
+  }
+
+public:
+  std::vector<Interest> m_sentInterests;
+  std::vector<Data>     m_sentDatas;
+
+private:
+  shared_ptr<DummyTransport> m_transport;
+};
+
+inline shared_ptr<DummyFace>
+makeDummyFace()
+{
+  return make_shared<DummyFace>(make_shared<DummyTransport>());
+}
+
+} // namespace ndn
+
+#endif // RIB_TESTS_UNIT_TESTS_TRANSPORT_DUMMY_FACE_HPP
diff --git a/tests/rib/rib-manager.cpp b/tests/rib/rib-manager.cpp
new file mode 100644
index 0000000..b878e30
--- /dev/null
+++ b/tests/rib/rib-manager.cpp
@@ -0,0 +1,226 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  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 "rib/rib-manager.hpp"
+
+#include "tests/test-common.hpp"
+#include "rib/dummy-face.hpp"
+
+namespace nfd {
+namespace rib {
+namespace tests {
+
+class RibManagerFixture : public nfd::tests::BaseFixture
+{
+public:
+  RibManagerFixture()
+    : COMMAND_PREFIX("/localhost/nfd/rib")
+    , ADD_NEXTHOP_VERB("add-nexthop")
+    , REMOVE_NEXTHOP_VERB("remove-nexthop")
+  {
+    face = ndn::makeDummyFace();
+
+    manager = make_shared<RibManager>(ndn::ref(*face));
+    manager->registerWithNfd();
+
+    face->processEvents(time::milliseconds(1));
+    face->m_sentInterests.clear();
+  }
+
+  ~RibManagerFixture()
+  {
+    manager.reset();
+    face.reset();
+  }
+
+  void extractParameters(Interest& interest, Name::Component& verb,
+                         ControlParameters& extractedParameters)
+  {
+    const Name& name = interest.getName();
+    verb = name[COMMAND_PREFIX.size()];
+    const Name::Component& parameterComponent = name[COMMAND_PREFIX.size() + 1];
+
+    Block rawParameters = parameterComponent.blockFromValue();
+    extractedParameters.wireDecode(rawParameters);
+  }
+
+  void receiveCommandInterest(Name& name, ControlParameters& parameters)
+  {
+    name.append(parameters.wireEncode());
+
+    Interest command(name);
+
+    face->receive(command);
+    face->processEvents(time::milliseconds(1));
+  }
+
+public:
+  shared_ptr<RibManager> manager;
+  shared_ptr<ndn::DummyFace> face;
+
+  const Name COMMAND_PREFIX;
+  const Name::Component ADD_NEXTHOP_VERB;
+  const Name::Component REMOVE_NEXTHOP_VERB;
+};
+
+class AuthorizedRibManager : public RibManagerFixture
+{
+public:
+  AuthorizedRibManager()
+  {
+    ConfigFile config;
+    manager->setConfigFile(config);
+
+    const std::string CONFIG_STRING =
+    "rib\n"
+    "{\n"
+    "  localhost_security\n"
+    "  {\n"
+    "    trust-anchor\n"
+    "    {\n"
+    "      type any\n"
+    "    }\n"
+    "  }"
+    "}";
+
+    config.parse(CONFIG_STRING, true, "test-rib");
+  }
+};
+
+typedef RibManagerFixture UnauthorizedRibManager;
+
+BOOST_FIXTURE_TEST_SUITE(RibRibManager, RibManagerFixture)
+
+BOOST_FIXTURE_TEST_CASE(Basic, AuthorizedRibManager)
+{
+  ControlParameters parameters;
+  parameters
+    .setName("/hello")
+    .setFaceId(1)
+    .setCost(10)
+    .setFlags(0)
+    .setOrigin(128)
+    .setExpirationPeriod(ndn::time::milliseconds::max());
+
+  Name commandName("/localhost/nfd/rib/register");
+
+  receiveCommandInterest(commandName, parameters);
+
+  BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1);
+}
+
+BOOST_FIXTURE_TEST_CASE(Register, AuthorizedRibManager)
+{
+  ControlParameters parameters;
+  parameters
+    .setName("/hello")
+    .setFaceId(1)
+    .setCost(10)
+    .setFlags(0)
+    .setOrigin(128)
+    .setExpirationPeriod(ndn::time::milliseconds::max());
+
+  Name commandName("/localhost/nfd/rib/register");
+
+  receiveCommandInterest(commandName, parameters);
+
+  BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1);
+
+  Interest& request = face->m_sentInterests[0];
+
+  ControlParameters extractedParameters;
+  Name::Component verb;
+  extractParameters(request, verb, extractedParameters);
+
+  BOOST_CHECK_EQUAL(verb, ADD_NEXTHOP_VERB);
+  BOOST_CHECK_EQUAL(extractedParameters.getName(), parameters.getName());
+  BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), parameters.getFaceId());
+  BOOST_CHECK_EQUAL(extractedParameters.getCost(), parameters.getCost());
+}
+
+BOOST_FIXTURE_TEST_CASE(Unregister, AuthorizedRibManager)
+{
+  ControlParameters addParameters;
+  addParameters
+    .setName("/hello")
+    .setFaceId(1)
+    .setCost(10)
+    .setFlags(0)
+    .setOrigin(128)
+    .setExpirationPeriod(ndn::time::milliseconds::max());
+
+  Name registerName("/localhost/nfd/rib/register");
+
+  receiveCommandInterest(registerName, addParameters);
+  face->m_sentInterests.clear();
+
+  ControlParameters removeParameters;
+  removeParameters
+    .setName("/hello")
+    .setFaceId(1)
+    .setOrigin(128);
+
+  Name unregisterName("/localhost/nfd/rib/unregister");
+
+  receiveCommandInterest(unregisterName, removeParameters);
+
+  BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1);
+
+  Interest& request = face->m_sentInterests[0];
+
+  ControlParameters extractedParameters;
+  Name::Component verb;
+  extractParameters(request, verb, extractedParameters);
+
+  BOOST_CHECK_EQUAL(verb, REMOVE_NEXTHOP_VERB);
+  BOOST_CHECK_EQUAL(extractedParameters.getName(), removeParameters.getName());
+  BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), removeParameters.getFaceId());
+}
+
+BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedRibManager)
+{
+  ControlParameters parameters;
+  parameters
+    .setName("/hello")
+    .setFaceId(1)
+    .setCost(10)
+    .setFlags(0)
+    .setOrigin(128)
+    .setExpirationPeriod(ndn::time::milliseconds::max());
+
+  Name commandName("/localhost/nfd/rib/register");
+
+  BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 0);
+
+  receiveCommandInterest(commandName, parameters);
+
+  BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace rib
+} // namespace nfd