blob: de91beac62b161f281e12cb161877a40a8241f48 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -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,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070019 */
Vince Lehman7c603292014-09-11 17:48:16 -050020
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050021#include "route/routing-table.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060022#include "nlsr.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050023#include "route/routing-table-entry.hpp"
24#include "route/nexthop.hpp"
Davide Pesaventocb065f12019-12-27 01:03:34 -050025
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040026#include "tests/io-key-chain-fixture.hpp"
Davide Pesaventocb065f12019-12-27 01:03:34 -050027#include "tests/test-common.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050028
29namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050030namespace test {
31
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040032class RoutingTableFixture : public IoKeyChainFixture
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050033{
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040034private:
35 ndn::Scheduler m_scheduler{m_io};
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070036
37public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000038 ndn::DummyClientFace face{m_io, m_keyChain, {true, true}};
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040039 ConfParameter conf{face, m_keyChain};
40 DummyConfFileProcessor confProcessor{conf};
Ashlesh Gawande85998a12017-12-07 22:22:13 -060041
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040042 Lsdb lsdb{face, m_keyChain, conf};
43 RoutingTable rt{m_scheduler, lsdb, conf};
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070044};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050045
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070046BOOST_AUTO_TEST_SUITE(TestRoutingTable)
47
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040048BOOST_FIXTURE_TEST_CASE(AddNextHop, RoutingTableFixture)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070049{
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050050 NextHop nh1;
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050051 const std::string DEST_ROUTER = "destRouter";
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070052 rt.addNextHop(DEST_ROUTER, nh1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050053
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070054 BOOST_CHECK_EQUAL(rt.findRoutingTableEntry(DEST_ROUTER)->getDestination(), DEST_ROUTER);
55}
56
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040057const uint8_t RoutingTableData1[] = {
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070058 // Header
Junxiao Shi6593a432023-08-21 10:50:28 +000059 0x90, 0x30,
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070060 // Routing table entry
Junxiao Shi6593a432023-08-21 10:50:28 +000061 0x91, 0x2e,
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070062 // Destination
Junxiao Shi6593a432023-08-21 10:50:28 +000063 0x07, 0x07, 0x08, 0x05, 0x64, 0x65, 0x73, 0x74, 0x31,
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070064 // Nexthop
Junxiao Shi6593a432023-08-21 10:50:28 +000065 0x8f, 0x23,
66 // Nexthop.Uri
67 0x8d, 0x17, 0x75, 0x64, 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38,
68 0x2e, 0x33, 0x2e, 0x31, 0x3a, 0x36, 0x33, 0x36, 0x33,
69 // Nexthop.CostDouble
70 0x86, 0x08, 0x3f, 0xfa, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070071};
72
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040073const uint8_t RoutingTableData2[] = {
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070074 // Header
75 0x90, 0x00
76};
77
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040078BOOST_FIXTURE_TEST_CASE(Encode, RoutingTableFixture)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070079{
80 NextHop nexthops;
Junxiao Shi6593a432023-08-21 10:50:28 +000081 nexthops.setConnectingFaceUri(ndn::FaceUri("udp4://192.168.3.1:6363"));
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070082 nexthops.setRouteCost(1.65);
83 rt.addNextHop("dest1", nexthops);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040084 BOOST_TEST(rt.wireEncode() == RoutingTableData1, boost::test_tools::per_element());
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070085}
86
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040087BOOST_FIXTURE_TEST_CASE(EncodeEmpty, RoutingTableFixture)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070088{
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040089 BOOST_TEST(rt.wireEncode() == RoutingTableData2, boost::test_tools::per_element());
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070090}
91
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040092BOOST_FIXTURE_TEST_CASE(Decode, RoutingTableFixture)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070093{
Davide Pesaventoe28d8752022-03-19 03:55:25 -040094 RoutingTableStatus rtStatus(ndn::Block{RoutingTableData1});
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070095
96 auto it1 = rtStatus.m_rTable.begin();
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070097 ndn::Name des1 = it1->getDestination();
98 BOOST_CHECK_EQUAL(des1, "dest1");
99
100 auto it2 = it1->getNexthopList().begin();
Junxiao Shi6593a432023-08-21 10:50:28 +0000101 BOOST_CHECK_EQUAL(it2->getConnectingFaceUri(), ndn::FaceUri("udp4://192.168.3.1:6363"));
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700102 BOOST_CHECK_EQUAL(it2->getRouteCost(), 1.65);
103
104 BOOST_CHECK_EQUAL(rtStatus.m_rTable.size(), 1);
105}
106
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400107BOOST_FIXTURE_TEST_CASE(OutputStream, RoutingTableFixture)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700108{
109 NextHop nexthops;
Junxiao Shi6593a432023-08-21 10:50:28 +0000110 nexthops.setConnectingFaceUri(ndn::FaceUri("udp4://192.168.3.1:6363"));
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700111 nexthops.setRouteCost(99);
112 rt.addNextHop("dest1", nexthops);
113
114 std::ostringstream os;
115 os << rt;
116
117 BOOST_CHECK_EQUAL(os.str(),
118 "Routing Table:\n"
119 " Destination: /dest1\n"
Junxiao Shi6593a432023-08-21 10:50:28 +0000120 " NextHop(Uri: udp4://192.168.3.1:6363, Cost: 99)\n");
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500121}
122
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700123BOOST_FIXTURE_TEST_CASE(UpdateFromLsdb, RoutingTableFixture)
124{
Davide Pesavento658fd852023-05-10 22:15:03 -0400125 auto testTimePoint = ndn::time::system_clock::now() + 3600_s;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700126 ndn::Name router2("/router2");
Junxiao Shib8752932024-01-07 15:18:46 +0000127 AdjLsa adjLsa(router2, 12, testTimePoint, conf.getAdjacencyList());
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700128 std::shared_ptr<Lsa> lsaPtr = std::make_shared<AdjLsa>(adjLsa);
129 BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
130 lsdb.installLsa(lsaPtr);
131 BOOST_CHECK(rt.m_isRouteCalculationScheduled);
132
133 // After 15_s (by default) routing table calculation is done
134 advanceClocks(15_s);
135 BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
136
137 // Update to installed LSA
138 std::shared_ptr<Lsa> lsaPtr2 = std::make_shared<AdjLsa>(adjLsa);
139 auto adjPtr = std::static_pointer_cast<AdjLsa>(lsaPtr2);
140 adjPtr->addAdjacent(Adjacent("router3"));
141 adjPtr->setSeqNo(13);
142 lsdb.installLsa(lsaPtr2);
143 BOOST_CHECK(rt.m_isRouteCalculationScheduled);
144
145 // Insert a neighbor so that AdjLsa can be installed
146 AdjacencyList adjl;
147 Adjacent ownAdj(conf.getRouterPrefix());
148 ownAdj.setStatus(Adjacent::STATUS_ACTIVE);
149 adjl.insert(ownAdj);
Junxiao Shib8752932024-01-07 15:18:46 +0000150 AdjLsa adjLsa4("/router4", 12, testTimePoint, adjl);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700151 lsaPtr = std::make_shared<AdjLsa>(adjLsa4);
152 lsdb.installLsa(lsaPtr);
153
154 Adjacent adj("/router4");
155 adj.setStatus(Adjacent::STATUS_ACTIVE);
156 conf.getAdjacencyList().insert(adj);
157 lsdb.scheduleAdjLsaBuild();
158 BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0);
159 advanceClocks(15_s);
160 BOOST_CHECK_EQUAL(rt.m_rTable.size(), 1);
161
162 rt.wireEncode();
163 BOOST_CHECK(rt.m_wire.isValid());
164 BOOST_CHECK_GT(rt.m_wire.size(), 0);
165
166 // Remove own Adj Lsa - Make sure routing table is wiped out
167 conf.getAdjacencyList().setStatusOfNeighbor("/router4", Adjacent::STATUS_INACTIVE);
168 conf.getAdjacencyList().setTimedOutInterestCount("/router4", HELLO_RETRIES_MAX);
169 lsdb.scheduleAdjLsaBuild();
170 advanceClocks(15_s);
171 BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0);
172 BOOST_CHECK(!rt.m_wire.isValid());
173
174 // Check that HR routing is scheduled, once Coordinate LSA is added
175 BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
176 rt.m_hyperbolicState = HYPERBOLIC_STATE_ON;
177 CoordinateLsa clsa("router5", 12, testTimePoint, 2.5, {30.0});
178 auto clsaPtr = std::make_shared<CoordinateLsa>(clsa);
179 lsdb.installLsa(clsaPtr);
180 BOOST_CHECK(rt.m_isRouteCalculationScheduled);
181
182 Adjacent router5("/router5");
183 router5.setStatus(Adjacent::STATUS_ACTIVE);
184 conf.getAdjacencyList().insert(router5);
185 conf.getAdjacencyList().setStatusOfNeighbor("/router5", Adjacent::STATUS_ACTIVE);
186 advanceClocks(15_s);
187 rt.wireEncode();
188 BOOST_CHECK(rt.m_wire.isValid());
189 BOOST_CHECK_GT(rt.m_wire.size(), 0);
190 BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
191
192 // Emulate HelloProtocol neighbor down
193 conf.getAdjacencyList().setStatusOfNeighbor("/router5", Adjacent::STATUS_INACTIVE);
194 rt.scheduleRoutingTableCalculation();
195 advanceClocks(15_s);
196 BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0);
197 BOOST_CHECK(!rt.m_wire.isValid());
198}
199
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500200BOOST_AUTO_TEST_SUITE_END()
201
Nick Gordonfad8e252016-08-11 14:21:38 -0500202} // namespace test
203} // namespace nlsr