blob: dcf5053c13122ac572b9e5a95c54835cca42ace5 [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 Shid3c792f2014-01-30 00:46:13 -070036BOOST_AUTO_TEST_CASE(EntryInOutRecords)
Junxiao Shicbba04c2014-01-26 14:21:22 -070037{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070038 shared_ptr<Face> face1 = make_shared<DummyFace>();
39 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shicbba04c2014-01-26 14:21:22 -070040 Name name("ndn:/KuYfjtRq");
Junxiao Shif3c07812014-03-11 21:48:49 -070041 shared_ptr<Interest> interest = makeInterest(name);
42 shared_ptr<Interest> interest1 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070043 interest1->setInterestLifetime(time::milliseconds(2528));
Junxiao Shif3c07812014-03-11 21:48:49 -070044 interest1->setNonce(25559);
45 shared_ptr<Interest> interest2 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070046 interest2->setInterestLifetime(time::milliseconds(6464));
Junxiao Shif3c07812014-03-11 21:48:49 -070047 interest2->setNonce(19004);
48 shared_ptr<Interest> interest3 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070049 interest3->setInterestLifetime(time::milliseconds(3585));
Junxiao Shif3c07812014-03-11 21:48:49 -070050 interest3->setNonce(24216);
51 shared_ptr<Interest> interest4 = makeInterest(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070052 interest4->setInterestLifetime(time::milliseconds(8795));
Junxiao Shif3c07812014-03-11 21:48:49 -070053 interest4->setNonce(17365);
Junxiao Shi57f0f312014-03-16 11:52:20 -070054
Junxiao Shif3c07812014-03-11 21:48:49 -070055 pit::Entry entry(*interest);
Junxiao Shi57f0f312014-03-16 11:52:20 -070056
Junxiao Shif3c07812014-03-11 21:48:49 -070057 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
58 BOOST_CHECK_EQUAL(entry.getName(), name);
Haowei Yuan78c84d12014-02-27 15:35:13 -060059
Junxiao Shicbba04c2014-01-26 14:21:22 -070060 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
61 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
62 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
63 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
Junxiao Shi57f0f312014-03-16 11:52:20 -070064
Junxiao Shicbba04c2014-01-26 14:21:22 -070065 // insert InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070066 time::steady_clock::TimePoint before1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070067 pit::InRecordCollection::iterator in1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070068 entry.insertOrUpdateInRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070069 time::steady_clock::TimePoint after1 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070070 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
71 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
72 BOOST_CHECK(in1 == inRecords2.begin());
73 BOOST_CHECK_EQUAL(in1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070074 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070075 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
76 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070077 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed()
78 - interest1->getInterestLifetime(),
79 (after1 - before1));
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070080 BOOST_CHECK(in1 == entry.getInRecord(*face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -070081
Junxiao Shicbba04c2014-01-26 14:21:22 -070082 // insert OutRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070083 time::steady_clock::TimePoint before2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070084 pit::OutRecordCollection::iterator out1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070085 entry.insertOrUpdateOutRecord(face1, *interest1);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070086 time::steady_clock::TimePoint after2 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -070087 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
88 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
89 BOOST_CHECK(out1 == outRecords2.begin());
90 BOOST_CHECK_EQUAL(out1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070091 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070092 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
93 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070094 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed()
95 - interest1->getInterestLifetime(),
96 (after2 - before2));
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070097 BOOST_CHECK(out1 == entry.getOutRecord(*face1));
Junxiao Shi57f0f312014-03-16 11:52:20 -070098
Junxiao Shicbba04c2014-01-26 14:21:22 -070099 // update InRecord
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700100 time::steady_clock::TimePoint before3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700101 pit::InRecordCollection::iterator in2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700102 entry.insertOrUpdateInRecord(face1, *interest2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700103 time::steady_clock::TimePoint after3 = time::steady_clock::now();
Junxiao Shicbba04c2014-01-26 14:21:22 -0700104 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
105 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
106 BOOST_CHECK(in2 == inRecords3.begin());
107 BOOST_CHECK_EQUAL(in2->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700108 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700109 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed()
110 - interest2->getInterestLifetime(),
111 (after3 - before3));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700112
Junxiao Shicbba04c2014-01-26 14:21:22 -0700113 // insert another InRecord
114 pit::InRecordCollection::iterator in3 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700115 entry.insertOrUpdateInRecord(face2, *interest3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700116 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
117 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
118 BOOST_CHECK_EQUAL(in3->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700119
Junxiao Shi66f91f82014-05-10 17:28:58 -0700120 // get InRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700121 pit::InRecordCollection::const_iterator in4 = entry.getInRecord(*face1);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700122 BOOST_REQUIRE(in4 != entry.getInRecords().end());
123 BOOST_CHECK_EQUAL(in4->getFace(), face1);
124
Junxiao Shicbba04c2014-01-26 14:21:22 -0700125 // delete all InRecords
126 entry.deleteInRecords();
127 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
128 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700129 BOOST_CHECK(entry.getInRecord(*face1) == entry.getInRecords().end());
Junxiao Shicbba04c2014-01-26 14:21:22 -0700130
Junxiao Shicbba04c2014-01-26 14:21:22 -0700131 // insert another OutRecord
132 pit::OutRecordCollection::iterator out2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700133 entry.insertOrUpdateOutRecord(face2, *interest4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700134 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
135 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
136 BOOST_CHECK_EQUAL(out2->getFace(), face2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700137
Junxiao Shi66f91f82014-05-10 17:28:58 -0700138 // get OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700139 pit::OutRecordCollection::const_iterator out3 = entry.getOutRecord(*face1);
Junxiao Shi66f91f82014-05-10 17:28:58 -0700140 BOOST_REQUIRE(out3 != entry.getOutRecords().end());
141 BOOST_CHECK_EQUAL(out3->getFace(), face1);
142
Junxiao Shicbba04c2014-01-26 14:21:22 -0700143 // delete OutRecord
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700144 entry.deleteOutRecord(*face2);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700145 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
146 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
147 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700148 BOOST_CHECK(entry.getOutRecord(*face2) == entry.getOutRecords().end());
Junxiao Shid3c792f2014-01-30 00:46:13 -0700149}
150
151BOOST_AUTO_TEST_CASE(EntryNonce)
152{
Junxiao Shia110f262014-10-12 12:35:20 -0700153 shared_ptr<Face> face1 = make_shared<DummyFace>();
154 shared_ptr<Face> face2 = make_shared<DummyFace>();
155
Junxiao Shi57f0f312014-03-16 11:52:20 -0700156 shared_ptr<Interest> interest = makeInterest("ndn:/qtCQ7I1c");
Junxiao Shia110f262014-10-12 12:35:20 -0700157 interest->setNonce(25559);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700158
Junxiao Shia110f262014-10-12 12:35:20 -0700159 pit::Entry entry0(*interest);
160 BOOST_CHECK_EQUAL(entry0.findNonce(25559, *face1), pit::DUPLICATE_NONCE_NONE);
161 BOOST_CHECK_EQUAL(entry0.findNonce(25559, *face2), pit::DUPLICATE_NONCE_NONE);
162 BOOST_CHECK_EQUAL(entry0.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
163 BOOST_CHECK_EQUAL(entry0.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700164
Junxiao Shia110f262014-10-12 12:35:20 -0700165 pit::Entry entry1(*interest);
166 entry1.insertOrUpdateInRecord(face1, *interest);
167 BOOST_CHECK_EQUAL(entry1.findNonce(25559, *face1), pit::DUPLICATE_NONCE_IN_SAME);
168 BOOST_CHECK_EQUAL(entry1.findNonce(25559, *face2), pit::DUPLICATE_NONCE_IN_OTHER);
169 BOOST_CHECK_EQUAL(entry1.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
170 BOOST_CHECK_EQUAL(entry1.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
171
172 pit::Entry entry2(*interest);
173 entry2.insertOrUpdateOutRecord(face1, *interest);
174 BOOST_CHECK_EQUAL(entry2.findNonce(25559, *face1), pit::DUPLICATE_NONCE_OUT_SAME);
175 BOOST_CHECK_EQUAL(entry2.findNonce(25559, *face2), pit::DUPLICATE_NONCE_OUT_OTHER);
176 BOOST_CHECK_EQUAL(entry2.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
177 BOOST_CHECK_EQUAL(entry2.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
178
179 pit::Entry entry3(*interest);
180 entry3.insertOrUpdateInRecord(face1, *interest);
181 entry3.insertOrUpdateOutRecord(face1, *interest);
182 BOOST_CHECK_EQUAL(entry3.findNonce(25559, *face1),
183 pit::DUPLICATE_NONCE_IN_SAME | pit::DUPLICATE_NONCE_OUT_SAME);
184 BOOST_CHECK_EQUAL(entry3.findNonce(25559, *face2),
185 pit::DUPLICATE_NONCE_IN_OTHER | pit::DUPLICATE_NONCE_OUT_OTHER);
186 BOOST_CHECK_EQUAL(entry3.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
187 BOOST_CHECK_EQUAL(entry3.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
188
189 pit::Entry entry4(*interest);
190 entry4.insertOrUpdateInRecord(face1, *interest);
191 entry4.insertOrUpdateInRecord(face2, *interest);
192 BOOST_CHECK_EQUAL(entry4.findNonce(25559, *face1),
193 pit::DUPLICATE_NONCE_IN_SAME | pit::DUPLICATE_NONCE_IN_OTHER);
194 BOOST_CHECK_EQUAL(entry4.findNonce(25559, *face2),
195 pit::DUPLICATE_NONCE_IN_SAME | pit::DUPLICATE_NONCE_IN_OTHER);
196 BOOST_CHECK_EQUAL(entry4.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
197 BOOST_CHECK_EQUAL(entry4.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
198
199 pit::Entry entry5(*interest);
200 entry5.insertOrUpdateOutRecord(face1, *interest);
201 entry5.insertOrUpdateOutRecord(face2, *interest);
202 BOOST_CHECK_EQUAL(entry5.findNonce(25559, *face1),
203 pit::DUPLICATE_NONCE_OUT_SAME | pit::DUPLICATE_NONCE_OUT_OTHER);
204 BOOST_CHECK_EQUAL(entry5.findNonce(25559, *face2),
205 pit::DUPLICATE_NONCE_OUT_SAME | pit::DUPLICATE_NONCE_OUT_OTHER);
206 BOOST_CHECK_EQUAL(entry5.findNonce(19004, *face1), pit::DUPLICATE_NONCE_NONE);
207 BOOST_CHECK_EQUAL(entry5.findNonce(19004, *face2), pit::DUPLICATE_NONCE_NONE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700208}
209
Junxiao Shi57f0f312014-03-16 11:52:20 -0700210BOOST_AUTO_TEST_CASE(EntryLifetime)
211{
212 shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
Junxiao Shia110f262014-10-12 12:35:20 -0700213 // library uses -1 to indicate unset lifetime
214 BOOST_ASSERT(interest->getInterestLifetime() < time::milliseconds::zero());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700215
216 shared_ptr<Face> face = make_shared<DummyFace>();
217 pit::Entry entry(*interest);
218
219 pit::InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700220 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700221
222 pit::OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(face, *interest);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700223 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
Junxiao Shi57f0f312014-03-16 11:52:20 -0700224}
225
226BOOST_AUTO_TEST_CASE(EntryCanForwardTo)
227{
228 shared_ptr<Interest> interest = makeInterest("ndn:/WDsuBLIMG");
229 pit::Entry entry(*interest);
230
231 shared_ptr<Face> face1 = make_shared<DummyFace>();
232 shared_ptr<Face> face2 = make_shared<DummyFace>();
233
234 entry.insertOrUpdateInRecord(face1, *interest);
235 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
236 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
237
238 entry.insertOrUpdateInRecord(face2, *interest);
239 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), true);
240 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
241
242 entry.insertOrUpdateOutRecord(face1, *interest);
243 BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
244 BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
245}
246
Junxiao Shicbba04c2014-01-26 14:21:22 -0700247BOOST_AUTO_TEST_CASE(Insert)
248{
249 Name name1("ndn:/5vzBNnMst");
250 Name name2("ndn:/igSGfEIM62");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700251 Exclude exclude1;
252 exclude1.excludeOne(Name::Component("u26p47oep"));
253 Exclude exclude2;
Junxiao Shi30d35992014-04-03 14:51:58 -0700254 exclude2.excludeBefore(Name::Component("u26p47oep"));
255 ndn::KeyLocator keyLocator1("ndn:/sGAE3peMHA");
256 ndn::KeyLocator keyLocator2("ndn:/nIJH6pr4");
Junxiao Shi57f0f312014-03-16 11:52:20 -0700257
Haowei Yuan78c84d12014-02-27 15:35:13 -0600258 NameTree nameTree(16);
259 Pit pit(nameTree);
Junxiao Shi30d35992014-04-03 14:51:58 -0700260 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700261 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700262
Junxiao Shi30d35992014-04-03 14:51:58 -0700263 // base
264 shared_ptr<Interest> interestA = make_shared<Interest>(name1);
265 insertResult = pit.insert(*interestA);
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(), 1);
268
Junxiao Shi30d35992014-04-03 14:51:58 -0700269 // A+MinSuffixComponents
270 shared_ptr<Interest> interestB = make_shared<Interest>(*interestA);
271 interestB->setMinSuffixComponents(2);
272 insertResult = pit.insert(*interestB);
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(), 2);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700275
Junxiao Shi30d35992014-04-03 14:51:58 -0700276 // A+MaxSuffixComponents
277 shared_ptr<Interest> interestC = make_shared<Interest>(*interestA);
278 interestC->setMaxSuffixComponents(4);
279 insertResult = pit.insert(*interestC);
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(), 3);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700282
Junxiao Shi30d35992014-04-03 14:51:58 -0700283 // A+KeyLocator1
284 shared_ptr<Interest> interestD = make_shared<Interest>(*interestA);
285 interestD->setPublisherPublicKeyLocator(keyLocator1);
286 insertResult = pit.insert(*interestD);
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(), 4);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700289
Junxiao Shi30d35992014-04-03 14:51:58 -0700290 // A+KeyLocator2
291 shared_ptr<Interest> interestE = make_shared<Interest>(*interestA);
292 interestE->setPublisherPublicKeyLocator(keyLocator2);
293 insertResult = pit.insert(*interestE);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700294 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600295 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700296
Junxiao Shi30d35992014-04-03 14:51:58 -0700297 // A+Exclude1
298 shared_ptr<Interest> interestF = make_shared<Interest>(*interestA);
299 interestF->setExclude(exclude1);
300 insertResult = pit.insert(*interestF);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700301 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600302 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700303
Junxiao Shi30d35992014-04-03 14:51:58 -0700304 // A+Exclude2
305 shared_ptr<Interest> interestG = make_shared<Interest>(*interestA);
306 interestG->setExclude(exclude2);
307 insertResult = pit.insert(*interestG);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700308 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600309 BOOST_CHECK_EQUAL(pit.size(), 7);
310
Junxiao Shi30d35992014-04-03 14:51:58 -0700311 // A+ChildSelector0
312 shared_ptr<Interest> interestH = make_shared<Interest>(*interestA);
313 interestH->setChildSelector(0);
314 insertResult = pit.insert(*interestH);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700315 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600316 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shi30d35992014-04-03 14:51:58 -0700317
318 // A+ChildSelector1
319 shared_ptr<Interest> interestI = make_shared<Interest>(*interestA);
320 interestI->setChildSelector(1);
321 insertResult = pit.insert(*interestI);
322 BOOST_CHECK_EQUAL(insertResult.second, true);
323 BOOST_CHECK_EQUAL(pit.size(), 9);
324
325 // A+MustBeFresh
326 shared_ptr<Interest> interestJ = make_shared<Interest>(*interestA);
327 interestJ->setMustBeFresh(true);
328 insertResult = pit.insert(*interestJ);
329 BOOST_CHECK_EQUAL(insertResult.second, true);
330 BOOST_CHECK_EQUAL(pit.size(), 10);
331
332 // A+InterestLifetime
333 shared_ptr<Interest> interestK = make_shared<Interest>(*interestA);
334 interestK->setInterestLifetime(time::milliseconds(1000));
335 insertResult = pit.insert(*interestK);
336 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
337 BOOST_CHECK_EQUAL(pit.size(), 10);
338
339 // A+Nonce
340 shared_ptr<Interest> interestL = make_shared<Interest>(*interestA);
341 interestL->setNonce(2192);
342 insertResult = pit.insert(*interestL);
343 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
344 BOOST_CHECK_EQUAL(pit.size(), 10);
345
346 // different Name+Exclude1
347 shared_ptr<Interest> interestM = make_shared<Interest>(name2);
348 interestM->setExclude(exclude1);
349 insertResult = pit.insert(*interestM);
350 BOOST_CHECK_EQUAL(insertResult.second, true);
351 BOOST_CHECK_EQUAL(pit.size(), 11);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700352}
353
Haowei Yuan78c84d12014-02-27 15:35:13 -0600354BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700355{
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700356 shared_ptr<Interest> interest = makeInterest("/z88Admz6A2");
Junxiao Shicbba04c2014-01-26 14:21:22 -0700357
Haowei Yuan78c84d12014-02-27 15:35:13 -0600358 NameTree nameTree(16);
359 Pit pit(nameTree);
360
Junxiao Shicbba04c2014-01-26 14:21:22 -0700361 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700362
Haowei Yuan78c84d12014-02-27 15:35:13 -0600363 BOOST_CHECK_EQUAL(pit.size(), 0);
364
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700365 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700366 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600367 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700368
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700369 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700370 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600371 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700372
Haowei Yuan78c84d12014-02-27 15:35:13 -0600373 pit.erase(insertResult.first);
374 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700375
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700376 insertResult = pit.insert(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700377 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600378 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700379
Haowei Yuan78c84d12014-02-27 15:35:13 -0600380}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700381
Junxiao Shiee5a4442014-07-27 17:13:43 -0700382BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
383{
384 NameTree nameTree;
385 Pit pit(nameTree);
386 size_t nNameTreeEntriesBefore = nameTree.size();
387
388 shared_ptr<Interest> interest = makeInterest("/37xWVvQ2K");
389 shared_ptr<pit::Entry> entry = pit.insert(*interest).first;
390 pit.erase(entry);
391 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
392}
393
Junxiao Shicbba04c2014-01-26 14:21:22 -0700394BOOST_AUTO_TEST_CASE(FindAllDataMatches)
395{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600396 Name nameA ("ndn:/A");
397 Name nameAB ("ndn:/A/B");
398 Name nameABC ("ndn:/A/B/C");
399 Name nameABCD("ndn:/A/B/C/D");
400 Name nameD ("ndn:/D");
401
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700402 shared_ptr<Interest> interestA = makeInterest(nameA );
403 shared_ptr<Interest> interestABC = makeInterest(nameABC);
404 shared_ptr<Interest> interestD = makeInterest(nameD );
Junxiao Shicbba04c2014-01-26 14:21:22 -0700405
Haowei Yuan78c84d12014-02-27 15:35:13 -0600406 NameTree nameTree(16);
407 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600408 int count = 0;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700409
Haowei Yuan78c84d12014-02-27 15:35:13 -0600410 BOOST_CHECK_EQUAL(pit.size(), 0);
411
Alexander Afanasyev28d586a2014-07-10 20:10:54 -0700412 pit.insert(*interestA );
413 pit.insert(*interestABC);
414 pit.insert(*interestD );
Haowei Yuane1079fc2014-03-08 14:41:25 -0600415
416 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shi57f0f312014-03-16 11:52:20 -0700417
Haowei Yuan78c84d12014-02-27 15:35:13 -0600418 BOOST_CHECK_EQUAL(pit.size(), 3);
419
Alexander Afanasyevc026d252014-06-16 11:14:15 -0700420 shared_ptr<Data> data = makeData(nameABCD);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700421
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700422 pit::DataMatchResult matches = pit.findAllDataMatches(*data);
Junxiao Shi57f0f312014-03-16 11:52:20 -0700423
Haowei Yuane1079fc2014-03-08 14:41:25 -0600424 bool hasA = false;
425 bool hasAB = false;
426 bool hasABC = false;
427 bool hasD = false;
428
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700429 for (const shared_ptr<pit::Entry>& entry : matches) {
Junxiao Shicbba04c2014-01-26 14:21:22 -0700430 ++count;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600431
432 if (entry->getName().equals(nameA ))
433 hasA = true;
434
435 if (entry->getName().equals(nameAB))
436 hasAB = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700437
Haowei Yuane1079fc2014-03-08 14:41:25 -0600438 if (entry->getName().equals(nameABC))
439 hasABC = true;
Junxiao Shi57f0f312014-03-16 11:52:20 -0700440
Haowei Yuane1079fc2014-03-08 14:41:25 -0600441 if (entry->getName().equals(nameD))
442 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700443 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600444 BOOST_CHECK_EQUAL(hasA , true);
445 BOOST_CHECK_EQUAL(hasAB , false);
446 BOOST_CHECK_EQUAL(hasABC, true);
447 BOOST_CHECK_EQUAL(hasD , false);
448
Junxiao Shicbba04c2014-01-26 14:21:22 -0700449 BOOST_CHECK_EQUAL(count, 2);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600450
Junxiao Shicbba04c2014-01-26 14:21:22 -0700451}
452
453BOOST_AUTO_TEST_SUITE_END()
454
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700455} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800456} // namespace nfd