blob: 1f95894ac2b76ed56550b32df3842ddaf304c826 [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
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400287 name.append(digest1->buf(), digest1->size());
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");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400363 name.append(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
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800532BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
533{
534 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700535
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400536 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800537 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700538
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400539 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800540 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700541
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400542 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800543 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700544
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400545 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800546 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700547
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400548 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800549 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700550
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400551 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800552 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700553
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400554 shared_ptr<Data> data8 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800555 cs.insert(*data8);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700556
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800557 shared_ptr<Interest> interest = make_shared<Interest>("/c/c");
558 interest->setMinSuffixComponents(3);
559 interest->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700560
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800561 const Data* found = cs.find(*interest);
562 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6");
563
564 shared_ptr<Interest> interest2 = make_shared<Interest>("/c/c");
565 interest2->setMinSuffixComponents(4);
566 interest2->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700567
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800568 const Data* found2 = cs.find(*interest2);
569 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700570
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800571 shared_ptr<Interest> interest3 = make_shared<Interest>("/c/c");
572 interest3->setMaxSuffixComponents(2);
573 interest3->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700574
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800575 const Data* found3 = cs.find(*interest3);
576 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
577}
578
579BOOST_AUTO_TEST_CASE(ExcludeSelector)
580{
581 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700582
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400583 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800584 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700585
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400586 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800587 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700588
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400589 shared_ptr<Data> data3 = makeData("/c/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800590 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700591
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400592 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800593 cs.insert(*data4);
594
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400595 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800596 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700597
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400598 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800599 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700600
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400601 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800602 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700603
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800604 shared_ptr<Interest> interest = make_shared<Interest>("/c");
605 interest->setChildSelector(1);
606 Exclude e;
607 e.excludeOne (Name::Component("n"));
608 interest->setExclude(e);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700609
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800610 const Data* found = cs.find(*interest);
611 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
612
613 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
614 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700615
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800616 Exclude e2;
617 e2.excludeOne (Name::Component("a"));
618 interest2->setExclude(e2);
619
620 const Data* found2 = cs.find(*interest2);
621 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700622
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800623 shared_ptr<Interest> interest3 = make_shared<Interest>("/c");
624 interest3->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700625
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800626 Exclude e3;
627 e3.excludeOne (Name::Component("c"));
628 interest3->setExclude(e3);
629
630 const Data* found3 = cs.find(*interest3);
631 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
632}
633
634//
635
636class FindFixture : protected BaseFixture
637{
638protected:
639 void
640 insert(uint32_t id, const Name& name)
641 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400642 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700643 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800644 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -0700645 signData(data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700646
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800647 m_cs.insert(*data);
648 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700649
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800650 Interest&
651 startInterest(const Name& name)
652 {
653 m_interest = make_shared<Interest>(name);
654 return *m_interest;
655 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700656
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800657 uint32_t
658 find()
659 {
660 const Data* found = m_cs.find(*m_interest);
661 if (found == 0) {
662 return 0;
663 }
664 const Block& content = found->getContent();
665 if (content.value_size() != sizeof(uint32_t)) {
666 return 0;
667 }
668 return *reinterpret_cast<const uint32_t*>(content.value());
669 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700670
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800671protected:
672 Cs m_cs;
673 shared_ptr<Interest> m_interest;
674};
675
676BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
677
678BOOST_AUTO_TEST_CASE(EmptyDataName)
679{
680 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700681
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800682 startInterest("ndn:/");
683 BOOST_CHECK_EQUAL(find(), 1);
684}
685
686BOOST_AUTO_TEST_CASE(EmptyInterestName)
687{
688 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700689
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800690 startInterest("ndn:/");
691 BOOST_CHECK_EQUAL(find(), 1);
692}
693
694BOOST_AUTO_TEST_CASE(Leftmost)
695{
696 insert(1, "ndn:/A");
697 insert(2, "ndn:/B/p/1");
698 insert(3, "ndn:/B/p/2");
699 insert(4, "ndn:/B/q/1");
700 insert(5, "ndn:/B/q/2");
701 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700702
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800703 startInterest("ndn:/B");
704 BOOST_CHECK_EQUAL(find(), 2);
705}
706
707BOOST_AUTO_TEST_CASE(Rightmost)
708{
709 insert(1, "ndn:/A");
710 insert(2, "ndn:/B/p/1");
711 insert(3, "ndn:/B/p/2");
712 insert(4, "ndn:/B/q/1");
713 insert(5, "ndn:/B/q/2");
714 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700715
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800716 startInterest("ndn:/B")
717 .setChildSelector(1);
718 BOOST_CHECK_EQUAL(find(), 4);
719}
720
721BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
722{
723 insert(1, "ndn:/");
724 insert(2, "ndn:/A/B");
725 insert(3, "ndn:/A/C");
726 insert(4, "ndn:/A");
727 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700728
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800729 // Intuitively you would think Data 4 should be between Data 1 and 2,
730 // but Data 4 has full Name ndn:/A/<32-octet hash>.
731 startInterest("ndn:/A");
732 BOOST_CHECK_EQUAL(find(), 2);
733}
734
735BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
736{
737 insert(1, "ndn:/");
738 insert(2, "ndn:/A");
739 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
740 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
741 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700742
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800743 // Data 2 is returned, because <32-octet hash> is less than Data 3.
744 startInterest("ndn:/A");
745 BOOST_CHECK_EQUAL(find(), 2);
746}
747
748BOOST_AUTO_TEST_CASE(MinSuffixComponents)
749{
750 insert(1, "ndn:/A/1/2/3/4");
751 insert(2, "ndn:/B/1/2/3");
752 insert(3, "ndn:/C/1/2");
753 insert(4, "ndn:/D/1");
754 insert(5, "ndn:/E");
755 insert(6, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700756
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800757 startInterest("ndn:/")
758 .setChildSelector(1)
759 .setMinSuffixComponents(0);
760 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700761
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800762 startInterest("ndn:/")
763 .setChildSelector(1)
764 .setMinSuffixComponents(1);
765 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700766
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800767 startInterest("ndn:/")
768 .setChildSelector(1)
769 .setMinSuffixComponents(2);
770 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700771
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800772 startInterest("ndn:/")
773 .setChildSelector(1)
774 .setMinSuffixComponents(3);
775 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700776
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800777 startInterest("ndn:/")
778 .setChildSelector(1)
779 .setMinSuffixComponents(4);
780 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700781
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800782 startInterest("ndn:/")
783 .setChildSelector(1)
784 .setMinSuffixComponents(5);
785 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700786
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800787 startInterest("ndn:/")
788 .setChildSelector(1)
789 .setMinSuffixComponents(6);
790 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700791
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800792 startInterest("ndn:/")
793 .setChildSelector(1)
794 .setMinSuffixComponents(7);
795 BOOST_CHECK_EQUAL(find(), 0);
796}
797
798BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
799{
800 insert(1, "ndn:/");
801 insert(2, "ndn:/A");
802 insert(3, "ndn:/A/B");
803 insert(4, "ndn:/A/B/C");
804 insert(5, "ndn:/A/B/C/D");
805 insert(6, "ndn:/A/B/C/D/E");
806 // 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 -0700807
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800808 startInterest("ndn:/")
809 .setMaxSuffixComponents(0);
810 BOOST_CHECK_EQUAL(find(), 0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700811
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800812 startInterest("ndn:/")
813 .setMaxSuffixComponents(1);
814 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700815
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800816 startInterest("ndn:/")
817 .setMaxSuffixComponents(2);
818 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700819
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800820 startInterest("ndn:/")
821 .setMaxSuffixComponents(3);
822 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700823
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800824 startInterest("ndn:/")
825 .setMaxSuffixComponents(4);
826 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700827
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800828 startInterest("ndn:/")
829 .setMaxSuffixComponents(5);
830 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700831
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800832 startInterest("ndn:/")
833 .setMaxSuffixComponents(6);
834 BOOST_CHECK_EQUAL(find(), 6);
835}
836
837BOOST_AUTO_TEST_CASE(DigestOrder)
838{
839 insert(1, "ndn:/A");
840 insert(2, "ndn:/A");
841 // We don't know which comes first, but there must be some order
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700842
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800843 startInterest("ndn:/A")
844 .setChildSelector(0);
845 uint32_t leftmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700846
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800847 startInterest("ndn:/A")
848 .setChildSelector(1);
849 uint32_t rightmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700850
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800851 BOOST_CHECK_NE(leftmost, rightmost);
852}
853
854BOOST_AUTO_TEST_CASE(DigestExclude)
855{
856 insert(1, "ndn:/A/B");
857 insert(2, "ndn:/A");
858 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700859
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800860 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400861 .setExclude(Exclude().excludeBefore(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800862 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
863 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
864 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700865
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800866 startInterest("ndn:/A")
867 .setChildSelector(1)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400868 .setExclude(Exclude().excludeAfter(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800869 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
870 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
871 "\x00"), 33))); // 33 0x00's
872 BOOST_CHECK_EQUAL(find(), 2);
873}
874
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400875BOOST_AUTO_TEST_CASE(ExactName32)
876{
877 insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
878 insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800879
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400880 startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
881 BOOST_CHECK_EQUAL(find(), 1);
882}
883
884BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
885{
886 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
887 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
888 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
889 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
890 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
891 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
892
893 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
894 .setChildSelector(1)
895 .setMinSuffixComponents(0);
896 BOOST_CHECK_EQUAL(find(), 6);
897
898 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
899 .setChildSelector(1)
900 .setMinSuffixComponents(1);
901 BOOST_CHECK_EQUAL(find(), 6);
902
903 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
904 .setChildSelector(1)
905 .setMinSuffixComponents(2);
906 BOOST_CHECK_EQUAL(find(), 5);
907
908 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
909 .setChildSelector(1)
910 .setMinSuffixComponents(3);
911 BOOST_CHECK_EQUAL(find(), 4);
912
913 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
914 .setChildSelector(1)
915 .setMinSuffixComponents(4);
916 BOOST_CHECK_EQUAL(find(), 3);
917
918 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
919 .setChildSelector(1)
920 .setMinSuffixComponents(5);
921 BOOST_CHECK_EQUAL(find(), 2);
922
923 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
924 .setChildSelector(1)
925 .setMinSuffixComponents(6);
926 BOOST_CHECK_EQUAL(find(), 1);
927
928 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
929 .setChildSelector(1)
930 .setMinSuffixComponents(7);
931 BOOST_CHECK_EQUAL(find(), 0);
932}
933
934BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
935{
936 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
937 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
938 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
939 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
940 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
941 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
942 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
943
944 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
945 .setMaxSuffixComponents(0);
946 BOOST_CHECK_EQUAL(find(), 0);
947
948 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
949 .setMaxSuffixComponents(1);
950 BOOST_CHECK_EQUAL(find(), 1);
951
952 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
953 .setMaxSuffixComponents(2);
954 BOOST_CHECK_EQUAL(find(), 2);
955
956 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
957 .setMaxSuffixComponents(3);
958 BOOST_CHECK_EQUAL(find(), 3);
959
960 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
961 .setMaxSuffixComponents(4);
962 BOOST_CHECK_EQUAL(find(), 4);
963
964 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
965 .setMaxSuffixComponents(5);
966 BOOST_CHECK_EQUAL(find(), 5);
967
968 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
969 .setMaxSuffixComponents(6);
970 BOOST_CHECK_EQUAL(find(), 6);
971}
972
973BOOST_AUTO_TEST_SUITE_END() // Find
974
975BOOST_AUTO_TEST_SUITE_END() // TableCs
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800976
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700977} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800978} // namespace nfd