blob: ac1c7c21914589ff4129cf3c57c4e5c51ccf5962 [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 Pesavento152ef442023-04-22 02:02:29 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040034namespace ndn::tests {
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070035
Junxiao Shic542f632017-07-18 14:20:32 +000036BOOST_AUTO_TEST_SUITE(Ims)
Junxiao Shi85d90832016-08-04 03:19:46 +000037BOOST_AUTO_TEST_SUITE(TestInMemoryStorage)
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070038
Davide Pesavento5d0b0102017-10-07 13:43:16 -040039using InMemoryStorages = boost::mpl::vector<InMemoryStoragePersistent,
40 InMemoryStorageFifo,
41 InMemoryStorageLfu,
42 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -070043
Jiewen Tan99135962014-09-20 02:18:53 -070044BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages)
45{
46 T ims;
47
Jiewen Tan99135962014-09-20 02:18:53 -070048 ims.insert(*makeData("/a"));
49 ims.insert(*makeData("/b"));
50 ims.insert(*makeData("/c"));
51 ims.insert(*makeData("/d"));
52
53 BOOST_CHECK_EQUAL(ims.size(), 4);
54}
55
Davide Pesaventoeee3e822016-11-26 19:19:34 +010056BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -070057{
58 T ims;
59
60 Name name("/a");
61
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050062 const uint8_t content1[] = {1, 2, 3, 4};
63 auto data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050064 data1->setFreshnessPeriod(99999_ms);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050065 data1->setContent(content1);
Jiewen Tan99135962014-09-20 02:18:53 -070066 signData(data1);
67 ims.insert(*data1);
68
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050069 const uint8_t content2[] = {5, 6, 7, 8};
70 auto data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050071 data2->setFreshnessPeriod(99999_ms);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050072 data2->setContent(content2);
Jiewen Tan99135962014-09-20 02:18:53 -070073 signData(data2);
74 ims.insert(*data2);
75
76 BOOST_CHECK_EQUAL(ims.size(), 2);
77}
78
79BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion, T, InMemoryStorages)
80{
81 T ims;
82
83 shared_ptr<Data> data0 = makeData("/insert/smth");
84 ims.insert(*data0);
85
86 shared_ptr<Data> data = makeData("/insert/duplicate");
87 ims.insert(*data);
88
89 ims.insert(*data);
90 BOOST_CHECK_EQUAL(ims.size(), 2);
91}
92
93BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion2, T, InMemoryStorages)
94{
95 T ims;
96
97 shared_ptr<Data> data = makeData("/insert/duplicate");
98 ims.insert(*data);
99
100 ims.insert(*data);
101 BOOST_CHECK_EQUAL(ims.size(), 1);
102
103 shared_ptr<Data> data2 = makeData("/insert/original");
104 ims.insert(*data2);
105 BOOST_CHECK_EQUAL(ims.size(), 2);
106}
107
108BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFind, T, InMemoryStorages)
109{
110 T ims;
111
112 Name name("/insert/and/find");
113
114 shared_ptr<Data> data = makeData(name);
115 ims.insert(*data);
116
117 shared_ptr<Interest> interest = makeInterest(name);
118
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500119 auto found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000120 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700121 BOOST_CHECK_EQUAL(data->getName(), found->getName());
122}
123
124BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages)
125{
126 T ims;
127
128 Name name("/insert/and/find");
129 shared_ptr<Data> data = makeData(name);
130 ims.insert(*data);
131
132 Name name2("/not/find");
133 shared_ptr<Interest> interest = makeInterest(name2);
134
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500135 auto found = ims.find(*interest);
136 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700137}
138
139BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages)
140{
141 T ims;
142
143 Name name("/insert/and/find");
144
145 shared_ptr<Data> data = makeData(name);
146 ims.insert(*data);
147
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500148 auto found = ims.find(name);
Junxiao Shi85d90832016-08-04 03:19:46 +0000149 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700150 BOOST_CHECK_EQUAL(data->getName(), found->getName());
151}
152
153BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages)
154{
155 T ims;
156
157 Name name("/insert/and/find");
158
159 shared_ptr<Data> data = makeData(name);
160 ims.insert(*data);
161
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500162 auto found = ims.find(data->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000163 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700164 BOOST_CHECK_EQUAL(data->getFullName(), found->getFullName());
165}
166
167BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByName, T, InMemoryStorages)
168{
169 T ims;
170
171 Name name("/insert/and/find");
172 shared_ptr<Data> data = makeData(name);
173 ims.insert(*data);
174
175 Name name2("/not/find");
176
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500177 auto found = ims.find(name2);
Junxiao Shi85d90832016-08-04 03:19:46 +0000178 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700179}
180
181BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByFullName, T, InMemoryStorages)
182{
183 T ims;
184
185 Name name("/a");
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500186 const uint8_t content1[] = {1, 2, 3, 4};
187 auto data1 = makeData(name);
188 data1->setContent(content1);
Jiewen Tan99135962014-09-20 02:18:53 -0700189 signData(data1);
190 ims.insert(*data1);
191
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500192 const uint8_t content2[] = {5, 6, 7, 8};
193 auto data2 = makeData(name);
194 data2->setContent(content2);
Jiewen Tan99135962014-09-20 02:18:53 -0700195 signData(data2);
196
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500197 auto found = ims.find(data2->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000198 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700199}
200
201BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages)
202{
203 T ims;
204
205 Name name("/insertandremovebyname");
206
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500207 const uint8_t content1[] = {1, 2, 3, 4};
208 auto data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500209 data1->setFreshnessPeriod(99999_ms);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500210 data1->setContent(content1);
Jiewen Tan99135962014-09-20 02:18:53 -0700211 signData(data1);
212 ims.insert(*data1);
213
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500214 const uint8_t content2[] = {5, 6, 7, 8};
215 auto data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500216 data2->setFreshnessPeriod(99999_ms);
Davide Pesaventoa3d809e2022-02-06 11:55:02 -0500217 data2->setContent(content2);
Jiewen Tan99135962014-09-20 02:18:53 -0700218 signData(data2);
219 ims.insert(*data2);
220
221 shared_ptr<Data> data3 = makeData("/insertandremovebyname/1");
222 ims.insert(*data3);
223
224 shared_ptr<Data> data4 = makeData("/insertandremovebyname/2");
225 ims.insert(*data4);
226
227 BOOST_CHECK_EQUAL(ims.size(), 4);
228
229 ims.erase(data1->getFullName(), false);
230 BOOST_CHECK_EQUAL(ims.size(), 3);
231}
232
233BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages)
234{
235 T ims;
236
237 shared_ptr<Data> data = makeData("/a");
238 ims.insert(*data);
239
240 shared_ptr<Data> data2 = makeData("/b");
241 ims.insert(*data2);
242
243 shared_ptr<Data> data3 = makeData("/c");
244 ims.insert(*data3);
245
246 shared_ptr<Data> data4 = makeData("/d");
247 ims.insert(*data4);
248
249 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
250 ims.insert(*data5);
251
252 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
253 ims.insert(*data6);
254
255 shared_ptr<Data> data7 = makeData("/c/c/1");
256 ims.insert(*data7);
257
258 BOOST_CHECK_EQUAL(ims.size(), 7);
259
260 Name name("/c");
261 ims.erase(name);
262 BOOST_CHECK_EQUAL(ims.size(), 3);
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600263 BOOST_CHECK_EQUAL(ims.getCapacity(), 16);
Jiewen Tan99135962014-09-20 02:18:53 -0700264}
265
266BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages)
267{
268 shared_ptr<Data> data = makeData("/digest/compute");
269
Davide Pesavento258d51a2022-02-27 21:26:28 -0500270 auto digest1 = util::Sha256::computeDigest(data->wireEncode());
Jiewen Tan99135962014-09-20 02:18:53 -0700271 BOOST_CHECK_EQUAL(digest1->size(), 32);
272
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800273 InMemoryStorageEntry entry;
274 entry.setData(*data);
Jiewen Tan99135962014-09-20 02:18:53 -0700275 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800276 entry.getFullName()[-1].value_begin(),
277 entry.getFullName()[-1].value_end());
Jiewen Tan99135962014-09-20 02:18:53 -0700278}
279
280BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
281{
282 T ims;
283
284 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
285
286 for (int i = 0; i < 10; i++) {
287 std::ostringstream convert;
288 convert << i;
289 Name name("/" + convert.str());
290 shared_ptr<Data> data = makeData(name);
291 ims.insert(*data);
292 }
293
294 InMemoryStorage::const_iterator it = ims.begin();
295 InMemoryStorage::const_iterator tmp1 = it;
296 BOOST_REQUIRE(tmp1 == it);
297 InMemoryStorage::const_iterator tmp2 = tmp1++;
298 BOOST_REQUIRE(tmp2 != tmp1);
299 tmp2 = ++tmp1;
300 BOOST_REQUIRE(tmp2 == tmp1);
301
302 int i = 0;
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400303 for (; it != ims.end(); it++) {
Jiewen Tan99135962014-09-20 02:18:53 -0700304 std::ostringstream convert;
305 convert << i;
306 Name name("/" + convert.str());
307 BOOST_CHECK_EQUAL(it->getName(), name);
308 BOOST_CHECK_EQUAL((*it).getName(), name);
309 i++;
310 }
311}
312
313BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
314{
315 T ims;
316
317 shared_ptr<Data> data = makeData("/a");
318 ims.insert(*data);
319
320 shared_ptr<Data> data2 = makeData("/b");
321 ims.insert(*data2);
322
323 shared_ptr<Data> data3 = makeData("/c");
324 ims.insert(*data3);
325
326 shared_ptr<Data> data4 = makeData("/d");
327 ims.insert(*data4);
328
329 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
330 ims.insert(*data5);
331
332 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
333 ims.insert(*data6);
334
335 shared_ptr<Data> data7 = makeData("/c/c/1");
336 ims.insert(*data7);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100337
338 // avoid "test case [...] did not check any assertions" message from Boost.Test
339 BOOST_CHECK(true);
Jiewen Tan99135962014-09-20 02:18:53 -0700340}
341
342BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
343{
344 T ims;
345
346 shared_ptr<Data> data = makeData("/a");
347 ims.insert(*data);
348
349 shared_ptr<Data> data2 = makeData("/b");
350 ims.insert(*data2);
351
352 shared_ptr<Data> data3 = makeData("/c");
353 ims.insert(*data3);
354
355 shared_ptr<Data> data4 = makeData("/d");
356 ims.insert(*data4);
357
358 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
359 ims.insert(*data5);
360
361 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
362 ims.insert(*data6);
363
364 shared_ptr<Data> data7 = makeData("/c/c/1");
365 ims.insert(*data7);
366
Davide Pesavento258d51a2022-02-27 21:26:28 -0500367 auto digest1 = util::Sha256::computeDigest(data->wireEncode());
Jiewen Tan99135962014-09-20 02:18:53 -0700368
369 Name name("/a");
370 ims.erase(name);
371 BOOST_CHECK_EQUAL(ims.size(), 6);
372}
373
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400374using InMemoryStoragesLimited = boost::mpl::vector<InMemoryStorageFifo,
375 InMemoryStorageLfu,
376 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -0700377
Yingdi Yu404eafd2016-03-06 14:54:25 -0800378BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited)
Jiewen Tan99135962014-09-20 02:18:53 -0700379{
380 T ims;
381
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600382 ims.setCapacity(18);
383 for (int i = 1; i < 19; ++i) {
384 ims.insert(*makeData(to_string(i)));
385 }
386 BOOST_CHECK_EQUAL(ims.size(), 18);
Jiewen Tan99135962014-09-20 02:18:53 -0700387
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600388 ims.setCapacity(16);
389 BOOST_CHECK_EQUAL(ims.size(), 16);
Jiewen Tan99135962014-09-20 02:18:53 -0700390}
391
392BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
393{
394 T ims(10000);
Jiewen Tan99135962014-09-20 02:18:53 -0700395 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
396
397 T ims2(4);
Jiewen Tan99135962014-09-20 02:18:53 -0700398 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
399}
400
401BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
402{
403 T ims(40);
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600404 size_t initialCapacity = ims.getCapacity();
Jiewen Tan99135962014-09-20 02:18:53 -0700405
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600406 for (size_t i = 0; i < initialCapacity + 1; i++) {
407 shared_ptr<Data> data = makeData(to_string(i));
Davide Pesavento0f830802018-01-16 23:58:58 -0500408 data->setFreshnessPeriod(5000_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700409 signData(data);
410 ims.insert(*data);
411 }
412
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600413 BOOST_CHECK_EQUAL(ims.size(), initialCapacity + 1);
414 BOOST_CHECK_EQUAL(ims.getCapacity(), initialCapacity * 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700415}
416
417BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
418{
419 T ims(2);
420
421 Name name("/insert/1");
422 shared_ptr<Data> data = makeData(name);
423 ims.insert(*data);
424
425 Name name2("/insert/2");
426 shared_ptr<Data> data2 = makeData(name2);
427 ims.insert(*data2);
428
429 Name name3("/insert/3");
430 shared_ptr<Data> data3 = makeData(name3);
431 ims.insert(*data3);
432
433 BOOST_CHECK_EQUAL(ims.size(), 2);
434
435 shared_ptr<Interest> interest = makeInterest(name);
436 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000437 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700438}
439
Junxiao Shic542f632017-07-18 14:20:32 +0000440// 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 -0500441class FindFixture : public IoFixture
Jiewen Tan99135962014-09-20 02:18:53 -0700442{
443protected:
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800444 Name
Yingdi Yu404eafd2016-03-06 14:54:25 -0800445 insert(uint32_t id, const Name& name,
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000446 const std::function<void(Data&)>& modifyData = nullptr,
Yingdi Yu404eafd2016-03-06 14:54:25 -0800447 const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW)
Jiewen Tan99135962014-09-20 02:18:53 -0700448 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000449 auto data = makeData(name);
Davide Pesavento152ef442023-04-22 02:02:29 -0400450 data->setContent({reinterpret_cast<const uint8_t*>(&id), sizeof(id)});
Jiewen Tan99135962014-09-20 02:18:53 -0700451
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000452 if (modifyData != nullptr) {
453 modifyData(*data);
454 }
455
456 data->wireEncode();
Yingdi Yu404eafd2016-03-06 14:54:25 -0800457 m_ims.insert(*data, freshWindow);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800458
459 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700460 }
461
462 Interest&
463 startInterest(const Name& name)
464 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000465 m_interest = makeInterest(name, false);
Jiewen Tan99135962014-09-20 02:18:53 -0700466 return *m_interest;
467 }
468
469 uint32_t
470 find()
471 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500472 auto found = m_ims.find(*m_interest);
473 if (found == nullptr) {
Jiewen Tan99135962014-09-20 02:18:53 -0700474 return 0;
475 }
476 const Block& content = found->getContent();
477 if (content.value_size() != sizeof(uint32_t)) {
478 return 0;
479 }
Junxiao Shibaab7322017-07-27 01:22:49 +0000480 uint32_t id = 0;
481 std::memcpy(&id, content.value(), sizeof(id));
482 return id;
Jiewen Tan99135962014-09-20 02:18:53 -0700483 }
484
485protected:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500486 InMemoryStoragePersistent m_ims{m_io};
Jiewen Tan99135962014-09-20 02:18:53 -0700487 shared_ptr<Interest> m_interest;
488};
489
490BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
491
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800492BOOST_AUTO_TEST_CASE(ExactName)
493{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000494 insert(1, "/");
495 insert(2, "/A");
496 insert(3, "/A/B");
497 insert(4, "/A/C");
498 insert(5, "/D");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800499
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000500 startInterest("/A");
501 BOOST_CHECK_EQUAL(find(), 2);
502}
503
504BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix)
505{
506 insert(1, "/");
507 insert(2, "/A");
508 insert(3, "/A/B");
509 insert(4, "/A/C");
510 insert(5, "/D");
511
512 startInterest("/A")
513 .setCanBePrefix(true);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800514 BOOST_CHECK_EQUAL(find(), 2);
515}
516
517BOOST_AUTO_TEST_CASE(FullName)
518{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000519 Name n1 = insert(1, "/A");
520 Name n2 = insert(2, "/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800521
522 startInterest(n1);
523 BOOST_CHECK_EQUAL(find(), 1);
524
525 startInterest(n2);
526 BOOST_CHECK_EQUAL(find(), 2);
527}
528
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000529BOOST_AUTO_TEST_CASE(FullName_EmptyDataName)
Jiewen Tan99135962014-09-20 02:18:53 -0700530{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000531 Name n1 = insert(1, "/");
532 Name n2 = insert(2, "/");
Jiewen Tan99135962014-09-20 02:18:53 -0700533
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000534 startInterest(n1);
535 BOOST_CHECK_EQUAL(find(), 1);
536
537 startInterest(n2);
Jiewen Tan99135962014-09-20 02:18:53 -0700538 BOOST_CHECK_EQUAL(find(), 2);
539}
540
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000541BOOST_AUTO_TEST_CASE(PrefixName)
Jiewen Tan99135962014-09-20 02:18:53 -0700542{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000543 insert(1, "/A");
544 insert(2, "/B/p/1");
545 insert(3, "/B/p/2");
546 insert(4, "/B/q/1");
547 insert(5, "/B/q/2");
548 insert(6, "/C");
Jiewen Tan99135962014-09-20 02:18:53 -0700549
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000550 startInterest("/B")
551 .setCanBePrefix(true);
552 BOOST_CHECK_EQUAL(find(), 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700553}
554
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000555BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix)
Jiewen Tan99135962014-09-20 02:18:53 -0700556{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000557 insert(1, "/B/p/1");
Jiewen Tan99135962014-09-20 02:18:53 -0700558
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000559 startInterest("/B");
Jiewen Tan99135962014-09-20 02:18:53 -0700560 BOOST_CHECK_EQUAL(find(), 0);
561}
562
Yingdi Yu404eafd2016-03-06 14:54:25 -0800563BOOST_AUTO_TEST_CASE(MustBeFresh)
564{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000565 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
566 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); });
567 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); }, 1_s);
568 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); }, 1_h);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800569
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000570 // lookup at exact same moment as insertion is not tested because this won't happen in reality
571
572 advanceClocks(500_ms); // @500ms
573 startInterest("/A")
574 .setCanBePrefix(true)
Yingdi Yu404eafd2016-03-06 14:54:25 -0800575 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800576 BOOST_CHECK_EQUAL(find(), 3);
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000577
578 advanceClocks(1500_ms); // @2s
579 startInterest("/A")
580 .setCanBePrefix(true)
581 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800582 BOOST_CHECK_EQUAL(find(), 4);
583
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000584 advanceClocks(3500_s); // @3502s
585 startInterest("/A")
586 .setCanBePrefix(true)
587 .setMustBeFresh(true);
588 BOOST_CHECK_EQUAL(find(), 4);
589
590 advanceClocks(3500_s); // @7002s
591 startInterest("/A")
592 .setCanBePrefix(true)
593 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800594 BOOST_CHECK_EQUAL(find(), 0);
595}
596
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -0700597BOOST_AUTO_TEST_SUITE_END() // Find
Junxiao Shi85d90832016-08-04 03:19:46 +0000598BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage
Junxiao Shic542f632017-07-18 14:20:32 +0000599BOOST_AUTO_TEST_SUITE_END() // Ims
Jiewen Tan99135962014-09-20 02:18:53 -0700600
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400601} // namespace ndn::tests