Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/cs.hpp" |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/util/crypto.hpp> |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 28 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 30 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 31 | #define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); }); |
| 32 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 33 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 34 | namespace cs { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | namespace tests { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 36 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 37 | using namespace nfd::tests; |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 38 | using ndn::nfd::LocalControlHeader; |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 39 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 40 | BOOST_FIXTURE_TEST_SUITE(TableCs, BaseFixture) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 41 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 42 | class FindFixture : protected BaseFixture |
| 43 | { |
| 44 | protected: |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 45 | Name |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 46 | insert(uint32_t id, const Name& name) |
| 47 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 48 | shared_ptr<Data> data = makeData(name); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 49 | data->setFreshnessPeriod(time::milliseconds(99999)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 50 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 51 | data->wireEncode(); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 52 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 53 | m_cs.insert(*data); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 54 | |
| 55 | return data->getFullName(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 56 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 57 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 58 | Interest& |
| 59 | startInterest(const Name& name) |
| 60 | { |
| 61 | m_interest = make_shared<Interest>(name); |
| 62 | return *m_interest; |
| 63 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 64 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 65 | void |
| 66 | find(const std::function<void(uint32_t)>& check) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 67 | { |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 68 | m_cs.find(*m_interest, |
| 69 | [&] (const Interest& interest, const Data& data) { |
| 70 | const Block& content = data.getContent(); |
| 71 | uint32_t found = *reinterpret_cast<const uint32_t*>(content.value()); |
| 72 | check(found); }, |
| 73 | bind([&] { check(0); })); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 74 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 75 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 76 | protected: |
| 77 | Cs m_cs; |
| 78 | shared_ptr<Interest> m_interest; |
| 79 | }; |
| 80 | |
| 81 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 84 | { |
| 85 | insert(1, "ndn:/"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 86 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 87 | startInterest("ndn:/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 88 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 92 | { |
| 93 | insert(1, "ndn:/A"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 94 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 95 | startInterest("ndn:/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 96 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 99 | BOOST_AUTO_TEST_CASE(ExactName) |
| 100 | { |
| 101 | insert(1, "ndn:/"); |
| 102 | insert(2, "ndn:/A"); |
| 103 | insert(3, "ndn:/A/B"); |
| 104 | insert(4, "ndn:/A/C"); |
| 105 | insert(5, "ndn:/D"); |
| 106 | |
| 107 | startInterest("ndn:/A"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 108 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_CASE(FullName) |
| 112 | { |
| 113 | Name n1 = insert(1, "ndn:/A"); |
| 114 | Name n2 = insert(2, "ndn:/A"); |
| 115 | |
| 116 | startInterest(n1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 117 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 118 | |
| 119 | startInterest(n2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 120 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 124 | { |
| 125 | insert(1, "ndn:/A"); |
| 126 | insert(2, "ndn:/B/p/1"); |
| 127 | insert(3, "ndn:/B/p/2"); |
| 128 | insert(4, "ndn:/B/q/1"); |
| 129 | insert(5, "ndn:/B/q/2"); |
| 130 | insert(6, "ndn:/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 131 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 132 | startInterest("ndn:/B"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 133 | CHECK_CS_FIND(2); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 137 | { |
| 138 | insert(1, "ndn:/A"); |
| 139 | insert(2, "ndn:/B/p/1"); |
| 140 | insert(3, "ndn:/B/p/2"); |
| 141 | insert(4, "ndn:/B/q/1"); |
| 142 | insert(5, "ndn:/B/q/2"); |
| 143 | insert(6, "ndn:/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 144 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 145 | startInterest("ndn:/B") |
| 146 | .setChildSelector(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 147 | CHECK_CS_FIND(4); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 151 | { |
| 152 | insert(1, "ndn:/"); |
| 153 | insert(2, "ndn:/A"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 154 | insert(3, "ndn:/B/1"); |
| 155 | insert(4, "ndn:/C/1/2"); |
| 156 | insert(5, "ndn:/D/1/2/3"); |
| 157 | insert(6, "ndn:/E/1/2/3/4"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 158 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 159 | startInterest("ndn:/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 160 | .setMinSuffixComponents(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 161 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 162 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 163 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 164 | .setMinSuffixComponents(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 165 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 166 | |
| 167 | startInterest("ndn:/") |
| 168 | .setMinSuffixComponents(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 169 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 170 | |
| 171 | startInterest("ndn:/") |
| 172 | .setMinSuffixComponents(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 173 | CHECK_CS_FIND(3); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 174 | |
| 175 | startInterest("ndn:/") |
| 176 | .setMinSuffixComponents(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 177 | CHECK_CS_FIND(4); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 178 | |
| 179 | startInterest("ndn:/") |
| 180 | .setMinSuffixComponents(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 181 | CHECK_CS_FIND(5); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 182 | |
| 183 | startInterest("ndn:/") |
| 184 | .setMinSuffixComponents(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 185 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 186 | |
| 187 | startInterest("ndn:/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 188 | .setMinSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 189 | CHECK_CS_FIND(0); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 193 | { |
| 194 | insert(1, "ndn:/"); |
| 195 | insert(2, "ndn:/A"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 196 | insert(3, "ndn:/B/2"); |
| 197 | insert(4, "ndn:/C/2/3"); |
| 198 | insert(5, "ndn:/D/2/3/4"); |
| 199 | insert(6, "ndn:/E/2/3/4/5"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 200 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 201 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 202 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 203 | .setMaxSuffixComponents(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 204 | CHECK_CS_FIND(0); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 205 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 206 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 207 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 208 | .setMaxSuffixComponents(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 209 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 210 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 211 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 212 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 213 | .setMaxSuffixComponents(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 214 | CHECK_CS_FIND(2); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 215 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 216 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 217 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 218 | .setMaxSuffixComponents(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 219 | CHECK_CS_FIND(3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 220 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 221 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 222 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 223 | .setMaxSuffixComponents(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 224 | CHECK_CS_FIND(4); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 225 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 226 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 227 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 228 | .setMaxSuffixComponents(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 229 | CHECK_CS_FIND(5); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 230 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 231 | startInterest("ndn:/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 232 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 233 | .setMaxSuffixComponents(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 234 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 235 | |
| 236 | startInterest("ndn:/") |
| 237 | .setChildSelector(1) |
| 238 | .setMaxSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 239 | CHECK_CS_FIND(6); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 243 | { |
| 244 | insert(1, "ndn:/A"); |
| 245 | insert(2, "ndn:/A"); |
| 246 | // 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] | 247 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 248 | int leftmost = 0, rightmost = 0; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 249 | startInterest("ndn:/A") |
| 250 | .setChildSelector(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 251 | m_cs.find(*m_interest, |
| 252 | [&leftmost] (const Interest& interest, const Data& data) { |
| 253 | leftmost = *reinterpret_cast<const uint32_t*>(data.getContent().value());}, |
| 254 | bind([] { BOOST_CHECK(false); })); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 255 | startInterest("ndn:/A") |
| 256 | .setChildSelector(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 257 | m_cs.find(*m_interest, |
| 258 | [&rightmost] (const Interest, const Data& data) { |
| 259 | rightmost = *reinterpret_cast<const uint32_t*>(data.getContent().value()); }, |
| 260 | bind([] { BOOST_CHECK(false); })); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 261 | BOOST_CHECK_NE(leftmost, rightmost); |
| 262 | } |
| 263 | |
| 264 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 265 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 266 | insert(1, "ndn:/A"); |
| 267 | Name n2 = insert(2, "ndn:/A"); |
| 268 | insert(3, "ndn:/A/B"); |
| 269 | |
| 270 | uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 271 | std::fill_n(digest00, sizeof(digest00), 0x00); |
| 272 | uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE]; |
| 273 | std::fill_n(digestFF, sizeof(digestFF), 0xFF); |
| 274 | |
| 275 | Exclude excludeDigest; |
| 276 | excludeDigest.excludeRange( |
| 277 | name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)), |
| 278 | name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF))); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 279 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 280 | startInterest("ndn:/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 281 | .setChildSelector(0) |
| 282 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 283 | CHECK_CS_FIND(3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 284 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 285 | startInterest("ndn:/A") |
| 286 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 287 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 288 | CHECK_CS_FIND(3); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 289 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 290 | Exclude excludeGeneric; |
| 291 | excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0)); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 292 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 293 | startInterest("ndn:/A") |
| 294 | .setChildSelector(0) |
| 295 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 296 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 297 | |
| 298 | startInterest("ndn:/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 299 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 300 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 301 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 302 | |
| 303 | Exclude exclude2 = excludeGeneric; |
| 304 | exclude2.excludeOne(n2.get(-1)); |
| 305 | |
| 306 | startInterest("ndn:/A") |
| 307 | .setChildSelector(0) |
| 308 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 309 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 310 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 311 | startInterest("ndn:/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 312 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 313 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 314 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 317 | BOOST_AUTO_TEST_SUITE_END() |
| 318 | |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 319 | BOOST_AUTO_TEST_CASE(CachingPolicyNoCache) |
| 320 | { |
| 321 | Cs cs(3); |
| 322 | |
| 323 | shared_ptr<Data> dataA = makeData("ndn:/A"); |
| 324 | dataA->getLocalControlHeader().setCachingPolicy(LocalControlHeader::CachingPolicy::NO_CACHE); |
| 325 | dataA->wireEncode(); |
| 326 | BOOST_CHECK_EQUAL(cs.insert(*dataA), false); |
| 327 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 328 | cs.find(Interest("ndn:/A"), |
| 329 | bind([] { BOOST_CHECK(false); }), |
| 330 | bind([] { BOOST_CHECK(true); })); |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 333 | BOOST_FIXTURE_TEST_CASE(Evict, UnitTestTimeFixture) |
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 | Cs cs(3); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 336 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 337 | shared_ptr<Data> dataA = makeData("ndn:/A"); |
| 338 | dataA->setFreshnessPeriod(time::milliseconds(99999)); |
| 339 | dataA->wireEncode(); |
| 340 | cs.insert(*dataA); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 341 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 342 | shared_ptr<Data> dataB = makeData("ndn:/B"); |
| 343 | dataB->setFreshnessPeriod(time::milliseconds(10)); |
| 344 | dataB->wireEncode(); |
| 345 | cs.insert(*dataB); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 346 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 347 | shared_ptr<Data> dataC = makeData("ndn:/C"); |
| 348 | dataC->setFreshnessPeriod(time::milliseconds(99999)); |
| 349 | dataC->wireEncode(); |
| 350 | cs.insert(*dataC, true); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 351 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 352 | this->advanceClocks(time::milliseconds(11)); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 353 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 354 | // evict unsolicited |
| 355 | shared_ptr<Data> dataD = makeData("ndn:/D"); |
| 356 | dataD->setFreshnessPeriod(time::milliseconds(99999)); |
| 357 | dataD->wireEncode(); |
| 358 | cs.insert(*dataD); |
| 359 | BOOST_CHECK_EQUAL(cs.size(), 3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 360 | cs.find(Interest("ndn:/C"), |
| 361 | bind([] { BOOST_CHECK(false); }), |
| 362 | bind([] { BOOST_CHECK(true); })); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 363 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 364 | // evict stale |
| 365 | shared_ptr<Data> dataE = makeData("ndn:/E"); |
| 366 | dataE->setFreshnessPeriod(time::milliseconds(99999)); |
| 367 | dataE->wireEncode(); |
| 368 | cs.insert(*dataE); |
| 369 | BOOST_CHECK_EQUAL(cs.size(), 3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 370 | cs.find(Interest("ndn:/B"), |
| 371 | bind([] { BOOST_CHECK(false); }), |
| 372 | bind([] { BOOST_CHECK(true); })); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 373 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 374 | // evict fifo |
| 375 | shared_ptr<Data> dataF = makeData("ndn:/F"); |
| 376 | dataF->setFreshnessPeriod(time::milliseconds(99999)); |
| 377 | dataF->wireEncode(); |
| 378 | cs.insert(*dataF); |
| 379 | BOOST_CHECK_EQUAL(cs.size(), 3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 380 | cs.find(Interest("ndn:/A"), |
| 381 | bind([] { BOOST_CHECK(false); }), |
| 382 | bind([] { BOOST_CHECK(true); })); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 385 | BOOST_FIXTURE_TEST_CASE(Refresh, UnitTestTimeFixture) |
| 386 | { |
| 387 | Cs cs(3); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 388 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 389 | shared_ptr<Data> dataA = makeData("ndn:/A"); |
| 390 | dataA->setFreshnessPeriod(time::milliseconds(99999)); |
| 391 | dataA->wireEncode(); |
| 392 | cs.insert(*dataA); |
| 393 | |
| 394 | shared_ptr<Data> dataB = makeData("ndn:/B"); |
| 395 | dataB->setFreshnessPeriod(time::milliseconds(10)); |
| 396 | dataB->wireEncode(); |
| 397 | cs.insert(*dataB); |
| 398 | |
| 399 | shared_ptr<Data> dataC = makeData("ndn:/C"); |
| 400 | dataC->setFreshnessPeriod(time::milliseconds(10)); |
| 401 | dataC->wireEncode(); |
| 402 | cs.insert(*dataC); |
| 403 | |
| 404 | this->advanceClocks(time::milliseconds(11)); |
| 405 | |
| 406 | // refresh dataB |
| 407 | shared_ptr<Data> dataB2 = make_shared<Data>(*dataB); |
| 408 | dataB2->wireEncode(); |
| 409 | cs.insert(*dataB2); |
| 410 | BOOST_CHECK_EQUAL(cs.size(), 3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 411 | cs.find(Interest("ndn:/A"), |
| 412 | bind([] { BOOST_CHECK(true); }), |
| 413 | bind([] { BOOST_CHECK(false); })); |
| 414 | |
| 415 | cs.find(Interest("ndn:/B"), |
| 416 | bind([] { BOOST_CHECK(true); }), |
| 417 | bind([] { BOOST_CHECK(false); })); |
| 418 | |
| 419 | cs.find(Interest("ndn:/C"), |
| 420 | bind([] { BOOST_CHECK(true); }), |
| 421 | bind([] { BOOST_CHECK(false); })); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 422 | |
| 423 | // evict dataC stale |
| 424 | shared_ptr<Data> dataD = makeData("ndn:/D"); |
| 425 | dataD->setFreshnessPeriod(time::milliseconds(99999)); |
| 426 | dataD->wireEncode(); |
| 427 | cs.insert(*dataD); |
| 428 | BOOST_CHECK_EQUAL(cs.size(), 3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame^] | 429 | cs.find(Interest("ndn:/C"), |
| 430 | bind([] { BOOST_CHECK(false); }), |
| 431 | bind([] { BOOST_CHECK(true); })); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 434 | BOOST_AUTO_TEST_CASE(Enumeration) |
| 435 | { |
| 436 | Cs cs; |
| 437 | |
| 438 | Name nameA("/A"); |
| 439 | Name nameAB("/A/B"); |
| 440 | Name nameABC("/A/B/C"); |
| 441 | Name nameD("/D"); |
| 442 | |
| 443 | BOOST_CHECK_EQUAL(cs.size(), 0); |
| 444 | BOOST_CHECK(cs.begin() == cs.end()); |
| 445 | |
| 446 | cs.insert(*makeData(nameABC)); |
| 447 | BOOST_CHECK_EQUAL(cs.size(), 1); |
| 448 | BOOST_CHECK(cs.begin() != cs.end()); |
| 449 | BOOST_CHECK(cs.begin()->getName() == nameABC); |
| 450 | BOOST_CHECK((*cs.begin()).getName() == nameABC); |
| 451 | |
| 452 | auto i = cs.begin(); |
| 453 | auto j = cs.begin(); |
| 454 | BOOST_CHECK(++i == cs.end()); |
| 455 | BOOST_CHECK(j++ == cs.begin()); |
| 456 | BOOST_CHECK(j == cs.end()); |
| 457 | |
| 458 | cs.insert(*makeData(nameA)); |
| 459 | cs.insert(*makeData(nameAB)); |
| 460 | cs.insert(*makeData(nameD)); |
| 461 | |
| 462 | std::set<Name> expected = {nameA, nameAB, nameABC, nameD}; |
| 463 | std::set<Name> actual; |
| 464 | for (const auto& csEntry : cs) { |
| 465 | actual.insert(csEntry.getName()); |
| 466 | } |
| 467 | BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end()); |
| 468 | } |
| 469 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 470 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 471 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 472 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 473 | } // namespace cs |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 474 | } // namespace nfd |