rib: Unit tests for RibManager
Change-Id: I8096089f6cc8a6eea7be51278b29700c79e68d5c
Refs: #1501
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