blob: a09d4ed5ade5cf410d981121b56816b66c56455c [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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,
9 * The University of Memphis.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * \author Ilya Moiseenko <iliamo@ucla.edu>
Alexander Afanasyev319f2c82015-01-07 14:56:53 -080026 */
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070027
28#include "table/cs.hpp"
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080029#include "table/cs-entry.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080030
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031#include "tests/test-common.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070035
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080036class CsAccessor : public Cs
37{
38public:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080039 bool
40 evictItem_accessor()
41 {
42 return evictItem();
43 }
44};
45
Junxiao Shid9ee45c2014-02-27 15:38:11 -070046BOOST_FIXTURE_TEST_SUITE(TableCs, BaseFixture)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070047
Alexander Afanasyev281b9162014-06-08 10:15:20 +030048BOOST_AUTO_TEST_CASE(SetLimit)
49{
50 Cs cs(1);
51
52 BOOST_CHECK_EQUAL(cs.insert(*makeData("/1")), true);
53 BOOST_CHECK_EQUAL(cs.insert(*makeData("/2")), true);
54 BOOST_CHECK_EQUAL(cs.size(), 1);
55
56 cs.setLimit(3);
57 BOOST_CHECK_EQUAL(cs.insert(*makeData("/3")), true);
58 BOOST_CHECK_EQUAL(cs.insert(*makeData("/4")), true);
59 BOOST_CHECK_EQUAL(cs.insert(*makeData("/5")), true);
60 BOOST_CHECK_EQUAL(cs.size(), 3);
61
62 cs.setLimit(2);
63 BOOST_CHECK_EQUAL(cs.size(), 2);
64}
65
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080066BOOST_AUTO_TEST_CASE(Insertion)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070067{
68 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070069
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040070 BOOST_CHECK_EQUAL(cs.insert(*makeData("/insertion")), true);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070071}
72
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080073BOOST_AUTO_TEST_CASE(Insertion2)
74{
75 Cs cs;
76
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040077 cs.insert(*makeData("/a"));
78 cs.insert(*makeData("/b"));
79 cs.insert(*makeData("/c"));
80 cs.insert(*makeData("/d"));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070081
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080082 BOOST_CHECK_EQUAL(cs.size(), 4);
83}
84
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080085BOOST_AUTO_TEST_CASE(DuplicateInsertion)
86{
87 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070088
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040089 shared_ptr<Data> data0 = makeData("/insert/smth");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080090 BOOST_CHECK_EQUAL(cs.insert(*data0), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070091
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040092 shared_ptr<Data> data = makeData("/insert/duplicate");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080093 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070094
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080095 cs.insert(*data);
96 BOOST_CHECK_EQUAL(cs.size(), 2);
97}
98
99
100BOOST_AUTO_TEST_CASE(DuplicateInsertion2)
101{
102 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700103
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400104 shared_ptr<Data> data = makeData("/insert/duplicate");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800105 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700106
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800107 cs.insert(*data);
108 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700109
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400110 shared_ptr<Data> data2 = makeData("/insert/original");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800111 BOOST_CHECK_EQUAL(cs.insert(*data2), true);
112 BOOST_CHECK_EQUAL(cs.size(), 2);
113}
114
115BOOST_AUTO_TEST_CASE(InsertAndFind)
116{
117 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700118
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800119 Name name("/insert/and/find");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700120
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400121 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800122 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700123
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800124 shared_ptr<Interest> interest = make_shared<Interest>(name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700125
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800126 const Data* found = cs.find(*interest);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700127
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800128 BOOST_REQUIRE(found != 0);
129 BOOST_CHECK_EQUAL(data->getName(), found->getName());
130}
131
132BOOST_AUTO_TEST_CASE(InsertAndNotFind)
133{
134 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700135
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800136 Name name("/insert/and/find");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400137 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800138 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700139
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800140 Name name2("/not/find");
141 shared_ptr<Interest> interest = make_shared<Interest>(name2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700142
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800143 const Data* found = cs.find(*interest);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700144
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800145 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
146}
147
148
149BOOST_AUTO_TEST_CASE(InsertAndErase)
150{
151 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700152
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400153 shared_ptr<Data> data = makeData("/insertandelete");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800154 cs.insert(*data);
155 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700156
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800157 cs.evictItem_accessor();
158 BOOST_CHECK_EQUAL(cs.size(), 0);
159}
160
161BOOST_AUTO_TEST_CASE(StalenessQueue)
162{
163 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700164
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800165 Name name2("/insert/fresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400166 shared_ptr<Data> data2 = makeData(name2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700167 data2->setFreshnessPeriod(time::milliseconds(5000));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700168 signData(data2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800169 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700170
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800171 Name name("/insert/expire");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400172 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700173 data->setFreshnessPeriod(time::milliseconds(500));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700174 signData(data);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800175 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700176
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800177 BOOST_CHECK_EQUAL(cs.size(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700178
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400179 sleep(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700180
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800181 cs.evictItem_accessor();
182 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700183
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800184 shared_ptr<Interest> interest = make_shared<Interest>(name2);
185 const Data* found = cs.find(*interest);
186 BOOST_REQUIRE(found != 0);
187 BOOST_CHECK_EQUAL(found->getName(), name2);
188}
189
Alexander Afanasyev4cf41702014-10-19 23:46:10 -0400190BOOST_AUTO_TEST_CASE(Bug2043) // eviction order of unsolicited packets
191{
192 Cs cs(3);
193
194 cs.insert(*makeData("/a"), true);
195 cs.insert(*makeData("/b"), true);
196 cs.insert(*makeData("/c"), true);
197 BOOST_CHECK_EQUAL(cs.size(), 3);
198
199 cs.insert(*makeData("/d"), true);
200 BOOST_CHECK_EQUAL(cs.size(), 3);
201
202 const Data* oldestData = cs.find(Interest("/a"));
203 const Data* newestData = cs.find(Interest("/c"));
204 BOOST_CHECK(oldestData == 0);
205 BOOST_CHECK(newestData != 0);
206}
207
208BOOST_AUTO_TEST_CASE(UnsolicitedWithSolicitedEvictionOrder)
209{
210 Cs cs(3);
211
212 cs.insert(*makeData("/a"), true);
213 cs.insert(*makeData("/b"), false);
214 cs.insert(*makeData("/c"), true);
215 BOOST_CHECK_EQUAL(cs.size(), 3);
216
217 cs.insert(*makeData("/d"), true);
218 BOOST_CHECK_EQUAL(cs.size(), 3);
219
220 const Data* oldestData = cs.find(Interest("/a"));
221 const Data* newestData = cs.find(Interest("/c"));
222 BOOST_CHECK(oldestData == 0);
223 BOOST_CHECK(newestData != 0);
224}
225
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800226//even max size
227BOOST_AUTO_TEST_CASE(ReplacementEvenSize)
228{
229 Cs cs(4);
230
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400231 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800232 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700233
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400234 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800235 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700236
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400237 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800238 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700239
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400240 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800241 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700242
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400243 shared_ptr<Data> data5 = makeData("/e");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800244 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700245
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800246 BOOST_CHECK_EQUAL(cs.size(), 4);
247}
248
249//odd max size
250BOOST_AUTO_TEST_CASE(Replacement2)
251{
252 Cs cs(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700253
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400254 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800255 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700256
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400257 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800258 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700259
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400260 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800261 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700262
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400263 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800264 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700265
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800266 BOOST_CHECK_EQUAL(cs.size(), 3);
267}
268
269BOOST_AUTO_TEST_CASE(InsertAndEraseByName)
270{
271 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700272
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800273 Name name("/insertandremovebyname");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400274 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800275 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700276
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400277 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
278 data->wireEncode().size());
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700279
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400280 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800281 cs.insert(*data2);
282
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400283 shared_ptr<Data> data3 = makeData("/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800284 cs.insert(*data3);
285
286 BOOST_CHECK_EQUAL(cs.size(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700287
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800288 name.appendImplicitSha256Digest(digest1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800289 cs.erase(name);
290 BOOST_CHECK_EQUAL(cs.size(), 2);
291}
292
293BOOST_AUTO_TEST_CASE(DigestCalculation)
294{
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400295 shared_ptr<Data> data = makeData("/digest/compute");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800296
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400297 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
298 data->wireEncode().size());
299 BOOST_CHECK_EQUAL(digest1->size(), 32);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700300
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400301 cs::Entry* entry = new cs::Entry();
302 entry->setData(*data, false);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700303
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400304 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700305 entry->getFullName()[-1].value_begin(),
306 entry->getFullName()[-1].value_end());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800307}
308
309BOOST_AUTO_TEST_CASE(InsertCanonical)
310{
311 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700312
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400313 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800314 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700315
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400316 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800317 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700318
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400319 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800320 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700321
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400322 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800323 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700324
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400325 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800326 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700327
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400328 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800329 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700330
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400331 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800332 cs.insert(*data7);
333}
334
335BOOST_AUTO_TEST_CASE(EraseCanonical)
336{
337 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700338
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400339 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800340 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700341
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400342 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800343 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700344
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400345 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800346 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700347
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400348 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800349 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700350
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400351 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800352 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700353
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400354 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800355 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700356
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400357 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800358 cs.insert(*data7);
359
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400360 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
361 data->wireEncode().size());
362
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800363 Name name("/a");
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800364 name.appendImplicitSha256Digest(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800365 cs.erase(name);
366 BOOST_CHECK_EQUAL(cs.size(), 6);
367}
368
369BOOST_AUTO_TEST_CASE(ImplicitDigestSelector)
370{
371 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700372
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800373 Name name("/digest/works");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400374 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800375 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700376
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400377 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800378 cs.insert(*data2);
379
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400380 shared_ptr<Data> data3 = makeData("/z/z/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800381 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700382
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400383 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
384 data->wireEncode().size());
385 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
386 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700387
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400388 shared_ptr<Interest> interest = make_shared<Interest>();
389 interest->setName(Name(name).append(digest1->buf(), digest1->size()));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800390 interest->setMinSuffixComponents(0);
391 interest->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700392
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800393 const Data* found = cs.find(*interest);
394 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
395 BOOST_CHECK_EQUAL(found->getName(), name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700396
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400397 shared_ptr<Interest> interest2 = make_shared<Interest>();
398 interest2->setName(Name(name).append(digest2, 32));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800399 interest2->setMinSuffixComponents(0);
400 interest2->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700401
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800402 const Data* notfound = cs.find(*interest2);
403 BOOST_CHECK_EQUAL(notfound, static_cast<const Data*>(0));
404}
405
406BOOST_AUTO_TEST_CASE(ChildSelector)
407{
408 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700409
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400410 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800411 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700412
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400413 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800414 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700415
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400416 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800417 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700418
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400419 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800420 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700421
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400422 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800423 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700424
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400425 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800426 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700427
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800428 shared_ptr<Interest> interest = make_shared<Interest>("/c");
429 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700430
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800431 const Data* found = cs.find(*interest);
432 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
433
434 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
435 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700436
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800437 const Data* found2 = cs.find(*interest2);
438 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
439}
440
441BOOST_AUTO_TEST_CASE(ChildSelector2)
442{
443 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700444
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400445 shared_ptr<Data> data = makeData("/a/b/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800446 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700447
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400448 shared_ptr<Data> data2 = makeData("/a/b/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800449 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700450
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400451 shared_ptr<Data> data3 = makeData("/a/z/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800452 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700453
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400454 shared_ptr<Data> data4 = makeData("/a/z/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800455 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700456
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800457 shared_ptr<Interest> interest = make_shared<Interest>("/a");
458 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700459
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800460 const Data* found = cs.find(*interest);
461 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
462}
463
464BOOST_AUTO_TEST_CASE(MustBeFreshSelector)
465{
466 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700467
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800468 Name name("/insert/nonfresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400469 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700470 data->setFreshnessPeriod(time::milliseconds(500));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700471 signData(data);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800472 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700473
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800474 sleep(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700475
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800476 shared_ptr<Interest> interest = make_shared<Interest>(name);
477 interest->setMustBeFresh(true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700478
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800479 const Data* found = cs.find(*interest);
480 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
481}
482
Ilya Moiseenko9c9231b2014-04-10 11:05:12 -0400483BOOST_AUTO_TEST_CASE(PublisherKeySelector)
484{
485 Cs cs;
486
487 Name name("/insert/withkey");
488 shared_ptr<Data> data = makeData(name);
489 cs.insert(*data);
490
491 shared_ptr<Interest> interest = make_shared<Interest>(name);
492 Name keyName("/somewhere/key");
493
494 ndn::KeyLocator locator(keyName);
495 interest->setPublisherPublicKeyLocator(locator);
496
497 const Data* found = cs.find(*interest);
498 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
499}
500
501BOOST_AUTO_TEST_CASE(PublisherKeySelector2)
502{
503 Cs cs;
504 Name name("/insert/withkey");
505 shared_ptr<Data> data = makeData(name);
506 cs.insert(*data);
507
508 Name name2("/insert/withkey2");
509 shared_ptr<Data> data2 = make_shared<Data>(name2);
510
511 Name keyName("/somewhere/key");
512 const ndn::KeyLocator locator(keyName);
513
514 ndn::SignatureSha256WithRsa fakeSignature;
515 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
516 reinterpret_cast<const uint8_t*>(0), 0));
517
518 fakeSignature.setKeyLocator(locator);
519 data2->setSignature(fakeSignature);
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700520 data2->wireEncode();
Ilya Moiseenko9c9231b2014-04-10 11:05:12 -0400521
522 cs.insert(*data2);
523
524 shared_ptr<Interest> interest = make_shared<Interest>(name2);
525 interest->setPublisherPublicKeyLocator(locator);
526
527 const Data* found = cs.find(*interest);
528 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
529 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
530}
531
532
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800533/// @todo Expected failures, needs to be fixed as part of Issue #2118
534BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinMaxComponentsSelector, 1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800535BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
536{
537 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700538
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400539 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800540 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700541
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400542 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800543 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700544
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400545 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800546 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700547
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400548 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800549 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700550
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400551 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800552 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700553
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400554 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800555 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700556
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400557 shared_ptr<Data> data8 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800558 cs.insert(*data8);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700559
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800560 shared_ptr<Interest> interest = make_shared<Interest>("/c/c");
561 interest->setMinSuffixComponents(3);
562 interest->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700563
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800564 const Data* found = cs.find(*interest);
565 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6");
566
567 shared_ptr<Interest> interest2 = make_shared<Interest>("/c/c");
568 interest2->setMinSuffixComponents(4);
569 interest2->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700570
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800571 const Data* found2 = cs.find(*interest2);
572 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700573
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800574 shared_ptr<Interest> interest3 = make_shared<Interest>("/c/c");
575 interest3->setMaxSuffixComponents(2);
576 interest3->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700577
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800578 const Data* found3 = cs.find(*interest3);
579 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
580}
581
582BOOST_AUTO_TEST_CASE(ExcludeSelector)
583{
584 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700585
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400586 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800587 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700588
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400589 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800590 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700591
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400592 shared_ptr<Data> data3 = makeData("/c/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800593 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700594
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400595 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800596 cs.insert(*data4);
597
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400598 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800599 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700600
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400601 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800602 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700603
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400604 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800605 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700606
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800607 shared_ptr<Interest> interest = make_shared<Interest>("/c");
608 interest->setChildSelector(1);
609 Exclude e;
610 e.excludeOne (Name::Component("n"));
611 interest->setExclude(e);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700612
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800613 const Data* found = cs.find(*interest);
614 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
615
616 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
617 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700618
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800619 Exclude e2;
620 e2.excludeOne (Name::Component("a"));
621 interest2->setExclude(e2);
622
623 const Data* found2 = cs.find(*interest2);
624 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700625
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800626 shared_ptr<Interest> interest3 = make_shared<Interest>("/c");
627 interest3->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700628
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800629 Exclude e3;
630 e3.excludeOne (Name::Component("c"));
631 interest3->setExclude(e3);
632
633 const Data* found3 = cs.find(*interest3);
634 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
635}
636
637//
638
639class FindFixture : protected BaseFixture
640{
641protected:
642 void
643 insert(uint32_t id, const Name& name)
644 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400645 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700646 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800647 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700648 signData(data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700649
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800650 m_cs.insert(*data);
651 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700652
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800653 Interest&
654 startInterest(const Name& name)
655 {
656 m_interest = make_shared<Interest>(name);
657 return *m_interest;
658 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700659
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800660 uint32_t
661 find()
662 {
663 const Data* found = m_cs.find(*m_interest);
664 if (found == 0) {
665 return 0;
666 }
667 const Block& content = found->getContent();
668 if (content.value_size() != sizeof(uint32_t)) {
669 return 0;
670 }
671 return *reinterpret_cast<const uint32_t*>(content.value());
672 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700673
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800674protected:
675 Cs m_cs;
676 shared_ptr<Interest> m_interest;
677};
678
679BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
680
681BOOST_AUTO_TEST_CASE(EmptyDataName)
682{
683 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700684
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800685 startInterest("ndn:/");
686 BOOST_CHECK_EQUAL(find(), 1);
687}
688
689BOOST_AUTO_TEST_CASE(EmptyInterestName)
690{
691 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700692
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800693 startInterest("ndn:/");
694 BOOST_CHECK_EQUAL(find(), 1);
695}
696
697BOOST_AUTO_TEST_CASE(Leftmost)
698{
699 insert(1, "ndn:/A");
700 insert(2, "ndn:/B/p/1");
701 insert(3, "ndn:/B/p/2");
702 insert(4, "ndn:/B/q/1");
703 insert(5, "ndn:/B/q/2");
704 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700705
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800706 startInterest("ndn:/B");
707 BOOST_CHECK_EQUAL(find(), 2);
708}
709
710BOOST_AUTO_TEST_CASE(Rightmost)
711{
712 insert(1, "ndn:/A");
713 insert(2, "ndn:/B/p/1");
714 insert(3, "ndn:/B/p/2");
715 insert(4, "ndn:/B/q/1");
716 insert(5, "ndn:/B/q/2");
717 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700718
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800719 startInterest("ndn:/B")
720 .setChildSelector(1);
721 BOOST_CHECK_EQUAL(find(), 4);
722}
723
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800724/// @todo Expected failures, needs to be fixed as part of Issue #2118
725BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(Leftmost_ExactName1, 1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800726BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
727{
728 insert(1, "ndn:/");
729 insert(2, "ndn:/A/B");
730 insert(3, "ndn:/A/C");
731 insert(4, "ndn:/A");
732 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700733
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800734 // Intuitively you would think Data 4 should be between Data 1 and 2,
735 // but Data 4 has full Name ndn:/A/<32-octet hash>.
736 startInterest("ndn:/A");
737 BOOST_CHECK_EQUAL(find(), 2);
738}
739
740BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
741{
742 insert(1, "ndn:/");
743 insert(2, "ndn:/A");
744 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
745 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
746 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700747
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800748 // Data 2 is returned, because <32-octet hash> is less than Data 3.
749 startInterest("ndn:/A");
750 BOOST_CHECK_EQUAL(find(), 2);
751}
752
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800753/// @todo Expected failures, needs to be fixed as part of Issue #2118
754BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinSuffixComponents, 2)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800755BOOST_AUTO_TEST_CASE(MinSuffixComponents)
756{
757 insert(1, "ndn:/A/1/2/3/4");
758 insert(2, "ndn:/B/1/2/3");
759 insert(3, "ndn:/C/1/2");
760 insert(4, "ndn:/D/1");
761 insert(5, "ndn:/E");
762 insert(6, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700763
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800764 startInterest("ndn:/")
765 .setChildSelector(1)
766 .setMinSuffixComponents(0);
767 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700768
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800769 startInterest("ndn:/")
770 .setChildSelector(1)
771 .setMinSuffixComponents(1);
772 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700773
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800774 startInterest("ndn:/")
775 .setChildSelector(1)
776 .setMinSuffixComponents(2);
777 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700778
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800779 startInterest("ndn:/")
780 .setChildSelector(1)
781 .setMinSuffixComponents(3);
782 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700783
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800784 startInterest("ndn:/")
785 .setChildSelector(1)
786 .setMinSuffixComponents(4);
787 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700788
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800789 startInterest("ndn:/")
790 .setChildSelector(1)
791 .setMinSuffixComponents(5);
792 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700793
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800794 startInterest("ndn:/")
795 .setChildSelector(1)
796 .setMinSuffixComponents(6);
797 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700798
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800799 startInterest("ndn:/")
800 .setChildSelector(1)
801 .setMinSuffixComponents(7);
802 BOOST_CHECK_EQUAL(find(), 0);
803}
804
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800805/// @todo Expected failures, needs to be fixed as part of Issue #2118
806BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MaxSuffixComponents, 5)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800807BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
808{
809 insert(1, "ndn:/");
810 insert(2, "ndn:/A");
811 insert(3, "ndn:/A/B");
812 insert(4, "ndn:/A/B/C");
813 insert(5, "ndn:/A/B/C/D");
814 insert(6, "ndn:/A/B/C/D/E");
815 // 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 -0700816
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800817 startInterest("ndn:/")
818 .setMaxSuffixComponents(0);
819 BOOST_CHECK_EQUAL(find(), 0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700820
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800821 startInterest("ndn:/")
822 .setMaxSuffixComponents(1);
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 .setMaxSuffixComponents(2);
827 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700828
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800829 startInterest("ndn:/")
830 .setMaxSuffixComponents(3);
831 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700832
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800833 startInterest("ndn:/")
834 .setMaxSuffixComponents(4);
835 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700836
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800837 startInterest("ndn:/")
838 .setMaxSuffixComponents(5);
839 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700840
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800841 startInterest("ndn:/")
842 .setMaxSuffixComponents(6);
843 BOOST_CHECK_EQUAL(find(), 6);
844}
845
846BOOST_AUTO_TEST_CASE(DigestOrder)
847{
848 insert(1, "ndn:/A");
849 insert(2, "ndn:/A");
850 // We don't know which comes first, but there must be some order
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700851
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800852 startInterest("ndn:/A")
853 .setChildSelector(0);
854 uint32_t leftmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700855
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800856 startInterest("ndn:/A")
857 .setChildSelector(1);
858 uint32_t rightmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700859
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800860 BOOST_CHECK_NE(leftmost, rightmost);
861}
862
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800863/// @todo Expected failures, needs to be fixed as part of Issue #2118
864BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(DigestExclude, 1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800865BOOST_AUTO_TEST_CASE(DigestExclude)
866{
867 insert(1, "ndn:/A/B");
868 insert(2, "ndn:/A");
869 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700870
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800871 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400872 .setExclude(Exclude().excludeBefore(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800873 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
874 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
875 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700876
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800877 startInterest("ndn:/A")
878 .setChildSelector(1)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400879 .setExclude(Exclude().excludeAfter(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800880 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
881 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
882 "\x00"), 33))); // 33 0x00's
883 BOOST_CHECK_EQUAL(find(), 2);
884}
885
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400886BOOST_AUTO_TEST_CASE(ExactName32)
887{
888 insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
889 insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800890
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400891 startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
892 BOOST_CHECK_EQUAL(find(), 1);
893}
894
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800895/// @todo Expected failures, needs to be fixed as part of Issue #2118
896BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinSuffixComponents32, 2)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400897BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
898{
899 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
900 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
901 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
902 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
903 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
904 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
905
906 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
907 .setChildSelector(1)
908 .setMinSuffixComponents(0);
909 BOOST_CHECK_EQUAL(find(), 6);
910
911 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
912 .setChildSelector(1)
913 .setMinSuffixComponents(1);
914 BOOST_CHECK_EQUAL(find(), 6);
915
916 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
917 .setChildSelector(1)
918 .setMinSuffixComponents(2);
919 BOOST_CHECK_EQUAL(find(), 5);
920
921 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
922 .setChildSelector(1)
923 .setMinSuffixComponents(3);
924 BOOST_CHECK_EQUAL(find(), 4);
925
926 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
927 .setChildSelector(1)
928 .setMinSuffixComponents(4);
929 BOOST_CHECK_EQUAL(find(), 3);
930
931 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
932 .setChildSelector(1)
933 .setMinSuffixComponents(5);
934 BOOST_CHECK_EQUAL(find(), 2);
935
936 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
937 .setChildSelector(1)
938 .setMinSuffixComponents(6);
939 BOOST_CHECK_EQUAL(find(), 1);
940
941 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
942 .setChildSelector(1)
943 .setMinSuffixComponents(7);
944 BOOST_CHECK_EQUAL(find(), 0);
945}
946
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800947/// @todo Expected failures, needs to be fixed as part of Issue #2118
948BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MaxSuffixComponents32, 5)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400949BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
950{
951 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
952 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
953 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
954 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
955 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
956 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
957 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
958
959 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
960 .setMaxSuffixComponents(0);
961 BOOST_CHECK_EQUAL(find(), 0);
962
963 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
964 .setMaxSuffixComponents(1);
965 BOOST_CHECK_EQUAL(find(), 1);
966
967 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
968 .setMaxSuffixComponents(2);
969 BOOST_CHECK_EQUAL(find(), 2);
970
971 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
972 .setMaxSuffixComponents(3);
973 BOOST_CHECK_EQUAL(find(), 3);
974
975 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
976 .setMaxSuffixComponents(4);
977 BOOST_CHECK_EQUAL(find(), 4);
978
979 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
980 .setMaxSuffixComponents(5);
981 BOOST_CHECK_EQUAL(find(), 5);
982
983 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
984 .setMaxSuffixComponents(6);
985 BOOST_CHECK_EQUAL(find(), 6);
986}
987
988BOOST_AUTO_TEST_SUITE_END() // Find
989
Alexander Afanasyevc91ebfa2015-01-03 18:09:57 -0800990BOOST_AUTO_TEST_CASE(Iterator)
991{
992 Cs cs;
993
994 Name nameA("/A");
995 Name nameAB("/A/B");
996 Name nameABC("/A/B/C");
997 Name nameD("/D");
998
999 BOOST_CHECK_EQUAL(cs.size(), 0);
1000 BOOST_CHECK(cs.begin() == cs.end());
1001
1002 cs.insert(*makeData(nameABC));
1003 BOOST_CHECK_EQUAL(cs.size(), 1);
1004 BOOST_CHECK(cs.begin() != cs.end());
1005 BOOST_CHECK(cs.begin()->getName() == nameABC);
1006 BOOST_CHECK((*cs.begin()).getName() == nameABC);
1007
1008 auto i = cs.begin();
1009 auto j = cs.begin();
1010 BOOST_CHECK(++i == cs.end());
1011 BOOST_CHECK(j++ == cs.begin());
1012 BOOST_CHECK(j == cs.end());
1013
1014 cs.insert(*makeData(nameA));
1015 cs.insert(*makeData(nameAB));
1016 cs.insert(*makeData(nameD));
1017
1018 std::set<Name> expected = {nameA, nameAB, nameABC, nameD};
1019 std::set<Name> actual;
1020 for (const auto& csEntry : cs) {
1021 actual.insert(csEntry.getName());
1022 }
1023 BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
1024}
1025
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -04001026BOOST_AUTO_TEST_SUITE_END() // TableCs
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -08001027
Junxiao Shid9ee45c2014-02-27 15:38:11 -07001028} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -08001029} // namespace nfd