blob: 3e41781ef3daad2f4a952119db0996f661276637 [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));
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));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800173 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700174
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800175 BOOST_CHECK_EQUAL(cs.size(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700176
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400177 sleep(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700178
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800179 cs.evictItem_accessor();
180 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700181
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800182 shared_ptr<Interest> interest = make_shared<Interest>(name2);
183 const Data* found = cs.find(*interest);
184 BOOST_REQUIRE(found != 0);
185 BOOST_CHECK_EQUAL(found->getName(), name2);
186}
187
188//even max size
189BOOST_AUTO_TEST_CASE(ReplacementEvenSize)
190{
191 Cs cs(4);
192
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400193 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800194 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700195
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400196 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800197 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700198
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400199 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800200 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700201
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400202 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800203 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700204
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400205 shared_ptr<Data> data5 = makeData("/e");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800206 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700207
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800208 BOOST_CHECK_EQUAL(cs.size(), 4);
209}
210
211//odd max size
212BOOST_AUTO_TEST_CASE(Replacement2)
213{
214 Cs cs(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700215
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400216 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800217 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700218
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400219 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800220 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700221
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400222 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800223 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700224
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400225 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800226 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700227
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800228 BOOST_CHECK_EQUAL(cs.size(), 3);
229}
230
231BOOST_AUTO_TEST_CASE(InsertAndEraseByName)
232{
233 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700234
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800235 Name name("/insertandremovebyname");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400236 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800237 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700238
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400239 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
240 data->wireEncode().size());
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700241
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400242 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800243 cs.insert(*data2);
244
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400245 shared_ptr<Data> data3 = makeData("/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800246 cs.insert(*data3);
247
248 BOOST_CHECK_EQUAL(cs.size(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700249
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400250 name.append(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800251 cs.erase(name);
252 BOOST_CHECK_EQUAL(cs.size(), 2);
253}
254
255BOOST_AUTO_TEST_CASE(DigestCalculation)
256{
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400257 shared_ptr<Data> data = makeData("/digest/compute");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800258
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400259 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
260 data->wireEncode().size());
261 BOOST_CHECK_EQUAL(digest1->size(), 32);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700262
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400263 cs::Entry* entry = new cs::Entry();
264 entry->setData(*data, false);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700265
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400266 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
267 entry->getDigest()->begin(), entry->getDigest()->end());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800268}
269
270BOOST_AUTO_TEST_CASE(InsertCanonical)
271{
272 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700273
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400274 shared_ptr<Data> data = makeData("/a");
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 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800278 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700279
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400280 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800281 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700282
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400283 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800284 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700285
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400286 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800287 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700288
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400289 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800290 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700291
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400292 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800293 cs.insert(*data7);
294}
295
296BOOST_AUTO_TEST_CASE(EraseCanonical)
297{
298 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700299
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400300 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800301 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700302
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400303 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800304 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700305
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400306 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800307 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700308
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400309 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800310 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700311
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400312 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800313 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700314
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400315 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800316 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700317
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400318 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800319 cs.insert(*data7);
320
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400321 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
322 data->wireEncode().size());
323
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800324 Name name("/a");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400325 name.append(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800326 cs.erase(name);
327 BOOST_CHECK_EQUAL(cs.size(), 6);
328}
329
330BOOST_AUTO_TEST_CASE(ImplicitDigestSelector)
331{
332 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700333
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800334 Name name("/digest/works");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400335 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800336 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700337
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400338 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800339 cs.insert(*data2);
340
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400341 shared_ptr<Data> data3 = makeData("/z/z/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800342 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700343
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400344 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
345 data->wireEncode().size());
346 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
347 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700348
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400349 shared_ptr<Interest> interest = make_shared<Interest>();
350 interest->setName(Name(name).append(digest1->buf(), digest1->size()));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800351 interest->setMinSuffixComponents(0);
352 interest->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700353
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800354 const Data* found = cs.find(*interest);
355 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
356 BOOST_CHECK_EQUAL(found->getName(), name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700357
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400358 shared_ptr<Interest> interest2 = make_shared<Interest>();
359 interest2->setName(Name(name).append(digest2, 32));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800360 interest2->setMinSuffixComponents(0);
361 interest2->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700362
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800363 const Data* notfound = cs.find(*interest2);
364 BOOST_CHECK_EQUAL(notfound, static_cast<const Data*>(0));
365}
366
367BOOST_AUTO_TEST_CASE(ChildSelector)
368{
369 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700370
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400371 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800372 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700373
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400374 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800375 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700376
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400377 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800378 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700379
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400380 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800381 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700382
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400383 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800384 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700385
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400386 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800387 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700388
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800389 shared_ptr<Interest> interest = make_shared<Interest>("/c");
390 interest->setChildSelector(1);
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_EQUAL(found->getName(), "/c/n");
394
395 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
396 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700397
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800398 const Data* found2 = cs.find(*interest2);
399 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
400}
401
402BOOST_AUTO_TEST_CASE(ChildSelector2)
403{
404 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700405
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400406 shared_ptr<Data> data = makeData("/a/b/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800407 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700408
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400409 shared_ptr<Data> data2 = makeData("/a/b/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800410 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700411
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400412 shared_ptr<Data> data3 = makeData("/a/z/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800413 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700414
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400415 shared_ptr<Data> data4 = makeData("/a/z/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800416 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700417
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800418 shared_ptr<Interest> interest = make_shared<Interest>("/a");
419 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700420
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800421 const Data* found = cs.find(*interest);
422 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
423}
424
425BOOST_AUTO_TEST_CASE(MustBeFreshSelector)
426{
427 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700428
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800429 Name name("/insert/nonfresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400430 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700431 data->setFreshnessPeriod(time::milliseconds(500));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800432 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700433
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800434 sleep(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700435
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800436 shared_ptr<Interest> interest = make_shared<Interest>(name);
437 interest->setMustBeFresh(true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700438
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800439 const Data* found = cs.find(*interest);
440 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
441}
442
Ilya Moiseenko9c9231b2014-04-10 11:05:12 -0400443BOOST_AUTO_TEST_CASE(PublisherKeySelector)
444{
445 Cs cs;
446
447 Name name("/insert/withkey");
448 shared_ptr<Data> data = makeData(name);
449 cs.insert(*data);
450
451 shared_ptr<Interest> interest = make_shared<Interest>(name);
452 Name keyName("/somewhere/key");
453
454 ndn::KeyLocator locator(keyName);
455 interest->setPublisherPublicKeyLocator(locator);
456
457 const Data* found = cs.find(*interest);
458 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
459}
460
461BOOST_AUTO_TEST_CASE(PublisherKeySelector2)
462{
463 Cs cs;
464 Name name("/insert/withkey");
465 shared_ptr<Data> data = makeData(name);
466 cs.insert(*data);
467
468 Name name2("/insert/withkey2");
469 shared_ptr<Data> data2 = make_shared<Data>(name2);
470
471 Name keyName("/somewhere/key");
472 const ndn::KeyLocator locator(keyName);
473
474 ndn::SignatureSha256WithRsa fakeSignature;
475 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
476 reinterpret_cast<const uint8_t*>(0), 0));
477
478 fakeSignature.setKeyLocator(locator);
479 data2->setSignature(fakeSignature);
480
481 cs.insert(*data2);
482
483 shared_ptr<Interest> interest = make_shared<Interest>(name2);
484 interest->setPublisherPublicKeyLocator(locator);
485
486 const Data* found = cs.find(*interest);
487 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
488 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
489}
490
491
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800492BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
493{
494 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700495
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400496 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800497 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700498
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400499 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800500 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700501
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400502 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800503 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700504
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400505 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800506 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700507
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400508 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800509 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700510
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400511 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800512 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700513
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400514 shared_ptr<Data> data8 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800515 cs.insert(*data8);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700516
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800517 shared_ptr<Interest> interest = make_shared<Interest>("/c/c");
518 interest->setMinSuffixComponents(3);
519 interest->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700520
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800521 const Data* found = cs.find(*interest);
522 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6");
523
524 shared_ptr<Interest> interest2 = make_shared<Interest>("/c/c");
525 interest2->setMinSuffixComponents(4);
526 interest2->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700527
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800528 const Data* found2 = cs.find(*interest2);
529 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700530
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800531 shared_ptr<Interest> interest3 = make_shared<Interest>("/c/c");
532 interest3->setMaxSuffixComponents(2);
533 interest3->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700534
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800535 const Data* found3 = cs.find(*interest3);
536 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
537}
538
539BOOST_AUTO_TEST_CASE(ExcludeSelector)
540{
541 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700542
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400543 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800544 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700545
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400546 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800547 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700548
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400549 shared_ptr<Data> data3 = makeData("/c/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800550 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700551
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400552 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800553 cs.insert(*data4);
554
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400555 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800556 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700557
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400558 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800559 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700560
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400561 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800562 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700563
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800564 shared_ptr<Interest> interest = make_shared<Interest>("/c");
565 interest->setChildSelector(1);
566 Exclude e;
567 e.excludeOne (Name::Component("n"));
568 interest->setExclude(e);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700569
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800570 const Data* found = cs.find(*interest);
571 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
572
573 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
574 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700575
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800576 Exclude e2;
577 e2.excludeOne (Name::Component("a"));
578 interest2->setExclude(e2);
579
580 const Data* found2 = cs.find(*interest2);
581 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700582
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800583 shared_ptr<Interest> interest3 = make_shared<Interest>("/c");
584 interest3->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700585
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800586 Exclude e3;
587 e3.excludeOne (Name::Component("c"));
588 interest3->setExclude(e3);
589
590 const Data* found3 = cs.find(*interest3);
591 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
592}
593
594//
595
596class FindFixture : protected BaseFixture
597{
598protected:
599 void
600 insert(uint32_t id, const Name& name)
601 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400602 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700603 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800604 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700605
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800606 m_cs.insert(*data);
607 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700608
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800609 Interest&
610 startInterest(const Name& name)
611 {
612 m_interest = make_shared<Interest>(name);
613 return *m_interest;
614 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700615
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800616 uint32_t
617 find()
618 {
619 const Data* found = m_cs.find(*m_interest);
620 if (found == 0) {
621 return 0;
622 }
623 const Block& content = found->getContent();
624 if (content.value_size() != sizeof(uint32_t)) {
625 return 0;
626 }
627 return *reinterpret_cast<const uint32_t*>(content.value());
628 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700629
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800630protected:
631 Cs m_cs;
632 shared_ptr<Interest> m_interest;
633};
634
635BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
636
637BOOST_AUTO_TEST_CASE(EmptyDataName)
638{
639 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700640
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800641 startInterest("ndn:/");
642 BOOST_CHECK_EQUAL(find(), 1);
643}
644
645BOOST_AUTO_TEST_CASE(EmptyInterestName)
646{
647 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700648
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800649 startInterest("ndn:/");
650 BOOST_CHECK_EQUAL(find(), 1);
651}
652
653BOOST_AUTO_TEST_CASE(Leftmost)
654{
655 insert(1, "ndn:/A");
656 insert(2, "ndn:/B/p/1");
657 insert(3, "ndn:/B/p/2");
658 insert(4, "ndn:/B/q/1");
659 insert(5, "ndn:/B/q/2");
660 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700661
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800662 startInterest("ndn:/B");
663 BOOST_CHECK_EQUAL(find(), 2);
664}
665
666BOOST_AUTO_TEST_CASE(Rightmost)
667{
668 insert(1, "ndn:/A");
669 insert(2, "ndn:/B/p/1");
670 insert(3, "ndn:/B/p/2");
671 insert(4, "ndn:/B/q/1");
672 insert(5, "ndn:/B/q/2");
673 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700674
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800675 startInterest("ndn:/B")
676 .setChildSelector(1);
677 BOOST_CHECK_EQUAL(find(), 4);
678}
679
680BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
681{
682 insert(1, "ndn:/");
683 insert(2, "ndn:/A/B");
684 insert(3, "ndn:/A/C");
685 insert(4, "ndn:/A");
686 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700687
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800688 // Intuitively you would think Data 4 should be between Data 1 and 2,
689 // but Data 4 has full Name ndn:/A/<32-octet hash>.
690 startInterest("ndn:/A");
691 BOOST_CHECK_EQUAL(find(), 2);
692}
693
694BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
695{
696 insert(1, "ndn:/");
697 insert(2, "ndn:/A");
698 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
699 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
700 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700701
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800702 // Data 2 is returned, because <32-octet hash> is less than Data 3.
703 startInterest("ndn:/A");
704 BOOST_CHECK_EQUAL(find(), 2);
705}
706
707BOOST_AUTO_TEST_CASE(MinSuffixComponents)
708{
709 insert(1, "ndn:/A/1/2/3/4");
710 insert(2, "ndn:/B/1/2/3");
711 insert(3, "ndn:/C/1/2");
712 insert(4, "ndn:/D/1");
713 insert(5, "ndn:/E");
714 insert(6, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700715
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800716 startInterest("ndn:/")
717 .setChildSelector(1)
718 .setMinSuffixComponents(0);
719 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700720
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800721 startInterest("ndn:/")
722 .setChildSelector(1)
723 .setMinSuffixComponents(1);
724 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700725
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800726 startInterest("ndn:/")
727 .setChildSelector(1)
728 .setMinSuffixComponents(2);
729 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700730
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800731 startInterest("ndn:/")
732 .setChildSelector(1)
733 .setMinSuffixComponents(3);
734 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700735
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800736 startInterest("ndn:/")
737 .setChildSelector(1)
738 .setMinSuffixComponents(4);
739 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700740
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800741 startInterest("ndn:/")
742 .setChildSelector(1)
743 .setMinSuffixComponents(5);
744 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700745
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800746 startInterest("ndn:/")
747 .setChildSelector(1)
748 .setMinSuffixComponents(6);
749 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700750
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800751 startInterest("ndn:/")
752 .setChildSelector(1)
753 .setMinSuffixComponents(7);
754 BOOST_CHECK_EQUAL(find(), 0);
755}
756
757BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
758{
759 insert(1, "ndn:/");
760 insert(2, "ndn:/A");
761 insert(3, "ndn:/A/B");
762 insert(4, "ndn:/A/B/C");
763 insert(5, "ndn:/A/B/C/D");
764 insert(6, "ndn:/A/B/C/D/E");
765 // 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 -0700766
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800767 startInterest("ndn:/")
768 .setMaxSuffixComponents(0);
769 BOOST_CHECK_EQUAL(find(), 0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700770
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800771 startInterest("ndn:/")
772 .setMaxSuffixComponents(1);
773 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700774
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800775 startInterest("ndn:/")
776 .setMaxSuffixComponents(2);
777 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700778
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800779 startInterest("ndn:/")
780 .setMaxSuffixComponents(3);
781 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700782
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800783 startInterest("ndn:/")
784 .setMaxSuffixComponents(4);
785 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700786
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800787 startInterest("ndn:/")
788 .setMaxSuffixComponents(5);
789 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700790
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800791 startInterest("ndn:/")
792 .setMaxSuffixComponents(6);
793 BOOST_CHECK_EQUAL(find(), 6);
794}
795
796BOOST_AUTO_TEST_CASE(DigestOrder)
797{
798 insert(1, "ndn:/A");
799 insert(2, "ndn:/A");
800 // We don't know which comes first, but there must be some order
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700801
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800802 startInterest("ndn:/A")
803 .setChildSelector(0);
804 uint32_t leftmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700805
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800806 startInterest("ndn:/A")
807 .setChildSelector(1);
808 uint32_t rightmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700809
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800810 BOOST_CHECK_NE(leftmost, rightmost);
811}
812
813BOOST_AUTO_TEST_CASE(DigestExclude)
814{
815 insert(1, "ndn:/A/B");
816 insert(2, "ndn:/A");
817 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700818
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800819 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400820 .setExclude(Exclude().excludeBefore(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800821 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
822 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
823 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700824
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800825 startInterest("ndn:/A")
826 .setChildSelector(1)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400827 .setExclude(Exclude().excludeAfter(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800828 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
829 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
830 "\x00"), 33))); // 33 0x00's
831 BOOST_CHECK_EQUAL(find(), 2);
832}
833
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400834BOOST_AUTO_TEST_CASE(ExactName32)
835{
836 insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
837 insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800838
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400839 startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
840 BOOST_CHECK_EQUAL(find(), 1);
841}
842
843BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
844{
845 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
846 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
847 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
848 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
849 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
850 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
851
852 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
853 .setChildSelector(1)
854 .setMinSuffixComponents(0);
855 BOOST_CHECK_EQUAL(find(), 6);
856
857 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
858 .setChildSelector(1)
859 .setMinSuffixComponents(1);
860 BOOST_CHECK_EQUAL(find(), 6);
861
862 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
863 .setChildSelector(1)
864 .setMinSuffixComponents(2);
865 BOOST_CHECK_EQUAL(find(), 5);
866
867 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
868 .setChildSelector(1)
869 .setMinSuffixComponents(3);
870 BOOST_CHECK_EQUAL(find(), 4);
871
872 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
873 .setChildSelector(1)
874 .setMinSuffixComponents(4);
875 BOOST_CHECK_EQUAL(find(), 3);
876
877 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
878 .setChildSelector(1)
879 .setMinSuffixComponents(5);
880 BOOST_CHECK_EQUAL(find(), 2);
881
882 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
883 .setChildSelector(1)
884 .setMinSuffixComponents(6);
885 BOOST_CHECK_EQUAL(find(), 1);
886
887 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
888 .setChildSelector(1)
889 .setMinSuffixComponents(7);
890 BOOST_CHECK_EQUAL(find(), 0);
891}
892
893BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
894{
895 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
896 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
897 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
898 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
899 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
900 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
901 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
902
903 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
904 .setMaxSuffixComponents(0);
905 BOOST_CHECK_EQUAL(find(), 0);
906
907 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
908 .setMaxSuffixComponents(1);
909 BOOST_CHECK_EQUAL(find(), 1);
910
911 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
912 .setMaxSuffixComponents(2);
913 BOOST_CHECK_EQUAL(find(), 2);
914
915 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
916 .setMaxSuffixComponents(3);
917 BOOST_CHECK_EQUAL(find(), 3);
918
919 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
920 .setMaxSuffixComponents(4);
921 BOOST_CHECK_EQUAL(find(), 4);
922
923 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
924 .setMaxSuffixComponents(5);
925 BOOST_CHECK_EQUAL(find(), 5);
926
927 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
928 .setMaxSuffixComponents(6);
929 BOOST_CHECK_EQUAL(find(), 6);
930}
931
932BOOST_AUTO_TEST_SUITE_END() // Find
933
934BOOST_AUTO_TEST_SUITE_END() // TableCs
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800935
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700936} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800937} // namespace nfd