blob: fe30864ba188f22645b41775e6ba2c0215a9ab5c [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 Gordon727d4832017-10-13 18:04:25 -050071 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://10.0.0.1"), 25,
72 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050073 neighbors.insert(neighborA);
74
Nick Gordon727d4832017-10-13 18:04:25 -050075 Adjacent neighborB("/ndn/neighborB", ndn::util::FaceUri("udp4://10.0.0.2"), 10,
76 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050077 neighbors.insert(neighborB);
78
Nick Gordon727d4832017-10-13 18:04:25 -050079 Adjacent neighborC("/ndn/neighborC", ndn::util::FaceUri("udp4://10.0.0.3"), 17,
80 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050081 neighbors.insert(neighborC);
82
83 nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060084 nlsr.getConfParameter().setNetwork(ndn::Name("/test"));
Vince Lehman09131122014-09-09 17:10:11 -050085
86 nlsr.initialize();
87
88 std::list<Adjacent> neighborList = neighbors.getAdjList();
89 for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
90 BOOST_CHECK_EQUAL(it->getLinkCost(), 0);
91 }
92}
93
94BOOST_AUTO_TEST_CASE(HyperbolicOff_LinkStateCost)
95{
Vince Lehman09131122014-09-09 17:10:11 -050096 // Simulate loading configuration file
Nick Gordon727d4832017-10-13 18:04:25 -050097 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://10.0.0.1"), 25,
98 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050099 neighbors.insert(neighborA);
100
Nick Gordon727d4832017-10-13 18:04:25 -0500101 Adjacent neighborB("/ndn/neighborB", ndn::util::FaceUri("udp4://10.0.0.2"), 10,
102 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -0500103 neighbors.insert(neighborB);
104
Nick Gordon727d4832017-10-13 18:04:25 -0500105 Adjacent neighborC("/ndn/neighborC", ndn::util::FaceUri("udp4://10.0.0.3"), 17,
106 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -0500107 neighbors.insert(neighborC);
108
109 nlsr.initialize();
110
111 std::list<Adjacent> neighborList = neighbors.getAdjList();
112 for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
113 BOOST_CHECK(it->getLinkCost() != 0);
114 }
115}
116
Vince Lehman7b616582014-10-17 16:25:39 -0500117BOOST_AUTO_TEST_CASE(SetEventIntervals)
118{
Vince Lehman7b616582014-10-17 16:25:39 -0500119 // Simulate loading configuration file
120 ConfParameter& conf = nlsr.getConfParameter();
121 conf.setAdjLsaBuildInterval(3);
122 conf.setFirstHelloInterval(6);
123 conf.setRoutingCalcInterval(9);
124
125 nlsr.initialize();
126
Vince Lehman50df6b72015-03-03 12:06:40 -0600127 const Lsdb& lsdb = nlsr.getLsdb();
Vince Lehman7b616582014-10-17 16:25:39 -0500128 const RoutingTable& rt = nlsr.getRoutingTable();
129
Vince Lehman50df6b72015-03-03 12:06:40 -0600130 BOOST_CHECK_EQUAL(lsdb.getAdjLsaBuildInterval(), ndn::time::seconds(3));
Vince Lehman7b616582014-10-17 16:25:39 -0500131 BOOST_CHECK_EQUAL(nlsr.getFirstHelloInterval(), 6);
132 BOOST_CHECK_EQUAL(rt.getRoutingCalcInterval(), ndn::time::seconds(9));
133}
134
Nick Gordond5c1a372016-10-31 13:56:23 -0500135BOOST_AUTO_TEST_CASE(FaceCreateEvent)
136{
137 // Setting constants for the unit test
138 const uint32_t faceId = 1;
139 const std::string faceUri = "udp4://10.0.0.1:6363";
Nick Gordon727d4832017-10-13 18:04:25 -0500140 Adjacent neighbor("/ndn/neighborA", ndn::util::FaceUri(faceUri), 10,
141 Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500142 BOOST_REQUIRE_EQUAL(nlsr.getAdjacencyList().insert(neighbor), 0);
143
144 this->advanceClocks(ndn::time::milliseconds(1));
145
146 // Build, sign, and send the Face Event
147 ndn::nfd::FaceEventNotification event;
148 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
149 .setRemoteUri(faceUri)
150 .setFaceId(faceId);
151 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
152 data->setContent(event.wireEncode());
153 nlsr.getKeyChain().sign(*data);
154 face->receive(*data);
155
156 // Move the clocks forward so that the Face processes the event.
157 this->advanceClocks(ndn::time::milliseconds(1));
158
159 // Need to explicitly provide a FaceUri object, because the
160 // conversion will attempt to create Name objects.
161 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::util::FaceUri(faceUri));
162 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
163 BOOST_CHECK_EQUAL(iterator->getFaceId(), faceId);
164}
165
166BOOST_AUTO_TEST_CASE(FaceCreateEventNoMatch)
167{
168 // Setting constants for the unit test
169 const uint32_t faceId = 1;
170 const std::string eventUri = "udp4://10.0.0.1:6363";
171 const std::string neighborUri = "udp4://10.0.0.2:6363";
Nick Gordon727d4832017-10-13 18:04:25 -0500172 Adjacent neighbor("/ndn/neighborA", ndn::util::FaceUri(neighborUri), 10,
173 Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500174 nlsr.getAdjacencyList().insert(neighbor);
175
176 // Build, sign, and send the Face Event
177 ndn::nfd::FaceEventNotification event;
178 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
179 .setRemoteUri(eventUri)
180 .setFaceId(faceId);
181 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
182 data->setContent(event.wireEncode());
183 nlsr.getKeyChain().sign(*data);
184 face->receive(*data);
185
186 // Move the clocks forward so that the Face processes the event.
187 this->advanceClocks(ndn::time::milliseconds(1));
188
189 // The Face URIs did not match, so this neighbor should be unconfigured.
190 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::util::FaceUri(neighborUri));
191 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
192 BOOST_CHECK_EQUAL(iterator->getFaceId(), 0);
193}
194
195BOOST_AUTO_TEST_CASE(FaceCreateEventAlreadyConfigured)
196{
197 // Setting constants for the unit test
198 const uint32_t eventFaceId = 1;
199 const uint32_t neighborFaceId = 2;
200 const std::string faceUri = "udp4://10.0.0.1:6363";
Nick Gordon727d4832017-10-13 18:04:25 -0500201 Adjacent neighbor("/ndn/neighborA", ndn::util::FaceUri(faceUri), 10,
202 Adjacent::STATUS_ACTIVE, 0, neighborFaceId);
Nick Gordond5c1a372016-10-31 13:56:23 -0500203 nlsr.getAdjacencyList().insert(neighbor);
204
205 // Build, sign, and send the Face Event
206 ndn::nfd::FaceEventNotification event;
207 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
208 .setRemoteUri(faceUri)
209 .setFaceId(eventFaceId);
210 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
211 data->setContent(event.wireEncode());
212 nlsr.getKeyChain().sign(*data);
213 face->receive(*data);
214
215 // Move the clocks forward so that the Face processes the event.
216 this->advanceClocks(ndn::time::milliseconds(1));
217
218 // Since the neighbor was already configured, this (simply erroneous) event should have no effect.
219 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::util::FaceUri(faceUri));
220 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
221 BOOST_CHECK_EQUAL(iterator->getFaceId(), neighborFaceId);
222}
223
Vince Lehman02e32992015-03-11 12:31:20 -0500224BOOST_FIXTURE_TEST_CASE(FaceDestroyEvent, UnitTestTimeFixture)
225{
Nick Gordon727d4832017-10-13 18:04:25 -0500226 std::shared_ptr<ndn::util::DummyClientFace> face =
227 std::make_shared<ndn::util::DummyClientFace>(g_ioService);
dmcoomes9f936662017-03-02 10:33:09 -0600228 Nlsr nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain);
Vince Lehman41b173e2015-05-07 14:13:26 -0500229 Lsdb& lsdb = nlsr.getLsdb();
Vince Lehman02e32992015-03-11 12:31:20 -0500230
231 // Simulate loading configuration file
232 ConfParameter& conf = nlsr.getConfParameter();
233 conf.setNetwork("/ndn");
234 conf.setSiteName("/site");
235 conf.setRouterName("/%C1.router/this-router");
236 conf.setAdjLsaBuildInterval(0);
237 conf.setRoutingCalcInterval(0);
238
239 // Add active neighbors
240 AdjacencyList& neighbors = nlsr.getAdjacencyList();
Vince Lehman02e32992015-03-11 12:31:20 -0500241 uint64_t destroyFaceId = 128;
242
243 // Create a neighbor whose Face will be destroyed
Nick Gordone9733ed2017-04-26 10:48:39 -0500244 Adjacent failNeighbor("/ndn/neighborA", ndn::util::FaceUri("udp4://10.0.0.1"), 10, Adjacent::STATUS_ACTIVE, 0,
Vince Lehman02e32992015-03-11 12:31:20 -0500245 destroyFaceId);
246 neighbors.insert(failNeighbor);
247
248 // Create an additional neighbor so an adjacency LSA can be built after the face is destroyed
Nick Gordon727d4832017-10-13 18:04:25 -0500249 Adjacent otherNeighbor("/ndn/neighborB", ndn::util::FaceUri("udp4://10.0.0.2"), 10,
250 Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman02e32992015-03-11 12:31:20 -0500251 neighbors.insert(otherNeighbor);
252
253 nlsr.initialize();
254
255 // Simulate successful HELLO responses
Vince Lehman41b173e2015-05-07 14:13:26 -0500256 lsdb.scheduleAdjLsaBuild();
257
258 // Set up adjacency LSAs
259 // This router
Nick Gordon727d4832017-10-13 18:04:25 -0500260 Adjacent thisRouter(conf.getRouterPrefix(), ndn::util::FaceUri("udp4://10.0.0.3"), 10,
261 Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman41b173e2015-05-07 14:13:26 -0500262
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600263 AdjLsa ownAdjLsa(conf.getRouterPrefix(), 10, ndn::time::system_clock::now(), 1, neighbors);
Vince Lehman41b173e2015-05-07 14:13:26 -0500264 lsdb.installAdjLsa(ownAdjLsa);
265
266 // Router that will fail
267 AdjacencyList failAdjacencies;
268 failAdjacencies.insert(thisRouter);
269
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600270 AdjLsa failAdjLsa("/ndn/neighborA", 10,
Vince Lehman41b173e2015-05-07 14:13:26 -0500271 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, failAdjacencies);
272
273 lsdb.installAdjLsa(failAdjLsa);
274
275 // Other router
276 AdjacencyList otherAdjacencies;
277 otherAdjacencies.insert(thisRouter);
278
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600279 AdjLsa otherAdjLsa("/ndn/neighborB", 10,
Vince Lehman41b173e2015-05-07 14:13:26 -0500280 ndn::time::system_clock::now() + ndn::time::seconds(3600), 1, otherAdjacencies);
281
282 lsdb.installAdjLsa(otherAdjLsa);
Vince Lehman02e32992015-03-11 12:31:20 -0500283
284 // Run the scheduler to build an adjacency LSA
285 this->advanceClocks(ndn::time::milliseconds(1));
286
287 // Make sure an adjacency LSA was built
Nick Gordon727d4832017-10-13 18:04:25 -0500288 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix())
289 .append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehman41b173e2015-05-07 14:13:26 -0500290 AdjLsa* lsa = lsdb.findAdjLsa(key);
Vince Lehman02e32992015-03-11 12:31:20 -0500291 BOOST_REQUIRE(lsa != nullptr);
292
alvy46ccaae2015-06-25 14:13:39 -0500293 uint32_t lastAdjLsaSeqNo = lsa->getLsSeqNo();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500294 nlsr.getLsdb().getSequencingManager().setAdjLsaSeq(lastAdjLsaSeqNo);
Vince Lehman02e32992015-03-11 12:31:20 -0500295
296 // Make sure the routing table was calculated
297 RoutingTableEntry* rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
298 BOOST_REQUIRE(rtEntry != nullptr);
Nick Gordonff9a6272017-10-12 13:38:29 -0500299 BOOST_REQUIRE_EQUAL(rtEntry->getNexthopList().size(), 1);
Vince Lehman02e32992015-03-11 12:31:20 -0500300
301 // Receive FaceEventDestroyed notification
302 ndn::nfd::FaceEventNotification event;
303 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
304 .setFaceId(destroyFaceId);
305
dmcoomes9f936662017-03-02 10:33:09 -0600306 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Vince Lehman02e32992015-03-11 12:31:20 -0500307 data->setContent(event.wireEncode());
308 nlsr.getKeyChain().sign(*data);
309
310 face->receive(*data);
311
312 // Run the scheduler to build an adjacency LSA
313 this->advanceClocks(ndn::time::milliseconds(1));
314
315 Adjacent updatedNeighbor = neighbors.getAdjacent(failNeighbor.getName());
316
317 BOOST_CHECK_EQUAL(updatedNeighbor.getFaceId(), 0);
318 BOOST_CHECK_EQUAL(updatedNeighbor.getInterestTimedOutNo(),
319 nlsr.getConfParameter().getInterestRetryNumber());
320 BOOST_CHECK_EQUAL(updatedNeighbor.getStatus(), Adjacent::STATUS_INACTIVE);
alvy46ccaae2015-06-25 14:13:39 -0500321
322 lsa = lsdb.findAdjLsa(key);
323 BOOST_REQUIRE(lsa != nullptr);
324
325 BOOST_CHECK_EQUAL(lsa->getLsSeqNo(), lastAdjLsaSeqNo + 1);
Vince Lehman02e32992015-03-11 12:31:20 -0500326
327 // Make sure the routing table was recalculated
328 rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
329 BOOST_CHECK(rtEntry == nullptr);
330}
331
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500332BOOST_AUTO_TEST_CASE(GetCertificate)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500333{
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500334 // Create certificate
335 ndn::Name identity("/TestNLSR/identity");
336 identity.appendVersion();
337
338 ndn::KeyChain keyChain;
339 keyChain.createIdentity(identity);
340 ndn::Name certName = keyChain.getDefaultCertificateNameForIdentity(identity);
dmcoomes9f936662017-03-02 10:33:09 -0600341 std::shared_ptr<ndn::IdentityCertificate> certificate = keyChain.getCertificate(certName);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500342
343 const ndn::Name certKey = certificate->getName().getPrefix(-1);
344
345 BOOST_CHECK(nlsr.getCertificate(certKey) == nullptr);
346
347 // Certificate should be retrievable from the CertificateStore
348 nlsr.loadCertToPublish(certificate);
349
350 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
351
352 nlsr.getCertificateStore().clear();
353
354 // Certificate should be retrievable from the cache
355 nlsr.addCertificateToCache(certificate);
356 this->advanceClocks(ndn::time::milliseconds(10));
357
358 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
359}
360
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500361BOOST_AUTO_TEST_CASE(SetRouterCommandPrefix)
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500362{
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500363 // Simulate loading configuration file
364 ConfParameter& conf = nlsr.getConfParameter();
365 conf.setNetwork("/ndn");
366 conf.setSiteName("/site");
367 conf.setRouterName("/%C1.router/this-router");
368
369 nlsr.initialize();
370
371 BOOST_CHECK_EQUAL(nlsr.getLsdbDatasetHandler().getRouterNameCommandPrefix(),
372 ndn::Name("/ndn/site/%C1.router/this-router/lsdb"));
373}
374
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500375BOOST_AUTO_TEST_CASE(BuildAdjLsaAfterHelloResponse)
376{
377 // Configure NLSR
378 ConfParameter& conf = nlsr.getConfParameter();
379 conf.setNetwork("/ndn");
380 conf.setSiteName("/site");
381
382 ndn::Name routerName("/%C1.Router/this-router");
383 conf.setRouterName(routerName);
384
385 conf.setAdjLsaBuildInterval(1);
386
387 // Add neighbors
388 // Router A
389 ndn::Name neighborAName("/ndn/site/%C1.router/routerA");
Nick Gordon727d4832017-10-13 18:04:25 -0500390 Adjacent neighborA(neighborAName, ndn::util::FaceUri("udp4://10.0.0.1"), 0,
391 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500392 neighbors.insert(neighborA);
393
394 // Router B
395 ndn::Name neighborBName("/ndn/site/%C1.router/routerB");
Nick Gordon727d4832017-10-13 18:04:25 -0500396 Adjacent neighborB(neighborBName, ndn::util::FaceUri("udp4://10.0.0.1"), 0,
397 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500398 neighbors.insert(neighborB);
399
400 nlsr.initialize();
401 this->advanceClocks(ndn::time::milliseconds(1));
402
403 // Receive HELLO response from Router A
404 receiveHelloData(neighborAName, conf.getRouterPrefix());
405 this->advanceClocks(ndn::time::seconds(1));
406
Nick Gordon727d4832017-10-13 18:04:25 -0500407 ndn::Name lsaKey = ndn::Name(conf.getRouterPrefix()).append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500408
409 // Adjacency LSA should be built even though other router is INACTIVE
410 AdjLsa* lsa = lsdb.findAdjLsa(lsaKey);
411 BOOST_REQUIRE(lsa != nullptr);
Nick Gordonff9a6272017-10-12 13:38:29 -0500412 BOOST_CHECK_EQUAL(lsa->getAdl().size(), 1);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500413
414 // Receive HELLO response from Router B
415 receiveHelloData(neighborBName, conf.getRouterPrefix());
416
417 // Both routers become INACTIVE and HELLO Interests have timed out
418 for (Adjacent& adjacency : neighbors.getAdjList()) {
419 adjacency.setStatus(Adjacent::STATUS_INACTIVE);
420 adjacency.setInterestTimedOutNo(HELLO_RETRIES_DEFAULT);
421 }
422
423 this->advanceClocks(ndn::time::seconds(1));
424
425 // Adjacency LSA should have been removed since this router's adjacencies are INACTIVE
426 // and have timed out
427 lsa = lsdb.findAdjLsa(lsaKey);
428 BOOST_CHECK(lsa == nullptr);
429
430 // Receive HELLO response from Router A and B
431 receiveHelloData(neighborAName, conf.getRouterPrefix());
432 receiveHelloData(neighborBName, conf.getRouterPrefix());
433 this->advanceClocks(ndn::time::seconds(1));
434
435 // Adjacency LSA should be built
436 lsa = lsdb.findAdjLsa(lsaKey);
437 BOOST_REQUIRE(lsa != nullptr);
Nick Gordonff9a6272017-10-12 13:38:29 -0500438 BOOST_CHECK_EQUAL(lsa->getAdl().size(), 2);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500439}
440
Nick Gordon9461afb2017-04-25 15:54:50 -0500441BOOST_AUTO_TEST_CASE(CanonizeUris)
442{
443 ndn::Name neighborAName("/ndn/site/%C1.router/routerA");
Nick Gordone9733ed2017-04-26 10:48:39 -0500444 ndn::util::FaceUri faceUriA("udp://10.0.0.1");
445 Adjacent neighborA(neighborAName, faceUriA, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500446 neighbors.insert(neighborA);
447
448 ndn::Name neighborBName("/ndn/site/%C1.router/routerB");
Nick Gordone9733ed2017-04-26 10:48:39 -0500449 ndn::util::FaceUri faceUriB("udp://10.0.0.2");
450 Adjacent neighborB(neighborBName, faceUriB, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500451 neighbors.insert(neighborB);
452
453 int nCanonizationsLeft = nlsr.getAdjacencyList().getAdjList().size();
Nick Gordon922714a2017-06-13 14:12:02 -0500454 std::function<void(void)> finallyCallback = [this] {
455 nlsr.initialize();
456 };
Nick Gordon9461afb2017-04-25 15:54:50 -0500457 std::function<void(std::list<Adjacent>::iterator)> thenCallback =
Nick Gordon727d4832017-10-13 18:04:25 -0500458 [this, &thenCallback, &finallyCallback, &nCanonizationsLeft]
459 (std::list<Adjacent>::iterator iterator) {
Nick Gordon9461afb2017-04-25 15:54:50 -0500460 nCanonizationsLeft--;
Nick Gordon922714a2017-06-13 14:12:02 -0500461 nlsr.canonizeNeighborUris(iterator, thenCallback, finallyCallback);
Nick Gordon9461afb2017-04-25 15:54:50 -0500462 };
463 nlsr.canonizeNeighborUris(nlsr.getAdjacencyList().getAdjList().begin(),
Nick Gordon727d4832017-10-13 18:04:25 -0500464 [&thenCallback, &finallyCallback]
465 (std::list<Adjacent>::iterator iterator) {
Nick Gordon9461afb2017-04-25 15:54:50 -0500466 thenCallback(iterator);
Nick Gordon922714a2017-06-13 14:12:02 -0500467 },
468 finallyCallback);
Nick Gordon9461afb2017-04-25 15:54:50 -0500469 while (nCanonizationsLeft != 0) {
470 this->advanceClocks(ndn::time::milliseconds(1));
471 }
472
Nick Gordone9733ed2017-04-26 10:48:39 -0500473 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborAName).getFaceUri(),
474 ndn::util::FaceUri("udp4://10.0.0.1:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500475
Nick Gordone9733ed2017-04-26 10:48:39 -0500476 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborBName).getFaceUri(),
477 ndn::util::FaceUri("udp4://10.0.0.2:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500478}
479
Nick Gordond5c1a372016-10-31 13:56:23 -0500480BOOST_AUTO_TEST_CASE(FaceDatasetFetchSuccess)
481{
482 bool hasResult = false;
483 nlsr.m_validator.m_shouldValidate = false;
484
485 nlsr.initializeFaces([&hasResult] (const std::vector<ndn::nfd::FaceStatus>& faces) {
486 hasResult = true;
487 BOOST_CHECK_EQUAL(faces.size(), 2);
488 BOOST_CHECK_EQUAL(faces.front().getFaceId(), 25401);
489 BOOST_CHECK_EQUAL(faces.back().getFaceId(), 25402);
490 },
491 [] (uint32_t code, const std::string& reason) {});
492
493 this->advanceClocks(ndn::time::milliseconds(500));
494
495 ndn::nfd::FaceStatus payload1;
496 payload1.setFaceId(25401);
497 ndn::nfd::FaceStatus payload2;
498 payload2.setFaceId(25402);
499 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
500
501 this->advanceClocks(ndn::time::milliseconds(500));
502 BOOST_CHECK(hasResult);
503}
504
505BOOST_AUTO_TEST_CASE(FaceDatasetFetchFailure)
506{
507 nlsr.m_validator.m_shouldValidate = false;
508 nlsr.initializeFaces([](const std::vector<ndn::nfd::FaceStatus>& faces) {},
509 [this](uint32_t code, const std::string& reason){
510 this->nFailureCallbacks++;
511 });
512 this->advanceClocks(ndn::time::milliseconds(500));
513
514 ndn::Name payload;
515 this->sendDataset("/localhost/nfd/faces/list", payload);
516 this->advanceClocks(ndn::time::milliseconds(500));
517
518 BOOST_CHECK_EQUAL(nFailureCallbacks, 1);
519 BOOST_CHECK_EQUAL(nSuccessCallbacks, 0);
520}
521
522BOOST_AUTO_TEST_CASE(FaceDatasetProcess)
523{
Nick Gordon727d4832017-10-13 18:04:25 -0500524 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://192.168.0.100:6363"), 25,
525 Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500526 neighbors.insert(neighborA);
527
Nick Gordon727d4832017-10-13 18:04:25 -0500528 Adjacent neighborB("/ndn/neighborB", ndn::util::FaceUri("udp4://192.168.0.101:6363"), 10,
529 Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500530 neighbors.insert(neighborB);
531
532 ndn::nfd::FaceStatus payload1;
533 payload1.setFaceId(1)
534 .setRemoteUri("udp4://192.168.0.100:6363");
535 ndn::nfd::FaceStatus payload2;
536 payload2.setFaceId(2)
537 .setRemoteUri("udp4://192.168.0.101:6363");
538 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload1, payload2};
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(), payload1.getFaceId());
546 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborB").getFaceId(), payload2.getFaceId());
547}
548
549BOOST_AUTO_TEST_CASE(UnconfiguredNeighbor)
550{
Nick Gordon727d4832017-10-13 18:04:25 -0500551 Adjacent neighborA("/ndn/neighborA", ndn::util::FaceUri("udp4://192.168.0.100:6363"), 25,
552 Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500553 neighbors.insert(neighborA);
554
555 ndn::nfd::FaceStatus payload;
556 payload.setFaceId(1)
557 .setRemoteUri("udp4://192.168.0.101:6363"); // Note dissimilar Face URI.
558 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload};
559
560 nlsr.processFaceDataset(faceStatuses);
561 this->advanceClocks(ndn::time::milliseconds(100));
562
563 AdjacencyList adjList = nlsr.getAdjacencyList();
564
565 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), 0);
566}
567
568BOOST_AUTO_TEST_CASE(FaceDatasetPeriodicFetch)
569{
570 int nNameMatches = 0;
571 ndn::Name datasetPrefix("/localhost/nfd/faces/list");
572 ndn::nfd::CommandOptions options;
573 ndn::time::milliseconds defaultTimeout = options.getTimeout();
574
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500575 int fetchInterval(1);
Nick Gordond5c1a372016-10-31 13:56:23 -0500576 ConfParameter& conf = nlsr.getConfParameter();
577 conf.setFaceDatasetFetchInterval(fetchInterval);
578 conf.setFaceDatasetFetchTries(0);
579
580 nlsr.initializeFaces(std::bind(&Nlsr::processFaceDataset, &nlsr, _1),
581 std::bind(&Nlsr::onFaceDatasetFetchTimeout, &nlsr, _1, _2, 0));
582
583 // Elapse the default timeout time of the interest.
584 this->advanceClocks(defaultTimeout);
585
586 // Check that we have one interest for face list in the sent interests.
587 for (const ndn::Interest& interest : face->sentInterests) {
588 if (datasetPrefix.isPrefixOf(interest.getName())) {
589 nNameMatches++;
590 }
591 }
592 BOOST_CHECK_EQUAL(nNameMatches, 1);
593
594 // Elapse the clock by the reschedule time (that we set)
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500595 this->advanceClocks(ndn::time::seconds(fetchInterval));
Nick Gordond5c1a372016-10-31 13:56:23 -0500596 // Elapse the default timeout on the interest.
597 this->advanceClocks(defaultTimeout);
598 // Plus a little more to let the events process.
599 this->advanceClocks(ndn::time::seconds(1));
600
601 // Check that we now have two interests
602 nNameMatches = 0;
603 for (const ndn::Interest& interest : face->sentInterests) {
604 if (datasetPrefix.isPrefixOf(interest.getName())) {
605 nNameMatches++;
606 }
607 }
608 BOOST_CHECK_EQUAL(nNameMatches, 2);
609}
610
Vince Lehman09131122014-09-09 17:10:11 -0500611BOOST_AUTO_TEST_SUITE_END()
612
Nick Gordonfad8e252016-08-11 14:21:38 -0500613} // namespace test
614} // namespace nlsr