blob: f6c2a40683e84d44902a89c29de4be6e71e7cf67 [file] [log] [blame]
Jiewen Tan99135962014-09-20 02:18:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento10b24be2017-07-12 23:23:46 -04002/*
Davide Pesaventoa3d809e2022-02-06 11:55:02 -05003 * Copyright (c) 2013-2022 Regents of the University of California.
Jiewen Tan99135962014-09-20 02:18:53 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/ims/in-memory-storage.hpp"
23#include "ndn-cxx/ims/in-memory-storage-fifo.hpp"
24#include "ndn-cxx/ims/in-memory-storage-lfu.hpp"
25#include "ndn-cxx/ims/in-memory-storage-lru.hpp"
26#include "ndn-cxx/ims/in-memory-storage-persistent.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "ndn-cxx/util/sha256.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070028
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050029#include "tests/test-common.hpp"
30#include "tests/unit/io-fixture.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070031
Davide Pesavento5d0b0102017-10-07 13:43:16 -040032#include <boost/mpl/vector.hpp>
Jiewen Tan99135962014-09-20 02:18:53 -070033
34namespace ndn {
Junxiao Shi85d90832016-08-04 03:19:46 +000035namespace tests {
Jiewen Tan99135962014-09-20 02:18:53 -070036
Junxiao Shi85d90832016-08-04 03:19:46 +000037using namespace ndn::tests;
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070038
Junxiao Shic542f632017-07-18 14:20:32 +000039BOOST_AUTO_TEST_SUITE(Ims)
Junxiao Shi85d90832016-08-04 03:19:46 +000040BOOST_AUTO_TEST_SUITE(TestInMemoryStorage)
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070041
Davide Pesavento5d0b0102017-10-07 13:43:16 -040042using InMemoryStorages = boost::mpl::vector<InMemoryStoragePersistent,
43 InMemoryStorageFifo,
44 InMemoryStorageLfu,
45 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -070046
Jiewen Tan99135962014-09-20 02:18:53 -070047BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages)
48{
49 T ims;
50
Jiewen Tan99135962014-09-20 02:18:53 -070051 ims.insert(*makeData("/a"));
52 ims.insert(*makeData("/b"));
53 ims.insert(*makeData("/c"));
54 ims.insert(*makeData("/d"));
55
56 BOOST_CHECK_EQUAL(ims.size(), 4);
57}
58
Davide Pesaventoeee3e822016-11-26 19:19:34 +010059BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -070060{
61 T ims;
62
63 Name name("/a");
64
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050065 const uint8_t content1[] = {1, 2, 3, 4};
66 auto data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050067 data1->setFreshnessPeriod(99999_ms);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050068 data1->setContent(content1);
Jiewen Tan99135962014-09-20 02:18:53 -070069 signData(data1);
70 ims.insert(*data1);
71
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050072 const uint8_t content2[] = {5, 6, 7, 8};
73 auto data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050074 data2->setFreshnessPeriod(99999_ms);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050075 data2->setContent(content2);
Jiewen Tan99135962014-09-20 02:18:53 -070076 signData(data2);
77 ims.insert(*data2);
78
79 BOOST_CHECK_EQUAL(ims.size(), 2);
80}
81
82BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion, T, InMemoryStorages)
83{
84 T ims;
85
86 shared_ptr<Data> data0 = makeData("/insert/smth");
87 ims.insert(*data0);
88
89 shared_ptr<Data> data = makeData("/insert/duplicate");
90 ims.insert(*data);
91
92 ims.insert(*data);
93 BOOST_CHECK_EQUAL(ims.size(), 2);
94}
95
96BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion2, T, InMemoryStorages)
97{
98 T ims;
99
100 shared_ptr<Data> data = makeData("/insert/duplicate");
101 ims.insert(*data);
102
103 ims.insert(*data);
104 BOOST_CHECK_EQUAL(ims.size(), 1);
105
106 shared_ptr<Data> data2 = makeData("/insert/original");
107 ims.insert(*data2);
108 BOOST_CHECK_EQUAL(ims.size(), 2);
109}
110
111BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFind, T, InMemoryStorages)
112{
113 T ims;
114
115 Name name("/insert/and/find");
116
117 shared_ptr<Data> data = makeData(name);
118 ims.insert(*data);
119
120 shared_ptr<Interest> interest = makeInterest(name);
121
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500122 auto found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000123 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700124 BOOST_CHECK_EQUAL(data->getName(), found->getName());
125}
126
127BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages)
128{
129 T ims;
130
131 Name name("/insert/and/find");
132 shared_ptr<Data> data = makeData(name);
133 ims.insert(*data);
134
135 Name name2("/not/find");
136 shared_ptr<Interest> interest = makeInterest(name2);
137
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500138 auto found = ims.find(*interest);
139 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700140}
141
142BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages)
143{
144 T ims;
145
146 Name name("/insert/and/find");
147
148 shared_ptr<Data> data = makeData(name);
149 ims.insert(*data);
150
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500151 auto found = ims.find(name);
Junxiao Shi85d90832016-08-04 03:19:46 +0000152 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700153 BOOST_CHECK_EQUAL(data->getName(), found->getName());
154}
155
156BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages)
157{
158 T ims;
159
160 Name name("/insert/and/find");
161
162 shared_ptr<Data> data = makeData(name);
163 ims.insert(*data);
164
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500165 auto found = ims.find(data->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000166 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700167 BOOST_CHECK_EQUAL(data->getFullName(), found->getFullName());
168}
169
170BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByName, T, InMemoryStorages)
171{
172 T ims;
173
174 Name name("/insert/and/find");
175 shared_ptr<Data> data = makeData(name);
176 ims.insert(*data);
177
178 Name name2("/not/find");
179
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500180 auto found = ims.find(name2);
Junxiao Shi85d90832016-08-04 03:19:46 +0000181 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700182}
183
184BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByFullName, T, InMemoryStorages)
185{
186 T ims;
187
188 Name name("/a");
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500189 const uint8_t content1[] = {1, 2, 3, 4};
190 auto data1 = makeData(name);
191 data1->setContent(content1);
Jiewen Tan99135962014-09-20 02:18:53 -0700192 signData(data1);
193 ims.insert(*data1);
194
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500195 const uint8_t content2[] = {5, 6, 7, 8};
196 auto data2 = makeData(name);
197 data2->setContent(content2);
Jiewen Tan99135962014-09-20 02:18:53 -0700198 signData(data2);
199
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500200 auto found = ims.find(data2->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000201 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700202}
203
204BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages)
205{
206 T ims;
207
208 Name name("/insertandremovebyname");
209
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500210 const uint8_t content1[] = {1, 2, 3, 4};
211 auto data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500212 data1->setFreshnessPeriod(99999_ms);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500213 data1->setContent(content1);
Jiewen Tan99135962014-09-20 02:18:53 -0700214 signData(data1);
215 ims.insert(*data1);
216
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500217 const uint8_t content2[] = {5, 6, 7, 8};
218 auto data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500219 data2->setFreshnessPeriod(99999_ms);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500220 data2->setContent(content2);
Jiewen Tan99135962014-09-20 02:18:53 -0700221 signData(data2);
222 ims.insert(*data2);
223
224 shared_ptr<Data> data3 = makeData("/insertandremovebyname/1");
225 ims.insert(*data3);
226
227 shared_ptr<Data> data4 = makeData("/insertandremovebyname/2");
228 ims.insert(*data4);
229
230 BOOST_CHECK_EQUAL(ims.size(), 4);
231
232 ims.erase(data1->getFullName(), false);
233 BOOST_CHECK_EQUAL(ims.size(), 3);
234}
235
236BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages)
237{
238 T ims;
239
240 shared_ptr<Data> data = makeData("/a");
241 ims.insert(*data);
242
243 shared_ptr<Data> data2 = makeData("/b");
244 ims.insert(*data2);
245
246 shared_ptr<Data> data3 = makeData("/c");
247 ims.insert(*data3);
248
249 shared_ptr<Data> data4 = makeData("/d");
250 ims.insert(*data4);
251
252 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
253 ims.insert(*data5);
254
255 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
256 ims.insert(*data6);
257
258 shared_ptr<Data> data7 = makeData("/c/c/1");
259 ims.insert(*data7);
260
261 BOOST_CHECK_EQUAL(ims.size(), 7);
262
263 Name name("/c");
264 ims.erase(name);
265 BOOST_CHECK_EQUAL(ims.size(), 3);
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600266 BOOST_CHECK_EQUAL(ims.getCapacity(), 16);
Jiewen Tan99135962014-09-20 02:18:53 -0700267}
268
269BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages)
270{
271 shared_ptr<Data> data = makeData("/digest/compute");
272
Davide Pesavento258d51a2022-02-27 21:26:28 -0500273 auto digest1 = util::Sha256::computeDigest(data->wireEncode());
Jiewen Tan99135962014-09-20 02:18:53 -0700274 BOOST_CHECK_EQUAL(digest1->size(), 32);
275
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800276 InMemoryStorageEntry entry;
277 entry.setData(*data);
Jiewen Tan99135962014-09-20 02:18:53 -0700278 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800279 entry.getFullName()[-1].value_begin(),
280 entry.getFullName()[-1].value_end());
Jiewen Tan99135962014-09-20 02:18:53 -0700281}
282
283BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
284{
285 T ims;
286
287 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
288
289 for (int i = 0; i < 10; i++) {
290 std::ostringstream convert;
291 convert << i;
292 Name name("/" + convert.str());
293 shared_ptr<Data> data = makeData(name);
294 ims.insert(*data);
295 }
296
297 InMemoryStorage::const_iterator it = ims.begin();
298 InMemoryStorage::const_iterator tmp1 = it;
299 BOOST_REQUIRE(tmp1 == it);
300 InMemoryStorage::const_iterator tmp2 = tmp1++;
301 BOOST_REQUIRE(tmp2 != tmp1);
302 tmp2 = ++tmp1;
303 BOOST_REQUIRE(tmp2 == tmp1);
304
305 int i = 0;
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400306 for (; it != ims.end(); it++) {
Jiewen Tan99135962014-09-20 02:18:53 -0700307 std::ostringstream convert;
308 convert << i;
309 Name name("/" + convert.str());
310 BOOST_CHECK_EQUAL(it->getName(), name);
311 BOOST_CHECK_EQUAL((*it).getName(), name);
312 i++;
313 }
314}
315
316BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
317{
318 T ims;
319
320 shared_ptr<Data> data = makeData("/a");
321 ims.insert(*data);
322
323 shared_ptr<Data> data2 = makeData("/b");
324 ims.insert(*data2);
325
326 shared_ptr<Data> data3 = makeData("/c");
327 ims.insert(*data3);
328
329 shared_ptr<Data> data4 = makeData("/d");
330 ims.insert(*data4);
331
332 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
333 ims.insert(*data5);
334
335 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
336 ims.insert(*data6);
337
338 shared_ptr<Data> data7 = makeData("/c/c/1");
339 ims.insert(*data7);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100340
341 // avoid "test case [...] did not check any assertions" message from Boost.Test
342 BOOST_CHECK(true);
Jiewen Tan99135962014-09-20 02:18:53 -0700343}
344
345BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
346{
347 T ims;
348
349 shared_ptr<Data> data = makeData("/a");
350 ims.insert(*data);
351
352 shared_ptr<Data> data2 = makeData("/b");
353 ims.insert(*data2);
354
355 shared_ptr<Data> data3 = makeData("/c");
356 ims.insert(*data3);
357
358 shared_ptr<Data> data4 = makeData("/d");
359 ims.insert(*data4);
360
361 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
362 ims.insert(*data5);
363
364 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
365 ims.insert(*data6);
366
367 shared_ptr<Data> data7 = makeData("/c/c/1");
368 ims.insert(*data7);
369
Davide Pesavento258d51a2022-02-27 21:26:28 -0500370 auto digest1 = util::Sha256::computeDigest(data->wireEncode());
Jiewen Tan99135962014-09-20 02:18:53 -0700371
372 Name name("/a");
373 ims.erase(name);
374 BOOST_CHECK_EQUAL(ims.size(), 6);
375}
376
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400377using InMemoryStoragesLimited = boost::mpl::vector<InMemoryStorageFifo,
378 InMemoryStorageLfu,
379 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -0700380
Yingdi Yu404eafd2016-03-06 14:54:25 -0800381BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited)
Jiewen Tan99135962014-09-20 02:18:53 -0700382{
383 T ims;
384
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600385 ims.setCapacity(18);
386 for (int i = 1; i < 19; ++i) {
387 ims.insert(*makeData(to_string(i)));
388 }
389 BOOST_CHECK_EQUAL(ims.size(), 18);
Jiewen Tan99135962014-09-20 02:18:53 -0700390
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600391 ims.setCapacity(16);
392 BOOST_CHECK_EQUAL(ims.size(), 16);
Jiewen Tan99135962014-09-20 02:18:53 -0700393}
394
395BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
396{
397 T ims(10000);
Jiewen Tan99135962014-09-20 02:18:53 -0700398 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
399
400 T ims2(4);
Jiewen Tan99135962014-09-20 02:18:53 -0700401 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
402}
403
404BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
405{
406 T ims(40);
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600407 size_t initialCapacity = ims.getCapacity();
Jiewen Tan99135962014-09-20 02:18:53 -0700408
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600409 for (size_t i = 0; i < initialCapacity + 1; i++) {
410 shared_ptr<Data> data = makeData(to_string(i));
Davide Pesavento0f830802018-01-16 23:58:58 -0500411 data->setFreshnessPeriod(5000_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700412 signData(data);
413 ims.insert(*data);
414 }
415
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600416 BOOST_CHECK_EQUAL(ims.size(), initialCapacity + 1);
417 BOOST_CHECK_EQUAL(ims.getCapacity(), initialCapacity * 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700418}
419
420BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
421{
422 T ims(2);
423
424 Name name("/insert/1");
425 shared_ptr<Data> data = makeData(name);
426 ims.insert(*data);
427
428 Name name2("/insert/2");
429 shared_ptr<Data> data2 = makeData(name2);
430 ims.insert(*data2);
431
432 Name name3("/insert/3");
433 shared_ptr<Data> data3 = makeData(name3);
434 ims.insert(*data3);
435
436 BOOST_CHECK_EQUAL(ims.size(), 2);
437
438 shared_ptr<Interest> interest = makeInterest(name);
439 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000440 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700441}
442
Junxiao Shic542f632017-07-18 14:20:32 +0000443// Find function is implemented at the base case, so it's sufficient to test for one derived class.
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500444class FindFixture : public IoFixture
Jiewen Tan99135962014-09-20 02:18:53 -0700445{
446protected:
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800447 Name
Yingdi Yu404eafd2016-03-06 14:54:25 -0800448 insert(uint32_t id, const Name& name,
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000449 const std::function<void(Data&)>& modifyData = nullptr,
Yingdi Yu404eafd2016-03-06 14:54:25 -0800450 const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW)
Jiewen Tan99135962014-09-20 02:18:53 -0700451 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000452 auto data = makeData(name);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500453 data->setContent(make_span(reinterpret_cast<const uint8_t*>(&id), sizeof(id)));
Jiewen Tan99135962014-09-20 02:18:53 -0700454
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000455 if (modifyData != nullptr) {
456 modifyData(*data);
457 }
458
459 data->wireEncode();
Yingdi Yu404eafd2016-03-06 14:54:25 -0800460 m_ims.insert(*data, freshWindow);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800461
462 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700463 }
464
465 Interest&
466 startInterest(const Name& name)
467 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000468 m_interest = makeInterest(name, false);
Jiewen Tan99135962014-09-20 02:18:53 -0700469 return *m_interest;
470 }
471
472 uint32_t
473 find()
474 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500475 auto found = m_ims.find(*m_interest);
476 if (found == nullptr) {
Jiewen Tan99135962014-09-20 02:18:53 -0700477 return 0;
478 }
479 const Block& content = found->getContent();
480 if (content.value_size() != sizeof(uint32_t)) {
481 return 0;
482 }
Junxiao Shibaab7322017-07-27 01:22:49 +0000483 uint32_t id = 0;
484 std::memcpy(&id, content.value(), sizeof(id));
485 return id;
Jiewen Tan99135962014-09-20 02:18:53 -0700486 }
487
488protected:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500489 InMemoryStoragePersistent m_ims{m_io};
Jiewen Tan99135962014-09-20 02:18:53 -0700490 shared_ptr<Interest> m_interest;
491};
492
493BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
494
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800495BOOST_AUTO_TEST_CASE(ExactName)
496{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000497 insert(1, "/");
498 insert(2, "/A");
499 insert(3, "/A/B");
500 insert(4, "/A/C");
501 insert(5, "/D");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800502
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000503 startInterest("/A");
504 BOOST_CHECK_EQUAL(find(), 2);
505}
506
507BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix)
508{
509 insert(1, "/");
510 insert(2, "/A");
511 insert(3, "/A/B");
512 insert(4, "/A/C");
513 insert(5, "/D");
514
515 startInterest("/A")
516 .setCanBePrefix(true);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800517 BOOST_CHECK_EQUAL(find(), 2);
518}
519
520BOOST_AUTO_TEST_CASE(FullName)
521{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000522 Name n1 = insert(1, "/A");
523 Name n2 = insert(2, "/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800524
525 startInterest(n1);
526 BOOST_CHECK_EQUAL(find(), 1);
527
528 startInterest(n2);
529 BOOST_CHECK_EQUAL(find(), 2);
530}
531
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000532BOOST_AUTO_TEST_CASE(FullName_EmptyDataName)
Jiewen Tan99135962014-09-20 02:18:53 -0700533{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000534 Name n1 = insert(1, "/");
535 Name n2 = insert(2, "/");
Jiewen Tan99135962014-09-20 02:18:53 -0700536
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000537 startInterest(n1);
538 BOOST_CHECK_EQUAL(find(), 1);
539
540 startInterest(n2);
Jiewen Tan99135962014-09-20 02:18:53 -0700541 BOOST_CHECK_EQUAL(find(), 2);
542}
543
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000544BOOST_AUTO_TEST_CASE(PrefixName)
Jiewen Tan99135962014-09-20 02:18:53 -0700545{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000546 insert(1, "/A");
547 insert(2, "/B/p/1");
548 insert(3, "/B/p/2");
549 insert(4, "/B/q/1");
550 insert(5, "/B/q/2");
551 insert(6, "/C");
Jiewen Tan99135962014-09-20 02:18:53 -0700552
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000553 startInterest("/B")
554 .setCanBePrefix(true);
555 BOOST_CHECK_EQUAL(find(), 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700556}
557
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000558BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix)
Jiewen Tan99135962014-09-20 02:18:53 -0700559{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000560 insert(1, "/B/p/1");
Jiewen Tan99135962014-09-20 02:18:53 -0700561
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000562 startInterest("/B");
Jiewen Tan99135962014-09-20 02:18:53 -0700563 BOOST_CHECK_EQUAL(find(), 0);
564}
565
Yingdi Yu404eafd2016-03-06 14:54:25 -0800566BOOST_AUTO_TEST_CASE(MustBeFresh)
567{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000568 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
569 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); });
570 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); }, 1_s);
571 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); }, 1_h);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800572
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000573 // lookup at exact same moment as insertion is not tested because this won't happen in reality
574
575 advanceClocks(500_ms); // @500ms
576 startInterest("/A")
577 .setCanBePrefix(true)
Yingdi Yu404eafd2016-03-06 14:54:25 -0800578 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800579 BOOST_CHECK_EQUAL(find(), 3);
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000580
581 advanceClocks(1500_ms); // @2s
582 startInterest("/A")
583 .setCanBePrefix(true)
584 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800585 BOOST_CHECK_EQUAL(find(), 4);
586
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000587 advanceClocks(3500_s); // @3502s
588 startInterest("/A")
589 .setCanBePrefix(true)
590 .setMustBeFresh(true);
591 BOOST_CHECK_EQUAL(find(), 4);
592
593 advanceClocks(3500_s); // @7002s
594 startInterest("/A")
595 .setCanBePrefix(true)
596 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800597 BOOST_CHECK_EQUAL(find(), 0);
598}
599
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -0700600BOOST_AUTO_TEST_SUITE_END() // Find
Junxiao Shi85d90832016-08-04 03:19:46 +0000601BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage
Junxiao Shic542f632017-07-18 14:20:32 +0000602BOOST_AUTO_TEST_SUITE_END() // Ims
Jiewen Tan99135962014-09-20 02:18:53 -0700603
Junxiao Shi85d90832016-08-04 03:19:46 +0000604} // namespace tests
Jiewen Tan99135962014-09-20 02:18:53 -0700605} // namespace ndn