blob: 393c1bc670aaea3597dc5b20b82f3fcd1627b755 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "table/pit.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -07008#include "tests/face/dummy-face.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -07009
Junxiao Shid9ee45c2014-02-27 15:38:11 -070010#include "tests/test-common.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070011
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080012namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070013namespace tests {
Junxiao Shicbba04c2014-01-26 14:21:22 -070014
Junxiao Shid9ee45c2014-02-27 15:38:11 -070015BOOST_FIXTURE_TEST_SUITE(TablePit, BaseFixture)
Junxiao Shicbba04c2014-01-26 14:21:22 -070016
Junxiao Shid3c792f2014-01-30 00:46:13 -070017BOOST_AUTO_TEST_CASE(EntryInOutRecords)
Junxiao Shicbba04c2014-01-26 14:21:22 -070018{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070019 shared_ptr<Face> face1 = make_shared<DummyFace>();
20 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shicbba04c2014-01-26 14:21:22 -070021 Name name("ndn:/KuYfjtRq");
Junxiao Shif3c07812014-03-11 21:48:49 -070022 shared_ptr<Interest> interest = makeInterest(name);
23 shared_ptr<Interest> interest1 = makeInterest(name);
24 interest1->setInterestLifetime(static_cast<ndn::Milliseconds>(2528));
25 interest1->setNonce(25559);
26 shared_ptr<Interest> interest2 = makeInterest(name);
27 interest2->setInterestLifetime(static_cast<ndn::Milliseconds>(6464));
28 interest2->setNonce(19004);
29 shared_ptr<Interest> interest3 = makeInterest(name);
30 interest3->setInterestLifetime(static_cast<ndn::Milliseconds>(3585));
31 interest3->setNonce(24216);
32 shared_ptr<Interest> interest4 = makeInterest(name);
33 interest4->setInterestLifetime(static_cast<ndn::Milliseconds>(8795));
34 interest4->setNonce(17365);
Junxiao Shicbba04c2014-01-26 14:21:22 -070035
Junxiao Shif3c07812014-03-11 21:48:49 -070036 pit::Entry entry(*interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -070037
Junxiao Shif3c07812014-03-11 21:48:49 -070038 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
39 BOOST_CHECK_EQUAL(entry.getName(), name);
Haowei Yuan78c84d12014-02-27 15:35:13 -060040
Junxiao Shicbba04c2014-01-26 14:21:22 -070041 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
42 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
43 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
44 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
45
46 // insert InRecord
47 time::Point before1 = time::now();
48 pit::InRecordCollection::iterator in1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070049 entry.insertOrUpdateInRecord(face1, *interest1);
Junxiao Shicbba04c2014-01-26 14:21:22 -070050 time::Point after1 = time::now();
51 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
52 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
53 BOOST_CHECK(in1 == inRecords2.begin());
54 BOOST_CHECK_EQUAL(in1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070055 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070056 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
57 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
58 BOOST_CHECK_LE(std::abs(in1->getExpiry() - in1->getLastRenewed()
Junxiao Shif3c07812014-03-11 21:48:49 -070059 - time::milliseconds(interest1->getInterestLifetime())),
Junxiao Shicbba04c2014-01-26 14:21:22 -070060 (after1 - before1));
61
62 // insert OutRecord
63 time::Point before2 = time::now();
64 pit::OutRecordCollection::iterator out1 =
Junxiao Shif3c07812014-03-11 21:48:49 -070065 entry.insertOrUpdateOutRecord(face1, *interest1);
Junxiao Shicbba04c2014-01-26 14:21:22 -070066 time::Point after2 = time::now();
67 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
68 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
69 BOOST_CHECK(out1 == outRecords2.begin());
70 BOOST_CHECK_EQUAL(out1->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070071 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070072 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
73 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
74 BOOST_CHECK_LE(std::abs(out1->getExpiry() - out1->getLastRenewed()
Junxiao Shif3c07812014-03-11 21:48:49 -070075 - time::milliseconds(interest1->getInterestLifetime())),
Junxiao Shicbba04c2014-01-26 14:21:22 -070076 (after2 - before2));
77
Junxiao Shicbba04c2014-01-26 14:21:22 -070078 // update InRecord
79 time::Point before3 = time::now();
80 pit::InRecordCollection::iterator in2 =
Junxiao Shif3c07812014-03-11 21:48:49 -070081 entry.insertOrUpdateInRecord(face1, *interest2);
Junxiao Shicbba04c2014-01-26 14:21:22 -070082 time::Point after3 = time::now();
83 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
84 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
85 BOOST_CHECK(in2 == inRecords3.begin());
86 BOOST_CHECK_EQUAL(in2->getFace(), face1);
Junxiao Shif3c07812014-03-11 21:48:49 -070087 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -070088 BOOST_CHECK_LE(std::abs(in2->getExpiry() - in2->getLastRenewed()
Junxiao Shif3c07812014-03-11 21:48:49 -070089 - time::milliseconds(interest2->getInterestLifetime())),
Junxiao Shicbba04c2014-01-26 14:21:22 -070090 (after3 - before3));
91
Junxiao Shicbba04c2014-01-26 14:21:22 -070092 // insert another InRecord
93 pit::InRecordCollection::iterator in3 =
Junxiao Shif3c07812014-03-11 21:48:49 -070094 entry.insertOrUpdateInRecord(face2, *interest3);
Junxiao Shicbba04c2014-01-26 14:21:22 -070095 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
96 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
97 BOOST_CHECK_EQUAL(in3->getFace(), face2);
98
Junxiao Shicbba04c2014-01-26 14:21:22 -070099 // delete all InRecords
100 entry.deleteInRecords();
101 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
102 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
103
Junxiao Shicbba04c2014-01-26 14:21:22 -0700104 // insert another OutRecord
105 pit::OutRecordCollection::iterator out2 =
Junxiao Shif3c07812014-03-11 21:48:49 -0700106 entry.insertOrUpdateOutRecord(face2, *interest4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700107 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
108 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
109 BOOST_CHECK_EQUAL(out2->getFace(), face2);
110
Junxiao Shicbba04c2014-01-26 14:21:22 -0700111 // delete OutRecord
112 entry.deleteOutRecord(face2);
113 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
114 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
115 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
116
Junxiao Shid3c792f2014-01-30 00:46:13 -0700117}
118
119BOOST_AUTO_TEST_CASE(EntryNonce)
120{
121 Name name("ndn:/qtCQ7I1c");
122 Interest interest(name);
123
124 pit::Entry entry(interest);
125
126 BOOST_CHECK_EQUAL(entry.addNonce(25559), true);
127 BOOST_CHECK_EQUAL(entry.addNonce(25559), false);
128 BOOST_CHECK_EQUAL(entry.addNonce(19004), true);
129 BOOST_CHECK_EQUAL(entry.addNonce(19004), false);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700130}
131
132BOOST_AUTO_TEST_CASE(Insert)
133{
134 Name name1("ndn:/5vzBNnMst");
135 Name name2("ndn:/igSGfEIM62");
136 Exclude exclude0;
137 Exclude exclude1;
138 exclude1.excludeOne(Name::Component("u26p47oep"));
139 Exclude exclude2;
140 exclude2.excludeOne(Name::Component("FG1Ni6nYcf"));
141
142 // base
143 Interest interestA(name1, -1, -1, exclude0, -1, false, -1, -1.0, 0);
144 // A+exclude1
145 Interest interestB(name1, -1, -1, exclude1, -1, false, -1, -1.0, 0);
146 // A+exclude2
147 Interest interestC(name1, -1, -1, exclude2, -1, false, -1, -1.0, 0);
148 // A+MinSuffixComponents
149 Interest interestD(name1, 2, -1, exclude0, -1, false, -1, -1.0, 0);
150 // A+MaxSuffixComponents
151 Interest interestE(name1, -1, 4, exclude0, -1, false, -1, -1.0, 0);
152 // A+ChildSelector
153 Interest interestF(name1, -1, -1, exclude0, 1, false, -1, -1.0, 0);
154 // A+MustBeFresh
155 Interest interestG(name1, -1, -1, exclude0, -1, true, -1, -1.0, 0);
156 // A+Scope
157 Interest interestH(name1, -1, -1, exclude0, -1, false, 2, -1.0, 0);
158 // A+InterestLifetime
159 Interest interestI(name1, -1, -1, exclude0, -1, false, -1, 2000, 0);
160 // A+Nonce
161 Interest interestJ(name1, -1, -1, exclude0, -1, false, -1, -1.0, 2192);
162 // different Name+exclude1
163 Interest interestK(name2, -1, -1, exclude1, -1, false, -1, -1.0, 0);
164
Haowei Yuan78c84d12014-02-27 15:35:13 -0600165 NameTree nameTree(16);
166 Pit pit(nameTree);
167
Junxiao Shicbba04c2014-01-26 14:21:22 -0700168 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
169
Haowei Yuan78c84d12014-02-27 15:35:13 -0600170 BOOST_CHECK_EQUAL(pit.size(), 0);
171
Junxiao Shicbba04c2014-01-26 14:21:22 -0700172 insertResult = pit.insert(interestA);
173 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600174 BOOST_CHECK_EQUAL(pit.size(), 1);
175
Junxiao Shicbba04c2014-01-26 14:21:22 -0700176 insertResult = pit.insert(interestB);
177 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600178 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700179
180 insertResult = pit.insert(interestC);
181 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600182 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700183
184 insertResult = pit.insert(interestD);
185 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600186 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700187
188 insertResult = pit.insert(interestE);
189 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600190 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700191
192 insertResult = pit.insert(interestF);
193 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600194 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700195
196 insertResult = pit.insert(interestG);
197 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600198 BOOST_CHECK_EQUAL(pit.size(), 7);
199
Junxiao Shicbba04c2014-01-26 14:21:22 -0700200
201 insertResult = pit.insert(interestH);
202 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600203 BOOST_CHECK_EQUAL(pit.size(), 7);
204
Junxiao Shicbba04c2014-01-26 14:21:22 -0700205 insertResult = pit.insert(interestI);
206 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600207 BOOST_CHECK_EQUAL(pit.size(), 7);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700208
209 insertResult = pit.insert(interestJ);
210 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600211 BOOST_CHECK_EQUAL(pit.size(), 7);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700212
213 insertResult = pit.insert(interestK);
214 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600215 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700216}
217
Haowei Yuan78c84d12014-02-27 15:35:13 -0600218BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700219{
220 Interest interest(Name("ndn:/z88Admz6A2"));
221
Haowei Yuan78c84d12014-02-27 15:35:13 -0600222 NameTree nameTree(16);
223 Pit pit(nameTree);
224
Junxiao Shicbba04c2014-01-26 14:21:22 -0700225 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
226
Haowei Yuan78c84d12014-02-27 15:35:13 -0600227 BOOST_CHECK_EQUAL(pit.size(), 0);
228
Junxiao Shicbba04c2014-01-26 14:21:22 -0700229 insertResult = pit.insert(interest);
230 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600231 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700232
233 insertResult = pit.insert(interest);
234 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600235 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700236
Haowei Yuan78c84d12014-02-27 15:35:13 -0600237 pit.erase(insertResult.first);
238 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700239
240 insertResult = pit.insert(interest);
241 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600242 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700243
Haowei Yuan78c84d12014-02-27 15:35:13 -0600244}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700245
246BOOST_AUTO_TEST_CASE(FindAllDataMatches)
247{
Haowei Yuane1079fc2014-03-08 14:41:25 -0600248 Name nameA ("ndn:/A");
249 Name nameAB ("ndn:/A/B");
250 Name nameABC ("ndn:/A/B/C");
251 Name nameABCD("ndn:/A/B/C/D");
252 Name nameD ("ndn:/D");
253
254 Interest interestA (nameA );
255 Interest interestABC(nameABC);
256 Interest interestD (nameD );
Junxiao Shicbba04c2014-01-26 14:21:22 -0700257
Haowei Yuan78c84d12014-02-27 15:35:13 -0600258 NameTree nameTree(16);
259 Pit pit(nameTree);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600260 int count = 0;
Haowei Yuan78c84d12014-02-27 15:35:13 -0600261
262 BOOST_CHECK_EQUAL(pit.size(), 0);
263
Haowei Yuane1079fc2014-03-08 14:41:25 -0600264 pit.insert(interestA );
265 pit.insert(interestABC);
266 pit.insert(interestD );
267
268 nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
Junxiao Shicbba04c2014-01-26 14:21:22 -0700269
Haowei Yuan78c84d12014-02-27 15:35:13 -0600270 BOOST_CHECK_EQUAL(pit.size(), 3);
271
Haowei Yuane1079fc2014-03-08 14:41:25 -0600272 Data data(nameABCD);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700273
274 shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(data);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600275
276 bool hasA = false;
277 bool hasAB = false;
278 bool hasABC = false;
279 bool hasD = false;
280
Junxiao Shicbba04c2014-01-26 14:21:22 -0700281 for (pit::DataMatchResult::iterator it = matches->begin();
282 it != matches->end(); ++it) {
283 ++count;
284 shared_ptr<pit::Entry> entry = *it;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600285
286 if (entry->getName().equals(nameA ))
287 hasA = true;
288
289 if (entry->getName().equals(nameAB))
290 hasAB = true;
291
292 if (entry->getName().equals(nameABC))
293 hasABC = true;
294
295 if (entry->getName().equals(nameD))
296 hasD = true;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700297 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600298 BOOST_CHECK_EQUAL(hasA , true);
299 BOOST_CHECK_EQUAL(hasAB , false);
300 BOOST_CHECK_EQUAL(hasABC, true);
301 BOOST_CHECK_EQUAL(hasD , false);
302
Junxiao Shicbba04c2014-01-26 14:21:22 -0700303 BOOST_CHECK_EQUAL(count, 2);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600304
Junxiao Shicbba04c2014-01-26 14:21:22 -0700305}
306
307BOOST_AUTO_TEST_SUITE_END()
308
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700309} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800310} // namespace nfd