Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame^] | 5 | * |
| 6 | * Author: Ilya Moiseenko <iliamo@ucla.edu> |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "table/cs.hpp" |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame^] | 10 | #include "table/cs-entry.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 11 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 12 | #include "tests/test-common.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 13 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame^] | 14 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 15 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 16 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 17 | namespace tests { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 18 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame^] | 19 | class CsAccessor : public Cs |
| 20 | { |
| 21 | public: |
| 22 | |
| 23 | bool |
| 24 | evictItem_accessor() |
| 25 | { |
| 26 | return evictItem(); |
| 27 | } |
| 28 | }; |
| 29 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 30 | BOOST_FIXTURE_TEST_SUITE(TableCs, BaseFixture) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 31 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame^] | 32 | BOOST_AUTO_TEST_CASE(Insertion) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 33 | { |
| 34 | Cs cs; |
| 35 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame^] | 36 | shared_ptr<Data> data = make_shared<Data>("/insertion"); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 37 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame^] | 38 | ndn::SignatureSha256WithRsa fakeSignature; |
| 39 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 40 | data->setSignature(fakeSignature); |
| 41 | |
| 42 | BOOST_CHECK_EQUAL(cs.insert(*data), true); |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame^] | 45 | BOOST_AUTO_TEST_CASE(Insertion2) |
| 46 | { |
| 47 | Cs cs; |
| 48 | |
| 49 | ndn::SignatureSha256WithRsa fakeSignature; |
| 50 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 51 | |
| 52 | shared_ptr<Data> data = make_shared<Data>("/a"); |
| 53 | data->setSignature(fakeSignature); |
| 54 | cs.insert(*data); |
| 55 | |
| 56 | shared_ptr<Data> data2 = make_shared<Data>("/b"); |
| 57 | data2->setSignature(fakeSignature); |
| 58 | cs.insert(*data2); |
| 59 | |
| 60 | shared_ptr<Data> data3 = make_shared<Data>("/c"); |
| 61 | data3->setSignature(fakeSignature); |
| 62 | cs.insert(*data3); |
| 63 | |
| 64 | shared_ptr<Data> data4 = make_shared<Data>("/d"); |
| 65 | data4->setSignature(fakeSignature); |
| 66 | cs.insert(*data4); |
| 67 | |
| 68 | BOOST_CHECK_EQUAL(cs.size(), 4); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | BOOST_AUTO_TEST_CASE(DuplicateInsertion) |
| 73 | { |
| 74 | Cs cs; |
| 75 | |
| 76 | ndn::SignatureSha256WithRsa fakeSignature; |
| 77 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 78 | |
| 79 | shared_ptr<Data> data0 = make_shared<Data>("/insert/smth"); |
| 80 | data0->setSignature(fakeSignature); |
| 81 | BOOST_CHECK_EQUAL(cs.insert(*data0), true); |
| 82 | |
| 83 | shared_ptr<Data> data = make_shared<Data>("/insert/duplicate"); |
| 84 | data->setSignature(fakeSignature); |
| 85 | BOOST_CHECK_EQUAL(cs.insert(*data), true); |
| 86 | |
| 87 | cs.insert(*data); |
| 88 | BOOST_CHECK_EQUAL(cs.size(), 2); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(DuplicateInsertion2) |
| 93 | { |
| 94 | Cs cs; |
| 95 | |
| 96 | ndn::SignatureSha256WithRsa fakeSignature; |
| 97 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 98 | |
| 99 | shared_ptr<Data> data = make_shared<Data>("/insert/duplicate"); |
| 100 | data->setSignature(fakeSignature); |
| 101 | BOOST_CHECK_EQUAL(cs.insert(*data), true); |
| 102 | |
| 103 | cs.insert(*data); |
| 104 | BOOST_CHECK_EQUAL(cs.size(), 1); |
| 105 | |
| 106 | shared_ptr<Data> data2 = make_shared<Data>("/insert/original"); |
| 107 | data2->setSignature(fakeSignature); |
| 108 | BOOST_CHECK_EQUAL(cs.insert(*data2), true); |
| 109 | BOOST_CHECK_EQUAL(cs.size(), 2); |
| 110 | } |
| 111 | |
| 112 | BOOST_AUTO_TEST_CASE(InsertAndFind) |
| 113 | { |
| 114 | Cs cs; |
| 115 | |
| 116 | ndn::SignatureSha256WithRsa fakeSignature; |
| 117 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 118 | |
| 119 | Name name("/insert/and/find"); |
| 120 | |
| 121 | shared_ptr<Data> data = make_shared<Data>(name); |
| 122 | data->setSignature(fakeSignature); |
| 123 | BOOST_CHECK_EQUAL(cs.insert(*data), true); |
| 124 | |
| 125 | shared_ptr<Interest> interest = make_shared<Interest>(name); |
| 126 | |
| 127 | const Data* found = cs.find(*interest); |
| 128 | |
| 129 | BOOST_REQUIRE(found != 0); |
| 130 | BOOST_CHECK_EQUAL(data->getName(), found->getName()); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(InsertAndNotFind) |
| 134 | { |
| 135 | Cs cs; |
| 136 | |
| 137 | ndn::SignatureSha256WithRsa fakeSignature; |
| 138 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 139 | |
| 140 | Name name("/insert/and/find"); |
| 141 | shared_ptr<Data> data = make_shared<Data>(name); |
| 142 | data->setSignature(fakeSignature); |
| 143 | BOOST_CHECK_EQUAL(cs.insert(*data), true); |
| 144 | |
| 145 | Name name2("/not/find"); |
| 146 | shared_ptr<Interest> interest = make_shared<Interest>(name2); |
| 147 | |
| 148 | const Data* found = cs.find(*interest); |
| 149 | |
| 150 | BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0)); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | BOOST_AUTO_TEST_CASE(InsertAndErase) |
| 155 | { |
| 156 | CsAccessor cs; |
| 157 | |
| 158 | ndn::SignatureSha256WithRsa fakeSignature; |
| 159 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 160 | |
| 161 | shared_ptr<Data> data = make_shared<Data>("/insertandelete"); |
| 162 | data->setSignature(fakeSignature); |
| 163 | cs.insert(*data); |
| 164 | BOOST_CHECK_EQUAL(cs.size(), 1); |
| 165 | |
| 166 | cs.evictItem_accessor(); |
| 167 | BOOST_CHECK_EQUAL(cs.size(), 0); |
| 168 | } |
| 169 | |
| 170 | BOOST_AUTO_TEST_CASE(StalenessQueue) |
| 171 | { |
| 172 | CsAccessor cs; |
| 173 | |
| 174 | ndn::SignatureSha256WithRsa fakeSignature; |
| 175 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 176 | |
| 177 | Name name2("/insert/fresh"); |
| 178 | shared_ptr<Data> data2 = make_shared<Data>(name2); |
| 179 | data2->setFreshnessPeriod(5000); |
| 180 | data2->setSignature(fakeSignature); |
| 181 | cs.insert(*data2); |
| 182 | |
| 183 | Name name("/insert/expire"); |
| 184 | shared_ptr<Data> data = make_shared<Data>(name); |
| 185 | data->setFreshnessPeriod(500); |
| 186 | data->setSignature(fakeSignature); |
| 187 | cs.insert(*data); |
| 188 | |
| 189 | BOOST_CHECK_EQUAL(cs.size(), 2); |
| 190 | |
| 191 | sleep(1); |
| 192 | |
| 193 | cs.evictItem_accessor(); |
| 194 | BOOST_CHECK_EQUAL(cs.size(), 1); |
| 195 | |
| 196 | shared_ptr<Interest> interest = make_shared<Interest>(name2); |
| 197 | const Data* found = cs.find(*interest); |
| 198 | BOOST_REQUIRE(found != 0); |
| 199 | BOOST_CHECK_EQUAL(found->getName(), name2); |
| 200 | } |
| 201 | |
| 202 | //even max size |
| 203 | BOOST_AUTO_TEST_CASE(ReplacementEvenSize) |
| 204 | { |
| 205 | Cs cs(4); |
| 206 | |
| 207 | ndn::SignatureSha256WithRsa fakeSignature; |
| 208 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 209 | |
| 210 | shared_ptr<Data> data = make_shared<Data>("/a"); |
| 211 | data->setSignature(fakeSignature); |
| 212 | cs.insert(*data); |
| 213 | |
| 214 | shared_ptr<Data> data2 = make_shared<Data>("/b"); |
| 215 | data2->setSignature(fakeSignature); |
| 216 | cs.insert(*data2); |
| 217 | |
| 218 | shared_ptr<Data> data3 = make_shared<Data>("/c"); |
| 219 | data3->setSignature(fakeSignature); |
| 220 | cs.insert(*data3); |
| 221 | |
| 222 | shared_ptr<Data> data4 = make_shared<Data>("/d"); |
| 223 | data4->setSignature(fakeSignature); |
| 224 | cs.insert(*data4); |
| 225 | |
| 226 | shared_ptr<Data> data5 = make_shared<Data>("/e"); |
| 227 | data5->setSignature(fakeSignature); |
| 228 | cs.insert(*data5); |
| 229 | |
| 230 | BOOST_CHECK_EQUAL(cs.size(), 4); |
| 231 | } |
| 232 | |
| 233 | //odd max size |
| 234 | BOOST_AUTO_TEST_CASE(Replacement2) |
| 235 | { |
| 236 | Cs cs(3); |
| 237 | |
| 238 | ndn::SignatureSha256WithRsa fakeSignature; |
| 239 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 240 | |
| 241 | shared_ptr<Data> data = make_shared<Data>("/a"); |
| 242 | data->setSignature(fakeSignature); |
| 243 | cs.insert(*data); |
| 244 | |
| 245 | shared_ptr<Data> data2 = make_shared<Data>("/b"); |
| 246 | data2->setSignature(fakeSignature); |
| 247 | cs.insert(*data2); |
| 248 | |
| 249 | shared_ptr<Data> data3 = make_shared<Data>("/c"); |
| 250 | data3->setSignature(fakeSignature); |
| 251 | cs.insert(*data3); |
| 252 | |
| 253 | shared_ptr<Data> data4 = make_shared<Data>("/d"); |
| 254 | data4->setSignature(fakeSignature); |
| 255 | cs.insert(*data4); |
| 256 | |
| 257 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 258 | } |
| 259 | |
| 260 | BOOST_AUTO_TEST_CASE(InsertAndEraseByName) |
| 261 | { |
| 262 | CsAccessor cs; |
| 263 | |
| 264 | ndn::SignatureSha256WithRsa fakeSignature; |
| 265 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 266 | |
| 267 | Name name("/insertandremovebyname"); |
| 268 | shared_ptr<Data> data = make_shared<Data>(name); |
| 269 | data->setSignature(fakeSignature); |
| 270 | cs.insert(*data); |
| 271 | |
| 272 | uint8_t digest1[32]; |
| 273 | ndn::ndn_digestSha256(data->wireEncode().wire(), data->wireEncode().size(), digest1); |
| 274 | |
| 275 | shared_ptr<Data> data2 = make_shared<Data>("/a"); |
| 276 | data2->setSignature(fakeSignature); |
| 277 | cs.insert(*data2); |
| 278 | |
| 279 | shared_ptr<Data> data3 = make_shared<Data>("/z"); |
| 280 | data3->setSignature(fakeSignature); |
| 281 | cs.insert(*data3); |
| 282 | |
| 283 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 284 | |
| 285 | name.append(digest1, 32); |
| 286 | cs.erase(name); |
| 287 | BOOST_CHECK_EQUAL(cs.size(), 2); |
| 288 | } |
| 289 | |
| 290 | BOOST_AUTO_TEST_CASE(DigestCalculation) |
| 291 | { |
| 292 | ndn::SignatureSha256WithRsa fakeSignature; |
| 293 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 294 | |
| 295 | shared_ptr<Data> data = make_shared<Data>("/digest/compute"); |
| 296 | data->setSignature(fakeSignature); |
| 297 | |
| 298 | uint8_t digest1[32]; |
| 299 | ndn::ndn_digestSha256(data->wireEncode().wire(), data->wireEncode().size(), digest1); |
| 300 | |
| 301 | cs::Entry* entry = new cs::Entry(*data, false); |
| 302 | |
| 303 | BOOST_CHECK_EQUAL_COLLECTIONS(digest1, digest1+32, |
| 304 | entry->getDigest()->buf(), entry->getDigest()->buf()+32); |
| 305 | } |
| 306 | |
| 307 | BOOST_AUTO_TEST_CASE(InsertCanonical) |
| 308 | { |
| 309 | Cs cs; |
| 310 | |
| 311 | ndn::SignatureSha256WithRsa fakeSignature; |
| 312 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 313 | |
| 314 | shared_ptr<Data> data = make_shared<Data>("/a"); |
| 315 | data->setSignature(fakeSignature); |
| 316 | cs.insert(*data); |
| 317 | |
| 318 | shared_ptr<Data> data2 = make_shared<Data>("/b"); |
| 319 | data2->setSignature(fakeSignature); |
| 320 | cs.insert(*data2); |
| 321 | |
| 322 | shared_ptr<Data> data3 = make_shared<Data>("/c"); |
| 323 | data3->setSignature(fakeSignature); |
| 324 | cs.insert(*data3); |
| 325 | |
| 326 | shared_ptr<Data> data4 = make_shared<Data>("/d"); |
| 327 | data4->setSignature(fakeSignature); |
| 328 | cs.insert(*data4); |
| 329 | |
| 330 | shared_ptr<Data> data5 = make_shared<Data>("/c/c/1/2/3/4/5/6"); |
| 331 | data5->setSignature(fakeSignature); |
| 332 | cs.insert(*data5); |
| 333 | |
| 334 | shared_ptr<Data> data6 = make_shared<Data>("/c/c/1/2/3"); |
| 335 | data6->setSignature(fakeSignature); |
| 336 | cs.insert(*data6); |
| 337 | |
| 338 | shared_ptr<Data> data7 = make_shared<Data>("/c/c/1"); |
| 339 | data7->setSignature(fakeSignature); |
| 340 | cs.insert(*data7); |
| 341 | } |
| 342 | |
| 343 | BOOST_AUTO_TEST_CASE(EraseCanonical) |
| 344 | { |
| 345 | Cs cs; |
| 346 | |
| 347 | ndn::SignatureSha256WithRsa fakeSignature; |
| 348 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 349 | |
| 350 | shared_ptr<Data> data = make_shared<Data>("/a"); |
| 351 | data->setSignature(fakeSignature); |
| 352 | cs.insert(*data); |
| 353 | |
| 354 | shared_ptr<Data> data2 = make_shared<Data>("/b"); |
| 355 | data2->setSignature(fakeSignature); |
| 356 | cs.insert(*data2); |
| 357 | |
| 358 | shared_ptr<Data> data3 = make_shared<Data>("/c"); |
| 359 | data3->setSignature(fakeSignature); |
| 360 | cs.insert(*data3); |
| 361 | |
| 362 | shared_ptr<Data> data4 = make_shared<Data>("/d"); |
| 363 | data4->setSignature(fakeSignature); |
| 364 | cs.insert(*data4); |
| 365 | |
| 366 | uint8_t digest1[32]; |
| 367 | ndn::ndn_digestSha256(data->wireEncode().wire(), data->wireEncode().size(), digest1); |
| 368 | |
| 369 | shared_ptr<Data> data5 = make_shared<Data>("/c/c/1/2/3/4/5/6"); |
| 370 | data5->setSignature(fakeSignature); |
| 371 | cs.insert(*data5); |
| 372 | |
| 373 | shared_ptr<Data> data6 = make_shared<Data>("/c/c/1/2/3"); |
| 374 | data6->setSignature(fakeSignature); |
| 375 | cs.insert(*data6); |
| 376 | |
| 377 | shared_ptr<Data> data7 = make_shared<Data>("/c/c/1"); |
| 378 | data7->setSignature(fakeSignature); |
| 379 | cs.insert(*data7); |
| 380 | |
| 381 | Name name("/a"); |
| 382 | name.append(digest1,32); |
| 383 | cs.erase(name); |
| 384 | BOOST_CHECK_EQUAL(cs.size(), 6); |
| 385 | } |
| 386 | |
| 387 | BOOST_AUTO_TEST_CASE(ImplicitDigestSelector) |
| 388 | { |
| 389 | CsAccessor cs; |
| 390 | |
| 391 | ndn::SignatureSha256WithRsa fakeSignature; |
| 392 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 393 | |
| 394 | Name name("/digest/works"); |
| 395 | shared_ptr<Data> data = make_shared<Data>(name); |
| 396 | data->setSignature(fakeSignature); |
| 397 | cs.insert(*data); |
| 398 | |
| 399 | shared_ptr<Data> data2 = make_shared<Data>("/a"); |
| 400 | data2->setSignature(fakeSignature); |
| 401 | cs.insert(*data2); |
| 402 | |
| 403 | shared_ptr<Data> data3 = make_shared<Data>("/z/z/z"); |
| 404 | data3->setSignature(fakeSignature); |
| 405 | cs.insert(*data3); |
| 406 | |
| 407 | uint8_t digest1[32]; |
| 408 | ndn::ndn_digestSha256(data->wireEncode().wire(), data->wireEncode().size(), digest1); |
| 409 | |
| 410 | uint8_t digest2[32] = {0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1}; |
| 411 | |
| 412 | shared_ptr<Interest> interest = make_shared<Interest>(boost::cref(Name(name).append(digest1, 32))); |
| 413 | interest->setMinSuffixComponents(0); |
| 414 | interest->setMaxSuffixComponents(0); |
| 415 | |
| 416 | const Data* found = cs.find(*interest); |
| 417 | BOOST_CHECK_NE(found, static_cast<const Data*>(0)); |
| 418 | BOOST_CHECK_EQUAL(found->getName(), name); |
| 419 | |
| 420 | shared_ptr<Interest> interest2 = make_shared<Interest>(boost::cref(Name(name).append(digest2, 32))); |
| 421 | interest2->setMinSuffixComponents(0); |
| 422 | interest2->setMaxSuffixComponents(0); |
| 423 | |
| 424 | const Data* notfound = cs.find(*interest2); |
| 425 | BOOST_CHECK_EQUAL(notfound, static_cast<const Data*>(0)); |
| 426 | } |
| 427 | |
| 428 | BOOST_AUTO_TEST_CASE(ChildSelector) |
| 429 | { |
| 430 | Cs cs; |
| 431 | |
| 432 | ndn::SignatureSha256WithRsa fakeSignature; |
| 433 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 434 | |
| 435 | shared_ptr<Data> data = make_shared<Data>("/a"); |
| 436 | data->setSignature(fakeSignature); |
| 437 | cs.insert(*data); |
| 438 | |
| 439 | shared_ptr<Data> data2 = make_shared<Data>("/b"); |
| 440 | data2->setSignature(fakeSignature); |
| 441 | cs.insert(*data2); |
| 442 | |
| 443 | shared_ptr<Data> data4 = make_shared<Data>("/d"); |
| 444 | data4->setSignature(fakeSignature); |
| 445 | cs.insert(*data4); |
| 446 | |
| 447 | shared_ptr<Data> data5 = make_shared<Data>("/c/c"); |
| 448 | data5->setSignature(fakeSignature); |
| 449 | cs.insert(*data5); |
| 450 | |
| 451 | shared_ptr<Data> data6 = make_shared<Data>("/c/f"); |
| 452 | data6->setSignature(fakeSignature); |
| 453 | cs.insert(*data6); |
| 454 | |
| 455 | shared_ptr<Data> data7 = make_shared<Data>("/c/n"); |
| 456 | data7->setSignature(fakeSignature); |
| 457 | cs.insert(*data7); |
| 458 | |
| 459 | shared_ptr<Interest> interest = make_shared<Interest>("/c"); |
| 460 | interest->setChildSelector(1); |
| 461 | |
| 462 | const Data* found = cs.find(*interest); |
| 463 | BOOST_CHECK_EQUAL(found->getName(), "/c/n"); |
| 464 | |
| 465 | shared_ptr<Interest> interest2 = make_shared<Interest>("/c"); |
| 466 | interest2->setChildSelector(0); |
| 467 | |
| 468 | const Data* found2 = cs.find(*interest2); |
| 469 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c"); |
| 470 | } |
| 471 | |
| 472 | BOOST_AUTO_TEST_CASE(ChildSelector2) |
| 473 | { |
| 474 | Cs cs; |
| 475 | |
| 476 | ndn::SignatureSha256WithRsa fakeSignature; |
| 477 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 478 | |
| 479 | shared_ptr<Data> data = make_shared<Data>("/a/b/1"); |
| 480 | data->setSignature(fakeSignature); |
| 481 | cs.insert(*data); |
| 482 | |
| 483 | shared_ptr<Data> data2 = make_shared<Data>("/a/b/2"); |
| 484 | data2->setSignature(fakeSignature); |
| 485 | cs.insert(*data2); |
| 486 | |
| 487 | shared_ptr<Data> data3 = make_shared<Data>("/a/z/1"); |
| 488 | data3->setSignature(fakeSignature); |
| 489 | cs.insert(*data3); |
| 490 | |
| 491 | shared_ptr<Data> data4 = make_shared<Data>("/a/z/2"); |
| 492 | data4->setSignature(fakeSignature); |
| 493 | cs.insert(*data4); |
| 494 | |
| 495 | shared_ptr<Interest> interest = make_shared<Interest>("/a"); |
| 496 | interest->setChildSelector(1); |
| 497 | |
| 498 | const Data* found = cs.find(*interest); |
| 499 | BOOST_CHECK_EQUAL(found->getName(), "/a/z/1"); |
| 500 | } |
| 501 | |
| 502 | BOOST_AUTO_TEST_CASE(MustBeFreshSelector) |
| 503 | { |
| 504 | Cs cs; |
| 505 | |
| 506 | ndn::SignatureSha256WithRsa fakeSignature; |
| 507 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 508 | |
| 509 | Name name("/insert/nonfresh"); |
| 510 | shared_ptr<Data> data = make_shared<Data>(name); |
| 511 | data->setFreshnessPeriod(500); |
| 512 | data->setSignature(fakeSignature); |
| 513 | cs.insert(*data); |
| 514 | |
| 515 | sleep(1); |
| 516 | |
| 517 | shared_ptr<Interest> interest = make_shared<Interest>(name); |
| 518 | interest->setMustBeFresh(true); |
| 519 | |
| 520 | const Data* found = cs.find(*interest); |
| 521 | BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0)); |
| 522 | } |
| 523 | |
| 524 | BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector) |
| 525 | { |
| 526 | Cs cs; |
| 527 | |
| 528 | ndn::SignatureSha256WithRsa fakeSignature; |
| 529 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 530 | |
| 531 | shared_ptr<Data> data = make_shared<Data>("/a"); |
| 532 | data->setSignature(fakeSignature); |
| 533 | cs.insert(*data); |
| 534 | |
| 535 | shared_ptr<Data> data2 = make_shared<Data>("/b"); |
| 536 | data2->setSignature(fakeSignature); |
| 537 | cs.insert(*data2); |
| 538 | |
| 539 | shared_ptr<Data> data4 = make_shared<Data>("/d"); |
| 540 | data4->setSignature(fakeSignature); |
| 541 | cs.insert(*data4); |
| 542 | |
| 543 | shared_ptr<Data> data5 = make_shared<Data>("/c/c/1/2/3/4/5/6"); |
| 544 | data5->setSignature(fakeSignature); |
| 545 | cs.insert(*data5); |
| 546 | |
| 547 | shared_ptr<Data> data6 = make_shared<Data>("/c/c/6/7/8/9"); |
| 548 | data6->setSignature(fakeSignature); |
| 549 | cs.insert(*data6); |
| 550 | |
| 551 | shared_ptr<Data> data7 = make_shared<Data>("/c/c/1/2/3"); |
| 552 | data7->setSignature(fakeSignature); |
| 553 | cs.insert(*data7); |
| 554 | |
| 555 | shared_ptr<Data> data8 = make_shared<Data>("/c/c/1"); |
| 556 | data8->setSignature(fakeSignature); |
| 557 | cs.insert(*data8); |
| 558 | |
| 559 | shared_ptr<Interest> interest = make_shared<Interest>("/c/c"); |
| 560 | interest->setMinSuffixComponents(3); |
| 561 | interest->setChildSelector(0); |
| 562 | |
| 563 | const Data* found = cs.find(*interest); |
| 564 | BOOST_CHECK_EQUAL(found->getName(), "/c/c/1/2/3/4/5/6"); |
| 565 | |
| 566 | shared_ptr<Interest> interest2 = make_shared<Interest>("/c/c"); |
| 567 | interest2->setMinSuffixComponents(4); |
| 568 | interest2->setChildSelector(1); |
| 569 | |
| 570 | const Data* found2 = cs.find(*interest2); |
| 571 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c/6/7/8/9"); |
| 572 | |
| 573 | shared_ptr<Interest> interest3 = make_shared<Interest>("/c/c"); |
| 574 | interest3->setMaxSuffixComponents(2); |
| 575 | interest3->setChildSelector(1); |
| 576 | |
| 577 | const Data* found3 = cs.find(*interest3); |
| 578 | BOOST_CHECK_EQUAL(found3->getName(), "/c/c/1"); |
| 579 | } |
| 580 | |
| 581 | BOOST_AUTO_TEST_CASE(ExcludeSelector) |
| 582 | { |
| 583 | Cs cs; |
| 584 | |
| 585 | ndn::SignatureSha256WithRsa fakeSignature; |
| 586 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 587 | |
| 588 | shared_ptr<Data> data = make_shared<Data>("/a"); |
| 589 | data->setSignature(fakeSignature); |
| 590 | cs.insert(*data); |
| 591 | |
| 592 | shared_ptr<Data> data2 = make_shared<Data>("/b"); |
| 593 | data2->setSignature(fakeSignature); |
| 594 | cs.insert(*data2); |
| 595 | |
| 596 | shared_ptr<Data> data3 = make_shared<Data>("/c/a"); |
| 597 | data3->setSignature(fakeSignature); |
| 598 | cs.insert(*data3); |
| 599 | |
| 600 | shared_ptr<Data> data4 = make_shared<Data>("/d"); |
| 601 | data4->setSignature(fakeSignature); |
| 602 | cs.insert(*data4); |
| 603 | |
| 604 | shared_ptr<Data> data5 = make_shared<Data>("/c/c"); |
| 605 | data5->setSignature(fakeSignature); |
| 606 | cs.insert(*data5); |
| 607 | |
| 608 | shared_ptr<Data> data6 = make_shared<Data>("/c/f"); |
| 609 | data6->setSignature(fakeSignature); |
| 610 | cs.insert(*data6); |
| 611 | |
| 612 | shared_ptr<Data> data7 = make_shared<Data>("/c/n"); |
| 613 | data7->setSignature(fakeSignature); |
| 614 | cs.insert(*data7); |
| 615 | |
| 616 | shared_ptr<Interest> interest = make_shared<Interest>("/c"); |
| 617 | interest->setChildSelector(1); |
| 618 | Exclude e; |
| 619 | e.excludeOne (Name::Component("n")); |
| 620 | interest->setExclude(e); |
| 621 | |
| 622 | const Data* found = cs.find(*interest); |
| 623 | BOOST_CHECK_EQUAL(found->getName(), "/c/f"); |
| 624 | |
| 625 | shared_ptr<Interest> interest2 = make_shared<Interest>("/c"); |
| 626 | interest2->setChildSelector(0); |
| 627 | |
| 628 | Exclude e2; |
| 629 | e2.excludeOne (Name::Component("a")); |
| 630 | interest2->setExclude(e2); |
| 631 | |
| 632 | const Data* found2 = cs.find(*interest2); |
| 633 | BOOST_CHECK_EQUAL(found2->getName(), "/c/c"); |
| 634 | |
| 635 | shared_ptr<Interest> interest3 = make_shared<Interest>("/c"); |
| 636 | interest3->setChildSelector(0); |
| 637 | |
| 638 | Exclude e3; |
| 639 | e3.excludeOne (Name::Component("c")); |
| 640 | interest3->setExclude(e3); |
| 641 | |
| 642 | const Data* found3 = cs.find(*interest3); |
| 643 | BOOST_CHECK_EQUAL(found3->getName(), "/c/a"); |
| 644 | } |
| 645 | |
| 646 | // |
| 647 | |
| 648 | class FindFixture : protected BaseFixture |
| 649 | { |
| 650 | protected: |
| 651 | void |
| 652 | insert(uint32_t id, const Name& name) |
| 653 | { |
| 654 | shared_ptr<Data> data = make_shared<Data>(name); |
| 655 | data->setFreshnessPeriod(99999); |
| 656 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
| 657 | |
| 658 | ndn::SignatureSha256WithRsa fakeSignature; |
| 659 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 660 | data->setSignature(fakeSignature); |
| 661 | |
| 662 | m_cs.insert(*data); |
| 663 | } |
| 664 | |
| 665 | Interest& |
| 666 | startInterest(const Name& name) |
| 667 | { |
| 668 | m_interest = make_shared<Interest>(name); |
| 669 | return *m_interest; |
| 670 | } |
| 671 | |
| 672 | uint32_t |
| 673 | find() |
| 674 | { |
| 675 | const Data* found = m_cs.find(*m_interest); |
| 676 | if (found == 0) { |
| 677 | return 0; |
| 678 | } |
| 679 | const Block& content = found->getContent(); |
| 680 | if (content.value_size() != sizeof(uint32_t)) { |
| 681 | return 0; |
| 682 | } |
| 683 | return *reinterpret_cast<const uint32_t*>(content.value()); |
| 684 | } |
| 685 | |
| 686 | protected: |
| 687 | Cs m_cs; |
| 688 | shared_ptr<Interest> m_interest; |
| 689 | }; |
| 690 | |
| 691 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 692 | |
| 693 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 694 | { |
| 695 | insert(1, "ndn:/"); |
| 696 | |
| 697 | startInterest("ndn:/"); |
| 698 | BOOST_CHECK_EQUAL(find(), 1); |
| 699 | } |
| 700 | |
| 701 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 702 | { |
| 703 | insert(1, "ndn:/A"); |
| 704 | |
| 705 | startInterest("ndn:/"); |
| 706 | BOOST_CHECK_EQUAL(find(), 1); |
| 707 | } |
| 708 | |
| 709 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 710 | { |
| 711 | insert(1, "ndn:/A"); |
| 712 | insert(2, "ndn:/B/p/1"); |
| 713 | insert(3, "ndn:/B/p/2"); |
| 714 | insert(4, "ndn:/B/q/1"); |
| 715 | insert(5, "ndn:/B/q/2"); |
| 716 | insert(6, "ndn:/C"); |
| 717 | |
| 718 | startInterest("ndn:/B"); |
| 719 | BOOST_CHECK_EQUAL(find(), 2); |
| 720 | } |
| 721 | |
| 722 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 723 | { |
| 724 | insert(1, "ndn:/A"); |
| 725 | insert(2, "ndn:/B/p/1"); |
| 726 | insert(3, "ndn:/B/p/2"); |
| 727 | insert(4, "ndn:/B/q/1"); |
| 728 | insert(5, "ndn:/B/q/2"); |
| 729 | insert(6, "ndn:/C"); |
| 730 | |
| 731 | startInterest("ndn:/B") |
| 732 | .setChildSelector(1); |
| 733 | BOOST_CHECK_EQUAL(find(), 4); |
| 734 | } |
| 735 | |
| 736 | BOOST_AUTO_TEST_CASE(Leftmost_ExactName1) |
| 737 | { |
| 738 | insert(1, "ndn:/"); |
| 739 | insert(2, "ndn:/A/B"); |
| 740 | insert(3, "ndn:/A/C"); |
| 741 | insert(4, "ndn:/A"); |
| 742 | insert(5, "ndn:/D"); |
| 743 | |
| 744 | // Intuitively you would think Data 4 should be between Data 1 and 2, |
| 745 | // but Data 4 has full Name ndn:/A/<32-octet hash>. |
| 746 | startInterest("ndn:/A"); |
| 747 | BOOST_CHECK_EQUAL(find(), 2); |
| 748 | } |
| 749 | |
| 750 | BOOST_AUTO_TEST_CASE(Leftmost_ExactName33) |
| 751 | { |
| 752 | insert(1, "ndn:/"); |
| 753 | insert(2, "ndn:/A"); |
| 754 | insert(3, "ndn:/A/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); // 33 'B's |
| 755 | insert(4, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's |
| 756 | insert(5, "ndn:/D"); |
| 757 | |
| 758 | // Data 2 is returned, because <32-octet hash> is less than Data 3. |
| 759 | startInterest("ndn:/A"); |
| 760 | BOOST_CHECK_EQUAL(find(), 2); |
| 761 | } |
| 762 | |
| 763 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
| 764 | { |
| 765 | insert(1, "ndn:/A/1/2/3/4"); |
| 766 | insert(2, "ndn:/B/1/2/3"); |
| 767 | insert(3, "ndn:/C/1/2"); |
| 768 | insert(4, "ndn:/D/1"); |
| 769 | insert(5, "ndn:/E"); |
| 770 | insert(6, "ndn:/"); |
| 771 | |
| 772 | startInterest("ndn:/") |
| 773 | .setChildSelector(1) |
| 774 | .setMinSuffixComponents(0); |
| 775 | BOOST_CHECK_EQUAL(find(), 6); |
| 776 | |
| 777 | startInterest("ndn:/") |
| 778 | .setChildSelector(1) |
| 779 | .setMinSuffixComponents(1); |
| 780 | BOOST_CHECK_EQUAL(find(), 6); |
| 781 | |
| 782 | startInterest("ndn:/") |
| 783 | .setChildSelector(1) |
| 784 | .setMinSuffixComponents(2); |
| 785 | BOOST_CHECK_EQUAL(find(), 5); |
| 786 | |
| 787 | startInterest("ndn:/") |
| 788 | .setChildSelector(1) |
| 789 | .setMinSuffixComponents(3); |
| 790 | BOOST_CHECK_EQUAL(find(), 4); |
| 791 | |
| 792 | startInterest("ndn:/") |
| 793 | .setChildSelector(1) |
| 794 | .setMinSuffixComponents(4); |
| 795 | BOOST_CHECK_EQUAL(find(), 3); |
| 796 | |
| 797 | startInterest("ndn:/") |
| 798 | .setChildSelector(1) |
| 799 | .setMinSuffixComponents(5); |
| 800 | BOOST_CHECK_EQUAL(find(), 2); |
| 801 | |
| 802 | startInterest("ndn:/") |
| 803 | .setChildSelector(1) |
| 804 | .setMinSuffixComponents(6); |
| 805 | BOOST_CHECK_EQUAL(find(), 1); |
| 806 | |
| 807 | startInterest("ndn:/") |
| 808 | .setChildSelector(1) |
| 809 | .setMinSuffixComponents(7); |
| 810 | BOOST_CHECK_EQUAL(find(), 0); |
| 811 | } |
| 812 | |
| 813 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 814 | { |
| 815 | insert(1, "ndn:/"); |
| 816 | insert(2, "ndn:/A"); |
| 817 | insert(3, "ndn:/A/B"); |
| 818 | insert(4, "ndn:/A/B/C"); |
| 819 | insert(5, "ndn:/A/B/C/D"); |
| 820 | insert(6, "ndn:/A/B/C/D/E"); |
| 821 | // Order is 6,5,4,3,2,1, because <32-octet hash> is greater than a 1-octet component. |
| 822 | |
| 823 | startInterest("ndn:/") |
| 824 | .setMaxSuffixComponents(0); |
| 825 | BOOST_CHECK_EQUAL(find(), 0); |
| 826 | |
| 827 | startInterest("ndn:/") |
| 828 | .setMaxSuffixComponents(1); |
| 829 | BOOST_CHECK_EQUAL(find(), 1); |
| 830 | |
| 831 | startInterest("ndn:/") |
| 832 | .setMaxSuffixComponents(2); |
| 833 | BOOST_CHECK_EQUAL(find(), 2); |
| 834 | |
| 835 | startInterest("ndn:/") |
| 836 | .setMaxSuffixComponents(3); |
| 837 | BOOST_CHECK_EQUAL(find(), 3); |
| 838 | |
| 839 | startInterest("ndn:/") |
| 840 | .setMaxSuffixComponents(4); |
| 841 | BOOST_CHECK_EQUAL(find(), 4); |
| 842 | |
| 843 | startInterest("ndn:/") |
| 844 | .setMaxSuffixComponents(5); |
| 845 | BOOST_CHECK_EQUAL(find(), 5); |
| 846 | |
| 847 | startInterest("ndn:/") |
| 848 | .setMaxSuffixComponents(6); |
| 849 | BOOST_CHECK_EQUAL(find(), 6); |
| 850 | } |
| 851 | |
| 852 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 853 | { |
| 854 | insert(1, "ndn:/A"); |
| 855 | insert(2, "ndn:/A"); |
| 856 | // We don't know which comes first, but there must be some order |
| 857 | |
| 858 | startInterest("ndn:/A") |
| 859 | .setChildSelector(0); |
| 860 | uint32_t leftmost = find(); |
| 861 | |
| 862 | startInterest("ndn:/A") |
| 863 | .setChildSelector(1); |
| 864 | uint32_t rightmost = find(); |
| 865 | |
| 866 | BOOST_CHECK_NE(leftmost, rightmost); |
| 867 | } |
| 868 | |
| 869 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 870 | { |
| 871 | insert(1, "ndn:/A/B"); |
| 872 | insert(2, "ndn:/A"); |
| 873 | insert(3, "ndn:/A/CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"); // 33 'C's |
| 874 | |
| 875 | startInterest("ndn:/A") |
| 876 | .setExclude(Exclude().excludeBefore(ndn::name::Component(reinterpret_cast<const uint8_t*>( |
| 877 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" |
| 878 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 31))); // 31 0xFF's |
| 879 | BOOST_CHECK_EQUAL(find(), 2); |
| 880 | |
| 881 | startInterest("ndn:/A") |
| 882 | .setChildSelector(1) |
| 883 | .setExclude(Exclude().excludeAfter(ndn::name::Component(reinterpret_cast<const uint8_t*>( |
| 884 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 885 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 886 | "\x00"), 33))); // 33 0x00's |
| 887 | BOOST_CHECK_EQUAL(find(), 2); |
| 888 | } |
| 889 | |
| 890 | BOOST_AUTO_TEST_SUITE_END() |
| 891 | |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 892 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 893 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 894 | } // namespace tests |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 895 | } // namespace nfd |