blob: 25405a6bd93e149a102b5663e3cb0ea94bc6363b [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;
ashiqopu3ad49db2018-10-20 22:38:47 +000052 fibEntry->addOrUpdateNextHop(*face1, 0, 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) {
ashiqopud3ae85d2019-02-17 02:29:55 +000057 pitEntry->insertOrUpdateInRecord(*face1, 0, *interest);
Junxiao Shi02b73f52016-07-28 01:48:27 +000058 }
59 if ((i & 0x02) != 0) {
ashiqopud3ae85d2019-02-17 02:29:55 +000060 pitEntry->insertOrUpdateOutRecord(*face1, 0, *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{
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;
ashiqopu3ad49db2018-10-20 22:38:47 +000091 entryA->addOrUpdateNextHop(*face1, 0, 0);
92 entryA->addOrUpdateNextHop(*face2, 0, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +000093 // {'/A':[1,2]}
94
95 fib::Entry* entryB = fib.insert("/B").first;
ashiqopu3ad49db2018-10-20 22:38:47 +000096 entryB->addOrUpdateNextHop(*face1, 0, 0);
Junxiao Shi02b73f52016-07-28 01:48:27 +000097 // {'/A':[1,2], '/B':[1]}
98
99 fib::Entry* entryC = fib.insert("/C").first;
ashiqopu3ad49db2018-10-20 22:38:47 +0000100 entryC->addOrUpdateNextHop(*face2, 0, 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;
ashiqopu3ad49db2018-10-20 22:38:47 +0000104 entryB1->addOrUpdateNextHop(*face1, 0, 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;
ashiqopu3ad49db2018-10-20 22:38:47 +0000108 entryB12->addOrUpdateNextHop(*face1, 0, 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{
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;
ashiqopud3ae85d2019-02-17 02:29:55 +0000146 entryA->insertOrUpdateInRecord(*face1, 0, *interestA);
147 entryA->insertOrUpdateInRecord(*face2, 0, *interestA);
148 entryA->insertOrUpdateOutRecord(*face1, 0, *interestA);
149 entryA->insertOrUpdateOutRecord(*face2, 0, *interestA);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000150 // {'/A':[1,2]}
151
152 shared_ptr<Interest> interestB = makeInterest("/B");
153 shared_ptr<pit::Entry> entryB = pit.insert(*interestB).first;
ashiqopud3ae85d2019-02-17 02:29:55 +0000154 entryB->insertOrUpdateInRecord(*face1, 0, *interestB);
155 entryB->insertOrUpdateOutRecord(*face1, 0, *interestB);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000156 // {'/A':[1,2], '/B':[1]}
157
158 shared_ptr<Interest> interestC = makeInterest("/C");
159 shared_ptr<pit::Entry> entryC = pit.insert(*interestC).first;
ashiqopud3ae85d2019-02-17 02:29:55 +0000160 entryC->insertOrUpdateInRecord(*face2, 0, *interestC);
161 entryC->insertOrUpdateOutRecord(*face2, 0, *interestC);
Junxiao Shi02b73f52016-07-28 01:48:27 +0000162 // {'/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 Shi9cff7792016-08-01 21:45:11 +0000174 BOOST_CHECK_EQUAL(&foundA->getInRecords().front().getFace(), face2.get());
Junxiao Shi02b73f52016-07-28 01:48:27 +0000175 BOOST_REQUIRE_EQUAL(foundA->getOutRecords().size(), 1);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000176 BOOST_CHECK_EQUAL(&foundA->getOutRecords().front().getFace(), face2.get());
Junxiao Shi02b73f52016-07-28 01:48:27 +0000177}
178
179BOOST_AUTO_TEST_SUITE_END() // FaceRemovalCleanup
180
181BOOST_AUTO_TEST_SUITE_END() // TestCleanup
182BOOST_AUTO_TEST_SUITE_END() // Table
183
184} // namespace tests
185} // namespace nfd