Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 4 | * 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 Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 22 | #include "route/name-prefix-table.hpp" |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 23 | #include "nlsr.hpp" |
| 24 | #include "test-common.hpp" |
| 25 | |
| 26 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 27 | |
| 28 | namespace nlsr { |
| 29 | namespace test { |
| 30 | |
| 31 | class NamePrefixTableFixture : public UnitTestTimeFixture |
| 32 | { |
| 33 | public: |
| 34 | NamePrefixTableFixture() |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 35 | : face(std::make_shared<ndn::util::DummyClientFace>(g_ioService)) |
| 36 | , nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain) |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 37 | , lsdb(nlsr.getLsdb()) |
| 38 | , npt(nlsr.getNamePrefixTable()) |
| 39 | { |
| 40 | INIT_LOGGERS("/tmp", "DEBUG"); |
| 41 | } |
| 42 | |
| 43 | public: |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 44 | std::shared_ptr<ndn::util::DummyClientFace> face; |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 45 | Nlsr nlsr; |
| 46 | |
| 47 | Lsdb& lsdb; |
| 48 | NamePrefixTable& npt; |
| 49 | }; |
| 50 | |
| 51 | BOOST_AUTO_TEST_SUITE(TestNamePrefixTable) |
| 52 | |
| 53 | BOOST_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 Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 66 | Adjacent thisRouter(conf.getRouterPrefix(), ndn::util::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 67 | |
| 68 | ndn::Name buptRouterName("/ndn/cn/edu/bupt/%C1.Router/bupthub"); |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 69 | Adjacent bupt(buptRouterName, ndn::util::FaceUri("udp4://10.0.0.2"), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 70 | |
| 71 | // This router's Adjacency LSA |
| 72 | nlsr.getAdjacencyList().insert(bupt); |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 73 | AdjLsa thisRouterAdjLsa(thisRouter.getName(), 1, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 74 | 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 Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 83 | AdjLsa buptAdjLsa(buptRouterName, 1, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 84 | 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 Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 95 | NameLsa buptNameLsa(buptRouterName, 1, ndn::time::system_clock::now(), |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 96 | 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 Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 108 | NameLsa buptNewNameLsa(buptRouterName, 12, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 109 | 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 Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 117 | AdjLsa buptNewAdjLsa(buptRouterName, 12, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 118 | 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 Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 126 | BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptRouterName); |
| 127 | BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1); |
| 128 | BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 129 | |
| 130 | ++it; |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 131 | BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptAdvertisedName); |
| 132 | BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1); |
| 133 | BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | BOOST_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 | |
| 147 | BOOST_FIXTURE_TEST_CASE(RemoveEntryFromPool, NamePrefixTableFixture) |
| 148 | { |
| 149 | NamePrefixTable& npt = nlsr.getNamePrefixTable(); |
| 150 | RoutingTablePoolEntry rtpe1("router1", 0); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 151 | std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 152 | |
| 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 | |
| 161 | BOOST_FIXTURE_TEST_CASE(AddRoutingEntryToNptEntry, NamePrefixTableFixture) |
| 162 | { |
| 163 | NamePrefixTable& npt = nlsr.getNamePrefixTable(); |
| 164 | RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 165 | std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 166 | NamePrefixTableEntry npte1("/ndn/memphis/rtr2"); |
| 167 | |
| 168 | npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1"); |
| 169 | |
| 170 | NamePrefixTable::NptEntryList::iterator nItr = |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 171 | 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 Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 176 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 177 | std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList(); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 178 | std::list<std::shared_ptr<RoutingTablePoolEntry>>::iterator rItr = |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 179 | std::find(rtpeList.begin(), |
| 180 | rtpeList.end(), |
| 181 | rtpePtr); |
| 182 | BOOST_CHECK_EQUAL(**rItr, *rtpePtr); |
| 183 | } |
| 184 | |
| 185 | BOOST_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 Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 191 | npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1)); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 192 | |
| 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 Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 199 | 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 Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 204 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 205 | std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList(); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 206 | |
| 207 | BOOST_CHECK_EQUAL(rtpeList.size(), 1); |
| 208 | BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 209 | } |
| 210 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame^] | 211 | BOOST_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 | |
| 238 | BOOST_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 | |
| 275 | BOOST_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 Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 319 | BOOST_AUTO_TEST_SUITE_END() |
| 320 | |
| 321 | } // namespace test |
| 322 | } // namespace nlsr |