blob: ca1fc357641ce2d2a2153940a1164bdf64a0bd7f [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/*
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +00003 * Copyright (c) 2013-2019 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"
27#include "ndn-cxx/security/signature-sha256-with-rsa.hpp"
28#include "ndn-cxx/util/sha256.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070029
Davide Pesavento7e780642018-11-24 15:51:34 -050030#include "tests/boost-test.hpp"
31#include "tests/make-interest-data.hpp"
32#include "tests/unit/unit-test-time-fixture.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070033
Davide Pesavento5d0b0102017-10-07 13:43:16 -040034#include <boost/mpl/vector.hpp>
Jiewen Tan99135962014-09-20 02:18:53 -070035
36namespace ndn {
Junxiao Shi85d90832016-08-04 03:19:46 +000037namespace tests {
Jiewen Tan99135962014-09-20 02:18:53 -070038
Junxiao Shi85d90832016-08-04 03:19:46 +000039using namespace ndn::tests;
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070040
Junxiao Shic542f632017-07-18 14:20:32 +000041BOOST_AUTO_TEST_SUITE(Ims)
Junxiao Shi85d90832016-08-04 03:19:46 +000042BOOST_AUTO_TEST_SUITE(TestInMemoryStorage)
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070043
Davide Pesavento5d0b0102017-10-07 13:43:16 -040044using InMemoryStorages = boost::mpl::vector<InMemoryStoragePersistent,
45 InMemoryStorageFifo,
46 InMemoryStorageLfu,
47 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -070048
Jiewen Tan99135962014-09-20 02:18:53 -070049BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages)
50{
51 T ims;
52
Jiewen Tan99135962014-09-20 02:18:53 -070053 ims.insert(*makeData("/a"));
54 ims.insert(*makeData("/b"));
55 ims.insert(*makeData("/c"));
56 ims.insert(*makeData("/d"));
57
58 BOOST_CHECK_EQUAL(ims.size(), 4);
59}
60
Davide Pesaventoeee3e822016-11-26 19:19:34 +010061BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -070062{
63 T ims;
64
65 Name name("/a");
66
67 uint32_t content1 = 1;
68 shared_ptr<Data> data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050069 data1->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -070070 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
71 signData(data1);
72 ims.insert(*data1);
73
74 uint32_t content2 = 2;
75 shared_ptr<Data> data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -050076 data2->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -070077 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
78 signData(data2);
79 ims.insert(*data2);
80
81 BOOST_CHECK_EQUAL(ims.size(), 2);
82}
83
84BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion, T, InMemoryStorages)
85{
86 T ims;
87
88 shared_ptr<Data> data0 = makeData("/insert/smth");
89 ims.insert(*data0);
90
91 shared_ptr<Data> data = makeData("/insert/duplicate");
92 ims.insert(*data);
93
94 ims.insert(*data);
95 BOOST_CHECK_EQUAL(ims.size(), 2);
96}
97
98BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion2, T, InMemoryStorages)
99{
100 T ims;
101
102 shared_ptr<Data> data = makeData("/insert/duplicate");
103 ims.insert(*data);
104
105 ims.insert(*data);
106 BOOST_CHECK_EQUAL(ims.size(), 1);
107
108 shared_ptr<Data> data2 = makeData("/insert/original");
109 ims.insert(*data2);
110 BOOST_CHECK_EQUAL(ims.size(), 2);
111}
112
113BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFind, T, InMemoryStorages)
114{
115 T ims;
116
117 Name name("/insert/and/find");
118
119 shared_ptr<Data> data = makeData(name);
120 ims.insert(*data);
121
122 shared_ptr<Interest> interest = makeInterest(name);
123
124 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000125 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700126 BOOST_CHECK_EQUAL(data->getName(), found->getName());
127}
128
129BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages)
130{
131 T ims;
132
133 Name name("/insert/and/find");
134 shared_ptr<Data> data = makeData(name);
135 ims.insert(*data);
136
137 Name name2("/not/find");
138 shared_ptr<Interest> interest = makeInterest(name2);
139
140 shared_ptr<const Data> found = ims.find(*interest);
141
142 BOOST_CHECK_EQUAL(found.get(), static_cast<const Data*>(0));
143}
144
145BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages)
146{
147 T ims;
148
149 Name name("/insert/and/find");
150
151 shared_ptr<Data> data = makeData(name);
152 ims.insert(*data);
153
154 shared_ptr<const Data> found = ims.find(name);
Junxiao Shi85d90832016-08-04 03:19:46 +0000155 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700156 BOOST_CHECK_EQUAL(data->getName(), found->getName());
157}
158
159BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages)
160{
161 T ims;
162
163 Name name("/insert/and/find");
164
165 shared_ptr<Data> data = makeData(name);
166 ims.insert(*data);
167
168 shared_ptr<const Data> found = ims.find(data->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000169 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700170 BOOST_CHECK_EQUAL(data->getFullName(), found->getFullName());
171}
172
173BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByName, T, InMemoryStorages)
174{
175 T ims;
176
177 Name name("/insert/and/find");
178 shared_ptr<Data> data = makeData(name);
179 ims.insert(*data);
180
181 Name name2("/not/find");
182
183 shared_ptr<const Data> found = ims.find(name2);
Junxiao Shi85d90832016-08-04 03:19:46 +0000184 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700185}
186
187BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByFullName, T, InMemoryStorages)
188{
189 T ims;
190
191 Name name("/a");
192 uint32_t content1 = 1;
193 shared_ptr<Data> data1 = makeData(name);
194 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
195 signData(data1);
196 ims.insert(*data1);
197
198 uint32_t content2 = 2;
199 shared_ptr<Data> data2 = makeData(name);
200 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
201 signData(data2);
202
203 shared_ptr<const Data> found = ims.find(data2->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000204 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700205}
206
207BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages)
208{
209 T ims;
210
211 Name name("/insertandremovebyname");
212
213 uint32_t content1 = 1;
214 shared_ptr<Data> data1 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500215 data1->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700216 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
217 signData(data1);
218 ims.insert(*data1);
219
220 uint32_t content2 = 2;
221 shared_ptr<Data> data2 = makeData(name);
Davide Pesavento0f830802018-01-16 23:58:58 -0500222 data2->setFreshnessPeriod(99999_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700223 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
224 signData(data2);
225 ims.insert(*data2);
226
227 shared_ptr<Data> data3 = makeData("/insertandremovebyname/1");
228 ims.insert(*data3);
229
230 shared_ptr<Data> data4 = makeData("/insertandremovebyname/2");
231 ims.insert(*data4);
232
233 BOOST_CHECK_EQUAL(ims.size(), 4);
234
235 ims.erase(data1->getFullName(), false);
236 BOOST_CHECK_EQUAL(ims.size(), 3);
237}
238
239BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages)
240{
241 T ims;
242
243 shared_ptr<Data> data = makeData("/a");
244 ims.insert(*data);
245
246 shared_ptr<Data> data2 = makeData("/b");
247 ims.insert(*data2);
248
249 shared_ptr<Data> data3 = makeData("/c");
250 ims.insert(*data3);
251
252 shared_ptr<Data> data4 = makeData("/d");
253 ims.insert(*data4);
254
255 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
256 ims.insert(*data5);
257
258 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
259 ims.insert(*data6);
260
261 shared_ptr<Data> data7 = makeData("/c/c/1");
262 ims.insert(*data7);
263
264 BOOST_CHECK_EQUAL(ims.size(), 7);
265
266 Name name("/c");
267 ims.erase(name);
268 BOOST_CHECK_EQUAL(ims.size(), 3);
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600269 BOOST_CHECK_EQUAL(ims.getCapacity(), 16);
Jiewen Tan99135962014-09-20 02:18:53 -0700270}
271
272BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages)
273{
274 shared_ptr<Data> data = makeData("/digest/compute");
275
Junxiao Shic542f632017-07-18 14:20:32 +0000276 ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700277 BOOST_CHECK_EQUAL(digest1->size(), 32);
278
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800279 InMemoryStorageEntry entry;
280 entry.setData(*data);
Jiewen Tan99135962014-09-20 02:18:53 -0700281
282 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800283 entry.getFullName()[-1].value_begin(),
284 entry.getFullName()[-1].value_end());
Jiewen Tan99135962014-09-20 02:18:53 -0700285}
286
287BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
288{
289 T ims;
290
291 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
292
293 for (int i = 0; i < 10; i++) {
294 std::ostringstream convert;
295 convert << i;
296 Name name("/" + convert.str());
297 shared_ptr<Data> data = makeData(name);
298 ims.insert(*data);
299 }
300
301 InMemoryStorage::const_iterator it = ims.begin();
302 InMemoryStorage::const_iterator tmp1 = it;
303 BOOST_REQUIRE(tmp1 == it);
304 InMemoryStorage::const_iterator tmp2 = tmp1++;
305 BOOST_REQUIRE(tmp2 != tmp1);
306 tmp2 = ++tmp1;
307 BOOST_REQUIRE(tmp2 == tmp1);
308
309 int i = 0;
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400310 for (; it != ims.end(); it++) {
Jiewen Tan99135962014-09-20 02:18:53 -0700311 std::ostringstream convert;
312 convert << i;
313 Name name("/" + convert.str());
314 BOOST_CHECK_EQUAL(it->getName(), name);
315 BOOST_CHECK_EQUAL((*it).getName(), name);
316 i++;
317 }
318}
319
320BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
321{
322 T ims;
323
324 shared_ptr<Data> data = makeData("/a");
325 ims.insert(*data);
326
327 shared_ptr<Data> data2 = makeData("/b");
328 ims.insert(*data2);
329
330 shared_ptr<Data> data3 = makeData("/c");
331 ims.insert(*data3);
332
333 shared_ptr<Data> data4 = makeData("/d");
334 ims.insert(*data4);
335
336 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
337 ims.insert(*data5);
338
339 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
340 ims.insert(*data6);
341
342 shared_ptr<Data> data7 = makeData("/c/c/1");
343 ims.insert(*data7);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100344
345 // avoid "test case [...] did not check any assertions" message from Boost.Test
346 BOOST_CHECK(true);
Jiewen Tan99135962014-09-20 02:18:53 -0700347}
348
349BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
350{
351 T ims;
352
353 shared_ptr<Data> data = makeData("/a");
354 ims.insert(*data);
355
356 shared_ptr<Data> data2 = makeData("/b");
357 ims.insert(*data2);
358
359 shared_ptr<Data> data3 = makeData("/c");
360 ims.insert(*data3);
361
362 shared_ptr<Data> data4 = makeData("/d");
363 ims.insert(*data4);
364
365 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
366 ims.insert(*data5);
367
368 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
369 ims.insert(*data6);
370
371 shared_ptr<Data> data7 = makeData("/c/c/1");
372 ims.insert(*data7);
373
Junxiao Shic542f632017-07-18 14:20:32 +0000374 ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700375
376 Name name("/a");
377 ims.erase(name);
378 BOOST_CHECK_EQUAL(ims.size(), 6);
379}
380
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400381using InMemoryStoragesLimited = boost::mpl::vector<InMemoryStorageFifo,
382 InMemoryStorageLfu,
383 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -0700384
Yingdi Yu404eafd2016-03-06 14:54:25 -0800385BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited)
Jiewen Tan99135962014-09-20 02:18:53 -0700386{
387 T ims;
388
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600389 ims.setCapacity(18);
390 for (int i = 1; i < 19; ++i) {
391 ims.insert(*makeData(to_string(i)));
392 }
393 BOOST_CHECK_EQUAL(ims.size(), 18);
Jiewen Tan99135962014-09-20 02:18:53 -0700394
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600395 ims.setCapacity(16);
396 BOOST_CHECK_EQUAL(ims.size(), 16);
Jiewen Tan99135962014-09-20 02:18:53 -0700397}
398
399BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
400{
401 T ims(10000);
Jiewen Tan99135962014-09-20 02:18:53 -0700402 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
403
404 T ims2(4);
Jiewen Tan99135962014-09-20 02:18:53 -0700405 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
406}
407
408BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
409{
410 T ims(40);
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600411 size_t initialCapacity = ims.getCapacity();
Jiewen Tan99135962014-09-20 02:18:53 -0700412
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600413 for (size_t i = 0; i < initialCapacity + 1; i++) {
414 shared_ptr<Data> data = makeData(to_string(i));
Davide Pesavento0f830802018-01-16 23:58:58 -0500415 data->setFreshnessPeriod(5000_ms);
Jiewen Tan99135962014-09-20 02:18:53 -0700416 signData(data);
417 ims.insert(*data);
418 }
419
Ashlesh Gawande1bbce6d2018-11-13 15:31:04 -0600420 BOOST_CHECK_EQUAL(ims.size(), initialCapacity + 1);
421 BOOST_CHECK_EQUAL(ims.getCapacity(), initialCapacity * 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700422}
423
424BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
425{
426 T ims(2);
427
428 Name name("/insert/1");
429 shared_ptr<Data> data = makeData(name);
430 ims.insert(*data);
431
432 Name name2("/insert/2");
433 shared_ptr<Data> data2 = makeData(name2);
434 ims.insert(*data2);
435
436 Name name3("/insert/3");
437 shared_ptr<Data> data3 = makeData(name3);
438 ims.insert(*data3);
439
440 BOOST_CHECK_EQUAL(ims.size(), 2);
441
442 shared_ptr<Interest> interest = makeInterest(name);
443 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000444 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700445}
446
Junxiao Shic542f632017-07-18 14:20:32 +0000447// 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 -0800448class FindFixture : public tests::UnitTestTimeFixture
Jiewen Tan99135962014-09-20 02:18:53 -0700449{
450protected:
Yingdi Yu404eafd2016-03-06 14:54:25 -0800451 FindFixture()
452 : m_ims(io)
453 {
454 }
455
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800456 Name
Yingdi Yu404eafd2016-03-06 14:54:25 -0800457 insert(uint32_t id, const Name& name,
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000458 const std::function<void(Data&)>& modifyData = nullptr,
Yingdi Yu404eafd2016-03-06 14:54:25 -0800459 const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW)
Jiewen Tan99135962014-09-20 02:18:53 -0700460 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000461 auto data = makeData(name);
Jiewen Tan99135962014-09-20 02:18:53 -0700462 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Jiewen Tan99135962014-09-20 02:18:53 -0700463
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000464 if (modifyData != nullptr) {
465 modifyData(*data);
466 }
467
468 data->wireEncode();
Yingdi Yu404eafd2016-03-06 14:54:25 -0800469 m_ims.insert(*data, freshWindow);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800470
471 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700472 }
473
474 Interest&
475 startInterest(const Name& name)
476 {
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000477 m_interest = makeInterest(name, false);
Jiewen Tan99135962014-09-20 02:18:53 -0700478 return *m_interest;
479 }
480
481 uint32_t
482 find()
483 {
484 shared_ptr<const Data> found = m_ims.find(*m_interest);
485 if (found == 0) {
486 return 0;
487 }
488 const Block& content = found->getContent();
489 if (content.value_size() != sizeof(uint32_t)) {
490 return 0;
491 }
Junxiao Shibaab7322017-07-27 01:22:49 +0000492 uint32_t id = 0;
493 std::memcpy(&id, content.value(), sizeof(id));
494 return id;
Jiewen Tan99135962014-09-20 02:18:53 -0700495 }
496
497protected:
498 InMemoryStoragePersistent m_ims;
499 shared_ptr<Interest> m_interest;
500};
501
502BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
503
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800504BOOST_AUTO_TEST_CASE(ExactName)
505{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000506 insert(1, "/");
507 insert(2, "/A");
508 insert(3, "/A/B");
509 insert(4, "/A/C");
510 insert(5, "/D");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800511
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000512 startInterest("/A");
513 BOOST_CHECK_EQUAL(find(), 2);
514}
515
516BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix)
517{
518 insert(1, "/");
519 insert(2, "/A");
520 insert(3, "/A/B");
521 insert(4, "/A/C");
522 insert(5, "/D");
523
524 startInterest("/A")
525 .setCanBePrefix(true);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800526 BOOST_CHECK_EQUAL(find(), 2);
527}
528
529BOOST_AUTO_TEST_CASE(FullName)
530{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000531 Name n1 = insert(1, "/A");
532 Name n2 = insert(2, "/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800533
534 startInterest(n1);
535 BOOST_CHECK_EQUAL(find(), 1);
536
537 startInterest(n2);
538 BOOST_CHECK_EQUAL(find(), 2);
539}
540
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000541BOOST_AUTO_TEST_CASE(FullName_EmptyDataName)
Jiewen Tan99135962014-09-20 02:18:53 -0700542{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000543 Name n1 = insert(1, "/");
544 Name n2 = insert(2, "/");
Jiewen Tan99135962014-09-20 02:18:53 -0700545
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000546 startInterest(n1);
547 BOOST_CHECK_EQUAL(find(), 1);
548
549 startInterest(n2);
Jiewen Tan99135962014-09-20 02:18:53 -0700550 BOOST_CHECK_EQUAL(find(), 2);
551}
552
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000553BOOST_AUTO_TEST_CASE(PrefixName)
Jiewen Tan99135962014-09-20 02:18:53 -0700554{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000555 insert(1, "/A");
556 insert(2, "/B/p/1");
557 insert(3, "/B/p/2");
558 insert(4, "/B/q/1");
559 insert(5, "/B/q/2");
560 insert(6, "/C");
Jiewen Tan99135962014-09-20 02:18:53 -0700561
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000562 startInterest("/B")
563 .setCanBePrefix(true);
564 BOOST_CHECK_EQUAL(find(), 2);
Jiewen Tan99135962014-09-20 02:18:53 -0700565}
566
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000567BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix)
Jiewen Tan99135962014-09-20 02:18:53 -0700568{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000569 insert(1, "/B/p/1");
Jiewen Tan99135962014-09-20 02:18:53 -0700570
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000571 startInterest("/B");
Jiewen Tan99135962014-09-20 02:18:53 -0700572 BOOST_CHECK_EQUAL(find(), 0);
573}
574
Yingdi Yu404eafd2016-03-06 14:54:25 -0800575BOOST_AUTO_TEST_CASE(MustBeFresh)
576{
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000577 insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms
578 insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); });
579 insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); }, 1_s);
580 insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); }, 1_h);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800581
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000582 // lookup at exact same moment as insertion is not tested because this won't happen in reality
583
584 advanceClocks(500_ms); // @500ms
585 startInterest("/A")
586 .setCanBePrefix(true)
Yingdi Yu404eafd2016-03-06 14:54:25 -0800587 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800588 BOOST_CHECK_EQUAL(find(), 3);
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000589
590 advanceClocks(1500_ms); // @2s
591 startInterest("/A")
592 .setCanBePrefix(true)
593 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800594 BOOST_CHECK_EQUAL(find(), 4);
595
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000596 advanceClocks(3500_s); // @3502s
597 startInterest("/A")
598 .setCanBePrefix(true)
599 .setMustBeFresh(true);
600 BOOST_CHECK_EQUAL(find(), 4);
601
602 advanceClocks(3500_s); // @7002s
603 startInterest("/A")
604 .setCanBePrefix(true)
605 .setMustBeFresh(true);
Yingdi Yu404eafd2016-03-06 14:54:25 -0800606 BOOST_CHECK_EQUAL(find(), 0);
607}
608
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -0700609BOOST_AUTO_TEST_SUITE_END() // Find
Junxiao Shi85d90832016-08-04 03:19:46 +0000610BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage
Junxiao Shic542f632017-07-18 14:20:32 +0000611BOOST_AUTO_TEST_SUITE_END() // Ims
Jiewen Tan99135962014-09-20 02:18:53 -0700612
Junxiao Shi85d90832016-08-04 03:19:46 +0000613} // namespace tests
Jiewen Tan99135962014-09-20 02:18:53 -0700614} // namespace ndn