blob: ce2892e45a64acd4f65e660c02ea7d3ebd0e3b7f [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/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 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"
23
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080025
26namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080027namespace tests {
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080028
29BOOST_AUTO_TEST_SUITE(TestExclude)
30
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070031BOOST_AUTO_TEST_CASE(Basic)
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080032{
33 Exclude e;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070034 e.excludeOne(name::Component("b"));
35 BOOST_CHECK_EQUAL(e.size(), 1);
36 BOOST_CHECK_EQUAL(e.toUri(), "b");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080037
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070038 e.excludeOne(name::Component("d"));
39 BOOST_CHECK_EQUAL(e.size(), 2);
40 BOOST_CHECK_EQUAL(e.toUri(), "b,d");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080041
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070042 e.excludeOne(name::Component("a"));
43 BOOST_CHECK_EQUAL(e.size(), 3);
44 BOOST_CHECK_EQUAL(e.toUri(), "a,b,d");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080045
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070046 e.excludeOne(name::Component("aa"));
47 BOOST_CHECK_EQUAL(e.size(), 4);
48 BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080049
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070050 e.excludeOne(name::Component("cc"));
51 BOOST_CHECK_EQUAL(e.size(), 5);
52 BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa,cc");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080053
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070054 e.excludeOne(name::Component("c"));
55 BOOST_CHECK_EQUAL(e.size(), 6);
56 BOOST_CHECK_EQUAL(e.toUri(), "a,b,c,d,aa,cc");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080057}
58
Alexander Afanasyevba096052014-09-19 15:36:37 -070059BOOST_AUTO_TEST_CASE(EqualityComparable)
60{
61 Exclude e1;
62 Exclude e2;
63 BOOST_CHECK_EQUAL(e1, e2);
64
65 e1.excludeOne(name::Component("T"));
66 BOOST_CHECK_NE(e1, e2);
67
68 e2.excludeOne(name::Component("D"));
69 BOOST_CHECK_NE(e1, e2);
70
71 e2.clear();
72 e2.excludeOne(name::Component("T"));
73 BOOST_CHECK_EQUAL(e1, e2);
74
75 e2.clear();
76 const uint8_t EXCLUDE[] = { 0x10, 0x15, 0x13, 0x00, 0x08, 0x01, 0x41, 0x08, 0x01, 0x42,
77 0x08, 0x01, 0x43, 0x13, 0x00, 0x08, 0x01, 0x44, 0x08, 0x01,
78 0x45, 0x13, 0x00 };
79 e2.wireDecode(Block(EXCLUDE, sizeof(EXCLUDE)));
80
81 e1.clear();
82 e1.excludeBefore(name::Component("A"));
83 e1.excludeOne(name::Component("B"));
84 e1.excludeRange(name::Component("C"), name::Component("D"));
85 e1.excludeAfter(name::Component("E"));
86 BOOST_CHECK_EQUAL(e1, e2);
87}
88
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070089BOOST_AUTO_TEST_CASE(Before)
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080090{
91 // based on http://redmine.named-data.net/issues/1158
92 ndn::Exclude e;
93 BOOST_REQUIRE_NO_THROW(e.excludeBefore(name::Component("PuQxMaf91")));
94
95 BOOST_CHECK_EQUAL(e.toUri(), "*,PuQxMaf91");
96}
97
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070098BOOST_AUTO_TEST_CASE(Ranges)
Alexander Afanasyev035c7b42014-02-06 18:26:19 -080099{
100// example: ANY /b /d ANY /f
101
102 Exclude e;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700103 e.excludeOne(name::Component("b0"));
104 BOOST_CHECK_EQUAL(e.size(), 1);
105 BOOST_CHECK_EQUAL(e.toUri(), "b0");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800106
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700107 e.excludeRange(name::Component(), name::Component("b1"));
108 BOOST_CHECK_EQUAL(e.size(), 2);
109 BOOST_CHECK_EQUAL(e.toUri(), "*,b1");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800110
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700111 e.excludeRange(name::Component(), name::Component("c0"));
112 BOOST_CHECK_EQUAL(e.size(), 2);
113 BOOST_CHECK_EQUAL(e.toUri(), "*,c0");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800114
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700115 e.excludeRange(name::Component("a0"), name::Component("c0"));
116 BOOST_CHECK_EQUAL(e.size(), 2);
117 BOOST_CHECK_EQUAL(e.toUri(), "*,c0");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800118
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700119 e.excludeRange(name::Component("d0"), name::Component("e0"));
120 BOOST_CHECK_EQUAL(e.size(), 4);
121 BOOST_CHECK_EQUAL(e.toUri(), "*,c0,d0,*,e0");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800122
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700123 e.excludeRange(name::Component("c1"), name::Component("d1"));
124 BOOST_CHECK_EQUAL(e.size(), 4);
125 BOOST_CHECK_EQUAL(e.toUri(), "*,c0,c1,*,e0");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800126
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700127 e.excludeRange(name::Component("a1"), name::Component("d1"));
128 BOOST_CHECK_EQUAL(e.size(), 2);
129 BOOST_CHECK_EQUAL(e.toUri(), "*,e0");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800130
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700131 e.excludeBefore(name::Component("e2"));
132 BOOST_CHECK_EQUAL(e.size(), 2);
133 BOOST_CHECK_EQUAL(e.toUri(), "*,e2");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800134
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700135 e.excludeAfter(name::Component("f0"));
136 BOOST_CHECK_EQUAL(e.size(), 3);
137 BOOST_CHECK_EQUAL(e.toUri(), "*,e2,f0,*");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800138
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700139 e.excludeAfter(name::Component("e5"));
140 BOOST_CHECK_EQUAL(e.size(), 3);
141 BOOST_CHECK_EQUAL(e.toUri(), "*,e2,e5,*");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800142
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700143 e.excludeAfter(name::Component("b2"));
144 BOOST_CHECK_EQUAL(e.size(), 1);
145 BOOST_CHECK_EQUAL(e.toUri(), "*");
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800146
Junxiao Shi75203022014-09-11 10:01:50 -0700147 BOOST_REQUIRE_THROW(e.excludeRange(name::Component("d0"), name::Component("a0")),
148 Exclude::Error);
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800149}
150
Junxiao Shi7284a402014-09-12 13:42:16 -0700151BOOST_AUTO_TEST_CASE(Malformed)
152{
153 Exclude e1;
154 BOOST_CHECK_THROW(e1.wireEncode(), Exclude::Error);
155
156 Exclude e2;
157
158 // top-level TLV-TYPE is not tlv::Exclude
159 const uint8_t NON_EXCLUDE[] = { 0x01, 0x02, 0x13, 0x00 };
160 BOOST_CHECK_THROW(e2.wireDecode(Block(NON_EXCLUDE, sizeof(NON_EXCLUDE))),
161 tlv::Error);
162
163 // Exclude element is empty
164 const uint8_t EMPTY_EXCLUDE[] = { 0x10, 0x00 };
165 BOOST_CHECK_THROW(e2.wireDecode(Block(EMPTY_EXCLUDE, sizeof(EMPTY_EXCLUDE))),
166 Exclude::Error);
167
168 // Exclude element contains unknown element
169 const uint8_t UNKNOWN_COMP1[] = { 0x10, 0x02, 0xAA, 0x00 };
170 BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP1, sizeof(UNKNOWN_COMP1))),
171 Exclude::Error);
172
173 // Exclude element contains unknown element
174 const uint8_t UNKNOWN_COMP2[] = { 0x10, 0x05, 0x08, 0x01, 0x54, 0xAA, 0x00 };
175 BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP2, sizeof(UNKNOWN_COMP2))),
176 Exclude::Error);
177
178 // // <Exclude><Any/></Exclude>
179 // const uint8_t ONLY_ANY[] = { 0x10, 0x02, 0x13, 0x00 };
180 // BOOST_CHECK_THROW(e2.wireDecode(Block(ONLY_ANY, sizeof(ONLY_ANY))),
181 // Exclude::Error);
182
183 // <Exclude><Any/><Any/></Exclude>
184 const uint8_t ANY_ANY[] = { 0x10, 0x04, 0x13, 0x00, 0x13, 0x00 };
185 BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_ANY, sizeof(ANY_ANY))),
186 Exclude::Error);
187
188 // // <Exclude><Any/><NameComponent>T</NameComponent><Any/></Exclude>
189 // const uint8_t ANY_COMPONENT_ANY[] = { 0x10, 0x07, 0x13, 0x00, 0x08, 0x01, 0x54, 0x13, 0x00 };
190 // BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_COMPONENT_ANY, sizeof(ANY_COMPONENT_ANY))),
191 // Exclude::Error);
Ilya Moiseenko741771f2015-03-10 16:53:44 -0700192
193 uint8_t WIRE[] = {
194 0x10, 0x20, // Exclude
195 0x01, 0x1E, // ImplicitSha256DigestComponent with incorrect length
196 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
197 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
198 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
199 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
200 };
201
202 BOOST_CHECK_THROW(Exclude().wireDecode(Block(WIRE, sizeof(WIRE))), Exclude::Error);
203}
204
205BOOST_AUTO_TEST_CASE(ImplicitSha256Digest)
206{
207 uint8_t WIRE[] = {
208 0x10, 0x22, // Exclude
209 0x01, 0x20, // ImplicitSha256DigestComponent
210 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
211 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
212 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
213 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
214 };
215
216 Block block(WIRE, sizeof(WIRE));
217
218 Exclude exclude;
219 BOOST_CHECK_NO_THROW(exclude.wireDecode(block));
Junxiao Shi7284a402014-09-12 13:42:16 -0700220}
221
Alexander Afanasyevc076e6d2015-04-02 20:07:13 -0700222BOOST_AUTO_TEST_CASE(ExcludeEmptyComponent) // Bug #2660
223{
224 Exclude e1, e2;
225
226 e1.excludeOne(name::Component());
227 e2.excludeOne(name::Component(""));
228
229 BOOST_CHECK_EQUAL(e1, e2);
230 BOOST_CHECK_EQUAL(e1.toUri(), e2.toUri());
231 BOOST_CHECK(e1.wireEncode() == e2.wireEncode());
232
233 BOOST_CHECK_EQUAL("...", e1.toUri());
234
235 uint8_t WIRE[] {0x10, 0x02, 0x08, 0x00};
236 BOOST_CHECK_EQUAL_COLLECTIONS(e1.wireEncode().begin(), e1.wireEncode().end(),
237 WIRE, WIRE + sizeof(WIRE));
238
239 Exclude e3(Block(WIRE, sizeof(WIRE)));
240 BOOST_CHECK_EQUAL(e1, e3);
241 BOOST_CHECK_EQUAL(e1.toUri(), e3.toUri());
242}
243
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800244BOOST_AUTO_TEST_SUITE_END()
245
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800246} // namespace tests
Alexander Afanasyev035c7b42014-02-06 18:26:19 -0800247} // namespace ndn