Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 43 | class FindFixture : protected BaseFixture |
| 44 | { |
| 45 | protected: |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 46 | Name |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 47 | insert(uint32_t id, const Name& name) |
| 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)); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 52 | data->wireEncode(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 53 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 54 | m_cs.insert(*data); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 55 | |
| 56 | return data->getFullName(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 57 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 58 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 59 | Interest& |
| 60 | startInterest(const Name& name) |
| 61 | { |
| 62 | m_interest = make_shared<Interest>(name); |
| 63 | return *m_interest; |
| 64 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 65 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 66 | void |
| 67 | find(const std::function<void(uint32_t)>& check) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 68 | { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 69 | m_cs.find(*m_interest, |
| 70 | [&] (const Interest& interest, const Data& data) { |
| 71 | const Block& content = data.getContent(); |
| 72 | uint32_t found = *reinterpret_cast<const uint32_t*>(content.value()); |
| 73 | check(found); }, |
| 74 | bind([&] { check(0); })); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 76 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 77 | protected: |
| 78 | Cs m_cs; |
| 79 | shared_ptr<Interest> m_interest; |
| 80 | }; |
| 81 | |
| 82 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 83 | |
| 84 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 85 | { |
| 86 | insert(1, "ndn:/"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 87 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 88 | startInterest("ndn:/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 89 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 93 | { |
| 94 | insert(1, "ndn:/A"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 95 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 96 | startInterest("ndn:/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 97 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 100 | BOOST_AUTO_TEST_CASE(ExactName) |
| 101 | { |
| 102 | insert(1, "ndn:/"); |
| 103 | insert(2, "ndn:/A"); |
| 104 | insert(3, "ndn:/A/B"); |
| 105 | insert(4, "ndn:/A/C"); |
| 106 | insert(5, "ndn:/D"); |
| 107 | |
| 108 | startInterest("ndn:/A"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 109 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | BOOST_AUTO_TEST_CASE(FullName) |
| 113 | { |
| 114 | Name n1 = insert(1, "ndn:/A"); |
| 115 | Name n2 = insert(2, "ndn:/A"); |
| 116 | |
| 117 | startInterest(n1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 118 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 119 | |
| 120 | startInterest(n2); |
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 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 124 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 125 | { |
| 126 | insert(1, "ndn:/A"); |
| 127 | insert(2, "ndn:/B/p/1"); |
| 128 | insert(3, "ndn:/B/p/2"); |
| 129 | insert(4, "ndn:/B/q/1"); |
| 130 | insert(5, "ndn:/B/q/2"); |
| 131 | insert(6, "ndn:/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 132 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 133 | startInterest("ndn:/B"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 134 | CHECK_CS_FIND(2); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 138 | { |
| 139 | insert(1, "ndn:/A"); |
| 140 | insert(2, "ndn:/B/p/1"); |
| 141 | insert(3, "ndn:/B/p/2"); |
| 142 | insert(4, "ndn:/B/q/1"); |
| 143 | insert(5, "ndn:/B/q/2"); |
| 144 | insert(6, "ndn:/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 145 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 146 | startInterest("ndn:/B") |
| 147 | .setChildSelector(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 148 | CHECK_CS_FIND(4); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 151 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 152 | { |
| 153 | insert(1, "ndn:/"); |
| 154 | insert(2, "ndn:/A"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 155 | insert(3, "ndn:/B/1"); |
| 156 | insert(4, "ndn:/C/1/2"); |
| 157 | insert(5, "ndn:/D/1/2/3"); |
| 158 | insert(6, "ndn:/E/1/2/3/4"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 159 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 160 | startInterest("ndn:/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 161 | .setMinSuffixComponents(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 162 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 163 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 164 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 165 | .setMinSuffixComponents(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 166 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 167 | |
| 168 | startInterest("ndn:/") |
| 169 | .setMinSuffixComponents(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 170 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 171 | |
| 172 | startInterest("ndn:/") |
| 173 | .setMinSuffixComponents(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 174 | CHECK_CS_FIND(3); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 175 | |
| 176 | startInterest("ndn:/") |
| 177 | .setMinSuffixComponents(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 178 | CHECK_CS_FIND(4); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 179 | |
| 180 | startInterest("ndn:/") |
| 181 | .setMinSuffixComponents(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 182 | CHECK_CS_FIND(5); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 183 | |
| 184 | startInterest("ndn:/") |
| 185 | .setMinSuffixComponents(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 186 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 187 | |
| 188 | startInterest("ndn:/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 189 | .setMinSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 190 | CHECK_CS_FIND(0); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 194 | { |
| 195 | insert(1, "ndn:/"); |
| 196 | insert(2, "ndn:/A"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 197 | insert(3, "ndn:/B/2"); |
| 198 | insert(4, "ndn:/C/2/3"); |
| 199 | insert(5, "ndn:/D/2/3/4"); |
| 200 | insert(6, "ndn:/E/2/3/4/5"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 201 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 202 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 203 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 204 | .setMaxSuffixComponents(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 205 | CHECK_CS_FIND(0); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 206 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 207 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 208 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 209 | .setMaxSuffixComponents(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 210 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 211 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 212 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 213 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 214 | .setMaxSuffixComponents(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 215 | CHECK_CS_FIND(2); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 216 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 217 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 218 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 219 | .setMaxSuffixComponents(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 220 | CHECK_CS_FIND(3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 221 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 222 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 223 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 224 | .setMaxSuffixComponents(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 225 | CHECK_CS_FIND(4); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 226 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 227 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 228 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 229 | .setMaxSuffixComponents(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 230 | CHECK_CS_FIND(5); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 231 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 232 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 233 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 234 | .setMaxSuffixComponents(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 235 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 236 | |
| 237 | startInterest("ndn:/") |
| 238 | .setChildSelector(1) |
| 239 | .setMaxSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 240 | CHECK_CS_FIND(6); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 244 | { |
| 245 | insert(1, "ndn:/A"); |
| 246 | insert(2, "ndn:/A"); |
| 247 | // 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] | 248 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 249 | int leftmost = 0, rightmost = 0; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 250 | startInterest("ndn:/A") |
| 251 | .setChildSelector(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 252 | m_cs.find(*m_interest, |
| 253 | [&leftmost] (const Interest& interest, const Data& data) { |
| 254 | leftmost = *reinterpret_cast<const uint32_t*>(data.getContent().value());}, |
| 255 | bind([] { BOOST_CHECK(false); })); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 256 | startInterest("ndn:/A") |
| 257 | .setChildSelector(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 258 | m_cs.find(*m_interest, |
| 259 | [&rightmost] (const Interest, const Data& data) { |
| 260 | rightmost = *reinterpret_cast<const uint32_t*>(data.getContent().value()); }, |
| 261 | bind([] { BOOST_CHECK(false); })); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 262 | BOOST_CHECK_NE(leftmost, rightmost); |
| 263 | } |
| 264 | |
| 265 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 266 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 267 | insert(1, "ndn:/A"); |
| 268 | Name n2 = insert(2, "ndn:/A"); |
| 269 | insert(3, "ndn:/A/B"); |
| 270 | |
| 271 | uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 272 | std::fill_n(digest00, sizeof(digest00), 0x00); |
| 273 | uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 274 | std::fill_n(digestFF, sizeof(digestFF), 0xFF); |
| 275 | |
| 276 | Exclude excludeDigest; |
| 277 | excludeDigest.excludeRange( |
| 278 | name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)), |
| 279 | name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF))); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 280 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 281 | startInterest("ndn:/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 282 | .setChildSelector(0) |
| 283 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 284 | CHECK_CS_FIND(3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 285 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 286 | startInterest("ndn:/A") |
| 287 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 288 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 289 | CHECK_CS_FIND(3); |
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 | Exclude excludeGeneric; |
| 292 | excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0)); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 293 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 294 | startInterest("ndn:/A") |
| 295 | .setChildSelector(0) |
| 296 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 297 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 298 | |
| 299 | startInterest("ndn:/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 300 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 301 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 302 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 303 | |
| 304 | Exclude exclude2 = excludeGeneric; |
| 305 | exclude2.excludeOne(n2.get(-1)); |
| 306 | |
| 307 | startInterest("ndn:/A") |
| 308 | .setChildSelector(0) |
| 309 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 310 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 311 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 312 | startInterest("ndn:/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 313 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 314 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 315 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 318 | /// \todo test MustBeFresh |
| 319 | |
| 320 | BOOST_AUTO_TEST_SUITE_END() // Find |
| 321 | |
| 322 | // When the capacity limit is set to zero, Data cannot be inserted; |
| 323 | // this test case covers this situation. |
| 324 | // The behavior of non-zero capacity limit depends on the eviction policy, |
| 325 | // and is tested in policy test suites. |
| 326 | BOOST_FIXTURE_TEST_CASE(ZeroCapacity, FindFixture) |
| 327 | { |
| 328 | m_cs.setLimit(0); |
| 329 | |
| 330 | BOOST_CHECK_EQUAL(m_cs.getLimit(), 0); |
| 331 | BOOST_CHECK_EQUAL(m_cs.size(), 0); |
| 332 | BOOST_CHECK(m_cs.begin() == m_cs.end()); |
| 333 | |
| 334 | insert(1, "ndn:/A"); |
| 335 | BOOST_CHECK_EQUAL(m_cs.size(), 0); |
| 336 | |
| 337 | startInterest("ndn:/A"); |
| 338 | CHECK_CS_FIND(0); |
| 339 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 340 | |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 341 | BOOST_AUTO_TEST_CASE(CachePolicyNoCache) |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 342 | { |
| 343 | Cs cs(3); |
| 344 | |
| 345 | shared_ptr<Data> dataA = makeData("ndn:/A"); |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 346 | dataA->wireEncode(); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 347 | dataA->setTag(make_shared<lp::CachePolicyTag>( |
| 348 | lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE))); |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 349 | cs.insert(*dataA); |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 350 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 351 | BOOST_CHECK_EQUAL(cs.size(), 0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 352 | cs.find(Interest("ndn:/A"), |
| 353 | bind([] { BOOST_CHECK(false); }), |
| 354 | bind([] { BOOST_CHECK(true); })); |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 357 | BOOST_AUTO_TEST_CASE(Enumeration) |
| 358 | { |
| 359 | Cs cs; |
| 360 | |
| 361 | Name nameA("/A"); |
| 362 | Name nameAB("/A/B"); |
| 363 | Name nameABC("/A/B/C"); |
| 364 | Name nameD("/D"); |
| 365 | |
| 366 | BOOST_CHECK_EQUAL(cs.size(), 0); |
| 367 | BOOST_CHECK(cs.begin() == cs.end()); |
| 368 | |
| 369 | cs.insert(*makeData(nameABC)); |
| 370 | BOOST_CHECK_EQUAL(cs.size(), 1); |
| 371 | BOOST_CHECK(cs.begin() != cs.end()); |
| 372 | BOOST_CHECK(cs.begin()->getName() == nameABC); |
| 373 | BOOST_CHECK((*cs.begin()).getName() == nameABC); |
| 374 | |
| 375 | auto i = cs.begin(); |
| 376 | auto j = cs.begin(); |
| 377 | BOOST_CHECK(++i == cs.end()); |
| 378 | BOOST_CHECK(j++ == cs.begin()); |
| 379 | BOOST_CHECK(j == cs.end()); |
| 380 | |
| 381 | cs.insert(*makeData(nameA)); |
| 382 | cs.insert(*makeData(nameAB)); |
| 383 | cs.insert(*makeData(nameD)); |
| 384 | |
| 385 | std::set<Name> expected = {nameA, nameAB, nameABC, nameD}; |
| 386 | std::set<Name> actual; |
| 387 | for (const auto& csEntry : cs) { |
| 388 | actual.insert(csEntry.getName()); |
| 389 | } |
| 390 | BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end()); |
| 391 | } |
| 392 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 393 | BOOST_AUTO_TEST_SUITE_END() // TestCs |
| 394 | BOOST_AUTO_TEST_SUITE_END() // Table |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 395 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 396 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 397 | } // namespace cs |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 398 | } // namespace nfd |