Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | bb6146e | 2017-07-26 23:07:52 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 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" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 27 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 29 | |
Junxiao Shi | 4e1b6ee | 2017-08-03 02:20:50 +0000 | [diff] [blame] | 30 | #include <cstring> |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 31 | |
| 32 | #include <ndn-cxx/exclude.hpp> |
Junxiao Shi | 4e1b6ee | 2017-08-03 02:20:50 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/lp/tags.hpp> |
| 34 | #include <ndn-cxx/util/sha256.hpp> |
| 35 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 36 | #define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); }); |
| 37 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 38 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 39 | namespace cs { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 40 | namespace tests { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 41 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 42 | using namespace nfd::tests; |
| 43 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 44 | BOOST_AUTO_TEST_SUITE(Table) |
| 45 | BOOST_FIXTURE_TEST_SUITE(TestCs, BaseFixture) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 46 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 47 | class FindFixture : public UnitTestTimeFixture |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 48 | { |
| 49 | protected: |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 50 | Name |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 51 | insert(uint32_t id, const Name& name, const std::function<void(Data&)>& modifyData = nullptr) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 52 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 53 | shared_ptr<Data> data = makeData(name); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 54 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 56 | if (modifyData != nullptr) { |
| 57 | modifyData(*data); |
| 58 | } |
| 59 | |
| 60 | data->wireEncode(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 61 | m_cs.insert(*data); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 62 | |
| 63 | return data->getFullName(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 64 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 65 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 66 | Interest& |
| 67 | startInterest(const Name& name) |
| 68 | { |
| 69 | m_interest = make_shared<Interest>(name); |
| 70 | return *m_interest; |
| 71 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 72 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 73 | void |
| 74 | find(const std::function<void(uint32_t)>& check) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 75 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 76 | bool hasResult = false; |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 77 | m_cs.find(*m_interest, |
| 78 | [&] (const Interest& interest, const Data& data) { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 79 | hasResult = true; |
| 80 | const Block& content = data.getContent(); |
Junxiao Shi | 4e1b6ee | 2017-08-03 02:20:50 +0000 | [diff] [blame] | 81 | uint32_t found = 0; |
| 82 | std::memcpy(&found, content.value(), sizeof(found)); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 83 | check(found); |
| 84 | }, |
| 85 | bind([&] { |
| 86 | hasResult = true; |
| 87 | check(0); |
| 88 | })); |
| 89 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 90 | // current Cs::find implementation is synchronous |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 91 | BOOST_CHECK(hasResult); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 92 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 94 | size_t |
| 95 | erase(const Name& prefix, size_t limit) |
| 96 | { |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 97 | optional<size_t> nErased; |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 98 | m_cs.erase(prefix, limit, [&] (size_t nErased1) { nErased = nErased1; }); |
| 99 | |
| 100 | // current Cs::erase implementation is synchronous |
| 101 | // if callback was not invoked, bad_optional_access would occur |
| 102 | return *nErased; |
| 103 | } |
| 104 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 105 | protected: |
| 106 | Cs m_cs; |
| 107 | shared_ptr<Interest> m_interest; |
| 108 | }; |
| 109 | |
| 110 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 111 | |
| 112 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 113 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 114 | insert(1, "/"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 115 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 116 | startInterest("/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 117 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 121 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 122 | insert(1, "/A"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 123 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 124 | startInterest("/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 125 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 128 | BOOST_AUTO_TEST_CASE(ExactName) |
| 129 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 130 | insert(1, "/"); |
| 131 | insert(2, "/A"); |
| 132 | insert(3, "/A/B"); |
| 133 | insert(4, "/A/C"); |
| 134 | insert(5, "/D"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 135 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 136 | startInterest("/A"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 137 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | BOOST_AUTO_TEST_CASE(FullName) |
| 141 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 142 | Name n1 = insert(1, "/A"); |
| 143 | Name n2 = insert(2, "/A"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 144 | |
| 145 | startInterest(n1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 146 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 147 | |
| 148 | startInterest(n2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 149 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 152 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 153 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 154 | insert(1, "/A"); |
| 155 | insert(2, "/B/p/1"); |
| 156 | insert(3, "/B/p/2"); |
| 157 | insert(4, "/B/q/1"); |
| 158 | insert(5, "/B/q/2"); |
| 159 | insert(6, "/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 160 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 161 | startInterest("/B"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 162 | CHECK_CS_FIND(2); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 166 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 167 | insert(1, "/A"); |
| 168 | insert(2, "/B/p/1"); |
| 169 | insert(3, "/B/p/2"); |
| 170 | insert(4, "/B/q/1"); |
| 171 | insert(5, "/B/q/2"); |
| 172 | insert(6, "/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 173 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 174 | startInterest("/B") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 175 | .setChildSelector(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 176 | CHECK_CS_FIND(4); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 179 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 180 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 181 | insert(1, "/"); |
| 182 | insert(2, "/A"); |
| 183 | insert(3, "/B/1"); |
| 184 | insert(4, "/C/1/2"); |
| 185 | insert(5, "/D/1/2/3"); |
| 186 | insert(6, "/E/1/2/3/4"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 187 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 188 | startInterest("/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 189 | .setMinSuffixComponents(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 190 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 191 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 192 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 193 | .setMinSuffixComponents(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 194 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 195 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 196 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 197 | .setMinSuffixComponents(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 198 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 199 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 200 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 201 | .setMinSuffixComponents(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 202 | CHECK_CS_FIND(3); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 203 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 204 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 205 | .setMinSuffixComponents(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 206 | CHECK_CS_FIND(4); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 207 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 208 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 209 | .setMinSuffixComponents(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 210 | CHECK_CS_FIND(5); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 211 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 212 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 213 | .setMinSuffixComponents(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 214 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 215 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 216 | startInterest("/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 217 | .setMinSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 218 | CHECK_CS_FIND(0); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 222 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 223 | insert(1, "/"); |
| 224 | insert(2, "/A"); |
| 225 | insert(3, "/B/2"); |
| 226 | insert(4, "/C/2/3"); |
| 227 | insert(5, "/D/2/3/4"); |
| 228 | insert(6, "/E/2/3/4/5"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 229 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 230 | startInterest("/") |
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(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 233 | CHECK_CS_FIND(0); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 234 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 235 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 236 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 237 | .setMaxSuffixComponents(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 238 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 239 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 240 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 241 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 242 | .setMaxSuffixComponents(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 243 | CHECK_CS_FIND(2); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 244 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 245 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 246 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 247 | .setMaxSuffixComponents(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 248 | CHECK_CS_FIND(3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 249 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 250 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 251 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 252 | .setMaxSuffixComponents(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 253 | CHECK_CS_FIND(4); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 254 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 255 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 256 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 257 | .setMaxSuffixComponents(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 258 | CHECK_CS_FIND(5); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 259 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 260 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 261 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 262 | .setMaxSuffixComponents(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 263 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 264 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 265 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 266 | .setChildSelector(1) |
| 267 | .setMaxSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 268 | CHECK_CS_FIND(6); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 271 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 272 | { |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 273 | insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 274 | insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(time::seconds(0)); }); |
| 275 | insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(time::seconds(1)); }); |
| 276 | insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(time::seconds(3600)); }); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 277 | |
| 278 | // lookup at exact same moment as insertion is not tested because this won't happen in reality |
| 279 | |
| 280 | this->advanceClocks(time::milliseconds(500)); // @500ms |
| 281 | startInterest("/A") |
| 282 | .setMustBeFresh(true); |
| 283 | CHECK_CS_FIND(3); |
| 284 | |
| 285 | this->advanceClocks(time::milliseconds(1500)); // @2s |
| 286 | startInterest("/A") |
| 287 | .setMustBeFresh(true); |
| 288 | CHECK_CS_FIND(4); |
| 289 | |
| 290 | this->advanceClocks(time::seconds(3500)); // @3502s |
| 291 | startInterest("/A") |
| 292 | .setMustBeFresh(true); |
| 293 | CHECK_CS_FIND(4); |
| 294 | |
| 295 | this->advanceClocks(time::seconds(3500)); // @7002s |
| 296 | startInterest("/A") |
| 297 | .setMustBeFresh(true); |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 298 | CHECK_CS_FIND(0); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 301 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 302 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 303 | Name n1 = insert(1, "/A"); |
| 304 | Name n2 = insert(2, "/A"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 305 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 306 | uint32_t expectedLeftmost = 0, expectedRightmost = 0; |
| 307 | if (n1 < n2) { |
| 308 | expectedLeftmost = 1; |
| 309 | expectedRightmost = 2; |
| 310 | } |
| 311 | else { |
| 312 | BOOST_CHECK_MESSAGE(n1 != n2, "implicit digest collision detected"); |
| 313 | expectedLeftmost = 2; |
| 314 | expectedRightmost = 1; |
| 315 | } |
| 316 | |
| 317 | startInterest("/A") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 318 | .setChildSelector(0); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 319 | CHECK_CS_FIND(expectedLeftmost); |
| 320 | startInterest("/A") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 321 | .setChildSelector(1); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 322 | CHECK_CS_FIND(expectedRightmost); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 326 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 327 | insert(1, "/A"); |
| 328 | Name n2 = insert(2, "/A"); |
| 329 | insert(3, "/A/B"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 330 | |
Junxiao Shi | bb6146e | 2017-07-26 23:07:52 +0000 | [diff] [blame] | 331 | uint8_t digest00[ndn::util::Sha256::DIGEST_SIZE]; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 332 | std::fill_n(digest00, sizeof(digest00), 0x00); |
Junxiao Shi | bb6146e | 2017-07-26 23:07:52 +0000 | [diff] [blame] | 333 | uint8_t digestFF[ndn::util::Sha256::DIGEST_SIZE]; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 334 | std::fill_n(digestFF, sizeof(digestFF), 0xFF); |
| 335 | |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 336 | ndn::Exclude excludeDigest; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 337 | excludeDigest.excludeRange( |
| 338 | name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)), |
| 339 | name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF))); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 340 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 341 | startInterest("/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 342 | .setChildSelector(0) |
| 343 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 344 | CHECK_CS_FIND(3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 345 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 346 | startInterest("/A") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 347 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 348 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 349 | CHECK_CS_FIND(3); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 350 | |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 351 | ndn::Exclude excludeGeneric; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 352 | excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0)); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 353 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 354 | startInterest("/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 355 | .setChildSelector(0) |
| 356 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 357 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 358 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 359 | startInterest("/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 360 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 361 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 362 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 363 | |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 364 | ndn::Exclude exclude2 = excludeGeneric; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 365 | exclude2.excludeOne(n2.get(-1)); |
| 366 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 367 | startInterest("/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 368 | .setChildSelector(0) |
| 369 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 370 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 371 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 372 | startInterest("/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 373 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 374 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 375 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 378 | BOOST_AUTO_TEST_SUITE_END() // Find |
| 379 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 380 | BOOST_FIXTURE_TEST_CASE(Erase, FindFixture) |
| 381 | { |
| 382 | insert(1, "/A/B/1"); |
| 383 | insert(2, "/A/B/2"); |
| 384 | insert(3, "/A/C/3"); |
| 385 | insert(4, "/A/C/4"); |
| 386 | insert(5, "/D/5"); |
| 387 | insert(6, "/E/6"); |
| 388 | BOOST_CHECK_EQUAL(m_cs.size(), 6); |
| 389 | |
| 390 | BOOST_CHECK_EQUAL(erase("/A", 3), 3); |
| 391 | BOOST_CHECK_EQUAL(m_cs.size(), 3); |
| 392 | int nDataUnderA = 0; |
| 393 | startInterest("/A/B/1"); |
| 394 | find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); }); |
| 395 | startInterest("/A/B/2"); |
| 396 | find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); }); |
| 397 | startInterest("/A/C/3"); |
| 398 | find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); }); |
| 399 | startInterest("/A/C/4"); |
| 400 | find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); }); |
| 401 | BOOST_CHECK_EQUAL(nDataUnderA, 1); |
| 402 | |
| 403 | BOOST_CHECK_EQUAL(erase("/D", 2), 1); |
| 404 | BOOST_CHECK_EQUAL(m_cs.size(), 2); |
| 405 | startInterest("/D/5"); |
| 406 | CHECK_CS_FIND(0); |
| 407 | |
| 408 | BOOST_CHECK_EQUAL(erase("/F", 2), 0); |
| 409 | BOOST_CHECK_EQUAL(m_cs.size(), 2); |
| 410 | } |
| 411 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 412 | // When the capacity limit is set to zero, Data cannot be inserted; |
| 413 | // this test case covers this situation. |
| 414 | // The behavior of non-zero capacity limit depends on the eviction policy, |
| 415 | // and is tested in policy test suites. |
| 416 | BOOST_FIXTURE_TEST_CASE(ZeroCapacity, FindFixture) |
| 417 | { |
| 418 | m_cs.setLimit(0); |
| 419 | |
| 420 | BOOST_CHECK_EQUAL(m_cs.getLimit(), 0); |
| 421 | BOOST_CHECK_EQUAL(m_cs.size(), 0); |
| 422 | BOOST_CHECK(m_cs.begin() == m_cs.end()); |
| 423 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 424 | insert(1, "/A"); |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 425 | BOOST_CHECK_EQUAL(m_cs.size(), 0); |
| 426 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 427 | startInterest("/A"); |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 428 | CHECK_CS_FIND(0); |
| 429 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 430 | |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 431 | BOOST_FIXTURE_TEST_CASE(EnablementFlags, FindFixture) |
| 432 | { |
| 433 | BOOST_CHECK_EQUAL(m_cs.shouldAdmit(), true); |
| 434 | BOOST_CHECK_EQUAL(m_cs.shouldServe(), true); |
| 435 | |
| 436 | insert(1, "/A"); |
| 437 | m_cs.enableAdmit(false); |
| 438 | insert(2, "/B"); |
| 439 | m_cs.enableAdmit(true); |
| 440 | insert(3, "/C"); |
| 441 | |
| 442 | startInterest("/A"); |
| 443 | CHECK_CS_FIND(1); |
| 444 | startInterest("/B"); |
| 445 | CHECK_CS_FIND(0); |
| 446 | startInterest("/C"); |
| 447 | CHECK_CS_FIND(3); |
| 448 | |
| 449 | m_cs.enableServe(false); |
| 450 | startInterest("/A"); |
| 451 | CHECK_CS_FIND(0); |
| 452 | startInterest("/C"); |
| 453 | CHECK_CS_FIND(0); |
| 454 | |
| 455 | m_cs.enableServe(true); |
| 456 | startInterest("/A"); |
| 457 | CHECK_CS_FIND(1); |
| 458 | startInterest("/C"); |
| 459 | CHECK_CS_FIND(3); |
| 460 | } |
| 461 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 462 | BOOST_FIXTURE_TEST_CASE(CachePolicyNoCache, FindFixture) |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 463 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 464 | insert(1, "/A", [] (Data& data) { |
| 465 | data.setTag(make_shared<lp::CachePolicyTag>( |
| 466 | lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE))); |
| 467 | }); |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 468 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 469 | startInterest("/A"); |
| 470 | CHECK_CS_FIND(0); |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 473 | BOOST_AUTO_TEST_CASE(Enumeration) |
| 474 | { |
| 475 | Cs cs; |
| 476 | |
| 477 | Name nameA("/A"); |
| 478 | Name nameAB("/A/B"); |
| 479 | Name nameABC("/A/B/C"); |
| 480 | Name nameD("/D"); |
| 481 | |
| 482 | BOOST_CHECK_EQUAL(cs.size(), 0); |
| 483 | BOOST_CHECK(cs.begin() == cs.end()); |
| 484 | |
| 485 | cs.insert(*makeData(nameABC)); |
| 486 | BOOST_CHECK_EQUAL(cs.size(), 1); |
| 487 | BOOST_CHECK(cs.begin() != cs.end()); |
| 488 | BOOST_CHECK(cs.begin()->getName() == nameABC); |
| 489 | BOOST_CHECK((*cs.begin()).getName() == nameABC); |
| 490 | |
| 491 | auto i = cs.begin(); |
| 492 | auto j = cs.begin(); |
| 493 | BOOST_CHECK(++i == cs.end()); |
| 494 | BOOST_CHECK(j++ == cs.begin()); |
| 495 | BOOST_CHECK(j == cs.end()); |
| 496 | |
| 497 | cs.insert(*makeData(nameA)); |
| 498 | cs.insert(*makeData(nameAB)); |
| 499 | cs.insert(*makeData(nameD)); |
| 500 | |
| 501 | std::set<Name> expected = {nameA, nameAB, nameABC, nameD}; |
| 502 | std::set<Name> actual; |
| 503 | for (const auto& csEntry : cs) { |
| 504 | actual.insert(csEntry.getName()); |
| 505 | } |
| 506 | BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end()); |
| 507 | } |
| 508 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 509 | BOOST_AUTO_TEST_SUITE_END() // TestCs |
| 510 | BOOST_AUTO_TEST_SUITE_END() // Table |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 511 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 512 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 513 | } // namespace cs |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 514 | } // namespace nfd |