blob: cba776bfbd712a7425876c796f61248d9124ab93 [file] [log] [blame]
Vince Lehman09131122014-09-09 17:10:11 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Vince Lehmanc2acdcb2015-04-29 11:14:35 -05004 * 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
Ashlesh Gawande3909aa12017-07-28 16:01:35 -050022#include "nlsr.hpp"
Vince Lehman02e32992015-03-11 12:31:20 -050023#include "test-common.hpp"
Vince Lehman199e9cf2015-04-07 13:22:16 -050024#include "control-commands.hpp"
Vince Lehman09131122014-09-09 17:10:11 -050025
Junxiao Shi3e5120c2016-09-10 16:58:34 +000026#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
Vince Lehman09131122014-09-09 17:10:11 -050027
28namespace nlsr {
29namespace test {
30
dmcoomes9f936662017-03-02 10:33:09 -060031using std::shared_ptr;
Vince Lehman09131122014-09-09 17:10:11 -050032
Nick Gordond5c1a372016-10-31 13:56:23 -050033class NlsrFixture : public MockNfdMgmtFixture
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050034{
35public:
36 NlsrFixture()
Nick Gordond5c1a372016-10-31 13:56:23 -050037 : nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050038 , lsdb(nlsr.getLsdb())
39 , neighbors(nlsr.getAdjacencyList())
Nick Gordond5c1a372016-10-31 13:56:23 -050040 , nSuccessCallbacks(0)
41 , nFailureCallbacks(0)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050042 {
43 }
44
45 void
46 receiveHelloData(const ndn::Name& sender, const ndn::Name& receiver)
47 {
48 ndn::Name dataName(sender);
49 dataName.append("NLSR").append("INFO").append(receiver.wireEncode()).appendVersion();
50
dmcoomes9f936662017-03-02 10:33:09 -060051 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(dataName);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050052
53 nlsr.m_helloProtocol.onContentValidated(data);
Nick Gordon9461afb2017-04-25 15:54:50 -050054 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050055
56public:
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050057 Nlsr nlsr;
58 Lsdb& lsdb;
59 AdjacencyList& neighbors;
Nick Gordond5c1a372016-10-31 13:56:23 -050060 uint32_t nSuccessCallbacks;
61 uint32_t nFailureCallbacks;
62
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050063};
64
65BOOST_FIXTURE_TEST_SUITE(TestNlsr, NlsrFixture)
Vince Lehman09131122014-09-09 17:10:11 -050066
67BOOST_AUTO_TEST_CASE(HyperbolicOn_ZeroCostNeighbors)
68{
Vince Lehman09131122014-09-09 17:10:11 -050069 // Simulate loading configuration file
Nick Gordone9733ed2017-04-26 10:48:39 -050070 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://10.0.0.1"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050071 neighbors.insert(neighborA);
72
Nick Gordone9733ed2017-04-26 10:48:39 -050073 Adjacent neighborB("/ndn/neighborB", ndn::util::FaceUri("udp4://10.0.0.2"), 10, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050074 neighbors.insert(neighborB);
75
Nick Gordone9733ed2017-04-26 10:48:39 -050076 Adjacent neighborC("/ndn/neighborC", ndn::util::FaceUri("udp4://10.0.0.3"), 17, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050077 neighbors.insert(neighborC);
78
79 nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060080 nlsr.getConfParameter().setNetwork(ndn::Name("/test"));
Vince Lehman09131122014-09-09 17:10:11 -050081
82 nlsr.initialize();
83
84 std::list<Adjacent> neighborList = neighbors.getAdjList();
85 for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
86 BOOST_CHECK_EQUAL(it->getLinkCost(), 0);
87 }
88}
89
90BOOST_AUTO_TEST_CASE(HyperbolicOff_LinkStateCost)
91{
Vince Lehman09131122014-09-09 17:10:11 -050092 // Simulate loading configuration file
Nick Gordone9733ed2017-04-26 10:48:39 -050093 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://10.0.0.1"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050094 neighbors.insert(neighborA);
95
Nick Gordone9733ed2017-04-26 10:48:39 -050096 Adjacent neighborB("/ndn/neighborB", ndn::util::FaceUri("udp4://10.0.0.2"), 10, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050097 neighbors.insert(neighborB);
98
Nick Gordone9733ed2017-04-26 10:48:39 -050099 Adjacent neighborC("/ndn/neighborC", ndn::util::FaceUri("udp4://10.0.0.3"), 17, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -0500100 neighbors.insert(neighborC);
101
102 nlsr.initialize();
103
104 std::list<Adjacent> neighborList = neighbors.getAdjList();
105 for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
106 BOOST_CHECK(it->getLinkCost() != 0);
107 }
108}
109
Vince Lehman7b616582014-10-17 16:25:39 -0500110BOOST_AUTO_TEST_CASE(SetEventIntervals)
111{
Vince Lehman7b616582014-10-17 16:25:39 -0500112 // Simulate loading configuration file
113 ConfParameter& conf = nlsr.getConfParameter();
114 conf.setAdjLsaBuildInterval(3);
115 conf.setFirstHelloInterval(6);
116 conf.setRoutingCalcInterval(9);
117
118 nlsr.initialize();
119
Vince Lehman50df6b72015-03-03 12:06:40 -0600120 const Lsdb& lsdb = nlsr.getLsdb();
Vince Lehman7b616582014-10-17 16:25:39 -0500121 const RoutingTable& rt = nlsr.getRoutingTable();
122
Vince Lehman50df6b72015-03-03 12:06:40 -0600123 BOOST_CHECK_EQUAL(lsdb.getAdjLsaBuildInterval(), ndn::time::seconds(3));
Vince Lehman7b616582014-10-17 16:25:39 -0500124 BOOST_CHECK_EQUAL(nlsr.getFirstHelloInterval(), 6);
125 BOOST_CHECK_EQUAL(rt.getRoutingCalcInterval(), ndn::time::seconds(9));
126}
127
Nick Gordond5c1a372016-10-31 13:56:23 -0500128BOOST_AUTO_TEST_CASE(FaceCreateEvent)
129{
130 // Setting constants for the unit test
131 const uint32_t faceId = 1;
132 const std::string faceUri = "udp4://10.0.0.1:6363";
133 Adjacent neighbor("/ndn/neighborA", ndn::util::FaceUri(faceUri), 10, Adjacent::STATUS_INACTIVE, 0, 0);
134 BOOST_REQUIRE_EQUAL(nlsr.getAdjacencyList().insert(neighbor), 0);
135
136 this->advanceClocks(ndn::time::milliseconds(1));
137
138 // Build, sign, and send the Face Event
139 ndn::nfd::FaceEventNotification event;
140 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
141 .setRemoteUri(faceUri)
142 .setFaceId(faceId);
143 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
144 data->setContent(event.wireEncode());
145 nlsr.getKeyChain().sign(*data);
146 face->receive(*data);
147
148 // Move the clocks forward so that the Face processes the event.
149 this->advanceClocks(ndn::time::milliseconds(1));
150
151 // Need to explicitly provide a FaceUri object, because the
152 // conversion will attempt to create Name objects.
153 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::util::FaceUri(faceUri));
154 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
155 BOOST_CHECK_EQUAL(iterator->getFaceId(), faceId);
156}
157
158BOOST_AUTO_TEST_CASE(FaceCreateEventNoMatch)
159{
160 // Setting constants for the unit test
161 const uint32_t faceId = 1;
162 const std::string eventUri = "udp4://10.0.0.1:6363";
163 const std::string neighborUri = "udp4://10.0.0.2:6363";
164 Adjacent neighbor("/ndn/neighborA", ndn::util::FaceUri(neighborUri), 10, Adjacent::STATUS_INACTIVE, 0, 0);
165 nlsr.getAdjacencyList().insert(neighbor);
166
167 // Build, sign, and send the Face Event
168 ndn::nfd::FaceEventNotification event;
169 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
170 .setRemoteUri(eventUri)
171 .setFaceId(faceId);
172 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
173 data->setContent(event.wireEncode());
174 nlsr.getKeyChain().sign(*data);
175 face->receive(*data);
176
177 // Move the clocks forward so that the Face processes the event.
178 this->advanceClocks(ndn::time::milliseconds(1));
179
180 // The Face URIs did not match, so this neighbor should be unconfigured.
181 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::util::FaceUri(neighborUri));
182 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
183 BOOST_CHECK_EQUAL(iterator->getFaceId(), 0);
184}
185
186BOOST_AUTO_TEST_CASE(FaceCreateEventAlreadyConfigured)
187{
188 // Setting constants for the unit test
189 const uint32_t eventFaceId = 1;
190 const uint32_t neighborFaceId = 2;
191 const std::string faceUri = "udp4://10.0.0.1:6363";
192 Adjacent neighbor("/ndn/neighborA", ndn::util::FaceUri(faceUri), 10, Adjacent::STATUS_ACTIVE, 0, neighborFaceId);
193 nlsr.getAdjacencyList().insert(neighbor);
194
195 // Build, sign, and send the Face Event
196 ndn::nfd::FaceEventNotification event;
197 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
198 .setRemoteUri(faceUri)
199 .setFaceId(eventFaceId);
200 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
201 data->setContent(event.wireEncode());
202 nlsr.getKeyChain().sign(*data);
203 face->receive(*data);
204
205 // Move the clocks forward so that the Face processes the event.
206 this->advanceClocks(ndn::time::milliseconds(1));
207
208 // Since the neighbor was already configured, this (simply erroneous) event should have no effect.
209 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::util::FaceUri(faceUri));
210 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
211 BOOST_CHECK_EQUAL(iterator->getFaceId(), neighborFaceId);
212}
213
Vince Lehman02e32992015-03-11 12:31:20 -0500214BOOST_FIXTURE_TEST_CASE(FaceDestroyEvent, UnitTestTimeFixture)
215{
dmcoomes9f936662017-03-02 10:33:09 -0600216 std::shared_ptr<ndn::util::DummyClientFace> face = std::make_shared<ndn::util::DummyClientFace>(g_ioService);
217 Nlsr nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain);
Vince Lehman41b173e2015-05-07 14:13:26 -0500218 Lsdb& lsdb = nlsr.getLsdb();
Vince Lehman02e32992015-03-11 12:31:20 -0500219
220 // Simulate loading configuration file
221 ConfParameter& conf = nlsr.getConfParameter();
222 conf.setNetwork("/ndn");
223 conf.setSiteName("/site");
224 conf.setRouterName("/%C1.router/this-router");
225 conf.setAdjLsaBuildInterval(0);
226 conf.setRoutingCalcInterval(0);
227
228 // Add active neighbors
229 AdjacencyList& neighbors = nlsr.getAdjacencyList();
Vince Lehman02e32992015-03-11 12:31:20 -0500230 uint64_t destroyFaceId = 128;
231
232 // Create a neighbor whose Face will be destroyed
Nick Gordone9733ed2017-04-26 10:48:39 -0500233 Adjacent failNeighbor("/ndn/neighborA", ndn::util::FaceUri("udp4://10.0.0.1"), 10, Adjacent::STATUS_ACTIVE, 0,
Vince Lehman02e32992015-03-11 12:31:20 -0500234 destroyFaceId);
235 neighbors.insert(failNeighbor);
236
237 // Create an additional neighbor so an adjacency LSA can be built after the face is destroyed
Nick Gordone9733ed2017-04-26 10:48:39 -0500238 Adjacent otherNeighbor("/ndn/neighborB", ndn::util::FaceUri("udp4://10.0.0.2"), 10, Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman02e32992015-03-11 12:31:20 -0500239 neighbors.insert(otherNeighbor);
240
241 nlsr.initialize();
242
243 // Simulate successful HELLO responses
Vince Lehman41b173e2015-05-07 14:13:26 -0500244 lsdb.scheduleAdjLsaBuild();
245
246 // Set up adjacency LSAs
247 // This router
Nick Gordone9733ed2017-04-26 10:48:39 -0500248 Adjacent thisRouter(conf.getRouterPrefix(), ndn::util::FaceUri("udp4://10.0.0.3"), 10, Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman41b173e2015-05-07 14:13:26 -0500249
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600250 AdjLsa ownAdjLsa(conf.getRouterPrefix(), 10, ndn::time::system_clock::now(), 1, neighbors);
Vince Lehman41b173e2015-05-07 14:13:26 -0500251 lsdb.installAdjLsa(ownAdjLsa);
252
253 // Router that will fail
254 AdjacencyList failAdjacencies;
255 failAdjacencies.insert(thisRouter);
256
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600257 AdjLsa failAdjLsa("/ndn/neighborA", 10,
Vince Lehman41b173e2015-05-07 14:13:26 -0500258 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, failAdjacencies);
259
260 lsdb.installAdjLsa(failAdjLsa);
261
262 // Other router
263 AdjacencyList otherAdjacencies;
264 otherAdjacencies.insert(thisRouter);
265
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600266 AdjLsa otherAdjLsa("/ndn/neighborB", 10,
Vince Lehman41b173e2015-05-07 14:13:26 -0500267 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, otherAdjacencies);
268
269 lsdb.installAdjLsa(otherAdjLsa);
Vince Lehman02e32992015-03-11 12:31:20 -0500270
271 // Run the scheduler to build an adjacency LSA
272 this->advanceClocks(ndn::time::milliseconds(1));
273
274 // Make sure an adjacency LSA was built
275 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING);
Vince Lehman41b173e2015-05-07 14:13:26 -0500276 AdjLsa* lsa = lsdb.findAdjLsa(key);
Vince Lehman02e32992015-03-11 12:31:20 -0500277 BOOST_REQUIRE(lsa != nullptr);
278
alvy46ccaae2015-06-25 14:13:39 -0500279 uint32_t lastAdjLsaSeqNo = lsa->getLsSeqNo();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500280 nlsr.getLsdb().getSequencingManager().setAdjLsaSeq(lastAdjLsaSeqNo);
Vince Lehman02e32992015-03-11 12:31:20 -0500281
282 // Make sure the routing table was calculated
283 RoutingTableEntry* rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
284 BOOST_REQUIRE(rtEntry != nullptr);
285 BOOST_REQUIRE_EQUAL(rtEntry->getNexthopList().getSize(), 1);
286
287 // Receive FaceEventDestroyed notification
288 ndn::nfd::FaceEventNotification event;
289 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
290 .setFaceId(destroyFaceId);
291
dmcoomes9f936662017-03-02 10:33:09 -0600292 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Vince Lehman02e32992015-03-11 12:31:20 -0500293 data->setContent(event.wireEncode());
294 nlsr.getKeyChain().sign(*data);
295
296 face->receive(*data);
297
298 // Run the scheduler to build an adjacency LSA
299 this->advanceClocks(ndn::time::milliseconds(1));
300
301 Adjacent updatedNeighbor = neighbors.getAdjacent(failNeighbor.getName());
302
303 BOOST_CHECK_EQUAL(updatedNeighbor.getFaceId(), 0);
304 BOOST_CHECK_EQUAL(updatedNeighbor.getInterestTimedOutNo(),
305 nlsr.getConfParameter().getInterestRetryNumber());
306 BOOST_CHECK_EQUAL(updatedNeighbor.getStatus(), Adjacent::STATUS_INACTIVE);
alvy46ccaae2015-06-25 14:13:39 -0500307
308 lsa = lsdb.findAdjLsa(key);
309 BOOST_REQUIRE(lsa != nullptr);
310
311 BOOST_CHECK_EQUAL(lsa->getLsSeqNo(), lastAdjLsaSeqNo + 1);
Vince Lehman02e32992015-03-11 12:31:20 -0500312
313 // Make sure the routing table was recalculated
314 rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
315 BOOST_CHECK(rtEntry == nullptr);
316}
317
Vince Lehman199e9cf2015-04-07 13:22:16 -0500318// Bug #2733
319// This test checks that when a face for an inactive node is destroyed, an
320// Adjacency LSA build does not postpone the LSA refresh and cause RIB
321// entries for other nodes' name prefixes to not be refreshed.
322//
323// This test is invalid when Issue #2732 is implemented since an Adjacency LSA
324// refresh will not cause RIB entries for other nodes' name prefixes to be refreshed.
325BOOST_FIXTURE_TEST_CASE(FaceDestroyEventInactive, UnitTestTimeFixture)
326{
dmcoomes9f936662017-03-02 10:33:09 -0600327 std::shared_ptr<ndn::util::DummyClientFace> face = std::make_shared<ndn::util::DummyClientFace>(g_ioService);
328 Nlsr nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain);
Vince Lehman199e9cf2015-04-07 13:22:16 -0500329 Lsdb& lsdb = nlsr.getLsdb();
330
331 // Simulate loading configuration file
332 ConfParameter& conf = nlsr.getConfParameter();
333 conf.setNetwork("/ndn");
334 conf.setSiteName("/site");
335 conf.setRouterName("/%C1.router/this-router");
336 conf.setFirstHelloInterval(0);
337 conf.setAdjLsaBuildInterval(0);
338 conf.setRoutingCalcInterval(0);
339
340 // Add neighbors
341 AdjacencyList& neighbors = nlsr.getAdjacencyList();
342
343 uint64_t destroyFaceId = 128;
344
345 // Create an inactive neighbor whose Face will be destroyed
Nick Gordone9733ed2017-04-26 10:48:39 -0500346 Adjacent failNeighbor("/ndn/neighborA", ndn::util::FaceUri("udp4://10.0.0.1"), 10, Adjacent::STATUS_INACTIVE, 3,
Vince Lehman199e9cf2015-04-07 13:22:16 -0500347 destroyFaceId);
348 neighbors.insert(failNeighbor);
349
350 // Create an additional active neighbor so an adjacency LSA can be built
Nick Gordone9733ed2017-04-26 10:48:39 -0500351 Adjacent otherNeighbor("/ndn/neighborB", ndn::util::FaceUri("udp4://10.0.0.2"), 25, Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman199e9cf2015-04-07 13:22:16 -0500352 neighbors.insert(otherNeighbor);
353
354 // Add a name for the neighbor to advertise
355 NamePrefixList nameList;
356 ndn::Name nameToAdvertise("/ndn/neighborB/name");
357 nameList.insert(nameToAdvertise);
358
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600359 NameLsa nameLsa("/ndn/neighborB", 25, ndn::time::system_clock::now(), nameList);
Vince Lehman199e9cf2015-04-07 13:22:16 -0500360 lsdb.installNameLsa(nameLsa);
361
362 nlsr.initialize();
363
364 // Simulate successful HELLO responses from neighbor B
365 lsdb.scheduleAdjLsaBuild();
366
Vince Lehman41b173e2015-05-07 14:13:26 -0500367 // Set up adjacency LSAs
368 // This router
Nick Gordone9733ed2017-04-26 10:48:39 -0500369 Adjacent thisRouter(conf.getRouterPrefix(), ndn::util::FaceUri("udp4://10.0.0.2"), 25, Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman41b173e2015-05-07 14:13:26 -0500370
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600371 AdjLsa ownAdjLsa(conf.getRouterPrefix(), 10, ndn::time::system_clock::now(), 1, neighbors);
Vince Lehman41b173e2015-05-07 14:13:26 -0500372 lsdb.installAdjLsa(ownAdjLsa);
373
374 // Other ACTIVE router
375 AdjacencyList otherAdjacencies;
376 otherAdjacencies.insert(thisRouter);
377
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600378 AdjLsa otherAdjLsa("/ndn/neighborB", 10,
Vince Lehman41b173e2015-05-07 14:13:26 -0500379 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, otherAdjacencies);
380
381 lsdb.installAdjLsa(otherAdjLsa);
382
Vince Lehman199e9cf2015-04-07 13:22:16 -0500383 // Run the scheduler to build an adjacency LSA
384 this->advanceClocks(ndn::time::milliseconds(1));
385
386 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING);
387 AdjLsa* lsa = lsdb.findAdjLsa(key);
388 BOOST_REQUIRE(lsa != nullptr);
389
390 // Cancel previous LSA expiration event
391 g_scheduler.cancelEvent(lsa->getExpiringEventId());
392
393 // Set expiration time for own Adjacency LSA to earlier value for unit-test
394 //
395 // Expiration time is negative to offset the GRACE_PERIOD (10 seconds) automatically applied
396 // to expiration times
397 ndn::EventId id = lsdb.scheduleAdjLsaExpiration(key, lsa->getLsSeqNo(), -ndn::time::seconds(9));
398 lsa->setExpiringEventId(id);
399
400 // Generate a FaceEventDestroyed notification
401 ndn::nfd::FaceEventNotification event;
402 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
403 .setFaceId(destroyFaceId);
404
dmcoomes9f936662017-03-02 10:33:09 -0600405 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Vince Lehman199e9cf2015-04-07 13:22:16 -0500406 data->setContent(event.wireEncode());
407 nlsr.getKeyChain().sign(*data);
408
409 // Receive the FaceEventDestroyed notification
410 face->receive(*data);
411
412 // Run the scheduler to expire the Adjacency LSA. The expiration should refresh the RIB
413 // entries associated with Neighbor B's advertised prefix.
414 face->sentInterests.clear();
415 this->advanceClocks(ndn::time::seconds(1));
416
417 // The Face should have two sent Interests: the face event and a RIB registration
418 BOOST_REQUIRE(face->sentInterests.size() > 0);
419 const ndn::Interest& interest = face->sentInterests.back();
420
421 ndn::nfd::ControlParameters parameters;
422 ndn::Name::Component verb;
Nick Gordonb50e51b2016-07-22 16:05:57 -0500423 BOOST_REQUIRE_NO_THROW(extractRibCommandParameters(interest,
424 verb,
425 parameters));
Vince Lehman199e9cf2015-04-07 13:22:16 -0500426 BOOST_CHECK_EQUAL(verb, ndn::Name::Component("register"));
427 BOOST_CHECK_EQUAL(parameters.getName(), nameToAdvertise);
428}
429
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500430BOOST_AUTO_TEST_CASE(GetCertificate)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500431{
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500432 // Create certificate
433 ndn::Name identity("/TestNLSR/identity");
434 identity.appendVersion();
435
436 ndn::KeyChain keyChain;
437 keyChain.createIdentity(identity);
438 ndn::Name certName = keyChain.getDefaultCertificateNameForIdentity(identity);
dmcoomes9f936662017-03-02 10:33:09 -0600439 std::shared_ptr<ndn::IdentityCertificate> certificate = keyChain.getCertificate(certName);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500440
441 const ndn::Name certKey = certificate->getName().getPrefix(-1);
442
443 BOOST_CHECK(nlsr.getCertificate(certKey) == nullptr);
444
445 // Certificate should be retrievable from the CertificateStore
446 nlsr.loadCertToPublish(certificate);
447
448 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
449
450 nlsr.getCertificateStore().clear();
451
452 // Certificate should be retrievable from the cache
453 nlsr.addCertificateToCache(certificate);
454 this->advanceClocks(ndn::time::milliseconds(10));
455
456 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
457}
458
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500459BOOST_AUTO_TEST_CASE(SetRouterCommandPrefix)
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500460{
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500461 // Simulate loading configuration file
462 ConfParameter& conf = nlsr.getConfParameter();
463 conf.setNetwork("/ndn");
464 conf.setSiteName("/site");
465 conf.setRouterName("/%C1.router/this-router");
466
467 nlsr.initialize();
468
469 BOOST_CHECK_EQUAL(nlsr.getLsdbDatasetHandler().getRouterNameCommandPrefix(),
470 ndn::Name("/ndn/site/%C1.router/this-router/lsdb"));
471}
472
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500473BOOST_AUTO_TEST_CASE(BuildAdjLsaAfterHelloResponse)
474{
475 // Configure NLSR
476 ConfParameter& conf = nlsr.getConfParameter();
477 conf.setNetwork("/ndn");
478 conf.setSiteName("/site");
479
480 ndn::Name routerName("/%C1.Router/this-router");
481 conf.setRouterName(routerName);
482
483 conf.setAdjLsaBuildInterval(1);
484
485 // Add neighbors
486 // Router A
487 ndn::Name neighborAName("/ndn/site/%C1.router/routerA");
Nick Gordone9733ed2017-04-26 10:48:39 -0500488 Adjacent neighborA(neighborAName, ndn::util::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500489 neighbors.insert(neighborA);
490
491 // Router B
492 ndn::Name neighborBName("/ndn/site/%C1.router/routerB");
Nick Gordone9733ed2017-04-26 10:48:39 -0500493 Adjacent neighborB(neighborBName, ndn::util::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500494 neighbors.insert(neighborB);
495
496 nlsr.initialize();
497 this->advanceClocks(ndn::time::milliseconds(1));
498
499 // Receive HELLO response from Router A
500 receiveHelloData(neighborAName, conf.getRouterPrefix());
501 this->advanceClocks(ndn::time::seconds(1));
502
503 ndn::Name lsaKey = ndn::Name(conf.getRouterPrefix()).append(AdjLsa::TYPE_STRING);
504
505 // Adjacency LSA should be built even though other router is INACTIVE
506 AdjLsa* lsa = lsdb.findAdjLsa(lsaKey);
507 BOOST_REQUIRE(lsa != nullptr);
508 BOOST_CHECK_EQUAL(lsa->getAdl().getSize(), 1);
509
510 // Receive HELLO response from Router B
511 receiveHelloData(neighborBName, conf.getRouterPrefix());
512
513 // Both routers become INACTIVE and HELLO Interests have timed out
514 for (Adjacent& adjacency : neighbors.getAdjList()) {
515 adjacency.setStatus(Adjacent::STATUS_INACTIVE);
516 adjacency.setInterestTimedOutNo(HELLO_RETRIES_DEFAULT);
517 }
518
519 this->advanceClocks(ndn::time::seconds(1));
520
521 // Adjacency LSA should have been removed since this router's adjacencies are INACTIVE
522 // and have timed out
523 lsa = lsdb.findAdjLsa(lsaKey);
524 BOOST_CHECK(lsa == nullptr);
525
526 // Receive HELLO response from Router A and B
527 receiveHelloData(neighborAName, conf.getRouterPrefix());
528 receiveHelloData(neighborBName, conf.getRouterPrefix());
529 this->advanceClocks(ndn::time::seconds(1));
530
531 // Adjacency LSA should be built
532 lsa = lsdb.findAdjLsa(lsaKey);
533 BOOST_REQUIRE(lsa != nullptr);
534 BOOST_CHECK_EQUAL(lsa->getAdl().getSize(), 2);
535}
536
Nick Gordon9461afb2017-04-25 15:54:50 -0500537BOOST_AUTO_TEST_CASE(CanonizeUris)
538{
539 ndn::Name neighborAName("/ndn/site/%C1.router/routerA");
Nick Gordone9733ed2017-04-26 10:48:39 -0500540 ndn::util::FaceUri faceUriA("udp://10.0.0.1");
541 Adjacent neighborA(neighborAName, faceUriA, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500542 neighbors.insert(neighborA);
543
544 ndn::Name neighborBName("/ndn/site/%C1.router/routerB");
Nick Gordone9733ed2017-04-26 10:48:39 -0500545 ndn::util::FaceUri faceUriB("udp://10.0.0.2");
546 Adjacent neighborB(neighborBName, faceUriB, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500547 neighbors.insert(neighborB);
548
549 int nCanonizationsLeft = nlsr.getAdjacencyList().getAdjList().size();
550 std::function<void(std::list<Adjacent>::iterator)> thenCallback =
551 [this, &thenCallback, &nCanonizationsLeft] (std::list<Adjacent>::iterator iterator) {
552 nCanonizationsLeft--;
553 nlsr.canonizeNeighborUris(iterator, thenCallback);
554 };
555 nlsr.canonizeNeighborUris(nlsr.getAdjacencyList().getAdjList().begin(),
556 [thenCallback] (std::list<Adjacent>::iterator iterator) {
557 thenCallback(iterator);
558 });
559 while (nCanonizationsLeft != 0) {
560 this->advanceClocks(ndn::time::milliseconds(1));
561 }
562
Nick Gordone9733ed2017-04-26 10:48:39 -0500563 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborAName).getFaceUri(),
564 ndn::util::FaceUri("udp4://10.0.0.1:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500565
Nick Gordone9733ed2017-04-26 10:48:39 -0500566 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborBName).getFaceUri(),
567 ndn::util::FaceUri("udp4://10.0.0.2:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500568}
569
Nick Gordond5c1a372016-10-31 13:56:23 -0500570BOOST_AUTO_TEST_CASE(FaceDatasetFetchSuccess)
571{
572 bool hasResult = false;
573 nlsr.m_validator.m_shouldValidate = false;
574
575 nlsr.initializeFaces([&hasResult] (const std::vector<ndn::nfd::FaceStatus>& faces) {
576 hasResult = true;
577 BOOST_CHECK_EQUAL(faces.size(), 2);
578 BOOST_CHECK_EQUAL(faces.front().getFaceId(), 25401);
579 BOOST_CHECK_EQUAL(faces.back().getFaceId(), 25402);
580 },
581 [] (uint32_t code, const std::string& reason) {});
582
583 this->advanceClocks(ndn::time::milliseconds(500));
584
585 ndn::nfd::FaceStatus payload1;
586 payload1.setFaceId(25401);
587 ndn::nfd::FaceStatus payload2;
588 payload2.setFaceId(25402);
589 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
590
591 this->advanceClocks(ndn::time::milliseconds(500));
592 BOOST_CHECK(hasResult);
593}
594
595BOOST_AUTO_TEST_CASE(FaceDatasetFetchFailure)
596{
597 nlsr.m_validator.m_shouldValidate = false;
598 nlsr.initializeFaces([](const std::vector<ndn::nfd::FaceStatus>& faces) {},
599 [this](uint32_t code, const std::string& reason){
600 this->nFailureCallbacks++;
601 });
602 this->advanceClocks(ndn::time::milliseconds(500));
603
604 ndn::Name payload;
605 this->sendDataset("/localhost/nfd/faces/list", payload);
606 this->advanceClocks(ndn::time::milliseconds(500));
607
608 BOOST_CHECK_EQUAL(nFailureCallbacks, 1);
609 BOOST_CHECK_EQUAL(nSuccessCallbacks, 0);
610}
611
612BOOST_AUTO_TEST_CASE(FaceDatasetProcess)
613{
614 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://192.168.0.100:6363"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
615 neighbors.insert(neighborA);
616
617 Adjacent neighborB("/ndn/neighborB", ndn::util::FaceUri("udp4://192.168.0.101:6363"), 10, Adjacent::STATUS_INACTIVE, 0, 0);
618 neighbors.insert(neighborB);
619
620 ndn::nfd::FaceStatus payload1;
621 payload1.setFaceId(1)
622 .setRemoteUri("udp4://192.168.0.100:6363");
623 ndn::nfd::FaceStatus payload2;
624 payload2.setFaceId(2)
625 .setRemoteUri("udp4://192.168.0.101:6363");
626 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload1, payload2};
627
628 nlsr.processFaceDataset(faceStatuses);
629 this->advanceClocks(ndn::time::milliseconds(100));
630
631 AdjacencyList adjList = nlsr.getAdjacencyList();
632
633 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), payload1.getFaceId());
634 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborB").getFaceId(), payload2.getFaceId());
635}
636
637BOOST_AUTO_TEST_CASE(UnconfiguredNeighbor)
638{
639 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://192.168.0.100:6363"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
640 neighbors.insert(neighborA);
641
642 ndn::nfd::FaceStatus payload;
643 payload.setFaceId(1)
644 .setRemoteUri("udp4://192.168.0.101:6363"); // Note dissimilar Face URI.
645 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload};
646
647 nlsr.processFaceDataset(faceStatuses);
648 this->advanceClocks(ndn::time::milliseconds(100));
649
650 AdjacencyList adjList = nlsr.getAdjacencyList();
651
652 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), 0);
653}
654
655BOOST_AUTO_TEST_CASE(FaceDatasetPeriodicFetch)
656{
657 int nNameMatches = 0;
658 ndn::Name datasetPrefix("/localhost/nfd/faces/list");
659 ndn::nfd::CommandOptions options;
660 ndn::time::milliseconds defaultTimeout = options.getTimeout();
661
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500662 int fetchInterval(1);
Nick Gordond5c1a372016-10-31 13:56:23 -0500663 ConfParameter& conf = nlsr.getConfParameter();
664 conf.setFaceDatasetFetchInterval(fetchInterval);
665 conf.setFaceDatasetFetchTries(0);
666
667 nlsr.initializeFaces(std::bind(&Nlsr::processFaceDataset, &nlsr, _1),
668 std::bind(&Nlsr::onFaceDatasetFetchTimeout, &nlsr, _1, _2, 0));
669
670 // Elapse the default timeout time of the interest.
671 this->advanceClocks(defaultTimeout);
672
673 // Check that we have one interest for face list in the sent interests.
674 for (const ndn::Interest& interest : face->sentInterests) {
675 if (datasetPrefix.isPrefixOf(interest.getName())) {
676 nNameMatches++;
677 }
678 }
679 BOOST_CHECK_EQUAL(nNameMatches, 1);
680
681 // Elapse the clock by the reschedule time (that we set)
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500682 this->advanceClocks(ndn::time::seconds(fetchInterval));
Nick Gordond5c1a372016-10-31 13:56:23 -0500683 // Elapse the default timeout on the interest.
684 this->advanceClocks(defaultTimeout);
685 // Plus a little more to let the events process.
686 this->advanceClocks(ndn::time::seconds(1));
687
688 // Check that we now have two interests
689 nNameMatches = 0;
690 for (const ndn::Interest& interest : face->sentInterests) {
691 if (datasetPrefix.isPrefixOf(interest.getName())) {
692 nNameMatches++;
693 }
694 }
695 BOOST_CHECK_EQUAL(nNameMatches, 2);
696}
697
Vince Lehman09131122014-09-09 17:10:11 -0500698BOOST_AUTO_TEST_SUITE_END()
699
Nick Gordonfad8e252016-08-11 14:21:38 -0500700} // namespace test
701} // namespace nlsr