blob: 99d23e71d21feba3511fb3b0f20dcef44064c03a [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 Pesaventocf7db2f2019-03-24 23:17:28 -04003 * Copyright (c) 2014-2019, 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"
30#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080033namespace pit {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034namespace tests {
Junxiao Shicbba04c2014-01-26 14:21:22 -070035
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080036using namespace nfd::tests;
37
Junxiao Shi5e5e4452015-09-24 16:56:52 -070038BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040039BOOST_FIXTURE_TEST_SUITE(TestPit, GlobalIoFixture)
Junxiao Shicbba04c2014-01-26 14:21:22 -070040
Junxiao Shi28797792016-05-26 18:10:18 +000041BOOST_AUTO_TEST_CASE(Find)
42{
Junxiao Shi9d727852019-05-14 13:44:22 -060043 auto interest1 = makeInterest("/6hNwxJjw");
44 auto interest2 = makeInterest("/v65zqxm4d");
Junxiao Shi28797792016-05-26 18:10:18 +000045
46 NameTree nameTree(16);
47 Pit pit(nameTree);
48
49 pit.insert(*interest1);
50 shared_ptr<pit::Entry> found1a = pit.find(*interest1);
51 shared_ptr<pit::Entry> found1b = pit.find(*interest1);
52 BOOST_CHECK(found1a != nullptr);
53 BOOST_CHECK(found1a == found1b);
54
55 shared_ptr<pit::Entry> found2 = pit.find(*interest2);
56 BOOST_CHECK(found2 == nullptr);
57 BOOST_CHECK(nameTree.findExactMatch(interest2->getName()) == nullptr);
58}
59
Junxiao Shicbba04c2014-01-26 14:21:22 -070060BOOST_AUTO_TEST_CASE(Insert)
61{
Junxiao Shi25d97282019-05-14 13:44:46 -060062 Name name1("/5vzBNnMst");
63 Name name2("/igSGfEIM62");
Junxiao Shi57f0f312014-03-16 11:52:20 -070064
Haowei Yuan78c84d12014-02-27 15:35:13 -060065 NameTree nameTree(16);
66 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -070067 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi25d97282019-05-14 13:44:46 -060068
69 shared_ptr<Entry> entry;
70 bool isNew = false;
Junxiao Shi57f0f312014-03-16 11:52:20 -070071
Junxiao Shi30d35992014-04-03 14:51:58 -070072 // base
Junxiao Shi25d97282019-05-14 13:44:46 -060073 auto interestA = makeInterest(name1, false, nullopt, 2148);
74 std::tie(entry, isNew) = pit.insert(*interestA);
75 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060076 BOOST_CHECK_EQUAL(pit.size(), 1);
77
Junxiao Shi25d97282019-05-14 13:44:46 -060078 // same Interest, same PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060079 auto interestA2 = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060080 std::tie(entry, isNew) = pit.insert(*interestA2);
81 BOOST_CHECK_EQUAL(isNew, false);
Eric Newberryf4056d02017-05-26 17:31:53 +000082 BOOST_CHECK_EQUAL(pit.size(), 1);
83
Junxiao Shi25d97282019-05-14 13:44:46 -060084 // different CanBePrefix, different PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060085 auto interestB = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060086 interestB->setCanBePrefix(true);
87 std::tie(entry, isNew) = pit.insert(*interestB);
88 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060089 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -070090
Junxiao Shi25d97282019-05-14 13:44:46 -060091 // different MustBeFresh, different PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060092 auto interestC = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -060093 interestC->setMustBeFresh(true);
94 std::tie(entry, isNew) = pit.insert(*interestC);
95 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060096 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -070097
Junxiao Shi25d97282019-05-14 13:44:46 -060098 // different InterestLifetime, same PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -060099 auto interestD = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -0600100 interestD->setInterestLifetime(1_s);
101 std::tie(entry, isNew) = pit.insert(*interestD);
102 BOOST_CHECK_EQUAL(isNew, false);
103 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700104
Junxiao Shi25d97282019-05-14 13:44:46 -0600105 // different Nonce, same PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -0600106 auto interestE = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -0600107 interestE->setNonce(2192);
108 std::tie(entry, isNew) = pit.insert(*interestE);
109 BOOST_CHECK_EQUAL(isNew, false);
110 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700111
Junxiao Shi25d97282019-05-14 13:44:46 -0600112 // different name, different PIT entry
Junxiao Shi9d727852019-05-14 13:44:22 -0600113 auto interestF = make_shared<Interest>(*interestA);
Junxiao Shi25d97282019-05-14 13:44:46 -0600114 interestF->setName(name2);
115 std::tie(entry, isNew) = pit.insert(*interestF);
116 BOOST_CHECK_EQUAL(isNew, true);
117 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700118}
119
Haowei Yuan78c84d12014-02-27 15:35:13 -0600120BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700121{
Junxiao Shi9d727852019-05-14 13:44:22 -0600122 auto interest = makeInterest("/z88Admz6A2");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700123
Haowei Yuan78c84d12014-02-27 15:35:13 -0600124 NameTree nameTree(16);
125 Pit pit(nameTree);
126
Junxiao Shi25d97282019-05-14 13:44:46 -0600127 shared_ptr<Entry> entry;
128 bool isNew = false;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700129
Haowei Yuan78c84d12014-02-27 15:35:13 -0600130 BOOST_CHECK_EQUAL(pit.size(), 0);
131
Junxiao Shi25d97282019-05-14 13:44:46 -0600132 std::tie(entry, isNew) = pit.insert(*interest);
133 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600134 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700135 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700136
Junxiao Shi25d97282019-05-14 13:44:46 -0600137 std::tie(entry, isNew) = pit.insert(*interest);
138 BOOST_CHECK_EQUAL(isNew, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600139 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700140 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700141
Junxiao Shi25d97282019-05-14 13:44:46 -0600142 pit.erase(entry.get());
Haowei Yuan78c84d12014-02-27 15:35:13 -0600143 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700144 BOOST_CHECK(pit.find(*interest) == nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700145
Junxiao Shi25d97282019-05-14 13:44:46 -0600146 std::tie(entry, isNew) = pit.insert(*interest);
147 BOOST_CHECK_EQUAL(isNew, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600148 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700149 BOOST_CHECK(pit.find(*interest) != nullptr);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600150}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700151
Junxiao Shiee5a4442014-07-27 17:13:43 -0700152BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
153{
154 NameTree nameTree;
155 Pit pit(nameTree);
156 size_t nNameTreeEntriesBefore = nameTree.size();
157
Junxiao Shi9d727852019-05-14 13:44:22 -0600158 auto interest = makeInterest("/37xWVvQ2K");
Junxiao Shi25d97282019-05-14 13:44:46 -0600159 auto entry = pit.insert(*interest).first;
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000160 pit.erase(entry.get());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700161 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
162}
163
spirosmastorakisff920302016-05-26 18:09:31 +0000164BOOST_AUTO_TEST_CASE(EraseWithFullName)
165{
Junxiao Shi9d727852019-05-14 13:44:22 -0600166 auto data = makeData("/test");
167 auto interest = makeInterest(data->getFullName());
spirosmastorakisff920302016-05-26 18:09:31 +0000168
169 NameTree nameTree(16);
170 Pit pit(nameTree);
171
172 BOOST_CHECK_EQUAL(pit.size(), 0);
173
174 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
175 BOOST_CHECK_EQUAL(pit.size(), 1);
176 BOOST_CHECK(pit.find(*interest) != nullptr);
177
178 BOOST_CHECK_EQUAL(pit.insert(*interest).second, false);
179 BOOST_CHECK_EQUAL(pit.size(), 1);
180 shared_ptr<pit::Entry> pitEntry = pit.find(*interest);
181 BOOST_REQUIRE(pitEntry != nullptr);
182
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000183 pit.erase(pitEntry.get());
spirosmastorakisff920302016-05-26 18:09:31 +0000184 BOOST_CHECK_EQUAL(pit.size(), 0);
185 BOOST_CHECK(pit.find(*interest) == nullptr);
186
187 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
188 BOOST_CHECK_EQUAL(pit.size(), 1);
189 BOOST_CHECK(pit.find(*interest) != nullptr);
190}
191
Junxiao Shicbba04c2014-01-26 14:21:22 -0700192BOOST_AUTO_TEST_CASE(FindAllDataMatches)
193{
Junxiao Shi25d97282019-05-14 13:44:46 -0600194 Name nameA ("/A");
195 Name nameAB ("/A/B");
196 Name nameABC ("/A/B/C");
197 Name nameABCD("/A/B/C/D");
198 Name nameD ("/D");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600199
Junxiao Shi9d727852019-05-14 13:44:22 -0600200 auto interestA = makeInterest(nameA, true);
201 auto interestABC = makeInterest(nameABC, true);
202 auto interestD = makeInterest(nameD, true);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700203
Haowei Yuan78c84d12014-02-27 15:35:13 -0600204 NameTree nameTree(16);
205 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600206 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700207
Haowei Yuan78c84d12014-02-27 15:35:13 -0600208 BOOST_CHECK_EQUAL(pit.size(), 0);
209
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700210 pit.insert(*interestA );
211 pit.insert(*interestABC);
212 pit.insert(*interestD );
Haowei Yuane1079fc2014-03-08 14:41:25 -0600213
214 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700215
Haowei Yuan78c84d12014-02-27 15:35:13 -0600216 BOOST_CHECK_EQUAL(pit.size(), 3);
217
Junxiao Shi9d727852019-05-14 13:44:22 -0600218 auto data = makeData(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700219
Junxiao Shi4846f372016-04-05 13:39:30 -0700220 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700221
Haowei Yuane1079fc2014-03-08 14:41:25 -0600222 bool hasA = false;
223 bool hasAB = false;
224 bool hasABC = false;
225 bool hasD = false;
226
Junxiao Shi25d97282019-05-14 13:44:46 -0600227 for (const auto& entry : matches) {
Junxiao Shicbba04c2014-01-26 14:21:22 -0700228 ++count;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600229
230 if (entry->getName().equals(nameA ))
231 hasA = true;
232
233 if (entry->getName().equals(nameAB))
234 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700235
Haowei Yuane1079fc2014-03-08 14:41:25 -0600236 if (entry->getName().equals(nameABC))
237 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700238
Haowei Yuane1079fc2014-03-08 14:41:25 -0600239 if (entry->getName().equals(nameD))
240 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700241 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600242 BOOST_CHECK_EQUAL(hasA , true);
243 BOOST_CHECK_EQUAL(hasAB , false);
244 BOOST_CHECK_EQUAL(hasABC, true);
245 BOOST_CHECK_EQUAL(hasD , false);
246
Junxiao Shicbba04c2014-01-26 14:21:22 -0700247 BOOST_CHECK_EQUAL(count, 2);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700248}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600249
Junxiao Shi4370fde2016-02-24 12:20:46 -0700250BOOST_AUTO_TEST_CASE(MatchFullName) // Bug 3363
251{
252 NameTree nameTree(16);
253 Pit pit(nameTree);
254
Junxiao Shi9d727852019-05-14 13:44:22 -0600255 auto data = makeData("/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700256 Name fullName = data->getFullName();
Junxiao Shi9d727852019-05-14 13:44:22 -0600257 auto interest = makeInterest(fullName);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700258
259 pit.insert(*interest);
Junxiao Shi4846f372016-04-05 13:39:30 -0700260 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700261
262 BOOST_REQUIRE_EQUAL(std::distance(matches.begin(), matches.end()), 1);
Junxiao Shi25d97282019-05-14 13:44:46 -0600263 auto found = *matches.begin();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700264 BOOST_CHECK_EQUAL(found->getName(), fullName);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700265}
266
Junxiao Shi042a3312017-09-15 02:51:20 +0000267BOOST_AUTO_TEST_CASE(InsertMatchLongName)
268{
269 NameTree nameTree(16);
270 Pit pit(nameTree);
271
272 Name n1;
273 while (n1.size() < NameTree::getMaxDepth()) {
274 n1.append("A");
275 }
276 Name n2 = n1;
277 while (n2.size() < NameTree::getMaxDepth() * 2) {
278 n2.append("B");
279 }
280 Name n3 = n1;
281 while (n3.size() < NameTree::getMaxDepth() * 2) {
282 n3.append("C");
283 }
284 auto d2 = makeData(n2);
285 auto i2 = makeInterest(n2);
286 auto d3 = makeData(n3);
287 auto i3 = makeInterest(d3->getFullName());
288
Junxiao Shi25d97282019-05-14 13:44:46 -0600289 auto entry2 = pit.insert(*i2).first;
290 auto entry3 = pit.insert(*i3).first;
Junxiao Shi042a3312017-09-15 02:51:20 +0000291
292 BOOST_CHECK_EQUAL(pit.size(), 2);
293 BOOST_CHECK_EQUAL(nameTree.size(), 1 + NameTree::getMaxDepth()); // root node + max depth
294 BOOST_CHECK(entry2->getInterest().matchesInterest(*i2));
295 BOOST_CHECK(entry3->getInterest().matchesInterest(*i3));
296
297 DataMatchResult matches2 = pit.findAllDataMatches(*d2);
298 BOOST_REQUIRE_EQUAL(std::distance(matches2.begin(), matches2.end()), 1);
299 BOOST_CHECK(*matches2.begin() == entry2);
300 DataMatchResult matches3 = pit.findAllDataMatches(*d3);
301 BOOST_REQUIRE_EQUAL(std::distance(matches3.begin(), matches3.end()), 1);
302 BOOST_CHECK(*matches3.begin() == entry3);
303}
304
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800305BOOST_AUTO_TEST_CASE(Iterator)
306{
307 NameTree nameTree(16);
308 Pit pit(nameTree);
309
Junxiao Shi25d97282019-05-14 13:44:46 -0600310 auto interestA = makeInterest("/A");
Junxiao Shi9d727852019-05-14 13:44:22 -0600311 auto interestABC1 = makeInterest("/A/B/C");
312 auto interestABC2 = makeInterest("/A/B/C");
Junxiao Shi25d97282019-05-14 13:44:46 -0600313 interestABC2->setMustBeFresh(true);
314 auto interestD = makeInterest("/D");
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800315
316 BOOST_CHECK_EQUAL(pit.size(), 0);
317 BOOST_CHECK(pit.begin() == pit.end());
318
319 pit.insert(*interestABC1);
320 BOOST_CHECK_EQUAL(pit.size(), 1);
321 BOOST_CHECK(pit.begin() != pit.end());
322 BOOST_CHECK(pit.begin()->getInterest() == *interestABC1);
323 BOOST_CHECK((*pit.begin()).getInterest() == *interestABC1);
324
325 auto i = pit.begin();
326 auto j = pit.begin();
327 BOOST_CHECK(++i == pit.end());
328 BOOST_CHECK(j++ == pit.begin());
329 BOOST_CHECK(j == pit.end());
330
331 pit.insert(*interestA);
332 pit.insert(*interestABC2);
333 pit.insert(*interestD);
334
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800335 std::set<const Interest*> actual;
336 for (const auto& pitEntry : pit) {
337 actual.insert(&pitEntry.getInterest());
338 }
Junxiao Shi25d97282019-05-14 13:44:46 -0600339 std::set<const Interest*> expected = {interestA.get(), interestABC1.get(),
340 interestABC2.get(), interestD.get()};
341 BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800342}
343
Davide Pesavento14e71f02019-03-28 17:35:25 -0400344BOOST_AUTO_TEST_SUITE_END() // TestPit
345BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shicbba04c2014-01-26 14:21:22 -0700346
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700347} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800348} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800349} // namespace nfd