blob: 5624ad8978df32d67b54f439010c21065562976d [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/*
Davide Pesaventoe28d8752022-03-19 03:55:25 -04003 * Copyright (c) 2014-2022, 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
26#include "tests/test-common.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050027
28namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050029namespace test {
30
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070031class RoutingTableFixture : public UnitTestTimeFixture
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050032{
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070033public:
34 RoutingTableFixture()
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070035 : face(m_ioService, m_keyChain, {true, true})
36 , conf(face, m_keyChain)
37 , confProcessor(conf)
38 , lsdb(face, m_keyChain, conf)
39 , rt(m_scheduler, lsdb, conf)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070040 {
41 }
42
43public:
Ashlesh Gawande85998a12017-12-07 22:22:13 -060044 ndn::util::DummyClientFace face;
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070045 ConfParameter conf;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070046 DummyConfFileProcessor confProcessor;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060047
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070048 Lsdb lsdb;
49 RoutingTable rt;
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070050};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050051
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070052BOOST_AUTO_TEST_SUITE(TestRoutingTable)
53
54BOOST_FIXTURE_TEST_CASE(RoutingTableAddNextHop, RoutingTableFixture)
55{
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050056 NextHop nh1;
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050057 const std::string DEST_ROUTER = "destRouter";
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070058 rt.addNextHop(DEST_ROUTER, nh1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050059
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070060 BOOST_CHECK_EQUAL(rt.findRoutingTableEntry(DEST_ROUTER)->getDestination(), DEST_ROUTER);
61}
62
63const uint8_t RoutingTableData1[] =
64{
65 // Header
66 0x90, 0x20,
67 // Routing table entry
68 0x91, 0x1e,
69 // Destination
70 0x07, 0x07, 0x08, 0x05, 0x64, 0x65, 0x73, 0x74, 0x31, 0x8f, 0x13,
71 // Nexthop
72 0x8d, 0x07, 0x6e, 0x65, 0x78, 0x74, 0x68, 0x6f, 0x70, 0x86, 0x08, 0x3f, 0xfa, 0x66,
73 0x66, 0x66, 0x66, 0x66, 0x66
74};
75
76const uint8_t RoutingTableData2[] =
77{
78 // Header
79 0x90, 0x00
80};
81
82BOOST_FIXTURE_TEST_CASE(RoutingTableEncode1, RoutingTableFixture)
83{
84 NextHop nexthops;
85 nexthops.setConnectingFaceUri("nexthop");
86 nexthops.setRouteCost(1.65);
87 rt.addNextHop("dest1", nexthops);
88
89 auto wire = rt.wireEncode();
90 BOOST_REQUIRE_EQUAL_COLLECTIONS(RoutingTableData1,
91 RoutingTableData1 + sizeof(RoutingTableData1),
92 wire.begin(), wire.end());
93}
94
95BOOST_FIXTURE_TEST_CASE(RoutingTableEncode2, RoutingTableFixture)
96{
97 auto wire = rt.wireEncode();
98 BOOST_REQUIRE_EQUAL_COLLECTIONS(RoutingTableData2,
99 RoutingTableData2 + sizeof(RoutingTableData2),
100 wire.begin(), wire.end());
101}
102
103BOOST_FIXTURE_TEST_CASE(RoutingTableDecode1, RoutingTableFixture)
104{
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400105 RoutingTableStatus rtStatus(ndn::Block{RoutingTableData1});
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700106
107 auto it1 = rtStatus.m_rTable.begin();
108
109 ndn::Name des1 = it1->getDestination();
110 BOOST_CHECK_EQUAL(des1, "dest1");
111
112 auto it2 = it1->getNexthopList().begin();
113 BOOST_CHECK_EQUAL(it2->getConnectingFaceUri(), "nexthop");
114 BOOST_CHECK_EQUAL(it2->getRouteCost(), 1.65);
115
116 BOOST_CHECK_EQUAL(rtStatus.m_rTable.size(), 1);
117}
118
119BOOST_FIXTURE_TEST_CASE(RoutingTableOutputStream, RoutingTableFixture)
120{
121 NextHop nexthops;
122 nexthops.setConnectingFaceUri("nexthop");
123 nexthops.setRouteCost(99);
124 rt.addNextHop("dest1", nexthops);
125
126 std::ostringstream os;
127 os << rt;
128
129 BOOST_CHECK_EQUAL(os.str(),
130 "Routing Table:\n"
131 " Destination: /dest1\n"
132 " NextHop(Uri: nexthop, Cost: 99)\n");
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500133}
134
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700135BOOST_FIXTURE_TEST_CASE(UpdateFromLsdb, RoutingTableFixture)
136{
137 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now() + 3600_s;
138 ndn::Name router2("/router2");
139 AdjLsa adjLsa(router2, 12, testTimePoint, 2, conf.getAdjacencyList());
140 std::shared_ptr<Lsa> lsaPtr = std::make_shared<AdjLsa>(adjLsa);
141 BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
142 lsdb.installLsa(lsaPtr);
143 BOOST_CHECK(rt.m_isRouteCalculationScheduled);
144
145 // After 15_s (by default) routing table calculation is done
146 advanceClocks(15_s);
147 BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
148
149 // Update to installed LSA
150 std::shared_ptr<Lsa> lsaPtr2 = std::make_shared<AdjLsa>(adjLsa);
151 auto adjPtr = std::static_pointer_cast<AdjLsa>(lsaPtr2);
152 adjPtr->addAdjacent(Adjacent("router3"));
153 adjPtr->setSeqNo(13);
154 lsdb.installLsa(lsaPtr2);
155 BOOST_CHECK(rt.m_isRouteCalculationScheduled);
156
157 // Insert a neighbor so that AdjLsa can be installed
158 AdjacencyList adjl;
159 Adjacent ownAdj(conf.getRouterPrefix());
160 ownAdj.setStatus(Adjacent::STATUS_ACTIVE);
161 adjl.insert(ownAdj);
162 AdjLsa adjLsa4("/router4", 12, testTimePoint, 2, adjl);
163 lsaPtr = std::make_shared<AdjLsa>(adjLsa4);
164 lsdb.installLsa(lsaPtr);
165
166 Adjacent adj("/router4");
167 adj.setStatus(Adjacent::STATUS_ACTIVE);
168 conf.getAdjacencyList().insert(adj);
169 lsdb.scheduleAdjLsaBuild();
170 BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0);
171 advanceClocks(15_s);
172 BOOST_CHECK_EQUAL(rt.m_rTable.size(), 1);
173
174 rt.wireEncode();
175 BOOST_CHECK(rt.m_wire.isValid());
176 BOOST_CHECK_GT(rt.m_wire.size(), 0);
177
178 // Remove own Adj Lsa - Make sure routing table is wiped out
179 conf.getAdjacencyList().setStatusOfNeighbor("/router4", Adjacent::STATUS_INACTIVE);
180 conf.getAdjacencyList().setTimedOutInterestCount("/router4", HELLO_RETRIES_MAX);
181 lsdb.scheduleAdjLsaBuild();
182 advanceClocks(15_s);
183 BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0);
184 BOOST_CHECK(!rt.m_wire.isValid());
185
186 // Check that HR routing is scheduled, once Coordinate LSA is added
187 BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
188 rt.m_hyperbolicState = HYPERBOLIC_STATE_ON;
189 CoordinateLsa clsa("router5", 12, testTimePoint, 2.5, {30.0});
190 auto clsaPtr = std::make_shared<CoordinateLsa>(clsa);
191 lsdb.installLsa(clsaPtr);
192 BOOST_CHECK(rt.m_isRouteCalculationScheduled);
193
194 Adjacent router5("/router5");
195 router5.setStatus(Adjacent::STATUS_ACTIVE);
196 conf.getAdjacencyList().insert(router5);
197 conf.getAdjacencyList().setStatusOfNeighbor("/router5", Adjacent::STATUS_ACTIVE);
198 advanceClocks(15_s);
199 rt.wireEncode();
200 BOOST_CHECK(rt.m_wire.isValid());
201 BOOST_CHECK_GT(rt.m_wire.size(), 0);
202 BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
203
204 // Emulate HelloProtocol neighbor down
205 conf.getAdjacencyList().setStatusOfNeighbor("/router5", Adjacent::STATUS_INACTIVE);
206 rt.scheduleRoutingTableCalculation();
207 advanceClocks(15_s);
208 BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0);
209 BOOST_CHECK(!rt.m_wire.isValid());
210}
211
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500212BOOST_AUTO_TEST_SUITE_END()
213
Nick Gordonfad8e252016-08-11 14:21:38 -0500214} // namespace test
215} // namespace nlsr