blob: 0b3bebb9f4146575c198868779b94b1e1480a7d8 [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shifc206962015-01-16 11:12:22 -07003 * 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 Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * 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 Shia9388182014-12-13 23:16:09 -070024 */
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070025
26#include "table/cs.hpp"
Junxiao Shia9388182014-12-13 23:16:09 -070027#include <ndn-cxx/util/crypto.hpp>
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080028
Junxiao Shid9ee45c2014-02-27 15:38:11 -070029#include "tests/test-common.hpp"
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -080030
mzhang4eab72492015-02-25 11:16:09 -060031#define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); });
32
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080034namespace cs {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070036
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080037using namespace nfd::tests;
Junxiao Shi35b16b12015-02-16 22:14:57 -070038using ndn::nfd::LocalControlHeader;
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080039
Junxiao Shid9ee45c2014-02-27 15:38:11 -070040BOOST_FIXTURE_TEST_SUITE(TableCs, BaseFixture)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070041
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080042class FindFixture : protected BaseFixture
43{
44protected:
Junxiao Shia9388182014-12-13 23:16:09 -070045 Name
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080046 insert(uint32_t id, const Name& name)
47 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040048 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070049 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080050 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Junxiao Shia9388182014-12-13 23:16:09 -070051 data->wireEncode();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070052
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080053 m_cs.insert(*data);
Junxiao Shia9388182014-12-13 23:16:09 -070054
55 return data->getFullName();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080056 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070057
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080058 Interest&
59 startInterest(const Name& name)
60 {
61 m_interest = make_shared<Interest>(name);
62 return *m_interest;
63 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070064
mzhang4eab72492015-02-25 11:16:09 -060065 void
66 find(const std::function<void(uint32_t)>& check)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080067 {
mzhang4eab72492015-02-25 11:16:09 -060068 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 Moiseenko76cf77a2014-03-05 14:35:51 -080074 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070075
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080076protected:
77 Cs m_cs;
78 shared_ptr<Interest> m_interest;
79};
80
81BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
82
83BOOST_AUTO_TEST_CASE(EmptyDataName)
84{
85 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070086
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080087 startInterest("ndn:/");
mzhang4eab72492015-02-25 11:16:09 -060088 CHECK_CS_FIND(1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080089}
90
91BOOST_AUTO_TEST_CASE(EmptyInterestName)
92{
93 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070094
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080095 startInterest("ndn:/");
mzhang4eab72492015-02-25 11:16:09 -060096 CHECK_CS_FIND(1);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080097}
98
Junxiao Shia9388182014-12-13 23:16:09 -070099BOOST_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");
mzhang4eab72492015-02-25 11:16:09 -0600108 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700109}
110
111BOOST_AUTO_TEST_CASE(FullName)
112{
113 Name n1 = insert(1, "ndn:/A");
114 Name n2 = insert(2, "ndn:/A");
115
116 startInterest(n1);
mzhang4eab72492015-02-25 11:16:09 -0600117 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -0700118
119 startInterest(n2);
mzhang4eab72492015-02-25 11:16:09 -0600120 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700121}
122
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800123BOOST_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 Afanasyev9bcbc7c2014-04-06 19:37:37 -0700131
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800132 startInterest("ndn:/B");
mzhang4eab72492015-02-25 11:16:09 -0600133 CHECK_CS_FIND(2);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800134}
135
136BOOST_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 Afanasyev9bcbc7c2014-04-06 19:37:37 -0700144
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800145 startInterest("ndn:/B")
146 .setChildSelector(1);
mzhang4eab72492015-02-25 11:16:09 -0600147 CHECK_CS_FIND(4);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800148}
149
Junxiao Shia9388182014-12-13 23:16:09 -0700150BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800151{
152 insert(1, "ndn:/");
153 insert(2, "ndn:/A");
Junxiao Shia9388182014-12-13 23:16:09 -0700154 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -0700158
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800159 startInterest("ndn:/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800160 .setMinSuffixComponents(0);
mzhang4eab72492015-02-25 11:16:09 -0600161 CHECK_CS_FIND(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700162
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800163 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700164 .setMinSuffixComponents(1);
mzhang4eab72492015-02-25 11:16:09 -0600165 CHECK_CS_FIND(1);
Junxiao Shia9388182014-12-13 23:16:09 -0700166
167 startInterest("ndn:/")
168 .setMinSuffixComponents(2);
mzhang4eab72492015-02-25 11:16:09 -0600169 CHECK_CS_FIND(2);
Junxiao Shia9388182014-12-13 23:16:09 -0700170
171 startInterest("ndn:/")
172 .setMinSuffixComponents(3);
mzhang4eab72492015-02-25 11:16:09 -0600173 CHECK_CS_FIND(3);
Junxiao Shia9388182014-12-13 23:16:09 -0700174
175 startInterest("ndn:/")
176 .setMinSuffixComponents(4);
mzhang4eab72492015-02-25 11:16:09 -0600177 CHECK_CS_FIND(4);
Junxiao Shia9388182014-12-13 23:16:09 -0700178
179 startInterest("ndn:/")
180 .setMinSuffixComponents(5);
mzhang4eab72492015-02-25 11:16:09 -0600181 CHECK_CS_FIND(5);
Junxiao Shia9388182014-12-13 23:16:09 -0700182
183 startInterest("ndn:/")
184 .setMinSuffixComponents(6);
mzhang4eab72492015-02-25 11:16:09 -0600185 CHECK_CS_FIND(6);
Junxiao Shia9388182014-12-13 23:16:09 -0700186
187 startInterest("ndn:/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800188 .setMinSuffixComponents(7);
mzhang4eab72492015-02-25 11:16:09 -0600189 CHECK_CS_FIND(0);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800190}
191
192BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
193{
194 insert(1, "ndn:/");
195 insert(2, "ndn:/A");
Junxiao Shia9388182014-12-13 23:16:09 -0700196 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -0700200
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800201 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700202 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800203 .setMaxSuffixComponents(0);
mzhang4eab72492015-02-25 11:16:09 -0600204 CHECK_CS_FIND(0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700205
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800206 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700207 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800208 .setMaxSuffixComponents(1);
mzhang4eab72492015-02-25 11:16:09 -0600209 CHECK_CS_FIND(1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700210
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800211 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700212 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800213 .setMaxSuffixComponents(2);
mzhang4eab72492015-02-25 11:16:09 -0600214 CHECK_CS_FIND(2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700215
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800216 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700217 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800218 .setMaxSuffixComponents(3);
mzhang4eab72492015-02-25 11:16:09 -0600219 CHECK_CS_FIND(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700220
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800221 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700222 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800223 .setMaxSuffixComponents(4);
mzhang4eab72492015-02-25 11:16:09 -0600224 CHECK_CS_FIND(4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700225
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800226 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700227 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800228 .setMaxSuffixComponents(5);
mzhang4eab72492015-02-25 11:16:09 -0600229 CHECK_CS_FIND(5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700230
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800231 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700232 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800233 .setMaxSuffixComponents(6);
mzhang4eab72492015-02-25 11:16:09 -0600234 CHECK_CS_FIND(6);
Junxiao Shia9388182014-12-13 23:16:09 -0700235
236 startInterest("ndn:/")
237 .setChildSelector(1)
238 .setMaxSuffixComponents(7);
mzhang4eab72492015-02-25 11:16:09 -0600239 CHECK_CS_FIND(6);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800240}
241
242BOOST_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 Afanasyev9bcbc7c2014-04-06 19:37:37 -0700247
mzhang4eab72492015-02-25 11:16:09 -0600248 int leftmost = 0, rightmost = 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800249 startInterest("ndn:/A")
250 .setChildSelector(0);
mzhang4eab72492015-02-25 11:16:09 -0600251 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 Moiseenko76cf77a2014-03-05 14:35:51 -0800255 startInterest("ndn:/A")
256 .setChildSelector(1);
mzhang4eab72492015-02-25 11:16:09 -0600257 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 Moiseenko76cf77a2014-03-05 14:35:51 -0800261 BOOST_CHECK_NE(leftmost, rightmost);
262}
263
264BOOST_AUTO_TEST_CASE(DigestExclude)
265{
Junxiao Shia9388182014-12-13 23:16:09 -0700266 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -0700279
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800280 startInterest("ndn:/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700281 .setChildSelector(0)
282 .setExclude(excludeDigest);
mzhang4eab72492015-02-25 11:16:09 -0600283 CHECK_CS_FIND(3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700284
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800285 startInterest("ndn:/A")
286 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700287 .setExclude(excludeDigest);
mzhang4eab72492015-02-25 11:16:09 -0600288 CHECK_CS_FIND(3);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400289
Junxiao Shia9388182014-12-13 23:16:09 -0700290 Exclude excludeGeneric;
291 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400292
Junxiao Shia9388182014-12-13 23:16:09 -0700293 startInterest("ndn:/A")
294 .setChildSelector(0)
295 .setExclude(excludeGeneric);
mzhang4eab72492015-02-25 11:16:09 -0600296 find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); });
Junxiao Shia9388182014-12-13 23:16:09 -0700297
298 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400299 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700300 .setExclude(excludeGeneric);
mzhang4eab72492015-02-25 11:16:09 -0600301 find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); });
Junxiao Shia9388182014-12-13 23:16:09 -0700302
303 Exclude exclude2 = excludeGeneric;
304 exclude2.excludeOne(n2.get(-1));
305
306 startInterest("ndn:/A")
307 .setChildSelector(0)
308 .setExclude(exclude2);
mzhang4eab72492015-02-25 11:16:09 -0600309 CHECK_CS_FIND(1);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400310
Junxiao Shia9388182014-12-13 23:16:09 -0700311 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400312 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700313 .setExclude(exclude2);
mzhang4eab72492015-02-25 11:16:09 -0600314 CHECK_CS_FIND(1);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400315}
316
Junxiao Shia9388182014-12-13 23:16:09 -0700317BOOST_AUTO_TEST_SUITE_END()
318
Junxiao Shi35b16b12015-02-16 22:14:57 -0700319BOOST_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
mzhang4eab72492015-02-25 11:16:09 -0600328 cs.find(Interest("ndn:/A"),
329 bind([] { BOOST_CHECK(false); }),
330 bind([] { BOOST_CHECK(true); }));
Junxiao Shi35b16b12015-02-16 22:14:57 -0700331}
332
Junxiao Shifc206962015-01-16 11:12:22 -0700333BOOST_AUTO_TEST_CASE(Enumeration)
334{
335 Cs cs;
336
337 Name nameA("/A");
338 Name nameAB("/A/B");
339 Name nameABC("/A/B/C");
340 Name nameD("/D");
341
342 BOOST_CHECK_EQUAL(cs.size(), 0);
343 BOOST_CHECK(cs.begin() == cs.end());
344
345 cs.insert(*makeData(nameABC));
346 BOOST_CHECK_EQUAL(cs.size(), 1);
347 BOOST_CHECK(cs.begin() != cs.end());
348 BOOST_CHECK(cs.begin()->getName() == nameABC);
349 BOOST_CHECK((*cs.begin()).getName() == nameABC);
350
351 auto i = cs.begin();
352 auto j = cs.begin();
353 BOOST_CHECK(++i == cs.end());
354 BOOST_CHECK(j++ == cs.begin());
355 BOOST_CHECK(j == cs.end());
356
357 cs.insert(*makeData(nameA));
358 cs.insert(*makeData(nameAB));
359 cs.insert(*makeData(nameD));
360
361 std::set<Name> expected = {nameA, nameAB, nameABC, nameD};
362 std::set<Name> actual;
363 for (const auto& csEntry : cs) {
364 actual.insert(csEntry.getName());
365 }
366 BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
367}
368
Junxiao Shia9388182014-12-13 23:16:09 -0700369BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800370
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700371} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800372} // namespace cs
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800373} // namespace nfd