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 | |
| 22 | #include "nlsr.hpp" |
| 23 | #include "test-common.hpp" |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 24 | #include "route/name-prefix-table.hpp" |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 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(); |
| 126 | BOOST_REQUIRE_EQUAL(it->getNamePrefix(), buptRouterName); |
| 127 | BOOST_REQUIRE_EQUAL(it->getRteList().size(), 1); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL((*it->getRteList().begin())->getDestination(), buptRouterName); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 129 | |
| 130 | ++it; |
| 131 | BOOST_REQUIRE_EQUAL(it->getNamePrefix(), buptAdvertisedName); |
| 132 | BOOST_REQUIRE_EQUAL(it->getRteList().size(), 1); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 133 | BOOST_CHECK_EQUAL((*it->getRteList().begin())->getDestination(), buptRouterName); |
| 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 = |
| 171 | std::find(npt.m_table.begin(), |
| 172 | npt.m_table.end(), |
| 173 | npte1); |
| 174 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 175 | std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = nItr->getRteList(); |
| 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"); |
| 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 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 201 | std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = nItr->getRteList(); |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 202 | |
| 203 | BOOST_CHECK_EQUAL(rtpeList.size(), 1); |
| 204 | BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1); |
Vince Lehman | cae33b6 | 2015-06-05 09:21:30 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | BOOST_AUTO_TEST_SUITE_END() |
| 208 | |
| 209 | } // namespace test |
| 210 | } // namespace nlsr |