Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/ims/in-memory-storage.hpp" |
| 23 | #include "ndn-cxx/ims/in-memory-storage-fifo.hpp" |
| 24 | #include "ndn-cxx/ims/in-memory-storage-lfu.hpp" |
| 25 | #include "ndn-cxx/ims/in-memory-storage-lru.hpp" |
| 26 | #include "ndn-cxx/ims/in-memory-storage-persistent.hpp" |
| 27 | #include "ndn-cxx/security/signature-sha256-with-rsa.hpp" |
| 28 | #include "ndn-cxx/util/sha256.hpp" |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 30 | #include "tests/boost-test.hpp" |
| 31 | #include "tests/make-interest-data.hpp" |
| 32 | #include "tests/unit/unit-test-time-fixture.hpp" |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 34 | #include <boost/mpl/vector.hpp> |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 37 | namespace tests { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 39 | using namespace ndn::tests; |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 40 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(Ims) |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 42 | BOOST_AUTO_TEST_SUITE(TestInMemoryStorage) |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 43 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 44 | using InMemoryStorages = boost::mpl::vector<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); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 69 | data1->setFreshnessPeriod(99999_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 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); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 76 | data2->setFreshnessPeriod(99999_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 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); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 215 | data1->setFreshnessPeriod(99999_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 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); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 222 | data2->setFreshnessPeriod(99999_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 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); |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 269 | BOOST_CHECK_EQUAL(ims.getCapacity(), 16); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages) |
| 273 | { |
| 274 | shared_ptr<Data> data = makeData("/digest/compute"); |
| 275 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 276 | ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 277 | BOOST_CHECK_EQUAL(digest1->size(), 32); |
| 278 | |
Alexander Afanasyev | 3e2c3a0 | 2017-01-19 13:55:56 -0800 | [diff] [blame] | 279 | InMemoryStorageEntry entry; |
| 280 | entry.setData(*data); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 281 | |
| 282 | BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(), |
Alexander Afanasyev | 3e2c3a0 | 2017-01-19 13:55:56 -0800 | [diff] [blame] | 283 | entry.getFullName()[-1].value_begin(), |
| 284 | entry.getFullName()[-1].value_end()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages) |
| 288 | { |
| 289 | T ims; |
| 290 | |
| 291 | BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>)); |
| 292 | |
| 293 | for (int i = 0; i < 10; i++) { |
| 294 | std::ostringstream convert; |
| 295 | convert << i; |
| 296 | Name name("/" + convert.str()); |
| 297 | shared_ptr<Data> data = makeData(name); |
| 298 | ims.insert(*data); |
| 299 | } |
| 300 | |
| 301 | InMemoryStorage::const_iterator it = ims.begin(); |
| 302 | InMemoryStorage::const_iterator tmp1 = it; |
| 303 | BOOST_REQUIRE(tmp1 == it); |
| 304 | InMemoryStorage::const_iterator tmp2 = tmp1++; |
| 305 | BOOST_REQUIRE(tmp2 != tmp1); |
| 306 | tmp2 = ++tmp1; |
| 307 | BOOST_REQUIRE(tmp2 == tmp1); |
| 308 | |
| 309 | int i = 0; |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 310 | for (; it != ims.end(); it++) { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 311 | std::ostringstream convert; |
| 312 | convert << i; |
| 313 | Name name("/" + convert.str()); |
| 314 | BOOST_CHECK_EQUAL(it->getName(), name); |
| 315 | BOOST_CHECK_EQUAL((*it).getName(), name); |
| 316 | i++; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages) |
| 321 | { |
| 322 | T ims; |
| 323 | |
| 324 | shared_ptr<Data> data = makeData("/a"); |
| 325 | ims.insert(*data); |
| 326 | |
| 327 | shared_ptr<Data> data2 = makeData("/b"); |
| 328 | ims.insert(*data2); |
| 329 | |
| 330 | shared_ptr<Data> data3 = makeData("/c"); |
| 331 | ims.insert(*data3); |
| 332 | |
| 333 | shared_ptr<Data> data4 = makeData("/d"); |
| 334 | ims.insert(*data4); |
| 335 | |
| 336 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 337 | ims.insert(*data5); |
| 338 | |
| 339 | shared_ptr<Data> data6 = makeData("/c/c/1/2/3"); |
| 340 | ims.insert(*data6); |
| 341 | |
| 342 | shared_ptr<Data> data7 = makeData("/c/c/1"); |
| 343 | ims.insert(*data7); |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 344 | |
| 345 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 346 | BOOST_CHECK(true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages) |
| 350 | { |
| 351 | T ims; |
| 352 | |
| 353 | shared_ptr<Data> data = makeData("/a"); |
| 354 | ims.insert(*data); |
| 355 | |
| 356 | shared_ptr<Data> data2 = makeData("/b"); |
| 357 | ims.insert(*data2); |
| 358 | |
| 359 | shared_ptr<Data> data3 = makeData("/c"); |
| 360 | ims.insert(*data3); |
| 361 | |
| 362 | shared_ptr<Data> data4 = makeData("/d"); |
| 363 | ims.insert(*data4); |
| 364 | |
| 365 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 366 | ims.insert(*data5); |
| 367 | |
| 368 | shared_ptr<Data> data6 = makeData("/c/c/1/2/3"); |
| 369 | ims.insert(*data6); |
| 370 | |
| 371 | shared_ptr<Data> data7 = makeData("/c/c/1"); |
| 372 | ims.insert(*data7); |
| 373 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 374 | ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 375 | |
| 376 | Name name("/a"); |
| 377 | ims.erase(name); |
| 378 | BOOST_CHECK_EQUAL(ims.size(), 6); |
| 379 | } |
| 380 | |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 381 | BOOST_AUTO_TEST_CASE_TEMPLATE(ImplicitDigestSelector, T, InMemoryStorages) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 382 | { |
| 383 | T ims; |
| 384 | |
| 385 | Name name("/digest/works"); |
| 386 | shared_ptr<Data> data = makeData(name); |
| 387 | ims.insert(*data); |
| 388 | |
| 389 | shared_ptr<Data> data2 = makeData("/a"); |
| 390 | ims.insert(*data2); |
| 391 | |
| 392 | shared_ptr<Data> data3 = makeData("/z/z/z"); |
| 393 | ims.insert(*data3); |
| 394 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 395 | ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 396 | |
| 397 | shared_ptr<Interest> interest = makeInterest(""); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 398 | interest->setName(Name(name).appendImplicitSha256Digest(digest1->data(), digest1->size())); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 399 | interest->setMinSuffixComponents(0); |
| 400 | interest->setMaxSuffixComponents(0); |
| 401 | |
| 402 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 403 | BOOST_REQUIRE(found != nullptr); |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 404 | BOOST_CHECK_EQUAL(found->getName(), name); |
| 405 | |
| 406 | shared_ptr<Interest> interest2 = makeInterest(""); |
| 407 | uint8_t digest2[32] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 408 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}; |
| 409 | interest2->setName(Name(name).appendImplicitSha256Digest(digest2, 32)); |
| 410 | interest2->setMinSuffixComponents(0); |
| 411 | interest2->setMaxSuffixComponents(0); |
| 412 | |
| 413 | shared_ptr<const Data> notfound = ims.find(*interest2); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 414 | BOOST_CHECK(notfound == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector, T, InMemoryStorages) |
| 418 | { |
| 419 | T ims; |
| 420 | |
| 421 | shared_ptr<Data> data = makeData("/a"); |
| 422 | ims.insert(*data); |
| 423 | |
| 424 | shared_ptr<Data> data2 = makeData("/b"); |
| 425 | ims.insert(*data2); |
| 426 | |
| 427 | shared_ptr<Data> data4 = makeData("/d"); |
| 428 | ims.insert(*data4); |
| 429 | |
| 430 | shared_ptr<Data> data5 = makeData("/c/c"); |
| 431 | ims.insert(*data5); |
| 432 | |
| 433 | shared_ptr<Data> data6 = makeData("/c/f"); |
| 434 | ims.insert(*data6); |
| 435 | |
| 436 | shared_ptr<Data> data7 = makeData("/c/n"); |
| 437 | ims.insert(*data7); |
| 438 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 439 | shared_ptr<Interest> interest = makeInterest("/c", true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 440 | interest->setChildSelector(1); |
| 441 | |
| 442 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 443 | BOOST_REQUIRE(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 444 | BOOST_CHECK_EQUAL(found->getName(), "/c/n"); |
| 445 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 446 | shared_ptr<Interest> interest2 = makeInterest("/c", true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 447 | interest2->setChildSelector(0); |
| 448 | |
| 449 | shared_ptr<const Data> found2 = ims.find(*interest2); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 450 | BOOST_REQUIRE(found2 != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 451 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c"); |
| 452 | } |
| 453 | |
| 454 | BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector2, T, InMemoryStorages) |
| 455 | { |
| 456 | T ims; |
| 457 | |
| 458 | shared_ptr<Data> data = makeData("/a/b/1"); |
| 459 | ims.insert(*data); |
| 460 | |
| 461 | shared_ptr<Data> data2 = makeData("/a/b/2"); |
| 462 | ims.insert(*data2); |
| 463 | |
| 464 | shared_ptr<Data> data3 = makeData("/a/z/1"); |
| 465 | ims.insert(*data3); |
| 466 | |
| 467 | shared_ptr<Data> data4 = makeData("/a/z/2"); |
| 468 | ims.insert(*data4); |
| 469 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 470 | shared_ptr<Interest> interest = makeInterest("/a", true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 471 | interest->setChildSelector(1); |
| 472 | |
| 473 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 474 | BOOST_REQUIRE(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 489 | KeyLocator locator(keyName); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 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"); |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 507 | const KeyLocator locator(keyName); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 508 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 509 | 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 | fakeSignature.setKeyLocator(locator); |
| 512 | data2->setSignature(fakeSignature); |
| 513 | data2->wireEncode(); |
| 514 | |
| 515 | ims.insert(*data2); |
| 516 | |
| 517 | shared_ptr<Interest> interest = makeInterest(name2); |
| 518 | interest->setPublisherPublicKeyLocator(locator); |
| 519 | |
| 520 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 521 | BOOST_CHECK(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 522 | BOOST_CHECK_EQUAL(found->getName(), data2->getName()); |
| 523 | } |
| 524 | |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 525 | BOOST_AUTO_TEST_CASE_TEMPLATE(MinMaxComponentsSelector, T, InMemoryStorages) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 526 | { |
| 527 | T ims; |
| 528 | |
| 529 | shared_ptr<Data> data = makeData("/a"); |
| 530 | ims.insert(*data); |
| 531 | |
| 532 | shared_ptr<Data> data2 = makeData("/b"); |
| 533 | ims.insert(*data2); |
| 534 | |
| 535 | shared_ptr<Data> data4 = makeData("/d"); |
| 536 | ims.insert(*data4); |
| 537 | |
| 538 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 539 | ims.insert(*data5); |
| 540 | |
| 541 | shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9"); |
| 542 | ims.insert(*data6); |
| 543 | |
| 544 | shared_ptr<Data> data7 = makeData("/c/c/1/2/3"); |
| 545 | ims.insert(*data7); |
| 546 | |
| 547 | shared_ptr<Data> data8 = makeData("/c/c/1"); |
| 548 | ims.insert(*data8); |
| 549 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 550 | shared_ptr<Interest> interest = makeInterest("/c/c", true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 551 | interest->setMinSuffixComponents(3); |
| 552 | interest->setChildSelector(0); |
| 553 | |
| 554 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 555 | BOOST_REQUIRE(found != nullptr); |
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 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 558 | shared_ptr<Interest> interest2 = makeInterest("/c/c", true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 559 | interest2->setMinSuffixComponents(4); |
| 560 | interest2->setChildSelector(1); |
| 561 | |
| 562 | shared_ptr<const Data> found2 = ims.find(*interest2); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 563 | BOOST_REQUIRE(found2 != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 564 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9"); |
| 565 | |
| 566 | shared_ptr<Interest> interest3 = makeInterest("/c/c"); |
| 567 | interest3->setMaxSuffixComponents(2); |
| 568 | interest3->setChildSelector(1); |
| 569 | |
| 570 | shared_ptr<const Data> found3 = ims.find(*interest3); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 571 | BOOST_REQUIRE(found3 != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 572 | BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1"); |
| 573 | } |
| 574 | |
| 575 | BOOST_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 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 600 | shared_ptr<Interest> interest = makeInterest("/c", true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 601 | interest->setChildSelector(1); |
| 602 | Exclude e; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 603 | e.excludeOne(Name::Component("n")); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 604 | interest->setExclude(e); |
| 605 | |
| 606 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 607 | BOOST_REQUIRE(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 608 | BOOST_CHECK_EQUAL(found->getName(), "/c/f"); |
| 609 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 610 | shared_ptr<Interest> interest2 = makeInterest("/c", true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 611 | interest2->setChildSelector(0); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 612 | Exclude e2; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 613 | e2.excludeOne(Name::Component("a")); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 614 | interest2->setExclude(e2); |
| 615 | |
| 616 | shared_ptr<const Data> found2 = ims.find(*interest2); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 617 | BOOST_REQUIRE(found2 != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 618 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c"); |
| 619 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 620 | shared_ptr<Interest> interest3 = makeInterest("/c", true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 621 | interest3->setChildSelector(0); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 622 | Exclude e3; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 623 | e3.excludeOne(Name::Component("c")); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 624 | interest3->setExclude(e3); |
| 625 | |
| 626 | shared_ptr<const Data> found3 = ims.find(*interest3); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 627 | BOOST_REQUIRE(found3 != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 628 | BOOST_CHECK_EQUAL(found3->getName(), "/c/a"); |
| 629 | } |
| 630 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 631 | using InMemoryStoragesLimited = boost::mpl::vector<InMemoryStorageFifo, |
| 632 | InMemoryStorageLfu, |
| 633 | InMemoryStorageLru>; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 634 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 635 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 636 | { |
| 637 | T ims; |
| 638 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 639 | ims.setCapacity(18); |
| 640 | for (int i = 1; i < 19; ++i) { |
| 641 | ims.insert(*makeData(to_string(i))); |
| 642 | } |
| 643 | BOOST_CHECK_EQUAL(ims.size(), 18); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 644 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 645 | ims.setCapacity(16); |
| 646 | BOOST_CHECK_EQUAL(ims.size(), 16); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited) |
| 650 | { |
| 651 | T ims(10000); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 652 | BOOST_CHECK_EQUAL(ims.getLimit(), 10000); |
| 653 | |
| 654 | T ims2(4); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 655 | BOOST_CHECK_EQUAL(ims2.getLimit(), 4); |
| 656 | } |
| 657 | |
| 658 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited) |
| 659 | { |
| 660 | T ims(40); |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 661 | size_t initialCapacity = ims.getCapacity(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 662 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 663 | for (size_t i = 0; i < initialCapacity + 1; i++) { |
| 664 | shared_ptr<Data> data = makeData(to_string(i)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 665 | data->setFreshnessPeriod(5000_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 666 | signData(data); |
| 667 | ims.insert(*data); |
| 668 | } |
| 669 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 670 | BOOST_CHECK_EQUAL(ims.size(), initialCapacity + 1); |
| 671 | BOOST_CHECK_EQUAL(ims.getCapacity(), initialCapacity * 2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | BOOST_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 Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 694 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 697 | // Find function is implemented at the base case, so it's sufficient to test for one derived class. |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 698 | class FindFixture : public tests::UnitTestTimeFixture |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 699 | { |
| 700 | protected: |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 701 | FindFixture() |
| 702 | : m_ims(io) |
| 703 | { |
| 704 | } |
| 705 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 706 | Name |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 707 | insert(uint32_t id, const Name& name, |
| 708 | const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 709 | { |
| 710 | shared_ptr<Data> data = makeData(name); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 711 | data->setFreshnessPeriod(99999_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 712 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
| 713 | signData(data); |
| 714 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 715 | m_ims.insert(*data, freshWindow); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 716 | |
| 717 | return data->getFullName(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | Interest& |
| 721 | startInterest(const Name& name) |
| 722 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 723 | m_interest = makeInterest(name, true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 724 | return *m_interest; |
| 725 | } |
| 726 | |
| 727 | uint32_t |
| 728 | find() |
| 729 | { |
| 730 | shared_ptr<const Data> found = m_ims.find(*m_interest); |
| 731 | if (found == 0) { |
| 732 | return 0; |
| 733 | } |
| 734 | const Block& content = found->getContent(); |
| 735 | if (content.value_size() != sizeof(uint32_t)) { |
| 736 | return 0; |
| 737 | } |
Junxiao Shi | baab732 | 2017-07-27 01:22:49 +0000 | [diff] [blame] | 738 | uint32_t id = 0; |
| 739 | std::memcpy(&id, content.value(), sizeof(id)); |
| 740 | return id; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | protected: |
| 744 | InMemoryStoragePersistent m_ims; |
| 745 | shared_ptr<Interest> m_interest; |
| 746 | }; |
| 747 | |
| 748 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 749 | |
| 750 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 751 | { |
| 752 | insert(1, "ndn:/"); |
| 753 | |
| 754 | startInterest("ndn:/"); |
| 755 | BOOST_CHECK_EQUAL(find(), 1); |
| 756 | } |
| 757 | |
| 758 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 759 | { |
| 760 | insert(1, "ndn:/A"); |
| 761 | |
| 762 | startInterest("ndn:/"); |
| 763 | BOOST_CHECK_EQUAL(find(), 1); |
| 764 | } |
| 765 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 766 | BOOST_AUTO_TEST_CASE(ExactName) |
| 767 | { |
| 768 | insert(1, "ndn:/"); |
| 769 | insert(2, "ndn:/A"); |
| 770 | insert(3, "ndn:/A/B"); |
| 771 | insert(4, "ndn:/A/C"); |
| 772 | insert(5, "ndn:/D"); |
| 773 | |
| 774 | startInterest("ndn:/A"); |
| 775 | BOOST_CHECK_EQUAL(find(), 2); |
| 776 | } |
| 777 | |
| 778 | BOOST_AUTO_TEST_CASE(FullName) |
| 779 | { |
| 780 | Name n1 = insert(1, "ndn:/A"); |
| 781 | Name n2 = insert(2, "ndn:/A"); |
| 782 | |
| 783 | startInterest(n1); |
| 784 | BOOST_CHECK_EQUAL(find(), 1); |
| 785 | |
| 786 | startInterest(n2); |
| 787 | BOOST_CHECK_EQUAL(find(), 2); |
| 788 | } |
| 789 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 790 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 791 | { |
| 792 | insert(1, "ndn:/A"); |
| 793 | insert(2, "ndn:/B/p/1"); |
| 794 | insert(3, "ndn:/B/p/2"); |
| 795 | insert(4, "ndn:/B/q/1"); |
| 796 | insert(5, "ndn:/B/q/2"); |
| 797 | insert(6, "ndn:/C"); |
| 798 | |
| 799 | startInterest("ndn:/B"); |
| 800 | BOOST_CHECK_EQUAL(find(), 2); |
| 801 | } |
| 802 | |
| 803 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 804 | { |
| 805 | insert(1, "ndn:/A"); |
| 806 | insert(2, "ndn:/B/p/1"); |
| 807 | insert(3, "ndn:/B/p/2"); |
| 808 | insert(4, "ndn:/B/q/1"); |
| 809 | insert(5, "ndn:/B/q/2"); |
| 810 | insert(6, "ndn:/C"); |
| 811 | |
| 812 | startInterest("ndn:/B") |
| 813 | .setChildSelector(1); |
| 814 | BOOST_CHECK_EQUAL(find(), 4); |
| 815 | } |
| 816 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 817 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 818 | { |
| 819 | insert(1, "ndn:/"); |
| 820 | insert(2, "ndn:/A"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 821 | insert(3, "ndn:/B/1"); |
| 822 | insert(4, "ndn:/C/1/2"); |
| 823 | insert(5, "ndn:/D/1/2/3"); |
| 824 | insert(6, "ndn:/E/1/2/3/4"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 825 | |
| 826 | startInterest("ndn:/") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 827 | .setMinSuffixComponents(0); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 828 | BOOST_CHECK_EQUAL(find(), 1); |
| 829 | |
| 830 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 831 | .setMinSuffixComponents(1); |
| 832 | BOOST_CHECK_EQUAL(find(), 1); |
| 833 | |
| 834 | startInterest("ndn:/") |
| 835 | .setMinSuffixComponents(2); |
| 836 | BOOST_CHECK_EQUAL(find(), 2); |
| 837 | |
| 838 | startInterest("ndn:/") |
| 839 | .setMinSuffixComponents(3); |
| 840 | BOOST_CHECK_EQUAL(find(), 3); |
| 841 | |
| 842 | startInterest("ndn:/") |
| 843 | .setMinSuffixComponents(4); |
| 844 | BOOST_CHECK_EQUAL(find(), 4); |
| 845 | |
| 846 | startInterest("ndn:/") |
| 847 | .setMinSuffixComponents(5); |
| 848 | BOOST_CHECK_EQUAL(find(), 5); |
| 849 | |
| 850 | startInterest("ndn:/") |
| 851 | .setMinSuffixComponents(6); |
| 852 | BOOST_CHECK_EQUAL(find(), 6); |
| 853 | |
| 854 | startInterest("ndn:/") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 855 | .setMinSuffixComponents(7); |
| 856 | BOOST_CHECK_EQUAL(find(), 0); |
| 857 | } |
| 858 | |
| 859 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 860 | { |
| 861 | insert(1, "ndn:/"); |
| 862 | insert(2, "ndn:/A"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 863 | insert(3, "ndn:/B/2"); |
| 864 | insert(4, "ndn:/C/2/3"); |
| 865 | insert(5, "ndn:/D/2/3/4"); |
| 866 | insert(6, "ndn:/E/2/3/4/5"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 867 | |
| 868 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 869 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 870 | .setMaxSuffixComponents(0); |
| 871 | BOOST_CHECK_EQUAL(find(), 0); |
| 872 | |
| 873 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 874 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 875 | .setMaxSuffixComponents(1); |
| 876 | BOOST_CHECK_EQUAL(find(), 1); |
| 877 | |
| 878 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 879 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 880 | .setMaxSuffixComponents(2); |
| 881 | BOOST_CHECK_EQUAL(find(), 2); |
| 882 | |
| 883 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 884 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 885 | .setMaxSuffixComponents(3); |
| 886 | BOOST_CHECK_EQUAL(find(), 3); |
| 887 | |
| 888 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 889 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 890 | .setMaxSuffixComponents(4); |
| 891 | BOOST_CHECK_EQUAL(find(), 4); |
| 892 | |
| 893 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 894 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 895 | .setMaxSuffixComponents(5); |
| 896 | BOOST_CHECK_EQUAL(find(), 5); |
| 897 | |
| 898 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 899 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 900 | .setMaxSuffixComponents(6); |
| 901 | BOOST_CHECK_EQUAL(find(), 6); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 902 | |
| 903 | startInterest("ndn:/") |
| 904 | .setChildSelector(1) |
| 905 | .setMaxSuffixComponents(7); |
| 906 | BOOST_CHECK_EQUAL(find(), 6); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 910 | { |
| 911 | insert(1, "ndn:/A"); |
| 912 | insert(2, "ndn:/A"); |
| 913 | // We don't know which comes first, but there must be some order |
| 914 | |
| 915 | startInterest("ndn:/A") |
| 916 | .setChildSelector(0); |
| 917 | uint32_t leftmost = find(); |
| 918 | |
| 919 | startInterest("ndn:/A") |
| 920 | .setChildSelector(1); |
| 921 | uint32_t rightmost = find(); |
| 922 | |
| 923 | BOOST_CHECK_NE(leftmost, rightmost); |
| 924 | } |
| 925 | |
| 926 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 927 | { |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 928 | insert(1, "ndn:/A"); |
| 929 | Name n2 = insert(2, "ndn:/A"); |
| 930 | insert(3, "ndn:/A/B"); |
| 931 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 932 | uint8_t digest00[util::Sha256::DIGEST_SIZE]; |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 933 | std::fill_n(digest00, sizeof(digest00), 0x00); |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 934 | uint8_t digestFF[util::Sha256::DIGEST_SIZE]; |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 935 | std::fill_n(digestFF, sizeof(digestFF), 0xFF); |
| 936 | |
| 937 | Exclude excludeDigest; |
| 938 | excludeDigest.excludeRange( |
| 939 | name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)), |
| 940 | name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF))); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 941 | |
| 942 | startInterest("ndn:/A") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 943 | .setChildSelector(0) |
| 944 | .setExclude(excludeDigest); |
| 945 | BOOST_CHECK_EQUAL(find(), 3); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 946 | |
| 947 | startInterest("ndn:/A") |
| 948 | .setChildSelector(1) |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 949 | .setExclude(excludeDigest); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 950 | BOOST_CHECK_EQUAL(find(), 3); |
| 951 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 952 | Exclude excludeGeneric; |
| 953 | excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0)); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 954 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 955 | startInterest("ndn:/A") |
| 956 | .setChildSelector(0) |
| 957 | .setExclude(excludeGeneric); |
| 958 | int found1 = find(); |
| 959 | BOOST_CHECK(found1 == 1 || found1 == 2); |
| 960 | |
| 961 | startInterest("ndn:/A") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 962 | .setChildSelector(1) |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 963 | .setExclude(excludeGeneric); |
| 964 | int found2 = find(); |
| 965 | BOOST_CHECK(found2 == 1 || found2 == 2); |
| 966 | |
| 967 | Exclude exclude2 = excludeGeneric; |
| 968 | exclude2.excludeOne(n2.get(-1)); |
| 969 | |
| 970 | startInterest("ndn:/A") |
| 971 | .setChildSelector(0) |
| 972 | .setExclude(exclude2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 973 | BOOST_CHECK_EQUAL(find(), 1); |
| 974 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 975 | startInterest("ndn:/A") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 976 | .setChildSelector(1) |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 977 | .setExclude(exclude2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 978 | BOOST_CHECK_EQUAL(find(), 1); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 981 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 982 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 983 | Name data1Name = insert(1, "ndn:/A/1", 500_ms); |
| 984 | insert(2, "ndn:/A/2", 2500_ms); |
| 985 | insert(3, "ndn:/A/3", 3500_ms); |
| 986 | insert(4, "ndn:/A/4", 1500_ms); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 987 | |
| 988 | // @0s, all Data are fresh |
| 989 | startInterest("ndn:/A/1") |
| 990 | .setMustBeFresh(true); |
| 991 | BOOST_CHECK_EQUAL(find(), 1); |
| 992 | |
| 993 | startInterest("ndn:/A/1") |
| 994 | .setMustBeFresh(false); |
| 995 | BOOST_CHECK_EQUAL(find(), 1); |
| 996 | |
| 997 | startInterest("ndn:/A") |
| 998 | .setMustBeFresh(true) |
| 999 | .setChildSelector(0); |
| 1000 | BOOST_CHECK_EQUAL(find(), 1); |
| 1001 | |
| 1002 | startInterest("ndn:/A") |
| 1003 | .setMustBeFresh(true) |
| 1004 | .setChildSelector(1); |
| 1005 | BOOST_CHECK_EQUAL(find(), 4); |
| 1006 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 1007 | advanceClocks(1000_ms); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 1008 | // @1s, /A/1 is stale |
| 1009 | startInterest("ndn:/A/1") |
| 1010 | .setMustBeFresh(true); |
| 1011 | BOOST_CHECK_EQUAL(find(), 0); |
| 1012 | startInterest("ndn:/A/1") |
| 1013 | .setMustBeFresh(false); |
| 1014 | BOOST_CHECK_EQUAL(find(), 1); |
| 1015 | |
| 1016 | // MustBeFresh is ignored when full Name is specified |
| 1017 | startInterest(data1Name) |
| 1018 | .setMustBeFresh(true); |
| 1019 | BOOST_CHECK_EQUAL(find(), 1); |
| 1020 | |
| 1021 | startInterest("ndn:/A") |
| 1022 | .setMustBeFresh(true) |
| 1023 | .setChildSelector(0); |
| 1024 | BOOST_CHECK_EQUAL(find(), 2); |
| 1025 | startInterest("ndn:/A") |
| 1026 | .setMustBeFresh(false) |
| 1027 | .setChildSelector(0); |
| 1028 | BOOST_CHECK_EQUAL(find(), 1); |
| 1029 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 1030 | advanceClocks(1000_ms); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 1031 | // @2s, /A/1 and /A/4 are stale |
| 1032 | startInterest("ndn:/A") |
| 1033 | .setMustBeFresh(true) |
| 1034 | .setChildSelector(1); |
| 1035 | BOOST_CHECK_EQUAL(find(), 3); |
| 1036 | startInterest("ndn:/A") |
| 1037 | .setMustBeFresh(false) |
| 1038 | .setChildSelector(1); |
| 1039 | BOOST_CHECK_EQUAL(find(), 4); |
| 1040 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 1041 | advanceClocks(2000_ms); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 1042 | // @4s, all Data are stale |
| 1043 | startInterest("ndn:/A") |
| 1044 | .setMustBeFresh(true) |
| 1045 | .setChildSelector(0); |
| 1046 | BOOST_CHECK_EQUAL(find(), 0); |
| 1047 | startInterest("ndn:/A") |
| 1048 | .setMustBeFresh(true) |
| 1049 | .setChildSelector(1); |
| 1050 | BOOST_CHECK_EQUAL(find(), 0); |
| 1051 | } |
| 1052 | |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 1053 | BOOST_AUTO_TEST_SUITE_END() // Find |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 1054 | BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 1055 | BOOST_AUTO_TEST_SUITE_END() // Ims |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1056 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 1057 | } // namespace tests |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1058 | } // namespace ndn |