blob: 9e7c814fb8863cc71c43ee5f34a040101017a8f0 [file] [log] [blame]
Jiewen Tan99135962014-09-20 02:18:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu404eafd2016-03-06 14:54:25 -08003 * Copyright (c) 2013-2016 Regents of the University of California.
Jiewen Tan99135962014-09-20 02:18:53 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "util/in-memory-storage-persistent.hpp"
23#include "util/in-memory-storage-fifo.hpp"
24#include "util/in-memory-storage-lfu.hpp"
25#include "util/in-memory-storage-lru.hpp"
26#include "security/key-chain.hpp"
27
28#include "boost-test.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029#include "../make-interest-data.hpp"
Yingdi Yu404eafd2016-03-06 14:54:25 -080030#include "../unit-test-time-fixture.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070031
32#include <boost/mpl/list.hpp>
33
34namespace ndn {
35namespace util {
Junxiao Shi85d90832016-08-04 03:19:46 +000036namespace tests {
Jiewen Tan99135962014-09-20 02:18:53 -070037
Junxiao Shi85d90832016-08-04 03:19:46 +000038using namespace ndn::tests;
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070039
Junxiao Shi85d90832016-08-04 03:19:46 +000040BOOST_AUTO_TEST_SUITE(Util)
41BOOST_AUTO_TEST_SUITE(TestInMemoryStorage)
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070042BOOST_AUTO_TEST_SUITE(Common)
43
Jiewen Tan99135962014-09-20 02:18:53 -070044typedef boost::mpl::list<InMemoryStoragePersistent, InMemoryStorageFifo, InMemoryStorageLfu,
45 InMemoryStorageLru> InMemoryStorages;
46
Jiewen Tan99135962014-09-20 02:18:53 -070047BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages)
48{
49 T ims;
50
51 ims.insert(*makeData("/insertion"));
52}
53
54BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages)
55{
56 T ims;
57
58 ims.insert(*makeData("/a"));
59 ims.insert(*makeData("/b"));
60 ims.insert(*makeData("/c"));
61 ims.insert(*makeData("/d"));
62
63 BOOST_CHECK_EQUAL(ims.size(), 4);
64}
65
66BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion3, T, InMemoryStorages)
67{
68 T ims;
69
70 Name name("/a");
71
72 uint32_t content1 = 1;
73 shared_ptr<Data> data1 = makeData(name);
74 data1->setFreshnessPeriod(time::milliseconds(99999));
75 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
76 signData(data1);
77 ims.insert(*data1);
78
79 uint32_t content2 = 2;
80 shared_ptr<Data> data2 = makeData(name);
81 data2->setFreshnessPeriod(time::milliseconds(99999));
82 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
83 signData(data2);
84 ims.insert(*data2);
85
86 BOOST_CHECK_EQUAL(ims.size(), 2);
87}
88
89BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion, T, InMemoryStorages)
90{
91 T ims;
92
93 shared_ptr<Data> data0 = makeData("/insert/smth");
94 ims.insert(*data0);
95
96 shared_ptr<Data> data = makeData("/insert/duplicate");
97 ims.insert(*data);
98
99 ims.insert(*data);
100 BOOST_CHECK_EQUAL(ims.size(), 2);
101}
102
103BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion2, T, InMemoryStorages)
104{
105 T ims;
106
107 shared_ptr<Data> data = makeData("/insert/duplicate");
108 ims.insert(*data);
109
110 ims.insert(*data);
111 BOOST_CHECK_EQUAL(ims.size(), 1);
112
113 shared_ptr<Data> data2 = makeData("/insert/original");
114 ims.insert(*data2);
115 BOOST_CHECK_EQUAL(ims.size(), 2);
116}
117
118BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFind, T, InMemoryStorages)
119{
120 T ims;
121
122 Name name("/insert/and/find");
123
124 shared_ptr<Data> data = makeData(name);
125 ims.insert(*data);
126
127 shared_ptr<Interest> interest = makeInterest(name);
128
129 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000130 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700131 BOOST_CHECK_EQUAL(data->getName(), found->getName());
132}
133
134BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages)
135{
136 T ims;
137
138 Name name("/insert/and/find");
139 shared_ptr<Data> data = makeData(name);
140 ims.insert(*data);
141
142 Name name2("/not/find");
143 shared_ptr<Interest> interest = makeInterest(name2);
144
145 shared_ptr<const Data> found = ims.find(*interest);
146
147 BOOST_CHECK_EQUAL(found.get(), static_cast<const Data*>(0));
148}
149
150BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages)
151{
152 T ims;
153
154 Name name("/insert/and/find");
155
156 shared_ptr<Data> data = makeData(name);
157 ims.insert(*data);
158
159 shared_ptr<const Data> found = ims.find(name);
Junxiao Shi85d90832016-08-04 03:19:46 +0000160 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700161 BOOST_CHECK_EQUAL(data->getName(), found->getName());
162}
163
164BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages)
165{
166 T ims;
167
168 Name name("/insert/and/find");
169
170 shared_ptr<Data> data = makeData(name);
171 ims.insert(*data);
172
173 shared_ptr<const Data> found = ims.find(data->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000174 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700175 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);
Junxiao Shi85d90832016-08-04 03:19:46 +0000189 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700190}
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());
Junxiao Shi85d90832016-08-04 03:19:46 +0000209 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700210}
211
212BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages)
213{
214 T ims;
215
216 Name name("/insertandremovebyname");
217
218 uint32_t content1 = 1;
219 shared_ptr<Data> data1 = makeData(name);
220 data1->setFreshnessPeriod(time::milliseconds(99999));
221 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
222 signData(data1);
223 ims.insert(*data1);
224
225 uint32_t content2 = 2;
226 shared_ptr<Data> data2 = makeData(name);
227 data2->setFreshnessPeriod(time::milliseconds(99999));
228 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
229 signData(data2);
230 ims.insert(*data2);
231
232 shared_ptr<Data> data3 = makeData("/insertandremovebyname/1");
233 ims.insert(*data3);
234
235 shared_ptr<Data> data4 = makeData("/insertandremovebyname/2");
236 ims.insert(*data4);
237
238 BOOST_CHECK_EQUAL(ims.size(), 4);
239
240 ims.erase(data1->getFullName(), false);
241 BOOST_CHECK_EQUAL(ims.size(), 3);
242}
243
244BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages)
245{
246 T ims;
247
248 shared_ptr<Data> data = makeData("/a");
249 ims.insert(*data);
250
251 shared_ptr<Data> data2 = makeData("/b");
252 ims.insert(*data2);
253
254 shared_ptr<Data> data3 = makeData("/c");
255 ims.insert(*data3);
256
257 shared_ptr<Data> data4 = makeData("/d");
258 ims.insert(*data4);
259
260 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
261 ims.insert(*data5);
262
263 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
264 ims.insert(*data6);
265
266 shared_ptr<Data> data7 = makeData("/c/c/1");
267 ims.insert(*data7);
268
269 BOOST_CHECK_EQUAL(ims.size(), 7);
270
271 Name name("/c");
272 ims.erase(name);
273 BOOST_CHECK_EQUAL(ims.size(), 3);
274 BOOST_CHECK_EQUAL(ims.getCapacity(), 5);
275}
276
277BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages)
278{
279 shared_ptr<Data> data = makeData("/digest/compute");
280
281 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
282 data->wireEncode().size());
283 BOOST_CHECK_EQUAL(digest1->size(), 32);
284
285 InMemoryStorageEntry* entry = new InMemoryStorageEntry();
286 entry->setData(*data);
287
288 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
289 entry->getFullName()[-1].value_begin(),
290 entry->getFullName()[-1].value_end());
291}
292
293BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
294{
295 T ims;
296
297 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
298
299 for (int i = 0; i < 10; i++) {
300 std::ostringstream convert;
301 convert << i;
302 Name name("/" + convert.str());
303 shared_ptr<Data> data = makeData(name);
304 ims.insert(*data);
305 }
306
307 InMemoryStorage::const_iterator it = ims.begin();
308 InMemoryStorage::const_iterator tmp1 = it;
309 BOOST_REQUIRE(tmp1 == it);
310 InMemoryStorage::const_iterator tmp2 = tmp1++;
311 BOOST_REQUIRE(tmp2 != tmp1);
312 tmp2 = ++tmp1;
313 BOOST_REQUIRE(tmp2 == tmp1);
314
315 int i = 0;
316 for (;it != ims.end(); it++) {
317 std::ostringstream convert;
318 convert << i;
319 Name name("/" + convert.str());
320 BOOST_CHECK_EQUAL(it->getName(), name);
321 BOOST_CHECK_EQUAL((*it).getName(), name);
322 i++;
323 }
324}
325
326BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
327{
328 T ims;
329
330 shared_ptr<Data> data = makeData("/a");
331 ims.insert(*data);
332
333 shared_ptr<Data> data2 = makeData("/b");
334 ims.insert(*data2);
335
336 shared_ptr<Data> data3 = makeData("/c");
337 ims.insert(*data3);
338
339 shared_ptr<Data> data4 = makeData("/d");
340 ims.insert(*data4);
341
342 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
343 ims.insert(*data5);
344
345 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
346 ims.insert(*data6);
347
348 shared_ptr<Data> data7 = makeData("/c/c/1");
349 ims.insert(*data7);
350}
351
352BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
353{
354 T ims;
355
356 shared_ptr<Data> data = makeData("/a");
357 ims.insert(*data);
358
359 shared_ptr<Data> data2 = makeData("/b");
360 ims.insert(*data2);
361
362 shared_ptr<Data> data3 = makeData("/c");
363 ims.insert(*data3);
364
365 shared_ptr<Data> data4 = makeData("/d");
366 ims.insert(*data4);
367
368 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
369 ims.insert(*data5);
370
371 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
372 ims.insert(*data6);
373
374 shared_ptr<Data> data7 = makeData("/c/c/1");
375 ims.insert(*data7);
376
377 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
378 data->wireEncode().size());
379
380 Name name("/a");
381 ims.erase(name);
382 BOOST_CHECK_EQUAL(ims.size(), 6);
383}
384
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800385BOOST_AUTO_TEST_CASE_TEMPLATE(ImplicitDigestSelector, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -0700386{
387 T ims;
388
389 Name name("/digest/works");
390 shared_ptr<Data> data = makeData(name);
391 ims.insert(*data);
392
393 shared_ptr<Data> data2 = makeData("/a");
394 ims.insert(*data2);
395
396 shared_ptr<Data> data3 = makeData("/z/z/z");
397 ims.insert(*data3);
398
399 ndn::ConstBufferPtr digest1 = ndn::crypto::sha256(data->wireEncode().wire(),
400 data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700401
402 shared_ptr<Interest> interest = makeInterest("");
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800403 interest->setName(Name(name).appendImplicitSha256Digest(digest1->buf(), digest1->size()));
Jiewen Tan99135962014-09-20 02:18:53 -0700404 interest->setMinSuffixComponents(0);
405 interest->setMaxSuffixComponents(0);
406
407 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000408 BOOST_REQUIRE(found != nullptr);
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800409 BOOST_CHECK_EQUAL(found->getName(), name);
410
411 shared_ptr<Interest> interest2 = makeInterest("");
412 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
413 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
414 interest2->setName(Name(name).appendImplicitSha256Digest(digest2, 32));
415 interest2->setMinSuffixComponents(0);
416 interest2->setMaxSuffixComponents(0);
417
418 shared_ptr<const Data> notfound = ims.find(*interest2);
Junxiao Shi85d90832016-08-04 03:19:46 +0000419 BOOST_CHECK(notfound == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700420}
421
422BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector, T, InMemoryStorages)
423{
424 T ims;
425
426 shared_ptr<Data> data = makeData("/a");
427 ims.insert(*data);
428
429 shared_ptr<Data> data2 = makeData("/b");
430 ims.insert(*data2);
431
432 shared_ptr<Data> data4 = makeData("/d");
433 ims.insert(*data4);
434
435 shared_ptr<Data> data5 = makeData("/c/c");
436 ims.insert(*data5);
437
438 shared_ptr<Data> data6 = makeData("/c/f");
439 ims.insert(*data6);
440
441 shared_ptr<Data> data7 = makeData("/c/n");
442 ims.insert(*data7);
443
444 shared_ptr<Interest> interest = makeInterest("/c");
445 interest->setChildSelector(1);
446
447 shared_ptr<const Data> found = ims.find(*interest);
448 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
449
450 shared_ptr<Interest> interest2 = makeInterest("/c");
451 interest2->setChildSelector(0);
452
453 shared_ptr<const Data> found2 = ims.find(*interest2);
454 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
455}
456
457BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector2, T, InMemoryStorages)
458{
459 T ims;
460
461 shared_ptr<Data> data = makeData("/a/b/1");
462 ims.insert(*data);
463
464 shared_ptr<Data> data2 = makeData("/a/b/2");
465 ims.insert(*data2);
466
467 shared_ptr<Data> data3 = makeData("/a/z/1");
468 ims.insert(*data3);
469
470 shared_ptr<Data> data4 = makeData("/a/z/2");
471 ims.insert(*data4);
472
473 shared_ptr<Interest> interest = makeInterest("/a");
474 interest->setChildSelector(1);
475
476 shared_ptr<const Data> found = ims.find(*interest);
477 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
478}
479
480BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector, T, InMemoryStorages)
481{
482 T ims;
483
484 Name name("/insert/withkey");
485 shared_ptr<Data> data = makeData(name);
486 ims.insert(*data);
487
488 shared_ptr<Interest> interest = makeInterest(name);
489 Name keyName("/somewhere/key");
490
491 ndn::KeyLocator locator(keyName);
492 interest->setPublisherPublicKeyLocator(locator);
493
494 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000495 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700496}
497
498BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector2, T, InMemoryStorages)
499{
500 T ims;
501 Name name("/insert/withkey");
502 shared_ptr<Data> data = makeData(name);
503 ims.insert(*data);
504
505 Name name2("/insert/withkey2");
506 shared_ptr<Data> data2 = make_shared<Data>(name2);
507
508 Name keyName("/somewhere/key");
509 const ndn::KeyLocator locator(keyName);
510
511 ndn::SignatureSha256WithRsa fakeSignature;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700512 fakeSignature.setValue(makeEmptyBlock(tlv::SignatureValue));
Jiewen Tan99135962014-09-20 02:18:53 -0700513
514 fakeSignature.setKeyLocator(locator);
515 data2->setSignature(fakeSignature);
516 data2->wireEncode();
517
518 ims.insert(*data2);
519
520 shared_ptr<Interest> interest = makeInterest(name2);
521 interest->setPublisherPublicKeyLocator(locator);
522
523 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000524 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700525 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
526}
527
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800528BOOST_AUTO_TEST_CASE_TEMPLATE(MinMaxComponentsSelector, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -0700529{
530 T ims;
531
532 shared_ptr<Data> data = makeData("/a");
533 ims.insert(*data);
534
535 shared_ptr<Data> data2 = makeData("/b");
536 ims.insert(*data2);
537
538 shared_ptr<Data> data4 = makeData("/d");
539 ims.insert(*data4);
540
541 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
542 ims.insert(*data5);
543
544 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
545 ims.insert(*data6);
546
547 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
548 ims.insert(*data7);
549
550 shared_ptr<Data> data8 = makeData("/c/c/1");
551 ims.insert(*data8);
552
553 shared_ptr<Interest> interest = makeInterest("/c/c");
554 interest->setMinSuffixComponents(3);
555 interest->setChildSelector(0);
556
557 shared_ptr<const Data> found = ims.find(*interest);
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800558 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3");
Jiewen Tan99135962014-09-20 02:18:53 -0700559
560 shared_ptr<Interest> interest2 = makeInterest("/c/c");
561 interest2->setMinSuffixComponents(4);
562 interest2->setChildSelector(1);
563
564 shared_ptr<const Data> found2 = ims.find(*interest2);
565 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
566
567 shared_ptr<Interest> interest3 = makeInterest("/c/c");
568 interest3->setMaxSuffixComponents(2);
569 interest3->setChildSelector(1);
570
571 shared_ptr<const Data> found3 = ims.find(*interest3);
572 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
573}
574
575BOOST_AUTO_TEST_CASE_TEMPLATE(ExcludeSelector, T, InMemoryStorages)
576{
577 T ims;
578
579 shared_ptr<Data> data = makeData("/a");
580 ims.insert(*data);
581
582 shared_ptr<Data> data2 = makeData("/b");
583 ims.insert(*data2);
584
585 shared_ptr<Data> data3 = makeData("/c/a");
586 ims.insert(*data3);
587
588 shared_ptr<Data> data4 = makeData("/d");
589 ims.insert(*data4);
590
591 shared_ptr<Data> data5 = makeData("/c/c");
592 ims.insert(*data5);
593
594 shared_ptr<Data> data6 = makeData("/c/f");
595 ims.insert(*data6);
596
597 shared_ptr<Data> data7 = makeData("/c/n");
598 ims.insert(*data7);
599
600 shared_ptr<Interest> interest = makeInterest("/c");
601 interest->setChildSelector(1);
602 Exclude e;
603 e.excludeOne (Name::Component("n"));
604 interest->setExclude(e);
605
606 shared_ptr<const Data> found = ims.find(*interest);
607 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
608
609 shared_ptr<Interest> interest2 = makeInterest("/c");
610 interest2->setChildSelector(0);
611
612 Exclude e2;
613 e2.excludeOne (Name::Component("a"));
614 interest2->setExclude(e2);
615
616 shared_ptr<const Data> found2 = ims.find(*interest2);
617 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
618
619 shared_ptr<Interest> interest3 = makeInterest("/c");
620 interest3->setChildSelector(0);
621
622 Exclude e3;
623 e3.excludeOne (Name::Component("c"));
624 interest3->setExclude(e3);
625
626 shared_ptr<const Data> found3 = ims.find(*interest3);
627 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
628}
629
630typedef boost::mpl::list<InMemoryStorageFifo, InMemoryStorageLfu, InMemoryStorageLru>
631 InMemoryStoragesLimited;
632
Yingdi Yu404eafd2016-03-06 14:54:25 -0800633BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited)
Jiewen Tan99135962014-09-20 02:18:53 -0700634{
635 T ims;
636
637 ims.setCapacity(3);
638 ims.insert(*makeData("/1"));
639 ims.insert(*makeData("/2"));
640 ims.insert(*makeData("/3"));
641 BOOST_CHECK_EQUAL(ims.size(), 3);
642
643 ims.setCapacity(2);
644 BOOST_CHECK_EQUAL(ims.size(), 2);
645}
646
647BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
648{
649 T ims(10000);
650
651 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
652
653 T ims2(4);
654
655 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
656}
657
658BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
659{
660 T ims(40);
661
662 for (int i = 0; i < 11; i++) {
663 std::ostringstream convert;
664 convert << i;
665 Name name("/" + convert.str());
666 shared_ptr<Data> data = makeData(name);
667 data->setFreshnessPeriod(time::milliseconds(5000));
668 signData(data);
669 ims.insert(*data);
670 }
671
672 BOOST_CHECK_EQUAL(ims.size(), 11);
673
674 BOOST_CHECK_EQUAL(ims.getCapacity(), 20);
675}
676
677BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
678{
679 T ims(2);
680
681 Name name("/insert/1");
682 shared_ptr<Data> data = makeData(name);
683 ims.insert(*data);
684
685 Name name2("/insert/2");
686 shared_ptr<Data> data2 = makeData(name2);
687 ims.insert(*data2);
688
689 Name name3("/insert/3");
690 shared_ptr<Data> data3 = makeData(name3);
691 ims.insert(*data3);
692
693 BOOST_CHECK_EQUAL(ims.size(), 2);
694
695 shared_ptr<Interest> interest = makeInterest(name);
696 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000697 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700698}
699
700///as Find function is implemented at the base case, therefore testing for one derived class is
701///sufficient for all
Yingdi Yu404eafd2016-03-06 14:54:25 -0800702class FindFixture : public ndn::tests::UnitTestTimeFixture
Jiewen Tan99135962014-09-20 02:18:53 -0700703{
704protected:
Yingdi Yu404eafd2016-03-06 14:54:25 -0800705 FindFixture()
706 : m_ims(io)
707 {
708 }
709
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800710 Name
Yingdi Yu404eafd2016-03-06 14:54:25 -0800711 insert(uint32_t id, const Name& name,
712 const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW)
Jiewen Tan99135962014-09-20 02:18:53 -0700713 {
714 shared_ptr<Data> data = makeData(name);
715 data->setFreshnessPeriod(time::milliseconds(99999));
716 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
717 signData(data);
718
Yingdi Yu404eafd2016-03-06 14:54:25 -0800719 m_ims.insert(*data, freshWindow);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800720
721 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700722 }
723
724 Interest&
725 startInterest(const Name& name)
726 {
727 m_interest = makeInterest(name);
728 return *m_interest;
729 }
730
731 uint32_t
732 find()
733 {
734 shared_ptr<const Data> found = m_ims.find(*m_interest);
735 if (found == 0) {
736 return 0;
737 }
738 const Block& content = found->getContent();
739 if (content.value_size() != sizeof(uint32_t)) {
740 return 0;
741 }
742 return *reinterpret_cast<const uint32_t*>(content.value());
743 }
744
745protected:
746 InMemoryStoragePersistent m_ims;
747 shared_ptr<Interest> m_interest;
748};
749
750BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
751
752BOOST_AUTO_TEST_CASE(EmptyDataName)
753{
754 insert(1, "ndn:/");
755
756 startInterest("ndn:/");
757 BOOST_CHECK_EQUAL(find(), 1);
758}
759
760BOOST_AUTO_TEST_CASE(EmptyInterestName)
761{
762 insert(1, "ndn:/A");
763
764 startInterest("ndn:/");
765 BOOST_CHECK_EQUAL(find(), 1);
766}
767
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800768BOOST_AUTO_TEST_CASE(ExactName)
769{
770 insert(1, "ndn:/");
771 insert(2, "ndn:/A");
772 insert(3, "ndn:/A/B");
773 insert(4, "ndn:/A/C");
774 insert(5, "ndn:/D");
775
776 startInterest("ndn:/A");
777 BOOST_CHECK_EQUAL(find(), 2);
778}
779
780BOOST_AUTO_TEST_CASE(FullName)
781{
782 Name n1 = insert(1, "ndn:/A");
783 Name n2 = insert(2, "ndn:/A");
784
785 startInterest(n1);
786 BOOST_CHECK_EQUAL(find(), 1);
787
788 startInterest(n2);
789 BOOST_CHECK_EQUAL(find(), 2);
790}
791
Jiewen Tan99135962014-09-20 02:18:53 -0700792BOOST_AUTO_TEST_CASE(Leftmost)
793{
794 insert(1, "ndn:/A");
795 insert(2, "ndn:/B/p/1");
796 insert(3, "ndn:/B/p/2");
797 insert(4, "ndn:/B/q/1");
798 insert(5, "ndn:/B/q/2");
799 insert(6, "ndn:/C");
800
801 startInterest("ndn:/B");
802 BOOST_CHECK_EQUAL(find(), 2);
803}
804
805BOOST_AUTO_TEST_CASE(Rightmost)
806{
807 insert(1, "ndn:/A");
808 insert(2, "ndn:/B/p/1");
809 insert(3, "ndn:/B/p/2");
810 insert(4, "ndn:/B/q/1");
811 insert(5, "ndn:/B/q/2");
812 insert(6, "ndn:/C");
813
814 startInterest("ndn:/B")
815 .setChildSelector(1);
816 BOOST_CHECK_EQUAL(find(), 4);
817}
818
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800819BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Jiewen Tan99135962014-09-20 02:18:53 -0700820{
821 insert(1, "ndn:/");
822 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800823 insert(3, "ndn:/B/1");
824 insert(4, "ndn:/C/1/2");
825 insert(5, "ndn:/D/1/2/3");
826 insert(6, "ndn:/E/1/2/3/4");
Jiewen Tan99135962014-09-20 02:18:53 -0700827
828 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700829 .setMinSuffixComponents(0);
Jiewen Tan99135962014-09-20 02:18:53 -0700830 BOOST_CHECK_EQUAL(find(), 1);
831
832 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800833 .setMinSuffixComponents(1);
834 BOOST_CHECK_EQUAL(find(), 1);
835
836 startInterest("ndn:/")
837 .setMinSuffixComponents(2);
838 BOOST_CHECK_EQUAL(find(), 2);
839
840 startInterest("ndn:/")
841 .setMinSuffixComponents(3);
842 BOOST_CHECK_EQUAL(find(), 3);
843
844 startInterest("ndn:/")
845 .setMinSuffixComponents(4);
846 BOOST_CHECK_EQUAL(find(), 4);
847
848 startInterest("ndn:/")
849 .setMinSuffixComponents(5);
850 BOOST_CHECK_EQUAL(find(), 5);
851
852 startInterest("ndn:/")
853 .setMinSuffixComponents(6);
854 BOOST_CHECK_EQUAL(find(), 6);
855
856 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700857 .setMinSuffixComponents(7);
858 BOOST_CHECK_EQUAL(find(), 0);
859}
860
861BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
862{
863 insert(1, "ndn:/");
864 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800865 insert(3, "ndn:/B/2");
866 insert(4, "ndn:/C/2/3");
867 insert(5, "ndn:/D/2/3/4");
868 insert(6, "ndn:/E/2/3/4/5");
Jiewen Tan99135962014-09-20 02:18:53 -0700869
870 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800871 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700872 .setMaxSuffixComponents(0);
873 BOOST_CHECK_EQUAL(find(), 0);
874
875 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800876 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700877 .setMaxSuffixComponents(1);
878 BOOST_CHECK_EQUAL(find(), 1);
879
880 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800881 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700882 .setMaxSuffixComponents(2);
883 BOOST_CHECK_EQUAL(find(), 2);
884
885 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800886 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700887 .setMaxSuffixComponents(3);
888 BOOST_CHECK_EQUAL(find(), 3);
889
890 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800891 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700892 .setMaxSuffixComponents(4);
893 BOOST_CHECK_EQUAL(find(), 4);
894
895 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800896 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700897 .setMaxSuffixComponents(5);
898 BOOST_CHECK_EQUAL(find(), 5);
899
900 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800901 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700902 .setMaxSuffixComponents(6);
903 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800904
905 startInterest("ndn:/")
906 .setChildSelector(1)
907 .setMaxSuffixComponents(7);
908 BOOST_CHECK_EQUAL(find(), 6);
Jiewen Tan99135962014-09-20 02:18:53 -0700909}
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
928BOOST_AUTO_TEST_CASE(DigestExclude)
929{
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800930 insert(1, "ndn:/A");
931 Name n2 = insert(2, "ndn:/A");
932 insert(3, "ndn:/A/B");
933
934 uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE];
935 std::fill_n(digest00, sizeof(digest00), 0x00);
936 uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE];
937 std::fill_n(digestFF, sizeof(digestFF), 0xFF);
938
939 Exclude excludeDigest;
940 excludeDigest.excludeRange(
941 name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)),
942 name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF)));
Jiewen Tan99135962014-09-20 02:18:53 -0700943
944 startInterest("ndn:/A")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800945 .setChildSelector(0)
946 .setExclude(excludeDigest);
947 BOOST_CHECK_EQUAL(find(), 3);
Jiewen Tan99135962014-09-20 02:18:53 -0700948
949 startInterest("ndn:/A")
950 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800951 .setExclude(excludeDigest);
Jiewen Tan99135962014-09-20 02:18:53 -0700952 BOOST_CHECK_EQUAL(find(), 3);
953
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800954 Exclude excludeGeneric;
955 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Jiewen Tan99135962014-09-20 02:18:53 -0700956
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800957 startInterest("ndn:/A")
958 .setChildSelector(0)
959 .setExclude(excludeGeneric);
960 int found1 = find();
961 BOOST_CHECK(found1 == 1 || found1 == 2);
962
963 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700964 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800965 .setExclude(excludeGeneric);
966 int found2 = find();
967 BOOST_CHECK(found2 == 1 || found2 == 2);
968
969 Exclude exclude2 = excludeGeneric;
970 exclude2.excludeOne(n2.get(-1));
971
972 startInterest("ndn:/A")
973 .setChildSelector(0)
974 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700975 BOOST_CHECK_EQUAL(find(), 1);
976
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800977 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700978 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800979 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700980 BOOST_CHECK_EQUAL(find(), 1);
Jiewen Tan99135962014-09-20 02:18:53 -0700981}
982
Yingdi Yu404eafd2016-03-06 14:54:25 -0800983BOOST_AUTO_TEST_CASE(MustBeFresh)
984{
985 Name data1Name = insert(1, "ndn:/A/1", time::milliseconds(500));
986 insert(2, "ndn:/A/2", time::milliseconds(2500));
987 insert(3, "ndn:/A/3", time::milliseconds(3500));
988 insert(4, "ndn:/A/4", time::milliseconds(1500));
989
990 // @0s, all Data are fresh
991 startInterest("ndn:/A/1")
992 .setMustBeFresh(true);
993 BOOST_CHECK_EQUAL(find(), 1);
994
995 startInterest("ndn:/A/1")
996 .setMustBeFresh(false);
997 BOOST_CHECK_EQUAL(find(), 1);
998
999 startInterest("ndn:/A")
1000 .setMustBeFresh(true)
1001 .setChildSelector(0);
1002 BOOST_CHECK_EQUAL(find(), 1);
1003
1004 startInterest("ndn:/A")
1005 .setMustBeFresh(true)
1006 .setChildSelector(1);
1007 BOOST_CHECK_EQUAL(find(), 4);
1008
1009 advanceClocks(time::milliseconds(1000));
1010 // @1s, /A/1 is stale
1011 startInterest("ndn:/A/1")
1012 .setMustBeFresh(true);
1013 BOOST_CHECK_EQUAL(find(), 0);
1014 startInterest("ndn:/A/1")
1015 .setMustBeFresh(false);
1016 BOOST_CHECK_EQUAL(find(), 1);
1017
1018 // MustBeFresh is ignored when full Name is specified
1019 startInterest(data1Name)
1020 .setMustBeFresh(true);
1021 BOOST_CHECK_EQUAL(find(), 1);
1022
1023 startInterest("ndn:/A")
1024 .setMustBeFresh(true)
1025 .setChildSelector(0);
1026 BOOST_CHECK_EQUAL(find(), 2);
1027 startInterest("ndn:/A")
1028 .setMustBeFresh(false)
1029 .setChildSelector(0);
1030 BOOST_CHECK_EQUAL(find(), 1);
1031
1032 advanceClocks(time::milliseconds(1000));
1033 // @2s, /A/1 and /A/4 are stale
1034 startInterest("ndn:/A")
1035 .setMustBeFresh(true)
1036 .setChildSelector(1);
1037 BOOST_CHECK_EQUAL(find(), 3);
1038 startInterest("ndn:/A")
1039 .setMustBeFresh(false)
1040 .setChildSelector(1);
1041 BOOST_CHECK_EQUAL(find(), 4);
1042
1043 advanceClocks(time::milliseconds(2000));
1044 // @4s, all Data are stale
1045 startInterest("ndn:/A")
1046 .setMustBeFresh(true)
1047 .setChildSelector(0);
1048 BOOST_CHECK_EQUAL(find(), 0);
1049 startInterest("ndn:/A")
1050 .setMustBeFresh(true)
1051 .setChildSelector(1);
1052 BOOST_CHECK_EQUAL(find(), 0);
1053}
1054
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -07001055BOOST_AUTO_TEST_SUITE_END() // Find
1056BOOST_AUTO_TEST_SUITE_END() // Common
Junxiao Shi85d90832016-08-04 03:19:46 +00001057BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage
1058BOOST_AUTO_TEST_SUITE_END() // Util
Jiewen Tan99135962014-09-20 02:18:53 -07001059
Junxiao Shi85d90832016-08-04 03:19:46 +00001060} // namespace tests
Jiewen Tan99135962014-09-20 02:18:53 -07001061} // namespace util
1062} // namespace ndn