blob: 213ba04b71413707095c6a74e5ca47d9dbf3dd81 [file] [log] [blame]
Vince Lehmancae33b62015-06-05 09:21:30 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, The University of Memphis,
Vince Lehmancae33b62015-06-05 09:21:30 -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/>.
20 **/
21
Nick Gordonc0c6bcf2017-08-15 18:11:21 -050022#include "route/name-prefix-table.hpp"
Vince Lehmancae33b62015-06-05 09:21:30 -050023#include "nlsr.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060024#include "../test-common.hpp"
Vince Lehmancae33b62015-06-05 09:21:30 -050025
26#include <ndn-cxx/util/dummy-client-face.hpp>
27
28namespace nlsr {
29namespace test {
30
31class NamePrefixTableFixture : public UnitTestTimeFixture
32{
33public:
34 NamePrefixTableFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050035 : face(m_ioService, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060036 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060037 , nlsr(face, m_keyChain, conf)
38 , lsdb(nlsr.m_lsdb)
39 , npt(nlsr.m_namePrefixTable)
Vince Lehmancae33b62015-06-05 09:21:30 -050040 {
Vince Lehmancae33b62015-06-05 09:21:30 -050041 }
42
43public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050044 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060045 ConfParameter conf;
Vince Lehmancae33b62015-06-05 09:21:30 -050046 Nlsr nlsr;
47
48 Lsdb& lsdb;
49 NamePrefixTable& npt;
50};
51
52BOOST_AUTO_TEST_SUITE(TestNamePrefixTable)
53
54BOOST_FIXTURE_TEST_CASE(Bupt, NamePrefixTableFixture)
55{
Vince Lehmancae33b62015-06-05 09:21:30 -050056 conf.setNetwork("/ndn");
57 conf.setSiteName("/router");
58 conf.setRouterName("/a");
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050059 conf.buildRouterAndSyncUserPrefix();
Vince Lehmancae33b62015-06-05 09:21:30 -050060
Ashlesh Gawande85998a12017-12-07 22:22:13 -060061 RoutingTable& routingTable = nlsr.m_routingTable;
Vince Lehmancae33b62015-06-05 09:21:30 -050062 routingTable.setRoutingCalcInterval(0);
63
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050064 Adjacent thisRouter(conf.getRouterPrefix(), ndn::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehmancae33b62015-06-05 09:21:30 -050065
66 ndn::Name buptRouterName("/ndn/cn/edu/bupt/%C1.Router/bupthub");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050067 Adjacent bupt(buptRouterName, ndn::FaceUri("udp4://10.0.0.2"), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehmancae33b62015-06-05 09:21:30 -050068
69 // This router's Adjacency LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -060070 conf.getAdjacencyList().insert(bupt);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060071 AdjLsa thisRouterAdjLsa(thisRouter.getName(), 1,
Vince Lehmancae33b62015-06-05 09:21:30 -050072 ndn::time::system_clock::now() + ndn::time::seconds::max(),
73 2,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060074 conf.getAdjacencyList());
Vince Lehmancae33b62015-06-05 09:21:30 -050075
76 lsdb.installAdjLsa(thisRouterAdjLsa);
77
78 // BUPT Adjacency LSA
79 AdjacencyList buptAdjacencies;
80 buptAdjacencies.insert(thisRouter);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060081 AdjLsa buptAdjLsa(buptRouterName, 1,
Vince Lehmancae33b62015-06-05 09:21:30 -050082 ndn::time::system_clock::now() + ndn::time::seconds(5),
83 0 , buptAdjacencies);
84
85 lsdb.installAdjLsa(buptAdjLsa);
86
87 // BUPT Name LSA
88 ndn::Name buptAdvertisedName("/ndn/cn/edu/bupt");
89
Nick Gordon96861ca2017-10-17 18:25:21 -050090 NamePrefixList buptNames{buptAdvertisedName};
Vince Lehmancae33b62015-06-05 09:21:30 -050091
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060092 NameLsa buptNameLsa(buptRouterName, 1, ndn::time::system_clock::now(),
Vince Lehmancae33b62015-06-05 09:21:30 -050093 buptNames);
94
95 lsdb.installNameLsa(buptNameLsa);
96
97 // Advance clocks to expire LSAs
98 this->advanceClocks(ndn::time::seconds(15));
99
100 // LSA expirations should cause NPT entries to be completely removed
101 NamePrefixTable::const_iterator it = npt.begin();
102 BOOST_REQUIRE(it == npt.end());
103
104 // Install new name LSA
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600105 NameLsa buptNewNameLsa(buptRouterName, 12,
Vince Lehmancae33b62015-06-05 09:21:30 -0500106 ndn::time::system_clock::now() + ndn::time::seconds(3600),
107 buptNames);
108
109 lsdb.installNameLsa(buptNewNameLsa);
110
111 this->advanceClocks(ndn::time::seconds(1));
112
113 // Install new adjacency LSA
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600114 AdjLsa buptNewAdjLsa(buptRouterName, 12,
Vince Lehmancae33b62015-06-05 09:21:30 -0500115 ndn::time::system_clock::now() + ndn::time::seconds(3600),
116 0, buptAdjacencies);
117 lsdb.installAdjLsa(buptNewAdjLsa);
118
119 this->advanceClocks(ndn::time::seconds(1));
120
121 // Each NPT entry should have a destination router
122 it = npt.begin();
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500123 BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptRouterName);
124 BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1);
125 BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName);
Vince Lehmancae33b62015-06-05 09:21:30 -0500126
127 ++it;
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500128 BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptAdvertisedName);
129 BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1);
130 BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500131}
132
133BOOST_FIXTURE_TEST_CASE(AddEntryToPool, NamePrefixTableFixture)
134{
Nick Gordonb50e51b2016-07-22 16:05:57 -0500135 RoutingTablePoolEntry rtpe1("router1");
136
137 npt.addRtpeToPool(rtpe1);
138
139 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
140 BOOST_CHECK_EQUAL(*(npt.m_rtpool.find("router1")->second), rtpe1);
141}
142
143BOOST_FIXTURE_TEST_CASE(RemoveEntryFromPool, NamePrefixTableFixture)
144{
Nick Gordonb50e51b2016-07-22 16:05:57 -0500145 RoutingTablePoolEntry rtpe1("router1", 0);
dmcoomes9f936662017-03-02 10:33:09 -0600146 std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500147
148 npt.addRtpeToPool(rtpe1);
149
150 npt.deleteRtpeFromPool(rtpePtr);
151
152 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 0);
153 BOOST_CHECK_EQUAL(npt.m_rtpool.count("router1"), 0);
154}
155
156BOOST_FIXTURE_TEST_CASE(AddRoutingEntryToNptEntry, NamePrefixTableFixture)
157{
Nick Gordonb50e51b2016-07-22 16:05:57 -0500158 RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
dmcoomes9f936662017-03-02 10:33:09 -0600159 std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500160 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
161
162 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
163
164 NamePrefixTable::NptEntryList::iterator nItr =
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500165 std::find_if(npt.m_table.begin(),
166 npt.m_table.end(),
167 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
168 return entry->getNamePrefix() == npte1.getNamePrefix();
169 });
Nick Gordonb50e51b2016-07-22 16:05:57 -0500170
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500171 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
dmcoomes9f936662017-03-02 10:33:09 -0600172 std::list<std::shared_ptr<RoutingTablePoolEntry>>::iterator rItr =
Nick Gordonb50e51b2016-07-22 16:05:57 -0500173 std::find(rtpeList.begin(),
174 rtpeList.end(),
175 rtpePtr);
176 BOOST_CHECK_EQUAL(**rItr, *rtpePtr);
177}
178
179BOOST_FIXTURE_TEST_CASE(RemoveRoutingEntryFromNptEntry, NamePrefixTableFixture)
180{
Nick Gordonb50e51b2016-07-22 16:05:57 -0500181 RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
182
183 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500184 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
Nick Gordonb50e51b2016-07-22 16:05:57 -0500185
186 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
187 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/altrtr");
188
189 npt.removeEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
190
191 NamePrefixTable::NptEntryList::iterator nItr =
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500192 std::find_if(npt.m_table.begin(),
193 npt.m_table.end(),
194 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
195 return entry->getNamePrefix() == npte1.getNamePrefix();
196 });
Nick Gordonb50e51b2016-07-22 16:05:57 -0500197
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500198 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
Nick Gordonb50e51b2016-07-22 16:05:57 -0500199
200 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
201 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
Vince Lehmancae33b62015-06-05 09:21:30 -0500202}
203
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500204BOOST_FIXTURE_TEST_CASE(AddNptEntryPtrToRoutingEntry, NamePrefixTableFixture)
205{
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500206 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
207 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
208
209 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
210
211 NamePrefixTable::NptEntryList::iterator nItr =
212 std::find_if(npt.m_table.begin(),
213 npt.m_table.end(),
214 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
215 return entry->getNamePrefix() == npte1.getNamePrefix();
216 });
217
218 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
219
220 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
221
222 auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries;
223
224 auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix());
225 BOOST_REQUIRE(nptIterator != namePrefixPtrs.end());
226 auto nptSharedPtr = nptIterator->second.lock();
227 BOOST_CHECK_EQUAL(*nptSharedPtr, npte1);
228}
229
230BOOST_FIXTURE_TEST_CASE(RemoveNptEntryPtrFromRoutingEntry, NamePrefixTableFixture)
231{
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500232 NamePrefixTableEntry npte1("/ndn/memphis/rtr1");
233 NamePrefixTableEntry npte2("/ndn/memphis/rtr2");
234 RoutingTableEntry rte1("/ndn/memphis/destination1");
235 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
236 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte2));
237
238 npt.addEntry(npte1.getNamePrefix(), rte1.getDestination());
239 // We have to add two entries, otherwise the routing pool entry will be deleted.
240 npt.addEntry(npte2.getNamePrefix(), rte1.getDestination());
241 npt.removeEntry(npte2.getNamePrefix(), rte1.getDestination());
242
243 NamePrefixTable::NptEntryList::iterator nItr =
244 std::find_if(npt.m_table.begin(),
245 npt.m_table.end(),
246 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
247 return entry->getNamePrefix() == npte1.getNamePrefix();
248 });
249
250 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
251
252 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
253
254 auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries;
255
256 // We should have removed the second one
257 BOOST_CHECK_EQUAL(namePrefixPtrs.size(), 1);
258
259 auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix());
260
261 BOOST_REQUIRE(nptIterator != namePrefixPtrs.end());
262 auto nptSharedPtr = nptIterator->second.lock();
263 BOOST_CHECK_EQUAL(*nptSharedPtr, npte1);
264}
265
266BOOST_FIXTURE_TEST_CASE(RoutingTableUpdate, NamePrefixTableFixture)
267{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600268 RoutingTable& routingTable = nlsr.m_routingTable;
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500269 const ndn::Name destination = ndn::Name{"/ndn/destination1"};
270 NextHop hop1{"upd4://10.0.0.1", 0};
271 NextHop hop2{"udp4://10.0.0.2", 1};
272 NextHop hop3{"udp4://10.0.0.3", 2};
273 const NamePrefixTableEntry entry1{"/ndn/router1"};
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600274 npt.addEntry(entry1.getNamePrefix(), destination);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500275
276 routingTable.addNextHop(destination, hop1);
277 routingTable.addNextHop(destination, hop2);
278
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600279 npt.updateWithNewRoute(routingTable.m_rTable);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500280
281 // At this point the NamePrefixTableEntry should have two NextHops.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600282 auto nameIterator = std::find_if(npt.begin(), npt.end(),
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500283 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
284 return entry1.getNamePrefix() == entry->getNamePrefix();
285 });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600286 BOOST_REQUIRE(nameIterator != npt.end());
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500287
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600288 auto iterator = npt.m_rtpool.find(destination);
289 BOOST_REQUIRE(iterator != npt.m_rtpool.end());
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500290 auto nextHops = (iterator->second)->getNexthopList();
Nick Gordonff9a6272017-10-12 13:38:29 -0500291 BOOST_CHECK_EQUAL(nextHops.size(), 2);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500292
293 // Add the other NextHop
294 routingTable.addNextHop(destination, hop3);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600295 npt.updateWithNewRoute(routingTable.m_rTable);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500296
297 // At this point the NamePrefixTableEntry should have three NextHops.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600298 nameIterator = std::find_if(npt.begin(), npt.end(),
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500299 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
300 return entry1.getNamePrefix() == entry->getNamePrefix();
301 });
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600302 BOOST_REQUIRE(nameIterator != npt.end());
303 iterator = npt.m_rtpool.find(destination);
304 BOOST_REQUIRE(iterator != npt.m_rtpool.end());
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500305 nextHops = (iterator->second)->getNexthopList();
Nick Gordonff9a6272017-10-12 13:38:29 -0500306 BOOST_CHECK_EQUAL(nextHops.size(), 3);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500307}
308
Vince Lehmancae33b62015-06-05 09:21:30 -0500309BOOST_AUTO_TEST_SUITE_END()
310
311} // namespace test
312} // namespace nlsr