blob: 4f759e0c686ca7289bf84c3a7ca6e286deaa61dc [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/*
Junxiao Shif4674672024-01-06 02:27:36 +00003 * Copyright (c) 2014-2024, 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"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060023#include "nlsr.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060024
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040025#include "tests/io-key-chain-fixture.hpp"
26#include "tests/test-common.hpp"
27
Junxiao Shif4674672024-01-06 02:27:36 +000028namespace nlsr::test {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060029
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040030class SyncLogicFixture : public IoKeyChainFixture
Ashlesh Gawande85998a12017-12-07 22:22:13 -060031{
32public:
33 SyncLogicFixture()
Ashlesh Gawande85998a12017-12-07 22:22:13 -060034 {
Junxiao Shif4674672024-01-06 02:27:36 +000035 m_keyChain.createIdentity(opts.routerPrefix);
36 }
37
38 SyncLogicHandler&
39 getSync()
40 {
41 if (m_sync == nullptr) {
42 m_sync.reset(new SyncLogicHandler(face, m_keyChain, testIsLsaNew, opts));
43 }
44 return *m_sync;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060045 }
46
47 void
Junxiao Shif4674672024-01-06 02:27:36 +000048 receiveUpdate(const ndn::Name& prefix, uint64_t seqNo)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060049 {
50 this->advanceClocks(ndn::time::milliseconds(1), 10);
51 face.sentInterests.clear();
52
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070053 std::vector<psync::MissingDataInfo> updates;
Junxiao Shif4674672024-01-06 02:27:36 +000054 updates.push_back({prefix, 0, seqNo, 0});
55 getSync().m_syncLogic.onPSyncUpdate(updates);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060056
57 this->advanceClocks(ndn::time::milliseconds(1), 10);
58 }
59
60public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000061 ndn::DummyClientFace face{m_io, m_keyChain};
Junxiao Shif4674672024-01-06 02:27:36 +000062 SyncLogicHandler::IsLsaNew testIsLsaNew = [] (auto&&...) { return true; };
63 SyncLogicOptions opts{
64 SyncProtocol::PSYNC,
65 ndn::Name("/ndn/nlsr/sync").appendVersion(ConfParameter::SYNC_VERSION),
66 "/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router",
67 ndn::time::milliseconds(SYNC_INTEREST_LIFETIME_DEFAULT),
68 "/ndn/site/%C1.Router/this-router",
69 HYPERBOLIC_STATE_OFF
70 };
Ashlesh Gawande85998a12017-12-07 22:22:13 -060071
Junxiao Shif4674672024-01-06 02:27:36 +000072 ndn::Name otherRouter = "/localhop/ndn/nlsr/LSA/site/%C1.Router/other-router";
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040073 const std::vector<Lsa::Type> lsaTypes{Lsa::Type::NAME,
74 Lsa::Type::ADJACENCY,
75 Lsa::Type::COORDINATE};
Junxiao Shif4674672024-01-06 02:27:36 +000076
77private:
78 std::unique_ptr<SyncLogicHandler> m_sync;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060079};
80
Davide Pesaventod1f1df82022-03-12 16:40:37 -050081BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060082
83/* Tests that when SyncLogicHandler receives an LSA of either Name or
84 Adjacency type that appears to be newer, it will emit to its signal
85 with those LSA details.
86 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -050087BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060088{
Davide Pesaventod1f1df82022-03-12 16:40:37 -050089 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060090 uint64_t syncSeqNo = 1;
91
Davide Pesaventod1f1df82022-03-12 16:40:37 -050092 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::ADJACENCY}) {
Junxiao Shif4674672024-01-06 02:27:36 +000093 auto updateName = makeLsaUserPrefix(otherRouter, lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060094
Junxiao Shif4674672024-01-06 02:27:36 +000095 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -040096 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Junxiao Shif4674672024-01-06 02:27:36 +000097 BOOST_CHECK_EQUAL(updateName, routerName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060098 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -050099 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600100 });
101
102 this->receiveUpdate(updateName, syncSeqNo);
103 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500104
105 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600106}
107
108/* Tests that when SyncLogicHandler in HR mode receives an LSA of
109 either Coordinate or Name type that appears to be newer, it will
110 emit to its signal with those LSA details.
111 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500112BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600113{
Junxiao Shif4674672024-01-06 02:27:36 +0000114 opts.hyperbolicState = HYPERBOLIC_STATE_ON;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600115
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500116 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600117 uint64_t syncSeqNo = 1;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600118
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500119 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::COORDINATE}) {
Junxiao Shif4674672024-01-06 02:27:36 +0000120 auto updateName = makeLsaUserPrefix(otherRouter, lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600121
Junxiao Shif4674672024-01-06 02:27:36 +0000122 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400123 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Junxiao Shif4674672024-01-06 02:27:36 +0000124 BOOST_CHECK_EQUAL(updateName, routerName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600125 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500126 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600127 });
128
129 this->receiveUpdate(updateName, syncSeqNo);
130 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500131
132 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600133}
134
135/* Tests that when SyncLogicHandler in HR-dry mode receives an LSA of
136 any type that appears to be newer, it will emit to its signal with
137 those LSA details.
138 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500139BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600140{
Junxiao Shif4674672024-01-06 02:27:36 +0000141 opts.hyperbolicState = HYPERBOLIC_STATE_DRY_RUN;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600142
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500143 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600144 uint64_t syncSeqNo = 1;
145
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500146 for (auto lsaType : this->lsaTypes) {
Junxiao Shif4674672024-01-06 02:27:36 +0000147 auto updateName = makeLsaUserPrefix(otherRouter, lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600148
Junxiao Shif4674672024-01-06 02:27:36 +0000149 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400150 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Junxiao Shif4674672024-01-06 02:27:36 +0000151 BOOST_CHECK_EQUAL(updateName, routerName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600152 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500153 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600154 });
155
156 this->receiveUpdate(updateName, syncSeqNo);
157 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500158
159 BOOST_CHECK_EQUAL(nCallbacks, 3);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600160}
161
162/* Tests that when SyncLogicHandler receives an update for an LSA with
163 details matching this router's details, it will *not* emit to its
164 signal those LSA details.
165 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500166BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600167{
168 const uint64_t sequenceNumber = 1;
169
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500170 for (auto lsaType : this->lsaTypes) {
Junxiao Shif4674672024-01-06 02:27:36 +0000171 auto updateName = makeLsaUserPrefix(opts.routerPrefix, lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600172
Junxiao Shif4674672024-01-06 02:27:36 +0000173 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400174 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600175 BOOST_FAIL("Updates for self should not be emitted!");
176 });
177
Junxiao Shif4674672024-01-06 02:27:36 +0000178 this->receiveUpdate(updateName, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600179 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500180
181 // avoid "test case [...] did not check any assertions" message from Boost.Test
182 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600183}
184
185/* Tests that when SyncLogicHandler receives an update for an LSA with
186 details that do not match the expected format, it will *not* emit
187 to its signal those LSA details.
188 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500189BOOST_AUTO_TEST_CASE(MalformedUpdate)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600190{
191 const uint64_t sequenceNumber = 1;
192
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500193 for (auto lsaType : this->lsaTypes) {
Junxiao Shif4674672024-01-06 02:27:36 +0000194 auto updateName = makeLsaUserPrefix("/site/%C1.Router/this-router", lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600195
Junxiao Shif4674672024-01-06 02:27:36 +0000196 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400197 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600198 BOOST_FAIL("Malformed updates should not be emitted!");
199 });
200
Junxiao Shif4674672024-01-06 02:27:36 +0000201 this->receiveUpdate(updateName, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600202 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500203
204 // avoid "test case [...] did not check any assertions" message from Boost.Test
205 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600206}
207
208/* Tests that when SyncLogicHandler receives an update for an LSA with
209 details that do not appear to be new, it will *not* emit to its
210 signal those LSA details.
211 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500212BOOST_AUTO_TEST_CASE(LsaNotNew)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600213{
Junxiao Shif4674672024-01-06 02:27:36 +0000214 testIsLsaNew = [] (const ndn::Name& routerName, const Lsa::Type& lsaType,
215 const uint64_t& sequenceNumber, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600216 return false;
217 };
218
219 const uint64_t sequenceNumber = 1;
Junxiao Shif4674672024-01-06 02:27:36 +0000220 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400221 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
222 BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!");
223 });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600224
Junxiao Shif4674672024-01-06 02:27:36 +0000225 auto updateName = makeLsaUserPrefix(otherRouter, 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{
Junxiao Shif4674672024-01-06 02:27:36 +0000238 BOOST_CHECK_EQUAL(getSync().m_nameLsaUserPrefix,
239 ndn::Name(opts.userPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)));
240 BOOST_CHECK_EQUAL(getSync().m_adjLsaUserPrefix,
241 ndn::Name(opts.userPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY)));
242 BOOST_CHECK_EQUAL(getSync().m_coorLsaUserPrefix,
243 ndn::Name(opts.userPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600244}
245
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600246BOOST_AUTO_TEST_SUITE_END()
247
Junxiao Shif4674672024-01-06 02:27:36 +0000248} // namespace nlsr::test