blob: 7111620eba0da02e46c3288d80eb451e6886b656 [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/**
Vince Lehmanc2e51f62015-01-20 15:03:11 -06003 * Copyright (c) 2014-2015, The University of Memphis,
4 * 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>
31
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050032namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050033namespace test {
34
Vince Lehman904c2412014-09-23 19:36:11 -050035using ndn::shared_ptr;
36
37class LsdbFixture : public BaseFixture
38{
39public:
40 LsdbFixture()
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060041 : face(make_shared<ndn::util::DummyClientFace>())
Vince Lehman904c2412014-09-23 19:36:11 -050042 , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
Vince Lehman0bcf9a32014-12-10 11:24:45 -060043 , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter(), nlsr.getSequencingManager())
Vince Lehmanf1aa5232014-10-06 17:57:35 -050044 , lsdb(nlsr.getLsdb())
45 , conf(nlsr.getConfParameter())
Vince Lehman904c2412014-09-23 19:36:11 -050046 , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib")
47 , REGISTER_VERB("register")
48 {
Vince Lehmanf1aa5232014-10-06 17:57:35 -050049 conf.setNetwork("/ndn");
50 conf.setSiteName("/site");
51 conf.setRouterName("/%C1.router/this-router");
52
53 nlsr.initialize();
54
55 face->processEvents(ndn::time::milliseconds(1));
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060056 face->sentInterests.clear();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -050057
58 INIT_LOGGERS("/tmp", "DEBUG");
Vince Lehman904c2412014-09-23 19:36:11 -050059 }
60
Vince Lehmanf1aa5232014-10-06 17:57:35 -050061 void
62 extractParameters(ndn::Interest& interest, ndn::Name::Component& verb,
63 ndn::nfd::ControlParameters& extractedParameters)
Vince Lehman904c2412014-09-23 19:36:11 -050064 {
65 const ndn::Name& name = interest.getName();
66 verb = name[REGISTER_COMMAND_PREFIX.size()];
67 const ndn::Name::Component& parameterComponent = name[REGISTER_COMMAND_PREFIX.size() + 1];
68
69 ndn::Block rawParameters = parameterComponent.blockFromValue();
70 extractedParameters.wireDecode(rawParameters);
71 }
72
Vince Lehmanf1aa5232014-10-06 17:57:35 -050073 void
74 areNamePrefixListsEqual(NamePrefixList& lhs, NamePrefixList& rhs)
75 {
76 typedef std::list<ndn::Name> NameList;
77
78 NameList& lhsList = lhs.getNameList();
79 NameList& rhsList = rhs.getNameList();
80
81 BOOST_REQUIRE_EQUAL(lhsList.size(), rhsList.size());
82
83 NameList::iterator i = lhsList.begin();
84 NameList::iterator j = rhsList.begin();
85
86 for (; i != lhsList.end(); ++i, ++j) {
87 BOOST_CHECK_EQUAL(*i, *j);
88 }
89 }
90
Vince Lehman904c2412014-09-23 19:36:11 -050091public:
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060092 shared_ptr<ndn::util::DummyClientFace> face;
Vince Lehman904c2412014-09-23 19:36:11 -050093 Nlsr nlsr;
94 SyncLogicHandler sync;
95
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);
114 face->processEvents(ndn::time::milliseconds(1));
115
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -0600116 std::vector<ndn::Interest>& interests = face->sentInterests;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500117
118 BOOST_REQUIRE(interests.size() > 0);
119 std::vector<ndn::Interest>::iterator it = interests.begin();
120
121 BOOST_CHECK_EQUAL(it->getName(), oldInterestName);
122 interests.clear();
123
124 steady_clock::TimePoint deadline = steady_clock::now() +
125 ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
126
127 // Simulate an LSA interest timeout
128 lsdb.processInterestTimedOut(oldInterestName, 0, deadline, interestName, oldSeqNo);
129 face->processEvents(ndn::time::milliseconds(1));
130
131 BOOST_REQUIRE(interests.size() > 0);
132 it = interests.begin();
133
134 BOOST_CHECK_EQUAL(it->getName(), oldInterestName);
135 interests.clear();
136
137 uint64_t newSeqNo = 83;
138
139 ndn::Name newInterestName = interestName;
140 newInterestName.appendNumber(newSeqNo);
141
142 lsdb.expressInterest(newInterestName, 0);
143 face->processEvents(ndn::time::milliseconds(1));
144
145 BOOST_REQUIRE(interests.size() > 0);
146 it = interests.begin();
147
148 BOOST_CHECK_EQUAL(it->getName(), newInterestName);
149 interests.clear();
150
151 // Simulate an LSA interest timeout where the sequence number is outdated
152 lsdb.processInterestTimedOut(oldInterestName, 0, deadline, interestName, oldSeqNo);
153 face->processEvents(ndn::time::milliseconds(1));
154
155 // Interest should not be expressed for outdated sequence number
156 BOOST_CHECK_EQUAL(interests.size(), 0);
157}
158
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500159BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
160{
akmhoquec7a79b22014-05-26 08:06:19 -0500161 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500162 NamePrefixList npl1;
163
akmhoquefdbddb12014-05-02 18:35:19 -0500164 std::string s1 = "name1";
165 std::string s2 = "name2";
166 std::string router1 = "router1/1";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500167
168 npl1.insert(s1);
169 npl1.insert(s2);
170
Vince Lehman904c2412014-09-23 19:36:11 -0500171 //For NameLsa lsType is name.
172 //12 is seqNo, randomly generated.
173 //1800 is the default life time.
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600174 NameLsa nlsa1(ndn::Name("/router1/1"), 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500175
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600176 Lsdb lsdb1(nlsr, g_scheduler, nlsr.getSyncLogicHandler());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500177
akmhoque31d1d4b2014-05-05 22:08:14 -0500178 lsdb1.installNameLsa(nlsa1);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700179 lsdb1.writeNameLsdbLog();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500180
alvy49b1c0c2014-12-19 13:57:46 -0600181 BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/name"), NameLsa::TYPE_STRING));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500182
akmhoque31d1d4b2014-05-05 22:08:14 -0500183 lsdb1.removeNameLsa(router1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500184
alvy49b1c0c2014-12-19 13:57:46 -0600185 BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), NameLsa::TYPE_STRING), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500186}
187
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500188BOOST_AUTO_TEST_CASE(InstallNameLsa)
189{
190 // Install lsa with name1 and name2
191 ndn::Name name1("/ndn/name1");
192 ndn::Name name2("/ndn/name2");
193
194 NamePrefixList prefixes;
195 prefixes.insert(name1);
196 prefixes.insert(name2);
197
198 std::string otherRouter("/ndn/site/%C1.router/other-router");
199 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
200
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600201 NameLsa lsa(otherRouter, 1, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500202 lsdb.installNameLsa(lsa);
203
alvy49b1c0c2014-12-19 13:57:46 -0600204 BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter + "/name", NameLsa::TYPE_STRING), true);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500205 NamePrefixList& nameList = lsdb.findNameLsa(otherRouter + "/name")->getNpl();
206
207 areNamePrefixListsEqual(nameList, prefixes);
208
209 // Add a prefix: name3
210 ndn::Name name3("/ndn/name3");
211 prefixes.insert(name3);
212
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600213 NameLsa addLsa(otherRouter, 2, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500214 lsdb.installNameLsa(addLsa);
215
216 // Lsa should include name1, name2, and name3
217 areNamePrefixListsEqual(nameList, prefixes);
218
219 // Remove a prefix: name2
220 prefixes.remove(name2);
221
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600222 NameLsa removeLsa(otherRouter, 3, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500223 lsdb.installNameLsa(removeLsa);
224
225 // Lsa should include name1 and name3
226 areNamePrefixListsEqual(nameList, prefixes);
227
228 // Add and remove a prefix: add name2, remove name3
229 prefixes.insert(name2);
230 prefixes.remove(name3);
231
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600232 NameLsa addAndRemoveLsa(otherRouter, 4, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500233 lsdb.installNameLsa(addAndRemoveLsa);
234
235 // Lsa should include name1 and name2
236 areNamePrefixListsEqual(nameList, prefixes);
237
238 // Install a completely new list of prefixes
239 ndn::Name name4("/ndn/name4");
240 ndn::Name name5("/ndn/name5");
241
242 NamePrefixList newPrefixes;
243 newPrefixes.insert(name4);
244 newPrefixes.insert(name5);
245
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600246 NameLsa newLsa(otherRouter, 5, MAX_TIME, newPrefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500247 lsdb.installNameLsa(newLsa);
248
249 // Lsa should include name4 and name5
250 areNamePrefixListsEqual(nameList, newPrefixes);
251}
252
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500253BOOST_AUTO_TEST_SUITE_END()
254
255} //namespace test
256} //namespace nlsr