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 | /* |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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" |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 24 | #include "route/fib.hpp" |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 26 | |
| 27 | namespace nlsr { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 28 | namespace test { |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestNhl) |
| 31 | |
| 32 | BOOST_AUTO_TEST_CASE(NhlAddNextHop) |
| 33 | { |
| 34 | NextHop np1; |
| 35 | |
| 36 | NexthopList nhl1; |
| 37 | |
| 38 | nhl1.addNextHop(np1); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 39 | BOOST_CHECK_EQUAL(nhl1.size(), (uint32_t)1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 40 | |
| 41 | nhl1.removeNextHop(np1); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(nhl1.size(), (uint32_t)0); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 45 | BOOST_AUTO_TEST_CASE(LinkStateRemoveNextHop) |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 46 | { |
| 47 | NextHop hop1; |
| 48 | hop1.setRouteCost(12.34); |
| 49 | |
| 50 | NexthopList hopList; |
| 51 | hopList.addNextHop(hop1); |
| 52 | |
| 53 | NextHop hop2; |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 54 | hop2.setRouteCost(13.01); |
| 55 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 56 | BOOST_REQUIRE_EQUAL(hopList.size(), 1); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 57 | |
| 58 | hopList.removeNextHop(hop2); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(hopList.size(), 1); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 60 | |
| 61 | hopList.removeNextHop(hop1); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(hopList.size(), 0); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE(HyperbolicRemoveNextHop) |
| 66 | { |
| 67 | NextHop hop1; |
| 68 | hop1.setHyperbolic(true); |
| 69 | hop1.setRouteCost(12.34); |
| 70 | |
| 71 | NexthopList hopList; |
| 72 | hopList.addNextHop(hop1); |
| 73 | |
| 74 | NextHop hop2; |
| 75 | hop2.setHyperbolic(true); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 76 | hop2.setRouteCost(12.35); |
| 77 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 78 | BOOST_REQUIRE_EQUAL(hopList.size(), 1); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 79 | |
| 80 | hopList.removeNextHop(hop2); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(hopList.size(), 1); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 82 | |
| 83 | hopList.removeNextHop(hop1); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(hopList.size(), 0); |
Vince Lehman | 145064a | 2014-08-23 11:44:16 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(TieBreaker) |
| 88 | { |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 89 | // equal-cost hops are sorted consistently |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 90 | NextHop hopA; |
| 91 | hopA.setRouteCost(25); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 92 | hopA.setConnectingFaceUri(ndn::FaceUri("udp4://192.168.3.1:6363")); |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 93 | |
| 94 | NextHop hopZ; |
| 95 | hopZ.setRouteCost(25); |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 96 | hopZ.setConnectingFaceUri(ndn::FaceUri("udp4://192.168.3.9:6363")); |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 97 | |
| 98 | NexthopList list; |
| 99 | list.addNextHop(hopA); |
| 100 | list.addNextHop(hopZ); |
| 101 | |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 102 | NexthopList::iterator it = list.begin(); |
| 103 | BOOST_CHECK_EQUAL(it->getConnectingFaceUri(), hopA.getConnectingFaceUri()); |
| 104 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 105 | list.clear(); |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 106 | list.addNextHop(hopZ); |
| 107 | list.addNextHop(hopA); |
| 108 | |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 109 | it = list.begin(); |
| 110 | BOOST_CHECK_EQUAL(it->getConnectingFaceUri(), hopA.getConnectingFaceUri()); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE(SortOnAddAndRemove) |
| 114 | { |
| 115 | NexthopList list; |
| 116 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 117 | NextHop hopA(ndn::FaceUri("udp4://192.168.3.1:6363"), 10); |
| 118 | NextHop hopB(ndn::FaceUri("udp4://192.168.3.2:6363"), 5); |
| 119 | NextHop hopC(ndn::FaceUri("udp4://192.168.3.3:6363"), 25); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 120 | |
| 121 | list.addNextHop(hopA); |
| 122 | list.addNextHop(hopB); |
| 123 | list.addNextHop(hopC); |
| 124 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 125 | BOOST_REQUIRE_EQUAL(list.size(), 3); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 126 | |
| 127 | double lastCost = 0; |
| 128 | for (const auto& hop : list) { |
| 129 | BOOST_CHECK(hop.getRouteCost() > lastCost); |
| 130 | lastCost = hop.getRouteCost(); |
| 131 | } |
| 132 | |
| 133 | // removing a hop keep the list sorted |
| 134 | list.removeNextHop(hopA); |
| 135 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 136 | BOOST_REQUIRE_EQUAL(list.size(), 2); |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 137 | |
| 138 | lastCost = 0; |
| 139 | for (const auto& hop : list) { |
| 140 | BOOST_CHECK(hop.getRouteCost() > lastCost); |
| 141 | lastCost = hop.getRouteCost(); |
| 142 | } |
Vince Lehman | cec3885 | 2015-03-31 13:21:38 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Nick Gordon | 608061b | 2017-07-06 12:45:04 -0500 | [diff] [blame] | 145 | /* If there are two NextHops going to the same neighbor, then the list |
| 146 | should always select the one with the cheaper cost. This would be |
| 147 | caused by a Name being advertised by two different routers, which |
| 148 | are reachable through the same neighbor. |
| 149 | */ |
| 150 | BOOST_AUTO_TEST_CASE(UseCheaperNextHop) |
| 151 | { |
| 152 | NexthopList list; |
| 153 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 154 | NextHop hopA(ndn::FaceUri("udp4://10.0.0.1:6363"), 10); |
| 155 | NextHop hopB(ndn::FaceUri("udp4://10.0.0.1:6363"), 5); |
Nick Gordon | 608061b | 2017-07-06 12:45:04 -0500 | [diff] [blame] | 156 | |
| 157 | list.addNextHop(hopA); |
| 158 | list.addNextHop(hopB); |
| 159 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 160 | BOOST_REQUIRE_EQUAL(list.size(), 1); |
Nick Gordon | 608061b | 2017-07-06 12:45:04 -0500 | [diff] [blame] | 161 | |
| 162 | for (const auto& hop : list) { |
| 163 | BOOST_CHECK_EQUAL(hop, hopB); |
| 164 | } |
| 165 | } |
| 166 | |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 167 | /* Fib needs a NexthopList to be sorted by FaceUri when updating |
| 168 | to avoid removing prefixes that were just installed. The above |
| 169 | test does not apply to this scenario as the NexthopList |
| 170 | sorted by cost is given to the Fib::update. |
| 171 | */ |
| 172 | BOOST_AUTO_TEST_CASE(NextHopListDiffForFibUpdate) // #5179 |
| 173 | { |
| 174 | // If default sorter is used then difference results in |
| 175 | // the same hops to remove as those that were added |
| 176 | NexthopList nhl1; |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 177 | nhl1.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.13:6363"), 28)); |
| 178 | nhl1.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.9:6363"), 38)); |
| 179 | nhl1.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.26:6363"), 44)); |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 180 | |
| 181 | NexthopList nhl2; |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 182 | nhl2.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.9:6363"), 21)); |
| 183 | nhl2.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.13:6363"), 26)); |
| 184 | nhl2.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.26:6363"), 42)); |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 185 | |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 186 | std::set<NextHop> hopsToRemove; |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 187 | std::set_difference(nhl2.begin(), nhl2.end(), |
| 188 | nhl1.begin(), nhl1.end(), |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 189 | std::inserter(hopsToRemove, hopsToRemove.begin())); |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 190 | |
| 191 | BOOST_CHECK_EQUAL(hopsToRemove.size(), 3); |
| 192 | |
| 193 | // Sorted by FaceUri |
| 194 | NextHopsUriSortedSet nhs1; |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 195 | nhs1.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.13:6363"), 28)); |
| 196 | nhs1.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.9:6363"), 38)); |
| 197 | nhs1.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.26:6363"), 44)); |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 198 | |
| 199 | NextHopsUriSortedSet nhs2; |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 200 | nhs2.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.9:6363"), 21)); |
| 201 | nhs2.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.13:6363"), 26)); |
| 202 | nhs2.addNextHop(NextHop(ndn::FaceUri("udp4://10.0.0.26:6363"), 42)); |
Ashlesh Gawande | 6f0f35d | 2021-08-21 23:52:14 -0700 | [diff] [blame] | 203 | |
| 204 | std::set<NextHop, NextHopUriSortedComparator> hopsToRemove2; |
| 205 | std::set_difference(nhs2.begin(), nhs2.end(), |
| 206 | nhs1.begin(), nhs1.end(), |
| 207 | std::inserter(hopsToRemove2, hopsToRemove2.begin()), |
| 208 | NextHopUriSortedComparator()); |
| 209 | |
| 210 | BOOST_CHECK_EQUAL(hopsToRemove2.size(), 0); |
| 211 | } |
| 212 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 213 | BOOST_AUTO_TEST_SUITE_END() |
| 214 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 215 | } // namespace test |
| 216 | } // namespace nlsr |