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