blob: 1631b9c30432ced31d02976f53f95766623751c5 [file] [log] [blame]
Junxiao Shi02b73f52016-07-28 01:48:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
ashiqopu3ad49db2018-10-20 22:38:47 +00002/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi02b73f52016-07-28 01:48:27 +00004 * 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 Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shi02b73f52016-07-28 01:48:27 +000031#include "tests/daemon/face/dummy-face.hpp"
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
Junxiao Shi02b73f52016-07-28 01:48:27 +000034
Junxiao Shi02b73f52016-07-28 01:48:27 +000035BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040036BOOST_FIXTURE_TEST_SUITE(TestCleanup, GlobalIoFixture)
Junxiao Shi02b73f52016-07-28 01:48:27 +000037
38BOOST_AUTO_TEST_SUITE(FaceRemoval)
39
40BOOST_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 Pand8315bf2019-07-31 06:59:07 +000051 fib.addOrUpdateNextHop(*fibEntry, *face1, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +000052
53 shared_ptr<Interest> interest = makeInterest(name);
54 shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first;
55 if ((i & 0x01) != 0) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000056 pitEntry->insertOrUpdateInRecord(*face1, *interest);
Junxiao Shi02b73f52016-07-28 01:48:27 +000057 }
58 if ((i & 0x02) != 0) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000059 pitEntry->insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shi02b73f52016-07-28 01:48:27 +000060 }
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
74BOOST_AUTO_TEST_CASE(RemoveFibNexthops)
75{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040076 FaceTable faceTable;
77 Forwarder forwarder(faceTable);
Junxiao Shi02b73f52016-07-28 01:48:27 +000078 NameTree& nameTree = forwarder.getNameTree();
79 Fib& fib = forwarder.getFib();
80
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040081 auto face1 = make_shared<DummyFace>();
82 auto face2 = make_shared<DummyFace>();
83 faceTable.add(face1);
84 faceTable.add(face2);
Junxiao Shi02b73f52016-07-28 01:48:27 +000085
86 // {}
87 size_t nNameTreeEntriesBefore = nameTree.size();
88 BOOST_CHECK_EQUAL(fib.size(), 0);
89
90 fib::Entry* entryA = fib.insert("/A").first;
Ju Pand8315bf2019-07-31 06:59:07 +000091 fib.addOrUpdateNextHop(*entryA, *face1, 0);
92 fib.addOrUpdateNextHop(*entryA, *face2, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +000093 // {'/A':[1,2]}
94
95 fib::Entry* entryB = fib.insert("/B").first;
Ju Pand8315bf2019-07-31 06:59:07 +000096 fib.addOrUpdateNextHop(*entryB, *face1, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +000097 // {'/A':[1,2], '/B':[1]}
98
99 fib::Entry* entryC = fib.insert("/C").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000100 fib.addOrUpdateNextHop(*entryC, *face2, 1);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000101 // {'/A':[1,2], '/B':[1], '/C':[2]}
102
103 fib::Entry* entryB1 = fib.insert("/B/1").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000104 fib.addOrUpdateNextHop(*entryB1, *face1, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000105 // {'/A':[1,2], '/B':[1], '/B/1':[1], '/C':[2]}
106
107 fib::Entry* entryB12 = fib.insert("/B/1/2").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000108 fib.addOrUpdateNextHop(*entryB12, *face1, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000109 // {'/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
131BOOST_AUTO_TEST_CASE(DeletePitInOutRecords)
132{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400133 FaceTable faceTable;
134 Forwarder forwarder(faceTable);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000135 Pit& pit = forwarder.getPit();
136
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400137 auto face1 = make_shared<DummyFace>();
138 auto face2 = make_shared<DummyFace>();
139 faceTable.add(face1);
140 faceTable.add(face2);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000141
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 Rahmanc88d2d42019-08-28 20:19:47 +0000147 entryA->insertOrUpdateInRecord(*face1, *interestA);
148 entryA->insertOrUpdateInRecord(*face2, *interestA);
149 entryA->insertOrUpdateOutRecord(*face1, *interestA);
150 entryA->insertOrUpdateOutRecord(*face2, *interestA);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000151 // {'/A':[1,2]}
152
153 shared_ptr<Interest> interestB = makeInterest("/B");
154 shared_ptr<pit::Entry> entryB = pit.insert(*interestB).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000155 entryB->insertOrUpdateInRecord(*face1, *interestB);
156 entryB->insertOrUpdateOutRecord(*face1, *interestB);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000157 // {'/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 Rahmanc88d2d42019-08-28 20:19:47 +0000161 entryC->insertOrUpdateInRecord(*face2, *interestC);
162 entryC->insertOrUpdateOutRecord(*face2, *interestC);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000163 // {'/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 Shi9cff7792016-08-01 21:45:11 +0000175 BOOST_CHECK_EQUAL(&foundA->getInRecords().front().getFace(), face2.get());
Junxiao Shi02b73f52016-07-28 01:48:27 +0000176 BOOST_REQUIRE_EQUAL(foundA->getOutRecords().size(), 1);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000177 BOOST_CHECK_EQUAL(&foundA->getOutRecords().front().getFace(), face2.get());
Junxiao Shi02b73f52016-07-28 01:48:27 +0000178}
179
180BOOST_AUTO_TEST_SUITE_END() // FaceRemovalCleanup
181
182BOOST_AUTO_TEST_SUITE_END() // TestCleanup
183BOOST_AUTO_TEST_SUITE_END() // Table
184
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400185} // namespace nfd::tests