Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/cs.hpp" |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/util/crypto.hpp> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 28 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 31 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 32 | namespace tests { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 34 | BOOST_FIXTURE_TEST_SUITE(TableCs, BaseFixture) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 35 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 36 | class FindFixture : protected BaseFixture |
| 37 | { |
| 38 | protected: |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 39 | Name |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 40 | insert(uint32_t id, const Name& name) |
| 41 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 42 | shared_ptr<Data> data = makeData(name); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 43 | data->setFreshnessPeriod(time::milliseconds(99999)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 44 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 45 | data->wireEncode(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 46 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 47 | m_cs.insert(*data); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 48 | |
| 49 | return data->getFullName(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 50 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 51 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 52 | Interest& |
| 53 | startInterest(const Name& name) |
| 54 | { |
| 55 | m_interest = make_shared<Interest>(name); |
| 56 | return *m_interest; |
| 57 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 58 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 59 | uint32_t |
| 60 | find() |
| 61 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 62 | // m_cs.dump(); |
| 63 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 64 | const Data* found = m_cs.find(*m_interest); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 65 | if (found == nullptr) { |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | const Block& content = found->getContent(); |
| 69 | if (content.value_size() != sizeof(uint32_t)) { |
| 70 | return 0; |
| 71 | } |
| 72 | return *reinterpret_cast<const uint32_t*>(content.value()); |
| 73 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 74 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 75 | protected: |
| 76 | Cs m_cs; |
| 77 | shared_ptr<Interest> m_interest; |
| 78 | }; |
| 79 | |
| 80 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 83 | { |
| 84 | insert(1, "ndn:/"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 85 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 86 | startInterest("ndn:/"); |
| 87 | BOOST_CHECK_EQUAL(find(), 1); |
| 88 | } |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 91 | { |
| 92 | insert(1, "ndn:/A"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 93 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 94 | startInterest("ndn:/"); |
| 95 | BOOST_CHECK_EQUAL(find(), 1); |
| 96 | } |
| 97 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(ExactName) |
| 99 | { |
| 100 | insert(1, "ndn:/"); |
| 101 | insert(2, "ndn:/A"); |
| 102 | insert(3, "ndn:/A/B"); |
| 103 | insert(4, "ndn:/A/C"); |
| 104 | insert(5, "ndn:/D"); |
| 105 | |
| 106 | startInterest("ndn:/A"); |
| 107 | BOOST_CHECK_EQUAL(find(), 2); |
| 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(FullName) |
| 111 | { |
| 112 | Name n1 = insert(1, "ndn:/A"); |
| 113 | Name n2 = insert(2, "ndn:/A"); |
| 114 | |
| 115 | startInterest(n1); |
| 116 | BOOST_CHECK_EQUAL(find(), 1); |
| 117 | |
| 118 | startInterest(n2); |
| 119 | BOOST_CHECK_EQUAL(find(), 2); |
| 120 | } |
| 121 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 122 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 123 | { |
| 124 | insert(1, "ndn:/A"); |
| 125 | insert(2, "ndn:/B/p/1"); |
| 126 | insert(3, "ndn:/B/p/2"); |
| 127 | insert(4, "ndn:/B/q/1"); |
| 128 | insert(5, "ndn:/B/q/2"); |
| 129 | insert(6, "ndn:/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 130 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 131 | startInterest("ndn:/B"); |
| 132 | BOOST_CHECK_EQUAL(find(), 2); |
| 133 | } |
| 134 | |
| 135 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 136 | { |
| 137 | insert(1, "ndn:/A"); |
| 138 | insert(2, "ndn:/B/p/1"); |
| 139 | insert(3, "ndn:/B/p/2"); |
| 140 | insert(4, "ndn:/B/q/1"); |
| 141 | insert(5, "ndn:/B/q/2"); |
| 142 | insert(6, "ndn:/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 143 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 144 | startInterest("ndn:/B") |
| 145 | .setChildSelector(1); |
| 146 | BOOST_CHECK_EQUAL(find(), 4); |
| 147 | } |
| 148 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 149 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 150 | { |
| 151 | insert(1, "ndn:/"); |
| 152 | insert(2, "ndn:/A"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 153 | insert(3, "ndn:/B/1"); |
| 154 | insert(4, "ndn:/C/1/2"); |
| 155 | insert(5, "ndn:/D/1/2/3"); |
| 156 | insert(6, "ndn:/E/1/2/3/4"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 157 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 158 | startInterest("ndn:/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 159 | .setMinSuffixComponents(0); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(find(), 1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 161 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 162 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 163 | .setMinSuffixComponents(1); |
| 164 | BOOST_CHECK_EQUAL(find(), 1); |
| 165 | |
| 166 | startInterest("ndn:/") |
| 167 | .setMinSuffixComponents(2); |
| 168 | BOOST_CHECK_EQUAL(find(), 2); |
| 169 | |
| 170 | startInterest("ndn:/") |
| 171 | .setMinSuffixComponents(3); |
| 172 | BOOST_CHECK_EQUAL(find(), 3); |
| 173 | |
| 174 | startInterest("ndn:/") |
| 175 | .setMinSuffixComponents(4); |
| 176 | BOOST_CHECK_EQUAL(find(), 4); |
| 177 | |
| 178 | startInterest("ndn:/") |
| 179 | .setMinSuffixComponents(5); |
| 180 | BOOST_CHECK_EQUAL(find(), 5); |
| 181 | |
| 182 | startInterest("ndn:/") |
| 183 | .setMinSuffixComponents(6); |
| 184 | BOOST_CHECK_EQUAL(find(), 6); |
| 185 | |
| 186 | startInterest("ndn:/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 187 | .setMinSuffixComponents(7); |
| 188 | BOOST_CHECK_EQUAL(find(), 0); |
| 189 | } |
| 190 | |
| 191 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 192 | { |
| 193 | insert(1, "ndn:/"); |
| 194 | insert(2, "ndn:/A"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 195 | insert(3, "ndn:/B/2"); |
| 196 | insert(4, "ndn:/C/2/3"); |
| 197 | insert(5, "ndn:/D/2/3/4"); |
| 198 | insert(6, "ndn:/E/2/3/4/5"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 199 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 200 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 201 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 202 | .setMaxSuffixComponents(0); |
| 203 | BOOST_CHECK_EQUAL(find(), 0); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 204 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 205 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 206 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 207 | .setMaxSuffixComponents(1); |
| 208 | BOOST_CHECK_EQUAL(find(), 1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 209 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 210 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 211 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 212 | .setMaxSuffixComponents(2); |
| 213 | BOOST_CHECK_EQUAL(find(), 2); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 214 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 215 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 216 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 217 | .setMaxSuffixComponents(3); |
| 218 | BOOST_CHECK_EQUAL(find(), 3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 219 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 220 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 221 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 222 | .setMaxSuffixComponents(4); |
| 223 | BOOST_CHECK_EQUAL(find(), 4); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 224 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 225 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 226 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 227 | .setMaxSuffixComponents(5); |
| 228 | BOOST_CHECK_EQUAL(find(), 5); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 229 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 230 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 231 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 232 | .setMaxSuffixComponents(6); |
| 233 | BOOST_CHECK_EQUAL(find(), 6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 234 | |
| 235 | startInterest("ndn:/") |
| 236 | .setChildSelector(1) |
| 237 | .setMaxSuffixComponents(7); |
| 238 | BOOST_CHECK_EQUAL(find(), 6); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 242 | { |
| 243 | insert(1, "ndn:/A"); |
| 244 | insert(2, "ndn:/A"); |
| 245 | // We don't know which comes first, but there must be some order |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 246 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 247 | startInterest("ndn:/A") |
| 248 | .setChildSelector(0); |
| 249 | uint32_t leftmost = find(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 250 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 251 | startInterest("ndn:/A") |
| 252 | .setChildSelector(1); |
| 253 | uint32_t rightmost = find(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 254 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 255 | BOOST_CHECK_NE(leftmost, rightmost); |
| 256 | } |
| 257 | |
| 258 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 259 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 260 | insert(1, "ndn:/A"); |
| 261 | Name n2 = insert(2, "ndn:/A"); |
| 262 | insert(3, "ndn:/A/B"); |
| 263 | |
| 264 | uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 265 | std::fill_n(digest00, sizeof(digest00), 0x00); |
| 266 | uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 267 | std::fill_n(digestFF, sizeof(digestFF), 0xFF); |
| 268 | |
| 269 | Exclude excludeDigest; |
| 270 | excludeDigest.excludeRange( |
| 271 | name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)), |
| 272 | name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF))); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 273 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 274 | startInterest("ndn:/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 275 | .setChildSelector(0) |
| 276 | .setExclude(excludeDigest); |
| 277 | BOOST_CHECK_EQUAL(find(), 3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 278 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 279 | startInterest("ndn:/A") |
| 280 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 281 | .setExclude(excludeDigest); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(find(), 3); |
| 283 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 284 | Exclude excludeGeneric; |
| 285 | excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0)); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 286 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 287 | startInterest("ndn:/A") |
| 288 | .setChildSelector(0) |
| 289 | .setExclude(excludeGeneric); |
| 290 | int found1 = find(); |
| 291 | BOOST_CHECK(found1 == 1 || found1 == 2); |
| 292 | |
| 293 | startInterest("ndn:/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 294 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 295 | .setExclude(excludeGeneric); |
| 296 | int found2 = find(); |
| 297 | BOOST_CHECK(found2 == 1 || found2 == 2); |
| 298 | |
| 299 | Exclude exclude2 = excludeGeneric; |
| 300 | exclude2.excludeOne(n2.get(-1)); |
| 301 | |
| 302 | startInterest("ndn:/A") |
| 303 | .setChildSelector(0) |
| 304 | .setExclude(exclude2); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 305 | BOOST_CHECK_EQUAL(find(), 1); |
| 306 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 307 | startInterest("ndn:/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 308 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 309 | .setExclude(exclude2); |
| 310 | BOOST_CHECK_EQUAL(find(), 1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 313 | BOOST_AUTO_TEST_SUITE_END() |
| 314 | |
| 315 | BOOST_FIXTURE_TEST_CASE(Evict, UnitTestTimeFixture) |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 316 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 317 | Cs cs(3); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 318 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 319 | shared_ptr<Data> dataA = makeData("ndn:/A"); |
| 320 | dataA->setFreshnessPeriod(time::milliseconds(99999)); |
| 321 | dataA->wireEncode(); |
| 322 | cs.insert(*dataA); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 323 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 324 | shared_ptr<Data> dataB = makeData("ndn:/B"); |
| 325 | dataB->setFreshnessPeriod(time::milliseconds(10)); |
| 326 | dataB->wireEncode(); |
| 327 | cs.insert(*dataB); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 328 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 329 | shared_ptr<Data> dataC = makeData("ndn:/C"); |
| 330 | dataC->setFreshnessPeriod(time::milliseconds(99999)); |
| 331 | dataC->wireEncode(); |
| 332 | cs.insert(*dataC, true); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 333 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 334 | this->advanceClocks(time::milliseconds(11)); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 335 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 336 | // evict unsolicited |
| 337 | shared_ptr<Data> dataD = makeData("ndn:/D"); |
| 338 | dataD->setFreshnessPeriod(time::milliseconds(99999)); |
| 339 | dataD->wireEncode(); |
| 340 | cs.insert(*dataD); |
| 341 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 342 | BOOST_CHECK(cs.find(Interest("ndn:/C")) == nullptr); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 343 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 344 | // evict stale |
| 345 | shared_ptr<Data> dataE = makeData("ndn:/E"); |
| 346 | dataE->setFreshnessPeriod(time::milliseconds(99999)); |
| 347 | dataE->wireEncode(); |
| 348 | cs.insert(*dataE); |
| 349 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 350 | BOOST_CHECK(cs.find(Interest("ndn:/B")) == nullptr); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 351 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 352 | // evict fifo |
| 353 | shared_ptr<Data> dataF = makeData("ndn:/F"); |
| 354 | dataF->setFreshnessPeriod(time::milliseconds(99999)); |
| 355 | dataF->wireEncode(); |
| 356 | cs.insert(*dataF); |
| 357 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 358 | BOOST_CHECK(cs.find(Interest("ndn:/A")) == nullptr); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 359 | } |
| 360 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 361 | BOOST_FIXTURE_TEST_CASE(Refresh, UnitTestTimeFixture) |
| 362 | { |
| 363 | Cs cs(3); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 364 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 365 | shared_ptr<Data> dataA = makeData("ndn:/A"); |
| 366 | dataA->setFreshnessPeriod(time::milliseconds(99999)); |
| 367 | dataA->wireEncode(); |
| 368 | cs.insert(*dataA); |
| 369 | |
| 370 | shared_ptr<Data> dataB = makeData("ndn:/B"); |
| 371 | dataB->setFreshnessPeriod(time::milliseconds(10)); |
| 372 | dataB->wireEncode(); |
| 373 | cs.insert(*dataB); |
| 374 | |
| 375 | shared_ptr<Data> dataC = makeData("ndn:/C"); |
| 376 | dataC->setFreshnessPeriod(time::milliseconds(10)); |
| 377 | dataC->wireEncode(); |
| 378 | cs.insert(*dataC); |
| 379 | |
| 380 | this->advanceClocks(time::milliseconds(11)); |
| 381 | |
| 382 | // refresh dataB |
| 383 | shared_ptr<Data> dataB2 = make_shared<Data>(*dataB); |
| 384 | dataB2->wireEncode(); |
| 385 | cs.insert(*dataB2); |
| 386 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 387 | BOOST_CHECK(cs.find(Interest("ndn:/A")) != nullptr); |
| 388 | BOOST_CHECK(cs.find(Interest("ndn:/B")) != nullptr); |
| 389 | BOOST_CHECK(cs.find(Interest("ndn:/C")) != nullptr); |
| 390 | |
| 391 | // evict dataC stale |
| 392 | shared_ptr<Data> dataD = makeData("ndn:/D"); |
| 393 | dataD->setFreshnessPeriod(time::milliseconds(99999)); |
| 394 | dataD->wireEncode(); |
| 395 | cs.insert(*dataD); |
| 396 | BOOST_CHECK_EQUAL(cs.size(), 3); |
| 397 | BOOST_CHECK(cs.find(Interest("ndn:/C")) == nullptr); |
| 398 | } |
| 399 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 400 | BOOST_AUTO_TEST_CASE(Enumeration) |
| 401 | { |
| 402 | Cs cs; |
| 403 | |
| 404 | Name nameA("/A"); |
| 405 | Name nameAB("/A/B"); |
| 406 | Name nameABC("/A/B/C"); |
| 407 | Name nameD("/D"); |
| 408 | |
| 409 | BOOST_CHECK_EQUAL(cs.size(), 0); |
| 410 | BOOST_CHECK(cs.begin() == cs.end()); |
| 411 | |
| 412 | cs.insert(*makeData(nameABC)); |
| 413 | BOOST_CHECK_EQUAL(cs.size(), 1); |
| 414 | BOOST_CHECK(cs.begin() != cs.end()); |
| 415 | BOOST_CHECK(cs.begin()->getName() == nameABC); |
| 416 | BOOST_CHECK((*cs.begin()).getName() == nameABC); |
| 417 | |
| 418 | auto i = cs.begin(); |
| 419 | auto j = cs.begin(); |
| 420 | BOOST_CHECK(++i == cs.end()); |
| 421 | BOOST_CHECK(j++ == cs.begin()); |
| 422 | BOOST_CHECK(j == cs.end()); |
| 423 | |
| 424 | cs.insert(*makeData(nameA)); |
| 425 | cs.insert(*makeData(nameAB)); |
| 426 | cs.insert(*makeData(nameD)); |
| 427 | |
| 428 | std::set<Name> expected = {nameA, nameAB, nameABC, nameD}; |
| 429 | std::set<Name> actual; |
| 430 | for (const auto& csEntry : cs) { |
| 431 | actual.insert(csEntry.getName()); |
| 432 | } |
| 433 | BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end()); |
| 434 | } |
| 435 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 436 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 437 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 438 | } // namespace tests |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 439 | } // namespace nfd |