Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 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" |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 29 | #include "../make-interest-data.hpp" |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 30 | #include "../unit-test-time-fixture.hpp" |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 31 | |
| 32 | #include <boost/mpl/list.hpp> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace util { |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 36 | namespace tests { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 38 | using namespace ndn::tests; |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(Util) |
| 41 | BOOST_AUTO_TEST_SUITE(TestInMemoryStorage) |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 42 | BOOST_AUTO_TEST_SUITE(Common) |
| 43 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 44 | using InMemoryStorages = boost::mpl::list<InMemoryStoragePersistent, |
| 45 | InMemoryStorageFifo, |
| 46 | InMemoryStorageLfu, |
| 47 | InMemoryStorageLru>; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 48 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 49 | BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages) |
| 50 | { |
| 51 | T ims; |
| 52 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 53 | ims.insert(*makeData("/a")); |
| 54 | ims.insert(*makeData("/b")); |
| 55 | ims.insert(*makeData("/c")); |
| 56 | ims.insert(*makeData("/d")); |
| 57 | |
| 58 | BOOST_CHECK_EQUAL(ims.size(), 4); |
| 59 | } |
| 60 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 61 | BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 62 | { |
| 63 | T ims; |
| 64 | |
| 65 | Name name("/a"); |
| 66 | |
| 67 | uint32_t content1 = 1; |
| 68 | shared_ptr<Data> data1 = makeData(name); |
| 69 | data1->setFreshnessPeriod(time::milliseconds(99999)); |
| 70 | data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1)); |
| 71 | signData(data1); |
| 72 | ims.insert(*data1); |
| 73 | |
| 74 | uint32_t content2 = 2; |
| 75 | shared_ptr<Data> data2 = makeData(name); |
| 76 | data2->setFreshnessPeriod(time::milliseconds(99999)); |
| 77 | data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2)); |
| 78 | signData(data2); |
| 79 | ims.insert(*data2); |
| 80 | |
| 81 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 82 | } |
| 83 | |
| 84 | BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion, T, InMemoryStorages) |
| 85 | { |
| 86 | T ims; |
| 87 | |
| 88 | shared_ptr<Data> data0 = makeData("/insert/smth"); |
| 89 | ims.insert(*data0); |
| 90 | |
| 91 | shared_ptr<Data> data = makeData("/insert/duplicate"); |
| 92 | ims.insert(*data); |
| 93 | |
| 94 | ims.insert(*data); |
| 95 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 96 | } |
| 97 | |
| 98 | BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion2, T, InMemoryStorages) |
| 99 | { |
| 100 | T ims; |
| 101 | |
| 102 | shared_ptr<Data> data = makeData("/insert/duplicate"); |
| 103 | ims.insert(*data); |
| 104 | |
| 105 | ims.insert(*data); |
| 106 | BOOST_CHECK_EQUAL(ims.size(), 1); |
| 107 | |
| 108 | shared_ptr<Data> data2 = makeData("/insert/original"); |
| 109 | ims.insert(*data2); |
| 110 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 111 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFind, T, InMemoryStorages) |
| 114 | { |
| 115 | T ims; |
| 116 | |
| 117 | Name name("/insert/and/find"); |
| 118 | |
| 119 | shared_ptr<Data> data = makeData(name); |
| 120 | ims.insert(*data); |
| 121 | |
| 122 | shared_ptr<Interest> interest = makeInterest(name); |
| 123 | |
| 124 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 125 | BOOST_CHECK(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(data->getName(), found->getName()); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages) |
| 130 | { |
| 131 | T ims; |
| 132 | |
| 133 | Name name("/insert/and/find"); |
| 134 | shared_ptr<Data> data = makeData(name); |
| 135 | ims.insert(*data); |
| 136 | |
| 137 | Name name2("/not/find"); |
| 138 | shared_ptr<Interest> interest = makeInterest(name2); |
| 139 | |
| 140 | shared_ptr<const Data> found = ims.find(*interest); |
| 141 | |
| 142 | BOOST_CHECK_EQUAL(found.get(), static_cast<const Data*>(0)); |
| 143 | } |
| 144 | |
| 145 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages) |
| 146 | { |
| 147 | T ims; |
| 148 | |
| 149 | Name name("/insert/and/find"); |
| 150 | |
| 151 | shared_ptr<Data> data = makeData(name); |
| 152 | ims.insert(*data); |
| 153 | |
| 154 | shared_ptr<const Data> found = ims.find(name); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 155 | BOOST_CHECK(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(data->getName(), found->getName()); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages) |
| 160 | { |
| 161 | T ims; |
| 162 | |
| 163 | Name name("/insert/and/find"); |
| 164 | |
| 165 | shared_ptr<Data> data = makeData(name); |
| 166 | ims.insert(*data); |
| 167 | |
| 168 | shared_ptr<const Data> found = ims.find(data->getFullName()); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 169 | BOOST_CHECK(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(data->getFullName(), found->getFullName()); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByName, T, InMemoryStorages) |
| 174 | { |
| 175 | T ims; |
| 176 | |
| 177 | Name name("/insert/and/find"); |
| 178 | shared_ptr<Data> data = makeData(name); |
| 179 | ims.insert(*data); |
| 180 | |
| 181 | Name name2("/not/find"); |
| 182 | |
| 183 | shared_ptr<const Data> found = ims.find(name2); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 184 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByFullName, T, InMemoryStorages) |
| 188 | { |
| 189 | T ims; |
| 190 | |
| 191 | Name name("/a"); |
| 192 | uint32_t content1 = 1; |
| 193 | shared_ptr<Data> data1 = makeData(name); |
| 194 | data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1)); |
| 195 | signData(data1); |
| 196 | ims.insert(*data1); |
| 197 | |
| 198 | uint32_t content2 = 2; |
| 199 | shared_ptr<Data> data2 = makeData(name); |
| 200 | data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2)); |
| 201 | signData(data2); |
| 202 | |
| 203 | shared_ptr<const Data> found = ims.find(data2->getFullName()); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 204 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages) |
| 208 | { |
| 209 | T ims; |
| 210 | |
| 211 | Name name("/insertandremovebyname"); |
| 212 | |
| 213 | uint32_t content1 = 1; |
| 214 | shared_ptr<Data> data1 = makeData(name); |
| 215 | data1->setFreshnessPeriod(time::milliseconds(99999)); |
| 216 | data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1)); |
| 217 | signData(data1); |
| 218 | ims.insert(*data1); |
| 219 | |
| 220 | uint32_t content2 = 2; |
| 221 | shared_ptr<Data> data2 = makeData(name); |
| 222 | data2->setFreshnessPeriod(time::milliseconds(99999)); |
| 223 | data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2)); |
| 224 | signData(data2); |
| 225 | ims.insert(*data2); |
| 226 | |
| 227 | shared_ptr<Data> data3 = makeData("/insertandremovebyname/1"); |
| 228 | ims.insert(*data3); |
| 229 | |
| 230 | shared_ptr<Data> data4 = makeData("/insertandremovebyname/2"); |
| 231 | ims.insert(*data4); |
| 232 | |
| 233 | BOOST_CHECK_EQUAL(ims.size(), 4); |
| 234 | |
| 235 | ims.erase(data1->getFullName(), false); |
| 236 | BOOST_CHECK_EQUAL(ims.size(), 3); |
| 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages) |
| 240 | { |
| 241 | T ims; |
| 242 | |
| 243 | shared_ptr<Data> data = makeData("/a"); |
| 244 | ims.insert(*data); |
| 245 | |
| 246 | shared_ptr<Data> data2 = makeData("/b"); |
| 247 | ims.insert(*data2); |
| 248 | |
| 249 | shared_ptr<Data> data3 = makeData("/c"); |
| 250 | ims.insert(*data3); |
| 251 | |
| 252 | shared_ptr<Data> data4 = makeData("/d"); |
| 253 | ims.insert(*data4); |
| 254 | |
| 255 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 256 | ims.insert(*data5); |
| 257 | |
| 258 | shared_ptr<Data> data6 = makeData("/c/c/1/2/3"); |
| 259 | ims.insert(*data6); |
| 260 | |
| 261 | shared_ptr<Data> data7 = makeData("/c/c/1"); |
| 262 | ims.insert(*data7); |
| 263 | |
| 264 | BOOST_CHECK_EQUAL(ims.size(), 7); |
| 265 | |
| 266 | Name name("/c"); |
| 267 | ims.erase(name); |
| 268 | BOOST_CHECK_EQUAL(ims.size(), 3); |
| 269 | BOOST_CHECK_EQUAL(ims.getCapacity(), 5); |
| 270 | } |
| 271 | |
| 272 | BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages) |
| 273 | { |
| 274 | shared_ptr<Data> data = makeData("/digest/compute"); |
| 275 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 276 | ndn::ConstBufferPtr digest1 = ndn::crypto::computeSha256Digest(data->wireEncode().wire(), |
| 277 | data->wireEncode().size()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 278 | BOOST_CHECK_EQUAL(digest1->size(), 32); |
| 279 | |
| 280 | InMemoryStorageEntry* entry = new InMemoryStorageEntry(); |
| 281 | entry->setData(*data); |
| 282 | |
| 283 | BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(), |
| 284 | entry->getFullName()[-1].value_begin(), |
| 285 | entry->getFullName()[-1].value_end()); |
| 286 | } |
| 287 | |
| 288 | BOOST_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 | |
| 321 | BOOST_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 Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 345 | |
| 346 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 347 | BOOST_CHECK(true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | BOOST_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 Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 375 | ndn::ConstBufferPtr digest1 = ndn::crypto::computeSha256Digest(data->wireEncode().wire(), |
| 376 | data->wireEncode().size()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 377 | |
| 378 | Name name("/a"); |
| 379 | ims.erase(name); |
| 380 | BOOST_CHECK_EQUAL(ims.size(), 6); |
| 381 | } |
| 382 | |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 383 | BOOST_AUTO_TEST_CASE_TEMPLATE(ImplicitDigestSelector, T, InMemoryStorages) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 384 | { |
| 385 | T ims; |
| 386 | |
| 387 | Name name("/digest/works"); |
| 388 | shared_ptr<Data> data = makeData(name); |
| 389 | ims.insert(*data); |
| 390 | |
| 391 | shared_ptr<Data> data2 = makeData("/a"); |
| 392 | ims.insert(*data2); |
| 393 | |
| 394 | shared_ptr<Data> data3 = makeData("/z/z/z"); |
| 395 | ims.insert(*data3); |
| 396 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 397 | ndn::ConstBufferPtr digest1 = ndn::crypto::computeSha256Digest(data->wireEncode().wire(), |
| 398 | data->wireEncode().size()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 399 | |
| 400 | shared_ptr<Interest> interest = makeInterest(""); |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 401 | interest->setName(Name(name).appendImplicitSha256Digest(digest1->buf(), digest1->size())); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 402 | interest->setMinSuffixComponents(0); |
| 403 | interest->setMaxSuffixComponents(0); |
| 404 | |
| 405 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 406 | BOOST_REQUIRE(found != nullptr); |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 407 | BOOST_CHECK_EQUAL(found->getName(), name); |
| 408 | |
| 409 | shared_ptr<Interest> interest2 = makeInterest(""); |
| 410 | uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 411 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}; |
| 412 | interest2->setName(Name(name).appendImplicitSha256Digest(digest2, 32)); |
| 413 | interest2->setMinSuffixComponents(0); |
| 414 | interest2->setMaxSuffixComponents(0); |
| 415 | |
| 416 | shared_ptr<const Data> notfound = ims.find(*interest2); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 417 | BOOST_CHECK(notfound == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector, T, InMemoryStorages) |
| 421 | { |
| 422 | T ims; |
| 423 | |
| 424 | shared_ptr<Data> data = makeData("/a"); |
| 425 | ims.insert(*data); |
| 426 | |
| 427 | shared_ptr<Data> data2 = makeData("/b"); |
| 428 | ims.insert(*data2); |
| 429 | |
| 430 | shared_ptr<Data> data4 = makeData("/d"); |
| 431 | ims.insert(*data4); |
| 432 | |
| 433 | shared_ptr<Data> data5 = makeData("/c/c"); |
| 434 | ims.insert(*data5); |
| 435 | |
| 436 | shared_ptr<Data> data6 = makeData("/c/f"); |
| 437 | ims.insert(*data6); |
| 438 | |
| 439 | shared_ptr<Data> data7 = makeData("/c/n"); |
| 440 | ims.insert(*data7); |
| 441 | |
| 442 | shared_ptr<Interest> interest = makeInterest("/c"); |
| 443 | interest->setChildSelector(1); |
| 444 | |
| 445 | shared_ptr<const Data> found = ims.find(*interest); |
| 446 | BOOST_CHECK_EQUAL(found->getName(), "/c/n"); |
| 447 | |
| 448 | shared_ptr<Interest> interest2 = makeInterest("/c"); |
| 449 | interest2->setChildSelector(0); |
| 450 | |
| 451 | shared_ptr<const Data> found2 = ims.find(*interest2); |
| 452 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c"); |
| 453 | } |
| 454 | |
| 455 | BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector2, T, InMemoryStorages) |
| 456 | { |
| 457 | T ims; |
| 458 | |
| 459 | shared_ptr<Data> data = makeData("/a/b/1"); |
| 460 | ims.insert(*data); |
| 461 | |
| 462 | shared_ptr<Data> data2 = makeData("/a/b/2"); |
| 463 | ims.insert(*data2); |
| 464 | |
| 465 | shared_ptr<Data> data3 = makeData("/a/z/1"); |
| 466 | ims.insert(*data3); |
| 467 | |
| 468 | shared_ptr<Data> data4 = makeData("/a/z/2"); |
| 469 | ims.insert(*data4); |
| 470 | |
| 471 | shared_ptr<Interest> interest = makeInterest("/a"); |
| 472 | interest->setChildSelector(1); |
| 473 | |
| 474 | shared_ptr<const Data> found = ims.find(*interest); |
| 475 | BOOST_CHECK_EQUAL(found->getName(), "/a/z/1"); |
| 476 | } |
| 477 | |
| 478 | BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector, T, InMemoryStorages) |
| 479 | { |
| 480 | T ims; |
| 481 | |
| 482 | Name name("/insert/withkey"); |
| 483 | shared_ptr<Data> data = makeData(name); |
| 484 | ims.insert(*data); |
| 485 | |
| 486 | shared_ptr<Interest> interest = makeInterest(name); |
| 487 | Name keyName("/somewhere/key"); |
| 488 | |
| 489 | ndn::KeyLocator locator(keyName); |
| 490 | interest->setPublisherPublicKeyLocator(locator); |
| 491 | |
| 492 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 493 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector2, T, InMemoryStorages) |
| 497 | { |
| 498 | T ims; |
| 499 | Name name("/insert/withkey"); |
| 500 | shared_ptr<Data> data = makeData(name); |
| 501 | ims.insert(*data); |
| 502 | |
| 503 | Name name2("/insert/withkey2"); |
| 504 | shared_ptr<Data> data2 = make_shared<Data>(name2); |
| 505 | |
| 506 | Name keyName("/somewhere/key"); |
| 507 | const ndn::KeyLocator locator(keyName); |
| 508 | |
| 509 | ndn::SignatureSha256WithRsa fakeSignature; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 510 | fakeSignature.setValue(makeEmptyBlock(tlv::SignatureValue)); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 511 | |
| 512 | fakeSignature.setKeyLocator(locator); |
| 513 | data2->setSignature(fakeSignature); |
| 514 | data2->wireEncode(); |
| 515 | |
| 516 | ims.insert(*data2); |
| 517 | |
| 518 | shared_ptr<Interest> interest = makeInterest(name2); |
| 519 | interest->setPublisherPublicKeyLocator(locator); |
| 520 | |
| 521 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 522 | BOOST_CHECK(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 523 | BOOST_CHECK_EQUAL(found->getName(), data2->getName()); |
| 524 | } |
| 525 | |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 526 | BOOST_AUTO_TEST_CASE_TEMPLATE(MinMaxComponentsSelector, T, InMemoryStorages) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 527 | { |
| 528 | T ims; |
| 529 | |
| 530 | shared_ptr<Data> data = makeData("/a"); |
| 531 | ims.insert(*data); |
| 532 | |
| 533 | shared_ptr<Data> data2 = makeData("/b"); |
| 534 | ims.insert(*data2); |
| 535 | |
| 536 | shared_ptr<Data> data4 = makeData("/d"); |
| 537 | ims.insert(*data4); |
| 538 | |
| 539 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 540 | ims.insert(*data5); |
| 541 | |
| 542 | shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9"); |
| 543 | ims.insert(*data6); |
| 544 | |
| 545 | shared_ptr<Data> data7 = makeData("/c/c/1/2/3"); |
| 546 | ims.insert(*data7); |
| 547 | |
| 548 | shared_ptr<Data> data8 = makeData("/c/c/1"); |
| 549 | ims.insert(*data8); |
| 550 | |
| 551 | shared_ptr<Interest> interest = makeInterest("/c/c"); |
| 552 | interest->setMinSuffixComponents(3); |
| 553 | interest->setChildSelector(0); |
| 554 | |
| 555 | shared_ptr<const Data> found = ims.find(*interest); |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 556 | BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 557 | |
| 558 | shared_ptr<Interest> interest2 = makeInterest("/c/c"); |
| 559 | interest2->setMinSuffixComponents(4); |
| 560 | interest2->setChildSelector(1); |
| 561 | |
| 562 | shared_ptr<const Data> found2 = ims.find(*interest2); |
| 563 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9"); |
| 564 | |
| 565 | shared_ptr<Interest> interest3 = makeInterest("/c/c"); |
| 566 | interest3->setMaxSuffixComponents(2); |
| 567 | interest3->setChildSelector(1); |
| 568 | |
| 569 | shared_ptr<const Data> found3 = ims.find(*interest3); |
| 570 | BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1"); |
| 571 | } |
| 572 | |
| 573 | BOOST_AUTO_TEST_CASE_TEMPLATE(ExcludeSelector, T, InMemoryStorages) |
| 574 | { |
| 575 | T ims; |
| 576 | |
| 577 | shared_ptr<Data> data = makeData("/a"); |
| 578 | ims.insert(*data); |
| 579 | |
| 580 | shared_ptr<Data> data2 = makeData("/b"); |
| 581 | ims.insert(*data2); |
| 582 | |
| 583 | shared_ptr<Data> data3 = makeData("/c/a"); |
| 584 | ims.insert(*data3); |
| 585 | |
| 586 | shared_ptr<Data> data4 = makeData("/d"); |
| 587 | ims.insert(*data4); |
| 588 | |
| 589 | shared_ptr<Data> data5 = makeData("/c/c"); |
| 590 | ims.insert(*data5); |
| 591 | |
| 592 | shared_ptr<Data> data6 = makeData("/c/f"); |
| 593 | ims.insert(*data6); |
| 594 | |
| 595 | shared_ptr<Data> data7 = makeData("/c/n"); |
| 596 | ims.insert(*data7); |
| 597 | |
| 598 | shared_ptr<Interest> interest = makeInterest("/c"); |
| 599 | interest->setChildSelector(1); |
| 600 | Exclude e; |
| 601 | e.excludeOne (Name::Component("n")); |
| 602 | interest->setExclude(e); |
| 603 | |
| 604 | shared_ptr<const Data> found = ims.find(*interest); |
| 605 | BOOST_CHECK_EQUAL(found->getName(), "/c/f"); |
| 606 | |
| 607 | shared_ptr<Interest> interest2 = makeInterest("/c"); |
| 608 | interest2->setChildSelector(0); |
| 609 | |
| 610 | Exclude e2; |
| 611 | e2.excludeOne (Name::Component("a")); |
| 612 | interest2->setExclude(e2); |
| 613 | |
| 614 | shared_ptr<const Data> found2 = ims.find(*interest2); |
| 615 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c"); |
| 616 | |
| 617 | shared_ptr<Interest> interest3 = makeInterest("/c"); |
| 618 | interest3->setChildSelector(0); |
| 619 | |
| 620 | Exclude e3; |
| 621 | e3.excludeOne (Name::Component("c")); |
| 622 | interest3->setExclude(e3); |
| 623 | |
| 624 | shared_ptr<const Data> found3 = ims.find(*interest3); |
| 625 | BOOST_CHECK_EQUAL(found3->getName(), "/c/a"); |
| 626 | } |
| 627 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 628 | using InMemoryStoragesLimited = boost::mpl::list<InMemoryStorageFifo, |
| 629 | InMemoryStorageLfu, |
| 630 | InMemoryStorageLru>; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 631 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 632 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 633 | { |
| 634 | T ims; |
| 635 | |
| 636 | ims.setCapacity(3); |
| 637 | ims.insert(*makeData("/1")); |
| 638 | ims.insert(*makeData("/2")); |
| 639 | ims.insert(*makeData("/3")); |
| 640 | BOOST_CHECK_EQUAL(ims.size(), 3); |
| 641 | |
| 642 | ims.setCapacity(2); |
| 643 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 644 | } |
| 645 | |
| 646 | BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited) |
| 647 | { |
| 648 | T ims(10000); |
| 649 | |
| 650 | BOOST_CHECK_EQUAL(ims.getLimit(), 10000); |
| 651 | |
| 652 | T ims2(4); |
| 653 | |
| 654 | BOOST_CHECK_EQUAL(ims2.getLimit(), 4); |
| 655 | } |
| 656 | |
| 657 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited) |
| 658 | { |
| 659 | T ims(40); |
| 660 | |
| 661 | for (int i = 0; i < 11; i++) { |
| 662 | std::ostringstream convert; |
| 663 | convert << i; |
| 664 | Name name("/" + convert.str()); |
| 665 | shared_ptr<Data> data = makeData(name); |
| 666 | data->setFreshnessPeriod(time::milliseconds(5000)); |
| 667 | signData(data); |
| 668 | ims.insert(*data); |
| 669 | } |
| 670 | |
| 671 | BOOST_CHECK_EQUAL(ims.size(), 11); |
| 672 | |
| 673 | BOOST_CHECK_EQUAL(ims.getCapacity(), 20); |
| 674 | } |
| 675 | |
| 676 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited) |
| 677 | { |
| 678 | T ims(2); |
| 679 | |
| 680 | Name name("/insert/1"); |
| 681 | shared_ptr<Data> data = makeData(name); |
| 682 | ims.insert(*data); |
| 683 | |
| 684 | Name name2("/insert/2"); |
| 685 | shared_ptr<Data> data2 = makeData(name2); |
| 686 | ims.insert(*data2); |
| 687 | |
| 688 | Name name3("/insert/3"); |
| 689 | shared_ptr<Data> data3 = makeData(name3); |
| 690 | ims.insert(*data3); |
| 691 | |
| 692 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 693 | |
| 694 | shared_ptr<Interest> interest = makeInterest(name); |
| 695 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 696 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | ///as Find function is implemented at the base case, therefore testing for one derived class is |
| 700 | ///sufficient for all |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 701 | class FindFixture : public ndn::tests::UnitTestTimeFixture |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 702 | { |
| 703 | protected: |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 704 | FindFixture() |
| 705 | : m_ims(io) |
| 706 | { |
| 707 | } |
| 708 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 709 | Name |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 710 | insert(uint32_t id, const Name& name, |
| 711 | const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 712 | { |
| 713 | shared_ptr<Data> data = makeData(name); |
| 714 | data->setFreshnessPeriod(time::milliseconds(99999)); |
| 715 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
| 716 | signData(data); |
| 717 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 718 | m_ims.insert(*data, freshWindow); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 719 | |
| 720 | return data->getFullName(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | Interest& |
| 724 | startInterest(const Name& name) |
| 725 | { |
| 726 | m_interest = makeInterest(name); |
| 727 | return *m_interest; |
| 728 | } |
| 729 | |
| 730 | uint32_t |
| 731 | find() |
| 732 | { |
| 733 | shared_ptr<const Data> found = m_ims.find(*m_interest); |
| 734 | if (found == 0) { |
| 735 | return 0; |
| 736 | } |
| 737 | const Block& content = found->getContent(); |
| 738 | if (content.value_size() != sizeof(uint32_t)) { |
| 739 | return 0; |
| 740 | } |
| 741 | return *reinterpret_cast<const uint32_t*>(content.value()); |
| 742 | } |
| 743 | |
| 744 | protected: |
| 745 | InMemoryStoragePersistent m_ims; |
| 746 | shared_ptr<Interest> m_interest; |
| 747 | }; |
| 748 | |
| 749 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 750 | |
| 751 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 752 | { |
| 753 | insert(1, "ndn:/"); |
| 754 | |
| 755 | startInterest("ndn:/"); |
| 756 | BOOST_CHECK_EQUAL(find(), 1); |
| 757 | } |
| 758 | |
| 759 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 760 | { |
| 761 | insert(1, "ndn:/A"); |
| 762 | |
| 763 | startInterest("ndn:/"); |
| 764 | BOOST_CHECK_EQUAL(find(), 1); |
| 765 | } |
| 766 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 767 | BOOST_AUTO_TEST_CASE(ExactName) |
| 768 | { |
| 769 | insert(1, "ndn:/"); |
| 770 | insert(2, "ndn:/A"); |
| 771 | insert(3, "ndn:/A/B"); |
| 772 | insert(4, "ndn:/A/C"); |
| 773 | insert(5, "ndn:/D"); |
| 774 | |
| 775 | startInterest("ndn:/A"); |
| 776 | BOOST_CHECK_EQUAL(find(), 2); |
| 777 | } |
| 778 | |
| 779 | BOOST_AUTO_TEST_CASE(FullName) |
| 780 | { |
| 781 | Name n1 = insert(1, "ndn:/A"); |
| 782 | Name n2 = insert(2, "ndn:/A"); |
| 783 | |
| 784 | startInterest(n1); |
| 785 | BOOST_CHECK_EQUAL(find(), 1); |
| 786 | |
| 787 | startInterest(n2); |
| 788 | BOOST_CHECK_EQUAL(find(), 2); |
| 789 | } |
| 790 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 791 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 792 | { |
| 793 | insert(1, "ndn:/A"); |
| 794 | insert(2, "ndn:/B/p/1"); |
| 795 | insert(3, "ndn:/B/p/2"); |
| 796 | insert(4, "ndn:/B/q/1"); |
| 797 | insert(5, "ndn:/B/q/2"); |
| 798 | insert(6, "ndn:/C"); |
| 799 | |
| 800 | startInterest("ndn:/B"); |
| 801 | BOOST_CHECK_EQUAL(find(), 2); |
| 802 | } |
| 803 | |
| 804 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 805 | { |
| 806 | insert(1, "ndn:/A"); |
| 807 | insert(2, "ndn:/B/p/1"); |
| 808 | insert(3, "ndn:/B/p/2"); |
| 809 | insert(4, "ndn:/B/q/1"); |
| 810 | insert(5, "ndn:/B/q/2"); |
| 811 | insert(6, "ndn:/C"); |
| 812 | |
| 813 | startInterest("ndn:/B") |
| 814 | .setChildSelector(1); |
| 815 | BOOST_CHECK_EQUAL(find(), 4); |
| 816 | } |
| 817 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 818 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 819 | { |
| 820 | insert(1, "ndn:/"); |
| 821 | insert(2, "ndn:/A"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 822 | insert(3, "ndn:/B/1"); |
| 823 | insert(4, "ndn:/C/1/2"); |
| 824 | insert(5, "ndn:/D/1/2/3"); |
| 825 | insert(6, "ndn:/E/1/2/3/4"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 826 | |
| 827 | startInterest("ndn:/") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 828 | .setMinSuffixComponents(0); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 829 | BOOST_CHECK_EQUAL(find(), 1); |
| 830 | |
| 831 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 832 | .setMinSuffixComponents(1); |
| 833 | BOOST_CHECK_EQUAL(find(), 1); |
| 834 | |
| 835 | startInterest("ndn:/") |
| 836 | .setMinSuffixComponents(2); |
| 837 | BOOST_CHECK_EQUAL(find(), 2); |
| 838 | |
| 839 | startInterest("ndn:/") |
| 840 | .setMinSuffixComponents(3); |
| 841 | BOOST_CHECK_EQUAL(find(), 3); |
| 842 | |
| 843 | startInterest("ndn:/") |
| 844 | .setMinSuffixComponents(4); |
| 845 | BOOST_CHECK_EQUAL(find(), 4); |
| 846 | |
| 847 | startInterest("ndn:/") |
| 848 | .setMinSuffixComponents(5); |
| 849 | BOOST_CHECK_EQUAL(find(), 5); |
| 850 | |
| 851 | startInterest("ndn:/") |
| 852 | .setMinSuffixComponents(6); |
| 853 | BOOST_CHECK_EQUAL(find(), 6); |
| 854 | |
| 855 | startInterest("ndn:/") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 856 | .setMinSuffixComponents(7); |
| 857 | BOOST_CHECK_EQUAL(find(), 0); |
| 858 | } |
| 859 | |
| 860 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 861 | { |
| 862 | insert(1, "ndn:/"); |
| 863 | insert(2, "ndn:/A"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 864 | insert(3, "ndn:/B/2"); |
| 865 | insert(4, "ndn:/C/2/3"); |
| 866 | insert(5, "ndn:/D/2/3/4"); |
| 867 | insert(6, "ndn:/E/2/3/4/5"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 868 | |
| 869 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 870 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 871 | .setMaxSuffixComponents(0); |
| 872 | BOOST_CHECK_EQUAL(find(), 0); |
| 873 | |
| 874 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 875 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 876 | .setMaxSuffixComponents(1); |
| 877 | BOOST_CHECK_EQUAL(find(), 1); |
| 878 | |
| 879 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 880 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 881 | .setMaxSuffixComponents(2); |
| 882 | BOOST_CHECK_EQUAL(find(), 2); |
| 883 | |
| 884 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 885 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 886 | .setMaxSuffixComponents(3); |
| 887 | BOOST_CHECK_EQUAL(find(), 3); |
| 888 | |
| 889 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 890 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 891 | .setMaxSuffixComponents(4); |
| 892 | BOOST_CHECK_EQUAL(find(), 4); |
| 893 | |
| 894 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 895 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 896 | .setMaxSuffixComponents(5); |
| 897 | BOOST_CHECK_EQUAL(find(), 5); |
| 898 | |
| 899 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 900 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 901 | .setMaxSuffixComponents(6); |
| 902 | BOOST_CHECK_EQUAL(find(), 6); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 903 | |
| 904 | startInterest("ndn:/") |
| 905 | .setChildSelector(1) |
| 906 | .setMaxSuffixComponents(7); |
| 907 | BOOST_CHECK_EQUAL(find(), 6); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 911 | { |
| 912 | insert(1, "ndn:/A"); |
| 913 | insert(2, "ndn:/A"); |
| 914 | // We don't know which comes first, but there must be some order |
| 915 | |
| 916 | startInterest("ndn:/A") |
| 917 | .setChildSelector(0); |
| 918 | uint32_t leftmost = find(); |
| 919 | |
| 920 | startInterest("ndn:/A") |
| 921 | .setChildSelector(1); |
| 922 | uint32_t rightmost = find(); |
| 923 | |
| 924 | BOOST_CHECK_NE(leftmost, rightmost); |
| 925 | } |
| 926 | |
| 927 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 928 | { |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 929 | insert(1, "ndn:/A"); |
| 930 | Name n2 = insert(2, "ndn:/A"); |
| 931 | insert(3, "ndn:/A/B"); |
| 932 | |
| 933 | uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 934 | std::fill_n(digest00, sizeof(digest00), 0x00); |
| 935 | uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 936 | std::fill_n(digestFF, sizeof(digestFF), 0xFF); |
| 937 | |
| 938 | Exclude excludeDigest; |
| 939 | excludeDigest.excludeRange( |
| 940 | name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)), |
| 941 | name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF))); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 942 | |
| 943 | startInterest("ndn:/A") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 944 | .setChildSelector(0) |
| 945 | .setExclude(excludeDigest); |
| 946 | BOOST_CHECK_EQUAL(find(), 3); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 947 | |
| 948 | startInterest("ndn:/A") |
| 949 | .setChildSelector(1) |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 950 | .setExclude(excludeDigest); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 951 | BOOST_CHECK_EQUAL(find(), 3); |
| 952 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 953 | Exclude excludeGeneric; |
| 954 | excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0)); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 955 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 956 | startInterest("ndn:/A") |
| 957 | .setChildSelector(0) |
| 958 | .setExclude(excludeGeneric); |
| 959 | int found1 = find(); |
| 960 | BOOST_CHECK(found1 == 1 || found1 == 2); |
| 961 | |
| 962 | startInterest("ndn:/A") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 963 | .setChildSelector(1) |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 964 | .setExclude(excludeGeneric); |
| 965 | int found2 = find(); |
| 966 | BOOST_CHECK(found2 == 1 || found2 == 2); |
| 967 | |
| 968 | Exclude exclude2 = excludeGeneric; |
| 969 | exclude2.excludeOne(n2.get(-1)); |
| 970 | |
| 971 | startInterest("ndn:/A") |
| 972 | .setChildSelector(0) |
| 973 | .setExclude(exclude2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 974 | BOOST_CHECK_EQUAL(find(), 1); |
| 975 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 976 | startInterest("ndn:/A") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 977 | .setChildSelector(1) |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 978 | .setExclude(exclude2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 979 | BOOST_CHECK_EQUAL(find(), 1); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 982 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 983 | { |
| 984 | Name data1Name = insert(1, "ndn:/A/1", time::milliseconds(500)); |
| 985 | insert(2, "ndn:/A/2", time::milliseconds(2500)); |
| 986 | insert(3, "ndn:/A/3", time::milliseconds(3500)); |
| 987 | insert(4, "ndn:/A/4", time::milliseconds(1500)); |
| 988 | |
| 989 | // @0s, all Data are fresh |
| 990 | startInterest("ndn:/A/1") |
| 991 | .setMustBeFresh(true); |
| 992 | BOOST_CHECK_EQUAL(find(), 1); |
| 993 | |
| 994 | startInterest("ndn:/A/1") |
| 995 | .setMustBeFresh(false); |
| 996 | BOOST_CHECK_EQUAL(find(), 1); |
| 997 | |
| 998 | startInterest("ndn:/A") |
| 999 | .setMustBeFresh(true) |
| 1000 | .setChildSelector(0); |
| 1001 | BOOST_CHECK_EQUAL(find(), 1); |
| 1002 | |
| 1003 | startInterest("ndn:/A") |
| 1004 | .setMustBeFresh(true) |
| 1005 | .setChildSelector(1); |
| 1006 | BOOST_CHECK_EQUAL(find(), 4); |
| 1007 | |
| 1008 | advanceClocks(time::milliseconds(1000)); |
| 1009 | // @1s, /A/1 is stale |
| 1010 | startInterest("ndn:/A/1") |
| 1011 | .setMustBeFresh(true); |
| 1012 | BOOST_CHECK_EQUAL(find(), 0); |
| 1013 | startInterest("ndn:/A/1") |
| 1014 | .setMustBeFresh(false); |
| 1015 | BOOST_CHECK_EQUAL(find(), 1); |
| 1016 | |
| 1017 | // MustBeFresh is ignored when full Name is specified |
| 1018 | startInterest(data1Name) |
| 1019 | .setMustBeFresh(true); |
| 1020 | BOOST_CHECK_EQUAL(find(), 1); |
| 1021 | |
| 1022 | startInterest("ndn:/A") |
| 1023 | .setMustBeFresh(true) |
| 1024 | .setChildSelector(0); |
| 1025 | BOOST_CHECK_EQUAL(find(), 2); |
| 1026 | startInterest("ndn:/A") |
| 1027 | .setMustBeFresh(false) |
| 1028 | .setChildSelector(0); |
| 1029 | BOOST_CHECK_EQUAL(find(), 1); |
| 1030 | |
| 1031 | advanceClocks(time::milliseconds(1000)); |
| 1032 | // @2s, /A/1 and /A/4 are stale |
| 1033 | startInterest("ndn:/A") |
| 1034 | .setMustBeFresh(true) |
| 1035 | .setChildSelector(1); |
| 1036 | BOOST_CHECK_EQUAL(find(), 3); |
| 1037 | startInterest("ndn:/A") |
| 1038 | .setMustBeFresh(false) |
| 1039 | .setChildSelector(1); |
| 1040 | BOOST_CHECK_EQUAL(find(), 4); |
| 1041 | |
| 1042 | advanceClocks(time::milliseconds(2000)); |
| 1043 | // @4s, all Data are stale |
| 1044 | startInterest("ndn:/A") |
| 1045 | .setMustBeFresh(true) |
| 1046 | .setChildSelector(0); |
| 1047 | BOOST_CHECK_EQUAL(find(), 0); |
| 1048 | startInterest("ndn:/A") |
| 1049 | .setMustBeFresh(true) |
| 1050 | .setChildSelector(1); |
| 1051 | BOOST_CHECK_EQUAL(find(), 0); |
| 1052 | } |
| 1053 | |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 1054 | BOOST_AUTO_TEST_SUITE_END() // Find |
| 1055 | BOOST_AUTO_TEST_SUITE_END() // Common |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 1056 | BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage |
| 1057 | BOOST_AUTO_TEST_SUITE_END() // Util |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1058 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 1059 | } // namespace tests |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1060 | } // namespace util |
| 1061 | } // namespace ndn |