blob: 0ae051f8fea1b5b26dcda63889ae1cb446896bb9 [file] [log] [blame]
Jiewen Tan99135962014-09-20 02:18:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
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"
29#include "../test-make-interest-data.hpp"
30
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:
707 void
708 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);
716 }
717
718 Interest&
719 startInterest(const Name& name)
720 {
721 m_interest = makeInterest(name);
722 return *m_interest;
723 }
724
725 uint32_t
726 find()
727 {
728 shared_ptr<const Data> found = m_ims.find(*m_interest);
729 if (found == 0) {
730 return 0;
731 }
732 const Block& content = found->getContent();
733 if (content.value_size() != sizeof(uint32_t)) {
734 return 0;
735 }
736 return *reinterpret_cast<const uint32_t*>(content.value());
737 }
738
739protected:
740 InMemoryStoragePersistent m_ims;
741 shared_ptr<Interest> m_interest;
742};
743
744BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
745
746BOOST_AUTO_TEST_CASE(EmptyDataName)
747{
748 insert(1, "ndn:/");
749
750 startInterest("ndn:/");
751 BOOST_CHECK_EQUAL(find(), 1);
752}
753
754BOOST_AUTO_TEST_CASE(EmptyInterestName)
755{
756 insert(1, "ndn:/A");
757
758 startInterest("ndn:/");
759 BOOST_CHECK_EQUAL(find(), 1);
760}
761
762BOOST_AUTO_TEST_CASE(Leftmost)
763{
764 insert(1, "ndn:/A");
765 insert(2, "ndn:/B/p/1");
766 insert(3, "ndn:/B/p/2");
767 insert(4, "ndn:/B/q/1");
768 insert(5, "ndn:/B/q/2");
769 insert(6, "ndn:/C");
770
771 startInterest("ndn:/B");
772 BOOST_CHECK_EQUAL(find(), 2);
773}
774
775BOOST_AUTO_TEST_CASE(Rightmost)
776{
777 insert(1, "ndn:/A");
778 insert(2, "ndn:/B/p/1");
779 insert(3, "ndn:/B/p/2");
780 insert(4, "ndn:/B/q/1");
781 insert(5, "ndn:/B/q/2");
782 insert(6, "ndn:/C");
783
784 startInterest("ndn:/B")
785 .setChildSelector(1);
786 BOOST_CHECK_EQUAL(find(), 4);
787}
788
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800789/// @todo Expected failures, needs to be fixed as part of Issue #2118
790BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(Leftmost_ExactName1, 1)
Jiewen Tan99135962014-09-20 02:18:53 -0700791BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
792{
793 insert(1, "ndn:/");
794 insert(2, "ndn:/A/B");
795 insert(3, "ndn:/A/C");
796 insert(4, "ndn:/A");
797 insert(5, "ndn:/D");
798
799 // Intuitively you would think Data 4 should be between Data 1 and 2,
800 // but Data 4 has full Name ndn:/A/<32-octet hash>.
801 startInterest("ndn:/A");
802 BOOST_CHECK_EQUAL(find(), 2);
803}
804
805BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
806{
807 insert(1, "ndn:/");
808 insert(2, "ndn:/A");
809 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
810 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
811 insert(5, "ndn:/D");
812
813 // Data 2 is returned, because <32-octet hash> is less than Data 3.
814 startInterest("ndn:/A");
815 BOOST_CHECK_EQUAL(find(), 2);
816}
817
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800818/// @todo Expected failures, needs to be fixed as part of Issue #2118
819BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinSuffixComponents, 2)
Jiewen Tan99135962014-09-20 02:18:53 -0700820BOOST_AUTO_TEST_CASE(MinSuffixComponents)
821{
822 insert(1, "ndn:/A/1/2/3/4");
823 insert(2, "ndn:/B/1/2/3");
824 insert(3, "ndn:/C/1/2");
825 insert(4, "ndn:/D/1");
826 insert(5, "ndn:/E");
827 insert(6, "ndn:/");
828
829 startInterest("ndn:/")
830 .setChildSelector(1)
831 .setMinSuffixComponents(0);
832 BOOST_CHECK_EQUAL(find(), 6);
833
834 startInterest("ndn:/")
835 .setChildSelector(1)
836 .setMinSuffixComponents(1);
837 BOOST_CHECK_EQUAL(find(), 6);
838
839 startInterest("ndn:/")
840 .setChildSelector(1)
841 .setMinSuffixComponents(2);
842 BOOST_CHECK_EQUAL(find(), 5);
843
844 startInterest("ndn:/")
845 .setChildSelector(1)
846 .setMinSuffixComponents(3);
847 BOOST_CHECK_EQUAL(find(), 4);
848
849 startInterest("ndn:/")
850 .setChildSelector(1)
851 .setMinSuffixComponents(4);
852 BOOST_CHECK_EQUAL(find(), 3);
853
854 startInterest("ndn:/")
855 .setChildSelector(1)
856 .setMinSuffixComponents(5);
857 BOOST_CHECK_EQUAL(find(), 2);
858
859 startInterest("ndn:/")
860 .setChildSelector(1)
861 .setMinSuffixComponents(6);
862 BOOST_CHECK_EQUAL(find(), 1);
863
864 startInterest("ndn:/")
865 .setChildSelector(1)
866 .setMinSuffixComponents(7);
867 BOOST_CHECK_EQUAL(find(), 0);
868}
869
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800870/// @todo Expected failures, needs to be fixed as part of Issue #2118
871BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MaxSuffixComponents, 5)
Jiewen Tan99135962014-09-20 02:18:53 -0700872BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
873{
874 insert(1, "ndn:/");
875 insert(2, "ndn:/A");
876 insert(3, "ndn:/A/B");
877 insert(4, "ndn:/A/B/C");
878 insert(5, "ndn:/A/B/C/D");
879 insert(6, "ndn:/A/B/C/D/E");
880 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
881
882 startInterest("ndn:/")
883 .setMaxSuffixComponents(0);
884 BOOST_CHECK_EQUAL(find(), 0);
885
886 startInterest("ndn:/")
887 .setMaxSuffixComponents(1);
888 BOOST_CHECK_EQUAL(find(), 1);
889
890 startInterest("ndn:/")
891 .setMaxSuffixComponents(2);
892 BOOST_CHECK_EQUAL(find(), 2);
893
894 startInterest("ndn:/")
895 .setMaxSuffixComponents(3);
896 BOOST_CHECK_EQUAL(find(), 3);
897
898 startInterest("ndn:/")
899 .setMaxSuffixComponents(4);
900 BOOST_CHECK_EQUAL(find(), 4);
901
902 startInterest("ndn:/")
903 .setMaxSuffixComponents(5);
904 BOOST_CHECK_EQUAL(find(), 5);
905
906 startInterest("ndn:/")
907 .setMaxSuffixComponents(6);
908 BOOST_CHECK_EQUAL(find(), 6);
909}
910
911BOOST_AUTO_TEST_CASE(DigestOrder)
912{
913 insert(1, "ndn:/A");
914 insert(2, "ndn:/A");
915 // We don't know which comes first, but there must be some order
916
917 startInterest("ndn:/A")
918 .setChildSelector(0);
919 uint32_t leftmost = find();
920
921 startInterest("ndn:/A")
922 .setChildSelector(1);
923 uint32_t rightmost = find();
924
925 BOOST_CHECK_NE(leftmost, rightmost);
926}
927
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800928/// @todo Expected failures, needs to be fixed as part of Issue #2118
929BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(DigestExclude, 1)
Jiewen Tan99135962014-09-20 02:18:53 -0700930BOOST_AUTO_TEST_CASE(DigestExclude)
931{
932 insert(1, "ndn:/A/B");
933 insert(2, "ndn:/A");
934 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
935
936 startInterest("ndn:/A")
937 .setExclude(Exclude().excludeBefore(name::Component(reinterpret_cast<const uint8_t*>(
938 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
939 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
940 BOOST_CHECK_EQUAL(find(), 2);
941
942 startInterest("ndn:/A")
943 .setChildSelector(1)
944 .setExclude(Exclude().excludeAfter(name::Component(reinterpret_cast<const uint8_t*>(
945 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
946 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
947 "\x00"), 33))); // 33 0x00's
948 BOOST_CHECK_EQUAL(find(), 2);
949}
950
951BOOST_AUTO_TEST_CASE(ExactName32)
952{
953 insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
954 insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
955
956 startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
957 BOOST_CHECK_EQUAL(find(), 1);
958}
959
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800960/// @todo Expected failures, needs to be fixed as part of Issue #2118
961BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinSuffixComponents32, 2)
Jiewen Tan99135962014-09-20 02:18:53 -0700962BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
963{
964 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
965 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
966 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
967 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
968 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
969 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
970
971 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
972 .setChildSelector(1)
973 .setMinSuffixComponents(0);
974 BOOST_CHECK_EQUAL(find(), 6);
975
976 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
977 .setChildSelector(1)
978 .setMinSuffixComponents(1);
979 BOOST_CHECK_EQUAL(find(), 6);
980
981 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
982 .setChildSelector(1)
983 .setMinSuffixComponents(2);
984 BOOST_CHECK_EQUAL(find(), 5);
985
986 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
987 .setChildSelector(1)
988 .setMinSuffixComponents(3);
989 BOOST_CHECK_EQUAL(find(), 4);
990
991 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
992 .setChildSelector(1)
993 .setMinSuffixComponents(4);
994 BOOST_CHECK_EQUAL(find(), 3);
995
996 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
997 .setChildSelector(1)
998 .setMinSuffixComponents(5);
999 BOOST_CHECK_EQUAL(find(), 2);
1000
1001 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1002 .setChildSelector(1)
1003 .setMinSuffixComponents(6);
1004 BOOST_CHECK_EQUAL(find(), 1);
1005
1006 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1007 .setChildSelector(1)
1008 .setMinSuffixComponents(7);
1009 BOOST_CHECK_EQUAL(find(), 0);
1010}
1011
Alexander Afanasyevafee0a52014-11-07 11:15:34 -08001012/// @todo Expected failures, needs to be fixed as part of Issue #2118
1013BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MaxSuffixComponents32, 5)
Jiewen Tan99135962014-09-20 02:18:53 -07001014BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
1015{
1016 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
1017 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
1018 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
1019 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
1020 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
1021 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
1022 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
1023
1024 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1025 .setMaxSuffixComponents(0);
1026 BOOST_CHECK_EQUAL(find(), 0);
1027
1028 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1029 .setMaxSuffixComponents(1);
1030 BOOST_CHECK_EQUAL(find(), 1);
1031
1032 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1033 .setMaxSuffixComponents(2);
1034 BOOST_CHECK_EQUAL(find(), 2);
1035
1036 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1037 .setMaxSuffixComponents(3);
1038 BOOST_CHECK_EQUAL(find(), 3);
1039
1040 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1041 .setMaxSuffixComponents(4);
1042 BOOST_CHECK_EQUAL(find(), 4);
1043
1044 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1045 .setMaxSuffixComponents(5);
1046 BOOST_CHECK_EQUAL(find(), 5);
1047
1048 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1049 .setMaxSuffixComponents(6);
1050 BOOST_CHECK_EQUAL(find(), 6);
1051}
1052
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -07001053BOOST_AUTO_TEST_SUITE_END() // Find
1054BOOST_AUTO_TEST_SUITE_END() // Common
1055BOOST_AUTO_TEST_SUITE_END() // UtilInMemoryStorage
Jiewen Tan99135962014-09-20 02:18:53 -07001056
1057} // namespace util
1058} // namespace ndn