blob: e6a3ab7c2e31502fa95637846d1b05f01480af50 [file] [log] [blame]
Jiewen Tan99135962014-09-20 02:18:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu404eafd2016-03-06 14:54:25 -08003 * Copyright (c) 2013-2016 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
22#include "util/in-memory-storage-persistent.hpp"
23#include "util/in-memory-storage-fifo.hpp"
24#include "util/in-memory-storage-lfu.hpp"
25#include "util/in-memory-storage-lru.hpp"
26#include "security/key-chain.hpp"
27
28#include "boost-test.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029#include "../make-interest-data.hpp"
Yingdi Yu404eafd2016-03-06 14:54:25 -080030#include "../unit-test-time-fixture.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070031
32#include <boost/mpl/list.hpp>
33
34namespace ndn {
35namespace util {
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 Shi85d90832016-08-04 03:19:46 +000040BOOST_AUTO_TEST_SUITE(Util)
41BOOST_AUTO_TEST_SUITE(TestInMemoryStorage)
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070042BOOST_AUTO_TEST_SUITE(Common)
43
Davide Pesaventoeee3e822016-11-26 19:19:34 +010044using InMemoryStorages = boost::mpl::list<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);
69 data1->setFreshnessPeriod(time::milliseconds(99999));
70 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);
76 data2->setFreshnessPeriod(time::milliseconds(99999));
77 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);
215 data1->setFreshnessPeriod(time::milliseconds(99999));
216 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);
222 data2->setFreshnessPeriod(time::milliseconds(99999));
223 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);
269 BOOST_CHECK_EQUAL(ims.getCapacity(), 5);
270}
271
272BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages)
273{
274 shared_ptr<Data> data = makeData("/digest/compute");
275
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700276 ndn::ConstBufferPtr digest1 = ndn::crypto::computeSha256Digest(data->wireEncode().wire(),
277 data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700278 BOOST_CHECK_EQUAL(digest1->size(), 32);
279
280 InMemoryStorageEntry* entry = new InMemoryStorageEntry();
281 entry->setData(*data);
282
283 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
284 entry->getFullName()[-1].value_begin(),
285 entry->getFullName()[-1].value_end());
286}
287
288BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
289{
290 T ims;
291
292 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
293
294 for (int i = 0; i < 10; i++) {
295 std::ostringstream convert;
296 convert << i;
297 Name name("/" + convert.str());
298 shared_ptr<Data> data = makeData(name);
299 ims.insert(*data);
300 }
301
302 InMemoryStorage::const_iterator it = ims.begin();
303 InMemoryStorage::const_iterator tmp1 = it;
304 BOOST_REQUIRE(tmp1 == it);
305 InMemoryStorage::const_iterator tmp2 = tmp1++;
306 BOOST_REQUIRE(tmp2 != tmp1);
307 tmp2 = ++tmp1;
308 BOOST_REQUIRE(tmp2 == tmp1);
309
310 int i = 0;
311 for (;it != ims.end(); it++) {
312 std::ostringstream convert;
313 convert << i;
314 Name name("/" + convert.str());
315 BOOST_CHECK_EQUAL(it->getName(), name);
316 BOOST_CHECK_EQUAL((*it).getName(), name);
317 i++;
318 }
319}
320
321BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
322{
323 T ims;
324
325 shared_ptr<Data> data = makeData("/a");
326 ims.insert(*data);
327
328 shared_ptr<Data> data2 = makeData("/b");
329 ims.insert(*data2);
330
331 shared_ptr<Data> data3 = makeData("/c");
332 ims.insert(*data3);
333
334 shared_ptr<Data> data4 = makeData("/d");
335 ims.insert(*data4);
336
337 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
338 ims.insert(*data5);
339
340 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
341 ims.insert(*data6);
342
343 shared_ptr<Data> data7 = makeData("/c/c/1");
344 ims.insert(*data7);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100345
346 // avoid "test case [...] did not check any assertions" message from Boost.Test
347 BOOST_CHECK(true);
Jiewen Tan99135962014-09-20 02:18:53 -0700348}
349
350BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
351{
352 T ims;
353
354 shared_ptr<Data> data = makeData("/a");
355 ims.insert(*data);
356
357 shared_ptr<Data> data2 = makeData("/b");
358 ims.insert(*data2);
359
360 shared_ptr<Data> data3 = makeData("/c");
361 ims.insert(*data3);
362
363 shared_ptr<Data> data4 = makeData("/d");
364 ims.insert(*data4);
365
366 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
367 ims.insert(*data5);
368
369 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
370 ims.insert(*data6);
371
372 shared_ptr<Data> data7 = makeData("/c/c/1");
373 ims.insert(*data7);
374
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700375 ndn::ConstBufferPtr digest1 = ndn::crypto::computeSha256Digest(data->wireEncode().wire(),
376 data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700377
378 Name name("/a");
379 ims.erase(name);
380 BOOST_CHECK_EQUAL(ims.size(), 6);
381}
382
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800383BOOST_AUTO_TEST_CASE_TEMPLATE(ImplicitDigestSelector, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -0700384{
385 T ims;
386
387 Name name("/digest/works");
388 shared_ptr<Data> data = makeData(name);
389 ims.insert(*data);
390
391 shared_ptr<Data> data2 = makeData("/a");
392 ims.insert(*data2);
393
394 shared_ptr<Data> data3 = makeData("/z/z/z");
395 ims.insert(*data3);
396
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700397 ndn::ConstBufferPtr digest1 = ndn::crypto::computeSha256Digest(data->wireEncode().wire(),
398 data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700399
400 shared_ptr<Interest> interest = makeInterest("");
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800401 interest->setName(Name(name).appendImplicitSha256Digest(digest1->buf(), digest1->size()));
Jiewen Tan99135962014-09-20 02:18:53 -0700402 interest->setMinSuffixComponents(0);
403 interest->setMaxSuffixComponents(0);
404
405 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000406 BOOST_REQUIRE(found != nullptr);
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800407 BOOST_CHECK_EQUAL(found->getName(), name);
408
409 shared_ptr<Interest> interest2 = makeInterest("");
410 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
411 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
412 interest2->setName(Name(name).appendImplicitSha256Digest(digest2, 32));
413 interest2->setMinSuffixComponents(0);
414 interest2->setMaxSuffixComponents(0);
415
416 shared_ptr<const Data> notfound = ims.find(*interest2);
Junxiao Shi85d90832016-08-04 03:19:46 +0000417 BOOST_CHECK(notfound == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700418}
419
420BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector, T, InMemoryStorages)
421{
422 T ims;
423
424 shared_ptr<Data> data = makeData("/a");
425 ims.insert(*data);
426
427 shared_ptr<Data> data2 = makeData("/b");
428 ims.insert(*data2);
429
430 shared_ptr<Data> data4 = makeData("/d");
431 ims.insert(*data4);
432
433 shared_ptr<Data> data5 = makeData("/c/c");
434 ims.insert(*data5);
435
436 shared_ptr<Data> data6 = makeData("/c/f");
437 ims.insert(*data6);
438
439 shared_ptr<Data> data7 = makeData("/c/n");
440 ims.insert(*data7);
441
442 shared_ptr<Interest> interest = makeInterest("/c");
443 interest->setChildSelector(1);
444
445 shared_ptr<const Data> found = ims.find(*interest);
446 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
447
448 shared_ptr<Interest> interest2 = makeInterest("/c");
449 interest2->setChildSelector(0);
450
451 shared_ptr<const Data> found2 = ims.find(*interest2);
452 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
453}
454
455BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector2, T, InMemoryStorages)
456{
457 T ims;
458
459 shared_ptr<Data> data = makeData("/a/b/1");
460 ims.insert(*data);
461
462 shared_ptr<Data> data2 = makeData("/a/b/2");
463 ims.insert(*data2);
464
465 shared_ptr<Data> data3 = makeData("/a/z/1");
466 ims.insert(*data3);
467
468 shared_ptr<Data> data4 = makeData("/a/z/2");
469 ims.insert(*data4);
470
471 shared_ptr<Interest> interest = makeInterest("/a");
472 interest->setChildSelector(1);
473
474 shared_ptr<const Data> found = ims.find(*interest);
475 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
476}
477
478BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector, T, InMemoryStorages)
479{
480 T ims;
481
482 Name name("/insert/withkey");
483 shared_ptr<Data> data = makeData(name);
484 ims.insert(*data);
485
486 shared_ptr<Interest> interest = makeInterest(name);
487 Name keyName("/somewhere/key");
488
489 ndn::KeyLocator locator(keyName);
490 interest->setPublisherPublicKeyLocator(locator);
491
492 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000493 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700494}
495
496BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector2, T, InMemoryStorages)
497{
498 T ims;
499 Name name("/insert/withkey");
500 shared_ptr<Data> data = makeData(name);
501 ims.insert(*data);
502
503 Name name2("/insert/withkey2");
504 shared_ptr<Data> data2 = make_shared<Data>(name2);
505
506 Name keyName("/somewhere/key");
507 const ndn::KeyLocator locator(keyName);
508
509 ndn::SignatureSha256WithRsa fakeSignature;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700510 fakeSignature.setValue(makeEmptyBlock(tlv::SignatureValue));
Jiewen Tan99135962014-09-20 02:18:53 -0700511
512 fakeSignature.setKeyLocator(locator);
513 data2->setSignature(fakeSignature);
514 data2->wireEncode();
515
516 ims.insert(*data2);
517
518 shared_ptr<Interest> interest = makeInterest(name2);
519 interest->setPublisherPublicKeyLocator(locator);
520
521 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000522 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700523 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
524}
525
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800526BOOST_AUTO_TEST_CASE_TEMPLATE(MinMaxComponentsSelector, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -0700527{
528 T ims;
529
530 shared_ptr<Data> data = makeData("/a");
531 ims.insert(*data);
532
533 shared_ptr<Data> data2 = makeData("/b");
534 ims.insert(*data2);
535
536 shared_ptr<Data> data4 = makeData("/d");
537 ims.insert(*data4);
538
539 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
540 ims.insert(*data5);
541
542 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
543 ims.insert(*data6);
544
545 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
546 ims.insert(*data7);
547
548 shared_ptr<Data> data8 = makeData("/c/c/1");
549 ims.insert(*data8);
550
551 shared_ptr<Interest> interest = makeInterest("/c/c");
552 interest->setMinSuffixComponents(3);
553 interest->setChildSelector(0);
554
555 shared_ptr<const Data> found = ims.find(*interest);
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800556 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3");
Jiewen Tan99135962014-09-20 02:18:53 -0700557
558 shared_ptr<Interest> interest2 = makeInterest("/c/c");
559 interest2->setMinSuffixComponents(4);
560 interest2->setChildSelector(1);
561
562 shared_ptr<const Data> found2 = ims.find(*interest2);
563 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
564
565 shared_ptr<Interest> interest3 = makeInterest("/c/c");
566 interest3->setMaxSuffixComponents(2);
567 interest3->setChildSelector(1);
568
569 shared_ptr<const Data> found3 = ims.find(*interest3);
570 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
571}
572
573BOOST_AUTO_TEST_CASE_TEMPLATE(ExcludeSelector, T, InMemoryStorages)
574{
575 T ims;
576
577 shared_ptr<Data> data = makeData("/a");
578 ims.insert(*data);
579
580 shared_ptr<Data> data2 = makeData("/b");
581 ims.insert(*data2);
582
583 shared_ptr<Data> data3 = makeData("/c/a");
584 ims.insert(*data3);
585
586 shared_ptr<Data> data4 = makeData("/d");
587 ims.insert(*data4);
588
589 shared_ptr<Data> data5 = makeData("/c/c");
590 ims.insert(*data5);
591
592 shared_ptr<Data> data6 = makeData("/c/f");
593 ims.insert(*data6);
594
595 shared_ptr<Data> data7 = makeData("/c/n");
596 ims.insert(*data7);
597
598 shared_ptr<Interest> interest = makeInterest("/c");
599 interest->setChildSelector(1);
600 Exclude e;
601 e.excludeOne (Name::Component("n"));
602 interest->setExclude(e);
603
604 shared_ptr<const Data> found = ims.find(*interest);
605 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
606
607 shared_ptr<Interest> interest2 = makeInterest("/c");
608 interest2->setChildSelector(0);
609
610 Exclude e2;
611 e2.excludeOne (Name::Component("a"));
612 interest2->setExclude(e2);
613
614 shared_ptr<const Data> found2 = ims.find(*interest2);
615 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
616
617 shared_ptr<Interest> interest3 = makeInterest("/c");
618 interest3->setChildSelector(0);
619
620 Exclude e3;
621 e3.excludeOne (Name::Component("c"));
622 interest3->setExclude(e3);
623
624 shared_ptr<const Data> found3 = ims.find(*interest3);
625 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
626}
627
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100628using InMemoryStoragesLimited = boost::mpl::list<InMemoryStorageFifo,
629 InMemoryStorageLfu,
630 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -0700631
Yingdi Yu404eafd2016-03-06 14:54:25 -0800632BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited)
Jiewen Tan99135962014-09-20 02:18:53 -0700633{
634 T ims;
635
636 ims.setCapacity(3);
637 ims.insert(*makeData("/1"));
638 ims.insert(*makeData("/2"));
639 ims.insert(*makeData("/3"));
640 BOOST_CHECK_EQUAL(ims.size(), 3);
641
642 ims.setCapacity(2);
643 BOOST_CHECK_EQUAL(ims.size(), 2);
644}
645
646BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
647{
648 T ims(10000);
649
650 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
651
652 T ims2(4);
653
654 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
655}
656
657BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
658{
659 T ims(40);
660
661 for (int i = 0; i < 11; i++) {
662 std::ostringstream convert;
663 convert << i;
664 Name name("/" + convert.str());
665 shared_ptr<Data> data = makeData(name);
666 data->setFreshnessPeriod(time::milliseconds(5000));
667 signData(data);
668 ims.insert(*data);
669 }
670
671 BOOST_CHECK_EQUAL(ims.size(), 11);
672
673 BOOST_CHECK_EQUAL(ims.getCapacity(), 20);
674}
675
676BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
677{
678 T ims(2);
679
680 Name name("/insert/1");
681 shared_ptr<Data> data = makeData(name);
682 ims.insert(*data);
683
684 Name name2("/insert/2");
685 shared_ptr<Data> data2 = makeData(name2);
686 ims.insert(*data2);
687
688 Name name3("/insert/3");
689 shared_ptr<Data> data3 = makeData(name3);
690 ims.insert(*data3);
691
692 BOOST_CHECK_EQUAL(ims.size(), 2);
693
694 shared_ptr<Interest> interest = makeInterest(name);
695 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000696 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700697}
698
699///as Find function is implemented at the base case, therefore testing for one derived class is
700///sufficient for all
Yingdi Yu404eafd2016-03-06 14:54:25 -0800701class FindFixture : public ndn::tests::UnitTestTimeFixture
Jiewen Tan99135962014-09-20 02:18:53 -0700702{
703protected:
Yingdi Yu404eafd2016-03-06 14:54:25 -0800704 FindFixture()
705 : m_ims(io)
706 {
707 }
708
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800709 Name
Yingdi Yu404eafd2016-03-06 14:54:25 -0800710 insert(uint32_t id, const Name& name,
711 const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW)
Jiewen Tan99135962014-09-20 02:18:53 -0700712 {
713 shared_ptr<Data> data = makeData(name);
714 data->setFreshnessPeriod(time::milliseconds(99999));
715 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
716 signData(data);
717
Yingdi Yu404eafd2016-03-06 14:54:25 -0800718 m_ims.insert(*data, freshWindow);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800719
720 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700721 }
722
723 Interest&
724 startInterest(const Name& name)
725 {
726 m_interest = makeInterest(name);
727 return *m_interest;
728 }
729
730 uint32_t
731 find()
732 {
733 shared_ptr<const Data> found = m_ims.find(*m_interest);
734 if (found == 0) {
735 return 0;
736 }
737 const Block& content = found->getContent();
738 if (content.value_size() != sizeof(uint32_t)) {
739 return 0;
740 }
741 return *reinterpret_cast<const uint32_t*>(content.value());
742 }
743
744protected:
745 InMemoryStoragePersistent m_ims;
746 shared_ptr<Interest> m_interest;
747};
748
749BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
750
751BOOST_AUTO_TEST_CASE(EmptyDataName)
752{
753 insert(1, "ndn:/");
754
755 startInterest("ndn:/");
756 BOOST_CHECK_EQUAL(find(), 1);
757}
758
759BOOST_AUTO_TEST_CASE(EmptyInterestName)
760{
761 insert(1, "ndn:/A");
762
763 startInterest("ndn:/");
764 BOOST_CHECK_EQUAL(find(), 1);
765}
766
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800767BOOST_AUTO_TEST_CASE(ExactName)
768{
769 insert(1, "ndn:/");
770 insert(2, "ndn:/A");
771 insert(3, "ndn:/A/B");
772 insert(4, "ndn:/A/C");
773 insert(5, "ndn:/D");
774
775 startInterest("ndn:/A");
776 BOOST_CHECK_EQUAL(find(), 2);
777}
778
779BOOST_AUTO_TEST_CASE(FullName)
780{
781 Name n1 = insert(1, "ndn:/A");
782 Name n2 = insert(2, "ndn:/A");
783
784 startInterest(n1);
785 BOOST_CHECK_EQUAL(find(), 1);
786
787 startInterest(n2);
788 BOOST_CHECK_EQUAL(find(), 2);
789}
790
Jiewen Tan99135962014-09-20 02:18:53 -0700791BOOST_AUTO_TEST_CASE(Leftmost)
792{
793 insert(1, "ndn:/A");
794 insert(2, "ndn:/B/p/1");
795 insert(3, "ndn:/B/p/2");
796 insert(4, "ndn:/B/q/1");
797 insert(5, "ndn:/B/q/2");
798 insert(6, "ndn:/C");
799
800 startInterest("ndn:/B");
801 BOOST_CHECK_EQUAL(find(), 2);
802}
803
804BOOST_AUTO_TEST_CASE(Rightmost)
805{
806 insert(1, "ndn:/A");
807 insert(2, "ndn:/B/p/1");
808 insert(3, "ndn:/B/p/2");
809 insert(4, "ndn:/B/q/1");
810 insert(5, "ndn:/B/q/2");
811 insert(6, "ndn:/C");
812
813 startInterest("ndn:/B")
814 .setChildSelector(1);
815 BOOST_CHECK_EQUAL(find(), 4);
816}
817
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800818BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Jiewen Tan99135962014-09-20 02:18:53 -0700819{
820 insert(1, "ndn:/");
821 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800822 insert(3, "ndn:/B/1");
823 insert(4, "ndn:/C/1/2");
824 insert(5, "ndn:/D/1/2/3");
825 insert(6, "ndn:/E/1/2/3/4");
Jiewen Tan99135962014-09-20 02:18:53 -0700826
827 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700828 .setMinSuffixComponents(0);
Jiewen Tan99135962014-09-20 02:18:53 -0700829 BOOST_CHECK_EQUAL(find(), 1);
830
831 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800832 .setMinSuffixComponents(1);
833 BOOST_CHECK_EQUAL(find(), 1);
834
835 startInterest("ndn:/")
836 .setMinSuffixComponents(2);
837 BOOST_CHECK_EQUAL(find(), 2);
838
839 startInterest("ndn:/")
840 .setMinSuffixComponents(3);
841 BOOST_CHECK_EQUAL(find(), 3);
842
843 startInterest("ndn:/")
844 .setMinSuffixComponents(4);
845 BOOST_CHECK_EQUAL(find(), 4);
846
847 startInterest("ndn:/")
848 .setMinSuffixComponents(5);
849 BOOST_CHECK_EQUAL(find(), 5);
850
851 startInterest("ndn:/")
852 .setMinSuffixComponents(6);
853 BOOST_CHECK_EQUAL(find(), 6);
854
855 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700856 .setMinSuffixComponents(7);
857 BOOST_CHECK_EQUAL(find(), 0);
858}
859
860BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
861{
862 insert(1, "ndn:/");
863 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800864 insert(3, "ndn:/B/2");
865 insert(4, "ndn:/C/2/3");
866 insert(5, "ndn:/D/2/3/4");
867 insert(6, "ndn:/E/2/3/4/5");
Jiewen Tan99135962014-09-20 02:18:53 -0700868
869 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800870 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700871 .setMaxSuffixComponents(0);
872 BOOST_CHECK_EQUAL(find(), 0);
873
874 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800875 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700876 .setMaxSuffixComponents(1);
877 BOOST_CHECK_EQUAL(find(), 1);
878
879 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800880 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700881 .setMaxSuffixComponents(2);
882 BOOST_CHECK_EQUAL(find(), 2);
883
884 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800885 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700886 .setMaxSuffixComponents(3);
887 BOOST_CHECK_EQUAL(find(), 3);
888
889 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800890 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700891 .setMaxSuffixComponents(4);
892 BOOST_CHECK_EQUAL(find(), 4);
893
894 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800895 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700896 .setMaxSuffixComponents(5);
897 BOOST_CHECK_EQUAL(find(), 5);
898
899 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800900 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700901 .setMaxSuffixComponents(6);
902 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800903
904 startInterest("ndn:/")
905 .setChildSelector(1)
906 .setMaxSuffixComponents(7);
907 BOOST_CHECK_EQUAL(find(), 6);
Jiewen Tan99135962014-09-20 02:18:53 -0700908}
909
910BOOST_AUTO_TEST_CASE(DigestOrder)
911{
912 insert(1, "ndn:/A");
913 insert(2, "ndn:/A");
914 // We don't know which comes first, but there must be some order
915
916 startInterest("ndn:/A")
917 .setChildSelector(0);
918 uint32_t leftmost = find();
919
920 startInterest("ndn:/A")
921 .setChildSelector(1);
922 uint32_t rightmost = find();
923
924 BOOST_CHECK_NE(leftmost, rightmost);
925}
926
927BOOST_AUTO_TEST_CASE(DigestExclude)
928{
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800929 insert(1, "ndn:/A");
930 Name n2 = insert(2, "ndn:/A");
931 insert(3, "ndn:/A/B");
932
933 uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE];
934 std::fill_n(digest00, sizeof(digest00), 0x00);
935 uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE];
936 std::fill_n(digestFF, sizeof(digestFF), 0xFF);
937
938 Exclude excludeDigest;
939 excludeDigest.excludeRange(
940 name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)),
941 name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF)));
Jiewen Tan99135962014-09-20 02:18:53 -0700942
943 startInterest("ndn:/A")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800944 .setChildSelector(0)
945 .setExclude(excludeDigest);
946 BOOST_CHECK_EQUAL(find(), 3);
Jiewen Tan99135962014-09-20 02:18:53 -0700947
948 startInterest("ndn:/A")
949 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800950 .setExclude(excludeDigest);
Jiewen Tan99135962014-09-20 02:18:53 -0700951 BOOST_CHECK_EQUAL(find(), 3);
952
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800953 Exclude excludeGeneric;
954 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Jiewen Tan99135962014-09-20 02:18:53 -0700955
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800956 startInterest("ndn:/A")
957 .setChildSelector(0)
958 .setExclude(excludeGeneric);
959 int found1 = find();
960 BOOST_CHECK(found1 == 1 || found1 == 2);
961
962 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700963 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800964 .setExclude(excludeGeneric);
965 int found2 = find();
966 BOOST_CHECK(found2 == 1 || found2 == 2);
967
968 Exclude exclude2 = excludeGeneric;
969 exclude2.excludeOne(n2.get(-1));
970
971 startInterest("ndn:/A")
972 .setChildSelector(0)
973 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700974 BOOST_CHECK_EQUAL(find(), 1);
975
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800976 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700977 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800978 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700979 BOOST_CHECK_EQUAL(find(), 1);
Jiewen Tan99135962014-09-20 02:18:53 -0700980}
981
Yingdi Yu404eafd2016-03-06 14:54:25 -0800982BOOST_AUTO_TEST_CASE(MustBeFresh)
983{
984 Name data1Name = insert(1, "ndn:/A/1", time::milliseconds(500));
985 insert(2, "ndn:/A/2", time::milliseconds(2500));
986 insert(3, "ndn:/A/3", time::milliseconds(3500));
987 insert(4, "ndn:/A/4", time::milliseconds(1500));
988
989 // @0s, all Data are fresh
990 startInterest("ndn:/A/1")
991 .setMustBeFresh(true);
992 BOOST_CHECK_EQUAL(find(), 1);
993
994 startInterest("ndn:/A/1")
995 .setMustBeFresh(false);
996 BOOST_CHECK_EQUAL(find(), 1);
997
998 startInterest("ndn:/A")
999 .setMustBeFresh(true)
1000 .setChildSelector(0);
1001 BOOST_CHECK_EQUAL(find(), 1);
1002
1003 startInterest("ndn:/A")
1004 .setMustBeFresh(true)
1005 .setChildSelector(1);
1006 BOOST_CHECK_EQUAL(find(), 4);
1007
1008 advanceClocks(time::milliseconds(1000));
1009 // @1s, /A/1 is stale
1010 startInterest("ndn:/A/1")
1011 .setMustBeFresh(true);
1012 BOOST_CHECK_EQUAL(find(), 0);
1013 startInterest("ndn:/A/1")
1014 .setMustBeFresh(false);
1015 BOOST_CHECK_EQUAL(find(), 1);
1016
1017 // MustBeFresh is ignored when full Name is specified
1018 startInterest(data1Name)
1019 .setMustBeFresh(true);
1020 BOOST_CHECK_EQUAL(find(), 1);
1021
1022 startInterest("ndn:/A")
1023 .setMustBeFresh(true)
1024 .setChildSelector(0);
1025 BOOST_CHECK_EQUAL(find(), 2);
1026 startInterest("ndn:/A")
1027 .setMustBeFresh(false)
1028 .setChildSelector(0);
1029 BOOST_CHECK_EQUAL(find(), 1);
1030
1031 advanceClocks(time::milliseconds(1000));
1032 // @2s, /A/1 and /A/4 are stale
1033 startInterest("ndn:/A")
1034 .setMustBeFresh(true)
1035 .setChildSelector(1);
1036 BOOST_CHECK_EQUAL(find(), 3);
1037 startInterest("ndn:/A")
1038 .setMustBeFresh(false)
1039 .setChildSelector(1);
1040 BOOST_CHECK_EQUAL(find(), 4);
1041
1042 advanceClocks(time::milliseconds(2000));
1043 // @4s, all Data are stale
1044 startInterest("ndn:/A")
1045 .setMustBeFresh(true)
1046 .setChildSelector(0);
1047 BOOST_CHECK_EQUAL(find(), 0);
1048 startInterest("ndn:/A")
1049 .setMustBeFresh(true)
1050 .setChildSelector(1);
1051 BOOST_CHECK_EQUAL(find(), 0);
1052}
1053
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -07001054BOOST_AUTO_TEST_SUITE_END() // Find
1055BOOST_AUTO_TEST_SUITE_END() // Common
Junxiao Shi85d90832016-08-04 03:19:46 +00001056BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage
1057BOOST_AUTO_TEST_SUITE_END() // Util
Jiewen Tan99135962014-09-20 02:18:53 -07001058
Junxiao Shi85d90832016-08-04 03:19:46 +00001059} // namespace tests
Jiewen Tan99135962014-09-20 02:18:53 -07001060} // namespace util
1061} // namespace ndn