blob: 2cfe46f7053e09b04f0906a34d50549d3d85f504 [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
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 Lehman09131122014-09-09 17:10:11 -050024
Vince Lehman02e32992015-03-11 12:31:20 -050025#include "nlsr.hpp"
26
Junxiao Shi3e5120c2016-09-10 16:58:34 +000027#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
Vince Lehman09131122014-09-09 17:10:11 -050028
29namespace nlsr {
30namespace test {
31
dmcoomes9f936662017-03-02 10:33:09 -060032using std::shared_ptr;
Vince Lehman09131122014-09-09 17:10:11 -050033
Nick Gordond5c1a372016-10-31 13:56:23 -050034class NlsrFixture : public MockNfdMgmtFixture
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050035{
36public:
37 NlsrFixture()
Nick Gordond5c1a372016-10-31 13:56:23 -050038 : nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050039 , lsdb(nlsr.getLsdb())
40 , neighbors(nlsr.getAdjacencyList())
Nick Gordond5c1a372016-10-31 13:56:23 -050041 , nSuccessCallbacks(0)
42 , nFailureCallbacks(0)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050043 {
44 }
45
46 void
47 receiveHelloData(const ndn::Name& sender, const ndn::Name& receiver)
48 {
49 ndn::Name dataName(sender);
50 dataName.append("NLSR").append("INFO").append(receiver.wireEncode()).appendVersion();
51
dmcoomes9f936662017-03-02 10:33:09 -060052 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(dataName);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050053
54 nlsr.m_helloProtocol.onContentValidated(data);
Nick Gordon9461afb2017-04-25 15:54:50 -050055 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050056
57public:
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050058 Nlsr nlsr;
59 Lsdb& lsdb;
60 AdjacencyList& neighbors;
Nick Gordond5c1a372016-10-31 13:56:23 -050061 uint32_t nSuccessCallbacks;
62 uint32_t nFailureCallbacks;
63
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050064};
65
66BOOST_FIXTURE_TEST_SUITE(TestNlsr, NlsrFixture)
Vince Lehman09131122014-09-09 17:10:11 -050067
68BOOST_AUTO_TEST_CASE(HyperbolicOn_ZeroCostNeighbors)
69{
Vince Lehman09131122014-09-09 17:10:11 -050070 // Simulate loading configuration file
Nick Gordone9733ed2017-04-26 10:48:39 -050071 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 -050072 neighbors.insert(neighborA);
73
Nick Gordone9733ed2017-04-26 10:48:39 -050074 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 -050075 neighbors.insert(neighborB);
76
Nick Gordone9733ed2017-04-26 10:48:39 -050077 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 -050078 neighbors.insert(neighborC);
79
80 nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060081 nlsr.getConfParameter().setNetwork(ndn::Name("/test"));
Vince Lehman09131122014-09-09 17:10:11 -050082
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_EQUAL(it->getLinkCost(), 0);
88 }
89}
90
91BOOST_AUTO_TEST_CASE(HyperbolicOff_LinkStateCost)
92{
Vince Lehman09131122014-09-09 17:10:11 -050093 // Simulate loading configuration file
Nick Gordone9733ed2017-04-26 10:48:39 -050094 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 -050095 neighbors.insert(neighborA);
96
Nick Gordone9733ed2017-04-26 10:48:39 -050097 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 -050098 neighbors.insert(neighborB);
99
Nick Gordone9733ed2017-04-26 10:48:39 -0500100 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 -0500101 neighbors.insert(neighborC);
102
103 nlsr.initialize();
104
105 std::list<Adjacent> neighborList = neighbors.getAdjList();
106 for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
107 BOOST_CHECK(it->getLinkCost() != 0);
108 }
109}
110
Vince Lehman7b616582014-10-17 16:25:39 -0500111BOOST_AUTO_TEST_CASE(SetEventIntervals)
112{
Vince Lehman7b616582014-10-17 16:25:39 -0500113 // Simulate loading configuration file
114 ConfParameter& conf = nlsr.getConfParameter();
115 conf.setAdjLsaBuildInterval(3);
116 conf.setFirstHelloInterval(6);
117 conf.setRoutingCalcInterval(9);
118
119 nlsr.initialize();
120
Vince Lehman50df6b72015-03-03 12:06:40 -0600121 const Lsdb& lsdb = nlsr.getLsdb();
Vince Lehman7b616582014-10-17 16:25:39 -0500122 const RoutingTable& rt = nlsr.getRoutingTable();
123
Vince Lehman50df6b72015-03-03 12:06:40 -0600124 BOOST_CHECK_EQUAL(lsdb.getAdjLsaBuildInterval(), ndn::time::seconds(3));
Vince Lehman7b616582014-10-17 16:25:39 -0500125 BOOST_CHECK_EQUAL(nlsr.getFirstHelloInterval(), 6);
126 BOOST_CHECK_EQUAL(rt.getRoutingCalcInterval(), ndn::time::seconds(9));
127}
128
Nick Gordond5c1a372016-10-31 13:56:23 -0500129BOOST_AUTO_TEST_CASE(FaceCreateEvent)
130{
131 // Setting constants for the unit test
132 const uint32_t faceId = 1;
133 const std::string faceUri = "udp4://10.0.0.1:6363";
134 Adjacent neighbor("/ndn/neighborA", ndn::util::FaceUri(faceUri), 10, Adjacent::STATUS_INACTIVE, 0, 0);
135 BOOST_REQUIRE_EQUAL(nlsr.getAdjacencyList().insert(neighbor), 0);
136
137 this->advanceClocks(ndn::time::milliseconds(1));
138
139 // Build, sign, and send the Face Event
140 ndn::nfd::FaceEventNotification event;
141 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
142 .setRemoteUri(faceUri)
143 .setFaceId(faceId);
144 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
145 data->setContent(event.wireEncode());
146 nlsr.getKeyChain().sign(*data);
147 face->receive(*data);
148
149 // Move the clocks forward so that the Face processes the event.
150 this->advanceClocks(ndn::time::milliseconds(1));
151
152 // Need to explicitly provide a FaceUri object, because the
153 // conversion will attempt to create Name objects.
154 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::util::FaceUri(faceUri));
155 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
156 BOOST_CHECK_EQUAL(iterator->getFaceId(), faceId);
157}
158
159BOOST_AUTO_TEST_CASE(FaceCreateEventNoMatch)
160{
161 // Setting constants for the unit test
162 const uint32_t faceId = 1;
163 const std::string eventUri = "udp4://10.0.0.1:6363";
164 const std::string neighborUri = "udp4://10.0.0.2:6363";
165 Adjacent neighbor("/ndn/neighborA", ndn::util::FaceUri(neighborUri), 10, Adjacent::STATUS_INACTIVE, 0, 0);
166 nlsr.getAdjacencyList().insert(neighbor);
167
168 // Build, sign, and send the Face Event
169 ndn::nfd::FaceEventNotification event;
170 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
171 .setRemoteUri(eventUri)
172 .setFaceId(faceId);
173 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
174 data->setContent(event.wireEncode());
175 nlsr.getKeyChain().sign(*data);
176 face->receive(*data);
177
178 // Move the clocks forward so that the Face processes the event.
179 this->advanceClocks(ndn::time::milliseconds(1));
180
181 // The Face URIs did not match, so this neighbor should be unconfigured.
182 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::util::FaceUri(neighborUri));
183 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
184 BOOST_CHECK_EQUAL(iterator->getFaceId(), 0);
185}
186
187BOOST_AUTO_TEST_CASE(FaceCreateEventAlreadyConfigured)
188{
189 // Setting constants for the unit test
190 const uint32_t eventFaceId = 1;
191 const uint32_t neighborFaceId = 2;
192 const std::string faceUri = "udp4://10.0.0.1:6363";
193 Adjacent neighbor("/ndn/neighborA", ndn::util::FaceUri(faceUri), 10, Adjacent::STATUS_ACTIVE, 0, neighborFaceId);
194 nlsr.getAdjacencyList().insert(neighbor);
195
196 // Build, sign, and send the Face Event
197 ndn::nfd::FaceEventNotification event;
198 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
199 .setRemoteUri(faceUri)
200 .setFaceId(eventFaceId);
201 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
202 data->setContent(event.wireEncode());
203 nlsr.getKeyChain().sign(*data);
204 face->receive(*data);
205
206 // Move the clocks forward so that the Face processes the event.
207 this->advanceClocks(ndn::time::milliseconds(1));
208
209 // Since the neighbor was already configured, this (simply erroneous) event should have no effect.
210 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::util::FaceUri(faceUri));
211 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
212 BOOST_CHECK_EQUAL(iterator->getFaceId(), neighborFaceId);
213}
214
Vince Lehman02e32992015-03-11 12:31:20 -0500215BOOST_FIXTURE_TEST_CASE(FaceDestroyEvent, UnitTestTimeFixture)
216{
dmcoomes9f936662017-03-02 10:33:09 -0600217 std::shared_ptr<ndn::util::DummyClientFace> face = std::make_shared<ndn::util::DummyClientFace>(g_ioService);
218 Nlsr nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain);
Vince Lehman41b173e2015-05-07 14:13:26 -0500219 Lsdb& lsdb = nlsr.getLsdb();
Vince Lehman02e32992015-03-11 12:31:20 -0500220
221 // Simulate loading configuration file
222 ConfParameter& conf = nlsr.getConfParameter();
223 conf.setNetwork("/ndn");
224 conf.setSiteName("/site");
225 conf.setRouterName("/%C1.router/this-router");
226 conf.setAdjLsaBuildInterval(0);
227 conf.setRoutingCalcInterval(0);
228
229 // Add active neighbors
230 AdjacencyList& neighbors = nlsr.getAdjacencyList();
Vince Lehman02e32992015-03-11 12:31:20 -0500231 uint64_t destroyFaceId = 128;
232
233 // Create a neighbor whose Face will be destroyed
Nick Gordone9733ed2017-04-26 10:48:39 -0500234 Adjacent failNeighbor("/ndn/neighborA", ndn::util::FaceUri("udp4://10.0.0.1"), 10, Adjacent::STATUS_ACTIVE, 0,
Vince Lehman02e32992015-03-11 12:31:20 -0500235 destroyFaceId);
236 neighbors.insert(failNeighbor);
237
238 // Create an additional neighbor so an adjacency LSA can be built after the face is destroyed
Nick Gordone9733ed2017-04-26 10:48:39 -0500239 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 -0500240 neighbors.insert(otherNeighbor);
241
242 nlsr.initialize();
243
244 // Simulate successful HELLO responses
Vince Lehman41b173e2015-05-07 14:13:26 -0500245 lsdb.scheduleAdjLsaBuild();
246
247 // Set up adjacency LSAs
248 // This router
Nick Gordone9733ed2017-04-26 10:48:39 -0500249 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 -0500250
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600251 AdjLsa ownAdjLsa(conf.getRouterPrefix(), 10, ndn::time::system_clock::now(), 1, neighbors);
Vince Lehman41b173e2015-05-07 14:13:26 -0500252 lsdb.installAdjLsa(ownAdjLsa);
253
254 // Router that will fail
255 AdjacencyList failAdjacencies;
256 failAdjacencies.insert(thisRouter);
257
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600258 AdjLsa failAdjLsa("/ndn/neighborA", 10,
Vince Lehman41b173e2015-05-07 14:13:26 -0500259 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, failAdjacencies);
260
261 lsdb.installAdjLsa(failAdjLsa);
262
263 // Other router
264 AdjacencyList otherAdjacencies;
265 otherAdjacencies.insert(thisRouter);
266
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600267 AdjLsa otherAdjLsa("/ndn/neighborB", 10,
Vince Lehman41b173e2015-05-07 14:13:26 -0500268 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, otherAdjacencies);
269
270 lsdb.installAdjLsa(otherAdjLsa);
Vince Lehman02e32992015-03-11 12:31:20 -0500271
272 // Run the scheduler to build an adjacency LSA
273 this->advanceClocks(ndn::time::milliseconds(1));
274
275 // Make sure an adjacency LSA was built
276 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING);
Vince Lehman41b173e2015-05-07 14:13:26 -0500277 AdjLsa* lsa = lsdb.findAdjLsa(key);
Vince Lehman02e32992015-03-11 12:31:20 -0500278 BOOST_REQUIRE(lsa != nullptr);
279
alvy46ccaae2015-06-25 14:13:39 -0500280 uint32_t lastAdjLsaSeqNo = lsa->getLsSeqNo();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500281 nlsr.getLsdb().getSequencingManager().setAdjLsaSeq(lastAdjLsaSeqNo);
Vince Lehman02e32992015-03-11 12:31:20 -0500282
283 // Make sure the routing table was calculated
284 RoutingTableEntry* rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
285 BOOST_REQUIRE(rtEntry != nullptr);
286 BOOST_REQUIRE_EQUAL(rtEntry->getNexthopList().getSize(), 1);
287
288 // Receive FaceEventDestroyed notification
289 ndn::nfd::FaceEventNotification event;
290 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
291 .setFaceId(destroyFaceId);
292
dmcoomes9f936662017-03-02 10:33:09 -0600293 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Vince Lehman02e32992015-03-11 12:31:20 -0500294 data->setContent(event.wireEncode());
295 nlsr.getKeyChain().sign(*data);
296
297 face->receive(*data);
298
299 // Run the scheduler to build an adjacency LSA
300 this->advanceClocks(ndn::time::milliseconds(1));
301
302 Adjacent updatedNeighbor = neighbors.getAdjacent(failNeighbor.getName());
303
304 BOOST_CHECK_EQUAL(updatedNeighbor.getFaceId(), 0);
305 BOOST_CHECK_EQUAL(updatedNeighbor.getInterestTimedOutNo(),
306 nlsr.getConfParameter().getInterestRetryNumber());
307 BOOST_CHECK_EQUAL(updatedNeighbor.getStatus(), Adjacent::STATUS_INACTIVE);
alvy46ccaae2015-06-25 14:13:39 -0500308
309 lsa = lsdb.findAdjLsa(key);
310 BOOST_REQUIRE(lsa != nullptr);
311
312 BOOST_CHECK_EQUAL(lsa->getLsSeqNo(), lastAdjLsaSeqNo + 1);
Vince Lehman02e32992015-03-11 12:31:20 -0500313
314 // Make sure the routing table was recalculated
315 rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
316 BOOST_CHECK(rtEntry == nullptr);
317}
318
Vince Lehman199e9cf2015-04-07 13:22:16 -0500319// Bug #2733
320// This test checks that when a face for an inactive node is destroyed, an
321// Adjacency LSA build does not postpone the LSA refresh and cause RIB
322// entries for other nodes' name prefixes to not be refreshed.
323//
324// This test is invalid when Issue #2732 is implemented since an Adjacency LSA
325// refresh will not cause RIB entries for other nodes' name prefixes to be refreshed.
326BOOST_FIXTURE_TEST_CASE(FaceDestroyEventInactive, UnitTestTimeFixture)
327{
dmcoomes9f936662017-03-02 10:33:09 -0600328 std::shared_ptr<ndn::util::DummyClientFace> face = std::make_shared<ndn::util::DummyClientFace>(g_ioService);
329 Nlsr nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain);
Vince Lehman199e9cf2015-04-07 13:22:16 -0500330 Lsdb& lsdb = nlsr.getLsdb();
331
332 // Simulate loading configuration file
333 ConfParameter& conf = nlsr.getConfParameter();
334 conf.setNetwork("/ndn");
335 conf.setSiteName("/site");
336 conf.setRouterName("/%C1.router/this-router");
337 conf.setFirstHelloInterval(0);
338 conf.setAdjLsaBuildInterval(0);
339 conf.setRoutingCalcInterval(0);
340
341 // Add neighbors
342 AdjacencyList& neighbors = nlsr.getAdjacencyList();
343
344 uint64_t destroyFaceId = 128;
345
346 // Create an inactive neighbor whose Face will be destroyed
Nick Gordone9733ed2017-04-26 10:48:39 -0500347 Adjacent failNeighbor("/ndn/neighborA", ndn::util::FaceUri("udp4://10.0.0.1"), 10, Adjacent::STATUS_INACTIVE, 3,
Vince Lehman199e9cf2015-04-07 13:22:16 -0500348 destroyFaceId);
349 neighbors.insert(failNeighbor);
350
351 // Create an additional active neighbor so an adjacency LSA can be built
Nick Gordone9733ed2017-04-26 10:48:39 -0500352 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 -0500353 neighbors.insert(otherNeighbor);
354
355 // Add a name for the neighbor to advertise
356 NamePrefixList nameList;
357 ndn::Name nameToAdvertise("/ndn/neighborB/name");
358 nameList.insert(nameToAdvertise);
359
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600360 NameLsa nameLsa("/ndn/neighborB", 25, ndn::time::system_clock::now(), nameList);
Vince Lehman199e9cf2015-04-07 13:22:16 -0500361 lsdb.installNameLsa(nameLsa);
362
363 nlsr.initialize();
364
365 // Simulate successful HELLO responses from neighbor B
366 lsdb.scheduleAdjLsaBuild();
367
Vince Lehman41b173e2015-05-07 14:13:26 -0500368 // Set up adjacency LSAs
369 // This router
Nick Gordone9733ed2017-04-26 10:48:39 -0500370 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 -0500371
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600372 AdjLsa ownAdjLsa(conf.getRouterPrefix(), 10, ndn::time::system_clock::now(), 1, neighbors);
Vince Lehman41b173e2015-05-07 14:13:26 -0500373 lsdb.installAdjLsa(ownAdjLsa);
374
375 // Other ACTIVE router
376 AdjacencyList otherAdjacencies;
377 otherAdjacencies.insert(thisRouter);
378
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600379 AdjLsa otherAdjLsa("/ndn/neighborB", 10,
Vince Lehman41b173e2015-05-07 14:13:26 -0500380 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, otherAdjacencies);
381
382 lsdb.installAdjLsa(otherAdjLsa);
383
Vince Lehman199e9cf2015-04-07 13:22:16 -0500384 // Run the scheduler to build an adjacency LSA
385 this->advanceClocks(ndn::time::milliseconds(1));
386
387 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING);
388 AdjLsa* lsa = lsdb.findAdjLsa(key);
389 BOOST_REQUIRE(lsa != nullptr);
390
391 // Cancel previous LSA expiration event
392 g_scheduler.cancelEvent(lsa->getExpiringEventId());
393
394 // Set expiration time for own Adjacency LSA to earlier value for unit-test
395 //
396 // Expiration time is negative to offset the GRACE_PERIOD (10 seconds) automatically applied
397 // to expiration times
398 ndn::EventId id = lsdb.scheduleAdjLsaExpiration(key, lsa->getLsSeqNo(), -ndn::time::seconds(9));
399 lsa->setExpiringEventId(id);
400
401 // Generate a FaceEventDestroyed notification
402 ndn::nfd::FaceEventNotification event;
403 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
404 .setFaceId(destroyFaceId);
405
dmcoomes9f936662017-03-02 10:33:09 -0600406 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Vince Lehman199e9cf2015-04-07 13:22:16 -0500407 data->setContent(event.wireEncode());
408 nlsr.getKeyChain().sign(*data);
409
410 // Receive the FaceEventDestroyed notification
411 face->receive(*data);
412
413 // Run the scheduler to expire the Adjacency LSA. The expiration should refresh the RIB
414 // entries associated with Neighbor B's advertised prefix.
415 face->sentInterests.clear();
416 this->advanceClocks(ndn::time::seconds(1));
417
418 // The Face should have two sent Interests: the face event and a RIB registration
419 BOOST_REQUIRE(face->sentInterests.size() > 0);
420 const ndn::Interest& interest = face->sentInterests.back();
421
422 ndn::nfd::ControlParameters parameters;
423 ndn::Name::Component verb;
Nick Gordonb50e51b2016-07-22 16:05:57 -0500424 BOOST_REQUIRE_NO_THROW(extractRibCommandParameters(interest,
425 verb,
426 parameters));
Vince Lehman199e9cf2015-04-07 13:22:16 -0500427 BOOST_CHECK_EQUAL(verb, ndn::Name::Component("register"));
428 BOOST_CHECK_EQUAL(parameters.getName(), nameToAdvertise);
429}
430
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500431BOOST_AUTO_TEST_CASE(GetCertificate)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500432{
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500433 // Create certificate
434 ndn::Name identity("/TestNLSR/identity");
435 identity.appendVersion();
436
437 ndn::KeyChain keyChain;
438 keyChain.createIdentity(identity);
439 ndn::Name certName = keyChain.getDefaultCertificateNameForIdentity(identity);
dmcoomes9f936662017-03-02 10:33:09 -0600440 std::shared_ptr<ndn::IdentityCertificate> certificate = keyChain.getCertificate(certName);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500441
442 const ndn::Name certKey = certificate->getName().getPrefix(-1);
443
444 BOOST_CHECK(nlsr.getCertificate(certKey) == nullptr);
445
446 // Certificate should be retrievable from the CertificateStore
447 nlsr.loadCertToPublish(certificate);
448
449 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
450
451 nlsr.getCertificateStore().clear();
452
453 // Certificate should be retrievable from the cache
454 nlsr.addCertificateToCache(certificate);
455 this->advanceClocks(ndn::time::milliseconds(10));
456
457 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
458}
459
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500460BOOST_AUTO_TEST_CASE(SetRouterCommandPrefix)
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500461{
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500462 // Simulate loading configuration file
463 ConfParameter& conf = nlsr.getConfParameter();
464 conf.setNetwork("/ndn");
465 conf.setSiteName("/site");
466 conf.setRouterName("/%C1.router/this-router");
467
468 nlsr.initialize();
469
470 BOOST_CHECK_EQUAL(nlsr.getLsdbDatasetHandler().getRouterNameCommandPrefix(),
471 ndn::Name("/ndn/site/%C1.router/this-router/lsdb"));
472}
473
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500474BOOST_AUTO_TEST_CASE(BuildAdjLsaAfterHelloResponse)
475{
476 // Configure NLSR
477 ConfParameter& conf = nlsr.getConfParameter();
478 conf.setNetwork("/ndn");
479 conf.setSiteName("/site");
480
481 ndn::Name routerName("/%C1.Router/this-router");
482 conf.setRouterName(routerName);
483
484 conf.setAdjLsaBuildInterval(1);
485
486 // Add neighbors
487 // Router A
488 ndn::Name neighborAName("/ndn/site/%C1.router/routerA");
Nick Gordone9733ed2017-04-26 10:48:39 -0500489 Adjacent neighborA(neighborAName, ndn::util::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500490 neighbors.insert(neighborA);
491
492 // Router B
493 ndn::Name neighborBName("/ndn/site/%C1.router/routerB");
Nick Gordone9733ed2017-04-26 10:48:39 -0500494 Adjacent neighborB(neighborBName, ndn::util::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500495 neighbors.insert(neighborB);
496
497 nlsr.initialize();
498 this->advanceClocks(ndn::time::milliseconds(1));
499
500 // Receive HELLO response from Router A
501 receiveHelloData(neighborAName, conf.getRouterPrefix());
502 this->advanceClocks(ndn::time::seconds(1));
503
504 ndn::Name lsaKey = ndn::Name(conf.getRouterPrefix()).append(AdjLsa::TYPE_STRING);
505
506 // Adjacency LSA should be built even though other router is INACTIVE
507 AdjLsa* lsa = lsdb.findAdjLsa(lsaKey);
508 BOOST_REQUIRE(lsa != nullptr);
509 BOOST_CHECK_EQUAL(lsa->getAdl().getSize(), 1);
510
511 // Receive HELLO response from Router B
512 receiveHelloData(neighborBName, conf.getRouterPrefix());
513
514 // Both routers become INACTIVE and HELLO Interests have timed out
515 for (Adjacent& adjacency : neighbors.getAdjList()) {
516 adjacency.setStatus(Adjacent::STATUS_INACTIVE);
517 adjacency.setInterestTimedOutNo(HELLO_RETRIES_DEFAULT);
518 }
519
520 this->advanceClocks(ndn::time::seconds(1));
521
522 // Adjacency LSA should have been removed since this router's adjacencies are INACTIVE
523 // and have timed out
524 lsa = lsdb.findAdjLsa(lsaKey);
525 BOOST_CHECK(lsa == nullptr);
526
527 // Receive HELLO response from Router A and B
528 receiveHelloData(neighborAName, conf.getRouterPrefix());
529 receiveHelloData(neighborBName, conf.getRouterPrefix());
530 this->advanceClocks(ndn::time::seconds(1));
531
532 // Adjacency LSA should be built
533 lsa = lsdb.findAdjLsa(lsaKey);
534 BOOST_REQUIRE(lsa != nullptr);
535 BOOST_CHECK_EQUAL(lsa->getAdl().getSize(), 2);
536}
537
Nick Gordon9461afb2017-04-25 15:54:50 -0500538BOOST_AUTO_TEST_CASE(CanonizeUris)
539{
540 ndn::Name neighborAName("/ndn/site/%C1.router/routerA");
Nick Gordone9733ed2017-04-26 10:48:39 -0500541 ndn::util::FaceUri faceUriA("udp://10.0.0.1");
542 Adjacent neighborA(neighborAName, faceUriA, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500543 neighbors.insert(neighborA);
544
545 ndn::Name neighborBName("/ndn/site/%C1.router/routerB");
Nick Gordone9733ed2017-04-26 10:48:39 -0500546 ndn::util::FaceUri faceUriB("udp://10.0.0.2");
547 Adjacent neighborB(neighborBName, faceUriB, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500548 neighbors.insert(neighborB);
549
550 int nCanonizationsLeft = nlsr.getAdjacencyList().getAdjList().size();
551 std::function<void(std::list<Adjacent>::iterator)> thenCallback =
552 [this, &thenCallback, &nCanonizationsLeft] (std::list<Adjacent>::iterator iterator) {
553 nCanonizationsLeft--;
554 nlsr.canonizeNeighborUris(iterator, thenCallback);
555 };
556 nlsr.canonizeNeighborUris(nlsr.getAdjacencyList().getAdjList().begin(),
557 [thenCallback] (std::list<Adjacent>::iterator iterator) {
558 thenCallback(iterator);
559 });
560 while (nCanonizationsLeft != 0) {
561 this->advanceClocks(ndn::time::milliseconds(1));
562 }
563
Nick Gordone9733ed2017-04-26 10:48:39 -0500564 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborAName).getFaceUri(),
565 ndn::util::FaceUri("udp4://10.0.0.1:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500566
Nick Gordone9733ed2017-04-26 10:48:39 -0500567 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborBName).getFaceUri(),
568 ndn::util::FaceUri("udp4://10.0.0.2:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500569}
570
Nick Gordond5c1a372016-10-31 13:56:23 -0500571BOOST_AUTO_TEST_CASE(FaceDatasetFetchSuccess)
572{
573 bool hasResult = false;
574 nlsr.m_validator.m_shouldValidate = false;
575
576 nlsr.initializeFaces([&hasResult] (const std::vector<ndn::nfd::FaceStatus>& faces) {
577 hasResult = true;
578 BOOST_CHECK_EQUAL(faces.size(), 2);
579 BOOST_CHECK_EQUAL(faces.front().getFaceId(), 25401);
580 BOOST_CHECK_EQUAL(faces.back().getFaceId(), 25402);
581 },
582 [] (uint32_t code, const std::string& reason) {});
583
584 this->advanceClocks(ndn::time::milliseconds(500));
585
586 ndn::nfd::FaceStatus payload1;
587 payload1.setFaceId(25401);
588 ndn::nfd::FaceStatus payload2;
589 payload2.setFaceId(25402);
590 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
591
592 this->advanceClocks(ndn::time::milliseconds(500));
593 BOOST_CHECK(hasResult);
594}
595
596BOOST_AUTO_TEST_CASE(FaceDatasetFetchFailure)
597{
598 nlsr.m_validator.m_shouldValidate = false;
599 nlsr.initializeFaces([](const std::vector<ndn::nfd::FaceStatus>& faces) {},
600 [this](uint32_t code, const std::string& reason){
601 this->nFailureCallbacks++;
602 });
603 this->advanceClocks(ndn::time::milliseconds(500));
604
605 ndn::Name payload;
606 this->sendDataset("/localhost/nfd/faces/list", payload);
607 this->advanceClocks(ndn::time::milliseconds(500));
608
609 BOOST_CHECK_EQUAL(nFailureCallbacks, 1);
610 BOOST_CHECK_EQUAL(nSuccessCallbacks, 0);
611}
612
613BOOST_AUTO_TEST_CASE(FaceDatasetProcess)
614{
615 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://192.168.0.100:6363"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
616 neighbors.insert(neighborA);
617
618 Adjacent neighborB("/ndn/neighborB", ndn::util::FaceUri("udp4://192.168.0.101:6363"), 10, Adjacent::STATUS_INACTIVE, 0, 0);
619 neighbors.insert(neighborB);
620
621 ndn::nfd::FaceStatus payload1;
622 payload1.setFaceId(1)
623 .setRemoteUri("udp4://192.168.0.100:6363");
624 ndn::nfd::FaceStatus payload2;
625 payload2.setFaceId(2)
626 .setRemoteUri("udp4://192.168.0.101:6363");
627 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload1, payload2};
628
629 nlsr.processFaceDataset(faceStatuses);
630 this->advanceClocks(ndn::time::milliseconds(100));
631
632 AdjacencyList adjList = nlsr.getAdjacencyList();
633
634 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), payload1.getFaceId());
635 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborB").getFaceId(), payload2.getFaceId());
636}
637
638BOOST_AUTO_TEST_CASE(UnconfiguredNeighbor)
639{
640 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://192.168.0.100:6363"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
641 neighbors.insert(neighborA);
642
643 ndn::nfd::FaceStatus payload;
644 payload.setFaceId(1)
645 .setRemoteUri("udp4://192.168.0.101:6363"); // Note dissimilar Face URI.
646 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload};
647
648 nlsr.processFaceDataset(faceStatuses);
649 this->advanceClocks(ndn::time::milliseconds(100));
650
651 AdjacencyList adjList = nlsr.getAdjacencyList();
652
653 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), 0);
654}
655
656BOOST_AUTO_TEST_CASE(FaceDatasetPeriodicFetch)
657{
658 int nNameMatches = 0;
659 ndn::Name datasetPrefix("/localhost/nfd/faces/list");
660 ndn::nfd::CommandOptions options;
661 ndn::time::milliseconds defaultTimeout = options.getTimeout();
662
663 ndn::time::seconds fetchInterval(1);
664 ConfParameter& conf = nlsr.getConfParameter();
665 conf.setFaceDatasetFetchInterval(fetchInterval);
666 conf.setFaceDatasetFetchTries(0);
667
668 nlsr.initializeFaces(std::bind(&Nlsr::processFaceDataset, &nlsr, _1),
669 std::bind(&Nlsr::onFaceDatasetFetchTimeout, &nlsr, _1, _2, 0));
670
671 // Elapse the default timeout time of the interest.
672 this->advanceClocks(defaultTimeout);
673
674 // Check that we have one interest for face list in the sent interests.
675 for (const ndn::Interest& interest : face->sentInterests) {
676 if (datasetPrefix.isPrefixOf(interest.getName())) {
677 nNameMatches++;
678 }
679 }
680 BOOST_CHECK_EQUAL(nNameMatches, 1);
681
682 // Elapse the clock by the reschedule time (that we set)
683 this->advanceClocks(fetchInterval);
684 // Elapse the default timeout on the interest.
685 this->advanceClocks(defaultTimeout);
686 // Plus a little more to let the events process.
687 this->advanceClocks(ndn::time::seconds(1));
688
689 // Check that we now have two interests
690 nNameMatches = 0;
691 for (const ndn::Interest& interest : face->sentInterests) {
692 if (datasetPrefix.isPrefixOf(interest.getName())) {
693 nNameMatches++;
694 }
695 }
696 BOOST_CHECK_EQUAL(nNameMatches, 2);
697}
698
Vince Lehman09131122014-09-09 17:10:11 -0500699BOOST_AUTO_TEST_SUITE_END()
700
Nick Gordonfad8e252016-08-11 14:21:38 -0500701} // namespace test
702} // namespace nlsr