blob: 79cd651e9f9a6bdeede9c37b164e6be2bc0c6fc5 [file] [log] [blame]
Vince Lehmancae33b62015-06-05 09:21:30 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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"
24#include "test-common.hpp"
25
26#include <ndn-cxx/util/dummy-client-face.hpp>
27
28namespace nlsr {
29namespace test {
30
31class NamePrefixTableFixture : public UnitTestTimeFixture
32{
33public:
34 NamePrefixTableFixture()
dmcoomes9f936662017-03-02 10:33:09 -060035 : face(std::make_shared<ndn::util::DummyClientFace>(g_ioService))
36 , nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain)
Vince Lehmancae33b62015-06-05 09:21:30 -050037 , lsdb(nlsr.getLsdb())
38 , npt(nlsr.getNamePrefixTable())
39 {
40 INIT_LOGGERS("/tmp", "DEBUG");
41 }
42
43public:
dmcoomes9f936662017-03-02 10:33:09 -060044 std::shared_ptr<ndn::util::DummyClientFace> face;
Vince Lehmancae33b62015-06-05 09:21:30 -050045 Nlsr nlsr;
46
47 Lsdb& lsdb;
48 NamePrefixTable& npt;
49};
50
51BOOST_AUTO_TEST_SUITE(TestNamePrefixTable)
52
53BOOST_FIXTURE_TEST_CASE(Bupt, NamePrefixTableFixture)
54{
55 ConfParameter& conf = nlsr.getConfParameter();
56 conf.setNetwork("/ndn");
57 conf.setSiteName("/router");
58 conf.setRouterName("/a");
59 conf.buildRouterPrefix();
60
61 RoutingTable& routingTable = nlsr.getRoutingTable();
62 routingTable.setRoutingCalcInterval(0);
63
64 NamePrefixTable& npt = nlsr.getNamePrefixTable();
65
Nick Gordone9733ed2017-04-26 10:48:39 -050066 Adjacent thisRouter(conf.getRouterPrefix(), ndn::util::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehmancae33b62015-06-05 09:21:30 -050067
68 ndn::Name buptRouterName("/ndn/cn/edu/bupt/%C1.Router/bupthub");
Nick Gordone9733ed2017-04-26 10:48:39 -050069 Adjacent bupt(buptRouterName, ndn::util::FaceUri("udp4://10.0.0.2"), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehmancae33b62015-06-05 09:21:30 -050070
71 // This router's Adjacency LSA
72 nlsr.getAdjacencyList().insert(bupt);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060073 AdjLsa thisRouterAdjLsa(thisRouter.getName(), 1,
Vince Lehmancae33b62015-06-05 09:21:30 -050074 ndn::time::system_clock::now() + ndn::time::seconds::max(),
75 2,
76 nlsr.getAdjacencyList());
77
78 lsdb.installAdjLsa(thisRouterAdjLsa);
79
80 // BUPT Adjacency LSA
81 AdjacencyList buptAdjacencies;
82 buptAdjacencies.insert(thisRouter);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060083 AdjLsa buptAdjLsa(buptRouterName, 1,
Vince Lehmancae33b62015-06-05 09:21:30 -050084 ndn::time::system_clock::now() + ndn::time::seconds(5),
85 0 , buptAdjacencies);
86
87 lsdb.installAdjLsa(buptAdjLsa);
88
89 // BUPT Name LSA
90 ndn::Name buptAdvertisedName("/ndn/cn/edu/bupt");
91
92 NamePrefixList buptNames;
93 buptNames.insert(buptAdvertisedName);
94
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060095 NameLsa buptNameLsa(buptRouterName, 1, ndn::time::system_clock::now(),
Vince Lehmancae33b62015-06-05 09:21:30 -050096 buptNames);
97
98 lsdb.installNameLsa(buptNameLsa);
99
100 // Advance clocks to expire LSAs
101 this->advanceClocks(ndn::time::seconds(15));
102
103 // LSA expirations should cause NPT entries to be completely removed
104 NamePrefixTable::const_iterator it = npt.begin();
105 BOOST_REQUIRE(it == npt.end());
106
107 // Install new name LSA
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600108 NameLsa buptNewNameLsa(buptRouterName, 12,
Vince Lehmancae33b62015-06-05 09:21:30 -0500109 ndn::time::system_clock::now() + ndn::time::seconds(3600),
110 buptNames);
111
112 lsdb.installNameLsa(buptNewNameLsa);
113
114 this->advanceClocks(ndn::time::seconds(1));
115
116 // Install new adjacency LSA
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600117 AdjLsa buptNewAdjLsa(buptRouterName, 12,
Vince Lehmancae33b62015-06-05 09:21:30 -0500118 ndn::time::system_clock::now() + ndn::time::seconds(3600),
119 0, buptAdjacencies);
120 lsdb.installAdjLsa(buptNewAdjLsa);
121
122 this->advanceClocks(ndn::time::seconds(1));
123
124 // Each NPT entry should have a destination router
125 it = npt.begin();
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500126 BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptRouterName);
127 BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1);
128 BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName);
Vince Lehmancae33b62015-06-05 09:21:30 -0500129
130 ++it;
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500131 BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptAdvertisedName);
132 BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1);
133 BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500134}
135
136BOOST_FIXTURE_TEST_CASE(AddEntryToPool, NamePrefixTableFixture)
137{
138 NamePrefixTable& npt = nlsr.getNamePrefixTable();
139 RoutingTablePoolEntry rtpe1("router1");
140
141 npt.addRtpeToPool(rtpe1);
142
143 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
144 BOOST_CHECK_EQUAL(*(npt.m_rtpool.find("router1")->second), rtpe1);
145}
146
147BOOST_FIXTURE_TEST_CASE(RemoveEntryFromPool, NamePrefixTableFixture)
148{
149 NamePrefixTable& npt = nlsr.getNamePrefixTable();
150 RoutingTablePoolEntry rtpe1("router1", 0);
dmcoomes9f936662017-03-02 10:33:09 -0600151 std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500152
153 npt.addRtpeToPool(rtpe1);
154
155 npt.deleteRtpeFromPool(rtpePtr);
156
157 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 0);
158 BOOST_CHECK_EQUAL(npt.m_rtpool.count("router1"), 0);
159}
160
161BOOST_FIXTURE_TEST_CASE(AddRoutingEntryToNptEntry, NamePrefixTableFixture)
162{
163 NamePrefixTable& npt = nlsr.getNamePrefixTable();
164 RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
dmcoomes9f936662017-03-02 10:33:09 -0600165 std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500166 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
167
168 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
169
170 NamePrefixTable::NptEntryList::iterator nItr =
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500171 std::find_if(npt.m_table.begin(),
172 npt.m_table.end(),
173 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
174 return entry->getNamePrefix() == npte1.getNamePrefix();
175 });
Nick Gordonb50e51b2016-07-22 16:05:57 -0500176
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500177 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
dmcoomes9f936662017-03-02 10:33:09 -0600178 std::list<std::shared_ptr<RoutingTablePoolEntry>>::iterator rItr =
Nick Gordonb50e51b2016-07-22 16:05:57 -0500179 std::find(rtpeList.begin(),
180 rtpeList.end(),
181 rtpePtr);
182 BOOST_CHECK_EQUAL(**rItr, *rtpePtr);
183}
184
185BOOST_FIXTURE_TEST_CASE(RemoveRoutingEntryFromNptEntry, NamePrefixTableFixture)
186{
187 NamePrefixTable& npt = nlsr.getNamePrefixTable();
188 RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
189
190 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500191 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
Nick Gordonb50e51b2016-07-22 16:05:57 -0500192
193 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
194 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/altrtr");
195
196 npt.removeEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
197
198 NamePrefixTable::NptEntryList::iterator nItr =
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500199 std::find_if(npt.m_table.begin(),
200 npt.m_table.end(),
201 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
202 return entry->getNamePrefix() == npte1.getNamePrefix();
203 });
Nick Gordonb50e51b2016-07-22 16:05:57 -0500204
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500205 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
Nick Gordonb50e51b2016-07-22 16:05:57 -0500206
207 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
208 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
Vince Lehmancae33b62015-06-05 09:21:30 -0500209}
210
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500211BOOST_FIXTURE_TEST_CASE(AddNptEntryPtrToRoutingEntry, NamePrefixTableFixture)
212{
213 NamePrefixTable& npt = nlsr.getNamePrefixTable();
214 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
215 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
216
217 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
218
219 NamePrefixTable::NptEntryList::iterator nItr =
220 std::find_if(npt.m_table.begin(),
221 npt.m_table.end(),
222 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
223 return entry->getNamePrefix() == npte1.getNamePrefix();
224 });
225
226 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
227
228 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
229
230 auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries;
231
232 auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix());
233 BOOST_REQUIRE(nptIterator != namePrefixPtrs.end());
234 auto nptSharedPtr = nptIterator->second.lock();
235 BOOST_CHECK_EQUAL(*nptSharedPtr, npte1);
236}
237
238BOOST_FIXTURE_TEST_CASE(RemoveNptEntryPtrFromRoutingEntry, NamePrefixTableFixture)
239{
240 NamePrefixTable& npt = nlsr.getNamePrefixTable();
241 NamePrefixTableEntry npte1("/ndn/memphis/rtr1");
242 NamePrefixTableEntry npte2("/ndn/memphis/rtr2");
243 RoutingTableEntry rte1("/ndn/memphis/destination1");
244 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
245 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte2));
246
247 npt.addEntry(npte1.getNamePrefix(), rte1.getDestination());
248 // We have to add two entries, otherwise the routing pool entry will be deleted.
249 npt.addEntry(npte2.getNamePrefix(), rte1.getDestination());
250 npt.removeEntry(npte2.getNamePrefix(), rte1.getDestination());
251
252 NamePrefixTable::NptEntryList::iterator nItr =
253 std::find_if(npt.m_table.begin(),
254 npt.m_table.end(),
255 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
256 return entry->getNamePrefix() == npte1.getNamePrefix();
257 });
258
259 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
260
261 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
262
263 auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries;
264
265 // We should have removed the second one
266 BOOST_CHECK_EQUAL(namePrefixPtrs.size(), 1);
267
268 auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix());
269
270 BOOST_REQUIRE(nptIterator != namePrefixPtrs.end());
271 auto nptSharedPtr = nptIterator->second.lock();
272 BOOST_CHECK_EQUAL(*nptSharedPtr, npte1);
273}
274
275BOOST_FIXTURE_TEST_CASE(RoutingTableUpdate, NamePrefixTableFixture)
276{
277 NamePrefixTable& namePrefixTable = nlsr.getNamePrefixTable();
278 RoutingTable& routingTable = nlsr.getRoutingTable();
279 const ndn::Name destination = ndn::Name{"/ndn/destination1"};
280 NextHop hop1{"upd4://10.0.0.1", 0};
281 NextHop hop2{"udp4://10.0.0.2", 1};
282 NextHop hop3{"udp4://10.0.0.3", 2};
283 const NamePrefixTableEntry entry1{"/ndn/router1"};
284 namePrefixTable.addEntry(entry1.getNamePrefix(), destination);
285
286 routingTable.addNextHop(destination, hop1);
287 routingTable.addNextHop(destination, hop2);
288
289 namePrefixTable.updateWithNewRoute();
290
291 // At this point the NamePrefixTableEntry should have two NextHops.
292 auto nameIterator = std::find_if(namePrefixTable.begin(), namePrefixTable.end(),
293 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
294 return entry1.getNamePrefix() == entry->getNamePrefix();
295 });
296 BOOST_REQUIRE(nameIterator != namePrefixTable.end());
297
298 auto iterator = namePrefixTable.m_rtpool.find(destination);
299 BOOST_REQUIRE(iterator != namePrefixTable.m_rtpool.end());
300 auto nextHops = (iterator->second)->getNexthopList();
301 BOOST_CHECK_EQUAL(nextHops.getSize(), 2);
302
303 // Add the other NextHop
304 routingTable.addNextHop(destination, hop3);
305 namePrefixTable.updateWithNewRoute();
306
307 // At this point the NamePrefixTableEntry should have three NextHops.
308 nameIterator = std::find_if(namePrefixTable.begin(), namePrefixTable.end(),
309 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
310 return entry1.getNamePrefix() == entry->getNamePrefix();
311 });
312 BOOST_REQUIRE(nameIterator != namePrefixTable.end());
313 iterator = namePrefixTable.m_rtpool.find(destination);
314 BOOST_REQUIRE(iterator != namePrefixTable.m_rtpool.end());
315 nextHops = (iterator->second)->getNexthopList();
316 BOOST_CHECK_EQUAL(nextHops.getSize(), 3);
317}
318
Vince Lehmancae33b62015-06-05 09:21:30 -0500319BOOST_AUTO_TEST_SUITE_END()
320
321} // namespace test
322} // namespace nlsr