blob: a8363de1e59952ef8fc1801896f655e26b3f5629 [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shibb6146e2017-07-26 23:07:52 +00002/*
Junxiao Shi3d2049f2018-01-26 23:46:06 +00003 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shifc206962015-01-16 11:12:22 -07004 * 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,
9 * The University of Memphis.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shia9388182014-12-13 23:16:09 -070024 */
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070025
26#include "table/cs.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080027
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080029
Junxiao Shi4e1b6ee2017-08-03 02:20:50 +000030#include <cstring>
Davide Pesavento87fc0f82018-04-11 23:43:51 -040031
32#include <ndn-cxx/exclude.hpp>
Junxiao Shi4e1b6ee2017-08-03 02:20:50 +000033#include <ndn-cxx/lp/tags.hpp>
34#include <ndn-cxx/util/sha256.hpp>
35
mzhang4eab72492015-02-25 11:16:09 -060036#define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); });
37
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080038namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080039namespace cs {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070040namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070041
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080042using namespace nfd::tests;
43
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070044BOOST_AUTO_TEST_SUITE(Table)
45BOOST_FIXTURE_TEST_SUITE(TestCs, BaseFixture)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070046
Junxiao Shi1fbdb542017-02-03 00:05:53 +000047class FindFixture : public UnitTestTimeFixture
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080048{
49protected:
Junxiao Shia9388182014-12-13 23:16:09 -070050 Name
Junxiao Shi1fbdb542017-02-03 00:05:53 +000051 insert(uint32_t id, const Name& name, const std::function<void(Data&)>& modifyData = nullptr)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080052 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040053 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080054 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070055
Junxiao Shi1fbdb542017-02-03 00:05:53 +000056 if (modifyData != nullptr) {
57 modifyData(*data);
58 }
59
60 data->wireEncode();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080061 m_cs.insert(*data);
Junxiao Shia9388182014-12-13 23:16:09 -070062
63 return data->getFullName();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080064 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070065
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080066 Interest&
67 startInterest(const Name& name)
68 {
69 m_interest = make_shared<Interest>(name);
70 return *m_interest;
71 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070072
mzhang4eab72492015-02-25 11:16:09 -060073 void
74 find(const std::function<void(uint32_t)>& check)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080075 {
Junxiao Shi1fbdb542017-02-03 00:05:53 +000076 bool hasResult = false;
mzhang4eab72492015-02-25 11:16:09 -060077 m_cs.find(*m_interest,
78 [&] (const Interest& interest, const Data& data) {
Junxiao Shi1fbdb542017-02-03 00:05:53 +000079 hasResult = true;
80 const Block& content = data.getContent();
Junxiao Shi4e1b6ee2017-08-03 02:20:50 +000081 uint32_t found = 0;
82 std::memcpy(&found, content.value(), sizeof(found));
Junxiao Shi1fbdb542017-02-03 00:05:53 +000083 check(found);
84 },
85 bind([&] {
86 hasResult = true;
87 check(0);
88 }));
89
Junxiao Shi30c37ab2018-04-09 14:26:47 +000090 // current Cs::find implementation is synchronous
Junxiao Shi1fbdb542017-02-03 00:05:53 +000091 BOOST_CHECK(hasResult);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080092 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070093
Junxiao Shi30c37ab2018-04-09 14:26:47 +000094 size_t
95 erase(const Name& prefix, size_t limit)
96 {
Davide Pesavento87fc0f82018-04-11 23:43:51 -040097 optional<size_t> nErased;
Junxiao Shi30c37ab2018-04-09 14:26:47 +000098 m_cs.erase(prefix, limit, [&] (size_t nErased1) { nErased = nErased1; });
99
100 // current Cs::erase implementation is synchronous
101 // if callback was not invoked, bad_optional_access would occur
102 return *nErased;
103 }
104
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800105protected:
106 Cs m_cs;
107 shared_ptr<Interest> m_interest;
108};
109
110BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
111
112BOOST_AUTO_TEST_CASE(EmptyDataName)
113{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000114 insert(1, "/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700115
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000116 startInterest("/");
mzhang4eab72492015-02-25 11:16:09 -0600117 CHECK_CS_FIND(1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800118}
119
120BOOST_AUTO_TEST_CASE(EmptyInterestName)
121{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000122 insert(1, "/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700123
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000124 startInterest("/");
mzhang4eab72492015-02-25 11:16:09 -0600125 CHECK_CS_FIND(1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800126}
127
Junxiao Shia9388182014-12-13 23:16:09 -0700128BOOST_AUTO_TEST_CASE(ExactName)
129{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000130 insert(1, "/");
131 insert(2, "/A");
132 insert(3, "/A/B");
133 insert(4, "/A/C");
134 insert(5, "/D");
Junxiao Shia9388182014-12-13 23:16:09 -0700135
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000136 startInterest("/A");
mzhang4eab72492015-02-25 11:16:09 -0600137 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700138}
139
140BOOST_AUTO_TEST_CASE(FullName)
141{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000142 Name n1 = insert(1, "/A");
143 Name n2 = insert(2, "/A");
Junxiao Shia9388182014-12-13 23:16:09 -0700144
145 startInterest(n1);
mzhang4eab72492015-02-25 11:16:09 -0600146 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -0700147
148 startInterest(n2);
mzhang4eab72492015-02-25 11:16:09 -0600149 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700150}
151
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800152BOOST_AUTO_TEST_CASE(Leftmost)
153{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000154 insert(1, "/A");
155 insert(2, "/B/p/1");
156 insert(3, "/B/p/2");
157 insert(4, "/B/q/1");
158 insert(5, "/B/q/2");
159 insert(6, "/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700160
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000161 startInterest("/B");
mzhang4eab72492015-02-25 11:16:09 -0600162 CHECK_CS_FIND(2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800163}
164
165BOOST_AUTO_TEST_CASE(Rightmost)
166{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000167 insert(1, "/A");
168 insert(2, "/B/p/1");
169 insert(3, "/B/p/2");
170 insert(4, "/B/q/1");
171 insert(5, "/B/q/2");
172 insert(6, "/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700173
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000174 startInterest("/B")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800175 .setChildSelector(1);
mzhang4eab72492015-02-25 11:16:09 -0600176 CHECK_CS_FIND(4);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800177}
178
Junxiao Shia9388182014-12-13 23:16:09 -0700179BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800180{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000181 insert(1, "/");
182 insert(2, "/A");
183 insert(3, "/B/1");
184 insert(4, "/C/1/2");
185 insert(5, "/D/1/2/3");
186 insert(6, "/E/1/2/3/4");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700187
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000188 startInterest("/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800189 .setMinSuffixComponents(0);
mzhang4eab72492015-02-25 11:16:09 -0600190 CHECK_CS_FIND(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700191
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000192 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700193 .setMinSuffixComponents(1);
mzhang4eab72492015-02-25 11:16:09 -0600194 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -0700195
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000196 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700197 .setMinSuffixComponents(2);
mzhang4eab72492015-02-25 11:16:09 -0600198 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700199
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000200 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700201 .setMinSuffixComponents(3);
mzhang4eab72492015-02-25 11:16:09 -0600202 CHECK_CS_FIND(3);
Junxiao Shia9388182014-12-13 23:16:09 -0700203
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000204 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700205 .setMinSuffixComponents(4);
mzhang4eab72492015-02-25 11:16:09 -0600206 CHECK_CS_FIND(4);
Junxiao Shia9388182014-12-13 23:16:09 -0700207
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000208 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700209 .setMinSuffixComponents(5);
mzhang4eab72492015-02-25 11:16:09 -0600210 CHECK_CS_FIND(5);
Junxiao Shia9388182014-12-13 23:16:09 -0700211
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000212 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700213 .setMinSuffixComponents(6);
mzhang4eab72492015-02-25 11:16:09 -0600214 CHECK_CS_FIND(6);
Junxiao Shia9388182014-12-13 23:16:09 -0700215
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000216 startInterest("/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800217 .setMinSuffixComponents(7);
mzhang4eab72492015-02-25 11:16:09 -0600218 CHECK_CS_FIND(0);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800219}
220
221BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
222{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000223 insert(1, "/");
224 insert(2, "/A");
225 insert(3, "/B/2");
226 insert(4, "/C/2/3");
227 insert(5, "/D/2/3/4");
228 insert(6, "/E/2/3/4/5");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700229
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000230 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700231 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800232 .setMaxSuffixComponents(0);
mzhang4eab72492015-02-25 11:16:09 -0600233 CHECK_CS_FIND(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700234
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000235 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700236 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800237 .setMaxSuffixComponents(1);
mzhang4eab72492015-02-25 11:16:09 -0600238 CHECK_CS_FIND(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700239
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000240 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700241 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800242 .setMaxSuffixComponents(2);
mzhang4eab72492015-02-25 11:16:09 -0600243 CHECK_CS_FIND(2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700244
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000245 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700246 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800247 .setMaxSuffixComponents(3);
mzhang4eab72492015-02-25 11:16:09 -0600248 CHECK_CS_FIND(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700249
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000250 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700251 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800252 .setMaxSuffixComponents(4);
mzhang4eab72492015-02-25 11:16:09 -0600253 CHECK_CS_FIND(4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700254
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000255 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700256 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800257 .setMaxSuffixComponents(5);
mzhang4eab72492015-02-25 11:16:09 -0600258 CHECK_CS_FIND(5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700259
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000260 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700261 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800262 .setMaxSuffixComponents(6);
mzhang4eab72492015-02-25 11:16:09 -0600263 CHECK_CS_FIND(6);
Junxiao Shia9388182014-12-13 23:16:09 -0700264
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000265 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700266 .setChildSelector(1)
267 .setMaxSuffixComponents(7);
mzhang4eab72492015-02-25 11:16:09 -0600268 CHECK_CS_FIND(6);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800269}
270
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000271BOOST_AUTO_TEST_CASE(MustBeFresh)
272{
Eric Newberryf4056d02017-05-26 17:31:53 +0000273 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000274 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(time::seconds(0)); });
275 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(time::seconds(1)); });
276 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(time::seconds(3600)); });
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000277
278 // lookup at exact same moment as insertion is not tested because this won't happen in reality
279
280 this->advanceClocks(time::milliseconds(500)); // @500ms
281 startInterest("/A")
282 .setMustBeFresh(true);
283 CHECK_CS_FIND(3);
284
285 this->advanceClocks(time::milliseconds(1500)); // @2s
286 startInterest("/A")
287 .setMustBeFresh(true);
288 CHECK_CS_FIND(4);
289
290 this->advanceClocks(time::seconds(3500)); // @3502s
291 startInterest("/A")
292 .setMustBeFresh(true);
293 CHECK_CS_FIND(4);
294
295 this->advanceClocks(time::seconds(3500)); // @7002s
296 startInterest("/A")
297 .setMustBeFresh(true);
Eric Newberryf4056d02017-05-26 17:31:53 +0000298 CHECK_CS_FIND(0);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000299}
300
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800301BOOST_AUTO_TEST_CASE(DigestOrder)
302{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000303 Name n1 = insert(1, "/A");
304 Name n2 = insert(2, "/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700305
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000306 uint32_t expectedLeftmost = 0, expectedRightmost = 0;
307 if (n1 < n2) {
308 expectedLeftmost = 1;
309 expectedRightmost = 2;
310 }
311 else {
312 BOOST_CHECK_MESSAGE(n1 != n2, "implicit digest collision detected");
313 expectedLeftmost = 2;
314 expectedRightmost = 1;
315 }
316
317 startInterest("/A")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800318 .setChildSelector(0);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000319 CHECK_CS_FIND(expectedLeftmost);
320 startInterest("/A")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800321 .setChildSelector(1);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000322 CHECK_CS_FIND(expectedRightmost);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800323}
324
325BOOST_AUTO_TEST_CASE(DigestExclude)
326{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000327 insert(1, "/A");
328 Name n2 = insert(2, "/A");
329 insert(3, "/A/B");
Junxiao Shia9388182014-12-13 23:16:09 -0700330
Junxiao Shibb6146e2017-07-26 23:07:52 +0000331 uint8_t digest00[ndn::util::Sha256::DIGEST_SIZE];
Junxiao Shia9388182014-12-13 23:16:09 -0700332 std::fill_n(digest00, sizeof(digest00), 0x00);
Junxiao Shibb6146e2017-07-26 23:07:52 +0000333 uint8_t digestFF[ndn::util::Sha256::DIGEST_SIZE];
Junxiao Shia9388182014-12-13 23:16:09 -0700334 std::fill_n(digestFF, sizeof(digestFF), 0xFF);
335
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400336 ndn::Exclude excludeDigest;
Junxiao Shia9388182014-12-13 23:16:09 -0700337 excludeDigest.excludeRange(
338 name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)),
339 name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF)));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700340
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000341 startInterest("/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700342 .setChildSelector(0)
343 .setExclude(excludeDigest);
mzhang4eab72492015-02-25 11:16:09 -0600344 CHECK_CS_FIND(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700345
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000346 startInterest("/A")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800347 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700348 .setExclude(excludeDigest);
mzhang4eab72492015-02-25 11:16:09 -0600349 CHECK_CS_FIND(3);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400350
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400351 ndn::Exclude excludeGeneric;
Junxiao Shia9388182014-12-13 23:16:09 -0700352 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400353
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000354 startInterest("/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700355 .setChildSelector(0)
356 .setExclude(excludeGeneric);
mzhang4eab72492015-02-25 11:16:09 -0600357 find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); });
Junxiao Shia9388182014-12-13 23:16:09 -0700358
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000359 startInterest("/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400360 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700361 .setExclude(excludeGeneric);
mzhang4eab72492015-02-25 11:16:09 -0600362 find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); });
Junxiao Shia9388182014-12-13 23:16:09 -0700363
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400364 ndn::Exclude exclude2 = excludeGeneric;
Junxiao Shia9388182014-12-13 23:16:09 -0700365 exclude2.excludeOne(n2.get(-1));
366
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000367 startInterest("/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700368 .setChildSelector(0)
369 .setExclude(exclude2);
mzhang4eab72492015-02-25 11:16:09 -0600370 CHECK_CS_FIND(1);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400371
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000372 startInterest("/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400373 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700374 .setExclude(exclude2);
mzhang4eab72492015-02-25 11:16:09 -0600375 CHECK_CS_FIND(1);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400376}
377
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700378BOOST_AUTO_TEST_SUITE_END() // Find
379
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000380BOOST_FIXTURE_TEST_CASE(Erase, FindFixture)
381{
382 insert(1, "/A/B/1");
383 insert(2, "/A/B/2");
384 insert(3, "/A/C/3");
385 insert(4, "/A/C/4");
386 insert(5, "/D/5");
387 insert(6, "/E/6");
388 BOOST_CHECK_EQUAL(m_cs.size(), 6);
389
390 BOOST_CHECK_EQUAL(erase("/A", 3), 3);
391 BOOST_CHECK_EQUAL(m_cs.size(), 3);
392 int nDataUnderA = 0;
393 startInterest("/A/B/1");
394 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
395 startInterest("/A/B/2");
396 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
397 startInterest("/A/C/3");
398 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
399 startInterest("/A/C/4");
400 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
401 BOOST_CHECK_EQUAL(nDataUnderA, 1);
402
403 BOOST_CHECK_EQUAL(erase("/D", 2), 1);
404 BOOST_CHECK_EQUAL(m_cs.size(), 2);
405 startInterest("/D/5");
406 CHECK_CS_FIND(0);
407
408 BOOST_CHECK_EQUAL(erase("/F", 2), 0);
409 BOOST_CHECK_EQUAL(m_cs.size(), 2);
410}
411
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700412// When the capacity limit is set to zero, Data cannot be inserted;
413// this test case covers this situation.
414// The behavior of non-zero capacity limit depends on the eviction policy,
415// and is tested in policy test suites.
416BOOST_FIXTURE_TEST_CASE(ZeroCapacity, FindFixture)
417{
418 m_cs.setLimit(0);
419
420 BOOST_CHECK_EQUAL(m_cs.getLimit(), 0);
421 BOOST_CHECK_EQUAL(m_cs.size(), 0);
422 BOOST_CHECK(m_cs.begin() == m_cs.end());
423
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000424 insert(1, "/A");
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700425 BOOST_CHECK_EQUAL(m_cs.size(), 0);
426
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000427 startInterest("/A");
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700428 CHECK_CS_FIND(0);
429}
Junxiao Shia9388182014-12-13 23:16:09 -0700430
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000431BOOST_FIXTURE_TEST_CASE(EnablementFlags, FindFixture)
432{
433 BOOST_CHECK_EQUAL(m_cs.shouldAdmit(), true);
434 BOOST_CHECK_EQUAL(m_cs.shouldServe(), true);
435
436 insert(1, "/A");
437 m_cs.enableAdmit(false);
438 insert(2, "/B");
439 m_cs.enableAdmit(true);
440 insert(3, "/C");
441
442 startInterest("/A");
443 CHECK_CS_FIND(1);
444 startInterest("/B");
445 CHECK_CS_FIND(0);
446 startInterest("/C");
447 CHECK_CS_FIND(3);
448
449 m_cs.enableServe(false);
450 startInterest("/A");
451 CHECK_CS_FIND(0);
452 startInterest("/C");
453 CHECK_CS_FIND(0);
454
455 m_cs.enableServe(true);
456 startInterest("/A");
457 CHECK_CS_FIND(1);
458 startInterest("/C");
459 CHECK_CS_FIND(3);
460}
461
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000462BOOST_FIXTURE_TEST_CASE(CachePolicyNoCache, FindFixture)
Junxiao Shi35b16b12015-02-16 22:14:57 -0700463{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000464 insert(1, "/A", [] (Data& data) {
465 data.setTag(make_shared<lp::CachePolicyTag>(
466 lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE)));
467 });
Junxiao Shi35b16b12015-02-16 22:14:57 -0700468
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000469 startInterest("/A");
470 CHECK_CS_FIND(0);
Junxiao Shi35b16b12015-02-16 22:14:57 -0700471}
472
Junxiao Shifc206962015-01-16 11:12:22 -0700473BOOST_AUTO_TEST_CASE(Enumeration)
474{
475 Cs cs;
476
477 Name nameA("/A");
478 Name nameAB("/A/B");
479 Name nameABC("/A/B/C");
480 Name nameD("/D");
481
482 BOOST_CHECK_EQUAL(cs.size(), 0);
483 BOOST_CHECK(cs.begin() == cs.end());
484
485 cs.insert(*makeData(nameABC));
486 BOOST_CHECK_EQUAL(cs.size(), 1);
487 BOOST_CHECK(cs.begin() != cs.end());
488 BOOST_CHECK(cs.begin()->getName() == nameABC);
489 BOOST_CHECK((*cs.begin()).getName() == nameABC);
490
491 auto i = cs.begin();
492 auto j = cs.begin();
493 BOOST_CHECK(++i == cs.end());
494 BOOST_CHECK(j++ == cs.begin());
495 BOOST_CHECK(j == cs.end());
496
497 cs.insert(*makeData(nameA));
498 cs.insert(*makeData(nameAB));
499 cs.insert(*makeData(nameD));
500
501 std::set<Name> expected = {nameA, nameAB, nameABC, nameD};
502 std::set<Name> actual;
503 for (const auto& csEntry : cs) {
504 actual.insert(csEntry.getName());
505 }
506 BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
507}
508
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700509BOOST_AUTO_TEST_SUITE_END() // TestCs
510BOOST_AUTO_TEST_SUITE_END() // Table
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800511
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700512} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800513} // namespace cs
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800514} // namespace nfd