blob: add6706ab5853a6fa7c19605ceb617a3732d5f1a [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/*
Davide Pesavento8de8a8b2022-05-12 01:26:43 -04003 * Copyright (c) 2014-2022, 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
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050035namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050036namespace test {
37
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040038class LsdbFixture : public IoKeyChainFixture
Vince Lehman904c2412014-09-23 19:36:11 -050039{
40public:
41 LsdbFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040042 : face(m_io, m_keyChain, {true, true})
Saurab Dulal427e0122019-11-28 11:58:02 -060043 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060044 , confProcessor(conf)
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070045 , lsdb(face, m_keyChain, conf)
Vince Lehman904c2412014-09-23 19:36:11 -050046 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040047 m_keyChain.createIdentity("/ndn/site/%C1.Router/this-router");
Vince Lehmanf1aa5232014-10-06 17:57:35 -050048
Ashlesh Gawande85998a12017-12-07 22:22:13 -060049 advanceClocks(10_ms);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050050 face.sentInterests.clear();
Vince Lehman904c2412014-09-23 19:36:11 -050051 }
52
Vince Lehmanf1aa5232014-10-06 17:57:35 -050053 void
Vince Lehmanf1aa5232014-10-06 17:57:35 -050054 areNamePrefixListsEqual(NamePrefixList& lhs, NamePrefixList& rhs)
55 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040056 auto lhsList = lhs.getNames();
57 auto rhsList = rhs.getNames();
Vince Lehmanf1aa5232014-10-06 17:57:35 -050058 BOOST_REQUIRE_EQUAL(lhsList.size(), rhsList.size());
59
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040060 auto i = lhsList.begin();
61 auto j = rhsList.begin();
Vince Lehmanf1aa5232014-10-06 17:57:35 -050062 for (; i != lhsList.end(); ++i, ++j) {
63 BOOST_CHECK_EQUAL(*i, *j);
64 }
65 }
66
Ashlesh Gawande57a87172020-05-09 19:47:06 -070067 void
68 isFirstNameLsaEqual(const Lsdb& otherLsdb)
69 {
70 auto selfLsaRange = lsdb.getLsdbIterator<NameLsa>();
71 auto otherLsaRange = otherLsdb.getLsdbIterator<NameLsa>();
72
73 if (selfLsaRange.first != selfLsaRange.second && otherLsaRange.first != otherLsaRange.second) {
74 auto ownLsa = std::static_pointer_cast<NameLsa>(*selfLsaRange.first);
75 auto otherLsa = std::static_pointer_cast<NameLsa>(*otherLsaRange.first);
76 BOOST_CHECK_EQUAL(ownLsa->getNpl(), otherLsa->getNpl());
77 return;
78 }
79 BOOST_CHECK(false);
80 }
81
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070082 void
83 checkSignalResult(LsdbUpdate updateType,
84 const std::shared_ptr<Lsa>& lsaPtr,
85 const std::list<ndn::Name>& namesToAdd,
86 const std::list<ndn::Name>& namesToRemove)
87 {
88 BOOST_CHECK(updateHappened);
89 BOOST_CHECK_EQUAL(lsaPtrCheck->getOriginRouter(), lsaPtr->getOriginRouter());
90 BOOST_CHECK_EQUAL(lsaPtrCheck->getType(), lsaPtr->getType());
91 BOOST_CHECK(updateType == updateTypeCheck);
92 BOOST_CHECK(namesToAdd == namesToAddCheck);
93 BOOST_CHECK(namesToRemove == namesToRemoveCheck);
94 updateHappened = false;
95 }
96
97 void
98 connectSignal()
99 {
100 lsdb.onLsdbModified.connect(
101 [&] (std::shared_ptr<Lsa> lsa, LsdbUpdate updateType,
102 const auto& namesToAdd, const auto& namesToRemove) {
103 lsaPtrCheck = lsa;
104 updateTypeCheck = updateType;
105 namesToAddCheck = namesToAdd;
106 namesToRemoveCheck = namesToRemove;
107 updateHappened = true;
108 }
109 );
110 }
111
Vince Lehman904c2412014-09-23 19:36:11 -0500112public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500113 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600114 ConfParameter conf;
115 DummyConfFileProcessor confProcessor;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700116 Lsdb lsdb;
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500117
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700118 LsdbUpdate updateTypeCheck = LsdbUpdate::INSTALLED;
119 std::list<ndn::Name> namesToAddCheck;
120 std::list<ndn::Name> namesToRemoveCheck;
121 std::shared_ptr<Lsa> lsaPtrCheck = nullptr;
122 bool updateHappened = false;
Vince Lehman904c2412014-09-23 19:36:11 -0500123};
124
125BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500126
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500127BOOST_AUTO_TEST_CASE(LsdbSync)
128{
129 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router2/name");
130 uint64_t oldSeqNo = 82;
131
132 ndn::Name oldInterestName = interestName;
133 oldInterestName.appendNumber(oldSeqNo);
134
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400135 lsdb.expressInterest(oldInterestName, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600136 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500137
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500138 std::vector<ndn::Interest>& interests = face.sentInterests;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500139
140 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500141
Nick Gordon098aae42017-08-23 15:18:46 -0500142 bool didFindInterest = false;
143 for (const auto& interest : interests) {
144 didFindInterest = didFindInterest || interest.getName() == oldInterestName;
145 }
146
147 BOOST_CHECK(didFindInterest);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500148 interests.clear();
149
Nick Gordone98480b2017-05-24 11:23:03 -0500150 ndn::time::steady_clock::TimePoint deadline = ndn::time::steady_clock::now() +
151 ndn::time::seconds(LSA_REFRESH_TIME_MAX);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500152
153 // Simulate an LSA interest timeout
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500154 lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
155 oldInterestName, 0, deadline, interestName, oldSeqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600156 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500157
158 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500159
Nick Gordon098aae42017-08-23 15:18:46 -0500160 didFindInterest = false;
161 for (const auto& interest : interests) {
162 didFindInterest = didFindInterest || interest.getName() == oldInterestName;
163 }
164
165 BOOST_CHECK(didFindInterest);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500166 interests.clear();
167
168 uint64_t newSeqNo = 83;
169
170 ndn::Name newInterestName = interestName;
171 newInterestName.appendNumber(newSeqNo);
172
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400173 lsdb.expressInterest(newInterestName, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600174 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500175
176 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500177
Nick Gordon098aae42017-08-23 15:18:46 -0500178 didFindInterest = false;
179 for (const auto& interest : interests) {
180 didFindInterest = didFindInterest || interest.getName() == newInterestName;
181 }
182
183 BOOST_CHECK(didFindInterest);
184
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500185 interests.clear();
186
187 // Simulate an LSA interest timeout where the sequence number is outdated
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500188 lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
189 oldInterestName, 0, deadline, interestName, oldSeqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600190 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500191
192 // Interest should not be expressed for outdated sequence number
193 BOOST_CHECK_EQUAL(interests.size(), 0);
194}
195
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600196BOOST_AUTO_TEST_CASE(LsdbSegmentedData)
197{
198 // Add a lot of NameLSAs to exceed max packet size
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700199 ndn::Name originRouter("/ndn/site/%C1.Router/this-router");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600200
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700201 auto nameLsa = lsdb.findLsa<NameLsa>(originRouter);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800202 BOOST_REQUIRE(nameLsa != nullptr);
203 uint64_t seqNo = nameLsa->getSeqNo();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600204
205 ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
206
207 int nPrefixes = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800208 while (nameLsa->wireEncode().size() < ndn::MAX_NDN_PACKET_SIZE) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600209 nameLsa->addName(ndn::Name(prefix).appendNumber(++nPrefixes));
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500210 break;
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600211 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700212 lsdb.installLsa(nameLsa);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600213
214 // Create another Lsdb and expressInterest
Davide Pesavento8de8a8b2022-05-12 01:26:43 -0400215 ndn::util::DummyClientFace face2(m_io, m_keyChain, {true, true});
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600216 face.linkTo(face2);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500217
Saurab Dulal427e0122019-11-28 11:58:02 -0600218 ConfParameter conf2(face2, m_keyChain);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600219 std::string config = R"CONF(
220 trust-anchor
221 {
222 type any
223 }
224 )CONF";
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600225 conf2.getValidator().load(config, "config-file-from-string");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600226
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700227 Lsdb lsdb2(face2, m_keyChain, conf2);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600228
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500229 advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600230
231 ndn::Name interestName("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/NAME");
232 interestName.appendNumber(seqNo);
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400233 lsdb2.expressInterest(interestName, 0/*= timeout count*/, 0);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600234
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500235 advanceClocks(ndn::time::milliseconds(200), 20);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600236
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700237 isFirstNameLsaEqual(lsdb2);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600238}
239
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500240BOOST_AUTO_TEST_CASE(SegmentLsaData)
241{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700242 ndn::Name originRouter("/ndn/site/%C1.Router/this-router");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500243
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700244 auto lsa = lsdb.findLsa<NameLsa>(originRouter);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800245 uint64_t seqNo = lsa->getSeqNo();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500246
247 ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
248
249 int nPrefixes = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800250 while (lsa->wireEncode().size() < ndn::MAX_NDN_PACKET_SIZE) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000251 lsa->addName(ndn::Name(prefix).appendNumber(++nPrefixes));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500252 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700253 lsdb.installLsa(lsa);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500254
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800255 ndn::Block expectedDataContent = lsa->wireEncode();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500256
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600257 ndn::Name interestName("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/NAME/");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500258 interestName.appendNumber(seqNo);
259
Davide Pesavento8de8a8b2022-05-12 01:26:43 -0400260 ndn::util::DummyClientFace face2(m_io, m_keyChain, {true, true});
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600261 face.linkTo(face2);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500262
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600263 auto fetcher = ndn::util::SegmentFetcher::start(face2, ndn::Interest(interestName),
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400264 ndn::security::getAcceptAllValidator());
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600265 fetcher->onComplete.connect([&expectedDataContent] (ndn::ConstBufferPtr bufferPtr) {
266 ndn::Block block(bufferPtr);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800267 BOOST_CHECK_EQUAL(expectedDataContent, block);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600268 });
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500269
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600270 advanceClocks(ndn::time::milliseconds(1), 100);
271 fetcher->stop();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500272}
273
274BOOST_AUTO_TEST_CASE(ReceiveSegmentedLsaData)
275{
276 ndn::Name router("/ndn/cs/%C1.Router/router1");
277 uint64_t seqNo = 12;
278 NamePrefixList prefixList;
279
280 NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList);
281
282 ndn::Name prefix("/prefix/");
283
284 for (int nPrefixes = 0; nPrefixes < 3; ++nPrefixes) {
285 lsa.addName(ndn::Name(prefix).appendNumber(nPrefixes));
286 }
287
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600288 ndn::Name interestName("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/NAME/");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500289 interestName.appendNumber(seqNo);
290
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800291 ndn::Block block = lsa.wireEncode();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600292 lsdb.afterFetchLsa(block.getBuffer(), interestName);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500293
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700294 auto foundLsa = std::static_pointer_cast<NameLsa>(lsdb.findLsa(lsa.getOriginRouter(), lsa.getType()));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500295 BOOST_REQUIRE(foundLsa != nullptr);
296
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800297 BOOST_CHECK_EQUAL(foundLsa->wireEncode(), lsa.wireEncode());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500298}
299
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500300BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
301{
akmhoquec7a79b22014-05-26 08:06:19 -0500302 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500303 NamePrefixList npl1;
304
akmhoquefdbddb12014-05-02 18:35:19 -0500305 std::string s1 = "name1";
306 std::string s2 = "name2";
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700307 std::string router1 = "/router1/1";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500308
309 npl1.insert(s1);
310 npl1.insert(s2);
311
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600312 // For NameLsa lsType is name.
313 // 12 is seqNo, randomly generated.
314 // 1800 seconds is the default life time.
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700315 NameLsa nlsa1(router1, 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500316
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700317 Lsdb& lsdb1(lsdb);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500318
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700319 lsdb1.installLsa(std::make_shared<NameLsa>(nlsa1));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500320
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700321 BOOST_CHECK(lsdb1.doesLsaExist(router1, Lsa::Type::NAME));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500322
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700323 lsdb1.removeLsa(router1, Lsa::Type::NAME);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500324
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700325 BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(router1, Lsa::Type::NAME), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500326}
327
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500328BOOST_AUTO_TEST_CASE(InstallNameLsa)
329{
330 // Install lsa with name1 and name2
331 ndn::Name name1("/ndn/name1");
332 ndn::Name name2("/ndn/name2");
333
334 NamePrefixList prefixes;
335 prefixes.insert(name1);
336 prefixes.insert(name2);
337
338 std::string otherRouter("/ndn/site/%C1.router/other-router");
339 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
340
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600341 NameLsa lsa(otherRouter, 1, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700342 lsdb.installLsa(std::make_shared<NameLsa>(lsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500343
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700344 BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter, Lsa::Type::NAME), true);
345 NamePrefixList& nameList = std::static_pointer_cast<NameLsa>(lsdb.findLsa(otherRouter, Lsa::Type::NAME))->getNpl();
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500346
Nick Gordonf14ec352017-07-24 16:09:58 -0500347 BOOST_CHECK_EQUAL(nameList, prefixes);
348 //areNamePrefixListsEqual(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500349
350 // Add a prefix: name3
351 ndn::Name name3("/ndn/name3");
352 prefixes.insert(name3);
353
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600354 NameLsa addLsa(otherRouter, 2, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700355 lsdb.installLsa(std::make_shared<NameLsa>(addLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500356
357 // Lsa should include name1, name2, and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500358 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500359
360 // Remove a prefix: name2
361 prefixes.remove(name2);
362
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600363 NameLsa removeLsa(otherRouter, 3, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700364 lsdb.installLsa(std::make_shared<NameLsa>(removeLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500365
366 // Lsa should include name1 and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500367 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500368
369 // Add and remove a prefix: add name2, remove name3
370 prefixes.insert(name2);
371 prefixes.remove(name3);
372
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600373 NameLsa addAndRemoveLsa(otherRouter, 4, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700374 lsdb.installLsa(std::make_shared<NameLsa>(addAndRemoveLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500375
376 // Lsa should include name1 and name2
Nick Gordonf14ec352017-07-24 16:09:58 -0500377 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500378
379 // Install a completely new list of prefixes
380 ndn::Name name4("/ndn/name4");
381 ndn::Name name5("/ndn/name5");
382
383 NamePrefixList newPrefixes;
384 newPrefixes.insert(name4);
385 newPrefixes.insert(name5);
386
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600387 NameLsa newLsa(otherRouter, 5, MAX_TIME, newPrefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700388 lsdb.installLsa(std::make_shared<NameLsa>(newLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500389
390 // Lsa should include name4 and name5
Nick Gordonf14ec352017-07-24 16:09:58 -0500391 BOOST_CHECK_EQUAL(nameList, newPrefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500392}
393
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500394BOOST_AUTO_TEST_CASE(TestIsLsaNew)
395{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700396 ndn::Name originRouter("/ndn/memphis/%C1.Router/other-router");
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500397
398 // Install Name LSA
399 NamePrefixList nameList;
400 NameLsa lsa(originRouter, 999, ndn::time::system_clock::TimePoint::max(), nameList);
401
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700402 lsdb.installLsa(std::make_shared<NameLsa>(lsa));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500403
404 // Lower NameLSA sequence number
405 uint64_t lowerSeqNo = 998;
Nick Gordon727d4832017-10-13 18:04:25 -0500406 BOOST_CHECK(!lsdb.isLsaNew(originRouter, Lsa::Type::NAME, lowerSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500407
408 // Same NameLSA sequence number
409 uint64_t sameSeqNo = 999;
Nick Gordon727d4832017-10-13 18:04:25 -0500410 BOOST_CHECK(!lsdb.isLsaNew(originRouter, Lsa::Type::NAME, sameSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500411
412 // Higher NameLSA sequence number
413 uint64_t higherSeqNo = 1000;
Nick Gordon727d4832017-10-13 18:04:25 -0500414 BOOST_CHECK(lsdb.isLsaNew(originRouter, Lsa::Type::NAME, higherSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500415}
416
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700417BOOST_AUTO_TEST_CASE(LsdbSignals)
418{
419 connectSignal();
420 auto testTimePoint = ndn::time::system_clock::now() + 3600_s;
421 ndn::Name router2("/router2");
422 AdjLsa adjLsa(router2, 12, testTimePoint, 2, conf.getAdjacencyList());
423 std::shared_ptr<Lsa> lsaPtr = std::make_shared<AdjLsa>(adjLsa);
424 lsdb.installLsa(lsaPtr);
425 checkSignalResult(LsdbUpdate::INSTALLED, lsaPtr, {}, {});
426
427 adjLsa.setSeqNo(13);
428 updateHappened = false;
429 lsaPtr = std::make_shared<AdjLsa>(adjLsa);
430 lsdb.installLsa(lsaPtr);
431 BOOST_CHECK(!updateHappened);
432
433 Adjacent adj("Neighbor1");
434 adjLsa.setSeqNo(14);
435 adjLsa.addAdjacent(adj);
436 lsaPtr = std::make_shared<AdjLsa>(adjLsa);
437 lsdb.installLsa(lsaPtr);
438 checkSignalResult(LsdbUpdate::UPDATED, lsaPtr, {}, {});
439
440 lsdb.removeLsa(lsaPtrCheck->getOriginRouter(), Lsa::Type::ADJACENCY);
441 checkSignalResult(LsdbUpdate::REMOVED, lsaPtr, {}, {});
442
443 // Name LSA
444 NamePrefixList npl1{"name1", "name2"};
445 NameLsa nameLsa(router2, 12, testTimePoint, npl1);
446 lsaPtr = std::make_shared<NameLsa>(nameLsa);
447 lsdb.installLsa(lsaPtr);
448 checkSignalResult(LsdbUpdate::INSTALLED, lsaPtr, {}, {});
449
450 nameLsa.setSeqNo(13);
451 lsaPtr = std::make_shared<NameLsa>(nameLsa);
452 lsdb.installLsa(lsaPtr);
453 BOOST_CHECK(!updateHappened);
454
455 NamePrefixList npl2{"name2", "name3"};
456 NameLsa nameLsa2(router2, 14, testTimePoint, npl2);
457 lsaPtr = std::make_shared<NameLsa>(nameLsa2);
458 lsdb.installLsa(lsaPtr);
459 checkSignalResult(LsdbUpdate::UPDATED, lsaPtr, {"name3"}, {"name1"});
460
461 lsdb.removeLsa(lsaPtrCheck->getOriginRouter(), Lsa::Type::NAME);
462 checkSignalResult(LsdbUpdate::REMOVED, lsaPtr, {}, {});
463
464 // Coordinate LSA
465 lsaPtr = std::make_shared<CoordinateLsa>(CoordinateLsa("router1", 12, testTimePoint, 2.5, {30}));
466 lsdb.installLsa(lsaPtr);
467 checkSignalResult(LsdbUpdate::INSTALLED, lsaPtr, {}, {});
468
469 lsaPtr = std::make_shared<CoordinateLsa>(CoordinateLsa("router1", 13, testTimePoint, 2.5, {30}));
470 lsdb.installLsa(lsaPtr);
471 BOOST_CHECK(!updateHappened);
472
473 lsaPtr = std::make_shared<CoordinateLsa>(CoordinateLsa("router1", 14, testTimePoint, 2.5, {40}));
474 lsdb.installLsa(lsaPtr);
475 checkSignalResult(LsdbUpdate::UPDATED, lsaPtr, {}, {});
476
477 lsdb.removeLsa(lsaPtrCheck->getOriginRouter(), Lsa::Type::COORDINATE);
478 checkSignalResult(LsdbUpdate::REMOVED, lsaPtr, {}, {});
479}
480
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500481BOOST_AUTO_TEST_SUITE_END() // TestLsdb
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500482
Nick Gordonfad8e252016-08-11 14:21:38 -0500483} // namespace test
484} // namespace nlsr