Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 33 | #include <boost/test/unit_test.hpp> |
| 34 | |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 35 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 36 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 37 | namespace nlsr { |
| 38 | namespace test { |
| 39 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 40 | using ndn::shared_ptr; |
| 41 | using ndn::time::system_clock; |
| 42 | static const system_clock::TimePoint MAX_TIME = system_clock::TimePoint::max(); |
| 43 | |
| 44 | class HyperbolicCalculatorFixture : public BaseFixture |
| 45 | { |
| 46 | public: |
| 47 | HyperbolicCalculatorFixture() |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 48 | : face(make_shared<ndn::util::DummyClientFace>()) |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 49 | , nlsr(g_ioService, g_scheduler, ndn::ref(*face)) |
| 50 | , routingTable(nlsr.getRoutingTable()) |
| 51 | , adjacencies(nlsr.getAdjacencyList()) |
| 52 | , lsdb(nlsr.getLsdb()) |
| 53 | { |
| 54 | setUpTopology(); |
| 55 | } |
| 56 | |
| 57 | // Triangle topology with routers A, B, C connected |
| 58 | void setUpTopology() |
| 59 | { |
| 60 | INIT_LOGGERS("/tmp", "TRACE"); |
| 61 | |
| 62 | Adjacent a(ROUTER_A_NAME, ROUTER_A_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 63 | Adjacent b(ROUTER_B_NAME, ROUTER_B_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 64 | Adjacent c(ROUTER_C_NAME, ROUTER_C_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 65 | |
| 66 | // Router A |
| 67 | adjacencies.insert(b); |
| 68 | adjacencies.insert(c); |
| 69 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 70 | AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencies); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 71 | lsdb.installAdjLsa(adjA); |
| 72 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 73 | CoordinateLsa coordA(adjA.getOrigRouter(), 1, MAX_TIME, 16.23, 2.97); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 74 | lsdb.installCoordinateLsa(coordA); |
| 75 | |
| 76 | // Router B |
| 77 | a.setFaceId(1); |
| 78 | c.setFaceId(2); |
| 79 | |
| 80 | AdjacencyList adjacencyListB; |
| 81 | adjacencyListB.insert(a); |
| 82 | adjacencyListB.insert(c); |
| 83 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 84 | AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 85 | lsdb.installAdjLsa(adjB); |
| 86 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 87 | CoordinateLsa coordB(adjB.getOrigRouter(), 1, MAX_TIME, 16.59, 3.0); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 88 | lsdb.installCoordinateLsa(coordB); |
| 89 | |
| 90 | // Router C |
| 91 | a.setFaceId(1); |
| 92 | b.setFaceId(2); |
| 93 | |
| 94 | AdjacencyList adjacencyListC; |
| 95 | adjacencyListC.insert(a); |
| 96 | adjacencyListC.insert(b); |
| 97 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 98 | AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 99 | lsdb.installAdjLsa(adjC); |
| 100 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 101 | CoordinateLsa coordC(adjC.getOrigRouter(), 1, MAX_TIME, 14.11, 2.99); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 102 | lsdb.installCoordinateLsa(coordC); |
| 103 | |
| 104 | map.createFromAdjLsdb(nlsr); |
| 105 | } |
| 106 | |
| 107 | public: |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 108 | shared_ptr<ndn::util::DummyClientFace> face; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 109 | Nlsr nlsr; |
| 110 | Map map; |
| 111 | |
| 112 | RoutingTable& routingTable; |
| 113 | AdjacencyList& adjacencies; |
| 114 | Lsdb& lsdb; |
| 115 | |
| 116 | static const ndn::Name ROUTER_A_NAME; |
| 117 | static const ndn::Name ROUTER_B_NAME; |
| 118 | static const ndn::Name ROUTER_C_NAME; |
| 119 | |
| 120 | static const std::string ROUTER_A_FACE; |
| 121 | static const std::string ROUTER_B_FACE; |
| 122 | static const std::string ROUTER_C_FACE; |
| 123 | }; |
| 124 | |
| 125 | const ndn::Name HyperbolicCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a"; |
| 126 | const ndn::Name HyperbolicCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b"; |
| 127 | const ndn::Name HyperbolicCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c"; |
| 128 | |
| 129 | const std::string HyperbolicCalculatorFixture::ROUTER_A_FACE = "face-a"; |
| 130 | const std::string HyperbolicCalculatorFixture::ROUTER_B_FACE = "face-b"; |
| 131 | const std::string HyperbolicCalculatorFixture::ROUTER_C_FACE = "face-c"; |
| 132 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 133 | uint64_t |
| 134 | applyHyperbolicFactorAndRound(double d) |
| 135 | { |
| 136 | // Hyperbolic costs in the tests were calculated with 1*10^-9 precision. |
| 137 | // A factor larger than 1*10^9 will cause the tests to fail. |
| 138 | BOOST_REQUIRE(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR <= 1000000000); |
| 139 | return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d); |
| 140 | } |
| 141 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 142 | BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture) |
| 143 | |
| 144 | BOOST_AUTO_TEST_CASE(Basic) |
| 145 | { |
| 146 | HyperbolicRoutingCalculator calculator(map.getMapSize(), false, ROUTER_A_NAME); |
| 147 | calculator.calculatePaths(map, routingTable, lsdb, adjacencies); |
| 148 | |
| 149 | RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME); |
| 150 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 151 | // Router A should be able to get to B through B with cost 0 and to B through C |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 152 | NexthopList& bHopList = entryB->getNexthopList(); |
| 153 | BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2); |
| 154 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 155 | for (std::set<NextHop, NextHopComparator>::iterator it = bHopList.begin(); it != bHopList.end(); ++it) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 156 | std::string faceUri = it->getConnectingFaceUri(); |
| 157 | uint64_t cost = it->getRouteCostAsAdjustedInteger(); |
| 158 | |
| 159 | BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == 0) || |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 160 | (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(20.103356956))); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME); |
| 164 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 165 | // Router A should be able to get to C through C with cost 0 and to C through B |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 166 | NexthopList& cHopList = entryC->getNexthopList(); |
| 167 | BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2); |
| 168 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 169 | for (std::set<NextHop, NextHopComparator>::iterator it = cHopList.begin(); it != cHopList.end(); ++it) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 170 | std::string faceUri = it->getConnectingFaceUri(); |
| 171 | uint64_t cost = it->getRouteCostAsAdjustedInteger(); |
| 172 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 173 | BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(20.103356956)) || |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 174 | (faceUri == ROUTER_C_FACE && cost == 0)); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_SUITE_END() |
| 179 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 180 | } // namespace test |
| 181 | } // namespace nlsr |