blob: f2c1b9f4c2cf02a5469f3941d6b09d0ef7897b67 [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;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700513 fakeSignature.setValue(makeEmptyBlock(tlv::SignatureValue));
Jiewen Tan99135962014-09-20 02:18:53 -0700514
515 fakeSignature.setKeyLocator(locator);
516 data2->setSignature(fakeSignature);
517 data2->wireEncode();
518
519 ims.insert(*data2);
520
521 shared_ptr<Interest> interest = makeInterest(name2);
522 interest->setPublisherPublicKeyLocator(locator);
523
524 shared_ptr<const Data> found = ims.find(*interest);
525 BOOST_CHECK(static_cast<bool>(found));
526 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
527}
528
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800529BOOST_AUTO_TEST_CASE_TEMPLATE(MinMaxComponentsSelector, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -0700530{
531 T ims;
532
533 shared_ptr<Data> data = makeData("/a");
534 ims.insert(*data);
535
536 shared_ptr<Data> data2 = makeData("/b");
537 ims.insert(*data2);
538
539 shared_ptr<Data> data4 = makeData("/d");
540 ims.insert(*data4);
541
542 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
543 ims.insert(*data5);
544
545 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
546 ims.insert(*data6);
547
548 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
549 ims.insert(*data7);
550
551 shared_ptr<Data> data8 = makeData("/c/c/1");
552 ims.insert(*data8);
553
554 shared_ptr<Interest> interest = makeInterest("/c/c");
555 interest->setMinSuffixComponents(3);
556 interest->setChildSelector(0);
557
558 shared_ptr<const Data> found = ims.find(*interest);
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800559 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3");
Jiewen Tan99135962014-09-20 02:18:53 -0700560
561 shared_ptr<Interest> interest2 = makeInterest("/c/c");
562 interest2->setMinSuffixComponents(4);
563 interest2->setChildSelector(1);
564
565 shared_ptr<const Data> found2 = ims.find(*interest2);
566 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
567
568 shared_ptr<Interest> interest3 = makeInterest("/c/c");
569 interest3->setMaxSuffixComponents(2);
570 interest3->setChildSelector(1);
571
572 shared_ptr<const Data> found3 = ims.find(*interest3);
573 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
574}
575
576BOOST_AUTO_TEST_CASE_TEMPLATE(ExcludeSelector, T, InMemoryStorages)
577{
578 T ims;
579
580 shared_ptr<Data> data = makeData("/a");
581 ims.insert(*data);
582
583 shared_ptr<Data> data2 = makeData("/b");
584 ims.insert(*data2);
585
586 shared_ptr<Data> data3 = makeData("/c/a");
587 ims.insert(*data3);
588
589 shared_ptr<Data> data4 = makeData("/d");
590 ims.insert(*data4);
591
592 shared_ptr<Data> data5 = makeData("/c/c");
593 ims.insert(*data5);
594
595 shared_ptr<Data> data6 = makeData("/c/f");
596 ims.insert(*data6);
597
598 shared_ptr<Data> data7 = makeData("/c/n");
599 ims.insert(*data7);
600
601 shared_ptr<Interest> interest = makeInterest("/c");
602 interest->setChildSelector(1);
603 Exclude e;
604 e.excludeOne (Name::Component("n"));
605 interest->setExclude(e);
606
607 shared_ptr<const Data> found = ims.find(*interest);
608 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
609
610 shared_ptr<Interest> interest2 = makeInterest("/c");
611 interest2->setChildSelector(0);
612
613 Exclude e2;
614 e2.excludeOne (Name::Component("a"));
615 interest2->setExclude(e2);
616
617 shared_ptr<const Data> found2 = ims.find(*interest2);
618 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
619
620 shared_ptr<Interest> interest3 = makeInterest("/c");
621 interest3->setChildSelector(0);
622
623 Exclude e3;
624 e3.excludeOne (Name::Component("c"));
625 interest3->setExclude(e3);
626
627 shared_ptr<const Data> found3 = ims.find(*interest3);
628 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
629}
630
631typedef boost::mpl::list<InMemoryStorageFifo, InMemoryStorageLfu, InMemoryStorageLru>
632 InMemoryStoragesLimited;
633
634BOOST_AUTO_TEST_CASE_TEMPLATE(setCapacity, T, InMemoryStoragesLimited)
635{
636 T ims;
637
638 ims.setCapacity(3);
639 ims.insert(*makeData("/1"));
640 ims.insert(*makeData("/2"));
641 ims.insert(*makeData("/3"));
642 BOOST_CHECK_EQUAL(ims.size(), 3);
643
644 ims.setCapacity(2);
645 BOOST_CHECK_EQUAL(ims.size(), 2);
646}
647
648BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
649{
650 T ims(10000);
651
652 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
653
654 T ims2(4);
655
656 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
657}
658
659BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
660{
661 T ims(40);
662
663 for (int i = 0; i < 11; i++) {
664 std::ostringstream convert;
665 convert << i;
666 Name name("/" + convert.str());
667 shared_ptr<Data> data = makeData(name);
668 data->setFreshnessPeriod(time::milliseconds(5000));
669 signData(data);
670 ims.insert(*data);
671 }
672
673 BOOST_CHECK_EQUAL(ims.size(), 11);
674
675 BOOST_CHECK_EQUAL(ims.getCapacity(), 20);
676}
677
678BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
679{
680 T ims(2);
681
682 Name name("/insert/1");
683 shared_ptr<Data> data = makeData(name);
684 ims.insert(*data);
685
686 Name name2("/insert/2");
687 shared_ptr<Data> data2 = makeData(name2);
688 ims.insert(*data2);
689
690 Name name3("/insert/3");
691 shared_ptr<Data> data3 = makeData(name3);
692 ims.insert(*data3);
693
694 BOOST_CHECK_EQUAL(ims.size(), 2);
695
696 shared_ptr<Interest> interest = makeInterest(name);
697 shared_ptr<const Data> found = ims.find(*interest);
698 BOOST_CHECK(!static_cast<bool>(found));
699}
700
701///as Find function is implemented at the base case, therefore testing for one derived class is
702///sufficient for all
703class FindFixture
704{
705protected:
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800706 Name
Jiewen Tan99135962014-09-20 02:18:53 -0700707 insert(uint32_t id, const Name& name)
708 {
709 shared_ptr<Data> data = makeData(name);
710 data->setFreshnessPeriod(time::milliseconds(99999));
711 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
712 signData(data);
713
714 m_ims.insert(*data);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800715
716 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700717 }
718
719 Interest&
720 startInterest(const Name& name)
721 {
722 m_interest = makeInterest(name);
723 return *m_interest;
724 }
725
726 uint32_t
727 find()
728 {
729 shared_ptr<const Data> found = m_ims.find(*m_interest);
730 if (found == 0) {
731 return 0;
732 }
733 const Block& content = found->getContent();
734 if (content.value_size() != sizeof(uint32_t)) {
735 return 0;
736 }
737 return *reinterpret_cast<const uint32_t*>(content.value());
738 }
739
740protected:
741 InMemoryStoragePersistent m_ims;
742 shared_ptr<Interest> m_interest;
743};
744
745BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
746
747BOOST_AUTO_TEST_CASE(EmptyDataName)
748{
749 insert(1, "ndn:/");
750
751 startInterest("ndn:/");
752 BOOST_CHECK_EQUAL(find(), 1);
753}
754
755BOOST_AUTO_TEST_CASE(EmptyInterestName)
756{
757 insert(1, "ndn:/A");
758
759 startInterest("ndn:/");
760 BOOST_CHECK_EQUAL(find(), 1);
761}
762
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800763BOOST_AUTO_TEST_CASE(ExactName)
764{
765 insert(1, "ndn:/");
766 insert(2, "ndn:/A");
767 insert(3, "ndn:/A/B");
768 insert(4, "ndn:/A/C");
769 insert(5, "ndn:/D");
770
771 startInterest("ndn:/A");
772 BOOST_CHECK_EQUAL(find(), 2);
773}
774
775BOOST_AUTO_TEST_CASE(FullName)
776{
777 Name n1 = insert(1, "ndn:/A");
778 Name n2 = insert(2, "ndn:/A");
779
780 startInterest(n1);
781 BOOST_CHECK_EQUAL(find(), 1);
782
783 startInterest(n2);
784 BOOST_CHECK_EQUAL(find(), 2);
785}
786
Jiewen Tan99135962014-09-20 02:18:53 -0700787BOOST_AUTO_TEST_CASE(Leftmost)
788{
789 insert(1, "ndn:/A");
790 insert(2, "ndn:/B/p/1");
791 insert(3, "ndn:/B/p/2");
792 insert(4, "ndn:/B/q/1");
793 insert(5, "ndn:/B/q/2");
794 insert(6, "ndn:/C");
795
796 startInterest("ndn:/B");
797 BOOST_CHECK_EQUAL(find(), 2);
798}
799
800BOOST_AUTO_TEST_CASE(Rightmost)
801{
802 insert(1, "ndn:/A");
803 insert(2, "ndn:/B/p/1");
804 insert(3, "ndn:/B/p/2");
805 insert(4, "ndn:/B/q/1");
806 insert(5, "ndn:/B/q/2");
807 insert(6, "ndn:/C");
808
809 startInterest("ndn:/B")
810 .setChildSelector(1);
811 BOOST_CHECK_EQUAL(find(), 4);
812}
813
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800814BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Jiewen Tan99135962014-09-20 02:18:53 -0700815{
816 insert(1, "ndn:/");
817 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800818 insert(3, "ndn:/B/1");
819 insert(4, "ndn:/C/1/2");
820 insert(5, "ndn:/D/1/2/3");
821 insert(6, "ndn:/E/1/2/3/4");
Jiewen Tan99135962014-09-20 02:18:53 -0700822
823 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700824 .setMinSuffixComponents(0);
Jiewen Tan99135962014-09-20 02:18:53 -0700825 BOOST_CHECK_EQUAL(find(), 1);
826
827 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800828 .setMinSuffixComponents(1);
829 BOOST_CHECK_EQUAL(find(), 1);
830
831 startInterest("ndn:/")
832 .setMinSuffixComponents(2);
833 BOOST_CHECK_EQUAL(find(), 2);
834
835 startInterest("ndn:/")
836 .setMinSuffixComponents(3);
837 BOOST_CHECK_EQUAL(find(), 3);
838
839 startInterest("ndn:/")
840 .setMinSuffixComponents(4);
841 BOOST_CHECK_EQUAL(find(), 4);
842
843 startInterest("ndn:/")
844 .setMinSuffixComponents(5);
845 BOOST_CHECK_EQUAL(find(), 5);
846
847 startInterest("ndn:/")
848 .setMinSuffixComponents(6);
849 BOOST_CHECK_EQUAL(find(), 6);
850
851 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700852 .setMinSuffixComponents(7);
853 BOOST_CHECK_EQUAL(find(), 0);
854}
855
856BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
857{
858 insert(1, "ndn:/");
859 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800860 insert(3, "ndn:/B/2");
861 insert(4, "ndn:/C/2/3");
862 insert(5, "ndn:/D/2/3/4");
863 insert(6, "ndn:/E/2/3/4/5");
Jiewen Tan99135962014-09-20 02:18:53 -0700864
865 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800866 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700867 .setMaxSuffixComponents(0);
868 BOOST_CHECK_EQUAL(find(), 0);
869
870 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800871 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700872 .setMaxSuffixComponents(1);
873 BOOST_CHECK_EQUAL(find(), 1);
874
875 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800876 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700877 .setMaxSuffixComponents(2);
878 BOOST_CHECK_EQUAL(find(), 2);
879
880 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800881 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700882 .setMaxSuffixComponents(3);
883 BOOST_CHECK_EQUAL(find(), 3);
884
885 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800886 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700887 .setMaxSuffixComponents(4);
888 BOOST_CHECK_EQUAL(find(), 4);
889
890 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800891 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700892 .setMaxSuffixComponents(5);
893 BOOST_CHECK_EQUAL(find(), 5);
894
895 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800896 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700897 .setMaxSuffixComponents(6);
898 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800899
900 startInterest("ndn:/")
901 .setChildSelector(1)
902 .setMaxSuffixComponents(7);
903 BOOST_CHECK_EQUAL(find(), 6);
Jiewen Tan99135962014-09-20 02:18:53 -0700904}
905
906BOOST_AUTO_TEST_CASE(DigestOrder)
907{
908 insert(1, "ndn:/A");
909 insert(2, "ndn:/A");
910 // We don't know which comes first, but there must be some order
911
912 startInterest("ndn:/A")
913 .setChildSelector(0);
914 uint32_t leftmost = find();
915
916 startInterest("ndn:/A")
917 .setChildSelector(1);
918 uint32_t rightmost = find();
919
920 BOOST_CHECK_NE(leftmost, rightmost);
921}
922
923BOOST_AUTO_TEST_CASE(DigestExclude)
924{
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800925 insert(1, "ndn:/A");
926 Name n2 = insert(2, "ndn:/A");
927 insert(3, "ndn:/A/B");
928
929 uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE];
930 std::fill_n(digest00, sizeof(digest00), 0x00);
931 uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE];
932 std::fill_n(digestFF, sizeof(digestFF), 0xFF);
933
934 Exclude excludeDigest;
935 excludeDigest.excludeRange(
936 name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)),
937 name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF)));
Jiewen Tan99135962014-09-20 02:18:53 -0700938
939 startInterest("ndn:/A")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800940 .setChildSelector(0)
941 .setExclude(excludeDigest);
942 BOOST_CHECK_EQUAL(find(), 3);
Jiewen Tan99135962014-09-20 02:18:53 -0700943
944 startInterest("ndn:/A")
945 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800946 .setExclude(excludeDigest);
Jiewen Tan99135962014-09-20 02:18:53 -0700947 BOOST_CHECK_EQUAL(find(), 3);
948
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800949 Exclude excludeGeneric;
950 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Jiewen Tan99135962014-09-20 02:18:53 -0700951
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800952 startInterest("ndn:/A")
953 .setChildSelector(0)
954 .setExclude(excludeGeneric);
955 int found1 = find();
956 BOOST_CHECK(found1 == 1 || found1 == 2);
957
958 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700959 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800960 .setExclude(excludeGeneric);
961 int found2 = find();
962 BOOST_CHECK(found2 == 1 || found2 == 2);
963
964 Exclude exclude2 = excludeGeneric;
965 exclude2.excludeOne(n2.get(-1));
966
967 startInterest("ndn:/A")
968 .setChildSelector(0)
969 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700970 BOOST_CHECK_EQUAL(find(), 1);
971
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800972 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700973 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800974 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700975 BOOST_CHECK_EQUAL(find(), 1);
Jiewen Tan99135962014-09-20 02:18:53 -0700976}
977
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -0700978BOOST_AUTO_TEST_SUITE_END() // Find
979BOOST_AUTO_TEST_SUITE_END() // Common
980BOOST_AUTO_TEST_SUITE_END() // UtilInMemoryStorage
Jiewen Tan99135962014-09-20 02:18:53 -0700981
982} // namespace util
983} // namespace ndn