Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "table/cleanup.hpp" |
| 27 | #include "fw/forwarder.hpp" |
| 28 | |
| 29 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 30 | #include "tests/daemon/global-io-fixture.hpp" |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 31 | #include "tests/daemon/face/dummy-face.hpp" |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 34 | |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(TestCleanup, GlobalIoFixture) |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(FaceRemoval) |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE(Basic) |
| 41 | { |
| 42 | NameTree nameTree(16); |
| 43 | Fib fib(nameTree); |
| 44 | Pit pit(nameTree); |
| 45 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 46 | |
| 47 | for (uint64_t i = 0; i < 300; ++i) { |
| 48 | Name name = Name("/P").appendVersion(i); |
| 49 | |
| 50 | fib::Entry* fibEntry = fib.insert(name).first; |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 51 | fib.addOrUpdateNextHop(*fibEntry, *face1, 0); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 52 | |
| 53 | shared_ptr<Interest> interest = makeInterest(name); |
| 54 | shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first; |
| 55 | if ((i & 0x01) != 0) { |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 56 | pitEntry->insertOrUpdateInRecord(*face1, *interest); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 57 | } |
| 58 | if ((i & 0x02) != 0) { |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 59 | pitEntry->insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | BOOST_CHECK_EQUAL(fib.size(), 300); |
| 63 | BOOST_CHECK_EQUAL(pit.size(), 300); |
| 64 | |
| 65 | cleanupOnFaceRemoval(nameTree, fib, pit, *face1); |
| 66 | BOOST_CHECK_EQUAL(fib.size(), 0); |
| 67 | BOOST_CHECK_EQUAL(pit.size(), 300); |
| 68 | for (const pit::Entry& pitEntry : pit) { |
| 69 | BOOST_CHECK_EQUAL(pitEntry.hasInRecords(), false); |
| 70 | BOOST_CHECK_EQUAL(pitEntry.hasOutRecords(), false); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | BOOST_AUTO_TEST_CASE(RemoveFibNexthops) |
| 75 | { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 76 | FaceTable faceTable; |
| 77 | Forwarder forwarder(faceTable); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 78 | NameTree& nameTree = forwarder.getNameTree(); |
| 79 | Fib& fib = forwarder.getFib(); |
| 80 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 81 | auto face1 = make_shared<DummyFace>(); |
| 82 | auto face2 = make_shared<DummyFace>(); |
| 83 | faceTable.add(face1); |
| 84 | faceTable.add(face2); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 85 | |
| 86 | // {} |
| 87 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 88 | BOOST_CHECK_EQUAL(fib.size(), 0); |
| 89 | |
| 90 | fib::Entry* entryA = fib.insert("/A").first; |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 91 | fib.addOrUpdateNextHop(*entryA, *face1, 0); |
| 92 | fib.addOrUpdateNextHop(*entryA, *face2, 0); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 93 | // {'/A':[1,2]} |
| 94 | |
| 95 | fib::Entry* entryB = fib.insert("/B").first; |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 96 | fib.addOrUpdateNextHop(*entryB, *face1, 0); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 97 | // {'/A':[1,2], '/B':[1]} |
| 98 | |
| 99 | fib::Entry* entryC = fib.insert("/C").first; |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 100 | fib.addOrUpdateNextHop(*entryC, *face2, 1); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 101 | // {'/A':[1,2], '/B':[1], '/C':[2]} |
| 102 | |
| 103 | fib::Entry* entryB1 = fib.insert("/B/1").first; |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 104 | fib.addOrUpdateNextHop(*entryB1, *face1, 0); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 105 | // {'/A':[1,2], '/B':[1], '/B/1':[1], '/C':[2]} |
| 106 | |
| 107 | fib::Entry* entryB12 = fib.insert("/B/1/2").first; |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 108 | fib.addOrUpdateNextHop(*entryB12, *face1, 0); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 109 | // {'/A':[1,2], '/B':[1], '/B/1':[1], '/B/1/2':[1], '/C':[2]} |
| 110 | |
| 111 | // ---- close face1 ---- |
| 112 | face1->close(); |
| 113 | BOOST_CHECK_EQUAL(face1->getState(), face::FaceState::CLOSED); |
| 114 | // {'/A':[2], '/C':[2]} |
| 115 | BOOST_CHECK_EQUAL(fib.size(), 2); |
| 116 | |
| 117 | const fib::Entry& foundA = fib.findLongestPrefixMatch("/A"); |
| 118 | BOOST_CHECK_EQUAL(foundA.getPrefix(), "/A"); |
| 119 | BOOST_CHECK_EQUAL(foundA.getNextHops().size(), 1); |
| 120 | BOOST_CHECK_EQUAL(&foundA.getNextHops().begin()->getFace(), face2.get()); |
| 121 | |
| 122 | BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/B").getPrefix(), "/"); |
| 123 | |
| 124 | // ---- close face2 ---- |
| 125 | face2->close(); |
| 126 | BOOST_CHECK_EQUAL(face2->getState(), face::FaceState::CLOSED); |
| 127 | BOOST_CHECK_EQUAL(fib.size(), 0); |
| 128 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(DeletePitInOutRecords) |
| 132 | { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 133 | FaceTable faceTable; |
| 134 | Forwarder forwarder(faceTable); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 135 | Pit& pit = forwarder.getPit(); |
| 136 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 137 | auto face1 = make_shared<DummyFace>(); |
| 138 | auto face2 = make_shared<DummyFace>(); |
| 139 | faceTable.add(face1); |
| 140 | faceTable.add(face2); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 141 | |
| 142 | // {} |
| 143 | BOOST_CHECK_EQUAL(pit.size(), 0); |
| 144 | |
| 145 | shared_ptr<Interest> interestA = makeInterest("/A"); |
| 146 | shared_ptr<pit::Entry> entryA = pit.insert(*interestA).first; |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 147 | entryA->insertOrUpdateInRecord(*face1, *interestA); |
| 148 | entryA->insertOrUpdateInRecord(*face2, *interestA); |
| 149 | entryA->insertOrUpdateOutRecord(*face1, *interestA); |
| 150 | entryA->insertOrUpdateOutRecord(*face2, *interestA); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 151 | // {'/A':[1,2]} |
| 152 | |
| 153 | shared_ptr<Interest> interestB = makeInterest("/B"); |
| 154 | shared_ptr<pit::Entry> entryB = pit.insert(*interestB).first; |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 155 | entryB->insertOrUpdateInRecord(*face1, *interestB); |
| 156 | entryB->insertOrUpdateOutRecord(*face1, *interestB); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 157 | // {'/A':[1,2], '/B':[1]} |
| 158 | |
| 159 | shared_ptr<Interest> interestC = makeInterest("/C"); |
| 160 | shared_ptr<pit::Entry> entryC = pit.insert(*interestC).first; |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 161 | entryC->insertOrUpdateInRecord(*face2, *interestC); |
| 162 | entryC->insertOrUpdateOutRecord(*face2, *interestC); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 163 | // {'/A':[1,2], '/B':[1], '/C':[2]} |
| 164 | BOOST_CHECK_EQUAL(pit.size(), 3); |
| 165 | |
| 166 | // ---- close face1 ---- |
| 167 | face1->close(); |
| 168 | BOOST_CHECK_EQUAL(face1->getState(), face::FaceState::CLOSED); |
| 169 | // {'/A':[2], '/B':[], '/C':[2]} |
| 170 | BOOST_CHECK_EQUAL(pit.size(), 3); |
| 171 | |
| 172 | shared_ptr<pit::Entry> foundA = pit.find(*interestA); |
| 173 | BOOST_REQUIRE(foundA != nullptr); |
| 174 | BOOST_REQUIRE_EQUAL(foundA->getInRecords().size(), 1); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 175 | BOOST_CHECK_EQUAL(&foundA->getInRecords().front().getFace(), face2.get()); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 176 | BOOST_REQUIRE_EQUAL(foundA->getOutRecords().size(), 1); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(&foundA->getOutRecords().front().getFace(), face2.get()); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | BOOST_AUTO_TEST_SUITE_END() // FaceRemovalCleanup |
| 181 | |
| 182 | BOOST_AUTO_TEST_SUITE_END() // TestCleanup |
| 183 | BOOST_AUTO_TEST_SUITE_END() // Table |
| 184 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 185 | } // namespace nfd::tests |