blob: da93a8583806d90e158541e8598fed54cfc74f44 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento10b24be2017-07-12 23:23:46 -04002/*
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +00003 * Copyright (c) 2013-2018 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 Shi6938e342017-07-25 21:56:58 +000023#include "util/sha256.hpp"
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080024
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025#include "boost-test.hpp"
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080026
Davide Pesaventoeee3e822016-11-26 19:19:34 +010027#include <boost/lexical_cast.hpp>
28
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080029namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080030namespace tests {
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080031
32BOOST_AUTO_TEST_SUITE(TestExclude)
33
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000034BOOST_AUTO_TEST_SUITE(GenericComponent) // exclude GenericNameComponent
Junxiao Shidf4b24e2016-07-14 21:41:43 +000035
36BOOST_AUTO_TEST_CASE(One)
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080037{
38 Exclude e;
Junxiao Shib80e9472016-07-20 13:45:24 +000039 std::vector<Exclude::Range> enumerated;
40
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070041 e.excludeOne(name::Component("b"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070042 BOOST_CHECK_EQUAL(e.toUri(), "b");
Junxiao Shib80e9472016-07-20 13:45:24 +000043 BOOST_CHECK_EQUAL(e.size(), 1);
44 enumerated.clear();
45 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
46 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
47 BOOST_CHECK_EQUAL(enumerated[0].isSingular(), true);
48 BOOST_CHECK_EQUAL(enumerated[0].from, name::Component("b"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080049
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070050 e.excludeOne(name::Component("d"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070051 BOOST_CHECK_EQUAL(e.toUri(), "b,d");
Junxiao Shib80e9472016-07-20 13:45:24 +000052 BOOST_CHECK_EQUAL(e.size(), 2);
53 enumerated.clear();
54 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
55 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
56 BOOST_CHECK_EQUAL(enumerated[0].isSingular(), true);
57 BOOST_CHECK_EQUAL(enumerated[0].from, name::Component("b"));
58 BOOST_CHECK_EQUAL(enumerated[1].isSingular(), true);
59 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("d"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080060
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070061 e.excludeOne(name::Component("a"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070062 BOOST_CHECK_EQUAL(e.toUri(), "a,b,d");
Junxiao Shib80e9472016-07-20 13:45:24 +000063 BOOST_CHECK_EQUAL(e.size(), 3);
64 enumerated.clear();
65 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
66 BOOST_REQUIRE_EQUAL(enumerated.size(), 3);
67 BOOST_CHECK_EQUAL(enumerated[0].isSingular(), true);
68 BOOST_CHECK_EQUAL(enumerated[0].from, name::Component("a"));
69 BOOST_CHECK_EQUAL(enumerated[1].isSingular(), true);
70 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("b"));
71 BOOST_CHECK_EQUAL(enumerated[2].isSingular(), true);
72 BOOST_CHECK_EQUAL(enumerated[2].from, name::Component("d"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080073
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070074 e.excludeOne(name::Component("aa"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070075 BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa");
Junxiao Shib80e9472016-07-20 13:45:24 +000076 BOOST_CHECK_EQUAL(e.size(), 4);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080077
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070078 e.excludeOne(name::Component("cc"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070079 BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa,cc");
Junxiao Shib80e9472016-07-20 13:45:24 +000080 BOOST_CHECK_EQUAL(e.size(), 5);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080081
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070082 e.excludeOne(name::Component("c"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070083 BOOST_CHECK_EQUAL(e.toUri(), "a,b,c,d,aa,cc");
Junxiao Shib80e9472016-07-20 13:45:24 +000084 BOOST_CHECK_EQUAL(e.size(), 6);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080085}
86
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070087BOOST_AUTO_TEST_CASE(Before)
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080088{
Davide Pesavento10b24be2017-07-12 23:23:46 -040089 // based on https://redmine.named-data.net/issues/1158
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080090 ndn::Exclude e;
91 BOOST_REQUIRE_NO_THROW(e.excludeBefore(name::Component("PuQxMaf91")));
92
93 BOOST_CHECK_EQUAL(e.toUri(), "*,PuQxMaf91");
94}
95
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070096BOOST_AUTO_TEST_CASE(Ranges)
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080097{
Junxiao Shidf4b24e2016-07-14 21:41:43 +000098 // example: ANY /b /d ANY /f
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080099
100 Exclude e;
Junxiao Shib80e9472016-07-20 13:45:24 +0000101 std::vector<Exclude::Range> enumerated;
102
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700103 e.excludeOne(name::Component("b0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700104 BOOST_CHECK_EQUAL(e.toUri(), "b0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000105 BOOST_CHECK_EQUAL(e.size(), 1);
106 enumerated.clear();
107 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
108 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
109 BOOST_CHECK_EQUAL(enumerated[0].isSingular(), true);
110 BOOST_CHECK_EQUAL(enumerated[0].from, name::Component("b0"));
111 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(enumerated[0]), "{b0}");
112 BOOST_CHECK_EQUAL(enumerated[0], (Exclude::Range{false, name::Component("b0"), false, name::Component("b0")}));
113 BOOST_CHECK_NE(enumerated[0], (Exclude::Range{false, name::Component("b0"), false, name::Component("b1")}));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800114
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000115 e.excludeBefore(name::Component("b1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700116 BOOST_CHECK_EQUAL(e.toUri(), "*,b1");
Junxiao Shib80e9472016-07-20 13:45:24 +0000117 BOOST_CHECK_EQUAL(e.size(), 1);
118 enumerated.clear();
119 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
120 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
121 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
122 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
123 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("b1"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800124
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000125 e.excludeBefore(name::Component("c0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700126 BOOST_CHECK_EQUAL(e.toUri(), "*,c0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000127 BOOST_CHECK_EQUAL(e.size(), 1);
128 enumerated.clear();
129 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
130 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
131 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
132 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
133 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("c0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800134
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700135 e.excludeRange(name::Component("a0"), name::Component("c0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700136 BOOST_CHECK_EQUAL(e.toUri(), "*,c0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000137 BOOST_CHECK_EQUAL(e.size(), 1);
138 enumerated.clear();
139 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
140 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
141 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
142 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
143 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("c0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800144
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700145 e.excludeRange(name::Component("d0"), name::Component("e0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700146 BOOST_CHECK_EQUAL(e.toUri(), "*,c0,d0,*,e0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000147 BOOST_CHECK_EQUAL(e.size(), 2);
148 enumerated.clear();
149 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
150 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
151 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
152 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
153 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("c0"));
154 BOOST_CHECK_EQUAL(enumerated[1].fromInfinity, false);
155 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("d0"));
156 BOOST_CHECK_EQUAL(enumerated[1].toInfinity, false);
157 BOOST_CHECK_EQUAL(enumerated[1].to, name::Component("e0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800158
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700159 e.excludeRange(name::Component("c1"), name::Component("d1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700160 BOOST_CHECK_EQUAL(e.toUri(), "*,c0,c1,*,e0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000161 BOOST_CHECK_EQUAL(e.size(), 2);
162 enumerated.clear();
163 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
164 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
165 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
166 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
167 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("c0"));
168 BOOST_CHECK_EQUAL(enumerated[1].fromInfinity, false);
169 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("c1"));
170 BOOST_CHECK_EQUAL(enumerated[1].toInfinity, false);
171 BOOST_CHECK_EQUAL(enumerated[1].to, name::Component("e0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800172
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700173 e.excludeRange(name::Component("a1"), name::Component("d1"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700174 BOOST_CHECK_EQUAL(e.toUri(), "*,e0");
Junxiao Shib80e9472016-07-20 13:45:24 +0000175 BOOST_CHECK_EQUAL(e.size(), 1);
176 enumerated.clear();
177 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
178 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
179 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
180 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
181 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("e0"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800182
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700183 e.excludeBefore(name::Component("e2"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700184 BOOST_CHECK_EQUAL(e.toUri(), "*,e2");
Junxiao Shib80e9472016-07-20 13:45:24 +0000185 BOOST_CHECK_EQUAL(e.size(), 1);
186 enumerated.clear();
187 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
188 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
189 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
190 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
191 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("e2"));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800192
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700193 e.excludeAfter(name::Component("f0"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700194 BOOST_CHECK_EQUAL(e.toUri(), "*,e2,f0,*");
Junxiao Shib80e9472016-07-20 13:45:24 +0000195 BOOST_CHECK_EQUAL(e.size(), 2);
196 enumerated.clear();
197 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
198 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
199 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
200 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
201 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("e2"));
202 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(enumerated[0]), "(-∞,e2]");
203 BOOST_CHECK_EQUAL(enumerated[0], (Exclude::Range{true, name::Component("ignore"), false, name::Component("e2")}));
204 BOOST_CHECK_EQUAL(enumerated[1].fromInfinity, false);
205 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("f0"));
206 BOOST_CHECK_EQUAL(enumerated[1].toInfinity, true);
207 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(enumerated[1]), "[f0,+∞)");
208 BOOST_CHECK_EQUAL(enumerated[1], (Exclude::Range{false, name::Component("f0"), true, name::Component("ignore")}));
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800209
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700210 e.excludeAfter(name::Component("e5"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700211 BOOST_CHECK_EQUAL(e.toUri(), "*,e2,e5,*");
Junxiao Shib80e9472016-07-20 13:45:24 +0000212 BOOST_CHECK_EQUAL(e.size(), 2);
213 enumerated.clear();
214 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
215 BOOST_REQUIRE_EQUAL(enumerated.size(), 2);
216 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
217 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, false);
218 BOOST_CHECK_EQUAL(enumerated[0].to, name::Component("e2"));
219 BOOST_CHECK_EQUAL(enumerated[1].fromInfinity, false);
220 BOOST_CHECK_EQUAL(enumerated[1].from, name::Component("e5"));
221 BOOST_CHECK_EQUAL(enumerated[1].toInfinity, true);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800222
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700223 e.excludeAfter(name::Component("b2"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700224 BOOST_CHECK_EQUAL(e.toUri(), "*");
Junxiao Shib80e9472016-07-20 13:45:24 +0000225 BOOST_CHECK_EQUAL(e.size(), 1);
226 enumerated.clear();
227 std::copy(e.begin(), e.end(), std::back_inserter(enumerated));
228 BOOST_REQUIRE_EQUAL(enumerated.size(), 1);
229 BOOST_CHECK_EQUAL(enumerated[0].fromInfinity, true);
230 BOOST_CHECK_EQUAL(enumerated[0].toInfinity, true);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800231
Junxiao Shi75203022014-09-11 10:01:50 -0700232 BOOST_REQUIRE_THROW(e.excludeRange(name::Component("d0"), name::Component("a0")),
233 Exclude::Error);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800234}
235
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000236BOOST_AUTO_TEST_SUITE_END() // GenericComponent
237
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000238BOOST_AUTO_TEST_SUITE(ImplicitDigest) // exclude ImplicitSha256DigestComponent
239
Davide Pesavento10b24be2017-07-12 23:23:46 -0400240/** \brief make a name::Component with an octet repeated util::Sha256::DIGEST_SIZE times
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000241 * \param octet the octet to fill the component
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000242 * \param isDigest whether to make an ImplicitSha256DigestComponent or a GenericNameComponent
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000243 * \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{
Davide Pesavento10b24be2017-07-12 23:23:46 -0400248 uint8_t wire[util::Sha256::DIGEST_SIZE];
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000249 std::memset(wire, octet, sizeof(wire));
250 if (lastOctet >= 0) {
Davide Pesavento10b24be2017-07-12 23:23:46 -0400251 wire[util::Sha256::DIGEST_SIZE - 1] = static_cast<uint8_t>(lastOctet);
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000252 }
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
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000437BOOST_AUTO_TEST_SUITE(WireCompare) // wireEncode, wireDecode, operator==, operator!=
438
439BOOST_AUTO_TEST_CASE(EqualityComparable)
440{
441 Exclude e1;
442 Exclude e2;
443 BOOST_CHECK_EQUAL(e1, e2);
444
445 e1.excludeOne(name::Component("T"));
446 BOOST_CHECK_NE(e1, e2);
447
448 e2.excludeOne(name::Component("D"));
449 BOOST_CHECK_NE(e1, e2);
450
451 e2.clear();
452 e2.excludeOne(name::Component("T"));
453 BOOST_CHECK_EQUAL(e1, e2);
454
455 e2.clear();
456 const uint8_t EXCLUDE[] = { 0x10, 0x15, 0x13, 0x00, 0x08, 0x01, 0x41, 0x08, 0x01, 0x42,
457 0x08, 0x01, 0x43, 0x13, 0x00, 0x08, 0x01, 0x44, 0x08, 0x01,
458 0x45, 0x13, 0x00 };
459 e2.wireDecode(Block(EXCLUDE, sizeof(EXCLUDE)));
460
461 e1.clear();
462 e1.excludeBefore(name::Component("A"));
463 e1.excludeOne(name::Component("B"));
464 e1.excludeRange(name::Component("C"), name::Component("D"));
465 e1.excludeAfter(name::Component("E"));
466 BOOST_CHECK_EQUAL(e1, e2);
467}
468
Junxiao Shi7284a402014-09-12 13:42:16 -0700469BOOST_AUTO_TEST_CASE(Malformed)
470{
471 Exclude e1;
472 BOOST_CHECK_THROW(e1.wireEncode(), Exclude::Error);
473
474 Exclude e2;
475
476 // top-level TLV-TYPE is not tlv::Exclude
477 const uint8_t NON_EXCLUDE[] = { 0x01, 0x02, 0x13, 0x00 };
478 BOOST_CHECK_THROW(e2.wireDecode(Block(NON_EXCLUDE, sizeof(NON_EXCLUDE))),
479 tlv::Error);
480
481 // Exclude element is empty
482 const uint8_t EMPTY_EXCLUDE[] = { 0x10, 0x00 };
483 BOOST_CHECK_THROW(e2.wireDecode(Block(EMPTY_EXCLUDE, sizeof(EMPTY_EXCLUDE))),
484 Exclude::Error);
485
486 // Exclude element contains unknown element
487 const uint8_t UNKNOWN_COMP1[] = { 0x10, 0x02, 0xAA, 0x00 };
488 BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP1, sizeof(UNKNOWN_COMP1))),
489 Exclude::Error);
490
491 // Exclude element contains unknown element
492 const uint8_t UNKNOWN_COMP2[] = { 0x10, 0x05, 0x08, 0x01, 0x54, 0xAA, 0x00 };
493 BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP2, sizeof(UNKNOWN_COMP2))),
494 Exclude::Error);
495
496 // // <Exclude><Any/></Exclude>
497 // const uint8_t ONLY_ANY[] = { 0x10, 0x02, 0x13, 0x00 };
498 // BOOST_CHECK_THROW(e2.wireDecode(Block(ONLY_ANY, sizeof(ONLY_ANY))),
499 // Exclude::Error);
500
501 // <Exclude><Any/><Any/></Exclude>
502 const uint8_t ANY_ANY[] = { 0x10, 0x04, 0x13, 0x00, 0x13, 0x00 };
503 BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_ANY, sizeof(ANY_ANY))),
504 Exclude::Error);
505
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000506 // // <Exclude><Any/><GenericNameComponent>T</GenericNameComponent><Any/></Exclude>
Junxiao Shi7284a402014-09-12 13:42:16 -0700507 // const uint8_t ANY_COMPONENT_ANY[] = { 0x10, 0x07, 0x13, 0x00, 0x08, 0x01, 0x54, 0x13, 0x00 };
508 // BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_COMPONENT_ANY, sizeof(ANY_COMPONENT_ANY))),
509 // Exclude::Error);
Ilya Moiseenko741771f2015-03-10 16:53:44 -0700510
511 uint8_t WIRE[] = {
512 0x10, 0x20, // Exclude
513 0x01, 0x1E, // ImplicitSha256DigestComponent with incorrect length
514 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
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
518 };
519
520 BOOST_CHECK_THROW(Exclude().wireDecode(Block(WIRE, sizeof(WIRE))), Exclude::Error);
521}
522
523BOOST_AUTO_TEST_CASE(ImplicitSha256Digest)
524{
525 uint8_t WIRE[] = {
526 0x10, 0x22, // Exclude
527 0x01, 0x20, // ImplicitSha256DigestComponent
528 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
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 };
533
534 Block block(WIRE, sizeof(WIRE));
535
536 Exclude exclude;
537 BOOST_CHECK_NO_THROW(exclude.wireDecode(block));
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000538 BOOST_CHECK(exclude.wireEncode() == block);
Junxiao Shi7284a402014-09-12 13:42:16 -0700539}
540
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000541BOOST_AUTO_TEST_CASE(EmptyComponent) // Bug #2660
Alexander Afanasyevc076e6d2015-04-02 20:07:13 -0700542{
543 Exclude e1, e2;
544
545 e1.excludeOne(name::Component());
546 e2.excludeOne(name::Component(""));
547
548 BOOST_CHECK_EQUAL(e1, e2);
549 BOOST_CHECK_EQUAL(e1.toUri(), e2.toUri());
550 BOOST_CHECK(e1.wireEncode() == e2.wireEncode());
551
552 BOOST_CHECK_EQUAL("...", e1.toUri());
553
554 uint8_t WIRE[] {0x10, 0x02, 0x08, 0x00};
555 BOOST_CHECK_EQUAL_COLLECTIONS(e1.wireEncode().begin(), e1.wireEncode().end(),
556 WIRE, WIRE + sizeof(WIRE));
557
558 Exclude e3(Block(WIRE, sizeof(WIRE)));
559 BOOST_CHECK_EQUAL(e1, e3);
560 BOOST_CHECK_EQUAL(e1.toUri(), e3.toUri());
561}
562
Junxiao Shidf4b24e2016-07-14 21:41:43 +0000563BOOST_AUTO_TEST_SUITE_END() // WireCompare
564
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100565BOOST_AUTO_TEST_SUITE_END() // TestExclude
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800566
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800567} // namespace tests
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800568} // namespace ndn