blob: 1d2cfde6104735c94bfe0bde2e4643f7a6825989 [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>
Alexander Afanasyevafee0a52014-11-07 11:15:34 -080032#include <boost/mpl/front.hpp>
Jiewen Tan99135962014-09-20 02:18:53 -070033
34namespace ndn {
35namespace util {
36
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070037BOOST_AUTO_TEST_SUITE(UtilInMemoryStorage)
38
39BOOST_AUTO_TEST_SUITE(Common)
40
Jiewen Tan99135962014-09-20 02:18:53 -070041typedef boost::mpl::list<InMemoryStoragePersistent, InMemoryStorageFifo, InMemoryStorageLfu,
42 InMemoryStorageLru> InMemoryStorages;
43
Jiewen Tan99135962014-09-20 02:18:53 -070044BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages)
45{
46 T ims;
47
48 ims.insert(*makeData("/insertion"));
49}
50
51BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages)
52{
53 T ims;
54
55 ims.insert(*makeData("/a"));
56 ims.insert(*makeData("/b"));
57 ims.insert(*makeData("/c"));
58 ims.insert(*makeData("/d"));
59
60 BOOST_CHECK_EQUAL(ims.size(), 4);
61}
62
63BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion3, T, InMemoryStorages)
64{
65 T ims;
66
67 Name name("/a");
68
69 uint32_t content1 = 1;
70 shared_ptr<Data> data1 = makeData(name);
71 data1->setFreshnessPeriod(time::milliseconds(99999));
72 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
73 signData(data1);
74 ims.insert(*data1);
75
76 uint32_t content2 = 2;
77 shared_ptr<Data> data2 = makeData(name);
78 data2->setFreshnessPeriod(time::milliseconds(99999));
79 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
80 signData(data2);
81 ims.insert(*data2);
82
83 BOOST_CHECK_EQUAL(ims.size(), 2);
84}
85
86BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion, T, InMemoryStorages)
87{
88 T ims;
89
90 shared_ptr<Data> data0 = makeData("/insert/smth");
91 ims.insert(*data0);
92
93 shared_ptr<Data> data = makeData("/insert/duplicate");
94 ims.insert(*data);
95
96 ims.insert(*data);
97 BOOST_CHECK_EQUAL(ims.size(), 2);
98}
99
100BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion2, T, InMemoryStorages)
101{
102 T ims;
103
104 shared_ptr<Data> data = makeData("/insert/duplicate");
105 ims.insert(*data);
106
107 ims.insert(*data);
108 BOOST_CHECK_EQUAL(ims.size(), 1);
109
110 shared_ptr<Data> data2 = makeData("/insert/original");
111 ims.insert(*data2);
112 BOOST_CHECK_EQUAL(ims.size(), 2);
113}
114
115BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFind, T, InMemoryStorages)
116{
117 T ims;
118
119 Name name("/insert/and/find");
120
121 shared_ptr<Data> data = makeData(name);
122 ims.insert(*data);
123
124 shared_ptr<Interest> interest = makeInterest(name);
125
126 shared_ptr<const Data> found = ims.find(*interest);
127
128 BOOST_CHECK(static_cast<bool>(found));
129 BOOST_CHECK_EQUAL(data->getName(), found->getName());
130}
131
132BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages)
133{
134 T ims;
135
136 Name name("/insert/and/find");
137 shared_ptr<Data> data = makeData(name);
138 ims.insert(*data);
139
140 Name name2("/not/find");
141 shared_ptr<Interest> interest = makeInterest(name2);
142
143 shared_ptr<const Data> found = ims.find(*interest);
144
145 BOOST_CHECK_EQUAL(found.get(), static_cast<const Data*>(0));
146}
147
148BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages)
149{
150 T ims;
151
152 Name name("/insert/and/find");
153
154 shared_ptr<Data> data = makeData(name);
155 ims.insert(*data);
156
157 shared_ptr<const Data> found = ims.find(name);
158
159 BOOST_CHECK(static_cast<bool>(found));
160 BOOST_CHECK_EQUAL(data->getName(), found->getName());
161}
162
163BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages)
164{
165 T ims;
166
167 Name name("/insert/and/find");
168
169 shared_ptr<Data> data = makeData(name);
170 ims.insert(*data);
171
172 shared_ptr<const Data> found = ims.find(data->getFullName());
173
174 BOOST_CHECK(static_cast<bool>(found));
175 BOOST_CHECK_EQUAL(data->getFullName(), found->getFullName());
176}
177
178BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByName, T, InMemoryStorages)
179{
180 T ims;
181
182 Name name("/insert/and/find");
183 shared_ptr<Data> data = makeData(name);
184 ims.insert(*data);
185
186 Name name2("/not/find");
187
188 shared_ptr<const Data> found = ims.find(name2);
189
190 BOOST_CHECK(!static_cast<bool>(found));
191}
192
193BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByFullName, T, InMemoryStorages)
194{
195 T ims;
196
197 Name name("/a");
198 uint32_t content1 = 1;
199 shared_ptr<Data> data1 = makeData(name);
200 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
201 signData(data1);
202 ims.insert(*data1);
203
204 uint32_t content2 = 2;
205 shared_ptr<Data> data2 = makeData(name);
206 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
207 signData(data2);
208
209 shared_ptr<const Data> found = ims.find(data2->getFullName());
210
211 BOOST_CHECK(!static_cast<bool>(found));
212}
213
214BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages)
215{
216 T ims;
217
218 Name name("/insertandremovebyname");
219
220 uint32_t content1 = 1;
221 shared_ptr<Data> data1 = makeData(name);
222 data1->setFreshnessPeriod(time::milliseconds(99999));
223 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
224 signData(data1);
225 ims.insert(*data1);
226
227 uint32_t content2 = 2;
228 shared_ptr<Data> data2 = makeData(name);
229 data2->setFreshnessPeriod(time::milliseconds(99999));
230 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
231 signData(data2);
232 ims.insert(*data2);
233
234 shared_ptr<Data> data3 = makeData("/insertandremovebyname/1");
235 ims.insert(*data3);
236
237 shared_ptr<Data> data4 = makeData("/insertandremovebyname/2");
238 ims.insert(*data4);
239
240 BOOST_CHECK_EQUAL(ims.size(), 4);
241
242 ims.erase(data1->getFullName(), false);
243 BOOST_CHECK_EQUAL(ims.size(), 3);
244}
245
246BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages)
247{
248 T ims;
249
250 shared_ptr<Data> data = makeData("/a");
251 ims.insert(*data);
252
253 shared_ptr<Data> data2 = makeData("/b");
254 ims.insert(*data2);
255
256 shared_ptr<Data> data3 = makeData("/c");
257 ims.insert(*data3);
258
259 shared_ptr<Data> data4 = makeData("/d");
260 ims.insert(*data4);
261
262 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
263 ims.insert(*data5);
264
265 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
266 ims.insert(*data6);
267
268 shared_ptr<Data> data7 = makeData("/c/c/1");
269 ims.insert(*data7);
270
271 BOOST_CHECK_EQUAL(ims.size(), 7);
272
273 Name name("/c");
274 ims.erase(name);
275 BOOST_CHECK_EQUAL(ims.size(), 3);
276 BOOST_CHECK_EQUAL(ims.getCapacity(), 5);
277}
278
279BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages)
280{
281 shared_ptr<Data> data = makeData("/digest/compute");
282
283 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
284 data->wireEncode().size());
285 BOOST_CHECK_EQUAL(digest1->size(), 32);
286
287 InMemoryStorageEntry* entry = new InMemoryStorageEntry();
288 entry->setData(*data);
289
290 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
291 entry->getFullName()[-1].value_begin(),
292 entry->getFullName()[-1].value_end());
293}
294
295BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
296{
297 T ims;
298
299 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
300
301 for (int i = 0; i < 10; i++) {
302 std::ostringstream convert;
303 convert << i;
304 Name name("/" + convert.str());
305 shared_ptr<Data> data = makeData(name);
306 ims.insert(*data);
307 }
308
309 InMemoryStorage::const_iterator it = ims.begin();
310 InMemoryStorage::const_iterator tmp1 = it;
311 BOOST_REQUIRE(tmp1 == it);
312 InMemoryStorage::const_iterator tmp2 = tmp1++;
313 BOOST_REQUIRE(tmp2 != tmp1);
314 tmp2 = ++tmp1;
315 BOOST_REQUIRE(tmp2 == tmp1);
316
317 int i = 0;
318 for (;it != ims.end(); it++) {
319 std::ostringstream convert;
320 convert << i;
321 Name name("/" + convert.str());
322 BOOST_CHECK_EQUAL(it->getName(), name);
323 BOOST_CHECK_EQUAL((*it).getName(), name);
324 i++;
325 }
326}
327
328BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
329{
330 T ims;
331
332 shared_ptr<Data> data = makeData("/a");
333 ims.insert(*data);
334
335 shared_ptr<Data> data2 = makeData("/b");
336 ims.insert(*data2);
337
338 shared_ptr<Data> data3 = makeData("/c");
339 ims.insert(*data3);
340
341 shared_ptr<Data> data4 = makeData("/d");
342 ims.insert(*data4);
343
344 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
345 ims.insert(*data5);
346
347 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
348 ims.insert(*data6);
349
350 shared_ptr<Data> data7 = makeData("/c/c/1");
351 ims.insert(*data7);
352}
353
354BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
355{
356 T ims;
357
358 shared_ptr<Data> data = makeData("/a");
359 ims.insert(*data);
360
361 shared_ptr<Data> data2 = makeData("/b");
362 ims.insert(*data2);
363
364 shared_ptr<Data> data3 = makeData("/c");
365 ims.insert(*data3);
366
367 shared_ptr<Data> data4 = makeData("/d");
368 ims.insert(*data4);
369
370 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
371 ims.insert(*data5);
372
373 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
374 ims.insert(*data6);
375
376 shared_ptr<Data> data7 = makeData("/c/c/1");
377 ims.insert(*data7);
378
379 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
380 data->wireEncode().size());
381
382 Name name("/a");
383 ims.erase(name);
384 BOOST_CHECK_EQUAL(ims.size(), 6);
385}
386
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800387/// @todo Expected failures, needs to be fixed as part of Issue #2118
388BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(ImplicitDigestSelector, 1) // doesn't work with
389 // templated test cases
390// BOOST_AUTO_TEST_CASE_TEMPLATE(ImplicitDigestSelector, T, InMemoryStorages)
391BOOST_AUTO_TEST_CASE(ImplicitDigestSelector)
Jiewen Tan99135962014-09-20 02:18:53 -0700392{
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800393 typedef boost::mpl::front<InMemoryStorages>::type T;
Jiewen Tan99135962014-09-20 02:18:53 -0700394 T ims;
395
396 Name name("/digest/works");
397 shared_ptr<Data> data = makeData(name);
398 ims.insert(*data);
399
400 shared_ptr<Data> data2 = makeData("/a");
401 ims.insert(*data2);
402
403 shared_ptr<Data> data3 = makeData("/z/z/z");
404 ims.insert(*data3);
405
406 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
407 data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700408
409 shared_ptr<Interest> interest = makeInterest("");
410 interest->setName(Name(name).append(digest1->buf(), digest1->size()));
411 interest->setMinSuffixComponents(0);
412 interest->setMaxSuffixComponents(0);
413
414 shared_ptr<const Data> found = ims.find(*interest);
415 BOOST_CHECK(static_cast<bool>(found));
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800416 // If changed to BOOST_REQUIRE, EXPECTED_FAILURES does not work
417 if (static_cast<bool>(found)) {
418 BOOST_CHECK_EQUAL(found->getName(), name);
Jiewen Tan99135962014-09-20 02:18:53 -0700419
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800420 shared_ptr<Interest> interest2 = makeInterest("");
421 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
422 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
423 interest2->setName(Name(name).append(digest2, 32));
424 interest2->setMinSuffixComponents(0);
425 interest2->setMaxSuffixComponents(0);
Jiewen Tan99135962014-09-20 02:18:53 -0700426
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800427 shared_ptr<const Data> notfound = ims.find(*interest2);
428 BOOST_CHECK(static_cast<bool>(found));
429 }
Jiewen Tan99135962014-09-20 02:18:53 -0700430}
431
432BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector, T, InMemoryStorages)
433{
434 T ims;
435
436 shared_ptr<Data> data = makeData("/a");
437 ims.insert(*data);
438
439 shared_ptr<Data> data2 = makeData("/b");
440 ims.insert(*data2);
441
442 shared_ptr<Data> data4 = makeData("/d");
443 ims.insert(*data4);
444
445 shared_ptr<Data> data5 = makeData("/c/c");
446 ims.insert(*data5);
447
448 shared_ptr<Data> data6 = makeData("/c/f");
449 ims.insert(*data6);
450
451 shared_ptr<Data> data7 = makeData("/c/n");
452 ims.insert(*data7);
453
454 shared_ptr<Interest> interest = makeInterest("/c");
455 interest->setChildSelector(1);
456
457 shared_ptr<const Data> found = ims.find(*interest);
458 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
459
460 shared_ptr<Interest> interest2 = makeInterest("/c");
461 interest2->setChildSelector(0);
462
463 shared_ptr<const Data> found2 = ims.find(*interest2);
464 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
465}
466
467BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector2, T, InMemoryStorages)
468{
469 T ims;
470
471 shared_ptr<Data> data = makeData("/a/b/1");
472 ims.insert(*data);
473
474 shared_ptr<Data> data2 = makeData("/a/b/2");
475 ims.insert(*data2);
476
477 shared_ptr<Data> data3 = makeData("/a/z/1");
478 ims.insert(*data3);
479
480 shared_ptr<Data> data4 = makeData("/a/z/2");
481 ims.insert(*data4);
482
483 shared_ptr<Interest> interest = makeInterest("/a");
484 interest->setChildSelector(1);
485
486 shared_ptr<const Data> found = ims.find(*interest);
487 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
488}
489
490BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector, T, InMemoryStorages)
491{
492 T ims;
493
494 Name name("/insert/withkey");
495 shared_ptr<Data> data = makeData(name);
496 ims.insert(*data);
497
498 shared_ptr<Interest> interest = makeInterest(name);
499 Name keyName("/somewhere/key");
500
501 ndn::KeyLocator locator(keyName);
502 interest->setPublisherPublicKeyLocator(locator);
503
504 shared_ptr<const Data> found = ims.find(*interest);
505 BOOST_CHECK(!static_cast<bool>(found));
506}
507
508BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector2, T, InMemoryStorages)
509{
510 T ims;
511 Name name("/insert/withkey");
512 shared_ptr<Data> data = makeData(name);
513 ims.insert(*data);
514
515 Name name2("/insert/withkey2");
516 shared_ptr<Data> data2 = make_shared<Data>(name2);
517
518 Name keyName("/somewhere/key");
519 const ndn::KeyLocator locator(keyName);
520
521 ndn::SignatureSha256WithRsa fakeSignature;
522 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
523 reinterpret_cast<const uint8_t*>(0), 0));
524
525 fakeSignature.setKeyLocator(locator);
526 data2->setSignature(fakeSignature);
527 data2->wireEncode();
528
529 ims.insert(*data2);
530
531 shared_ptr<Interest> interest = makeInterest(name2);
532 interest->setPublisherPublicKeyLocator(locator);
533
534 shared_ptr<const Data> found = ims.find(*interest);
535 BOOST_CHECK(static_cast<bool>(found));
536 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
537}
538
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800539/// @todo Expected failures, needs to be fixed as part of Issue #2118
540BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinMaxComponentsSelector, 1) // doesn't work with
541 // templated test cases
542// BOOST_AUTO_TEST_CASE_TEMPLATE(MinMaxComponentsSelector, T, InMemoryStorages)
543BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
Jiewen Tan99135962014-09-20 02:18:53 -0700544{
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800545 typedef boost::mpl::front<InMemoryStorages>::type T;
Jiewen Tan99135962014-09-20 02:18:53 -0700546 T ims;
547
548 shared_ptr<Data> data = makeData("/a");
549 ims.insert(*data);
550
551 shared_ptr<Data> data2 = makeData("/b");
552 ims.insert(*data2);
553
554 shared_ptr<Data> data4 = makeData("/d");
555 ims.insert(*data4);
556
557 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
558 ims.insert(*data5);
559
560 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
561 ims.insert(*data6);
562
563 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
564 ims.insert(*data7);
565
566 shared_ptr<Data> data8 = makeData("/c/c/1");
567 ims.insert(*data8);
568
569 shared_ptr<Interest> interest = makeInterest("/c/c");
570 interest->setMinSuffixComponents(3);
571 interest->setChildSelector(0);
572
573 shared_ptr<const Data> found = ims.find(*interest);
574 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6");
575
576 shared_ptr<Interest> interest2 = makeInterest("/c/c");
577 interest2->setMinSuffixComponents(4);
578 interest2->setChildSelector(1);
579
580 shared_ptr<const Data> found2 = ims.find(*interest2);
581 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
582
583 shared_ptr<Interest> interest3 = makeInterest("/c/c");
584 interest3->setMaxSuffixComponents(2);
585 interest3->setChildSelector(1);
586
587 shared_ptr<const Data> found3 = ims.find(*interest3);
588 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
589}
590
591BOOST_AUTO_TEST_CASE_TEMPLATE(ExcludeSelector, T, InMemoryStorages)
592{
593 T ims;
594
595 shared_ptr<Data> data = makeData("/a");
596 ims.insert(*data);
597
598 shared_ptr<Data> data2 = makeData("/b");
599 ims.insert(*data2);
600
601 shared_ptr<Data> data3 = makeData("/c/a");
602 ims.insert(*data3);
603
604 shared_ptr<Data> data4 = makeData("/d");
605 ims.insert(*data4);
606
607 shared_ptr<Data> data5 = makeData("/c/c");
608 ims.insert(*data5);
609
610 shared_ptr<Data> data6 = makeData("/c/f");
611 ims.insert(*data6);
612
613 shared_ptr<Data> data7 = makeData("/c/n");
614 ims.insert(*data7);
615
616 shared_ptr<Interest> interest = makeInterest("/c");
617 interest->setChildSelector(1);
618 Exclude e;
619 e.excludeOne (Name::Component("n"));
620 interest->setExclude(e);
621
622 shared_ptr<const Data> found = ims.find(*interest);
623 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
624
625 shared_ptr<Interest> interest2 = makeInterest("/c");
626 interest2->setChildSelector(0);
627
628 Exclude e2;
629 e2.excludeOne (Name::Component("a"));
630 interest2->setExclude(e2);
631
632 shared_ptr<const Data> found2 = ims.find(*interest2);
633 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
634
635 shared_ptr<Interest> interest3 = makeInterest("/c");
636 interest3->setChildSelector(0);
637
638 Exclude e3;
639 e3.excludeOne (Name::Component("c"));
640 interest3->setExclude(e3);
641
642 shared_ptr<const Data> found3 = ims.find(*interest3);
643 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
644}
645
646typedef boost::mpl::list<InMemoryStorageFifo, InMemoryStorageLfu, InMemoryStorageLru>
647 InMemoryStoragesLimited;
648
649BOOST_AUTO_TEST_CASE_TEMPLATE(setCapacity, T, InMemoryStoragesLimited)
650{
651 T ims;
652
653 ims.setCapacity(3);
654 ims.insert(*makeData("/1"));
655 ims.insert(*makeData("/2"));
656 ims.insert(*makeData("/3"));
657 BOOST_CHECK_EQUAL(ims.size(), 3);
658
659 ims.setCapacity(2);
660 BOOST_CHECK_EQUAL(ims.size(), 2);
661}
662
663BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
664{
665 T ims(10000);
666
667 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
668
669 T ims2(4);
670
671 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
672}
673
674BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
675{
676 T ims(40);
677
678 for (int i = 0; i < 11; i++) {
679 std::ostringstream convert;
680 convert << i;
681 Name name("/" + convert.str());
682 shared_ptr<Data> data = makeData(name);
683 data->setFreshnessPeriod(time::milliseconds(5000));
684 signData(data);
685 ims.insert(*data);
686 }
687
688 BOOST_CHECK_EQUAL(ims.size(), 11);
689
690 BOOST_CHECK_EQUAL(ims.getCapacity(), 20);
691}
692
693BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
694{
695 T ims(2);
696
697 Name name("/insert/1");
698 shared_ptr<Data> data = makeData(name);
699 ims.insert(*data);
700
701 Name name2("/insert/2");
702 shared_ptr<Data> data2 = makeData(name2);
703 ims.insert(*data2);
704
705 Name name3("/insert/3");
706 shared_ptr<Data> data3 = makeData(name3);
707 ims.insert(*data3);
708
709 BOOST_CHECK_EQUAL(ims.size(), 2);
710
711 shared_ptr<Interest> interest = makeInterest(name);
712 shared_ptr<const Data> found = ims.find(*interest);
713 BOOST_CHECK(!static_cast<bool>(found));
714}
715
716///as Find function is implemented at the base case, therefore testing for one derived class is
717///sufficient for all
718class FindFixture
719{
720protected:
721 void
722 insert(uint32_t id, const Name& name)
723 {
724 shared_ptr<Data> data = makeData(name);
725 data->setFreshnessPeriod(time::milliseconds(99999));
726 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
727 signData(data);
728
729 m_ims.insert(*data);
730 }
731
732 Interest&
733 startInterest(const Name& name)
734 {
735 m_interest = makeInterest(name);
736 return *m_interest;
737 }
738
739 uint32_t
740 find()
741 {
742 shared_ptr<const Data> found = m_ims.find(*m_interest);
743 if (found == 0) {
744 return 0;
745 }
746 const Block& content = found->getContent();
747 if (content.value_size() != sizeof(uint32_t)) {
748 return 0;
749 }
750 return *reinterpret_cast<const uint32_t*>(content.value());
751 }
752
753protected:
754 InMemoryStoragePersistent m_ims;
755 shared_ptr<Interest> m_interest;
756};
757
758BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
759
760BOOST_AUTO_TEST_CASE(EmptyDataName)
761{
762 insert(1, "ndn:/");
763
764 startInterest("ndn:/");
765 BOOST_CHECK_EQUAL(find(), 1);
766}
767
768BOOST_AUTO_TEST_CASE(EmptyInterestName)
769{
770 insert(1, "ndn:/A");
771
772 startInterest("ndn:/");
773 BOOST_CHECK_EQUAL(find(), 1);
774}
775
776BOOST_AUTO_TEST_CASE(Leftmost)
777{
778 insert(1, "ndn:/A");
779 insert(2, "ndn:/B/p/1");
780 insert(3, "ndn:/B/p/2");
781 insert(4, "ndn:/B/q/1");
782 insert(5, "ndn:/B/q/2");
783 insert(6, "ndn:/C");
784
785 startInterest("ndn:/B");
786 BOOST_CHECK_EQUAL(find(), 2);
787}
788
789BOOST_AUTO_TEST_CASE(Rightmost)
790{
791 insert(1, "ndn:/A");
792 insert(2, "ndn:/B/p/1");
793 insert(3, "ndn:/B/p/2");
794 insert(4, "ndn:/B/q/1");
795 insert(5, "ndn:/B/q/2");
796 insert(6, "ndn:/C");
797
798 startInterest("ndn:/B")
799 .setChildSelector(1);
800 BOOST_CHECK_EQUAL(find(), 4);
801}
802
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800803/// @todo Expected failures, needs to be fixed as part of Issue #2118
804BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(Leftmost_ExactName1, 1)
Jiewen Tan99135962014-09-20 02:18:53 -0700805BOOST_AUTO_TEST_CASE(Leftmost_ExactName1)
806{
807 insert(1, "ndn:/");
808 insert(2, "ndn:/A/B");
809 insert(3, "ndn:/A/C");
810 insert(4, "ndn:/A");
811 insert(5, "ndn:/D");
812
813 // Intuitively you would think Data 4 should be between Data 1 and 2,
814 // but Data 4 has full Name ndn:/A/<32-octet hash>.
815 startInterest("ndn:/A");
816 BOOST_CHECK_EQUAL(find(), 2);
817}
818
819BOOST_AUTO_TEST_CASE(Leftmost_ExactName33)
820{
821 insert(1, "ndn:/");
822 insert(2, "ndn:/A");
823 insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's
824 insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
825 insert(5, "ndn:/D");
826
827 // Data 2 is returned, because <32-octet hash> is less than Data 3.
828 startInterest("ndn:/A");
829 BOOST_CHECK_EQUAL(find(), 2);
830}
831
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800832/// @todo Expected failures, needs to be fixed as part of Issue #2118
833BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinSuffixComponents, 2)
Jiewen Tan99135962014-09-20 02:18:53 -0700834BOOST_AUTO_TEST_CASE(MinSuffixComponents)
835{
836 insert(1, "ndn:/A/1/2/3/4");
837 insert(2, "ndn:/B/1/2/3");
838 insert(3, "ndn:/C/1/2");
839 insert(4, "ndn:/D/1");
840 insert(5, "ndn:/E");
841 insert(6, "ndn:/");
842
843 startInterest("ndn:/")
844 .setChildSelector(1)
845 .setMinSuffixComponents(0);
846 BOOST_CHECK_EQUAL(find(), 6);
847
848 startInterest("ndn:/")
849 .setChildSelector(1)
850 .setMinSuffixComponents(1);
851 BOOST_CHECK_EQUAL(find(), 6);
852
853 startInterest("ndn:/")
854 .setChildSelector(1)
855 .setMinSuffixComponents(2);
856 BOOST_CHECK_EQUAL(find(), 5);
857
858 startInterest("ndn:/")
859 .setChildSelector(1)
860 .setMinSuffixComponents(3);
861 BOOST_CHECK_EQUAL(find(), 4);
862
863 startInterest("ndn:/")
864 .setChildSelector(1)
865 .setMinSuffixComponents(4);
866 BOOST_CHECK_EQUAL(find(), 3);
867
868 startInterest("ndn:/")
869 .setChildSelector(1)
870 .setMinSuffixComponents(5);
871 BOOST_CHECK_EQUAL(find(), 2);
872
873 startInterest("ndn:/")
874 .setChildSelector(1)
875 .setMinSuffixComponents(6);
876 BOOST_CHECK_EQUAL(find(), 1);
877
878 startInterest("ndn:/")
879 .setChildSelector(1)
880 .setMinSuffixComponents(7);
881 BOOST_CHECK_EQUAL(find(), 0);
882}
883
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800884/// @todo Expected failures, needs to be fixed as part of Issue #2118
885BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MaxSuffixComponents, 5)
Jiewen Tan99135962014-09-20 02:18:53 -0700886BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
887{
888 insert(1, "ndn:/");
889 insert(2, "ndn:/A");
890 insert(3, "ndn:/A/B");
891 insert(4, "ndn:/A/B/C");
892 insert(5, "ndn:/A/B/C/D");
893 insert(6, "ndn:/A/B/C/D/E");
894 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
895
896 startInterest("ndn:/")
897 .setMaxSuffixComponents(0);
898 BOOST_CHECK_EQUAL(find(), 0);
899
900 startInterest("ndn:/")
901 .setMaxSuffixComponents(1);
902 BOOST_CHECK_EQUAL(find(), 1);
903
904 startInterest("ndn:/")
905 .setMaxSuffixComponents(2);
906 BOOST_CHECK_EQUAL(find(), 2);
907
908 startInterest("ndn:/")
909 .setMaxSuffixComponents(3);
910 BOOST_CHECK_EQUAL(find(), 3);
911
912 startInterest("ndn:/")
913 .setMaxSuffixComponents(4);
914 BOOST_CHECK_EQUAL(find(), 4);
915
916 startInterest("ndn:/")
917 .setMaxSuffixComponents(5);
918 BOOST_CHECK_EQUAL(find(), 5);
919
920 startInterest("ndn:/")
921 .setMaxSuffixComponents(6);
922 BOOST_CHECK_EQUAL(find(), 6);
923}
924
925BOOST_AUTO_TEST_CASE(DigestOrder)
926{
927 insert(1, "ndn:/A");
928 insert(2, "ndn:/A");
929 // We don't know which comes first, but there must be some order
930
931 startInterest("ndn:/A")
932 .setChildSelector(0);
933 uint32_t leftmost = find();
934
935 startInterest("ndn:/A")
936 .setChildSelector(1);
937 uint32_t rightmost = find();
938
939 BOOST_CHECK_NE(leftmost, rightmost);
940}
941
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800942/// @todo Expected failures, needs to be fixed as part of Issue #2118
943BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(DigestExclude, 1)
Jiewen Tan99135962014-09-20 02:18:53 -0700944BOOST_AUTO_TEST_CASE(DigestExclude)
945{
946 insert(1, "ndn:/A/B");
947 insert(2, "ndn:/A");
948 insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's
949
950 startInterest("ndn:/A")
951 .setExclude(Exclude().excludeBefore(name::Component(reinterpret_cast<const uint8_t*>(
952 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
953 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's
954 BOOST_CHECK_EQUAL(find(), 2);
955
956 startInterest("ndn:/A")
957 .setChildSelector(1)
958 .setExclude(Exclude().excludeAfter(name::Component(reinterpret_cast<const uint8_t*>(
959 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
960 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
961 "\x00"), 33))); // 33 0x00's
962 BOOST_CHECK_EQUAL(find(), 2);
963}
964
965BOOST_AUTO_TEST_CASE(ExactName32)
966{
967 insert(1, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 32 'B's
968 insert(2, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 32 'C's
969
970 startInterest("ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
971 BOOST_CHECK_EQUAL(find(), 1);
972}
973
Alexander Afanasyevafee0a52014-11-07 11:15:34 -0800974/// @todo Expected failures, needs to be fixed as part of Issue #2118
975BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MinSuffixComponents32, 2)
Jiewen Tan99135962014-09-20 02:18:53 -0700976BOOST_AUTO_TEST_CASE(MinSuffixComponents32)
977{
978 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/1/2/3/4"); // 32 'x's
979 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/B/1/2/3");
980 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/C/1/2");
981 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/D/1");
982 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/E");
983 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
984
985 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
986 .setChildSelector(1)
987 .setMinSuffixComponents(0);
988 BOOST_CHECK_EQUAL(find(), 6);
989
990 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
991 .setChildSelector(1)
992 .setMinSuffixComponents(1);
993 BOOST_CHECK_EQUAL(find(), 6);
994
995 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
996 .setChildSelector(1)
997 .setMinSuffixComponents(2);
998 BOOST_CHECK_EQUAL(find(), 5);
999
1000 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1001 .setChildSelector(1)
1002 .setMinSuffixComponents(3);
1003 BOOST_CHECK_EQUAL(find(), 4);
1004
1005 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1006 .setChildSelector(1)
1007 .setMinSuffixComponents(4);
1008 BOOST_CHECK_EQUAL(find(), 3);
1009
1010 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1011 .setChildSelector(1)
1012 .setMinSuffixComponents(5);
1013 BOOST_CHECK_EQUAL(find(), 2);
1014
1015 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1016 .setChildSelector(1)
1017 .setMinSuffixComponents(6);
1018 BOOST_CHECK_EQUAL(find(), 1);
1019
1020 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1021 .setChildSelector(1)
1022 .setMinSuffixComponents(7);
1023 BOOST_CHECK_EQUAL(find(), 0);
1024}
1025
Alexander Afanasyevafee0a52014-11-07 11:15:34 -08001026/// @todo Expected failures, needs to be fixed as part of Issue #2118
1027BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MaxSuffixComponents32, 5)
Jiewen Tan99135962014-09-20 02:18:53 -07001028BOOST_AUTO_TEST_CASE(MaxSuffixComponents32)
1029{
1030 insert(1, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"); // 32 'x's
1031 insert(2, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A");
1032 insert(3, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B");
1033 insert(4, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C");
1034 insert(5, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D");
1035 insert(6, "ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/A/B/C/D/E");
1036 // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component.
1037
1038 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1039 .setMaxSuffixComponents(0);
1040 BOOST_CHECK_EQUAL(find(), 0);
1041
1042 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1043 .setMaxSuffixComponents(1);
1044 BOOST_CHECK_EQUAL(find(), 1);
1045
1046 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1047 .setMaxSuffixComponents(2);
1048 BOOST_CHECK_EQUAL(find(), 2);
1049
1050 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1051 .setMaxSuffixComponents(3);
1052 BOOST_CHECK_EQUAL(find(), 3);
1053
1054 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1055 .setMaxSuffixComponents(4);
1056 BOOST_CHECK_EQUAL(find(), 4);
1057
1058 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1059 .setMaxSuffixComponents(5);
1060 BOOST_CHECK_EQUAL(find(), 5);
1061
1062 startInterest("ndn:/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
1063 .setMaxSuffixComponents(6);
1064 BOOST_CHECK_EQUAL(find(), 6);
1065}
1066
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -07001067BOOST_AUTO_TEST_SUITE_END() // Find
1068BOOST_AUTO_TEST_SUITE_END() // Common
1069BOOST_AUTO_TEST_SUITE_END() // UtilInMemoryStorage
Jiewen Tan99135962014-09-20 02:18:53 -07001070
1071} // namespace util
1072} // namespace ndn