blob: b7395a397bfa34ba3cbff84ee54521539d91208c [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/*
3 * Copyright (c) 2014-2019, 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
33namespace nfd {
34namespace tests {
35
Junxiao Shi02b73f52016-07-28 01:48:27 +000036BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040037BOOST_FIXTURE_TEST_SUITE(TestCleanup, GlobalIoFixture)
Junxiao Shi02b73f52016-07-28 01:48:27 +000038
39BOOST_AUTO_TEST_SUITE(FaceRemoval)
40
41BOOST_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;
Ju Pand8315bf2019-07-31 06:59:07 +000052 fib.addOrUpdateNextHop(*fibEntry, *face1, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +000053
54 shared_ptr<Interest> interest = makeInterest(name);
55 shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first;
56 if ((i & 0x01) != 0) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000057 pitEntry->insertOrUpdateInRecord(*face1, *interest);
Junxiao Shi02b73f52016-07-28 01:48:27 +000058 }
59 if ((i & 0x02) != 0) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000060 pitEntry->insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shi02b73f52016-07-28 01:48:27 +000061 }
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
75BOOST_AUTO_TEST_CASE(RemoveFibNexthops)
76{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040077 FaceTable faceTable;
78 Forwarder forwarder(faceTable);
Junxiao Shi02b73f52016-07-28 01:48:27 +000079 NameTree& nameTree = forwarder.getNameTree();
80 Fib& fib = forwarder.getFib();
81
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040082 auto face1 = make_shared<DummyFace>();
83 auto face2 = make_shared<DummyFace>();
84 faceTable.add(face1);
85 faceTable.add(face2);
Junxiao Shi02b73f52016-07-28 01:48:27 +000086
87 // {}
88 size_t nNameTreeEntriesBefore = nameTree.size();
89 BOOST_CHECK_EQUAL(fib.size(), 0);
90
91 fib::Entry* entryA = fib.insert("/A").first;
Ju Pand8315bf2019-07-31 06:59:07 +000092 fib.addOrUpdateNextHop(*entryA, *face1, 0);
93 fib.addOrUpdateNextHop(*entryA, *face2, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +000094 // {'/A':[1,2]}
95
96 fib::Entry* entryB = fib.insert("/B").first;
Ju Pand8315bf2019-07-31 06:59:07 +000097 fib.addOrUpdateNextHop(*entryB, *face1, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +000098 // {'/A':[1,2], '/B':[1]}
99
100 fib::Entry* entryC = fib.insert("/C").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000101 fib.addOrUpdateNextHop(*entryC, *face2, 1);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000102 // {'/A':[1,2], '/B':[1], '/C':[2]}
103
104 fib::Entry* entryB1 = fib.insert("/B/1").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000105 fib.addOrUpdateNextHop(*entryB1, *face1, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000106 // {'/A':[1,2], '/B':[1], '/B/1':[1], '/C':[2]}
107
108 fib::Entry* entryB12 = fib.insert("/B/1/2").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000109 fib.addOrUpdateNextHop(*entryB12, *face1, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000110 // {'/A':[1,2], '/B':[1], '/B/1':[1], '/B/1/2':[1], '/C':[2]}
111
112 // ---- close face1 ----
113 face1->close();
114 BOOST_CHECK_EQUAL(face1->getState(), face::FaceState::CLOSED);
115 // {'/A':[2], '/C':[2]}
116 BOOST_CHECK_EQUAL(fib.size(), 2);
117
118 const fib::Entry& foundA = fib.findLongestPrefixMatch("/A");
119 BOOST_CHECK_EQUAL(foundA.getPrefix(), "/A");
120 BOOST_CHECK_EQUAL(foundA.getNextHops().size(), 1);
121 BOOST_CHECK_EQUAL(&foundA.getNextHops().begin()->getFace(), face2.get());
122
123 BOOST_CHECK_EQUAL(fib.findLongestPrefixMatch("/B").getPrefix(), "/");
124
125 // ---- close face2 ----
126 face2->close();
127 BOOST_CHECK_EQUAL(face2->getState(), face::FaceState::CLOSED);
128 BOOST_CHECK_EQUAL(fib.size(), 0);
129 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
130}
131
132BOOST_AUTO_TEST_CASE(DeletePitInOutRecords)
133{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400134 FaceTable faceTable;
135 Forwarder forwarder(faceTable);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000136 Pit& pit = forwarder.getPit();
137
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400138 auto face1 = make_shared<DummyFace>();
139 auto face2 = make_shared<DummyFace>();
140 faceTable.add(face1);
141 faceTable.add(face2);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000142
143 // {}
144 BOOST_CHECK_EQUAL(pit.size(), 0);
145
146 shared_ptr<Interest> interestA = makeInterest("/A");
147 shared_ptr<pit::Entry> entryA = pit.insert(*interestA).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000148 entryA->insertOrUpdateInRecord(*face1, *interestA);
149 entryA->insertOrUpdateInRecord(*face2, *interestA);
150 entryA->insertOrUpdateOutRecord(*face1, *interestA);
151 entryA->insertOrUpdateOutRecord(*face2, *interestA);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000152 // {'/A':[1,2]}
153
154 shared_ptr<Interest> interestB = makeInterest("/B");
155 shared_ptr<pit::Entry> entryB = pit.insert(*interestB).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000156 entryB->insertOrUpdateInRecord(*face1, *interestB);
157 entryB->insertOrUpdateOutRecord(*face1, *interestB);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000158 // {'/A':[1,2], '/B':[1]}
159
160 shared_ptr<Interest> interestC = makeInterest("/C");
161 shared_ptr<pit::Entry> entryC = pit.insert(*interestC).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000162 entryC->insertOrUpdateInRecord(*face2, *interestC);
163 entryC->insertOrUpdateOutRecord(*face2, *interestC);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000164 // {'/A':[1,2], '/B':[1], '/C':[2]}
165 BOOST_CHECK_EQUAL(pit.size(), 3);
166
167 // ---- close face1 ----
168 face1->close();
169 BOOST_CHECK_EQUAL(face1->getState(), face::FaceState::CLOSED);
170 // {'/A':[2], '/B':[], '/C':[2]}
171 BOOST_CHECK_EQUAL(pit.size(), 3);
172
173 shared_ptr<pit::Entry> foundA = pit.find(*interestA);
174 BOOST_REQUIRE(foundA != nullptr);
175 BOOST_REQUIRE_EQUAL(foundA->getInRecords().size(), 1);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000176 BOOST_CHECK_EQUAL(&foundA->getInRecords().front().getFace(), face2.get());
Junxiao Shi02b73f52016-07-28 01:48:27 +0000177 BOOST_REQUIRE_EQUAL(foundA->getOutRecords().size(), 1);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000178 BOOST_CHECK_EQUAL(&foundA->getOutRecords().front().getFace(), face2.get());
Junxiao Shi02b73f52016-07-28 01:48:27 +0000179}
180
181BOOST_AUTO_TEST_SUITE_END() // FaceRemovalCleanup
182
183BOOST_AUTO_TEST_SUITE_END() // TestCleanup
184BOOST_AUTO_TEST_SUITE_END() // Table
185
186} // namespace tests
187} // namespace nfd