blob: 197d5cd8dcebb0d95adcf1cd1cdfd379a5ba2e5c [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -05002/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehman7c603292014-09-11 17:48:16 -050021
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050022#include "lsdb.hpp"
Nick Gordon098aae42017-08-23 15:18:46 -050023#include "test-common.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050024#include "nlsr.hpp"
25#include "lsa.hpp"
26#include "name-prefix-list.hpp"
27#include <boost/test/unit_test.hpp>
28
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060029#include <ndn-cxx/util/dummy-client-face.hpp>
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050030#include <ndn-cxx/util/segment-fetcher.hpp>
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060031
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050032#include <unistd.h>
33
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050034namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050035namespace test {
36
dmcoomes9f936662017-03-02 10:33:09 -060037using std::shared_ptr;
Vince Lehman904c2412014-09-23 19:36:11 -050038
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050039class LsdbFixture : public UnitTestTimeFixture
Vince Lehman904c2412014-09-23 19:36:11 -050040{
41public:
42 LsdbFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050043 : face(m_ioService, m_keyChain)
44 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Vince Lehmanf1aa5232014-10-06 17:57:35 -050045 , lsdb(nlsr.getLsdb())
46 , conf(nlsr.getConfParameter())
Vince Lehman904c2412014-09-23 19:36:11 -050047 , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib")
48 , REGISTER_VERB("register")
49 {
Vince Lehmanf1aa5232014-10-06 17:57:35 -050050 conf.setNetwork("/ndn");
51 conf.setSiteName("/site");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050052 conf.setRouterName("/%C1.Router/this-router");
53
54 addIdentity("/ndn/site/%C1.Router/this-router");
Vince Lehmanf1aa5232014-10-06 17:57:35 -050055
56 nlsr.initialize();
57
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050058 advanceClocks(ndn::time::milliseconds(1), 10);
59 face.sentInterests.clear();
Vince Lehman904c2412014-09-23 19:36:11 -050060 }
61
Vince Lehmanf1aa5232014-10-06 17:57:35 -050062 void
63 extractParameters(ndn::Interest& interest, ndn::Name::Component& verb,
64 ndn::nfd::ControlParameters& extractedParameters)
Vince Lehman904c2412014-09-23 19:36:11 -050065 {
66 const ndn::Name& name = interest.getName();
67 verb = name[REGISTER_COMMAND_PREFIX.size()];
68 const ndn::Name::Component& parameterComponent = name[REGISTER_COMMAND_PREFIX.size() + 1];
69
70 ndn::Block rawParameters = parameterComponent.blockFromValue();
71 extractedParameters.wireDecode(rawParameters);
72 }
73
Vince Lehmanf1aa5232014-10-06 17:57:35 -050074 void
75 areNamePrefixListsEqual(NamePrefixList& lhs, NamePrefixList& rhs)
76 {
Nick Gordonf14ec352017-07-24 16:09:58 -050077
Vince Lehmanf1aa5232014-10-06 17:57:35 -050078 typedef std::list<ndn::Name> NameList;
79
Nick Gordonf14ec352017-07-24 16:09:58 -050080 NameList lhsList = lhs.getNames();
81 NameList rhsList = rhs.getNames();
Vince Lehmanf1aa5232014-10-06 17:57:35 -050082
83 BOOST_REQUIRE_EQUAL(lhsList.size(), rhsList.size());
84
85 NameList::iterator i = lhsList.begin();
86 NameList::iterator j = rhsList.begin();
87
88 for (; i != lhsList.end(); ++i, ++j) {
89 BOOST_CHECK_EQUAL(*i, *j);
90 }
91 }
92
Vince Lehman904c2412014-09-23 19:36:11 -050093public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050094 ndn::util::DummyClientFace face;
Vince Lehman904c2412014-09-23 19:36:11 -050095 Nlsr nlsr;
Vince Lehmanf1aa5232014-10-06 17:57:35 -050096 Lsdb& lsdb;
97 ConfParameter& conf;
98
Vince Lehman904c2412014-09-23 19:36:11 -050099 ndn::Name REGISTER_COMMAND_PREFIX;
100 ndn::Name::Component REGISTER_VERB;
101};
102
103BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500104
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500105BOOST_AUTO_TEST_CASE(LsdbSync)
106{
107 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router2/name");
108 uint64_t oldSeqNo = 82;
109
110 ndn::Name oldInterestName = interestName;
111 oldInterestName.appendNumber(oldSeqNo);
112
113 lsdb.expressInterest(oldInterestName, 0);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500114 advanceClocks(ndn::time::milliseconds(1), 10);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500115
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500116 std::vector<ndn::Interest>& interests = face.sentInterests;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500117
118 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500119
Nick Gordon098aae42017-08-23 15:18:46 -0500120 bool didFindInterest = false;
121 for (const auto& interest : interests) {
122 didFindInterest = didFindInterest || interest.getName() == oldInterestName;
123 }
124
125 BOOST_CHECK(didFindInterest);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500126 interests.clear();
127
Nick Gordone98480b2017-05-24 11:23:03 -0500128 ndn::time::steady_clock::TimePoint deadline = ndn::time::steady_clock::now() +
129 ndn::time::seconds(LSA_REFRESH_TIME_MAX);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500130
131 // Simulate an LSA interest timeout
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500132 lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
133 oldInterestName, 0, deadline, interestName, oldSeqNo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500134 advanceClocks(ndn::time::milliseconds(1), 10);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500135
136 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500137
Nick Gordon098aae42017-08-23 15:18:46 -0500138 didFindInterest = false;
139 for (const auto& interest : interests) {
140 didFindInterest = didFindInterest || interest.getName() == oldInterestName;
141 }
142
143 BOOST_CHECK(didFindInterest);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500144 interests.clear();
145
146 uint64_t newSeqNo = 83;
147
148 ndn::Name newInterestName = interestName;
149 newInterestName.appendNumber(newSeqNo);
150
151 lsdb.expressInterest(newInterestName, 0);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500152 advanceClocks(ndn::time::milliseconds(1), 10);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500153
154 BOOST_REQUIRE(interests.size() > 0);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500155
Nick Gordon098aae42017-08-23 15:18:46 -0500156 didFindInterest = false;
157 for (const auto& interest : interests) {
158 didFindInterest = didFindInterest || interest.getName() == newInterestName;
159 }
160
161 BOOST_CHECK(didFindInterest);
162
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500163 interests.clear();
164
165 // Simulate an LSA interest timeout where the sequence number is outdated
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500166 lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
167 oldInterestName, 0, deadline, interestName, oldSeqNo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500168 advanceClocks(ndn::time::milliseconds(1), 10);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500169
170 // Interest should not be expressed for outdated sequence number
171 BOOST_CHECK_EQUAL(interests.size(), 0);
172}
173
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500174BOOST_AUTO_TEST_CASE(SegmentLsaData)
175{
176 ndn::Name router("/ndn/cs/%C1.Router/router1");
177 uint64_t seqNo = 12;
178 NamePrefixList prefixList;
179
180 NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList);
181
182 ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
183
184 int nPrefixes = 0;
Nick Gordonfaf49f42017-10-23 12:36:28 -0500185 while (lsa.serialize().size() < ndn::MAX_NDN_PACKET_SIZE) {
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500186 lsa.addName(ndn::Name(prefix).appendNumber(++nPrefixes));
187 }
188
Nick Gordonfaf49f42017-10-23 12:36:28 -0500189 std::string expectedDataContent = lsa.serialize();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500190 lsdb.installNameLsa(lsa);
191
Nick Gordon727d4832017-10-13 18:04:25 -0500192 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500193 interestName.appendNumber(seqNo);
194
195 ndn::Interest interest(interestName);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500196 lsdb.processInterest(ndn::Name(), interest);
197 advanceClocks(ndn::time::milliseconds(1), 10);
198 face.sentData.clear();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500199
200 lsdb.processInterest(ndn::Name(), interest);
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500201
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500202 advanceClocks(ndn::time::milliseconds(1), 10);
203
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500204 std::string recvDataContent;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500205 for (const ndn::Data& data : face.sentData)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500206 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500207 const ndn::Block& nameBlock = data.getContent();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500208 std::string nameBlockContent(reinterpret_cast<char const*>(nameBlock.value()),
209 nameBlock.value_size());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500210
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500211 recvDataContent += nameBlockContent;
212 }
213
214 BOOST_CHECK_EQUAL(expectedDataContent, recvDataContent);
215}
216
217BOOST_AUTO_TEST_CASE(ReceiveSegmentedLsaData)
218{
219 ndn::Name router("/ndn/cs/%C1.Router/router1");
220 uint64_t seqNo = 12;
221 NamePrefixList prefixList;
222
223 NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList);
224
225 ndn::Name prefix("/prefix/");
226
227 for (int nPrefixes = 0; nPrefixes < 3; ++nPrefixes) {
228 lsa.addName(ndn::Name(prefix).appendNumber(nPrefixes));
229 }
230
Nick Gordon727d4832017-10-13 18:04:25 -0500231 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500232 interestName.appendNumber(seqNo);
233
Nick Gordonfaf49f42017-10-23 12:36:28 -0500234 const ndn::ConstBufferPtr bufferPtr = std::make_shared<ndn::Buffer>(lsa.serialize().c_str(),
235 lsa.serialize().size());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500236 lsdb.afterFetchLsa(bufferPtr, interestName);
237
238 NameLsa* foundLsa = lsdb.findNameLsa(lsa.getKey());
239 BOOST_REQUIRE(foundLsa != nullptr);
240
Nick Gordonfaf49f42017-10-23 12:36:28 -0500241 BOOST_CHECK_EQUAL(foundLsa->serialize(), lsa.serialize());
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500242}
243
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500244BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
245{
akmhoquec7a79b22014-05-26 08:06:19 -0500246 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500247 NamePrefixList npl1;
248
akmhoquefdbddb12014-05-02 18:35:19 -0500249 std::string s1 = "name1";
250 std::string s2 = "name2";
251 std::string router1 = "router1/1";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500252
253 npl1.insert(s1);
254 npl1.insert(s2);
255
Vince Lehman904c2412014-09-23 19:36:11 -0500256 //For NameLsa lsType is name.
257 //12 is seqNo, randomly generated.
258 //1800 is the default life time.
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600259 NameLsa nlsa1(ndn::Name("/router1/1"), 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500260
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500261 Lsdb lsdb1(nlsr, m_scheduler);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500262
akmhoque31d1d4b2014-05-05 22:08:14 -0500263 lsdb1.installNameLsa(nlsa1);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700264 lsdb1.writeNameLsdbLog();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500265
Nick Gordon727d4832017-10-13 18:04:25 -0500266 BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/NAME"), Lsa::Type::NAME));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500267
akmhoque31d1d4b2014-05-05 22:08:14 -0500268 lsdb1.removeNameLsa(router1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500269
Nick Gordon727d4832017-10-13 18:04:25 -0500270 BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), Lsa::Type::NAME), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500271}
272
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500273BOOST_AUTO_TEST_CASE(InstallNameLsa)
274{
275 // Install lsa with name1 and name2
276 ndn::Name name1("/ndn/name1");
277 ndn::Name name2("/ndn/name2");
278
279 NamePrefixList prefixes;
280 prefixes.insert(name1);
281 prefixes.insert(name2);
282
283 std::string otherRouter("/ndn/site/%C1.router/other-router");
284 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
285
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600286 NameLsa lsa(otherRouter, 1, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500287 lsdb.installNameLsa(lsa);
288
Nick Gordon727d4832017-10-13 18:04:25 -0500289 BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter + "/NAME", Lsa::Type::NAME), true);
290 NamePrefixList& nameList = lsdb.findNameLsa(otherRouter + "/NAME")->getNpl();
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500291
Nick Gordonf14ec352017-07-24 16:09:58 -0500292 BOOST_CHECK_EQUAL(nameList, prefixes);
293 //areNamePrefixListsEqual(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500294
295 // Add a prefix: name3
296 ndn::Name name3("/ndn/name3");
297 prefixes.insert(name3);
298
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600299 NameLsa addLsa(otherRouter, 2, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500300 lsdb.installNameLsa(addLsa);
301
302 // Lsa should include name1, name2, and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500303 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500304
305 // Remove a prefix: name2
306 prefixes.remove(name2);
307
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600308 NameLsa removeLsa(otherRouter, 3, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500309 lsdb.installNameLsa(removeLsa);
310
311 // Lsa should include name1 and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500312 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500313
314 // Add and remove a prefix: add name2, remove name3
315 prefixes.insert(name2);
316 prefixes.remove(name3);
317
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600318 NameLsa addAndRemoveLsa(otherRouter, 4, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500319 lsdb.installNameLsa(addAndRemoveLsa);
320
321 // Lsa should include name1 and name2
Nick Gordonf14ec352017-07-24 16:09:58 -0500322 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500323
324 // Install a completely new list of prefixes
325 ndn::Name name4("/ndn/name4");
326 ndn::Name name5("/ndn/name5");
327
328 NamePrefixList newPrefixes;
329 newPrefixes.insert(name4);
330 newPrefixes.insert(name5);
331
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600332 NameLsa newLsa(otherRouter, 5, MAX_TIME, newPrefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500333 lsdb.installNameLsa(newLsa);
334
335 // Lsa should include name4 and name5
Nick Gordonf14ec352017-07-24 16:09:58 -0500336 BOOST_CHECK_EQUAL(nameList, newPrefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500337}
338
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500339BOOST_AUTO_TEST_CASE(TestIsLsaNew)
340{
341 const ndn::Name::Component CONFIG_NETWORK{"/ndn"};
342 const ndn::Name::Component CONFIG_SITE{"/memphis"};
343 ndn::Name originRouter{};
344 originRouter.append(CONFIG_NETWORK).append(CONFIG_SITE).append("/%C1.Router/other-router");
345
346 // Install Name LSA
347 NamePrefixList nameList;
348 NameLsa lsa(originRouter, 999, ndn::time::system_clock::TimePoint::max(), nameList);
349
350 lsdb.installNameLsa(lsa);
351
352 // Lower NameLSA sequence number
353 uint64_t lowerSeqNo = 998;
Nick Gordon727d4832017-10-13 18:04:25 -0500354 BOOST_CHECK(!lsdb.isLsaNew(originRouter, Lsa::Type::NAME, lowerSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500355
356 // Same NameLSA sequence number
357 uint64_t sameSeqNo = 999;
Nick Gordon727d4832017-10-13 18:04:25 -0500358 BOOST_CHECK(!lsdb.isLsaNew(originRouter, Lsa::Type::NAME, sameSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500359
360 // Higher NameLSA sequence number
361 uint64_t higherSeqNo = 1000;
Nick Gordon727d4832017-10-13 18:04:25 -0500362 BOOST_CHECK(lsdb.isLsaNew(originRouter, Lsa::Type::NAME, higherSeqNo));
Nick Gordon8f23b5d2017-08-31 17:53:07 -0500363}
364
365BOOST_AUTO_TEST_SUITE_END() // TestLsdb
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500366
Nick Gordonfad8e252016-08-11 14:21:38 -0500367} // namespace test
368} // namespace nlsr