blob: 594837b42c3810a1e85ca38fdf7769cb53b8d531 [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 Pesavento14c56cd2020-05-21 01:44:03 -04003 * Copyright (c) 2013-2020 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
65 uint32_t content1 = 1;
66 shared_ptr<Data> data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050067 data1->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -070068 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
69 signData(data1);
70 ims.insert(*data1);
71
72 uint32_t content2 = 2;
73 shared_ptr<Data> data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050074 data2->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -070075 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
76 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");
189 uint32_t content1 = 1;
190 shared_ptr<Data> data1 = makeData(name);
191 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
192 signData(data1);
193 ims.insert(*data1);
194
195 uint32_t content2 = 2;
196 shared_ptr<Data> data2 = makeData(name);
197 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
198 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
210 uint32_t content1 = 1;
211 shared_ptr<Data> data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500212 data1->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700213 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
214 signData(data1);
215 ims.insert(*data1);
216
217 uint32_t content2 = 2;
218 shared_ptr<Data> data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500219 data2->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700220 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
221 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
Junxiao Shic542f632017-07-18 14:20:32 +0000273 ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size());
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
279 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800280 entry.getFullName()[-1].value_begin(),
281 entry.getFullName()[-1].value_end());
Jiewen Tan99135962014-09-20 02:18:53 -0700282}
283
284BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
285{
286 T ims;
287
288 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
289
290 for (int i = 0; i < 10; i++) {
291 std::ostringstream convert;
292 convert << i;
293 Name name("/" + convert.str());
294 shared_ptr<Data> data = makeData(name);
295 ims.insert(*data);
296 }
297
298 InMemoryStorage::const_iterator it = ims.begin();
299 InMemoryStorage::const_iterator tmp1 = it;
300 BOOST_REQUIRE(tmp1 == it);
301 InMemoryStorage::const_iterator tmp2 = tmp1++;
302 BOOST_REQUIRE(tmp2 != tmp1);
303 tmp2 = ++tmp1;
304 BOOST_REQUIRE(tmp2 == tmp1);
305
306 int i = 0;
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400307 for (; it != ims.end(); it++) {
Jiewen Tan99135962014-09-20 02:18:53 -0700308 std::ostringstream convert;
309 convert << i;
310 Name name("/" + convert.str());
311 BOOST_CHECK_EQUAL(it->getName(), name);
312 BOOST_CHECK_EQUAL((*it).getName(), name);
313 i++;
314 }
315}
316
317BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
318{
319 T ims;
320
321 shared_ptr<Data> data = makeData("/a");
322 ims.insert(*data);
323
324 shared_ptr<Data> data2 = makeData("/b");
325 ims.insert(*data2);
326
327 shared_ptr<Data> data3 = makeData("/c");
328 ims.insert(*data3);
329
330 shared_ptr<Data> data4 = makeData("/d");
331 ims.insert(*data4);
332
333 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
334 ims.insert(*data5);
335
336 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
337 ims.insert(*data6);
338
339 shared_ptr<Data> data7 = makeData("/c/c/1");
340 ims.insert(*data7);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100341
342 // avoid "test case [...] did not check any assertions" message from Boost.Test
343 BOOST_CHECK(true);
Jiewen Tan99135962014-09-20 02:18:53 -0700344}
345
346BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
347{
348 T ims;
349
350 shared_ptr<Data> data = makeData("/a");
351 ims.insert(*data);
352
353 shared_ptr<Data> data2 = makeData("/b");
354 ims.insert(*data2);
355
356 shared_ptr<Data> data3 = makeData("/c");
357 ims.insert(*data3);
358
359 shared_ptr<Data> data4 = makeData("/d");
360 ims.insert(*data4);
361
362 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
363 ims.insert(*data5);
364
365 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
366 ims.insert(*data6);
367
368 shared_ptr<Data> data7 = makeData("/c/c/1");
369 ims.insert(*data7);
370
Junxiao Shic542f632017-07-18 14:20:32 +0000371 ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700372
373 Name name("/a");
374 ims.erase(name);
375 BOOST_CHECK_EQUAL(ims.size(), 6);
376}
377
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400378using InMemoryStoragesLimited = boost::mpl::vector<InMemoryStorageFifo,
379 InMemoryStorageLfu,
380 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -0700381
Yingdi Yu404eafd2016-03-06 14:54:25 -0800382BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited)
Jiewen Tan99135962014-09-20 02:18:53 -0700383{
384 T ims;
385
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600386 ims.setCapacity(18);
387 for (int i = 1; i < 19; ++i) {
388 ims.insert(*makeData(to_string(i)));
389 }
390 BOOST_CHECK_EQUAL(ims.size(), 18);
Jiewen Tan99135962014-09-20 02:18:53 -0700391
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600392 ims.setCapacity(16);
393 BOOST_CHECK_EQUAL(ims.size(), 16);
Jiewen Tan99135962014-09-20 02:18:53 -0700394}
395
396BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
397{
398 T ims(10000);
Jiewen Tan99135962014-09-20 02:18:53 -0700399 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
400
401 T ims2(4);
Jiewen Tan99135962014-09-20 02:18:53 -0700402 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
403}
404
405BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
406{
407 T ims(40);
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600408 size_t initialCapacity = ims.getCapacity();
Jiewen Tan99135962014-09-20 02:18:53 -0700409
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600410 for (size_t i = 0; i < initialCapacity + 1; i++) {
411 shared_ptr<Data> data = makeData(to_string(i));
Davide Pesavento0f830802018-01-16 23:58:58 -0500412 data->setFreshnessPeriod(5000_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700413 signData(data);
414 ims.insert(*data);
415 }
416
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600417 BOOST_CHECK_EQUAL(ims.size(), initialCapacity + 1);
418 BOOST_CHECK_EQUAL(ims.getCapacity(), initialCapacity * 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700419}
420
421BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
422{
423 T ims(2);
424
425 Name name("/insert/1");
426 shared_ptr<Data> data = makeData(name);
427 ims.insert(*data);
428
429 Name name2("/insert/2");
430 shared_ptr<Data> data2 = makeData(name2);
431 ims.insert(*data2);
432
433 Name name3("/insert/3");
434 shared_ptr<Data> data3 = makeData(name3);
435 ims.insert(*data3);
436
437 BOOST_CHECK_EQUAL(ims.size(), 2);
438
439 shared_ptr<Interest> interest = makeInterest(name);
440 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000441 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700442}
443
Junxiao Shic542f632017-07-18 14:20:32 +0000444// 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 -0500445class FindFixture : public IoFixture
Jiewen Tan99135962014-09-20 02:18:53 -0700446{
447protected:
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800448 Name
Yingdi Yu404eafd2016-03-06 14:54:25 -0800449 insert(uint32_t id, const Name& name,
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000450 const std::function<void(Data&)>& modifyData = nullptr,
Yingdi Yu404eafd2016-03-06 14:54:25 -0800451 const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW)
Jiewen Tan99135962014-09-20 02:18:53 -0700452 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000453 auto data = makeData(name);
Jiewen Tan99135962014-09-20 02:18:53 -0700454 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Jiewen Tan99135962014-09-20 02:18:53 -0700455
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000456 if (modifyData != nullptr) {
457 modifyData(*data);
458 }
459
460 data->wireEncode();
Yingdi Yu404eafd2016-03-06 14:54:25 -0800461 m_ims.insert(*data, freshWindow);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800462
463 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700464 }
465
466 Interest&
467 startInterest(const Name& name)
468 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000469 m_interest = makeInterest(name, false);
Jiewen Tan99135962014-09-20 02:18:53 -0700470 return *m_interest;
471 }
472
473 uint32_t
474 find()
475 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500476 auto found = m_ims.find(*m_interest);
477 if (found == nullptr) {
Jiewen Tan99135962014-09-20 02:18:53 -0700478 return 0;
479 }
480 const Block& content = found->getContent();
481 if (content.value_size() != sizeof(uint32_t)) {
482 return 0;
483 }
Junxiao Shibaab7322017-07-27 01:22:49 +0000484 uint32_t id = 0;
485 std::memcpy(&id, content.value(), sizeof(id));
486 return id;
Jiewen Tan99135962014-09-20 02:18:53 -0700487 }
488
489protected:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500490 InMemoryStoragePersistent m_ims{m_io};
Jiewen Tan99135962014-09-20 02:18:53 -0700491 shared_ptr<Interest> m_interest;
492};
493
494BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
495
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800496BOOST_AUTO_TEST_CASE(ExactName)
497{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000498 insert(1, "/");
499 insert(2, "/A");
500 insert(3, "/A/B");
501 insert(4, "/A/C");
502 insert(5, "/D");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800503
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000504 startInterest("/A");
505 BOOST_CHECK_EQUAL(find(), 2);
506}
507
508BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix)
509{
510 insert(1, "/");
511 insert(2, "/A");
512 insert(3, "/A/B");
513 insert(4, "/A/C");
514 insert(5, "/D");
515
516 startInterest("/A")
517 .setCanBePrefix(true);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800518 BOOST_CHECK_EQUAL(find(), 2);
519}
520
521BOOST_AUTO_TEST_CASE(FullName)
522{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000523 Name n1 = insert(1, "/A");
524 Name n2 = insert(2, "/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800525
526 startInterest(n1);
527 BOOST_CHECK_EQUAL(find(), 1);
528
529 startInterest(n2);
530 BOOST_CHECK_EQUAL(find(), 2);
531}
532
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000533BOOST_AUTO_TEST_CASE(FullName_EmptyDataName)
Jiewen Tan99135962014-09-20 02:18:53 -0700534{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000535 Name n1 = insert(1, "/");
536 Name n2 = insert(2, "/");
Jiewen Tan99135962014-09-20 02:18:53 -0700537
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000538 startInterest(n1);
539 BOOST_CHECK_EQUAL(find(), 1);
540
541 startInterest(n2);
Jiewen Tan99135962014-09-20 02:18:53 -0700542 BOOST_CHECK_EQUAL(find(), 2);
543}
544
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000545BOOST_AUTO_TEST_CASE(PrefixName)
Jiewen Tan99135962014-09-20 02:18:53 -0700546{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000547 insert(1, "/A");
548 insert(2, "/B/p/1");
549 insert(3, "/B/p/2");
550 insert(4, "/B/q/1");
551 insert(5, "/B/q/2");
552 insert(6, "/C");
Jiewen Tan99135962014-09-20 02:18:53 -0700553
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000554 startInterest("/B")
555 .setCanBePrefix(true);
556 BOOST_CHECK_EQUAL(find(), 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700557}
558
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000559BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix)
Jiewen Tan99135962014-09-20 02:18:53 -0700560{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000561 insert(1, "/B/p/1");
Jiewen Tan99135962014-09-20 02:18:53 -0700562
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000563 startInterest("/B");
Jiewen Tan99135962014-09-20 02:18:53 -0700564 BOOST_CHECK_EQUAL(find(), 0);
565}
566
Yingdi Yu404eafd2016-03-06 14:54:25 -0800567BOOST_AUTO_TEST_CASE(MustBeFresh)
568{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000569 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
570 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); });
571 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); }, 1_s);
572 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); }, 1_h);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800573
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000574 // lookup at exact same moment as insertion is not tested because this won't happen in reality
575
576 advanceClocks(500_ms); // @500ms
577 startInterest("/A")
578 .setCanBePrefix(true)
Yingdi Yu404eafd2016-03-06 14:54:25 -0800579 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800580 BOOST_CHECK_EQUAL(find(), 3);
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000581
582 advanceClocks(1500_ms); // @2s
583 startInterest("/A")
584 .setCanBePrefix(true)
585 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800586 BOOST_CHECK_EQUAL(find(), 4);
587
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000588 advanceClocks(3500_s); // @3502s
589 startInterest("/A")
590 .setCanBePrefix(true)
591 .setMustBeFresh(true);
592 BOOST_CHECK_EQUAL(find(), 4);
593
594 advanceClocks(3500_s); // @7002s
595 startInterest("/A")
596 .setCanBePrefix(true)
597 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800598 BOOST_CHECK_EQUAL(find(), 0);
599}
600
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -0700601BOOST_AUTO_TEST_SUITE_END() // Find
Junxiao Shi85d90832016-08-04 03:19:46 +0000602BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage
Junxiao Shic542f632017-07-18 14:20:32 +0000603BOOST_AUTO_TEST_SUITE_END() // Ims
Jiewen Tan99135962014-09-20 02:18:53 -0700604
Junxiao Shi85d90832016-08-04 03:19:46 +0000605} // namespace tests
Jiewen Tan99135962014-09-20 02:18:53 -0700606} // namespace ndn