blob: 28cf59e38f1c5956bdca6cec3ef9a465d8166629 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -04002/*
Junxiao Shib8752932024-01-07 15:18:46 +00003 * Copyright (c) 2014-2024, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040020 */
Vince Lehman7c603292014-09-11 17:48:16 -050021
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050022#include "lsdb.hpp"
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080023#include "lsa/lsa.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050024#include "name-prefix-list.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050025
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040026#include "tests/io-key-chain-fixture.hpp"
27#include "tests/test-common.hpp"
28
29#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
30#include <ndn-cxx/security/validator-null.hpp>
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050031#include <ndn-cxx/util/segment-fetcher.hpp>
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060032
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050033#include <unistd.h>
34
Davide Pesavento288141a2024-02-13 17:30:35 -050035namespace nlsr::tests {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050036
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040037class LsdbFixture : public IoKeyChainFixture
Vince Lehman904c2412014-09-23 19:36:11 -050038{
39public:
40 LsdbFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040041 : face(m_io, m_keyChain, {true, true})
Saurab Dulal427e0122019-11-28 11:58:02 -060042 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060043 , confProcessor(conf)
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070044 , lsdb(face, m_keyChain, conf)
Vince Lehman904c2412014-09-23 19:36:11 -050045 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040046 m_keyChain.createIdentity("/ndn/site/%C1.Router/this-router");
Vince Lehmanf1aa5232014-10-06 17:57:35 -050047
Ashlesh Gawande85998a12017-12-07 22:22:13 -060048 advanceClocks(10_ms);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050049 face.sentInterests.clear();
Vince Lehman904c2412014-09-23 19:36:11 -050050 }
51
Vince Lehmanf1aa5232014-10-06 17:57:35 -050052 void
Ashlesh Gawande57a87172020-05-09 19:47:06 -070053 isFirstNameLsaEqual(const Lsdb& otherLsdb)
54 {
55 auto selfLsaRange = lsdb.getLsdbIterator<NameLsa>();
56 auto otherLsaRange = otherLsdb.getLsdbIterator<NameLsa>();
57
58 if (selfLsaRange.first != selfLsaRange.second && otherLsaRange.first != otherLsaRange.second) {
59 auto ownLsa = std::static_pointer_cast<NameLsa>(*selfLsaRange.first);
60 auto otherLsa = std::static_pointer_cast<NameLsa>(*otherLsaRange.first);
61 BOOST_CHECK_EQUAL(ownLsa->getNpl(), otherLsa->getNpl());
62 return;
63 }
64 BOOST_CHECK(false);
65 }
66
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070067 void
68 checkSignalResult(LsdbUpdate updateType,
69 const std::shared_ptr<Lsa>& lsaPtr,
70 const std::list<ndn::Name>& namesToAdd,
71 const std::list<ndn::Name>& namesToRemove)
72 {
73 BOOST_CHECK(updateHappened);
74 BOOST_CHECK_EQUAL(lsaPtrCheck->getOriginRouter(), lsaPtr->getOriginRouter());
75 BOOST_CHECK_EQUAL(lsaPtrCheck->getType(), lsaPtr->getType());
76 BOOST_CHECK(updateType == updateTypeCheck);
77 BOOST_CHECK(namesToAdd == namesToAddCheck);
78 BOOST_CHECK(namesToRemove == namesToRemoveCheck);
79 updateHappened = false;
80 }
81
82 void
83 connectSignal()
84 {
85 lsdb.onLsdbModified.connect(
86 [&] (std::shared_ptr<Lsa> lsa, LsdbUpdate updateType,
87 const auto& namesToAdd, const auto& namesToRemove) {
88 lsaPtrCheck = lsa;
89 updateTypeCheck = updateType;
90 namesToAddCheck = namesToAdd;
91 namesToRemoveCheck = namesToRemove;
92 updateHappened = true;
93 }
94 );
95 }
96
Vince Lehman904c2412014-09-23 19:36:11 -050097public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000098 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060099 ConfParameter conf;
100 DummyConfFileProcessor confProcessor;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700101 Lsdb lsdb;
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500102
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700103 LsdbUpdate updateTypeCheck = LsdbUpdate::INSTALLED;
104 std::list<ndn::Name> namesToAddCheck;
105 std::list<ndn::Name> namesToRemoveCheck;
106 std::shared_ptr<Lsa> lsaPtrCheck = nullptr;
107 bool updateHappened = false;
Vince Lehman904c2412014-09-23 19:36:11 -0500108};
109
110BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500111
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500112BOOST_AUTO_TEST_CASE(LsdbSync)
113{
114 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router2/name");
115 uint64_t oldSeqNo = 82;
116
117 ndn::Name oldInterestName = interestName;
118 oldInterestName.appendNumber(oldSeqNo);
119
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400120 lsdb.expressInterest(oldInterestName, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600121 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500122
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500123 std::vector<ndn::Interest>& interests = face.sentInterests;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500124
125 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500126
Nick Gordon098aae42017-08-23 15:18:46 -0500127 bool didFindInterest = false;
128 for (const auto& interest : interests) {
129 didFindInterest = didFindInterest || interest.getName() == oldInterestName;
130 }
131
132 BOOST_CHECK(didFindInterest);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500133 interests.clear();
134
Davide Pesavento658fd852023-05-10 22:15:03 -0400135 auto deadline = ndn::time::steady_clock::now() + ndn::time::seconds(LSA_REFRESH_TIME_MAX);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500136
137 // Simulate an LSA interest timeout
Junxiao Shi43f37a02023-08-09 00:09:00 +0000138 lsdb.onFetchLsaError(ndn::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500139 oldInterestName, 0, deadline, interestName, oldSeqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600140 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500141
142 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500143
Nick Gordon098aae42017-08-23 15:18:46 -0500144 didFindInterest = false;
145 for (const auto& interest : interests) {
146 didFindInterest = didFindInterest || interest.getName() == oldInterestName;
147 }
148
149 BOOST_CHECK(didFindInterest);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500150 interests.clear();
151
152 uint64_t newSeqNo = 83;
153
154 ndn::Name newInterestName = interestName;
155 newInterestName.appendNumber(newSeqNo);
156
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400157 lsdb.expressInterest(newInterestName, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600158 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500159
160 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500161
Nick Gordon098aae42017-08-23 15:18:46 -0500162 didFindInterest = false;
163 for (const auto& interest : interests) {
164 didFindInterest = didFindInterest || interest.getName() == newInterestName;
165 }
166
167 BOOST_CHECK(didFindInterest);
168
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500169 interests.clear();
170
171 // Simulate an LSA interest timeout where the sequence number is outdated
Junxiao Shi43f37a02023-08-09 00:09:00 +0000172 lsdb.onFetchLsaError(ndn::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500173 oldInterestName, 0, deadline, interestName, oldSeqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600174 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500175
176 // Interest should not be expressed for outdated sequence number
177 BOOST_CHECK_EQUAL(interests.size(), 0);
178}
179
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600180BOOST_AUTO_TEST_CASE(LsdbSegmentedData)
181{
182 // Add a lot of NameLSAs to exceed max packet size
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700183 ndn::Name originRouter("/ndn/site/%C1.Router/this-router");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600184
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700185 auto nameLsa = lsdb.findLsa<NameLsa>(originRouter);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800186 BOOST_REQUIRE(nameLsa != nullptr);
187 uint64_t seqNo = nameLsa->getSeqNo();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600188
189 ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
190
191 int nPrefixes = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800192 while (nameLsa->wireEncode().size() < ndn::MAX_NDN_PACKET_SIZE) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600193 nameLsa->addName(ndn::Name(prefix).appendNumber(++nPrefixes));
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500194 break;
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600195 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700196 lsdb.installLsa(nameLsa);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600197
198 // Create another Lsdb and expressInterest
Junxiao Shi43f37a02023-08-09 00:09:00 +0000199 ndn::DummyClientFace face2(m_io, m_keyChain, {true, true});
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600200 face.linkTo(face2);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500201
Saurab Dulal427e0122019-11-28 11:58:02 -0600202 ConfParameter conf2(face2, m_keyChain);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600203 std::string config = R"CONF(
204 trust-anchor
205 {
206 type any
207 }
208 )CONF";
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600209 conf2.getValidator().load(config, "config-file-from-string");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600210
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700211 Lsdb lsdb2(face2, m_keyChain, conf2);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600212
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500213 advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600214
215 ndn::Name interestName("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/NAME");
216 interestName.appendNumber(seqNo);
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400217 lsdb2.expressInterest(interestName, 0/*= timeout count*/, 0);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600218
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500219 advanceClocks(ndn::time::milliseconds(200), 20);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600220
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700221 isFirstNameLsaEqual(lsdb2);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600222}
223
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500224BOOST_AUTO_TEST_CASE(SegmentLsaData)
225{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700226 ndn::Name originRouter("/ndn/site/%C1.Router/this-router");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500227
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700228 auto lsa = lsdb.findLsa<NameLsa>(originRouter);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800229 uint64_t seqNo = lsa->getSeqNo();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500230
231 ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
232
233 int nPrefixes = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800234 while (lsa->wireEncode().size() < ndn::MAX_NDN_PACKET_SIZE) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000235 lsa->addName(ndn::Name(prefix).appendNumber(++nPrefixes));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500236 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700237 lsdb.installLsa(lsa);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500238
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800239 ndn::Block expectedDataContent = lsa->wireEncode();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500240
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600241 ndn::Name interestName("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/NAME/");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500242 interestName.appendNumber(seqNo);
243
Junxiao Shi43f37a02023-08-09 00:09:00 +0000244 ndn::DummyClientFace face2(m_io, m_keyChain, {true, true});
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600245 face.linkTo(face2);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500246
Junxiao Shi43f37a02023-08-09 00:09:00 +0000247 auto fetcher = ndn::SegmentFetcher::start(face2, ndn::Interest(interestName),
248 ndn::security::getAcceptAllValidator());
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600249 fetcher->onComplete.connect([&expectedDataContent] (ndn::ConstBufferPtr bufferPtr) {
250 ndn::Block block(bufferPtr);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800251 BOOST_CHECK_EQUAL(expectedDataContent, block);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600252 });
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500253
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600254 advanceClocks(ndn::time::milliseconds(1), 100);
255 fetcher->stop();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500256}
257
258BOOST_AUTO_TEST_CASE(ReceiveSegmentedLsaData)
259{
260 ndn::Name router("/ndn/cs/%C1.Router/router1");
261 uint64_t seqNo = 12;
262 NamePrefixList prefixList;
263
264 NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList);
265
266 ndn::Name prefix("/prefix/");
267
268 for (int nPrefixes = 0; nPrefixes < 3; ++nPrefixes) {
269 lsa.addName(ndn::Name(prefix).appendNumber(nPrefixes));
270 }
271
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600272 ndn::Name interestName("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/NAME/");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500273 interestName.appendNumber(seqNo);
274
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800275 ndn::Block block = lsa.wireEncode();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600276 lsdb.afterFetchLsa(block.getBuffer(), interestName);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500277
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700278 auto foundLsa = std::static_pointer_cast<NameLsa>(lsdb.findLsa(lsa.getOriginRouter(), lsa.getType()));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500279 BOOST_REQUIRE(foundLsa != nullptr);
280
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800281 BOOST_CHECK_EQUAL(foundLsa->wireEncode(), lsa.wireEncode());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500282}
283
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500284BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
285{
Davide Pesavento658fd852023-05-10 22:15:03 -0400286 auto testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500287 NamePrefixList npl1;
288
akmhoquefdbddb12014-05-02 18:35:19 -0500289 std::string s1 = "name1";
290 std::string s2 = "name2";
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700291 std::string router1 = "/router1/1";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500292
293 npl1.insert(s1);
294 npl1.insert(s2);
295
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600296 // For NameLsa lsType is name.
297 // 12 is seqNo, randomly generated.
298 // 1800 seconds is the default life time.
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700299 NameLsa nlsa1(router1, 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500300
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700301 Lsdb& lsdb1(lsdb);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500302
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700303 lsdb1.installLsa(std::make_shared<NameLsa>(nlsa1));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500304
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700305 BOOST_CHECK(lsdb1.doesLsaExist(router1, Lsa::Type::NAME));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500306
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700307 lsdb1.removeLsa(router1, Lsa::Type::NAME);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500308
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700309 BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(router1, Lsa::Type::NAME), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500310}
311
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500312BOOST_AUTO_TEST_CASE(InstallNameLsa)
313{
314 // Install lsa with name1 and name2
315 ndn::Name name1("/ndn/name1");
316 ndn::Name name2("/ndn/name2");
317
318 NamePrefixList prefixes;
319 prefixes.insert(name1);
320 prefixes.insert(name2);
321
322 std::string otherRouter("/ndn/site/%C1.router/other-router");
Davide Pesavento658fd852023-05-10 22:15:03 -0400323 const auto MAX_TIME = ndn::time::system_clock::time_point::max();
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500324
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600325 NameLsa lsa(otherRouter, 1, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700326 lsdb.installLsa(std::make_shared<NameLsa>(lsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500327
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700328 BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter, Lsa::Type::NAME), true);
329 NamePrefixList& nameList = std::static_pointer_cast<NameLsa>(lsdb.findLsa(otherRouter, Lsa::Type::NAME))->getNpl();
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500330
Nick Gordonf14ec352017-07-24 16:09:58 -0500331 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500332
333 // Add a prefix: name3
334 ndn::Name name3("/ndn/name3");
335 prefixes.insert(name3);
336
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600337 NameLsa addLsa(otherRouter, 2, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700338 lsdb.installLsa(std::make_shared<NameLsa>(addLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500339
340 // Lsa should include name1, name2, and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500341 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500342
343 // Remove a prefix: name2
Junxiao Shi52f16642023-08-15 00:18:35 +0000344 prefixes.erase(name2);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500345
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600346 NameLsa removeLsa(otherRouter, 3, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700347 lsdb.installLsa(std::make_shared<NameLsa>(removeLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500348
349 // Lsa should include name1 and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500350 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500351
352 // Add and remove a prefix: add name2, remove name3
353 prefixes.insert(name2);
Junxiao Shi52f16642023-08-15 00:18:35 +0000354 prefixes.erase(name3);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500355
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600356 NameLsa addAndRemoveLsa(otherRouter, 4, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700357 lsdb.installLsa(std::make_shared<NameLsa>(addAndRemoveLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500358
359 // Lsa should include name1 and name2
Nick Gordonf14ec352017-07-24 16:09:58 -0500360 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500361
362 // Install a completely new list of prefixes
363 ndn::Name name4("/ndn/name4");
364 ndn::Name name5("/ndn/name5");
365
366 NamePrefixList newPrefixes;
367 newPrefixes.insert(name4);
368 newPrefixes.insert(name5);
369
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600370 NameLsa newLsa(otherRouter, 5, MAX_TIME, newPrefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700371 lsdb.installLsa(std::make_shared<NameLsa>(newLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500372
373 // Lsa should include name4 and name5
Nick Gordonf14ec352017-07-24 16:09:58 -0500374 BOOST_CHECK_EQUAL(nameList, newPrefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500375}
376
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500377BOOST_AUTO_TEST_CASE(TestIsLsaNew)
378{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700379 ndn::Name originRouter("/ndn/memphis/%C1.Router/other-router");
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500380
381 // Install Name LSA
382 NamePrefixList nameList;
Davide Pesavento658fd852023-05-10 22:15:03 -0400383 NameLsa lsa(originRouter, 999, ndn::time::system_clock::time_point::max(), nameList);
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500384
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700385 lsdb.installLsa(std::make_shared<NameLsa>(lsa));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500386
387 // Lower NameLSA sequence number
388 uint64_t lowerSeqNo = 998;
Nick Gordon727d4832017-10-13 18:04:25 -0500389 BOOST_CHECK(!lsdb.isLsaNew(originRouter, Lsa::Type::NAME, lowerSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500390
391 // Same NameLSA sequence number
392 uint64_t sameSeqNo = 999;
Nick Gordon727d4832017-10-13 18:04:25 -0500393 BOOST_CHECK(!lsdb.isLsaNew(originRouter, Lsa::Type::NAME, sameSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500394
395 // Higher NameLSA sequence number
396 uint64_t higherSeqNo = 1000;
Nick Gordon727d4832017-10-13 18:04:25 -0500397 BOOST_CHECK(lsdb.isLsaNew(originRouter, Lsa::Type::NAME, higherSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500398}
399
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700400BOOST_AUTO_TEST_CASE(LsdbSignals)
401{
402 connectSignal();
403 auto testTimePoint = ndn::time::system_clock::now() + 3600_s;
404 ndn::Name router2("/router2");
Junxiao Shib8752932024-01-07 15:18:46 +0000405 AdjLsa adjLsa(router2, 12, testTimePoint, conf.getAdjacencyList());
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700406 std::shared_ptr<Lsa> lsaPtr = std::make_shared<AdjLsa>(adjLsa);
407 lsdb.installLsa(lsaPtr);
408 checkSignalResult(LsdbUpdate::INSTALLED, lsaPtr, {}, {});
409
410 adjLsa.setSeqNo(13);
411 updateHappened = false;
412 lsaPtr = std::make_shared<AdjLsa>(adjLsa);
413 lsdb.installLsa(lsaPtr);
414 BOOST_CHECK(!updateHappened);
415
416 Adjacent adj("Neighbor1");
417 adjLsa.setSeqNo(14);
418 adjLsa.addAdjacent(adj);
419 lsaPtr = std::make_shared<AdjLsa>(adjLsa);
420 lsdb.installLsa(lsaPtr);
421 checkSignalResult(LsdbUpdate::UPDATED, lsaPtr, {}, {});
422
423 lsdb.removeLsa(lsaPtrCheck->getOriginRouter(), Lsa::Type::ADJACENCY);
424 checkSignalResult(LsdbUpdate::REMOVED, lsaPtr, {}, {});
425
426 // Name LSA
427 NamePrefixList npl1{"name1", "name2"};
428 NameLsa nameLsa(router2, 12, testTimePoint, npl1);
429 lsaPtr = std::make_shared<NameLsa>(nameLsa);
430 lsdb.installLsa(lsaPtr);
431 checkSignalResult(LsdbUpdate::INSTALLED, lsaPtr, {}, {});
432
433 nameLsa.setSeqNo(13);
434 lsaPtr = std::make_shared<NameLsa>(nameLsa);
435 lsdb.installLsa(lsaPtr);
436 BOOST_CHECK(!updateHappened);
437
438 NamePrefixList npl2{"name2", "name3"};
439 NameLsa nameLsa2(router2, 14, testTimePoint, npl2);
440 lsaPtr = std::make_shared<NameLsa>(nameLsa2);
441 lsdb.installLsa(lsaPtr);
442 checkSignalResult(LsdbUpdate::UPDATED, lsaPtr, {"name3"}, {"name1"});
443
444 lsdb.removeLsa(lsaPtrCheck->getOriginRouter(), Lsa::Type::NAME);
445 checkSignalResult(LsdbUpdate::REMOVED, lsaPtr, {}, {});
446
447 // Coordinate LSA
448 lsaPtr = std::make_shared<CoordinateLsa>(CoordinateLsa("router1", 12, testTimePoint, 2.5, {30}));
449 lsdb.installLsa(lsaPtr);
450 checkSignalResult(LsdbUpdate::INSTALLED, lsaPtr, {}, {});
451
452 lsaPtr = std::make_shared<CoordinateLsa>(CoordinateLsa("router1", 13, testTimePoint, 2.5, {30}));
453 lsdb.installLsa(lsaPtr);
454 BOOST_CHECK(!updateHappened);
455
456 lsaPtr = std::make_shared<CoordinateLsa>(CoordinateLsa("router1", 14, testTimePoint, 2.5, {40}));
457 lsdb.installLsa(lsaPtr);
458 checkSignalResult(LsdbUpdate::UPDATED, lsaPtr, {}, {});
459
460 lsdb.removeLsa(lsaPtrCheck->getOriginRouter(), Lsa::Type::COORDINATE);
461 checkSignalResult(LsdbUpdate::REMOVED, lsaPtr, {}, {});
462}
463
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500464BOOST_AUTO_TEST_SUITE_END() // TestLsdb
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500465
Davide Pesavento288141a2024-02-13 17:30:35 -0500466} // namespace nlsr::tests