Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -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 | /* |
Junxiao Shi | b875293 | 2024-01-07 15:18:46 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, The University of Memphis, |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -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/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 20 | */ |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 21 | |
Junxiao Shi | aead588 | 2024-02-21 12:32:02 +0000 | [diff] [blame] | 22 | #include "route/routing-calculator.hpp" |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 23 | |
| 24 | #include "adjacency-list.hpp" |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 25 | #include "lsdb.hpp" |
| 26 | #include "nlsr.hpp" |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 27 | #include "route/name-map.hpp" |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 28 | #include "route/routing-table.hpp" |
| 29 | |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 30 | #include "tests/io-key-chain-fixture.hpp" |
| 31 | #include "tests/test-common.hpp" |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 32 | |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 33 | namespace nlsr::tests { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 34 | |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 35 | constexpr time::system_clock::time_point MAX_TIME = time::system_clock::time_point::max(); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 36 | static const ndn::Name ROUTER_A_NAME = "/ndn/router/a"; |
| 37 | static const ndn::Name ROUTER_B_NAME = "/ndn/router/b"; |
| 38 | static const ndn::Name ROUTER_C_NAME = "/ndn/router/c"; |
| 39 | static const ndn::FaceUri ROUTER_A_FACE("udp4://10.0.0.1:6363"); |
| 40 | static const ndn::FaceUri ROUTER_B_FACE("udp4://10.0.0.2:6363"); |
| 41 | static const ndn::FaceUri ROUTER_C_FACE("udp4://10.0.0.3:6363"); |
| 42 | |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 43 | class HyperbolicCalculatorFixture : public IoKeyChainFixture |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 44 | { |
| 45 | public: |
| 46 | HyperbolicCalculatorFixture() |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 47 | : face(m_io, m_keyChain) |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 48 | , conf(face, m_keyChain) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 49 | , nlsr(face, m_keyChain, conf) |
| 50 | , routingTable(nlsr.m_routingTable) |
| 51 | , adjacencies(conf.getAdjacencyList()) |
| 52 | , lsdb(nlsr.m_lsdb) |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 53 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Triangle topology with routers A, B, C connected |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 57 | void setUpTopology(std::vector<double> anglesA, std::vector<double> anglesB, |
| 58 | std::vector<double> anglesC) |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 59 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 60 | Adjacent a(ROUTER_A_NAME, ndn::FaceUri(ROUTER_A_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 61 | Adjacent b(ROUTER_B_NAME, ndn::FaceUri(ROUTER_B_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 62 | Adjacent c(ROUTER_C_NAME, ndn::FaceUri(ROUTER_C_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 63 | |
| 64 | // Router A |
| 65 | adjacencies.insert(b); |
| 66 | adjacencies.insert(c); |
| 67 | |
Junxiao Shi | b875293 | 2024-01-07 15:18:46 +0000 | [diff] [blame] | 68 | AdjLsa adjA(a.getName(), 1, MAX_TIME, adjacencies); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 69 | lsdb.installLsa(std::make_shared<AdjLsa>(adjA)); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 70 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 71 | CoordinateLsa coordA(adjA.getOriginRouter(), 1, MAX_TIME, 16.23, anglesA); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 72 | lsdb.installLsa(std::make_shared<CoordinateLsa>(coordA)); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 73 | |
| 74 | // Router B |
| 75 | a.setFaceId(1); |
| 76 | c.setFaceId(2); |
| 77 | |
| 78 | AdjacencyList adjacencyListB; |
| 79 | adjacencyListB.insert(a); |
| 80 | adjacencyListB.insert(c); |
| 81 | |
Junxiao Shi | b875293 | 2024-01-07 15:18:46 +0000 | [diff] [blame] | 82 | AdjLsa adjB(b.getName(), 1, MAX_TIME, adjacencyListB); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 83 | lsdb.installLsa(std::make_shared<AdjLsa>(adjB)); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 84 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 85 | CoordinateLsa coordB(adjB.getOriginRouter(), 1, MAX_TIME, 16.59, anglesB); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 86 | lsdb.installLsa(std::make_shared<CoordinateLsa>(coordB)); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 87 | |
| 88 | // Router C |
| 89 | a.setFaceId(1); |
| 90 | b.setFaceId(2); |
| 91 | |
| 92 | AdjacencyList adjacencyListC; |
| 93 | adjacencyListC.insert(a); |
| 94 | adjacencyListC.insert(b); |
| 95 | |
Junxiao Shi | b875293 | 2024-01-07 15:18:46 +0000 | [diff] [blame] | 96 | AdjLsa adjC(c.getName(), 1, MAX_TIME, adjacencyListC); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 97 | lsdb.installLsa(std::make_shared<AdjLsa>(adjC)); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 98 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 99 | CoordinateLsa coordC(adjC.getOriginRouter(), 1, MAX_TIME, 14.11, anglesC); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 100 | lsdb.installLsa(std::make_shared<CoordinateLsa>(coordC)); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 101 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 102 | auto lsaRange = lsdb.getLsdbIterator<CoordinateLsa>(); |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 103 | map = NameMap::createFromCoordinateLsdb(lsaRange.first, lsaRange.second); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 106 | void runTest(const double& expectedCost) |
| 107 | { |
Junxiao Shi | aead588 | 2024-02-21 12:32:02 +0000 | [diff] [blame] | 108 | calculateHyperbolicRoutingPath(map, routingTable, lsdb, adjacencies, ROUTER_A_NAME, false); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 109 | |
| 110 | RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME); |
| 111 | |
| 112 | // Router A should be able to get to B through B with cost 0 and to B through C |
| 113 | NexthopList& bHopList = entryB->getNexthopList(); |
| 114 | BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2); |
| 115 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 116 | for (auto it = bHopList.begin(); it != bHopList.end(); ++it) { |
| 117 | auto faceUri = it->getConnectingFaceUri(); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 118 | uint64_t cost = it->getRouteCostAsAdjustedInteger(); |
| 119 | |
| 120 | BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == 0) || |
| 121 | (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(expectedCost))); |
| 122 | } |
| 123 | |
| 124 | RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME); |
| 125 | |
| 126 | // Router A should be able to get to C through C with cost 0 and to C through B |
| 127 | NexthopList& cHopList = entryC->getNexthopList(); |
| 128 | BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2); |
| 129 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 130 | for (auto it = cHopList.begin(); it != cHopList.end(); ++it) { |
| 131 | auto faceUri = it->getConnectingFaceUri(); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 132 | uint64_t cost = it->getRouteCostAsAdjustedInteger(); |
| 133 | |
| 134 | BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)) || |
| 135 | (faceUri == ROUTER_C_FACE && cost == 0)); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | uint64_t |
| 140 | applyHyperbolicFactorAndRound(double d) |
| 141 | { |
| 142 | // Hyperbolic costs in the tests were calculated with 1*10^-9 precision. |
| 143 | // A factor larger than 1*10^9 will cause the tests to fail. |
| 144 | BOOST_REQUIRE(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR <= 1000000000); |
| 145 | return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d); |
| 146 | } |
| 147 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 148 | public: |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame] | 149 | ndn::DummyClientFace face; |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 150 | ConfParameter conf; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 151 | Nlsr nlsr; |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 152 | NameMap map; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 153 | |
| 154 | RoutingTable& routingTable; |
| 155 | AdjacencyList& adjacencies; |
| 156 | Lsdb& lsdb; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 157 | }; |
| 158 | |
Junxiao Shi | aead588 | 2024-02-21 12:32:02 +0000 | [diff] [blame] | 159 | BOOST_FIXTURE_TEST_SUITE(TestRoutingCalculatorHyperbolic, HyperbolicCalculatorFixture) |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 160 | |
| 161 | BOOST_AUTO_TEST_CASE(Basic) |
| 162 | { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 163 | std::vector<double> anglesA = {2.97}, |
| 164 | anglesB = {3.0}, |
| 165 | anglesC = {2.99}; |
| 166 | setUpTopology(anglesA, anglesB, anglesC); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 167 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 168 | runTest(20.103356956); |
| 169 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 170 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 171 | BOOST_AUTO_TEST_CASE(BasicMultipleAngles) |
| 172 | { |
| 173 | std::vector<double> anglesA = {2.97,1.22}, |
| 174 | anglesB = {3.0, 0.09}, |
| 175 | anglesC = {321, 2.99}; |
| 176 | setUpTopology(anglesA, anglesB, anglesC); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 177 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 178 | runTest(30.655296361); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | BOOST_AUTO_TEST_SUITE_END() |
| 182 | |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 183 | } // namespace nlsr::tests |