Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 6 | * |
| 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 Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 20 | **/ |
| 21 | |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 22 | #include "test-common.hpp" |
Vince Lehman | 199e9cf | 2015-04-07 13:22:16 -0500 | [diff] [blame] | 23 | #include "control-commands.hpp" |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 24 | #include "dummy-face.hpp" |
Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 25 | |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 26 | #include "nlsr.hpp" |
| 27 | |
| 28 | #include <ndn-cxx/management/nfd-face-event-notification.hpp> |
| 29 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 30 | |
| 31 | namespace nlsr { |
| 32 | namespace test { |
| 33 | |
| 34 | using ndn::DummyFace; |
| 35 | using ndn::shared_ptr; |
| 36 | |
| 37 | BOOST_FIXTURE_TEST_SUITE(TestNlsr, BaseFixture) |
| 38 | |
| 39 | BOOST_AUTO_TEST_CASE(HyperbolicOn_ZeroCostNeighbors) |
| 40 | { |
| 41 | shared_ptr<DummyFace> face = ndn::makeDummyFace(); |
| 42 | Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face)); |
| 43 | |
| 44 | // Simulate loading configuration file |
| 45 | AdjacencyList& neighbors = nlsr.getAdjacencyList(); |
| 46 | |
| 47 | Adjacent neighborA("/ndn/neighborA", "uri://faceA", 25, Adjacent::STATUS_INACTIVE, 0, 0); |
| 48 | neighbors.insert(neighborA); |
| 49 | |
| 50 | Adjacent neighborB("/ndn/neighborB", "uri://faceB", 10, Adjacent::STATUS_INACTIVE, 0, 0); |
| 51 | neighbors.insert(neighborB); |
| 52 | |
| 53 | Adjacent neighborC("/ndn/neighborC", "uri://faceC", 17, Adjacent::STATUS_INACTIVE, 0, 0); |
| 54 | neighbors.insert(neighborC); |
| 55 | |
| 56 | nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON); |
| 57 | |
| 58 | nlsr.initialize(); |
| 59 | |
| 60 | std::list<Adjacent> neighborList = neighbors.getAdjList(); |
| 61 | for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) { |
| 62 | BOOST_CHECK_EQUAL(it->getLinkCost(), 0); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(HyperbolicOff_LinkStateCost) |
| 67 | { |
| 68 | shared_ptr<DummyFace> face = ndn::makeDummyFace(); |
| 69 | Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face)); |
| 70 | |
| 71 | // Simulate loading configuration file |
| 72 | AdjacencyList& neighbors = nlsr.getAdjacencyList(); |
| 73 | |
| 74 | Adjacent neighborA("/ndn/neighborA", "uri://faceA", 25, Adjacent::STATUS_INACTIVE, 0, 0); |
| 75 | neighbors.insert(neighborA); |
| 76 | |
| 77 | Adjacent neighborB("/ndn/neighborB", "uri://faceB", 10, Adjacent::STATUS_INACTIVE, 0, 0); |
| 78 | neighbors.insert(neighborB); |
| 79 | |
| 80 | Adjacent neighborC("/ndn/neighborC", "uri://faceC", 17, Adjacent::STATUS_INACTIVE, 0, 0); |
| 81 | neighbors.insert(neighborC); |
| 82 | |
| 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(it->getLinkCost() != 0); |
| 88 | } |
| 89 | } |
| 90 | |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 91 | BOOST_AUTO_TEST_CASE(SetEventIntervals) |
| 92 | { |
| 93 | shared_ptr<DummyFace> face = ndn::makeDummyFace(); |
| 94 | Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face)); |
| 95 | |
| 96 | // Simulate loading configuration file |
| 97 | ConfParameter& conf = nlsr.getConfParameter(); |
| 98 | conf.setAdjLsaBuildInterval(3); |
| 99 | conf.setFirstHelloInterval(6); |
| 100 | conf.setRoutingCalcInterval(9); |
| 101 | |
| 102 | nlsr.initialize(); |
| 103 | |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 104 | const Lsdb& lsdb = nlsr.getLsdb(); |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 105 | const RoutingTable& rt = nlsr.getRoutingTable(); |
| 106 | |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(lsdb.getAdjLsaBuildInterval(), ndn::time::seconds(3)); |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(nlsr.getFirstHelloInterval(), 6); |
| 109 | BOOST_CHECK_EQUAL(rt.getRoutingCalcInterval(), ndn::time::seconds(9)); |
| 110 | } |
| 111 | |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 112 | BOOST_FIXTURE_TEST_CASE(FaceDestroyEvent, UnitTestTimeFixture) |
| 113 | { |
| 114 | shared_ptr<ndn::util::DummyClientFace> face = ndn::util::makeDummyClientFace(g_ioService); |
| 115 | Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face)); |
| 116 | |
| 117 | // Simulate loading configuration file |
| 118 | ConfParameter& conf = nlsr.getConfParameter(); |
| 119 | conf.setNetwork("/ndn"); |
| 120 | conf.setSiteName("/site"); |
| 121 | conf.setRouterName("/%C1.router/this-router"); |
| 122 | conf.setAdjLsaBuildInterval(0); |
| 123 | conf.setRoutingCalcInterval(0); |
| 124 | |
| 125 | // Add active neighbors |
| 126 | AdjacencyList& neighbors = nlsr.getAdjacencyList(); |
| 127 | |
| 128 | uint64_t destroyFaceId = 128; |
| 129 | |
| 130 | // Create a neighbor whose Face will be destroyed |
| 131 | Adjacent failNeighbor("/ndn/neighborA", "uri://faceA", 10, Adjacent::STATUS_ACTIVE, 0, |
| 132 | destroyFaceId); |
| 133 | neighbors.insert(failNeighbor); |
| 134 | |
| 135 | // Create an additional neighbor so an adjacency LSA can be built after the face is destroyed |
| 136 | Adjacent otherNeighbor("/ndn/neighborB", "uri://faceB", 25, Adjacent::STATUS_ACTIVE, 0, 256); |
| 137 | neighbors.insert(otherNeighbor); |
| 138 | |
| 139 | nlsr.initialize(); |
| 140 | |
| 141 | // Simulate successful HELLO responses |
| 142 | nlsr.getLsdb().scheduleAdjLsaBuild(); |
| 143 | |
| 144 | // Run the scheduler to build an adjacency LSA |
| 145 | this->advanceClocks(ndn::time::milliseconds(1)); |
| 146 | |
| 147 | // Make sure an adjacency LSA was built |
| 148 | ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING); |
| 149 | AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key); |
| 150 | BOOST_REQUIRE(lsa != nullptr); |
| 151 | |
| 152 | uint32_t lastAdjLsaSeqNo = nlsr.getSequencingManager().getAdjLsaSeq(); |
| 153 | |
| 154 | // Make sure the routing table was calculated |
| 155 | RoutingTableEntry* rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName()); |
| 156 | BOOST_REQUIRE(rtEntry != nullptr); |
| 157 | BOOST_REQUIRE_EQUAL(rtEntry->getNexthopList().getSize(), 1); |
| 158 | |
| 159 | // Receive FaceEventDestroyed notification |
| 160 | ndn::nfd::FaceEventNotification event; |
| 161 | event.setKind(ndn::nfd::FACE_EVENT_DESTROYED) |
| 162 | .setFaceId(destroyFaceId); |
| 163 | |
| 164 | shared_ptr<ndn::Data> data = make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00"); |
| 165 | data->setContent(event.wireEncode()); |
| 166 | nlsr.getKeyChain().sign(*data); |
| 167 | |
| 168 | face->receive(*data); |
| 169 | |
| 170 | // Run the scheduler to build an adjacency LSA |
| 171 | this->advanceClocks(ndn::time::milliseconds(1)); |
| 172 | |
| 173 | Adjacent updatedNeighbor = neighbors.getAdjacent(failNeighbor.getName()); |
| 174 | |
| 175 | BOOST_CHECK_EQUAL(updatedNeighbor.getFaceId(), 0); |
| 176 | BOOST_CHECK_EQUAL(updatedNeighbor.getInterestTimedOutNo(), |
| 177 | nlsr.getConfParameter().getInterestRetryNumber()); |
| 178 | BOOST_CHECK_EQUAL(updatedNeighbor.getStatus(), Adjacent::STATUS_INACTIVE); |
| 179 | BOOST_CHECK_EQUAL(nlsr.getSequencingManager().getAdjLsaSeq(), lastAdjLsaSeqNo + 1); |
| 180 | |
| 181 | // Make sure the routing table was recalculated |
| 182 | rtEntry = nlsr.getRoutingTable().findRoutingTableEntry(failNeighbor.getName()); |
| 183 | BOOST_CHECK(rtEntry == nullptr); |
| 184 | } |
| 185 | |
Vince Lehman | 199e9cf | 2015-04-07 13:22:16 -0500 | [diff] [blame] | 186 | // Bug #2733 |
| 187 | // This test checks that when a face for an inactive node is destroyed, an |
| 188 | // Adjacency LSA build does not postpone the LSA refresh and cause RIB |
| 189 | // entries for other nodes' name prefixes to not be refreshed. |
| 190 | // |
| 191 | // This test is invalid when Issue #2732 is implemented since an Adjacency LSA |
| 192 | // refresh will not cause RIB entries for other nodes' name prefixes to be refreshed. |
| 193 | BOOST_FIXTURE_TEST_CASE(FaceDestroyEventInactive, UnitTestTimeFixture) |
| 194 | { |
| 195 | shared_ptr<ndn::util::DummyClientFace> face = ndn::util::makeDummyClientFace(g_ioService); |
| 196 | Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face)); |
| 197 | Lsdb& lsdb = nlsr.getLsdb(); |
| 198 | |
| 199 | // Simulate loading configuration file |
| 200 | ConfParameter& conf = nlsr.getConfParameter(); |
| 201 | conf.setNetwork("/ndn"); |
| 202 | conf.setSiteName("/site"); |
| 203 | conf.setRouterName("/%C1.router/this-router"); |
| 204 | conf.setFirstHelloInterval(0); |
| 205 | conf.setAdjLsaBuildInterval(0); |
| 206 | conf.setRoutingCalcInterval(0); |
| 207 | |
| 208 | // Add neighbors |
| 209 | AdjacencyList& neighbors = nlsr.getAdjacencyList(); |
| 210 | |
| 211 | uint64_t destroyFaceId = 128; |
| 212 | |
| 213 | // Create an inactive neighbor whose Face will be destroyed |
| 214 | Adjacent failNeighbor("/ndn/neighborA", "uri://faceA", 10, Adjacent::STATUS_INACTIVE, 3, |
| 215 | destroyFaceId); |
| 216 | neighbors.insert(failNeighbor); |
| 217 | |
| 218 | // Create an additional active neighbor so an adjacency LSA can be built |
| 219 | Adjacent otherNeighbor("/ndn/neighborB", "uri://faceB", 25, Adjacent::STATUS_ACTIVE, 0, 256); |
| 220 | neighbors.insert(otherNeighbor); |
| 221 | |
| 222 | // Add a name for the neighbor to advertise |
| 223 | NamePrefixList nameList; |
| 224 | ndn::Name nameToAdvertise("/ndn/neighborB/name"); |
| 225 | nameList.insert(nameToAdvertise); |
| 226 | |
| 227 | NameLsa nameLsa("/ndn/neighborB", "name", 25, ndn::time::system_clock::now(), nameList); |
| 228 | lsdb.installNameLsa(nameLsa); |
| 229 | |
| 230 | nlsr.initialize(); |
| 231 | |
| 232 | // Simulate successful HELLO responses from neighbor B |
| 233 | lsdb.scheduleAdjLsaBuild(); |
| 234 | |
| 235 | // Run the scheduler to build an adjacency LSA |
| 236 | this->advanceClocks(ndn::time::milliseconds(1)); |
| 237 | |
| 238 | ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING); |
| 239 | AdjLsa* lsa = lsdb.findAdjLsa(key); |
| 240 | BOOST_REQUIRE(lsa != nullptr); |
| 241 | |
| 242 | // Cancel previous LSA expiration event |
| 243 | g_scheduler.cancelEvent(lsa->getExpiringEventId()); |
| 244 | |
| 245 | // Set expiration time for own Adjacency LSA to earlier value for unit-test |
| 246 | // |
| 247 | // Expiration time is negative to offset the GRACE_PERIOD (10 seconds) automatically applied |
| 248 | // to expiration times |
| 249 | ndn::EventId id = lsdb.scheduleAdjLsaExpiration(key, lsa->getLsSeqNo(), -ndn::time::seconds(9)); |
| 250 | lsa->setExpiringEventId(id); |
| 251 | |
| 252 | // Generate a FaceEventDestroyed notification |
| 253 | ndn::nfd::FaceEventNotification event; |
| 254 | event.setKind(ndn::nfd::FACE_EVENT_DESTROYED) |
| 255 | .setFaceId(destroyFaceId); |
| 256 | |
| 257 | shared_ptr<ndn::Data> data = make_shared<ndn::Data>("/localhost/nfd/faces/events/%FE%00"); |
| 258 | data->setContent(event.wireEncode()); |
| 259 | nlsr.getKeyChain().sign(*data); |
| 260 | |
| 261 | // Receive the FaceEventDestroyed notification |
| 262 | face->receive(*data); |
| 263 | |
| 264 | // Run the scheduler to expire the Adjacency LSA. The expiration should refresh the RIB |
| 265 | // entries associated with Neighbor B's advertised prefix. |
| 266 | face->sentInterests.clear(); |
| 267 | this->advanceClocks(ndn::time::seconds(1)); |
| 268 | |
| 269 | // The Face should have two sent Interests: the face event and a RIB registration |
| 270 | BOOST_REQUIRE(face->sentInterests.size() > 0); |
| 271 | const ndn::Interest& interest = face->sentInterests.back(); |
| 272 | |
| 273 | ndn::nfd::ControlParameters parameters; |
| 274 | ndn::Name::Component verb; |
| 275 | BOOST_REQUIRE_NO_THROW(extractRibCommandParameters(interest, verb, parameters)); |
| 276 | |
| 277 | BOOST_CHECK_EQUAL(verb, ndn::Name::Component("register")); |
| 278 | BOOST_CHECK_EQUAL(parameters.getName(), nameToAdvertise); |
| 279 | } |
| 280 | |
Vince Lehman | c2acdcb | 2015-04-29 11:14:35 -0500 | [diff] [blame^] | 281 | BOOST_FIXTURE_TEST_CASE(GetCertificate, UnitTestTimeFixture) |
| 282 | { |
| 283 | shared_ptr<ndn::util::DummyClientFace> face = ndn::util::makeDummyClientFace(g_ioService); |
| 284 | Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face)); |
| 285 | |
| 286 | // Create certificate |
| 287 | ndn::Name identity("/TestNLSR/identity"); |
| 288 | identity.appendVersion(); |
| 289 | |
| 290 | ndn::KeyChain keyChain; |
| 291 | keyChain.createIdentity(identity); |
| 292 | ndn::Name certName = keyChain.getDefaultCertificateNameForIdentity(identity); |
| 293 | shared_ptr<ndn::IdentityCertificate> certificate = keyChain.getCertificate(certName); |
| 294 | |
| 295 | const ndn::Name certKey = certificate->getName().getPrefix(-1); |
| 296 | |
| 297 | BOOST_CHECK(nlsr.getCertificate(certKey) == nullptr); |
| 298 | |
| 299 | // Certificate should be retrievable from the CertificateStore |
| 300 | nlsr.loadCertToPublish(certificate); |
| 301 | |
| 302 | BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr); |
| 303 | |
| 304 | nlsr.getCertificateStore().clear(); |
| 305 | |
| 306 | // Certificate should be retrievable from the cache |
| 307 | nlsr.addCertificateToCache(certificate); |
| 308 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 309 | |
| 310 | BOOST_CHECK(nlsr.getCertificate(certKey) != nullptr); |
| 311 | } |
| 312 | |
Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 313 | BOOST_AUTO_TEST_SUITE_END() |
| 314 | |
| 315 | } //namespace test |
| 316 | } //namespace nlsr |