blob: 3c71f747c956171b102f8b3364308d6cc3ad44a0 [file] [log] [blame]
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande30d96e42021-03-21 19:15:33 -07002/*
Davide Pesaventod1f1df82022-03-12 16:40:37 -05003 * Copyright (c) 2014-2022, The University of Memphis,
Ashlesh Gawande85998a12017-12-07 22:22:13 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
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/>.
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070020 */
Ashlesh Gawande85998a12017-12-07 22:22:13 -060021
22#include "communication/sync-logic-handler.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060023#include "tests/test-common.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060024#include "common.hpp"
25#include "nlsr.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060026
27#include <ndn-cxx/util/dummy-client-face.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070028#include <boost/lexical_cast.hpp>
Ashlesh Gawande85998a12017-12-07 22:22:13 -060029
30namespace nlsr {
31namespace test {
32
33using std::shared_ptr;
34
Ashlesh Gawande85998a12017-12-07 22:22:13 -060035class SyncLogicFixture : public UnitTestTimeFixture
36{
37public:
38 SyncLogicFixture()
Davide Pesaventod1f1df82022-03-12 16:40:37 -050039 : testIsLsaNew([] (auto&&...) { return true; })
Ashlesh Gawande85998a12017-12-07 22:22:13 -060040 , sync(face, testIsLsaNew, conf)
41 , updateNamePrefix(this->conf.getLsaPrefix().toUri() +
42 this->conf.getSiteName().toUri() +
43 "/%C1.Router/other-router/")
44 {
45 addIdentity(conf.getRouterPrefix());
46 }
47
48 void
49 receiveUpdate(const std::string& prefix, uint64_t seqNo)
50 {
51 this->advanceClocks(ndn::time::milliseconds(1), 10);
52 face.sentInterests.clear();
53
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070054 std::vector<psync::MissingDataInfo> updates;
55 updates.push_back({ndn::Name(prefix), 0, seqNo});
56 sync.m_syncLogic.onPSyncUpdate(updates);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060057
58 this->advanceClocks(ndn::time::milliseconds(1), 10);
59 }
60
61public:
Davide Pesaventod1f1df82022-03-12 16:40:37 -050062 ndn::util::DummyClientFace face{m_ioService, m_keyChain};
63 ConfParameter conf{face, m_keyChain};
64 DummyConfFileProcessor confProcessor{conf, SYNC_PROTOCOL_PSYNC};
Ashlesh Gawande85998a12017-12-07 22:22:13 -060065 SyncLogicHandler::IsLsaNew testIsLsaNew;
66 SyncLogicHandler sync;
67
68 const std::string updateNamePrefix;
Davide Pesaventod1f1df82022-03-12 16:40:37 -050069 const std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME,
70 Lsa::Type::ADJACENCY,
71 Lsa::Type::COORDINATE};
Ashlesh Gawande85998a12017-12-07 22:22:13 -060072};
73
Davide Pesaventod1f1df82022-03-12 16:40:37 -050074BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060075
76/* Tests that when SyncLogicHandler receives an LSA of either Name or
77 Adjacency type that appears to be newer, it will emit to its signal
78 with those LSA details.
79 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -050080BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060081{
Davide Pesaventod1f1df82022-03-12 16:40:37 -050082 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060083 uint64_t syncSeqNo = 1;
84
Davide Pesaventod1f1df82022-03-12 16:40:37 -050085 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::ADJACENCY}) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080086 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060087
Ashlesh Gawande85998a12017-12-07 22:22:13 -060088 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Davide Pesaventod1f1df82022-03-12 16:40:37 -050089 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060090 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
91 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -050092 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060093 });
94
95 this->receiveUpdate(updateName, syncSeqNo);
96 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -050097
98 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060099}
100
101/* Tests that when SyncLogicHandler in HR mode receives an LSA of
102 either Coordinate or Name type that appears to be newer, it will
103 emit to its signal with those LSA details.
104 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500105BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600106{
107 this->conf.setHyperbolicState(HYPERBOLIC_STATE_ON);
108
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500109 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600110 uint64_t syncSeqNo = 1;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600111
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500112 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::COORDINATE}) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800113 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600114
115 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500116 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600117 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
118 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500119 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600120 });
121
122 this->receiveUpdate(updateName, syncSeqNo);
123 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500124
125 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600126}
127
128/* Tests that when SyncLogicHandler in HR-dry mode receives an LSA of
129 any type that appears to be newer, it will emit to its signal with
130 those LSA details.
131 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500132BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600133{
134 this->conf.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
135
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500136 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600137 uint64_t syncSeqNo = 1;
138
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500139 for (auto lsaType : this->lsaTypes) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800140 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600141
142 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500143 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600144 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
145 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500146 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600147 });
148
149 this->receiveUpdate(updateName, syncSeqNo);
150 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500151
152 BOOST_CHECK_EQUAL(nCallbacks, 3);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600153}
154
155/* Tests that when SyncLogicHandler receives an update for an LSA with
156 details matching this router's details, it will *not* emit to its
157 signal those LSA details.
158 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500159BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600160{
161 const uint64_t sequenceNumber = 1;
162
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500163 for (auto lsaType : this->lsaTypes) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600164 // To ensure that we get correctly-separated components, create
165 // and modify a Name to hand off.
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500166 ndn::Name updateName{this->conf.getLsaPrefix()};
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600167 updateName.append(this->conf.getSiteName())
168 .append(this->conf.getRouterName())
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800169 .append(boost::lexical_cast<std::string>(lsaType));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600170
171 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500172 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600173 BOOST_FAIL("Updates for self should not be emitted!");
174 });
175
176 this->receiveUpdate(updateName.toUri(), sequenceNumber);
177 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500178
179 // avoid "test case [...] did not check any assertions" message from Boost.Test
180 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600181}
182
183/* Tests that when SyncLogicHandler receives an update for an LSA with
184 details that do not match the expected format, it will *not* emit
185 to its signal those LSA details.
186 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500187BOOST_AUTO_TEST_CASE(MalformedUpdate)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600188{
189 const uint64_t sequenceNumber = 1;
190
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500191 for (auto lsaType : this->lsaTypes) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600192 ndn::Name updateName{this->conf.getSiteName()};
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800193 updateName.append(this->conf.getRouterName()).append(boost::lexical_cast<std::string>(lsaType));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600194
195 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500196 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600197 BOOST_FAIL("Malformed updates should not be emitted!");
198 });
199
200 this->receiveUpdate(updateName.toUri(), sequenceNumber);
201 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500202
203 // avoid "test case [...] did not check any assertions" message from Boost.Test
204 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600205}
206
207/* Tests that when SyncLogicHandler receives an update for an LSA with
208 details that do not appear to be new, it will *not* emit to its
209 signal those LSA details.
210 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500211BOOST_AUTO_TEST_CASE(LsaNotNew)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600212{
213 auto testLsaAlwaysFalse = [] (const ndn::Name& routerName, const Lsa::Type& lsaType,
214 const uint64_t& sequenceNumber) {
215 return false;
216 };
217
218 const uint64_t sequenceNumber = 1;
219 SyncLogicHandler sync{this->face, testLsaAlwaysFalse, this->conf};
220 ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect(
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500221 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600222 BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!");
223 });
224
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800225 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(Lsa::Type::NAME);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600226 this->receiveUpdate(updateName, sequenceNumber);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500227
228 // avoid "test case [...] did not check any assertions" message from Boost.Test
229 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600230}
231
232/* Tests that SyncLogicHandler successfully concatenates configured
233 variables together to form the necessary prefixes to advertise
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700234 through sync.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600235 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500236BOOST_AUTO_TEST_CASE(UpdatePrefix)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600237{
238 ndn::Name expectedPrefix = this->conf.getLsaPrefix();
239 expectedPrefix.append(this->conf.getSiteName());
240 expectedPrefix.append(this->conf.getRouterName());
241
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600242 BOOST_CHECK_EQUAL(this->sync.m_nameLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800243 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600244 BOOST_CHECK_EQUAL(this->sync.m_adjLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800245 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600246 BOOST_CHECK_EQUAL(this->sync.m_coorLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800247 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600248}
249
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600250BOOST_AUTO_TEST_SUITE_END()
251
252} // namespace test
253} // namespace nlsr