blob: 46cefa0e51eda38de60e07cd43e49b230750c0fd [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08002/**
Junxiao Shidf4b24e2016-07-14 21:41:43 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080020 */
21
22#include "exclude.hpp"
Junxiao Shidf4b24e2016-07-14 21:41:43 +000023#include "util/crypto.hpp"
Junxiao Shib80e9472016-07-20 13:45:24 +000024#include <boost/lexical_cast.hpp>
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080025
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026#include "boost-test.hpp"
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080027
28namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080030
31BOOST_AUTO_TEST_SUITE(TestExclude)
32
Junxiao Shidf4b24e2016-07-14 21:41:43 +000033BOOST_AUTO_TEST_SUITE(GenericComponent) // exclude generic NameComponent
34
35BOOST_AUTO_TEST_CASE(One)
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080036{
37 Exclude e;
Junxiao Shib80e9472016-07-20 13:45:24 +000038 std::vector<Exclude::Range> enumerated;
39
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070040 e.excludeOne(name::Component("b"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070041 BOOST_CHECK_EQUAL(e.toUri(), "b");
Junxiao Shib80e9472016-07-20 13:45:24 +000042 BOOST_CHECK_EQUAL(e.size(), 1);
43 enumerated.clear();
44 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
45 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
46 BOOST_CHECK_EQUAL(enumerated[0].isSingular(), true);
47 BOOST_CHECK_EQUAL(enumerated[0].from, name::Component("b"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080048
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070049 e.excludeOne(name::Component("d"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070050 BOOST_CHECK_EQUAL(e.toUri(), "b,d");
Junxiao Shib80e9472016-07-20 13:45:24 +000051 BOOST_CHECK_EQUAL(e.size(), 2);
52 enumerated.clear();
53 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
54 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
55 BOOST_CHECK_EQUAL(enumerated[0].isSingular(), true);
56 BOOST_CHECK_EQUAL(enumerated[0].from, name::Component("b"));
57 BOOST_CHECK_EQUAL(enumerated[1].isSingular(), true);
58 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("d"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080059
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070060 e.excludeOne(name::Component("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070061 BOOST_CHECK_EQUAL(e.toUri(), "a,b,d");
Junxiao Shib80e9472016-07-20 13:45:24 +000062 BOOST_CHECK_EQUAL(e.size(), 3);
63 enumerated.clear();
64 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
65 BOOST_REQUIRE_EQUAL(enumerated.size(), 3);
66 BOOST_CHECK_EQUAL(enumerated[0].isSingular(), true);
67 BOOST_CHECK_EQUAL(enumerated[0].from, name::Component("a"));
68 BOOST_CHECK_EQUAL(enumerated[1].isSingular(), true);
69 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("b"));
70 BOOST_CHECK_EQUAL(enumerated[2].isSingular(), true);
71 BOOST_CHECK_EQUAL(enumerated[2].from, name::Component("d"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080072
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070073 e.excludeOne(name::Component("aa"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070074 BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa");
Junxiao Shib80e9472016-07-20 13:45:24 +000075 BOOST_CHECK_EQUAL(e.size(), 4);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080076
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070077 e.excludeOne(name::Component("cc"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070078 BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa,cc");
Junxiao Shib80e9472016-07-20 13:45:24 +000079 BOOST_CHECK_EQUAL(e.size(), 5);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080080
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070081 e.excludeOne(name::Component("c"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070082 BOOST_CHECK_EQUAL(e.toUri(), "a,b,c,d,aa,cc");
Junxiao Shib80e9472016-07-20 13:45:24 +000083 BOOST_CHECK_EQUAL(e.size(), 6);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080084}
85
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070086BOOST_AUTO_TEST_CASE(Before)
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080087{
88 // based on http://redmine.named-data.net/issues/1158
89 ndn::Exclude e;
90 BOOST_REQUIRE_NO_THROW(e.excludeBefore(name::Component("PuQxMaf91")));
91
92 BOOST_CHECK_EQUAL(e.toUri(), "*,PuQxMaf91");
93}
94
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070095BOOST_AUTO_TEST_CASE(Ranges)
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080096{
Junxiao Shidf4b24e2016-07-14 21:41:43 +000097 // example: ANY /b /d ANY /f
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080098
99 Exclude e;
Junxiao Shib80e9472016-07-20 13:45:24 +0000100 std::vector<Exclude::Range> enumerated;
101
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700102 e.excludeOne(name::Component("b0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700103 BOOST_CHECK_EQUAL(e.toUri(), "b0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000104 BOOST_CHECK_EQUAL(e.size(), 1);
105 enumerated.clear();
106 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
107 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
108 BOOST_CHECK_EQUAL(enumerated[0].isSingular(), true);
109 BOOST_CHECK_EQUAL(enumerated[0].from, name::Component("b0"));
110 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(enumerated[0]), "{b0}");
111 BOOST_CHECK_EQUAL(enumerated[0], (Exclude::Range{false, name::Component("b0"), false, name::Component("b0")}));
112 BOOST_CHECK_NE(enumerated[0], (Exclude::Range{false, name::Component("b0"), false, name::Component("b1")}));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800113
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000114 e.excludeBefore(name::Component("b1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700115 BOOST_CHECK_EQUAL(e.toUri(), "*,b1");
Junxiao Shib80e9472016-07-20 13:45:24 +0000116 BOOST_CHECK_EQUAL(e.size(), 1);
117 enumerated.clear();
118 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
119 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
120 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
121 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
122 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("b1"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800123
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000124 e.excludeBefore(name::Component("c0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700125 BOOST_CHECK_EQUAL(e.toUri(), "*,c0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000126 BOOST_CHECK_EQUAL(e.size(), 1);
127 enumerated.clear();
128 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
129 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
130 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
131 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
132 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("c0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800133
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700134 e.excludeRange(name::Component("a0"), name::Component("c0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700135 BOOST_CHECK_EQUAL(e.toUri(), "*,c0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000136 BOOST_CHECK_EQUAL(e.size(), 1);
137 enumerated.clear();
138 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
139 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
140 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
141 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
142 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("c0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800143
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700144 e.excludeRange(name::Component("d0"), name::Component("e0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700145 BOOST_CHECK_EQUAL(e.toUri(), "*,c0,d0,*,e0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000146 BOOST_CHECK_EQUAL(e.size(), 2);
147 enumerated.clear();
148 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
149 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
150 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
151 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
152 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("c0"));
153 BOOST_CHECK_EQUAL(enumerated[1].fromInfinity, false);
154 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("d0"));
155 BOOST_CHECK_EQUAL(enumerated[1].toInfinity, false);
156 BOOST_CHECK_EQUAL(enumerated[1].to, name::Component("e0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800157
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700158 e.excludeRange(name::Component("c1"), name::Component("d1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700159 BOOST_CHECK_EQUAL(e.toUri(), "*,c0,c1,*,e0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000160 BOOST_CHECK_EQUAL(e.size(), 2);
161 enumerated.clear();
162 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
163 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
164 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
165 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
166 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("c0"));
167 BOOST_CHECK_EQUAL(enumerated[1].fromInfinity, false);
168 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("c1"));
169 BOOST_CHECK_EQUAL(enumerated[1].toInfinity, false);
170 BOOST_CHECK_EQUAL(enumerated[1].to, name::Component("e0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800171
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700172 e.excludeRange(name::Component("a1"), name::Component("d1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700173 BOOST_CHECK_EQUAL(e.toUri(), "*,e0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000174 BOOST_CHECK_EQUAL(e.size(), 1);
175 enumerated.clear();
176 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
177 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
178 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
179 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
180 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("e0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800181
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700182 e.excludeBefore(name::Component("e2"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700183 BOOST_CHECK_EQUAL(e.toUri(), "*,e2");
Junxiao Shib80e9472016-07-20 13:45:24 +0000184 BOOST_CHECK_EQUAL(e.size(), 1);
185 enumerated.clear();
186 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
187 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
188 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
189 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
190 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("e2"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800191
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700192 e.excludeAfter(name::Component("f0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700193 BOOST_CHECK_EQUAL(e.toUri(), "*,e2,f0,*");
Junxiao Shib80e9472016-07-20 13:45:24 +0000194 BOOST_CHECK_EQUAL(e.size(), 2);
195 enumerated.clear();
196 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
197 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
198 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
199 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
200 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("e2"));
201 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(enumerated[0]), "(-∞,e2]");
202 BOOST_CHECK_EQUAL(enumerated[0], (Exclude::Range{true, name::Component("ignore"), false, name::Component("e2")}));
203 BOOST_CHECK_EQUAL(enumerated[1].fromInfinity, false);
204 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("f0"));
205 BOOST_CHECK_EQUAL(enumerated[1].toInfinity, true);
206 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(enumerated[1]), "[f0,+∞)");
207 BOOST_CHECK_EQUAL(enumerated[1], (Exclude::Range{false, name::Component("f0"), true, name::Component("ignore")}));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800208
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700209 e.excludeAfter(name::Component("e5"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700210 BOOST_CHECK_EQUAL(e.toUri(), "*,e2,e5,*");
Junxiao Shib80e9472016-07-20 13:45:24 +0000211 BOOST_CHECK_EQUAL(e.size(), 2);
212 enumerated.clear();
213 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
214 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
215 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
216 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
217 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("e2"));
218 BOOST_CHECK_EQUAL(enumerated[1].fromInfinity, false);
219 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("e5"));
220 BOOST_CHECK_EQUAL(enumerated[1].toInfinity, true);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800221
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700222 e.excludeAfter(name::Component("b2"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700223 BOOST_CHECK_EQUAL(e.toUri(), "*");
Junxiao Shib80e9472016-07-20 13:45:24 +0000224 BOOST_CHECK_EQUAL(e.size(), 1);
225 enumerated.clear();
226 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
227 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
228 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
229 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, true);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800230
Junxiao Shi75203022014-09-11 10:01:50 -0700231 BOOST_REQUIRE_THROW(e.excludeRange(name::Component("d0"), name::Component("a0")),
232 Exclude::Error);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800233}
234
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000235BOOST_AUTO_TEST_SUITE_END() // GenericComponent
236
237
238BOOST_AUTO_TEST_SUITE(ImplicitDigest) // exclude ImplicitSha256DigestComponent
239
240/** \brief make a name::Component with an octet repeated crypto::SHA256_DIGEST_SIZE times
241 * \param octet the octet to fill the component
242 * \param isDigest whether to make an ImplicitSha256DigestComponent or a generic NameComponent
243 * \param lastOctet if non-negative, set the last octet to a different value
244 */
245static name::Component
246makeComponent(uint8_t octet, bool isDigest, int lastOctet = -1)
247{
248 uint8_t wire[crypto::SHA256_DIGEST_SIZE];
249 std::memset(wire, octet, sizeof(wire));
250 if (lastOctet >= 0) {
251 wire[crypto::SHA256_DIGEST_SIZE - 1] = static_cast<uint8_t>(lastOctet);
252 }
253
254 if (isDigest) {
255 return name::Component::fromImplicitSha256Digest(wire, sizeof(wire));
256 }
257 else {
258 return name::Component(wire, sizeof(wire));
259 }
260}
261
262BOOST_AUTO_TEST_CASE(One)
263{
264 name::Component digestC = makeComponent(0xCC, true);;
265 name::Component genericC = makeComponent(0xCC, false);
266 name::Component digestD = makeComponent(0xDD, true);
267
268 Exclude e;
269 e.excludeOne(digestC);
270 BOOST_CHECK_EQUAL(e.isExcluded(digestC), true);
271 BOOST_CHECK_EQUAL(e.isExcluded(genericC), false);
272 BOOST_CHECK_EQUAL(e.isExcluded(digestD), false);
273
274 e.clear();
275 e.excludeOne(genericC);
276 BOOST_CHECK_EQUAL(e.isExcluded(digestC), false);
277 BOOST_CHECK_EQUAL(e.isExcluded(genericC), true);
278}
279
280BOOST_AUTO_TEST_CASE(BeforeDigest)
281{
282 name::Component digestBA = makeComponent(0xBB, true, 0xBA);
283 name::Component digestBB = makeComponent(0xBB, true);
284 name::Component digestBC = makeComponent(0xBB, true, 0xBC);
285
286 Exclude e;
287 e.excludeBefore(digestBB);
288 BOOST_CHECK_EQUAL(e.isExcluded(digestBA), true);
289 BOOST_CHECK_EQUAL(e.isExcluded(digestBB), true);
290 BOOST_CHECK_EQUAL(e.isExcluded(digestBC), false);
291 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), false);
292 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false);
Junxiao Shib80e9472016-07-20 13:45:24 +0000293
294 BOOST_CHECK_EQUAL(e.size(), 1);
295 std::vector<Exclude::Range> enumerated;
296 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
297 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
298 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
299 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
300 BOOST_CHECK_EQUAL(enumerated[0].to, digestBB);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000301}
302
303BOOST_AUTO_TEST_CASE(BeforeGeneric)
304{
305 name::Component digest0 = makeComponent(0x00, true);
306 name::Component digest9 = makeComponent(0x99, true);
307 name::Component digestF = makeComponent(0xFF, true);
308
309 Exclude e;
310 e.excludeBefore(name::Component(""));
311 BOOST_CHECK_EQUAL(e.isExcluded(digest0), true);
312 BOOST_CHECK_EQUAL(e.isExcluded(digest9), true);
313 BOOST_CHECK_EQUAL(e.isExcluded(digestF), true);
314 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
315 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false);
316}
317
318BOOST_AUTO_TEST_CASE(AfterDigest)
319{
320 name::Component digestBA = makeComponent(0xBB, true, 0xBA);
321 name::Component digestBB = makeComponent(0xBB, true);
322 name::Component digestBC = makeComponent(0xBB, true, 0xBC);
323
324 Exclude e;
325 e.excludeAfter(digestBB);
326 BOOST_CHECK_EQUAL(e.isExcluded(digestBA), false);
327 BOOST_CHECK_EQUAL(e.isExcluded(digestBB), true);
328 BOOST_CHECK_EQUAL(e.isExcluded(digestBC), true);
329 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
330 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), true);
Junxiao Shib80e9472016-07-20 13:45:24 +0000331
332 BOOST_CHECK_EQUAL(e.size(), 1);
333 std::vector<Exclude::Range> enumerated;
334 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
335 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
336 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, false);
337 BOOST_CHECK_EQUAL(enumerated[0].from, digestBB);
338 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, true);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000339}
340
341BOOST_AUTO_TEST_CASE(AfterDigestFF)
342{
343 name::Component digest00 = makeComponent(0x00, true);
344 name::Component digest99 = makeComponent(0x99, true);
345 name::Component digestFE = makeComponent(0xFF, true, 0xFE);
346 name::Component digestFF = makeComponent(0xFF, true);
347
348 Exclude e;
349 e.excludeAfter(digestFF);
350 BOOST_CHECK_EQUAL(e.isExcluded(digest00), false);
351 BOOST_CHECK_EQUAL(e.isExcluded(digest99), false);
352 BOOST_CHECK_EQUAL(e.isExcluded(digestFE), false);
353 BOOST_CHECK_EQUAL(e.isExcluded(digestFF), true);
354 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
355 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), true);
356}
357
358BOOST_AUTO_TEST_CASE(AfterGeneric)
359{
360 name::Component digest0 = makeComponent(0x00, true);
361 name::Component digest9 = makeComponent(0x99, true);
362 name::Component digestF = makeComponent(0xFF, true);
363
364 Exclude e;
365 e.excludeAfter(name::Component(""));
366 BOOST_CHECK_EQUAL(e.isExcluded(digest0), false);
367 BOOST_CHECK_EQUAL(e.isExcluded(digest9), false);
368 BOOST_CHECK_EQUAL(e.isExcluded(digestF), false);
369 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
370 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), true);
371}
372
373BOOST_AUTO_TEST_CASE(RangeDigest)
374{
375 name::Component digest0 = makeComponent(0x00, true);
376 name::Component digest7 = makeComponent(0x77, true);
377 name::Component digest8 = makeComponent(0x88, true);
378 name::Component digest9 = makeComponent(0x99, true);
379 name::Component digestF = makeComponent(0xFF, true);
380
381 Exclude e;
382 e.excludeRange(digest7, digest9);
383 BOOST_CHECK_EQUAL(e.isExcluded(digest0), false);
384 BOOST_CHECK_EQUAL(e.isExcluded(digest7), true);
385 BOOST_CHECK_EQUAL(e.isExcluded(digest8), true);
386 BOOST_CHECK_EQUAL(e.isExcluded(digest9), true);
387 BOOST_CHECK_EQUAL(e.isExcluded(digestF), false);
388 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), false);
389 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false);
390}
391
392BOOST_AUTO_TEST_CASE(RangeDigestReverse)
393{
394 name::Component digest7 = makeComponent(0x77, true);
395 name::Component digest9 = makeComponent(0x99, true);
396
397 Exclude e;
398 BOOST_CHECK_THROW(e.excludeRange(digest9, digest7), Exclude::Error);
399}
400
401BOOST_AUTO_TEST_CASE(RangeDigestGeneric)
402{
403 name::Component digest0 = makeComponent(0x00, true);
404 name::Component digest7 = makeComponent(0x77, true);
405 name::Component digest9 = makeComponent(0x99, true);
406 name::Component digestF = makeComponent(0xFF, true);
407
408 Exclude e;
409 e.excludeRange(digest9, name::Component(""));
410 BOOST_CHECK_EQUAL(e.isExcluded(digest0), false);
411 BOOST_CHECK_EQUAL(e.isExcluded(digest7), false);
412 BOOST_CHECK_EQUAL(e.isExcluded(digest9), true);
413 BOOST_CHECK_EQUAL(e.isExcluded(digestF), true);
414 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
415 BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false);
Junxiao Shib80e9472016-07-20 13:45:24 +0000416
417 BOOST_CHECK_EQUAL(e.size(), 1);
418 std::vector<Exclude::Range> enumerated;
419 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
420 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
421 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, false);
422 BOOST_CHECK_EQUAL(enumerated[0].from, digest9);
423 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
424 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component(""));
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000425}
426
427BOOST_AUTO_TEST_CASE(RangeGenericDigest)
428{
429 name::Component digestF = makeComponent(0xFF, true);
430
431 Exclude e;
432 BOOST_CHECK_THROW(e.excludeRange(name::Component(""), digestF), Exclude::Error);
433}
434
435BOOST_AUTO_TEST_SUITE_END() // ImplicitDigest
436
437
438BOOST_AUTO_TEST_SUITE(WireCompare) // wireEncode, wireDecode, operator==, operator!=
439
440BOOST_AUTO_TEST_CASE(EqualityComparable)
441{
442 Exclude e1;
443 Exclude e2;
444 BOOST_CHECK_EQUAL(e1, e2);
445
446 e1.excludeOne(name::Component("T"));
447 BOOST_CHECK_NE(e1, e2);
448
449 e2.excludeOne(name::Component("D"));
450 BOOST_CHECK_NE(e1, e2);
451
452 e2.clear();
453 e2.excludeOne(name::Component("T"));
454 BOOST_CHECK_EQUAL(e1, e2);
455
456 e2.clear();
457 const uint8_t EXCLUDE[] = { 0x10, 0x15, 0x13, 0x00, 0x08, 0x01, 0x41, 0x08, 0x01, 0x42,
458 0x08, 0x01, 0x43, 0x13, 0x00, 0x08, 0x01, 0x44, 0x08, 0x01,
459 0x45, 0x13, 0x00 };
460 e2.wireDecode(Block(EXCLUDE, sizeof(EXCLUDE)));
461
462 e1.clear();
463 e1.excludeBefore(name::Component("A"));
464 e1.excludeOne(name::Component("B"));
465 e1.excludeRange(name::Component("C"), name::Component("D"));
466 e1.excludeAfter(name::Component("E"));
467 BOOST_CHECK_EQUAL(e1, e2);
468}
469
Junxiao Shi7284a402014-09-12 13:42:16 -0700470BOOST_AUTO_TEST_CASE(Malformed)
471{
472 Exclude e1;
473 BOOST_CHECK_THROW(e1.wireEncode(), Exclude::Error);
474
475 Exclude e2;
476
477 // top-level TLV-TYPE is not tlv::Exclude
478 const uint8_t NON_EXCLUDE[] = { 0x01, 0x02, 0x13, 0x00 };
479 BOOST_CHECK_THROW(e2.wireDecode(Block(NON_EXCLUDE, sizeof(NON_EXCLUDE))),
480 tlv::Error);
481
482 // Exclude element is empty
483 const uint8_t EMPTY_EXCLUDE[] = { 0x10, 0x00 };
484 BOOST_CHECK_THROW(e2.wireDecode(Block(EMPTY_EXCLUDE, sizeof(EMPTY_EXCLUDE))),
485 Exclude::Error);
486
487 // Exclude element contains unknown element
488 const uint8_t UNKNOWN_COMP1[] = { 0x10, 0x02, 0xAA, 0x00 };
489 BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP1, sizeof(UNKNOWN_COMP1))),
490 Exclude::Error);
491
492 // Exclude element contains unknown element
493 const uint8_t UNKNOWN_COMP2[] = { 0x10, 0x05, 0x08, 0x01, 0x54, 0xAA, 0x00 };
494 BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP2, sizeof(UNKNOWN_COMP2))),
495 Exclude::Error);
496
497 // // <Exclude><Any/></Exclude>
498 // const uint8_t ONLY_ANY[] = { 0x10, 0x02, 0x13, 0x00 };
499 // BOOST_CHECK_THROW(e2.wireDecode(Block(ONLY_ANY, sizeof(ONLY_ANY))),
500 // Exclude::Error);
501
502 // <Exclude><Any/><Any/></Exclude>
503 const uint8_t ANY_ANY[] = { 0x10, 0x04, 0x13, 0x00, 0x13, 0x00 };
504 BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_ANY, sizeof(ANY_ANY))),
505 Exclude::Error);
506
507 // // <Exclude><Any/><NameComponent>T</NameComponent><Any/></Exclude>
508 // const uint8_t ANY_COMPONENT_ANY[] = { 0x10, 0x07, 0x13, 0x00, 0x08, 0x01, 0x54, 0x13, 0x00 };
509 // BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_COMPONENT_ANY, sizeof(ANY_COMPONENT_ANY))),
510 // Exclude::Error);
Ilya Moiseenko741771f2015-03-10 16:53:44 -0700511
512 uint8_t WIRE[] = {
513 0x10, 0x20, // Exclude
514 0x01, 0x1E, // ImplicitSha256DigestComponent with incorrect length
515 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
516 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
517 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
518 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
519 };
520
521 BOOST_CHECK_THROW(Exclude().wireDecode(Block(WIRE, sizeof(WIRE))), Exclude::Error);
522}
523
524BOOST_AUTO_TEST_CASE(ImplicitSha256Digest)
525{
526 uint8_t WIRE[] = {
527 0x10, 0x22, // Exclude
528 0x01, 0x20, // ImplicitSha256DigestComponent
529 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
530 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
531 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
532 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
533 };
534
535 Block block(WIRE, sizeof(WIRE));
536
537 Exclude exclude;
538 BOOST_CHECK_NO_THROW(exclude.wireDecode(block));
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000539 BOOST_CHECK(exclude.wireEncode() == block);
Junxiao Shi7284a402014-09-12 13:42:16 -0700540}
541
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000542BOOST_AUTO_TEST_CASE(EmptyComponent) // Bug #2660
Alexander Afanasyevc076e6d2015-04-02 20:07:13 -0700543{
544 Exclude e1, e2;
545
546 e1.excludeOne(name::Component());
547 e2.excludeOne(name::Component(""));
548
549 BOOST_CHECK_EQUAL(e1, e2);
550 BOOST_CHECK_EQUAL(e1.toUri(), e2.toUri());
551 BOOST_CHECK(e1.wireEncode() == e2.wireEncode());
552
553 BOOST_CHECK_EQUAL("...", e1.toUri());
554
555 uint8_t WIRE[] {0x10, 0x02, 0x08, 0x00};
556 BOOST_CHECK_EQUAL_COLLECTIONS(e1.wireEncode().begin(), e1.wireEncode().end(),
557 WIRE, WIRE + sizeof(WIRE));
558
559 Exclude e3(Block(WIRE, sizeof(WIRE)));
560 BOOST_CHECK_EQUAL(e1, e3);
561 BOOST_CHECK_EQUAL(e1.toUri(), e3.toUri());
562}
563
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000564BOOST_AUTO_TEST_SUITE_END() // WireCompare
565
Junxiao Shib80e9472016-07-20 13:45:24 +0000566
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800567BOOST_AUTO_TEST_SUITE_END()
568
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800569} // namespace tests
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800570} // namespace ndn