rib: remote prefix registration
Change-Id: I0ee01317c213380481eed5c3a13cc19fb0b897ee
Refs: #2056
diff --git a/tests/identity-management-fixture.cpp b/tests/identity-management-fixture.cpp
new file mode 100644
index 0000000..c668f54
--- /dev/null
+++ b/tests/identity-management-fixture.cpp
@@ -0,0 +1,53 @@
+/* -*- 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 "identity-management-fixture.hpp"
+
+namespace nfd {
+namespace tests {
+
+IdentityManagementFixture::IdentityManagementFixture()
+ : m_keyChain("sqlite3", "file")
+{
+}
+
+IdentityManagementFixture::~IdentityManagementFixture()
+{
+ for (auto&& id : m_identities) {
+ m_keyChain.deleteIdentity(id);
+ }
+}
+
+bool
+IdentityManagementFixture::addIdentity(const ndn::Name& identity, const ndn::KeyParams& params)
+{
+ try {
+ m_keyChain.createIdentity(identity, params);
+ m_identities.push_back(identity);
+ return true;
+ }
+ catch (std::runtime_error&) {
+ return false;
+ }
+}
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/identity-management-fixture.hpp b/tests/identity-management-fixture.hpp
new file mode 100644
index 0000000..265c3bc
--- /dev/null
+++ b/tests/identity-management-fixture.hpp
@@ -0,0 +1,56 @@
+/* -*- 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 "tests/test-common.hpp"
+#include <ndn-cxx/security/key-chain.hpp>
+#include <vector>
+
+#include "boost-test.hpp"
+
+namespace nfd {
+namespace tests {
+
+/**
+ * @brief IdentityManagementFixture is a test suite level fixture.
+ *
+ * Test cases in the suite can use this fixture to create identities.
+ * Identities added via addIdentity method are automatically deleted
+ * during test teardown.
+ */
+class IdentityManagementFixture : public nfd::tests::BaseFixture
+{
+public:
+ IdentityManagementFixture();
+
+ ~IdentityManagementFixture();
+
+ // @brief add identity, return true if succeed.
+ bool
+ addIdentity(const ndn::Name& identity,
+ const ndn::KeyParams& params = ndn::KeyChain::DEFAULT_KEY_PARAMS);
+
+protected:
+ ndn::KeyChain m_keyChain;
+ std::vector<ndn::Name> m_identities;
+};
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/rib/remote-registrator.cpp b/tests/rib/remote-registrator.cpp
new file mode 100644
index 0000000..e0b4d3a
--- /dev/null
+++ b/tests/rib/remote-registrator.cpp
@@ -0,0 +1,459 @@
+/* -*- 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/remote-registrator.hpp"
+
+#include "tests/limited-io.hpp"
+#include "tests/identity-management-fixture.hpp"
+#include <ndn-cxx/util/dummy-client-face.hpp>
+
+namespace nfd {
+namespace rib {
+namespace tests {
+
+class RemoteRegistratorFixture : public nfd::tests::IdentityManagementFixture
+{
+public:
+ RemoteRegistratorFixture()
+ : face(ndn::util::makeDummyClientFace())
+ , controller(make_shared<ndn::nfd::Controller>(std::ref(*face), m_keyChain))
+ , remoteRegistrator(make_shared<RemoteRegistrator>(std::ref(*controller),
+ m_keyChain,
+ rib))
+ , COMMAND_PREFIX("/localhop/nfd/rib")
+ , REGISTER_VERB("register")
+ , UNREGISTER_VERB("unregister")
+ {
+ readConfig();
+
+ registerCallback();
+
+ face->processEvents(time::milliseconds(1));
+ face->sentInterests.clear();
+ }
+
+ void
+ readConfig()
+ {
+ ConfigFile config;
+ config.addSectionHandler("remote_register",
+ bind(&RemoteRegistrator::loadConfig, remoteRegistrator, _1));
+
+ const std::string CONFIG_STRING =
+ "remote_register\n"
+ "{\n"
+ " cost 15\n"
+ " timeout 10000\n"
+ " retry 2\n"
+ " refresh_interval 5\n"
+ "}";
+
+ config.parse(CONFIG_STRING, true, "test-remote-register");
+ }
+
+ void
+ registerCallback()
+ {
+ rib.afterInsertEntry += [this] (const Name& prefix) {
+ remoteRegistrator->registerPrefix(prefix);
+ };
+
+ rib.afterEraseEntry += [this] (const Name& prefix) {
+ remoteRegistrator->unregisterPrefix(prefix);
+ };
+ }
+
+ void
+ insertEntryWithIdentity(Name identity,
+ name::Component appName = DEFAULT_APP_NAME,
+ uint64_t faceId = 0)
+ {
+ BOOST_CHECK_EQUAL(addIdentity(identity), true);
+
+ FaceEntry faceEntry;
+ faceEntry.faceId = faceId;
+
+ rib.insert(identity.append(appName), faceEntry);
+
+ face->processEvents(time::milliseconds(1));
+ }
+
+ void
+ insertEntryWithoutIdentity(Name identity,
+ name::Component appName = DEFAULT_APP_NAME,
+ uint64_t faceId = 0)
+ {
+ FaceEntry faceEntry;
+ faceEntry.faceId = faceId;
+
+ rib.insert(identity.append(appName), faceEntry);
+
+ face->processEvents(time::milliseconds(1));
+ }
+
+ void
+ eraseEntryWithIdentity(Name identity,
+ name::Component appName = DEFAULT_APP_NAME,
+ uint64_t faceId = 0)
+ {
+ BOOST_CHECK_EQUAL(addIdentity(identity), true);
+
+ FaceEntry faceEntry;
+ faceEntry.faceId = faceId;
+
+ rib.erase(identity.append(appName), faceEntry);
+
+ face->processEvents(time::milliseconds(1));
+ }
+
+ void
+ eraseEntryWithoutIdentity(Name identity,
+ name::Component appName = DEFAULT_APP_NAME,
+ uint64_t faceId = 0)
+ {
+ FaceEntry faceEntry;
+ faceEntry.faceId = faceId;
+
+ rib.erase(identity.append(appName), faceEntry);
+
+ face->processEvents(time::milliseconds(1));
+ }
+
+ void
+ eraseFace(uint64_t faceId)
+ {
+ rib.erase(faceId);
+
+ face->processEvents(time::milliseconds(1));
+ }
+
+ void
+ connectToHub()
+ {
+ rib.insert(Name("/localhop/nfd"), FaceEntry());
+
+ face->processEvents(time::milliseconds(1));
+ }
+
+ void
+ disconnectToHub()
+ {
+ rib.erase(Name("/localhop/nfd"), FaceEntry());
+
+ face->processEvents(time::milliseconds(1));
+ }
+
+ void
+ extractParameters(Interest& interest, Name::Component& verb,
+ ndn::nfd::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);
+ }
+
+public:
+ Rib rib;
+ shared_ptr<ndn::util::DummyClientFace> face;
+ shared_ptr<ndn::nfd::Controller> controller;
+ shared_ptr<RemoteRegistrator> remoteRegistrator;
+
+ const Name COMMAND_PREFIX;
+ const name::Component REGISTER_VERB;
+ const name::Component UNREGISTER_VERB;
+
+ static const name::Component DEFAULT_APP_NAME;
+};
+
+const name::Component RemoteRegistratorFixture::DEFAULT_APP_NAME("app");
+
+BOOST_FIXTURE_TEST_SUITE(RemoteRegistrator, RemoteRegistratorFixture)
+
+BOOST_FIXTURE_TEST_CASE(AutoTest, RemoteRegistratorFixture)
+{
+ BOOST_REQUIRE_EQUAL(1, 1);
+}
+
+BOOST_FIXTURE_TEST_CASE(RegisterWithoutConnection, RemoteRegistratorFixture)
+{
+ insertEntryWithIdentity("/remote/register");
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(RegisterWithoutIdentity, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ insertEntryWithoutIdentity("/remote/register");
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(RegisterWithHubPrefix, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(RegisterWithLocalPrefix, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ insertEntryWithIdentity("/localhost/prefix");
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(RegisterBasic, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ Name identity("/remote/register");
+ insertEntryWithIdentity(identity);
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
+
+ Interest& request = face->sentInterests[0];
+
+ ndn::nfd::ControlParameters extractedParameters;
+ Name::Component verb;
+ extractParameters(request, verb, extractedParameters);
+
+ BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
+ BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
+}
+
+BOOST_FIXTURE_TEST_CASE(RegisterAdvanced, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ Name identity("/remote/register");
+ Name identityAddRib("/remote/register/rib");
+ insertEntryWithIdentity(identityAddRib);
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
+
+ Interest& request = face->sentInterests[0];
+
+ ndn::nfd::ControlParameters extractedParameters;
+ Name::Component verb;
+ extractParameters(request, verb, extractedParameters);
+
+ BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
+ BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
+}
+
+BOOST_FIXTURE_TEST_CASE(UnregisterWithoutInsert, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ eraseEntryWithIdentity("/remote/register");
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(UnregisterWithoutConnection, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ disconnectToHub();
+
+ Name indentity("/remote/register");
+ remoteRegistrator->m_regEntries.insert(
+ nfd::rib::RemoteRegistrator::RegisteredEntry(indentity, EventId()));
+
+ eraseEntryWithIdentity(indentity);
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(UnregisterWithoutSuccessfullRegistration,
+ RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ Name identity("/remote/register");
+
+ insertEntryWithIdentity(identity);
+
+ eraseEntryWithIdentity(identity);
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
+
+ Interest& request = face->sentInterests[0];
+
+ ndn::nfd::ControlParameters extractedParameters;
+ Name::Component verb;
+ extractParameters(request, verb, extractedParameters);
+
+ BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
+ BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
+}
+
+BOOST_FIXTURE_TEST_CASE(UnregisterBasic, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ Name identity("/remote/register");
+
+ insertEntryWithIdentity(identity);
+
+ EventId event;
+
+ remoteRegistrator->m_regEntries.insert(
+ nfd::rib::RemoteRegistrator::RegisteredEntry(identity, event));
+
+ eraseEntryWithIdentity(identity);
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
+
+ Interest& request = face->sentInterests[1];
+
+ ndn::nfd::ControlParameters extractedParameters;
+ Name::Component verb;
+ extractParameters(request, verb, extractedParameters);
+
+ BOOST_CHECK_EQUAL(verb, UNREGISTER_VERB);
+ BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
+}
+
+BOOST_FIXTURE_TEST_CASE(UnregisterAdvanced, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ Name identityShort("/remote/register");
+ Name identityLong("/remote/register/long");
+
+ EventId eventShort;
+ EventId eventLong;
+
+ insertEntryWithIdentity(identityShort, name::Component("appA"));
+
+ remoteRegistrator->m_regEntries.insert(
+ nfd::rib::RemoteRegistrator::RegisteredEntry(identityShort,
+ eventShort));
+
+ insertEntryWithIdentity(identityShort, name::Component("appB"));
+
+ insertEntryWithIdentity(identityLong);
+
+ remoteRegistrator->m_regEntries.insert(
+ nfd::rib::RemoteRegistrator::RegisteredEntry(identityLong,
+ eventLong));
+
+ // two registration commands are generated for identityShort and identityLong
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
+
+ eraseEntryWithIdentity(identityShort, name::Component("appA"));
+
+ // no unregistration command is generated as appB also exists
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
+
+ eraseEntryWithIdentity(identityShort, name::Component("appB"));
+
+ // one unregistration command is generated for identityShort
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 3);
+
+ Interest& request = face->sentInterests[2];
+
+ ndn::nfd::ControlParameters extractedParameters;
+ Name::Component verb;
+ extractParameters(request, verb, extractedParameters);
+
+ BOOST_CHECK_EQUAL(verb, UNREGISTER_VERB);
+ BOOST_CHECK_EQUAL(extractedParameters.getName(), identityShort);
+}
+
+BOOST_FIXTURE_TEST_CASE(EraseFace, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ Name identity("/remote/register");
+ uint64_t faceId = 517;
+
+ insertEntryWithIdentity(identity, DEFAULT_APP_NAME, faceId);
+
+ EventId event;
+
+ remoteRegistrator->m_regEntries.insert(
+ nfd::rib::RemoteRegistrator::RegisteredEntry(identity, event));
+
+ eraseFace(faceId);
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
+
+ Interest& request = face->sentInterests[1];
+
+ ndn::nfd::ControlParameters extractedParameters;
+ Name::Component verb;
+ extractParameters(request, verb, extractedParameters);
+
+ BOOST_CHECK_EQUAL(verb, UNREGISTER_VERB);
+ BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
+}
+
+BOOST_FIXTURE_TEST_CASE(RebuildConnection, RemoteRegistratorFixture)
+{
+ connectToHub();
+
+ Name identity("/remote/register");
+
+ insertEntryWithIdentity(identity);
+
+ EventId event;
+
+ remoteRegistrator->m_regEntries.insert(
+ nfd::rib::RemoteRegistrator::RegisteredEntry(identity, event));
+
+ disconnectToHub();
+
+ connectToHub();
+
+ BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
+
+ Interest& request1 = face->sentInterests[0];
+ Interest& request2 = face->sentInterests[1];
+
+ ndn::nfd::ControlParameters extractedParameters1, extractedParameters2;
+ Name::Component verb1, verb2;
+ extractParameters(request1, verb1, extractedParameters1);
+ extractParameters(request2, verb2, extractedParameters2);
+
+ BOOST_CHECK_EQUAL(verb1, REGISTER_VERB);
+ BOOST_CHECK_EQUAL(verb2, REGISTER_VERB);
+ BOOST_CHECK_EQUAL(extractedParameters1.getName(),
+ extractedParameters2.getName());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace rib
+} // namespace nfd