blob: b52ac0ea7a36f6676f7636005cc262e7e7ff0531 [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 Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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 Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tests {
Junxiao Shicbba04c2014-01-26 14:21:22 -070032
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033using namespace nfd::pit;
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080034
Junxiao Shi5e5e4452015-09-24 16:56:52 -070035BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040036BOOST_FIXTURE_TEST_SUITE(TestPit, GlobalIoFixture)
Junxiao Shicbba04c2014-01-26 14:21:22 -070037
Junxiao Shi28797792016-05-26 18:10:18 +000038BOOST_AUTO_TEST_CASE(Find)
39{
Junxiao Shi9d727852019-05-14 13:44:22 -060040 auto interest1 = makeInterest("/6hNwxJjw");
41 auto interest2 = makeInterest("/v65zqxm4d");
Junxiao Shi28797792016-05-26 18:10:18 +000042
43 NameTree nameTree(16);
44 Pit pit(nameTree);
45
46 pit.insert(*interest1);
47 shared_ptr<pit::Entry> found1a = pit.find(*interest1);
48 shared_ptr<pit::Entry> found1b = pit.find(*interest1);
49 BOOST_CHECK(found1a != nullptr);
50 BOOST_CHECK(found1a == found1b);
51
52 shared_ptr<pit::Entry> found2 = pit.find(*interest2);
53 BOOST_CHECK(found2 == nullptr);
54 BOOST_CHECK(nameTree.findExactMatch(interest2->getName()) == nullptr);
55}
56
Junxiao Shicbba04c2014-01-26 14:21:22 -070057BOOST_AUTO_TEST_CASE(Insert)
58{
Junxiao Shi25d97282019-05-14 13:44:46 -060059 Name name1("/5vzBNnMst");
60 Name name2("/igSGfEIM62");
Junxiao Shi57f0f312014-03-16 11:52:20 -070061
Haowei Yuan78c84d12014-02-27 15:35:13 -060062 NameTree nameTree(16);
63 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -070064 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi25d97282019-05-14 13:44:46 -060065
66 shared_ptr<Entry> entry;
67 bool isNew = false;
Junxiao Shi57f0f312014-03-16 11:52:20 -070068
Junxiao Shi30d35992014-04-03 14:51:58 -070069 // base
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040070 auto interestA = makeInterest(name1, false, std::nullopt, 2148);
Junxiao Shi25d97282019-05-14 13:44:46 -060071 std::tie(entry, isNew) = pit.insert(*interestA);
72 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060073 BOOST_CHECK_EQUAL(pit.size(), 1);
74
Junxiao Shi25d97282019-05-14 13:44:46 -060075 // same Interest, same PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060076 auto interestA2 = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060077 std::tie(entry, isNew) = pit.insert(*interestA2);
78 BOOST_CHECK_EQUAL(isNew, false);
Eric Newberryf4056d02017-05-26 17:31:53 +000079 BOOST_CHECK_EQUAL(pit.size(), 1);
80
Junxiao Shi25d97282019-05-14 13:44:46 -060081 // different CanBePrefix, different PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060082 auto interestB = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060083 interestB->setCanBePrefix(true);
84 std::tie(entry, isNew) = pit.insert(*interestB);
85 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060086 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -070087
Junxiao Shi25d97282019-05-14 13:44:46 -060088 // different MustBeFresh, different PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060089 auto interestC = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060090 interestC->setMustBeFresh(true);
91 std::tie(entry, isNew) = pit.insert(*interestC);
92 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060093 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -070094
Junxiao Shi25d97282019-05-14 13:44:46 -060095 // different InterestLifetime, same PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060096 auto interestD = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060097 interestD->setInterestLifetime(1_s);
98 std::tie(entry, isNew) = pit.insert(*interestD);
99 BOOST_CHECK_EQUAL(isNew, false);
100 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700101
Junxiao Shi25d97282019-05-14 13:44:46 -0600102 // different Nonce, same PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -0600103 auto interestE = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -0600104 interestE->setNonce(2192);
105 std::tie(entry, isNew) = pit.insert(*interestE);
106 BOOST_CHECK_EQUAL(isNew, false);
107 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700108
Junxiao Shi25d97282019-05-14 13:44:46 -0600109 // different name, different PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -0600110 auto interestF = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -0600111 interestF->setName(name2);
112 std::tie(entry, isNew) = pit.insert(*interestF);
113 BOOST_CHECK_EQUAL(isNew, true);
114 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700115}
116
Haowei Yuan78c84d12014-02-27 15:35:13 -0600117BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700118{
Junxiao Shi9d727852019-05-14 13:44:22 -0600119 auto interest = makeInterest("/z88Admz6A2");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700120
Haowei Yuan78c84d12014-02-27 15:35:13 -0600121 NameTree nameTree(16);
122 Pit pit(nameTree);
123
Junxiao Shi25d97282019-05-14 13:44:46 -0600124 shared_ptr<Entry> entry;
125 bool isNew = false;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700126
Haowei Yuan78c84d12014-02-27 15:35:13 -0600127 BOOST_CHECK_EQUAL(pit.size(), 0);
128
Junxiao Shi25d97282019-05-14 13:44:46 -0600129 std::tie(entry, isNew) = pit.insert(*interest);
130 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600131 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700132 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700133
Junxiao Shi25d97282019-05-14 13:44:46 -0600134 std::tie(entry, isNew) = pit.insert(*interest);
135 BOOST_CHECK_EQUAL(isNew, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600136 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700137 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700138
Junxiao Shi25d97282019-05-14 13:44:46 -0600139 pit.erase(entry.get());
Haowei Yuan78c84d12014-02-27 15:35:13 -0600140 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700141 BOOST_CHECK(pit.find(*interest) == nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700142
Junxiao Shi25d97282019-05-14 13:44:46 -0600143 std::tie(entry, isNew) = pit.insert(*interest);
144 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600145 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700146 BOOST_CHECK(pit.find(*interest) != nullptr);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600147}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700148
Junxiao Shiee5a4442014-07-27 17:13:43 -0700149BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
150{
151 NameTree nameTree;
152 Pit pit(nameTree);
153 size_t nNameTreeEntriesBefore = nameTree.size();
154
Junxiao Shi9d727852019-05-14 13:44:22 -0600155 auto interest = makeInterest("/37xWVvQ2K");
Junxiao Shi25d97282019-05-14 13:44:46 -0600156 auto entry = pit.insert(*interest).first;
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000157 pit.erase(entry.get());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700158 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
159}
160
spirosmastorakisff920302016-05-26 18:09:31 +0000161BOOST_AUTO_TEST_CASE(EraseWithFullName)
162{
Junxiao Shi9d727852019-05-14 13:44:22 -0600163 auto data = makeData("/test");
164 auto interest = makeInterest(data->getFullName());
spirosmastorakisff920302016-05-26 18:09:31 +0000165
166 NameTree nameTree(16);
167 Pit pit(nameTree);
168
169 BOOST_CHECK_EQUAL(pit.size(), 0);
170
171 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
172 BOOST_CHECK_EQUAL(pit.size(), 1);
173 BOOST_CHECK(pit.find(*interest) != nullptr);
174
175 BOOST_CHECK_EQUAL(pit.insert(*interest).second, false);
176 BOOST_CHECK_EQUAL(pit.size(), 1);
177 shared_ptr<pit::Entry> pitEntry = pit.find(*interest);
178 BOOST_REQUIRE(pitEntry != nullptr);
179
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000180 pit.erase(pitEntry.get());
spirosmastorakisff920302016-05-26 18:09:31 +0000181 BOOST_CHECK_EQUAL(pit.size(), 0);
182 BOOST_CHECK(pit.find(*interest) == nullptr);
183
184 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
185 BOOST_CHECK_EQUAL(pit.size(), 1);
186 BOOST_CHECK(pit.find(*interest) != nullptr);
187}
188
Junxiao Shicbba04c2014-01-26 14:21:22 -0700189BOOST_AUTO_TEST_CASE(FindAllDataMatches)
190{
Junxiao Shi25d97282019-05-14 13:44:46 -0600191 Name nameA ("/A");
192 Name nameAB ("/A/B");
193 Name nameABC ("/A/B/C");
194 Name nameABCD("/A/B/C/D");
195 Name nameD ("/D");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600196
Junxiao Shi9d727852019-05-14 13:44:22 -0600197 auto interestA = makeInterest(nameA, true);
198 auto interestABC = makeInterest(nameABC, true);
199 auto interestD = makeInterest(nameD, true);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700200
Haowei Yuan78c84d12014-02-27 15:35:13 -0600201 NameTree nameTree(16);
202 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600203 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700204
Haowei Yuan78c84d12014-02-27 15:35:13 -0600205 BOOST_CHECK_EQUAL(pit.size(), 0);
206
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700207 pit.insert(*interestA );
208 pit.insert(*interestABC);
209 pit.insert(*interestD );
Haowei Yuane1079fc2014-03-08 14:41:25 -0600210
211 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700212
Haowei Yuan78c84d12014-02-27 15:35:13 -0600213 BOOST_CHECK_EQUAL(pit.size(), 3);
214
Junxiao Shi9d727852019-05-14 13:44:22 -0600215 auto data = makeData(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700216
Junxiao Shi4846f372016-04-05 13:39:30 -0700217 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700218
Haowei Yuane1079fc2014-03-08 14:41:25 -0600219 bool hasA = false;
220 bool hasAB = false;
221 bool hasABC = false;
222 bool hasD = false;
223
Junxiao Shi25d97282019-05-14 13:44:46 -0600224 for (const auto& entry : matches) {
Junxiao Shicbba04c2014-01-26 14:21:22 -0700225 ++count;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600226
227 if (entry->getName().equals(nameA ))
228 hasA = true;
229
230 if (entry->getName().equals(nameAB))
231 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700232
Haowei Yuane1079fc2014-03-08 14:41:25 -0600233 if (entry->getName().equals(nameABC))
234 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700235
Haowei Yuane1079fc2014-03-08 14:41:25 -0600236 if (entry->getName().equals(nameD))
237 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700238 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600239 BOOST_CHECK_EQUAL(hasA , true);
240 BOOST_CHECK_EQUAL(hasAB , false);
241 BOOST_CHECK_EQUAL(hasABC, true);
242 BOOST_CHECK_EQUAL(hasD , false);
243
Junxiao Shicbba04c2014-01-26 14:21:22 -0700244 BOOST_CHECK_EQUAL(count, 2);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700245}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600246
Junxiao Shi4370fde2016-02-24 12:20:46 -0700247BOOST_AUTO_TEST_CASE(MatchFullName) // Bug 3363
248{
249 NameTree nameTree(16);
250 Pit pit(nameTree);
251
Junxiao Shi9d727852019-05-14 13:44:22 -0600252 auto data = makeData("/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700253 Name fullName = data->getFullName();
Junxiao Shi9d727852019-05-14 13:44:22 -0600254 auto interest = makeInterest(fullName);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700255
256 pit.insert(*interest);
Junxiao Shi4846f372016-04-05 13:39:30 -0700257 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700258
259 BOOST_REQUIRE_EQUAL(std::distance(matches.begin(), matches.end()), 1);
Junxiao Shi25d97282019-05-14 13:44:46 -0600260 auto found = *matches.begin();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700261 BOOST_CHECK_EQUAL(found->getName(), fullName);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700262}
263
Junxiao Shi042a3312017-09-15 02:51:20 +0000264BOOST_AUTO_TEST_CASE(InsertMatchLongName)
265{
266 NameTree nameTree(16);
267 Pit pit(nameTree);
268
269 Name n1;
270 while (n1.size() < NameTree::getMaxDepth()) {
271 n1.append("A");
272 }
273 Name n2 = n1;
274 while (n2.size() < NameTree::getMaxDepth() * 2) {
275 n2.append("B");
276 }
277 Name n3 = n1;
278 while (n3.size() < NameTree::getMaxDepth() * 2) {
279 n3.append("C");
280 }
281 auto d2 = makeData(n2);
282 auto i2 = makeInterest(n2);
283 auto d3 = makeData(n3);
284 auto i3 = makeInterest(d3->getFullName());
285
Junxiao Shi25d97282019-05-14 13:44:46 -0600286 auto entry2 = pit.insert(*i2).first;
287 auto entry3 = pit.insert(*i3).first;
Junxiao Shi042a3312017-09-15 02:51:20 +0000288
289 BOOST_CHECK_EQUAL(pit.size(), 2);
290 BOOST_CHECK_EQUAL(nameTree.size(), 1 + NameTree::getMaxDepth()); // root node + max depth
291 BOOST_CHECK(entry2->getInterest().matchesInterest(*i2));
292 BOOST_CHECK(entry3->getInterest().matchesInterest(*i3));
293
294 DataMatchResult matches2 = pit.findAllDataMatches(*d2);
295 BOOST_REQUIRE_EQUAL(std::distance(matches2.begin(), matches2.end()), 1);
296 BOOST_CHECK(*matches2.begin() == entry2);
297 DataMatchResult matches3 = pit.findAllDataMatches(*d3);
298 BOOST_REQUIRE_EQUAL(std::distance(matches3.begin(), matches3.end()), 1);
299 BOOST_CHECK(*matches3.begin() == entry3);
300}
301
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800302BOOST_AUTO_TEST_CASE(Iterator)
303{
304 NameTree nameTree(16);
305 Pit pit(nameTree);
306
Junxiao Shi25d97282019-05-14 13:44:46 -0600307 auto interestA = makeInterest("/A");
Junxiao Shi9d727852019-05-14 13:44:22 -0600308 auto interestABC1 = makeInterest("/A/B/C");
309 auto interestABC2 = makeInterest("/A/B/C");
Junxiao Shi25d97282019-05-14 13:44:46 -0600310 interestABC2->setMustBeFresh(true);
311 auto interestD = makeInterest("/D");
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800312
313 BOOST_CHECK_EQUAL(pit.size(), 0);
314 BOOST_CHECK(pit.begin() == pit.end());
315
316 pit.insert(*interestABC1);
317 BOOST_CHECK_EQUAL(pit.size(), 1);
318 BOOST_CHECK(pit.begin() != pit.end());
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400319 BOOST_CHECK(&pit.begin()->getInterest() == interestABC1.get());
320 BOOST_CHECK(&(*pit.begin()).getInterest() == interestABC1.get());
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800321
322 auto i = pit.begin();
323 auto j = pit.begin();
324 BOOST_CHECK(++i == pit.end());
325 BOOST_CHECK(j++ == pit.begin());
326 BOOST_CHECK(j == pit.end());
327
328 pit.insert(*interestA);
329 pit.insert(*interestABC2);
330 pit.insert(*interestD);
331
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800332 std::set<const Interest*> actual;
333 for (const auto& pitEntry : pit) {
334 actual.insert(&pitEntry.getInterest());
335 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400336 const auto expected = std::set{interestA.get(), interestABC1.get(),
337 interestABC2.get(), interestD.get()};
338 BOOST_TEST(actual == expected, boost::test_tools::per_element());
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800339}
340
Davide Pesavento14e71f02019-03-28 17:35:25 -0400341BOOST_AUTO_TEST_SUITE_END() // TestPit
342BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shicbba04c2014-01-26 14:21:22 -0700343
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400344} // namespace nfd::tests