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 | /* |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 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 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 381 | using InMemoryStoragesLimited = boost::mpl::vector<InMemoryStorageFifo, |
| 382 | InMemoryStorageLfu, |
| 383 | InMemoryStorageLru>; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 384 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 385 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 386 | { |
| 387 | T ims; |
| 388 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 389 | ims.setCapacity(18); |
| 390 | for (int i = 1; i < 19; ++i) { |
| 391 | ims.insert(*makeData(to_string(i))); |
| 392 | } |
| 393 | BOOST_CHECK_EQUAL(ims.size(), 18); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 394 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 395 | ims.setCapacity(16); |
| 396 | BOOST_CHECK_EQUAL(ims.size(), 16); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited) |
| 400 | { |
| 401 | T ims(10000); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 402 | BOOST_CHECK_EQUAL(ims.getLimit(), 10000); |
| 403 | |
| 404 | T ims2(4); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 405 | BOOST_CHECK_EQUAL(ims2.getLimit(), 4); |
| 406 | } |
| 407 | |
| 408 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited) |
| 409 | { |
| 410 | T ims(40); |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 411 | size_t initialCapacity = ims.getCapacity(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 412 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 413 | for (size_t i = 0; i < initialCapacity + 1; i++) { |
| 414 | shared_ptr<Data> data = makeData(to_string(i)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 415 | data->setFreshnessPeriod(5000_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 416 | signData(data); |
| 417 | ims.insert(*data); |
| 418 | } |
| 419 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 420 | BOOST_CHECK_EQUAL(ims.size(), initialCapacity + 1); |
| 421 | BOOST_CHECK_EQUAL(ims.getCapacity(), initialCapacity * 2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited) |
| 425 | { |
| 426 | T ims(2); |
| 427 | |
| 428 | Name name("/insert/1"); |
| 429 | shared_ptr<Data> data = makeData(name); |
| 430 | ims.insert(*data); |
| 431 | |
| 432 | Name name2("/insert/2"); |
| 433 | shared_ptr<Data> data2 = makeData(name2); |
| 434 | ims.insert(*data2); |
| 435 | |
| 436 | Name name3("/insert/3"); |
| 437 | shared_ptr<Data> data3 = makeData(name3); |
| 438 | ims.insert(*data3); |
| 439 | |
| 440 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 441 | |
| 442 | shared_ptr<Interest> interest = makeInterest(name); |
| 443 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 444 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 447 | // 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] | 448 | class FindFixture : public tests::UnitTestTimeFixture |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 449 | { |
| 450 | protected: |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 451 | FindFixture() |
| 452 | : m_ims(io) |
| 453 | { |
| 454 | } |
| 455 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 456 | Name |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 457 | insert(uint32_t id, const Name& name, |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 458 | const std::function<void(Data&)>& modifyData = nullptr, |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 459 | const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 460 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 461 | auto data = makeData(name); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 462 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 463 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 464 | if (modifyData != nullptr) { |
| 465 | modifyData(*data); |
| 466 | } |
| 467 | |
| 468 | data->wireEncode(); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 469 | m_ims.insert(*data, freshWindow); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 470 | |
| 471 | return data->getFullName(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | Interest& |
| 475 | startInterest(const Name& name) |
| 476 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 477 | m_interest = makeInterest(name, false); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 478 | return *m_interest; |
| 479 | } |
| 480 | |
| 481 | uint32_t |
| 482 | find() |
| 483 | { |
| 484 | shared_ptr<const Data> found = m_ims.find(*m_interest); |
| 485 | if (found == 0) { |
| 486 | return 0; |
| 487 | } |
| 488 | const Block& content = found->getContent(); |
| 489 | if (content.value_size() != sizeof(uint32_t)) { |
| 490 | return 0; |
| 491 | } |
Junxiao Shi | baab732 | 2017-07-27 01:22:49 +0000 | [diff] [blame] | 492 | uint32_t id = 0; |
| 493 | std::memcpy(&id, content.value(), sizeof(id)); |
| 494 | return id; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | protected: |
| 498 | InMemoryStoragePersistent m_ims; |
| 499 | shared_ptr<Interest> m_interest; |
| 500 | }; |
| 501 | |
| 502 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 503 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 504 | BOOST_AUTO_TEST_CASE(ExactName) |
| 505 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 506 | insert(1, "/"); |
| 507 | insert(2, "/A"); |
| 508 | insert(3, "/A/B"); |
| 509 | insert(4, "/A/C"); |
| 510 | insert(5, "/D"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 511 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 512 | startInterest("/A"); |
| 513 | BOOST_CHECK_EQUAL(find(), 2); |
| 514 | } |
| 515 | |
| 516 | BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix) |
| 517 | { |
| 518 | insert(1, "/"); |
| 519 | insert(2, "/A"); |
| 520 | insert(3, "/A/B"); |
| 521 | insert(4, "/A/C"); |
| 522 | insert(5, "/D"); |
| 523 | |
| 524 | startInterest("/A") |
| 525 | .setCanBePrefix(true); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 526 | BOOST_CHECK_EQUAL(find(), 2); |
| 527 | } |
| 528 | |
| 529 | BOOST_AUTO_TEST_CASE(FullName) |
| 530 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 531 | Name n1 = insert(1, "/A"); |
| 532 | Name n2 = insert(2, "/A"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 533 | |
| 534 | startInterest(n1); |
| 535 | BOOST_CHECK_EQUAL(find(), 1); |
| 536 | |
| 537 | startInterest(n2); |
| 538 | BOOST_CHECK_EQUAL(find(), 2); |
| 539 | } |
| 540 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 541 | BOOST_AUTO_TEST_CASE(FullName_EmptyDataName) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 542 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 543 | Name n1 = insert(1, "/"); |
| 544 | Name n2 = insert(2, "/"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 545 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 546 | startInterest(n1); |
| 547 | BOOST_CHECK_EQUAL(find(), 1); |
| 548 | |
| 549 | startInterest(n2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 550 | BOOST_CHECK_EQUAL(find(), 2); |
| 551 | } |
| 552 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 553 | BOOST_AUTO_TEST_CASE(PrefixName) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 554 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 555 | insert(1, "/A"); |
| 556 | insert(2, "/B/p/1"); |
| 557 | insert(3, "/B/p/2"); |
| 558 | insert(4, "/B/q/1"); |
| 559 | insert(5, "/B/q/2"); |
| 560 | insert(6, "/C"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 561 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 562 | startInterest("/B") |
| 563 | .setCanBePrefix(true); |
| 564 | BOOST_CHECK_EQUAL(find(), 2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 567 | BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 568 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 569 | insert(1, "/B/p/1"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 570 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 571 | startInterest("/B"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 572 | BOOST_CHECK_EQUAL(find(), 0); |
| 573 | } |
| 574 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 575 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 576 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 577 | insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms |
| 578 | insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); }); |
| 579 | insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); }, 1_s); |
| 580 | insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); }, 1_h); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 581 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 582 | // lookup at exact same moment as insertion is not tested because this won't happen in reality |
| 583 | |
| 584 | advanceClocks(500_ms); // @500ms |
| 585 | startInterest("/A") |
| 586 | .setCanBePrefix(true) |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 587 | .setMustBeFresh(true); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 588 | BOOST_CHECK_EQUAL(find(), 3); |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 589 | |
| 590 | advanceClocks(1500_ms); // @2s |
| 591 | startInterest("/A") |
| 592 | .setCanBePrefix(true) |
| 593 | .setMustBeFresh(true); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 594 | BOOST_CHECK_EQUAL(find(), 4); |
| 595 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 596 | advanceClocks(3500_s); // @3502s |
| 597 | startInterest("/A") |
| 598 | .setCanBePrefix(true) |
| 599 | .setMustBeFresh(true); |
| 600 | BOOST_CHECK_EQUAL(find(), 4); |
| 601 | |
| 602 | advanceClocks(3500_s); // @7002s |
| 603 | startInterest("/A") |
| 604 | .setCanBePrefix(true) |
| 605 | .setMustBeFresh(true); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 606 | BOOST_CHECK_EQUAL(find(), 0); |
| 607 | } |
| 608 | |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 609 | BOOST_AUTO_TEST_SUITE_END() // Find |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 610 | BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 611 | BOOST_AUTO_TEST_SUITE_END() // Ims |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 612 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 613 | } // namespace tests |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 614 | } // namespace ndn |