blob: 2564799437947635aa15eb0cbcae8f4cd9e17238 [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
16BOOST_AUTO_TEST_CASE(Entry)
17{
18 shared_ptr<Face> face1 = make_shared<DummyFace>(1);
19 shared_ptr<Face> face2 = make_shared<DummyFace>(2);
20 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));
35
36 // isNonceSeen should not record the Nonce
37 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest1.getNonce()), false);
38 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest1.getNonce()), false);
39
40 const pit::InRecordCollection& inRecords1 = entry.getInRecords();
41 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
42 const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
43 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
44
45 // insert InRecord
46 time::Point before1 = time::now();
47 pit::InRecordCollection::iterator in1 =
48 entry.insertOrUpdateInRecord(face1, interest1);
49 time::Point after1 = time::now();
50 const pit::InRecordCollection& inRecords2 = entry.getInRecords();
51 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
52 BOOST_CHECK(in1 == inRecords2.begin());
53 BOOST_CHECK_EQUAL(in1->getFace(), face1);
54 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1.getNonce());
55 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
56 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
57 BOOST_CHECK_LE(std::abs(in1->getExpiry() - in1->getLastRenewed()
58 - time::milliseconds(interest1.getInterestLifetime())),
59 (after1 - before1));
60
61 // insert OutRecord
62 time::Point before2 = time::now();
63 pit::OutRecordCollection::iterator out1 =
64 entry.insertOrUpdateOutRecord(face1, interest1);
65 time::Point after2 = time::now();
66 const pit::OutRecordCollection& outRecords2 = entry.getOutRecords();
67 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
68 BOOST_CHECK(out1 == outRecords2.begin());
69 BOOST_CHECK_EQUAL(out1->getFace(), face1);
70 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1.getNonce());
71 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
72 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
73 BOOST_CHECK_LE(std::abs(out1->getExpiry() - out1->getLastRenewed()
74 - time::milliseconds(interest1.getInterestLifetime())),
75 (after2 - before2));
76
77 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest1.getNonce()), true);
78 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest2.getNonce()), false);
79
80 // update InRecord
81 time::Point before3 = time::now();
82 pit::InRecordCollection::iterator in2 =
83 entry.insertOrUpdateInRecord(face1, interest2);
84 time::Point after3 = time::now();
85 const pit::InRecordCollection& inRecords3 = entry.getInRecords();
86 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
87 BOOST_CHECK(in2 == inRecords3.begin());
88 BOOST_CHECK_EQUAL(in2->getFace(), face1);
89 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2.getNonce());
90 BOOST_CHECK_LE(std::abs(in2->getExpiry() - in2->getLastRenewed()
91 - time::milliseconds(interest2.getInterestLifetime())),
92 (after3 - before3));
93
94 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest1.getNonce()), true);
95 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest2.getNonce()), true);
96 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest3.getNonce()), false);
97
98 // insert another InRecord
99 pit::InRecordCollection::iterator in3 =
100 entry.insertOrUpdateInRecord(face2, interest3);
101 const pit::InRecordCollection& inRecords4 = entry.getInRecords();
102 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
103 BOOST_CHECK_EQUAL(in3->getFace(), face2);
104
105 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest1.getNonce()), true);
106 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest2.getNonce()), true);
107 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest3.getNonce()), true);
108
109 // delete all InRecords
110 entry.deleteInRecords();
111 const pit::InRecordCollection& inRecords5 = entry.getInRecords();
112 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
113
114 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest1.getNonce()), true);
115 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest2.getNonce()), true);
116 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest3.getNonce()), true);
117 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest4.getNonce()), false);
118
119 // insert another OutRecord
120 pit::OutRecordCollection::iterator out2 =
121 entry.insertOrUpdateOutRecord(face2, interest4);
122 const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
123 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
124 BOOST_CHECK_EQUAL(out2->getFace(), face2);
125
126 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest1.getNonce()), true);
127 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest2.getNonce()), true);
128 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest3.getNonce()), true);
129 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest4.getNonce()), true);
130
131 // delete OutRecord
132 entry.deleteOutRecord(face2);
133 const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
134 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
135 BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
136
137 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest1.getNonce()), true);
138 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest2.getNonce()), true);
139 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest3.getNonce()), true);
140 BOOST_CHECK_EQUAL(entry.isNonceSeen(interest4.getNonce()), true);
141}
142
143BOOST_AUTO_TEST_CASE(Insert)
144{
145 Name name1("ndn:/5vzBNnMst");
146 Name name2("ndn:/igSGfEIM62");
147 Exclude exclude0;
148 Exclude exclude1;
149 exclude1.excludeOne(Name::Component("u26p47oep"));
150 Exclude exclude2;
151 exclude2.excludeOne(Name::Component("FG1Ni6nYcf"));
152
153 // base
154 Interest interestA(name1, -1, -1, exclude0, -1, false, -1, -1.0, 0);
155 // A+exclude1
156 Interest interestB(name1, -1, -1, exclude1, -1, false, -1, -1.0, 0);
157 // A+exclude2
158 Interest interestC(name1, -1, -1, exclude2, -1, false, -1, -1.0, 0);
159 // A+MinSuffixComponents
160 Interest interestD(name1, 2, -1, exclude0, -1, false, -1, -1.0, 0);
161 // A+MaxSuffixComponents
162 Interest interestE(name1, -1, 4, exclude0, -1, false, -1, -1.0, 0);
163 // A+ChildSelector
164 Interest interestF(name1, -1, -1, exclude0, 1, false, -1, -1.0, 0);
165 // A+MustBeFresh
166 Interest interestG(name1, -1, -1, exclude0, -1, true, -1, -1.0, 0);
167 // A+Scope
168 Interest interestH(name1, -1, -1, exclude0, -1, false, 2, -1.0, 0);
169 // A+InterestLifetime
170 Interest interestI(name1, -1, -1, exclude0, -1, false, -1, 2000, 0);
171 // A+Nonce
172 Interest interestJ(name1, -1, -1, exclude0, -1, false, -1, -1.0, 2192);
173 // different Name+exclude1
174 Interest interestK(name2, -1, -1, exclude1, -1, false, -1, -1.0, 0);
175
176 Pit pit;
177 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
178
179 insertResult = pit.insert(interestA);
180 BOOST_CHECK_EQUAL(insertResult.second, true);
181
182 insertResult = pit.insert(interestB);
183 BOOST_CHECK_EQUAL(insertResult.second, true);
184
185 insertResult = pit.insert(interestC);
186 BOOST_CHECK_EQUAL(insertResult.second, true);
187
188 insertResult = pit.insert(interestD);
189 BOOST_CHECK_EQUAL(insertResult.second, true);
190
191 insertResult = pit.insert(interestE);
192 BOOST_CHECK_EQUAL(insertResult.second, true);
193
194 insertResult = pit.insert(interestF);
195 BOOST_CHECK_EQUAL(insertResult.second, true);
196
197 insertResult = pit.insert(interestG);
198 BOOST_CHECK_EQUAL(insertResult.second, true);
199
200 insertResult = pit.insert(interestH);
201 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
202
203 insertResult = pit.insert(interestI);
204 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
205
206 insertResult = pit.insert(interestJ);
207 BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
208
209 insertResult = pit.insert(interestK);
210 BOOST_CHECK_EQUAL(insertResult.second, true);
211}
212
213BOOST_AUTO_TEST_CASE(Remove)
214{
215 Interest interest(Name("ndn:/z88Admz6A2"));
216
217 Pit pit;
218 std::pair<shared_ptr<pit::Entry>, bool> insertResult;
219
220 insertResult = pit.insert(interest);
221 BOOST_CHECK_EQUAL(insertResult.second, true);
222
223 insertResult = pit.insert(interest);
224 BOOST_CHECK_EQUAL(insertResult.second, false);
225
226 pit.remove(insertResult.first);
227
228 insertResult = pit.insert(interest);
229 BOOST_CHECK_EQUAL(insertResult.second, true);
230}
231
232
233BOOST_AUTO_TEST_CASE(FindAllDataMatches)
234{
235 Name nameA ("ndn:/A");
236 Name nameAB ("ndn:/A/B");
237 Name nameABC("ndn:/A/B/C");
238 Name nameD ("ndn:/D");
239 Interest interestA (nameA );
240 Interest interestAB(nameAB);
241 Interest interestD (nameD );
242
243 Pit pit;
244 pit.insert(interestA );
245 pit.insert(interestAB);
246 pit.insert(interestD );
247
248 Data data(nameABC);
249
250 shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(data);
251 int count = 0;
252 bool hasA = false;
253 bool hasAB = false;
254 bool hasD = false;
255 for (pit::DataMatchResult::iterator it = matches->begin();
256 it != matches->end(); ++it) {
257 ++count;
258 shared_ptr<pit::Entry> entry = *it;
259 if (entry->getName().equals(nameA )) {
260 hasA = true;
261 }
262 if (entry->getName().equals(nameAB)) {
263 hasAB = true;
264 }
265 if (entry->getName().equals(nameD )) {
266 hasD = true;
267 }
268 }
269 BOOST_CHECK_EQUAL(count, 2);
270 BOOST_CHECK_EQUAL(hasA , true);
271 BOOST_CHECK_EQUAL(hasAB, true);
272 BOOST_CHECK_EQUAL(hasD , false);
273}
274
275BOOST_AUTO_TEST_SUITE_END()
276
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800277} // namespace nfd