blob: 180c295cb944b7e0fb5418a3b9928d7abe6be47b [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()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050038 : nlsr(m_ioService, m_scheduler, m_face, m_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 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050044 nlsr.getConfParameter().setNetwork("/ndn");
45 nlsr.getConfParameter().setSiteName("/site");
46 nlsr.getConfParameter().setRouterName("/%C1.Router/this-router");
47 nlsr.getConfParameter().buildRouterPrefix();
48
49 addIdentity(nlsr.getConfParameter().getRouterPrefix());
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050050 }
51
52 void
53 receiveHelloData(const ndn::Name& sender, const ndn::Name& receiver)
54 {
55 ndn::Name dataName(sender);
56 dataName.append("NLSR").append("INFO").append(receiver.wireEncode()).appendVersion();
57
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050058 ndn::Data data(dataName);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050059
60 nlsr.m_helloProtocol.onContentValidated(data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050061 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050062
63public:
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050064 Nlsr nlsr;
65 Lsdb& lsdb;
66 AdjacencyList& neighbors;
Nick Gordond5c1a372016-10-31 13:56:23 -050067 uint32_t nSuccessCallbacks;
68 uint32_t nFailureCallbacks;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050069};
70
71BOOST_FIXTURE_TEST_SUITE(TestNlsr, NlsrFixture)
Vince Lehman09131122014-09-09 17:10:11 -050072
73BOOST_AUTO_TEST_CASE(HyperbolicOn_ZeroCostNeighbors)
74{
Vince Lehman09131122014-09-09 17:10:11 -050075 // Simulate loading configuration file
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050076 Adjacent neighborA("/ndn/neighborA", ndn::FaceUri("udp4://10.0.0.1"), 25,
Nick Gordon727d4832017-10-13 18:04:25 -050077 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050078 neighbors.insert(neighborA);
79
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050080 Adjacent neighborB("/ndn/neighborB", ndn::FaceUri("udp4://10.0.0.2"), 10,
Nick Gordon727d4832017-10-13 18:04:25 -050081 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050082 neighbors.insert(neighborB);
83
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050084 Adjacent neighborC("/ndn/neighborC", ndn::FaceUri("udp4://10.0.0.3"), 17,
Nick Gordon727d4832017-10-13 18:04:25 -050085 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -050086 neighbors.insert(neighborC);
87
88 nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
89
90 nlsr.initialize();
91
92 std::list<Adjacent> neighborList = neighbors.getAdjList();
93 for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
94 BOOST_CHECK_EQUAL(it->getLinkCost(), 0);
95 }
96}
97
98BOOST_AUTO_TEST_CASE(HyperbolicOff_LinkStateCost)
99{
Vince Lehman09131122014-09-09 17:10:11 -0500100 // Simulate loading configuration file
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500101 Adjacent neighborA("/ndn/neighborA", ndn::FaceUri("udp4://10.0.0.1"), 25,
Nick Gordon727d4832017-10-13 18:04:25 -0500102 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -0500103 neighbors.insert(neighborA);
104
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500105 Adjacent neighborB("/ndn/neighborB", ndn::FaceUri("udp4://10.0.0.2"), 10,
Nick Gordon727d4832017-10-13 18:04:25 -0500106 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -0500107 neighbors.insert(neighborB);
108
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500109 Adjacent neighborC("/ndn/neighborC", ndn::FaceUri("udp4://10.0.0.3"), 17,
Nick Gordon727d4832017-10-13 18:04:25 -0500110 Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehman09131122014-09-09 17:10:11 -0500111 neighbors.insert(neighborC);
112
113 nlsr.initialize();
114
115 std::list<Adjacent> neighborList = neighbors.getAdjList();
116 for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
117 BOOST_CHECK(it->getLinkCost() != 0);
118 }
119}
120
Vince Lehman7b616582014-10-17 16:25:39 -0500121BOOST_AUTO_TEST_CASE(SetEventIntervals)
122{
Vince Lehman7b616582014-10-17 16:25:39 -0500123 // Simulate loading configuration file
124 ConfParameter& conf = nlsr.getConfParameter();
125 conf.setAdjLsaBuildInterval(3);
126 conf.setFirstHelloInterval(6);
127 conf.setRoutingCalcInterval(9);
128
129 nlsr.initialize();
130
Vince Lehman50df6b72015-03-03 12:06:40 -0600131 const Lsdb& lsdb = nlsr.getLsdb();
Vince Lehman7b616582014-10-17 16:25:39 -0500132 const RoutingTable& rt = nlsr.getRoutingTable();
133
Vince Lehman50df6b72015-03-03 12:06:40 -0600134 BOOST_CHECK_EQUAL(lsdb.getAdjLsaBuildInterval(), ndn::time::seconds(3));
Vince Lehman7b616582014-10-17 16:25:39 -0500135 BOOST_CHECK_EQUAL(nlsr.getFirstHelloInterval(), 6);
136 BOOST_CHECK_EQUAL(rt.getRoutingCalcInterval(), ndn::time::seconds(9));
137}
138
Nick Gordond5c1a372016-10-31 13:56:23 -0500139BOOST_AUTO_TEST_CASE(FaceCreateEvent)
140{
141 // Setting constants for the unit test
142 const uint32_t faceId = 1;
143 const std::string faceUri = "udp4://10.0.0.1:6363";
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500144
145 Adjacent neighbor("/ndn/neighborA", ndn::FaceUri(faceUri), 10,
Nick Gordon727d4832017-10-13 18:04:25 -0500146 Adjacent::STATUS_INACTIVE, 0, 0);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500147
Nick Gordond5c1a372016-10-31 13:56:23 -0500148 BOOST_REQUIRE_EQUAL(nlsr.getAdjacencyList().insert(neighbor), 0);
149
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500150 this->advanceClocks(ndn::time::milliseconds(1), 10);
Nick Gordond5c1a372016-10-31 13:56:23 -0500151
152 // Build, sign, and send the Face Event
153 ndn::nfd::FaceEventNotification event;
154 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
155 .setRemoteUri(faceUri)
156 .setFaceId(faceId);
157 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
158 data->setContent(event.wireEncode());
159 nlsr.getKeyChain().sign(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500160 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500161
162 // Move the clocks forward so that the Face processes the event.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500163 this->advanceClocks(ndn::time::milliseconds(1), 10);
Nick Gordond5c1a372016-10-31 13:56:23 -0500164
165 // Need to explicitly provide a FaceUri object, because the
166 // conversion will attempt to create Name objects.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500167 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::FaceUri(faceUri));
Nick Gordond5c1a372016-10-31 13:56:23 -0500168 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
169 BOOST_CHECK_EQUAL(iterator->getFaceId(), faceId);
170}
171
172BOOST_AUTO_TEST_CASE(FaceCreateEventNoMatch)
173{
174 // Setting constants for the unit test
175 const uint32_t faceId = 1;
176 const std::string eventUri = "udp4://10.0.0.1:6363";
177 const std::string neighborUri = "udp4://10.0.0.2:6363";
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500178
179 Adjacent neighbor("/ndn/neighborA", ndn::FaceUri(neighborUri), 10,
Nick Gordon727d4832017-10-13 18:04:25 -0500180 Adjacent::STATUS_INACTIVE, 0, 0);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500181
Nick Gordond5c1a372016-10-31 13:56:23 -0500182 nlsr.getAdjacencyList().insert(neighbor);
183
184 // Build, sign, and send the Face Event
185 ndn::nfd::FaceEventNotification event;
186 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
187 .setRemoteUri(eventUri)
188 .setFaceId(faceId);
189 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
190 data->setContent(event.wireEncode());
191 nlsr.getKeyChain().sign(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500192 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500193
194 // Move the clocks forward so that the Face processes the event.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500195 this->advanceClocks(ndn::time::milliseconds(1), 10);
Nick Gordond5c1a372016-10-31 13:56:23 -0500196
197 // The Face URIs did not match, so this neighbor should be unconfigured.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500198 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::FaceUri(neighborUri));
Nick Gordond5c1a372016-10-31 13:56:23 -0500199 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
200 BOOST_CHECK_EQUAL(iterator->getFaceId(), 0);
201}
202
203BOOST_AUTO_TEST_CASE(FaceCreateEventAlreadyConfigured)
204{
205 // Setting constants for the unit test
206 const uint32_t eventFaceId = 1;
207 const uint32_t neighborFaceId = 2;
208 const std::string faceUri = "udp4://10.0.0.1:6363";
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500209
210 Adjacent neighbor("/ndn/neighborA", ndn::FaceUri(faceUri), 10,
Nick Gordon727d4832017-10-13 18:04:25 -0500211 Adjacent::STATUS_ACTIVE, 0, neighborFaceId);
Nick Gordond5c1a372016-10-31 13:56:23 -0500212 nlsr.getAdjacencyList().insert(neighbor);
213
214 // Build, sign, and send the Face Event
215 ndn::nfd::FaceEventNotification event;
216 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
217 .setRemoteUri(faceUri)
218 .setFaceId(eventFaceId);
219 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
220 data->setContent(event.wireEncode());
221 nlsr.getKeyChain().sign(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500222 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500223
224 // Move the clocks forward so that the Face processes the event.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500225 this->advanceClocks(ndn::time::milliseconds(1), 10);
Nick Gordond5c1a372016-10-31 13:56:23 -0500226
227 // Since the neighbor was already configured, this (simply erroneous) event should have no effect.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500228 auto iterator = nlsr.getAdjacencyList().findAdjacent(ndn::FaceUri(faceUri));
Nick Gordond5c1a372016-10-31 13:56:23 -0500229 BOOST_REQUIRE(iterator != nlsr.getAdjacencyList().end());
230 BOOST_CHECK_EQUAL(iterator->getFaceId(), neighborFaceId);
231}
232
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500233BOOST_AUTO_TEST_CASE(FaceDestroyEvent)
Vince Lehman02e32992015-03-11 12:31:20 -0500234{
Vince Lehman02e32992015-03-11 12:31:20 -0500235 // Simulate loading configuration file
236 ConfParameter& conf = nlsr.getConfParameter();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500237
Vince Lehman02e32992015-03-11 12:31:20 -0500238 conf.setAdjLsaBuildInterval(0);
239 conf.setRoutingCalcInterval(0);
240
241 // Add active neighbors
242 AdjacencyList& neighbors = nlsr.getAdjacencyList();
Vince Lehman02e32992015-03-11 12:31:20 -0500243 uint64_t destroyFaceId = 128;
244
245 // Create a neighbor whose Face will be destroyed
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500246 Adjacent failNeighbor("/ndn/neighborA", ndn::FaceUri("udp4://10.0.0.1"),
247 10, Adjacent::STATUS_ACTIVE, 0, destroyFaceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500248 neighbors.insert(failNeighbor);
249
250 // Create an additional neighbor so an adjacency LSA can be built after the face is destroyed
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500251 Adjacent otherNeighbor("/ndn/neighborB", ndn::FaceUri("udp4://10.0.0.2"),
252 10, Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman02e32992015-03-11 12:31:20 -0500253 neighbors.insert(otherNeighbor);
254
255 nlsr.initialize();
256
257 // Simulate successful HELLO responses
Vince Lehman41b173e2015-05-07 14:13:26 -0500258 lsdb.scheduleAdjLsaBuild();
259
260 // Set up adjacency LSAs
261 // This router
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500262 Adjacent thisRouter(conf.getRouterPrefix(), ndn::FaceUri("udp4://10.0.0.3"),
263 10, Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman41b173e2015-05-07 14:13:26 -0500264
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500265 AdjLsa ownAdjLsa(conf.getRouterPrefix(), 10,
266 ndn::time::system_clock::now(), 1, neighbors);
Vince Lehman41b173e2015-05-07 14:13:26 -0500267 lsdb.installAdjLsa(ownAdjLsa);
268
269 // Router that will fail
270 AdjacencyList failAdjacencies;
271 failAdjacencies.insert(thisRouter);
272
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600273 AdjLsa failAdjLsa("/ndn/neighborA", 10,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500274 ndn::time::system_clock::now() + ndn::time::seconds(3600),
275 1, failAdjacencies);
Vince Lehman41b173e2015-05-07 14:13:26 -0500276
277 lsdb.installAdjLsa(failAdjLsa);
278
279 // Other router
280 AdjacencyList otherAdjacencies;
281 otherAdjacencies.insert(thisRouter);
282
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600283 AdjLsa otherAdjLsa("/ndn/neighborB", 10,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500284 ndn::time::system_clock::now() + ndn::time::seconds(3600),
285 1, otherAdjacencies);
Vince Lehman41b173e2015-05-07 14:13:26 -0500286
287 lsdb.installAdjLsa(otherAdjLsa);
Vince Lehman02e32992015-03-11 12:31:20 -0500288
289 // Run the scheduler to build an adjacency LSA
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500290 this->advanceClocks(ndn::time::milliseconds(1), 10);
Vince Lehman02e32992015-03-11 12:31:20 -0500291
292 // Make sure an adjacency LSA was built
Nick Gordon727d4832017-10-13 18:04:25 -0500293 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix())
294 .append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehman41b173e2015-05-07 14:13:26 -0500295 AdjLsa* lsa = lsdb.findAdjLsa(key);
Vince Lehman02e32992015-03-11 12:31:20 -0500296 BOOST_REQUIRE(lsa != nullptr);
297
alvy46ccaae2015-06-25 14:13:39 -0500298 uint32_t lastAdjLsaSeqNo = lsa->getLsSeqNo();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500299 nlsr.getLsdb().getSequencingManager().setAdjLsaSeq(lastAdjLsaSeqNo);
Vince Lehman02e32992015-03-11 12:31:20 -0500300
301 // Make sure the routing table was calculated
302 RoutingTableEntry* rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
303 BOOST_REQUIRE(rtEntry != nullptr);
Nick Gordonff9a6272017-10-12 13:38:29 -0500304 BOOST_REQUIRE_EQUAL(rtEntry->getNexthopList().size(), 1);
Vince Lehman02e32992015-03-11 12:31:20 -0500305
306 // Receive FaceEventDestroyed notification
307 ndn::nfd::FaceEventNotification event;
308 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
309 .setFaceId(destroyFaceId);
310
dmcoomes9f936662017-03-02 10:33:09 -0600311 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Vince Lehman02e32992015-03-11 12:31:20 -0500312 data->setContent(event.wireEncode());
313 nlsr.getKeyChain().sign(*data);
314
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500315 m_face.receive(*data);
Vince Lehman02e32992015-03-11 12:31:20 -0500316
317 // Run the scheduler to build an adjacency LSA
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500318 this->advanceClocks(ndn::time::milliseconds(1), 10);
Vince Lehman02e32992015-03-11 12:31:20 -0500319
320 Adjacent updatedNeighbor = neighbors.getAdjacent(failNeighbor.getName());
321
322 BOOST_CHECK_EQUAL(updatedNeighbor.getFaceId(), 0);
323 BOOST_CHECK_EQUAL(updatedNeighbor.getInterestTimedOutNo(),
324 nlsr.getConfParameter().getInterestRetryNumber());
325 BOOST_CHECK_EQUAL(updatedNeighbor.getStatus(), Adjacent::STATUS_INACTIVE);
alvy46ccaae2015-06-25 14:13:39 -0500326
327 lsa = lsdb.findAdjLsa(key);
328 BOOST_REQUIRE(lsa != nullptr);
329
330 BOOST_CHECK_EQUAL(lsa->getLsSeqNo(), lastAdjLsaSeqNo + 1);
Vince Lehman02e32992015-03-11 12:31:20 -0500331
332 // Make sure the routing table was recalculated
333 rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName());
334 BOOST_CHECK(rtEntry == nullptr);
335}
336
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500337BOOST_AUTO_TEST_CASE(GetCertificate)
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500338{
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500339 // Create certificate
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500340 ndn::Name identityName("/TestNLSR/identity");
341 identityName.appendVersion();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500342
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500343 ndn::security::pib::Identity identity = nlsr.getKeyChain().
344 createIdentity(identityName);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500345
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500346 ndn::security::v2::Certificate certificate =
347 identity.getDefaultKey().getDefaultCertificate();
348
349 const ndn::Name certKey = certificate.getKeyName();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500350
351 BOOST_CHECK(nlsr.getCertificate(certKey) == nullptr);
352
353 // Certificate should be retrievable from the CertificateStore
354 nlsr.loadCertToPublish(certificate);
355
356 BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr);
357
358 nlsr.getCertificateStore().clear();
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500359}
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 nlsr.initialize();
364
365 BOOST_CHECK_EQUAL(nlsr.getLsdbDatasetHandler().getRouterNameCommandPrefix(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500366 ndn::Name("/ndn/site/%C1.Router/this-router/lsdb"));
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500367}
368
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500369BOOST_AUTO_TEST_CASE(BuildAdjLsaAfterHelloResponse)
370{
371 // Configure NLSR
372 ConfParameter& conf = nlsr.getConfParameter();
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500373
374 conf.setAdjLsaBuildInterval(1);
375
376 // Add neighbors
377 // Router A
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500378 ndn::Name neighborAName("/ndn/site/%C1.Router/routerA");
379 Adjacent neighborA(neighborAName, ndn::FaceUri("udp4://10.0.0.1"),
380 0, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500381 neighbors.insert(neighborA);
382
383 // Router B
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500384 ndn::Name neighborBName("/ndn/site/%C1.Router/routerB");
385 Adjacent neighborB(neighborBName, ndn::FaceUri("udp4://10.0.0.1"),
386 0, Adjacent::STATUS_INACTIVE, 0, 0);
387
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500388 neighbors.insert(neighborB);
389
390 nlsr.initialize();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500391
392 this->advanceClocks(ndn::time::milliseconds(1), 10);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500393
394 // Receive HELLO response from Router A
395 receiveHelloData(neighborAName, conf.getRouterPrefix());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500396 this->advanceClocks(ndn::time::seconds(1), 10);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500397
Nick Gordon727d4832017-10-13 18:04:25 -0500398 ndn::Name lsaKey = ndn::Name(conf.getRouterPrefix()).append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500399
400 // Adjacency LSA should be built even though other router is INACTIVE
401 AdjLsa* lsa = lsdb.findAdjLsa(lsaKey);
402 BOOST_REQUIRE(lsa != nullptr);
Nick Gordonff9a6272017-10-12 13:38:29 -0500403 BOOST_CHECK_EQUAL(lsa->getAdl().size(), 1);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500404
405 // Receive HELLO response from Router B
406 receiveHelloData(neighborBName, conf.getRouterPrefix());
407
408 // Both routers become INACTIVE and HELLO Interests have timed out
409 for (Adjacent& adjacency : neighbors.getAdjList()) {
410 adjacency.setStatus(Adjacent::STATUS_INACTIVE);
411 adjacency.setInterestTimedOutNo(HELLO_RETRIES_DEFAULT);
412 }
413
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500414 this->advanceClocks(ndn::time::seconds(1), 10);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500415
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500416 // Adjacency LSA should have been removed since this router's adjacencies are
417 // INACTIVE and have timed out
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500418 lsa = lsdb.findAdjLsa(lsaKey);
419 BOOST_CHECK(lsa == nullptr);
420
421 // Receive HELLO response from Router A and B
422 receiveHelloData(neighborAName, conf.getRouterPrefix());
423 receiveHelloData(neighborBName, conf.getRouterPrefix());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500424 this->advanceClocks(ndn::time::seconds(1), 10);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500425
426 // Adjacency LSA should be built
427 lsa = lsdb.findAdjLsa(lsaKey);
428 BOOST_REQUIRE(lsa != nullptr);
Nick Gordonff9a6272017-10-12 13:38:29 -0500429 BOOST_CHECK_EQUAL(lsa->getAdl().size(), 2);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500430}
431
Nick Gordon9461afb2017-04-25 15:54:50 -0500432BOOST_AUTO_TEST_CASE(CanonizeUris)
433{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500434 ndn::Name neighborAName("/ndn/site/%C1.Router/routerA");
435 ndn::FaceUri faceUriA("udp://10.0.0.1");
Nick Gordone9733ed2017-04-26 10:48:39 -0500436 Adjacent neighborA(neighborAName, faceUriA, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500437 neighbors.insert(neighborA);
438
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500439 ndn::Name neighborBName("/ndn/site/%C1.Router/routerB");
440 ndn::FaceUri faceUriB("udp://10.0.0.2");
Nick Gordone9733ed2017-04-26 10:48:39 -0500441 Adjacent neighborB(neighborBName, faceUriB, 0, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordon9461afb2017-04-25 15:54:50 -0500442 neighbors.insert(neighborB);
443
444 int nCanonizationsLeft = nlsr.getAdjacencyList().getAdjList().size();
Nick Gordon922714a2017-06-13 14:12:02 -0500445 std::function<void(void)> finallyCallback = [this] {
446 nlsr.initialize();
447 };
Nick Gordon9461afb2017-04-25 15:54:50 -0500448 std::function<void(std::list<Adjacent>::iterator)> thenCallback =
Nick Gordon727d4832017-10-13 18:04:25 -0500449 [this, &thenCallback, &finallyCallback, &nCanonizationsLeft]
450 (std::list<Adjacent>::iterator iterator) {
Nick Gordon9461afb2017-04-25 15:54:50 -0500451 nCanonizationsLeft--;
Nick Gordon922714a2017-06-13 14:12:02 -0500452 nlsr.canonizeNeighborUris(iterator, thenCallback, finallyCallback);
Nick Gordon9461afb2017-04-25 15:54:50 -0500453 };
454 nlsr.canonizeNeighborUris(nlsr.getAdjacencyList().getAdjList().begin(),
Nick Gordon727d4832017-10-13 18:04:25 -0500455 [&thenCallback, &finallyCallback]
456 (std::list<Adjacent>::iterator iterator) {
Nick Gordon9461afb2017-04-25 15:54:50 -0500457 thenCallback(iterator);
Nick Gordon922714a2017-06-13 14:12:02 -0500458 },
459 finallyCallback);
Nick Gordon9461afb2017-04-25 15:54:50 -0500460 while (nCanonizationsLeft != 0) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500461 this->advanceClocks(ndn::time::milliseconds(1), 10);
Nick Gordon9461afb2017-04-25 15:54:50 -0500462 }
463
Nick Gordone9733ed2017-04-26 10:48:39 -0500464 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborAName).getFaceUri(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500465 ndn::FaceUri("udp4://10.0.0.1:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500466
Nick Gordone9733ed2017-04-26 10:48:39 -0500467 BOOST_CHECK_EQUAL(nlsr.getAdjacencyList().getAdjacent(neighborBName).getFaceUri(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500468 ndn::FaceUri("udp4://10.0.0.2:6363"));
Nick Gordon9461afb2017-04-25 15:54:50 -0500469}
470
Nick Gordond5c1a372016-10-31 13:56:23 -0500471BOOST_AUTO_TEST_CASE(FaceDatasetFetchSuccess)
472{
473 bool hasResult = false;
Nick Gordond5c1a372016-10-31 13:56:23 -0500474
475 nlsr.initializeFaces([&hasResult] (const std::vector<ndn::nfd::FaceStatus>& faces) {
476 hasResult = true;
477 BOOST_CHECK_EQUAL(faces.size(), 2);
478 BOOST_CHECK_EQUAL(faces.front().getFaceId(), 25401);
479 BOOST_CHECK_EQUAL(faces.back().getFaceId(), 25402);
480 },
481 [] (uint32_t code, const std::string& reason) {});
482
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500483 this->advanceClocks(ndn::time::milliseconds(100), 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500484
485 ndn::nfd::FaceStatus payload1;
486 payload1.setFaceId(25401);
487 ndn::nfd::FaceStatus payload2;
488 payload2.setFaceId(25402);
489 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
490
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500491 this->advanceClocks(ndn::time::milliseconds(100), 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500492 BOOST_CHECK(hasResult);
493}
494
495BOOST_AUTO_TEST_CASE(FaceDatasetFetchFailure)
496{
Nick Gordond5c1a372016-10-31 13:56:23 -0500497 nlsr.initializeFaces([](const std::vector<ndn::nfd::FaceStatus>& faces) {},
498 [this](uint32_t code, const std::string& reason){
499 this->nFailureCallbacks++;
500 });
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500501 this->advanceClocks(ndn::time::milliseconds(100), 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500502
503 ndn::Name payload;
504 this->sendDataset("/localhost/nfd/faces/list", payload);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500505 this->advanceClocks(ndn::time::milliseconds(100), 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500506
507 BOOST_CHECK_EQUAL(nFailureCallbacks, 1);
508 BOOST_CHECK_EQUAL(nSuccessCallbacks, 0);
509}
510
511BOOST_AUTO_TEST_CASE(FaceDatasetProcess)
512{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500513 Adjacent neighborA("/ndn/neighborA", ndn::FaceUri("udp4://192.168.0.100:6363"),
514 25, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500515 neighbors.insert(neighborA);
516
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500517 Adjacent neighborB("/ndn/neighborB", ndn::FaceUri("udp4://192.168.0.101:6363"),
518 10, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500519 neighbors.insert(neighborB);
520
521 ndn::nfd::FaceStatus payload1;
522 payload1.setFaceId(1)
523 .setRemoteUri("udp4://192.168.0.100:6363");
524 ndn::nfd::FaceStatus payload2;
525 payload2.setFaceId(2)
526 .setRemoteUri("udp4://192.168.0.101:6363");
527 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload1, payload2};
528
529 nlsr.processFaceDataset(faceStatuses);
Nick Gordond5c1a372016-10-31 13:56:23 -0500530
531 AdjacencyList adjList = nlsr.getAdjacencyList();
532
533 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), payload1.getFaceId());
534 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborB").getFaceId(), payload2.getFaceId());
535}
536
537BOOST_AUTO_TEST_CASE(UnconfiguredNeighbor)
538{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500539 Adjacent neighborA("/ndn/neighborA", ndn::FaceUri("udp4://192.168.0.100:6363"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500540 neighbors.insert(neighborA);
541
542 ndn::nfd::FaceStatus payload;
543 payload.setFaceId(1)
544 .setRemoteUri("udp4://192.168.0.101:6363"); // Note dissimilar Face URI.
545 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload};
546
547 nlsr.processFaceDataset(faceStatuses);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500548 this->advanceClocks(ndn::time::milliseconds(20), 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500549
550 AdjacencyList adjList = nlsr.getAdjacencyList();
551
552 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), 0);
553}
554
555BOOST_AUTO_TEST_CASE(FaceDatasetPeriodicFetch)
556{
557 int nNameMatches = 0;
558 ndn::Name datasetPrefix("/localhost/nfd/faces/list");
559 ndn::nfd::CommandOptions options;
560 ndn::time::milliseconds defaultTimeout = options.getTimeout();
561
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500562 int fetchInterval(1);
Nick Gordond5c1a372016-10-31 13:56:23 -0500563 ConfParameter& conf = nlsr.getConfParameter();
564 conf.setFaceDatasetFetchInterval(fetchInterval);
565 conf.setFaceDatasetFetchTries(0);
566
567 nlsr.initializeFaces(std::bind(&Nlsr::processFaceDataset, &nlsr, _1),
568 std::bind(&Nlsr::onFaceDatasetFetchTimeout, &nlsr, _1, _2, 0));
569
570 // Elapse the default timeout time of the interest.
571 this->advanceClocks(defaultTimeout);
572
573 // Check that we have one interest for face list in the sent interests.
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500574 for (const ndn::Interest& interest : m_face.sentInterests) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500575 if (datasetPrefix.isPrefixOf(interest.getName())) {
576 nNameMatches++;
577 }
578 }
579 BOOST_CHECK_EQUAL(nNameMatches, 1);
580
581 // Elapse the clock by the reschedule time (that we set)
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500582 this->advanceClocks(ndn::time::seconds(fetchInterval));
Nick Gordond5c1a372016-10-31 13:56:23 -0500583 // Elapse the default timeout on the interest.
584 this->advanceClocks(defaultTimeout);
585 // Plus a little more to let the events process.
586 this->advanceClocks(ndn::time::seconds(1));
587
588 // Check that we now have two interests
589 nNameMatches = 0;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500590 for (const ndn::Interest& interest : m_face.sentInterests) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500591 if (datasetPrefix.isPrefixOf(interest.getName())) {
592 nNameMatches++;
593 }
594 }
595 BOOST_CHECK_EQUAL(nNameMatches, 2);
596}
597
Vince Lehman09131122014-09-09 17:10:11 -0500598BOOST_AUTO_TEST_SUITE_END()
599
Nick Gordonfad8e252016-08-11 14:21:38 -0500600} // namespace test
601} // namespace nlsr