blob: e761ef877df6488eae6d0dbe8a4d423eedc82425 [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 Shi4846f372016-04-05 13:39:30 -070061 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 Shi4846f372016-04-05 13:39:30 -070066 const InRecordCollection& inRecords1 = entry.getInRecords();
Junxiao Shicbba04c2014-01-26 14:21:22 -070067 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
Junxiao Shi4846f372016-04-05 13:39:30 -070068 const OutRecordCollection& outRecords1 = entry.getOutRecords();
Junxiao Shicbba04c2014-01-26 14:21:22 -070069 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
Junxiao Shi57f0f312014-03-16 11:52:20 -070070
Junxiao Shi4846f372016-04-05 13:39:30 -070071 // insert in-record
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070072 time::steady_clock::TimePoint before1 = time::steady_clock::now();
Junxiao Shi9cff7792016-08-01 21:45:11 +000073 InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070074 time::steady_clock::TimePoint after1 = time::steady_clock::now();
Junxiao Shi4846f372016-04-05 13:39:30 -070075 const InRecordCollection& inRecords2 = entry.getInRecords();
Junxiao Shicbba04c2014-01-26 14:21:22 -070076 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
77 BOOST_CHECK(in1 == inRecords2.begin());
Junxiao Shi9cff7792016-08-01 21:45:11 +000078 BOOST_CHECK_EQUAL(&in1->getFace(), face1.get());
Junxiao Shif3c07812014-03-11 21:48:49 -070079 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070080 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
81 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070082 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed()
83 - interest1->getInterestLifetime(),
84 (after1 - before1));
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070085 BOOST_CHECK(in1 == entry.getInRecord(*face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -070086
Junxiao Shi4846f372016-04-05 13:39:30 -070087 // insert out-record
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070088 time::steady_clock::TimePoint before2 = time::steady_clock::now();
Junxiao Shi9cff7792016-08-01 21:45:11 +000089 OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070090 time::steady_clock::TimePoint after2 = time::steady_clock::now();
Junxiao Shi4846f372016-04-05 13:39:30 -070091 const OutRecordCollection& outRecords2 = entry.getOutRecords();
Junxiao Shicbba04c2014-01-26 14:21:22 -070092 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
93 BOOST_CHECK(out1 == outRecords2.begin());
Junxiao Shi9cff7792016-08-01 21:45:11 +000094 BOOST_CHECK_EQUAL(&out1->getFace(), face1.get());
Junxiao Shif3c07812014-03-11 21:48:49 -070095 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070096 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
97 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070098 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed()
99 - interest1->getInterestLifetime(),
100 (after2 - before2));
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700101 BOOST_CHECK(out1 == entry.getOutRecord(*face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -0700102
Junxiao Shi4846f372016-04-05 13:39:30 -0700103 // update in-record
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700104 time::steady_clock::TimePoint before3 = time::steady_clock::now();
Junxiao Shi9cff7792016-08-01 21:45:11 +0000105 InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, *interest2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700106 time::steady_clock::TimePoint after3 = time::steady_clock::now();
Junxiao Shi4846f372016-04-05 13:39:30 -0700107 const InRecordCollection& inRecords3 = entry.getInRecords();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700108 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
109 BOOST_CHECK(in2 == inRecords3.begin());
Junxiao Shi9cff7792016-08-01 21:45:11 +0000110 BOOST_CHECK_EQUAL(&in2->getFace(), face1.get());
Junxiao Shif3c07812014-03-11 21:48:49 -0700111 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700112 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed()
113 - interest2->getInterestLifetime(),
114 (after3 - before3));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700115
Junxiao Shi4846f372016-04-05 13:39:30 -0700116 // insert another in-record
Junxiao Shi9cff7792016-08-01 21:45:11 +0000117 InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, *interest3);
Junxiao Shi4846f372016-04-05 13:39:30 -0700118 const InRecordCollection& inRecords4 = entry.getInRecords();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700119 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000120 BOOST_CHECK_EQUAL(&in3->getFace(), face2.get());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700121
Junxiao Shi4846f372016-04-05 13:39:30 -0700122 // get in-record
123 InRecordCollection::iterator in4 = entry.getInRecord(*face1);
124 BOOST_REQUIRE(in4 != entry.in_end());
Junxiao Shi9cff7792016-08-01 21:45:11 +0000125 BOOST_CHECK_EQUAL(&in4->getFace(), face1.get());
Junxiao Shi66f91f82014-05-10 17:28:58 -0700126
Junxiao Shi4846f372016-04-05 13:39:30 -0700127 // clear in-records
128 entry.clearInRecords();
129 const InRecordCollection& inRecords5 = entry.getInRecords();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700130 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
Junxiao Shi4846f372016-04-05 13:39:30 -0700131 BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end());
Junxiao Shicbba04c2014-01-26 14:21:22 -0700132
Junxiao Shi4846f372016-04-05 13:39:30 -0700133 // insert another out-record
134 OutRecordCollection::iterator out2 =
Junxiao Shi9cff7792016-08-01 21:45:11 +0000135 entry.insertOrUpdateOutRecord(*face2, *interest4);
Junxiao Shi4846f372016-04-05 13:39:30 -0700136 const OutRecordCollection& outRecords3 = entry.getOutRecords();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700137 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000138 BOOST_CHECK_EQUAL(&out2->getFace(), face2.get());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700139
Junxiao Shi4846f372016-04-05 13:39:30 -0700140 // get out-record
141 OutRecordCollection::iterator out3 = entry.getOutRecord(*face1);
142 BOOST_REQUIRE(out3 != entry.out_end());
Junxiao Shi9cff7792016-08-01 21:45:11 +0000143 BOOST_CHECK_EQUAL(&out3->getFace(), face1.get());
Junxiao Shi66f91f82014-05-10 17:28:58 -0700144
Junxiao Shi4846f372016-04-05 13:39:30 -0700145 // delete out-record
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700146 entry.deleteOutRecord(*face2);
Junxiao Shi4846f372016-04-05 13:39:30 -0700147 const OutRecordCollection& outRecords4 = entry.getOutRecords();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700148 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000149 BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get());
Junxiao Shi4846f372016-04-05 13:39:30 -0700150 BOOST_CHECK(entry.getOutRecord(*face2) == entry.out_end());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700151}
152
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700153BOOST_AUTO_TEST_CASE(Lifetime)
Junxiao Shi57f0f312014-03-16 11:52:20 -0700154{
155 shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
Junxiao Shia110f262014-10-12 12:35:20 -0700156 // library uses -1 to indicate unset lifetime
157 BOOST_ASSERT(interest->getInterestLifetime() < time::milliseconds::zero());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700158
159 shared_ptr<Face> face = make_shared<DummyFace>();
Junxiao Shi4846f372016-04-05 13:39:30 -0700160 Entry entry(*interest);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700161
Junxiao Shi9cff7792016-08-01 21:45:11 +0000162 InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(*face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700163 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700164
Junxiao Shi9cff7792016-08-01 21:45:11 +0000165 OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(*face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700166 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700167}
168
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700169BOOST_AUTO_TEST_CASE(OutRecordNack)
170{
171 shared_ptr<Face> face1 = make_shared<DummyFace>();
Junxiao Shi9cff7792016-08-01 21:45:11 +0000172 OutRecord outR(*face1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700173 BOOST_CHECK(outR.getIncomingNack() == nullptr);
174
175 shared_ptr<Interest> interest1 = makeInterest("ndn:/uWiapGjYL");
176 interest1->setNonce(165);
177 outR.update(*interest1);
178 BOOST_CHECK(outR.getIncomingNack() == nullptr);
179
180 shared_ptr<Interest> interest2 = makeInterest("ndn:/uWiapGjYL");
181 interest2->setNonce(996);
182 lp::Nack nack2(*interest2);
183 nack2.setReason(lp::NackReason::CONGESTION);
184 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false);
185 BOOST_CHECK(outR.getIncomingNack() == nullptr);
186
187 lp::Nack nack1(*interest1);
188 nack1.setReason(lp::NackReason::DUPLICATE);
189 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true);
190 BOOST_REQUIRE(outR.getIncomingNack() != nullptr);
191 BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE);
192
193 outR.clearIncomingNack();
194 BOOST_CHECK(outR.getIncomingNack() == nullptr);
195}
196
197BOOST_AUTO_TEST_SUITE_END() // PitEntry
198
199
Junxiao Shi28797792016-05-26 18:10:18 +0000200BOOST_AUTO_TEST_CASE(Find)
201{
202 shared_ptr<Interest> interest1 = makeInterest("/6hNwxJjw");
203 shared_ptr<Interest> interest2 = makeInterest("/v65zqxm4d");
204
205 NameTree nameTree(16);
206 Pit pit(nameTree);
207
208 pit.insert(*interest1);
209 shared_ptr<pit::Entry> found1a = pit.find(*interest1);
210 shared_ptr<pit::Entry> found1b = pit.find(*interest1);
211 BOOST_CHECK(found1a != nullptr);
212 BOOST_CHECK(found1a == found1b);
213
214 shared_ptr<pit::Entry> found2 = pit.find(*interest2);
215 BOOST_CHECK(found2 == nullptr);
216 BOOST_CHECK(nameTree.findExactMatch(interest2->getName()) == nullptr);
217}
218
Junxiao Shicbba04c2014-01-26 14:21:22 -0700219BOOST_AUTO_TEST_CASE(Insert)
220{
221 Name name1("ndn:/5vzBNnMst");
222 Name name2("ndn:/igSGfEIM62");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700223 Exclude exclude1;
224 exclude1.excludeOne(Name::Component("u26p47oep"));
225 Exclude exclude2;
Junxiao Shi30d35992014-04-03 14:51:58 -0700226 exclude2.excludeBefore(Name::Component("u26p47oep"));
227 ndn::KeyLocator keyLocator1("ndn:/sGAE3peMHA");
228 ndn::KeyLocator keyLocator2("ndn:/nIJH6pr4");
Junxiao Shi57f0f312014-03-16 11:52:20 -0700229
Haowei Yuan78c84d12014-02-27 15:35:13 -0600230 NameTree nameTree(16);
231 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -0700232 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi4846f372016-04-05 13:39:30 -0700233 std::pair<shared_ptr<Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700234
Junxiao Shi30d35992014-04-03 14:51:58 -0700235 // base
236 shared_ptr<Interest> interestA = make_shared<Interest>(name1);
237 insertResult = pit.insert(*interestA);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700238 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600239 BOOST_CHECK_EQUAL(pit.size(), 1);
240
Junxiao Shi30d35992014-04-03 14:51:58 -0700241 // A+MinSuffixComponents
242 shared_ptr<Interest> interestB = make_shared<Interest>(*interestA);
243 interestB->setMinSuffixComponents(2);
244 insertResult = pit.insert(*interestB);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700245 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600246 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700247
Junxiao Shi30d35992014-04-03 14:51:58 -0700248 // A+MaxSuffixComponents
249 shared_ptr<Interest> interestC = make_shared<Interest>(*interestA);
250 interestC->setMaxSuffixComponents(4);
251 insertResult = pit.insert(*interestC);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700252 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600253 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700254
Junxiao Shi30d35992014-04-03 14:51:58 -0700255 // A+KeyLocator1
256 shared_ptr<Interest> interestD = make_shared<Interest>(*interestA);
257 interestD->setPublisherPublicKeyLocator(keyLocator1);
258 insertResult = pit.insert(*interestD);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700259 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600260 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700261
Junxiao Shi30d35992014-04-03 14:51:58 -0700262 // A+KeyLocator2
263 shared_ptr<Interest> interestE = make_shared<Interest>(*interestA);
264 interestE->setPublisherPublicKeyLocator(keyLocator2);
265 insertResult = pit.insert(*interestE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700266 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600267 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700268
Junxiao Shi30d35992014-04-03 14:51:58 -0700269 // A+Exclude1
270 shared_ptr<Interest> interestF = make_shared<Interest>(*interestA);
271 interestF->setExclude(exclude1);
272 insertResult = pit.insert(*interestF);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700273 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600274 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700275
Junxiao Shi30d35992014-04-03 14:51:58 -0700276 // A+Exclude2
277 shared_ptr<Interest> interestG = make_shared<Interest>(*interestA);
278 interestG->setExclude(exclude2);
279 insertResult = pit.insert(*interestG);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700280 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600281 BOOST_CHECK_EQUAL(pit.size(), 7);
282
Junxiao Shi30d35992014-04-03 14:51:58 -0700283 // A+ChildSelector0
284 shared_ptr<Interest> interestH = make_shared<Interest>(*interestA);
285 interestH->setChildSelector(0);
286 insertResult = pit.insert(*interestH);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700287 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600288 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shi30d35992014-04-03 14:51:58 -0700289
290 // A+ChildSelector1
291 shared_ptr<Interest> interestI = make_shared<Interest>(*interestA);
292 interestI->setChildSelector(1);
293 insertResult = pit.insert(*interestI);
294 BOOST_CHECK_EQUAL(insertResult.second, true);
295 BOOST_CHECK_EQUAL(pit.size(), 9);
296
297 // A+MustBeFresh
298 shared_ptr<Interest> interestJ = make_shared<Interest>(*interestA);
299 interestJ->setMustBeFresh(true);
300 insertResult = pit.insert(*interestJ);
301 BOOST_CHECK_EQUAL(insertResult.second, true);
302 BOOST_CHECK_EQUAL(pit.size(), 10);
303
304 // A+InterestLifetime
305 shared_ptr<Interest> interestK = make_shared<Interest>(*interestA);
306 interestK->setInterestLifetime(time::milliseconds(1000));
307 insertResult = pit.insert(*interestK);
308 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
309 BOOST_CHECK_EQUAL(pit.size(), 10);
310
311 // A+Nonce
312 shared_ptr<Interest> interestL = make_shared<Interest>(*interestA);
313 interestL->setNonce(2192);
314 insertResult = pit.insert(*interestL);
315 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
316 BOOST_CHECK_EQUAL(pit.size(), 10);
317
318 // different Name+Exclude1
319 shared_ptr<Interest> interestM = make_shared<Interest>(name2);
320 interestM->setExclude(exclude1);
321 insertResult = pit.insert(*interestM);
322 BOOST_CHECK_EQUAL(insertResult.second, true);
323 BOOST_CHECK_EQUAL(pit.size(), 11);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700324}
325
Haowei Yuan78c84d12014-02-27 15:35:13 -0600326BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700327{
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700328 shared_ptr<Interest> interest = makeInterest("/z88Admz6A2");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700329
Haowei Yuan78c84d12014-02-27 15:35:13 -0600330 NameTree nameTree(16);
331 Pit pit(nameTree);
332
Junxiao Shi4846f372016-04-05 13:39:30 -0700333 std::pair<shared_ptr<Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700334
Haowei Yuan78c84d12014-02-27 15:35:13 -0600335 BOOST_CHECK_EQUAL(pit.size(), 0);
336
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700337 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700338 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600339 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700340 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700341
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700342 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700343 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600344 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700345 BOOST_CHECK(pit.find(*interest) != nullptr);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700346
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000347 pit.erase(insertResult.first.get());
Haowei Yuan78c84d12014-02-27 15:35:13 -0600348 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700349 BOOST_CHECK(pit.find(*interest) == nullptr);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700350
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700351 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700352 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600353 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700354 BOOST_CHECK(pit.find(*interest) != nullptr);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600355}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700356
Junxiao Shiee5a4442014-07-27 17:13:43 -0700357BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
358{
359 NameTree nameTree;
360 Pit pit(nameTree);
361 size_t nNameTreeEntriesBefore = nameTree.size();
362
363 shared_ptr<Interest> interest = makeInterest("/37xWVvQ2K");
Junxiao Shi4846f372016-04-05 13:39:30 -0700364 shared_ptr<Entry> entry = pit.insert(*interest).first;
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000365 pit.erase(entry.get());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700366 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
367}
368
spirosmastorakisff920302016-05-26 18:09:31 +0000369BOOST_AUTO_TEST_CASE(EraseWithFullName)
370{
371 shared_ptr<Data> data = makeData("/test");
372 shared_ptr<Interest> interest = makeInterest(data->getFullName());
373
374 NameTree nameTree(16);
375 Pit pit(nameTree);
376
377 BOOST_CHECK_EQUAL(pit.size(), 0);
378
379 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
380 BOOST_CHECK_EQUAL(pit.size(), 1);
381 BOOST_CHECK(pit.find(*interest) != nullptr);
382
383 BOOST_CHECK_EQUAL(pit.insert(*interest).second, false);
384 BOOST_CHECK_EQUAL(pit.size(), 1);
385 shared_ptr<pit::Entry> pitEntry = pit.find(*interest);
386 BOOST_REQUIRE(pitEntry != nullptr);
387
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000388 pit.erase(pitEntry.get());
spirosmastorakisff920302016-05-26 18:09:31 +0000389 BOOST_CHECK_EQUAL(pit.size(), 0);
390 BOOST_CHECK(pit.find(*interest) == nullptr);
391
392 BOOST_CHECK_EQUAL(pit.insert(*interest).second, true);
393 BOOST_CHECK_EQUAL(pit.size(), 1);
394 BOOST_CHECK(pit.find(*interest) != nullptr);
395}
396
Junxiao Shicbba04c2014-01-26 14:21:22 -0700397BOOST_AUTO_TEST_CASE(FindAllDataMatches)
398{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600399 Name nameA ("ndn:/A");
400 Name nameAB ("ndn:/A/B");
401 Name nameABC ("ndn:/A/B/C");
402 Name nameABCD("ndn:/A/B/C/D");
403 Name nameD ("ndn:/D");
404
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700405 shared_ptr<Interest> interestA = makeInterest(nameA );
406 shared_ptr<Interest> interestABC = makeInterest(nameABC);
407 shared_ptr<Interest> interestD = makeInterest(nameD );
Junxiao Shicbba04c2014-01-26 14:21:22 -0700408
Haowei Yuan78c84d12014-02-27 15:35:13 -0600409 NameTree nameTree(16);
410 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600411 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700412
Haowei Yuan78c84d12014-02-27 15:35:13 -0600413 BOOST_CHECK_EQUAL(pit.size(), 0);
414
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700415 pit.insert(*interestA );
416 pit.insert(*interestABC);
417 pit.insert(*interestD );
Haowei Yuane1079fc2014-03-08 14:41:25 -0600418
419 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700420
Haowei Yuan78c84d12014-02-27 15:35:13 -0600421 BOOST_CHECK_EQUAL(pit.size(), 3);
422
Alexander Afanasyevc026d252014-06-16 11:14:15 -0700423 shared_ptr<Data> data = makeData(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700424
Junxiao Shi4846f372016-04-05 13:39:30 -0700425 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700426
Haowei Yuane1079fc2014-03-08 14:41:25 -0600427 bool hasA = false;
428 bool hasAB = false;
429 bool hasABC = false;
430 bool hasD = false;
431
Junxiao Shi4846f372016-04-05 13:39:30 -0700432 for (const shared_ptr<Entry>& entry : matches) {
Junxiao Shicbba04c2014-01-26 14:21:22 -0700433 ++count;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600434
435 if (entry->getName().equals(nameA ))
436 hasA = true;
437
438 if (entry->getName().equals(nameAB))
439 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700440
Haowei Yuane1079fc2014-03-08 14:41:25 -0600441 if (entry->getName().equals(nameABC))
442 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700443
Haowei Yuane1079fc2014-03-08 14:41:25 -0600444 if (entry->getName().equals(nameD))
445 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700446 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600447 BOOST_CHECK_EQUAL(hasA , true);
448 BOOST_CHECK_EQUAL(hasAB , false);
449 BOOST_CHECK_EQUAL(hasABC, true);
450 BOOST_CHECK_EQUAL(hasD , false);
451
Junxiao Shicbba04c2014-01-26 14:21:22 -0700452 BOOST_CHECK_EQUAL(count, 2);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700453}
Haowei Yuane1079fc2014-03-08 14:41:25 -0600454
Junxiao Shi4370fde2016-02-24 12:20:46 -0700455BOOST_AUTO_TEST_CASE(MatchFullName) // Bug 3363
456{
457 NameTree nameTree(16);
458 Pit pit(nameTree);
459
460 shared_ptr<Data> data = makeData("/A");
461 Name fullName = data->getFullName();
462 shared_ptr<Interest> interest = makeInterest(fullName);
463
464 pit.insert(*interest);
Junxiao Shi4846f372016-04-05 13:39:30 -0700465 DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi4370fde2016-02-24 12:20:46 -0700466
467 BOOST_REQUIRE_EQUAL(std::distance(matches.begin(), matches.end()), 1);
Junxiao Shi4846f372016-04-05 13:39:30 -0700468 shared_ptr<Entry> found = *matches.begin();
Junxiao Shi4370fde2016-02-24 12:20:46 -0700469 BOOST_CHECK_EQUAL(found->getName(), fullName);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700470}
471
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800472BOOST_AUTO_TEST_CASE(Iterator)
473{
474 NameTree nameTree(16);
475 Pit pit(nameTree);
476
477 shared_ptr<Interest> interestA = makeInterest("/A");
478 shared_ptr<Interest> interestABC1 = makeInterest("/A/B/C");
479 shared_ptr<Interest> interestABC2 = makeInterest("/A/B/C");
480 interestABC2->setSelectors(ndn::Selectors().setMinSuffixComponents(10));
481 shared_ptr<Interest> interestD = makeInterest("/D");
482
483 BOOST_CHECK_EQUAL(pit.size(), 0);
484 BOOST_CHECK(pit.begin() == pit.end());
485
486 pit.insert(*interestABC1);
487 BOOST_CHECK_EQUAL(pit.size(), 1);
488 BOOST_CHECK(pit.begin() != pit.end());
489 BOOST_CHECK(pit.begin()->getInterest() == *interestABC1);
490 BOOST_CHECK((*pit.begin()).getInterest() == *interestABC1);
491
492 auto i = pit.begin();
493 auto j = pit.begin();
494 BOOST_CHECK(++i == pit.end());
495 BOOST_CHECK(j++ == pit.begin());
496 BOOST_CHECK(j == pit.end());
497
498 pit.insert(*interestA);
499 pit.insert(*interestABC2);
500 pit.insert(*interestD);
501
502 std::set<const Interest*> expected = {&*interestA, &*interestABC1, &*interestABC2, &*interestD};
503 std::set<const Interest*> actual;
504 for (const auto& pitEntry : pit) {
505 actual.insert(&pitEntry.getInterest());
506 }
507 BOOST_CHECK(actual == expected);
508 for (auto actualIt = actual.begin(), expectedIt = expected.begin();
509 actualIt != actual.end() && expectedIt != expected.end(); ++actualIt, ++expectedIt) {
510 BOOST_CHECK_EQUAL(**actualIt, **expectedIt);
511 }
512}
513
Junxiao Shicbba04c2014-01-26 14:21:22 -0700514BOOST_AUTO_TEST_SUITE_END()
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700515BOOST_AUTO_TEST_SUITE_END()
Junxiao Shicbba04c2014-01-26 14:21:22 -0700516
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700517} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800518} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800519} // namespace nfd