akmhoque | 3d06e79 | 2014-05-27 16:23:20 -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 | /* |
| 3 | * Copyright (c) 2014-2020, The University of Memphis, |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -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 | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 21 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 22 | #include "route/nexthop-list.hpp" |
| 23 | #include "route/nexthop.hpp" |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 25 | |
| 26 | namespace nlsr { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 27 | namespace test { |
| 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(TestNhl) |
| 30 | |
| 31 | BOOST_AUTO_TEST_CASE(NhlAddNextHop) |
| 32 | { |
| 33 | NextHop np1; |
| 34 | |
| 35 | NexthopList nhl1; |
| 36 | |
| 37 | nhl1.addNextHop(np1); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 38 | BOOST_CHECK_EQUAL(nhl1.size(), (uint32_t)1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 39 | |
| 40 | nhl1.removeNextHop(np1); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 41 | BOOST_CHECK_EQUAL(nhl1.size(), (uint32_t)0); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 44 | BOOST_AUTO_TEST_CASE(LinkStateRemoveNextHop) |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 45 | { |
| 46 | NextHop hop1; |
| 47 | hop1.setRouteCost(12.34); |
| 48 | |
| 49 | NexthopList hopList; |
| 50 | hopList.addNextHop(hop1); |
| 51 | |
| 52 | NextHop hop2; |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 53 | hop2.setRouteCost(13.01); |
| 54 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 55 | BOOST_REQUIRE_EQUAL(hopList.size(), 1); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 56 | |
| 57 | hopList.removeNextHop(hop2); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(hopList.size(), 1); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 59 | |
| 60 | hopList.removeNextHop(hop1); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(hopList.size(), 0); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(HyperbolicRemoveNextHop) |
| 65 | { |
| 66 | NextHop hop1; |
| 67 | hop1.setHyperbolic(true); |
| 68 | hop1.setRouteCost(12.34); |
| 69 | |
| 70 | NexthopList hopList; |
| 71 | hopList.addNextHop(hop1); |
| 72 | |
| 73 | NextHop hop2; |
| 74 | hop2.setHyperbolic(true); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 75 | hop2.setRouteCost(12.35); |
| 76 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 77 | BOOST_REQUIRE_EQUAL(hopList.size(), 1); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 78 | |
| 79 | hopList.removeNextHop(hop2); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(hopList.size(), 1); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 81 | |
| 82 | hopList.removeNextHop(hop1); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(hopList.size(), 0); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 86 | BOOST_AUTO_TEST_CASE(TieBreaker) |
| 87 | { |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 88 | // equal-cost hops are sorted lexicographically |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 89 | NextHop hopA; |
| 90 | hopA.setRouteCost(25); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 91 | hopA.setConnectingFaceUri("AAAZZ"); |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 92 | |
| 93 | NextHop hopZ; |
| 94 | hopZ.setRouteCost(25); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 95 | hopZ.setConnectingFaceUri("ZZA"); |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 96 | |
| 97 | NexthopList list; |
| 98 | list.addNextHop(hopA); |
| 99 | list.addNextHop(hopZ); |
| 100 | |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 101 | NexthopList::iterator it = list.begin(); |
| 102 | BOOST_CHECK_EQUAL(it->getConnectingFaceUri(), hopA.getConnectingFaceUri()); |
| 103 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame^] | 104 | list.clear(); |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 105 | list.addNextHop(hopZ); |
| 106 | list.addNextHop(hopA); |
| 107 | |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 108 | it = list.begin(); |
| 109 | BOOST_CHECK_EQUAL(it->getConnectingFaceUri(), hopA.getConnectingFaceUri()); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 110 | |
| 111 | |
| 112 | // equal-cost and lexicographically equal hops are sorted by the length of their face uris |
| 113 | NextHop longUriHop; |
| 114 | longUriHop.setRouteCost(25); |
| 115 | longUriHop.setConnectingFaceUri("AAAAAA"); |
| 116 | |
| 117 | NextHop shortUriHop; |
| 118 | shortUriHop.setRouteCost(25); |
| 119 | shortUriHop.setConnectingFaceUri("AAA"); |
| 120 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame^] | 121 | list.clear(); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 122 | list.addNextHop(longUriHop); |
| 123 | list.addNextHop(shortUriHop); |
| 124 | |
| 125 | it = list.begin(); |
| 126 | BOOST_CHECK_EQUAL(it->getConnectingFaceUri(), shortUriHop.getConnectingFaceUri()); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(SortOnAddAndRemove) |
| 130 | { |
| 131 | NexthopList list; |
| 132 | |
| 133 | NextHop hopA("A", 10); |
| 134 | NextHop hopB("B", 5); |
| 135 | NextHop hopC("C", 25); |
| 136 | |
| 137 | list.addNextHop(hopA); |
| 138 | list.addNextHop(hopB); |
| 139 | list.addNextHop(hopC); |
| 140 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 141 | BOOST_REQUIRE_EQUAL(list.size(), 3); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 142 | |
| 143 | double lastCost = 0; |
| 144 | for (const auto& hop : list) { |
| 145 | BOOST_CHECK(hop.getRouteCost() > lastCost); |
| 146 | lastCost = hop.getRouteCost(); |
| 147 | } |
| 148 | |
| 149 | // removing a hop keep the list sorted |
| 150 | list.removeNextHop(hopA); |
| 151 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 152 | BOOST_REQUIRE_EQUAL(list.size(), 2); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 153 | |
| 154 | lastCost = 0; |
| 155 | for (const auto& hop : list) { |
| 156 | BOOST_CHECK(hop.getRouteCost() > lastCost); |
| 157 | lastCost = hop.getRouteCost(); |
| 158 | } |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Nick Gordon | 608061b | 2017-07-06 12:45:04 -0500 | [diff] [blame] | 161 | /* If there are two NextHops going to the same neighbor, then the list |
| 162 | should always select the one with the cheaper cost. This would be |
| 163 | caused by a Name being advertised by two different routers, which |
| 164 | are reachable through the same neighbor. |
| 165 | */ |
| 166 | BOOST_AUTO_TEST_CASE(UseCheaperNextHop) |
| 167 | { |
| 168 | NexthopList list; |
| 169 | |
| 170 | NextHop hopA("udp4://10.0.0.1:6363", 10); |
| 171 | NextHop hopB("udp4://10.0.0.1:6363", 5); |
| 172 | |
| 173 | list.addNextHop(hopA); |
| 174 | list.addNextHop(hopB); |
| 175 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 176 | BOOST_REQUIRE_EQUAL(list.size(), 1); |
Nick Gordon | 608061b | 2017-07-06 12:45:04 -0500 | [diff] [blame] | 177 | |
| 178 | for (const auto& hop : list) { |
| 179 | BOOST_CHECK_EQUAL(hop, hopB); |
| 180 | } |
| 181 | } |
| 182 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 183 | BOOST_AUTO_TEST_SUITE_END() |
| 184 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 185 | } // namespace test |
| 186 | } // namespace nlsr |