blob: 73f0878af253f90e70a80fb92d13db9ccbc62ef5 [file] [log] [blame]
Vince Lehman9a709032014-09-13 16:28:07 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Junxiao Shib8752932024-01-07 15:18:46 +00003 * Copyright (c) 2014-2024, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
Vince Lehman9a709032014-09-13 16:28:07 -05006 *
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 Gawande0421bc62020-05-08 20:42:19 -070020 */
Vince Lehman9a709032014-09-13 16:28:07 -050021
Junxiao Shiaead5882024-02-21 12:32:02 +000022#include "route/routing-calculator.hpp"
Vince Lehman9a709032014-09-13 16:28:07 -050023
24#include "adjacency-list.hpp"
Vince Lehman9a709032014-09-13 16:28:07 -050025#include "lsdb.hpp"
26#include "nlsr.hpp"
Junxiao Shi4eb4eae2024-02-14 12:36:57 +000027#include "route/name-map.hpp"
Vince Lehman9a709032014-09-13 16:28:07 -050028#include "route/routing-table.hpp"
29
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040030#include "tests/io-key-chain-fixture.hpp"
31#include "tests/test-common.hpp"
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060032
Davide Pesavento288141a2024-02-13 17:30:35 -050033namespace nlsr::tests {
Vince Lehman9a709032014-09-13 16:28:07 -050034
Davide Pesavento288141a2024-02-13 17:30:35 -050035constexpr time::system_clock::time_point MAX_TIME = time::system_clock::time_point::max();
Junxiao Shi6593a432023-08-21 10:50:28 +000036static const ndn::Name ROUTER_A_NAME = "/ndn/router/a";
37static const ndn::Name ROUTER_B_NAME = "/ndn/router/b";
38static const ndn::Name ROUTER_C_NAME = "/ndn/router/c";
39static const ndn::FaceUri ROUTER_A_FACE("udp4://10.0.0.1:6363");
40static const ndn::FaceUri ROUTER_B_FACE("udp4://10.0.0.2:6363");
41static const ndn::FaceUri ROUTER_C_FACE("udp4://10.0.0.3:6363");
42
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040043class HyperbolicCalculatorFixture : public IoKeyChainFixture
Vince Lehman9a709032014-09-13 16:28:07 -050044{
45public:
46 HyperbolicCalculatorFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040047 : face(m_io, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060048 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060049 , nlsr(face, m_keyChain, conf)
50 , routingTable(nlsr.m_routingTable)
51 , adjacencies(conf.getAdjacencyList())
52 , lsdb(nlsr.m_lsdb)
Vince Lehman9a709032014-09-13 16:28:07 -050053 {
Vince Lehman9a709032014-09-13 16:28:07 -050054 }
55
56 // Triangle topology with routers A, B, C connected
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060057 void setUpTopology(std::vector<double> anglesA, std::vector<double> anglesB,
58 std::vector<double> anglesC)
Vince Lehman9a709032014-09-13 16:28:07 -050059 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050060 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 Lehman9a709032014-09-13 16:28:07 -050063
64 // Router A
65 adjacencies.insert(b);
66 adjacencies.insert(c);
67
Junxiao Shib8752932024-01-07 15:18:46 +000068 AdjLsa adjA(a.getName(), 1, MAX_TIME, adjacencies);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070069 lsdb.installLsa(std::make_shared<AdjLsa>(adjA));
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060070
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080071 CoordinateLsa coordA(adjA.getOriginRouter(), 1, MAX_TIME, 16.23, anglesA);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070072 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordA));
Vince Lehman9a709032014-09-13 16:28:07 -050073
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 Shib8752932024-01-07 15:18:46 +000082 AdjLsa adjB(b.getName(), 1, MAX_TIME, adjacencyListB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070083 lsdb.installLsa(std::make_shared<AdjLsa>(adjB));
Vince Lehman9a709032014-09-13 16:28:07 -050084
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080085 CoordinateLsa coordB(adjB.getOriginRouter(), 1, MAX_TIME, 16.59, anglesB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070086 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordB));
Vince Lehman9a709032014-09-13 16:28:07 -050087
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 Shib8752932024-01-07 15:18:46 +000096 AdjLsa adjC(c.getName(), 1, MAX_TIME, adjacencyListC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070097 lsdb.installLsa(std::make_shared<AdjLsa>(adjC));
Vince Lehman9a709032014-09-13 16:28:07 -050098
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080099 CoordinateLsa coordC(adjC.getOriginRouter(), 1, MAX_TIME, 14.11, anglesC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700100 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordC));
Vince Lehman9a709032014-09-13 16:28:07 -0500101
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700102 auto lsaRange = lsdb.getLsdbIterator<CoordinateLsa>();
Junxiao Shi4eb4eae2024-02-14 12:36:57 +0000103 map = NameMap::createFromCoordinateLsdb(lsaRange.first, lsaRange.second);
Vince Lehman9a709032014-09-13 16:28:07 -0500104 }
105
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600106 void runTest(const double& expectedCost)
107 {
Junxiao Shiaead5882024-02-21 12:32:02 +0000108 calculateHyperbolicRoutingPath(map, routingTable, lsdb, adjacencies, ROUTER_A_NAME, false);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600109
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 Shi6593a432023-08-21 10:50:28 +0000116 for (auto it = bHopList.begin(); it != bHopList.end(); ++it) {
117 auto faceUri = it->getConnectingFaceUri();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600118 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 Shi6593a432023-08-21 10:50:28 +0000130 for (auto it = cHopList.begin(); it != cHopList.end(); ++it) {
131 auto faceUri = it->getConnectingFaceUri();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600132 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 Lehman9a709032014-09-13 16:28:07 -0500148public:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000149 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600150 ConfParameter conf;
Vince Lehman9a709032014-09-13 16:28:07 -0500151 Nlsr nlsr;
Junxiao Shi4eb4eae2024-02-14 12:36:57 +0000152 NameMap map;
Vince Lehman9a709032014-09-13 16:28:07 -0500153
154 RoutingTable& routingTable;
155 AdjacencyList& adjacencies;
156 Lsdb& lsdb;
Vince Lehman9a709032014-09-13 16:28:07 -0500157};
158
Junxiao Shiaead5882024-02-21 12:32:02 +0000159BOOST_FIXTURE_TEST_SUITE(TestRoutingCalculatorHyperbolic, HyperbolicCalculatorFixture)
Vince Lehman9a709032014-09-13 16:28:07 -0500160
161BOOST_AUTO_TEST_CASE(Basic)
162{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600163 std::vector<double> anglesA = {2.97},
164 anglesB = {3.0},
165 anglesC = {2.99};
166 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500167
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600168 runTest(20.103356956);
169}
Vince Lehman9a709032014-09-13 16:28:07 -0500170
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600171BOOST_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 Lehman9a709032014-09-13 16:28:07 -0500177
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600178 runTest(30.655296361);
Vince Lehman9a709032014-09-13 16:28:07 -0500179}
180
181BOOST_AUTO_TEST_SUITE_END()
182
Davide Pesavento288141a2024-02-13 17:30:35 -0500183} // namespace nlsr::tests