blob: d0508e1d2e6a079cd71a3425464c3d7dff83e5c1 [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/**
akmhoque3d06e792014-05-27 16:23:20 -05003 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author Ashlesh Gawande <agawande@memphis.edu>
21 *
22 **/
Vince Lehman7c603292014-09-11 17:48:16 -050023
24#include "test-common.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050025#include "dummy-face.hpp"
Vince Lehman7c603292014-09-11 17:48:16 -050026
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050027#include "lsdb.hpp"
28#include "nlsr.hpp"
29#include "lsa.hpp"
30#include "name-prefix-list.hpp"
31#include <boost/test/unit_test.hpp>
32
33namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050034namespace test {
35
Vince Lehman904c2412014-09-23 19:36:11 -050036using ndn::DummyFace;
37using ndn::shared_ptr;
38
39class LsdbFixture : public BaseFixture
40{
41public:
42 LsdbFixture()
43 : face(ndn::makeDummyFace())
44 , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
Vince Lehman0bcf9a32014-12-10 11:24:45 -060045 , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter(), nlsr.getSequencingManager())
Vince Lehmanf1aa5232014-10-06 17:57:35 -050046 , lsdb(nlsr.getLsdb())
47 , conf(nlsr.getConfParameter())
Vince Lehman904c2412014-09-23 19:36:11 -050048 , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib")
49 , REGISTER_VERB("register")
50 {
Vince Lehmanf1aa5232014-10-06 17:57:35 -050051 conf.setNetwork("/ndn");
52 conf.setSiteName("/site");
53 conf.setRouterName("/%C1.router/this-router");
54
55 nlsr.initialize();
56
57 face->processEvents(ndn::time::milliseconds(1));
58 face->m_sentInterests.clear();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -050059
60 INIT_LOGGERS("/tmp", "DEBUG");
Vince Lehman904c2412014-09-23 19:36:11 -050061 }
62
Vince Lehmanf1aa5232014-10-06 17:57:35 -050063 void
64 extractParameters(ndn::Interest& interest, ndn::Name::Component& verb,
65 ndn::nfd::ControlParameters& extractedParameters)
Vince Lehman904c2412014-09-23 19:36:11 -050066 {
67 const ndn::Name& name = interest.getName();
68 verb = name[REGISTER_COMMAND_PREFIX.size()];
69 const ndn::Name::Component& parameterComponent = name[REGISTER_COMMAND_PREFIX.size() + 1];
70
71 ndn::Block rawParameters = parameterComponent.blockFromValue();
72 extractedParameters.wireDecode(rawParameters);
73 }
74
Vince Lehmanf1aa5232014-10-06 17:57:35 -050075 void
76 areNamePrefixListsEqual(NamePrefixList& lhs, NamePrefixList& rhs)
77 {
78 typedef std::list<ndn::Name> NameList;
79
80 NameList& lhsList = lhs.getNameList();
81 NameList& rhsList = rhs.getNameList();
82
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:
94 shared_ptr<DummyFace> face;
95 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
118 std::vector<ndn::Interest>& interests = face->m_sentInterests;
119
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() +
127 ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
128
129 // Simulate an LSA interest timeout
130 lsdb.processInterestTimedOut(oldInterestName, 0, deadline, interestName, oldSeqNo);
131 face->processEvents(ndn::time::milliseconds(1));
132
133 BOOST_REQUIRE(interests.size() > 0);
134 it = interests.begin();
135
136 BOOST_CHECK_EQUAL(it->getName(), oldInterestName);
137 interests.clear();
138
139 uint64_t newSeqNo = 83;
140
141 ndn::Name newInterestName = interestName;
142 newInterestName.appendNumber(newSeqNo);
143
144 lsdb.expressInterest(newInterestName, 0);
145 face->processEvents(ndn::time::milliseconds(1));
146
147 BOOST_REQUIRE(interests.size() > 0);
148 it = interests.begin();
149
150 BOOST_CHECK_EQUAL(it->getName(), newInterestName);
151 interests.clear();
152
153 // Simulate an LSA interest timeout where the sequence number is outdated
154 lsdb.processInterestTimedOut(oldInterestName, 0, deadline, interestName, oldSeqNo);
155 face->processEvents(ndn::time::milliseconds(1));
156
157 // Interest should not be expressed for outdated sequence number
158 BOOST_CHECK_EQUAL(interests.size(), 0);
159}
160
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500161BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
162{
akmhoquec7a79b22014-05-26 08:06:19 -0500163 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500164 NamePrefixList npl1;
165
akmhoquefdbddb12014-05-02 18:35:19 -0500166 std::string s1 = "name1";
167 std::string s2 = "name2";
168 std::string router1 = "router1/1";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500169
170 npl1.insert(s1);
171 npl1.insert(s2);
172
Vince Lehman904c2412014-09-23 19:36:11 -0500173 //For NameLsa lsType is name.
174 //12 is seqNo, randomly generated.
175 //1800 is the default life time.
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700176 NameLsa nlsa1(ndn::Name("/router1/1"), std::string("name"), 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500177
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600178 Lsdb lsdb1(nlsr, g_scheduler, nlsr.getSyncLogicHandler());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500179
akmhoque31d1d4b2014-05-05 22:08:14 -0500180 lsdb1.installNameLsa(nlsa1);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700181 lsdb1.writeNameLsdbLog();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500182
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700183 BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/name"), std::string("name")));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500184
akmhoque31d1d4b2014-05-05 22:08:14 -0500185 lsdb1.removeNameLsa(router1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500186
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700187 BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), std::string("name")), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500188}
189
Vince Lehman904c2412014-09-23 19:36:11 -0500190BOOST_AUTO_TEST_CASE(RegisterSyncPrefixOnFirstAdjLsaBuild)
191{
Vince Lehman904c2412014-09-23 19:36:11 -0500192 // Should register Sync prefix
193 lsdb.buildAndInstallOwnAdjLsa();
194 face->processEvents(ndn::time::milliseconds(1));
195
196 std::vector<ndn::Interest>& interests = face->m_sentInterests;
197
198 BOOST_REQUIRE(interests.size() > 0);
199
200 ndn::nfd::ControlParameters extractedParameters;
201 ndn::Name::Component verb;
202 extractParameters(interests[0], verb, extractedParameters);
203
204 BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
205 BOOST_CHECK_EQUAL(extractedParameters.getName(), conf.getChronosyncPrefix());
206
207 // Should not register Sync prefix
208 face->m_sentInterests.clear();
209 lsdb.buildAndInstallOwnAdjLsa();
210 face->processEvents(ndn::time::milliseconds(1));
211
212 BOOST_CHECK_EQUAL(interests.size(), 0);
213}
214
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500215BOOST_AUTO_TEST_CASE(InstallNameLsa)
216{
217 // Install lsa with name1 and name2
218 ndn::Name name1("/ndn/name1");
219 ndn::Name name2("/ndn/name2");
220
221 NamePrefixList prefixes;
222 prefixes.insert(name1);
223 prefixes.insert(name2);
224
225 std::string otherRouter("/ndn/site/%C1.router/other-router");
226 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
227
228 NameLsa lsa(otherRouter, "name", 1, MAX_TIME, prefixes);
229 lsdb.installNameLsa(lsa);
230
231 BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter + "/name", "name"), true);
232 NamePrefixList& nameList = lsdb.findNameLsa(otherRouter + "/name")->getNpl();
233
234 areNamePrefixListsEqual(nameList, prefixes);
235
236 // Add a prefix: name3
237 ndn::Name name3("/ndn/name3");
238 prefixes.insert(name3);
239
240 NameLsa addLsa(otherRouter, "name", 2, MAX_TIME, prefixes);
241 lsdb.installNameLsa(addLsa);
242
243 // Lsa should include name1, name2, and name3
244 areNamePrefixListsEqual(nameList, prefixes);
245
246 // Remove a prefix: name2
247 prefixes.remove(name2);
248
249 NameLsa removeLsa(otherRouter, "name", 3, MAX_TIME, prefixes);
250 lsdb.installNameLsa(removeLsa);
251
252 // Lsa should include name1 and name3
253 areNamePrefixListsEqual(nameList, prefixes);
254
255 // Add and remove a prefix: add name2, remove name3
256 prefixes.insert(name2);
257 prefixes.remove(name3);
258
259 NameLsa addAndRemoveLsa(otherRouter, "name", 4, MAX_TIME, prefixes);
260 lsdb.installNameLsa(addAndRemoveLsa);
261
262 // Lsa should include name1 and name2
263 areNamePrefixListsEqual(nameList, prefixes);
264
265 // Install a completely new list of prefixes
266 ndn::Name name4("/ndn/name4");
267 ndn::Name name5("/ndn/name5");
268
269 NamePrefixList newPrefixes;
270 newPrefixes.insert(name4);
271 newPrefixes.insert(name5);
272
273 NameLsa newLsa(otherRouter, "name", 5, MAX_TIME, newPrefixes);
274 lsdb.installNameLsa(newLsa);
275
276 // Lsa should include name4 and name5
277 areNamePrefixListsEqual(nameList, newPrefixes);
278}
279
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500280BOOST_AUTO_TEST_SUITE_END()
281
282} //namespace test
283} //namespace nlsr