blob: 7bd89fc28197995b328c1b4e981b43f5e9a14ea9 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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 {
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 Shid9ee45c2014-02-27 15:38:11 -070037BOOST_FIXTURE_TEST_SUITE(TablePit, BaseFixture)
Junxiao Shicbba04c2014-01-26 14:21:22 -070038
Junxiao Shid3c792f2014-01-30 00:46:13 -070039BOOST_AUTO_TEST_CASE(EntryInOutRecords)
Junxiao Shicbba04c2014-01-26 14:21:22 -070040{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070041 shared_ptr<Face> face1 = make_shared<DummyFace>();
42 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shicbba04c2014-01-26 14:21:22 -070043 Name name("ndn:/KuYfjtRq");
Junxiao Shif3c07812014-03-11 21:48:49 -070044 shared_ptr<Interest> interest = makeInterest(name);
45 shared_ptr<Interest> interest1 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070046 interest1->setInterestLifetime(time::milliseconds(2528));
Junxiao Shif3c07812014-03-11 21:48:49 -070047 interest1->setNonce(25559);
48 shared_ptr<Interest> interest2 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070049 interest2->setInterestLifetime(time::milliseconds(6464));
Junxiao Shif3c07812014-03-11 21:48:49 -070050 interest2->setNonce(19004);
51 shared_ptr<Interest> interest3 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070052 interest3->setInterestLifetime(time::milliseconds(3585));
Junxiao Shif3c07812014-03-11 21:48:49 -070053 interest3->setNonce(24216);
54 shared_ptr<Interest> interest4 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070055 interest4->setInterestLifetime(time::milliseconds(8795));
Junxiao Shif3c07812014-03-11 21:48:49 -070056 interest4->setNonce(17365);
Junxiao Shi57f0f312014-03-16 11:52:20 -070057
Junxiao Shif3c07812014-03-11 21:48:49 -070058 pit::Entry entry(*interest);
Junxiao Shi57f0f312014-03-16 11:52:20 -070059
Junxiao Shif3c07812014-03-11 21:48:49 -070060 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
61 BOOST_CHECK_EQUAL(entry.getName(), name);
Haowei Yuan78c84d12014-02-27 15:35:13 -060062
Junxiao Shicbba04c2014-01-26 14:21:22 -070063 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
64 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
65 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
66 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
Junxiao Shi57f0f312014-03-16 11:52:20 -070067
Junxiao Shicbba04c2014-01-26 14:21:22 -070068 // insert InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070069 time::steady_clock::TimePoint before1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070070 pit::InRecordCollection::iterator in1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070071 entry.insertOrUpdateInRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070072 time::steady_clock::TimePoint after1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070073 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
74 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
75 BOOST_CHECK(in1 == inRecords2.begin());
76 BOOST_CHECK_EQUAL(in1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070077 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070078 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
79 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070080 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed()
81 - interest1->getInterestLifetime(),
82 (after1 - before1));
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070083 BOOST_CHECK(in1 == entry.getInRecord(*face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -070084
Junxiao Shicbba04c2014-01-26 14:21:22 -070085 // insert OutRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070086 time::steady_clock::TimePoint before2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070087 pit::OutRecordCollection::iterator out1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070088 entry.insertOrUpdateOutRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070089 time::steady_clock::TimePoint after2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070090 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
91 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
92 BOOST_CHECK(out1 == outRecords2.begin());
93 BOOST_CHECK_EQUAL(out1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070094 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070095 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
96 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070097 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed()
98 - interest1->getInterestLifetime(),
99 (after2 - before2));
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700100 BOOST_CHECK(out1 == entry.getOutRecord(*face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -0700101
Junxiao Shicbba04c2014-01-26 14:21:22 -0700102 // update InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700103 time::steady_clock::TimePoint before3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700104 pit::InRecordCollection::iterator in2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700105 entry.insertOrUpdateInRecord(face1, *interest2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700106 time::steady_clock::TimePoint after3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700107 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
108 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
109 BOOST_CHECK(in2 == inRecords3.begin());
110 BOOST_CHECK_EQUAL(in2->getFace(), face1);
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 Shicbba04c2014-01-26 14:21:22 -0700116 // insert another InRecord
117 pit::InRecordCollection::iterator in3 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700118 entry.insertOrUpdateInRecord(face2, *interest3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700119 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
120 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
121 BOOST_CHECK_EQUAL(in3->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700122
Junxiao Shi66f91f82014-05-10 17:28:58 -0700123 // get InRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700124 pit::InRecordCollection::const_iterator in4 = entry.getInRecord(*face1);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700125 BOOST_REQUIRE(in4 != entry.getInRecords().end());
126 BOOST_CHECK_EQUAL(in4->getFace(), face1);
127
Junxiao Shicbba04c2014-01-26 14:21:22 -0700128 // delete all InRecords
129 entry.deleteInRecords();
130 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
131 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700132 BOOST_CHECK(entry.getInRecord(*face1) == entry.getInRecords().end());
Junxiao Shicbba04c2014-01-26 14:21:22 -0700133
Junxiao Shicbba04c2014-01-26 14:21:22 -0700134 // insert another OutRecord
135 pit::OutRecordCollection::iterator out2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700136 entry.insertOrUpdateOutRecord(face2, *interest4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700137 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
138 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
139 BOOST_CHECK_EQUAL(out2->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700140
Junxiao Shi66f91f82014-05-10 17:28:58 -0700141 // get OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700142 pit::OutRecordCollection::const_iterator out3 = entry.getOutRecord(*face1);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700143 BOOST_REQUIRE(out3 != entry.getOutRecords().end());
144 BOOST_CHECK_EQUAL(out3->getFace(), face1);
145
Junxiao Shicbba04c2014-01-26 14:21:22 -0700146 // delete OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700147 entry.deleteOutRecord(*face2);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700148 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
149 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
150 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700151 BOOST_CHECK(entry.getOutRecord(*face2) == entry.getOutRecords().end());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700152}
153
154BOOST_AUTO_TEST_CASE(EntryNonce)
155{
Junxiao Shia110f262014-10-12 12:35:20 -0700156 shared_ptr<Face> face1 = make_shared<DummyFace>();
157 shared_ptr<Face> face2 = make_shared<DummyFace>();
158
Junxiao Shi57f0f312014-03-16 11:52:20 -0700159 shared_ptr<Interest> interest = makeInterest("ndn:/qtCQ7I1c");
Junxiao Shia110f262014-10-12 12:35:20 -0700160 interest->setNonce(25559);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700161
Junxiao Shia110f262014-10-12 12:35:20 -0700162 pit::Entry entry0(*interest);
163 BOOST_CHECK_EQUAL(entry0.findNonce(25559, *face1), pit::DUPLICATE_NONCE_NONE);
164 BOOST_CHECK_EQUAL(entry0.findNonce(25559, *face2), pit::DUPLICATE_NONCE_NONE);
165 BOOST_CHECK_EQUAL(entry0.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
166 BOOST_CHECK_EQUAL(entry0.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700167
Junxiao Shia110f262014-10-12 12:35:20 -0700168 pit::Entry entry1(*interest);
169 entry1.insertOrUpdateInRecord(face1, *interest);
170 BOOST_CHECK_EQUAL(entry1.findNonce(25559, *face1), pit::DUPLICATE_NONCE_IN_SAME);
171 BOOST_CHECK_EQUAL(entry1.findNonce(25559, *face2), pit::DUPLICATE_NONCE_IN_OTHER);
172 BOOST_CHECK_EQUAL(entry1.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
173 BOOST_CHECK_EQUAL(entry1.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
174
175 pit::Entry entry2(*interest);
176 entry2.insertOrUpdateOutRecord(face1, *interest);
177 BOOST_CHECK_EQUAL(entry2.findNonce(25559, *face1), pit::DUPLICATE_NONCE_OUT_SAME);
178 BOOST_CHECK_EQUAL(entry2.findNonce(25559, *face2), pit::DUPLICATE_NONCE_OUT_OTHER);
179 BOOST_CHECK_EQUAL(entry2.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
180 BOOST_CHECK_EQUAL(entry2.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
181
182 pit::Entry entry3(*interest);
183 entry3.insertOrUpdateInRecord(face1, *interest);
184 entry3.insertOrUpdateOutRecord(face1, *interest);
185 BOOST_CHECK_EQUAL(entry3.findNonce(25559, *face1),
186 pit::DUPLICATE_NONCE_IN_SAME | pit::DUPLICATE_NONCE_OUT_SAME);
187 BOOST_CHECK_EQUAL(entry3.findNonce(25559, *face2),
188 pit::DUPLICATE_NONCE_IN_OTHER | pit::DUPLICATE_NONCE_OUT_OTHER);
189 BOOST_CHECK_EQUAL(entry3.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
190 BOOST_CHECK_EQUAL(entry3.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
191
192 pit::Entry entry4(*interest);
193 entry4.insertOrUpdateInRecord(face1, *interest);
194 entry4.insertOrUpdateInRecord(face2, *interest);
195 BOOST_CHECK_EQUAL(entry4.findNonce(25559, *face1),
196 pit::DUPLICATE_NONCE_IN_SAME | pit::DUPLICATE_NONCE_IN_OTHER);
197 BOOST_CHECK_EQUAL(entry4.findNonce(25559, *face2),
198 pit::DUPLICATE_NONCE_IN_SAME | pit::DUPLICATE_NONCE_IN_OTHER);
199 BOOST_CHECK_EQUAL(entry4.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
200 BOOST_CHECK_EQUAL(entry4.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
201
202 pit::Entry entry5(*interest);
203 entry5.insertOrUpdateOutRecord(face1, *interest);
204 entry5.insertOrUpdateOutRecord(face2, *interest);
205 BOOST_CHECK_EQUAL(entry5.findNonce(25559, *face1),
206 pit::DUPLICATE_NONCE_OUT_SAME | pit::DUPLICATE_NONCE_OUT_OTHER);
207 BOOST_CHECK_EQUAL(entry5.findNonce(25559, *face2),
208 pit::DUPLICATE_NONCE_OUT_SAME | pit::DUPLICATE_NONCE_OUT_OTHER);
209 BOOST_CHECK_EQUAL(entry5.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
210 BOOST_CHECK_EQUAL(entry5.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700211}
212
Junxiao Shi57f0f312014-03-16 11:52:20 -0700213BOOST_AUTO_TEST_CASE(EntryLifetime)
214{
215 shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
Junxiao Shia110f262014-10-12 12:35:20 -0700216 // library uses -1 to indicate unset lifetime
217 BOOST_ASSERT(interest->getInterestLifetime() < time::milliseconds::zero());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700218
219 shared_ptr<Face> face = make_shared<DummyFace>();
220 pit::Entry entry(*interest);
221
222 pit::InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700223 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700224
225 pit::OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700226 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700227}
228
229BOOST_AUTO_TEST_CASE(EntryCanForwardTo)
230{
231 shared_ptr<Interest> interest = makeInterest("ndn:/WDsuBLIMG");
232 pit::Entry entry(*interest);
233
234 shared_ptr<Face> face1 = make_shared<DummyFace>();
235 shared_ptr<Face> face2 = make_shared<DummyFace>();
236
237 entry.insertOrUpdateInRecord(face1, *interest);
238 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
239 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
240
241 entry.insertOrUpdateInRecord(face2, *interest);
242 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), true);
243 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
244
245 entry.insertOrUpdateOutRecord(face1, *interest);
246 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
247 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
248}
249
Junxiao Shicbba04c2014-01-26 14:21:22 -0700250BOOST_AUTO_TEST_CASE(Insert)
251{
252 Name name1("ndn:/5vzBNnMst");
253 Name name2("ndn:/igSGfEIM62");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700254 Exclude exclude1;
255 exclude1.excludeOne(Name::Component("u26p47oep"));
256 Exclude exclude2;
Junxiao Shi30d35992014-04-03 14:51:58 -0700257 exclude2.excludeBefore(Name::Component("u26p47oep"));
258 ndn::KeyLocator keyLocator1("ndn:/sGAE3peMHA");
259 ndn::KeyLocator keyLocator2("ndn:/nIJH6pr4");
Junxiao Shi57f0f312014-03-16 11:52:20 -0700260
Haowei Yuan78c84d12014-02-27 15:35:13 -0600261 NameTree nameTree(16);
262 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -0700263 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700264 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700265
Junxiao Shi30d35992014-04-03 14:51:58 -0700266 // base
267 shared_ptr<Interest> interestA = make_shared<Interest>(name1);
268 insertResult = pit.insert(*interestA);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700269 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600270 BOOST_CHECK_EQUAL(pit.size(), 1);
271
Junxiao Shi30d35992014-04-03 14:51:58 -0700272 // A+MinSuffixComponents
273 shared_ptr<Interest> interestB = make_shared<Interest>(*interestA);
274 interestB->setMinSuffixComponents(2);
275 insertResult = pit.insert(*interestB);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700276 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600277 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700278
Junxiao Shi30d35992014-04-03 14:51:58 -0700279 // A+MaxSuffixComponents
280 shared_ptr<Interest> interestC = make_shared<Interest>(*interestA);
281 interestC->setMaxSuffixComponents(4);
282 insertResult = pit.insert(*interestC);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700283 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600284 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700285
Junxiao Shi30d35992014-04-03 14:51:58 -0700286 // A+KeyLocator1
287 shared_ptr<Interest> interestD = make_shared<Interest>(*interestA);
288 interestD->setPublisherPublicKeyLocator(keyLocator1);
289 insertResult = pit.insert(*interestD);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700290 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600291 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700292
Junxiao Shi30d35992014-04-03 14:51:58 -0700293 // A+KeyLocator2
294 shared_ptr<Interest> interestE = make_shared<Interest>(*interestA);
295 interestE->setPublisherPublicKeyLocator(keyLocator2);
296 insertResult = pit.insert(*interestE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700297 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600298 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700299
Junxiao Shi30d35992014-04-03 14:51:58 -0700300 // A+Exclude1
301 shared_ptr<Interest> interestF = make_shared<Interest>(*interestA);
302 interestF->setExclude(exclude1);
303 insertResult = pit.insert(*interestF);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700304 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600305 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700306
Junxiao Shi30d35992014-04-03 14:51:58 -0700307 // A+Exclude2
308 shared_ptr<Interest> interestG = make_shared<Interest>(*interestA);
309 interestG->setExclude(exclude2);
310 insertResult = pit.insert(*interestG);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700311 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600312 BOOST_CHECK_EQUAL(pit.size(), 7);
313
Junxiao Shi30d35992014-04-03 14:51:58 -0700314 // A+ChildSelector0
315 shared_ptr<Interest> interestH = make_shared<Interest>(*interestA);
316 interestH->setChildSelector(0);
317 insertResult = pit.insert(*interestH);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700318 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600319 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shi30d35992014-04-03 14:51:58 -0700320
321 // A+ChildSelector1
322 shared_ptr<Interest> interestI = make_shared<Interest>(*interestA);
323 interestI->setChildSelector(1);
324 insertResult = pit.insert(*interestI);
325 BOOST_CHECK_EQUAL(insertResult.second, true);
326 BOOST_CHECK_EQUAL(pit.size(), 9);
327
328 // A+MustBeFresh
329 shared_ptr<Interest> interestJ = make_shared<Interest>(*interestA);
330 interestJ->setMustBeFresh(true);
331 insertResult = pit.insert(*interestJ);
332 BOOST_CHECK_EQUAL(insertResult.second, true);
333 BOOST_CHECK_EQUAL(pit.size(), 10);
334
335 // A+InterestLifetime
336 shared_ptr<Interest> interestK = make_shared<Interest>(*interestA);
337 interestK->setInterestLifetime(time::milliseconds(1000));
338 insertResult = pit.insert(*interestK);
339 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
340 BOOST_CHECK_EQUAL(pit.size(), 10);
341
342 // A+Nonce
343 shared_ptr<Interest> interestL = make_shared<Interest>(*interestA);
344 interestL->setNonce(2192);
345 insertResult = pit.insert(*interestL);
346 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
347 BOOST_CHECK_EQUAL(pit.size(), 10);
348
349 // different Name+Exclude1
350 shared_ptr<Interest> interestM = make_shared<Interest>(name2);
351 interestM->setExclude(exclude1);
352 insertResult = pit.insert(*interestM);
353 BOOST_CHECK_EQUAL(insertResult.second, true);
354 BOOST_CHECK_EQUAL(pit.size(), 11);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700355}
356
Haowei Yuan78c84d12014-02-27 15:35:13 -0600357BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700358{
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700359 shared_ptr<Interest> interest = makeInterest("/z88Admz6A2");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700360
Haowei Yuan78c84d12014-02-27 15:35:13 -0600361 NameTree nameTree(16);
362 Pit pit(nameTree);
363
Junxiao Shicbba04c2014-01-26 14:21:22 -0700364 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700365
Haowei Yuan78c84d12014-02-27 15:35:13 -0600366 BOOST_CHECK_EQUAL(pit.size(), 0);
367
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700368 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700369 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600370 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700371
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700372 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700373 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600374 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700375
Haowei Yuan78c84d12014-02-27 15:35:13 -0600376 pit.erase(insertResult.first);
377 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700378
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700379 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700380 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600381 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700382
Haowei Yuan78c84d12014-02-27 15:35:13 -0600383}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700384
Junxiao Shiee5a4442014-07-27 17:13:43 -0700385BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
386{
387 NameTree nameTree;
388 Pit pit(nameTree);
389 size_t nNameTreeEntriesBefore = nameTree.size();
390
391 shared_ptr<Interest> interest = makeInterest("/37xWVvQ2K");
392 shared_ptr<pit::Entry> entry = pit.insert(*interest).first;
393 pit.erase(entry);
394 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
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 Shib2bcbcd2014-11-08 09:30:28 -0700425 pit::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 Shib2bcbcd2014-11-08 09:30:28 -0700432 for (const shared_ptr<pit::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);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600453
Junxiao Shicbba04c2014-01-26 14:21:22 -0700454}
455
Alexander Afanasyev750fa1c2015-01-03 17:28:31 -0800456BOOST_AUTO_TEST_CASE(Iterator)
457{
458 NameTree nameTree(16);
459 Pit pit(nameTree);
460
461 shared_ptr<Interest> interestA = makeInterest("/A");
462 shared_ptr<Interest> interestABC1 = makeInterest("/A/B/C");
463 shared_ptr<Interest> interestABC2 = makeInterest("/A/B/C");
464 interestABC2->setSelectors(ndn::Selectors().setMinSuffixComponents(10));
465 shared_ptr<Interest> interestD = makeInterest("/D");
466
467 BOOST_CHECK_EQUAL(pit.size(), 0);
468 BOOST_CHECK(pit.begin() == pit.end());
469
470 pit.insert(*interestABC1);
471 BOOST_CHECK_EQUAL(pit.size(), 1);
472 BOOST_CHECK(pit.begin() != pit.end());
473 BOOST_CHECK(pit.begin()->getInterest() == *interestABC1);
474 BOOST_CHECK((*pit.begin()).getInterest() == *interestABC1);
475
476 auto i = pit.begin();
477 auto j = pit.begin();
478 BOOST_CHECK(++i == pit.end());
479 BOOST_CHECK(j++ == pit.begin());
480 BOOST_CHECK(j == pit.end());
481
482 pit.insert(*interestA);
483 pit.insert(*interestABC2);
484 pit.insert(*interestD);
485
486 std::set<const Interest*> expected = {&*interestA, &*interestABC1, &*interestABC2, &*interestD};
487 std::set<const Interest*> actual;
488 for (const auto& pitEntry : pit) {
489 actual.insert(&pitEntry.getInterest());
490 }
491 BOOST_CHECK(actual == expected);
492 for (auto actualIt = actual.begin(), expectedIt = expected.begin();
493 actualIt != actual.end() && expectedIt != expected.end(); ++actualIt, ++expectedIt) {
494 BOOST_CHECK_EQUAL(**actualIt, **expectedIt);
495 }
496}
497
Junxiao Shicbba04c2014-01-26 14:21:22 -0700498BOOST_AUTO_TEST_SUITE_END()
499
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700500} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800501} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800502} // namespace nfd