Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, 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/>. |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 20 | **/ |
| 21 | |
| 22 | #include "test-common.hpp" |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 23 | |
| 24 | #include "route/routing-table-calculator.hpp" |
| 25 | |
| 26 | #include "adjacency-list.hpp" |
| 27 | #include "lsa.hpp" |
| 28 | #include "lsdb.hpp" |
| 29 | #include "nlsr.hpp" |
| 30 | #include "route/map.hpp" |
| 31 | #include "route/routing-table.hpp" |
| 32 | |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 33 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 34 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 35 | namespace nlsr { |
| 36 | namespace test { |
| 37 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 38 | using std::shared_ptr; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 39 | using ndn::time::system_clock; |
| 40 | static const system_clock::TimePoint MAX_TIME = system_clock::TimePoint::max(); |
| 41 | |
| 42 | class HyperbolicCalculatorFixture : public BaseFixture |
| 43 | { |
| 44 | public: |
| 45 | HyperbolicCalculatorFixture() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 46 | : face(m_ioService, m_keyChain) |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame^] | 47 | , conf(face, m_keyChain) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 48 | , nlsr(face, m_keyChain, conf) |
| 49 | , routingTable(nlsr.m_routingTable) |
| 50 | , adjacencies(conf.getAdjacencyList()) |
| 51 | , lsdb(nlsr.m_lsdb) |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 52 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Triangle topology with routers A, B, C connected |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 56 | void setUpTopology(std::vector<double> anglesA, std::vector<double> anglesB, |
| 57 | std::vector<double> anglesC) |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 58 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 59 | Adjacent a(ROUTER_A_NAME, ndn::FaceUri(ROUTER_A_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 60 | Adjacent b(ROUTER_B_NAME, ndn::FaceUri(ROUTER_B_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 61 | 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] | 62 | |
| 63 | // Router A |
| 64 | adjacencies.insert(b); |
| 65 | adjacencies.insert(c); |
| 66 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 67 | AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencies); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 68 | lsdb.installAdjLsa(adjA); |
| 69 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 70 | |
| 71 | CoordinateLsa coordA(adjA.getOrigRouter(), 1, MAX_TIME, 16.23, anglesA); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 72 | lsdb.installCoordinateLsa(coordA); |
| 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 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 82 | AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 83 | lsdb.installAdjLsa(adjB); |
| 84 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 85 | CoordinateLsa coordB(adjB.getOrigRouter(), 1, MAX_TIME, 16.59, anglesB); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 86 | lsdb.installCoordinateLsa(coordB); |
| 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 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 96 | AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 97 | lsdb.installAdjLsa(adjC); |
| 98 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 99 | CoordinateLsa coordC(adjC.getOrigRouter(), 1, MAX_TIME, 14.11, anglesC); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 100 | lsdb.installCoordinateLsa(coordC); |
| 101 | |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 102 | map.createFromAdjLsdb(lsdb.getAdjLsdb().begin(), lsdb.getAdjLsdb().end()); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 105 | void runTest(const double& expectedCost) |
| 106 | { |
| 107 | HyperbolicRoutingCalculator calculator(map.getMapSize(), false, ROUTER_A_NAME); |
Saurab Dulal | 72b2b25 | 2019-01-22 16:58:08 -0600 | [diff] [blame] | 108 | calculator.calculatePath(map, routingTable, lsdb, adjacencies); |
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 | |
| 116 | for (std::set<NextHop, NextHopComparator>::iterator it = bHopList.begin(); it != bHopList.end(); ++it) { |
| 117 | std::string faceUri = it->getConnectingFaceUri(); |
| 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 | |
| 130 | for (std::set<NextHop, NextHopComparator>::iterator it = cHopList.begin(); it != cHopList.end(); ++it) { |
| 131 | std::string faceUri = it->getConnectingFaceUri(); |
| 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: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 149 | ndn::util::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; |
| 152 | Map map; |
| 153 | |
| 154 | RoutingTable& routingTable; |
| 155 | AdjacencyList& adjacencies; |
| 156 | Lsdb& lsdb; |
| 157 | |
| 158 | static const ndn::Name ROUTER_A_NAME; |
| 159 | static const ndn::Name ROUTER_B_NAME; |
| 160 | static const ndn::Name ROUTER_C_NAME; |
| 161 | |
| 162 | static const std::string ROUTER_A_FACE; |
| 163 | static const std::string ROUTER_B_FACE; |
| 164 | static const std::string ROUTER_C_FACE; |
| 165 | }; |
| 166 | |
| 167 | const ndn::Name HyperbolicCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a"; |
| 168 | const ndn::Name HyperbolicCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b"; |
| 169 | const ndn::Name HyperbolicCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c"; |
| 170 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 171 | const std::string HyperbolicCalculatorFixture::ROUTER_A_FACE = "udp4://10.0.0.1"; |
| 172 | const std::string HyperbolicCalculatorFixture::ROUTER_B_FACE = "udp4://10.0.0.2"; |
| 173 | const std::string HyperbolicCalculatorFixture::ROUTER_C_FACE = "udp4://10.0.0.3"; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 174 | |
| 175 | BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture) |
| 176 | |
| 177 | BOOST_AUTO_TEST_CASE(Basic) |
| 178 | { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 179 | std::vector<double> anglesA = {2.97}, |
| 180 | anglesB = {3.0}, |
| 181 | anglesC = {2.99}; |
| 182 | setUpTopology(anglesA, anglesB, anglesC); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 183 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 184 | runTest(20.103356956); |
| 185 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 186 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 187 | BOOST_AUTO_TEST_CASE(BasicMultipleAngles) |
| 188 | { |
| 189 | std::vector<double> anglesA = {2.97,1.22}, |
| 190 | anglesB = {3.0, 0.09}, |
| 191 | anglesC = {321, 2.99}; |
| 192 | setUpTopology(anglesA, anglesB, anglesC); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 193 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 194 | runTest(30.655296361); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_SUITE_END() |
| 198 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 199 | } // namespace test |
| 200 | } // namespace nlsr |