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 | /* |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 22 | #include "ims/in-memory-storage.hpp" |
| 23 | #include "ims/in-memory-storage-fifo.hpp" |
| 24 | #include "ims/in-memory-storage-lfu.hpp" |
| 25 | #include "ims/in-memory-storage-lru.hpp" |
| 26 | #include "ims/in-memory-storage-persistent.hpp" |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 27 | #include "security/signature-sha256-with-rsa.hpp" |
Junxiao Shi | 6938e34 | 2017-07-25 21:56:58 +0000 | [diff] [blame] | 28 | #include "util/sha256.hpp" |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 29 | |
| 30 | #include "boost-test.hpp" |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 31 | #include "make-interest-data.hpp" |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 32 | #include "../unit-test-time-fixture.hpp" |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 33 | |
| 34 | #include <boost/mpl/list.hpp> |
| 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 | 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 | |
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; |
| 310 | for (;it != ims.end(); it++) { |
| 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(""); |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 398 | interest->setName(Name(name).appendImplicitSha256Digest(digest1->buf(), 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 | |
| 439 | shared_ptr<Interest> interest = makeInterest("/c"); |
| 440 | interest->setChildSelector(1); |
| 441 | |
| 442 | shared_ptr<const Data> found = ims.find(*interest); |
| 443 | BOOST_CHECK_EQUAL(found->getName(), "/c/n"); |
| 444 | |
| 445 | shared_ptr<Interest> interest2 = makeInterest("/c"); |
| 446 | interest2->setChildSelector(0); |
| 447 | |
| 448 | shared_ptr<const Data> found2 = ims.find(*interest2); |
| 449 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c"); |
| 450 | } |
| 451 | |
| 452 | BOOST_AUTO_TEST_CASE_TEMPLATE(ChildSelector2, T, InMemoryStorages) |
| 453 | { |
| 454 | T ims; |
| 455 | |
| 456 | shared_ptr<Data> data = makeData("/a/b/1"); |
| 457 | ims.insert(*data); |
| 458 | |
| 459 | shared_ptr<Data> data2 = makeData("/a/b/2"); |
| 460 | ims.insert(*data2); |
| 461 | |
| 462 | shared_ptr<Data> data3 = makeData("/a/z/1"); |
| 463 | ims.insert(*data3); |
| 464 | |
| 465 | shared_ptr<Data> data4 = makeData("/a/z/2"); |
| 466 | ims.insert(*data4); |
| 467 | |
| 468 | shared_ptr<Interest> interest = makeInterest("/a"); |
| 469 | interest->setChildSelector(1); |
| 470 | |
| 471 | shared_ptr<const Data> found = ims.find(*interest); |
| 472 | BOOST_CHECK_EQUAL(found->getName(), "/a/z/1"); |
| 473 | } |
| 474 | |
| 475 | BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector, T, InMemoryStorages) |
| 476 | { |
| 477 | T ims; |
| 478 | |
| 479 | Name name("/insert/withkey"); |
| 480 | shared_ptr<Data> data = makeData(name); |
| 481 | ims.insert(*data); |
| 482 | |
| 483 | shared_ptr<Interest> interest = makeInterest(name); |
| 484 | Name keyName("/somewhere/key"); |
| 485 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 486 | KeyLocator locator(keyName); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 487 | interest->setPublisherPublicKeyLocator(locator); |
| 488 | |
| 489 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 490 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | BOOST_AUTO_TEST_CASE_TEMPLATE(PublisherKeySelector2, T, InMemoryStorages) |
| 494 | { |
| 495 | T ims; |
| 496 | Name name("/insert/withkey"); |
| 497 | shared_ptr<Data> data = makeData(name); |
| 498 | ims.insert(*data); |
| 499 | |
| 500 | Name name2("/insert/withkey2"); |
| 501 | shared_ptr<Data> data2 = make_shared<Data>(name2); |
| 502 | |
| 503 | Name keyName("/somewhere/key"); |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 504 | const KeyLocator locator(keyName); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 505 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 506 | SignatureSha256WithRsa fakeSignature; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 507 | fakeSignature.setValue(makeEmptyBlock(tlv::SignatureValue)); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 508 | fakeSignature.setKeyLocator(locator); |
| 509 | data2->setSignature(fakeSignature); |
| 510 | data2->wireEncode(); |
| 511 | |
| 512 | ims.insert(*data2); |
| 513 | |
| 514 | shared_ptr<Interest> interest = makeInterest(name2); |
| 515 | interest->setPublisherPublicKeyLocator(locator); |
| 516 | |
| 517 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 518 | BOOST_CHECK(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 519 | BOOST_CHECK_EQUAL(found->getName(), data2->getName()); |
| 520 | } |
| 521 | |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 522 | BOOST_AUTO_TEST_CASE_TEMPLATE(MinMaxComponentsSelector, T, InMemoryStorages) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 523 | { |
| 524 | T ims; |
| 525 | |
| 526 | shared_ptr<Data> data = makeData("/a"); |
| 527 | ims.insert(*data); |
| 528 | |
| 529 | shared_ptr<Data> data2 = makeData("/b"); |
| 530 | ims.insert(*data2); |
| 531 | |
| 532 | shared_ptr<Data> data4 = makeData("/d"); |
| 533 | ims.insert(*data4); |
| 534 | |
| 535 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 536 | ims.insert(*data5); |
| 537 | |
| 538 | shared_ptr<Data> data6 = makeData("/c/c/6/7/8/9"); |
| 539 | ims.insert(*data6); |
| 540 | |
| 541 | shared_ptr<Data> data7 = makeData("/c/c/1/2/3"); |
| 542 | ims.insert(*data7); |
| 543 | |
| 544 | shared_ptr<Data> data8 = makeData("/c/c/1"); |
| 545 | ims.insert(*data8); |
| 546 | |
| 547 | shared_ptr<Interest> interest = makeInterest("/c/c"); |
| 548 | interest->setMinSuffixComponents(3); |
| 549 | interest->setChildSelector(0); |
| 550 | |
| 551 | shared_ptr<const Data> found = ims.find(*interest); |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 552 | BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 553 | |
| 554 | shared_ptr<Interest> interest2 = makeInterest("/c/c"); |
| 555 | interest2->setMinSuffixComponents(4); |
| 556 | interest2->setChildSelector(1); |
| 557 | |
| 558 | shared_ptr<const Data> found2 = ims.find(*interest2); |
| 559 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9"); |
| 560 | |
| 561 | shared_ptr<Interest> interest3 = makeInterest("/c/c"); |
| 562 | interest3->setMaxSuffixComponents(2); |
| 563 | interest3->setChildSelector(1); |
| 564 | |
| 565 | shared_ptr<const Data> found3 = ims.find(*interest3); |
| 566 | BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1"); |
| 567 | } |
| 568 | |
| 569 | BOOST_AUTO_TEST_CASE_TEMPLATE(ExcludeSelector, T, InMemoryStorages) |
| 570 | { |
| 571 | T ims; |
| 572 | |
| 573 | shared_ptr<Data> data = makeData("/a"); |
| 574 | ims.insert(*data); |
| 575 | |
| 576 | shared_ptr<Data> data2 = makeData("/b"); |
| 577 | ims.insert(*data2); |
| 578 | |
| 579 | shared_ptr<Data> data3 = makeData("/c/a"); |
| 580 | ims.insert(*data3); |
| 581 | |
| 582 | shared_ptr<Data> data4 = makeData("/d"); |
| 583 | ims.insert(*data4); |
| 584 | |
| 585 | shared_ptr<Data> data5 = makeData("/c/c"); |
| 586 | ims.insert(*data5); |
| 587 | |
| 588 | shared_ptr<Data> data6 = makeData("/c/f"); |
| 589 | ims.insert(*data6); |
| 590 | |
| 591 | shared_ptr<Data> data7 = makeData("/c/n"); |
| 592 | ims.insert(*data7); |
| 593 | |
| 594 | shared_ptr<Interest> interest = makeInterest("/c"); |
| 595 | interest->setChildSelector(1); |
| 596 | Exclude e; |
| 597 | e.excludeOne (Name::Component("n")); |
| 598 | interest->setExclude(e); |
| 599 | |
| 600 | shared_ptr<const Data> found = ims.find(*interest); |
| 601 | BOOST_CHECK_EQUAL(found->getName(), "/c/f"); |
| 602 | |
| 603 | shared_ptr<Interest> interest2 = makeInterest("/c"); |
| 604 | interest2->setChildSelector(0); |
| 605 | |
| 606 | Exclude e2; |
| 607 | e2.excludeOne (Name::Component("a")); |
| 608 | interest2->setExclude(e2); |
| 609 | |
| 610 | shared_ptr<const Data> found2 = ims.find(*interest2); |
| 611 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c"); |
| 612 | |
| 613 | shared_ptr<Interest> interest3 = makeInterest("/c"); |
| 614 | interest3->setChildSelector(0); |
| 615 | |
| 616 | Exclude e3; |
| 617 | e3.excludeOne (Name::Component("c")); |
| 618 | interest3->setExclude(e3); |
| 619 | |
| 620 | shared_ptr<const Data> found3 = ims.find(*interest3); |
| 621 | BOOST_CHECK_EQUAL(found3->getName(), "/c/a"); |
| 622 | } |
| 623 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 624 | using InMemoryStoragesLimited = boost::mpl::list<InMemoryStorageFifo, |
| 625 | InMemoryStorageLfu, |
| 626 | InMemoryStorageLru>; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 627 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 628 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 629 | { |
| 630 | T ims; |
| 631 | |
| 632 | ims.setCapacity(3); |
| 633 | ims.insert(*makeData("/1")); |
| 634 | ims.insert(*makeData("/2")); |
| 635 | ims.insert(*makeData("/3")); |
| 636 | BOOST_CHECK_EQUAL(ims.size(), 3); |
| 637 | |
| 638 | ims.setCapacity(2); |
| 639 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 640 | } |
| 641 | |
| 642 | BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited) |
| 643 | { |
| 644 | T ims(10000); |
| 645 | |
| 646 | BOOST_CHECK_EQUAL(ims.getLimit(), 10000); |
| 647 | |
| 648 | T ims2(4); |
| 649 | |
| 650 | BOOST_CHECK_EQUAL(ims2.getLimit(), 4); |
| 651 | } |
| 652 | |
| 653 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited) |
| 654 | { |
| 655 | T ims(40); |
| 656 | |
| 657 | for (int i = 0; i < 11; i++) { |
| 658 | std::ostringstream convert; |
| 659 | convert << i; |
| 660 | Name name("/" + convert.str()); |
| 661 | shared_ptr<Data> data = makeData(name); |
| 662 | data->setFreshnessPeriod(time::milliseconds(5000)); |
| 663 | signData(data); |
| 664 | ims.insert(*data); |
| 665 | } |
| 666 | |
| 667 | BOOST_CHECK_EQUAL(ims.size(), 11); |
| 668 | |
| 669 | BOOST_CHECK_EQUAL(ims.getCapacity(), 20); |
| 670 | } |
| 671 | |
| 672 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited) |
| 673 | { |
| 674 | T ims(2); |
| 675 | |
| 676 | Name name("/insert/1"); |
| 677 | shared_ptr<Data> data = makeData(name); |
| 678 | ims.insert(*data); |
| 679 | |
| 680 | Name name2("/insert/2"); |
| 681 | shared_ptr<Data> data2 = makeData(name2); |
| 682 | ims.insert(*data2); |
| 683 | |
| 684 | Name name3("/insert/3"); |
| 685 | shared_ptr<Data> data3 = makeData(name3); |
| 686 | ims.insert(*data3); |
| 687 | |
| 688 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 689 | |
| 690 | shared_ptr<Interest> interest = makeInterest(name); |
| 691 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 692 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 695 | // 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] | 696 | class FindFixture : public tests::UnitTestTimeFixture |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 697 | { |
| 698 | protected: |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 699 | FindFixture() |
| 700 | : m_ims(io) |
| 701 | { |
| 702 | } |
| 703 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 704 | Name |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 705 | insert(uint32_t id, const Name& name, |
| 706 | const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 707 | { |
| 708 | shared_ptr<Data> data = makeData(name); |
| 709 | data->setFreshnessPeriod(time::milliseconds(99999)); |
| 710 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
| 711 | signData(data); |
| 712 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 713 | m_ims.insert(*data, freshWindow); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 714 | |
| 715 | return data->getFullName(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | Interest& |
| 719 | startInterest(const Name& name) |
| 720 | { |
| 721 | m_interest = makeInterest(name); |
| 722 | return *m_interest; |
| 723 | } |
| 724 | |
| 725 | uint32_t |
| 726 | find() |
| 727 | { |
| 728 | shared_ptr<const Data> found = m_ims.find(*m_interest); |
| 729 | if (found == 0) { |
| 730 | return 0; |
| 731 | } |
| 732 | const Block& content = found->getContent(); |
| 733 | if (content.value_size() != sizeof(uint32_t)) { |
| 734 | return 0; |
| 735 | } |
Junxiao Shi | baab732 | 2017-07-27 01:22:49 +0000 | [diff] [blame] | 736 | uint32_t id = 0; |
| 737 | std::memcpy(&id, content.value(), sizeof(id)); |
| 738 | return id; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | protected: |
| 742 | InMemoryStoragePersistent m_ims; |
| 743 | shared_ptr<Interest> m_interest; |
| 744 | }; |
| 745 | |
| 746 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 747 | |
| 748 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 749 | { |
| 750 | insert(1, "ndn:/"); |
| 751 | |
| 752 | startInterest("ndn:/"); |
| 753 | BOOST_CHECK_EQUAL(find(), 1); |
| 754 | } |
| 755 | |
| 756 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 757 | { |
| 758 | insert(1, "ndn:/A"); |
| 759 | |
| 760 | startInterest("ndn:/"); |
| 761 | BOOST_CHECK_EQUAL(find(), 1); |
| 762 | } |
| 763 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 764 | BOOST_AUTO_TEST_CASE(ExactName) |
| 765 | { |
| 766 | insert(1, "ndn:/"); |
| 767 | insert(2, "ndn:/A"); |
| 768 | insert(3, "ndn:/A/B"); |
| 769 | insert(4, "ndn:/A/C"); |
| 770 | insert(5, "ndn:/D"); |
| 771 | |
| 772 | startInterest("ndn:/A"); |
| 773 | BOOST_CHECK_EQUAL(find(), 2); |
| 774 | } |
| 775 | |
| 776 | BOOST_AUTO_TEST_CASE(FullName) |
| 777 | { |
| 778 | Name n1 = insert(1, "ndn:/A"); |
| 779 | Name n2 = insert(2, "ndn:/A"); |
| 780 | |
| 781 | startInterest(n1); |
| 782 | BOOST_CHECK_EQUAL(find(), 1); |
| 783 | |
| 784 | startInterest(n2); |
| 785 | BOOST_CHECK_EQUAL(find(), 2); |
| 786 | } |
| 787 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 788 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 789 | { |
| 790 | insert(1, "ndn:/A"); |
| 791 | insert(2, "ndn:/B/p/1"); |
| 792 | insert(3, "ndn:/B/p/2"); |
| 793 | insert(4, "ndn:/B/q/1"); |
| 794 | insert(5, "ndn:/B/q/2"); |
| 795 | insert(6, "ndn:/C"); |
| 796 | |
| 797 | startInterest("ndn:/B"); |
| 798 | BOOST_CHECK_EQUAL(find(), 2); |
| 799 | } |
| 800 | |
| 801 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 802 | { |
| 803 | insert(1, "ndn:/A"); |
| 804 | insert(2, "ndn:/B/p/1"); |
| 805 | insert(3, "ndn:/B/p/2"); |
| 806 | insert(4, "ndn:/B/q/1"); |
| 807 | insert(5, "ndn:/B/q/2"); |
| 808 | insert(6, "ndn:/C"); |
| 809 | |
| 810 | startInterest("ndn:/B") |
| 811 | .setChildSelector(1); |
| 812 | BOOST_CHECK_EQUAL(find(), 4); |
| 813 | } |
| 814 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 815 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 816 | { |
| 817 | insert(1, "ndn:/"); |
| 818 | insert(2, "ndn:/A"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 819 | insert(3, "ndn:/B/1"); |
| 820 | insert(4, "ndn:/C/1/2"); |
| 821 | insert(5, "ndn:/D/1/2/3"); |
| 822 | insert(6, "ndn:/E/1/2/3/4"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 823 | |
| 824 | startInterest("ndn:/") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 825 | .setMinSuffixComponents(0); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 826 | BOOST_CHECK_EQUAL(find(), 1); |
| 827 | |
| 828 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 829 | .setMinSuffixComponents(1); |
| 830 | BOOST_CHECK_EQUAL(find(), 1); |
| 831 | |
| 832 | startInterest("ndn:/") |
| 833 | .setMinSuffixComponents(2); |
| 834 | BOOST_CHECK_EQUAL(find(), 2); |
| 835 | |
| 836 | startInterest("ndn:/") |
| 837 | .setMinSuffixComponents(3); |
| 838 | BOOST_CHECK_EQUAL(find(), 3); |
| 839 | |
| 840 | startInterest("ndn:/") |
| 841 | .setMinSuffixComponents(4); |
| 842 | BOOST_CHECK_EQUAL(find(), 4); |
| 843 | |
| 844 | startInterest("ndn:/") |
| 845 | .setMinSuffixComponents(5); |
| 846 | BOOST_CHECK_EQUAL(find(), 5); |
| 847 | |
| 848 | startInterest("ndn:/") |
| 849 | .setMinSuffixComponents(6); |
| 850 | BOOST_CHECK_EQUAL(find(), 6); |
| 851 | |
| 852 | startInterest("ndn:/") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 853 | .setMinSuffixComponents(7); |
| 854 | BOOST_CHECK_EQUAL(find(), 0); |
| 855 | } |
| 856 | |
| 857 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 858 | { |
| 859 | insert(1, "ndn:/"); |
| 860 | insert(2, "ndn:/A"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 861 | insert(3, "ndn:/B/2"); |
| 862 | insert(4, "ndn:/C/2/3"); |
| 863 | insert(5, "ndn:/D/2/3/4"); |
| 864 | insert(6, "ndn:/E/2/3/4/5"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 865 | |
| 866 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 867 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 868 | .setMaxSuffixComponents(0); |
| 869 | BOOST_CHECK_EQUAL(find(), 0); |
| 870 | |
| 871 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 872 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 873 | .setMaxSuffixComponents(1); |
| 874 | BOOST_CHECK_EQUAL(find(), 1); |
| 875 | |
| 876 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 877 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 878 | .setMaxSuffixComponents(2); |
| 879 | BOOST_CHECK_EQUAL(find(), 2); |
| 880 | |
| 881 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 882 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 883 | .setMaxSuffixComponents(3); |
| 884 | BOOST_CHECK_EQUAL(find(), 3); |
| 885 | |
| 886 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 887 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 888 | .setMaxSuffixComponents(4); |
| 889 | BOOST_CHECK_EQUAL(find(), 4); |
| 890 | |
| 891 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 892 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 893 | .setMaxSuffixComponents(5); |
| 894 | BOOST_CHECK_EQUAL(find(), 5); |
| 895 | |
| 896 | startInterest("ndn:/") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 897 | .setChildSelector(1) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 898 | .setMaxSuffixComponents(6); |
| 899 | BOOST_CHECK_EQUAL(find(), 6); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 900 | |
| 901 | startInterest("ndn:/") |
| 902 | .setChildSelector(1) |
| 903 | .setMaxSuffixComponents(7); |
| 904 | BOOST_CHECK_EQUAL(find(), 6); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 908 | { |
| 909 | insert(1, "ndn:/A"); |
| 910 | insert(2, "ndn:/A"); |
| 911 | // We don't know which comes first, but there must be some order |
| 912 | |
| 913 | startInterest("ndn:/A") |
| 914 | .setChildSelector(0); |
| 915 | uint32_t leftmost = find(); |
| 916 | |
| 917 | startInterest("ndn:/A") |
| 918 | .setChildSelector(1); |
| 919 | uint32_t rightmost = find(); |
| 920 | |
| 921 | BOOST_CHECK_NE(leftmost, rightmost); |
| 922 | } |
| 923 | |
| 924 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 925 | { |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 926 | insert(1, "ndn:/A"); |
| 927 | Name n2 = insert(2, "ndn:/A"); |
| 928 | insert(3, "ndn:/A/B"); |
| 929 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 930 | uint8_t digest00[util::Sha256::DIGEST_SIZE]; |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 931 | std::fill_n(digest00, sizeof(digest00), 0x00); |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 932 | uint8_t digestFF[util::Sha256::DIGEST_SIZE]; |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 933 | std::fill_n(digestFF, sizeof(digestFF), 0xFF); |
| 934 | |
| 935 | Exclude excludeDigest; |
| 936 | excludeDigest.excludeRange( |
| 937 | name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)), |
| 938 | name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF))); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 939 | |
| 940 | startInterest("ndn:/A") |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 941 | .setChildSelector(0) |
| 942 | .setExclude(excludeDigest); |
| 943 | BOOST_CHECK_EQUAL(find(), 3); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 944 | |
| 945 | startInterest("ndn:/A") |
| 946 | .setChildSelector(1) |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 947 | .setExclude(excludeDigest); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 948 | BOOST_CHECK_EQUAL(find(), 3); |
| 949 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 950 | Exclude excludeGeneric; |
| 951 | excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0)); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 952 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 953 | startInterest("ndn:/A") |
| 954 | .setChildSelector(0) |
| 955 | .setExclude(excludeGeneric); |
| 956 | int found1 = find(); |
| 957 | BOOST_CHECK(found1 == 1 || found1 == 2); |
| 958 | |
| 959 | startInterest("ndn:/A") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 960 | .setChildSelector(1) |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 961 | .setExclude(excludeGeneric); |
| 962 | int found2 = find(); |
| 963 | BOOST_CHECK(found2 == 1 || found2 == 2); |
| 964 | |
| 965 | Exclude exclude2 = excludeGeneric; |
| 966 | exclude2.excludeOne(n2.get(-1)); |
| 967 | |
| 968 | startInterest("ndn:/A") |
| 969 | .setChildSelector(0) |
| 970 | .setExclude(exclude2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 971 | BOOST_CHECK_EQUAL(find(), 1); |
| 972 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 973 | startInterest("ndn:/A") |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 974 | .setChildSelector(1) |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 975 | .setExclude(exclude2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 976 | BOOST_CHECK_EQUAL(find(), 1); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 979 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 980 | { |
| 981 | Name data1Name = insert(1, "ndn:/A/1", time::milliseconds(500)); |
| 982 | insert(2, "ndn:/A/2", time::milliseconds(2500)); |
| 983 | insert(3, "ndn:/A/3", time::milliseconds(3500)); |
| 984 | insert(4, "ndn:/A/4", time::milliseconds(1500)); |
| 985 | |
| 986 | // @0s, all Data are fresh |
| 987 | startInterest("ndn:/A/1") |
| 988 | .setMustBeFresh(true); |
| 989 | BOOST_CHECK_EQUAL(find(), 1); |
| 990 | |
| 991 | startInterest("ndn:/A/1") |
| 992 | .setMustBeFresh(false); |
| 993 | BOOST_CHECK_EQUAL(find(), 1); |
| 994 | |
| 995 | startInterest("ndn:/A") |
| 996 | .setMustBeFresh(true) |
| 997 | .setChildSelector(0); |
| 998 | BOOST_CHECK_EQUAL(find(), 1); |
| 999 | |
| 1000 | startInterest("ndn:/A") |
| 1001 | .setMustBeFresh(true) |
| 1002 | .setChildSelector(1); |
| 1003 | BOOST_CHECK_EQUAL(find(), 4); |
| 1004 | |
| 1005 | advanceClocks(time::milliseconds(1000)); |
| 1006 | // @1s, /A/1 is stale |
| 1007 | startInterest("ndn:/A/1") |
| 1008 | .setMustBeFresh(true); |
| 1009 | BOOST_CHECK_EQUAL(find(), 0); |
| 1010 | startInterest("ndn:/A/1") |
| 1011 | .setMustBeFresh(false); |
| 1012 | BOOST_CHECK_EQUAL(find(), 1); |
| 1013 | |
| 1014 | // MustBeFresh is ignored when full Name is specified |
| 1015 | startInterest(data1Name) |
| 1016 | .setMustBeFresh(true); |
| 1017 | BOOST_CHECK_EQUAL(find(), 1); |
| 1018 | |
| 1019 | startInterest("ndn:/A") |
| 1020 | .setMustBeFresh(true) |
| 1021 | .setChildSelector(0); |
| 1022 | BOOST_CHECK_EQUAL(find(), 2); |
| 1023 | startInterest("ndn:/A") |
| 1024 | .setMustBeFresh(false) |
| 1025 | .setChildSelector(0); |
| 1026 | BOOST_CHECK_EQUAL(find(), 1); |
| 1027 | |
| 1028 | advanceClocks(time::milliseconds(1000)); |
| 1029 | // @2s, /A/1 and /A/4 are stale |
| 1030 | startInterest("ndn:/A") |
| 1031 | .setMustBeFresh(true) |
| 1032 | .setChildSelector(1); |
| 1033 | BOOST_CHECK_EQUAL(find(), 3); |
| 1034 | startInterest("ndn:/A") |
| 1035 | .setMustBeFresh(false) |
| 1036 | .setChildSelector(1); |
| 1037 | BOOST_CHECK_EQUAL(find(), 4); |
| 1038 | |
| 1039 | advanceClocks(time::milliseconds(2000)); |
| 1040 | // @4s, all Data are stale |
| 1041 | startInterest("ndn:/A") |
| 1042 | .setMustBeFresh(true) |
| 1043 | .setChildSelector(0); |
| 1044 | BOOST_CHECK_EQUAL(find(), 0); |
| 1045 | startInterest("ndn:/A") |
| 1046 | .setMustBeFresh(true) |
| 1047 | .setChildSelector(1); |
| 1048 | BOOST_CHECK_EQUAL(find(), 0); |
| 1049 | } |
| 1050 | |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 1051 | BOOST_AUTO_TEST_SUITE_END() // Find |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 1052 | BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 1053 | BOOST_AUTO_TEST_SUITE_END() // Ims |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1054 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 1055 | } // namespace tests |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1056 | } // namespace ndn |