blob: 3592290ba8c2f56529d2948642bd553302ba49c6 [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
84
85BOOST_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
190//even max size
191BOOST_AUTO_TEST_CASE(ReplacementEvenSize)
192{
193 Cs cs(4);
194
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400195 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800196 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700197
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400198 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800199 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700200
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400201 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800202 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700203
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400204 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800205 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700206
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400207 shared_ptr<Data> data5 = makeData("/e");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800208 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700209
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800210 BOOST_CHECK_EQUAL(cs.size(), 4);
211}
212
213//odd max size
214BOOST_AUTO_TEST_CASE(Replacement2)
215{
216 Cs cs(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700217
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400218 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800219 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700220
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400221 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800222 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700223
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400224 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800225 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700226
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400227 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800228 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700229
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800230 BOOST_CHECK_EQUAL(cs.size(), 3);
231}
232
233BOOST_AUTO_TEST_CASE(InsertAndEraseByName)
234{
235 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700236
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800237 Name name("/insertandremovebyname");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400238 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800239 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700240
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400241 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
242 data->wireEncode().size());
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700243
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400244 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800245 cs.insert(*data2);
246
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400247 shared_ptr<Data> data3 = makeData("/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800248 cs.insert(*data3);
249
250 BOOST_CHECK_EQUAL(cs.size(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700251
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400252 name.append(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800253 cs.erase(name);
254 BOOST_CHECK_EQUAL(cs.size(), 2);
255}
256
257BOOST_AUTO_TEST_CASE(DigestCalculation)
258{
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400259 shared_ptr<Data> data = makeData("/digest/compute");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800260
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400261 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
262 data->wireEncode().size());
263 BOOST_CHECK_EQUAL(digest1->size(), 32);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700264
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400265 cs::Entry* entry = new cs::Entry();
266 entry->setData(*data, false);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700267
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400268 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700269 entry->getFullName()[-1].value_begin(),
270 entry->getFullName()[-1].value_end());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800271}
272
273BOOST_AUTO_TEST_CASE(InsertCanonical)
274{
275 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700276
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400277 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800278 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700279
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400280 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800281 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700282
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400283 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800284 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700285
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400286 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800287 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700288
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400289 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800290 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700291
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400292 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800293 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700294
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400295 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800296 cs.insert(*data7);
297}
298
299BOOST_AUTO_TEST_CASE(EraseCanonical)
300{
301 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700302
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400303 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800304 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700305
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400306 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800307 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700308
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400309 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800310 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700311
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400312 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800313 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700314
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400315 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800316 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700317
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400318 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800319 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700320
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400321 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800322 cs.insert(*data7);
323
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400324 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
325 data->wireEncode().size());
326
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800327 Name name("/a");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400328 name.append(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800329 cs.erase(name);
330 BOOST_CHECK_EQUAL(cs.size(), 6);
331}
332
333BOOST_AUTO_TEST_CASE(ImplicitDigestSelector)
334{
335 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700336
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800337 Name name("/digest/works");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400338 shared_ptr<Data> data = makeData(name);
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("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800342 cs.insert(*data2);
343
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400344 shared_ptr<Data> data3 = makeData("/z/z/z");
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 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
348 data->wireEncode().size());
349 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
350 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700351
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400352 shared_ptr<Interest> interest = make_shared<Interest>();
353 interest->setName(Name(name).append(digest1->buf(), digest1->size()));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800354 interest->setMinSuffixComponents(0);
355 interest->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700356
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800357 const Data* found = cs.find(*interest);
358 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
359 BOOST_CHECK_EQUAL(found->getName(), name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700360
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400361 shared_ptr<Interest> interest2 = make_shared<Interest>();
362 interest2->setName(Name(name).append(digest2, 32));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800363 interest2->setMinSuffixComponents(0);
364 interest2->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700365
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800366 const Data* notfound = cs.find(*interest2);
367 BOOST_CHECK_EQUAL(notfound, static_cast<const Data*>(0));
368}
369
370BOOST_AUTO_TEST_CASE(ChildSelector)
371{
372 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700373
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400374 shared_ptr<Data> data = makeData("/a");
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("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800378 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700379
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400380 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800381 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700382
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400383 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800384 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700385
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400386 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800387 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700388
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400389 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800390 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700391
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800392 shared_ptr<Interest> interest = make_shared<Interest>("/c");
393 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700394
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800395 const Data* found = cs.find(*interest);
396 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
397
398 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
399 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700400
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800401 const Data* found2 = cs.find(*interest2);
402 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
403}
404
405BOOST_AUTO_TEST_CASE(ChildSelector2)
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/b/1");
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("/a/b/2");
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> data3 = makeData("/a/z/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800416 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700417
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400418 shared_ptr<Data> data4 = makeData("/a/z/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800419 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700420
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800421 shared_ptr<Interest> interest = make_shared<Interest>("/a");
422 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700423
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800424 const Data* found = cs.find(*interest);
425 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
426}
427
428BOOST_AUTO_TEST_CASE(MustBeFreshSelector)
429{
430 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700431
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800432 Name name("/insert/nonfresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400433 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700434 data->setFreshnessPeriod(time::milliseconds(500));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700435 signData(data);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800436 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700437
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800438 sleep(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700439
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800440 shared_ptr<Interest> interest = make_shared<Interest>(name);
441 interest->setMustBeFresh(true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700442
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800443 const Data* found = cs.find(*interest);
444 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
445}
446
Ilya Moiseenko9c9231b2014-04-10 11:05:12 -0400447BOOST_AUTO_TEST_CASE(PublisherKeySelector)
448{
449 Cs cs;
450
451 Name name("/insert/withkey");
452 shared_ptr<Data> data = makeData(name);
453 cs.insert(*data);
454
455 shared_ptr<Interest> interest = make_shared<Interest>(name);
456 Name keyName("/somewhere/key");
457
458 ndn::KeyLocator locator(keyName);
459 interest->setPublisherPublicKeyLocator(locator);
460
461 const Data* found = cs.find(*interest);
462 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
463}
464
465BOOST_AUTO_TEST_CASE(PublisherKeySelector2)
466{
467 Cs cs;
468 Name name("/insert/withkey");
469 shared_ptr<Data> data = makeData(name);
470 cs.insert(*data);
471
472 Name name2("/insert/withkey2");
473 shared_ptr<Data> data2 = make_shared<Data>(name2);
474
475 Name keyName("/somewhere/key");
476 const ndn::KeyLocator locator(keyName);
477
478 ndn::SignatureSha256WithRsa fakeSignature;
479 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
480 reinterpret_cast<const uint8_t*>(0), 0));
481
482 fakeSignature.setKeyLocator(locator);
483 data2->setSignature(fakeSignature);
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700484 data2->wireEncode();
Ilya Moiseenko9c9231b2014-04-10 11:05:12 -0400485
486 cs.insert(*data2);
487
488 shared_ptr<Interest> interest = make_shared<Interest>(name2);
489 interest->setPublisherPublicKeyLocator(locator);
490
491 const Data* found = cs.find(*interest);
492 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
493 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
494}
495
496
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800497BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
498{
499 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700500
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400501 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800502 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700503
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400504 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800505 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700506
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400507 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800508 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700509
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400510 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800511 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700512
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400513 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800514 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700515
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400516 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800517 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700518
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400519 shared_ptr<Data> data8 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800520 cs.insert(*data8);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700521
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800522 shared_ptr<Interest> interest = make_shared<Interest>("/c/c");
523 interest->setMinSuffixComponents(3);
524 interest->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700525
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800526 const Data* found = cs.find(*interest);
527 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6");
528
529 shared_ptr<Interest> interest2 = make_shared<Interest>("/c/c");
530 interest2->setMinSuffixComponents(4);
531 interest2->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700532
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800533 const Data* found2 = cs.find(*interest2);
534 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700535
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800536 shared_ptr<Interest> interest3 = make_shared<Interest>("/c/c");
537 interest3->setMaxSuffixComponents(2);
538 interest3->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700539
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800540 const Data* found3 = cs.find(*interest3);
541 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
542}
543
544BOOST_AUTO_TEST_CASE(ExcludeSelector)
545{
546 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700547
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400548 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800549 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700550
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400551 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800552 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700553
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400554 shared_ptr<Data> data3 = makeData("/c/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800555 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700556
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400557 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800558 cs.insert(*data4);
559
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400560 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800561 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700562
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400563 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800564 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700565
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400566 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800567 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700568
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800569 shared_ptr<Interest> interest = make_shared<Interest>("/c");
570 interest->setChildSelector(1);
571 Exclude e;
572 e.excludeOne (Name::Component("n"));
573 interest->setExclude(e);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700574
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800575 const Data* found = cs.find(*interest);
576 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
577
578 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
579 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700580
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800581 Exclude e2;
582 e2.excludeOne (Name::Component("a"));
583 interest2->setExclude(e2);
584
585 const Data* found2 = cs.find(*interest2);
586 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700587
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800588 shared_ptr<Interest> interest3 = make_shared<Interest>("/c");
589 interest3->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700590
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800591 Exclude e3;
592 e3.excludeOne (Name::Component("c"));
593 interest3->setExclude(e3);
594
595 const Data* found3 = cs.find(*interest3);
596 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
597}
598
599//
600
601class FindFixture : protected BaseFixture
602{
603protected:
604 void
605 insert(uint32_t id, const Name& name)
606 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400607 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700608 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800609 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700610 signData(data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700611
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800612 m_cs.insert(*data);
613 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700614
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800615 Interest&
616 startInterest(const Name& name)
617 {
618 m_interest = make_shared<Interest>(name);
619 return *m_interest;
620 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700621
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800622 uint32_t
623 find()
624 {
625 const Data* found = m_cs.find(*m_interest);
626 if (found == 0) {
627 return 0;
628 }
629 const Block& content = found->getContent();
630 if (content.value_size() != sizeof(uint32_t)) {
631 return 0;
632 }
633 return *reinterpret_cast<const uint32_t*>(content.value());
634 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700635
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800636protected:
637 Cs m_cs;
638 shared_ptr<Interest> m_interest;
639};
640
641BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
642
643BOOST_AUTO_TEST_CASE(EmptyDataName)
644{
645 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700646
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800647 startInterest("ndn:/");
648 BOOST_CHECK_EQUAL(find(), 1);
649}
650
651BOOST_AUTO_TEST_CASE(EmptyInterestName)
652{
653 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700654
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800655 startInterest("ndn:/");
656 BOOST_CHECK_EQUAL(find(), 1);
657}
658
659BOOST_AUTO_TEST_CASE(Leftmost)
660{
661 insert(1, "ndn:/A");
662 insert(2, "ndn:/B/p/1");
663 insert(3, "ndn:/B/p/2");
664 insert(4, "ndn:/B/q/1");
665 insert(5, "ndn:/B/q/2");
666 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700667
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800668 startInterest("ndn:/B");
669 BOOST_CHECK_EQUAL(find(), 2);
670}
671
672BOOST_AUTO_TEST_CASE(Rightmost)
673{
674 insert(1, "ndn:/A");
675 insert(2, "ndn:/B/p/1");
676 insert(3, "ndn:/B/p/2");
677 insert(4, "ndn:/B/q/1");
678 insert(5, "ndn:/B/q/2");
679 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700680
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800681 startInterest("ndn:/B")
682 .setChildSelector(1);
683 BOOST_CHECK_EQUAL(find(), 4);
684}
685
686BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
687{
688 insert(1, "ndn:/");
689 insert(2, "ndn:/A/B");
690 insert(3, "ndn:/A/C");
691 insert(4, "ndn:/A");
692 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700693
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800694 // Intuitively you would think Data 4 should be between Data 1 and 2,
695 // but Data 4 has full Name ndn:/A/<32-octet hash>.
696 startInterest("ndn:/A");
697 BOOST_CHECK_EQUAL(find(), 2);
698}
699
700BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
701{
702 insert(1, "ndn:/");
703 insert(2, "ndn:/A");
704 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
705 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
706 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700707
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800708 // Data 2 is returned, because <32-octet hash> is less than Data 3.
709 startInterest("ndn:/A");
710 BOOST_CHECK_EQUAL(find(), 2);
711}
712
713BOOST_AUTO_TEST_CASE(MinSuffixComponents)
714{
715 insert(1, "ndn:/A/1/2/3/4");
716 insert(2, "ndn:/B/1/2/3");
717 insert(3, "ndn:/C/1/2");
718 insert(4, "ndn:/D/1");
719 insert(5, "ndn:/E");
720 insert(6, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700721
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800722 startInterest("ndn:/")
723 .setChildSelector(1)
724 .setMinSuffixComponents(0);
725 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700726
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800727 startInterest("ndn:/")
728 .setChildSelector(1)
729 .setMinSuffixComponents(1);
730 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700731
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800732 startInterest("ndn:/")
733 .setChildSelector(1)
734 .setMinSuffixComponents(2);
735 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700736
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800737 startInterest("ndn:/")
738 .setChildSelector(1)
739 .setMinSuffixComponents(3);
740 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700741
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800742 startInterest("ndn:/")
743 .setChildSelector(1)
744 .setMinSuffixComponents(4);
745 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700746
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800747 startInterest("ndn:/")
748 .setChildSelector(1)
749 .setMinSuffixComponents(5);
750 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700751
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800752 startInterest("ndn:/")
753 .setChildSelector(1)
754 .setMinSuffixComponents(6);
755 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700756
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800757 startInterest("ndn:/")
758 .setChildSelector(1)
759 .setMinSuffixComponents(7);
760 BOOST_CHECK_EQUAL(find(), 0);
761}
762
763BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
764{
765 insert(1, "ndn:/");
766 insert(2, "ndn:/A");
767 insert(3, "ndn:/A/B");
768 insert(4, "ndn:/A/B/C");
769 insert(5, "ndn:/A/B/C/D");
770 insert(6, "ndn:/A/B/C/D/E");
771 // 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 -0700772
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800773 startInterest("ndn:/")
774 .setMaxSuffixComponents(0);
775 BOOST_CHECK_EQUAL(find(), 0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700776
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800777 startInterest("ndn:/")
778 .setMaxSuffixComponents(1);
779 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700780
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800781 startInterest("ndn:/")
782 .setMaxSuffixComponents(2);
783 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700784
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800785 startInterest("ndn:/")
786 .setMaxSuffixComponents(3);
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 .setMaxSuffixComponents(4);
791 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700792
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800793 startInterest("ndn:/")
794 .setMaxSuffixComponents(5);
795 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700796
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800797 startInterest("ndn:/")
798 .setMaxSuffixComponents(6);
799 BOOST_CHECK_EQUAL(find(), 6);
800}
801
802BOOST_AUTO_TEST_CASE(DigestOrder)
803{
804 insert(1, "ndn:/A");
805 insert(2, "ndn:/A");
806 // We don't know which comes first, but there must be some order
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700807
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800808 startInterest("ndn:/A")
809 .setChildSelector(0);
810 uint32_t leftmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700811
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800812 startInterest("ndn:/A")
813 .setChildSelector(1);
814 uint32_t rightmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700815
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800816 BOOST_CHECK_NE(leftmost, rightmost);
817}
818
819BOOST_AUTO_TEST_CASE(DigestExclude)
820{
821 insert(1, "ndn:/A/B");
822 insert(2, "ndn:/A");
823 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700824
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800825 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400826 .setExclude(Exclude().excludeBefore(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800827 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
828 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
829 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700830
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800831 startInterest("ndn:/A")
832 .setChildSelector(1)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400833 .setExclude(Exclude().excludeAfter(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800834 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
835 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
836 "\x00"), 33))); // 33 0x00's
837 BOOST_CHECK_EQUAL(find(), 2);
838}
839
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400840BOOST_AUTO_TEST_CASE(ExactName32)
841{
842 insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
843 insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800844
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400845 startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
846 BOOST_CHECK_EQUAL(find(), 1);
847}
848
849BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
850{
851 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
852 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
853 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
854 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
855 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
856 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
857
858 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
859 .setChildSelector(1)
860 .setMinSuffixComponents(0);
861 BOOST_CHECK_EQUAL(find(), 6);
862
863 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
864 .setChildSelector(1)
865 .setMinSuffixComponents(1);
866 BOOST_CHECK_EQUAL(find(), 6);
867
868 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
869 .setChildSelector(1)
870 .setMinSuffixComponents(2);
871 BOOST_CHECK_EQUAL(find(), 5);
872
873 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
874 .setChildSelector(1)
875 .setMinSuffixComponents(3);
876 BOOST_CHECK_EQUAL(find(), 4);
877
878 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
879 .setChildSelector(1)
880 .setMinSuffixComponents(4);
881 BOOST_CHECK_EQUAL(find(), 3);
882
883 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
884 .setChildSelector(1)
885 .setMinSuffixComponents(5);
886 BOOST_CHECK_EQUAL(find(), 2);
887
888 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
889 .setChildSelector(1)
890 .setMinSuffixComponents(6);
891 BOOST_CHECK_EQUAL(find(), 1);
892
893 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
894 .setChildSelector(1)
895 .setMinSuffixComponents(7);
896 BOOST_CHECK_EQUAL(find(), 0);
897}
898
899BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
900{
901 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
902 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
903 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
904 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
905 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
906 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
907 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
908
909 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
910 .setMaxSuffixComponents(0);
911 BOOST_CHECK_EQUAL(find(), 0);
912
913 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
914 .setMaxSuffixComponents(1);
915 BOOST_CHECK_EQUAL(find(), 1);
916
917 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
918 .setMaxSuffixComponents(2);
919 BOOST_CHECK_EQUAL(find(), 2);
920
921 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
922 .setMaxSuffixComponents(3);
923 BOOST_CHECK_EQUAL(find(), 3);
924
925 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
926 .setMaxSuffixComponents(4);
927 BOOST_CHECK_EQUAL(find(), 4);
928
929 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
930 .setMaxSuffixComponents(5);
931 BOOST_CHECK_EQUAL(find(), 5);
932
933 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
934 .setMaxSuffixComponents(6);
935 BOOST_CHECK_EQUAL(find(), 6);
936}
937
938BOOST_AUTO_TEST_SUITE_END() // Find
939
940BOOST_AUTO_TEST_SUITE_END() // TableCs
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800941
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700942} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800943} // namespace nfd