blob: d38538e2d98d3beb9323ca5e58b15915a43a799c [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
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070034
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080035class CsAccessor : public Cs
36{
37public:
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080038 bool
39 evictItem_accessor()
40 {
41 return evictItem();
42 }
43};
44
Junxiao Shid9ee45c2014-02-27 15:38:11 -070045BOOST_FIXTURE_TEST_SUITE(TableCs, BaseFixture)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070046
Alexander Afanasyev281b9162014-06-08 10:15:20 +030047BOOST_AUTO_TEST_CASE(SetLimit)
48{
49 Cs cs(1);
50
51 BOOST_CHECK_EQUAL(cs.insert(*makeData("/1")), true);
52 BOOST_CHECK_EQUAL(cs.insert(*makeData("/2")), true);
53 BOOST_CHECK_EQUAL(cs.size(), 1);
54
55 cs.setLimit(3);
56 BOOST_CHECK_EQUAL(cs.insert(*makeData("/3")), true);
57 BOOST_CHECK_EQUAL(cs.insert(*makeData("/4")), true);
58 BOOST_CHECK_EQUAL(cs.insert(*makeData("/5")), true);
59 BOOST_CHECK_EQUAL(cs.size(), 3);
60
61 cs.setLimit(2);
62 BOOST_CHECK_EQUAL(cs.size(), 2);
63}
64
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080065BOOST_AUTO_TEST_CASE(Insertion)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070066{
67 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070068
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040069 BOOST_CHECK_EQUAL(cs.insert(*makeData("/insertion")), true);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070070}
71
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080072BOOST_AUTO_TEST_CASE(Insertion2)
73{
74 Cs cs;
75
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040076 cs.insert(*makeData("/a"));
77 cs.insert(*makeData("/b"));
78 cs.insert(*makeData("/c"));
79 cs.insert(*makeData("/d"));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070080
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080081 BOOST_CHECK_EQUAL(cs.size(), 4);
82}
83
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080084BOOST_AUTO_TEST_CASE(DuplicateInsertion)
85{
86 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070087
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040088 shared_ptr<Data> data0 = makeData("/insert/smth");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080089 BOOST_CHECK_EQUAL(cs.insert(*data0), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070090
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040091 shared_ptr<Data> data = makeData("/insert/duplicate");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080092 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070093
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080094 cs.insert(*data);
95 BOOST_CHECK_EQUAL(cs.size(), 2);
96}
97
98
99BOOST_AUTO_TEST_CASE(DuplicateInsertion2)
100{
101 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700102
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400103 shared_ptr<Data> data = makeData("/insert/duplicate");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800104 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700105
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800106 cs.insert(*data);
107 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700108
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400109 shared_ptr<Data> data2 = makeData("/insert/original");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800110 BOOST_CHECK_EQUAL(cs.insert(*data2), true);
111 BOOST_CHECK_EQUAL(cs.size(), 2);
112}
113
114BOOST_AUTO_TEST_CASE(InsertAndFind)
115{
116 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700117
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800118 Name name("/insert/and/find");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700119
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400120 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800121 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700122
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800123 shared_ptr<Interest> interest = make_shared<Interest>(name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700124
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800125 const Data* found = cs.find(*interest);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700126
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800127 BOOST_REQUIRE(found != 0);
128 BOOST_CHECK_EQUAL(data->getName(), found->getName());
129}
130
131BOOST_AUTO_TEST_CASE(InsertAndNotFind)
132{
133 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700134
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800135 Name name("/insert/and/find");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400136 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800137 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700138
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800139 Name name2("/not/find");
140 shared_ptr<Interest> interest = make_shared<Interest>(name2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700141
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800142 const Data* found = cs.find(*interest);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700143
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800144 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
145}
146
147
148BOOST_AUTO_TEST_CASE(InsertAndErase)
149{
150 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700151
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400152 shared_ptr<Data> data = makeData("/insertandelete");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800153 cs.insert(*data);
154 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700155
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800156 cs.evictItem_accessor();
157 BOOST_CHECK_EQUAL(cs.size(), 0);
158}
159
160BOOST_AUTO_TEST_CASE(StalenessQueue)
161{
162 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700163
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800164 Name name2("/insert/fresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400165 shared_ptr<Data> data2 = makeData(name2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700166 data2->setFreshnessPeriod(time::milliseconds(5000));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700167 signData(data2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800168 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700169
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800170 Name name("/insert/expire");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400171 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700172 data->setFreshnessPeriod(time::milliseconds(500));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700173 signData(data);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800174 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700175
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800176 BOOST_CHECK_EQUAL(cs.size(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700177
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400178 sleep(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700179
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800180 cs.evictItem_accessor();
181 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700182
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800183 shared_ptr<Interest> interest = make_shared<Interest>(name2);
184 const Data* found = cs.find(*interest);
185 BOOST_REQUIRE(found != 0);
186 BOOST_CHECK_EQUAL(found->getName(), name2);
187}
188
Alexander Afanasyev4cf41702014-10-19 23:46:10 -0400189BOOST_AUTO_TEST_CASE(Bug2043) // eviction order of unsolicited packets
190{
191 Cs cs(3);
192
193 cs.insert(*makeData("/a"), true);
194 cs.insert(*makeData("/b"), true);
195 cs.insert(*makeData("/c"), true);
196 BOOST_CHECK_EQUAL(cs.size(), 3);
197
198 cs.insert(*makeData("/d"), true);
199 BOOST_CHECK_EQUAL(cs.size(), 3);
200
201 const Data* oldestData = cs.find(Interest("/a"));
202 const Data* newestData = cs.find(Interest("/c"));
203 BOOST_CHECK(oldestData == 0);
204 BOOST_CHECK(newestData != 0);
205}
206
207BOOST_AUTO_TEST_CASE(UnsolicitedWithSolicitedEvictionOrder)
208{
209 Cs cs(3);
210
211 cs.insert(*makeData("/a"), true);
212 cs.insert(*makeData("/b"), false);
213 cs.insert(*makeData("/c"), true);
214 BOOST_CHECK_EQUAL(cs.size(), 3);
215
216 cs.insert(*makeData("/d"), true);
217 BOOST_CHECK_EQUAL(cs.size(), 3);
218
219 const Data* oldestData = cs.find(Interest("/a"));
220 const Data* newestData = cs.find(Interest("/c"));
221 BOOST_CHECK(oldestData == 0);
222 BOOST_CHECK(newestData != 0);
223}
224
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800225//even max size
226BOOST_AUTO_TEST_CASE(ReplacementEvenSize)
227{
228 Cs cs(4);
229
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400230 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800231 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700232
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400233 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800234 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700235
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400236 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800237 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700238
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400239 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800240 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700241
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400242 shared_ptr<Data> data5 = makeData("/e");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800243 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700244
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800245 BOOST_CHECK_EQUAL(cs.size(), 4);
246}
247
248//odd max size
249BOOST_AUTO_TEST_CASE(Replacement2)
250{
251 Cs cs(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700252
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400253 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800254 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700255
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400256 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800257 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700258
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400259 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800260 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700261
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400262 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800263 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700264
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800265 BOOST_CHECK_EQUAL(cs.size(), 3);
266}
267
268BOOST_AUTO_TEST_CASE(InsertAndEraseByName)
269{
270 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700271
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800272 Name name("/insertandremovebyname");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400273 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800274 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700275
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400276 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
277 data->wireEncode().size());
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700278
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400279 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800280 cs.insert(*data2);
281
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400282 shared_ptr<Data> data3 = makeData("/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800283 cs.insert(*data3);
284
285 BOOST_CHECK_EQUAL(cs.size(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700286
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800287 name.appendImplicitSha256Digest(digest1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800288 cs.erase(name);
289 BOOST_CHECK_EQUAL(cs.size(), 2);
290}
291
292BOOST_AUTO_TEST_CASE(DigestCalculation)
293{
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400294 shared_ptr<Data> data = makeData("/digest/compute");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800295
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400296 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
297 data->wireEncode().size());
298 BOOST_CHECK_EQUAL(digest1->size(), 32);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700299
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400300 cs::Entry* entry = new cs::Entry();
301 entry->setData(*data, false);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700302
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400303 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700304 entry->getFullName()[-1].value_begin(),
305 entry->getFullName()[-1].value_end());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800306}
307
308BOOST_AUTO_TEST_CASE(InsertCanonical)
309{
310 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700311
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400312 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800313 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700314
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400315 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800316 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700317
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400318 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800319 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700320
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400321 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800322 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700323
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400324 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800325 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700326
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400327 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800328 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700329
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400330 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800331 cs.insert(*data7);
332}
333
334BOOST_AUTO_TEST_CASE(EraseCanonical)
335{
336 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700337
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400338 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800339 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700340
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400341 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800342 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700343
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400344 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800345 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700346
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400347 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800348 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700349
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400350 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800351 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700352
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400353 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800354 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700355
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400356 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800357 cs.insert(*data7);
358
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400359 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
360 data->wireEncode().size());
361
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800362 Name name("/a");
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800363 name.appendImplicitSha256Digest(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800364 cs.erase(name);
365 BOOST_CHECK_EQUAL(cs.size(), 6);
366}
367
368BOOST_AUTO_TEST_CASE(ImplicitDigestSelector)
369{
370 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700371
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800372 Name name("/digest/works");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400373 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800374 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700375
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400376 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800377 cs.insert(*data2);
378
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400379 shared_ptr<Data> data3 = makeData("/z/z/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800380 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700381
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400382 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
383 data->wireEncode().size());
384 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
385 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700386
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400387 shared_ptr<Interest> interest = make_shared<Interest>();
388 interest->setName(Name(name).append(digest1->buf(), digest1->size()));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800389 interest->setMinSuffixComponents(0);
390 interest->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700391
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800392 const Data* found = cs.find(*interest);
393 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
394 BOOST_CHECK_EQUAL(found->getName(), name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700395
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400396 shared_ptr<Interest> interest2 = make_shared<Interest>();
397 interest2->setName(Name(name).append(digest2, 32));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800398 interest2->setMinSuffixComponents(0);
399 interest2->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700400
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800401 const Data* notfound = cs.find(*interest2);
402 BOOST_CHECK_EQUAL(notfound, static_cast<const Data*>(0));
403}
404
405BOOST_AUTO_TEST_CASE(ChildSelector)
406{
407 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700408
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400409 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800410 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700411
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400412 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800413 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700414
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400415 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800416 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700417
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400418 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800419 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700420
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400421 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800422 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700423
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400424 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800425 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700426
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800427 shared_ptr<Interest> interest = make_shared<Interest>("/c");
428 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700429
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800430 const Data* found = cs.find(*interest);
431 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
432
433 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
434 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700435
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800436 const Data* found2 = cs.find(*interest2);
437 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
438}
439
440BOOST_AUTO_TEST_CASE(ChildSelector2)
441{
442 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700443
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400444 shared_ptr<Data> data = makeData("/a/b/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800445 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700446
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400447 shared_ptr<Data> data2 = makeData("/a/b/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800448 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700449
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400450 shared_ptr<Data> data3 = makeData("/a/z/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800451 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700452
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400453 shared_ptr<Data> data4 = makeData("/a/z/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800454 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700455
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800456 shared_ptr<Interest> interest = make_shared<Interest>("/a");
457 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700458
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800459 const Data* found = cs.find(*interest);
460 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
461}
462
463BOOST_AUTO_TEST_CASE(MustBeFreshSelector)
464{
465 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700466
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800467 Name name("/insert/nonfresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400468 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700469 data->setFreshnessPeriod(time::milliseconds(500));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700470 signData(data);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800471 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700472
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800473 sleep(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700474
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800475 shared_ptr<Interest> interest = make_shared<Interest>(name);
476 interest->setMustBeFresh(true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700477
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800478 const Data* found = cs.find(*interest);
479 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
480}
481
Ilya Moiseenko9c9231b2014-04-10 11:05:12 -0400482BOOST_AUTO_TEST_CASE(PublisherKeySelector)
483{
484 Cs cs;
485
486 Name name("/insert/withkey");
487 shared_ptr<Data> data = makeData(name);
488 cs.insert(*data);
489
490 shared_ptr<Interest> interest = make_shared<Interest>(name);
491 Name keyName("/somewhere/key");
492
493 ndn::KeyLocator locator(keyName);
494 interest->setPublisherPublicKeyLocator(locator);
495
496 const Data* found = cs.find(*interest);
497 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
498}
499
500BOOST_AUTO_TEST_CASE(PublisherKeySelector2)
501{
502 Cs cs;
503 Name name("/insert/withkey");
504 shared_ptr<Data> data = makeData(name);
505 cs.insert(*data);
506
507 Name name2("/insert/withkey2");
508 shared_ptr<Data> data2 = make_shared<Data>(name2);
509
510 Name keyName("/somewhere/key");
511 const ndn::KeyLocator locator(keyName);
512
513 ndn::SignatureSha256WithRsa fakeSignature;
514 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
515 reinterpret_cast<const uint8_t*>(0), 0));
516
517 fakeSignature.setKeyLocator(locator);
518 data2->setSignature(fakeSignature);
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700519 data2->wireEncode();
Ilya Moiseenko9c9231b2014-04-10 11:05:12 -0400520
521 cs.insert(*data2);
522
523 shared_ptr<Interest> interest = make_shared<Interest>(name2);
524 interest->setPublisherPublicKeyLocator(locator);
525
526 const Data* found = cs.find(*interest);
527 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
528 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
529}
530
531
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800532/// @todo Expected failures, needs to be fixed as part of Issue #2118
533BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinMaxComponentsSelector, 1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800534BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
535{
536 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700537
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400538 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800539 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700540
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400541 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800542 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700543
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400544 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800545 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700546
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400547 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800548 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700549
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400550 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800551 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700552
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400553 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800554 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700555
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400556 shared_ptr<Data> data8 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800557 cs.insert(*data8);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700558
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800559 shared_ptr<Interest> interest = make_shared<Interest>("/c/c");
560 interest->setMinSuffixComponents(3);
561 interest->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700562
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800563 const Data* found = cs.find(*interest);
564 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6");
565
566 shared_ptr<Interest> interest2 = make_shared<Interest>("/c/c");
567 interest2->setMinSuffixComponents(4);
568 interest2->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700569
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800570 const Data* found2 = cs.find(*interest2);
571 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700572
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800573 shared_ptr<Interest> interest3 = make_shared<Interest>("/c/c");
574 interest3->setMaxSuffixComponents(2);
575 interest3->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700576
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800577 const Data* found3 = cs.find(*interest3);
578 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
579}
580
581BOOST_AUTO_TEST_CASE(ExcludeSelector)
582{
583 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700584
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400585 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800586 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700587
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400588 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800589 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700590
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400591 shared_ptr<Data> data3 = makeData("/c/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800592 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700593
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400594 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800595 cs.insert(*data4);
596
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400597 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800598 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700599
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400600 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800601 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700602
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400603 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800604 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700605
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800606 shared_ptr<Interest> interest = make_shared<Interest>("/c");
607 interest->setChildSelector(1);
608 Exclude e;
609 e.excludeOne (Name::Component("n"));
610 interest->setExclude(e);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700611
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800612 const Data* found = cs.find(*interest);
613 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
614
615 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
616 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700617
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800618 Exclude e2;
619 e2.excludeOne (Name::Component("a"));
620 interest2->setExclude(e2);
621
622 const Data* found2 = cs.find(*interest2);
623 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700624
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800625 shared_ptr<Interest> interest3 = make_shared<Interest>("/c");
626 interest3->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700627
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800628 Exclude e3;
629 e3.excludeOne (Name::Component("c"));
630 interest3->setExclude(e3);
631
632 const Data* found3 = cs.find(*interest3);
633 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
634}
635
636//
637
638class FindFixture : protected BaseFixture
639{
640protected:
641 void
642 insert(uint32_t id, const Name& name)
643 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400644 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700645 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800646 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700647 signData(data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700648
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800649 m_cs.insert(*data);
650 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700651
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800652 Interest&
653 startInterest(const Name& name)
654 {
655 m_interest = make_shared<Interest>(name);
656 return *m_interest;
657 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700658
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800659 uint32_t
660 find()
661 {
662 const Data* found = m_cs.find(*m_interest);
663 if (found == 0) {
664 return 0;
665 }
666 const Block& content = found->getContent();
667 if (content.value_size() != sizeof(uint32_t)) {
668 return 0;
669 }
670 return *reinterpret_cast<const uint32_t*>(content.value());
671 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700672
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800673protected:
674 Cs m_cs;
675 shared_ptr<Interest> m_interest;
676};
677
678BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
679
680BOOST_AUTO_TEST_CASE(EmptyDataName)
681{
682 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700683
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800684 startInterest("ndn:/");
685 BOOST_CHECK_EQUAL(find(), 1);
686}
687
688BOOST_AUTO_TEST_CASE(EmptyInterestName)
689{
690 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700691
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800692 startInterest("ndn:/");
693 BOOST_CHECK_EQUAL(find(), 1);
694}
695
696BOOST_AUTO_TEST_CASE(Leftmost)
697{
698 insert(1, "ndn:/A");
699 insert(2, "ndn:/B/p/1");
700 insert(3, "ndn:/B/p/2");
701 insert(4, "ndn:/B/q/1");
702 insert(5, "ndn:/B/q/2");
703 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700704
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800705 startInterest("ndn:/B");
706 BOOST_CHECK_EQUAL(find(), 2);
707}
708
709BOOST_AUTO_TEST_CASE(Rightmost)
710{
711 insert(1, "ndn:/A");
712 insert(2, "ndn:/B/p/1");
713 insert(3, "ndn:/B/p/2");
714 insert(4, "ndn:/B/q/1");
715 insert(5, "ndn:/B/q/2");
716 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700717
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800718 startInterest("ndn:/B")
719 .setChildSelector(1);
720 BOOST_CHECK_EQUAL(find(), 4);
721}
722
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800723/// @todo Expected failures, needs to be fixed as part of Issue #2118
724BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(Leftmost_ExactName1, 1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800725BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
726{
727 insert(1, "ndn:/");
728 insert(2, "ndn:/A/B");
729 insert(3, "ndn:/A/C");
730 insert(4, "ndn:/A");
731 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700732
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800733 // Intuitively you would think Data 4 should be between Data 1 and 2,
734 // but Data 4 has full Name ndn:/A/<32-octet hash>.
735 startInterest("ndn:/A");
736 BOOST_CHECK_EQUAL(find(), 2);
737}
738
739BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
740{
741 insert(1, "ndn:/");
742 insert(2, "ndn:/A");
743 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
744 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
745 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700746
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800747 // Data 2 is returned, because <32-octet hash> is less than Data 3.
748 startInterest("ndn:/A");
749 BOOST_CHECK_EQUAL(find(), 2);
750}
751
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800752/// @todo Expected failures, needs to be fixed as part of Issue #2118
753BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinSuffixComponents, 2)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800754BOOST_AUTO_TEST_CASE(MinSuffixComponents)
755{
756 insert(1, "ndn:/A/1/2/3/4");
757 insert(2, "ndn:/B/1/2/3");
758 insert(3, "ndn:/C/1/2");
759 insert(4, "ndn:/D/1");
760 insert(5, "ndn:/E");
761 insert(6, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700762
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800763 startInterest("ndn:/")
764 .setChildSelector(1)
765 .setMinSuffixComponents(0);
766 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700767
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800768 startInterest("ndn:/")
769 .setChildSelector(1)
770 .setMinSuffixComponents(1);
771 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700772
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800773 startInterest("ndn:/")
774 .setChildSelector(1)
775 .setMinSuffixComponents(2);
776 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700777
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800778 startInterest("ndn:/")
779 .setChildSelector(1)
780 .setMinSuffixComponents(3);
781 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700782
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800783 startInterest("ndn:/")
784 .setChildSelector(1)
785 .setMinSuffixComponents(4);
786 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700787
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800788 startInterest("ndn:/")
789 .setChildSelector(1)
790 .setMinSuffixComponents(5);
791 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700792
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800793 startInterest("ndn:/")
794 .setChildSelector(1)
795 .setMinSuffixComponents(6);
796 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700797
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800798 startInterest("ndn:/")
799 .setChildSelector(1)
800 .setMinSuffixComponents(7);
801 BOOST_CHECK_EQUAL(find(), 0);
802}
803
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800804/// @todo Expected failures, needs to be fixed as part of Issue #2118
805BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MaxSuffixComponents, 5)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800806BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
807{
808 insert(1, "ndn:/");
809 insert(2, "ndn:/A");
810 insert(3, "ndn:/A/B");
811 insert(4, "ndn:/A/B/C");
812 insert(5, "ndn:/A/B/C/D");
813 insert(6, "ndn:/A/B/C/D/E");
814 // 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 -0700815
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800816 startInterest("ndn:/")
817 .setMaxSuffixComponents(0);
818 BOOST_CHECK_EQUAL(find(), 0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700819
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800820 startInterest("ndn:/")
821 .setMaxSuffixComponents(1);
822 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700823
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800824 startInterest("ndn:/")
825 .setMaxSuffixComponents(2);
826 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700827
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800828 startInterest("ndn:/")
829 .setMaxSuffixComponents(3);
830 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700831
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800832 startInterest("ndn:/")
833 .setMaxSuffixComponents(4);
834 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700835
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800836 startInterest("ndn:/")
837 .setMaxSuffixComponents(5);
838 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700839
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800840 startInterest("ndn:/")
841 .setMaxSuffixComponents(6);
842 BOOST_CHECK_EQUAL(find(), 6);
843}
844
845BOOST_AUTO_TEST_CASE(DigestOrder)
846{
847 insert(1, "ndn:/A");
848 insert(2, "ndn:/A");
849 // We don't know which comes first, but there must be some order
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700850
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800851 startInterest("ndn:/A")
852 .setChildSelector(0);
853 uint32_t leftmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700854
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800855 startInterest("ndn:/A")
856 .setChildSelector(1);
857 uint32_t rightmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700858
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800859 BOOST_CHECK_NE(leftmost, rightmost);
860}
861
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800862/// @todo Expected failures, needs to be fixed as part of Issue #2118
863BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(DigestExclude, 1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800864BOOST_AUTO_TEST_CASE(DigestExclude)
865{
866 insert(1, "ndn:/A/B");
867 insert(2, "ndn:/A");
868 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700869
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800870 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400871 .setExclude(Exclude().excludeBefore(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800872 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
873 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
874 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700875
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800876 startInterest("ndn:/A")
877 .setChildSelector(1)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400878 .setExclude(Exclude().excludeAfter(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800879 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
880 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
881 "\x00"), 33))); // 33 0x00's
882 BOOST_CHECK_EQUAL(find(), 2);
883}
884
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400885BOOST_AUTO_TEST_CASE(ExactName32)
886{
887 insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
888 insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800889
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400890 startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
891 BOOST_CHECK_EQUAL(find(), 1);
892}
893
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800894/// @todo Expected failures, needs to be fixed as part of Issue #2118
895BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinSuffixComponents32, 2)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400896BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
897{
898 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
899 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
900 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
901 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
902 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
903 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
904
905 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
906 .setChildSelector(1)
907 .setMinSuffixComponents(0);
908 BOOST_CHECK_EQUAL(find(), 6);
909
910 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
911 .setChildSelector(1)
912 .setMinSuffixComponents(1);
913 BOOST_CHECK_EQUAL(find(), 6);
914
915 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
916 .setChildSelector(1)
917 .setMinSuffixComponents(2);
918 BOOST_CHECK_EQUAL(find(), 5);
919
920 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
921 .setChildSelector(1)
922 .setMinSuffixComponents(3);
923 BOOST_CHECK_EQUAL(find(), 4);
924
925 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
926 .setChildSelector(1)
927 .setMinSuffixComponents(4);
928 BOOST_CHECK_EQUAL(find(), 3);
929
930 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
931 .setChildSelector(1)
932 .setMinSuffixComponents(5);
933 BOOST_CHECK_EQUAL(find(), 2);
934
935 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
936 .setChildSelector(1)
937 .setMinSuffixComponents(6);
938 BOOST_CHECK_EQUAL(find(), 1);
939
940 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
941 .setChildSelector(1)
942 .setMinSuffixComponents(7);
943 BOOST_CHECK_EQUAL(find(), 0);
944}
945
Alexander Afanasyev3651fd72014-11-10 10:41:47 -0800946/// @todo Expected failures, needs to be fixed as part of Issue #2118
947BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MaxSuffixComponents32, 5)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400948BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
949{
950 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
951 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
952 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
953 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
954 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
955 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
956 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
957
958 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
959 .setMaxSuffixComponents(0);
960 BOOST_CHECK_EQUAL(find(), 0);
961
962 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
963 .setMaxSuffixComponents(1);
964 BOOST_CHECK_EQUAL(find(), 1);
965
966 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
967 .setMaxSuffixComponents(2);
968 BOOST_CHECK_EQUAL(find(), 2);
969
970 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
971 .setMaxSuffixComponents(3);
972 BOOST_CHECK_EQUAL(find(), 3);
973
974 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
975 .setMaxSuffixComponents(4);
976 BOOST_CHECK_EQUAL(find(), 4);
977
978 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
979 .setMaxSuffixComponents(5);
980 BOOST_CHECK_EQUAL(find(), 5);
981
982 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
983 .setMaxSuffixComponents(6);
984 BOOST_CHECK_EQUAL(find(), 6);
985}
986
987BOOST_AUTO_TEST_SUITE_END() // Find
988
989BOOST_AUTO_TEST_SUITE_END() // TableCs
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800990
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700991} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800992} // namespace nfd