blob: 9a0c7e3d43e78be9e01e9a89d57062413618f300 [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
Davide Pesavento288141a2024-02-13 17:30:35 -050028namespace nlsr::tests {
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) {
Davide Pesaventob6adfe12024-07-05 13:03:15 -040042 m_sync = std::make_unique<SyncLogicHandler>(face, m_keyChain, testIsLsaNew, opts);
Junxiao Shif4674672024-01-06 02:27:36 +000043 }
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 {
Davide Pesavento288141a2024-02-13 17:30:35 -050050 this->advanceClocks(1_ms, 10);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060051 face.sentInterests.clear();
52
Davide Pesaventob6adfe12024-07-05 13:03:15 -040053#ifdef HAVE_PSYNC
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070054 std::vector<psync::MissingDataInfo> updates;
Junxiao Shif4674672024-01-06 02:27:36 +000055 updates.push_back({prefix, 0, seqNo, 0});
56 getSync().m_syncLogic.onPSyncUpdate(updates);
Davide Pesaventob6adfe12024-07-05 13:03:15 -040057#endif
Ashlesh Gawande85998a12017-12-07 22:22:13 -060058
Davide Pesavento288141a2024-02-13 17:30:35 -050059 this->advanceClocks(1_ms, 10);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060060 }
61
62public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000063 ndn::DummyClientFace face{m_io, m_keyChain};
Junxiao Shif4674672024-01-06 02:27:36 +000064 SyncLogicHandler::IsLsaNew testIsLsaNew = [] (auto&&...) { return true; };
65 SyncLogicOptions opts{
66 SyncProtocol::PSYNC,
67 ndn::Name("/ndn/nlsr/sync").appendVersion(ConfParameter::SYNC_VERSION),
68 "/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router",
Davide Pesavento288141a2024-02-13 17:30:35 -050069 time::milliseconds(SYNC_INTEREST_LIFETIME_DEFAULT),
Junxiao Shif4674672024-01-06 02:27:36 +000070 "/ndn/site/%C1.Router/this-router",
71 HYPERBOLIC_STATE_OFF
72 };
Ashlesh Gawande85998a12017-12-07 22:22:13 -060073
Junxiao Shif4674672024-01-06 02:27:36 +000074 ndn::Name otherRouter = "/localhop/ndn/nlsr/LSA/site/%C1.Router/other-router";
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040075 const std::vector<Lsa::Type> lsaTypes{Lsa::Type::NAME,
76 Lsa::Type::ADJACENCY,
77 Lsa::Type::COORDINATE};
Junxiao Shif4674672024-01-06 02:27:36 +000078
79private:
80 std::unique_ptr<SyncLogicHandler> m_sync;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060081};
82
Davide Pesaventod1f1df82022-03-12 16:40:37 -050083BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060084
85/* Tests that when SyncLogicHandler receives an LSA of either Name or
86 Adjacency type that appears to be newer, it will emit to its signal
87 with those LSA details.
88 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -050089BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060090{
Davide Pesaventod1f1df82022-03-12 16:40:37 -050091 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060092 uint64_t syncSeqNo = 1;
93
Davide Pesaventod1f1df82022-03-12 16:40:37 -050094 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::ADJACENCY}) {
Junxiao Shif4674672024-01-06 02:27:36 +000095 auto updateName = makeLsaUserPrefix(otherRouter, lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060096
Junxiao Shif4674672024-01-06 02:27:36 +000097 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -040098 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Junxiao Shif4674672024-01-06 02:27:36 +000099 BOOST_CHECK_EQUAL(updateName, routerName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600100 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500101 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600102 });
103
104 this->receiveUpdate(updateName, syncSeqNo);
105 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500106
107 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600108}
109
110/* Tests that when SyncLogicHandler in HR mode receives an LSA of
111 either Coordinate or Name type that appears to be newer, it will
112 emit to its signal with those LSA details.
113 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500114BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600115{
Junxiao Shif4674672024-01-06 02:27:36 +0000116 opts.hyperbolicState = HYPERBOLIC_STATE_ON;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600117
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500118 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600119 uint64_t syncSeqNo = 1;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600120
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500121 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::COORDINATE}) {
Junxiao Shif4674672024-01-06 02:27:36 +0000122 auto updateName = makeLsaUserPrefix(otherRouter, lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600123
Junxiao Shif4674672024-01-06 02:27:36 +0000124 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400125 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Junxiao Shif4674672024-01-06 02:27:36 +0000126 BOOST_CHECK_EQUAL(updateName, routerName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600127 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500128 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600129 });
130
131 this->receiveUpdate(updateName, syncSeqNo);
132 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500133
134 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600135}
136
137/* Tests that when SyncLogicHandler in HR-dry mode receives an LSA of
138 any type that appears to be newer, it will emit to its signal with
139 those LSA details.
140 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500141BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600142{
Junxiao Shif4674672024-01-06 02:27:36 +0000143 opts.hyperbolicState = HYPERBOLIC_STATE_DRY_RUN;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600144
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500145 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600146 uint64_t syncSeqNo = 1;
147
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500148 for (auto lsaType : this->lsaTypes) {
Junxiao Shif4674672024-01-06 02:27:36 +0000149 auto updateName = makeLsaUserPrefix(otherRouter, lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600150
Junxiao Shif4674672024-01-06 02:27:36 +0000151 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400152 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Junxiao Shif4674672024-01-06 02:27:36 +0000153 BOOST_CHECK_EQUAL(updateName, routerName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600154 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500155 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600156 });
157
158 this->receiveUpdate(updateName, syncSeqNo);
159 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500160
161 BOOST_CHECK_EQUAL(nCallbacks, 3);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600162}
163
164/* Tests that when SyncLogicHandler receives an update for an LSA with
165 details matching this router's details, it will *not* emit to its
166 signal those LSA details.
167 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500168BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600169{
170 const uint64_t sequenceNumber = 1;
171
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500172 for (auto lsaType : this->lsaTypes) {
Junxiao Shif4674672024-01-06 02:27:36 +0000173 auto updateName = makeLsaUserPrefix(opts.routerPrefix, lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600174
Junxiao Shif4674672024-01-06 02:27:36 +0000175 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400176 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600177 BOOST_FAIL("Updates for self should not be emitted!");
178 });
179
Junxiao Shif4674672024-01-06 02:27:36 +0000180 this->receiveUpdate(updateName, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600181 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500182
183 // avoid "test case [...] did not check any assertions" message from Boost.Test
184 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600185}
186
187/* Tests that when SyncLogicHandler receives an update for an LSA with
188 details that do not match the expected format, it will *not* emit
189 to its signal those LSA details.
190 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500191BOOST_AUTO_TEST_CASE(MalformedUpdate)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600192{
193 const uint64_t sequenceNumber = 1;
194
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500195 for (auto lsaType : this->lsaTypes) {
Junxiao Shif4674672024-01-06 02:27:36 +0000196 auto updateName = makeLsaUserPrefix("/site/%C1.Router/this-router", lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600197
Junxiao Shif4674672024-01-06 02:27:36 +0000198 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400199 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600200 BOOST_FAIL("Malformed updates should not be emitted!");
201 });
202
Junxiao Shif4674672024-01-06 02:27:36 +0000203 this->receiveUpdate(updateName, sequenceNumber);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600204 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500205
206 // avoid "test case [...] did not check any assertions" message from Boost.Test
207 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600208}
209
210/* Tests that when SyncLogicHandler receives an update for an LSA with
211 details that do not appear to be new, it will *not* emit to its
212 signal those LSA details.
213 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500214BOOST_AUTO_TEST_CASE(LsaNotNew)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600215{
Junxiao Shif4674672024-01-06 02:27:36 +0000216 testIsLsaNew = [] (const ndn::Name& routerName, const Lsa::Type& lsaType,
217 const uint64_t& sequenceNumber, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600218 return false;
219 };
220
221 const uint64_t sequenceNumber = 1;
Junxiao Shif4674672024-01-06 02:27:36 +0000222 ndn::signal::ScopedConnection connection = getSync().onNewLsa.connect(
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400223 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
224 BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!");
225 });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600226
Junxiao Shif4674672024-01-06 02:27:36 +0000227 auto updateName = makeLsaUserPrefix(otherRouter, Lsa::Type::NAME);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600228 this->receiveUpdate(updateName, sequenceNumber);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500229
230 // avoid "test case [...] did not check any assertions" message from Boost.Test
231 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600232}
233
234/* Tests that SyncLogicHandler successfully concatenates configured
235 variables together to form the necessary prefixes to advertise
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700236 through sync.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600237 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500238BOOST_AUTO_TEST_CASE(UpdatePrefix)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600239{
Junxiao Shif4674672024-01-06 02:27:36 +0000240 BOOST_CHECK_EQUAL(getSync().m_nameLsaUserPrefix,
241 ndn::Name(opts.userPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)));
242 BOOST_CHECK_EQUAL(getSync().m_adjLsaUserPrefix,
243 ndn::Name(opts.userPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY)));
244 BOOST_CHECK_EQUAL(getSync().m_coorLsaUserPrefix,
245 ndn::Name(opts.userPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600246}
247
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600248BOOST_AUTO_TEST_SUITE_END()
249
Davide Pesavento288141a2024-02-13 17:30:35 -0500250} // namespace nlsr::tests