blob: 5fe9805549d8de7033a0dcdabd4f32ecefedcf25 [file] [log] [blame]
Jiewen Tan99135962014-09-20 02:18:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev5946ed12015-01-19 23:41:39 -08003 * Copyright (c) 2013-2015 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"
Jiewen Tan99135962014-09-20 02:18:53 -070030
31#include <boost/mpl/list.hpp>
32
33namespace ndn {
34namespace util {
35
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070036BOOST_AUTO_TEST_SUITE(UtilInMemoryStorage)
37
38BOOST_AUTO_TEST_SUITE(Common)
39
Jiewen Tan99135962014-09-20 02:18:53 -070040typedef boost::mpl::list<InMemoryStoragePersistent, InMemoryStorageFifo, InMemoryStorageLfu,
41 InMemoryStorageLru> InMemoryStorages;
42
Jiewen Tan99135962014-09-20 02:18:53 -070043BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages)
44{
45 T ims;
46
47 ims.insert(*makeData("/insertion"));
48}
49
50BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages)
51{
52 T ims;
53
54 ims.insert(*makeData("/a"));
55 ims.insert(*makeData("/b"));
56 ims.insert(*makeData("/c"));
57 ims.insert(*makeData("/d"));
58
59 BOOST_CHECK_EQUAL(ims.size(), 4);
60}
61
62BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion3, T, InMemoryStorages)
63{
64 T ims;
65
66 Name name("/a");
67
68 uint32_t content1 = 1;
69 shared_ptr<Data> data1 = makeData(name);
70 data1->setFreshnessPeriod(time::milliseconds(99999));
71 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
72 signData(data1);
73 ims.insert(*data1);
74
75 uint32_t content2 = 2;
76 shared_ptr<Data> data2 = makeData(name);
77 data2->setFreshnessPeriod(time::milliseconds(99999));
78 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
79 signData(data2);
80 ims.insert(*data2);
81
82 BOOST_CHECK_EQUAL(ims.size(), 2);
83}
84
85BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion, T, InMemoryStorages)
86{
87 T ims;
88
89 shared_ptr<Data> data0 = makeData("/insert/smth");
90 ims.insert(*data0);
91
92 shared_ptr<Data> data = makeData("/insert/duplicate");
93 ims.insert(*data);
94
95 ims.insert(*data);
96 BOOST_CHECK_EQUAL(ims.size(), 2);
97}
98
99BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion2, T, InMemoryStorages)
100{
101 T ims;
102
103 shared_ptr<Data> data = makeData("/insert/duplicate");
104 ims.insert(*data);
105
106 ims.insert(*data);
107 BOOST_CHECK_EQUAL(ims.size(), 1);
108
109 shared_ptr<Data> data2 = makeData("/insert/original");
110 ims.insert(*data2);
111 BOOST_CHECK_EQUAL(ims.size(), 2);
112}
113
114BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFind, T, InMemoryStorages)
115{
116 T ims;
117
118 Name name("/insert/and/find");
119
120 shared_ptr<Data> data = makeData(name);
121 ims.insert(*data);
122
123 shared_ptr<Interest> interest = makeInterest(name);
124
125 shared_ptr<const Data> found = ims.find(*interest);
126
127 BOOST_CHECK(static_cast<bool>(found));
128 BOOST_CHECK_EQUAL(data->getName(), found->getName());
129}
130
131BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages)
132{
133 T ims;
134
135 Name name("/insert/and/find");
136 shared_ptr<Data> data = makeData(name);
137 ims.insert(*data);
138
139 Name name2("/not/find");
140 shared_ptr<Interest> interest = makeInterest(name2);
141
142 shared_ptr<const Data> found = ims.find(*interest);
143
144 BOOST_CHECK_EQUAL(found.get(), static_cast<const Data*>(0));
145}
146
147BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages)
148{
149 T ims;
150
151 Name name("/insert/and/find");
152
153 shared_ptr<Data> data = makeData(name);
154 ims.insert(*data);
155
156 shared_ptr<const Data> found = ims.find(name);
157
158 BOOST_CHECK(static_cast<bool>(found));
159 BOOST_CHECK_EQUAL(data->getName(), found->getName());
160}
161
162BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages)
163{
164 T ims;
165
166 Name name("/insert/and/find");
167
168 shared_ptr<Data> data = makeData(name);
169 ims.insert(*data);
170
171 shared_ptr<const Data> found = ims.find(data->getFullName());
172
173 BOOST_CHECK(static_cast<bool>(found));
174 BOOST_CHECK_EQUAL(data->getFullName(), found->getFullName());
175}
176
177BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByName, T, InMemoryStorages)
178{
179 T ims;
180
181 Name name("/insert/and/find");
182 shared_ptr<Data> data = makeData(name);
183 ims.insert(*data);
184
185 Name name2("/not/find");
186
187 shared_ptr<const Data> found = ims.find(name2);
188
189 BOOST_CHECK(!static_cast<bool>(found));
190}
191
192BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByFullName, T, InMemoryStorages)
193{
194 T ims;
195
196 Name name("/a");
197 uint32_t content1 = 1;
198 shared_ptr<Data> data1 = makeData(name);
199 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
200 signData(data1);
201 ims.insert(*data1);
202
203 uint32_t content2 = 2;
204 shared_ptr<Data> data2 = makeData(name);
205 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
206 signData(data2);
207
208 shared_ptr<const Data> found = ims.find(data2->getFullName());
209
210 BOOST_CHECK(!static_cast<bool>(found));
211}
212
213BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages)
214{
215 T ims;
216
217 Name name("/insertandremovebyname");
218
219 uint32_t content1 = 1;
220 shared_ptr<Data> data1 = makeData(name);
221 data1->setFreshnessPeriod(time::milliseconds(99999));
222 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
223 signData(data1);
224 ims.insert(*data1);
225
226 uint32_t content2 = 2;
227 shared_ptr<Data> data2 = makeData(name);
228 data2->setFreshnessPeriod(time::milliseconds(99999));
229 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
230 signData(data2);
231 ims.insert(*data2);
232
233 shared_ptr<Data> data3 = makeData("/insertandremovebyname/1");
234 ims.insert(*data3);
235
236 shared_ptr<Data> data4 = makeData("/insertandremovebyname/2");
237 ims.insert(*data4);
238
239 BOOST_CHECK_EQUAL(ims.size(), 4);
240
241 ims.erase(data1->getFullName(), false);
242 BOOST_CHECK_EQUAL(ims.size(), 3);
243}
244
245BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages)
246{
247 T ims;
248
249 shared_ptr<Data> data = makeData("/a");
250 ims.insert(*data);
251
252 shared_ptr<Data> data2 = makeData("/b");
253 ims.insert(*data2);
254
255 shared_ptr<Data> data3 = makeData("/c");
256 ims.insert(*data3);
257
258 shared_ptr<Data> data4 = makeData("/d");
259 ims.insert(*data4);
260
261 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
262 ims.insert(*data5);
263
264 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
265 ims.insert(*data6);
266
267 shared_ptr<Data> data7 = makeData("/c/c/1");
268 ims.insert(*data7);
269
270 BOOST_CHECK_EQUAL(ims.size(), 7);
271
272 Name name("/c");
273 ims.erase(name);
274 BOOST_CHECK_EQUAL(ims.size(), 3);
275 BOOST_CHECK_EQUAL(ims.getCapacity(), 5);
276}
277
278BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages)
279{
280 shared_ptr<Data> data = makeData("/digest/compute");
281
282 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
283 data->wireEncode().size());
284 BOOST_CHECK_EQUAL(digest1->size(), 32);
285
286 InMemoryStorageEntry* entry = new InMemoryStorageEntry();
287 entry->setData(*data);
288
289 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
290 entry->getFullName()[-1].value_begin(),
291 entry->getFullName()[-1].value_end());
292}
293
294BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
295{
296 T ims;
297
298 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
299
300 for (int i = 0; i < 10; i++) {
301 std::ostringstream convert;
302 convert << i;
303 Name name("/" + convert.str());
304 shared_ptr<Data> data = makeData(name);
305 ims.insert(*data);
306 }
307
308 InMemoryStorage::const_iterator it = ims.begin();
309 InMemoryStorage::const_iterator tmp1 = it;
310 BOOST_REQUIRE(tmp1 == it);
311 InMemoryStorage::const_iterator tmp2 = tmp1++;
312 BOOST_REQUIRE(tmp2 != tmp1);
313 tmp2 = ++tmp1;
314 BOOST_REQUIRE(tmp2 == tmp1);
315
316 int i = 0;
317 for (;it != ims.end(); it++) {
318 std::ostringstream convert;
319 convert << i;
320 Name name("/" + convert.str());
321 BOOST_CHECK_EQUAL(it->getName(), name);
322 BOOST_CHECK_EQUAL((*it).getName(), name);
323 i++;
324 }
325}
326
327BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
328{
329 T ims;
330
331 shared_ptr<Data> data = makeData("/a");
332 ims.insert(*data);
333
334 shared_ptr<Data> data2 = makeData("/b");
335 ims.insert(*data2);
336
337 shared_ptr<Data> data3 = makeData("/c");
338 ims.insert(*data3);
339
340 shared_ptr<Data> data4 = makeData("/d");
341 ims.insert(*data4);
342
343 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
344 ims.insert(*data5);
345
346 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
347 ims.insert(*data6);
348
349 shared_ptr<Data> data7 = makeData("/c/c/1");
350 ims.insert(*data7);
351}
352
353BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
354{
355 T ims;
356
357 shared_ptr<Data> data = makeData("/a");
358 ims.insert(*data);
359
360 shared_ptr<Data> data2 = makeData("/b");
361 ims.insert(*data2);
362
363 shared_ptr<Data> data3 = makeData("/c");
364 ims.insert(*data3);
365
366 shared_ptr<Data> data4 = makeData("/d");
367 ims.insert(*data4);
368
369 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
370 ims.insert(*data5);
371
372 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
373 ims.insert(*data6);
374
375 shared_ptr<Data> data7 = makeData("/c/c/1");
376 ims.insert(*data7);
377
378 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
379 data->wireEncode().size());
380
381 Name name("/a");
382 ims.erase(name);
383 BOOST_CHECK_EQUAL(ims.size(), 6);
384}
385
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800386BOOST_AUTO_TEST_CASE_TEMPLATE(ImplicitDigestSelector, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -0700387{
388 T ims;
389
390 Name name("/digest/works");
391 shared_ptr<Data> data = makeData(name);
392 ims.insert(*data);
393
394 shared_ptr<Data> data2 = makeData("/a");
395 ims.insert(*data2);
396
397 shared_ptr<Data> data3 = makeData("/z/z/z");
398 ims.insert(*data3);
399
400 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
401 data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700402
403 shared_ptr<Interest> interest = makeInterest("");
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800404 interest->setName(Name(name).appendImplicitSha256Digest(digest1->buf(), digest1->size()));
Jiewen Tan99135962014-09-20 02:18:53 -0700405 interest->setMinSuffixComponents(0);
406 interest->setMaxSuffixComponents(0);
407
408 shared_ptr<const Data> found = ims.find(*interest);
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800409 BOOST_REQUIRE(static_cast<bool>(found));
410 BOOST_CHECK_EQUAL(found->getName(), name);
411
412 shared_ptr<Interest> interest2 = makeInterest("");
413 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
414 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
415 interest2->setName(Name(name).appendImplicitSha256Digest(digest2, 32));
416 interest2->setMinSuffixComponents(0);
417 interest2->setMaxSuffixComponents(0);
418
419 shared_ptr<const Data> notfound = ims.find(*interest2);
Jiewen Tan99135962014-09-20 02:18:53 -0700420 BOOST_CHECK(static_cast<bool>(found));
Jiewen Tan99135962014-09-20 02:18:53 -0700421}
422
423BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector, T, InMemoryStorages)
424{
425 T ims;
426
427 shared_ptr<Data> data = makeData("/a");
428 ims.insert(*data);
429
430 shared_ptr<Data> data2 = makeData("/b");
431 ims.insert(*data2);
432
433 shared_ptr<Data> data4 = makeData("/d");
434 ims.insert(*data4);
435
436 shared_ptr<Data> data5 = makeData("/c/c");
437 ims.insert(*data5);
438
439 shared_ptr<Data> data6 = makeData("/c/f");
440 ims.insert(*data6);
441
442 shared_ptr<Data> data7 = makeData("/c/n");
443 ims.insert(*data7);
444
445 shared_ptr<Interest> interest = makeInterest("/c");
446 interest->setChildSelector(1);
447
448 shared_ptr<const Data> found = ims.find(*interest);
449 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
450
451 shared_ptr<Interest> interest2 = makeInterest("/c");
452 interest2->setChildSelector(0);
453
454 shared_ptr<const Data> found2 = ims.find(*interest2);
455 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
456}
457
458BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector2, T, InMemoryStorages)
459{
460 T ims;
461
462 shared_ptr<Data> data = makeData("/a/b/1");
463 ims.insert(*data);
464
465 shared_ptr<Data> data2 = makeData("/a/b/2");
466 ims.insert(*data2);
467
468 shared_ptr<Data> data3 = makeData("/a/z/1");
469 ims.insert(*data3);
470
471 shared_ptr<Data> data4 = makeData("/a/z/2");
472 ims.insert(*data4);
473
474 shared_ptr<Interest> interest = makeInterest("/a");
475 interest->setChildSelector(1);
476
477 shared_ptr<const Data> found = ims.find(*interest);
478 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
479}
480
481BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector, T, InMemoryStorages)
482{
483 T ims;
484
485 Name name("/insert/withkey");
486 shared_ptr<Data> data = makeData(name);
487 ims.insert(*data);
488
489 shared_ptr<Interest> interest = makeInterest(name);
490 Name keyName("/somewhere/key");
491
492 ndn::KeyLocator locator(keyName);
493 interest->setPublisherPublicKeyLocator(locator);
494
495 shared_ptr<const Data> found = ims.find(*interest);
496 BOOST_CHECK(!static_cast<bool>(found));
497}
498
499BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector2, T, InMemoryStorages)
500{
501 T ims;
502 Name name("/insert/withkey");
503 shared_ptr<Data> data = makeData(name);
504 ims.insert(*data);
505
506 Name name2("/insert/withkey2");
507 shared_ptr<Data> data2 = make_shared<Data>(name2);
508
509 Name keyName("/somewhere/key");
510 const ndn::KeyLocator locator(keyName);
511
512 ndn::SignatureSha256WithRsa fakeSignature;
513 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
514 reinterpret_cast<const uint8_t*>(0), 0));
515
516 fakeSignature.setKeyLocator(locator);
517 data2->setSignature(fakeSignature);
518 data2->wireEncode();
519
520 ims.insert(*data2);
521
522 shared_ptr<Interest> interest = makeInterest(name2);
523 interest->setPublisherPublicKeyLocator(locator);
524
525 shared_ptr<const Data> found = ims.find(*interest);
526 BOOST_CHECK(static_cast<bool>(found));
527 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
528}
529
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800530BOOST_AUTO_TEST_CASE_TEMPLATE(MinMaxComponentsSelector, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -0700531{
532 T ims;
533
534 shared_ptr<Data> data = makeData("/a");
535 ims.insert(*data);
536
537 shared_ptr<Data> data2 = makeData("/b");
538 ims.insert(*data2);
539
540 shared_ptr<Data> data4 = makeData("/d");
541 ims.insert(*data4);
542
543 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
544 ims.insert(*data5);
545
546 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
547 ims.insert(*data6);
548
549 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
550 ims.insert(*data7);
551
552 shared_ptr<Data> data8 = makeData("/c/c/1");
553 ims.insert(*data8);
554
555 shared_ptr<Interest> interest = makeInterest("/c/c");
556 interest->setMinSuffixComponents(3);
557 interest->setChildSelector(0);
558
559 shared_ptr<const Data> found = ims.find(*interest);
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800560 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3");
Jiewen Tan99135962014-09-20 02:18:53 -0700561
562 shared_ptr<Interest> interest2 = makeInterest("/c/c");
563 interest2->setMinSuffixComponents(4);
564 interest2->setChildSelector(1);
565
566 shared_ptr<const Data> found2 = ims.find(*interest2);
567 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
568
569 shared_ptr<Interest> interest3 = makeInterest("/c/c");
570 interest3->setMaxSuffixComponents(2);
571 interest3->setChildSelector(1);
572
573 shared_ptr<const Data> found3 = ims.find(*interest3);
574 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
575}
576
577BOOST_AUTO_TEST_CASE_TEMPLATE(ExcludeSelector, T, InMemoryStorages)
578{
579 T ims;
580
581 shared_ptr<Data> data = makeData("/a");
582 ims.insert(*data);
583
584 shared_ptr<Data> data2 = makeData("/b");
585 ims.insert(*data2);
586
587 shared_ptr<Data> data3 = makeData("/c/a");
588 ims.insert(*data3);
589
590 shared_ptr<Data> data4 = makeData("/d");
591 ims.insert(*data4);
592
593 shared_ptr<Data> data5 = makeData("/c/c");
594 ims.insert(*data5);
595
596 shared_ptr<Data> data6 = makeData("/c/f");
597 ims.insert(*data6);
598
599 shared_ptr<Data> data7 = makeData("/c/n");
600 ims.insert(*data7);
601
602 shared_ptr<Interest> interest = makeInterest("/c");
603 interest->setChildSelector(1);
604 Exclude e;
605 e.excludeOne (Name::Component("n"));
606 interest->setExclude(e);
607
608 shared_ptr<const Data> found = ims.find(*interest);
609 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
610
611 shared_ptr<Interest> interest2 = makeInterest("/c");
612 interest2->setChildSelector(0);
613
614 Exclude e2;
615 e2.excludeOne (Name::Component("a"));
616 interest2->setExclude(e2);
617
618 shared_ptr<const Data> found2 = ims.find(*interest2);
619 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
620
621 shared_ptr<Interest> interest3 = makeInterest("/c");
622 interest3->setChildSelector(0);
623
624 Exclude e3;
625 e3.excludeOne (Name::Component("c"));
626 interest3->setExclude(e3);
627
628 shared_ptr<const Data> found3 = ims.find(*interest3);
629 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
630}
631
632typedef boost::mpl::list<InMemoryStorageFifo, InMemoryStorageLfu, InMemoryStorageLru>
633 InMemoryStoragesLimited;
634
635BOOST_AUTO_TEST_CASE_TEMPLATE(setCapacity, T, InMemoryStoragesLimited)
636{
637 T ims;
638
639 ims.setCapacity(3);
640 ims.insert(*makeData("/1"));
641 ims.insert(*makeData("/2"));
642 ims.insert(*makeData("/3"));
643 BOOST_CHECK_EQUAL(ims.size(), 3);
644
645 ims.setCapacity(2);
646 BOOST_CHECK_EQUAL(ims.size(), 2);
647}
648
649BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
650{
651 T ims(10000);
652
653 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
654
655 T ims2(4);
656
657 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
658}
659
660BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
661{
662 T ims(40);
663
664 for (int i = 0; i < 11; i++) {
665 std::ostringstream convert;
666 convert << i;
667 Name name("/" + convert.str());
668 shared_ptr<Data> data = makeData(name);
669 data->setFreshnessPeriod(time::milliseconds(5000));
670 signData(data);
671 ims.insert(*data);
672 }
673
674 BOOST_CHECK_EQUAL(ims.size(), 11);
675
676 BOOST_CHECK_EQUAL(ims.getCapacity(), 20);
677}
678
679BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
680{
681 T ims(2);
682
683 Name name("/insert/1");
684 shared_ptr<Data> data = makeData(name);
685 ims.insert(*data);
686
687 Name name2("/insert/2");
688 shared_ptr<Data> data2 = makeData(name2);
689 ims.insert(*data2);
690
691 Name name3("/insert/3");
692 shared_ptr<Data> data3 = makeData(name3);
693 ims.insert(*data3);
694
695 BOOST_CHECK_EQUAL(ims.size(), 2);
696
697 shared_ptr<Interest> interest = makeInterest(name);
698 shared_ptr<const Data> found = ims.find(*interest);
699 BOOST_CHECK(!static_cast<bool>(found));
700}
701
702///as Find function is implemented at the base case, therefore testing for one derived class is
703///sufficient for all
704class FindFixture
705{
706protected:
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800707 Name
Jiewen Tan99135962014-09-20 02:18:53 -0700708 insert(uint32_t id, const Name& name)
709 {
710 shared_ptr<Data> data = makeData(name);
711 data->setFreshnessPeriod(time::milliseconds(99999));
712 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
713 signData(data);
714
715 m_ims.insert(*data);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800716
717 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700718 }
719
720 Interest&
721 startInterest(const Name& name)
722 {
723 m_interest = makeInterest(name);
724 return *m_interest;
725 }
726
727 uint32_t
728 find()
729 {
730 shared_ptr<const Data> found = m_ims.find(*m_interest);
731 if (found == 0) {
732 return 0;
733 }
734 const Block& content = found->getContent();
735 if (content.value_size() != sizeof(uint32_t)) {
736 return 0;
737 }
738 return *reinterpret_cast<const uint32_t*>(content.value());
739 }
740
741protected:
742 InMemoryStoragePersistent m_ims;
743 shared_ptr<Interest> m_interest;
744};
745
746BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
747
748BOOST_AUTO_TEST_CASE(EmptyDataName)
749{
750 insert(1, "ndn:/");
751
752 startInterest("ndn:/");
753 BOOST_CHECK_EQUAL(find(), 1);
754}
755
756BOOST_AUTO_TEST_CASE(EmptyInterestName)
757{
758 insert(1, "ndn:/A");
759
760 startInterest("ndn:/");
761 BOOST_CHECK_EQUAL(find(), 1);
762}
763
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800764BOOST_AUTO_TEST_CASE(ExactName)
765{
766 insert(1, "ndn:/");
767 insert(2, "ndn:/A");
768 insert(3, "ndn:/A/B");
769 insert(4, "ndn:/A/C");
770 insert(5, "ndn:/D");
771
772 startInterest("ndn:/A");
773 BOOST_CHECK_EQUAL(find(), 2);
774}
775
776BOOST_AUTO_TEST_CASE(FullName)
777{
778 Name n1 = insert(1, "ndn:/A");
779 Name n2 = insert(2, "ndn:/A");
780
781 startInterest(n1);
782 BOOST_CHECK_EQUAL(find(), 1);
783
784 startInterest(n2);
785 BOOST_CHECK_EQUAL(find(), 2);
786}
787
Jiewen Tan99135962014-09-20 02:18:53 -0700788BOOST_AUTO_TEST_CASE(Leftmost)
789{
790 insert(1, "ndn:/A");
791 insert(2, "ndn:/B/p/1");
792 insert(3, "ndn:/B/p/2");
793 insert(4, "ndn:/B/q/1");
794 insert(5, "ndn:/B/q/2");
795 insert(6, "ndn:/C");
796
797 startInterest("ndn:/B");
798 BOOST_CHECK_EQUAL(find(), 2);
799}
800
801BOOST_AUTO_TEST_CASE(Rightmost)
802{
803 insert(1, "ndn:/A");
804 insert(2, "ndn:/B/p/1");
805 insert(3, "ndn:/B/p/2");
806 insert(4, "ndn:/B/q/1");
807 insert(5, "ndn:/B/q/2");
808 insert(6, "ndn:/C");
809
810 startInterest("ndn:/B")
811 .setChildSelector(1);
812 BOOST_CHECK_EQUAL(find(), 4);
813}
814
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800815BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Jiewen Tan99135962014-09-20 02:18:53 -0700816{
817 insert(1, "ndn:/");
818 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800819 insert(3, "ndn:/B/1");
820 insert(4, "ndn:/C/1/2");
821 insert(5, "ndn:/D/1/2/3");
822 insert(6, "ndn:/E/1/2/3/4");
Jiewen Tan99135962014-09-20 02:18:53 -0700823
824 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700825 .setMinSuffixComponents(0);
Jiewen Tan99135962014-09-20 02:18:53 -0700826 BOOST_CHECK_EQUAL(find(), 1);
827
828 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800829 .setMinSuffixComponents(1);
830 BOOST_CHECK_EQUAL(find(), 1);
831
832 startInterest("ndn:/")
833 .setMinSuffixComponents(2);
834 BOOST_CHECK_EQUAL(find(), 2);
835
836 startInterest("ndn:/")
837 .setMinSuffixComponents(3);
838 BOOST_CHECK_EQUAL(find(), 3);
839
840 startInterest("ndn:/")
841 .setMinSuffixComponents(4);
842 BOOST_CHECK_EQUAL(find(), 4);
843
844 startInterest("ndn:/")
845 .setMinSuffixComponents(5);
846 BOOST_CHECK_EQUAL(find(), 5);
847
848 startInterest("ndn:/")
849 .setMinSuffixComponents(6);
850 BOOST_CHECK_EQUAL(find(), 6);
851
852 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700853 .setMinSuffixComponents(7);
854 BOOST_CHECK_EQUAL(find(), 0);
855}
856
857BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
858{
859 insert(1, "ndn:/");
860 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800861 insert(3, "ndn:/B/2");
862 insert(4, "ndn:/C/2/3");
863 insert(5, "ndn:/D/2/3/4");
864 insert(6, "ndn:/E/2/3/4/5");
Jiewen Tan99135962014-09-20 02:18:53 -0700865
866 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800867 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700868 .setMaxSuffixComponents(0);
869 BOOST_CHECK_EQUAL(find(), 0);
870
871 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800872 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700873 .setMaxSuffixComponents(1);
874 BOOST_CHECK_EQUAL(find(), 1);
875
876 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800877 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700878 .setMaxSuffixComponents(2);
879 BOOST_CHECK_EQUAL(find(), 2);
880
881 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800882 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700883 .setMaxSuffixComponents(3);
884 BOOST_CHECK_EQUAL(find(), 3);
885
886 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800887 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700888 .setMaxSuffixComponents(4);
889 BOOST_CHECK_EQUAL(find(), 4);
890
891 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800892 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700893 .setMaxSuffixComponents(5);
894 BOOST_CHECK_EQUAL(find(), 5);
895
896 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800897 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700898 .setMaxSuffixComponents(6);
899 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800900
901 startInterest("ndn:/")
902 .setChildSelector(1)
903 .setMaxSuffixComponents(7);
904 BOOST_CHECK_EQUAL(find(), 6);
Jiewen Tan99135962014-09-20 02:18:53 -0700905}
906
907BOOST_AUTO_TEST_CASE(DigestOrder)
908{
909 insert(1, "ndn:/A");
910 insert(2, "ndn:/A");
911 // We don't know which comes first, but there must be some order
912
913 startInterest("ndn:/A")
914 .setChildSelector(0);
915 uint32_t leftmost = find();
916
917 startInterest("ndn:/A")
918 .setChildSelector(1);
919 uint32_t rightmost = find();
920
921 BOOST_CHECK_NE(leftmost, rightmost);
922}
923
924BOOST_AUTO_TEST_CASE(DigestExclude)
925{
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800926 insert(1, "ndn:/A");
927 Name n2 = insert(2, "ndn:/A");
928 insert(3, "ndn:/A/B");
929
930 uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE];
931 std::fill_n(digest00, sizeof(digest00), 0x00);
932 uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE];
933 std::fill_n(digestFF, sizeof(digestFF), 0xFF);
934
935 Exclude excludeDigest;
936 excludeDigest.excludeRange(
937 name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)),
938 name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF)));
Jiewen Tan99135962014-09-20 02:18:53 -0700939
940 startInterest("ndn:/A")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800941 .setChildSelector(0)
942 .setExclude(excludeDigest);
943 BOOST_CHECK_EQUAL(find(), 3);
Jiewen Tan99135962014-09-20 02:18:53 -0700944
945 startInterest("ndn:/A")
946 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800947 .setExclude(excludeDigest);
Jiewen Tan99135962014-09-20 02:18:53 -0700948 BOOST_CHECK_EQUAL(find(), 3);
949
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800950 Exclude excludeGeneric;
951 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Jiewen Tan99135962014-09-20 02:18:53 -0700952
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800953 startInterest("ndn:/A")
954 .setChildSelector(0)
955 .setExclude(excludeGeneric);
956 int found1 = find();
957 BOOST_CHECK(found1 == 1 || found1 == 2);
958
959 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700960 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800961 .setExclude(excludeGeneric);
962 int found2 = find();
963 BOOST_CHECK(found2 == 1 || found2 == 2);
964
965 Exclude exclude2 = excludeGeneric;
966 exclude2.excludeOne(n2.get(-1));
967
968 startInterest("ndn:/A")
969 .setChildSelector(0)
970 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700971 BOOST_CHECK_EQUAL(find(), 1);
972
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800973 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700974 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800975 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700976 BOOST_CHECK_EQUAL(find(), 1);
Jiewen Tan99135962014-09-20 02:18:53 -0700977}
978
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -0700979BOOST_AUTO_TEST_SUITE_END() // Find
980BOOST_AUTO_TEST_SUITE_END() // Common
981BOOST_AUTO_TEST_SUITE_END() // UtilInMemoryStorage
Jiewen Tan99135962014-09-20 02:18:53 -0700982
983} // namespace util
984} // namespace ndn