From 8f23b5d58e20d737e9ac108aef8a891f928a923c Mon Sep 17 00:00:00 2001 From: Nick Gordon Date: Thu, 31 Aug 2017 17:53:07 -0500 Subject: [PATCH] sync: improved unit tests refs: #4264 Change-Id: I981f3d8f653e4750ee6f9d77f2c89188f11d8ebb --- src/common.hpp | 3 + src/communication/sync-logic-handler.hpp | 3 +- src/lsdb.cpp | 39 ++-- src/lsdb.hpp | 4 +- tests/test-lsdb.cpp | 28 ++- tests/test-sync-logic-handler.cpp | 256 +++++++++++++---------- 6 files changed, 198 insertions(+), 135 deletions(-) diff --git a/src/common.hpp b/src/common.hpp index 0d67efc..93b3e87 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -29,6 +29,7 @@ #include #include +#include namespace nlsr { @@ -55,6 +56,8 @@ struct is_iterator; + } // namespace nlsr #endif // NLSR_COMMON_HPP diff --git a/src/communication/sync-logic-handler.hpp b/src/communication/sync-logic-handler.hpp index b46ac7c..afa1464 100644 --- a/src/communication/sync-logic-handler.hpp +++ b/src/communication/sync-logic-handler.hpp @@ -94,12 +94,13 @@ public: void createSyncSocket(const ndn::Name& syncPrefix); -private: +PUBLIC_WITH_TESTS_ELSE_PRIVATE: /*! \brief Simple function to glue Name components together */ void buildUpdatePrefix(); +private: /*! \brief Determine which kind of LSA was updated and fetch it. * * Checks that the received update is not from us, which can happen, diff --git a/src/lsdb.cpp b/src/lsdb.cpp index 4de69ef..789edf6 100644 --- a/src/lsdb.cpp +++ b/src/lsdb.cpp @@ -68,23 +68,8 @@ Lsdb::Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler) , m_sync(m_nlsr.getNlsrFace(), [this] (const ndn::Name& routerName, const std::string& lsaType, const uint64_t& sequenceNumber) { - ndn::Name lsaKey = routerName; - lsaKey.append(lsaType); - - if (lsaType == NameLsa::TYPE_STRING) { - return isNameLsaNew(lsaKey, sequenceNumber); - } - else if (lsaType == AdjLsa::TYPE_STRING) { - return isAdjLsaNew(lsaKey, sequenceNumber); - } - else if (lsaType == CoordinateLsa::TYPE_STRING) { - return isCoordinateLsaNew(lsaKey, sequenceNumber); - } - else { - return false; - } - }, - m_nlsr.getConfParameter()) + return isLsaNew(routerName, lsaType, sequenceNumber); + }, m_nlsr.getConfParameter()) , m_lsaRefreshTime(0) , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT) , m_sequencingManager() @@ -1313,4 +1298,24 @@ Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType) return false; } +bool +Lsdb::isLsaNew(const ndn::Name& routerName, const std::string& lsaType, + const uint64_t& sequenceNumber) { + ndn::Name lsaKey = routerName; + lsaKey.append(lsaType); + + if (lsaType == NameLsa::TYPE_STRING) { + return isNameLsaNew(lsaKey, sequenceNumber); + } + else if (lsaType == AdjLsa::TYPE_STRING) { + return isAdjLsaNew(lsaKey, sequenceNumber); + } + else if (lsaType == CoordinateLsa::TYPE_STRING) { + return isCoordinateLsaNew(lsaKey, sequenceNumber); + } + else { + return false; + } +} + } // namespace nlsr diff --git a/src/lsdb.hpp b/src/lsdb.hpp index 042d727..4574242 100644 --- a/src/lsdb.hpp +++ b/src/lsdb.hpp @@ -51,8 +51,10 @@ public: } bool - doesLsaExist(const ndn::Name& key, const std::string& lsType); + isLsaNew(const ndn::Name& routerName, const std::string& lsaType, const uint64_t& sequenceNumber); + bool + doesLsaExist(const ndn::Name& key, const std::string& lsType); /*! \brief Builds a name LSA for this router and then installs it into the LSDB. diff --git a/tests/test-lsdb.cpp b/tests/test-lsdb.cpp index ad2dfde..866c91b 100644 --- a/tests/test-lsdb.cpp +++ b/tests/test-lsdb.cpp @@ -331,7 +331,33 @@ BOOST_AUTO_TEST_CASE(InstallNameLsa) BOOST_CHECK_EQUAL(nameList, newPrefixes); } -BOOST_AUTO_TEST_SUITE_END() +BOOST_AUTO_TEST_CASE(TestIsLsaNew) +{ + const ndn::Name::Component CONFIG_NETWORK{"/ndn"}; + const ndn::Name::Component CONFIG_SITE{"/memphis"}; + ndn::Name originRouter{}; + originRouter.append(CONFIG_NETWORK).append(CONFIG_SITE).append("/%C1.Router/other-router"); + + // Install Name LSA + NamePrefixList nameList; + NameLsa lsa(originRouter, 999, ndn::time::system_clock::TimePoint::max(), nameList); + + lsdb.installNameLsa(lsa); + + // Lower NameLSA sequence number + uint64_t lowerSeqNo = 998; + BOOST_CHECK(!lsdb.isLsaNew(originRouter, NameLsa::TYPE_STRING, lowerSeqNo)); + + // Same NameLSA sequence number + uint64_t sameSeqNo = 999; + BOOST_CHECK(!lsdb.isLsaNew(originRouter, NameLsa::TYPE_STRING, sameSeqNo)); + + // Higher NameLSA sequence number + uint64_t higherSeqNo = 1000; + BOOST_CHECK(lsdb.isLsaNew(originRouter, NameLsa::TYPE_STRING, higherSeqNo)); +} + +BOOST_AUTO_TEST_SUITE_END() // TestLsdb } // namespace test } // namespace nlsr diff --git a/tests/test-sync-logic-handler.cpp b/tests/test-sync-logic-handler.cpp index 5ebe679..bb6a6db 100644 --- a/tests/test-sync-logic-handler.cpp +++ b/tests/test-sync-logic-handler.cpp @@ -19,10 +19,11 @@ * NLSR, e.g., in COPYING.md file. If not, see . **/ -#include "test-common.hpp" - -#include "nlsr.hpp" #include "communication/sync-logic-handler.hpp" +#include "test-common.hpp" +#include "common.hpp" +#include "nlsr.hpp" +#include "logger.hpp" #include @@ -37,15 +38,25 @@ public: SyncLogicFixture() : face(std::make_shared()) , nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain) - , sync(nlsr.getLsdb().getSyncLogicHandler()) + , testIsLsaNew([] (const ndn::Name& name, const std::string& lsaType, + const uint64_t sequenceNumber) { + return true; + }) , CONFIG_NETWORK("/ndn") , CONFIG_SITE("/site") , CONFIG_ROUTER_NAME("/%C1.Router/this-router") + , OTHER_ROUTER_NAME("/%C1.Router/other-router/") { nlsr.getConfParameter().setNetwork(CONFIG_NETWORK); nlsr.getConfParameter().setSiteName(CONFIG_SITE); nlsr.getConfParameter().setRouterName(CONFIG_ROUTER_NAME); nlsr.getConfParameter().buildRouterPrefix(); + + conf.setNetwork(CONFIG_NETWORK); + conf.setSiteName(CONFIG_SITE); + conf.setRouterName(CONFIG_ROUTER_NAME); + conf.buildRouterPrefix(); + INIT_LOGGERS("/tmp", "TRACE"); } void @@ -67,193 +78,207 @@ public: public: std::shared_ptr face; Nlsr nlsr; - SyncLogicHandler& sync; + ConfParameter conf; + IsLsaNew testIsLsaNew; const std::string CONFIG_NETWORK; const std::string CONFIG_SITE; const std::string CONFIG_ROUTER_NAME; + const std::string OTHER_ROUTER_NAME; const std::vector lsaTypes = {NameLsa::TYPE_STRING, AdjLsa::TYPE_STRING, CoordinateLsa::TYPE_STRING}; }; BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture) +/* Tests that when SyncLogicHandler receives an LSA of either Name or + Adjacency type that appears to be newer, it will emit to its signal + with those LSA details. + */ BOOST_AUTO_TEST_CASE(UpdateForOtherLS) { + SyncLogicHandler sync{std::ref(*face), testIsLsaNew, conf}; + sync.createSyncSocket(conf.getChronosyncPrefix()); + std::vector lsaTypes = {NameLsa::TYPE_STRING, AdjLsa::TYPE_STRING}; uint64_t syncSeqNo = 1; for (const std::string& lsaType : lsaTypes) { - std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() + - CONFIG_SITE + "/%C1.Router/other-router/" + lsaType; + std::string updateName = conf.getLsaPrefix().toUri() + CONFIG_SITE + + OTHER_ROUTER_NAME + lsaType; + + // Actual testing done here -- signal function callback + ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect( + [&, this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) { + BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName); + BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo); + }); receiveUpdate(updateName, syncSeqNo, sync); - - const auto& it = std::find_if(face->sentInterests.begin(), face->sentInterests.end(), - [updateName] (const ndn::Interest& interest) { - return interest.getName().getPrefix(-1) == updateName + "/"; - }); - BOOST_REQUIRE(it != face->sentInterests.end()); } } +/* Tests that when SyncLogicHandler in HR mode receives an LSA of + either Coordinate or Name type that appears to be newer, it will + emit to its signal with those LSA details. + */ BOOST_AUTO_TEST_CASE(UpdateForOtherHR) { - Nlsr nlsr_hr(g_ioService, g_scheduler, std::ref(*face), g_keyChain); - SyncLogicHandler& sync_hr(nlsr_hr.getLsdb().getSyncLogicHandler()); + conf.setHyperbolicState(HYPERBOLIC_STATE_ON); - nlsr_hr.getConfParameter().setNetwork(CONFIG_NETWORK); - nlsr_hr.getConfParameter().setSiteName(CONFIG_SITE); - nlsr_hr.getConfParameter().setRouterName(CONFIG_ROUTER_NAME); - nlsr_hr.getConfParameter().buildRouterPrefix(); - - nlsr_hr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON); - - nlsr_hr.initialize(); + SyncLogicHandler sync{std::ref(*face), testIsLsaNew, conf}; + sync.createSyncSocket(conf.getChronosyncPrefix()); uint64_t syncSeqNo = 1; - std::vector lsaTypes = {NameLsa::TYPE_STRING, CoordinateLsa::TYPE_STRING}; for (const std::string& lsaType : lsaTypes) { - std::string updateName = nlsr_hr.getConfParameter().getLsaPrefix().toUri() + - CONFIG_SITE + "/%C1.Router/other-router/" + lsaType; + std::string updateName = conf.getLsaPrefix().toUri() + CONFIG_SITE + + OTHER_ROUTER_NAME + lsaType; - receiveUpdate(updateName, syncSeqNo, sync_hr); + ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect( + [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) { + BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName); + BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo); + }); - const auto& it = std::find_if(face->sentInterests.begin(), face->sentInterests.end(), - [updateName] (const ndn::Interest& interest) { - return interest.getName().getPrefix(-1) == updateName + "/"; - }); - - BOOST_REQUIRE(it != face->sentInterests.end()); + receiveUpdate(updateName, syncSeqNo, sync); } } +/* Tests that when SyncLogicHandler in HR-dry mode receives an LSA of + any type that appears to be newer, it will emit to its signal with + those LSA details. + */ BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry) { + conf.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN); - Nlsr nlsr_hrdry(g_ioService, g_scheduler, std::ref(*face),g_keyChain); - SyncLogicHandler& sync_hrdry(nlsr_hrdry.getLsdb().getSyncLogicHandler()); - - nlsr_hrdry.getConfParameter().setNetwork(CONFIG_NETWORK); - nlsr_hrdry.getConfParameter().setSiteName(CONFIG_SITE); - nlsr_hrdry.getConfParameter().setRouterName(CONFIG_ROUTER_NAME); - nlsr_hrdry.getConfParameter().buildRouterPrefix(); - - nlsr_hrdry.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN); - - nlsr_hrdry.initialize(); + SyncLogicHandler sync{std::ref(*face), testIsLsaNew, conf}; + sync.createSyncSocket(conf.getChronosyncPrefix()); for (const std::string& lsaType : lsaTypes) { - - std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() + - CONFIG_SITE + "/%C1.Router/other-router/" + lsaType; - - uint64_t syncSeqNo = 1; - receiveUpdate(updateName, syncSeqNo, sync_hrdry); + std::string updateName = conf.getLsaPrefix().toUri() + CONFIG_SITE + + OTHER_ROUTER_NAME + lsaType; - // In HR dry-state all LSA's should be published - const auto& it = std::find_if(face->sentInterests.begin(), face->sentInterests.end(), - [updateName] (const ndn::Interest& interest) { - return interest.getName().getPrefix(-1) == updateName + "/"; - }); - BOOST_REQUIRE(it != face->sentInterests.end()); + ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect( + [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) { + BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName); + BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo); + }); + + receiveUpdate(updateName, syncSeqNo, sync); } } +/* Tests that when SyncLogicHandler receives an update for an LSA with + details matching this router's details, it will *not* emit to its + signal those LSA details. + */ BOOST_AUTO_TEST_CASE(NoUpdateForSelf) { + const uint64_t sequenceNumber = 1; + + SyncLogicHandler sync{std::ref(*face), testIsLsaNew, conf}; + sync.createSyncSocket(conf.getChronosyncPrefix()); + for (const std::string& lsaType : lsaTypes) { - std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() + - CONFIG_SITE + CONFIG_ROUTER_NAME + lsaType; + // To ensure that we get correctly-separated components, create + // and modify a Name to hand off. + ndn::Name updateName = ndn::Name{conf.getLsaPrefix()}; + updateName.append(CONFIG_SITE).append(CONFIG_ROUTER_NAME).append(lsaType); - receiveUpdate(updateName, 1, sync); + ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect( + [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) { + BOOST_FAIL("Updates for self should not be emitted!"); + }); - std::vector& interests = face->sentInterests; - BOOST_CHECK_EQUAL(interests.size(), 0); + receiveUpdate(updateName.toUri(), sequenceNumber, sync); } } +/* Tests that when SyncLogicHandler receives an update for an LSA with + details that do not match the expected format, it will *not* emit + to its signal those LSA details. + */ BOOST_AUTO_TEST_CASE(MalformedUpdate) { - for (const std::string& lsaType : lsaTypes) { - std::string updateName = CONFIG_SITE + nlsr.getConfParameter().getLsaPrefix().toUri() + - CONFIG_ROUTER_NAME + lsaType; + const uint64_t sequenceNumber = 1; - std::vector& interests = face->sentInterests; - BOOST_CHECK_EQUAL(interests.size(), 0); + SyncLogicHandler sync{std::ref(*face), testIsLsaNew, conf}; + sync.createSyncSocket(conf.getChronosyncPrefix()); + + for (const std::string& lsaType : lsaTypes) { + ndn::Name updateName{CONFIG_SITE}; + updateName.append(CONFIG_ROUTER_NAME).append(lsaType); + + ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect( + [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) { + BOOST_FAIL("Malformed updates should not be emitted!"); + }); + + receiveUpdate(updateName.toUri(), sequenceNumber, sync); } } -BOOST_AUTO_TEST_CASE(SequenceNumber) +/* Tests that when SyncLogicHandler receives an update for an LSA with + details that do not appear to be new, it will *not* emit to its + signal those LSA details. + */ +BOOST_AUTO_TEST_CASE(LsaNotNew) { - std::string originRouter = CONFIG_NETWORK + CONFIG_SITE + "/%C1.Router/other-router/"; + auto testLsaAlwaysFalse = [] (const ndn::Name& routerName, const std::string& lsaType, + const uint64_t& sequenceNumber) { + return false; + }; - Lsdb& lsdb = nlsr.getLsdb(); - - // Install Name LSA - NamePrefixList nameList; - NameLsa lsa(originRouter, 999, ndn::time::system_clock::TimePoint::max(), nameList); - lsdb.installNameLsa(lsa); - - // Install Adj LSA - AdjacencyList adjList; - AdjLsa adjLsa(originRouter, 1000, ndn::time::system_clock::TimePoint::max(), - 3 , adjList); - lsdb.installAdjLsa(adjLsa); - - std::vector angles = {0.0}; - - // Install Cor LSA - CoordinateLsa corLsa(originRouter, 1000, ndn::time::system_clock::TimePoint::max(), - 0, angles); - lsdb.installCoordinateLsa(corLsa); + const uint64_t sequenceNumber = 1; + SyncLogicHandler sync{std::ref(*face), testLsaAlwaysFalse, conf}; + sync.createSyncSocket(conf.getChronosyncPrefix()); + ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect( + [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) { + BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!"); + }); std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() + CONFIG_SITE + "/%C1.Router/other-router/" + NameLsa::TYPE_STRING; - // Lower NameLSA sequence number - uint64_t lowerSeqNo = 998; - receiveUpdate(updateName, lowerSeqNo, sync); - - std::vector& interests = face->sentInterests; - BOOST_REQUIRE_EQUAL(interests.size(), 0); - - // Same NameLSA sequence number - uint64_t sameSeqNo = 999; - receiveUpdate(updateName, sameSeqNo, sync); - - interests = face->sentInterests; - BOOST_REQUIRE_EQUAL(interests.size(), 0); - - // Higher NameLSA sequence number - uint64_t higherSeqNo = 1000; - receiveUpdate(updateName, higherSeqNo, sync); - - interests = face->sentInterests; - BOOST_REQUIRE_EQUAL(interests.size(), 1); - - std::vector::iterator it = interests.begin(); - BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "/"); + receiveUpdate(updateName, sequenceNumber, sync); } +/* Tests that SyncLogicHandler successfully concatenates configured + variables together to form the necessary prefixes to advertise + through ChronoSync. + */ BOOST_AUTO_TEST_CASE(UpdatePrefix) { + + SyncLogicHandler sync{std::ref(*face), testIsLsaNew, conf}; + ndn::Name expectedPrefix = nlsr.getConfParameter().getLsaPrefix(); expectedPrefix.append(CONFIG_SITE); expectedPrefix.append(CONFIG_ROUTER_NAME); - nlsr.initialize(); + sync.buildUpdatePrefix(); - BOOST_CHECK_EQUAL(sync.m_nameLsaUserPrefix, ndn::Name(expectedPrefix).append(NameLsa::TYPE_STRING)); - BOOST_CHECK_EQUAL(sync.m_adjLsaUserPrefix, ndn::Name(expectedPrefix).append(AdjLsa::TYPE_STRING)); - BOOST_CHECK_EQUAL(sync.m_coorLsaUserPrefix, ndn::Name(expectedPrefix).append(CoordinateLsa::TYPE_STRING)); + BOOST_CHECK_EQUAL(sync.m_nameLsaUserPrefix, + ndn::Name(expectedPrefix).append(NameLsa::TYPE_STRING)); + BOOST_CHECK_EQUAL(sync.m_adjLsaUserPrefix, + ndn::Name(expectedPrefix).append(AdjLsa::TYPE_STRING)); + BOOST_CHECK_EQUAL(sync.m_coorLsaUserPrefix, + ndn::Name(expectedPrefix).append(CoordinateLsa::TYPE_STRING)); } +/* Tests that SyncLogicHandler's socket will be created when + Nlsr::initialize is called, preventing use of sync before the + socket is created. + + NB: This test is as much an Nlsr class test as a SyncLogicHandler + class test, but it rides the line and ends up here. + */ BOOST_AUTO_TEST_CASE(CreateSyncSocketOnInitialization) // Bug #2649 { nlsr.initialize(); @@ -264,7 +289,8 @@ BOOST_AUTO_TEST_CASE(CreateSyncSocketOnInitialization) // Bug #2649 BOOST_REQUIRE(lsa == nullptr); // Publish a routing update before an Adjacency LSA is built - BOOST_CHECK_NO_THROW(sync.publishRoutingUpdate(AdjLsa::TYPE_STRING, 0)); + BOOST_CHECK_NO_THROW(nlsr.getLsdb().getSyncLogicHandler() + .publishRoutingUpdate(AdjLsa::TYPE_STRING, 0)); } BOOST_AUTO_TEST_SUITE_END()