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