blob: b233b2287f1ffb3e2b8e9c6b8353d86d79cb70f7 [file] [log] [blame]
Vince Lehman904c2412014-09-23 19:36:11 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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"
Vince Lehman904c2412014-09-23 19:36:11 -050027
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060028#include <ndn-cxx/util/dummy-client-face.hpp>
29
Vince Lehman904c2412014-09-23 19:36:11 -050030namespace nlsr {
31namespace test {
32
dmcoomes9f936662017-03-02 10:33:09 -060033using std::shared_ptr;
Vince Lehman904c2412014-09-23 19:36:11 -050034
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050035class SyncLogicFixture : public UnitTestTimeFixture
Vince Lehman904c2412014-09-23 19:36:11 -050036{
37public:
38 SyncLogicFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050039 : face(m_ioService, m_keyChain)
40 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Nick Gordon727d4832017-10-13 18:04:25 -050041 , testIsLsaNew([] (const ndn::Name& name, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -050042 const uint64_t sequenceNumber) {
43 return true;
44 })
Vince Lehman904c2412014-09-23 19:36:11 -050045 , CONFIG_NETWORK("/ndn")
46 , CONFIG_SITE("/site")
47 , CONFIG_ROUTER_NAME("/%C1.Router/this-router")
Nick Gordon8f23b5d2017-08-31 17:53:07 -050048 , OTHER_ROUTER_NAME("/%C1.Router/other-router/")
Vince Lehman904c2412014-09-23 19:36:11 -050049 {
50 nlsr.getConfParameter().setNetwork(CONFIG_NETWORK);
51 nlsr.getConfParameter().setSiteName(CONFIG_SITE);
52 nlsr.getConfParameter().setRouterName(CONFIG_ROUTER_NAME);
53 nlsr.getConfParameter().buildRouterPrefix();
Nick Gordon8f23b5d2017-08-31 17:53:07 -050054
55 conf.setNetwork(CONFIG_NETWORK);
56 conf.setSiteName(CONFIG_SITE);
57 conf.setRouterName(CONFIG_ROUTER_NAME);
58 conf.buildRouterPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050059
60 addIdentity(conf.getRouterPrefix());
Vince Lehman904c2412014-09-23 19:36:11 -050061 }
62
63 void
Nick Gordon5c467f02016-07-13 13:40:10 -050064 receiveUpdate(std::string prefix, uint64_t seqNo, SyncLogicHandler& p_sync)
Vince Lehman904c2412014-09-23 19:36:11 -050065 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060066 chronosync::MissingDataInfo info = {ndn::Name(prefix).appendNumber(1), 0, seqNo};
Vince Lehman904c2412014-09-23 19:36:11 -050067
Ashlesh Gawande415676b2016-12-22 00:26:23 -060068 std::vector<chronosync::MissingDataInfo> updates;
Vince Lehman904c2412014-09-23 19:36:11 -050069 updates.push_back(info);
70
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050071 this->advanceClocks(ndn::time::milliseconds(1), 10);
72 face.sentInterests.clear();
Vince Lehman904c2412014-09-23 19:36:11 -050073
Ashlesh Gawande7600c902017-06-21 13:28:35 -050074 p_sync.onChronoSyncUpdate(updates);
Vince Lehman904c2412014-09-23 19:36:11 -050075
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050076 this->advanceClocks(ndn::time::milliseconds(1), 10);
Vince Lehman904c2412014-09-23 19:36:11 -050077 }
78
79public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050080 ndn::util::DummyClientFace face;
Vince Lehman904c2412014-09-23 19:36:11 -050081 Nlsr nlsr;
Nick Gordon8f23b5d2017-08-31 17:53:07 -050082 ConfParameter conf;
Nick Gordon727d4832017-10-13 18:04:25 -050083 SyncLogicHandler::IsLsaNew testIsLsaNew;
Vince Lehman904c2412014-09-23 19:36:11 -050084
85 const std::string CONFIG_NETWORK;
86 const std::string CONFIG_SITE;
87 const std::string CONFIG_ROUTER_NAME;
Nick Gordon8f23b5d2017-08-31 17:53:07 -050088 const std::string OTHER_ROUTER_NAME;
Nick Gordon727d4832017-10-13 18:04:25 -050089 const std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME, Lsa::Type::ADJACENCY,
90 Lsa::Type::COORDINATE};
Vince Lehman904c2412014-09-23 19:36:11 -050091};
92
93BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
94
Nick Gordon8f23b5d2017-08-31 17:53:07 -050095/* Tests that when SyncLogicHandler receives an LSA of either Name or
96 Adjacency type that appears to be newer, it will emit to its signal
97 with those LSA details.
98 */
Nick Gordon5c467f02016-07-13 13:40:10 -050099BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
Vince Lehman904c2412014-09-23 19:36:11 -0500100{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500101 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500102 sync.createSyncSocket(conf.getChronosyncPrefix());
103
Nick Gordon727d4832017-10-13 18:04:25 -0500104 std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME, Lsa::Type::ADJACENCY};
Vince Lehman904c2412014-09-23 19:36:11 -0500105
Nick Gordon5c467f02016-07-13 13:40:10 -0500106 uint64_t syncSeqNo = 1;
Vince Lehman904c2412014-09-23 19:36:11 -0500107
Nick Gordon727d4832017-10-13 18:04:25 -0500108 for (const Lsa::Type& lsaType : lsaTypes) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500109 std::string updateName = conf.getLsaPrefix().toUri() + CONFIG_SITE
Nick Gordon727d4832017-10-13 18:04:25 -0500110 + OTHER_ROUTER_NAME + std::to_string(lsaType);
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500111
112 // Actual testing done here -- signal function callback
113 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
114 [&, this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
115 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
116 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
117 });
Nick Gordone8e03ac2016-07-07 14:24:38 -0500118
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500119 receiveUpdate(updateName, syncSeqNo, sync);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500120 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500121}
122
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500123/* Tests that when SyncLogicHandler in HR mode receives an LSA of
124 either Coordinate or Name type that appears to be newer, it will
125 emit to its signal with those LSA details.
126 */
Nick Gordon5c467f02016-07-13 13:40:10 -0500127BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
128{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500129 conf.setHyperbolicState(HYPERBOLIC_STATE_ON);
Nick Gordon5c467f02016-07-13 13:40:10 -0500130
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500131 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500132 sync.createSyncSocket(conf.getChronosyncPrefix());
Nick Gordon5c467f02016-07-13 13:40:10 -0500133
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500134 uint64_t syncSeqNo = 1;
Nick Gordon727d4832017-10-13 18:04:25 -0500135 std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME, Lsa::Type::COORDINATE};
Nick Gordon5c467f02016-07-13 13:40:10 -0500136
Nick Gordon727d4832017-10-13 18:04:25 -0500137 for (const Lsa::Type& lsaType : lsaTypes) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500138 std::string updateName = conf.getLsaPrefix().toUri() + CONFIG_SITE
Nick Gordon727d4832017-10-13 18:04:25 -0500139 + OTHER_ROUTER_NAME + std::to_string(lsaType);
Nick Gordon5c467f02016-07-13 13:40:10 -0500140
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500141 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
142 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
143 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
144 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
145 });
Nick Gordon5c467f02016-07-13 13:40:10 -0500146
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500147 receiveUpdate(updateName, syncSeqNo, sync);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500148 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500149}
150
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500151/* Tests that when SyncLogicHandler in HR-dry mode receives an LSA of
152 any type that appears to be newer, it will emit to its signal with
153 those LSA details.
154 */
Nick Gordon5c467f02016-07-13 13:40:10 -0500155BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
156{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500157 conf.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
Nick Gordon5c467f02016-07-13 13:40:10 -0500158
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500159 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500160 sync.createSyncSocket(conf.getChronosyncPrefix());
Nick Gordon5c467f02016-07-13 13:40:10 -0500161
Nick Gordon727d4832017-10-13 18:04:25 -0500162 for (const Lsa::Type& lsaType : lsaTypes) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500163 uint64_t syncSeqNo = 1;
Nick Gordon5c467f02016-07-13 13:40:10 -0500164
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500165 std::string updateName = conf.getLsaPrefix().toUri() + CONFIG_SITE
Nick Gordon727d4832017-10-13 18:04:25 -0500166 + OTHER_ROUTER_NAME + std::to_string(lsaType);
Nick Gordon5c467f02016-07-13 13:40:10 -0500167
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500168 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
169 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
170 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
171 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
172 });
173
174 receiveUpdate(updateName, syncSeqNo, sync);
Nick Gordone8e03ac2016-07-07 14:24:38 -0500175 }
Vince Lehman904c2412014-09-23 19:36:11 -0500176}
177
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500178/* Tests that when SyncLogicHandler receives an update for an LSA with
179 details matching this router's details, it will *not* emit to its
180 signal those LSA details.
181 */
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500182BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
183{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500184 const uint64_t sequenceNumber = 1;
185
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500186 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500187 sync.createSyncSocket(conf.getChronosyncPrefix());
188
Nick Gordon727d4832017-10-13 18:04:25 -0500189 for (const Lsa::Type& lsaType : lsaTypes) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500190 // To ensure that we get correctly-separated components, create
191 // and modify a Name to hand off.
192 ndn::Name updateName = ndn::Name{conf.getLsaPrefix()};
Nick Gordon727d4832017-10-13 18:04:25 -0500193 updateName.append(CONFIG_SITE).append(CONFIG_ROUTER_NAME).append(std::to_string(lsaType));
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500194
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500195 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
196 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
197 BOOST_FAIL("Updates for self should not be emitted!");
198 });
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500199
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500200 receiveUpdate(updateName.toUri(), sequenceNumber, sync);
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500201 }
202}
203
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500204/* Tests that when SyncLogicHandler receives an update for an LSA with
205 details that do not match the expected format, it will *not* emit
206 to its signal those LSA details.
207 */
Vince Lehman904c2412014-09-23 19:36:11 -0500208BOOST_AUTO_TEST_CASE(MalformedUpdate)
209{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500210 const uint64_t sequenceNumber = 1;
Vince Lehman904c2412014-09-23 19:36:11 -0500211
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500212 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500213 sync.createSyncSocket(conf.getChronosyncPrefix());
214
Nick Gordon727d4832017-10-13 18:04:25 -0500215 for (const Lsa::Type& lsaType : lsaTypes) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500216 ndn::Name updateName{CONFIG_SITE};
Nick Gordon727d4832017-10-13 18:04:25 -0500217 updateName.append(CONFIG_ROUTER_NAME).append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500218
219 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
220 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
221 BOOST_FAIL("Malformed updates should not be emitted!");
222 });
223
224 receiveUpdate(updateName.toUri(), sequenceNumber, sync);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500225 }
Vince Lehman904c2412014-09-23 19:36:11 -0500226}
227
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500228/* Tests that when SyncLogicHandler receives an update for an LSA with
229 details that do not appear to be new, it will *not* emit to its
230 signal those LSA details.
231 */
232BOOST_AUTO_TEST_CASE(LsaNotNew)
Vince Lehman904c2412014-09-23 19:36:11 -0500233{
Nick Gordon727d4832017-10-13 18:04:25 -0500234 auto testLsaAlwaysFalse = [] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500235 const uint64_t& sequenceNumber) {
236 return false;
237 };
Vince Lehman904c2412014-09-23 19:36:11 -0500238
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500239 const uint64_t sequenceNumber = 1;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500240 SyncLogicHandler sync{face, testLsaAlwaysFalse, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500241 sync.createSyncSocket(conf.getChronosyncPrefix());
242 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
243 [& ,this] (const ndn::Name& routerName, const uint64_t& sequenceNumber) {
244 BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!");
245 });
Vince Lehman904c2412014-09-23 19:36:11 -0500246
247 std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
Nick Gordon727d4832017-10-13 18:04:25 -0500248 CONFIG_SITE + "/%C1.Router/other-router/" +
249 std::to_string(Lsa::Type::NAME);
Vince Lehman904c2412014-09-23 19:36:11 -0500250
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500251 receiveUpdate(updateName, sequenceNumber, sync);
Vince Lehman904c2412014-09-23 19:36:11 -0500252}
253
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500254/* Tests that SyncLogicHandler successfully concatenates configured
255 variables together to form the necessary prefixes to advertise
256 through ChronoSync.
257 */
Vince Lehmanc11cc202015-01-20 11:41:33 -0600258BOOST_AUTO_TEST_CASE(UpdatePrefix)
259{
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500260
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500261 SyncLogicHandler sync{face, testIsLsaNew, conf};
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500262
Vince Lehmanc11cc202015-01-20 11:41:33 -0600263 ndn::Name expectedPrefix = nlsr.getConfParameter().getLsaPrefix();
264 expectedPrefix.append(CONFIG_SITE);
265 expectedPrefix.append(CONFIG_ROUTER_NAME);
266
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500267 sync.buildUpdatePrefix();
Vince Lehmanc11cc202015-01-20 11:41:33 -0600268
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500269 BOOST_CHECK_EQUAL(sync.m_nameLsaUserPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500270 ndn::Name(expectedPrefix).append(std::to_string(Lsa::Type::NAME)));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500271 BOOST_CHECK_EQUAL(sync.m_adjLsaUserPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500272 ndn::Name(expectedPrefix).append(std::to_string(Lsa::Type::ADJACENCY)));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500273 BOOST_CHECK_EQUAL(sync.m_coorLsaUserPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500274 ndn::Name(expectedPrefix).append(std::to_string(Lsa::Type::COORDINATE)));
Vince Lehmanc11cc202015-01-20 11:41:33 -0600275}
276
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500277/* Tests that SyncLogicHandler's socket will be created when
278 Nlsr::initialize is called, preventing use of sync before the
279 socket is created.
280
Nick Gordon727d4832017-10-13 18:04:25 -0500281 NB: This test is as much an Nlsr class test as a
282 SyncLogicHandler class test, but it rides the line and ends up here.
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500283 */
Vince Lehman9d097802015-03-16 17:55:59 -0500284BOOST_AUTO_TEST_CASE(CreateSyncSocketOnInitialization) // Bug #2649
285{
286 nlsr.initialize();
287
288 // Make sure an adjacency LSA has not been built yet
Nick Gordon727d4832017-10-13 18:04:25 -0500289 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehman9d097802015-03-16 17:55:59 -0500290 AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key);
291 BOOST_REQUIRE(lsa == nullptr);
292
293 // Publish a routing update before an Adjacency LSA is built
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500294 BOOST_CHECK_NO_THROW(nlsr.getLsdb().getSyncLogicHandler()
Nick Gordon727d4832017-10-13 18:04:25 -0500295 .publishRoutingUpdate(Lsa::Type::ADJACENCY, 0));
Vince Lehman9d097802015-03-16 17:55:59 -0500296}
297
Vince Lehman904c2412014-09-23 19:36:11 -0500298BOOST_AUTO_TEST_SUITE_END()
299
300} // namespace test
301} // namespace nlsr