Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 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 | #include "test-common.hpp" |
| 23 | #include "control-commands.hpp" |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 24 | |
| 25 | #include "route/fib.hpp" |
| 26 | |
| 27 | #include "adjacency-list.hpp" |
| 28 | #include "conf-parameter.hpp" |
| 29 | |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 31 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 32 | namespace nlsr { |
| 33 | namespace test { |
| 34 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 35 | using std::shared_ptr; |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 36 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 37 | class FibFixture : public UnitTestTimeFixture |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | FibFixture() |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 41 | : face(std::make_shared<ndn::util::DummyClientFace>()) |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 42 | , interests(face->sentInterests) |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 43 | { |
| 44 | INIT_LOGGERS("/tmp", "DEBUG"); |
| 45 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame^] | 46 | Adjacent neighbor1(router1Name, ndn::util::FaceUri(router1FaceUri), 0, Adjacent::STATUS_ACTIVE, 0, router1FaceId); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 47 | adjacencies.insert(neighbor1); |
| 48 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame^] | 49 | Adjacent neighbor2(router2Name, ndn::util::FaceUri(router2FaceUri), 0, Adjacent::STATUS_ACTIVE, 0, router2FaceId); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 50 | adjacencies.insert(neighbor2); |
| 51 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame^] | 52 | Adjacent neighbor3(router3Name, ndn::util::FaceUri(router3FaceUri), 0, Adjacent::STATUS_ACTIVE, 0, router3FaceId); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 53 | adjacencies.insert(neighbor3); |
| 54 | |
| 55 | conf.setMaxFacesPerPrefix(2); |
| 56 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 57 | fib = std::make_shared<Fib>(std::ref(*face), std::ref(g_scheduler), std::ref(adjacencies), |
| 58 | std::ref(conf), keyChain); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 59 | |
| 60 | fib->m_faceMap.update(router1FaceUri, router1FaceId); |
| 61 | fib->m_faceMap.update(router2FaceUri, router2FaceId); |
| 62 | fib->m_faceMap.update(router3FaceUri, router3FaceId); |
| 63 | } |
| 64 | |
| 65 | public: |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 66 | std::shared_ptr<ndn::util::DummyClientFace> face; |
Vince Lehman | b7079a1 | 2014-11-04 12:45:50 -0600 | [diff] [blame] | 67 | ndn::KeyChain keyChain; |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 68 | std::shared_ptr<Fib> fib; |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 69 | |
| 70 | AdjacencyList adjacencies; |
| 71 | ConfParameter conf; |
| 72 | std::vector<ndn::Interest>& interests; |
| 73 | |
| 74 | static const ndn::Name router1Name; |
| 75 | static const ndn::Name router2Name; |
| 76 | static const ndn::Name router3Name; |
| 77 | |
| 78 | static const std::string router1FaceUri; |
| 79 | static const std::string router2FaceUri; |
| 80 | static const std::string router3FaceUri; |
| 81 | |
| 82 | static const uint32_t router1FaceId; |
| 83 | static const uint32_t router2FaceId; |
| 84 | static const uint32_t router3FaceId; |
| 85 | }; |
| 86 | |
| 87 | const ndn::Name FibFixture::router1Name = "/ndn/router1"; |
| 88 | const ndn::Name FibFixture::router2Name = "/ndn/router2"; |
| 89 | const ndn::Name FibFixture::router3Name = "/ndn/router3"; |
| 90 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame^] | 91 | const std::string FibFixture::router1FaceUri = "udp4://10.0.0.1"; |
| 92 | const std::string FibFixture::router2FaceUri = "udp4://10.0.0.2"; |
| 93 | const std::string FibFixture::router3FaceUri = "udp4://10.0.0.3"; |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 94 | |
| 95 | const uint32_t FibFixture::router1FaceId = 1; |
| 96 | const uint32_t FibFixture::router2FaceId = 2; |
| 97 | const uint32_t FibFixture::router3FaceId = 3; |
| 98 | |
| 99 | BOOST_FIXTURE_TEST_SUITE(TestFib, FibFixture) |
| 100 | |
| 101 | BOOST_AUTO_TEST_CASE(NextHopsAdd) |
| 102 | { |
| 103 | NextHop hop1(router1FaceUri, 10); |
| 104 | NextHop hop2(router2FaceUri, 20); |
| 105 | |
| 106 | NexthopList hops; |
| 107 | hops.addNextHop(hop1); |
| 108 | hops.addNextHop(hop2); |
| 109 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 110 | fib->processUpdate("/ndn/name", hops); |
| 111 | face->processEvents(ndn::time::milliseconds(-1)); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 112 | |
| 113 | // Should register faces 1 and 2 for /ndn/name |
| 114 | BOOST_REQUIRE_EQUAL(interests.size(), 2); |
| 115 | |
| 116 | ndn::nfd::ControlParameters extractedParameters; |
| 117 | ndn::Name::Component verb; |
| 118 | std::vector<ndn::Interest>::iterator it = interests.begin(); |
| 119 | |
| 120 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 121 | |
| 122 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 123 | extractedParameters.getFaceId() == router1FaceId && |
| 124 | verb == ndn::Name::Component("register")); |
| 125 | |
| 126 | ++it; |
| 127 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 128 | |
| 129 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 130 | extractedParameters.getFaceId() == router2FaceId && |
| 131 | verb == ndn::Name::Component("register")); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | BOOST_AUTO_TEST_CASE(NextHopsNoChange) |
| 136 | { |
| 137 | NextHop hop1(router1FaceUri, 10); |
| 138 | NextHop hop2(router2FaceUri, 20); |
| 139 | |
| 140 | NexthopList oldHops; |
| 141 | oldHops.addNextHop(hop1); |
| 142 | oldHops.addNextHop(hop2); |
| 143 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 144 | fib->processUpdate("/ndn/name", oldHops); |
| 145 | face->processEvents(ndn::time::milliseconds(-1)); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 146 | |
| 147 | BOOST_REQUIRE_EQUAL(interests.size(), 2); |
| 148 | interests.clear(); |
| 149 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 150 | fib->processUpdate("/ndn/name", oldHops); |
| 151 | face->processEvents(ndn::time::milliseconds(-1)); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 152 | |
| 153 | // Should register face 1 and 2 for /ndn/name |
| 154 | BOOST_REQUIRE_EQUAL(interests.size(), 2); |
| 155 | |
| 156 | ndn::nfd::ControlParameters extractedParameters; |
| 157 | ndn::Name::Component verb; |
| 158 | std::vector<ndn::Interest>::iterator it = interests.begin(); |
| 159 | |
| 160 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 161 | |
| 162 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 163 | extractedParameters.getFaceId() == router1FaceId && |
| 164 | verb == ndn::Name::Component("register")); |
| 165 | |
| 166 | ++it; |
| 167 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 168 | |
| 169 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 170 | extractedParameters.getFaceId() == router2FaceId && |
| 171 | verb == ndn::Name::Component("register")); |
| 172 | } |
| 173 | |
| 174 | BOOST_AUTO_TEST_CASE(NextHopsRemoveAll) |
| 175 | { |
| 176 | NextHop hop1(router1FaceUri, 10); |
| 177 | NextHop hop2(router2FaceUri, 20); |
| 178 | |
| 179 | NexthopList oldHops; |
| 180 | oldHops.addNextHop(hop1); |
| 181 | oldHops.addNextHop(hop2); |
| 182 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 183 | fib->processUpdate("/ndn/name", oldHops); |
| 184 | face->processEvents(ndn::time::milliseconds(-1)); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 185 | |
| 186 | BOOST_REQUIRE_EQUAL(interests.size(), 2); |
| 187 | interests.clear(); |
| 188 | |
| 189 | NexthopList empty; |
| 190 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 191 | fib->processUpdate("/ndn/name", empty); |
| 192 | face->processEvents(ndn::time::milliseconds(-1)); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 193 | |
| 194 | // Should unregister faces 1 and 2 for /ndn/name |
| 195 | BOOST_CHECK_EQUAL(interests.size(), 2); |
| 196 | |
| 197 | ndn::nfd::ControlParameters extractedParameters; |
| 198 | ndn::Name::Component verb; |
| 199 | std::vector<ndn::Interest>::iterator it = interests.begin(); |
| 200 | |
| 201 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 202 | |
| 203 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 204 | extractedParameters.getFaceId() == router1FaceId && |
| 205 | verb == ndn::Name::Component("unregister")); |
| 206 | |
| 207 | ++it; |
| 208 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 209 | |
| 210 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 211 | extractedParameters.getFaceId() == router2FaceId && |
| 212 | verb == ndn::Name::Component("unregister")); |
| 213 | } |
| 214 | |
| 215 | BOOST_AUTO_TEST_CASE(NextHopsMaxPrefixes) |
| 216 | { |
| 217 | NextHop hop1(router1FaceUri, 10); |
| 218 | NextHop hop2(router2FaceUri, 20); |
| 219 | NextHop hop3(router3FaceUri, 30); |
| 220 | |
| 221 | NexthopList hops; |
| 222 | hops.addNextHop(hop1); |
| 223 | hops.addNextHop(hop2); |
| 224 | hops.addNextHop(hop3); |
| 225 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 226 | fib->processUpdate("/ndn/name", hops); |
| 227 | face->processEvents(ndn::time::milliseconds(-1)); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 228 | |
| 229 | // Should only register faces 1 and 2 for /ndn/name |
| 230 | BOOST_CHECK_EQUAL(interests.size(), 2); |
| 231 | |
| 232 | ndn::nfd::ControlParameters extractedParameters; |
| 233 | ndn::Name::Component verb; |
| 234 | std::vector<ndn::Interest>::iterator it = interests.begin(); |
| 235 | |
| 236 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 237 | |
| 238 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 239 | extractedParameters.getFaceId() == router1FaceId && |
| 240 | verb == ndn::Name::Component("register")); |
| 241 | |
| 242 | ++it; |
| 243 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 244 | |
| 245 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 246 | extractedParameters.getFaceId() == router2FaceId && |
| 247 | verb == ndn::Name::Component("register")); |
| 248 | } |
| 249 | |
| 250 | BOOST_AUTO_TEST_CASE(NextHopsMaxPrefixesAfterRecalculation) |
| 251 | { |
| 252 | NextHop hop1(router1FaceUri, 10); |
| 253 | NextHop hop2(router2FaceUri, 20); |
| 254 | |
| 255 | NexthopList hops; |
| 256 | hops.addNextHop(hop1); |
| 257 | hops.addNextHop(hop2); |
| 258 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 259 | fib->processUpdate("/ndn/name", hops); |
| 260 | face->processEvents(ndn::time::milliseconds(-1)); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 261 | |
| 262 | // FIB |
| 263 | // Name NextHops |
| 264 | // /ndn/name (faceId=1, cost=10), (faceId=2, cost=20) |
| 265 | BOOST_REQUIRE_EQUAL(interests.size(), 2); |
| 266 | interests.clear(); |
| 267 | |
| 268 | // Routing table is recalculated; a new more optimal path is found |
| 269 | NextHop hop3(router3FaceUri, 5); |
| 270 | hops.addNextHop(hop3); |
| 271 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 272 | fib->processUpdate("/ndn/name", hops); |
| 273 | face->processEvents(ndn::time::milliseconds(-1)); |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 274 | |
| 275 | // To maintain a max 2 face requirement, face 3 should be registered and face 2 should be |
| 276 | // unregistered. Face 1 will also be re-registered. |
| 277 | // |
| 278 | // FIB |
| 279 | // Name NextHops |
| 280 | // /ndn/name (faceId=3, cost=5), (faceId=1, cost=10) |
| 281 | |
| 282 | BOOST_CHECK_EQUAL(interests.size(), 3); |
| 283 | |
| 284 | ndn::nfd::ControlParameters extractedParameters; |
| 285 | ndn::Name::Component verb; |
| 286 | std::vector<ndn::Interest>::iterator it = interests.begin(); |
| 287 | |
| 288 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 289 | |
| 290 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 291 | extractedParameters.getFaceId() == router3FaceId && |
| 292 | verb == ndn::Name::Component("register")); |
| 293 | |
| 294 | ++it; |
| 295 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 296 | |
| 297 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 298 | extractedParameters.getFaceId() == router1FaceId && |
| 299 | verb == ndn::Name::Component("register")); |
| 300 | |
| 301 | ++it; |
| 302 | extractRibCommandParameters(*it, verb, extractedParameters); |
| 303 | |
| 304 | BOOST_CHECK(extractedParameters.getName() == "/ndn/name" && |
| 305 | extractedParameters.getFaceId() == router2FaceId && |
| 306 | verb == ndn::Name::Component("unregister")); |
| 307 | } |
| 308 | |
Muktadir Chowdhury | 3be6466 | 2015-05-01 14:50:53 -0500 | [diff] [blame] | 309 | BOOST_FIXTURE_TEST_CASE(ScheduleFibEntryRefresh, FibFixture) |
| 310 | { |
| 311 | ndn::Name name1("/name/1"); |
| 312 | FibEntry fe(name1); |
| 313 | fib->m_table.emplace(name1, fe); |
| 314 | int origSeqNo = fe.getSeqNo(); |
| 315 | |
| 316 | fib->scheduleEntryRefresh(fe, |
| 317 | [&, this] (FibEntry& entry) { |
| 318 | BOOST_CHECK_EQUAL(origSeqNo+1, entry.getSeqNo()); |
| 319 | }); |
| 320 | this->advanceClocks(ndn::time::milliseconds(10), 1); |
| 321 | } |
| 322 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 323 | BOOST_AUTO_TEST_SUITE_END() |
| 324 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 325 | } // namespace test |
| 326 | } // namespace nlsr |