blob: c259bfc447df2500a055942c6ec1d8bf93590525 [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 Shi43f37a02023-08-09 00:09:00 +00003 * Copyright (c) 2014-2023, 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
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
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040033class SyncLogicFixture : public IoKeyChainFixture
Ashlesh Gawande85998a12017-12-07 22:22:13 -060034{
35public:
36 SyncLogicFixture()
Davide Pesaventod1f1df82022-03-12 16:40:37 -050037 : testIsLsaNew([] (auto&&...) { return true; })
Ashlesh Gawande85998a12017-12-07 22:22:13 -060038 , updateNamePrefix(this->conf.getLsaPrefix().toUri() +
39 this->conf.getSiteName().toUri() +
40 "/%C1.Router/other-router/")
41 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040042 m_keyChain.createIdentity(conf.getRouterPrefix());
Ashlesh Gawande85998a12017-12-07 22:22:13 -060043 }
44
45 void
46 receiveUpdate(const std::string& prefix, uint64_t seqNo)
47 {
48 this->advanceClocks(ndn::time::milliseconds(1), 10);
49 face.sentInterests.clear();
50
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070051 std::vector<psync::MissingDataInfo> updates;
Alexander Afanasyev135288c2022-04-23 23:06:56 -040052 updates.push_back({ndn::Name(prefix), 0, seqNo, 0});
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070053 sync.m_syncLogic.onPSyncUpdate(updates);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060054
55 this->advanceClocks(ndn::time::milliseconds(1), 10);
56 }
57
58public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000059 ndn::DummyClientFace face{m_io, m_keyChain};
Davide Pesaventod1f1df82022-03-12 16:40:37 -050060 ConfParameter conf{face, m_keyChain};
Davide Pesavento1954a0c2022-09-30 15:56:04 -040061 DummyConfFileProcessor confProcessor{conf, SyncProtocol::PSYNC};
Ashlesh Gawande85998a12017-12-07 22:22:13 -060062 SyncLogicHandler::IsLsaNew testIsLsaNew;
Davide Pesavento1954a0c2022-09-30 15:56:04 -040063 SyncLogicHandler sync{face, m_keyChain, testIsLsaNew, conf};
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064
65 const std::string updateNamePrefix;
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040066 const std::vector<Lsa::Type> lsaTypes{Lsa::Type::NAME,
67 Lsa::Type::ADJACENCY,
68 Lsa::Type::COORDINATE};
Ashlesh Gawande85998a12017-12-07 22:22:13 -060069};
70
Davide Pesaventod1f1df82022-03-12 16:40:37 -050071BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060072
73/* Tests that when SyncLogicHandler receives an LSA of either Name or
74 Adjacency type that appears to be newer, it will emit to its signal
75 with those LSA details.
76 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -050077BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060078{
Davide Pesaventod1f1df82022-03-12 16:40:37 -050079 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060080 uint64_t syncSeqNo = 1;
81
Davide Pesaventod1f1df82022-03-12 16:40:37 -050082 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::ADJACENCY}) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080083 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060084
Junxiao Shi43f37a02023-08-09 00:09:00 +000085 ndn::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -040086 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060087 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
88 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -050089 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060090 });
91
92 this->receiveUpdate(updateName, syncSeqNo);
93 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -050094
95 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060096}
97
98/* Tests that when SyncLogicHandler in HR mode receives an LSA of
99 either Coordinate or Name type that appears to be newer, it will
100 emit to its signal with those LSA details.
101 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500102BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600103{
104 this->conf.setHyperbolicState(HYPERBOLIC_STATE_ON);
105
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500106 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600107 uint64_t syncSeqNo = 1;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600108
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500109 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::COORDINATE}) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800110 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600111
Junxiao Shi43f37a02023-08-09 00:09:00 +0000112 ndn::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400113 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600114 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
115 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500116 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600117 });
118
119 this->receiveUpdate(updateName, syncSeqNo);
120 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500121
122 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600123}
124
125/* Tests that when SyncLogicHandler in HR-dry mode receives an LSA of
126 any type that appears to be newer, it will emit to its signal with
127 those LSA details.
128 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500129BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600130{
131 this->conf.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
132
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500133 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600134 uint64_t syncSeqNo = 1;
135
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500136 for (auto lsaType : this->lsaTypes) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800137 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600138
Junxiao Shi43f37a02023-08-09 00:09:00 +0000139 ndn::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400140 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600141 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
142 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500143 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600144 });
145
146 this->receiveUpdate(updateName, syncSeqNo);
147 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500148
149 BOOST_CHECK_EQUAL(nCallbacks, 3);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600150}
151
152/* Tests that when SyncLogicHandler receives an update for an LSA with
153 details matching this router's details, it will *not* emit to its
154 signal those LSA details.
155 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500156BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600157{
158 const uint64_t sequenceNumber = 1;
159
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500160 for (auto lsaType : this->lsaTypes) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600161 // To ensure that we get correctly-separated components, create
162 // and modify a Name to hand off.
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500163 ndn::Name updateName{this->conf.getLsaPrefix()};
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600164 updateName.append(this->conf.getSiteName())
165 .append(this->conf.getRouterName())
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800166 .append(boost::lexical_cast<std::string>(lsaType));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600167
Junxiao Shi43f37a02023-08-09 00:09:00 +0000168 ndn::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400169 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600170 BOOST_FAIL("Updates for self should not be emitted!");
171 });
172
173 this->receiveUpdate(updateName.toUri(), sequenceNumber);
174 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500175
176 // avoid "test case [...] did not check any assertions" message from Boost.Test
177 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600178}
179
180/* Tests that when SyncLogicHandler receives an update for an LSA with
181 details that do not match the expected format, it will *not* emit
182 to its signal those LSA details.
183 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500184BOOST_AUTO_TEST_CASE(MalformedUpdate)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600185{
186 const uint64_t sequenceNumber = 1;
187
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500188 for (auto lsaType : this->lsaTypes) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600189 ndn::Name updateName{this->conf.getSiteName()};
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800190 updateName.append(this->conf.getRouterName()).append(boost::lexical_cast<std::string>(lsaType));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600191
Junxiao Shi43f37a02023-08-09 00:09:00 +0000192 ndn::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400193 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600194 BOOST_FAIL("Malformed updates should not be emitted!");
195 });
196
197 this->receiveUpdate(updateName.toUri(), sequenceNumber);
198 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500199
200 // avoid "test case [...] did not check any assertions" message from Boost.Test
201 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600202}
203
204/* Tests that when SyncLogicHandler receives an update for an LSA with
205 details that do not appear to be new, it will *not* emit to its
206 signal those LSA details.
207 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500208BOOST_AUTO_TEST_CASE(LsaNotNew)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600209{
210 auto testLsaAlwaysFalse = [] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400211 const uint64_t& sequenceNumber, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600212 return false;
213 };
214
215 const uint64_t sequenceNumber = 1;
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400216 SyncLogicHandler sync{this->face, this->m_keyChain, testLsaAlwaysFalse, this->conf};
Junxiao Shi43f37a02023-08-09 00:09:00 +0000217 ndn::signal::ScopedConnection connection = sync.onNewLsa.connect(
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400218 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
219 BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!");
220 });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600221
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800222 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(Lsa::Type::NAME);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600223 this->receiveUpdate(updateName, sequenceNumber);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500224
225 // avoid "test case [...] did not check any assertions" message from Boost.Test
226 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600227}
228
229/* Tests that SyncLogicHandler successfully concatenates configured
230 variables together to form the necessary prefixes to advertise
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700231 through sync.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600232 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500233BOOST_AUTO_TEST_CASE(UpdatePrefix)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600234{
235 ndn::Name expectedPrefix = this->conf.getLsaPrefix();
236 expectedPrefix.append(this->conf.getSiteName());
237 expectedPrefix.append(this->conf.getRouterName());
238
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600239 BOOST_CHECK_EQUAL(this->sync.m_nameLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800240 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600241 BOOST_CHECK_EQUAL(this->sync.m_adjLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800242 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600243 BOOST_CHECK_EQUAL(this->sync.m_coorLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800244 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600245}
246
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600247BOOST_AUTO_TEST_SUITE_END()
248
249} // namespace test
250} // namespace nlsr