blob: 6a553531ece5621025757c11f173379fd33ffe95 [file] [log] [blame]
Junxiao Shi0fcb41e2014-01-24 10:29:43 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shia9388182014-12-13 23:16:09 -07003 * Copyright (c) 2014, 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
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080031namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070033
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034BOOST_FIXTURE_TEST_SUITE(TableCs, BaseFixture)
Junxiao Shi0fcb41e2014-01-24 10:29:43 -070035
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080036class FindFixture : protected BaseFixture
37{
38protected:
Junxiao Shia9388182014-12-13 23:16:09 -070039 Name
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080040 insert(uint32_t id, const Name& name)
41 {
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -040042 shared_ptr<Data> data = makeData(name);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070043 data->setFreshnessPeriod(time::milliseconds(99999));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080044 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
Junxiao Shia9388182014-12-13 23:16:09 -070045 data->wireEncode();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070046
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080047 m_cs.insert(*data);
Junxiao Shia9388182014-12-13 23:16:09 -070048
49 return data->getFullName();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080050 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070051
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080052 Interest&
53 startInterest(const Name& name)
54 {
55 m_interest = make_shared<Interest>(name);
56 return *m_interest;
57 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070058
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080059 uint32_t
60 find()
61 {
Junxiao Shia9388182014-12-13 23:16:09 -070062 // m_cs.dump();
63
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080064 const Data* found = m_cs.find(*m_interest);
Junxiao Shia9388182014-12-13 23:16:09 -070065 if (found == nullptr) {
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080066 return 0;
67 }
68 const Block& content = found->getContent();
69 if (content.value_size() != sizeof(uint32_t)) {
70 return 0;
71 }
72 return *reinterpret_cast<const uint32_t*>(content.value());
73 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070074
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080075protected:
76 Cs m_cs;
77 shared_ptr<Interest> m_interest;
78};
79
80BOOST_FIXTURE_TEST_SUITE(Find, FindFixture)
81
82BOOST_AUTO_TEST_CASE(EmptyDataName)
83{
84 insert(1, "ndn:/");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070085
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080086 startInterest("ndn:/");
87 BOOST_CHECK_EQUAL(find(), 1);
88}
89
90BOOST_AUTO_TEST_CASE(EmptyInterestName)
91{
92 insert(1, "ndn:/A");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070093
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080094 startInterest("ndn:/");
95 BOOST_CHECK_EQUAL(find(), 1);
96}
97
Junxiao Shia9388182014-12-13 23:16:09 -070098BOOST_AUTO_TEST_CASE(ExactName)
99{
100 insert(1, "ndn:/");
101 insert(2, "ndn:/A");
102 insert(3, "ndn:/A/B");
103 insert(4, "ndn:/A/C");
104 insert(5, "ndn:/D");
105
106 startInterest("ndn:/A");
107 BOOST_CHECK_EQUAL(find(), 2);
108}
109
110BOOST_AUTO_TEST_CASE(FullName)
111{
112 Name n1 = insert(1, "ndn:/A");
113 Name n2 = insert(2, "ndn:/A");
114
115 startInterest(n1);
116 BOOST_CHECK_EQUAL(find(), 1);
117
118 startInterest(n2);
119 BOOST_CHECK_EQUAL(find(), 2);
120}
121
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800122BOOST_AUTO_TEST_CASE(Leftmost)
123{
124 insert(1, "ndn:/A");
125 insert(2, "ndn:/B/p/1");
126 insert(3, "ndn:/B/p/2");
127 insert(4, "ndn:/B/q/1");
128 insert(5, "ndn:/B/q/2");
129 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700130
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800131 startInterest("ndn:/B");
132 BOOST_CHECK_EQUAL(find(), 2);
133}
134
135BOOST_AUTO_TEST_CASE(Rightmost)
136{
137 insert(1, "ndn:/A");
138 insert(2, "ndn:/B/p/1");
139 insert(3, "ndn:/B/p/2");
140 insert(4, "ndn:/B/q/1");
141 insert(5, "ndn:/B/q/2");
142 insert(6, "ndn:/C");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700143
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800144 startInterest("ndn:/B")
145 .setChildSelector(1);
146 BOOST_CHECK_EQUAL(find(), 4);
147}
148
Junxiao Shia9388182014-12-13 23:16:09 -0700149BOOST_AUTO_TEST_CASE(MinSuffixComponents)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800150{
151 insert(1, "ndn:/");
152 insert(2, "ndn:/A");
Junxiao Shia9388182014-12-13 23:16:09 -0700153 insert(3, "ndn:/B/1");
154 insert(4, "ndn:/C/1/2");
155 insert(5, "ndn:/D/1/2/3");
156 insert(6, "ndn:/E/1/2/3/4");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700157
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800158 startInterest("ndn:/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800159 .setMinSuffixComponents(0);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800160 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700161
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800162 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700163 .setMinSuffixComponents(1);
164 BOOST_CHECK_EQUAL(find(), 1);
165
166 startInterest("ndn:/")
167 .setMinSuffixComponents(2);
168 BOOST_CHECK_EQUAL(find(), 2);
169
170 startInterest("ndn:/")
171 .setMinSuffixComponents(3);
172 BOOST_CHECK_EQUAL(find(), 3);
173
174 startInterest("ndn:/")
175 .setMinSuffixComponents(4);
176 BOOST_CHECK_EQUAL(find(), 4);
177
178 startInterest("ndn:/")
179 .setMinSuffixComponents(5);
180 BOOST_CHECK_EQUAL(find(), 5);
181
182 startInterest("ndn:/")
183 .setMinSuffixComponents(6);
184 BOOST_CHECK_EQUAL(find(), 6);
185
186 startInterest("ndn:/")
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800187 .setMinSuffixComponents(7);
188 BOOST_CHECK_EQUAL(find(), 0);
189}
190
191BOOST_AUTO_TEST_CASE(MaxSuffixComponents)
192{
193 insert(1, "ndn:/");
194 insert(2, "ndn:/A");
Junxiao Shia9388182014-12-13 23:16:09 -0700195 insert(3, "ndn:/B/2");
196 insert(4, "ndn:/C/2/3");
197 insert(5, "ndn:/D/2/3/4");
198 insert(6, "ndn:/E/2/3/4/5");
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700199
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800200 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700201 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800202 .setMaxSuffixComponents(0);
203 BOOST_CHECK_EQUAL(find(), 0);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700204
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800205 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700206 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800207 .setMaxSuffixComponents(1);
208 BOOST_CHECK_EQUAL(find(), 1);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700209
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800210 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700211 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800212 .setMaxSuffixComponents(2);
213 BOOST_CHECK_EQUAL(find(), 2);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700214
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800215 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700216 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800217 .setMaxSuffixComponents(3);
218 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700219
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800220 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700221 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800222 .setMaxSuffixComponents(4);
223 BOOST_CHECK_EQUAL(find(), 4);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700224
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800225 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700226 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800227 .setMaxSuffixComponents(5);
228 BOOST_CHECK_EQUAL(find(), 5);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700229
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800230 startInterest("ndn:/")
Junxiao Shia9388182014-12-13 23:16:09 -0700231 .setChildSelector(1)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800232 .setMaxSuffixComponents(6);
233 BOOST_CHECK_EQUAL(find(), 6);
Junxiao Shia9388182014-12-13 23:16:09 -0700234
235 startInterest("ndn:/")
236 .setChildSelector(1)
237 .setMaxSuffixComponents(7);
238 BOOST_CHECK_EQUAL(find(), 6);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800239}
240
241BOOST_AUTO_TEST_CASE(DigestOrder)
242{
243 insert(1, "ndn:/A");
244 insert(2, "ndn:/A");
245 // We don't know which comes first, but there must be some order
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700246
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800247 startInterest("ndn:/A")
248 .setChildSelector(0);
249 uint32_t leftmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700250
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800251 startInterest("ndn:/A")
252 .setChildSelector(1);
253 uint32_t rightmost = find();
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700254
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800255 BOOST_CHECK_NE(leftmost, rightmost);
256}
257
258BOOST_AUTO_TEST_CASE(DigestExclude)
259{
Junxiao Shia9388182014-12-13 23:16:09 -0700260 insert(1, "ndn:/A");
261 Name n2 = insert(2, "ndn:/A");
262 insert(3, "ndn:/A/B");
263
264 uint8_t digest00[ndn::crypto::SHA256_DIGEST_SIZE];
265 std::fill_n(digest00, sizeof(digest00), 0x00);
266 uint8_t digestFF[ndn::crypto::SHA256_DIGEST_SIZE];
267 std::fill_n(digestFF, sizeof(digestFF), 0xFF);
268
269 Exclude excludeDigest;
270 excludeDigest.excludeRange(
271 name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)),
272 name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF)));
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700273
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800274 startInterest("ndn:/A")
Junxiao Shia9388182014-12-13 23:16:09 -0700275 .setChildSelector(0)
276 .setExclude(excludeDigest);
277 BOOST_CHECK_EQUAL(find(), 3);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700278
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800279 startInterest("ndn:/A")
280 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700281 .setExclude(excludeDigest);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400282 BOOST_CHECK_EQUAL(find(), 3);
283
Junxiao Shia9388182014-12-13 23:16:09 -0700284 Exclude excludeGeneric;
285 excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0));
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400286
Junxiao Shia9388182014-12-13 23:16:09 -0700287 startInterest("ndn:/A")
288 .setChildSelector(0)
289 .setExclude(excludeGeneric);
290 int found1 = find();
291 BOOST_CHECK(found1 == 1 || found1 == 2);
292
293 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400294 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700295 .setExclude(excludeGeneric);
296 int found2 = find();
297 BOOST_CHECK(found2 == 1 || found2 == 2);
298
299 Exclude exclude2 = excludeGeneric;
300 exclude2.excludeOne(n2.get(-1));
301
302 startInterest("ndn:/A")
303 .setChildSelector(0)
304 .setExclude(exclude2);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400305 BOOST_CHECK_EQUAL(find(), 1);
306
Junxiao Shia9388182014-12-13 23:16:09 -0700307 startInterest("ndn:/A")
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400308 .setChildSelector(1)
Junxiao Shia9388182014-12-13 23:16:09 -0700309 .setExclude(exclude2);
310 BOOST_CHECK_EQUAL(find(), 1);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400311}
312
Junxiao Shia9388182014-12-13 23:16:09 -0700313BOOST_AUTO_TEST_SUITE_END()
314
315BOOST_FIXTURE_TEST_CASE(Evict, UnitTestTimeFixture)
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400316{
Junxiao Shia9388182014-12-13 23:16:09 -0700317 Cs cs(3);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400318
Junxiao Shia9388182014-12-13 23:16:09 -0700319 shared_ptr<Data> dataA = makeData("ndn:/A");
320 dataA->setFreshnessPeriod(time::milliseconds(99999));
321 dataA->wireEncode();
322 cs.insert(*dataA);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400323
Junxiao Shia9388182014-12-13 23:16:09 -0700324 shared_ptr<Data> dataB = makeData("ndn:/B");
325 dataB->setFreshnessPeriod(time::milliseconds(10));
326 dataB->wireEncode();
327 cs.insert(*dataB);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400328
Junxiao Shia9388182014-12-13 23:16:09 -0700329 shared_ptr<Data> dataC = makeData("ndn:/C");
330 dataC->setFreshnessPeriod(time::milliseconds(99999));
331 dataC->wireEncode();
332 cs.insert(*dataC, true);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400333
Junxiao Shia9388182014-12-13 23:16:09 -0700334 this->advanceClocks(time::milliseconds(11));
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400335
Junxiao Shia9388182014-12-13 23:16:09 -0700336 // evict unsolicited
337 shared_ptr<Data> dataD = makeData("ndn:/D");
338 dataD->setFreshnessPeriod(time::milliseconds(99999));
339 dataD->wireEncode();
340 cs.insert(*dataD);
341 BOOST_CHECK_EQUAL(cs.size(), 3);
342 BOOST_CHECK(cs.find(Interest("ndn:/C")) == nullptr);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400343
Junxiao Shia9388182014-12-13 23:16:09 -0700344 // evict stale
345 shared_ptr<Data> dataE = makeData("ndn:/E");
346 dataE->setFreshnessPeriod(time::milliseconds(99999));
347 dataE->wireEncode();
348 cs.insert(*dataE);
349 BOOST_CHECK_EQUAL(cs.size(), 3);
350 BOOST_CHECK(cs.find(Interest("ndn:/B")) == nullptr);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400351
Junxiao Shia9388182014-12-13 23:16:09 -0700352 // evict fifo
353 shared_ptr<Data> dataF = makeData("ndn:/F");
354 dataF->setFreshnessPeriod(time::milliseconds(99999));
355 dataF->wireEncode();
356 cs.insert(*dataF);
357 BOOST_CHECK_EQUAL(cs.size(), 3);
358 BOOST_CHECK(cs.find(Interest("ndn:/A")) == nullptr);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400359}
360
Junxiao Shia9388182014-12-13 23:16:09 -0700361BOOST_FIXTURE_TEST_CASE(Refresh, UnitTestTimeFixture)
362{
363 Cs cs(3);
Ilya Moiseenko96b80bb2014-04-05 20:53:27 -0400364
Junxiao Shia9388182014-12-13 23:16:09 -0700365 shared_ptr<Data> dataA = makeData("ndn:/A");
366 dataA->setFreshnessPeriod(time::milliseconds(99999));
367 dataA->wireEncode();
368 cs.insert(*dataA);
369
370 shared_ptr<Data> dataB = makeData("ndn:/B");
371 dataB->setFreshnessPeriod(time::milliseconds(10));
372 dataB->wireEncode();
373 cs.insert(*dataB);
374
375 shared_ptr<Data> dataC = makeData("ndn:/C");
376 dataC->setFreshnessPeriod(time::milliseconds(10));
377 dataC->wireEncode();
378 cs.insert(*dataC);
379
380 this->advanceClocks(time::milliseconds(11));
381
382 // refresh dataB
383 shared_ptr<Data> dataB2 = make_shared<Data>(*dataB);
384 dataB2->wireEncode();
385 cs.insert(*dataB2);
386 BOOST_CHECK_EQUAL(cs.size(), 3);
387 BOOST_CHECK(cs.find(Interest("ndn:/A")) != nullptr);
388 BOOST_CHECK(cs.find(Interest("ndn:/B")) != nullptr);
389 BOOST_CHECK(cs.find(Interest("ndn:/C")) != nullptr);
390
391 // evict dataC stale
392 shared_ptr<Data> dataD = makeData("ndn:/D");
393 dataD->setFreshnessPeriod(time::milliseconds(99999));
394 dataD->wireEncode();
395 cs.insert(*dataD);
396 BOOST_CHECK_EQUAL(cs.size(), 3);
397 BOOST_CHECK(cs.find(Interest("ndn:/C")) == nullptr);
398}
399
400BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyevb927a3a2014-01-24 10:41:47 -0800401
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700402} // namespace tests
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800403} // namespace nfd