blob: f366ea2db40a5a53e0ac996c80e21ba5c7daee13 [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");
22 Interest interest(name);
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080023 Interest interest1(name, static_cast<ndn::Milliseconds>(2528));
Junxiao Shicbba04c2014-01-26 14:21:22 -070024 interest1.setNonce(25559);
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080025 Interest interest2(name, static_cast<ndn::Milliseconds>(6464));
Junxiao Shicbba04c2014-01-26 14:21:22 -070026 interest2.setNonce(19004);
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080027 Interest interest3(name, static_cast<ndn::Milliseconds>(3585));
Junxiao Shicbba04c2014-01-26 14:21:22 -070028 interest3.setNonce(24216);
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029 Interest interest4(name, static_cast<ndn::Milliseconds>(8795));
Junxiao Shicbba04c2014-01-26 14:21:22 -070030 interest4.setNonce(17365);
31
32 pit::Entry entry(interest);
33
34 BOOST_CHECK(entry.getInterest().getName().equals(name));
35 BOOST_CHECK(entry.getName().equals(name));
Haowei Yuan78c84d12014-02-27 15:35:13 -060036
Junxiao Shicbba04c2014-01-26 14:21:22 -070037 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
38 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
39 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
40 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
41
42 // insert InRecord
43 time::Point before1 = time::now();
44 pit::InRecordCollection::iterator in1 =
45 entry.insertOrUpdateInRecord(face1, interest1);
46 time::Point after1 = time::now();
47 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
48 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
49 BOOST_CHECK(in1 == inRecords2.begin());
50 BOOST_CHECK_EQUAL(in1->getFace(), face1);
51 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1.getNonce());
52 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
53 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
54 BOOST_CHECK_LE(std::abs(in1->getExpiry() - in1->getLastRenewed()
55 - time::milliseconds(interest1.getInterestLifetime())),
56 (after1 - before1));
57
58 // insert OutRecord
59 time::Point before2 = time::now();
60 pit::OutRecordCollection::iterator out1 =
61 entry.insertOrUpdateOutRecord(face1, interest1);
62 time::Point after2 = time::now();
63 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
64 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
65 BOOST_CHECK(out1 == outRecords2.begin());
66 BOOST_CHECK_EQUAL(out1->getFace(), face1);
67 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1.getNonce());
68 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
69 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
70 BOOST_CHECK_LE(std::abs(out1->getExpiry() - out1->getLastRenewed()
71 - time::milliseconds(interest1.getInterestLifetime())),
72 (after2 - before2));
73
Junxiao Shicbba04c2014-01-26 14:21:22 -070074 // update InRecord
75 time::Point before3 = time::now();
76 pit::InRecordCollection::iterator in2 =
77 entry.insertOrUpdateInRecord(face1, interest2);
78 time::Point after3 = time::now();
79 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
80 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
81 BOOST_CHECK(in2 == inRecords3.begin());
82 BOOST_CHECK_EQUAL(in2->getFace(), face1);
83 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2.getNonce());
84 BOOST_CHECK_LE(std::abs(in2->getExpiry() - in2->getLastRenewed()
85 - time::milliseconds(interest2.getInterestLifetime())),
86 (after3 - before3));
87
Junxiao Shicbba04c2014-01-26 14:21:22 -070088 // insert another InRecord
89 pit::InRecordCollection::iterator in3 =
90 entry.insertOrUpdateInRecord(face2, interest3);
91 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
92 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
93 BOOST_CHECK_EQUAL(in3->getFace(), face2);
94
Junxiao Shicbba04c2014-01-26 14:21:22 -070095 // delete all InRecords
96 entry.deleteInRecords();
97 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
98 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
99
Junxiao Shicbba04c2014-01-26 14:21:22 -0700100 // insert another OutRecord
101 pit::OutRecordCollection::iterator out2 =
102 entry.insertOrUpdateOutRecord(face2, interest4);
103 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
104 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
105 BOOST_CHECK_EQUAL(out2->getFace(), face2);
106
Junxiao Shicbba04c2014-01-26 14:21:22 -0700107 // delete OutRecord
108 entry.deleteOutRecord(face2);
109 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
110 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
111 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
112
Junxiao Shid3c792f2014-01-30 00:46:13 -0700113}
114
115BOOST_AUTO_TEST_CASE(EntryNonce)
116{
117 Name name("ndn:/qtCQ7I1c");
118 Interest interest(name);
119
120 pit::Entry entry(interest);
121
122 BOOST_CHECK_EQUAL(entry.addNonce(25559), true);
123 BOOST_CHECK_EQUAL(entry.addNonce(25559), false);
124 BOOST_CHECK_EQUAL(entry.addNonce(19004), true);
125 BOOST_CHECK_EQUAL(entry.addNonce(19004), false);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700126}
127
128BOOST_AUTO_TEST_CASE(Insert)
129{
130 Name name1("ndn:/5vzBNnMst");
131 Name name2("ndn:/igSGfEIM62");
132 Exclude exclude0;
133 Exclude exclude1;
134 exclude1.excludeOne(Name::Component("u26p47oep"));
135 Exclude exclude2;
136 exclude2.excludeOne(Name::Component("FG1Ni6nYcf"));
137
138 // base
139 Interest interestA(name1, -1, -1, exclude0, -1, false, -1, -1.0, 0);
140 // A+exclude1
141 Interest interestB(name1, -1, -1, exclude1, -1, false, -1, -1.0, 0);
142 // A+exclude2
143 Interest interestC(name1, -1, -1, exclude2, -1, false, -1, -1.0, 0);
144 // A+MinSuffixComponents
145 Interest interestD(name1, 2, -1, exclude0, -1, false, -1, -1.0, 0);
146 // A+MaxSuffixComponents
147 Interest interestE(name1, -1, 4, exclude0, -1, false, -1, -1.0, 0);
148 // A+ChildSelector
149 Interest interestF(name1, -1, -1, exclude0, 1, false, -1, -1.0, 0);
150 // A+MustBeFresh
151 Interest interestG(name1, -1, -1, exclude0, -1, true, -1, -1.0, 0);
152 // A+Scope
153 Interest interestH(name1, -1, -1, exclude0, -1, false, 2, -1.0, 0);
154 // A+InterestLifetime
155 Interest interestI(name1, -1, -1, exclude0, -1, false, -1, 2000, 0);
156 // A+Nonce
157 Interest interestJ(name1, -1, -1, exclude0, -1, false, -1, -1.0, 2192);
158 // different Name+exclude1
159 Interest interestK(name2, -1, -1, exclude1, -1, false, -1, -1.0, 0);
160
Haowei Yuan78c84d12014-02-27 15:35:13 -0600161 NameTree nameTree(16);
162 Pit pit(nameTree);
163
Junxiao Shicbba04c2014-01-26 14:21:22 -0700164 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
165
Haowei Yuan78c84d12014-02-27 15:35:13 -0600166 BOOST_CHECK_EQUAL(pit.size(), 0);
167
Junxiao Shicbba04c2014-01-26 14:21:22 -0700168 insertResult = pit.insert(interestA);
169 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600170 BOOST_CHECK_EQUAL(pit.size(), 1);
171
Junxiao Shicbba04c2014-01-26 14:21:22 -0700172 insertResult = pit.insert(interestB);
173 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600174 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700175
176 insertResult = pit.insert(interestC);
177 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600178 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700179
180 insertResult = pit.insert(interestD);
181 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600182 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700183
184 insertResult = pit.insert(interestE);
185 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600186 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700187
188 insertResult = pit.insert(interestF);
189 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600190 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700191
192 insertResult = pit.insert(interestG);
193 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600194 BOOST_CHECK_EQUAL(pit.size(), 7);
195
Junxiao Shicbba04c2014-01-26 14:21:22 -0700196
197 insertResult = pit.insert(interestH);
198 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600199 BOOST_CHECK_EQUAL(pit.size(), 7);
200
Junxiao Shicbba04c2014-01-26 14:21:22 -0700201 insertResult = pit.insert(interestI);
202 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600203 BOOST_CHECK_EQUAL(pit.size(), 7);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700204
205 insertResult = pit.insert(interestJ);
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(interestK);
210 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600211 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700212}
213
Haowei Yuan78c84d12014-02-27 15:35:13 -0600214BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700215{
216 Interest interest(Name("ndn:/z88Admz6A2"));
217
Haowei Yuan78c84d12014-02-27 15:35:13 -0600218 NameTree nameTree(16);
219 Pit pit(nameTree);
220
Junxiao Shicbba04c2014-01-26 14:21:22 -0700221 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
222
Haowei Yuan78c84d12014-02-27 15:35:13 -0600223 BOOST_CHECK_EQUAL(pit.size(), 0);
224
Junxiao Shicbba04c2014-01-26 14:21:22 -0700225 insertResult = pit.insert(interest);
226 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600227 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700228
229 insertResult = pit.insert(interest);
230 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600231 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700232
Haowei Yuan78c84d12014-02-27 15:35:13 -0600233 pit.erase(insertResult.first);
234 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700235
236 insertResult = pit.insert(interest);
237 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600238 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700239
Haowei Yuan78c84d12014-02-27 15:35:13 -0600240}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700241
242BOOST_AUTO_TEST_CASE(FindAllDataMatches)
243{
244 Name nameA ("ndn:/A");
245 Name nameAB ("ndn:/A/B");
246 Name nameABC("ndn:/A/B/C");
247 Name nameD ("ndn:/D");
248 Interest interestA (nameA );
249 Interest interestAB(nameAB);
250 Interest interestD (nameD );
251
Haowei Yuan78c84d12014-02-27 15:35:13 -0600252 NameTree nameTree(16);
253 Pit pit(nameTree);
254
255 BOOST_CHECK_EQUAL(pit.size(), 0);
256
Junxiao Shicbba04c2014-01-26 14:21:22 -0700257 pit.insert(interestA );
258 pit.insert(interestAB);
259 pit.insert(interestD );
260
Haowei Yuan78c84d12014-02-27 15:35:13 -0600261 BOOST_CHECK_EQUAL(pit.size(), 3);
262
Junxiao Shicbba04c2014-01-26 14:21:22 -0700263 Data data(nameABC);
264
265 shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(data);
266 int count = 0;
267 bool hasA = false;
268 bool hasAB = false;
269 bool hasD = false;
270 for (pit::DataMatchResult::iterator it = matches->begin();
271 it != matches->end(); ++it) {
272 ++count;
273 shared_ptr<pit::Entry> entry = *it;
274 if (entry->getName().equals(nameA )) {
275 hasA = true;
276 }
277 if (entry->getName().equals(nameAB)) {
278 hasAB = true;
279 }
280 if (entry->getName().equals(nameD )) {
281 hasD = true;
282 }
283 }
284 BOOST_CHECK_EQUAL(count, 2);
285 BOOST_CHECK_EQUAL(hasA , true);
286 BOOST_CHECK_EQUAL(hasAB, true);
287 BOOST_CHECK_EQUAL(hasD , false);
288}
289
290BOOST_AUTO_TEST_SUITE_END()
291
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700292} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800293} // namespace nfd