blob: 1585a4d67400a9359d1f66c94829401589d2472b [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"
Vince Lehman904c2412014-09-23 19:36:11 -050023#include "dummy-face.hpp"
Vince Lehman7c603292014-09-11 17:48:16 -050024
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050025#include "lsdb.hpp"
26#include "nlsr.hpp"
27#include "lsa.hpp"
28#include "name-prefix-list.hpp"
29#include <boost/test/unit_test.hpp>
30
31namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050032namespace test {
33
Vince Lehman904c2412014-09-23 19:36:11 -050034using ndn::DummyFace;
35using ndn::shared_ptr;
36
37class LsdbFixture : public BaseFixture
38{
39public:
40 LsdbFixture()
41 : face(ndn::makeDummyFace())
42 , 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));
56 face->m_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:
92 shared_ptr<DummyFace> face;
93 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
116 std::vector<ndn::Interest>& interests = face->m_sentInterests;
117
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.
alvy49b1c0c2014-12-19 13:57:46 -0600174 NameLsa nlsa1(ndn::Name("/router1/1"), NameLsa::TYPE_STRING, 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 Lehman904c2412014-09-23 19:36:11 -0500188BOOST_AUTO_TEST_CASE(RegisterSyncPrefixOnFirstAdjLsaBuild)
189{
Vince Lehman904c2412014-09-23 19:36:11 -0500190 // Should register Sync prefix
191 lsdb.buildAndInstallOwnAdjLsa();
192 face->processEvents(ndn::time::milliseconds(1));
193
194 std::vector<ndn::Interest>& interests = face->m_sentInterests;
195
196 BOOST_REQUIRE(interests.size() > 0);
197
198 ndn::nfd::ControlParameters extractedParameters;
199 ndn::Name::Component verb;
200 extractParameters(interests[0], verb, extractedParameters);
201
202 BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
203 BOOST_CHECK_EQUAL(extractedParameters.getName(), conf.getChronosyncPrefix());
204
205 // Should not register Sync prefix
206 face->m_sentInterests.clear();
207 lsdb.buildAndInstallOwnAdjLsa();
208 face->processEvents(ndn::time::milliseconds(1));
209
210 BOOST_CHECK_EQUAL(interests.size(), 0);
211}
212
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500213BOOST_AUTO_TEST_CASE(InstallNameLsa)
214{
215 // Install lsa with name1 and name2
216 ndn::Name name1("/ndn/name1");
217 ndn::Name name2("/ndn/name2");
218
219 NamePrefixList prefixes;
220 prefixes.insert(name1);
221 prefixes.insert(name2);
222
223 std::string otherRouter("/ndn/site/%C1.router/other-router");
224 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
225
alvy49b1c0c2014-12-19 13:57:46 -0600226 NameLsa lsa(otherRouter, NameLsa::TYPE_STRING, 1, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500227 lsdb.installNameLsa(lsa);
228
alvy49b1c0c2014-12-19 13:57:46 -0600229 BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter + "/name", NameLsa::TYPE_STRING), true);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500230 NamePrefixList& nameList = lsdb.findNameLsa(otherRouter + "/name")->getNpl();
231
232 areNamePrefixListsEqual(nameList, prefixes);
233
234 // Add a prefix: name3
235 ndn::Name name3("/ndn/name3");
236 prefixes.insert(name3);
237
alvy49b1c0c2014-12-19 13:57:46 -0600238 NameLsa addLsa(otherRouter, NameLsa::TYPE_STRING, 2, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500239 lsdb.installNameLsa(addLsa);
240
241 // Lsa should include name1, name2, and name3
242 areNamePrefixListsEqual(nameList, prefixes);
243
244 // Remove a prefix: name2
245 prefixes.remove(name2);
246
alvy49b1c0c2014-12-19 13:57:46 -0600247 NameLsa removeLsa(otherRouter, NameLsa::TYPE_STRING, 3, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500248 lsdb.installNameLsa(removeLsa);
249
250 // Lsa should include name1 and name3
251 areNamePrefixListsEqual(nameList, prefixes);
252
253 // Add and remove a prefix: add name2, remove name3
254 prefixes.insert(name2);
255 prefixes.remove(name3);
256
alvy49b1c0c2014-12-19 13:57:46 -0600257 NameLsa addAndRemoveLsa(otherRouter, NameLsa::TYPE_STRING, 4, MAX_TIME, prefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500258 lsdb.installNameLsa(addAndRemoveLsa);
259
260 // Lsa should include name1 and name2
261 areNamePrefixListsEqual(nameList, prefixes);
262
263 // Install a completely new list of prefixes
264 ndn::Name name4("/ndn/name4");
265 ndn::Name name5("/ndn/name5");
266
267 NamePrefixList newPrefixes;
268 newPrefixes.insert(name4);
269 newPrefixes.insert(name5);
270
alvy49b1c0c2014-12-19 13:57:46 -0600271 NameLsa newLsa(otherRouter, NameLsa::TYPE_STRING, 5, MAX_TIME, newPrefixes);
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500272 lsdb.installNameLsa(newLsa);
273
274 // Lsa should include name4 and name5
275 areNamePrefixListsEqual(nameList, newPrefixes);
276}
277
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500278BOOST_AUTO_TEST_SUITE_END()
279
280} //namespace test
281} //namespace nlsr