blob: 077a381f7e54e6cb118129d6d2208e394a33452b [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>
akmhoquec7a79b22014-05-26 08:06:19 -050032#include <ndn-cxx/util/time.hpp>
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050033
34namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050035namespace test {
36
Vince Lehman904c2412014-09-23 19:36:11 -050037using ndn::DummyFace;
38using ndn::shared_ptr;
39
40class LsdbFixture : public BaseFixture
41{
42public:
43 LsdbFixture()
44 : face(ndn::makeDummyFace())
45 , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
46 , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter())
Vince Lehmanf1aa5232014-10-06 17:57:35 -050047 , lsdb(nlsr.getLsdb())
48 , conf(nlsr.getConfParameter())
Vince Lehman904c2412014-09-23 19:36:11 -050049 , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib")
50 , REGISTER_VERB("register")
51 {
Vince Lehmanf1aa5232014-10-06 17:57:35 -050052 conf.setNetwork("/ndn");
53 conf.setSiteName("/site");
54 conf.setRouterName("/%C1.router/this-router");
55
56 nlsr.initialize();
57
58 face->processEvents(ndn::time::milliseconds(1));
59 face->m_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 {
77 typedef std::list<ndn::Name> NameList;
78
79 NameList& lhsList = lhs.getNameList();
80 NameList& rhsList = rhs.getNameList();
81
82 BOOST_REQUIRE_EQUAL(lhsList.size(), rhsList.size());
83
84 NameList::iterator i = lhsList.begin();
85 NameList::iterator j = rhsList.begin();
86
87 for (; i != lhsList.end(); ++i, ++j) {
88 BOOST_CHECK_EQUAL(*i, *j);
89 }
90 }
91
Vince Lehman904c2412014-09-23 19:36:11 -050092public:
93 shared_ptr<DummyFace> face;
94 Nlsr nlsr;
95 SyncLogicHandler sync;
96
Vince Lehmanf1aa5232014-10-06 17:57:35 -050097 Lsdb& lsdb;
98 ConfParameter& conf;
99
Vince Lehman904c2412014-09-23 19:36:11 -0500100 ndn::Name REGISTER_COMMAND_PREFIX;
101 ndn::Name::Component REGISTER_VERB;
102};
103
104BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500105
106BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
107{
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500108 INIT_LOGGERS("/tmp", "DEBUG");
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700109
akmhoquec7a79b22014-05-26 08:06:19 -0500110 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500111 NamePrefixList npl1;
112
akmhoquefdbddb12014-05-02 18:35:19 -0500113 std::string s1 = "name1";
114 std::string s2 = "name2";
115 std::string router1 = "router1/1";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500116
117 npl1.insert(s1);
118 npl1.insert(s2);
119
Vince Lehman904c2412014-09-23 19:36:11 -0500120 //For NameLsa lsType is name.
121 //12 is seqNo, randomly generated.
122 //1800 is the default life time.
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700123 NameLsa nlsa1(ndn::Name("/router1/1"), std::string("name"), 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500124
Vince Lehman904c2412014-09-23 19:36:11 -0500125 Lsdb lsdb1(nlsr, g_scheduler);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500126
akmhoque31d1d4b2014-05-05 22:08:14 -0500127 lsdb1.installNameLsa(nlsa1);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700128 lsdb1.writeNameLsdbLog();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500129
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700130 BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/name"), std::string("name")));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500131
akmhoque31d1d4b2014-05-05 22:08:14 -0500132 lsdb1.removeNameLsa(router1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500133
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700134 BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), std::string("name")), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500135}
136
Vince Lehman904c2412014-09-23 19:36:11 -0500137BOOST_AUTO_TEST_CASE(RegisterSyncPrefixOnFirstAdjLsaBuild)
138{
Vince Lehman904c2412014-09-23 19:36:11 -0500139 // Should register Sync prefix
140 lsdb.buildAndInstallOwnAdjLsa();
141 face->processEvents(ndn::time::milliseconds(1));
142
143 std::vector<ndn::Interest>& interests = face->m_sentInterests;
144
145 BOOST_REQUIRE(interests.size() > 0);
146
147 ndn::nfd::ControlParameters extractedParameters;
148 ndn::Name::Component verb;
149 extractParameters(interests[0], verb, extractedParameters);
150
151 BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
152 BOOST_CHECK_EQUAL(extractedParameters.getName(), conf.getChronosyncPrefix());
153
154 // Should not register Sync prefix
155 face->m_sentInterests.clear();
156 lsdb.buildAndInstallOwnAdjLsa();
157 face->processEvents(ndn::time::milliseconds(1));
158
159 BOOST_CHECK_EQUAL(interests.size(), 0);
160}
161
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500162BOOST_AUTO_TEST_CASE(InstallNameLsa)
163{
164 // Install lsa with name1 and name2
165 ndn::Name name1("/ndn/name1");
166 ndn::Name name2("/ndn/name2");
167
168 NamePrefixList prefixes;
169 prefixes.insert(name1);
170 prefixes.insert(name2);
171
172 std::string otherRouter("/ndn/site/%C1.router/other-router");
173 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
174
175 NameLsa lsa(otherRouter, "name", 1, MAX_TIME, prefixes);
176 lsdb.installNameLsa(lsa);
177
178 BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter + "/name", "name"), true);
179 NamePrefixList& nameList = lsdb.findNameLsa(otherRouter + "/name")->getNpl();
180
181 areNamePrefixListsEqual(nameList, prefixes);
182
183 // Add a prefix: name3
184 ndn::Name name3("/ndn/name3");
185 prefixes.insert(name3);
186
187 NameLsa addLsa(otherRouter, "name", 2, MAX_TIME, prefixes);
188 lsdb.installNameLsa(addLsa);
189
190 // Lsa should include name1, name2, and name3
191 areNamePrefixListsEqual(nameList, prefixes);
192
193 // Remove a prefix: name2
194 prefixes.remove(name2);
195
196 NameLsa removeLsa(otherRouter, "name", 3, MAX_TIME, prefixes);
197 lsdb.installNameLsa(removeLsa);
198
199 // Lsa should include name1 and name3
200 areNamePrefixListsEqual(nameList, prefixes);
201
202 // Add and remove a prefix: add name2, remove name3
203 prefixes.insert(name2);
204 prefixes.remove(name3);
205
206 NameLsa addAndRemoveLsa(otherRouter, "name", 4, MAX_TIME, prefixes);
207 lsdb.installNameLsa(addAndRemoveLsa);
208
209 // Lsa should include name1 and name2
210 areNamePrefixListsEqual(nameList, prefixes);
211
212 // Install a completely new list of prefixes
213 ndn::Name name4("/ndn/name4");
214 ndn::Name name5("/ndn/name5");
215
216 NamePrefixList newPrefixes;
217 newPrefixes.insert(name4);
218 newPrefixes.insert(name5);
219
220 NameLsa newLsa(otherRouter, "name", 5, MAX_TIME, newPrefixes);
221 lsdb.installNameLsa(newLsa);
222
223 // Lsa should include name4 and name5
224 areNamePrefixListsEqual(nameList, newPrefixes);
225}
226
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500227BOOST_AUTO_TEST_SUITE_END()
228
229} //namespace test
230} //namespace nlsr