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