blob: 05b816871c45cab98ed1840ead2a94927591e622 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi042a3312017-09-15 02:51:20 +00002/*
Davide Pesavento8f0b8b62023-08-29 21:04:08 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Alexander Afanasyevc026d252014-06-16 11:14:15 -070024 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070025
26#include "table/pit.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070027
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070030
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040031#include <ndn-cxx/util/concepts.hpp>
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
Junxiao Shicbba04c2014-01-26 14:21:22 -070034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035using namespace nfd::pit;
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080036
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040037NDN_CXX_ASSERT_FORWARD_ITERATOR(Pit::const_iterator);
38
Junxiao Shi5e5e4452015-09-24 16:56:52 -070039BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040040BOOST_FIXTURE_TEST_SUITE(TestPit, GlobalIoFixture)
Junxiao Shicbba04c2014-01-26 14:21:22 -070041
Junxiao Shi28797792016-05-26 18:10:18 +000042BOOST_AUTO_TEST_CASE(Find)
43{
Junxiao Shi9d727852019-05-14 13:44:22 -060044 auto interest1 = makeInterest("/6hNwxJjw");
45 auto interest2 = makeInterest("/v65zqxm4d");
Junxiao Shi28797792016-05-26 18:10:18 +000046
47 NameTree nameTree(16);
48 Pit pit(nameTree);
49
50 pit.insert(*interest1);
51 shared_ptr<pit::Entry> found1a = pit.find(*interest1);
52 shared_ptr<pit::Entry> found1b = pit.find(*interest1);
53 BOOST_CHECK(found1a != nullptr);
54 BOOST_CHECK(found1a == found1b);
55
56 shared_ptr<pit::Entry> found2 = pit.find(*interest2);
57 BOOST_CHECK(found2 == nullptr);
58 BOOST_CHECK(nameTree.findExactMatch(interest2->getName()) == nullptr);
59}
60
Junxiao Shicbba04c2014-01-26 14:21:22 -070061BOOST_AUTO_TEST_CASE(Insert)
62{
Junxiao Shi25d97282019-05-14 13:44:46 -060063 Name name1("/5vzBNnMst");
64 Name name2("/igSGfEIM62");
Junxiao Shi57f0f312014-03-16 11:52:20 -070065
Haowei Yuan78c84d12014-02-27 15:35:13 -060066 NameTree nameTree(16);
67 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -070068 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi25d97282019-05-14 13:44:46 -060069
70 shared_ptr<Entry> entry;
71 bool isNew = false;
Junxiao Shi57f0f312014-03-16 11:52:20 -070072
Junxiao Shi30d35992014-04-03 14:51:58 -070073 // base
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040074 auto interestA = makeInterest(name1, false, std::nullopt, 2148);
Junxiao Shi25d97282019-05-14 13:44:46 -060075 std::tie(entry, isNew) = pit.insert(*interestA);
76 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060077 BOOST_CHECK_EQUAL(pit.size(), 1);
78
Junxiao Shi25d97282019-05-14 13:44:46 -060079 // same Interest, same PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060080 auto interestA2 = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060081 std::tie(entry, isNew) = pit.insert(*interestA2);
82 BOOST_CHECK_EQUAL(isNew, false);
Eric Newberryf4056d02017-05-26 17:31:53 +000083 BOOST_CHECK_EQUAL(pit.size(), 1);
84
Junxiao Shi25d97282019-05-14 13:44:46 -060085 // different CanBePrefix, different PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060086 auto interestB = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060087 interestB->setCanBePrefix(true);
88 std::tie(entry, isNew) = pit.insert(*interestB);
89 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060090 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -070091
Junxiao Shi25d97282019-05-14 13:44:46 -060092 // different MustBeFresh, different PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060093 auto interestC = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060094 interestC->setMustBeFresh(true);
95 std::tie(entry, isNew) = pit.insert(*interestC);
96 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060097 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -070098
Junxiao Shi25d97282019-05-14 13:44:46 -060099 // different InterestLifetime, same PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -0600100 auto interestD = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -0600101 interestD->setInterestLifetime(1_s);
102 std::tie(entry, isNew) = pit.insert(*interestD);
103 BOOST_CHECK_EQUAL(isNew, false);
104 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700105
Junxiao Shi25d97282019-05-14 13:44:46 -0600106 // different Nonce, same PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -0600107 auto interestE = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -0600108 interestE->setNonce(2192);
109 std::tie(entry, isNew) = pit.insert(*interestE);
110 BOOST_CHECK_EQUAL(isNew, false);
111 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700112
Junxiao Shi25d97282019-05-14 13:44:46 -0600113 // different name, different PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -0600114 auto interestF = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -0600115 interestF->setName(name2);
116 std::tie(entry, isNew) = pit.insert(*interestF);
117 BOOST_CHECK_EQUAL(isNew, true);
118 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700119}
120
Haowei Yuan78c84d12014-02-27 15:35:13 -0600121BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700122{
Junxiao Shi9d727852019-05-14 13:44:22 -0600123 auto interest = makeInterest("/z88Admz6A2");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700124
Haowei Yuan78c84d12014-02-27 15:35:13 -0600125 NameTree nameTree(16);
126 Pit pit(nameTree);
127
Junxiao Shi25d97282019-05-14 13:44:46 -0600128 shared_ptr<Entry> entry;
129 bool isNew = false;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700130
Haowei Yuan78c84d12014-02-27 15:35:13 -0600131 BOOST_CHECK_EQUAL(pit.size(), 0);
132
Junxiao Shi25d97282019-05-14 13:44:46 -0600133 std::tie(entry, isNew) = pit.insert(*interest);
134 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600135 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700136 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700137
Junxiao Shi25d97282019-05-14 13:44:46 -0600138 std::tie(entry, isNew) = pit.insert(*interest);
139 BOOST_CHECK_EQUAL(isNew, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600140 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700141 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700142
Junxiao Shi25d97282019-05-14 13:44:46 -0600143 pit.erase(entry.get());
Haowei Yuan78c84d12014-02-27 15:35:13 -0600144 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700145 BOOST_CHECK(pit.find(*interest) == nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700146
Junxiao Shi25d97282019-05-14 13:44:46 -0600147 std::tie(entry, isNew) = pit.insert(*interest);
148 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600149 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700150 BOOST_CHECK(pit.find(*interest) != nullptr);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600151}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700152
Junxiao Shiee5a4442014-07-27 17:13:43 -0700153BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
154{
155 NameTree nameTree;
156 Pit pit(nameTree);
157 size_t nNameTreeEntriesBefore = nameTree.size();
158
Junxiao Shi9d727852019-05-14 13:44:22 -0600159 auto interest = makeInterest("/37xWVvQ2K");
Junxiao Shi25d97282019-05-14 13:44:46 -0600160 auto entry = pit.insert(*interest).first;
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000161 pit.erase(entry.get());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700162 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
163}
164
spirosmastorakisff920302016-05-26 18:09:31 +0000165BOOST_AUTO_TEST_CASE(EraseWithFullName)
166{
Junxiao Shi9d727852019-05-14 13:44:22 -0600167 auto data = makeData("/test");
168 auto interest = makeInterest(data->getFullName());
spirosmastorakisff920302016-05-26 18:09:31 +0000169
170 NameTree nameTree(16);
171 Pit pit(nameTree);
172
173 BOOST_CHECK_EQUAL(pit.size(), 0);
174
175 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
176 BOOST_CHECK_EQUAL(pit.size(), 1);
177 BOOST_CHECK(pit.find(*interest) != nullptr);
178
179 BOOST_CHECK_EQUAL(pit.insert(*interest).second, false);
180 BOOST_CHECK_EQUAL(pit.size(), 1);
181 shared_ptr<pit::Entry> pitEntry = pit.find(*interest);
182 BOOST_REQUIRE(pitEntry != nullptr);
183
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000184 pit.erase(pitEntry.get());
spirosmastorakisff920302016-05-26 18:09:31 +0000185 BOOST_CHECK_EQUAL(pit.size(), 0);
186 BOOST_CHECK(pit.find(*interest) == nullptr);
187
188 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
189 BOOST_CHECK_EQUAL(pit.size(), 1);
190 BOOST_CHECK(pit.find(*interest) != nullptr);
191}
192
Junxiao Shicbba04c2014-01-26 14:21:22 -0700193BOOST_AUTO_TEST_CASE(FindAllDataMatches)
194{
Junxiao Shi25d97282019-05-14 13:44:46 -0600195 Name nameA ("/A");
196 Name nameAB ("/A/B");
197 Name nameABC ("/A/B/C");
198 Name nameABCD("/A/B/C/D");
199 Name nameD ("/D");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600200
Junxiao Shi9d727852019-05-14 13:44:22 -0600201 auto interestA = makeInterest(nameA, true);
202 auto interestABC = makeInterest(nameABC, true);
203 auto interestD = makeInterest(nameD, true);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700204
Haowei Yuan78c84d12014-02-27 15:35:13 -0600205 NameTree nameTree(16);
206 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600207 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700208
Haowei Yuan78c84d12014-02-27 15:35:13 -0600209 BOOST_CHECK_EQUAL(pit.size(), 0);
210
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700211 pit.insert(*interestA );
212 pit.insert(*interestABC);
213 pit.insert(*interestD );
Haowei Yuane1079fc2014-03-08 14:41:25 -0600214
215 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700216
Haowei Yuan78c84d12014-02-27 15:35:13 -0600217 BOOST_CHECK_EQUAL(pit.size(), 3);
218
Junxiao Shi9d727852019-05-14 13:44:22 -0600219 auto data = makeData(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700220
Junxiao Shi4846f372016-04-05 13:39:30 -0700221 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700222
Haowei Yuane1079fc2014-03-08 14:41:25 -0600223 bool hasA = false;
224 bool hasAB = false;
225 bool hasABC = false;
226 bool hasD = false;
227
Junxiao Shi25d97282019-05-14 13:44:46 -0600228 for (const auto& entry : matches) {
Junxiao Shicbba04c2014-01-26 14:21:22 -0700229 ++count;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600230
231 if (entry->getName().equals(nameA ))
232 hasA = true;
233
234 if (entry->getName().equals(nameAB))
235 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700236
Haowei Yuane1079fc2014-03-08 14:41:25 -0600237 if (entry->getName().equals(nameABC))
238 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700239
Haowei Yuane1079fc2014-03-08 14:41:25 -0600240 if (entry->getName().equals(nameD))
241 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700242 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600243 BOOST_CHECK_EQUAL(hasA , true);
244 BOOST_CHECK_EQUAL(hasAB , false);
245 BOOST_CHECK_EQUAL(hasABC, true);
246 BOOST_CHECK_EQUAL(hasD , false);
247
Junxiao Shicbba04c2014-01-26 14:21:22 -0700248 BOOST_CHECK_EQUAL(count, 2);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700249}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600250
Junxiao Shi4370fde2016-02-24 12:20:46 -0700251BOOST_AUTO_TEST_CASE(MatchFullName) // Bug 3363
252{
253 NameTree nameTree(16);
254 Pit pit(nameTree);
255
Junxiao Shi9d727852019-05-14 13:44:22 -0600256 auto data = makeData("/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700257 Name fullName = data->getFullName();
Junxiao Shi9d727852019-05-14 13:44:22 -0600258 auto interest = makeInterest(fullName);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700259
260 pit.insert(*interest);
Junxiao Shi4846f372016-04-05 13:39:30 -0700261 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700262
263 BOOST_REQUIRE_EQUAL(std::distance(matches.begin(), matches.end()), 1);
Junxiao Shi25d97282019-05-14 13:44:46 -0600264 auto found = *matches.begin();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700265 BOOST_CHECK_EQUAL(found->getName(), fullName);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700266}
267
Junxiao Shi042a3312017-09-15 02:51:20 +0000268BOOST_AUTO_TEST_CASE(InsertMatchLongName)
269{
270 NameTree nameTree(16);
271 Pit pit(nameTree);
272
273 Name n1;
274 while (n1.size() < NameTree::getMaxDepth()) {
275 n1.append("A");
276 }
277 Name n2 = n1;
278 while (n2.size() < NameTree::getMaxDepth() * 2) {
279 n2.append("B");
280 }
281 Name n3 = n1;
282 while (n3.size() < NameTree::getMaxDepth() * 2) {
283 n3.append("C");
284 }
285 auto d2 = makeData(n2);
286 auto i2 = makeInterest(n2);
287 auto d3 = makeData(n3);
288 auto i3 = makeInterest(d3->getFullName());
289
Junxiao Shi25d97282019-05-14 13:44:46 -0600290 auto entry2 = pit.insert(*i2).first;
291 auto entry3 = pit.insert(*i3).first;
Junxiao Shi042a3312017-09-15 02:51:20 +0000292
293 BOOST_CHECK_EQUAL(pit.size(), 2);
294 BOOST_CHECK_EQUAL(nameTree.size(), 1 + NameTree::getMaxDepth()); // root node + max depth
295 BOOST_CHECK(entry2->getInterest().matchesInterest(*i2));
296 BOOST_CHECK(entry3->getInterest().matchesInterest(*i3));
297
298 DataMatchResult matches2 = pit.findAllDataMatches(*d2);
299 BOOST_REQUIRE_EQUAL(std::distance(matches2.begin(), matches2.end()), 1);
300 BOOST_CHECK(*matches2.begin() == entry2);
301 DataMatchResult matches3 = pit.findAllDataMatches(*d3);
302 BOOST_REQUIRE_EQUAL(std::distance(matches3.begin(), matches3.end()), 1);
303 BOOST_CHECK(*matches3.begin() == entry3);
304}
305
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800306BOOST_AUTO_TEST_CASE(Iterator)
307{
308 NameTree nameTree(16);
309 Pit pit(nameTree);
310
Junxiao Shi25d97282019-05-14 13:44:46 -0600311 auto interestA = makeInterest("/A");
Junxiao Shi9d727852019-05-14 13:44:22 -0600312 auto interestABC1 = makeInterest("/A/B/C");
313 auto interestABC2 = makeInterest("/A/B/C");
Junxiao Shi25d97282019-05-14 13:44:46 -0600314 interestABC2->setMustBeFresh(true);
315 auto interestD = makeInterest("/D");
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800316
317 BOOST_CHECK_EQUAL(pit.size(), 0);
318 BOOST_CHECK(pit.begin() == pit.end());
319
320 pit.insert(*interestABC1);
321 BOOST_CHECK_EQUAL(pit.size(), 1);
322 BOOST_CHECK(pit.begin() != pit.end());
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400323 BOOST_CHECK(&pit.begin()->getInterest() == interestABC1.get());
324 BOOST_CHECK(&(*pit.begin()).getInterest() == interestABC1.get());
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800325
326 auto i = pit.begin();
327 auto j = pit.begin();
328 BOOST_CHECK(++i == pit.end());
329 BOOST_CHECK(j++ == pit.begin());
330 BOOST_CHECK(j == pit.end());
331
332 pit.insert(*interestA);
333 pit.insert(*interestABC2);
334 pit.insert(*interestD);
335
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800336 std::set<const Interest*> actual;
337 for (const auto& pitEntry : pit) {
338 actual.insert(&pitEntry.getInterest());
339 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400340 const auto expected = std::set{interestA.get(), interestABC1.get(),
341 interestABC2.get(), interestD.get()};
342 BOOST_TEST(actual == expected, boost::test_tools::per_element());
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800343}
344
Davide Pesavento14e71f02019-03-28 17:35:25 -0400345BOOST_AUTO_TEST_SUITE_END() // TestPit
346BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shicbba04c2014-01-26 14:21:22 -0700347
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400348} // namespace nfd::tests