Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 35 | : face(m_ioService, m_keyChain) |
| 36 | , nlsr(m_ioService, m_scheduler, face, m_keyChain) |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 37 | , lsdb(nlsr.getLsdb()) |
| 38 | , npt(nlsr.getNamePrefixTable()) |
| 39 | { |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | public: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 43 | ndn::util::DummyClientFace face; |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 44 | Nlsr nlsr; |
| 45 | |
| 46 | Lsdb& lsdb; |
| 47 | NamePrefixTable& npt; |
| 48 | }; |
| 49 | |
| 50 | BOOST_AUTO_TEST_SUITE(TestNamePrefixTable) |
| 51 | |
| 52 | BOOST_FIXTURE_TEST_CASE(Bupt, NamePrefixTableFixture) |
| 53 | { |
| 54 | ConfParameter& conf = nlsr.getConfParameter(); |
| 55 | conf.setNetwork("/ndn"); |
| 56 | conf.setSiteName("/router"); |
| 57 | conf.setRouterName("/a"); |
| 58 | conf.buildRouterPrefix(); |
| 59 | |
| 60 | RoutingTable& routingTable = nlsr.getRoutingTable(); |
| 61 | routingTable.setRoutingCalcInterval(0); |
| 62 | |
| 63 | NamePrefixTable& npt = nlsr.getNamePrefixTable(); |
| 64 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 65 | Adjacent thisRouter(conf.getRouterPrefix(), ndn::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 66 | |
| 67 | ndn::Name buptRouterName("/ndn/cn/edu/bupt/%C1.Router/bupthub"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 68 | Adjacent bupt(buptRouterName, ndn::FaceUri("udp4://10.0.0.2"), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 69 | |
| 70 | // This router's Adjacency LSA |
| 71 | nlsr.getAdjacencyList().insert(bupt); |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 72 | AdjLsa thisRouterAdjLsa(thisRouter.getName(), 1, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 73 | ndn::time::system_clock::now() + ndn::time::seconds::max(), |
| 74 | 2, |
| 75 | nlsr.getAdjacencyList()); |
| 76 | |
| 77 | lsdb.installAdjLsa(thisRouterAdjLsa); |
| 78 | |
| 79 | // BUPT Adjacency LSA |
| 80 | AdjacencyList buptAdjacencies; |
| 81 | buptAdjacencies.insert(thisRouter); |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 82 | AdjLsa buptAdjLsa(buptRouterName, 1, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 83 | ndn::time::system_clock::now() + ndn::time::seconds(5), |
| 84 | 0 , buptAdjacencies); |
| 85 | |
| 86 | lsdb.installAdjLsa(buptAdjLsa); |
| 87 | |
| 88 | // BUPT Name LSA |
| 89 | ndn::Name buptAdvertisedName("/ndn/cn/edu/bupt"); |
| 90 | |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 91 | NamePrefixList buptNames{buptAdvertisedName}; |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 92 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 93 | NameLsa buptNameLsa(buptRouterName, 1, ndn::time::system_clock::now(), |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 94 | buptNames); |
| 95 | |
| 96 | lsdb.installNameLsa(buptNameLsa); |
| 97 | |
| 98 | // Advance clocks to expire LSAs |
| 99 | this->advanceClocks(ndn::time::seconds(15)); |
| 100 | |
| 101 | // LSA expirations should cause NPT entries to be completely removed |
| 102 | NamePrefixTable::const_iterator it = npt.begin(); |
| 103 | BOOST_REQUIRE(it == npt.end()); |
| 104 | |
| 105 | // Install new name LSA |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 106 | NameLsa buptNewNameLsa(buptRouterName, 12, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 107 | ndn::time::system_clock::now() + ndn::time::seconds(3600), |
| 108 | buptNames); |
| 109 | |
| 110 | lsdb.installNameLsa(buptNewNameLsa); |
| 111 | |
| 112 | this->advanceClocks(ndn::time::seconds(1)); |
| 113 | |
| 114 | // Install new adjacency LSA |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 115 | AdjLsa buptNewAdjLsa(buptRouterName, 12, |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 116 | ndn::time::system_clock::now() + ndn::time::seconds(3600), |
| 117 | 0, buptAdjacencies); |
| 118 | lsdb.installAdjLsa(buptNewAdjLsa); |
| 119 | |
| 120 | this->advanceClocks(ndn::time::seconds(1)); |
| 121 | |
| 122 | // Each NPT entry should have a destination router |
| 123 | it = npt.begin(); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 124 | BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptRouterName); |
| 125 | BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1); |
| 126 | BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 127 | |
| 128 | ++it; |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 129 | BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptAdvertisedName); |
| 130 | BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1); |
| 131 | BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | BOOST_FIXTURE_TEST_CASE(AddEntryToPool, NamePrefixTableFixture) |
| 135 | { |
| 136 | NamePrefixTable& npt = nlsr.getNamePrefixTable(); |
| 137 | RoutingTablePoolEntry rtpe1("router1"); |
| 138 | |
| 139 | npt.addRtpeToPool(rtpe1); |
| 140 | |
| 141 | BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1); |
| 142 | BOOST_CHECK_EQUAL(*(npt.m_rtpool.find("router1")->second), rtpe1); |
| 143 | } |
| 144 | |
| 145 | BOOST_FIXTURE_TEST_CASE(RemoveEntryFromPool, NamePrefixTableFixture) |
| 146 | { |
| 147 | NamePrefixTable& npt = nlsr.getNamePrefixTable(); |
| 148 | RoutingTablePoolEntry rtpe1("router1", 0); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 149 | std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 150 | |
| 151 | npt.addRtpeToPool(rtpe1); |
| 152 | |
| 153 | npt.deleteRtpeFromPool(rtpePtr); |
| 154 | |
| 155 | BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 0); |
| 156 | BOOST_CHECK_EQUAL(npt.m_rtpool.count("router1"), 0); |
| 157 | } |
| 158 | |
| 159 | BOOST_FIXTURE_TEST_CASE(AddRoutingEntryToNptEntry, NamePrefixTableFixture) |
| 160 | { |
| 161 | NamePrefixTable& npt = nlsr.getNamePrefixTable(); |
| 162 | RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 163 | std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 164 | NamePrefixTableEntry npte1("/ndn/memphis/rtr2"); |
| 165 | |
| 166 | npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1"); |
| 167 | |
| 168 | NamePrefixTable::NptEntryList::iterator nItr = |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 169 | std::find_if(npt.m_table.begin(), |
| 170 | npt.m_table.end(), |
| 171 | [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) { |
| 172 | return entry->getNamePrefix() == npte1.getNamePrefix(); |
| 173 | }); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 174 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 175 | std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList(); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 176 | std::list<std::shared_ptr<RoutingTablePoolEntry>>::iterator rItr = |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 177 | std::find(rtpeList.begin(), |
| 178 | rtpeList.end(), |
| 179 | rtpePtr); |
| 180 | BOOST_CHECK_EQUAL(**rItr, *rtpePtr); |
| 181 | } |
| 182 | |
| 183 | BOOST_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"); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 189 | npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1)); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 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 = |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 197 | std::find_if(npt.m_table.begin(), |
| 198 | npt.m_table.end(), |
| 199 | [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) { |
| 200 | return entry->getNamePrefix() == npte1.getNamePrefix(); |
| 201 | }); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 202 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 203 | std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList(); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 204 | |
| 205 | BOOST_CHECK_EQUAL(rtpeList.size(), 1); |
| 206 | BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 209 | BOOST_FIXTURE_TEST_CASE(AddNptEntryPtrToRoutingEntry, NamePrefixTableFixture) |
| 210 | { |
| 211 | NamePrefixTable& npt = nlsr.getNamePrefixTable(); |
| 212 | NamePrefixTableEntry npte1("/ndn/memphis/rtr2"); |
| 213 | npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1)); |
| 214 | |
| 215 | npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1"); |
| 216 | |
| 217 | NamePrefixTable::NptEntryList::iterator nItr = |
| 218 | std::find_if(npt.m_table.begin(), |
| 219 | npt.m_table.end(), |
| 220 | [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) { |
| 221 | return entry->getNamePrefix() == npte1.getNamePrefix(); |
| 222 | }); |
| 223 | |
| 224 | std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList(); |
| 225 | |
| 226 | BOOST_CHECK_EQUAL(rtpeList.size(), 1); |
| 227 | |
| 228 | auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries; |
| 229 | |
| 230 | auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix()); |
| 231 | BOOST_REQUIRE(nptIterator != namePrefixPtrs.end()); |
| 232 | auto nptSharedPtr = nptIterator->second.lock(); |
| 233 | BOOST_CHECK_EQUAL(*nptSharedPtr, npte1); |
| 234 | } |
| 235 | |
| 236 | BOOST_FIXTURE_TEST_CASE(RemoveNptEntryPtrFromRoutingEntry, NamePrefixTableFixture) |
| 237 | { |
| 238 | NamePrefixTable& npt = nlsr.getNamePrefixTable(); |
| 239 | NamePrefixTableEntry npte1("/ndn/memphis/rtr1"); |
| 240 | NamePrefixTableEntry npte2("/ndn/memphis/rtr2"); |
| 241 | RoutingTableEntry rte1("/ndn/memphis/destination1"); |
| 242 | npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1)); |
| 243 | npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte2)); |
| 244 | |
| 245 | npt.addEntry(npte1.getNamePrefix(), rte1.getDestination()); |
| 246 | // We have to add two entries, otherwise the routing pool entry will be deleted. |
| 247 | npt.addEntry(npte2.getNamePrefix(), rte1.getDestination()); |
| 248 | npt.removeEntry(npte2.getNamePrefix(), rte1.getDestination()); |
| 249 | |
| 250 | NamePrefixTable::NptEntryList::iterator nItr = |
| 251 | std::find_if(npt.m_table.begin(), |
| 252 | npt.m_table.end(), |
| 253 | [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) { |
| 254 | return entry->getNamePrefix() == npte1.getNamePrefix(); |
| 255 | }); |
| 256 | |
| 257 | std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList(); |
| 258 | |
| 259 | BOOST_CHECK_EQUAL(rtpeList.size(), 1); |
| 260 | |
| 261 | auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries; |
| 262 | |
| 263 | // We should have removed the second one |
| 264 | BOOST_CHECK_EQUAL(namePrefixPtrs.size(), 1); |
| 265 | |
| 266 | auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix()); |
| 267 | |
| 268 | BOOST_REQUIRE(nptIterator != namePrefixPtrs.end()); |
| 269 | auto nptSharedPtr = nptIterator->second.lock(); |
| 270 | BOOST_CHECK_EQUAL(*nptSharedPtr, npte1); |
| 271 | } |
| 272 | |
| 273 | BOOST_FIXTURE_TEST_CASE(RoutingTableUpdate, NamePrefixTableFixture) |
| 274 | { |
| 275 | NamePrefixTable& namePrefixTable = nlsr.getNamePrefixTable(); |
| 276 | RoutingTable& routingTable = nlsr.getRoutingTable(); |
| 277 | const ndn::Name destination = ndn::Name{"/ndn/destination1"}; |
| 278 | NextHop hop1{"upd4://10.0.0.1", 0}; |
| 279 | NextHop hop2{"udp4://10.0.0.2", 1}; |
| 280 | NextHop hop3{"udp4://10.0.0.3", 2}; |
| 281 | const NamePrefixTableEntry entry1{"/ndn/router1"}; |
| 282 | namePrefixTable.addEntry(entry1.getNamePrefix(), destination); |
| 283 | |
| 284 | routingTable.addNextHop(destination, hop1); |
| 285 | routingTable.addNextHop(destination, hop2); |
| 286 | |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 287 | namePrefixTable.updateWithNewRoute(routingTable.m_rTable); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 288 | |
| 289 | // At this point the NamePrefixTableEntry should have two NextHops. |
| 290 | auto nameIterator = std::find_if(namePrefixTable.begin(), namePrefixTable.end(), |
| 291 | [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) { |
| 292 | return entry1.getNamePrefix() == entry->getNamePrefix(); |
| 293 | }); |
| 294 | BOOST_REQUIRE(nameIterator != namePrefixTable.end()); |
| 295 | |
| 296 | auto iterator = namePrefixTable.m_rtpool.find(destination); |
| 297 | BOOST_REQUIRE(iterator != namePrefixTable.m_rtpool.end()); |
| 298 | auto nextHops = (iterator->second)->getNexthopList(); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 299 | BOOST_CHECK_EQUAL(nextHops.size(), 2); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 300 | |
| 301 | // Add the other NextHop |
| 302 | routingTable.addNextHop(destination, hop3); |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 303 | namePrefixTable.updateWithNewRoute(routingTable.m_rTable); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 304 | |
| 305 | // At this point the NamePrefixTableEntry should have three NextHops. |
| 306 | nameIterator = std::find_if(namePrefixTable.begin(), namePrefixTable.end(), |
| 307 | [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) { |
| 308 | return entry1.getNamePrefix() == entry->getNamePrefix(); |
| 309 | }); |
| 310 | BOOST_REQUIRE(nameIterator != namePrefixTable.end()); |
| 311 | iterator = namePrefixTable.m_rtpool.find(destination); |
| 312 | BOOST_REQUIRE(iterator != namePrefixTable.m_rtpool.end()); |
| 313 | nextHops = (iterator->second)->getNexthopList(); |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 314 | BOOST_CHECK_EQUAL(nextHops.size(), 3); |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 317 | BOOST_AUTO_TEST_SUITE_END() |
| 318 | |
| 319 | } // namespace test |
| 320 | } // namespace nlsr |