blob: 916db810c0d9e122bd9a5a51231c0d479540b81b [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"
8#include "../face/dummy-face.hpp"
9
10#include <boost/test/unit_test.hpp>
11
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080012namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070013
14BOOST_AUTO_TEST_SUITE(TablePit)
15
Junxiao Shid3c792f2014-01-30 00:46:13 -070016BOOST_AUTO_TEST_CASE(EntryInOutRecords)
Junxiao Shicbba04c2014-01-26 14:21:22 -070017{
Junxiao Shi8c8d2182014-01-30 22:33:00 -070018 shared_ptr<Face> face1 = make_shared<DummyFace>();
19 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shicbba04c2014-01-26 14:21:22 -070020 Name name("ndn:/KuYfjtRq");
21 Interest interest(name);
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080022 Interest interest1(name, static_cast<ndn::Milliseconds>(2528));
Junxiao Shicbba04c2014-01-26 14:21:22 -070023 interest1.setNonce(25559);
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080024 Interest interest2(name, static_cast<ndn::Milliseconds>(6464));
Junxiao Shicbba04c2014-01-26 14:21:22 -070025 interest2.setNonce(19004);
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080026 Interest interest3(name, static_cast<ndn::Milliseconds>(3585));
Junxiao Shicbba04c2014-01-26 14:21:22 -070027 interest3.setNonce(24216);
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080028 Interest interest4(name, static_cast<ndn::Milliseconds>(8795));
Junxiao Shicbba04c2014-01-26 14:21:22 -070029 interest4.setNonce(17365);
30
31 pit::Entry entry(interest);
32
33 BOOST_CHECK(entry.getInterest().getName().equals(name));
34 BOOST_CHECK(entry.getName().equals(name));
Haowei Yuan78c84d12014-02-27 15:35:13 -060035
Junxiao Shicbba04c2014-01-26 14:21:22 -070036 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
37 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
38 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
39 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
40
41 // insert InRecord
42 time::Point before1 = time::now();
43 pit::InRecordCollection::iterator in1 =
44 entry.insertOrUpdateInRecord(face1, interest1);
45 time::Point after1 = time::now();
46 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
47 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
48 BOOST_CHECK(in1 == inRecords2.begin());
49 BOOST_CHECK_EQUAL(in1->getFace(), face1);
50 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1.getNonce());
51 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
52 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
53 BOOST_CHECK_LE(std::abs(in1->getExpiry() - in1->getLastRenewed()
54 - time::milliseconds(interest1.getInterestLifetime())),
55 (after1 - before1));
56
57 // insert OutRecord
58 time::Point before2 = time::now();
59 pit::OutRecordCollection::iterator out1 =
60 entry.insertOrUpdateOutRecord(face1, interest1);
61 time::Point after2 = time::now();
62 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
63 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
64 BOOST_CHECK(out1 == outRecords2.begin());
65 BOOST_CHECK_EQUAL(out1->getFace(), face1);
66 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1.getNonce());
67 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
68 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
69 BOOST_CHECK_LE(std::abs(out1->getExpiry() - out1->getLastRenewed()
70 - time::milliseconds(interest1.getInterestLifetime())),
71 (after2 - before2));
72
Junxiao Shicbba04c2014-01-26 14:21:22 -070073 // update InRecord
74 time::Point before3 = time::now();
75 pit::InRecordCollection::iterator in2 =
76 entry.insertOrUpdateInRecord(face1, interest2);
77 time::Point after3 = time::now();
78 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
79 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
80 BOOST_CHECK(in2 == inRecords3.begin());
81 BOOST_CHECK_EQUAL(in2->getFace(), face1);
82 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2.getNonce());
83 BOOST_CHECK_LE(std::abs(in2->getExpiry() - in2->getLastRenewed()
84 - time::milliseconds(interest2.getInterestLifetime())),
85 (after3 - before3));
86
Junxiao Shicbba04c2014-01-26 14:21:22 -070087 // insert another InRecord
88 pit::InRecordCollection::iterator in3 =
89 entry.insertOrUpdateInRecord(face2, interest3);
90 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
91 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
92 BOOST_CHECK_EQUAL(in3->getFace(), face2);
93
Junxiao Shicbba04c2014-01-26 14:21:22 -070094 // delete all InRecords
95 entry.deleteInRecords();
96 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
97 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
98
Junxiao Shicbba04c2014-01-26 14:21:22 -070099 // insert another OutRecord
100 pit::OutRecordCollection::iterator out2 =
101 entry.insertOrUpdateOutRecord(face2, interest4);
102 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
103 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
104 BOOST_CHECK_EQUAL(out2->getFace(), face2);
105
Junxiao Shicbba04c2014-01-26 14:21:22 -0700106 // delete OutRecord
107 entry.deleteOutRecord(face2);
108 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
109 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
110 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
111
Junxiao Shid3c792f2014-01-30 00:46:13 -0700112}
113
114BOOST_AUTO_TEST_CASE(EntryNonce)
115{
116 Name name("ndn:/qtCQ7I1c");
117 Interest interest(name);
118
119 pit::Entry entry(interest);
120
121 BOOST_CHECK_EQUAL(entry.addNonce(25559), true);
122 BOOST_CHECK_EQUAL(entry.addNonce(25559), false);
123 BOOST_CHECK_EQUAL(entry.addNonce(19004), true);
124 BOOST_CHECK_EQUAL(entry.addNonce(19004), false);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700125}
126
127BOOST_AUTO_TEST_CASE(Insert)
128{
129 Name name1("ndn:/5vzBNnMst");
130 Name name2("ndn:/igSGfEIM62");
131 Exclude exclude0;
132 Exclude exclude1;
133 exclude1.excludeOne(Name::Component("u26p47oep"));
134 Exclude exclude2;
135 exclude2.excludeOne(Name::Component("FG1Ni6nYcf"));
136
137 // base
138 Interest interestA(name1, -1, -1, exclude0, -1, false, -1, -1.0, 0);
139 // A+exclude1
140 Interest interestB(name1, -1, -1, exclude1, -1, false, -1, -1.0, 0);
141 // A+exclude2
142 Interest interestC(name1, -1, -1, exclude2, -1, false, -1, -1.0, 0);
143 // A+MinSuffixComponents
144 Interest interestD(name1, 2, -1, exclude0, -1, false, -1, -1.0, 0);
145 // A+MaxSuffixComponents
146 Interest interestE(name1, -1, 4, exclude0, -1, false, -1, -1.0, 0);
147 // A+ChildSelector
148 Interest interestF(name1, -1, -1, exclude0, 1, false, -1, -1.0, 0);
149 // A+MustBeFresh
150 Interest interestG(name1, -1, -1, exclude0, -1, true, -1, -1.0, 0);
151 // A+Scope
152 Interest interestH(name1, -1, -1, exclude0, -1, false, 2, -1.0, 0);
153 // A+InterestLifetime
154 Interest interestI(name1, -1, -1, exclude0, -1, false, -1, 2000, 0);
155 // A+Nonce
156 Interest interestJ(name1, -1, -1, exclude0, -1, false, -1, -1.0, 2192);
157 // different Name+exclude1
158 Interest interestK(name2, -1, -1, exclude1, -1, false, -1, -1.0, 0);
159
Haowei Yuan78c84d12014-02-27 15:35:13 -0600160 NameTree nameTree(16);
161 Pit pit(nameTree);
162
Junxiao Shicbba04c2014-01-26 14:21:22 -0700163 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
164
Haowei Yuan78c84d12014-02-27 15:35:13 -0600165 BOOST_CHECK_EQUAL(pit.size(), 0);
166
Junxiao Shicbba04c2014-01-26 14:21:22 -0700167 insertResult = pit.insert(interestA);
168 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600169 BOOST_CHECK_EQUAL(pit.size(), 1);
170
Junxiao Shicbba04c2014-01-26 14:21:22 -0700171 insertResult = pit.insert(interestB);
172 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600173 BOOST_CHECK_EQUAL(pit.size(), 2);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700174
175 insertResult = pit.insert(interestC);
176 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600177 BOOST_CHECK_EQUAL(pit.size(), 3);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700178
179 insertResult = pit.insert(interestD);
180 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600181 BOOST_CHECK_EQUAL(pit.size(), 4);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700182
183 insertResult = pit.insert(interestE);
184 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600185 BOOST_CHECK_EQUAL(pit.size(), 5);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700186
187 insertResult = pit.insert(interestF);
188 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600189 BOOST_CHECK_EQUAL(pit.size(), 6);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700190
191 insertResult = pit.insert(interestG);
192 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600193 BOOST_CHECK_EQUAL(pit.size(), 7);
194
Junxiao Shicbba04c2014-01-26 14:21:22 -0700195
196 insertResult = pit.insert(interestH);
197 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600198 BOOST_CHECK_EQUAL(pit.size(), 7);
199
Junxiao Shicbba04c2014-01-26 14:21:22 -0700200 insertResult = pit.insert(interestI);
201 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600202 BOOST_CHECK_EQUAL(pit.size(), 7);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700203
204 insertResult = pit.insert(interestJ);
205 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
Haowei Yuan78c84d12014-02-27 15:35:13 -0600206 BOOST_CHECK_EQUAL(pit.size(), 7);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700207
208 insertResult = pit.insert(interestK);
209 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600210 BOOST_CHECK_EQUAL(pit.size(), 8);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700211}
212
Haowei Yuan78c84d12014-02-27 15:35:13 -0600213BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700214{
215 Interest interest(Name("ndn:/z88Admz6A2"));
216
Haowei Yuan78c84d12014-02-27 15:35:13 -0600217 NameTree nameTree(16);
218 Pit pit(nameTree);
219
Junxiao Shicbba04c2014-01-26 14:21:22 -0700220 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
221
Haowei Yuan78c84d12014-02-27 15:35:13 -0600222 BOOST_CHECK_EQUAL(pit.size(), 0);
223
Junxiao Shicbba04c2014-01-26 14:21:22 -0700224 insertResult = pit.insert(interest);
225 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600226 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700227
228 insertResult = pit.insert(interest);
229 BOOST_CHECK_EQUAL(insertResult.second, false);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600230 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700231
Haowei Yuan78c84d12014-02-27 15:35:13 -0600232 pit.erase(insertResult.first);
233 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700234
235 insertResult = pit.insert(interest);
236 BOOST_CHECK_EQUAL(insertResult.second, true);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600237 BOOST_CHECK_EQUAL(pit.size(), 1);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700238
Haowei Yuan78c84d12014-02-27 15:35:13 -0600239}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700240
241BOOST_AUTO_TEST_CASE(FindAllDataMatches)
242{
243 Name nameA ("ndn:/A");
244 Name nameAB ("ndn:/A/B");
245 Name nameABC("ndn:/A/B/C");
246 Name nameD ("ndn:/D");
247 Interest interestA (nameA );
248 Interest interestAB(nameAB);
249 Interest interestD (nameD );
250
Haowei Yuan78c84d12014-02-27 15:35:13 -0600251 NameTree nameTree(16);
252 Pit pit(nameTree);
253
254 BOOST_CHECK_EQUAL(pit.size(), 0);
255
Junxiao Shicbba04c2014-01-26 14:21:22 -0700256 pit.insert(interestA );
257 pit.insert(interestAB);
258 pit.insert(interestD );
259
Haowei Yuan78c84d12014-02-27 15:35:13 -0600260 BOOST_CHECK_EQUAL(pit.size(), 3);
261
Junxiao Shicbba04c2014-01-26 14:21:22 -0700262 Data data(nameABC);
263
264 shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(data);
265 int count = 0;
266 bool hasA = false;
267 bool hasAB = false;
268 bool hasD = false;
269 for (pit::DataMatchResult::iterator it = matches->begin();
270 it != matches->end(); ++it) {
271 ++count;
272 shared_ptr<pit::Entry> entry = *it;
273 if (entry->getName().equals(nameA )) {
274 hasA = true;
275 }
276 if (entry->getName().equals(nameAB)) {
277 hasAB = true;
278 }
279 if (entry->getName().equals(nameD )) {
280 hasD = true;
281 }
282 }
283 BOOST_CHECK_EQUAL(count, 2);
284 BOOST_CHECK_EQUAL(hasA , true);
285 BOOST_CHECK_EQUAL(hasAB, true);
286 BOOST_CHECK_EQUAL(hasD , false);
287}
288
289BOOST_AUTO_TEST_SUITE_END()
290
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800291} // namespace nfd