blob: 8a784f644444ea335830b3c6972863f9344e7228 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi4370fde2016-02-24 12:20:46 -07003 * Copyright (c) 2014-2016, 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"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070027#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070028
Junxiao Shid9ee45c2014-02-27 15:38:11 -070029#include "tests/test-common.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070030
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080031namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080032namespace pit {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Junxiao Shicbba04c2014-01-26 14:21:22 -070034
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080035using namespace nfd::tests;
36
Junxiao Shi5e5e4452015-09-24 16:56:52 -070037BOOST_AUTO_TEST_SUITE(Table)
38BOOST_FIXTURE_TEST_SUITE(TestPit, BaseFixture)
Junxiao Shicbba04c2014-01-26 14:21:22 -070039
Junxiao Shi28797792016-05-26 18:10:18 +000040BOOST_AUTO_TEST_CASE(Find)
41{
42 shared_ptr<Interest> interest1 = makeInterest("/6hNwxJjw");
43 shared_ptr<Interest> interest2 = makeInterest("/v65zqxm4d");
44
45 NameTree nameTree(16);
46 Pit pit(nameTree);
47
48 pit.insert(*interest1);
49 shared_ptr<pit::Entry> found1a = pit.find(*interest1);
50 shared_ptr<pit::Entry> found1b = pit.find(*interest1);
51 BOOST_CHECK(found1a != nullptr);
52 BOOST_CHECK(found1a == found1b);
53
54 shared_ptr<pit::Entry> found2 = pit.find(*interest2);
55 BOOST_CHECK(found2 == nullptr);
56 BOOST_CHECK(nameTree.findExactMatch(interest2->getName()) == nullptr);
57}
58
Junxiao Shicbba04c2014-01-26 14:21:22 -070059BOOST_AUTO_TEST_CASE(Insert)
60{
61 Name name1("ndn:/5vzBNnMst");
62 Name name2("ndn:/igSGfEIM62");
Junxiao Shicbba04c2014-01-26 14:21:22 -070063 Exclude exclude1;
64 exclude1.excludeOne(Name::Component("u26p47oep"));
65 Exclude exclude2;
Junxiao Shi30d35992014-04-03 14:51:58 -070066 exclude2.excludeBefore(Name::Component("u26p47oep"));
67 ndn::KeyLocator keyLocator1("ndn:/sGAE3peMHA");
68 ndn::KeyLocator keyLocator2("ndn:/nIJH6pr4");
Junxiao Shi57f0f312014-03-16 11:52:20 -070069
Haowei Yuan78c84d12014-02-27 15:35:13 -060070 NameTree nameTree(16);
71 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -070072 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi4846f372016-04-05 13:39:30 -070073 std::pair<shared_ptr<Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -070074
Junxiao Shi30d35992014-04-03 14:51:58 -070075 // base
76 shared_ptr<Interest> interestA = make_shared<Interest>(name1);
77 insertResult = pit.insert(*interestA);
Junxiao Shicbba04c2014-01-26 14:21:22 -070078 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060079 BOOST_CHECK_EQUAL(pit.size(), 1);
80
Junxiao Shi30d35992014-04-03 14:51:58 -070081 // A+MinSuffixComponents
82 shared_ptr<Interest> interestB = make_shared<Interest>(*interestA);
83 interestB->setMinSuffixComponents(2);
84 insertResult = pit.insert(*interestB);
Junxiao Shicbba04c2014-01-26 14:21:22 -070085 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060086 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -070087
Junxiao Shi30d35992014-04-03 14:51:58 -070088 // A+MaxSuffixComponents
89 shared_ptr<Interest> interestC = make_shared<Interest>(*interestA);
90 interestC->setMaxSuffixComponents(4);
91 insertResult = pit.insert(*interestC);
Junxiao Shicbba04c2014-01-26 14:21:22 -070092 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -060093 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -070094
Junxiao Shi30d35992014-04-03 14:51:58 -070095 // A+KeyLocator1
96 shared_ptr<Interest> interestD = make_shared<Interest>(*interestA);
97 interestD->setPublisherPublicKeyLocator(keyLocator1);
98 insertResult = pit.insert(*interestD);
Junxiao Shicbba04c2014-01-26 14:21:22 -070099 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600100 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700101
Junxiao Shi30d35992014-04-03 14:51:58 -0700102 // A+KeyLocator2
103 shared_ptr<Interest> interestE = make_shared<Interest>(*interestA);
104 interestE->setPublisherPublicKeyLocator(keyLocator2);
105 insertResult = pit.insert(*interestE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700106 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600107 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700108
Junxiao Shi30d35992014-04-03 14:51:58 -0700109 // A+Exclude1
110 shared_ptr<Interest> interestF = make_shared<Interest>(*interestA);
111 interestF->setExclude(exclude1);
112 insertResult = pit.insert(*interestF);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700113 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600114 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700115
Junxiao Shi30d35992014-04-03 14:51:58 -0700116 // A+Exclude2
117 shared_ptr<Interest> interestG = make_shared<Interest>(*interestA);
118 interestG->setExclude(exclude2);
119 insertResult = pit.insert(*interestG);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700120 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600121 BOOST_CHECK_EQUAL(pit.size(), 7);
122
Junxiao Shi30d35992014-04-03 14:51:58 -0700123 // A+ChildSelector0
124 shared_ptr<Interest> interestH = make_shared<Interest>(*interestA);
125 interestH->setChildSelector(0);
126 insertResult = pit.insert(*interestH);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700127 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600128 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shi30d35992014-04-03 14:51:58 -0700129
130 // A+ChildSelector1
131 shared_ptr<Interest> interestI = make_shared<Interest>(*interestA);
132 interestI->setChildSelector(1);
133 insertResult = pit.insert(*interestI);
134 BOOST_CHECK_EQUAL(insertResult.second, true);
135 BOOST_CHECK_EQUAL(pit.size(), 9);
136
137 // A+MustBeFresh
138 shared_ptr<Interest> interestJ = make_shared<Interest>(*interestA);
139 interestJ->setMustBeFresh(true);
140 insertResult = pit.insert(*interestJ);
141 BOOST_CHECK_EQUAL(insertResult.second, true);
142 BOOST_CHECK_EQUAL(pit.size(), 10);
143
144 // A+InterestLifetime
145 shared_ptr<Interest> interestK = make_shared<Interest>(*interestA);
146 interestK->setInterestLifetime(time::milliseconds(1000));
147 insertResult = pit.insert(*interestK);
148 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
149 BOOST_CHECK_EQUAL(pit.size(), 10);
150
151 // A+Nonce
152 shared_ptr<Interest> interestL = make_shared<Interest>(*interestA);
153 interestL->setNonce(2192);
154 insertResult = pit.insert(*interestL);
155 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
156 BOOST_CHECK_EQUAL(pit.size(), 10);
157
158 // different Name+Exclude1
159 shared_ptr<Interest> interestM = make_shared<Interest>(name2);
160 interestM->setExclude(exclude1);
161 insertResult = pit.insert(*interestM);
162 BOOST_CHECK_EQUAL(insertResult.second, true);
163 BOOST_CHECK_EQUAL(pit.size(), 11);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700164}
165
Haowei Yuan78c84d12014-02-27 15:35:13 -0600166BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700167{
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700168 shared_ptr<Interest> interest = makeInterest("/z88Admz6A2");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700169
Haowei Yuan78c84d12014-02-27 15:35:13 -0600170 NameTree nameTree(16);
171 Pit pit(nameTree);
172
Junxiao Shi4846f372016-04-05 13:39:30 -0700173 std::pair<shared_ptr<Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700174
Haowei Yuan78c84d12014-02-27 15:35:13 -0600175 BOOST_CHECK_EQUAL(pit.size(), 0);
176
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700177 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700178 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600179 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700180 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700181
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700182 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700183 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600184 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700185 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700186
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000187 pit.erase(insertResult.first.get());
Haowei Yuan78c84d12014-02-27 15:35:13 -0600188 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700189 BOOST_CHECK(pit.find(*interest) == nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700190
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700191 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700192 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600193 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700194 BOOST_CHECK(pit.find(*interest) != nullptr);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600195}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700196
Junxiao Shiee5a4442014-07-27 17:13:43 -0700197BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
198{
199 NameTree nameTree;
200 Pit pit(nameTree);
201 size_t nNameTreeEntriesBefore = nameTree.size();
202
203 shared_ptr<Interest> interest = makeInterest("/37xWVvQ2K");
Junxiao Shi4846f372016-04-05 13:39:30 -0700204 shared_ptr<Entry> entry = pit.insert(*interest).first;
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000205 pit.erase(entry.get());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700206 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
207}
208
spirosmastorakisff920302016-05-26 18:09:31 +0000209BOOST_AUTO_TEST_CASE(EraseWithFullName)
210{
211 shared_ptr<Data> data = makeData("/test");
212 shared_ptr<Interest> interest = makeInterest(data->getFullName());
213
214 NameTree nameTree(16);
215 Pit pit(nameTree);
216
217 BOOST_CHECK_EQUAL(pit.size(), 0);
218
219 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
220 BOOST_CHECK_EQUAL(pit.size(), 1);
221 BOOST_CHECK(pit.find(*interest) != nullptr);
222
223 BOOST_CHECK_EQUAL(pit.insert(*interest).second, false);
224 BOOST_CHECK_EQUAL(pit.size(), 1);
225 shared_ptr<pit::Entry> pitEntry = pit.find(*interest);
226 BOOST_REQUIRE(pitEntry != nullptr);
227
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000228 pit.erase(pitEntry.get());
spirosmastorakisff920302016-05-26 18:09:31 +0000229 BOOST_CHECK_EQUAL(pit.size(), 0);
230 BOOST_CHECK(pit.find(*interest) == nullptr);
231
232 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
233 BOOST_CHECK_EQUAL(pit.size(), 1);
234 BOOST_CHECK(pit.find(*interest) != nullptr);
235}
236
Junxiao Shicbba04c2014-01-26 14:21:22 -0700237BOOST_AUTO_TEST_CASE(FindAllDataMatches)
238{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600239 Name nameA ("ndn:/A");
240 Name nameAB ("ndn:/A/B");
241 Name nameABC ("ndn:/A/B/C");
242 Name nameABCD("ndn:/A/B/C/D");
243 Name nameD ("ndn:/D");
244
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700245 shared_ptr<Interest> interestA = makeInterest(nameA );
246 shared_ptr<Interest> interestABC = makeInterest(nameABC);
247 shared_ptr<Interest> interestD = makeInterest(nameD );
Junxiao Shicbba04c2014-01-26 14:21:22 -0700248
Haowei Yuan78c84d12014-02-27 15:35:13 -0600249 NameTree nameTree(16);
250 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600251 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700252
Haowei Yuan78c84d12014-02-27 15:35:13 -0600253 BOOST_CHECK_EQUAL(pit.size(), 0);
254
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700255 pit.insert(*interestA );
256 pit.insert(*interestABC);
257 pit.insert(*interestD );
Haowei Yuane1079fc2014-03-08 14:41:25 -0600258
259 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700260
Haowei Yuan78c84d12014-02-27 15:35:13 -0600261 BOOST_CHECK_EQUAL(pit.size(), 3);
262
Alexander Afanasyevc026d252014-06-16 11:14:15 -0700263 shared_ptr<Data> data = makeData(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700264
Junxiao Shi4846f372016-04-05 13:39:30 -0700265 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700266
Haowei Yuane1079fc2014-03-08 14:41:25 -0600267 bool hasA = false;
268 bool hasAB = false;
269 bool hasABC = false;
270 bool hasD = false;
271
Junxiao Shi4846f372016-04-05 13:39:30 -0700272 for (const shared_ptr<Entry>& entry : matches) {
Junxiao Shicbba04c2014-01-26 14:21:22 -0700273 ++count;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600274
275 if (entry->getName().equals(nameA ))
276 hasA = true;
277
278 if (entry->getName().equals(nameAB))
279 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700280
Haowei Yuane1079fc2014-03-08 14:41:25 -0600281 if (entry->getName().equals(nameABC))
282 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700283
Haowei Yuane1079fc2014-03-08 14:41:25 -0600284 if (entry->getName().equals(nameD))
285 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700286 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600287 BOOST_CHECK_EQUAL(hasA , true);
288 BOOST_CHECK_EQUAL(hasAB , false);
289 BOOST_CHECK_EQUAL(hasABC, true);
290 BOOST_CHECK_EQUAL(hasD , false);
291
Junxiao Shicbba04c2014-01-26 14:21:22 -0700292 BOOST_CHECK_EQUAL(count, 2);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700293}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600294
Junxiao Shi4370fde2016-02-24 12:20:46 -0700295BOOST_AUTO_TEST_CASE(MatchFullName) // Bug 3363
296{
297 NameTree nameTree(16);
298 Pit pit(nameTree);
299
300 shared_ptr<Data> data = makeData("/A");
301 Name fullName = data->getFullName();
302 shared_ptr<Interest> interest = makeInterest(fullName);
303
304 pit.insert(*interest);
Junxiao Shi4846f372016-04-05 13:39:30 -0700305 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700306
307 BOOST_REQUIRE_EQUAL(std::distance(matches.begin(), matches.end()), 1);
Junxiao Shi4846f372016-04-05 13:39:30 -0700308 shared_ptr<Entry> found = *matches.begin();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700309 BOOST_CHECK_EQUAL(found->getName(), fullName);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700310}
311
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800312BOOST_AUTO_TEST_CASE(Iterator)
313{
314 NameTree nameTree(16);
315 Pit pit(nameTree);
316
317 shared_ptr<Interest> interestA = makeInterest("/A");
318 shared_ptr<Interest> interestABC1 = makeInterest("/A/B/C");
319 shared_ptr<Interest> interestABC2 = makeInterest("/A/B/C");
320 interestABC2->setSelectors(ndn::Selectors().setMinSuffixComponents(10));
321 shared_ptr<Interest> interestD = makeInterest("/D");
322
323 BOOST_CHECK_EQUAL(pit.size(), 0);
324 BOOST_CHECK(pit.begin() == pit.end());
325
326 pit.insert(*interestABC1);
327 BOOST_CHECK_EQUAL(pit.size(), 1);
328 BOOST_CHECK(pit.begin() != pit.end());
329 BOOST_CHECK(pit.begin()->getInterest() == *interestABC1);
330 BOOST_CHECK((*pit.begin()).getInterest() == *interestABC1);
331
332 auto i = pit.begin();
333 auto j = pit.begin();
334 BOOST_CHECK(++i == pit.end());
335 BOOST_CHECK(j++ == pit.begin());
336 BOOST_CHECK(j == pit.end());
337
338 pit.insert(*interestA);
339 pit.insert(*interestABC2);
340 pit.insert(*interestD);
341
342 std::set<const Interest*> expected = {&*interestA, &*interestABC1, &*interestABC2, &*interestD};
343 std::set<const Interest*> actual;
344 for (const auto& pitEntry : pit) {
345 actual.insert(&pitEntry.getInterest());
346 }
347 BOOST_CHECK(actual == expected);
348 for (auto actualIt = actual.begin(), expectedIt = expected.begin();
349 actualIt != actual.end() && expectedIt != expected.end(); ++actualIt, ++expectedIt) {
350 BOOST_CHECK_EQUAL(**actualIt, **expectedIt);
351 }
352}
353
Junxiao Shicbba04c2014-01-26 14:21:22 -0700354BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700355BOOST_AUTO_TEST_SUITE_END()
Junxiao Shicbba04c2014-01-26 14:21:22 -0700356
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700357} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800358} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800359} // namespace nfd