blob: 8c0cc3de9279003aee46a885d9abb72dc98b151c [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"
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
33using std::shared_ptr;
34
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040035class SyncLogicFixture : public IoKeyChainFixture
Ashlesh Gawande85998a12017-12-07 22:22:13 -060036{
37public:
38 SyncLogicFixture()
Davide Pesaventod1f1df82022-03-12 16:40:37 -050039 : testIsLsaNew([] (auto&&...) { return true; })
Ashlesh Gawande85998a12017-12-07 22:22:13 -060040 , updateNamePrefix(this->conf.getLsaPrefix().toUri() +
41 this->conf.getSiteName().toUri() +
42 "/%C1.Router/other-router/")
43 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040044 m_keyChain.createIdentity(conf.getRouterPrefix());
Ashlesh Gawande85998a12017-12-07 22:22:13 -060045 }
46
47 void
48 receiveUpdate(const std::string& prefix, uint64_t seqNo)
49 {
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;
Alexander Afanasyev135288c2022-04-23 23:06:56 -040054 updates.push_back({ndn::Name(prefix), 0, seqNo, 0});
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070055 sync.m_syncLogic.onPSyncUpdate(updates);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060056
57 this->advanceClocks(ndn::time::milliseconds(1), 10);
58 }
59
60public:
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040061 ndn::util::DummyClientFace face{m_io, m_keyChain};
Davide Pesaventod1f1df82022-03-12 16:40:37 -050062 ConfParameter conf{face, m_keyChain};
Davide Pesavento1954a0c2022-09-30 15:56:04 -040063 DummyConfFileProcessor confProcessor{conf, SyncProtocol::PSYNC};
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064 SyncLogicHandler::IsLsaNew testIsLsaNew;
Davide Pesavento1954a0c2022-09-30 15:56:04 -040065 SyncLogicHandler sync{face, m_keyChain, testIsLsaNew, conf};
Ashlesh Gawande85998a12017-12-07 22:22:13 -060066
67 const std::string updateNamePrefix;
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040068 const std::vector<Lsa::Type> lsaTypes{Lsa::Type::NAME,
69 Lsa::Type::ADJACENCY,
70 Lsa::Type::COORDINATE};
Ashlesh Gawande85998a12017-12-07 22:22:13 -060071};
72
Davide Pesaventod1f1df82022-03-12 16:40:37 -050073BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060074
75/* Tests that when SyncLogicHandler receives an LSA of either Name or
76 Adjacency type that appears to be newer, it will emit to its signal
77 with those LSA details.
78 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -050079BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060080{
Davide Pesaventod1f1df82022-03-12 16:40:37 -050081 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060082 uint64_t syncSeqNo = 1;
83
Davide Pesaventod1f1df82022-03-12 16:40:37 -050084 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::ADJACENCY}) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080085 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060086
Davide Pesavento1954a0c2022-09-30 15:56:04 -040087 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -040088 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060089 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
90 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -050091 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060092 });
93
94 this->receiveUpdate(updateName, syncSeqNo);
95 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -050096
97 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060098}
99
100/* Tests that when SyncLogicHandler in HR mode receives an LSA of
101 either Coordinate or Name type that appears to be newer, it will
102 emit to its signal with those LSA details.
103 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500104BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600105{
106 this->conf.setHyperbolicState(HYPERBOLIC_STATE_ON);
107
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500108 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600109 uint64_t syncSeqNo = 1;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600110
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500111 for (auto lsaType : {Lsa::Type::NAME, Lsa::Type::COORDINATE}) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800112 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600113
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400114 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400115 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600116 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
117 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500118 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600119 });
120
121 this->receiveUpdate(updateName, syncSeqNo);
122 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500123
124 BOOST_CHECK_EQUAL(nCallbacks, 2);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600125}
126
127/* Tests that when SyncLogicHandler in HR-dry mode receives an LSA of
128 any type that appears to be newer, it will emit to its signal with
129 those LSA details.
130 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500131BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600132{
133 this->conf.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
134
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500135 size_t nCallbacks = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600136 uint64_t syncSeqNo = 1;
137
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500138 for (auto lsaType : this->lsaTypes) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800139 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600140
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400141 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400142 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600143 BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName);
144 BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500145 ++nCallbacks;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600146 });
147
148 this->receiveUpdate(updateName, syncSeqNo);
149 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500150
151 BOOST_CHECK_EQUAL(nCallbacks, 3);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600152}
153
154/* Tests that when SyncLogicHandler receives an update for an LSA with
155 details matching this router's details, it will *not* emit to its
156 signal those LSA details.
157 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500158BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600159{
160 const uint64_t sequenceNumber = 1;
161
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500162 for (auto lsaType : this->lsaTypes) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600163 // To ensure that we get correctly-separated components, create
164 // and modify a Name to hand off.
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500165 ndn::Name updateName{this->conf.getLsaPrefix()};
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600166 updateName.append(this->conf.getSiteName())
167 .append(this->conf.getRouterName())
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800168 .append(boost::lexical_cast<std::string>(lsaType));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600169
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400170 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400171 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600172 BOOST_FAIL("Updates for self should not be emitted!");
173 });
174
175 this->receiveUpdate(updateName.toUri(), sequenceNumber);
176 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500177
178 // avoid "test case [...] did not check any assertions" message from Boost.Test
179 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600180}
181
182/* Tests that when SyncLogicHandler receives an update for an LSA with
183 details that do not match the expected format, it will *not* emit
184 to its signal those LSA details.
185 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500186BOOST_AUTO_TEST_CASE(MalformedUpdate)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600187{
188 const uint64_t sequenceNumber = 1;
189
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500190 for (auto lsaType : this->lsaTypes) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600191 ndn::Name updateName{this->conf.getSiteName()};
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800192 updateName.append(this->conf.getRouterName()).append(boost::lexical_cast<std::string>(lsaType));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600193
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400194 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa.connect(
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400195 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600196 BOOST_FAIL("Malformed updates should not be emitted!");
197 });
198
199 this->receiveUpdate(updateName.toUri(), sequenceNumber);
200 }
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500201
202 // avoid "test case [...] did not check any assertions" message from Boost.Test
203 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600204}
205
206/* Tests that when SyncLogicHandler receives an update for an LSA with
207 details that do not appear to be new, it will *not* emit to its
208 signal those LSA details.
209 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500210BOOST_AUTO_TEST_CASE(LsaNotNew)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600211{
212 auto testLsaAlwaysFalse = [] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400213 const uint64_t& sequenceNumber, uint64_t incomingFaceId) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600214 return false;
215 };
216
217 const uint64_t sequenceNumber = 1;
Davide Pesavento1954a0c2022-09-30 15:56:04 -0400218 SyncLogicHandler sync{this->face, this->m_keyChain, testLsaAlwaysFalse, this->conf};
219 ndn::util::signal::ScopedConnection connection = sync.onNewLsa.connect(
220 [&] (const auto& routerName, uint64_t sequenceNumber, const auto& originRouter, uint64_t incomingFaceId) {
221 BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!");
222 });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600223
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800224 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(Lsa::Type::NAME);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600225 this->receiveUpdate(updateName, sequenceNumber);
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500226
227 // avoid "test case [...] did not check any assertions" message from Boost.Test
228 BOOST_CHECK(true);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600229}
230
231/* Tests that SyncLogicHandler successfully concatenates configured
232 variables together to form the necessary prefixes to advertise
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700233 through sync.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600234 */
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500235BOOST_AUTO_TEST_CASE(UpdatePrefix)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600236{
237 ndn::Name expectedPrefix = this->conf.getLsaPrefix();
238 expectedPrefix.append(this->conf.getSiteName());
239 expectedPrefix.append(this->conf.getRouterName());
240
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600241 BOOST_CHECK_EQUAL(this->sync.m_nameLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800242 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600243 BOOST_CHECK_EQUAL(this->sync.m_adjLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800244 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600245 BOOST_CHECK_EQUAL(this->sync.m_coorLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800246 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600247}
248
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600249BOOST_AUTO_TEST_SUITE_END()
250
251} // namespace test
252} // namespace nlsr