blob: 6f1af7d7631c6b424be43bd7fbc98efe214da765 [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
179 uint32_t lastAdjLsaSeqNo = nlsr.getSequencingManager().getAdjLsaSeq();
180
181 // Make sure the routing table was calculated
182 RoutingTableEntry* rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
183 BOOST_REQUIRE(rtEntry != nullptr);
184 BOOST_REQUIRE_EQUAL(rtEntry->getNexthopList().getSize(), 1);
185
186 // Receive FaceEventDestroyed notification
187 ndn::nfd::FaceEventNotification event;
188 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
189 .setFaceId(destroyFaceId);
190
191 shared_ptr<ndn::Data> data = make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
192 data->setContent(event.wireEncode());
193 nlsr.getKeyChain().sign(*data);
194
195 face->receive(*data);
196
197 // Run the scheduler to build an adjacency LSA
198 this->advanceClocks(ndn::time::milliseconds(1));
199
200 Adjacent updatedNeighbor = neighbors.getAdjacent(failNeighbor.getName());
201
202 BOOST_CHECK_EQUAL(updatedNeighbor.getFaceId(), 0);
203 BOOST_CHECK_EQUAL(updatedNeighbor.getInterestTimedOutNo(),
204 nlsr.getConfParameter().getInterestRetryNumber());
205 BOOST_CHECK_EQUAL(updatedNeighbor.getStatus(), Adjacent::STATUS_INACTIVE);
206 BOOST_CHECK_EQUAL(nlsr.getSequencingManager().getAdjLsaSeq(), lastAdjLsaSeqNo + 1);
207
208 // Make sure the routing table was recalculated
209 rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
210 BOOST_CHECK(rtEntry == nullptr);
211}
212
Vince Lehman199e9cf2015-04-07 13:22:16 -0500213// Bug #2733
214// This test checks that when a face for an inactive node is destroyed, an
215// Adjacency LSA build does not postpone the LSA refresh and cause RIB
216// entries for other nodes' name prefixes to not be refreshed.
217//
218// This test is invalid when Issue #2732 is implemented since an Adjacency LSA
219// refresh will not cause RIB entries for other nodes' name prefixes to be refreshed.
220BOOST_FIXTURE_TEST_CASE(FaceDestroyEventInactive, UnitTestTimeFixture)
221{
222 shared_ptr<ndn::util::DummyClientFace> face = ndn::util::makeDummyClientFace(g_ioService);
223 Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
224 Lsdb& lsdb = nlsr.getLsdb();
225
226 // Simulate loading configuration file
227 ConfParameter& conf = nlsr.getConfParameter();
228 conf.setNetwork("/ndn");
229 conf.setSiteName("/site");
230 conf.setRouterName("/%C1.router/this-router");
231 conf.setFirstHelloInterval(0);
232 conf.setAdjLsaBuildInterval(0);
233 conf.setRoutingCalcInterval(0);
234
235 // Add neighbors
236 AdjacencyList& neighbors = nlsr.getAdjacencyList();
237
238 uint64_t destroyFaceId = 128;
239
240 // Create an inactive neighbor whose Face will be destroyed
241 Adjacent failNeighbor("/ndn/neighborA", "uri://faceA", 10, Adjacent::STATUS_INACTIVE, 3,
242 destroyFaceId);
243 neighbors.insert(failNeighbor);
244
245 // Create an additional active neighbor so an adjacency LSA can be built
246 Adjacent otherNeighbor("/ndn/neighborB", "uri://faceB", 25, Adjacent::STATUS_ACTIVE, 0, 256);
247 neighbors.insert(otherNeighbor);
248
249 // Add a name for the neighbor to advertise
250 NamePrefixList nameList;
251 ndn::Name nameToAdvertise("/ndn/neighborB/name");
252 nameList.insert(nameToAdvertise);
253
254 NameLsa nameLsa("/ndn/neighborB", "name", 25, ndn::time::system_clock::now(), nameList);
255 lsdb.installNameLsa(nameLsa);
256
257 nlsr.initialize();
258
259 // Simulate successful HELLO responses from neighbor B
260 lsdb.scheduleAdjLsaBuild();
261
Vince Lehman41b173e2015-05-07 14:13:26 -0500262 // Set up adjacency LSAs
263 // This router
264 Adjacent thisRouter(conf.getRouterPrefix(), "uri://faceB", 25, Adjacent::STATUS_ACTIVE, 0, 256);
265
266 AdjLsa ownAdjLsa(conf.getRouterPrefix(), AdjLsa::TYPE_STRING, 10, ndn::time::system_clock::now(),
267 1, neighbors);
268 lsdb.installAdjLsa(ownAdjLsa);
269
270 // Other ACTIVE router
271 AdjacencyList otherAdjacencies;
272 otherAdjacencies.insert(thisRouter);
273
274 AdjLsa otherAdjLsa("/ndn/neighborB", AdjLsa::TYPE_STRING, 10,
275 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, otherAdjacencies);
276
277 lsdb.installAdjLsa(otherAdjLsa);
278
Vince Lehman199e9cf2015-04-07 13:22:16 -0500279 // Run the scheduler to build an adjacency LSA
280 this->advanceClocks(ndn::time::milliseconds(1));
281
282 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING);
283 AdjLsa* lsa = lsdb.findAdjLsa(key);
284 BOOST_REQUIRE(lsa != nullptr);
285
286 // Cancel previous LSA expiration event
287 g_scheduler.cancelEvent(lsa->getExpiringEventId());
288
289 // Set expiration time for own Adjacency LSA to earlier value for unit-test
290 //
291 // Expiration time is negative to offset the GRACE_PERIOD (10 seconds) automatically applied
292 // to expiration times
293 ndn::EventId id = lsdb.scheduleAdjLsaExpiration(key, lsa->getLsSeqNo(), -ndn::time::seconds(9));
294 lsa->setExpiringEventId(id);
295
296 // Generate a FaceEventDestroyed notification
297 ndn::nfd::FaceEventNotification event;
298 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
299 .setFaceId(destroyFaceId);
300
301 shared_ptr<ndn::Data> data = make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
302 data->setContent(event.wireEncode());
303 nlsr.getKeyChain().sign(*data);
304
305 // Receive the FaceEventDestroyed notification
306 face->receive(*data);
307
308 // Run the scheduler to expire the Adjacency LSA. The expiration should refresh the RIB
309 // entries associated with Neighbor B's advertised prefix.
310 face->sentInterests.clear();
311 this->advanceClocks(ndn::time::seconds(1));
312
313 // The Face should have two sent Interests: the face event and a RIB registration
314 BOOST_REQUIRE(face->sentInterests.size() > 0);
315 const ndn::Interest& interest = face->sentInterests.back();
316
317 ndn::nfd::ControlParameters parameters;
318 ndn::Name::Component verb;
319 BOOST_REQUIRE_NO_THROW(extractRibCommandParameters(interest, verb, parameters));
320
321 BOOST_CHECK_EQUAL(verb, ndn::Name::Component("register"));
322 BOOST_CHECK_EQUAL(parameters.getName(), nameToAdvertise);
323}
324
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500325BOOST_FIXTURE_TEST_CASE(GetCertificate, UnitTestTimeFixture)
326{
327 shared_ptr<ndn::util::DummyClientFace> face = ndn::util::makeDummyClientFace(g_ioService);
328 Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
329
330 // Create certificate
331 ndn::Name identity("/TestNLSR/identity");
332 identity.appendVersion();
333
334 ndn::KeyChain keyChain;
335 keyChain.createIdentity(identity);
336 ndn::Name certName = keyChain.getDefaultCertificateNameForIdentity(identity);
337 shared_ptr<ndn::IdentityCertificate> certificate = keyChain.getCertificate(certName);
338
339 const ndn::Name certKey = certificate->getName().getPrefix(-1);
340
341 BOOST_CHECK(nlsr.getCertificate(certKey) == nullptr);
342
343 // Certificate should be retrievable from the CertificateStore
344 nlsr.loadCertToPublish(certificate);
345
346 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
347
348 nlsr.getCertificateStore().clear();
349
350 // Certificate should be retrievable from the cache
351 nlsr.addCertificateToCache(certificate);
352 this->advanceClocks(ndn::time::milliseconds(10));
353
354 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
355}
356
Vince Lehman09131122014-09-09 17:10:11 -0500357BOOST_AUTO_TEST_SUITE_END()
358
359} //namespace test
360} //namespace nlsr