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