akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -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/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 19 | */ |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 20 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 21 | #include "route/routing-table.hpp" |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 22 | #include "nlsr.hpp" |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 23 | #include "route/routing-table-entry.hpp" |
| 24 | #include "route/nexthop.hpp" |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame] | 25 | |
| 26 | #include "tests/test-common.hpp" |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 27 | |
| 28 | namespace nlsr { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 29 | namespace test { |
| 30 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame^] | 31 | class RoutingTableFixture : public UnitTestTimeFixture |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 32 | { |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 33 | public: |
| 34 | RoutingTableFixture() |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame^] | 35 | : face(m_ioService, m_keyChain, {true, true}) |
| 36 | , conf(face, m_keyChain) |
| 37 | , confProcessor(conf) |
| 38 | , lsdb(face, m_keyChain, conf) |
| 39 | , rt(m_scheduler, lsdb, conf) |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
| 43 | public: |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 44 | ndn::util::DummyClientFace face; |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 45 | ConfParameter conf; |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame^] | 46 | DummyConfFileProcessor confProcessor; |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 47 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame^] | 48 | Lsdb lsdb; |
| 49 | RoutingTable rt; |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 50 | }; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 51 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 52 | BOOST_AUTO_TEST_SUITE(TestRoutingTable) |
| 53 | |
| 54 | BOOST_FIXTURE_TEST_CASE(RoutingTableAddNextHop, RoutingTableFixture) |
| 55 | { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 56 | NextHop nh1; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 57 | const std::string DEST_ROUTER = "destRouter"; |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 58 | rt.addNextHop(DEST_ROUTER, nh1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 59 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(rt.findRoutingTableEntry(DEST_ROUTER)->getDestination(), DEST_ROUTER); |
| 61 | } |
| 62 | |
| 63 | const uint8_t RoutingTableData1[] = |
| 64 | { |
| 65 | // Header |
| 66 | 0x90, 0x20, |
| 67 | // Routing table entry |
| 68 | 0x91, 0x1e, |
| 69 | // Destination |
| 70 | 0x07, 0x07, 0x08, 0x05, 0x64, 0x65, 0x73, 0x74, 0x31, 0x8f, 0x13, |
| 71 | // Nexthop |
| 72 | 0x8d, 0x07, 0x6e, 0x65, 0x78, 0x74, 0x68, 0x6f, 0x70, 0x86, 0x08, 0x3f, 0xfa, 0x66, |
| 73 | 0x66, 0x66, 0x66, 0x66, 0x66 |
| 74 | }; |
| 75 | |
| 76 | const uint8_t RoutingTableData2[] = |
| 77 | { |
| 78 | // Header |
| 79 | 0x90, 0x00 |
| 80 | }; |
| 81 | |
| 82 | BOOST_FIXTURE_TEST_CASE(RoutingTableEncode1, RoutingTableFixture) |
| 83 | { |
| 84 | NextHop nexthops; |
| 85 | nexthops.setConnectingFaceUri("nexthop"); |
| 86 | nexthops.setRouteCost(1.65); |
| 87 | rt.addNextHop("dest1", nexthops); |
| 88 | |
| 89 | auto wire = rt.wireEncode(); |
| 90 | BOOST_REQUIRE_EQUAL_COLLECTIONS(RoutingTableData1, |
| 91 | RoutingTableData1 + sizeof(RoutingTableData1), |
| 92 | wire.begin(), wire.end()); |
| 93 | } |
| 94 | |
| 95 | BOOST_FIXTURE_TEST_CASE(RoutingTableEncode2, RoutingTableFixture) |
| 96 | { |
| 97 | auto wire = rt.wireEncode(); |
| 98 | BOOST_REQUIRE_EQUAL_COLLECTIONS(RoutingTableData2, |
| 99 | RoutingTableData2 + sizeof(RoutingTableData2), |
| 100 | wire.begin(), wire.end()); |
| 101 | } |
| 102 | |
| 103 | BOOST_FIXTURE_TEST_CASE(RoutingTableDecode1, RoutingTableFixture) |
| 104 | { |
| 105 | RoutingTableStatus rtStatus(ndn::Block(RoutingTableData1, sizeof(RoutingTableData1))); |
| 106 | |
| 107 | auto it1 = rtStatus.m_rTable.begin(); |
| 108 | |
| 109 | ndn::Name des1 = it1->getDestination(); |
| 110 | BOOST_CHECK_EQUAL(des1, "dest1"); |
| 111 | |
| 112 | auto it2 = it1->getNexthopList().begin(); |
| 113 | BOOST_CHECK_EQUAL(it2->getConnectingFaceUri(), "nexthop"); |
| 114 | BOOST_CHECK_EQUAL(it2->getRouteCost(), 1.65); |
| 115 | |
| 116 | BOOST_CHECK_EQUAL(rtStatus.m_rTable.size(), 1); |
| 117 | } |
| 118 | |
| 119 | BOOST_FIXTURE_TEST_CASE(RoutingTableOutputStream, RoutingTableFixture) |
| 120 | { |
| 121 | NextHop nexthops; |
| 122 | nexthops.setConnectingFaceUri("nexthop"); |
| 123 | nexthops.setRouteCost(99); |
| 124 | rt.addNextHop("dest1", nexthops); |
| 125 | |
| 126 | std::ostringstream os; |
| 127 | os << rt; |
| 128 | |
| 129 | BOOST_CHECK_EQUAL(os.str(), |
| 130 | "Routing Table:\n" |
| 131 | " Destination: /dest1\n" |
| 132 | " NextHop(Uri: nexthop, Cost: 99)\n"); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame^] | 135 | BOOST_FIXTURE_TEST_CASE(UpdateFromLsdb, RoutingTableFixture) |
| 136 | { |
| 137 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now() + 3600_s; |
| 138 | ndn::Name router2("/router2"); |
| 139 | AdjLsa adjLsa(router2, 12, testTimePoint, 2, conf.getAdjacencyList()); |
| 140 | std::shared_ptr<Lsa> lsaPtr = std::make_shared<AdjLsa>(adjLsa); |
| 141 | BOOST_CHECK(!rt.m_isRouteCalculationScheduled); |
| 142 | lsdb.installLsa(lsaPtr); |
| 143 | BOOST_CHECK(rt.m_isRouteCalculationScheduled); |
| 144 | |
| 145 | // After 15_s (by default) routing table calculation is done |
| 146 | advanceClocks(15_s); |
| 147 | BOOST_CHECK(!rt.m_isRouteCalculationScheduled); |
| 148 | |
| 149 | // Update to installed LSA |
| 150 | std::shared_ptr<Lsa> lsaPtr2 = std::make_shared<AdjLsa>(adjLsa); |
| 151 | auto adjPtr = std::static_pointer_cast<AdjLsa>(lsaPtr2); |
| 152 | adjPtr->addAdjacent(Adjacent("router3")); |
| 153 | adjPtr->setSeqNo(13); |
| 154 | lsdb.installLsa(lsaPtr2); |
| 155 | BOOST_CHECK(rt.m_isRouteCalculationScheduled); |
| 156 | |
| 157 | // Insert a neighbor so that AdjLsa can be installed |
| 158 | AdjacencyList adjl; |
| 159 | Adjacent ownAdj(conf.getRouterPrefix()); |
| 160 | ownAdj.setStatus(Adjacent::STATUS_ACTIVE); |
| 161 | adjl.insert(ownAdj); |
| 162 | AdjLsa adjLsa4("/router4", 12, testTimePoint, 2, adjl); |
| 163 | lsaPtr = std::make_shared<AdjLsa>(adjLsa4); |
| 164 | lsdb.installLsa(lsaPtr); |
| 165 | |
| 166 | Adjacent adj("/router4"); |
| 167 | adj.setStatus(Adjacent::STATUS_ACTIVE); |
| 168 | conf.getAdjacencyList().insert(adj); |
| 169 | lsdb.scheduleAdjLsaBuild(); |
| 170 | BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0); |
| 171 | advanceClocks(15_s); |
| 172 | BOOST_CHECK_EQUAL(rt.m_rTable.size(), 1); |
| 173 | |
| 174 | rt.wireEncode(); |
| 175 | BOOST_CHECK(rt.m_wire.isValid()); |
| 176 | BOOST_CHECK_GT(rt.m_wire.size(), 0); |
| 177 | |
| 178 | // Remove own Adj Lsa - Make sure routing table is wiped out |
| 179 | conf.getAdjacencyList().setStatusOfNeighbor("/router4", Adjacent::STATUS_INACTIVE); |
| 180 | conf.getAdjacencyList().setTimedOutInterestCount("/router4", HELLO_RETRIES_MAX); |
| 181 | lsdb.scheduleAdjLsaBuild(); |
| 182 | advanceClocks(15_s); |
| 183 | BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0); |
| 184 | BOOST_CHECK(!rt.m_wire.isValid()); |
| 185 | |
| 186 | // Check that HR routing is scheduled, once Coordinate LSA is added |
| 187 | BOOST_CHECK(!rt.m_isRouteCalculationScheduled); |
| 188 | rt.m_hyperbolicState = HYPERBOLIC_STATE_ON; |
| 189 | CoordinateLsa clsa("router5", 12, testTimePoint, 2.5, {30.0}); |
| 190 | auto clsaPtr = std::make_shared<CoordinateLsa>(clsa); |
| 191 | lsdb.installLsa(clsaPtr); |
| 192 | BOOST_CHECK(rt.m_isRouteCalculationScheduled); |
| 193 | |
| 194 | Adjacent router5("/router5"); |
| 195 | router5.setStatus(Adjacent::STATUS_ACTIVE); |
| 196 | conf.getAdjacencyList().insert(router5); |
| 197 | conf.getAdjacencyList().setStatusOfNeighbor("/router5", Adjacent::STATUS_ACTIVE); |
| 198 | advanceClocks(15_s); |
| 199 | rt.wireEncode(); |
| 200 | BOOST_CHECK(rt.m_wire.isValid()); |
| 201 | BOOST_CHECK_GT(rt.m_wire.size(), 0); |
| 202 | BOOST_CHECK(!rt.m_isRouteCalculationScheduled); |
| 203 | |
| 204 | // Emulate HelloProtocol neighbor down |
| 205 | conf.getAdjacencyList().setStatusOfNeighbor("/router5", Adjacent::STATUS_INACTIVE); |
| 206 | rt.scheduleRoutingTableCalculation(); |
| 207 | advanceClocks(15_s); |
| 208 | BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0); |
| 209 | BOOST_CHECK(!rt.m_wire.isValid()); |
| 210 | } |
| 211 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 212 | BOOST_AUTO_TEST_SUITE_END() |
| 213 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 214 | } // namespace test |
| 215 | } // namespace nlsr |