blob: 5f38703119384a7469a0262b7c12bded246a8cd8 [file] [log] [blame]
Vince Lehman41b173e2015-05-07 14:13:26 -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 Lehman41b173e2015-05-07 14:13:26 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
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 Gawande0421bc62020-05-08 20:42:19 -070020 */
Vince Lehman41b173e2015-05-07 14:13:26 -050021
Junxiao Shiaead5882024-02-21 12:32:02 +000022#include "route/routing-calculator.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050023
24#include "adjacency-list.hpp"
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040025#include "adjacent.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050026#include "lsdb.hpp"
27#include "nlsr.hpp"
Junxiao Shi4eb4eae2024-02-14 12:36:57 +000028#include "route/name-map.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050029#include "route/routing-table.hpp"
30
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040031#include "tests/io-key-chain-fixture.hpp"
32#include "tests/test-common.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050033
Davide Pesavento288141a2024-02-13 17:30:35 -050034namespace nlsr::tests {
Vince Lehman41b173e2015-05-07 14:13:26 -050035
Davide Pesavento288141a2024-02-13 17:30:35 -050036constexpr time::system_clock::time_point MAX_TIME = time::system_clock::time_point::max();
Junxiao Shi6593a432023-08-21 10:50:28 +000037static const ndn::Name ROUTER_A_NAME = "/ndn/site/%C1.Router/this-router";
38static const ndn::Name ROUTER_B_NAME = "/ndn/site/%C1.Router/b";
39static const ndn::Name ROUTER_C_NAME = "/ndn/site/%C1.Router/c";
Junxiao Shi0fef0572024-02-22 04:15:49 +000040static const ndn::FaceUri ROUTER_A_FACE("udp4://10.0.0.1:6363");
41static const ndn::FaceUri ROUTER_B_FACE("udp4://10.0.0.2:6363");
42static const ndn::FaceUri ROUTER_C_FACE("udp4://10.0.0.3:6363");
43constexpr double LINK_AB_COST = 5.0;
44constexpr double LINK_AC_COST = 10.0;
45constexpr double LINK_BC_COST = 17.0;
Vince Lehman41b173e2015-05-07 14:13:26 -050046
Junxiao Shi0fef0572024-02-22 04:15:49 +000047/**
48 * @brief Provide a topology for link-state routing calculator testing.
49 *
50 * The topology consists of three routers: A, B, C.
51 * After calling all three setupRouter functions, they will form a triangle topology:
52 *
53 * A-----B
54 * \ /
55 * \ /
56 * C
57 *
58 * The local router, as reported by `conf.getRouterPrefix()`, is router A.
59 */
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040060class LinkStateCalculatorFixture : public IoKeyChainFixture
Vince Lehman41b173e2015-05-07 14:13:26 -050061{
62public:
63 LinkStateCalculatorFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040064 : face(m_io, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060065 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060066 , confProcessor(conf)
67 , nlsr(face, m_keyChain, conf)
68 , routingTable(nlsr.m_routingTable)
69 , lsdb(nlsr.m_lsdb)
Vince Lehman41b173e2015-05-07 14:13:26 -050070 {
Vince Lehman41b173e2015-05-07 14:13:26 -050071 }
72
Junxiao Shi0fef0572024-02-22 04:15:49 +000073 /**
74 * @brief Insert Adjacency LSA of router A into LSDB.
75 */
76 void
77 setupRouterA(double costAB = LINK_AB_COST, double costAC = LINK_AC_COST)
Vince Lehman41b173e2015-05-07 14:13:26 -050078 {
Junxiao Shi0fef0572024-02-22 04:15:49 +000079 AdjacencyList& adjList = conf.getAdjacencyList();
80 if (!std::isnan(costAB)) {
81 adjList.insert(Adjacent(ROUTER_B_NAME, ROUTER_B_FACE, costAB, Adjacent::STATUS_ACTIVE, 0, 0));
82 }
83 if (!std::isnan(costAC)) {
84 adjList.insert(Adjacent(ROUTER_C_NAME, ROUTER_C_FACE, costAC, Adjacent::STATUS_ACTIVE, 0, 0));
85 }
Vince Lehman41b173e2015-05-07 14:13:26 -050086
Junxiao Shi0fef0572024-02-22 04:15:49 +000087 lsdb.installLsa(std::make_shared<AdjLsa>(ROUTER_A_NAME, 1, MAX_TIME, adjList));
88 }
Vince Lehman41b173e2015-05-07 14:13:26 -050089
Junxiao Shi0fef0572024-02-22 04:15:49 +000090 /**
91 * @brief Insert Adjacency LSA of router B into LSDB.
92 */
93 void
94 setupRouterB(double costBC = LINK_BC_COST, double costBA = LINK_AB_COST)
95 {
96 AdjacencyList adjList;
97 if (!std::isnan(costBC)) {
98 adjList.insert(Adjacent(ROUTER_C_NAME, ROUTER_C_FACE, costBC, Adjacent::STATUS_ACTIVE, 0, 0));
99 }
100 if (!std::isnan(costBA)) {
101 adjList.insert(Adjacent(ROUTER_A_NAME, ROUTER_A_FACE, costBA, Adjacent::STATUS_ACTIVE, 0, 0));
102 }
Vince Lehman41b173e2015-05-07 14:13:26 -0500103
Junxiao Shi0fef0572024-02-22 04:15:49 +0000104 lsdb.installLsa(std::make_shared<AdjLsa>(ROUTER_B_NAME, 1, MAX_TIME, adjList));
105 }
Vince Lehman41b173e2015-05-07 14:13:26 -0500106
Junxiao Shi0fef0572024-02-22 04:15:49 +0000107 /**
108 * @brief Insert Adjacency LSA of router C into LSDB.
109 */
110 void
111 setupRouterC(double costCA = LINK_AC_COST, double costCB = LINK_BC_COST)
112 {
113 AdjacencyList adjList;
114 if (!std::isnan(costCA)) {
115 adjList.insert(Adjacent(ROUTER_A_NAME, ROUTER_A_FACE, costCA, Adjacent::STATUS_ACTIVE, 0, 0));
116 }
117 if (!std::isnan(costCB)) {
118 adjList.insert(Adjacent(ROUTER_B_NAME, ROUTER_B_FACE, costCB, Adjacent::STATUS_ACTIVE, 0, 0));
119 }
Vince Lehman41b173e2015-05-07 14:13:26 -0500120
Junxiao Shi0fef0572024-02-22 04:15:49 +0000121 lsdb.installLsa(std::make_shared<AdjLsa>(ROUTER_C_NAME, 1, MAX_TIME, adjList));
122 }
Vince Lehman41b173e2015-05-07 14:13:26 -0500123
Junxiao Shi0fef0572024-02-22 04:15:49 +0000124 /**
125 * @brief Run link-state routing calculator.
126 */
127 void
128 calculatePath()
129 {
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700130 auto lsaRange = lsdb.getLsdbIterator<AdjLsa>();
Junxiao Shi0fef0572024-02-22 04:15:49 +0000131 NameMap map = NameMap::createFromAdjLsdb(lsaRange.first, lsaRange.second);
132 calculateLinkStateRoutingPath(map, routingTable, conf, lsdb);
133 }
134
135 /**
136 * @brief Verify that the routing table contains an entry with specific next hops.
137 * @param destination Destination router.
138 * @param expectedNextHops Expected next hops; order does not matter.
139 */
140 void
141 checkRoutingTableEntry(const ndn::Name& destination,
142 std::initializer_list<NextHop> expectedNextHops) const
143 {
144 BOOST_TEST_CONTEXT("Checking routing table entry " << destination)
145 {
146 using NextHopSet = std::set<NextHop, NextHopUriSortedComparator>;
147
148 NextHopSet expectedNextHopSet(expectedNextHops);
149
150 const RoutingTableEntry* entry = routingTable.findRoutingTableEntry(destination);
151 BOOST_REQUIRE(entry != nullptr);
152
153 const NexthopList& actualNextHopList = entry->getNexthopList();
154 NextHopSet actualNextHopSet(actualNextHopList.begin(), actualNextHopList.end());
155
156 BOOST_TEST(expectedNextHopSet == actualNextHopSet, boost::test_tools::per_element());
157 }
Vince Lehman41b173e2015-05-07 14:13:26 -0500158 }
159
160public:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000161 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600162 ConfParameter conf;
163 DummyConfFileProcessor confProcessor;
Vince Lehman41b173e2015-05-07 14:13:26 -0500164 Nlsr nlsr;
Vince Lehman41b173e2015-05-07 14:13:26 -0500165
166 RoutingTable& routingTable;
167 Lsdb& lsdb;
Vince Lehman41b173e2015-05-07 14:13:26 -0500168};
169
Junxiao Shiaead5882024-02-21 12:32:02 +0000170BOOST_FIXTURE_TEST_SUITE(TestRoutingCalculatorLinkState, LinkStateCalculatorFixture)
Vince Lehman41b173e2015-05-07 14:13:26 -0500171
172BOOST_AUTO_TEST_CASE(Basic)
173{
Junxiao Shi0fef0572024-02-22 04:15:49 +0000174 setupRouterA();
175 setupRouterB();
176 setupRouterC();
177 calculatePath();
Vince Lehman41b173e2015-05-07 14:13:26 -0500178
179 // Router A should be able to get to B through B and to B through C
Junxiao Shi0fef0572024-02-22 04:15:49 +0000180 checkRoutingTableEntry(ROUTER_B_NAME, {
181 {ROUTER_B_FACE, LINK_AB_COST},
182 {ROUTER_C_FACE, LINK_AC_COST + LINK_BC_COST},
183 });
Vince Lehman41b173e2015-05-07 14:13:26 -0500184
185 // Router A should be able to get to C through C and to C through B
Junxiao Shi0fef0572024-02-22 04:15:49 +0000186 checkRoutingTableEntry(ROUTER_C_NAME, {
187 {ROUTER_C_FACE, LINK_AC_COST},
188 {ROUTER_B_FACE, LINK_AB_COST + LINK_BC_COST},
189 });
Vince Lehman41b173e2015-05-07 14:13:26 -0500190}
191
192BOOST_AUTO_TEST_CASE(Asymmetric)
193{
194 // Asymmetric link cost between B and C
Junxiao Shi0fef0572024-02-22 04:15:49 +0000195 double higherCostBC = LINK_BC_COST + 1;
196 setupRouterA();
197 setupRouterB(higherCostBC);
198 setupRouterC();
Vince Lehman41b173e2015-05-07 14:13:26 -0500199
Junxiao Shi0fef0572024-02-22 04:15:49 +0000200 // Calculation should consider the link between B and C as having cost = higherCostBC
201 calculatePath();
Vince Lehman41b173e2015-05-07 14:13:26 -0500202
203 // Router A should be able to get to B through B and to B through C
Junxiao Shi0fef0572024-02-22 04:15:49 +0000204 checkRoutingTableEntry(ROUTER_B_NAME, {
205 {ROUTER_B_FACE, LINK_AB_COST},
206 {ROUTER_C_FACE, LINK_AC_COST + higherCostBC},
207 });
Vince Lehman41b173e2015-05-07 14:13:26 -0500208
209 // Router A should be able to get to C through C and to C through B
Junxiao Shi0fef0572024-02-22 04:15:49 +0000210 checkRoutingTableEntry(ROUTER_C_NAME, {
211 {ROUTER_C_FACE, LINK_AC_COST},
212 {ROUTER_B_FACE, LINK_AB_COST + higherCostBC},
213 });
Vince Lehman41b173e2015-05-07 14:13:26 -0500214}
215
dulalsaurabd0816a32019-07-26 13:11:24 +0000216BOOST_AUTO_TEST_CASE(NonAdjacentCost)
Vince Lehman41b173e2015-05-07 14:13:26 -0500217{
Junxiao Shi0fef0572024-02-22 04:15:49 +0000218 // Break the link between B - C by setting it to NON_ADJACENT_COST.
219 setupRouterA();
220 setupRouterB(
221 Adjacent::NON_ADJACENT_COST // B to C
222 );
223 setupRouterC();
Vince Lehman41b173e2015-05-07 14:13:26 -0500224
225 // Calculation should consider the link between B and C as down
Junxiao Shi0fef0572024-02-22 04:15:49 +0000226 calculatePath();
Vince Lehman41b173e2015-05-07 14:13:26 -0500227
228 // Router A should be able to get to B through B but not through C
Junxiao Shi0fef0572024-02-22 04:15:49 +0000229 checkRoutingTableEntry(ROUTER_B_NAME, {
230 {ROUTER_B_FACE, LINK_AB_COST},
231 });
Vince Lehman41b173e2015-05-07 14:13:26 -0500232
233 // Router A should be able to get to C through C but not through B
Junxiao Shi0fef0572024-02-22 04:15:49 +0000234 checkRoutingTableEntry(ROUTER_C_NAME, {
235 {ROUTER_C_FACE, LINK_AC_COST},
236 });
dulalsaurabd0816a32019-07-26 13:11:24 +0000237}
238
239BOOST_AUTO_TEST_CASE(AsymmetricZeroCostLink)
240{
241 // Asymmetric and zero link cost between B - C, and B - A.
Junxiao Shi0fef0572024-02-22 04:15:49 +0000242 setupRouterA(
243 0 // A to B
244 );
245 setupRouterB(
246 0, // B to C: effective cost is still LINK_BC_COST, as specified on C-B link
247 0 // B to A
248 );
249 setupRouterC();
dulalsaurabd0816a32019-07-26 13:11:24 +0000250
Junxiao Shi0fef0572024-02-22 04:15:49 +0000251 // Calculation should consider 0 link-cost between A and B
252 calculatePath();
dulalsaurabd0816a32019-07-26 13:11:24 +0000253
254 // Router A should be able to get to B through B and C
Junxiao Shi0fef0572024-02-22 04:15:49 +0000255 checkRoutingTableEntry(ROUTER_B_NAME, {
256 {ROUTER_B_FACE, 0},
257 {ROUTER_C_FACE, LINK_AC_COST + LINK_BC_COST},
258 });
dulalsaurabd0816a32019-07-26 13:11:24 +0000259
260 // Router A should be able to get to C through C and B
Junxiao Shi0fef0572024-02-22 04:15:49 +0000261 checkRoutingTableEntry(ROUTER_C_NAME, {
262 {ROUTER_C_FACE, LINK_AC_COST},
263 {ROUTER_B_FACE, 0 + LINK_BC_COST},
264 });
265}
dulalsaurabd0816a32019-07-26 13:11:24 +0000266
Junxiao Shi0fef0572024-02-22 04:15:49 +0000267BOOST_AUTO_TEST_CASE(OnePath)
268{
269 double costBC = 2.0;
270 setupRouterA();
271 setupRouterB(
272 costBC // B to C
273 );
274 setupRouterC(
275 LINK_AC_COST, // C to A
276 costBC // C to B
277 );
dulalsaurabd0816a32019-07-26 13:11:24 +0000278
Junxiao Shi0fef0572024-02-22 04:15:49 +0000279 // Calculate only one path per destination router.
280 conf.setMaxFacesPerPrefix(1);
281 // This triggers a different code path.
282 calculatePath();
dulalsaurabd0816a32019-07-26 13:11:24 +0000283
Junxiao Shi0fef0572024-02-22 04:15:49 +0000284 // Shortest path to router B is via router B.
285 checkRoutingTableEntry(ROUTER_B_NAME, {
286 {ROUTER_B_FACE, LINK_AB_COST},
287 });
288
289 // Shortest path to router C is via router B.
290 checkRoutingTableEntry(ROUTER_C_NAME, {
291 {ROUTER_B_FACE, LINK_AB_COST + costBC},
292 });
293}
294
295BOOST_AUTO_TEST_CASE(SourceRouterAbsent)
296{
297 // RouterA does not exist in the LSDB.
298 // setupRouterA is not invoked.
299 setupRouterB(
300 LINK_BC_COST, // B to C
301 NAN // B to A: skipped
302 );
303 setupRouterC(
304 NAN // C to A: skipped
305 );
306
307 // Calculation should do nothing and not cause errors.
308 calculatePath();
309
310 // There should be no routes.
311 BOOST_CHECK(routingTable.m_rTable.empty());
Vince Lehman41b173e2015-05-07 14:13:26 -0500312}
313
314BOOST_AUTO_TEST_SUITE_END()
315
Davide Pesavento288141a2024-02-13 17:30:35 -0500316} // namespace nlsr::tests