blob: 4407a557daa78b62c01d554b842dc7abfd73b49f [file] [log] [blame]
Vince Lehman09131122014-09-09 17:10:11 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehmanc2acdcb2015-04-29 11:14:35 -05003 * Copyright (c) 2014-2015, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
Vince Lehman09131122014-09-09 17:10:11 -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/>.
Vince Lehman09131122014-09-09 17:10:11 -050020 **/
21
Vince Lehman02e32992015-03-11 12:31:20 -050022#include "test-common.hpp"
Vince Lehman199e9cf2015-04-07 13:22:16 -050023#include "control-commands.hpp"
Vince Lehman02e32992015-03-11 12:31:20 -050024#include "dummy-face.hpp"
Vince Lehman09131122014-09-09 17:10:11 -050025
Vince Lehman02e32992015-03-11 12:31:20 -050026#include "nlsr.hpp"
27
28#include <ndn-cxx/management/nfd-face-event-notification.hpp>
29#include <ndn-cxx/util/dummy-client-face.hpp>
Vince Lehman09131122014-09-09 17:10:11 -050030
31namespace nlsr {
32namespace test {
33
34using ndn::DummyFace;
35using ndn::shared_ptr;
36
37BOOST_FIXTURE_TEST_SUITE(TestNlsr, BaseFixture)
38
39BOOST_AUTO_TEST_CASE(HyperbolicOn_ZeroCostNeighbors)
40{
41 shared_ptr<DummyFace> face = ndn::makeDummyFace();
42 Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
43
44 // Simulate loading configuration file
45 AdjacencyList& neighbors = nlsr.getAdjacencyList();
46
47 Adjacent neighborA("/ndn/neighborA", "uri://faceA", 25, Adjacent::STATUS_INACTIVE, 0, 0);
48 neighbors.insert(neighborA);
49
50 Adjacent neighborB("/ndn/neighborB", "uri://faceB", 10, Adjacent::STATUS_INACTIVE, 0, 0);
51 neighbors.insert(neighborB);
52
53 Adjacent neighborC("/ndn/neighborC", "uri://faceC", 17, Adjacent::STATUS_INACTIVE, 0, 0);
54 neighbors.insert(neighborC);
55
56 nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
57
58 nlsr.initialize();
59
60 std::list<Adjacent> neighborList = neighbors.getAdjList();
61 for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
62 BOOST_CHECK_EQUAL(it->getLinkCost(), 0);
63 }
64}
65
66BOOST_AUTO_TEST_CASE(HyperbolicOff_LinkStateCost)
67{
68 shared_ptr<DummyFace> face = ndn::makeDummyFace();
69 Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
70
71 // Simulate loading configuration file
72 AdjacencyList& neighbors = nlsr.getAdjacencyList();
73
74 Adjacent neighborA("/ndn/neighborA", "uri://faceA", 25, Adjacent::STATUS_INACTIVE, 0, 0);
75 neighbors.insert(neighborA);
76
77 Adjacent neighborB("/ndn/neighborB", "uri://faceB", 10, Adjacent::STATUS_INACTIVE, 0, 0);
78 neighbors.insert(neighborB);
79
80 Adjacent neighborC("/ndn/neighborC", "uri://faceC", 17, Adjacent::STATUS_INACTIVE, 0, 0);
81 neighbors.insert(neighborC);
82
83 nlsr.initialize();
84
85 std::list<Adjacent> neighborList = neighbors.getAdjList();
86 for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
87 BOOST_CHECK(it->getLinkCost() != 0);
88 }
89}
90
Vince Lehman7b616582014-10-17 16:25:39 -050091BOOST_AUTO_TEST_CASE(SetEventIntervals)
92{
93 shared_ptr<DummyFace> face = ndn::makeDummyFace();
94 Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
95
96 // Simulate loading configuration file
97 ConfParameter& conf = nlsr.getConfParameter();
98 conf.setAdjLsaBuildInterval(3);
99 conf.setFirstHelloInterval(6);
100 conf.setRoutingCalcInterval(9);
101
102 nlsr.initialize();
103
Vince Lehman50df6b72015-03-03 12:06:40 -0600104 const Lsdb& lsdb = nlsr.getLsdb();
Vince Lehman7b616582014-10-17 16:25:39 -0500105 const RoutingTable& rt = nlsr.getRoutingTable();
106
Vince Lehman50df6b72015-03-03 12:06:40 -0600107 BOOST_CHECK_EQUAL(lsdb.getAdjLsaBuildInterval(), ndn::time::seconds(3));
Vince Lehman7b616582014-10-17 16:25:39 -0500108 BOOST_CHECK_EQUAL(nlsr.getFirstHelloInterval(), 6);
109 BOOST_CHECK_EQUAL(rt.getRoutingCalcInterval(), ndn::time::seconds(9));
110}
111
Vince Lehman02e32992015-03-11 12:31:20 -0500112BOOST_FIXTURE_TEST_CASE(FaceDestroyEvent, UnitTestTimeFixture)
113{
114 shared_ptr<ndn::util::DummyClientFace> face = ndn::util::makeDummyClientFace(g_ioService);
115 Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
Vince Lehman41b173e2015-05-07 14:13:26 -0500116 Lsdb& lsdb = nlsr.getLsdb();
Vince Lehman02e32992015-03-11 12:31:20 -0500117
118 // Simulate loading configuration file
119 ConfParameter& conf = nlsr.getConfParameter();
120 conf.setNetwork("/ndn");
121 conf.setSiteName("/site");
122 conf.setRouterName("/%C1.router/this-router");
123 conf.setAdjLsaBuildInterval(0);
124 conf.setRoutingCalcInterval(0);
125
126 // Add active neighbors
127 AdjacencyList& neighbors = nlsr.getAdjacencyList();
128
129 uint64_t destroyFaceId = 128;
130
131 // Create a neighbor whose Face will be destroyed
132 Adjacent failNeighbor("/ndn/neighborA", "uri://faceA", 10, Adjacent::STATUS_ACTIVE, 0,
133 destroyFaceId);
134 neighbors.insert(failNeighbor);
135
136 // Create an additional neighbor so an adjacency LSA can be built after the face is destroyed
Vince Lehman41b173e2015-05-07 14:13:26 -0500137 Adjacent otherNeighbor("/ndn/neighborB", "uri://faceB", 10, Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman02e32992015-03-11 12:31:20 -0500138 neighbors.insert(otherNeighbor);
139
140 nlsr.initialize();
141
142 // Simulate successful HELLO responses
Vince Lehman41b173e2015-05-07 14:13:26 -0500143 lsdb.scheduleAdjLsaBuild();
144
145 // Set up adjacency LSAs
146 // This router
147 Adjacent thisRouter(conf.getRouterPrefix(), "uri://faceB", 10, Adjacent::STATUS_ACTIVE, 0, 256);
148
149 AdjLsa ownAdjLsa(conf.getRouterPrefix(), AdjLsa::TYPE_STRING, 10, ndn::time::system_clock::now(),
150 1, neighbors);
151 lsdb.installAdjLsa(ownAdjLsa);
152
153 // Router that will fail
154 AdjacencyList failAdjacencies;
155 failAdjacencies.insert(thisRouter);
156
157 AdjLsa failAdjLsa("/ndn/neighborA", AdjLsa::TYPE_STRING, 10,
158 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, failAdjacencies);
159
160 lsdb.installAdjLsa(failAdjLsa);
161
162 // Other router
163 AdjacencyList otherAdjacencies;
164 otherAdjacencies.insert(thisRouter);
165
166 AdjLsa otherAdjLsa("/ndn/neighborB", AdjLsa::TYPE_STRING, 10,
167 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, otherAdjacencies);
168
169 lsdb.installAdjLsa(otherAdjLsa);
Vince Lehman02e32992015-03-11 12:31:20 -0500170
171 // Run the scheduler to build an adjacency LSA
172 this->advanceClocks(ndn::time::milliseconds(1));
173
174 // Make sure an adjacency LSA was built
175 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING);
Vince Lehman41b173e2015-05-07 14:13:26 -0500176 AdjLsa* lsa = lsdb.findAdjLsa(key);
Vince Lehman02e32992015-03-11 12:31:20 -0500177 BOOST_REQUIRE(lsa != nullptr);
178
alvy46ccaae2015-06-25 14:13:39 -0500179 uint32_t lastAdjLsaSeqNo = lsa->getLsSeqNo();
180 nlsr.getSequencingManager().setAdjLsaSeq(lastAdjLsaSeqNo);
Vince Lehman02e32992015-03-11 12:31:20 -0500181
182 // Make sure the routing table was calculated
183 RoutingTableEntry* rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
184 BOOST_REQUIRE(rtEntry != nullptr);
185 BOOST_REQUIRE_EQUAL(rtEntry->getNexthopList().getSize(), 1);
186
187 // Receive FaceEventDestroyed notification
188 ndn::nfd::FaceEventNotification event;
189 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
190 .setFaceId(destroyFaceId);
191
192 shared_ptr<ndn::Data> data = make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
193 data->setContent(event.wireEncode());
194 nlsr.getKeyChain().sign(*data);
195
196 face->receive(*data);
197
198 // Run the scheduler to build an adjacency LSA
199 this->advanceClocks(ndn::time::milliseconds(1));
200
201 Adjacent updatedNeighbor = neighbors.getAdjacent(failNeighbor.getName());
202
203 BOOST_CHECK_EQUAL(updatedNeighbor.getFaceId(), 0);
204 BOOST_CHECK_EQUAL(updatedNeighbor.getInterestTimedOutNo(),
205 nlsr.getConfParameter().getInterestRetryNumber());
206 BOOST_CHECK_EQUAL(updatedNeighbor.getStatus(), Adjacent::STATUS_INACTIVE);
alvy46ccaae2015-06-25 14:13:39 -0500207
208 lsa = lsdb.findAdjLsa(key);
209 BOOST_REQUIRE(lsa != nullptr);
210
211 BOOST_CHECK_EQUAL(lsa->getLsSeqNo(), lastAdjLsaSeqNo + 1);
Vince Lehman02e32992015-03-11 12:31:20 -0500212
213 // Make sure the routing table was recalculated
214 rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
215 BOOST_CHECK(rtEntry == nullptr);
216}
217
Vince Lehman199e9cf2015-04-07 13:22:16 -0500218// Bug #2733
219// This test checks that when a face for an inactive node is destroyed, an
220// Adjacency LSA build does not postpone the LSA refresh and cause RIB
221// entries for other nodes' name prefixes to not be refreshed.
222//
223// This test is invalid when Issue #2732 is implemented since an Adjacency LSA
224// refresh will not cause RIB entries for other nodes' name prefixes to be refreshed.
225BOOST_FIXTURE_TEST_CASE(FaceDestroyEventInactive, UnitTestTimeFixture)
226{
227 shared_ptr<ndn::util::DummyClientFace> face = ndn::util::makeDummyClientFace(g_ioService);
228 Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
229 Lsdb& lsdb = nlsr.getLsdb();
230
231 // Simulate loading configuration file
232 ConfParameter& conf = nlsr.getConfParameter();
233 conf.setNetwork("/ndn");
234 conf.setSiteName("/site");
235 conf.setRouterName("/%C1.router/this-router");
236 conf.setFirstHelloInterval(0);
237 conf.setAdjLsaBuildInterval(0);
238 conf.setRoutingCalcInterval(0);
239
240 // Add neighbors
241 AdjacencyList& neighbors = nlsr.getAdjacencyList();
242
243 uint64_t destroyFaceId = 128;
244
245 // Create an inactive neighbor whose Face will be destroyed
246 Adjacent failNeighbor("/ndn/neighborA", "uri://faceA", 10, Adjacent::STATUS_INACTIVE, 3,
247 destroyFaceId);
248 neighbors.insert(failNeighbor);
249
250 // Create an additional active neighbor so an adjacency LSA can be built
251 Adjacent otherNeighbor("/ndn/neighborB", "uri://faceB", 25, Adjacent::STATUS_ACTIVE, 0, 256);
252 neighbors.insert(otherNeighbor);
253
254 // Add a name for the neighbor to advertise
255 NamePrefixList nameList;
256 ndn::Name nameToAdvertise("/ndn/neighborB/name");
257 nameList.insert(nameToAdvertise);
258
259 NameLsa nameLsa("/ndn/neighborB", "name", 25, ndn::time::system_clock::now(), nameList);
260 lsdb.installNameLsa(nameLsa);
261
262 nlsr.initialize();
263
264 // Simulate successful HELLO responses from neighbor B
265 lsdb.scheduleAdjLsaBuild();
266
Vince Lehman41b173e2015-05-07 14:13:26 -0500267 // Set up adjacency LSAs
268 // This router
269 Adjacent thisRouter(conf.getRouterPrefix(), "uri://faceB", 25, Adjacent::STATUS_ACTIVE, 0, 256);
270
271 AdjLsa ownAdjLsa(conf.getRouterPrefix(), AdjLsa::TYPE_STRING, 10, ndn::time::system_clock::now(),
272 1, neighbors);
273 lsdb.installAdjLsa(ownAdjLsa);
274
275 // Other ACTIVE router
276 AdjacencyList otherAdjacencies;
277 otherAdjacencies.insert(thisRouter);
278
279 AdjLsa otherAdjLsa("/ndn/neighborB", AdjLsa::TYPE_STRING, 10,
280 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, otherAdjacencies);
281
282 lsdb.installAdjLsa(otherAdjLsa);
283
Vince Lehman199e9cf2015-04-07 13:22:16 -0500284 // Run the scheduler to build an adjacency LSA
285 this->advanceClocks(ndn::time::milliseconds(1));
286
287 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING);
288 AdjLsa* lsa = lsdb.findAdjLsa(key);
289 BOOST_REQUIRE(lsa != nullptr);
290
291 // Cancel previous LSA expiration event
292 g_scheduler.cancelEvent(lsa->getExpiringEventId());
293
294 // Set expiration time for own Adjacency LSA to earlier value for unit-test
295 //
296 // Expiration time is negative to offset the GRACE_PERIOD (10 seconds) automatically applied
297 // to expiration times
298 ndn::EventId id = lsdb.scheduleAdjLsaExpiration(key, lsa->getLsSeqNo(), -ndn::time::seconds(9));
299 lsa->setExpiringEventId(id);
300
301 // Generate a FaceEventDestroyed notification
302 ndn::nfd::FaceEventNotification event;
303 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
304 .setFaceId(destroyFaceId);
305
306 shared_ptr<ndn::Data> data = make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
307 data->setContent(event.wireEncode());
308 nlsr.getKeyChain().sign(*data);
309
310 // Receive the FaceEventDestroyed notification
311 face->receive(*data);
312
313 // Run the scheduler to expire the Adjacency LSA. The expiration should refresh the RIB
314 // entries associated with Neighbor B's advertised prefix.
315 face->sentInterests.clear();
316 this->advanceClocks(ndn::time::seconds(1));
317
318 // The Face should have two sent Interests: the face event and a RIB registration
319 BOOST_REQUIRE(face->sentInterests.size() > 0);
320 const ndn::Interest& interest = face->sentInterests.back();
321
322 ndn::nfd::ControlParameters parameters;
323 ndn::Name::Component verb;
324 BOOST_REQUIRE_NO_THROW(extractRibCommandParameters(interest, verb, parameters));
325
326 BOOST_CHECK_EQUAL(verb, ndn::Name::Component("register"));
327 BOOST_CHECK_EQUAL(parameters.getName(), nameToAdvertise);
328}
329
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500330BOOST_FIXTURE_TEST_CASE(GetCertificate, UnitTestTimeFixture)
331{
332 shared_ptr<ndn::util::DummyClientFace> face = ndn::util::makeDummyClientFace(g_ioService);
333 Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
334
335 // Create certificate
336 ndn::Name identity("/TestNLSR/identity");
337 identity.appendVersion();
338
339 ndn::KeyChain keyChain;
340 keyChain.createIdentity(identity);
341 ndn::Name certName = keyChain.getDefaultCertificateNameForIdentity(identity);
342 shared_ptr<ndn::IdentityCertificate> certificate = keyChain.getCertificate(certName);
343
344 const ndn::Name certKey = certificate->getName().getPrefix(-1);
345
346 BOOST_CHECK(nlsr.getCertificate(certKey) == nullptr);
347
348 // Certificate should be retrievable from the CertificateStore
349 nlsr.loadCertToPublish(certificate);
350
351 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
352
353 nlsr.getCertificateStore().clear();
354
355 // Certificate should be retrievable from the cache
356 nlsr.addCertificateToCache(certificate);
357 this->advanceClocks(ndn::time::milliseconds(10));
358
359 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
360}
361
Vince Lehman09131122014-09-09 17:10:11 -0500362BOOST_AUTO_TEST_SUITE_END()
363
364} //namespace test
365} //namespace nlsr