blob: 9ca3ec710780f089e5cca3b741c40fea00b09f9c [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 Shi5e5e4452015-09-24 16:56:52 -070040BOOST_AUTO_TEST_SUITE(PitEntry)
41
42BOOST_AUTO_TEST_CASE(InOutRecords)
Junxiao Shicbba04c2014-01-26 14:21:22 -070043{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070044 shared_ptr<Face> face1 = make_shared<DummyFace>();
45 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shicbba04c2014-01-26 14:21:22 -070046 Name name("ndn:/KuYfjtRq");
Junxiao Shif3c07812014-03-11 21:48:49 -070047 shared_ptr<Interest> interest = makeInterest(name);
48 shared_ptr<Interest> interest1 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070049 interest1->setInterestLifetime(time::milliseconds(2528));
Junxiao Shif3c07812014-03-11 21:48:49 -070050 interest1->setNonce(25559);
51 shared_ptr<Interest> interest2 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070052 interest2->setInterestLifetime(time::milliseconds(6464));
Junxiao Shif3c07812014-03-11 21:48:49 -070053 interest2->setNonce(19004);
54 shared_ptr<Interest> interest3 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070055 interest3->setInterestLifetime(time::milliseconds(3585));
Junxiao Shif3c07812014-03-11 21:48:49 -070056 interest3->setNonce(24216);
57 shared_ptr<Interest> interest4 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070058 interest4->setInterestLifetime(time::milliseconds(8795));
Junxiao Shif3c07812014-03-11 21:48:49 -070059 interest4->setNonce(17365);
Junxiao Shi57f0f312014-03-16 11:52:20 -070060
Junxiao Shif3c07812014-03-11 21:48:49 -070061 pit::Entry entry(*interest);
Junxiao Shi57f0f312014-03-16 11:52:20 -070062
Junxiao Shif3c07812014-03-11 21:48:49 -070063 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
64 BOOST_CHECK_EQUAL(entry.getName(), name);
Haowei Yuan78c84d12014-02-27 15:35:13 -060065
Junxiao Shicbba04c2014-01-26 14:21:22 -070066 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
67 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
68 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
69 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
Junxiao Shi57f0f312014-03-16 11:52:20 -070070
Junxiao Shicbba04c2014-01-26 14:21:22 -070071 // insert InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070072 time::steady_clock::TimePoint before1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070073 pit::InRecordCollection::iterator in1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070074 entry.insertOrUpdateInRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070075 time::steady_clock::TimePoint after1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070076 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
77 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
78 BOOST_CHECK(in1 == inRecords2.begin());
79 BOOST_CHECK_EQUAL(in1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070080 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070081 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
82 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070083 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed()
84 - interest1->getInterestLifetime(),
85 (after1 - before1));
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070086 BOOST_CHECK(in1 == entry.getInRecord(*face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -070087
Junxiao Shicbba04c2014-01-26 14:21:22 -070088 // insert OutRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070089 time::steady_clock::TimePoint before2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070090 pit::OutRecordCollection::iterator out1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070091 entry.insertOrUpdateOutRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070092 time::steady_clock::TimePoint after2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070093 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
94 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
95 BOOST_CHECK(out1 == outRecords2.begin());
96 BOOST_CHECK_EQUAL(out1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070097 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070098 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
99 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700100 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed()
101 - interest1->getInterestLifetime(),
102 (after2 - before2));
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700103 BOOST_CHECK(out1 == entry.getOutRecord(*face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -0700104
Junxiao Shicbba04c2014-01-26 14:21:22 -0700105 // update InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700106 time::steady_clock::TimePoint before3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700107 pit::InRecordCollection::iterator in2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700108 entry.insertOrUpdateInRecord(face1, *interest2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700109 time::steady_clock::TimePoint after3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700110 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
111 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
112 BOOST_CHECK(in2 == inRecords3.begin());
113 BOOST_CHECK_EQUAL(in2->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700114 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700115 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed()
116 - interest2->getInterestLifetime(),
117 (after3 - before3));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700118
Junxiao Shicbba04c2014-01-26 14:21:22 -0700119 // insert another InRecord
120 pit::InRecordCollection::iterator in3 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700121 entry.insertOrUpdateInRecord(face2, *interest3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700122 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
123 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
124 BOOST_CHECK_EQUAL(in3->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700125
Junxiao Shi66f91f82014-05-10 17:28:58 -0700126 // get InRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700127 pit::InRecordCollection::const_iterator in4 = entry.getInRecord(*face1);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700128 BOOST_REQUIRE(in4 != entry.getInRecords().end());
129 BOOST_CHECK_EQUAL(in4->getFace(), face1);
130
Junxiao Shicbba04c2014-01-26 14:21:22 -0700131 // delete all InRecords
132 entry.deleteInRecords();
133 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
134 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700135 BOOST_CHECK(entry.getInRecord(*face1) == entry.getInRecords().end());
Junxiao Shicbba04c2014-01-26 14:21:22 -0700136
Junxiao Shicbba04c2014-01-26 14:21:22 -0700137 // insert another OutRecord
138 pit::OutRecordCollection::iterator out2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700139 entry.insertOrUpdateOutRecord(face2, *interest4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700140 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
141 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
142 BOOST_CHECK_EQUAL(out2->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700143
Junxiao Shi66f91f82014-05-10 17:28:58 -0700144 // get OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700145 pit::OutRecordCollection::const_iterator out3 = entry.getOutRecord(*face1);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700146 BOOST_REQUIRE(out3 != entry.getOutRecords().end());
147 BOOST_CHECK_EQUAL(out3->getFace(), face1);
148
Junxiao Shicbba04c2014-01-26 14:21:22 -0700149 // delete OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700150 entry.deleteOutRecord(*face2);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700151 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
152 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
153 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700154 BOOST_CHECK(entry.getOutRecord(*face2) == entry.getOutRecords().end());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700155}
156
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700157BOOST_AUTO_TEST_CASE(Lifetime)
Junxiao Shi57f0f312014-03-16 11:52:20 -0700158{
159 shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
Junxiao Shia110f262014-10-12 12:35:20 -0700160 // library uses -1 to indicate unset lifetime
161 BOOST_ASSERT(interest->getInterestLifetime() < time::milliseconds::zero());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700162
163 shared_ptr<Face> face = make_shared<DummyFace>();
164 pit::Entry entry(*interest);
165
166 pit::InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700167 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700168
169 pit::OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700170 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700171}
172
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700173BOOST_AUTO_TEST_CASE(OutRecordNack)
174{
175 shared_ptr<Face> face1 = make_shared<DummyFace>();
176 pit::OutRecord outR(face1);
177 BOOST_CHECK(outR.getIncomingNack() == nullptr);
178
179 shared_ptr<Interest> interest1 = makeInterest("ndn:/uWiapGjYL");
180 interest1->setNonce(165);
181 outR.update(*interest1);
182 BOOST_CHECK(outR.getIncomingNack() == nullptr);
183
184 shared_ptr<Interest> interest2 = makeInterest("ndn:/uWiapGjYL");
185 interest2->setNonce(996);
186 lp::Nack nack2(*interest2);
187 nack2.setReason(lp::NackReason::CONGESTION);
188 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false);
189 BOOST_CHECK(outR.getIncomingNack() == nullptr);
190
191 lp::Nack nack1(*interest1);
192 nack1.setReason(lp::NackReason::DUPLICATE);
193 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true);
194 BOOST_REQUIRE(outR.getIncomingNack() != nullptr);
195 BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE);
196
197 outR.clearIncomingNack();
198 BOOST_CHECK(outR.getIncomingNack() == nullptr);
199}
200
201BOOST_AUTO_TEST_SUITE_END() // PitEntry
202
203
Junxiao Shicbba04c2014-01-26 14:21:22 -0700204BOOST_AUTO_TEST_CASE(Insert)
205{
206 Name name1("ndn:/5vzBNnMst");
207 Name name2("ndn:/igSGfEIM62");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700208 Exclude exclude1;
209 exclude1.excludeOne(Name::Component("u26p47oep"));
210 Exclude exclude2;
Junxiao Shi30d35992014-04-03 14:51:58 -0700211 exclude2.excludeBefore(Name::Component("u26p47oep"));
212 ndn::KeyLocator keyLocator1("ndn:/sGAE3peMHA");
213 ndn::KeyLocator keyLocator2("ndn:/nIJH6pr4");
Junxiao Shi57f0f312014-03-16 11:52:20 -0700214
Haowei Yuan78c84d12014-02-27 15:35:13 -0600215 NameTree nameTree(16);
216 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -0700217 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700218 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700219
Junxiao Shi30d35992014-04-03 14:51:58 -0700220 // base
221 shared_ptr<Interest> interestA = make_shared<Interest>(name1);
222 insertResult = pit.insert(*interestA);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700223 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600224 BOOST_CHECK_EQUAL(pit.size(), 1);
225
Junxiao Shi30d35992014-04-03 14:51:58 -0700226 // A+MinSuffixComponents
227 shared_ptr<Interest> interestB = make_shared<Interest>(*interestA);
228 interestB->setMinSuffixComponents(2);
229 insertResult = pit.insert(*interestB);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700230 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600231 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700232
Junxiao Shi30d35992014-04-03 14:51:58 -0700233 // A+MaxSuffixComponents
234 shared_ptr<Interest> interestC = make_shared<Interest>(*interestA);
235 interestC->setMaxSuffixComponents(4);
236 insertResult = pit.insert(*interestC);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700237 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600238 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700239
Junxiao Shi30d35992014-04-03 14:51:58 -0700240 // A+KeyLocator1
241 shared_ptr<Interest> interestD = make_shared<Interest>(*interestA);
242 interestD->setPublisherPublicKeyLocator(keyLocator1);
243 insertResult = pit.insert(*interestD);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700244 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600245 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700246
Junxiao Shi30d35992014-04-03 14:51:58 -0700247 // A+KeyLocator2
248 shared_ptr<Interest> interestE = make_shared<Interest>(*interestA);
249 interestE->setPublisherPublicKeyLocator(keyLocator2);
250 insertResult = pit.insert(*interestE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700251 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600252 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700253
Junxiao Shi30d35992014-04-03 14:51:58 -0700254 // A+Exclude1
255 shared_ptr<Interest> interestF = make_shared<Interest>(*interestA);
256 interestF->setExclude(exclude1);
257 insertResult = pit.insert(*interestF);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700258 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600259 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700260
Junxiao Shi30d35992014-04-03 14:51:58 -0700261 // A+Exclude2
262 shared_ptr<Interest> interestG = make_shared<Interest>(*interestA);
263 interestG->setExclude(exclude2);
264 insertResult = pit.insert(*interestG);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700265 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600266 BOOST_CHECK_EQUAL(pit.size(), 7);
267
Junxiao Shi30d35992014-04-03 14:51:58 -0700268 // A+ChildSelector0
269 shared_ptr<Interest> interestH = make_shared<Interest>(*interestA);
270 interestH->setChildSelector(0);
271 insertResult = pit.insert(*interestH);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700272 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600273 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shi30d35992014-04-03 14:51:58 -0700274
275 // A+ChildSelector1
276 shared_ptr<Interest> interestI = make_shared<Interest>(*interestA);
277 interestI->setChildSelector(1);
278 insertResult = pit.insert(*interestI);
279 BOOST_CHECK_EQUAL(insertResult.second, true);
280 BOOST_CHECK_EQUAL(pit.size(), 9);
281
282 // A+MustBeFresh
283 shared_ptr<Interest> interestJ = make_shared<Interest>(*interestA);
284 interestJ->setMustBeFresh(true);
285 insertResult = pit.insert(*interestJ);
286 BOOST_CHECK_EQUAL(insertResult.second, true);
287 BOOST_CHECK_EQUAL(pit.size(), 10);
288
289 // A+InterestLifetime
290 shared_ptr<Interest> interestK = make_shared<Interest>(*interestA);
291 interestK->setInterestLifetime(time::milliseconds(1000));
292 insertResult = pit.insert(*interestK);
293 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
294 BOOST_CHECK_EQUAL(pit.size(), 10);
295
296 // A+Nonce
297 shared_ptr<Interest> interestL = make_shared<Interest>(*interestA);
298 interestL->setNonce(2192);
299 insertResult = pit.insert(*interestL);
300 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
301 BOOST_CHECK_EQUAL(pit.size(), 10);
302
303 // different Name+Exclude1
304 shared_ptr<Interest> interestM = make_shared<Interest>(name2);
305 interestM->setExclude(exclude1);
306 insertResult = pit.insert(*interestM);
307 BOOST_CHECK_EQUAL(insertResult.second, true);
308 BOOST_CHECK_EQUAL(pit.size(), 11);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700309}
310
Haowei Yuan78c84d12014-02-27 15:35:13 -0600311BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700312{
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700313 shared_ptr<Interest> interest = makeInterest("/z88Admz6A2");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700314
Haowei Yuan78c84d12014-02-27 15:35:13 -0600315 NameTree nameTree(16);
316 Pit pit(nameTree);
317
Junxiao Shicbba04c2014-01-26 14:21:22 -0700318 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700319
Haowei Yuan78c84d12014-02-27 15:35:13 -0600320 BOOST_CHECK_EQUAL(pit.size(), 0);
321
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700322 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700323 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600324 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700325 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700326
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700327 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700328 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600329 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700330 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700331
Haowei Yuan78c84d12014-02-27 15:35:13 -0600332 pit.erase(insertResult.first);
333 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700334 BOOST_CHECK(pit.find(*interest) == nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700335
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700336 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700337 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600338 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700339 BOOST_CHECK(pit.find(*interest) != nullptr);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600340}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700341
Junxiao Shiee5a4442014-07-27 17:13:43 -0700342BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
343{
344 NameTree nameTree;
345 Pit pit(nameTree);
346 size_t nNameTreeEntriesBefore = nameTree.size();
347
348 shared_ptr<Interest> interest = makeInterest("/37xWVvQ2K");
349 shared_ptr<pit::Entry> entry = pit.insert(*interest).first;
350 pit.erase(entry);
351 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
352}
353
Junxiao Shicbba04c2014-01-26 14:21:22 -0700354BOOST_AUTO_TEST_CASE(FindAllDataMatches)
355{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600356 Name nameA ("ndn:/A");
357 Name nameAB ("ndn:/A/B");
358 Name nameABC ("ndn:/A/B/C");
359 Name nameABCD("ndn:/A/B/C/D");
360 Name nameD ("ndn:/D");
361
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700362 shared_ptr<Interest> interestA = makeInterest(nameA );
363 shared_ptr<Interest> interestABC = makeInterest(nameABC);
364 shared_ptr<Interest> interestD = makeInterest(nameD );
Junxiao Shicbba04c2014-01-26 14:21:22 -0700365
Haowei Yuan78c84d12014-02-27 15:35:13 -0600366 NameTree nameTree(16);
367 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600368 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700369
Haowei Yuan78c84d12014-02-27 15:35:13 -0600370 BOOST_CHECK_EQUAL(pit.size(), 0);
371
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700372 pit.insert(*interestA );
373 pit.insert(*interestABC);
374 pit.insert(*interestD );
Haowei Yuane1079fc2014-03-08 14:41:25 -0600375
376 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700377
Haowei Yuan78c84d12014-02-27 15:35:13 -0600378 BOOST_CHECK_EQUAL(pit.size(), 3);
379
Alexander Afanasyevc026d252014-06-16 11:14:15 -0700380 shared_ptr<Data> data = makeData(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700381
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700382 pit::DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700383
Haowei Yuane1079fc2014-03-08 14:41:25 -0600384 bool hasA = false;
385 bool hasAB = false;
386 bool hasABC = false;
387 bool hasD = false;
388
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700389 for (const shared_ptr<pit::Entry>& entry : matches) {
Junxiao Shicbba04c2014-01-26 14:21:22 -0700390 ++count;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600391
392 if (entry->getName().equals(nameA ))
393 hasA = true;
394
395 if (entry->getName().equals(nameAB))
396 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700397
Haowei Yuane1079fc2014-03-08 14:41:25 -0600398 if (entry->getName().equals(nameABC))
399 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700400
Haowei Yuane1079fc2014-03-08 14:41:25 -0600401 if (entry->getName().equals(nameD))
402 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700403 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600404 BOOST_CHECK_EQUAL(hasA , true);
405 BOOST_CHECK_EQUAL(hasAB , false);
406 BOOST_CHECK_EQUAL(hasABC, true);
407 BOOST_CHECK_EQUAL(hasD , false);
408
Junxiao Shicbba04c2014-01-26 14:21:22 -0700409 BOOST_CHECK_EQUAL(count, 2);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700410}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600411
Junxiao Shi4370fde2016-02-24 12:20:46 -0700412BOOST_AUTO_TEST_CASE(MatchFullName) // Bug 3363
413{
414 NameTree nameTree(16);
415 Pit pit(nameTree);
416
417 shared_ptr<Data> data = makeData("/A");
418 Name fullName = data->getFullName();
419 shared_ptr<Interest> interest = makeInterest(fullName);
420
421 pit.insert(*interest);
422 pit::DataMatchResult matches = pit.findAllDataMatches(*data);
423
424 BOOST_REQUIRE_EQUAL(std::distance(matches.begin(), matches.end()), 1);
425 shared_ptr<pit::Entry> found = *matches.begin();
426 BOOST_CHECK_EQUAL(found->getName(), fullName);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700427}
428
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800429BOOST_AUTO_TEST_CASE(Iterator)
430{
431 NameTree nameTree(16);
432 Pit pit(nameTree);
433
434 shared_ptr<Interest> interestA = makeInterest("/A");
435 shared_ptr<Interest> interestABC1 = makeInterest("/A/B/C");
436 shared_ptr<Interest> interestABC2 = makeInterest("/A/B/C");
437 interestABC2->setSelectors(ndn::Selectors().setMinSuffixComponents(10));
438 shared_ptr<Interest> interestD = makeInterest("/D");
439
440 BOOST_CHECK_EQUAL(pit.size(), 0);
441 BOOST_CHECK(pit.begin() == pit.end());
442
443 pit.insert(*interestABC1);
444 BOOST_CHECK_EQUAL(pit.size(), 1);
445 BOOST_CHECK(pit.begin() != pit.end());
446 BOOST_CHECK(pit.begin()->getInterest() == *interestABC1);
447 BOOST_CHECK((*pit.begin()).getInterest() == *interestABC1);
448
449 auto i = pit.begin();
450 auto j = pit.begin();
451 BOOST_CHECK(++i == pit.end());
452 BOOST_CHECK(j++ == pit.begin());
453 BOOST_CHECK(j == pit.end());
454
455 pit.insert(*interestA);
456 pit.insert(*interestABC2);
457 pit.insert(*interestD);
458
459 std::set<const Interest*> expected = {&*interestA, &*interestABC1, &*interestABC2, &*interestD};
460 std::set<const Interest*> actual;
461 for (const auto& pitEntry : pit) {
462 actual.insert(&pitEntry.getInterest());
463 }
464 BOOST_CHECK(actual == expected);
465 for (auto actualIt = actual.begin(), expectedIt = expected.begin();
466 actualIt != actual.end() && expectedIt != expected.end(); ++actualIt, ++expectedIt) {
467 BOOST_CHECK_EQUAL(**actualIt, **expectedIt);
468 }
469}
470
Junxiao Shicbba04c2014-01-26 14:21:22 -0700471BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700472BOOST_AUTO_TEST_SUITE_END()
Junxiao Shicbba04c2014-01-26 14:21:22 -0700473
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700474} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800475} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800476} // namespace nfd