blob: 566421ff5d8edc80f282fc067db3557eb81b6a44 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevc026d252014-06-16 11:14:15 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * 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 {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Junxiao Shicbba04c2014-01-26 14:21:22 -070033
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034BOOST_FIXTURE_TEST_SUITE(TablePit, BaseFixture)
Junxiao Shicbba04c2014-01-26 14:21:22 -070035
Junxiao Shi651b75e2014-07-17 20:15:09 -070036BOOST_AUTO_TEST_CASE(NonceList)
37{
38 BOOST_REQUIRE_GE(pit::NonceList::CAPACITY, 32);
39 BOOST_REQUIRE_LE(pit::NonceList::CAPACITY, 4096);
40
41 pit::NonceList nl;
42 for (uint32_t nonce = 0; nonce < static_cast<uint32_t>(pit::NonceList::CAPACITY); ++nonce) {
43 BOOST_CHECK_EQUAL(nl.add(nonce), true);
44 }
45 BOOST_CHECK_EQUAL(nl.size(), pit::NonceList::CAPACITY);
46
47 BOOST_CHECK_EQUAL(nl.add(32), false);
48 BOOST_CHECK_EQUAL(nl.size(), pit::NonceList::CAPACITY);
49
50 BOOST_CHECK_EQUAL(nl.add(4096), true);
51 BOOST_CHECK_EQUAL(nl.size(), pit::NonceList::CAPACITY);
52
53 BOOST_CHECK_EQUAL(nl.add(0), true);// 0 is evicted
54 BOOST_CHECK_EQUAL(nl.size(), pit::NonceList::CAPACITY);
55}
56
Junxiao Shid3c792f2014-01-30 00:46:13 -070057BOOST_AUTO_TEST_CASE(EntryInOutRecords)
Junxiao Shicbba04c2014-01-26 14:21:22 -070058{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070059 shared_ptr<Face> face1 = make_shared<DummyFace>();
60 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shicbba04c2014-01-26 14:21:22 -070061 Name name("ndn:/KuYfjtRq");
Junxiao Shif3c07812014-03-11 21:48:49 -070062 shared_ptr<Interest> interest = makeInterest(name);
63 shared_ptr<Interest> interest1 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070064 interest1->setInterestLifetime(time::milliseconds(2528));
Junxiao Shif3c07812014-03-11 21:48:49 -070065 interest1->setNonce(25559);
66 shared_ptr<Interest> interest2 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070067 interest2->setInterestLifetime(time::milliseconds(6464));
Junxiao Shif3c07812014-03-11 21:48:49 -070068 interest2->setNonce(19004);
69 shared_ptr<Interest> interest3 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070070 interest3->setInterestLifetime(time::milliseconds(3585));
Junxiao Shif3c07812014-03-11 21:48:49 -070071 interest3->setNonce(24216);
72 shared_ptr<Interest> interest4 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070073 interest4->setInterestLifetime(time::milliseconds(8795));
Junxiao Shif3c07812014-03-11 21:48:49 -070074 interest4->setNonce(17365);
Junxiao Shi57f0f312014-03-16 11:52:20 -070075
Junxiao Shif3c07812014-03-11 21:48:49 -070076 pit::Entry entry(*interest);
Junxiao Shi57f0f312014-03-16 11:52:20 -070077
Junxiao Shif3c07812014-03-11 21:48:49 -070078 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
79 BOOST_CHECK_EQUAL(entry.getName(), name);
Haowei Yuan78c84d12014-02-27 15:35:13 -060080
Junxiao Shicbba04c2014-01-26 14:21:22 -070081 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
82 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
83 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
84 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
Junxiao Shi57f0f312014-03-16 11:52:20 -070085
Junxiao Shicbba04c2014-01-26 14:21:22 -070086 // insert InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070087 time::steady_clock::TimePoint before1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070088 pit::InRecordCollection::iterator in1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070089 entry.insertOrUpdateInRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070090 time::steady_clock::TimePoint after1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070091 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
92 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
93 BOOST_CHECK(in1 == inRecords2.begin());
94 BOOST_CHECK_EQUAL(in1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070095 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070096 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
97 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070098 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed()
99 - interest1->getInterestLifetime(),
100 (after1 - before1));
Junxiao Shi66f91f82014-05-10 17:28:58 -0700101 BOOST_CHECK(in1 == entry.getInRecord(face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -0700102
Junxiao Shicbba04c2014-01-26 14:21:22 -0700103 // insert OutRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700104 time::steady_clock::TimePoint before2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700105 pit::OutRecordCollection::iterator out1 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700106 entry.insertOrUpdateOutRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700107 time::steady_clock::TimePoint after2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700108 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
109 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
110 BOOST_CHECK(out1 == outRecords2.begin());
111 BOOST_CHECK_EQUAL(out1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700112 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -0700113 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
114 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700115 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed()
116 - interest1->getInterestLifetime(),
117 (after2 - before2));
Junxiao Shi66f91f82014-05-10 17:28:58 -0700118 BOOST_CHECK(out1 == entry.getOutRecord(face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -0700119
Junxiao Shicbba04c2014-01-26 14:21:22 -0700120 // update InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700121 time::steady_clock::TimePoint before3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700122 pit::InRecordCollection::iterator in2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700123 entry.insertOrUpdateInRecord(face1, *interest2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700124 time::steady_clock::TimePoint after3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700125 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
126 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
127 BOOST_CHECK(in2 == inRecords3.begin());
128 BOOST_CHECK_EQUAL(in2->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700129 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700130 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed()
131 - interest2->getInterestLifetime(),
132 (after3 - before3));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700133
Junxiao Shicbba04c2014-01-26 14:21:22 -0700134 // insert another InRecord
135 pit::InRecordCollection::iterator in3 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700136 entry.insertOrUpdateInRecord(face2, *interest3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700137 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
138 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
139 BOOST_CHECK_EQUAL(in3->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700140
Junxiao Shi66f91f82014-05-10 17:28:58 -0700141 // get InRecord
142 pit::InRecordCollection::const_iterator in4 = entry.getInRecord(face1);
143 BOOST_REQUIRE(in4 != entry.getInRecords().end());
144 BOOST_CHECK_EQUAL(in4->getFace(), face1);
145
Junxiao Shicbba04c2014-01-26 14:21:22 -0700146 // delete all InRecords
147 entry.deleteInRecords();
148 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
149 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700150 BOOST_CHECK(entry.getInRecord(face1) == entry.getInRecords().end());
Junxiao Shicbba04c2014-01-26 14:21:22 -0700151
Junxiao Shicbba04c2014-01-26 14:21:22 -0700152 // insert another OutRecord
153 pit::OutRecordCollection::iterator out2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700154 entry.insertOrUpdateOutRecord(face2, *interest4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700155 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
156 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
157 BOOST_CHECK_EQUAL(out2->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700158
Junxiao Shi66f91f82014-05-10 17:28:58 -0700159 // get OutRecord
160 pit::OutRecordCollection::const_iterator out3 = entry.getOutRecord(face1);
161 BOOST_REQUIRE(out3 != entry.getOutRecords().end());
162 BOOST_CHECK_EQUAL(out3->getFace(), face1);
163
Junxiao Shicbba04c2014-01-26 14:21:22 -0700164 // delete OutRecord
165 entry.deleteOutRecord(face2);
166 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
167 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
168 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700169 BOOST_CHECK(entry.getOutRecord(face2) == entry.getOutRecords().end());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700170}
171
172BOOST_AUTO_TEST_CASE(EntryNonce)
173{
Junxiao Shi57f0f312014-03-16 11:52:20 -0700174 shared_ptr<Interest> interest = makeInterest("ndn:/qtCQ7I1c");
175
176 pit::Entry entry(*interest);
177
Junxiao Shid3c792f2014-01-30 00:46:13 -0700178 BOOST_CHECK_EQUAL(entry.addNonce(25559), true);
179 BOOST_CHECK_EQUAL(entry.addNonce(25559), false);
180 BOOST_CHECK_EQUAL(entry.addNonce(19004), true);
181 BOOST_CHECK_EQUAL(entry.addNonce(19004), false);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700182}
183
Junxiao Shi57f0f312014-03-16 11:52:20 -0700184BOOST_AUTO_TEST_CASE(EntryLifetime)
185{
186 shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700187 BOOST_ASSERT(interest->getInterestLifetime() < time::milliseconds::zero()); // library uses -1 to indicate unset lifetime
Junxiao Shi57f0f312014-03-16 11:52:20 -0700188
189 shared_ptr<Face> face = make_shared<DummyFace>();
190 pit::Entry entry(*interest);
191
192 pit::InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700193 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700194
195 pit::OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700196 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700197}
198
199BOOST_AUTO_TEST_CASE(EntryCanForwardTo)
200{
201 shared_ptr<Interest> interest = makeInterest("ndn:/WDsuBLIMG");
202 pit::Entry entry(*interest);
203
204 shared_ptr<Face> face1 = make_shared<DummyFace>();
205 shared_ptr<Face> face2 = make_shared<DummyFace>();
206
207 entry.insertOrUpdateInRecord(face1, *interest);
208 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
209 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
210
211 entry.insertOrUpdateInRecord(face2, *interest);
212 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), true);
213 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
214
215 entry.insertOrUpdateOutRecord(face1, *interest);
216 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
217 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
218}
219
Junxiao Shicbba04c2014-01-26 14:21:22 -0700220BOOST_AUTO_TEST_CASE(Insert)
221{
222 Name name1("ndn:/5vzBNnMst");
223 Name name2("ndn:/igSGfEIM62");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700224 Exclude exclude1;
225 exclude1.excludeOne(Name::Component("u26p47oep"));
226 Exclude exclude2;
Junxiao Shi30d35992014-04-03 14:51:58 -0700227 exclude2.excludeBefore(Name::Component("u26p47oep"));
228 ndn::KeyLocator keyLocator1("ndn:/sGAE3peMHA");
229 ndn::KeyLocator keyLocator2("ndn:/nIJH6pr4");
Junxiao Shi57f0f312014-03-16 11:52:20 -0700230
Haowei Yuan78c84d12014-02-27 15:35:13 -0600231 NameTree nameTree(16);
232 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -0700233 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700234 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700235
Junxiao Shi30d35992014-04-03 14:51:58 -0700236 // base
237 shared_ptr<Interest> interestA = make_shared<Interest>(name1);
238 insertResult = pit.insert(*interestA);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700239 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600240 BOOST_CHECK_EQUAL(pit.size(), 1);
241
Junxiao Shi30d35992014-04-03 14:51:58 -0700242 // A+MinSuffixComponents
243 shared_ptr<Interest> interestB = make_shared<Interest>(*interestA);
244 interestB->setMinSuffixComponents(2);
245 insertResult = pit.insert(*interestB);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700246 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600247 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700248
Junxiao Shi30d35992014-04-03 14:51:58 -0700249 // A+MaxSuffixComponents
250 shared_ptr<Interest> interestC = make_shared<Interest>(*interestA);
251 interestC->setMaxSuffixComponents(4);
252 insertResult = pit.insert(*interestC);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700253 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600254 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700255
Junxiao Shi30d35992014-04-03 14:51:58 -0700256 // A+KeyLocator1
257 shared_ptr<Interest> interestD = make_shared<Interest>(*interestA);
258 interestD->setPublisherPublicKeyLocator(keyLocator1);
259 insertResult = pit.insert(*interestD);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700260 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600261 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700262
Junxiao Shi30d35992014-04-03 14:51:58 -0700263 // A+KeyLocator2
264 shared_ptr<Interest> interestE = make_shared<Interest>(*interestA);
265 interestE->setPublisherPublicKeyLocator(keyLocator2);
266 insertResult = pit.insert(*interestE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700267 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600268 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700269
Junxiao Shi30d35992014-04-03 14:51:58 -0700270 // A+Exclude1
271 shared_ptr<Interest> interestF = make_shared<Interest>(*interestA);
272 interestF->setExclude(exclude1);
273 insertResult = pit.insert(*interestF);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700274 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600275 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700276
Junxiao Shi30d35992014-04-03 14:51:58 -0700277 // A+Exclude2
278 shared_ptr<Interest> interestG = make_shared<Interest>(*interestA);
279 interestG->setExclude(exclude2);
280 insertResult = pit.insert(*interestG);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700281 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600282 BOOST_CHECK_EQUAL(pit.size(), 7);
283
Junxiao Shi30d35992014-04-03 14:51:58 -0700284 // A+ChildSelector0
285 shared_ptr<Interest> interestH = make_shared<Interest>(*interestA);
286 interestH->setChildSelector(0);
287 insertResult = pit.insert(*interestH);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700288 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600289 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shi30d35992014-04-03 14:51:58 -0700290
291 // A+ChildSelector1
292 shared_ptr<Interest> interestI = make_shared<Interest>(*interestA);
293 interestI->setChildSelector(1);
294 insertResult = pit.insert(*interestI);
295 BOOST_CHECK_EQUAL(insertResult.second, true);
296 BOOST_CHECK_EQUAL(pit.size(), 9);
297
298 // A+MustBeFresh
299 shared_ptr<Interest> interestJ = make_shared<Interest>(*interestA);
300 interestJ->setMustBeFresh(true);
301 insertResult = pit.insert(*interestJ);
302 BOOST_CHECK_EQUAL(insertResult.second, true);
303 BOOST_CHECK_EQUAL(pit.size(), 10);
304
305 // A+InterestLifetime
306 shared_ptr<Interest> interestK = make_shared<Interest>(*interestA);
307 interestK->setInterestLifetime(time::milliseconds(1000));
308 insertResult = pit.insert(*interestK);
309 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
310 BOOST_CHECK_EQUAL(pit.size(), 10);
311
312 // A+Nonce
313 shared_ptr<Interest> interestL = make_shared<Interest>(*interestA);
314 interestL->setNonce(2192);
315 insertResult = pit.insert(*interestL);
316 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
317 BOOST_CHECK_EQUAL(pit.size(), 10);
318
319 // different Name+Exclude1
320 shared_ptr<Interest> interestM = make_shared<Interest>(name2);
321 interestM->setExclude(exclude1);
322 insertResult = pit.insert(*interestM);
323 BOOST_CHECK_EQUAL(insertResult.second, true);
324 BOOST_CHECK_EQUAL(pit.size(), 11);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700325}
326
Haowei Yuan78c84d12014-02-27 15:35:13 -0600327BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700328{
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700329 shared_ptr<Interest> interest = makeInterest("/z88Admz6A2");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700330
Haowei Yuan78c84d12014-02-27 15:35:13 -0600331 NameTree nameTree(16);
332 Pit pit(nameTree);
333
Junxiao Shicbba04c2014-01-26 14:21:22 -0700334 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700335
Haowei Yuan78c84d12014-02-27 15:35:13 -0600336 BOOST_CHECK_EQUAL(pit.size(), 0);
337
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700338 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700339 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600340 BOOST_CHECK_EQUAL(pit.size(), 1);
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 Shi57f0f312014-03-16 11:52:20 -0700345
Haowei Yuan78c84d12014-02-27 15:35:13 -0600346 pit.erase(insertResult.first);
347 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700348
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700349 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700350 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600351 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700352
Haowei Yuan78c84d12014-02-27 15:35:13 -0600353}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700354
Junxiao Shiee5a4442014-07-27 17:13:43 -0700355BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
356{
357 NameTree nameTree;
358 Pit pit(nameTree);
359 size_t nNameTreeEntriesBefore = nameTree.size();
360
361 shared_ptr<Interest> interest = makeInterest("/37xWVvQ2K");
362 shared_ptr<pit::Entry> entry = pit.insert(*interest).first;
363 pit.erase(entry);
364 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
365}
366
Junxiao Shicbba04c2014-01-26 14:21:22 -0700367BOOST_AUTO_TEST_CASE(FindAllDataMatches)
368{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600369 Name nameA ("ndn:/A");
370 Name nameAB ("ndn:/A/B");
371 Name nameABC ("ndn:/A/B/C");
372 Name nameABCD("ndn:/A/B/C/D");
373 Name nameD ("ndn:/D");
374
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700375 shared_ptr<Interest> interestA = makeInterest(nameA );
376 shared_ptr<Interest> interestABC = makeInterest(nameABC);
377 shared_ptr<Interest> interestD = makeInterest(nameD );
Junxiao Shicbba04c2014-01-26 14:21:22 -0700378
Haowei Yuan78c84d12014-02-27 15:35:13 -0600379 NameTree nameTree(16);
380 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600381 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700382
Haowei Yuan78c84d12014-02-27 15:35:13 -0600383 BOOST_CHECK_EQUAL(pit.size(), 0);
384
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700385 pit.insert(*interestA );
386 pit.insert(*interestABC);
387 pit.insert(*interestD );
Haowei Yuane1079fc2014-03-08 14:41:25 -0600388
389 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700390
Haowei Yuan78c84d12014-02-27 15:35:13 -0600391 BOOST_CHECK_EQUAL(pit.size(), 3);
392
Alexander Afanasyevc026d252014-06-16 11:14:15 -0700393 shared_ptr<Data> data = makeData(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700394
Alexander Afanasyevc026d252014-06-16 11:14:15 -0700395 shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(*data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700396
Haowei Yuane1079fc2014-03-08 14:41:25 -0600397 bool hasA = false;
398 bool hasAB = false;
399 bool hasABC = false;
400 bool hasD = false;
401
Junxiao Shicbba04c2014-01-26 14:21:22 -0700402 for (pit::DataMatchResult::iterator it = matches->begin();
403 it != matches->end(); ++it) {
404 ++count;
405 shared_ptr<pit::Entry> entry = *it;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600406
407 if (entry->getName().equals(nameA ))
408 hasA = true;
409
410 if (entry->getName().equals(nameAB))
411 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700412
Haowei Yuane1079fc2014-03-08 14:41:25 -0600413 if (entry->getName().equals(nameABC))
414 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700415
Haowei Yuane1079fc2014-03-08 14:41:25 -0600416 if (entry->getName().equals(nameD))
417 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700418 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600419 BOOST_CHECK_EQUAL(hasA , true);
420 BOOST_CHECK_EQUAL(hasAB , false);
421 BOOST_CHECK_EQUAL(hasABC, true);
422 BOOST_CHECK_EQUAL(hasD , false);
423
Junxiao Shicbba04c2014-01-26 14:21:22 -0700424 BOOST_CHECK_EQUAL(count, 2);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600425
Junxiao Shicbba04c2014-01-26 14:21:22 -0700426}
427
428BOOST_AUTO_TEST_SUITE_END()
429
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700430} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800431} // namespace nfd