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