blob: ec2668b757c6ac8ac21d5e58ad030822c690c67e [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 , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib")
47 , REGISTER_VERB("register")
48 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040049 m_keyChain.createIdentity("/ndn/site/%C1.Router/this-router");
Vince Lehmanf1aa5232014-10-06 17:57:35 -050050
Ashlesh Gawande85998a12017-12-07 22:22:13 -060051 advanceClocks(10_ms);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050052 face.sentInterests.clear();
Vince Lehman904c2412014-09-23 19:36:11 -050053 }
54
Vince Lehmanf1aa5232014-10-06 17:57:35 -050055 void
56 extractParameters(ndn::Interest& interest, ndn::Name::Component& verb,
57 ndn::nfd::ControlParameters& extractedParameters)
Vince Lehman904c2412014-09-23 19:36:11 -050058 {
59 const ndn::Name& name = interest.getName();
60 verb = name[REGISTER_COMMAND_PREFIX.size()];
61 const ndn::Name::Component& parameterComponent = name[REGISTER_COMMAND_PREFIX.size() + 1];
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040062 extractedParameters.wireDecode(parameterComponent.blockFromValue());
Vince Lehman904c2412014-09-23 19:36:11 -050063 }
64
Vince Lehmanf1aa5232014-10-06 17:57:35 -050065 void
66 areNamePrefixListsEqual(NamePrefixList& lhs, NamePrefixList& rhs)
67 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040068 auto lhsList = lhs.getNames();
69 auto rhsList = rhs.getNames();
Vince Lehmanf1aa5232014-10-06 17:57:35 -050070 BOOST_REQUIRE_EQUAL(lhsList.size(), rhsList.size());
71
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040072 auto i = lhsList.begin();
73 auto j = rhsList.begin();
Vince Lehmanf1aa5232014-10-06 17:57:35 -050074 for (; i != lhsList.end(); ++i, ++j) {
75 BOOST_CHECK_EQUAL(*i, *j);
76 }
77 }
78
Ashlesh Gawande57a87172020-05-09 19:47:06 -070079 void
80 isFirstNameLsaEqual(const Lsdb& otherLsdb)
81 {
82 auto selfLsaRange = lsdb.getLsdbIterator<NameLsa>();
83 auto otherLsaRange = otherLsdb.getLsdbIterator<NameLsa>();
84
85 if (selfLsaRange.first != selfLsaRange.second && otherLsaRange.first != otherLsaRange.second) {
86 auto ownLsa = std::static_pointer_cast<NameLsa>(*selfLsaRange.first);
87 auto otherLsa = std::static_pointer_cast<NameLsa>(*otherLsaRange.first);
88 BOOST_CHECK_EQUAL(ownLsa->getNpl(), otherLsa->getNpl());
89 return;
90 }
91 BOOST_CHECK(false);
92 }
93
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070094 void
95 checkSignalResult(LsdbUpdate updateType,
96 const std::shared_ptr<Lsa>& lsaPtr,
97 const std::list<ndn::Name>& namesToAdd,
98 const std::list<ndn::Name>& namesToRemove)
99 {
100 BOOST_CHECK(updateHappened);
101 BOOST_CHECK_EQUAL(lsaPtrCheck->getOriginRouter(), lsaPtr->getOriginRouter());
102 BOOST_CHECK_EQUAL(lsaPtrCheck->getType(), lsaPtr->getType());
103 BOOST_CHECK(updateType == updateTypeCheck);
104 BOOST_CHECK(namesToAdd == namesToAddCheck);
105 BOOST_CHECK(namesToRemove == namesToRemoveCheck);
106 updateHappened = false;
107 }
108
109 void
110 connectSignal()
111 {
112 lsdb.onLsdbModified.connect(
113 [&] (std::shared_ptr<Lsa> lsa, LsdbUpdate updateType,
114 const auto& namesToAdd, const auto& namesToRemove) {
115 lsaPtrCheck = lsa;
116 updateTypeCheck = updateType;
117 namesToAddCheck = namesToAdd;
118 namesToRemoveCheck = namesToRemove;
119 updateHappened = true;
120 }
121 );
122 }
123
Vince Lehman904c2412014-09-23 19:36:11 -0500124public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500125 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600126 ConfParameter conf;
127 DummyConfFileProcessor confProcessor;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700128 Lsdb lsdb;
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500129
Vince Lehman904c2412014-09-23 19:36:11 -0500130 ndn::Name REGISTER_COMMAND_PREFIX;
131 ndn::Name::Component REGISTER_VERB;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700132
133 LsdbUpdate updateTypeCheck = LsdbUpdate::INSTALLED;
134 std::list<ndn::Name> namesToAddCheck;
135 std::list<ndn::Name> namesToRemoveCheck;
136 std::shared_ptr<Lsa> lsaPtrCheck = nullptr;
137 bool updateHappened = false;
Vince Lehman904c2412014-09-23 19:36:11 -0500138};
139
140BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500141
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500142BOOST_AUTO_TEST_CASE(LsdbSync)
143{
144 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router2/name");
145 uint64_t oldSeqNo = 82;
146
147 ndn::Name oldInterestName = interestName;
148 oldInterestName.appendNumber(oldSeqNo);
149
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400150 lsdb.expressInterest(oldInterestName, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600151 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500152
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500153 std::vector<ndn::Interest>& interests = face.sentInterests;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500154
155 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500156
Nick Gordon098aae42017-08-23 15:18:46 -0500157 bool didFindInterest = false;
158 for (const auto& interest : interests) {
159 didFindInterest = didFindInterest || interest.getName() == oldInterestName;
160 }
161
162 BOOST_CHECK(didFindInterest);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500163 interests.clear();
164
Nick Gordone98480b2017-05-24 11:23:03 -0500165 ndn::time::steady_clock::TimePoint deadline = ndn::time::steady_clock::now() +
166 ndn::time::seconds(LSA_REFRESH_TIME_MAX);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500167
168 // Simulate an LSA interest timeout
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500169 lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
170 oldInterestName, 0, deadline, interestName, oldSeqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600171 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500172
173 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500174
Nick Gordon098aae42017-08-23 15:18:46 -0500175 didFindInterest = false;
176 for (const auto& interest : interests) {
177 didFindInterest = didFindInterest || interest.getName() == oldInterestName;
178 }
179
180 BOOST_CHECK(didFindInterest);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500181 interests.clear();
182
183 uint64_t newSeqNo = 83;
184
185 ndn::Name newInterestName = interestName;
186 newInterestName.appendNumber(newSeqNo);
187
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400188 lsdb.expressInterest(newInterestName, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600189 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500190
191 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500192
Nick Gordon098aae42017-08-23 15:18:46 -0500193 didFindInterest = false;
194 for (const auto& interest : interests) {
195 didFindInterest = didFindInterest || interest.getName() == newInterestName;
196 }
197
198 BOOST_CHECK(didFindInterest);
199
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500200 interests.clear();
201
202 // Simulate an LSA interest timeout where the sequence number is outdated
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500203 lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
204 oldInterestName, 0, deadline, interestName, oldSeqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600205 advanceClocks(10_ms);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500206
207 // Interest should not be expressed for outdated sequence number
208 BOOST_CHECK_EQUAL(interests.size(), 0);
209}
210
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600211BOOST_AUTO_TEST_CASE(LsdbSegmentedData)
212{
213 // Add a lot of NameLSAs to exceed max packet size
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700214 ndn::Name originRouter("/ndn/site/%C1.Router/this-router");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600215
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700216 auto nameLsa = lsdb.findLsa<NameLsa>(originRouter);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800217 BOOST_REQUIRE(nameLsa != nullptr);
218 uint64_t seqNo = nameLsa->getSeqNo();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600219
220 ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
221
222 int nPrefixes = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800223 while (nameLsa->wireEncode().size() < ndn::MAX_NDN_PACKET_SIZE) {
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600224 nameLsa->addName(ndn::Name(prefix).appendNumber(++nPrefixes));
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500225 break;
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600226 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700227 lsdb.installLsa(nameLsa);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600228
229 // Create another Lsdb and expressInterest
Davide Pesavento8de8a8b2022-05-12 01:26:43 -0400230 ndn::util::DummyClientFace face2(m_io, m_keyChain, {true, true});
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600231 face.linkTo(face2);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500232
Saurab Dulal427e0122019-11-28 11:58:02 -0600233 ConfParameter conf2(face2, m_keyChain);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600234 std::string config = R"CONF(
235 trust-anchor
236 {
237 type any
238 }
239 )CONF";
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600240 conf2.getValidator().load(config, "config-file-from-string");
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600241
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700242 Lsdb lsdb2(face2, m_keyChain, conf2);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600243
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500244 advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600245
246 ndn::Name interestName("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/NAME");
247 interestName.appendNumber(seqNo);
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400248 lsdb2.expressInterest(interestName, 0/*= timeout count*/, 0);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600249
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500250 advanceClocks(ndn::time::milliseconds(200), 20);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600251
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700252 isFirstNameLsaEqual(lsdb2);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600253}
254
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500255BOOST_AUTO_TEST_CASE(SegmentLsaData)
256{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700257 ndn::Name originRouter("/ndn/site/%C1.Router/this-router");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500258
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700259 auto lsa = lsdb.findLsa<NameLsa>(originRouter);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800260 uint64_t seqNo = lsa->getSeqNo();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500261
262 ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
263
264 int nPrefixes = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800265 while (lsa->wireEncode().size() < ndn::MAX_NDN_PACKET_SIZE) {
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000266 lsa->addName(ndn::Name(prefix).appendNumber(++nPrefixes));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500267 }
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700268 lsdb.installLsa(lsa);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500269
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800270 ndn::Block expectedDataContent = lsa->wireEncode();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500271
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600272 ndn::Name interestName("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/NAME/");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500273 interestName.appendNumber(seqNo);
274
Davide Pesavento8de8a8b2022-05-12 01:26:43 -0400275 ndn::util::DummyClientFace face2(m_io, m_keyChain, {true, true});
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600276 face.linkTo(face2);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500277
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600278 auto fetcher = ndn::util::SegmentFetcher::start(face2, ndn::Interest(interestName),
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400279 ndn::security::getAcceptAllValidator());
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600280 fetcher->onComplete.connect([&expectedDataContent] (ndn::ConstBufferPtr bufferPtr) {
281 ndn::Block block(bufferPtr);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800282 BOOST_CHECK_EQUAL(expectedDataContent, block);
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600283 });
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500284
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600285 advanceClocks(ndn::time::milliseconds(1), 100);
286 fetcher->stop();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500287}
288
289BOOST_AUTO_TEST_CASE(ReceiveSegmentedLsaData)
290{
291 ndn::Name router("/ndn/cs/%C1.Router/router1");
292 uint64_t seqNo = 12;
293 NamePrefixList prefixList;
294
295 NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList);
296
297 ndn::Name prefix("/prefix/");
298
299 for (int nPrefixes = 0; nPrefixes < 3; ++nPrefixes) {
300 lsa.addName(ndn::Name(prefix).appendNumber(nPrefixes));
301 }
302
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600303 ndn::Name interestName("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/NAME/");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500304 interestName.appendNumber(seqNo);
305
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800306 ndn::Block block = lsa.wireEncode();
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600307 lsdb.afterFetchLsa(block.getBuffer(), interestName);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500308
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700309 auto foundLsa = std::static_pointer_cast<NameLsa>(lsdb.findLsa(lsa.getOriginRouter(), lsa.getType()));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500310 BOOST_REQUIRE(foundLsa != nullptr);
311
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800312 BOOST_CHECK_EQUAL(foundLsa->wireEncode(), lsa.wireEncode());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500313}
314
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500315BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
316{
akmhoquec7a79b22014-05-26 08:06:19 -0500317 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500318 NamePrefixList npl1;
319
akmhoquefdbddb12014-05-02 18:35:19 -0500320 std::string s1 = "name1";
321 std::string s2 = "name2";
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700322 std::string router1 = "/router1/1";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500323
324 npl1.insert(s1);
325 npl1.insert(s2);
326
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600327 // For NameLsa lsType is name.
328 // 12 is seqNo, randomly generated.
329 // 1800 seconds is the default life time.
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700330 NameLsa nlsa1(router1, 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500331
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700332 Lsdb& lsdb1(lsdb);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500333
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700334 lsdb1.installLsa(std::make_shared<NameLsa>(nlsa1));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500335
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700336 BOOST_CHECK(lsdb1.doesLsaExist(router1, Lsa::Type::NAME));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500337
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700338 lsdb1.removeLsa(router1, Lsa::Type::NAME);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500339
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700340 BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(router1, Lsa::Type::NAME), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500341}
342
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500343BOOST_AUTO_TEST_CASE(InstallNameLsa)
344{
345 // Install lsa with name1 and name2
346 ndn::Name name1("/ndn/name1");
347 ndn::Name name2("/ndn/name2");
348
349 NamePrefixList prefixes;
350 prefixes.insert(name1);
351 prefixes.insert(name2);
352
353 std::string otherRouter("/ndn/site/%C1.router/other-router");
354 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
355
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600356 NameLsa lsa(otherRouter, 1, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700357 lsdb.installLsa(std::make_shared<NameLsa>(lsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500358
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700359 BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter, Lsa::Type::NAME), true);
360 NamePrefixList& nameList = std::static_pointer_cast<NameLsa>(lsdb.findLsa(otherRouter, Lsa::Type::NAME))->getNpl();
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500361
Nick Gordonf14ec352017-07-24 16:09:58 -0500362 BOOST_CHECK_EQUAL(nameList, prefixes);
363 //areNamePrefixListsEqual(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500364
365 // Add a prefix: name3
366 ndn::Name name3("/ndn/name3");
367 prefixes.insert(name3);
368
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600369 NameLsa addLsa(otherRouter, 2, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700370 lsdb.installLsa(std::make_shared<NameLsa>(addLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500371
372 // Lsa should include name1, name2, and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500373 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500374
375 // Remove a prefix: name2
376 prefixes.remove(name2);
377
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600378 NameLsa removeLsa(otherRouter, 3, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700379 lsdb.installLsa(std::make_shared<NameLsa>(removeLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500380
381 // Lsa should include name1 and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500382 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500383
384 // Add and remove a prefix: add name2, remove name3
385 prefixes.insert(name2);
386 prefixes.remove(name3);
387
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600388 NameLsa addAndRemoveLsa(otherRouter, 4, MAX_TIME, prefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700389 lsdb.installLsa(std::make_shared<NameLsa>(addAndRemoveLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500390
391 // Lsa should include name1 and name2
Nick Gordonf14ec352017-07-24 16:09:58 -0500392 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500393
394 // Install a completely new list of prefixes
395 ndn::Name name4("/ndn/name4");
396 ndn::Name name5("/ndn/name5");
397
398 NamePrefixList newPrefixes;
399 newPrefixes.insert(name4);
400 newPrefixes.insert(name5);
401
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600402 NameLsa newLsa(otherRouter, 5, MAX_TIME, newPrefixes);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700403 lsdb.installLsa(std::make_shared<NameLsa>(newLsa));
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500404
405 // Lsa should include name4 and name5
Nick Gordonf14ec352017-07-24 16:09:58 -0500406 BOOST_CHECK_EQUAL(nameList, newPrefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500407}
408
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500409BOOST_AUTO_TEST_CASE(TestIsLsaNew)
410{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700411 ndn::Name originRouter("/ndn/memphis/%C1.Router/other-router");
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500412
413 // Install Name LSA
414 NamePrefixList nameList;
415 NameLsa lsa(originRouter, 999, ndn::time::system_clock::TimePoint::max(), nameList);
416
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700417 lsdb.installLsa(std::make_shared<NameLsa>(lsa));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500418
419 // Lower NameLSA sequence number
420 uint64_t lowerSeqNo = 998;
Nick Gordon727d4832017-10-13 18:04:25 -0500421 BOOST_CHECK(!lsdb.isLsaNew(originRouter, Lsa::Type::NAME, lowerSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500422
423 // Same NameLSA sequence number
424 uint64_t sameSeqNo = 999;
Nick Gordon727d4832017-10-13 18:04:25 -0500425 BOOST_CHECK(!lsdb.isLsaNew(originRouter, Lsa::Type::NAME, sameSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500426
427 // Higher NameLSA sequence number
428 uint64_t higherSeqNo = 1000;
Nick Gordon727d4832017-10-13 18:04:25 -0500429 BOOST_CHECK(lsdb.isLsaNew(originRouter, Lsa::Type::NAME, higherSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500430}
431
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700432BOOST_AUTO_TEST_CASE(LsdbSignals)
433{
434 connectSignal();
435 auto testTimePoint = ndn::time::system_clock::now() + 3600_s;
436 ndn::Name router2("/router2");
437 AdjLsa adjLsa(router2, 12, testTimePoint, 2, conf.getAdjacencyList());
438 std::shared_ptr<Lsa> lsaPtr = std::make_shared<AdjLsa>(adjLsa);
439 lsdb.installLsa(lsaPtr);
440 checkSignalResult(LsdbUpdate::INSTALLED, lsaPtr, {}, {});
441
442 adjLsa.setSeqNo(13);
443 updateHappened = false;
444 lsaPtr = std::make_shared<AdjLsa>(adjLsa);
445 lsdb.installLsa(lsaPtr);
446 BOOST_CHECK(!updateHappened);
447
448 Adjacent adj("Neighbor1");
449 adjLsa.setSeqNo(14);
450 adjLsa.addAdjacent(adj);
451 lsaPtr = std::make_shared<AdjLsa>(adjLsa);
452 lsdb.installLsa(lsaPtr);
453 checkSignalResult(LsdbUpdate::UPDATED, lsaPtr, {}, {});
454
455 lsdb.removeLsa(lsaPtrCheck->getOriginRouter(), Lsa::Type::ADJACENCY);
456 checkSignalResult(LsdbUpdate::REMOVED, lsaPtr, {}, {});
457
458 // Name LSA
459 NamePrefixList npl1{"name1", "name2"};
460 NameLsa nameLsa(router2, 12, testTimePoint, npl1);
461 lsaPtr = std::make_shared<NameLsa>(nameLsa);
462 lsdb.installLsa(lsaPtr);
463 checkSignalResult(LsdbUpdate::INSTALLED, lsaPtr, {}, {});
464
465 nameLsa.setSeqNo(13);
466 lsaPtr = std::make_shared<NameLsa>(nameLsa);
467 lsdb.installLsa(lsaPtr);
468 BOOST_CHECK(!updateHappened);
469
470 NamePrefixList npl2{"name2", "name3"};
471 NameLsa nameLsa2(router2, 14, testTimePoint, npl2);
472 lsaPtr = std::make_shared<NameLsa>(nameLsa2);
473 lsdb.installLsa(lsaPtr);
474 checkSignalResult(LsdbUpdate::UPDATED, lsaPtr, {"name3"}, {"name1"});
475
476 lsdb.removeLsa(lsaPtrCheck->getOriginRouter(), Lsa::Type::NAME);
477 checkSignalResult(LsdbUpdate::REMOVED, lsaPtr, {}, {});
478
479 // Coordinate LSA
480 lsaPtr = std::make_shared<CoordinateLsa>(CoordinateLsa("router1", 12, testTimePoint, 2.5, {30}));
481 lsdb.installLsa(lsaPtr);
482 checkSignalResult(LsdbUpdate::INSTALLED, lsaPtr, {}, {});
483
484 lsaPtr = std::make_shared<CoordinateLsa>(CoordinateLsa("router1", 13, testTimePoint, 2.5, {30}));
485 lsdb.installLsa(lsaPtr);
486 BOOST_CHECK(!updateHappened);
487
488 lsaPtr = std::make_shared<CoordinateLsa>(CoordinateLsa("router1", 14, testTimePoint, 2.5, {40}));
489 lsdb.installLsa(lsaPtr);
490 checkSignalResult(LsdbUpdate::UPDATED, lsaPtr, {}, {});
491
492 lsdb.removeLsa(lsaPtrCheck->getOriginRouter(), Lsa::Type::COORDINATE);
493 checkSignalResult(LsdbUpdate::REMOVED, lsaPtr, {}, {});
494}
495
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500496BOOST_AUTO_TEST_SUITE_END() // TestLsdb
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500497
Nick Gordonfad8e252016-08-11 14:21:38 -0500498} // namespace test
499} // namespace nlsr