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