blob: db980d227194003669a89a36ca541708c0e85a90 [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/*
Davide Pesaventocf7db2f2019-03-24 23:17:28 -04003 * Copyright (c) 2014-2019, 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"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080030
Junxiao Shi4e1b6ee2017-08-03 02:20:50 +000031#include <cstring>
Davide Pesavento87fc0f82018-04-11 23:43:51 -040032
33#include <ndn-cxx/exclude.hpp>
Junxiao Shi4e1b6ee2017-08-03 02:20:50 +000034#include <ndn-cxx/lp/tags.hpp>
35#include <ndn-cxx/util/sha256.hpp>
36
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080037namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080038namespace cs {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070039namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070040
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080041using namespace nfd::tests;
42
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040043#define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); });
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070044
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040045BOOST_AUTO_TEST_SUITE(Table)
46BOOST_FIXTURE_TEST_SUITE(TestCs, GlobalIoFixture)
47
48class FindFixture : public GlobalIoTimeFixture
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080049{
50protected:
Junxiao Shia9388182014-12-13 23:16:09 -070051 Name
Junxiao Shi1fbdb542017-02-03 00:05:53 +000052 insert(uint32_t id, const Name& name, const std::function<void(Data&)>& modifyData = nullptr)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080053 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040054 shared_ptr<Data> data = makeData(name);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080055 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070056
Junxiao Shi1fbdb542017-02-03 00:05:53 +000057 if (modifyData != nullptr) {
58 modifyData(*data);
59 }
60
61 data->wireEncode();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080062 m_cs.insert(*data);
Junxiao Shia9388182014-12-13 23:16:09 -070063
64 return data->getFullName();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080065 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070066
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080067 Interest&
68 startInterest(const Name& name)
69 {
70 m_interest = make_shared<Interest>(name);
71 return *m_interest;
72 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070073
mzhang4eab72492015-02-25 11:16:09 -060074 void
75 find(const std::function<void(uint32_t)>& check)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080076 {
Junxiao Shi1fbdb542017-02-03 00:05:53 +000077 bool hasResult = false;
mzhang4eab72492015-02-25 11:16:09 -060078 m_cs.find(*m_interest,
79 [&] (const Interest& interest, const Data& data) {
Junxiao Shi1fbdb542017-02-03 00:05:53 +000080 hasResult = true;
81 const Block& content = data.getContent();
Junxiao Shi4e1b6ee2017-08-03 02:20:50 +000082 uint32_t found = 0;
83 std::memcpy(&found, content.value(), sizeof(found));
Junxiao Shi1fbdb542017-02-03 00:05:53 +000084 check(found);
85 },
86 bind([&] {
87 hasResult = true;
88 check(0);
89 }));
90
Junxiao Shi30c37ab2018-04-09 14:26:47 +000091 // current Cs::find implementation is synchronous
Junxiao Shi1fbdb542017-02-03 00:05:53 +000092 BOOST_CHECK(hasResult);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080093 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070094
Junxiao Shi30c37ab2018-04-09 14:26:47 +000095 size_t
96 erase(const Name& prefix, size_t limit)
97 {
Davide Pesavento87fc0f82018-04-11 23:43:51 -040098 optional<size_t> nErased;
Junxiao Shi30c37ab2018-04-09 14:26:47 +000099 m_cs.erase(prefix, limit, [&] (size_t nErased1) { nErased = nErased1; });
100
101 // current Cs::erase implementation is synchronous
102 // if callback was not invoked, bad_optional_access would occur
103 return *nErased;
104 }
105
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800106protected:
107 Cs m_cs;
108 shared_ptr<Interest> m_interest;
109};
110
111BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
112
113BOOST_AUTO_TEST_CASE(EmptyDataName)
114{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000115 insert(1, "/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700116
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000117 startInterest("/");
mzhang4eab72492015-02-25 11:16:09 -0600118 CHECK_CS_FIND(1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800119}
120
121BOOST_AUTO_TEST_CASE(EmptyInterestName)
122{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000123 insert(1, "/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700124
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000125 startInterest("/");
mzhang4eab72492015-02-25 11:16:09 -0600126 CHECK_CS_FIND(1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800127}
128
Junxiao Shia9388182014-12-13 23:16:09 -0700129BOOST_AUTO_TEST_CASE(ExactName)
130{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000131 insert(1, "/");
132 insert(2, "/A");
133 insert(3, "/A/B");
134 insert(4, "/A/C");
135 insert(5, "/D");
Junxiao Shia9388182014-12-13 23:16:09 -0700136
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000137 startInterest("/A");
mzhang4eab72492015-02-25 11:16:09 -0600138 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700139}
140
141BOOST_AUTO_TEST_CASE(FullName)
142{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000143 Name n1 = insert(1, "/A");
144 Name n2 = insert(2, "/A");
Junxiao Shia9388182014-12-13 23:16:09 -0700145
146 startInterest(n1);
mzhang4eab72492015-02-25 11:16:09 -0600147 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -0700148
149 startInterest(n2);
mzhang4eab72492015-02-25 11:16:09 -0600150 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700151}
152
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800153BOOST_AUTO_TEST_CASE(Leftmost)
154{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000155 insert(1, "/A");
156 insert(2, "/B/p/1");
157 insert(3, "/B/p/2");
158 insert(4, "/B/q/1");
159 insert(5, "/B/q/2");
160 insert(6, "/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700161
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000162 startInterest("/B");
mzhang4eab72492015-02-25 11:16:09 -0600163 CHECK_CS_FIND(2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800164}
165
166BOOST_AUTO_TEST_CASE(Rightmost)
167{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000168 insert(1, "/A");
169 insert(2, "/B/p/1");
170 insert(3, "/B/p/2");
171 insert(4, "/B/q/1");
172 insert(5, "/B/q/2");
173 insert(6, "/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700174
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000175 startInterest("/B")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800176 .setChildSelector(1);
mzhang4eab72492015-02-25 11:16:09 -0600177 CHECK_CS_FIND(4);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800178}
179
Junxiao Shia9388182014-12-13 23:16:09 -0700180BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800181{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000182 insert(1, "/");
183 insert(2, "/A");
184 insert(3, "/B/1");
185 insert(4, "/C/1/2");
186 insert(5, "/D/1/2/3");
187 insert(6, "/E/1/2/3/4");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700188
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000189 startInterest("/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800190 .setMinSuffixComponents(0);
mzhang4eab72492015-02-25 11:16:09 -0600191 CHECK_CS_FIND(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700192
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000193 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700194 .setMinSuffixComponents(1);
mzhang4eab72492015-02-25 11:16:09 -0600195 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -0700196
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000197 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700198 .setMinSuffixComponents(2);
mzhang4eab72492015-02-25 11:16:09 -0600199 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700200
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000201 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700202 .setMinSuffixComponents(3);
mzhang4eab72492015-02-25 11:16:09 -0600203 CHECK_CS_FIND(3);
Junxiao Shia9388182014-12-13 23:16:09 -0700204
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000205 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700206 .setMinSuffixComponents(4);
mzhang4eab72492015-02-25 11:16:09 -0600207 CHECK_CS_FIND(4);
Junxiao Shia9388182014-12-13 23:16:09 -0700208
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000209 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700210 .setMinSuffixComponents(5);
mzhang4eab72492015-02-25 11:16:09 -0600211 CHECK_CS_FIND(5);
Junxiao Shia9388182014-12-13 23:16:09 -0700212
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000213 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700214 .setMinSuffixComponents(6);
mzhang4eab72492015-02-25 11:16:09 -0600215 CHECK_CS_FIND(6);
Junxiao Shia9388182014-12-13 23:16:09 -0700216
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000217 startInterest("/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800218 .setMinSuffixComponents(7);
mzhang4eab72492015-02-25 11:16:09 -0600219 CHECK_CS_FIND(0);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800220}
221
222BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
223{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000224 insert(1, "/");
225 insert(2, "/A");
226 insert(3, "/B/2");
227 insert(4, "/C/2/3");
228 insert(5, "/D/2/3/4");
229 insert(6, "/E/2/3/4/5");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700230
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000231 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700232 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800233 .setMaxSuffixComponents(0);
mzhang4eab72492015-02-25 11:16:09 -0600234 CHECK_CS_FIND(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700235
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000236 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700237 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800238 .setMaxSuffixComponents(1);
mzhang4eab72492015-02-25 11:16:09 -0600239 CHECK_CS_FIND(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700240
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000241 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700242 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800243 .setMaxSuffixComponents(2);
mzhang4eab72492015-02-25 11:16:09 -0600244 CHECK_CS_FIND(2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700245
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000246 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700247 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800248 .setMaxSuffixComponents(3);
mzhang4eab72492015-02-25 11:16:09 -0600249 CHECK_CS_FIND(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700250
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000251 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700252 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800253 .setMaxSuffixComponents(4);
mzhang4eab72492015-02-25 11:16:09 -0600254 CHECK_CS_FIND(4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700255
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000256 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700257 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800258 .setMaxSuffixComponents(5);
mzhang4eab72492015-02-25 11:16:09 -0600259 CHECK_CS_FIND(5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700260
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000261 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700262 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800263 .setMaxSuffixComponents(6);
mzhang4eab72492015-02-25 11:16:09 -0600264 CHECK_CS_FIND(6);
Junxiao Shia9388182014-12-13 23:16:09 -0700265
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000266 startInterest("/")
Junxiao Shia9388182014-12-13 23:16:09 -0700267 .setChildSelector(1)
268 .setMaxSuffixComponents(7);
mzhang4eab72492015-02-25 11:16:09 -0600269 CHECK_CS_FIND(6);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800270}
271
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000272BOOST_AUTO_TEST_CASE(MustBeFresh)
273{
Eric Newberryf4056d02017-05-26 17:31:53 +0000274 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
Davide Pesavento14e71f02019-03-28 17:35:25 -0400275 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); });
276 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); });
277 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); });
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000278
279 // lookup at exact same moment as insertion is not tested because this won't happen in reality
280
Davide Pesavento14e71f02019-03-28 17:35:25 -0400281 this->advanceClocks(500_ms); // @500ms
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000282 startInterest("/A")
283 .setMustBeFresh(true);
284 CHECK_CS_FIND(3);
285
Davide Pesavento14e71f02019-03-28 17:35:25 -0400286 this->advanceClocks(1500_ms); // @2s
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000287 startInterest("/A")
288 .setMustBeFresh(true);
289 CHECK_CS_FIND(4);
290
Davide Pesavento14e71f02019-03-28 17:35:25 -0400291 this->advanceClocks(3500_s); // @3502s
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000292 startInterest("/A")
293 .setMustBeFresh(true);
294 CHECK_CS_FIND(4);
295
Davide Pesavento14e71f02019-03-28 17:35:25 -0400296 this->advanceClocks(3500_s); // @7002s
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000297 startInterest("/A")
298 .setMustBeFresh(true);
Eric Newberryf4056d02017-05-26 17:31:53 +0000299 CHECK_CS_FIND(0);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000300}
301
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800302BOOST_AUTO_TEST_CASE(DigestOrder)
303{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000304 Name n1 = insert(1, "/A");
305 Name n2 = insert(2, "/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700306
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000307 uint32_t expectedLeftmost = 0, expectedRightmost = 0;
308 if (n1 < n2) {
309 expectedLeftmost = 1;
310 expectedRightmost = 2;
311 }
312 else {
313 BOOST_CHECK_MESSAGE(n1 != n2, "implicit digest collision detected");
314 expectedLeftmost = 2;
315 expectedRightmost = 1;
316 }
317
318 startInterest("/A")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800319 .setChildSelector(0);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000320 CHECK_CS_FIND(expectedLeftmost);
321 startInterest("/A")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800322 .setChildSelector(1);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000323 CHECK_CS_FIND(expectedRightmost);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800324}
325
326BOOST_AUTO_TEST_CASE(DigestExclude)
327{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000328 insert(1, "/A");
329 Name n2 = insert(2, "/A");
330 insert(3, "/A/B");
Junxiao Shia9388182014-12-13 23:16:09 -0700331
Junxiao Shibb6146e2017-07-26 23:07:52 +0000332 uint8_t digest00[ndn::util::Sha256::DIGEST_SIZE];
Junxiao Shia9388182014-12-13 23:16:09 -0700333 std::fill_n(digest00, sizeof(digest00), 0x00);
Junxiao Shibb6146e2017-07-26 23:07:52 +0000334 uint8_t digestFF[ndn::util::Sha256::DIGEST_SIZE];
Junxiao Shia9388182014-12-13 23:16:09 -0700335 std::fill_n(digestFF, sizeof(digestFF), 0xFF);
336
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400337 ndn::Exclude excludeDigest;
Junxiao Shia9388182014-12-13 23:16:09 -0700338 excludeDigest.excludeRange(
339 name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)),
340 name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF)));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700341
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000342 startInterest("/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700343 .setChildSelector(0)
344 .setExclude(excludeDigest);
mzhang4eab72492015-02-25 11:16:09 -0600345 CHECK_CS_FIND(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700346
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000347 startInterest("/A")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800348 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700349 .setExclude(excludeDigest);
mzhang4eab72492015-02-25 11:16:09 -0600350 CHECK_CS_FIND(3);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400351
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400352 ndn::Exclude excludeGeneric;
Junxiao Shia9388182014-12-13 23:16:09 -0700353 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400354
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000355 startInterest("/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700356 .setChildSelector(0)
357 .setExclude(excludeGeneric);
mzhang4eab72492015-02-25 11:16:09 -0600358 find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); });
Junxiao Shia9388182014-12-13 23:16:09 -0700359
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000360 startInterest("/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400361 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700362 .setExclude(excludeGeneric);
mzhang4eab72492015-02-25 11:16:09 -0600363 find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); });
Junxiao Shia9388182014-12-13 23:16:09 -0700364
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400365 ndn::Exclude exclude2 = excludeGeneric;
Junxiao Shia9388182014-12-13 23:16:09 -0700366 exclude2.excludeOne(n2.get(-1));
367
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000368 startInterest("/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700369 .setChildSelector(0)
370 .setExclude(exclude2);
mzhang4eab72492015-02-25 11:16:09 -0600371 CHECK_CS_FIND(1);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400372
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000373 startInterest("/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400374 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700375 .setExclude(exclude2);
mzhang4eab72492015-02-25 11:16:09 -0600376 CHECK_CS_FIND(1);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400377}
378
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700379BOOST_AUTO_TEST_SUITE_END() // Find
380
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000381BOOST_FIXTURE_TEST_CASE(Erase, FindFixture)
382{
383 insert(1, "/A/B/1");
384 insert(2, "/A/B/2");
385 insert(3, "/A/C/3");
386 insert(4, "/A/C/4");
387 insert(5, "/D/5");
388 insert(6, "/E/6");
389 BOOST_CHECK_EQUAL(m_cs.size(), 6);
390
391 BOOST_CHECK_EQUAL(erase("/A", 3), 3);
392 BOOST_CHECK_EQUAL(m_cs.size(), 3);
393 int nDataUnderA = 0;
394 startInterest("/A/B/1");
395 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
396 startInterest("/A/B/2");
397 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
398 startInterest("/A/C/3");
399 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
400 startInterest("/A/C/4");
401 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
402 BOOST_CHECK_EQUAL(nDataUnderA, 1);
403
404 BOOST_CHECK_EQUAL(erase("/D", 2), 1);
405 BOOST_CHECK_EQUAL(m_cs.size(), 2);
406 startInterest("/D/5");
407 CHECK_CS_FIND(0);
408
409 BOOST_CHECK_EQUAL(erase("/F", 2), 0);
410 BOOST_CHECK_EQUAL(m_cs.size(), 2);
411}
412
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700413// When the capacity limit is set to zero, Data cannot be inserted;
414// this test case covers this situation.
415// The behavior of non-zero capacity limit depends on the eviction policy,
416// and is tested in policy test suites.
417BOOST_FIXTURE_TEST_CASE(ZeroCapacity, FindFixture)
418{
419 m_cs.setLimit(0);
420
421 BOOST_CHECK_EQUAL(m_cs.getLimit(), 0);
422 BOOST_CHECK_EQUAL(m_cs.size(), 0);
423 BOOST_CHECK(m_cs.begin() == m_cs.end());
424
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000425 insert(1, "/A");
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700426 BOOST_CHECK_EQUAL(m_cs.size(), 0);
427
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000428 startInterest("/A");
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700429 CHECK_CS_FIND(0);
430}
Junxiao Shia9388182014-12-13 23:16:09 -0700431
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000432BOOST_FIXTURE_TEST_CASE(EnablementFlags, FindFixture)
433{
434 BOOST_CHECK_EQUAL(m_cs.shouldAdmit(), true);
435 BOOST_CHECK_EQUAL(m_cs.shouldServe(), true);
436
437 insert(1, "/A");
438 m_cs.enableAdmit(false);
439 insert(2, "/B");
440 m_cs.enableAdmit(true);
441 insert(3, "/C");
442
443 startInterest("/A");
444 CHECK_CS_FIND(1);
445 startInterest("/B");
446 CHECK_CS_FIND(0);
447 startInterest("/C");
448 CHECK_CS_FIND(3);
449
450 m_cs.enableServe(false);
451 startInterest("/A");
452 CHECK_CS_FIND(0);
453 startInterest("/C");
454 CHECK_CS_FIND(0);
455
456 m_cs.enableServe(true);
457 startInterest("/A");
458 CHECK_CS_FIND(1);
459 startInterest("/C");
460 CHECK_CS_FIND(3);
461}
462
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000463BOOST_FIXTURE_TEST_CASE(CachePolicyNoCache, FindFixture)
Junxiao Shi35b16b12015-02-16 22:14:57 -0700464{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000465 insert(1, "/A", [] (Data& data) {
466 data.setTag(make_shared<lp::CachePolicyTag>(
467 lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE)));
468 });
Junxiao Shi35b16b12015-02-16 22:14:57 -0700469
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000470 startInterest("/A");
471 CHECK_CS_FIND(0);
Junxiao Shi35b16b12015-02-16 22:14:57 -0700472}
473
Junxiao Shifc206962015-01-16 11:12:22 -0700474BOOST_AUTO_TEST_CASE(Enumeration)
475{
476 Cs cs;
477
478 Name nameA("/A");
479 Name nameAB("/A/B");
480 Name nameABC("/A/B/C");
481 Name nameD("/D");
482
483 BOOST_CHECK_EQUAL(cs.size(), 0);
484 BOOST_CHECK(cs.begin() == cs.end());
485
486 cs.insert(*makeData(nameABC));
487 BOOST_CHECK_EQUAL(cs.size(), 1);
488 BOOST_CHECK(cs.begin() != cs.end());
489 BOOST_CHECK(cs.begin()->getName() == nameABC);
490 BOOST_CHECK((*cs.begin()).getName() == nameABC);
491
492 auto i = cs.begin();
493 auto j = cs.begin();
494 BOOST_CHECK(++i == cs.end());
495 BOOST_CHECK(j++ == cs.begin());
496 BOOST_CHECK(j == cs.end());
497
498 cs.insert(*makeData(nameA));
499 cs.insert(*makeData(nameAB));
500 cs.insert(*makeData(nameD));
501
502 std::set<Name> expected = {nameA, nameAB, nameABC, nameD};
503 std::set<Name> actual;
504 for (const auto& csEntry : cs) {
505 actual.insert(csEntry.getName());
506 }
507 BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
508}
509
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700510BOOST_AUTO_TEST_SUITE_END() // TestCs
511BOOST_AUTO_TEST_SUITE_END() // Table
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800512
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700513} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800514} // namespace cs
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800515} // namespace nfd