blob: 737bb0eff5e71ef281a06419ae03c92d443132dd [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 Pesavento7e780642018-11-24 15:51:34 -050029#include "tests/boost-test.hpp"
30#include "tests/make-interest-data.hpp"
31#include "tests/unit/unit-test-time-fixture.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070032
Davide Pesavento5d0b0102017-10-07 13:43:16 -040033#include <boost/mpl/vector.hpp>
Jiewen Tan99135962014-09-20 02:18:53 -070034
35namespace ndn {
Junxiao Shi85d90832016-08-04 03:19:46 +000036namespace tests {
Jiewen Tan99135962014-09-20 02:18:53 -070037
Junxiao Shi85d90832016-08-04 03:19:46 +000038using namespace ndn::tests;
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070039
Junxiao Shic542f632017-07-18 14:20:32 +000040BOOST_AUTO_TEST_SUITE(Ims)
Junxiao Shi85d90832016-08-04 03:19:46 +000041BOOST_AUTO_TEST_SUITE(TestInMemoryStorage)
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070042
Davide Pesavento5d0b0102017-10-07 13:43:16 -040043using InMemoryStorages = boost::mpl::vector<InMemoryStoragePersistent,
44 InMemoryStorageFifo,
45 InMemoryStorageLfu,
46 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -070047
Jiewen Tan99135962014-09-20 02:18:53 -070048BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages)
49{
50 T ims;
51
Jiewen Tan99135962014-09-20 02:18:53 -070052 ims.insert(*makeData("/a"));
53 ims.insert(*makeData("/b"));
54 ims.insert(*makeData("/c"));
55 ims.insert(*makeData("/d"));
56
57 BOOST_CHECK_EQUAL(ims.size(), 4);
58}
59
Davide Pesaventoeee3e822016-11-26 19:19:34 +010060BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -070061{
62 T ims;
63
64 Name name("/a");
65
66 uint32_t content1 = 1;
67 shared_ptr<Data> data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050068 data1->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -070069 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
70 signData(data1);
71 ims.insert(*data1);
72
73 uint32_t content2 = 2;
74 shared_ptr<Data> data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050075 data2->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -070076 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
77 signData(data2);
78 ims.insert(*data2);
79
80 BOOST_CHECK_EQUAL(ims.size(), 2);
81}
82
83BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion, T, InMemoryStorages)
84{
85 T ims;
86
87 shared_ptr<Data> data0 = makeData("/insert/smth");
88 ims.insert(*data0);
89
90 shared_ptr<Data> data = makeData("/insert/duplicate");
91 ims.insert(*data);
92
93 ims.insert(*data);
94 BOOST_CHECK_EQUAL(ims.size(), 2);
95}
96
97BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion2, T, InMemoryStorages)
98{
99 T ims;
100
101 shared_ptr<Data> data = makeData("/insert/duplicate");
102 ims.insert(*data);
103
104 ims.insert(*data);
105 BOOST_CHECK_EQUAL(ims.size(), 1);
106
107 shared_ptr<Data> data2 = makeData("/insert/original");
108 ims.insert(*data2);
109 BOOST_CHECK_EQUAL(ims.size(), 2);
110}
111
112BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFind, T, InMemoryStorages)
113{
114 T ims;
115
116 Name name("/insert/and/find");
117
118 shared_ptr<Data> data = makeData(name);
119 ims.insert(*data);
120
121 shared_ptr<Interest> interest = makeInterest(name);
122
123 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000124 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700125 BOOST_CHECK_EQUAL(data->getName(), found->getName());
126}
127
128BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages)
129{
130 T ims;
131
132 Name name("/insert/and/find");
133 shared_ptr<Data> data = makeData(name);
134 ims.insert(*data);
135
136 Name name2("/not/find");
137 shared_ptr<Interest> interest = makeInterest(name2);
138
139 shared_ptr<const Data> found = ims.find(*interest);
140
141 BOOST_CHECK_EQUAL(found.get(), static_cast<const Data*>(0));
142}
143
144BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages)
145{
146 T ims;
147
148 Name name("/insert/and/find");
149
150 shared_ptr<Data> data = makeData(name);
151 ims.insert(*data);
152
153 shared_ptr<const Data> found = ims.find(name);
Junxiao Shi85d90832016-08-04 03:19:46 +0000154 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700155 BOOST_CHECK_EQUAL(data->getName(), found->getName());
156}
157
158BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages)
159{
160 T ims;
161
162 Name name("/insert/and/find");
163
164 shared_ptr<Data> data = makeData(name);
165 ims.insert(*data);
166
167 shared_ptr<const Data> found = ims.find(data->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000168 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700169 BOOST_CHECK_EQUAL(data->getFullName(), found->getFullName());
170}
171
172BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByName, T, InMemoryStorages)
173{
174 T ims;
175
176 Name name("/insert/and/find");
177 shared_ptr<Data> data = makeData(name);
178 ims.insert(*data);
179
180 Name name2("/not/find");
181
182 shared_ptr<const Data> found = ims.find(name2);
Junxiao Shi85d90832016-08-04 03:19:46 +0000183 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700184}
185
186BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByFullName, T, InMemoryStorages)
187{
188 T ims;
189
190 Name name("/a");
191 uint32_t content1 = 1;
192 shared_ptr<Data> data1 = makeData(name);
193 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
194 signData(data1);
195 ims.insert(*data1);
196
197 uint32_t content2 = 2;
198 shared_ptr<Data> data2 = makeData(name);
199 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
200 signData(data2);
201
202 shared_ptr<const Data> found = ims.find(data2->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000203 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700204}
205
206BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages)
207{
208 T ims;
209
210 Name name("/insertandremovebyname");
211
212 uint32_t content1 = 1;
213 shared_ptr<Data> data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500214 data1->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700215 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
216 signData(data1);
217 ims.insert(*data1);
218
219 uint32_t content2 = 2;
220 shared_ptr<Data> data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500221 data2->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700222 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
223 signData(data2);
224 ims.insert(*data2);
225
226 shared_ptr<Data> data3 = makeData("/insertandremovebyname/1");
227 ims.insert(*data3);
228
229 shared_ptr<Data> data4 = makeData("/insertandremovebyname/2");
230 ims.insert(*data4);
231
232 BOOST_CHECK_EQUAL(ims.size(), 4);
233
234 ims.erase(data1->getFullName(), false);
235 BOOST_CHECK_EQUAL(ims.size(), 3);
236}
237
238BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages)
239{
240 T ims;
241
242 shared_ptr<Data> data = makeData("/a");
243 ims.insert(*data);
244
245 shared_ptr<Data> data2 = makeData("/b");
246 ims.insert(*data2);
247
248 shared_ptr<Data> data3 = makeData("/c");
249 ims.insert(*data3);
250
251 shared_ptr<Data> data4 = makeData("/d");
252 ims.insert(*data4);
253
254 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
255 ims.insert(*data5);
256
257 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
258 ims.insert(*data6);
259
260 shared_ptr<Data> data7 = makeData("/c/c/1");
261 ims.insert(*data7);
262
263 BOOST_CHECK_EQUAL(ims.size(), 7);
264
265 Name name("/c");
266 ims.erase(name);
267 BOOST_CHECK_EQUAL(ims.size(), 3);
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600268 BOOST_CHECK_EQUAL(ims.getCapacity(), 16);
Jiewen Tan99135962014-09-20 02:18:53 -0700269}
270
271BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages)
272{
273 shared_ptr<Data> data = makeData("/digest/compute");
274
Junxiao Shic542f632017-07-18 14:20:32 +0000275 ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700276 BOOST_CHECK_EQUAL(digest1->size(), 32);
277
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800278 InMemoryStorageEntry entry;
279 entry.setData(*data);
Jiewen Tan99135962014-09-20 02:18:53 -0700280
281 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800282 entry.getFullName()[-1].value_begin(),
283 entry.getFullName()[-1].value_end());
Jiewen Tan99135962014-09-20 02:18:53 -0700284}
285
286BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
287{
288 T ims;
289
290 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
291
292 for (int i = 0; i < 10; i++) {
293 std::ostringstream convert;
294 convert << i;
295 Name name("/" + convert.str());
296 shared_ptr<Data> data = makeData(name);
297 ims.insert(*data);
298 }
299
300 InMemoryStorage::const_iterator it = ims.begin();
301 InMemoryStorage::const_iterator tmp1 = it;
302 BOOST_REQUIRE(tmp1 == it);
303 InMemoryStorage::const_iterator tmp2 = tmp1++;
304 BOOST_REQUIRE(tmp2 != tmp1);
305 tmp2 = ++tmp1;
306 BOOST_REQUIRE(tmp2 == tmp1);
307
308 int i = 0;
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400309 for (; it != ims.end(); it++) {
Jiewen Tan99135962014-09-20 02:18:53 -0700310 std::ostringstream convert;
311 convert << i;
312 Name name("/" + convert.str());
313 BOOST_CHECK_EQUAL(it->getName(), name);
314 BOOST_CHECK_EQUAL((*it).getName(), name);
315 i++;
316 }
317}
318
319BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
320{
321 T ims;
322
323 shared_ptr<Data> data = makeData("/a");
324 ims.insert(*data);
325
326 shared_ptr<Data> data2 = makeData("/b");
327 ims.insert(*data2);
328
329 shared_ptr<Data> data3 = makeData("/c");
330 ims.insert(*data3);
331
332 shared_ptr<Data> data4 = makeData("/d");
333 ims.insert(*data4);
334
335 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
336 ims.insert(*data5);
337
338 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
339 ims.insert(*data6);
340
341 shared_ptr<Data> data7 = makeData("/c/c/1");
342 ims.insert(*data7);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100343
344 // avoid "test case [...] did not check any assertions" message from Boost.Test
345 BOOST_CHECK(true);
Jiewen Tan99135962014-09-20 02:18:53 -0700346}
347
348BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
349{
350 T ims;
351
352 shared_ptr<Data> data = makeData("/a");
353 ims.insert(*data);
354
355 shared_ptr<Data> data2 = makeData("/b");
356 ims.insert(*data2);
357
358 shared_ptr<Data> data3 = makeData("/c");
359 ims.insert(*data3);
360
361 shared_ptr<Data> data4 = makeData("/d");
362 ims.insert(*data4);
363
364 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
365 ims.insert(*data5);
366
367 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
368 ims.insert(*data6);
369
370 shared_ptr<Data> data7 = makeData("/c/c/1");
371 ims.insert(*data7);
372
Junxiao Shic542f632017-07-18 14:20:32 +0000373 ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700374
375 Name name("/a");
376 ims.erase(name);
377 BOOST_CHECK_EQUAL(ims.size(), 6);
378}
379
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400380using InMemoryStoragesLimited = boost::mpl::vector<InMemoryStorageFifo,
381 InMemoryStorageLfu,
382 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -0700383
Yingdi Yu404eafd2016-03-06 14:54:25 -0800384BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited)
Jiewen Tan99135962014-09-20 02:18:53 -0700385{
386 T ims;
387
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600388 ims.setCapacity(18);
389 for (int i = 1; i < 19; ++i) {
390 ims.insert(*makeData(to_string(i)));
391 }
392 BOOST_CHECK_EQUAL(ims.size(), 18);
Jiewen Tan99135962014-09-20 02:18:53 -0700393
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600394 ims.setCapacity(16);
395 BOOST_CHECK_EQUAL(ims.size(), 16);
Jiewen Tan99135962014-09-20 02:18:53 -0700396}
397
398BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
399{
400 T ims(10000);
Jiewen Tan99135962014-09-20 02:18:53 -0700401 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
402
403 T ims2(4);
Jiewen Tan99135962014-09-20 02:18:53 -0700404 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
405}
406
407BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
408{
409 T ims(40);
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600410 size_t initialCapacity = ims.getCapacity();
Jiewen Tan99135962014-09-20 02:18:53 -0700411
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600412 for (size_t i = 0; i < initialCapacity + 1; i++) {
413 shared_ptr<Data> data = makeData(to_string(i));
Davide Pesavento0f830802018-01-16 23:58:58 -0500414 data->setFreshnessPeriod(5000_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700415 signData(data);
416 ims.insert(*data);
417 }
418
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600419 BOOST_CHECK_EQUAL(ims.size(), initialCapacity + 1);
420 BOOST_CHECK_EQUAL(ims.getCapacity(), initialCapacity * 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700421}
422
423BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
424{
425 T ims(2);
426
427 Name name("/insert/1");
428 shared_ptr<Data> data = makeData(name);
429 ims.insert(*data);
430
431 Name name2("/insert/2");
432 shared_ptr<Data> data2 = makeData(name2);
433 ims.insert(*data2);
434
435 Name name3("/insert/3");
436 shared_ptr<Data> data3 = makeData(name3);
437 ims.insert(*data3);
438
439 BOOST_CHECK_EQUAL(ims.size(), 2);
440
441 shared_ptr<Interest> interest = makeInterest(name);
442 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000443 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700444}
445
Junxiao Shic542f632017-07-18 14:20:32 +0000446// Find function is implemented at the base case, so it's sufficient to test for one derived class.
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800447class FindFixture : public tests::UnitTestTimeFixture
Jiewen Tan99135962014-09-20 02:18:53 -0700448{
449protected:
Yingdi Yu404eafd2016-03-06 14:54:25 -0800450 FindFixture()
451 : m_ims(io)
452 {
453 }
454
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800455 Name
Yingdi Yu404eafd2016-03-06 14:54:25 -0800456 insert(uint32_t id, const Name& name,
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000457 const std::function<void(Data&)>& modifyData = nullptr,
Yingdi Yu404eafd2016-03-06 14:54:25 -0800458 const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW)
Jiewen Tan99135962014-09-20 02:18:53 -0700459 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000460 auto data = makeData(name);
Jiewen Tan99135962014-09-20 02:18:53 -0700461 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Jiewen Tan99135962014-09-20 02:18:53 -0700462
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000463 if (modifyData != nullptr) {
464 modifyData(*data);
465 }
466
467 data->wireEncode();
Yingdi Yu404eafd2016-03-06 14:54:25 -0800468 m_ims.insert(*data, freshWindow);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800469
470 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700471 }
472
473 Interest&
474 startInterest(const Name& name)
475 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000476 m_interest = makeInterest(name, false);
Jiewen Tan99135962014-09-20 02:18:53 -0700477 return *m_interest;
478 }
479
480 uint32_t
481 find()
482 {
483 shared_ptr<const Data> found = m_ims.find(*m_interest);
484 if (found == 0) {
485 return 0;
486 }
487 const Block& content = found->getContent();
488 if (content.value_size() != sizeof(uint32_t)) {
489 return 0;
490 }
Junxiao Shibaab7322017-07-27 01:22:49 +0000491 uint32_t id = 0;
492 std::memcpy(&id, content.value(), sizeof(id));
493 return id;
Jiewen Tan99135962014-09-20 02:18:53 -0700494 }
495
496protected:
497 InMemoryStoragePersistent m_ims;
498 shared_ptr<Interest> m_interest;
499};
500
501BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
502
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800503BOOST_AUTO_TEST_CASE(ExactName)
504{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000505 insert(1, "/");
506 insert(2, "/A");
507 insert(3, "/A/B");
508 insert(4, "/A/C");
509 insert(5, "/D");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800510
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000511 startInterest("/A");
512 BOOST_CHECK_EQUAL(find(), 2);
513}
514
515BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix)
516{
517 insert(1, "/");
518 insert(2, "/A");
519 insert(3, "/A/B");
520 insert(4, "/A/C");
521 insert(5, "/D");
522
523 startInterest("/A")
524 .setCanBePrefix(true);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800525 BOOST_CHECK_EQUAL(find(), 2);
526}
527
528BOOST_AUTO_TEST_CASE(FullName)
529{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000530 Name n1 = insert(1, "/A");
531 Name n2 = insert(2, "/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800532
533 startInterest(n1);
534 BOOST_CHECK_EQUAL(find(), 1);
535
536 startInterest(n2);
537 BOOST_CHECK_EQUAL(find(), 2);
538}
539
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000540BOOST_AUTO_TEST_CASE(FullName_EmptyDataName)
Jiewen Tan99135962014-09-20 02:18:53 -0700541{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000542 Name n1 = insert(1, "/");
543 Name n2 = insert(2, "/");
Jiewen Tan99135962014-09-20 02:18:53 -0700544
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000545 startInterest(n1);
546 BOOST_CHECK_EQUAL(find(), 1);
547
548 startInterest(n2);
Jiewen Tan99135962014-09-20 02:18:53 -0700549 BOOST_CHECK_EQUAL(find(), 2);
550}
551
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000552BOOST_AUTO_TEST_CASE(PrefixName)
Jiewen Tan99135962014-09-20 02:18:53 -0700553{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000554 insert(1, "/A");
555 insert(2, "/B/p/1");
556 insert(3, "/B/p/2");
557 insert(4, "/B/q/1");
558 insert(5, "/B/q/2");
559 insert(6, "/C");
Jiewen Tan99135962014-09-20 02:18:53 -0700560
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000561 startInterest("/B")
562 .setCanBePrefix(true);
563 BOOST_CHECK_EQUAL(find(), 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700564}
565
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000566BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix)
Jiewen Tan99135962014-09-20 02:18:53 -0700567{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000568 insert(1, "/B/p/1");
Jiewen Tan99135962014-09-20 02:18:53 -0700569
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000570 startInterest("/B");
Jiewen Tan99135962014-09-20 02:18:53 -0700571 BOOST_CHECK_EQUAL(find(), 0);
572}
573
Yingdi Yu404eafd2016-03-06 14:54:25 -0800574BOOST_AUTO_TEST_CASE(MustBeFresh)
575{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000576 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
577 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); });
578 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); }, 1_s);
579 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); }, 1_h);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800580
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000581 // lookup at exact same moment as insertion is not tested because this won't happen in reality
582
583 advanceClocks(500_ms); // @500ms
584 startInterest("/A")
585 .setCanBePrefix(true)
Yingdi Yu404eafd2016-03-06 14:54:25 -0800586 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800587 BOOST_CHECK_EQUAL(find(), 3);
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000588
589 advanceClocks(1500_ms); // @2s
590 startInterest("/A")
591 .setCanBePrefix(true)
592 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800593 BOOST_CHECK_EQUAL(find(), 4);
594
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000595 advanceClocks(3500_s); // @3502s
596 startInterest("/A")
597 .setCanBePrefix(true)
598 .setMustBeFresh(true);
599 BOOST_CHECK_EQUAL(find(), 4);
600
601 advanceClocks(3500_s); // @7002s
602 startInterest("/A")
603 .setCanBePrefix(true)
604 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800605 BOOST_CHECK_EQUAL(find(), 0);
606}
607
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -0700608BOOST_AUTO_TEST_SUITE_END() // Find
Junxiao Shi85d90832016-08-04 03:19:46 +0000609BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage
Junxiao Shic542f632017-07-18 14:20:32 +0000610BOOST_AUTO_TEST_SUITE_END() // Ims
Jiewen Tan99135962014-09-20 02:18:53 -0700611
Junxiao Shi85d90832016-08-04 03:19:46 +0000612} // namespace tests
Jiewen Tan99135962014-09-20 02:18:53 -0700613} // namespace ndn