blob: cdcc0db7db64f28d63441bc6d60ac6016881130a [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
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080047BOOST_AUTO_TEST_CASE(Insertion)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070048{
49 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070050
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040051 BOOST_CHECK_EQUAL(cs.insert(*makeData("/insertion")), true);
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070052}
53
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080054BOOST_AUTO_TEST_CASE(Insertion2)
55{
56 Cs cs;
57
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040058 cs.insert(*makeData("/a"));
59 cs.insert(*makeData("/b"));
60 cs.insert(*makeData("/c"));
61 cs.insert(*makeData("/d"));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070062
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080063 BOOST_CHECK_EQUAL(cs.size(), 4);
64}
65
66
67BOOST_AUTO_TEST_CASE(DuplicateInsertion)
68{
69 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070070
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040071 shared_ptr<Data> data0 = makeData("/insert/smth");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080072 BOOST_CHECK_EQUAL(cs.insert(*data0), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070073
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040074 shared_ptr<Data> data = makeData("/insert/duplicate");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080075 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070076
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080077 cs.insert(*data);
78 BOOST_CHECK_EQUAL(cs.size(), 2);
79}
80
81
82BOOST_AUTO_TEST_CASE(DuplicateInsertion2)
83{
84 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070085
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040086 shared_ptr<Data> data = makeData("/insert/duplicate");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080087 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070088
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080089 cs.insert(*data);
90 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070091
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040092 shared_ptr<Data> data2 = makeData("/insert/original");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080093 BOOST_CHECK_EQUAL(cs.insert(*data2), true);
94 BOOST_CHECK_EQUAL(cs.size(), 2);
95}
96
97BOOST_AUTO_TEST_CASE(InsertAndFind)
98{
99 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700100
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800101 Name name("/insert/and/find");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700102
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400103 shared_ptr<Data> data = makeData(name);
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 shared_ptr<Interest> interest = make_shared<Interest>(name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700107
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800108 const Data* found = cs.find(*interest);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700109
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800110 BOOST_REQUIRE(found != 0);
111 BOOST_CHECK_EQUAL(data->getName(), found->getName());
112}
113
114BOOST_AUTO_TEST_CASE(InsertAndNotFind)
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");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400119 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800120 BOOST_CHECK_EQUAL(cs.insert(*data), true);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700121
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800122 Name name2("/not/find");
123 shared_ptr<Interest> interest = make_shared<Interest>(name2);
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_CHECK_EQUAL(found, static_cast<const Data*>(0));
128}
129
130
131BOOST_AUTO_TEST_CASE(InsertAndErase)
132{
133 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700134
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400135 shared_ptr<Data> data = makeData("/insertandelete");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800136 cs.insert(*data);
137 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700138
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800139 cs.evictItem_accessor();
140 BOOST_CHECK_EQUAL(cs.size(), 0);
141}
142
143BOOST_AUTO_TEST_CASE(StalenessQueue)
144{
145 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700146
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800147 Name name2("/insert/fresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400148 shared_ptr<Data> data2 = makeData(name2);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700149 data2->setFreshnessPeriod(time::milliseconds(5000));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800150 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700151
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800152 Name name("/insert/expire");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400153 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700154 data->setFreshnessPeriod(time::milliseconds(500));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800155 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700156
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800157 BOOST_CHECK_EQUAL(cs.size(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700158
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400159 sleep(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700160
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800161 cs.evictItem_accessor();
162 BOOST_CHECK_EQUAL(cs.size(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700163
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800164 shared_ptr<Interest> interest = make_shared<Interest>(name2);
165 const Data* found = cs.find(*interest);
166 BOOST_REQUIRE(found != 0);
167 BOOST_CHECK_EQUAL(found->getName(), name2);
168}
169
170//even max size
171BOOST_AUTO_TEST_CASE(ReplacementEvenSize)
172{
173 Cs cs(4);
174
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400175 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800176 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700177
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400178 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800179 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700180
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400181 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800182 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700183
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400184 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800185 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700186
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400187 shared_ptr<Data> data5 = makeData("/e");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800188 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700189
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800190 BOOST_CHECK_EQUAL(cs.size(), 4);
191}
192
193//odd max size
194BOOST_AUTO_TEST_CASE(Replacement2)
195{
196 Cs cs(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700197
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400198 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800199 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700200
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400201 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800202 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700203
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400204 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800205 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700206
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400207 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800208 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700209
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800210 BOOST_CHECK_EQUAL(cs.size(), 3);
211}
212
213BOOST_AUTO_TEST_CASE(InsertAndEraseByName)
214{
215 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700216
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800217 Name name("/insertandremovebyname");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400218 shared_ptr<Data> data = makeData(name);
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 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
222 data->wireEncode().size());
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700223
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400224 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800225 cs.insert(*data2);
226
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400227 shared_ptr<Data> data3 = makeData("/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800228 cs.insert(*data3);
229
230 BOOST_CHECK_EQUAL(cs.size(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700231
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400232 name.append(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800233 cs.erase(name);
234 BOOST_CHECK_EQUAL(cs.size(), 2);
235}
236
237BOOST_AUTO_TEST_CASE(DigestCalculation)
238{
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400239 shared_ptr<Data> data = makeData("/digest/compute");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800240
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400241 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
242 data->wireEncode().size());
243 BOOST_CHECK_EQUAL(digest1->size(), 32);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700244
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400245 cs::Entry* entry = new cs::Entry();
246 entry->setData(*data, false);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700247
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400248 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
249 entry->getDigest()->begin(), entry->getDigest()->end());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800250}
251
252BOOST_AUTO_TEST_CASE(InsertCanonical)
253{
254 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700255
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400256 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800257 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700258
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400259 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800260 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700261
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400262 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800263 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700264
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400265 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800266 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700267
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400268 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800269 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700270
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400271 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800272 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700273
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400274 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800275 cs.insert(*data7);
276}
277
278BOOST_AUTO_TEST_CASE(EraseCanonical)
279{
280 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700281
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400282 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800283 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700284
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400285 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800286 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700287
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400288 shared_ptr<Data> data3 = makeData("/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800289 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700290
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400291 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800292 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700293
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400294 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800295 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700296
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400297 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800298 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700299
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400300 shared_ptr<Data> data7 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800301 cs.insert(*data7);
302
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400303 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
304 data->wireEncode().size());
305
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800306 Name name("/a");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400307 name.append(digest1->buf(), digest1->size());
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800308 cs.erase(name);
309 BOOST_CHECK_EQUAL(cs.size(), 6);
310}
311
312BOOST_AUTO_TEST_CASE(ImplicitDigestSelector)
313{
314 CsAccessor cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700315
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800316 Name name("/digest/works");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400317 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800318 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700319
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400320 shared_ptr<Data> data2 = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800321 cs.insert(*data2);
322
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400323 shared_ptr<Data> data3 = makeData("/z/z/z");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800324 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700325
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400326 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
327 data->wireEncode().size());
328 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700330
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400331 shared_ptr<Interest> interest = make_shared<Interest>();
332 interest->setName(Name(name).append(digest1->buf(), digest1->size()));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800333 interest->setMinSuffixComponents(0);
334 interest->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700335
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800336 const Data* found = cs.find(*interest);
337 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
338 BOOST_CHECK_EQUAL(found->getName(), name);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700339
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400340 shared_ptr<Interest> interest2 = make_shared<Interest>();
341 interest2->setName(Name(name).append(digest2, 32));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800342 interest2->setMinSuffixComponents(0);
343 interest2->setMaxSuffixComponents(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700344
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800345 const Data* notfound = cs.find(*interest2);
346 BOOST_CHECK_EQUAL(notfound, static_cast<const Data*>(0));
347}
348
349BOOST_AUTO_TEST_CASE(ChildSelector)
350{
351 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700352
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400353 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800354 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700355
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400356 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800357 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700358
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400359 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800360 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700361
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400362 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800363 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700364
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400365 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800366 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700367
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400368 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800369 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700370
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800371 shared_ptr<Interest> interest = make_shared<Interest>("/c");
372 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700373
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800374 const Data* found = cs.find(*interest);
375 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
376
377 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
378 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700379
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800380 const Data* found2 = cs.find(*interest2);
381 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
382}
383
384BOOST_AUTO_TEST_CASE(ChildSelector2)
385{
386 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700387
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400388 shared_ptr<Data> data = makeData("/a/b/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800389 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700390
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400391 shared_ptr<Data> data2 = makeData("/a/b/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800392 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700393
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400394 shared_ptr<Data> data3 = makeData("/a/z/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800395 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700396
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400397 shared_ptr<Data> data4 = makeData("/a/z/2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800398 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700399
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800400 shared_ptr<Interest> interest = make_shared<Interest>("/a");
401 interest->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700402
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800403 const Data* found = cs.find(*interest);
404 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
405}
406
407BOOST_AUTO_TEST_CASE(MustBeFreshSelector)
408{
409 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700410
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800411 Name name("/insert/nonfresh");
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400412 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700413 data->setFreshnessPeriod(time::milliseconds(500));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800414 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700415
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800416 sleep(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700417
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800418 shared_ptr<Interest> interest = make_shared<Interest>(name);
419 interest->setMustBeFresh(true);
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, static_cast<const Data*>(0));
423}
424
Ilya Moiseenko9c9231b2014-04-10 11:05:12 -0400425BOOST_AUTO_TEST_CASE(PublisherKeySelector)
426{
427 Cs cs;
428
429 Name name("/insert/withkey");
430 shared_ptr<Data> data = makeData(name);
431 cs.insert(*data);
432
433 shared_ptr<Interest> interest = make_shared<Interest>(name);
434 Name keyName("/somewhere/key");
435
436 ndn::KeyLocator locator(keyName);
437 interest->setPublisherPublicKeyLocator(locator);
438
439 const Data* found = cs.find(*interest);
440 BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
441}
442
443BOOST_AUTO_TEST_CASE(PublisherKeySelector2)
444{
445 Cs cs;
446 Name name("/insert/withkey");
447 shared_ptr<Data> data = makeData(name);
448 cs.insert(*data);
449
450 Name name2("/insert/withkey2");
451 shared_ptr<Data> data2 = make_shared<Data>(name2);
452
453 Name keyName("/somewhere/key");
454 const ndn::KeyLocator locator(keyName);
455
456 ndn::SignatureSha256WithRsa fakeSignature;
457 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
458 reinterpret_cast<const uint8_t*>(0), 0));
459
460 fakeSignature.setKeyLocator(locator);
461 data2->setSignature(fakeSignature);
462
463 cs.insert(*data2);
464
465 shared_ptr<Interest> interest = make_shared<Interest>(name2);
466 interest->setPublisherPublicKeyLocator(locator);
467
468 const Data* found = cs.find(*interest);
469 BOOST_CHECK_NE(found, static_cast<const Data*>(0));
470 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
471}
472
473
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800474BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
475{
476 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700477
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400478 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800479 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700480
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400481 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800482 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700483
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400484 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800485 cs.insert(*data4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700486
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400487 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800488 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700489
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400490 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800491 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700492
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400493 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800494 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700495
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400496 shared_ptr<Data> data8 = makeData("/c/c/1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800497 cs.insert(*data8);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700498
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800499 shared_ptr<Interest> interest = make_shared<Interest>("/c/c");
500 interest->setMinSuffixComponents(3);
501 interest->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700502
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800503 const Data* found = cs.find(*interest);
504 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6");
505
506 shared_ptr<Interest> interest2 = make_shared<Interest>("/c/c");
507 interest2->setMinSuffixComponents(4);
508 interest2->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700509
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800510 const Data* found2 = cs.find(*interest2);
511 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700512
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800513 shared_ptr<Interest> interest3 = make_shared<Interest>("/c/c");
514 interest3->setMaxSuffixComponents(2);
515 interest3->setChildSelector(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700516
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800517 const Data* found3 = cs.find(*interest3);
518 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
519}
520
521BOOST_AUTO_TEST_CASE(ExcludeSelector)
522{
523 Cs cs;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700524
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400525 shared_ptr<Data> data = makeData("/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800526 cs.insert(*data);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700527
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400528 shared_ptr<Data> data2 = makeData("/b");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800529 cs.insert(*data2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700530
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400531 shared_ptr<Data> data3 = makeData("/c/a");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800532 cs.insert(*data3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700533
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400534 shared_ptr<Data> data4 = makeData("/d");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800535 cs.insert(*data4);
536
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400537 shared_ptr<Data> data5 = makeData("/c/c");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800538 cs.insert(*data5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700539
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400540 shared_ptr<Data> data6 = makeData("/c/f");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800541 cs.insert(*data6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700542
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400543 shared_ptr<Data> data7 = makeData("/c/n");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800544 cs.insert(*data7);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700545
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800546 shared_ptr<Interest> interest = make_shared<Interest>("/c");
547 interest->setChildSelector(1);
548 Exclude e;
549 e.excludeOne (Name::Component("n"));
550 interest->setExclude(e);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700551
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800552 const Data* found = cs.find(*interest);
553 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
554
555 shared_ptr<Interest> interest2 = make_shared<Interest>("/c");
556 interest2->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700557
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800558 Exclude e2;
559 e2.excludeOne (Name::Component("a"));
560 interest2->setExclude(e2);
561
562 const Data* found2 = cs.find(*interest2);
563 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700564
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800565 shared_ptr<Interest> interest3 = make_shared<Interest>("/c");
566 interest3->setChildSelector(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700567
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800568 Exclude e3;
569 e3.excludeOne (Name::Component("c"));
570 interest3->setExclude(e3);
571
572 const Data* found3 = cs.find(*interest3);
573 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
574}
575
576//
577
578class FindFixture : protected BaseFixture
579{
580protected:
581 void
582 insert(uint32_t id, const Name& name)
583 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400584 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700585 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800586 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700587
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800588 m_cs.insert(*data);
589 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700590
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800591 Interest&
592 startInterest(const Name& name)
593 {
594 m_interest = make_shared<Interest>(name);
595 return *m_interest;
596 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700597
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800598 uint32_t
599 find()
600 {
601 const Data* found = m_cs.find(*m_interest);
602 if (found == 0) {
603 return 0;
604 }
605 const Block& content = found->getContent();
606 if (content.value_size() != sizeof(uint32_t)) {
607 return 0;
608 }
609 return *reinterpret_cast<const uint32_t*>(content.value());
610 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700611
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800612protected:
613 Cs m_cs;
614 shared_ptr<Interest> m_interest;
615};
616
617BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
618
619BOOST_AUTO_TEST_CASE(EmptyDataName)
620{
621 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700622
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800623 startInterest("ndn:/");
624 BOOST_CHECK_EQUAL(find(), 1);
625}
626
627BOOST_AUTO_TEST_CASE(EmptyInterestName)
628{
629 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700630
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800631 startInterest("ndn:/");
632 BOOST_CHECK_EQUAL(find(), 1);
633}
634
635BOOST_AUTO_TEST_CASE(Leftmost)
636{
637 insert(1, "ndn:/A");
638 insert(2, "ndn:/B/p/1");
639 insert(3, "ndn:/B/p/2");
640 insert(4, "ndn:/B/q/1");
641 insert(5, "ndn:/B/q/2");
642 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700643
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800644 startInterest("ndn:/B");
645 BOOST_CHECK_EQUAL(find(), 2);
646}
647
648BOOST_AUTO_TEST_CASE(Rightmost)
649{
650 insert(1, "ndn:/A");
651 insert(2, "ndn:/B/p/1");
652 insert(3, "ndn:/B/p/2");
653 insert(4, "ndn:/B/q/1");
654 insert(5, "ndn:/B/q/2");
655 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700656
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800657 startInterest("ndn:/B")
658 .setChildSelector(1);
659 BOOST_CHECK_EQUAL(find(), 4);
660}
661
662BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
663{
664 insert(1, "ndn:/");
665 insert(2, "ndn:/A/B");
666 insert(3, "ndn:/A/C");
667 insert(4, "ndn:/A");
668 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700669
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800670 // Intuitively you would think Data 4 should be between Data 1 and 2,
671 // but Data 4 has full Name ndn:/A/<32-octet hash>.
672 startInterest("ndn:/A");
673 BOOST_CHECK_EQUAL(find(), 2);
674}
675
676BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
677{
678 insert(1, "ndn:/");
679 insert(2, "ndn:/A");
680 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
681 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
682 insert(5, "ndn:/D");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700683
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800684 // Data 2 is returned, because <32-octet hash> is less than Data 3.
685 startInterest("ndn:/A");
686 BOOST_CHECK_EQUAL(find(), 2);
687}
688
689BOOST_AUTO_TEST_CASE(MinSuffixComponents)
690{
691 insert(1, "ndn:/A/1/2/3/4");
692 insert(2, "ndn:/B/1/2/3");
693 insert(3, "ndn:/C/1/2");
694 insert(4, "ndn:/D/1");
695 insert(5, "ndn:/E");
696 insert(6, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700697
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800698 startInterest("ndn:/")
699 .setChildSelector(1)
700 .setMinSuffixComponents(0);
701 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700702
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800703 startInterest("ndn:/")
704 .setChildSelector(1)
705 .setMinSuffixComponents(1);
706 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700707
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800708 startInterest("ndn:/")
709 .setChildSelector(1)
710 .setMinSuffixComponents(2);
711 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700712
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800713 startInterest("ndn:/")
714 .setChildSelector(1)
715 .setMinSuffixComponents(3);
716 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700717
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800718 startInterest("ndn:/")
719 .setChildSelector(1)
720 .setMinSuffixComponents(4);
721 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700722
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800723 startInterest("ndn:/")
724 .setChildSelector(1)
725 .setMinSuffixComponents(5);
726 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700727
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800728 startInterest("ndn:/")
729 .setChildSelector(1)
730 .setMinSuffixComponents(6);
731 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700732
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800733 startInterest("ndn:/")
734 .setChildSelector(1)
735 .setMinSuffixComponents(7);
736 BOOST_CHECK_EQUAL(find(), 0);
737}
738
739BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
740{
741 insert(1, "ndn:/");
742 insert(2, "ndn:/A");
743 insert(3, "ndn:/A/B");
744 insert(4, "ndn:/A/B/C");
745 insert(5, "ndn:/A/B/C/D");
746 insert(6, "ndn:/A/B/C/D/E");
747 // 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 -0700748
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800749 startInterest("ndn:/")
750 .setMaxSuffixComponents(0);
751 BOOST_CHECK_EQUAL(find(), 0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700752
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800753 startInterest("ndn:/")
754 .setMaxSuffixComponents(1);
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 .setMaxSuffixComponents(2);
759 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700760
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800761 startInterest("ndn:/")
762 .setMaxSuffixComponents(3);
763 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700764
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800765 startInterest("ndn:/")
766 .setMaxSuffixComponents(4);
767 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700768
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800769 startInterest("ndn:/")
770 .setMaxSuffixComponents(5);
771 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700772
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800773 startInterest("ndn:/")
774 .setMaxSuffixComponents(6);
775 BOOST_CHECK_EQUAL(find(), 6);
776}
777
778BOOST_AUTO_TEST_CASE(DigestOrder)
779{
780 insert(1, "ndn:/A");
781 insert(2, "ndn:/A");
782 // We don't know which comes first, but there must be some order
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700783
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800784 startInterest("ndn:/A")
785 .setChildSelector(0);
786 uint32_t leftmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700787
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800788 startInterest("ndn:/A")
789 .setChildSelector(1);
790 uint32_t rightmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700791
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800792 BOOST_CHECK_NE(leftmost, rightmost);
793}
794
795BOOST_AUTO_TEST_CASE(DigestExclude)
796{
797 insert(1, "ndn:/A/B");
798 insert(2, "ndn:/A");
799 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700800
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800801 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400802 .setExclude(Exclude().excludeBefore(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800803 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
804 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
805 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700806
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800807 startInterest("ndn:/A")
808 .setChildSelector(1)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400809 .setExclude(Exclude().excludeAfter(name::Component(reinterpret_cast<const uint8_t*>(
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800810 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
811 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
812 "\x00"), 33))); // 33 0x00's
813 BOOST_CHECK_EQUAL(find(), 2);
814}
815
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400816BOOST_AUTO_TEST_CASE(ExactName32)
817{
818 insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
819 insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800820
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400821 startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
822 BOOST_CHECK_EQUAL(find(), 1);
823}
824
825BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
826{
827 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
828 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
829 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
830 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
831 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
832 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
833
834 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
835 .setChildSelector(1)
836 .setMinSuffixComponents(0);
837 BOOST_CHECK_EQUAL(find(), 6);
838
839 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
840 .setChildSelector(1)
841 .setMinSuffixComponents(1);
842 BOOST_CHECK_EQUAL(find(), 6);
843
844 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
845 .setChildSelector(1)
846 .setMinSuffixComponents(2);
847 BOOST_CHECK_EQUAL(find(), 5);
848
849 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
850 .setChildSelector(1)
851 .setMinSuffixComponents(3);
852 BOOST_CHECK_EQUAL(find(), 4);
853
854 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
855 .setChildSelector(1)
856 .setMinSuffixComponents(4);
857 BOOST_CHECK_EQUAL(find(), 3);
858
859 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
860 .setChildSelector(1)
861 .setMinSuffixComponents(5);
862 BOOST_CHECK_EQUAL(find(), 2);
863
864 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
865 .setChildSelector(1)
866 .setMinSuffixComponents(6);
867 BOOST_CHECK_EQUAL(find(), 1);
868
869 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
870 .setChildSelector(1)
871 .setMinSuffixComponents(7);
872 BOOST_CHECK_EQUAL(find(), 0);
873}
874
875BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
876{
877 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
878 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
879 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
880 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
881 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
882 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
883 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
884
885 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
886 .setMaxSuffixComponents(0);
887 BOOST_CHECK_EQUAL(find(), 0);
888
889 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
890 .setMaxSuffixComponents(1);
891 BOOST_CHECK_EQUAL(find(), 1);
892
893 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
894 .setMaxSuffixComponents(2);
895 BOOST_CHECK_EQUAL(find(), 2);
896
897 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
898 .setMaxSuffixComponents(3);
899 BOOST_CHECK_EQUAL(find(), 3);
900
901 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
902 .setMaxSuffixComponents(4);
903 BOOST_CHECK_EQUAL(find(), 4);
904
905 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
906 .setMaxSuffixComponents(5);
907 BOOST_CHECK_EQUAL(find(), 5);
908
909 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
910 .setMaxSuffixComponents(6);
911 BOOST_CHECK_EQUAL(find(), 6);
912}
913
914BOOST_AUTO_TEST_SUITE_END() // Find
915
916BOOST_AUTO_TEST_SUITE_END() // TableCs
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800917
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700918} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800919} // namespace nfd