blob: 67a5621e1dbb57a687bda79c1ad5d3430e130e77 [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 Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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 Shi9222f362019-04-16 15:15:23 +000028#include "tests/daemon/table/cs-fixture.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080029
Junxiao Shi4e1b6ee2017-08-03 02:20:50 +000030#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi4e1b6ee2017-08-03 02:20:50 +000031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070033
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040034BOOST_AUTO_TEST_SUITE(Table)
Junxiao Shi9222f362019-04-16 15:15:23 +000035BOOST_FIXTURE_TEST_SUITE(TestCs, CsFixture)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040036
Junxiao Shi9222f362019-04-16 15:15:23 +000037BOOST_AUTO_TEST_SUITE(Find)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080038
Junxiao Shia9388182014-12-13 23:16:09 -070039BOOST_AUTO_TEST_CASE(ExactName)
40{
Junxiao Shi1fbdb542017-02-03 00:05:53 +000041 insert(1, "/");
42 insert(2, "/A");
43 insert(3, "/A/B");
44 insert(4, "/A/C");
45 insert(5, "/D");
Junxiao Shia9388182014-12-13 23:16:09 -070046
Junxiao Shi1fbdb542017-02-03 00:05:53 +000047 startInterest("/A");
mzhang4eab72492015-02-25 11:16:09 -060048 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -070049}
50
Junxiao Shi9222f362019-04-16 15:15:23 +000051BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix)
52{
53 insert(1, "/");
54 insert(2, "/A");
55 insert(3, "/A/B");
56 insert(4, "/A/C");
57 insert(5, "/D");
58
59 startInterest("/A")
60 .setCanBePrefix(true);
61 CHECK_CS_FIND(2);
62}
63
Junxiao Shia9388182014-12-13 23:16:09 -070064BOOST_AUTO_TEST_CASE(FullName)
65{
Junxiao Shi1fbdb542017-02-03 00:05:53 +000066 Name n1 = insert(1, "/A");
67 Name n2 = insert(2, "/A");
Junxiao Shia9388182014-12-13 23:16:09 -070068
69 startInterest(n1);
mzhang4eab72492015-02-25 11:16:09 -060070 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -070071
72 startInterest(n2);
mzhang4eab72492015-02-25 11:16:09 -060073 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -070074}
75
Junxiao Shi9222f362019-04-16 15:15:23 +000076BOOST_AUTO_TEST_CASE(FullName_EmptyDataName)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080077{
Junxiao Shi9222f362019-04-16 15:15:23 +000078 Name n1 = insert(1, "/");
79 Name n2 = insert(2, "/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070080
Junxiao Shi9222f362019-04-16 15:15:23 +000081 startInterest(n1);
82 CHECK_CS_FIND(1);
83
84 startInterest(n2);
mzhang4eab72492015-02-25 11:16:09 -060085 CHECK_CS_FIND(2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080086}
87
Junxiao Shi9222f362019-04-16 15:15:23 +000088BOOST_AUTO_TEST_CASE(PrefixName)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080089{
Junxiao Shi1fbdb542017-02-03 00:05:53 +000090 insert(1, "/A");
91 insert(2, "/B/p/1");
92 insert(3, "/B/p/2");
93 insert(4, "/B/q/1");
94 insert(5, "/B/q/2");
95 insert(6, "/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070096
Junxiao Shi1fbdb542017-02-03 00:05:53 +000097 startInterest("/B")
Junxiao Shi9222f362019-04-16 15:15:23 +000098 .setCanBePrefix(true);
99 CHECK_CS_FIND(2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800100}
101
Junxiao Shi9222f362019-04-16 15:15:23 +0000102BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800103{
Junxiao Shi9222f362019-04-16 15:15:23 +0000104 insert(1, "/B/p/1");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700105
Junxiao Shi9222f362019-04-16 15:15:23 +0000106 startInterest("/B");
mzhang4eab72492015-02-25 11:16:09 -0600107 CHECK_CS_FIND(0);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800108}
109
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000110BOOST_AUTO_TEST_CASE(MustBeFresh)
111{
Eric Newberryf4056d02017-05-26 17:31:53 +0000112 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
Davide Pesavento14e71f02019-03-28 17:35:25 -0400113 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); });
114 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); });
115 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); });
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000116
117 // lookup at exact same moment as insertion is not tested because this won't happen in reality
118
Junxiao Shi9222f362019-04-16 15:15:23 +0000119 advanceClocks(500_ms); // @500ms
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000120 startInterest("/A")
Junxiao Shi9222f362019-04-16 15:15:23 +0000121 .setCanBePrefix(true)
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000122 .setMustBeFresh(true);
123 CHECK_CS_FIND(3);
124
Junxiao Shi9222f362019-04-16 15:15:23 +0000125 advanceClocks(1500_ms); // @2s
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000126 startInterest("/A")
Junxiao Shi9222f362019-04-16 15:15:23 +0000127 .setCanBePrefix(true)
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000128 .setMustBeFresh(true);
129 CHECK_CS_FIND(4);
130
Junxiao Shi9222f362019-04-16 15:15:23 +0000131 advanceClocks(3500_s); // @3502s
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000132 startInterest("/A")
Junxiao Shi9222f362019-04-16 15:15:23 +0000133 .setCanBePrefix(true)
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000134 .setMustBeFresh(true);
135 CHECK_CS_FIND(4);
136
Junxiao Shi9222f362019-04-16 15:15:23 +0000137 advanceClocks(3500_s); // @7002s
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000138 startInterest("/A")
Junxiao Shi9222f362019-04-16 15:15:23 +0000139 .setCanBePrefix(true)
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000140 .setMustBeFresh(true);
Eric Newberryf4056d02017-05-26 17:31:53 +0000141 CHECK_CS_FIND(0);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000142}
143
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700144BOOST_AUTO_TEST_SUITE_END() // Find
145
Junxiao Shi9222f362019-04-16 15:15:23 +0000146BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000147{
148 insert(1, "/A/B/1");
149 insert(2, "/A/B/2");
150 insert(3, "/A/C/3");
151 insert(4, "/A/C/4");
152 insert(5, "/D/5");
153 insert(6, "/E/6");
Junxiao Shi9222f362019-04-16 15:15:23 +0000154 BOOST_CHECK_EQUAL(cs.size(), 6);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000155
156 BOOST_CHECK_EQUAL(erase("/A", 3), 3);
Junxiao Shi9222f362019-04-16 15:15:23 +0000157 BOOST_CHECK_EQUAL(cs.size(), 3);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000158 int nDataUnderA = 0;
159 startInterest("/A/B/1");
160 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
161 startInterest("/A/B/2");
162 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
163 startInterest("/A/C/3");
164 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
165 startInterest("/A/C/4");
166 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
167 BOOST_CHECK_EQUAL(nDataUnderA, 1);
168
169 BOOST_CHECK_EQUAL(erase("/D", 2), 1);
Junxiao Shi9222f362019-04-16 15:15:23 +0000170 BOOST_CHECK_EQUAL(cs.size(), 2);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000171 startInterest("/D/5");
172 CHECK_CS_FIND(0);
173
174 BOOST_CHECK_EQUAL(erase("/F", 2), 0);
Junxiao Shi9222f362019-04-16 15:15:23 +0000175 BOOST_CHECK_EQUAL(cs.size(), 2);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000176}
177
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700178// When the capacity limit is set to zero, Data cannot be inserted;
179// this test case covers this situation.
180// The behavior of non-zero capacity limit depends on the eviction policy,
181// and is tested in policy test suites.
Junxiao Shi9222f362019-04-16 15:15:23 +0000182BOOST_AUTO_TEST_CASE(ZeroCapacity)
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700183{
Junxiao Shi9222f362019-04-16 15:15:23 +0000184 cs.setLimit(0);
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700185
Junxiao Shi9222f362019-04-16 15:15:23 +0000186 BOOST_CHECK_EQUAL(cs.getLimit(), 0);
187 BOOST_CHECK_EQUAL(cs.size(), 0);
188 BOOST_CHECK(cs.begin() == cs.end());
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700189
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000190 insert(1, "/A");
Junxiao Shi9222f362019-04-16 15:15:23 +0000191 BOOST_CHECK_EQUAL(cs.size(), 0);
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700192
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000193 startInterest("/A");
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700194 CHECK_CS_FIND(0);
195}
Junxiao Shia9388182014-12-13 23:16:09 -0700196
Junxiao Shi9222f362019-04-16 15:15:23 +0000197BOOST_AUTO_TEST_CASE(EnablementFlags)
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000198{
Junxiao Shi9222f362019-04-16 15:15:23 +0000199 BOOST_CHECK_EQUAL(cs.shouldAdmit(), true);
200 BOOST_CHECK_EQUAL(cs.shouldServe(), true);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000201
202 insert(1, "/A");
Junxiao Shi9222f362019-04-16 15:15:23 +0000203 cs.enableAdmit(false);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000204 insert(2, "/B");
Junxiao Shi9222f362019-04-16 15:15:23 +0000205 cs.enableAdmit(true);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000206 insert(3, "/C");
207
208 startInterest("/A");
209 CHECK_CS_FIND(1);
210 startInterest("/B");
211 CHECK_CS_FIND(0);
212 startInterest("/C");
213 CHECK_CS_FIND(3);
214
Junxiao Shi9222f362019-04-16 15:15:23 +0000215 cs.enableServe(false);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000216 startInterest("/A");
217 CHECK_CS_FIND(0);
218 startInterest("/C");
219 CHECK_CS_FIND(0);
220
Junxiao Shi9222f362019-04-16 15:15:23 +0000221 cs.enableServe(true);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000222 startInterest("/A");
223 CHECK_CS_FIND(1);
224 startInterest("/C");
225 CHECK_CS_FIND(3);
226}
227
Junxiao Shi9222f362019-04-16 15:15:23 +0000228BOOST_AUTO_TEST_CASE(CachePolicyNoCache)
Junxiao Shi35b16b12015-02-16 22:14:57 -0700229{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000230 insert(1, "/A", [] (Data& data) {
231 data.setTag(make_shared<lp::CachePolicyTag>(
232 lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE)));
233 });
Junxiao Shi35b16b12015-02-16 22:14:57 -0700234
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000235 startInterest("/A");
236 CHECK_CS_FIND(0);
Junxiao Shi35b16b12015-02-16 22:14:57 -0700237}
238
Junxiao Shifc206962015-01-16 11:12:22 -0700239BOOST_AUTO_TEST_CASE(Enumeration)
240{
Junxiao Shifc206962015-01-16 11:12:22 -0700241 Name nameA("/A");
242 Name nameAB("/A/B");
243 Name nameABC("/A/B/C");
244 Name nameD("/D");
245
246 BOOST_CHECK_EQUAL(cs.size(), 0);
247 BOOST_CHECK(cs.begin() == cs.end());
248
Junxiao Shi9222f362019-04-16 15:15:23 +0000249 insert(123, nameABC);
Junxiao Shifc206962015-01-16 11:12:22 -0700250 BOOST_CHECK_EQUAL(cs.size(), 1);
251 BOOST_CHECK(cs.begin() != cs.end());
252 BOOST_CHECK(cs.begin()->getName() == nameABC);
253 BOOST_CHECK((*cs.begin()).getName() == nameABC);
254
255 auto i = cs.begin();
256 auto j = cs.begin();
257 BOOST_CHECK(++i == cs.end());
258 BOOST_CHECK(j++ == cs.begin());
259 BOOST_CHECK(j == cs.end());
260
Junxiao Shi9222f362019-04-16 15:15:23 +0000261 insert(1, nameA);
262 insert(12, nameAB);
263 insert(4, nameD);
Junxiao Shifc206962015-01-16 11:12:22 -0700264
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400265 const std::set<Name> expected{nameA, nameAB, nameABC, nameD};
Junxiao Shifc206962015-01-16 11:12:22 -0700266 std::set<Name> actual;
267 for (const auto& csEntry : cs) {
268 actual.insert(csEntry.getName());
269 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400270 BOOST_TEST(actual == expected, boost::test_tools::per_element());
Junxiao Shifc206962015-01-16 11:12:22 -0700271}
272
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700273BOOST_AUTO_TEST_SUITE_END() // TestCs
274BOOST_AUTO_TEST_SUITE_END() // Table
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800275
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400276} // namespace nfd::tests