blob: 85886da3ce9bfb04d3d9f7fabe71783f141112c4 [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 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
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080033namespace cs {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070035
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040036BOOST_AUTO_TEST_SUITE(Table)
Junxiao Shi9222f362019-04-16 15:15:23 +000037BOOST_FIXTURE_TEST_SUITE(TestCs, CsFixture)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040038
Junxiao Shi9222f362019-04-16 15:15:23 +000039BOOST_AUTO_TEST_SUITE(Find)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080040
Junxiao Shia9388182014-12-13 23:16:09 -070041BOOST_AUTO_TEST_CASE(ExactName)
42{
Junxiao Shi1fbdb542017-02-03 00:05:53 +000043 insert(1, "/");
44 insert(2, "/A");
45 insert(3, "/A/B");
46 insert(4, "/A/C");
47 insert(5, "/D");
Junxiao Shia9388182014-12-13 23:16:09 -070048
Junxiao Shi1fbdb542017-02-03 00:05:53 +000049 startInterest("/A");
mzhang4eab72492015-02-25 11:16:09 -060050 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -070051}
52
Junxiao Shi9222f362019-04-16 15:15:23 +000053BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix)
54{
55 insert(1, "/");
56 insert(2, "/A");
57 insert(3, "/A/B");
58 insert(4, "/A/C");
59 insert(5, "/D");
60
61 startInterest("/A")
62 .setCanBePrefix(true);
63 CHECK_CS_FIND(2);
64}
65
Junxiao Shia9388182014-12-13 23:16:09 -070066BOOST_AUTO_TEST_CASE(FullName)
67{
Junxiao Shi1fbdb542017-02-03 00:05:53 +000068 Name n1 = insert(1, "/A");
69 Name n2 = insert(2, "/A");
Junxiao Shia9388182014-12-13 23:16:09 -070070
71 startInterest(n1);
mzhang4eab72492015-02-25 11:16:09 -060072 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -070073
74 startInterest(n2);
mzhang4eab72492015-02-25 11:16:09 -060075 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -070076}
77
Junxiao Shi9222f362019-04-16 15:15:23 +000078BOOST_AUTO_TEST_CASE(FullName_EmptyDataName)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080079{
Junxiao Shi9222f362019-04-16 15:15:23 +000080 Name n1 = insert(1, "/");
81 Name n2 = insert(2, "/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070082
Junxiao Shi9222f362019-04-16 15:15:23 +000083 startInterest(n1);
84 CHECK_CS_FIND(1);
85
86 startInterest(n2);
mzhang4eab72492015-02-25 11:16:09 -060087 CHECK_CS_FIND(2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080088}
89
Junxiao Shi9222f362019-04-16 15:15:23 +000090BOOST_AUTO_TEST_CASE(PrefixName)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080091{
Junxiao Shi1fbdb542017-02-03 00:05:53 +000092 insert(1, "/A");
93 insert(2, "/B/p/1");
94 insert(3, "/B/p/2");
95 insert(4, "/B/q/1");
96 insert(5, "/B/q/2");
97 insert(6, "/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070098
Junxiao Shi1fbdb542017-02-03 00:05:53 +000099 startInterest("/B")
Junxiao Shi9222f362019-04-16 15:15:23 +0000100 .setCanBePrefix(true);
101 CHECK_CS_FIND(2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800102}
103
Junxiao Shi9222f362019-04-16 15:15:23 +0000104BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800105{
Junxiao Shi9222f362019-04-16 15:15:23 +0000106 insert(1, "/B/p/1");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700107
Junxiao Shi9222f362019-04-16 15:15:23 +0000108 startInterest("/B");
mzhang4eab72492015-02-25 11:16:09 -0600109 CHECK_CS_FIND(0);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800110}
111
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000112BOOST_AUTO_TEST_CASE(MustBeFresh)
113{
Eric Newberryf4056d02017-05-26 17:31:53 +0000114 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
Davide Pesavento14e71f02019-03-28 17:35:25 -0400115 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); });
116 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); });
117 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); });
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000118
119 // lookup at exact same moment as insertion is not tested because this won't happen in reality
120
Junxiao Shi9222f362019-04-16 15:15:23 +0000121 advanceClocks(500_ms); // @500ms
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000122 startInterest("/A")
Junxiao Shi9222f362019-04-16 15:15:23 +0000123 .setCanBePrefix(true)
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000124 .setMustBeFresh(true);
125 CHECK_CS_FIND(3);
126
Junxiao Shi9222f362019-04-16 15:15:23 +0000127 advanceClocks(1500_ms); // @2s
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000128 startInterest("/A")
Junxiao Shi9222f362019-04-16 15:15:23 +0000129 .setCanBePrefix(true)
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000130 .setMustBeFresh(true);
131 CHECK_CS_FIND(4);
132
Junxiao Shi9222f362019-04-16 15:15:23 +0000133 advanceClocks(3500_s); // @3502s
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000134 startInterest("/A")
Junxiao Shi9222f362019-04-16 15:15:23 +0000135 .setCanBePrefix(true)
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000136 .setMustBeFresh(true);
137 CHECK_CS_FIND(4);
138
Junxiao Shi9222f362019-04-16 15:15:23 +0000139 advanceClocks(3500_s); // @7002s
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000140 startInterest("/A")
Junxiao Shi9222f362019-04-16 15:15:23 +0000141 .setCanBePrefix(true)
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000142 .setMustBeFresh(true);
Eric Newberryf4056d02017-05-26 17:31:53 +0000143 CHECK_CS_FIND(0);
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000144}
145
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700146BOOST_AUTO_TEST_SUITE_END() // Find
147
Junxiao Shi9222f362019-04-16 15:15:23 +0000148BOOST_AUTO_TEST_CASE(Erase)
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000149{
150 insert(1, "/A/B/1");
151 insert(2, "/A/B/2");
152 insert(3, "/A/C/3");
153 insert(4, "/A/C/4");
154 insert(5, "/D/5");
155 insert(6, "/E/6");
Junxiao Shi9222f362019-04-16 15:15:23 +0000156 BOOST_CHECK_EQUAL(cs.size(), 6);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000157
158 BOOST_CHECK_EQUAL(erase("/A", 3), 3);
Junxiao Shi9222f362019-04-16 15:15:23 +0000159 BOOST_CHECK_EQUAL(cs.size(), 3);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000160 int nDataUnderA = 0;
161 startInterest("/A/B/1");
162 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
163 startInterest("/A/B/2");
164 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
165 startInterest("/A/C/3");
166 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
167 startInterest("/A/C/4");
168 find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); });
169 BOOST_CHECK_EQUAL(nDataUnderA, 1);
170
171 BOOST_CHECK_EQUAL(erase("/D", 2), 1);
Junxiao Shi9222f362019-04-16 15:15:23 +0000172 BOOST_CHECK_EQUAL(cs.size(), 2);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000173 startInterest("/D/5");
174 CHECK_CS_FIND(0);
175
176 BOOST_CHECK_EQUAL(erase("/F", 2), 0);
Junxiao Shi9222f362019-04-16 15:15:23 +0000177 BOOST_CHECK_EQUAL(cs.size(), 2);
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000178}
179
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700180// When the capacity limit is set to zero, Data cannot be inserted;
181// this test case covers this situation.
182// The behavior of non-zero capacity limit depends on the eviction policy,
183// and is tested in policy test suites.
Junxiao Shi9222f362019-04-16 15:15:23 +0000184BOOST_AUTO_TEST_CASE(ZeroCapacity)
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700185{
Junxiao Shi9222f362019-04-16 15:15:23 +0000186 cs.setLimit(0);
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700187
Junxiao Shi9222f362019-04-16 15:15:23 +0000188 BOOST_CHECK_EQUAL(cs.getLimit(), 0);
189 BOOST_CHECK_EQUAL(cs.size(), 0);
190 BOOST_CHECK(cs.begin() == cs.end());
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700191
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000192 insert(1, "/A");
Junxiao Shi9222f362019-04-16 15:15:23 +0000193 BOOST_CHECK_EQUAL(cs.size(), 0);
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700194
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000195 startInterest("/A");
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700196 CHECK_CS_FIND(0);
197}
Junxiao Shia9388182014-12-13 23:16:09 -0700198
Junxiao Shi9222f362019-04-16 15:15:23 +0000199BOOST_AUTO_TEST_CASE(EnablementFlags)
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000200{
Junxiao Shi9222f362019-04-16 15:15:23 +0000201 BOOST_CHECK_EQUAL(cs.shouldAdmit(), true);
202 BOOST_CHECK_EQUAL(cs.shouldServe(), true);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000203
204 insert(1, "/A");
Junxiao Shi9222f362019-04-16 15:15:23 +0000205 cs.enableAdmit(false);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000206 insert(2, "/B");
Junxiao Shi9222f362019-04-16 15:15:23 +0000207 cs.enableAdmit(true);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000208 insert(3, "/C");
209
210 startInterest("/A");
211 CHECK_CS_FIND(1);
212 startInterest("/B");
213 CHECK_CS_FIND(0);
214 startInterest("/C");
215 CHECK_CS_FIND(3);
216
Junxiao Shi9222f362019-04-16 15:15:23 +0000217 cs.enableServe(false);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000218 startInterest("/A");
219 CHECK_CS_FIND(0);
220 startInterest("/C");
221 CHECK_CS_FIND(0);
222
Junxiao Shi9222f362019-04-16 15:15:23 +0000223 cs.enableServe(true);
Junxiao Shi3d2049f2018-01-26 23:46:06 +0000224 startInterest("/A");
225 CHECK_CS_FIND(1);
226 startInterest("/C");
227 CHECK_CS_FIND(3);
228}
229
Junxiao Shi9222f362019-04-16 15:15:23 +0000230BOOST_AUTO_TEST_CASE(CachePolicyNoCache)
Junxiao Shi35b16b12015-02-16 22:14:57 -0700231{
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000232 insert(1, "/A", [] (Data& data) {
233 data.setTag(make_shared<lp::CachePolicyTag>(
234 lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE)));
235 });
Junxiao Shi35b16b12015-02-16 22:14:57 -0700236
Junxiao Shi1fbdb542017-02-03 00:05:53 +0000237 startInterest("/A");
238 CHECK_CS_FIND(0);
Junxiao Shi35b16b12015-02-16 22:14:57 -0700239}
240
Junxiao Shifc206962015-01-16 11:12:22 -0700241BOOST_AUTO_TEST_CASE(Enumeration)
242{
Junxiao Shifc206962015-01-16 11:12:22 -0700243 Name nameA("/A");
244 Name nameAB("/A/B");
245 Name nameABC("/A/B/C");
246 Name nameD("/D");
247
248 BOOST_CHECK_EQUAL(cs.size(), 0);
249 BOOST_CHECK(cs.begin() == cs.end());
250
Junxiao Shi9222f362019-04-16 15:15:23 +0000251 insert(123, nameABC);
Junxiao Shifc206962015-01-16 11:12:22 -0700252 BOOST_CHECK_EQUAL(cs.size(), 1);
253 BOOST_CHECK(cs.begin() != cs.end());
254 BOOST_CHECK(cs.begin()->getName() == nameABC);
255 BOOST_CHECK((*cs.begin()).getName() == nameABC);
256
257 auto i = cs.begin();
258 auto j = cs.begin();
259 BOOST_CHECK(++i == cs.end());
260 BOOST_CHECK(j++ == cs.begin());
261 BOOST_CHECK(j == cs.end());
262
Junxiao Shi9222f362019-04-16 15:15:23 +0000263 insert(1, nameA);
264 insert(12, nameAB);
265 insert(4, nameD);
Junxiao Shifc206962015-01-16 11:12:22 -0700266
267 std::set<Name> expected = {nameA, nameAB, nameABC, nameD};
268 std::set<Name> actual;
269 for (const auto& csEntry : cs) {
270 actual.insert(csEntry.getName());
271 }
272 BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
273}
274
Minsheng Zhangffe8bbb2016-03-10 13:40:37 -0700275BOOST_AUTO_TEST_SUITE_END() // TestCs
276BOOST_AUTO_TEST_SUITE_END() // Table
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800277
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700278} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800279} // namespace cs
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800280} // namespace nfd