blob: f236d77dcec7a01ee95f134b056127dadbfbf848 [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -08009 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * \author Ilya Moiseenko <iliamo@ucla.edu>
25 **/
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070026
27#include "table/cs.hpp"
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080028#include "table/cs-entry.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080029
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030#include "tests/test-common.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080031
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080032#include <ndn-cpp-dev/security/key-chain.hpp>
33
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080034namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070036
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080037class CsAccessor : public Cs
38{
39public:
40
41 bool
42 evictItem_accessor()
43 {
44 return evictItem();
45 }
46};
47
Junxiao Shid9ee45c2014-02-27 15:38:11 -070048BOOST_FIXTURE_TEST_SUITE(TableCs, BaseFixture)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070049
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080050BOOST_AUTO_TEST_CASE(Insertion)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070051{
52 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070053
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080054 shared_ptr<Data> data = make_shared<Data>("/insertion");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070055
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080056 ndn::SignatureSha256WithRsa fakeSignature;
57 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
58 data->setSignature(fakeSignature);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070059
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080060 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070061}
62
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080063BOOST_AUTO_TEST_CASE(Insertion2)
64{
65 Cs cs;
66
67 ndn::SignatureSha256WithRsa fakeSignature;
68 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070069
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080070 shared_ptr<Data> data = make_shared<Data>("/a");
71 data->setSignature(fakeSignature);
72 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070073
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080074 shared_ptr<Data> data2 = make_shared<Data>("/b");
75 data2->setSignature(fakeSignature);
76 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070077
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080078 shared_ptr<Data> data3 = make_shared<Data>("/c");
79 data3->setSignature(fakeSignature);
80 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070081
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080082 shared_ptr<Data> data4 = make_shared<Data>("/d");
83 data4->setSignature(fakeSignature);
84 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070085
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080086 BOOST_CHECK_EQUAL(cs.size(), 4);
87}
88
89
90BOOST_AUTO_TEST_CASE(DuplicateInsertion)
91{
92 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070093
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080094 ndn::SignatureSha256WithRsa fakeSignature;
95 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070096
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080097 shared_ptr<Data> data0 = make_shared<Data>("/insert/smth");
98 data0->setSignature(fakeSignature);
99 BOOST_CHECK_EQUAL(cs.insert(*data0), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700100
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800101 shared_ptr<Data> data = make_shared<Data>("/insert/duplicate");
102 data->setSignature(fakeSignature);
103 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700104
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800105 cs.insert(*data);
106 BOOST_CHECK_EQUAL(cs.size(), 2);
107}
108
109
110BOOST_AUTO_TEST_CASE(DuplicateInsertion2)
111{
112 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700113
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800114 ndn::SignatureSha256WithRsa fakeSignature;
115 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700116
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800117 shared_ptr<Data> data = make_shared<Data>("/insert/duplicate");
118 data->setSignature(fakeSignature);
119 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700120
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800121 cs.insert(*data);
122 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700123
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800124 shared_ptr<Data> data2 = make_shared<Data>("/insert/original");
125 data2->setSignature(fakeSignature);
126 BOOST_CHECK_EQUAL(cs.insert(*data2), true);
127 BOOST_CHECK_EQUAL(cs.size(), 2);
128}
129
130BOOST_AUTO_TEST_CASE(InsertAndFind)
131{
132 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700133
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800134 ndn::SignatureSha256WithRsa fakeSignature;
135 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700136
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800137 Name name("/insert/and/find");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700138
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800139 shared_ptr<Data> data = make_shared<Data>(name);
140 data->setSignature(fakeSignature);
141 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700142
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800143 shared_ptr<Interest> interest = make_shared<Interest>(name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700144
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800145 const Data* found = cs.find(*interest);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700146
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800147 BOOST_REQUIRE(found != 0);
148 BOOST_CHECK_EQUAL(data->getName(), found->getName());
149}
150
151BOOST_AUTO_TEST_CASE(InsertAndNotFind)
152{
153 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700154
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800155 ndn::SignatureSha256WithRsa fakeSignature;
156 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700157
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800158 Name name("/insert/and/find");
159 shared_ptr<Data> data = make_shared<Data>(name);
160 data->setSignature(fakeSignature);
161 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700162
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800163 Name name2("/not/find");
164 shared_ptr<Interest> interest = make_shared<Interest>(name2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700165
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800166 const Data* found = cs.find(*interest);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700167
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800168 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
169}
170
171
172BOOST_AUTO_TEST_CASE(InsertAndErase)
173{
174 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700175
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800176 ndn::SignatureSha256WithRsa fakeSignature;
177 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700178
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800179 shared_ptr<Data> data = make_shared<Data>("/insertandelete");
180 data->setSignature(fakeSignature);
181 cs.insert(*data);
182 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700183
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800184 cs.evictItem_accessor();
185 BOOST_CHECK_EQUAL(cs.size(), 0);
186}
187
188BOOST_AUTO_TEST_CASE(StalenessQueue)
189{
190 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700191
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800192 ndn::SignatureSha256WithRsa fakeSignature;
193 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700194
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800195 Name name2("/insert/fresh");
196 shared_ptr<Data> data2 = make_shared<Data>(name2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700197 data2->setFreshnessPeriod(time::milliseconds(5000));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800198 data2->setSignature(fakeSignature);
199 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700200
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800201 Name name("/insert/expire");
202 shared_ptr<Data> data = make_shared<Data>(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700203 data->setFreshnessPeriod(time::milliseconds(500));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800204 data->setSignature(fakeSignature);
205 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700206
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800207 BOOST_CHECK_EQUAL(cs.size(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700208
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800209 sleep(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700210
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800211 cs.evictItem_accessor();
212 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700213
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800214 shared_ptr<Interest> interest = make_shared<Interest>(name2);
215 const Data* found = cs.find(*interest);
216 BOOST_REQUIRE(found != 0);
217 BOOST_CHECK_EQUAL(found->getName(), name2);
218}
219
220//even max size
221BOOST_AUTO_TEST_CASE(ReplacementEvenSize)
222{
223 Cs cs(4);
224
225 ndn::SignatureSha256WithRsa fakeSignature;
226 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
227
228 shared_ptr<Data> data = make_shared<Data>("/a");
229 data->setSignature(fakeSignature);
230 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700231
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800232 shared_ptr<Data> data2 = make_shared<Data>("/b");
233 data2->setSignature(fakeSignature);
234 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700235
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800236 shared_ptr<Data> data3 = make_shared<Data>("/c");
237 data3->setSignature(fakeSignature);
238 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700239
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800240 shared_ptr<Data> data4 = make_shared<Data>("/d");
241 data4->setSignature(fakeSignature);
242 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700243
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800244 shared_ptr<Data> data5 = make_shared<Data>("/e");
245 data5->setSignature(fakeSignature);
246 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700247
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800248 BOOST_CHECK_EQUAL(cs.size(), 4);
249}
250
251//odd max size
252BOOST_AUTO_TEST_CASE(Replacement2)
253{
254 Cs cs(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700255
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800256 ndn::SignatureSha256WithRsa fakeSignature;
257 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
258
259 shared_ptr<Data> data = make_shared<Data>("/a");
260 data->setSignature(fakeSignature);
261 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700262
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800263 shared_ptr<Data> data2 = make_shared<Data>("/b");
264 data2->setSignature(fakeSignature);
265 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700266
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800267 shared_ptr<Data> data3 = make_shared<Data>("/c");
268 data3->setSignature(fakeSignature);
269 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700270
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800271 shared_ptr<Data> data4 = make_shared<Data>("/d");
272 data4->setSignature(fakeSignature);
273 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700274
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800275 BOOST_CHECK_EQUAL(cs.size(), 3);
276}
277
278BOOST_AUTO_TEST_CASE(InsertAndEraseByName)
279{
280 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700281
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800282 ndn::SignatureSha256WithRsa fakeSignature;
283 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
284
285 Name name("/insertandremovebyname");
286 shared_ptr<Data> data = make_shared<Data>(name);
287 data->setSignature(fakeSignature);
288 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700289
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800290 uint8_t digest1[32];
291 ndn::ndn_digestSha256(data->wireEncode().wire(), data->wireEncode().size(), digest1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700292
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800293 shared_ptr<Data> data2 = make_shared<Data>("/a");
294 data2->setSignature(fakeSignature);
295 cs.insert(*data2);
296
297 shared_ptr<Data> data3 = make_shared<Data>("/z");
298 data3->setSignature(fakeSignature);
299 cs.insert(*data3);
300
301 BOOST_CHECK_EQUAL(cs.size(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700302
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800303 name.append(digest1, 32);
304 cs.erase(name);
305 BOOST_CHECK_EQUAL(cs.size(), 2);
306}
307
308BOOST_AUTO_TEST_CASE(DigestCalculation)
309{
310 ndn::SignatureSha256WithRsa fakeSignature;
311 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
312
313 shared_ptr<Data> data = make_shared<Data>("/digest/compute");
314 data->setSignature(fakeSignature);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700315
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800316 uint8_t digest1[32];
317 ndn::ndn_digestSha256(data->wireEncode().wire(), data->wireEncode().size(), digest1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700318
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800319 cs::Entry* entry = new cs::Entry(*data, false);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700320
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800321 BOOST_CHECK_EQUAL_COLLECTIONS(digest1, digest1+32,
322 entry->getDigest()->buf(), entry->getDigest()->buf()+32);
323}
324
325BOOST_AUTO_TEST_CASE(InsertCanonical)
326{
327 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700328
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800329 ndn::SignatureSha256WithRsa fakeSignature;
330 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700331
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800332 shared_ptr<Data> data = make_shared<Data>("/a");
333 data->setSignature(fakeSignature);
334 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700335
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800336 shared_ptr<Data> data2 = make_shared<Data>("/b");
337 data2->setSignature(fakeSignature);
338 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700339
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800340 shared_ptr<Data> data3 = make_shared<Data>("/c");
341 data3->setSignature(fakeSignature);
342 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700343
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800344 shared_ptr<Data> data4 = make_shared<Data>("/d");
345 data4->setSignature(fakeSignature);
346 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700347
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800348 shared_ptr<Data> data5 = make_shared<Data>("/c/c/1/2/3/4/5/6");
349 data5->setSignature(fakeSignature);
350 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700351
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800352 shared_ptr<Data> data6 = make_shared<Data>("/c/c/1/2/3");
353 data6->setSignature(fakeSignature);
354 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700355
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800356 shared_ptr<Data> data7 = make_shared<Data>("/c/c/1");
357 data7->setSignature(fakeSignature);
358 cs.insert(*data7);
359}
360
361BOOST_AUTO_TEST_CASE(EraseCanonical)
362{
363 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700364
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800365 ndn::SignatureSha256WithRsa fakeSignature;
366 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700367
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800368 shared_ptr<Data> data = make_shared<Data>("/a");
369 data->setSignature(fakeSignature);
370 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700371
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800372 shared_ptr<Data> data2 = make_shared<Data>("/b");
373 data2->setSignature(fakeSignature);
374 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700375
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800376 shared_ptr<Data> data3 = make_shared<Data>("/c");
377 data3->setSignature(fakeSignature);
378 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700379
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800380 shared_ptr<Data> data4 = make_shared<Data>("/d");
381 data4->setSignature(fakeSignature);
382 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700383
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800384 uint8_t digest1[32];
385 ndn::ndn_digestSha256(data->wireEncode().wire(), data->wireEncode().size(), digest1);
386
387 shared_ptr<Data> data5 = make_shared<Data>("/c/c/1/2/3/4/5/6");
388 data5->setSignature(fakeSignature);
389 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700390
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800391 shared_ptr<Data> data6 = make_shared<Data>("/c/c/1/2/3");
392 data6->setSignature(fakeSignature);
393 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700394
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800395 shared_ptr<Data> data7 = make_shared<Data>("/c/c/1");
396 data7->setSignature(fakeSignature);
397 cs.insert(*data7);
398
399 Name name("/a");
400 name.append(digest1,32);
401 cs.erase(name);
402 BOOST_CHECK_EQUAL(cs.size(), 6);
403}
404
405BOOST_AUTO_TEST_CASE(ImplicitDigestSelector)
406{
407 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700408
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800409 ndn::SignatureSha256WithRsa fakeSignature;
410 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700411
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800412 Name name("/digest/works");
413 shared_ptr<Data> data = make_shared<Data>(name);
414 data->setSignature(fakeSignature);
415 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700416
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800417 shared_ptr<Data> data2 = make_shared<Data>("/a");
418 data2->setSignature(fakeSignature);
419 cs.insert(*data2);
420
421 shared_ptr<Data> data3 = make_shared<Data>("/z/z/z");
422 data3->setSignature(fakeSignature);
423 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700424
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800425 uint8_t digest1[32];
426 ndn::ndn_digestSha256(data->wireEncode().wire(), data->wireEncode().size(), digest1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700427
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800428 uint8_t digest2[32] = {0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1};
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700429
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800430 shared_ptr<Interest> interest = make_shared<Interest>(boost::cref(Name(name).append(digest1, 32)));
431 interest->setMinSuffixComponents(0);
432 interest->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700433
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800434 const Data* found = cs.find(*interest);
435 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
436 BOOST_CHECK_EQUAL(found->getName(), name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700437
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800438 shared_ptr<Interest> interest2 = make_shared<Interest>(boost::cref(Name(name).append(digest2, 32)));
439 interest2->setMinSuffixComponents(0);
440 interest2->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700441
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800442 const Data* notfound = cs.find(*interest2);
443 BOOST_CHECK_EQUAL(notfound, static_cast<const Data*>(0));
444}
445
446BOOST_AUTO_TEST_CASE(ChildSelector)
447{
448 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700449
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800450 ndn::SignatureSha256WithRsa fakeSignature;
451 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700452
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800453 shared_ptr<Data> data = make_shared<Data>("/a");
454 data->setSignature(fakeSignature);
455 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700456
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800457 shared_ptr<Data> data2 = make_shared<Data>("/b");
458 data2->setSignature(fakeSignature);
459 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700460
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800461 shared_ptr<Data> data4 = make_shared<Data>("/d");
462 data4->setSignature(fakeSignature);
463 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700464
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800465 shared_ptr<Data> data5 = make_shared<Data>("/c/c");
466 data5->setSignature(fakeSignature);
467 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700468
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800469 shared_ptr<Data> data6 = make_shared<Data>("/c/f");
470 data6->setSignature(fakeSignature);
471 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700472
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800473 shared_ptr<Data> data7 = make_shared<Data>("/c/n");
474 data7->setSignature(fakeSignature);
475 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700476
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800477 shared_ptr<Interest> interest = make_shared<Interest>("/c");
478 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700479
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800480 const Data* found = cs.find(*interest);
481 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
482
483 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
484 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700485
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800486 const Data* found2 = cs.find(*interest2);
487 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
488}
489
490BOOST_AUTO_TEST_CASE(ChildSelector2)
491{
492 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700493
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800494 ndn::SignatureSha256WithRsa fakeSignature;
495 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
496
497 shared_ptr<Data> data = make_shared<Data>("/a/b/1");
498 data->setSignature(fakeSignature);
499 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700500
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800501 shared_ptr<Data> data2 = make_shared<Data>("/a/b/2");
502 data2->setSignature(fakeSignature);
503 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700504
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800505 shared_ptr<Data> data3 = make_shared<Data>("/a/z/1");
506 data3->setSignature(fakeSignature);
507 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700508
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800509 shared_ptr<Data> data4 = make_shared<Data>("/a/z/2");
510 data4->setSignature(fakeSignature);
511 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700512
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800513 shared_ptr<Interest> interest = make_shared<Interest>("/a");
514 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700515
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800516 const Data* found = cs.find(*interest);
517 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
518}
519
520BOOST_AUTO_TEST_CASE(MustBeFreshSelector)
521{
522 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700523
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800524 ndn::SignatureSha256WithRsa fakeSignature;
525 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700526
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800527 Name name("/insert/nonfresh");
528 shared_ptr<Data> data = make_shared<Data>(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700529 data->setFreshnessPeriod(time::milliseconds(500));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800530 data->setSignature(fakeSignature);
531 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700532
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800533 sleep(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700534
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800535 shared_ptr<Interest> interest = make_shared<Interest>(name);
536 interest->setMustBeFresh(true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700537
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800538 const Data* found = cs.find(*interest);
539 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
540}
541
542BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
543{
544 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700545
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800546 ndn::SignatureSha256WithRsa fakeSignature;
547 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700548
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800549 shared_ptr<Data> data = make_shared<Data>("/a");
550 data->setSignature(fakeSignature);
551 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700552
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800553 shared_ptr<Data> data2 = make_shared<Data>("/b");
554 data2->setSignature(fakeSignature);
555 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700556
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800557 shared_ptr<Data> data4 = make_shared<Data>("/d");
558 data4->setSignature(fakeSignature);
559 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700560
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800561 shared_ptr<Data> data5 = make_shared<Data>("/c/c/1/2/3/4/5/6");
562 data5->setSignature(fakeSignature);
563 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700564
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800565 shared_ptr<Data> data6 = make_shared<Data>("/c/c/6/7/8/9");
566 data6->setSignature(fakeSignature);
567 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700568
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800569 shared_ptr<Data> data7 = make_shared<Data>("/c/c/1/2/3");
570 data7->setSignature(fakeSignature);
571 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700572
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800573 shared_ptr<Data> data8 = make_shared<Data>("/c/c/1");
574 data8->setSignature(fakeSignature);
575 cs.insert(*data8);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700576
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800577 shared_ptr<Interest> interest = make_shared<Interest>("/c/c");
578 interest->setMinSuffixComponents(3);
579 interest->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700580
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800581 const Data* found = cs.find(*interest);
582 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6");
583
584 shared_ptr<Interest> interest2 = make_shared<Interest>("/c/c");
585 interest2->setMinSuffixComponents(4);
586 interest2->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700587
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800588 const Data* found2 = cs.find(*interest2);
589 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700590
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800591 shared_ptr<Interest> interest3 = make_shared<Interest>("/c/c");
592 interest3->setMaxSuffixComponents(2);
593 interest3->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700594
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800595 const Data* found3 = cs.find(*interest3);
596 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
597}
598
599BOOST_AUTO_TEST_CASE(ExcludeSelector)
600{
601 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700602
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800603 ndn::SignatureSha256WithRsa fakeSignature;
604 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700605
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800606 shared_ptr<Data> data = make_shared<Data>("/a");
607 data->setSignature(fakeSignature);
608 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700609
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800610 shared_ptr<Data> data2 = make_shared<Data>("/b");
611 data2->setSignature(fakeSignature);
612 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700613
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800614 shared_ptr<Data> data3 = make_shared<Data>("/c/a");
615 data3->setSignature(fakeSignature);
616 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700617
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800618 shared_ptr<Data> data4 = make_shared<Data>("/d");
619 data4->setSignature(fakeSignature);
620 cs.insert(*data4);
621
622 shared_ptr<Data> data5 = make_shared<Data>("/c/c");
623 data5->setSignature(fakeSignature);
624 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700625
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800626 shared_ptr<Data> data6 = make_shared<Data>("/c/f");
627 data6->setSignature(fakeSignature);
628 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700629
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800630 shared_ptr<Data> data7 = make_shared<Data>("/c/n");
631 data7->setSignature(fakeSignature);
632 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700633
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800634 shared_ptr<Interest> interest = make_shared<Interest>("/c");
635 interest->setChildSelector(1);
636 Exclude e;
637 e.excludeOne (Name::Component("n"));
638 interest->setExclude(e);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700639
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800640 const Data* found = cs.find(*interest);
641 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
642
643 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
644 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700645
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800646 Exclude e2;
647 e2.excludeOne (Name::Component("a"));
648 interest2->setExclude(e2);
649
650 const Data* found2 = cs.find(*interest2);
651 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700652
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800653 shared_ptr<Interest> interest3 = make_shared<Interest>("/c");
654 interest3->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700655
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800656 Exclude e3;
657 e3.excludeOne (Name::Component("c"));
658 interest3->setExclude(e3);
659
660 const Data* found3 = cs.find(*interest3);
661 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
662}
663
664//
665
666class FindFixture : protected BaseFixture
667{
668protected:
669 void
670 insert(uint32_t id, const Name& name)
671 {
672 shared_ptr<Data> data = make_shared<Data>(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700673 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800674 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700675
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800676 ndn::SignatureSha256WithRsa fakeSignature;
677 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
678 data->setSignature(fakeSignature);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700679
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800680 m_cs.insert(*data);
681 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700682
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800683 Interest&
684 startInterest(const Name& name)
685 {
686 m_interest = make_shared<Interest>(name);
687 return *m_interest;
688 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700689
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800690 uint32_t
691 find()
692 {
693 const Data* found = m_cs.find(*m_interest);
694 if (found == 0) {
695 return 0;
696 }
697 const Block& content = found->getContent();
698 if (content.value_size() != sizeof(uint32_t)) {
699 return 0;
700 }
701 return *reinterpret_cast<const uint32_t*>(content.value());
702 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700703
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800704protected:
705 Cs m_cs;
706 shared_ptr<Interest> m_interest;
707};
708
709BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
710
711BOOST_AUTO_TEST_CASE(EmptyDataName)
712{
713 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700714
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800715 startInterest("ndn:/");
716 BOOST_CHECK_EQUAL(find(), 1);
717}
718
719BOOST_AUTO_TEST_CASE(EmptyInterestName)
720{
721 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700722
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800723 startInterest("ndn:/");
724 BOOST_CHECK_EQUAL(find(), 1);
725}
726
727BOOST_AUTO_TEST_CASE(Leftmost)
728{
729 insert(1, "ndn:/A");
730 insert(2, "ndn:/B/p/1");
731 insert(3, "ndn:/B/p/2");
732 insert(4, "ndn:/B/q/1");
733 insert(5, "ndn:/B/q/2");
734 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700735
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800736 startInterest("ndn:/B");
737 BOOST_CHECK_EQUAL(find(), 2);
738}
739
740BOOST_AUTO_TEST_CASE(Rightmost)
741{
742 insert(1, "ndn:/A");
743 insert(2, "ndn:/B/p/1");
744 insert(3, "ndn:/B/p/2");
745 insert(4, "ndn:/B/q/1");
746 insert(5, "ndn:/B/q/2");
747 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700748
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800749 startInterest("ndn:/B")
750 .setChildSelector(1);
751 BOOST_CHECK_EQUAL(find(), 4);
752}
753
754BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
755{
756 insert(1, "ndn:/");
757 insert(2, "ndn:/A/B");
758 insert(3, "ndn:/A/C");
759 insert(4, "ndn:/A");
760 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700761
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800762 // Intuitively you would think Data 4 should be between Data 1 and 2,
763 // but Data 4 has full Name ndn:/A/<32-octet hash>.
764 startInterest("ndn:/A");
765 BOOST_CHECK_EQUAL(find(), 2);
766}
767
768BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
769{
770 insert(1, "ndn:/");
771 insert(2, "ndn:/A");
772 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
773 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
774 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700775
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800776 // Data 2 is returned, because <32-octet hash> is less than Data 3.
777 startInterest("ndn:/A");
778 BOOST_CHECK_EQUAL(find(), 2);
779}
780
781BOOST_AUTO_TEST_CASE(MinSuffixComponents)
782{
783 insert(1, "ndn:/A/1/2/3/4");
784 insert(2, "ndn:/B/1/2/3");
785 insert(3, "ndn:/C/1/2");
786 insert(4, "ndn:/D/1");
787 insert(5, "ndn:/E");
788 insert(6, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700789
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800790 startInterest("ndn:/")
791 .setChildSelector(1)
792 .setMinSuffixComponents(0);
793 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700794
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800795 startInterest("ndn:/")
796 .setChildSelector(1)
797 .setMinSuffixComponents(1);
798 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700799
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800800 startInterest("ndn:/")
801 .setChildSelector(1)
802 .setMinSuffixComponents(2);
803 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700804
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800805 startInterest("ndn:/")
806 .setChildSelector(1)
807 .setMinSuffixComponents(3);
808 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700809
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800810 startInterest("ndn:/")
811 .setChildSelector(1)
812 .setMinSuffixComponents(4);
813 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700814
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800815 startInterest("ndn:/")
816 .setChildSelector(1)
817 .setMinSuffixComponents(5);
818 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700819
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800820 startInterest("ndn:/")
821 .setChildSelector(1)
822 .setMinSuffixComponents(6);
823 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700824
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800825 startInterest("ndn:/")
826 .setChildSelector(1)
827 .setMinSuffixComponents(7);
828 BOOST_CHECK_EQUAL(find(), 0);
829}
830
831BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
832{
833 insert(1, "ndn:/");
834 insert(2, "ndn:/A");
835 insert(3, "ndn:/A/B");
836 insert(4, "ndn:/A/B/C");
837 insert(5, "ndn:/A/B/C/D");
838 insert(6, "ndn:/A/B/C/D/E");
839 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700840
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800841 startInterest("ndn:/")
842 .setMaxSuffixComponents(0);
843 BOOST_CHECK_EQUAL(find(), 0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700844
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800845 startInterest("ndn:/")
846 .setMaxSuffixComponents(1);
847 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700848
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800849 startInterest("ndn:/")
850 .setMaxSuffixComponents(2);
851 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700852
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800853 startInterest("ndn:/")
854 .setMaxSuffixComponents(3);
855 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700856
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800857 startInterest("ndn:/")
858 .setMaxSuffixComponents(4);
859 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700860
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800861 startInterest("ndn:/")
862 .setMaxSuffixComponents(5);
863 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700864
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800865 startInterest("ndn:/")
866 .setMaxSuffixComponents(6);
867 BOOST_CHECK_EQUAL(find(), 6);
868}
869
870BOOST_AUTO_TEST_CASE(DigestOrder)
871{
872 insert(1, "ndn:/A");
873 insert(2, "ndn:/A");
874 // We don't know which comes first, but there must be some order
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700875
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800876 startInterest("ndn:/A")
877 .setChildSelector(0);
878 uint32_t leftmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700879
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800880 startInterest("ndn:/A")
881 .setChildSelector(1);
882 uint32_t rightmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700883
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800884 BOOST_CHECK_NE(leftmost, rightmost);
885}
886
887BOOST_AUTO_TEST_CASE(DigestExclude)
888{
889 insert(1, "ndn:/A/B");
890 insert(2, "ndn:/A");
891 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700892
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800893 startInterest("ndn:/A")
894 .setExclude(Exclude().excludeBefore(ndn::name::Component(reinterpret_cast<const uint8_t*>(
895 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
896 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
897 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700898
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800899 startInterest("ndn:/A")
900 .setChildSelector(1)
901 .setExclude(Exclude().excludeAfter(ndn::name::Component(reinterpret_cast<const uint8_t*>(
902 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
903 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
904 "\x00"), 33))); // 33 0x00's
905 BOOST_CHECK_EQUAL(find(), 2);
906}
907
908BOOST_AUTO_TEST_SUITE_END()
909
Junxiao Shi0fcb41e2014-01-24 10:29:43 -0700910BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800911
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700912} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800913} // namespace nfd