blob: 0159cf4952fb394eace130930bd03da9bddf8012 [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"
Nick Gordonc0c6bcf2017-08-15 18:11:21 -050025#include "logger.hpp"
Vince Lehman09131122014-09-09 17:10:11 -050026
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 Lehmanf7eec4f2015-05-08 19:02:31 -0500319BOOST_AUTO_TEST_CASE(GetCertificate)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500320{
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500321 // Create certificate
322 ndn::Name identity("/TestNLSR/identity");
323 identity.appendVersion();
324
325 ndn::KeyChain keyChain;
326 keyChain.createIdentity(identity);
327 ndn::Name certName = keyChain.getDefaultCertificateNameForIdentity(identity);
dmcoomes9f936662017-03-02 10:33:09 -0600328 std::shared_ptr<ndn::IdentityCertificate> certificate = keyChain.getCertificate(certName);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500329
330 const ndn::Name certKey = certificate->getName().getPrefix(-1);
331
332 BOOST_CHECK(nlsr.getCertificate(certKey) == nullptr);
333
334 // Certificate should be retrievable from the CertificateStore
335 nlsr.loadCertToPublish(certificate);
336
337 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
338
339 nlsr.getCertificateStore().clear();
340
341 // Certificate should be retrievable from the cache
342 nlsr.addCertificateToCache(certificate);
343 this->advanceClocks(ndn::time::milliseconds(10));
344
345 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
346}
347
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500348BOOST_AUTO_TEST_CASE(SetRouterCommandPrefix)
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500349{
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500350 // Simulate loading configuration file
351 ConfParameter& conf = nlsr.getConfParameter();
352 conf.setNetwork("/ndn");
353 conf.setSiteName("/site");
354 conf.setRouterName("/%C1.router/this-router");
355
356 nlsr.initialize();
357
358 BOOST_CHECK_EQUAL(nlsr.getLsdbDatasetHandler().getRouterNameCommandPrefix(),
359 ndn::Name("/ndn/site/%C1.router/this-router/lsdb"));
360}
361
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500362BOOST_AUTO_TEST_CASE(BuildAdjLsaAfterHelloResponse)
363{
364 // Configure NLSR
365 ConfParameter& conf = nlsr.getConfParameter();
366 conf.setNetwork("/ndn");
367 conf.setSiteName("/site");
368
369 ndn::Name routerName("/%C1.Router/this-router");
370 conf.setRouterName(routerName);
371
372 conf.setAdjLsaBuildInterval(1);
373
374 // Add neighbors
375 // Router A
376 ndn::Name neighborAName("/ndn/site/%C1.router/routerA");
Nick Gordone9733ed2017-04-26 10:48:39 -0500377 Adjacent neighborA(neighborAName, ndn::util::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500378 neighbors.insert(neighborA);
379
380 // Router B
381 ndn::Name neighborBName("/ndn/site/%C1.router/routerB");
Nick Gordone9733ed2017-04-26 10:48:39 -0500382 Adjacent neighborB(neighborBName, ndn::util::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500383 neighbors.insert(neighborB);
384
385 nlsr.initialize();
386 this->advanceClocks(ndn::time::milliseconds(1));
387
388 // Receive HELLO response from Router A
389 receiveHelloData(neighborAName, conf.getRouterPrefix());
390 this->advanceClocks(ndn::time::seconds(1));
391
392 ndn::Name lsaKey = ndn::Name(conf.getRouterPrefix()).append(AdjLsa::TYPE_STRING);
393
394 // Adjacency LSA should be built even though other router is INACTIVE
395 AdjLsa* lsa = lsdb.findAdjLsa(lsaKey);
396 BOOST_REQUIRE(lsa != nullptr);
397 BOOST_CHECK_EQUAL(lsa->getAdl().getSize(), 1);
398
399 // Receive HELLO response from Router B
400 receiveHelloData(neighborBName, conf.getRouterPrefix());
401
402 // Both routers become INACTIVE and HELLO Interests have timed out
403 for (Adjacent& adjacency : neighbors.getAdjList()) {
404 adjacency.setStatus(Adjacent::STATUS_INACTIVE);
405 adjacency.setInterestTimedOutNo(HELLO_RETRIES_DEFAULT);
406 }
407
408 this->advanceClocks(ndn::time::seconds(1));
409
410 // Adjacency LSA should have been removed since this router's adjacencies are INACTIVE
411 // and have timed out
412 lsa = lsdb.findAdjLsa(lsaKey);
413 BOOST_CHECK(lsa == nullptr);
414
415 // Receive HELLO response from Router A and B
416 receiveHelloData(neighborAName, conf.getRouterPrefix());
417 receiveHelloData(neighborBName, conf.getRouterPrefix());
418 this->advanceClocks(ndn::time::seconds(1));
419
420 // Adjacency LSA should be built
421 lsa = lsdb.findAdjLsa(lsaKey);
422 BOOST_REQUIRE(lsa != nullptr);
423 BOOST_CHECK_EQUAL(lsa->getAdl().getSize(), 2);
424}
425
Nick Gordon9461afb2017-04-25 15:54:50 -0500426BOOST_AUTO_TEST_CASE(CanonizeUris)
427{
428 ndn::Name neighborAName("/ndn/site/%C1.router/routerA");
Nick Gordone9733ed2017-04-26 10:48:39 -0500429 ndn::util::FaceUri faceUriA("udp://10.0.0.1");
430 Adjacent neighborA(neighborAName, faceUriA, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500431 neighbors.insert(neighborA);
432
433 ndn::Name neighborBName("/ndn/site/%C1.router/routerB");
Nick Gordone9733ed2017-04-26 10:48:39 -0500434 ndn::util::FaceUri faceUriB("udp://10.0.0.2");
435 Adjacent neighborB(neighborBName, faceUriB, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500436 neighbors.insert(neighborB);
437
438 int nCanonizationsLeft = nlsr.getAdjacencyList().getAdjList().size();
Nick Gordon922714a2017-06-13 14:12:02 -0500439 std::function<void(void)> finallyCallback = [this] {
440 nlsr.initialize();
441 };
Nick Gordon9461afb2017-04-25 15:54:50 -0500442 std::function<void(std::list<Adjacent>::iterator)> thenCallback =
Nick Gordon922714a2017-06-13 14:12:02 -0500443 [this, &thenCallback, &finallyCallback, &nCanonizationsLeft] (std::list<Adjacent>::iterator iterator) {
Nick Gordon9461afb2017-04-25 15:54:50 -0500444 nCanonizationsLeft--;
Nick Gordon922714a2017-06-13 14:12:02 -0500445 nlsr.canonizeNeighborUris(iterator, thenCallback, finallyCallback);
Nick Gordon9461afb2017-04-25 15:54:50 -0500446 };
447 nlsr.canonizeNeighborUris(nlsr.getAdjacencyList().getAdjList().begin(),
Nick Gordon922714a2017-06-13 14:12:02 -0500448 [&thenCallback, &finallyCallback] (std::list<Adjacent>::iterator iterator) {
Nick Gordon9461afb2017-04-25 15:54:50 -0500449 thenCallback(iterator);
Nick Gordon922714a2017-06-13 14:12:02 -0500450 },
451 finallyCallback);
Nick Gordon9461afb2017-04-25 15:54:50 -0500452 while (nCanonizationsLeft != 0) {
453 this->advanceClocks(ndn::time::milliseconds(1));
454 }
455
Nick Gordone9733ed2017-04-26 10:48:39 -0500456 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborAName).getFaceUri(),
457 ndn::util::FaceUri("udp4://10.0.0.1:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500458
Nick Gordone9733ed2017-04-26 10:48:39 -0500459 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborBName).getFaceUri(),
460 ndn::util::FaceUri("udp4://10.0.0.2:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500461}
462
Nick Gordond5c1a372016-10-31 13:56:23 -0500463BOOST_AUTO_TEST_CASE(FaceDatasetFetchSuccess)
464{
465 bool hasResult = false;
466 nlsr.m_validator.m_shouldValidate = false;
467
468 nlsr.initializeFaces([&hasResult] (const std::vector<ndn::nfd::FaceStatus>& faces) {
469 hasResult = true;
470 BOOST_CHECK_EQUAL(faces.size(), 2);
471 BOOST_CHECK_EQUAL(faces.front().getFaceId(), 25401);
472 BOOST_CHECK_EQUAL(faces.back().getFaceId(), 25402);
473 },
474 [] (uint32_t code, const std::string& reason) {});
475
476 this->advanceClocks(ndn::time::milliseconds(500));
477
478 ndn::nfd::FaceStatus payload1;
479 payload1.setFaceId(25401);
480 ndn::nfd::FaceStatus payload2;
481 payload2.setFaceId(25402);
482 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
483
484 this->advanceClocks(ndn::time::milliseconds(500));
485 BOOST_CHECK(hasResult);
486}
487
488BOOST_AUTO_TEST_CASE(FaceDatasetFetchFailure)
489{
490 nlsr.m_validator.m_shouldValidate = false;
491 nlsr.initializeFaces([](const std::vector<ndn::nfd::FaceStatus>& faces) {},
492 [this](uint32_t code, const std::string& reason){
493 this->nFailureCallbacks++;
494 });
495 this->advanceClocks(ndn::time::milliseconds(500));
496
497 ndn::Name payload;
498 this->sendDataset("/localhost/nfd/faces/list", payload);
499 this->advanceClocks(ndn::time::milliseconds(500));
500
501 BOOST_CHECK_EQUAL(nFailureCallbacks, 1);
502 BOOST_CHECK_EQUAL(nSuccessCallbacks, 0);
503}
504
505BOOST_AUTO_TEST_CASE(FaceDatasetProcess)
506{
507 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://192.168.0.100:6363"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
508 neighbors.insert(neighborA);
509
510 Adjacent neighborB("/ndn/neighborB", ndn::util::FaceUri("udp4://192.168.0.101:6363"), 10, Adjacent::STATUS_INACTIVE, 0, 0);
511 neighbors.insert(neighborB);
512
513 ndn::nfd::FaceStatus payload1;
514 payload1.setFaceId(1)
515 .setRemoteUri("udp4://192.168.0.100:6363");
516 ndn::nfd::FaceStatus payload2;
517 payload2.setFaceId(2)
518 .setRemoteUri("udp4://192.168.0.101:6363");
519 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload1, payload2};
520
521 nlsr.processFaceDataset(faceStatuses);
522 this->advanceClocks(ndn::time::milliseconds(100));
523
524 AdjacencyList adjList = nlsr.getAdjacencyList();
525
526 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), payload1.getFaceId());
527 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborB").getFaceId(), payload2.getFaceId());
528}
529
530BOOST_AUTO_TEST_CASE(UnconfiguredNeighbor)
531{
532 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://192.168.0.100:6363"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
533 neighbors.insert(neighborA);
534
535 ndn::nfd::FaceStatus payload;
536 payload.setFaceId(1)
537 .setRemoteUri("udp4://192.168.0.101:6363"); // Note dissimilar Face URI.
538 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload};
539
540 nlsr.processFaceDataset(faceStatuses);
541 this->advanceClocks(ndn::time::milliseconds(100));
542
543 AdjacencyList adjList = nlsr.getAdjacencyList();
544
545 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), 0);
546}
547
548BOOST_AUTO_TEST_CASE(FaceDatasetPeriodicFetch)
549{
550 int nNameMatches = 0;
551 ndn::Name datasetPrefix("/localhost/nfd/faces/list");
552 ndn::nfd::CommandOptions options;
553 ndn::time::milliseconds defaultTimeout = options.getTimeout();
554
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500555 int fetchInterval(1);
Nick Gordond5c1a372016-10-31 13:56:23 -0500556 ConfParameter& conf = nlsr.getConfParameter();
557 conf.setFaceDatasetFetchInterval(fetchInterval);
558 conf.setFaceDatasetFetchTries(0);
559
560 nlsr.initializeFaces(std::bind(&Nlsr::processFaceDataset, &nlsr, _1),
561 std::bind(&Nlsr::onFaceDatasetFetchTimeout, &nlsr, _1, _2, 0));
562
563 // Elapse the default timeout time of the interest.
564 this->advanceClocks(defaultTimeout);
565
566 // Check that we have one interest for face list in the sent interests.
567 for (const ndn::Interest& interest : face->sentInterests) {
568 if (datasetPrefix.isPrefixOf(interest.getName())) {
569 nNameMatches++;
570 }
571 }
572 BOOST_CHECK_EQUAL(nNameMatches, 1);
573
574 // Elapse the clock by the reschedule time (that we set)
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500575 this->advanceClocks(ndn::time::seconds(fetchInterval));
Nick Gordond5c1a372016-10-31 13:56:23 -0500576 // Elapse the default timeout on the interest.
577 this->advanceClocks(defaultTimeout);
578 // Plus a little more to let the events process.
579 this->advanceClocks(ndn::time::seconds(1));
580
581 // Check that we now have two interests
582 nNameMatches = 0;
583 for (const ndn::Interest& interest : face->sentInterests) {
584 if (datasetPrefix.isPrefixOf(interest.getName())) {
585 nNameMatches++;
586 }
587 }
588 BOOST_CHECK_EQUAL(nNameMatches, 2);
589}
590
Vince Lehman09131122014-09-09 17:10:11 -0500591BOOST_AUTO_TEST_SUITE_END()
592
Nick Gordonfad8e252016-08-11 14:21:38 -0500593} // namespace test
594} // namespace nlsr