blob: 2dd6f3ea057f02bfa7256cf5020a0940512474af [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/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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
22#include "test-common.hpp"
23
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050024#include "lsdb.hpp"
25#include "nlsr.hpp"
26#include "lsa.hpp"
27#include "name-prefix-list.hpp"
28#include <boost/test/unit_test.hpp>
29
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060030#include <ndn-cxx/util/dummy-client-face.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
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050033namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050034namespace test {
35
dmcoomes9f936662017-03-02 10:33:09 -060036using std::shared_ptr;
Vince Lehman904c2412014-09-23 19:36:11 -050037
38class LsdbFixture : public BaseFixture
39{
40public:
41 LsdbFixture()
dmcoomes9f936662017-03-02 10:33:09 -060042 : face(std::make_shared<ndn::util::DummyClientFace>(g_ioService))
43 , nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain)
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050044 , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter())
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");
52 conf.setRouterName("/%C1.router/this-router");
53
54 nlsr.initialize();
55
56 face->processEvents(ndn::time::milliseconds(1));
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060057 face->sentInterests.clear();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -050058
59 INIT_LOGGERS("/tmp", "DEBUG");
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:
dmcoomes9f936662017-03-02 10:33:09 -060094 std::shared_ptr<ndn::util::DummyClientFace> face;
Vince Lehman904c2412014-09-23 19:36:11 -050095 Nlsr nlsr;
96 SyncLogicHandler sync;
97
Vince Lehmanf1aa5232014-10-06 17:57:35 -050098 Lsdb& lsdb;
99 ConfParameter& conf;
100
Vince Lehman904c2412014-09-23 19:36:11 -0500101 ndn::Name REGISTER_COMMAND_PREFIX;
102 ndn::Name::Component REGISTER_VERB;
103};
104
105BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500106
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500107BOOST_AUTO_TEST_CASE(LsdbSync)
108{
109 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router2/name");
110 uint64_t oldSeqNo = 82;
111
112 ndn::Name oldInterestName = interestName;
113 oldInterestName.appendNumber(oldSeqNo);
114
115 lsdb.expressInterest(oldInterestName, 0);
116 face->processEvents(ndn::time::milliseconds(1));
117
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -0600118 std::vector<ndn::Interest>& interests = face->sentInterests;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500119
120 BOOST_REQUIRE(interests.size() > 0);
121 std::vector<ndn::Interest>::iterator it = interests.begin();
122
123 BOOST_CHECK_EQUAL(it->getName(), oldInterestName);
124 interests.clear();
125
126 steady_clock::TimePoint deadline = steady_clock::now() +
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500127 ndn::time::seconds(LSA_REFRESH_TIME_MAX);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500128
129 // Simulate an LSA interest timeout
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500130 lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
131 oldInterestName, 0, deadline, interestName, oldSeqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500132 face->processEvents(ndn::time::milliseconds(1));
133
134 BOOST_REQUIRE(interests.size() > 0);
135 it = interests.begin();
136
137 BOOST_CHECK_EQUAL(it->getName(), oldInterestName);
138 interests.clear();
139
140 uint64_t newSeqNo = 83;
141
142 ndn::Name newInterestName = interestName;
143 newInterestName.appendNumber(newSeqNo);
144
145 lsdb.expressInterest(newInterestName, 0);
146 face->processEvents(ndn::time::milliseconds(1));
147
148 BOOST_REQUIRE(interests.size() > 0);
149 it = interests.begin();
150
151 BOOST_CHECK_EQUAL(it->getName(), newInterestName);
152 interests.clear();
153
154 // Simulate an LSA interest timeout where the sequence number is outdated
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500155 lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout",
156 oldInterestName, 0, deadline, interestName, oldSeqNo);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500157 face->processEvents(ndn::time::milliseconds(1));
158
159 // Interest should not be expressed for outdated sequence number
160 BOOST_CHECK_EQUAL(interests.size(), 0);
161}
162
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500163BOOST_AUTO_TEST_CASE(SegmentLsaData)
164{
165 ndn::Name router("/ndn/cs/%C1.Router/router1");
166 uint64_t seqNo = 12;
167 NamePrefixList prefixList;
168
169 NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList);
170
171 ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
172
173 int nPrefixes = 0;
174 while (lsa.getData().size() < ndn::MAX_NDN_PACKET_SIZE) {
175 lsa.addName(ndn::Name(prefix).appendNumber(++nPrefixes));
176 }
177
178 std::string expectedDataContent = lsa.getData();
179 lsdb.installNameLsa(lsa);
180
181 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/name/");
182 interestName.appendNumber(seqNo);
183
184 ndn::Interest interest(interestName);
185
186 lsdb.processInterest(ndn::Name(), interest);
187 face->processEvents(ndn::time::milliseconds(1));
188
189 std::vector<ndn::Data> data = face->sentData;
190 std::string recvDataContent;
191 for (unsigned int i = 0; i < data.size(); ++i)
192 {
193 const ndn::Block& nameBlock = data[i].getContent();
194
195 std::string nameBlockContent(reinterpret_cast<char const*>(nameBlock.value()),
196 nameBlock.value_size());
197 recvDataContent += nameBlockContent;
198 }
199
200 BOOST_CHECK_EQUAL(expectedDataContent, recvDataContent);
201}
202
203BOOST_AUTO_TEST_CASE(ReceiveSegmentedLsaData)
204{
205 ndn::Name router("/ndn/cs/%C1.Router/router1");
206 uint64_t seqNo = 12;
207 NamePrefixList prefixList;
208
209 NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList);
210
211 ndn::Name prefix("/prefix/");
212
213 for (int nPrefixes = 0; nPrefixes < 3; ++nPrefixes) {
214 lsa.addName(ndn::Name(prefix).appendNumber(nPrefixes));
215 }
216
217 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/name/");
218 interestName.appendNumber(seqNo);
219
dmcoomes9f936662017-03-02 10:33:09 -0600220 const ndn::ConstBufferPtr bufferPtr = std::make_shared<ndn::Buffer>(lsa.getData().c_str(),
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500221 lsa.getData().size());
222 lsdb.afterFetchLsa(bufferPtr, interestName);
223
224 NameLsa* foundLsa = lsdb.findNameLsa(lsa.getKey());
225 BOOST_REQUIRE(foundLsa != nullptr);
226
227 BOOST_CHECK_EQUAL(foundLsa->getData(), lsa.getData());
228}
229
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500230BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
231{
akmhoquec7a79b22014-05-26 08:06:19 -0500232 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500233 NamePrefixList npl1;
234
akmhoquefdbddb12014-05-02 18:35:19 -0500235 std::string s1 = "name1";
236 std::string s2 = "name2";
237 std::string router1 = "router1/1";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500238
239 npl1.insert(s1);
240 npl1.insert(s2);
241
Vince Lehman904c2412014-09-23 19:36:11 -0500242 //For NameLsa lsType is name.
243 //12 is seqNo, randomly generated.
244 //1800 is the default life time.
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600245 NameLsa nlsa1(ndn::Name("/router1/1"), 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500246
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500247 Lsdb lsdb1(nlsr, g_scheduler);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500248
akmhoque31d1d4b2014-05-05 22:08:14 -0500249 lsdb1.installNameLsa(nlsa1);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700250 lsdb1.writeNameLsdbLog();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500251
alvy49b1c0c2014-12-19 13:57:46 -0600252 BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/name"), NameLsa::TYPE_STRING));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500253
akmhoque31d1d4b2014-05-05 22:08:14 -0500254 lsdb1.removeNameLsa(router1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500255
alvy49b1c0c2014-12-19 13:57:46 -0600256 BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), NameLsa::TYPE_STRING), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500257}
258
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500259BOOST_AUTO_TEST_CASE(InstallNameLsa)
260{
261 // Install lsa with name1 and name2
262 ndn::Name name1("/ndn/name1");
263 ndn::Name name2("/ndn/name2");
264
265 NamePrefixList prefixes;
266 prefixes.insert(name1);
267 prefixes.insert(name2);
268
269 std::string otherRouter("/ndn/site/%C1.router/other-router");
270 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
271
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600272 NameLsa lsa(otherRouter, 1, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500273 lsdb.installNameLsa(lsa);
274
alvy49b1c0c2014-12-19 13:57:46 -0600275 BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter + "/name", NameLsa::TYPE_STRING), true);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500276 NamePrefixList& nameList = lsdb.findNameLsa(otherRouter + "/name")->getNpl();
277
Nick Gordonf14ec352017-07-24 16:09:58 -0500278 BOOST_CHECK_EQUAL(nameList, prefixes);
279 //areNamePrefixListsEqual(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500280
281 // Add a prefix: name3
282 ndn::Name name3("/ndn/name3");
283 prefixes.insert(name3);
284
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600285 NameLsa addLsa(otherRouter, 2, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500286 lsdb.installNameLsa(addLsa);
287
288 // Lsa should include name1, name2, and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500289 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500290
291 // Remove a prefix: name2
292 prefixes.remove(name2);
293
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600294 NameLsa removeLsa(otherRouter, 3, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500295 lsdb.installNameLsa(removeLsa);
296
297 // Lsa should include name1 and name3
Nick Gordonf14ec352017-07-24 16:09:58 -0500298 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500299
300 // Add and remove a prefix: add name2, remove name3
301 prefixes.insert(name2);
302 prefixes.remove(name3);
303
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600304 NameLsa addAndRemoveLsa(otherRouter, 4, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500305 lsdb.installNameLsa(addAndRemoveLsa);
306
307 // Lsa should include name1 and name2
Nick Gordonf14ec352017-07-24 16:09:58 -0500308 BOOST_CHECK_EQUAL(nameList, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500309
310 // Install a completely new list of prefixes
311 ndn::Name name4("/ndn/name4");
312 ndn::Name name5("/ndn/name5");
313
314 NamePrefixList newPrefixes;
315 newPrefixes.insert(name4);
316 newPrefixes.insert(name5);
317
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600318 NameLsa newLsa(otherRouter, 5, MAX_TIME, newPrefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500319 lsdb.installNameLsa(newLsa);
320
321 // Lsa should include name4 and name5
Nick Gordonf14ec352017-07-24 16:09:58 -0500322 BOOST_CHECK_EQUAL(nameList, newPrefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500323}
324
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500325BOOST_AUTO_TEST_SUITE_END()
326
Nick Gordonfad8e252016-08-11 14:21:38 -0500327} // namespace test
328} // namespace nlsr