blob: 66b7e95ecf4f8b22dc1c2541bc7ea031627c4a92 [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>
31#include <ndn-cxx/lp/tags.hpp>
32#include <ndn-cxx/util/sha256.hpp>
33
mzhang4eab72492015-02-25 11:16:09 -060034#define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); });
35
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080037namespace cs {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070038namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070039
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080040using namespace nfd::tests;
41
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -070042BOOST_AUTO_TEST_SUITE(Table)
43BOOST_FIXTURE_TEST_SUITE(TestCs, BaseFixture)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070044
Junxiao Shi1fbdb542017-02-03 00:05:53 +000045class FindFixture : public UnitTestTimeFixture
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080046{
47protected:
Junxiao Shia9388182014-12-13 23:16:09 -070048 Name
Junxiao Shi1fbdb542017-02-03 00:05:53 +000049 insert(uint32_t id, const Name& name, const std::function<void(Data&)>& modifyData = nullptr)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080050 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040051 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080052 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070053
Junxiao Shi1fbdb542017-02-03 00:05:53 +000054 if (modifyData != nullptr) {
55 modifyData(*data);
56 }
57
58 data->wireEncode();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080059 m_cs.insert(*data);
Junxiao Shia9388182014-12-13 23:16:09 -070060
61 return data->getFullName();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080062 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070063
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080064 Interest&
65 startInterest(const Name& name)
66 {
67 m_interest = make_shared<Interest>(name);
68 return *m_interest;
69 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070070
mzhang4eab72492015-02-25 11:16:09 -060071 void
72 find(const std::function<void(uint32_t)>& check)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080073 {
Junxiao Shi1fbdb542017-02-03 00:05:53 +000074 bool hasResult = false;
mzhang4eab72492015-02-25 11:16:09 -060075 m_cs.find(*m_interest,
76 [&] (const Interest& interest, const Data& data) {
Junxiao Shi1fbdb542017-02-03 00:05:53 +000077 hasResult = true;
78 const Block& content = data.getContent();
Junxiao Shi4e1b6ee2017-08-03 02:20:50 +000079 uint32_t found = 0;
80 std::memcpy(&found, content.value(), sizeof(found));
Junxiao Shi1fbdb542017-02-03 00:05:53 +000081 check(found);
82 },
83 bind([&] {
84 hasResult = true;
85 check(0);
86 }));
87
Junxiao Shi30c37ab2018-04-09 14:26:47 +000088 // current Cs::find implementation is synchronous
Junxiao Shi1fbdb542017-02-03 00:05:53 +000089 BOOST_CHECK(hasResult);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080090 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070091
Junxiao Shi30c37ab2018-04-09 14:26:47 +000092 size_t
93 erase(const Name& prefix, size_t limit)
94 {
95 ndn::optional<size_t> nErased;
96 m_cs.erase(prefix, limit, [&] (size_t nErased1) { nErased = nErased1; });
97
98 // current Cs::erase implementation is synchronous
99 // if callback was not invoked, bad_optional_access would occur
100 return *nErased;
101 }
102
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800103protected:
104 Cs m_cs;
105 shared_ptr<Interest> m_interest;
106};
107
108BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
109
110BOOST_AUTO_TEST_CASE(EmptyDataName)
111{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000112 insert(1, "/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700113
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000114 startInterest("/");
mzhang4eab72492015-02-25 11:16:09 -0600115 CHECK_CS_FIND(1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800116}
117
118BOOST_AUTO_TEST_CASE(EmptyInterestName)
119{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000120 insert(1, "/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700121
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000122 startInterest("/");
mzhang4eab72492015-02-25 11:16:09 -0600123 CHECK_CS_FIND(1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800124}
125
Junxiao Shia9388182014-12-13 23:16:09 -0700126BOOST_AUTO_TEST_CASE(ExactName)
127{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000128 insert(1, "/");
129 insert(2, "/A");
130 insert(3, "/A/B");
131 insert(4, "/A/C");
132 insert(5, "/D");
Junxiao Shia9388182014-12-13 23:16:09 -0700133
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000134 startInterest("/A");
mzhang4eab72492015-02-25 11:16:09 -0600135 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700136}
137
138BOOST_AUTO_TEST_CASE(FullName)
139{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000140 Name n1 = insert(1, "/A");
141 Name n2 = insert(2, "/A");
Junxiao Shia9388182014-12-13 23:16:09 -0700142
143 startInterest(n1);
mzhang4eab72492015-02-25 11:16:09 -0600144 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -0700145
146 startInterest(n2);
mzhang4eab72492015-02-25 11:16:09 -0600147 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700148}
149
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800150BOOST_AUTO_TEST_CASE(Leftmost)
151{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000152 insert(1, "/A");
153 insert(2, "/B/p/1");
154 insert(3, "/B/p/2");
155 insert(4, "/B/q/1");
156 insert(5, "/B/q/2");
157 insert(6, "/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700158
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000159 startInterest("/B");
mzhang4eab72492015-02-25 11:16:09 -0600160 CHECK_CS_FIND(2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800161}
162
163BOOST_AUTO_TEST_CASE(Rightmost)
164{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000165 insert(1, "/A");
166 insert(2, "/B/p/1");
167 insert(3, "/B/p/2");
168 insert(4, "/B/q/1");
169 insert(5, "/B/q/2");
170 insert(6, "/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700171
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000172 startInterest("/B")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800173 .setChildSelector(1);
mzhang4eab72492015-02-25 11:16:09 -0600174 CHECK_CS_FIND(4);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800175}
176
Junxiao Shia9388182014-12-13 23:16:09 -0700177BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800178{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000179 insert(1, "/");
180 insert(2, "/A");
181 insert(3, "/B/1");
182 insert(4, "/C/1/2");
183 insert(5, "/D/1/2/3");
184 insert(6, "/E/1/2/3/4");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700185
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000186 startInterest("/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800187 .setMinSuffixComponents(0);
mzhang4eab72492015-02-25 11:16:09 -0600188 CHECK_CS_FIND(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700189
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000190 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700191 .setMinSuffixComponents(1);
mzhang4eab72492015-02-25 11:16:09 -0600192 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -0700193
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000194 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700195 .setMinSuffixComponents(2);
mzhang4eab72492015-02-25 11:16:09 -0600196 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700197
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000198 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700199 .setMinSuffixComponents(3);
mzhang4eab72492015-02-25 11:16:09 -0600200 CHECK_CS_FIND(3);
Junxiao Shia9388182014-12-13 23:16:09 -0700201
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000202 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700203 .setMinSuffixComponents(4);
mzhang4eab72492015-02-25 11:16:09 -0600204 CHECK_CS_FIND(4);
Junxiao Shia9388182014-12-13 23:16:09 -0700205
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000206 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700207 .setMinSuffixComponents(5);
mzhang4eab72492015-02-25 11:16:09 -0600208 CHECK_CS_FIND(5);
Junxiao Shia9388182014-12-13 23:16:09 -0700209
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000210 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700211 .setMinSuffixComponents(6);
mzhang4eab72492015-02-25 11:16:09 -0600212 CHECK_CS_FIND(6);
Junxiao Shia9388182014-12-13 23:16:09 -0700213
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000214 startInterest("/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800215 .setMinSuffixComponents(7);
mzhang4eab72492015-02-25 11:16:09 -0600216 CHECK_CS_FIND(0);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800217}
218
219BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
220{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000221 insert(1, "/");
222 insert(2, "/A");
223 insert(3, "/B/2");
224 insert(4, "/C/2/3");
225 insert(5, "/D/2/3/4");
226 insert(6, "/E/2/3/4/5");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700227
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000228 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700229 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800230 .setMaxSuffixComponents(0);
mzhang4eab72492015-02-25 11:16:09 -0600231 CHECK_CS_FIND(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700232
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000233 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700234 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800235 .setMaxSuffixComponents(1);
mzhang4eab72492015-02-25 11:16:09 -0600236 CHECK_CS_FIND(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700237
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000238 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700239 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800240 .setMaxSuffixComponents(2);
mzhang4eab72492015-02-25 11:16:09 -0600241 CHECK_CS_FIND(2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700242
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000243 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700244 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800245 .setMaxSuffixComponents(3);
mzhang4eab72492015-02-25 11:16:09 -0600246 CHECK_CS_FIND(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700247
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000248 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700249 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800250 .setMaxSuffixComponents(4);
mzhang4eab72492015-02-25 11:16:09 -0600251 CHECK_CS_FIND(4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700252
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000253 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700254 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800255 .setMaxSuffixComponents(5);
mzhang4eab72492015-02-25 11:16:09 -0600256 CHECK_CS_FIND(5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700257
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000258 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700259 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800260 .setMaxSuffixComponents(6);
mzhang4eab72492015-02-25 11:16:09 -0600261 CHECK_CS_FIND(6);
Junxiao Shia9388182014-12-13 23:16:09 -0700262
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000263 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700264 .setChildSelector(1)
265 .setMaxSuffixComponents(7);
mzhang4eab72492015-02-25 11:16:09 -0600266 CHECK_CS_FIND(6);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800267}
268
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000269BOOST_AUTO_TEST_CASE(MustBeFresh)
270{
Eric Newberryf4056d02017-05-26 17:31:53 +0000271 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000272 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(time::seconds(0)); });
273 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(time::seconds(1)); });
274 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(time::seconds(3600)); });
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000275
276 // lookup at exact same moment as insertion is not tested because this won't happen in reality
277
278 this->advanceClocks(time::milliseconds(500)); // @500ms
279 startInterest("/A")
280 .setMustBeFresh(true);
281 CHECK_CS_FIND(3);
282
283 this->advanceClocks(time::milliseconds(1500)); // @2s
284 startInterest("/A")
285 .setMustBeFresh(true);
286 CHECK_CS_FIND(4);
287
288 this->advanceClocks(time::seconds(3500)); // @3502s
289 startInterest("/A")
290 .setMustBeFresh(true);
291 CHECK_CS_FIND(4);
292
293 this->advanceClocks(time::seconds(3500)); // @7002s
294 startInterest("/A")
295 .setMustBeFresh(true);
Eric Newberryf4056d02017-05-26 17:31:53 +0000296 CHECK_CS_FIND(0);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000297}
298
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800299BOOST_AUTO_TEST_CASE(DigestOrder)
300{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000301 Name n1 = insert(1, "/A");
302 Name n2 = insert(2, "/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700303
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000304 uint32_t expectedLeftmost = 0, expectedRightmost = 0;
305 if (n1 < n2) {
306 expectedLeftmost = 1;
307 expectedRightmost = 2;
308 }
309 else {
310 BOOST_CHECK_MESSAGE(n1 != n2, "implicit digest collision detected");
311 expectedLeftmost = 2;
312 expectedRightmost = 1;
313 }
314
315 startInterest("/A")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800316 .setChildSelector(0);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000317 CHECK_CS_FIND(expectedLeftmost);
318 startInterest("/A")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800319 .setChildSelector(1);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000320 CHECK_CS_FIND(expectedRightmost);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800321}
322
323BOOST_AUTO_TEST_CASE(DigestExclude)
324{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000325 insert(1, "/A");
326 Name n2 = insert(2, "/A");
327 insert(3, "/A/B");
Junxiao Shia9388182014-12-13 23:16:09 -0700328
Junxiao Shibb6146e2017-07-26 23:07:52 +0000329 uint8_t digest00[ndn::util::Sha256::DIGEST_SIZE];
Junxiao Shia9388182014-12-13 23:16:09 -0700330 std::fill_n(digest00, sizeof(digest00), 0x00);
Junxiao Shibb6146e2017-07-26 23:07:52 +0000331 uint8_t digestFF[ndn::util::Sha256::DIGEST_SIZE];
Junxiao Shia9388182014-12-13 23:16:09 -0700332 std::fill_n(digestFF, sizeof(digestFF), 0xFF);
333
334 Exclude excludeDigest;
335 excludeDigest.excludeRange(
336 name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)),
337 name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF)));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700338
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000339 startInterest("/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700340 .setChildSelector(0)
341 .setExclude(excludeDigest);
mzhang4eab72492015-02-25 11:16:09 -0600342 CHECK_CS_FIND(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700343
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000344 startInterest("/A")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800345 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700346 .setExclude(excludeDigest);
mzhang4eab72492015-02-25 11:16:09 -0600347 CHECK_CS_FIND(3);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400348
Junxiao Shia9388182014-12-13 23:16:09 -0700349 Exclude excludeGeneric;
350 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400351
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000352 startInterest("/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700353 .setChildSelector(0)
354 .setExclude(excludeGeneric);
mzhang4eab72492015-02-25 11:16:09 -0600355 find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); });
Junxiao Shia9388182014-12-13 23:16:09 -0700356
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000357 startInterest("/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400358 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700359 .setExclude(excludeGeneric);
mzhang4eab72492015-02-25 11:16:09 -0600360 find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); });
Junxiao Shia9388182014-12-13 23:16:09 -0700361
362 Exclude exclude2 = excludeGeneric;
363 exclude2.excludeOne(n2.get(-1));
364
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000365 startInterest("/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700366 .setChildSelector(0)
367 .setExclude(exclude2);
mzhang4eab72492015-02-25 11:16:09 -0600368 CHECK_CS_FIND(1);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400369
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000370 startInterest("/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400371 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700372 .setExclude(exclude2);
mzhang4eab72492015-02-25 11:16:09 -0600373 CHECK_CS_FIND(1);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400374}
375
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700376BOOST_AUTO_TEST_SUITE_END() // Find
377
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000378BOOST_FIXTURE_TEST_CASE(Erase, FindFixture)
379{
380 insert(1, "/A/B/1");
381 insert(2, "/A/B/2");
382 insert(3, "/A/C/3");
383 insert(4, "/A/C/4");
384 insert(5, "/D/5");
385 insert(6, "/E/6");
386 BOOST_CHECK_EQUAL(m_cs.size(), 6);
387
388 BOOST_CHECK_EQUAL(erase("/A", 3), 3);
389 BOOST_CHECK_EQUAL(m_cs.size(), 3);
390 int nDataUnderA = 0;
391 startInterest("/A/B/1");
392 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
393 startInterest("/A/B/2");
394 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
395 startInterest("/A/C/3");
396 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
397 startInterest("/A/C/4");
398 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
399 BOOST_CHECK_EQUAL(nDataUnderA, 1);
400
401 BOOST_CHECK_EQUAL(erase("/D", 2), 1);
402 BOOST_CHECK_EQUAL(m_cs.size(), 2);
403 startInterest("/D/5");
404 CHECK_CS_FIND(0);
405
406 BOOST_CHECK_EQUAL(erase("/F", 2), 0);
407 BOOST_CHECK_EQUAL(m_cs.size(), 2);
408}
409
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700410// When the capacity limit is set to zero, Data cannot be inserted;
411// this test case covers this situation.
412// The behavior of non-zero capacity limit depends on the eviction policy,
413// and is tested in policy test suites.
414BOOST_FIXTURE_TEST_CASE(ZeroCapacity, FindFixture)
415{
416 m_cs.setLimit(0);
417
418 BOOST_CHECK_EQUAL(m_cs.getLimit(), 0);
419 BOOST_CHECK_EQUAL(m_cs.size(), 0);
420 BOOST_CHECK(m_cs.begin() == m_cs.end());
421
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000422 insert(1, "/A");
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700423 BOOST_CHECK_EQUAL(m_cs.size(), 0);
424
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000425 startInterest("/A");
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700426 CHECK_CS_FIND(0);
427}
Junxiao Shia9388182014-12-13 23:16:09 -0700428
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000429BOOST_FIXTURE_TEST_CASE(EnablementFlags, FindFixture)
430{
431 BOOST_CHECK_EQUAL(m_cs.shouldAdmit(), true);
432 BOOST_CHECK_EQUAL(m_cs.shouldServe(), true);
433
434 insert(1, "/A");
435 m_cs.enableAdmit(false);
436 insert(2, "/B");
437 m_cs.enableAdmit(true);
438 insert(3, "/C");
439
440 startInterest("/A");
441 CHECK_CS_FIND(1);
442 startInterest("/B");
443 CHECK_CS_FIND(0);
444 startInterest("/C");
445 CHECK_CS_FIND(3);
446
447 m_cs.enableServe(false);
448 startInterest("/A");
449 CHECK_CS_FIND(0);
450 startInterest("/C");
451 CHECK_CS_FIND(0);
452
453 m_cs.enableServe(true);
454 startInterest("/A");
455 CHECK_CS_FIND(1);
456 startInterest("/C");
457 CHECK_CS_FIND(3);
458}
459
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000460BOOST_FIXTURE_TEST_CASE(CachePolicyNoCache, FindFixture)
Junxiao Shi35b16b12015-02-16 22:14:57 -0700461{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000462 insert(1, "/A", [] (Data& data) {
463 data.setTag(make_shared<lp::CachePolicyTag>(
464 lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE)));
465 });
Junxiao Shi35b16b12015-02-16 22:14:57 -0700466
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000467 startInterest("/A");
468 CHECK_CS_FIND(0);
Junxiao Shi35b16b12015-02-16 22:14:57 -0700469}
470
Junxiao Shifc206962015-01-16 11:12:22 -0700471BOOST_AUTO_TEST_CASE(Enumeration)
472{
473 Cs cs;
474
475 Name nameA("/A");
476 Name nameAB("/A/B");
477 Name nameABC("/A/B/C");
478 Name nameD("/D");
479
480 BOOST_CHECK_EQUAL(cs.size(), 0);
481 BOOST_CHECK(cs.begin() == cs.end());
482
483 cs.insert(*makeData(nameABC));
484 BOOST_CHECK_EQUAL(cs.size(), 1);
485 BOOST_CHECK(cs.begin() != cs.end());
486 BOOST_CHECK(cs.begin()->getName() == nameABC);
487 BOOST_CHECK((*cs.begin()).getName() == nameABC);
488
489 auto i = cs.begin();
490 auto j = cs.begin();
491 BOOST_CHECK(++i == cs.end());
492 BOOST_CHECK(j++ == cs.begin());
493 BOOST_CHECK(j == cs.end());
494
495 cs.insert(*makeData(nameA));
496 cs.insert(*makeData(nameAB));
497 cs.insert(*makeData(nameD));
498
499 std::set<Name> expected = {nameA, nameAB, nameABC, nameD};
500 std::set<Name> actual;
501 for (const auto& csEntry : cs) {
502 actual.insert(csEntry.getName());
503 }
504 BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
505}
506
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700507BOOST_AUTO_TEST_SUITE_END() // TestCs
508BOOST_AUTO_TEST_SUITE_END() // Table
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800509
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700510} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800511} // namespace cs
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800512} // namespace nfd