blob: a4a992b33631fc473834bb47fed20a374b9a8b15 [file] [log] [blame]
Jiewen Tan99135962014-09-20 02:18:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080026#include "util/crypto.hpp"
27#include "security/signature-sha256-with-rsa.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070028
29#include "boost-test.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080030#include "../make-interest-data.hpp"
Yingdi Yu404eafd2016-03-06 14:54:25 -080031#include "../unit-test-time-fixture.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070032
33#include <boost/mpl/list.hpp>
34
35namespace ndn {
36namespace util {
Junxiao Shi85d90832016-08-04 03:19:46 +000037namespace tests {
Jiewen Tan99135962014-09-20 02:18:53 -070038
Junxiao Shi85d90832016-08-04 03:19:46 +000039using namespace ndn::tests;
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070040
Junxiao Shi85d90832016-08-04 03:19:46 +000041BOOST_AUTO_TEST_SUITE(Util)
42BOOST_AUTO_TEST_SUITE(TestInMemoryStorage)
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070043BOOST_AUTO_TEST_SUITE(Common)
44
Davide Pesaventoeee3e822016-11-26 19:19:34 +010045using InMemoryStorages = boost::mpl::list<InMemoryStoragePersistent,
46 InMemoryStorageFifo,
47 InMemoryStorageLfu,
48 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -070049
Jiewen Tan99135962014-09-20 02:18:53 -070050BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages)
51{
52 T ims;
53
Jiewen Tan99135962014-09-20 02:18:53 -070054 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
Davide Pesaventoeee3e822016-11-26 19:19:34 +010062BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -070063{
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);
Junxiao Shi85d90832016-08-04 03:19:46 +0000126 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700127 BOOST_CHECK_EQUAL(data->getName(), found->getName());
128}
129
130BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages)
131{
132 T ims;
133
134 Name name("/insert/and/find");
135 shared_ptr<Data> data = makeData(name);
136 ims.insert(*data);
137
138 Name name2("/not/find");
139 shared_ptr<Interest> interest = makeInterest(name2);
140
141 shared_ptr<const Data> found = ims.find(*interest);
142
143 BOOST_CHECK_EQUAL(found.get(), static_cast<const Data*>(0));
144}
145
146BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages)
147{
148 T ims;
149
150 Name name("/insert/and/find");
151
152 shared_ptr<Data> data = makeData(name);
153 ims.insert(*data);
154
155 shared_ptr<const Data> found = ims.find(name);
Junxiao Shi85d90832016-08-04 03:19:46 +0000156 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700157 BOOST_CHECK_EQUAL(data->getName(), found->getName());
158}
159
160BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages)
161{
162 T ims;
163
164 Name name("/insert/and/find");
165
166 shared_ptr<Data> data = makeData(name);
167 ims.insert(*data);
168
169 shared_ptr<const Data> found = ims.find(data->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000170 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700171 BOOST_CHECK_EQUAL(data->getFullName(), found->getFullName());
172}
173
174BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByName, T, InMemoryStorages)
175{
176 T ims;
177
178 Name name("/insert/and/find");
179 shared_ptr<Data> data = makeData(name);
180 ims.insert(*data);
181
182 Name name2("/not/find");
183
184 shared_ptr<const Data> found = ims.find(name2);
Junxiao Shi85d90832016-08-04 03:19:46 +0000185 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700186}
187
188BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByFullName, T, InMemoryStorages)
189{
190 T ims;
191
192 Name name("/a");
193 uint32_t content1 = 1;
194 shared_ptr<Data> data1 = makeData(name);
195 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
196 signData(data1);
197 ims.insert(*data1);
198
199 uint32_t content2 = 2;
200 shared_ptr<Data> data2 = makeData(name);
201 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
202 signData(data2);
203
204 shared_ptr<const Data> found = ims.find(data2->getFullName());
Junxiao Shi85d90832016-08-04 03:19:46 +0000205 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700206}
207
208BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages)
209{
210 T ims;
211
212 Name name("/insertandremovebyname");
213
214 uint32_t content1 = 1;
215 shared_ptr<Data> data1 = makeData(name);
216 data1->setFreshnessPeriod(time::milliseconds(99999));
217 data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1));
218 signData(data1);
219 ims.insert(*data1);
220
221 uint32_t content2 = 2;
222 shared_ptr<Data> data2 = makeData(name);
223 data2->setFreshnessPeriod(time::milliseconds(99999));
224 data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2));
225 signData(data2);
226 ims.insert(*data2);
227
228 shared_ptr<Data> data3 = makeData("/insertandremovebyname/1");
229 ims.insert(*data3);
230
231 shared_ptr<Data> data4 = makeData("/insertandremovebyname/2");
232 ims.insert(*data4);
233
234 BOOST_CHECK_EQUAL(ims.size(), 4);
235
236 ims.erase(data1->getFullName(), false);
237 BOOST_CHECK_EQUAL(ims.size(), 3);
238}
239
240BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages)
241{
242 T ims;
243
244 shared_ptr<Data> data = makeData("/a");
245 ims.insert(*data);
246
247 shared_ptr<Data> data2 = makeData("/b");
248 ims.insert(*data2);
249
250 shared_ptr<Data> data3 = makeData("/c");
251 ims.insert(*data3);
252
253 shared_ptr<Data> data4 = makeData("/d");
254 ims.insert(*data4);
255
256 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
257 ims.insert(*data5);
258
259 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
260 ims.insert(*data6);
261
262 shared_ptr<Data> data7 = makeData("/c/c/1");
263 ims.insert(*data7);
264
265 BOOST_CHECK_EQUAL(ims.size(), 7);
266
267 Name name("/c");
268 ims.erase(name);
269 BOOST_CHECK_EQUAL(ims.size(), 3);
270 BOOST_CHECK_EQUAL(ims.getCapacity(), 5);
271}
272
273BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages)
274{
275 shared_ptr<Data> data = makeData("/digest/compute");
276
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800277 ConstBufferPtr digest1 = crypto::computeSha256Digest(data->wireEncode().wire(), data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700278 BOOST_CHECK_EQUAL(digest1->size(), 32);
279
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800280 InMemoryStorageEntry entry;
281 entry.setData(*data);
Jiewen Tan99135962014-09-20 02:18:53 -0700282
283 BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(),
Alexander Afanasyev3e2c3a02017-01-19 13:55:56 -0800284 entry.getFullName()[-1].value_begin(),
285 entry.getFullName()[-1].value_end());
Jiewen Tan99135962014-09-20 02:18:53 -0700286}
287
288BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages)
289{
290 T ims;
291
292 BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>));
293
294 for (int i = 0; i < 10; i++) {
295 std::ostringstream convert;
296 convert << i;
297 Name name("/" + convert.str());
298 shared_ptr<Data> data = makeData(name);
299 ims.insert(*data);
300 }
301
302 InMemoryStorage::const_iterator it = ims.begin();
303 InMemoryStorage::const_iterator tmp1 = it;
304 BOOST_REQUIRE(tmp1 == it);
305 InMemoryStorage::const_iterator tmp2 = tmp1++;
306 BOOST_REQUIRE(tmp2 != tmp1);
307 tmp2 = ++tmp1;
308 BOOST_REQUIRE(tmp2 == tmp1);
309
310 int i = 0;
311 for (;it != ims.end(); it++) {
312 std::ostringstream convert;
313 convert << i;
314 Name name("/" + convert.str());
315 BOOST_CHECK_EQUAL(it->getName(), name);
316 BOOST_CHECK_EQUAL((*it).getName(), name);
317 i++;
318 }
319}
320
321BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages)
322{
323 T ims;
324
325 shared_ptr<Data> data = makeData("/a");
326 ims.insert(*data);
327
328 shared_ptr<Data> data2 = makeData("/b");
329 ims.insert(*data2);
330
331 shared_ptr<Data> data3 = makeData("/c");
332 ims.insert(*data3);
333
334 shared_ptr<Data> data4 = makeData("/d");
335 ims.insert(*data4);
336
337 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
338 ims.insert(*data5);
339
340 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
341 ims.insert(*data6);
342
343 shared_ptr<Data> data7 = makeData("/c/c/1");
344 ims.insert(*data7);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100345
346 // avoid "test case [...] did not check any assertions" message from Boost.Test
347 BOOST_CHECK(true);
Jiewen Tan99135962014-09-20 02:18:53 -0700348}
349
350BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages)
351{
352 T ims;
353
354 shared_ptr<Data> data = makeData("/a");
355 ims.insert(*data);
356
357 shared_ptr<Data> data2 = makeData("/b");
358 ims.insert(*data2);
359
360 shared_ptr<Data> data3 = makeData("/c");
361 ims.insert(*data3);
362
363 shared_ptr<Data> data4 = makeData("/d");
364 ims.insert(*data4);
365
366 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
367 ims.insert(*data5);
368
369 shared_ptr<Data> data6 = makeData("/c/c/1/2/3");
370 ims.insert(*data6);
371
372 shared_ptr<Data> data7 = makeData("/c/c/1");
373 ims.insert(*data7);
374
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800375 ConstBufferPtr digest1 = crypto::computeSha256Digest(data->wireEncode().wire(), data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700376
377 Name name("/a");
378 ims.erase(name);
379 BOOST_CHECK_EQUAL(ims.size(), 6);
380}
381
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800382BOOST_AUTO_TEST_CASE_TEMPLATE(ImplicitDigestSelector, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -0700383{
384 T ims;
385
386 Name name("/digest/works");
387 shared_ptr<Data> data = makeData(name);
388 ims.insert(*data);
389
390 shared_ptr<Data> data2 = makeData("/a");
391 ims.insert(*data2);
392
393 shared_ptr<Data> data3 = makeData("/z/z/z");
394 ims.insert(*data3);
395
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800396 ConstBufferPtr digest1 = crypto::computeSha256Digest(data->wireEncode().wire(), data->wireEncode().size());
Jiewen Tan99135962014-09-20 02:18:53 -0700397
398 shared_ptr<Interest> interest = makeInterest("");
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800399 interest->setName(Name(name).appendImplicitSha256Digest(digest1->buf(), digest1->size()));
Jiewen Tan99135962014-09-20 02:18:53 -0700400 interest->setMinSuffixComponents(0);
401 interest->setMaxSuffixComponents(0);
402
403 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000404 BOOST_REQUIRE(found != nullptr);
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800405 BOOST_CHECK_EQUAL(found->getName(), name);
406
407 shared_ptr<Interest> interest2 = makeInterest("");
408 uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
409 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
410 interest2->setName(Name(name).appendImplicitSha256Digest(digest2, 32));
411 interest2->setMinSuffixComponents(0);
412 interest2->setMaxSuffixComponents(0);
413
414 shared_ptr<const Data> notfound = ims.find(*interest2);
Junxiao Shi85d90832016-08-04 03:19:46 +0000415 BOOST_CHECK(notfound == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700416}
417
418BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector, T, InMemoryStorages)
419{
420 T ims;
421
422 shared_ptr<Data> data = makeData("/a");
423 ims.insert(*data);
424
425 shared_ptr<Data> data2 = makeData("/b");
426 ims.insert(*data2);
427
428 shared_ptr<Data> data4 = makeData("/d");
429 ims.insert(*data4);
430
431 shared_ptr<Data> data5 = makeData("/c/c");
432 ims.insert(*data5);
433
434 shared_ptr<Data> data6 = makeData("/c/f");
435 ims.insert(*data6);
436
437 shared_ptr<Data> data7 = makeData("/c/n");
438 ims.insert(*data7);
439
440 shared_ptr<Interest> interest = makeInterest("/c");
441 interest->setChildSelector(1);
442
443 shared_ptr<const Data> found = ims.find(*interest);
444 BOOST_CHECK_EQUAL(found->getName(), "/c/n");
445
446 shared_ptr<Interest> interest2 = makeInterest("/c");
447 interest2->setChildSelector(0);
448
449 shared_ptr<const Data> found2 = ims.find(*interest2);
450 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
451}
452
453BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector2, T, InMemoryStorages)
454{
455 T ims;
456
457 shared_ptr<Data> data = makeData("/a/b/1");
458 ims.insert(*data);
459
460 shared_ptr<Data> data2 = makeData("/a/b/2");
461 ims.insert(*data2);
462
463 shared_ptr<Data> data3 = makeData("/a/z/1");
464 ims.insert(*data3);
465
466 shared_ptr<Data> data4 = makeData("/a/z/2");
467 ims.insert(*data4);
468
469 shared_ptr<Interest> interest = makeInterest("/a");
470 interest->setChildSelector(1);
471
472 shared_ptr<const Data> found = ims.find(*interest);
473 BOOST_CHECK_EQUAL(found->getName(), "/a/z/1");
474}
475
476BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector, T, InMemoryStorages)
477{
478 T ims;
479
480 Name name("/insert/withkey");
481 shared_ptr<Data> data = makeData(name);
482 ims.insert(*data);
483
484 shared_ptr<Interest> interest = makeInterest(name);
485 Name keyName("/somewhere/key");
486
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800487 KeyLocator locator(keyName);
Jiewen Tan99135962014-09-20 02:18:53 -0700488 interest->setPublisherPublicKeyLocator(locator);
489
490 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000491 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700492}
493
494BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector2, T, InMemoryStorages)
495{
496 T ims;
497 Name name("/insert/withkey");
498 shared_ptr<Data> data = makeData(name);
499 ims.insert(*data);
500
501 Name name2("/insert/withkey2");
502 shared_ptr<Data> data2 = make_shared<Data>(name2);
503
504 Name keyName("/somewhere/key");
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800505 const KeyLocator locator(keyName);
Jiewen Tan99135962014-09-20 02:18:53 -0700506
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800507 SignatureSha256WithRsa fakeSignature;
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700508 fakeSignature.setValue(makeEmptyBlock(tlv::SignatureValue));
Jiewen Tan99135962014-09-20 02:18:53 -0700509
510 fakeSignature.setKeyLocator(locator);
511 data2->setSignature(fakeSignature);
512 data2->wireEncode();
513
514 ims.insert(*data2);
515
516 shared_ptr<Interest> interest = makeInterest(name2);
517 interest->setPublisherPublicKeyLocator(locator);
518
519 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000520 BOOST_CHECK(found != nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700521 BOOST_CHECK_EQUAL(found->getName(), data2->getName());
522}
523
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800524BOOST_AUTO_TEST_CASE_TEMPLATE(MinMaxComponentsSelector, T, InMemoryStorages)
Jiewen Tan99135962014-09-20 02:18:53 -0700525{
526 T ims;
527
528 shared_ptr<Data> data = makeData("/a");
529 ims.insert(*data);
530
531 shared_ptr<Data> data2 = makeData("/b");
532 ims.insert(*data2);
533
534 shared_ptr<Data> data4 = makeData("/d");
535 ims.insert(*data4);
536
537 shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6");
538 ims.insert(*data5);
539
540 shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9");
541 ims.insert(*data6);
542
543 shared_ptr<Data> data7 = makeData("/c/c/1/2/3");
544 ims.insert(*data7);
545
546 shared_ptr<Data> data8 = makeData("/c/c/1");
547 ims.insert(*data8);
548
549 shared_ptr<Interest> interest = makeInterest("/c/c");
550 interest->setMinSuffixComponents(3);
551 interest->setChildSelector(0);
552
553 shared_ptr<const Data> found = ims.find(*interest);
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800554 BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3");
Jiewen Tan99135962014-09-20 02:18:53 -0700555
556 shared_ptr<Interest> interest2 = makeInterest("/c/c");
557 interest2->setMinSuffixComponents(4);
558 interest2->setChildSelector(1);
559
560 shared_ptr<const Data> found2 = ims.find(*interest2);
561 BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9");
562
563 shared_ptr<Interest> interest3 = makeInterest("/c/c");
564 interest3->setMaxSuffixComponents(2);
565 interest3->setChildSelector(1);
566
567 shared_ptr<const Data> found3 = ims.find(*interest3);
568 BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1");
569}
570
571BOOST_AUTO_TEST_CASE_TEMPLATE(ExcludeSelector, T, InMemoryStorages)
572{
573 T ims;
574
575 shared_ptr<Data> data = makeData("/a");
576 ims.insert(*data);
577
578 shared_ptr<Data> data2 = makeData("/b");
579 ims.insert(*data2);
580
581 shared_ptr<Data> data3 = makeData("/c/a");
582 ims.insert(*data3);
583
584 shared_ptr<Data> data4 = makeData("/d");
585 ims.insert(*data4);
586
587 shared_ptr<Data> data5 = makeData("/c/c");
588 ims.insert(*data5);
589
590 shared_ptr<Data> data6 = makeData("/c/f");
591 ims.insert(*data6);
592
593 shared_ptr<Data> data7 = makeData("/c/n");
594 ims.insert(*data7);
595
596 shared_ptr<Interest> interest = makeInterest("/c");
597 interest->setChildSelector(1);
598 Exclude e;
599 e.excludeOne (Name::Component("n"));
600 interest->setExclude(e);
601
602 shared_ptr<const Data> found = ims.find(*interest);
603 BOOST_CHECK_EQUAL(found->getName(), "/c/f");
604
605 shared_ptr<Interest> interest2 = makeInterest("/c");
606 interest2->setChildSelector(0);
607
608 Exclude e2;
609 e2.excludeOne (Name::Component("a"));
610 interest2->setExclude(e2);
611
612 shared_ptr<const Data> found2 = ims.find(*interest2);
613 BOOST_CHECK_EQUAL(found2->getName(), "/c/c");
614
615 shared_ptr<Interest> interest3 = makeInterest("/c");
616 interest3->setChildSelector(0);
617
618 Exclude e3;
619 e3.excludeOne (Name::Component("c"));
620 interest3->setExclude(e3);
621
622 shared_ptr<const Data> found3 = ims.find(*interest3);
623 BOOST_CHECK_EQUAL(found3->getName(), "/c/a");
624}
625
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100626using InMemoryStoragesLimited = boost::mpl::list<InMemoryStorageFifo,
627 InMemoryStorageLfu,
628 InMemoryStorageLru>;
Jiewen Tan99135962014-09-20 02:18:53 -0700629
Yingdi Yu404eafd2016-03-06 14:54:25 -0800630BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited)
Jiewen Tan99135962014-09-20 02:18:53 -0700631{
632 T ims;
633
634 ims.setCapacity(3);
635 ims.insert(*makeData("/1"));
636 ims.insert(*makeData("/2"));
637 ims.insert(*makeData("/3"));
638 BOOST_CHECK_EQUAL(ims.size(), 3);
639
640 ims.setCapacity(2);
641 BOOST_CHECK_EQUAL(ims.size(), 2);
642}
643
644BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited)
645{
646 T ims(10000);
647
648 BOOST_CHECK_EQUAL(ims.getLimit(), 10000);
649
650 T ims2(4);
651
652 BOOST_CHECK_EQUAL(ims2.getLimit(), 4);
653}
654
655BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited)
656{
657 T ims(40);
658
659 for (int i = 0; i < 11; i++) {
660 std::ostringstream convert;
661 convert << i;
662 Name name("/" + convert.str());
663 shared_ptr<Data> data = makeData(name);
664 data->setFreshnessPeriod(time::milliseconds(5000));
665 signData(data);
666 ims.insert(*data);
667 }
668
669 BOOST_CHECK_EQUAL(ims.size(), 11);
670
671 BOOST_CHECK_EQUAL(ims.getCapacity(), 20);
672}
673
674BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited)
675{
676 T ims(2);
677
678 Name name("/insert/1");
679 shared_ptr<Data> data = makeData(name);
680 ims.insert(*data);
681
682 Name name2("/insert/2");
683 shared_ptr<Data> data2 = makeData(name2);
684 ims.insert(*data2);
685
686 Name name3("/insert/3");
687 shared_ptr<Data> data3 = makeData(name3);
688 ims.insert(*data3);
689
690 BOOST_CHECK_EQUAL(ims.size(), 2);
691
692 shared_ptr<Interest> interest = makeInterest(name);
693 shared_ptr<const Data> found = ims.find(*interest);
Junxiao Shi85d90832016-08-04 03:19:46 +0000694 BOOST_CHECK(found == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700695}
696
697///as Find function is implemented at the base case, therefore testing for one derived class is
698///sufficient for all
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800699class FindFixture : public tests::UnitTestTimeFixture
Jiewen Tan99135962014-09-20 02:18:53 -0700700{
701protected:
Yingdi Yu404eafd2016-03-06 14:54:25 -0800702 FindFixture()
703 : m_ims(io)
704 {
705 }
706
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800707 Name
Yingdi Yu404eafd2016-03-06 14:54:25 -0800708 insert(uint32_t id, const Name& name,
709 const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW)
Jiewen Tan99135962014-09-20 02:18:53 -0700710 {
711 shared_ptr<Data> data = makeData(name);
712 data->setFreshnessPeriod(time::milliseconds(99999));
713 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
714 signData(data);
715
Yingdi Yu404eafd2016-03-06 14:54:25 -0800716 m_ims.insert(*data, freshWindow);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800717
718 return data->getFullName();
Jiewen Tan99135962014-09-20 02:18:53 -0700719 }
720
721 Interest&
722 startInterest(const Name& name)
723 {
724 m_interest = makeInterest(name);
725 return *m_interest;
726 }
727
728 uint32_t
729 find()
730 {
731 shared_ptr<const Data> found = m_ims.find(*m_interest);
732 if (found == 0) {
733 return 0;
734 }
735 const Block& content = found->getContent();
736 if (content.value_size() != sizeof(uint32_t)) {
737 return 0;
738 }
739 return *reinterpret_cast<const uint32_t*>(content.value());
740 }
741
742protected:
743 InMemoryStoragePersistent m_ims;
744 shared_ptr<Interest> m_interest;
745};
746
747BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
748
749BOOST_AUTO_TEST_CASE(EmptyDataName)
750{
751 insert(1, "ndn:/");
752
753 startInterest("ndn:/");
754 BOOST_CHECK_EQUAL(find(), 1);
755}
756
757BOOST_AUTO_TEST_CASE(EmptyInterestName)
758{
759 insert(1, "ndn:/A");
760
761 startInterest("ndn:/");
762 BOOST_CHECK_EQUAL(find(), 1);
763}
764
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800765BOOST_AUTO_TEST_CASE(ExactName)
766{
767 insert(1, "ndn:/");
768 insert(2, "ndn:/A");
769 insert(3, "ndn:/A/B");
770 insert(4, "ndn:/A/C");
771 insert(5, "ndn:/D");
772
773 startInterest("ndn:/A");
774 BOOST_CHECK_EQUAL(find(), 2);
775}
776
777BOOST_AUTO_TEST_CASE(FullName)
778{
779 Name n1 = insert(1, "ndn:/A");
780 Name n2 = insert(2, "ndn:/A");
781
782 startInterest(n1);
783 BOOST_CHECK_EQUAL(find(), 1);
784
785 startInterest(n2);
786 BOOST_CHECK_EQUAL(find(), 2);
787}
788
Jiewen Tan99135962014-09-20 02:18:53 -0700789BOOST_AUTO_TEST_CASE(Leftmost)
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 BOOST_CHECK_EQUAL(find(), 2);
800}
801
802BOOST_AUTO_TEST_CASE(Rightmost)
803{
804 insert(1, "ndn:/A");
805 insert(2, "ndn:/B/p/1");
806 insert(3, "ndn:/B/p/2");
807 insert(4, "ndn:/B/q/1");
808 insert(5, "ndn:/B/q/2");
809 insert(6, "ndn:/C");
810
811 startInterest("ndn:/B")
812 .setChildSelector(1);
813 BOOST_CHECK_EQUAL(find(), 4);
814}
815
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800816BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Jiewen Tan99135962014-09-20 02:18:53 -0700817{
818 insert(1, "ndn:/");
819 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800820 insert(3, "ndn:/B/1");
821 insert(4, "ndn:/C/1/2");
822 insert(5, "ndn:/D/1/2/3");
823 insert(6, "ndn:/E/1/2/3/4");
Jiewen Tan99135962014-09-20 02:18:53 -0700824
825 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700826 .setMinSuffixComponents(0);
Jiewen Tan99135962014-09-20 02:18:53 -0700827 BOOST_CHECK_EQUAL(find(), 1);
828
829 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800830 .setMinSuffixComponents(1);
831 BOOST_CHECK_EQUAL(find(), 1);
832
833 startInterest("ndn:/")
834 .setMinSuffixComponents(2);
835 BOOST_CHECK_EQUAL(find(), 2);
836
837 startInterest("ndn:/")
838 .setMinSuffixComponents(3);
839 BOOST_CHECK_EQUAL(find(), 3);
840
841 startInterest("ndn:/")
842 .setMinSuffixComponents(4);
843 BOOST_CHECK_EQUAL(find(), 4);
844
845 startInterest("ndn:/")
846 .setMinSuffixComponents(5);
847 BOOST_CHECK_EQUAL(find(), 5);
848
849 startInterest("ndn:/")
850 .setMinSuffixComponents(6);
851 BOOST_CHECK_EQUAL(find(), 6);
852
853 startInterest("ndn:/")
Jiewen Tan99135962014-09-20 02:18:53 -0700854 .setMinSuffixComponents(7);
855 BOOST_CHECK_EQUAL(find(), 0);
856}
857
858BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
859{
860 insert(1, "ndn:/");
861 insert(2, "ndn:/A");
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800862 insert(3, "ndn:/B/2");
863 insert(4, "ndn:/C/2/3");
864 insert(5, "ndn:/D/2/3/4");
865 insert(6, "ndn:/E/2/3/4/5");
Jiewen Tan99135962014-09-20 02:18:53 -0700866
867 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800868 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700869 .setMaxSuffixComponents(0);
870 BOOST_CHECK_EQUAL(find(), 0);
871
872 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800873 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700874 .setMaxSuffixComponents(1);
875 BOOST_CHECK_EQUAL(find(), 1);
876
877 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800878 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700879 .setMaxSuffixComponents(2);
880 BOOST_CHECK_EQUAL(find(), 2);
881
882 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800883 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700884 .setMaxSuffixComponents(3);
885 BOOST_CHECK_EQUAL(find(), 3);
886
887 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800888 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700889 .setMaxSuffixComponents(4);
890 BOOST_CHECK_EQUAL(find(), 4);
891
892 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800893 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700894 .setMaxSuffixComponents(5);
895 BOOST_CHECK_EQUAL(find(), 5);
896
897 startInterest("ndn:/")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800898 .setChildSelector(1)
Jiewen Tan99135962014-09-20 02:18:53 -0700899 .setMaxSuffixComponents(6);
900 BOOST_CHECK_EQUAL(find(), 6);
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800901
902 startInterest("ndn:/")
903 .setChildSelector(1)
904 .setMaxSuffixComponents(7);
905 BOOST_CHECK_EQUAL(find(), 6);
Jiewen Tan99135962014-09-20 02:18:53 -0700906}
907
908BOOST_AUTO_TEST_CASE(DigestOrder)
909{
910 insert(1, "ndn:/A");
911 insert(2, "ndn:/A");
912 // We don't know which comes first, but there must be some order
913
914 startInterest("ndn:/A")
915 .setChildSelector(0);
916 uint32_t leftmost = find();
917
918 startInterest("ndn:/A")
919 .setChildSelector(1);
920 uint32_t rightmost = find();
921
922 BOOST_CHECK_NE(leftmost, rightmost);
923}
924
925BOOST_AUTO_TEST_CASE(DigestExclude)
926{
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800927 insert(1, "ndn:/A");
928 Name n2 = insert(2, "ndn:/A");
929 insert(3, "ndn:/A/B");
930
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800931 uint8_t digest00[crypto::SHA256_DIGEST_SIZE];
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800932 std::fill_n(digest00, sizeof(digest00), 0x00);
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800933 uint8_t digestFF[crypto::SHA256_DIGEST_SIZE];
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800934 std::fill_n(digestFF, sizeof(digestFF), 0xFF);
935
936 Exclude excludeDigest;
937 excludeDigest.excludeRange(
938 name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)),
939 name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF)));
Jiewen Tan99135962014-09-20 02:18:53 -0700940
941 startInterest("ndn:/A")
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800942 .setChildSelector(0)
943 .setExclude(excludeDigest);
944 BOOST_CHECK_EQUAL(find(), 3);
Jiewen Tan99135962014-09-20 02:18:53 -0700945
946 startInterest("ndn:/A")
947 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800948 .setExclude(excludeDigest);
Jiewen Tan99135962014-09-20 02:18:53 -0700949 BOOST_CHECK_EQUAL(find(), 3);
950
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800951 Exclude excludeGeneric;
952 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Jiewen Tan99135962014-09-20 02:18:53 -0700953
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800954 startInterest("ndn:/A")
955 .setChildSelector(0)
956 .setExclude(excludeGeneric);
957 int found1 = find();
958 BOOST_CHECK(found1 == 1 || found1 == 2);
959
960 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700961 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800962 .setExclude(excludeGeneric);
963 int found2 = find();
964 BOOST_CHECK(found2 == 1 || found2 == 2);
965
966 Exclude exclude2 = excludeGeneric;
967 exclude2.excludeOne(n2.get(-1));
968
969 startInterest("ndn:/A")
970 .setChildSelector(0)
971 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700972 BOOST_CHECK_EQUAL(find(), 1);
973
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800974 startInterest("ndn:/A")
Jiewen Tan99135962014-09-20 02:18:53 -0700975 .setChildSelector(1)
Alexander Afanasyev8cf2f2c2015-01-31 21:33:46 -0800976 .setExclude(exclude2);
Jiewen Tan99135962014-09-20 02:18:53 -0700977 BOOST_CHECK_EQUAL(find(), 1);
Jiewen Tan99135962014-09-20 02:18:53 -0700978}
979
Yingdi Yu404eafd2016-03-06 14:54:25 -0800980BOOST_AUTO_TEST_CASE(MustBeFresh)
981{
982 Name data1Name = insert(1, "ndn:/A/1", time::milliseconds(500));
983 insert(2, "ndn:/A/2", time::milliseconds(2500));
984 insert(3, "ndn:/A/3", time::milliseconds(3500));
985 insert(4, "ndn:/A/4", time::milliseconds(1500));
986
987 // @0s, all Data are fresh
988 startInterest("ndn:/A/1")
989 .setMustBeFresh(true);
990 BOOST_CHECK_EQUAL(find(), 1);
991
992 startInterest("ndn:/A/1")
993 .setMustBeFresh(false);
994 BOOST_CHECK_EQUAL(find(), 1);
995
996 startInterest("ndn:/A")
997 .setMustBeFresh(true)
998 .setChildSelector(0);
999 BOOST_CHECK_EQUAL(find(), 1);
1000
1001 startInterest("ndn:/A")
1002 .setMustBeFresh(true)
1003 .setChildSelector(1);
1004 BOOST_CHECK_EQUAL(find(), 4);
1005
1006 advanceClocks(time::milliseconds(1000));
1007 // @1s, /A/1 is stale
1008 startInterest("ndn:/A/1")
1009 .setMustBeFresh(true);
1010 BOOST_CHECK_EQUAL(find(), 0);
1011 startInterest("ndn:/A/1")
1012 .setMustBeFresh(false);
1013 BOOST_CHECK_EQUAL(find(), 1);
1014
1015 // MustBeFresh is ignored when full Name is specified
1016 startInterest(data1Name)
1017 .setMustBeFresh(true);
1018 BOOST_CHECK_EQUAL(find(), 1);
1019
1020 startInterest("ndn:/A")
1021 .setMustBeFresh(true)
1022 .setChildSelector(0);
1023 BOOST_CHECK_EQUAL(find(), 2);
1024 startInterest("ndn:/A")
1025 .setMustBeFresh(false)
1026 .setChildSelector(0);
1027 BOOST_CHECK_EQUAL(find(), 1);
1028
1029 advanceClocks(time::milliseconds(1000));
1030 // @2s, /A/1 and /A/4 are stale
1031 startInterest("ndn:/A")
1032 .setMustBeFresh(true)
1033 .setChildSelector(1);
1034 BOOST_CHECK_EQUAL(find(), 3);
1035 startInterest("ndn:/A")
1036 .setMustBeFresh(false)
1037 .setChildSelector(1);
1038 BOOST_CHECK_EQUAL(find(), 4);
1039
1040 advanceClocks(time::milliseconds(2000));
1041 // @4s, all Data are stale
1042 startInterest("ndn:/A")
1043 .setMustBeFresh(true)
1044 .setChildSelector(0);
1045 BOOST_CHECK_EQUAL(find(), 0);
1046 startInterest("ndn:/A")
1047 .setMustBeFresh(true)
1048 .setChildSelector(1);
1049 BOOST_CHECK_EQUAL(find(), 0);
1050}
1051
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -07001052BOOST_AUTO_TEST_SUITE_END() // Find
1053BOOST_AUTO_TEST_SUITE_END() // Common
Junxiao Shi85d90832016-08-04 03:19:46 +00001054BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage
1055BOOST_AUTO_TEST_SUITE_END() // Util
Jiewen Tan99135962014-09-20 02:18:53 -07001056
Junxiao Shi85d90832016-08-04 03:19:46 +00001057} // namespace tests
Jiewen Tan99135962014-09-20 02:18:53 -07001058} // namespace util
1059} // namespace ndn