blob: eca19f6490a0888add96ff0bb61f732645d88293 [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
22#include "nlsr.hpp"
23#include "test-common.hpp"
Nick Gordonb50e51b2016-07-22 16:05:57 -050024#include "route/name-prefix-table.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()
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();
126 BOOST_REQUIRE_EQUAL(it->getNamePrefix(), buptRouterName);
127 BOOST_REQUIRE_EQUAL(it->getRteList().size(), 1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500128 BOOST_CHECK_EQUAL((*it->getRteList().begin())->getDestination(), buptRouterName);
Vince Lehmancae33b62015-06-05 09:21:30 -0500129
130 ++it;
131 BOOST_REQUIRE_EQUAL(it->getNamePrefix(), buptAdvertisedName);
132 BOOST_REQUIRE_EQUAL(it->getRteList().size(), 1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500133 BOOST_CHECK_EQUAL((*it->getRteList().begin())->getDestination(), buptRouterName);
134}
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 =
171 std::find(npt.m_table.begin(),
172 npt.m_table.end(),
173 npte1);
174
dmcoomes9f936662017-03-02 10:33:09 -0600175 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = nItr->getRteList();
176 std::list<std::shared_ptr<RoutingTablePoolEntry>>::iterator rItr =
Nick Gordonb50e51b2016-07-22 16:05:57 -0500177 std::find(rtpeList.begin(),
178 rtpeList.end(),
179 rtpePtr);
180 BOOST_CHECK_EQUAL(**rItr, *rtpePtr);
181}
182
183BOOST_FIXTURE_TEST_CASE(RemoveRoutingEntryFromNptEntry, NamePrefixTableFixture)
184{
185 NamePrefixTable& npt = nlsr.getNamePrefixTable();
186 RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
187
188 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
189 npt.m_table.push_back(npte1);
190
191 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
192 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/altrtr");
193
194 npt.removeEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
195
196 NamePrefixTable::NptEntryList::iterator nItr =
197 std::find(npt.m_table.begin(),
198 npt.m_table.end(),
199 npte1);
200
dmcoomes9f936662017-03-02 10:33:09 -0600201 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = nItr->getRteList();
Nick Gordonb50e51b2016-07-22 16:05:57 -0500202
203 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
204 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
Vince Lehmancae33b62015-06-05 09:21:30 -0500205}
206
207BOOST_AUTO_TEST_SUITE_END()
208
209} // namespace test
210} // namespace nlsr