Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/fib.hpp" |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 27 | #include "table/pit.hpp" |
| 28 | #include "table/measurements.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 29 | #include "tests/daemon/face/dummy-face.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 31 | #include "tests/test-common.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 34 | namespace fib { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | namespace tests { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 37 | using namespace nfd::tests; |
| 38 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(Table) |
| 40 | BOOST_FIXTURE_TEST_SUITE(TestFib, BaseFixture) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(FibEntry) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 43 | { |
| 44 | Name prefix("ndn:/pxWhfFza"); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 45 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 46 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 48 | Entry entry(prefix); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(entry.getPrefix(), prefix); |
| 50 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 51 | // [] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 53 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 54 | entry.addNextHop(*face1, 20); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 55 | // [(face1,20)] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
| 57 | BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get()); |
| 58 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 20); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 60 | entry.addNextHop(*face1, 30); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 61 | // [(face1,30)] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
| 63 | BOOST_CHECK_EQUAL(&entry.getNextHops().begin()->getFace(), face1.get()); |
| 64 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 30); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 65 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 66 | entry.addNextHop(*face2, 40); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 67 | // [(face1,30), (face2,40)] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2); |
| 69 | { |
| 70 | NextHopList::const_iterator it = entry.getNextHops().begin(); |
| 71 | BOOST_REQUIRE(it != entry.getNextHops().end()); |
| 72 | BOOST_CHECK_EQUAL(&it->getFace(), face1.get()); |
| 73 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 74 | |
| 75 | ++it; |
| 76 | BOOST_REQUIRE(it != entry.getNextHops().end()); |
| 77 | BOOST_CHECK_EQUAL(&it->getFace(), face2.get()); |
| 78 | BOOST_CHECK_EQUAL(it->getCost(), 40); |
| 79 | |
| 80 | ++it; |
| 81 | BOOST_CHECK(it == entry.getNextHops().end()); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 84 | entry.addNextHop(*face2, 10); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 85 | // [(face2,10), (face1,30)] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 2); |
| 87 | { |
| 88 | NextHopList::const_iterator it = entry.getNextHops().begin(); |
| 89 | BOOST_REQUIRE(it != entry.getNextHops().end()); |
| 90 | BOOST_CHECK_EQUAL(&it->getFace(), face2.get()); |
| 91 | BOOST_CHECK_EQUAL(it->getCost(), 10); |
| 92 | |
| 93 | ++it; |
| 94 | BOOST_REQUIRE(it != entry.getNextHops().end()); |
| 95 | BOOST_CHECK_EQUAL(&it->getFace(), face1.get()); |
| 96 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 97 | |
| 98 | ++it; |
| 99 | BOOST_CHECK(it == entry.getNextHops().end()); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 100 | } |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 101 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 102 | entry.removeNextHop(*face1); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 103 | // [(face2,10)] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
| 105 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId()); |
| 106 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 108 | entry.removeNextHop(*face1); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 109 | // [(face2,10)] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 1); |
| 111 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getFace().getId(), face2->getId()); |
| 112 | BOOST_CHECK_EQUAL(entry.getNextHops().begin()->getCost(), 10); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 114 | entry.removeNextHop(*face2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 115 | // [] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 116 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 117 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 118 | entry.removeNextHop(*face2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 119 | // [] |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(entry.getNextHops().size(), 0); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch) |
| 124 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 125 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 126 | Fib fib(nameTree); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 127 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 128 | // [] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL(fib.size(), 0); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); // the empty entry |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 131 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 132 | std::pair<Entry*, bool> insertRes = fib.insert("/"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 133 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 134 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 135 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 136 | // ['/'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(fib.size(), 1); |
| 138 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 140 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 141 | insertRes = fib.insert("/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 143 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 144 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 145 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 146 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 147 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 148 | insertRes = fib.insert("/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 149 | BOOST_CHECK_EQUAL(insertRes.second, false); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 150 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 151 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 152 | // ['/', '/A'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 153 | BOOST_CHECK_EQUAL(fib.size(), 2); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 154 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 156 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A"); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 158 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 159 | insertRes = fib.insert("/A/B/C"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(insertRes.second, true); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 161 | BOOST_REQUIRE(insertRes.first != nullptr); |
| 162 | BOOST_CHECK_EQUAL(insertRes.first->getPrefix(), "/A/B/C"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 163 | // ['/', '/A', '/A/B/C'] |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(fib.size(), 3); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 165 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A").getPrefix(), "/A"); |
| 167 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B").getPrefix(), "/A"); |
| 168 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/A/B/C/D").getPrefix(), "/A/B/C"); |
| 169 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/E").getPrefix(), "/"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 172 | BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithPitEntry) |
| 173 | { |
| 174 | NameTree nameTree; |
| 175 | Fib fib(nameTree); |
| 176 | |
| 177 | shared_ptr<Data> dataABC = makeData("/A/B/C"); |
| 178 | Name fullNameABC = dataABC->getFullName(); |
| 179 | shared_ptr<Data> dataADE = makeData("/A/D/E"); |
| 180 | Name fullNameADE = dataADE->getFullName(); |
| 181 | fib.insert("/A"); |
| 182 | fib.insert(fullNameABC); |
| 183 | |
| 184 | Pit pit(nameTree); |
| 185 | shared_ptr<Interest> interestAB = makeInterest("/A/B"); |
| 186 | shared_ptr<pit::Entry> pitAB = pit.insert(*interestAB).first; |
| 187 | shared_ptr<Interest> interestABC = makeInterest(fullNameABC); |
| 188 | shared_ptr<pit::Entry> pitABC = pit.insert(*interestABC).first; |
| 189 | shared_ptr<Interest> interestADE = makeInterest(fullNameADE); |
| 190 | shared_ptr<pit::Entry> pitADE = pit.insert(*interestADE).first; |
| 191 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 192 | size_t nNameTreeEntriesBefore = nameTree.size(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 193 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 194 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitAB).getPrefix(), "/A"); |
| 195 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitABC).getPrefix(), fullNameABC); |
| 196 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(*pitADE).getPrefix(), "/A"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 197 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 198 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | BOOST_AUTO_TEST_CASE(LongestPrefixMatchWithMeasurementsEntry) |
| 202 | { |
| 203 | NameTree nameTree; |
| 204 | Fib fib(nameTree); |
| 205 | |
| 206 | fib.insert("/A"); |
| 207 | fib.insert("/A/B/C"); |
| 208 | |
| 209 | Measurements measurements(nameTree); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame^] | 210 | measurements::Entry& mAB = measurements.get("/A/B"); |
| 211 | measurements::Entry& mABCD = measurements.get("/A/B/C/D"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 212 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame^] | 213 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mAB).getPrefix(), "/A"); |
| 214 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch(mABCD).getPrefix(), "/A/B/C"); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 217 | BOOST_AUTO_TEST_CASE(RemoveNextHopFromAllEntries) |
| 218 | { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 219 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 220 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 221 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 222 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 223 | Fib fib(nameTree); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 224 | // {} |
| 225 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 226 | Entry* entryA = fib.insert("/A").first; |
| 227 | entryA->addNextHop(*face1, 0); |
| 228 | entryA->addNextHop(*face2, 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 229 | // {'/A':[1,2]} |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 230 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 231 | Entry* entryB = fib.insert("/B").first; |
| 232 | entryB->addNextHop(*face1, 0); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 233 | // {'/A':[1,2], '/B':[1]} |
| 234 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 235 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 236 | Entry* entryC = fib.insert("/C").first; |
| 237 | entryC->addNextHop(*face2, 1); |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 238 | // {'/A':[1,2], '/B':[1], '/C':[2]} |
| 239 | BOOST_CHECK_EQUAL(fib.size(), 3); |
| 240 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 241 | Entry* entryB1 = fib.insert("/B/1").first; |
| 242 | entryB1->addNextHop(*face1, 0); |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 243 | // {'/A':[1,2], '/B':[1], '/B/1':[1], '/C':[2]} |
| 244 | BOOST_CHECK_EQUAL(fib.size(), 4); |
| 245 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 246 | Entry* entryB12 = fib.insert("/B/1/2").first; |
| 247 | entryB12->addNextHop(*face1, 0); |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 248 | // {'/A':[1,2], '/B':[1], '/B/1':[1], '/B/1/2':[1], '/C':[2]} |
| 249 | BOOST_CHECK_EQUAL(fib.size(), 5); |
| 250 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 251 | ///////////// |
| 252 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 253 | fib.removeNextHopFromAllEntries(*face1); |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 254 | // {'/A':[2], '/C':[2]} |
| 255 | BOOST_CHECK_EQUAL(fib.size(), 2); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 256 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 257 | const Entry& foundA = fib.findLongestPrefixMatch("/A"); |
| 258 | BOOST_CHECK_EQUAL(foundA.getPrefix(), "/A"); |
| 259 | const NextHopList& nexthopsA = foundA.getNextHops(); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 260 | BOOST_CHECK_EQUAL(nexthopsA.size(), 1); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 261 | BOOST_CHECK_EQUAL(&nexthopsA.begin()->getFace(), face2.get()); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 262 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 263 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/B").getPrefix(), "/"); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Junxiao Shi | 4b2e6cb | 2014-12-03 00:51:45 -0700 | [diff] [blame] | 266 | BOOST_AUTO_TEST_CASE(RemoveNextHopFromManyEntries) |
| 267 | { |
| 268 | NameTree nameTree(16); |
| 269 | Fib fib(nameTree); |
| 270 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 271 | |
| 272 | for (uint64_t i = 0; i < 300; ++i) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 273 | Entry* entry = fib.insert(Name("/P").appendVersion(i)).first; |
| 274 | entry->addNextHop(*face1, 0); |
Junxiao Shi | 4b2e6cb | 2014-12-03 00:51:45 -0700 | [diff] [blame] | 275 | } |
| 276 | BOOST_CHECK_EQUAL(fib.size(), 300); |
| 277 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 278 | fib.removeNextHopFromAllEntries(*face1); |
Junxiao Shi | 4b2e6cb | 2014-12-03 00:51:45 -0700 | [diff] [blame] | 279 | BOOST_CHECK_EQUAL(fib.size(), 0); |
| 280 | } |
| 281 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 282 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 283 | validateFindExactMatch(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 284 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 285 | const Entry* entry = fib.findExactMatch(target); |
| 286 | BOOST_REQUIRE_MESSAGE(entry != nullptr, "No entry found for " << target); |
| 287 | BOOST_CHECK_EQUAL(entry->getPrefix(), target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 291 | validateNoExactMatch(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 292 | { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 293 | const Entry* entry = fib.findExactMatch(target); |
| 294 | BOOST_CHECK_MESSAGE(entry == nullptr, |
| 295 | "Found unexpected entry for " << target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 298 | BOOST_AUTO_TEST_CASE(ExactMatch) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 299 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 300 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 301 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 302 | fib.insert("/A"); |
| 303 | fib.insert("/A/B"); |
| 304 | fib.insert("/A/B/C"); |
| 305 | |
| 306 | validateFindExactMatch(fib, "/A"); |
| 307 | validateFindExactMatch(fib, "/A/B"); |
| 308 | validateFindExactMatch(fib, "/A/B/C"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 309 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 310 | validateNoExactMatch(fib, "/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 311 | validateNoExactMatch(fib, "/does/not/exist"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 312 | } |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 313 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 314 | BOOST_AUTO_TEST_CASE(ExactMatchGap) |
| 315 | { |
| 316 | NameTree nameTree; |
| 317 | Fib fib(nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 318 | fib.insert("/X"); |
| 319 | fib.insert("/X/Y/Z"); |
| 320 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 321 | validateNoExactMatch(fib, "/X/Y"); |
| 322 | } |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 323 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 324 | BOOST_AUTO_TEST_CASE(ExactMatchEmpty) |
| 325 | { |
| 326 | NameTree nameTree; |
| 327 | Fib fib(nameTree); |
| 328 | validateNoExactMatch(fib, "/"); |
| 329 | validateNoExactMatch(fib, "/nothing/here"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | void |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 333 | validateErase(Fib& fib, const Name& target) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 334 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 335 | fib.erase(target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 336 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 337 | const Entry* entry = fib.findExactMatch(target); |
| 338 | BOOST_CHECK_MESSAGE(entry == nullptr, "Found \"removed\" entry for " << target); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 341 | BOOST_AUTO_TEST_CASE(Erase) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 342 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 343 | NameTree nameTree; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 344 | Fib fib(nameTree); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 345 | fib.insert("/"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 346 | fib.insert("/A"); |
| 347 | fib.insert("/A/B"); |
| 348 | fib.insert("/A/B/C"); |
| 349 | |
| 350 | // check if we remove the right thing and leave |
| 351 | // everything else alone |
| 352 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 353 | validateErase(fib, "/A/B"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 354 | validateFindExactMatch(fib, "/A"); |
| 355 | validateFindExactMatch(fib, "/A/B/C"); |
| 356 | validateFindExactMatch(fib, "/"); |
| 357 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 358 | validateErase(fib, "/A/B/C"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 359 | validateFindExactMatch(fib, "/A"); |
| 360 | validateFindExactMatch(fib, "/"); |
| 361 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 362 | validateErase(fib, "/A"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 363 | validateFindExactMatch(fib, "/"); |
| 364 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 365 | validateErase(fib, "/"); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 366 | validateNoExactMatch(fib, "/"); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 367 | } |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 368 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 369 | BOOST_AUTO_TEST_CASE(EraseGap) |
| 370 | { |
| 371 | NameTree nameTree; |
| 372 | Fib fib(nameTree); |
| 373 | fib.insert("/X"); |
| 374 | fib.insert("/X/Y/Z"); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 375 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 376 | fib.erase("/X/Y"); //should do nothing |
| 377 | validateFindExactMatch(fib, "/X"); |
| 378 | validateFindExactMatch(fib, "/X/Y/Z"); |
| 379 | } |
| 380 | |
| 381 | BOOST_AUTO_TEST_CASE(EraseEmpty) |
| 382 | { |
| 383 | NameTree nameTree; |
| 384 | Fib fib(nameTree); |
| 385 | |
| 386 | BOOST_CHECK_NO_THROW(fib.erase("/does/not/exist")); |
| 387 | validateErase(fib, "/"); |
| 388 | BOOST_CHECK_NO_THROW(fib.erase("/still/does/not/exist")); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 391 | BOOST_AUTO_TEST_CASE(EraseNameTreeEntry) |
| 392 | { |
| 393 | NameTree nameTree; |
| 394 | Fib fib(nameTree); |
| 395 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 396 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 397 | fib.insert("/A/B/C"); |
| 398 | fib.erase("/A/B/C"); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 399 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 400 | } |
| 401 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 402 | BOOST_AUTO_TEST_CASE(Iterator) |
| 403 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 404 | NameTree nameTree; |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 405 | Fib fib(nameTree); |
| 406 | Name nameA("/A"); |
| 407 | Name nameAB("/A/B"); |
| 408 | Name nameABC("/A/B/C"); |
| 409 | Name nameRoot("/"); |
| 410 | |
| 411 | fib.insert(nameA); |
| 412 | fib.insert(nameAB); |
| 413 | fib.insert(nameABC); |
| 414 | fib.insert(nameRoot); |
| 415 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 416 | std::set<Name> expected{nameA, nameAB, nameABC, nameRoot}; |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 417 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 418 | for (Fib::const_iterator it = fib.begin(); it != fib.end(); it++) { |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 419 | bool isInSet = expected.find(it->getPrefix()) != expected.end(); |
| 420 | BOOST_CHECK(isInSet); |
| 421 | expected.erase(it->getPrefix()); |
| 422 | } |
| 423 | |
| 424 | BOOST_CHECK_EQUAL(expected.size(), 0); |
| 425 | } |
| 426 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 427 | BOOST_AUTO_TEST_SUITE_END() // TestFib |
| 428 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 429 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 430 | } // namespace tests |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 431 | } // namespace fib |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 432 | } // namespace nfd |