blob: f696d426bd868fd34825d80e5d3d51a835c6a85c [file] [log] [blame]
Vince Lehman904c2412014-09-23 19:36:11 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
Vince Lehman904c2412014-09-23 19:36:11 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Vince Lehman904c2412014-09-23 19:36:11 -050020 **/
21
Vince Lehman904c2412014-09-23 19:36:11 -050022#include "communication/sync-logic-handler.hpp"
Nick Gordon8f23b5d2017-08-31 17:53:07 -050023#include "test-common.hpp"
24#include "common.hpp"
25#include "nlsr.hpp"
Nick Gordon727d4832017-10-13 18:04:25 -050026#include "lsa.hpp"
Nick Gordon8f23b5d2017-08-31 17:53:07 -050027#include "logger.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050028
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060029#include <ndn-cxx/util/dummy-client-face.hpp>
30
Vince Lehman904c2412014-09-23 19:36:11 -050031namespace nlsr {
32namespace test {
33
dmcoomes9f936662017-03-02 10:33:09 -060034using std::shared_ptr;
Vince Lehman904c2412014-09-23 19:36:11 -050035
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050036class SyncLogicFixture : public UnitTestTimeFixture
Vince Lehman904c2412014-09-23 19:36:11 -050037{
38public:
39 SyncLogicFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050040 : face(m_ioService, m_keyChain)
41 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Nick Gordon727d4832017-10-13 18:04:25 -050042 , testIsLsaNew([] (const ndn::Name& name, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -050043 const uint64_t sequenceNumber) {
44 return true;
45 })
Vince Lehman904c2412014-09-23 19:36:11 -050046 , CONFIG_NETWORK("/ndn")
47 , CONFIG_SITE("/site")
48 , CONFIG_ROUTER_NAME("/%C1.Router/this-router")
Nick Gordon8f23b5d2017-08-31 17:53:07 -050049 , OTHER_ROUTER_NAME("/%C1.Router/other-router/")
Vince Lehman904c2412014-09-23 19:36:11 -050050 {
51 nlsr.getConfParameter().setNetwork(CONFIG_NETWORK);
52 nlsr.getConfParameter().setSiteName(CONFIG_SITE);
53 nlsr.getConfParameter().setRouterName(CONFIG_ROUTER_NAME);
54 nlsr.getConfParameter().buildRouterPrefix();
Nick Gordon8f23b5d2017-08-31 17:53:07 -050055
56 conf.setNetwork(CONFIG_NETWORK);
57 conf.setSiteName(CONFIG_SITE);
58 conf.setRouterName(CONFIG_ROUTER_NAME);
59 conf.buildRouterPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050060
61 addIdentity(conf.getRouterPrefix());
62
Nick Gordon8f23b5d2017-08-31 17:53:07 -050063 INIT_LOGGERS("/tmp", "TRACE");
Vince Lehman904c2412014-09-23 19:36:11 -050064 }
65
66 void
Nick Gordon5c467f02016-07-13 13:40:10 -050067 receiveUpdate(std::string prefix, uint64_t seqNo, SyncLogicHandler& p_sync)
Vince Lehman904c2412014-09-23 19:36:11 -050068 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060069 chronosync::MissingDataInfo info = {ndn::Name(prefix).appendNumber(1), 0, seqNo};
Vince Lehman904c2412014-09-23 19:36:11 -050070
Ashlesh Gawande415676b2016-12-22 00:26:23 -060071 std::vector<chronosync::MissingDataInfo> updates;
Vince Lehman904c2412014-09-23 19:36:11 -050072 updates.push_back(info);
73
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050074 this->advanceClocks(ndn::time::milliseconds(1), 10);
75 face.sentInterests.clear();
Vince Lehman904c2412014-09-23 19:36:11 -050076
Ashlesh Gawande7600c902017-06-21 13:28:35 -050077 p_sync.onChronoSyncUpdate(updates);
Vince Lehman904c2412014-09-23 19:36:11 -050078
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050079 this->advanceClocks(ndn::time::milliseconds(1), 10);
Vince Lehman904c2412014-09-23 19:36:11 -050080 }
81
82public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050083 ndn::util::DummyClientFace face;
Vince Lehman904c2412014-09-23 19:36:11 -050084 Nlsr nlsr;
Nick Gordon8f23b5d2017-08-31 17:53:07 -050085 ConfParameter conf;
Nick Gordon727d4832017-10-13 18:04:25 -050086 SyncLogicHandler::IsLsaNew testIsLsaNew;
Vince Lehman904c2412014-09-23 19:36:11 -050087
88 const std::string CONFIG_NETWORK;
89 const std::string CONFIG_SITE;
90 const std::string CONFIG_ROUTER_NAME;
Nick Gordon8f23b5d2017-08-31 17:53:07 -050091 const std::string OTHER_ROUTER_NAME;
Nick Gordon727d4832017-10-13 18:04:25 -050092 const std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME, Lsa::Type::ADJACENCY,
93 Lsa::Type::COORDINATE};
Vince Lehman904c2412014-09-23 19:36:11 -050094};
95
96BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
97
Nick Gordon8f23b5d2017-08-31 17:53:07 -050098/* Tests that when SyncLogicHandler receives an LSA of either Name or
99 Adjacency type that appears to be newer, it will emit to its signal
100 with those LSA details.
101 */
Nick Gordon5c467f02016-07-13 13:40:10 -0500102BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
Vince Lehman904c2412014-09-23 19:36:11 -0500103{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500104 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500105 sync.createSyncSocket(conf.getChronosyncPrefix());
106
Nick Gordon727d4832017-10-13 18:04:25 -0500107 std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME, Lsa::Type::ADJACENCY};
Vince Lehman904c2412014-09-23 19:36:11 -0500108
Nick Gordon5c467f02016-07-13 13:40:10 -0500109 uint64_t syncSeqNo = 1;
Vince Lehman904c2412014-09-23 19:36:11 -0500110
Nick Gordon727d4832017-10-13 18:04:25 -0500111 for (const Lsa::Type& lsaType : lsaTypes) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500112 std::string updateName = conf.getLsaPrefix().toUri() + CONFIG_SITE
Nick Gordon727d4832017-10-13 18:04:25 -0500113 + OTHER_ROUTER_NAME + std::to_string(lsaType);
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500114
115 // Actual testing done here -- signal function callback
116 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
117 [&, this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
118 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
119 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
120 });
Nick Gordone8e03ac2016-07-07 14:24:38 -0500121
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500122 receiveUpdate(updateName, syncSeqNo, sync);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500123 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500124}
125
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500126/* Tests that when SyncLogicHandler in HR mode receives an LSA of
127 either Coordinate or Name type that appears to be newer, it will
128 emit to its signal with those LSA details.
129 */
Nick Gordon5c467f02016-07-13 13:40:10 -0500130BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
131{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500132 conf.setHyperbolicState(HYPERBOLIC_STATE_ON);
Nick Gordon5c467f02016-07-13 13:40:10 -0500133
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500134 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500135 sync.createSyncSocket(conf.getChronosyncPrefix());
Nick Gordon5c467f02016-07-13 13:40:10 -0500136
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500137 uint64_t syncSeqNo = 1;
Nick Gordon727d4832017-10-13 18:04:25 -0500138 std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME, Lsa::Type::COORDINATE};
Nick Gordon5c467f02016-07-13 13:40:10 -0500139
Nick Gordon727d4832017-10-13 18:04:25 -0500140 for (const Lsa::Type& lsaType : lsaTypes) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500141 std::string updateName = conf.getLsaPrefix().toUri() + CONFIG_SITE
Nick Gordon727d4832017-10-13 18:04:25 -0500142 + OTHER_ROUTER_NAME + std::to_string(lsaType);
Nick Gordon5c467f02016-07-13 13:40:10 -0500143
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500144 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
145 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
146 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
147 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
148 });
Nick Gordon5c467f02016-07-13 13:40:10 -0500149
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500150 receiveUpdate(updateName, syncSeqNo, sync);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500151 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500152}
153
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500154/* Tests that when SyncLogicHandler in HR-dry mode receives an LSA of
155 any type that appears to be newer, it will emit to its signal with
156 those LSA details.
157 */
Nick Gordon5c467f02016-07-13 13:40:10 -0500158BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
159{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500160 conf.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
Nick Gordon5c467f02016-07-13 13:40:10 -0500161
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500162 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500163 sync.createSyncSocket(conf.getChronosyncPrefix());
Nick Gordon5c467f02016-07-13 13:40:10 -0500164
Nick Gordon727d4832017-10-13 18:04:25 -0500165 for (const Lsa::Type& lsaType : lsaTypes) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500166 uint64_t syncSeqNo = 1;
Nick Gordon5c467f02016-07-13 13:40:10 -0500167
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500168 std::string updateName = conf.getLsaPrefix().toUri() + CONFIG_SITE
Nick Gordon727d4832017-10-13 18:04:25 -0500169 + OTHER_ROUTER_NAME + std::to_string(lsaType);
Nick Gordon5c467f02016-07-13 13:40:10 -0500170
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500171 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
172 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
173 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
174 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
175 });
176
177 receiveUpdate(updateName, syncSeqNo, sync);
Nick Gordone8e03ac2016-07-07 14:24:38 -0500178 }
Vince Lehman904c2412014-09-23 19:36:11 -0500179}
180
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500181/* Tests that when SyncLogicHandler receives an update for an LSA with
182 details matching this router's details, it will *not* emit to its
183 signal those LSA details.
184 */
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500185BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
186{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500187 const uint64_t sequenceNumber = 1;
188
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500189 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500190 sync.createSyncSocket(conf.getChronosyncPrefix());
191
Nick Gordon727d4832017-10-13 18:04:25 -0500192 for (const Lsa::Type& lsaType : lsaTypes) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500193 // To ensure that we get correctly-separated components, create
194 // and modify a Name to hand off.
195 ndn::Name updateName = ndn::Name{conf.getLsaPrefix()};
Nick Gordon727d4832017-10-13 18:04:25 -0500196 updateName.append(CONFIG_SITE).append(CONFIG_ROUTER_NAME).append(std::to_string(lsaType));
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500197
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500198 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
199 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
200 BOOST_FAIL("Updates for self should not be emitted!");
201 });
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500202
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500203 receiveUpdate(updateName.toUri(), sequenceNumber, sync);
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500204 }
205}
206
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500207/* Tests that when SyncLogicHandler receives an update for an LSA with
208 details that do not match the expected format, it will *not* emit
209 to its signal those LSA details.
210 */
Vince Lehman904c2412014-09-23 19:36:11 -0500211BOOST_AUTO_TEST_CASE(MalformedUpdate)
212{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500213 const uint64_t sequenceNumber = 1;
Vince Lehman904c2412014-09-23 19:36:11 -0500214
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500215 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500216 sync.createSyncSocket(conf.getChronosyncPrefix());
217
Nick Gordon727d4832017-10-13 18:04:25 -0500218 for (const Lsa::Type& lsaType : lsaTypes) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500219 ndn::Name updateName{CONFIG_SITE};
Nick Gordon727d4832017-10-13 18:04:25 -0500220 updateName.append(CONFIG_ROUTER_NAME).append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500221
222 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
223 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
224 BOOST_FAIL("Malformed updates should not be emitted!");
225 });
226
227 receiveUpdate(updateName.toUri(), sequenceNumber, sync);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500228 }
Vince Lehman904c2412014-09-23 19:36:11 -0500229}
230
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500231/* Tests that when SyncLogicHandler receives an update for an LSA with
232 details that do not appear to be new, it will *not* emit to its
233 signal those LSA details.
234 */
235BOOST_AUTO_TEST_CASE(LsaNotNew)
Vince Lehman904c2412014-09-23 19:36:11 -0500236{
Nick Gordon727d4832017-10-13 18:04:25 -0500237 auto testLsaAlwaysFalse = [] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500238 const uint64_t& sequenceNumber) {
239 return false;
240 };
Vince Lehman904c2412014-09-23 19:36:11 -0500241
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500242 const uint64_t sequenceNumber = 1;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500243 SyncLogicHandler sync{face, testLsaAlwaysFalse, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500244 sync.createSyncSocket(conf.getChronosyncPrefix());
245 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
246 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
247 BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!");
248 });
Vince Lehman904c2412014-09-23 19:36:11 -0500249
250 std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
Nick Gordon727d4832017-10-13 18:04:25 -0500251 CONFIG_SITE + "/%C1.Router/other-router/" +
252 std::to_string(Lsa::Type::NAME);
Vince Lehman904c2412014-09-23 19:36:11 -0500253
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500254 receiveUpdate(updateName, sequenceNumber, sync);
Vince Lehman904c2412014-09-23 19:36:11 -0500255}
256
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500257/* Tests that SyncLogicHandler successfully concatenates configured
258 variables together to form the necessary prefixes to advertise
259 through ChronoSync.
260 */
Vince Lehmanc11cc202015-01-20 11:41:33 -0600261BOOST_AUTO_TEST_CASE(UpdatePrefix)
262{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500263
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500264 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500265
Vince Lehmanc11cc202015-01-20 11:41:33 -0600266 ndn::Name expectedPrefix = nlsr.getConfParameter().getLsaPrefix();
267 expectedPrefix.append(CONFIG_SITE);
268 expectedPrefix.append(CONFIG_ROUTER_NAME);
269
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500270 sync.buildUpdatePrefix();
Vince Lehmanc11cc202015-01-20 11:41:33 -0600271
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500272 BOOST_CHECK_EQUAL(sync.m_nameLsaUserPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500273 ndn::Name(expectedPrefix).append(std::to_string(Lsa::Type::NAME)));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500274 BOOST_CHECK_EQUAL(sync.m_adjLsaUserPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500275 ndn::Name(expectedPrefix).append(std::to_string(Lsa::Type::ADJACENCY)));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500276 BOOST_CHECK_EQUAL(sync.m_coorLsaUserPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500277 ndn::Name(expectedPrefix).append(std::to_string(Lsa::Type::COORDINATE)));
Vince Lehmanc11cc202015-01-20 11:41:33 -0600278}
279
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500280/* Tests that SyncLogicHandler's socket will be created when
281 Nlsr::initialize is called, preventing use of sync before the
282 socket is created.
283
Nick Gordon727d4832017-10-13 18:04:25 -0500284 NB: This test is as much an Nlsr class test as a
285 SyncLogicHandler class test, but it rides the line and ends up here.
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500286 */
Vince Lehman9d097802015-03-16 17:55:59 -0500287BOOST_AUTO_TEST_CASE(CreateSyncSocketOnInitialization) // Bug #2649
288{
289 nlsr.initialize();
290
291 // Make sure an adjacency LSA has not been built yet
Nick Gordon727d4832017-10-13 18:04:25 -0500292 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehman9d097802015-03-16 17:55:59 -0500293 AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key);
294 BOOST_REQUIRE(lsa == nullptr);
295
296 // Publish a routing update before an Adjacency LSA is built
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500297 BOOST_CHECK_NO_THROW(nlsr.getLsdb().getSyncLogicHandler()
Nick Gordon727d4832017-10-13 18:04:25 -0500298 .publishRoutingUpdate(Lsa::Type::ADJACENCY, 0));
Vince Lehman9d097802015-03-16 17:55:59 -0500299}
300
Vince Lehman904c2412014-09-23 19:36:11 -0500301BOOST_AUTO_TEST_SUITE_END()
302
303} // namespace test
304} // namespace nlsr