blob: ab5fd151882287a2f0bb1d284f65f724b5e5f127 [file] [log] [blame]
Vince Lehman09131122014-09-09 17:10:11 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi0b1b7d92019-05-22 15:37:18 +00002/*
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, 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/>.
Junxiao Shi0b1b7d92019-05-22 15:37:18 +000020 */
Vince Lehman09131122014-09-09 17:10:11 -050021
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
Ashlesh Gawande85998a12017-12-07 22:22:13 -060032using namespace ndn::time_literals;
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()
Saurab Dulal427e0122019-11-28 11:58:02 -060038 : conf(m_face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060039 , confProcessor(conf)
40 , nlsr(m_face, m_keyChain, conf)
41 , lsdb(nlsr.m_lsdb)
42 , neighbors(conf.getAdjacencyList())
Nick Gordond5c1a372016-10-31 13:56:23 -050043 , nSuccessCallbacks(0)
44 , nFailureCallbacks(0)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050045 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060046 addIdentity(conf.getRouterPrefix());
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050047 }
48
49 void
50 receiveHelloData(const ndn::Name& sender, const ndn::Name& receiver)
51 {
52 ndn::Name dataName(sender);
53 dataName.append("NLSR").append("INFO").append(receiver.wireEncode()).appendVersion();
54
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050055 ndn::Data data(dataName);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050056
57 nlsr.m_helloProtocol.onContentValidated(data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050058 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050059
60public:
Ashlesh Gawande85998a12017-12-07 22:22:13 -060061 ConfParameter conf;
62 DummyConfFileProcessor confProcessor;
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050063 Nlsr nlsr;
64 Lsdb& lsdb;
65 AdjacencyList& neighbors;
Nick Gordond5c1a372016-10-31 13:56:23 -050066 uint32_t nSuccessCallbacks;
67 uint32_t nFailureCallbacks;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050068 ndn::util::signal::ScopedConnection connection;
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
Ashlesh Gawande85998a12017-12-07 22:22:13 -060088 conf.setHyperbolicState(HYPERBOLIC_STATE_ON);
Vince Lehman09131122014-09-09 17:10:11 -050089
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
Vince Lehman7b616582014-10-17 16:25:39 -0500124 conf.setAdjLsaBuildInterval(3);
Vince Lehman7b616582014-10-17 16:25:39 -0500125 conf.setRoutingCalcInterval(9);
126
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600127 Nlsr nlsr2(m_face, m_keyChain, conf);
Vince Lehman7b616582014-10-17 16:25:39 -0500128
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600129 const Lsdb& lsdb = nlsr2.m_lsdb;
130 const RoutingTable& rt = nlsr2.m_routingTable;
Vince Lehman7b616582014-10-17 16:25:39 -0500131
Ashlesh Gawande15052402018-12-12 20:20:00 -0600132 BOOST_CHECK_EQUAL(lsdb.m_adjLsaBuildInterval, ndn::time::seconds(3));
Vince Lehman7b616582014-10-17 16:25:39 -0500133 BOOST_CHECK_EQUAL(rt.getRoutingCalcInterval(), ndn::time::seconds(9));
134}
135
Nick Gordond5c1a372016-10-31 13:56:23 -0500136BOOST_AUTO_TEST_CASE(FaceCreateEvent)
137{
138 // Setting constants for the unit test
139 const uint32_t faceId = 1;
140 const std::string faceUri = "udp4://10.0.0.1:6363";
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500141
142 Adjacent neighbor("/ndn/neighborA", ndn::FaceUri(faceUri), 10,
Nick Gordon727d4832017-10-13 18:04:25 -0500143 Adjacent::STATUS_INACTIVE, 0, 0);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500144
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800145 BOOST_REQUIRE_EQUAL(conf.getAdjacencyList().insert(neighbor), true);
Nick Gordond5c1a372016-10-31 13:56:23 -0500146
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600147 this->advanceClocks(10_ms);
Nick Gordond5c1a372016-10-31 13:56:23 -0500148
149 // Build, sign, and send the Face Event
150 ndn::nfd::FaceEventNotification event;
151 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
152 .setRemoteUri(faceUri)
153 .setFaceId(faceId);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600154 auto data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Junxiao Shi0b1b7d92019-05-22 15:37:18 +0000155 data->setFreshnessPeriod(1_s);
Nick Gordond5c1a372016-10-31 13:56:23 -0500156 data->setContent(event.wireEncode());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600157 m_keyChain.sign(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500158 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500159
160 // Move the clocks forward so that the Face processes the event.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600161 this->advanceClocks(10_ms);
Nick Gordond5c1a372016-10-31 13:56:23 -0500162
163 // Need to explicitly provide a FaceUri object, because the
164 // conversion will attempt to create Name objects.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600165 auto iterator = conf.getAdjacencyList().findAdjacent(ndn::FaceUri(faceUri));
166 BOOST_REQUIRE(iterator != conf.getAdjacencyList().end());
Nick Gordond5c1a372016-10-31 13:56:23 -0500167 BOOST_CHECK_EQUAL(iterator->getFaceId(), faceId);
168}
169
170BOOST_AUTO_TEST_CASE(FaceCreateEventNoMatch)
171{
172 // Setting constants for the unit test
173 const uint32_t faceId = 1;
174 const std::string eventUri = "udp4://10.0.0.1:6363";
175 const std::string neighborUri = "udp4://10.0.0.2:6363";
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500176
177 Adjacent neighbor("/ndn/neighborA", ndn::FaceUri(neighborUri), 10,
Nick Gordon727d4832017-10-13 18:04:25 -0500178 Adjacent::STATUS_INACTIVE, 0, 0);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500179
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600180 conf.getAdjacencyList().insert(neighbor);
Nick Gordond5c1a372016-10-31 13:56:23 -0500181
182 // Build, sign, and send the Face Event
183 ndn::nfd::FaceEventNotification event;
184 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
185 .setRemoteUri(eventUri)
186 .setFaceId(faceId);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600187 auto data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Junxiao Shi0b1b7d92019-05-22 15:37:18 +0000188 data->setFreshnessPeriod(1_s);
Nick Gordond5c1a372016-10-31 13:56:23 -0500189 data->setContent(event.wireEncode());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600190 m_keyChain.sign(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500191 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500192
193 // Move the clocks forward so that the Face processes the event.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600194 this->advanceClocks(10_ms);
Nick Gordond5c1a372016-10-31 13:56:23 -0500195
196 // The Face URIs did not match, so this neighbor should be unconfigured.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600197 auto iterator = conf.getAdjacencyList().findAdjacent(ndn::FaceUri(neighborUri));
198 BOOST_REQUIRE(iterator != conf.getAdjacencyList().end());
Nick Gordond5c1a372016-10-31 13:56:23 -0500199 BOOST_CHECK_EQUAL(iterator->getFaceId(), 0);
200}
201
202BOOST_AUTO_TEST_CASE(FaceCreateEventAlreadyConfigured)
203{
Ashlesh Gawande41878572019-09-29 00:16:02 -0500204 // So if NLSR gets the notification and registers prefixes it
205 // will change the Id to 1 and our tests will fail
206 // Need to disable registrationReply in dummy face and have own registration reply in the future
207 const uint32_t neighborFaceId = 1;
Nick Gordond5c1a372016-10-31 13:56:23 -0500208 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,
Ashlesh Gawande41878572019-09-29 00:16:02 -0500211 Adjacent::STATUS_ACTIVE, 0, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600212 conf.getAdjacencyList().insert(neighbor);
Nick Gordond5c1a372016-10-31 13:56:23 -0500213
Ashlesh Gawande41878572019-09-29 00:16:02 -0500214 // Let NLSR start the face monitor
215 this->advanceClocks(10_ms);
216
Nick Gordond5c1a372016-10-31 13:56:23 -0500217 // Build, sign, and send the Face Event
218 ndn::nfd::FaceEventNotification event;
219 event.setKind(ndn::nfd::FACE_EVENT_CREATED)
220 .setRemoteUri(faceUri)
Ashlesh Gawande41878572019-09-29 00:16:02 -0500221 .setFaceId(neighborFaceId); // Does not matter what we set here, dummy face always returns 1
Nick Gordond5c1a372016-10-31 13:56:23 -0500222 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Junxiao Shi0b1b7d92019-05-22 15:37:18 +0000223 data->setFreshnessPeriod(1_s);
Nick Gordond5c1a372016-10-31 13:56:23 -0500224 data->setContent(event.wireEncode());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600225 m_keyChain.sign(*data);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500226 m_face.receive(*data);
Nick Gordond5c1a372016-10-31 13:56:23 -0500227
228 // Move the clocks forward so that the Face processes the event.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600229 this->advanceClocks(10_ms);
Nick Gordond5c1a372016-10-31 13:56:23 -0500230
Ashlesh Gawande41878572019-09-29 00:16:02 -0500231 // Check that the neighbor is configured with the face id of 1
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600232 auto iterator = conf.getAdjacencyList().findAdjacent(ndn::FaceUri(faceUri));
233 BOOST_REQUIRE(iterator != conf.getAdjacencyList().end());
Nick Gordond5c1a372016-10-31 13:56:23 -0500234 BOOST_CHECK_EQUAL(iterator->getFaceId(), neighborFaceId);
Ashlesh Gawande41878572019-09-29 00:16:02 -0500235
236 // Resend same event notification again
237 m_face.sentInterests.clear();
238 data->setName("/localhost/nfd/faces/events/%FE%01");
239 m_keyChain.sign(*data);
240 m_face.receive(*data);
241 this->advanceClocks(10_ms);
242
243 for (const auto& interest : m_face.sentInterests) {
244 // Should not re-register prefix since this is the same event notification
245 if (ndn::Name("/localhost/nfd/rib/register").isPrefixOf(interest.getName())) {
246 BOOST_CHECK(false);
247 }
248 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500249}
250
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500251BOOST_AUTO_TEST_CASE(FaceDestroyEvent)
Vince Lehman02e32992015-03-11 12:31:20 -0500252{
Vince Lehman02e32992015-03-11 12:31:20 -0500253 // Add active neighbors
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600254 AdjacencyList& neighbors = conf.getAdjacencyList();
Vince Lehman02e32992015-03-11 12:31:20 -0500255 uint64_t destroyFaceId = 128;
256
257 // Create a neighbor whose Face will be destroyed
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500258 Adjacent failNeighbor("/ndn/neighborA", ndn::FaceUri("udp4://10.0.0.1"),
259 10, Adjacent::STATUS_ACTIVE, 0, destroyFaceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500260 neighbors.insert(failNeighbor);
261
262 // Create an additional neighbor so an adjacency LSA can be built after the face is destroyed
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500263 Adjacent otherNeighbor("/ndn/neighborB", ndn::FaceUri("udp4://10.0.0.2"),
264 10, Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman02e32992015-03-11 12:31:20 -0500265 neighbors.insert(otherNeighbor);
266
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500267 // Set HelloInterest lifetime as 10 seconds so that neighbors are not marked INACTIVE
268 // upon timeout before this test ends
269 conf.setInterestResendTime(10);
Vince Lehman02e32992015-03-11 12:31:20 -0500270 nlsr.initialize();
271
272 // Simulate successful HELLO responses
Vince Lehman41b173e2015-05-07 14:13:26 -0500273 lsdb.scheduleAdjLsaBuild();
274
275 // Set up adjacency LSAs
276 // This router
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500277 Adjacent thisRouter(conf.getRouterPrefix(), ndn::FaceUri("udp4://10.0.0.3"),
278 10, Adjacent::STATUS_ACTIVE, 0, 256);
Vince Lehman41b173e2015-05-07 14:13:26 -0500279
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500280 AdjLsa ownAdjLsa(conf.getRouterPrefix(), 10,
281 ndn::time::system_clock::now(), 1, neighbors);
Vince Lehman41b173e2015-05-07 14:13:26 -0500282 lsdb.installAdjLsa(ownAdjLsa);
283
284 // Router that will fail
285 AdjacencyList failAdjacencies;
286 failAdjacencies.insert(thisRouter);
287
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600288 AdjLsa failAdjLsa("/ndn/neighborA", 10,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500289 ndn::time::system_clock::now() + ndn::time::seconds(3600),
290 1, failAdjacencies);
Vince Lehman41b173e2015-05-07 14:13:26 -0500291
292 lsdb.installAdjLsa(failAdjLsa);
293
294 // Other router
295 AdjacencyList otherAdjacencies;
296 otherAdjacencies.insert(thisRouter);
297
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600298 AdjLsa otherAdjLsa("/ndn/neighborB", 10,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500299 ndn::time::system_clock::now() + ndn::time::seconds(3600),
300 1, otherAdjacencies);
Vince Lehman41b173e2015-05-07 14:13:26 -0500301
302 lsdb.installAdjLsa(otherAdjLsa);
Vince Lehman02e32992015-03-11 12:31:20 -0500303
304 // Run the scheduler to build an adjacency LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600305 this->advanceClocks(10_ms);
Vince Lehman02e32992015-03-11 12:31:20 -0500306
307 // Make sure an adjacency LSA was built
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600308 ndn::Name key = ndn::Name(conf.getRouterPrefix())
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800309 .append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY));
Vince Lehman41b173e2015-05-07 14:13:26 -0500310 AdjLsa* lsa = lsdb.findAdjLsa(key);
Vince Lehman02e32992015-03-11 12:31:20 -0500311 BOOST_REQUIRE(lsa != nullptr);
312
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800313 uint32_t lastAdjLsaSeqNo = lsa->getSeqNo();
Ashlesh Gawande15052402018-12-12 20:20:00 -0600314 nlsr.m_lsdb.m_sequencingManager.setAdjLsaSeq(lastAdjLsaSeqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600315
316 this->advanceClocks(1500_ms, 10);
Vince Lehman02e32992015-03-11 12:31:20 -0500317
318 // Make sure the routing table was calculated
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600319 RoutingTableEntry* rtEntry = nlsr.m_routingTable.findRoutingTableEntry(failNeighbor.getName());
Vince Lehman02e32992015-03-11 12:31:20 -0500320 BOOST_REQUIRE(rtEntry != nullptr);
Nick Gordonff9a6272017-10-12 13:38:29 -0500321 BOOST_REQUIRE_EQUAL(rtEntry->getNexthopList().size(), 1);
Vince Lehman02e32992015-03-11 12:31:20 -0500322
323 // Receive FaceEventDestroyed notification
324 ndn::nfd::FaceEventNotification event;
325 event.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
326 .setFaceId(destroyFaceId);
327
dmcoomes9f936662017-03-02 10:33:09 -0600328 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00");
Junxiao Shi0b1b7d92019-05-22 15:37:18 +0000329 data->setFreshnessPeriod(1_s);
Vince Lehman02e32992015-03-11 12:31:20 -0500330 data->setContent(event.wireEncode());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600331 m_keyChain.sign(*data);
Vince Lehman02e32992015-03-11 12:31:20 -0500332
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500333 m_face.receive(*data);
Vince Lehman02e32992015-03-11 12:31:20 -0500334
335 // Run the scheduler to build an adjacency LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600336 this->advanceClocks(10_ms);
Vince Lehman02e32992015-03-11 12:31:20 -0500337
338 Adjacent updatedNeighbor = neighbors.getAdjacent(failNeighbor.getName());
339
340 BOOST_CHECK_EQUAL(updatedNeighbor.getFaceId(), 0);
341 BOOST_CHECK_EQUAL(updatedNeighbor.getInterestTimedOutNo(),
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600342 conf.getInterestRetryNumber());
Vince Lehman02e32992015-03-11 12:31:20 -0500343 BOOST_CHECK_EQUAL(updatedNeighbor.getStatus(), Adjacent::STATUS_INACTIVE);
alvy46ccaae2015-06-25 14:13:39 -0500344
345 lsa = lsdb.findAdjLsa(key);
346 BOOST_REQUIRE(lsa != nullptr);
347
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800348 BOOST_CHECK_EQUAL(lsa->getSeqNo(), lastAdjLsaSeqNo + 1);
Vince Lehman02e32992015-03-11 12:31:20 -0500349
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600350 this->advanceClocks(15_s, 10);
351
Vince Lehman02e32992015-03-11 12:31:20 -0500352 // Make sure the routing table was recalculated
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600353 rtEntry = nlsr.m_routingTable.findRoutingTableEntry(failNeighbor.getName());
Vince Lehman02e32992015-03-11 12:31:20 -0500354 BOOST_CHECK(rtEntry == nullptr);
355}
356
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500357BOOST_AUTO_TEST_CASE(BuildAdjLsaAfterHelloResponse)
358{
359 // Configure NLSR
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500360 conf.setAdjLsaBuildInterval(1);
361
362 // Add neighbors
363 // Router A
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500364 ndn::Name neighborAName("/ndn/site/%C1.Router/routerA");
365 Adjacent neighborA(neighborAName, ndn::FaceUri("udp4://10.0.0.1"),
366 0, Adjacent::STATUS_INACTIVE, 0, 0);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500367 neighbors.insert(neighborA);
368
369 // Router B
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500370 ndn::Name neighborBName("/ndn/site/%C1.Router/routerB");
371 Adjacent neighborB(neighborBName, ndn::FaceUri("udp4://10.0.0.1"),
372 0, Adjacent::STATUS_INACTIVE, 0, 0);
373
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500374 neighbors.insert(neighborB);
375
376 nlsr.initialize();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500377
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600378 this->advanceClocks(10_ms);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500379
380 // Receive HELLO response from Router A
381 receiveHelloData(neighborAName, conf.getRouterPrefix());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600382 this->advanceClocks(1_s, 10);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500383
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800384 ndn::Name lsaKey = ndn::Name(conf.getRouterPrefix()).append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500385
386 // Adjacency LSA should be built even though other router is INACTIVE
387 AdjLsa* lsa = lsdb.findAdjLsa(lsaKey);
388 BOOST_REQUIRE(lsa != nullptr);
Nick Gordonff9a6272017-10-12 13:38:29 -0500389 BOOST_CHECK_EQUAL(lsa->getAdl().size(), 1);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500390
391 // Receive HELLO response from Router B
392 receiveHelloData(neighborBName, conf.getRouterPrefix());
393
394 // Both routers become INACTIVE and HELLO Interests have timed out
395 for (Adjacent& adjacency : neighbors.getAdjList()) {
396 adjacency.setStatus(Adjacent::STATUS_INACTIVE);
397 adjacency.setInterestTimedOutNo(HELLO_RETRIES_DEFAULT);
398 }
399
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600400 this->advanceClocks(1_s, 10);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500401
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500402 // Adjacency LSA should have been removed since this router's adjacencies are
403 // INACTIVE and have timed out
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500404 lsa = lsdb.findAdjLsa(lsaKey);
405 BOOST_CHECK(lsa == nullptr);
406
407 // Receive HELLO response from Router A and B
408 receiveHelloData(neighborAName, conf.getRouterPrefix());
409 receiveHelloData(neighborBName, conf.getRouterPrefix());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600410 this->advanceClocks(1_s, 10);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500411
412 // Adjacency LSA should be built
413 lsa = lsdb.findAdjLsa(lsaKey);
414 BOOST_REQUIRE(lsa != nullptr);
Nick Gordonff9a6272017-10-12 13:38:29 -0500415 BOOST_CHECK_EQUAL(lsa->getAdl().size(), 2);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500416}
417
Nick Gordond5c1a372016-10-31 13:56:23 -0500418BOOST_AUTO_TEST_CASE(FaceDatasetFetchSuccess)
419{
420 bool hasResult = false;
Nick Gordond5c1a372016-10-31 13:56:23 -0500421
422 nlsr.initializeFaces([&hasResult] (const std::vector<ndn::nfd::FaceStatus>& faces) {
423 hasResult = true;
424 BOOST_CHECK_EQUAL(faces.size(), 2);
425 BOOST_CHECK_EQUAL(faces.front().getFaceId(), 25401);
426 BOOST_CHECK_EQUAL(faces.back().getFaceId(), 25402);
427 },
428 [] (uint32_t code, const std::string& reason) {});
429
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600430 this->advanceClocks(100_ms, 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500431
432 ndn::nfd::FaceStatus payload1;
433 payload1.setFaceId(25401);
434 ndn::nfd::FaceStatus payload2;
435 payload2.setFaceId(25402);
436 this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
437
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600438 this->advanceClocks(100_ms, 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500439 BOOST_CHECK(hasResult);
440}
441
442BOOST_AUTO_TEST_CASE(FaceDatasetFetchFailure)
443{
Nick Gordond5c1a372016-10-31 13:56:23 -0500444 nlsr.initializeFaces([](const std::vector<ndn::nfd::FaceStatus>& faces) {},
445 [this](uint32_t code, const std::string& reason){
446 this->nFailureCallbacks++;
447 });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600448 this->advanceClocks(100_ms, 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500449
450 ndn::Name payload;
451 this->sendDataset("/localhost/nfd/faces/list", payload);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600452 this->advanceClocks(100_ms, 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500453
454 BOOST_CHECK_EQUAL(nFailureCallbacks, 1);
455 BOOST_CHECK_EQUAL(nSuccessCallbacks, 0);
456}
457
458BOOST_AUTO_TEST_CASE(FaceDatasetProcess)
459{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500460 Adjacent neighborA("/ndn/neighborA", ndn::FaceUri("udp4://192.168.0.100:6363"),
461 25, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500462 neighbors.insert(neighborA);
463
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500464 Adjacent neighborB("/ndn/neighborB", ndn::FaceUri("udp4://192.168.0.101:6363"),
465 10, Adjacent::STATUS_INACTIVE, 0, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500466 neighbors.insert(neighborB);
467
468 ndn::nfd::FaceStatus payload1;
469 payload1.setFaceId(1)
470 .setRemoteUri("udp4://192.168.0.100:6363");
471 ndn::nfd::FaceStatus payload2;
472 payload2.setFaceId(2)
473 .setRemoteUri("udp4://192.168.0.101:6363");
474 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload1, payload2};
475
476 nlsr.processFaceDataset(faceStatuses);
Nick Gordond5c1a372016-10-31 13:56:23 -0500477
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600478 AdjacencyList adjList = conf.getAdjacencyList();
Nick Gordond5c1a372016-10-31 13:56:23 -0500479
480 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), payload1.getFaceId());
481 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborB").getFaceId(), payload2.getFaceId());
482}
483
484BOOST_AUTO_TEST_CASE(UnconfiguredNeighbor)
485{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500486 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 -0500487 neighbors.insert(neighborA);
488
489 ndn::nfd::FaceStatus payload;
490 payload.setFaceId(1)
491 .setRemoteUri("udp4://192.168.0.101:6363"); // Note dissimilar Face URI.
492 std::vector<ndn::nfd::FaceStatus> faceStatuses = {payload};
493
494 nlsr.processFaceDataset(faceStatuses);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600495 this->advanceClocks(20_ms, 5);
Nick Gordond5c1a372016-10-31 13:56:23 -0500496
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600497 AdjacencyList adjList = conf.getAdjacencyList();
Nick Gordond5c1a372016-10-31 13:56:23 -0500498
499 BOOST_CHECK_EQUAL(adjList.getAdjacent("/ndn/neighborA").getFaceId(), 0);
500}
501
502BOOST_AUTO_TEST_CASE(FaceDatasetPeriodicFetch)
503{
504 int nNameMatches = 0;
505 ndn::Name datasetPrefix("/localhost/nfd/faces/list");
506 ndn::nfd::CommandOptions options;
507 ndn::time::milliseconds defaultTimeout = options.getTimeout();
508
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500509 int fetchInterval(1);
Nick Gordond5c1a372016-10-31 13:56:23 -0500510 conf.setFaceDatasetFetchInterval(fetchInterval);
511 conf.setFaceDatasetFetchTries(0);
512
Nick Gordond5c1a372016-10-31 13:56:23 -0500513 // Elapse the default timeout time of the interest.
514 this->advanceClocks(defaultTimeout);
515
516 // Check that we have one interest for face list in the sent interests.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600517 for (const auto& interest : m_face.sentInterests) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500518 if (datasetPrefix.isPrefixOf(interest.getName())) {
519 nNameMatches++;
520 }
521 }
522 BOOST_CHECK_EQUAL(nNameMatches, 1);
523
524 // Elapse the clock by the reschedule time (that we set)
Ashlesh Gawande3909aa12017-07-28 16:01:35 -0500525 this->advanceClocks(ndn::time::seconds(fetchInterval));
Nick Gordond5c1a372016-10-31 13:56:23 -0500526 // Elapse the default timeout on the interest.
527 this->advanceClocks(defaultTimeout);
Nick Gordond5c1a372016-10-31 13:56:23 -0500528
529 // Check that we now have two interests
530 nNameMatches = 0;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600531 for (const auto& interest : m_face.sentInterests) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500532 if (datasetPrefix.isPrefixOf(interest.getName())) {
533 nNameMatches++;
534 }
535 }
536 BOOST_CHECK_EQUAL(nNameMatches, 2);
537}
538
Vince Lehman09131122014-09-09 17:10:11 -0500539BOOST_AUTO_TEST_SUITE_END()
540
Nick Gordonfad8e252016-08-11 14:21:38 -0500541} // namespace test
542} // namespace nlsr