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 | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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" |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 27 | #include <ndn-cxx/lp/tags.hpp> |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/util/crypto.hpp> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 29 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 30 | #include "tests/test-common.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 31 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 32 | #define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); }); |
| 33 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 34 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 35 | namespace cs { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | namespace tests { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 37 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 38 | using namespace nfd::tests; |
| 39 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(Table) |
| 41 | BOOST_FIXTURE_TEST_SUITE(TestCs, BaseFixture) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 42 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 43 | class FindFixture : public UnitTestTimeFixture |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 44 | { |
| 45 | protected: |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 46 | Name |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 47 | 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] | 48 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 49 | shared_ptr<Data> data = makeData(name); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 50 | data->setFreshnessPeriod(time::milliseconds(99999)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 51 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 53 | if (modifyData != nullptr) { |
| 54 | modifyData(*data); |
| 55 | } |
| 56 | |
| 57 | data->wireEncode(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 58 | m_cs.insert(*data); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 59 | |
| 60 | return data->getFullName(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 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 | Interest& |
| 64 | startInterest(const Name& name) |
| 65 | { |
| 66 | m_interest = make_shared<Interest>(name); |
| 67 | return *m_interest; |
| 68 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 69 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 70 | void |
| 71 | find(const std::function<void(uint32_t)>& check) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 72 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 73 | bool hasResult = false; |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 74 | m_cs.find(*m_interest, |
| 75 | [&] (const Interest& interest, const Data& data) { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 76 | hasResult = true; |
| 77 | const Block& content = data.getContent(); |
| 78 | uint32_t found = *reinterpret_cast<const uint32_t*>(content.value()); |
| 79 | check(found); |
| 80 | }, |
| 81 | bind([&] { |
| 82 | hasResult = true; |
| 83 | check(0); |
| 84 | })); |
| 85 | |
| 86 | // current Cs::find implementation performs lookup synchronously |
| 87 | BOOST_CHECK(hasResult); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 88 | } |
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 | protected: |
| 91 | Cs m_cs; |
| 92 | shared_ptr<Interest> m_interest; |
| 93 | }; |
| 94 | |
| 95 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 96 | |
| 97 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 98 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 99 | insert(1, "/"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 100 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 101 | startInterest("/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 102 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 106 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 107 | insert(1, "/A"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 108 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 109 | startInterest("/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 110 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 113 | BOOST_AUTO_TEST_CASE(ExactName) |
| 114 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 115 | insert(1, "/"); |
| 116 | insert(2, "/A"); |
| 117 | insert(3, "/A/B"); |
| 118 | insert(4, "/A/C"); |
| 119 | insert(5, "/D"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 120 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 121 | startInterest("/A"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 122 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | BOOST_AUTO_TEST_CASE(FullName) |
| 126 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 127 | Name n1 = insert(1, "/A"); |
| 128 | Name n2 = insert(2, "/A"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 129 | |
| 130 | startInterest(n1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 131 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 132 | |
| 133 | startInterest(n2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 134 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 137 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 138 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 139 | insert(1, "/A"); |
| 140 | insert(2, "/B/p/1"); |
| 141 | insert(3, "/B/p/2"); |
| 142 | insert(4, "/B/q/1"); |
| 143 | insert(5, "/B/q/2"); |
| 144 | insert(6, "/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 145 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 146 | startInterest("/B"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 147 | CHECK_CS_FIND(2); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 151 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 152 | insert(1, "/A"); |
| 153 | insert(2, "/B/p/1"); |
| 154 | insert(3, "/B/p/2"); |
| 155 | insert(4, "/B/q/1"); |
| 156 | insert(5, "/B/q/2"); |
| 157 | insert(6, "/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 158 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 159 | startInterest("/B") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 160 | .setChildSelector(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 161 | CHECK_CS_FIND(4); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 164 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 165 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 166 | insert(1, "/"); |
| 167 | insert(2, "/A"); |
| 168 | insert(3, "/B/1"); |
| 169 | insert(4, "/C/1/2"); |
| 170 | insert(5, "/D/1/2/3"); |
| 171 | insert(6, "/E/1/2/3/4"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 172 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 173 | startInterest("/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 174 | .setMinSuffixComponents(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 175 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 176 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 177 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 178 | .setMinSuffixComponents(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 179 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 180 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 181 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 182 | .setMinSuffixComponents(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 183 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 184 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 185 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 186 | .setMinSuffixComponents(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 187 | CHECK_CS_FIND(3); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 188 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 189 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 190 | .setMinSuffixComponents(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 191 | CHECK_CS_FIND(4); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 192 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 193 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 194 | .setMinSuffixComponents(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 195 | CHECK_CS_FIND(5); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 196 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 197 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 198 | .setMinSuffixComponents(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 199 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 200 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 201 | startInterest("/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 202 | .setMinSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 203 | CHECK_CS_FIND(0); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 207 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 208 | insert(1, "/"); |
| 209 | insert(2, "/A"); |
| 210 | insert(3, "/B/2"); |
| 211 | insert(4, "/C/2/3"); |
| 212 | insert(5, "/D/2/3/4"); |
| 213 | insert(6, "/E/2/3/4/5"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 214 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 215 | startInterest("/") |
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(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 218 | CHECK_CS_FIND(0); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 219 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 220 | startInterest("/") |
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(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 223 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 224 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 225 | startInterest("/") |
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(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 228 | CHECK_CS_FIND(2); |
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(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 233 | CHECK_CS_FIND(3); |
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(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 238 | CHECK_CS_FIND(4); |
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(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 243 | CHECK_CS_FIND(5); |
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(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 248 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -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) |
| 252 | .setMaxSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 253 | CHECK_CS_FIND(6); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 256 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MustBeFresh, 1) |
| 257 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 258 | { |
| 259 | ///\todo #3944 add /A/1 without FreshnessPeriod, which is same as FreshnessPeriod=0ms |
| 260 | insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(time::seconds(0)); }); |
| 261 | insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(time::seconds(1)); }); |
| 262 | insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(time::seconds(3600)); }); |
| 263 | insert(5, "/A/5"); // omitted FreshnessPeriod means infinite |
| 264 | ///\todo #3944 delete /A/5 |
| 265 | |
| 266 | // lookup at exact same moment as insertion is not tested because this won't happen in reality |
| 267 | |
| 268 | this->advanceClocks(time::milliseconds(500)); // @500ms |
| 269 | startInterest("/A") |
| 270 | .setMustBeFresh(true); |
| 271 | CHECK_CS_FIND(3); |
| 272 | |
| 273 | this->advanceClocks(time::milliseconds(1500)); // @2s |
| 274 | startInterest("/A") |
| 275 | .setMustBeFresh(true); |
| 276 | CHECK_CS_FIND(4); |
| 277 | |
| 278 | this->advanceClocks(time::seconds(3500)); // @3502s |
| 279 | startInterest("/A") |
| 280 | .setMustBeFresh(true); |
| 281 | CHECK_CS_FIND(4); |
| 282 | |
| 283 | this->advanceClocks(time::seconds(3500)); // @7002s |
| 284 | startInterest("/A") |
| 285 | .setMustBeFresh(true); |
| 286 | CHECK_CS_FIND(5); // expected failure https://redmine.named-data.net/issues/3944#note-2 |
| 287 | ///\todo #3944 this should not find any Data |
| 288 | } |
| 289 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 290 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 291 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 292 | Name n1 = insert(1, "/A"); |
| 293 | Name n2 = insert(2, "/A"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 294 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 295 | uint32_t expectedLeftmost = 0, expectedRightmost = 0; |
| 296 | if (n1 < n2) { |
| 297 | expectedLeftmost = 1; |
| 298 | expectedRightmost = 2; |
| 299 | } |
| 300 | else { |
| 301 | BOOST_CHECK_MESSAGE(n1 != n2, "implicit digest collision detected"); |
| 302 | expectedLeftmost = 2; |
| 303 | expectedRightmost = 1; |
| 304 | } |
| 305 | |
| 306 | startInterest("/A") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 307 | .setChildSelector(0); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 308 | CHECK_CS_FIND(expectedLeftmost); |
| 309 | startInterest("/A") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 310 | .setChildSelector(1); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 311 | CHECK_CS_FIND(expectedRightmost); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 315 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 316 | insert(1, "/A"); |
| 317 | Name n2 = insert(2, "/A"); |
| 318 | insert(3, "/A/B"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 319 | |
| 320 | uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 321 | std::fill_n(digest00, sizeof(digest00), 0x00); |
| 322 | uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 323 | std::fill_n(digestFF, sizeof(digestFF), 0xFF); |
| 324 | |
| 325 | Exclude excludeDigest; |
| 326 | excludeDigest.excludeRange( |
| 327 | name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)), |
| 328 | name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF))); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 329 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 330 | startInterest("/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 331 | .setChildSelector(0) |
| 332 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 333 | CHECK_CS_FIND(3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 334 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 335 | startInterest("/A") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 336 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 337 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 338 | CHECK_CS_FIND(3); |
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 | Exclude excludeGeneric; |
| 341 | excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0)); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 342 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 343 | startInterest("/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 344 | .setChildSelector(0) |
| 345 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 346 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 347 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 348 | startInterest("/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 349 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 350 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 351 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 352 | |
| 353 | Exclude exclude2 = excludeGeneric; |
| 354 | exclude2.excludeOne(n2.get(-1)); |
| 355 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 356 | startInterest("/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 357 | .setChildSelector(0) |
| 358 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 359 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 360 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 361 | startInterest("/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 362 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 363 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 364 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 365 | } |
| 366 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 367 | BOOST_AUTO_TEST_SUITE_END() // Find |
| 368 | |
| 369 | // When the capacity limit is set to zero, Data cannot be inserted; |
| 370 | // this test case covers this situation. |
| 371 | // The behavior of non-zero capacity limit depends on the eviction policy, |
| 372 | // and is tested in policy test suites. |
| 373 | BOOST_FIXTURE_TEST_CASE(ZeroCapacity, FindFixture) |
| 374 | { |
| 375 | m_cs.setLimit(0); |
| 376 | |
| 377 | BOOST_CHECK_EQUAL(m_cs.getLimit(), 0); |
| 378 | BOOST_CHECK_EQUAL(m_cs.size(), 0); |
| 379 | BOOST_CHECK(m_cs.begin() == m_cs.end()); |
| 380 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 381 | insert(1, "/A"); |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 382 | BOOST_CHECK_EQUAL(m_cs.size(), 0); |
| 383 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 384 | startInterest("/A"); |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 385 | CHECK_CS_FIND(0); |
| 386 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 387 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 388 | BOOST_FIXTURE_TEST_CASE(CachePolicyNoCache, FindFixture) |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 389 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 390 | insert(1, "/A", [] (Data& data) { |
| 391 | data.setTag(make_shared<lp::CachePolicyTag>( |
| 392 | lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE))); |
| 393 | }); |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 394 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 395 | startInterest("/A"); |
| 396 | CHECK_CS_FIND(0); |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 399 | BOOST_AUTO_TEST_CASE(Enumeration) |
| 400 | { |
| 401 | Cs cs; |
| 402 | |
| 403 | Name nameA("/A"); |
| 404 | Name nameAB("/A/B"); |
| 405 | Name nameABC("/A/B/C"); |
| 406 | Name nameD("/D"); |
| 407 | |
| 408 | BOOST_CHECK_EQUAL(cs.size(), 0); |
| 409 | BOOST_CHECK(cs.begin() == cs.end()); |
| 410 | |
| 411 | cs.insert(*makeData(nameABC)); |
| 412 | BOOST_CHECK_EQUAL(cs.size(), 1); |
| 413 | BOOST_CHECK(cs.begin() != cs.end()); |
| 414 | BOOST_CHECK(cs.begin()->getName() == nameABC); |
| 415 | BOOST_CHECK((*cs.begin()).getName() == nameABC); |
| 416 | |
| 417 | auto i = cs.begin(); |
| 418 | auto j = cs.begin(); |
| 419 | BOOST_CHECK(++i == cs.end()); |
| 420 | BOOST_CHECK(j++ == cs.begin()); |
| 421 | BOOST_CHECK(j == cs.end()); |
| 422 | |
| 423 | cs.insert(*makeData(nameA)); |
| 424 | cs.insert(*makeData(nameAB)); |
| 425 | cs.insert(*makeData(nameD)); |
| 426 | |
| 427 | std::set<Name> expected = {nameA, nameAB, nameABC, nameD}; |
| 428 | std::set<Name> actual; |
| 429 | for (const auto& csEntry : cs) { |
| 430 | actual.insert(csEntry.getName()); |
| 431 | } |
| 432 | BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end()); |
| 433 | } |
| 434 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 435 | BOOST_AUTO_TEST_SUITE_END() // TestCs |
| 436 | BOOST_AUTO_TEST_SUITE_END() // Table |
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 |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 439 | } // namespace cs |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 440 | } // namespace nfd |