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 | /* |
| 3 | * Copyright (c) 2014-2019, 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 | |
| 33 | namespace nfd { |
| 34 | namespace tests { |
| 35 | |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 37 | BOOST_FIXTURE_TEST_SUITE(TestCleanup, GlobalIoFixture) |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 38 | |
| 39 | BOOST_AUTO_TEST_SUITE(FaceRemoval) |
| 40 | |
| 41 | BOOST_AUTO_TEST_CASE(Basic) |
| 42 | { |
| 43 | NameTree nameTree(16); |
| 44 | Fib fib(nameTree); |
| 45 | Pit pit(nameTree); |
| 46 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 47 | |
| 48 | for (uint64_t i = 0; i < 300; ++i) { |
| 49 | Name name = Name("/P").appendVersion(i); |
| 50 | |
| 51 | fib::Entry* fibEntry = fib.insert(name).first; |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 52 | fibEntry->addOrUpdateNextHop(*face1, 0, 0); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 53 | |
| 54 | shared_ptr<Interest> interest = makeInterest(name); |
| 55 | shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first; |
| 56 | if ((i & 0x01) != 0) { |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 57 | pitEntry->insertOrUpdateInRecord(*face1, 0, *interest); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 58 | } |
| 59 | if ((i & 0x02) != 0) { |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 60 | pitEntry->insertOrUpdateOutRecord(*face1, 0, *interest); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | BOOST_CHECK_EQUAL(fib.size(), 300); |
| 64 | BOOST_CHECK_EQUAL(pit.size(), 300); |
| 65 | |
| 66 | cleanupOnFaceRemoval(nameTree, fib, pit, *face1); |
| 67 | BOOST_CHECK_EQUAL(fib.size(), 0); |
| 68 | BOOST_CHECK_EQUAL(pit.size(), 300); |
| 69 | for (const pit::Entry& pitEntry : pit) { |
| 70 | BOOST_CHECK_EQUAL(pitEntry.hasInRecords(), false); |
| 71 | BOOST_CHECK_EQUAL(pitEntry.hasOutRecords(), false); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | BOOST_AUTO_TEST_CASE(RemoveFibNexthops) |
| 76 | { |
| 77 | Forwarder forwarder; |
| 78 | NameTree& nameTree = forwarder.getNameTree(); |
| 79 | Fib& fib = forwarder.getFib(); |
| 80 | |
| 81 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 82 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 83 | forwarder.addFace(face1); |
| 84 | forwarder.addFace(face2); |
| 85 | |
| 86 | // {} |
| 87 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 88 | BOOST_CHECK_EQUAL(fib.size(), 0); |
| 89 | |
| 90 | fib::Entry* entryA = fib.insert("/A").first; |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 91 | entryA->addOrUpdateNextHop(*face1, 0, 0); |
| 92 | entryA->addOrUpdateNextHop(*face2, 0, 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; |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 96 | entryB->addOrUpdateNextHop(*face1, 0, 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; |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 100 | entryC->addOrUpdateNextHop(*face2, 0, 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; |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 104 | entryB1->addOrUpdateNextHop(*face1, 0, 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; |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 108 | entryB12->addOrUpdateNextHop(*face1, 0, 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 | { |
| 133 | Forwarder forwarder; |
| 134 | Pit& pit = forwarder.getPit(); |
| 135 | |
| 136 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 137 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 138 | forwarder.addFace(face1); |
| 139 | forwarder.addFace(face2); |
| 140 | |
| 141 | // {} |
| 142 | BOOST_CHECK_EQUAL(pit.size(), 0); |
| 143 | |
| 144 | shared_ptr<Interest> interestA = makeInterest("/A"); |
| 145 | shared_ptr<pit::Entry> entryA = pit.insert(*interestA).first; |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 146 | entryA->insertOrUpdateInRecord(*face1, 0, *interestA); |
| 147 | entryA->insertOrUpdateInRecord(*face2, 0, *interestA); |
| 148 | entryA->insertOrUpdateOutRecord(*face1, 0, *interestA); |
| 149 | entryA->insertOrUpdateOutRecord(*face2, 0, *interestA); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 150 | // {'/A':[1,2]} |
| 151 | |
| 152 | shared_ptr<Interest> interestB = makeInterest("/B"); |
| 153 | shared_ptr<pit::Entry> entryB = pit.insert(*interestB).first; |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 154 | entryB->insertOrUpdateInRecord(*face1, 0, *interestB); |
| 155 | entryB->insertOrUpdateOutRecord(*face1, 0, *interestB); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 156 | // {'/A':[1,2], '/B':[1]} |
| 157 | |
| 158 | shared_ptr<Interest> interestC = makeInterest("/C"); |
| 159 | shared_ptr<pit::Entry> entryC = pit.insert(*interestC).first; |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 160 | entryC->insertOrUpdateInRecord(*face2, 0, *interestC); |
| 161 | entryC->insertOrUpdateOutRecord(*face2, 0, *interestC); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 162 | // {'/A':[1,2], '/B':[1], '/C':[2]} |
| 163 | BOOST_CHECK_EQUAL(pit.size(), 3); |
| 164 | |
| 165 | // ---- close face1 ---- |
| 166 | face1->close(); |
| 167 | BOOST_CHECK_EQUAL(face1->getState(), face::FaceState::CLOSED); |
| 168 | // {'/A':[2], '/B':[], '/C':[2]} |
| 169 | BOOST_CHECK_EQUAL(pit.size(), 3); |
| 170 | |
| 171 | shared_ptr<pit::Entry> foundA = pit.find(*interestA); |
| 172 | BOOST_REQUIRE(foundA != nullptr); |
| 173 | BOOST_REQUIRE_EQUAL(foundA->getInRecords().size(), 1); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 174 | BOOST_CHECK_EQUAL(&foundA->getInRecords().front().getFace(), face2.get()); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 175 | BOOST_REQUIRE_EQUAL(foundA->getOutRecords().size(), 1); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 176 | BOOST_CHECK_EQUAL(&foundA->getOutRecords().front().getFace(), face2.get()); |
Junxiao Shi | 02b73f5 | 2016-07-28 01:48:27 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | BOOST_AUTO_TEST_SUITE_END() // FaceRemovalCleanup |
| 180 | |
| 181 | BOOST_AUTO_TEST_SUITE_END() // TestCleanup |
| 182 | BOOST_AUTO_TEST_SUITE_END() // Table |
| 183 | |
| 184 | } // namespace tests |
| 185 | } // namespace nfd |