blob: 12af61b502953abc4327b69dffbc08d5e9fbacd0 [file] [log] [blame]
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
22#include "security/v2/validator-config/checker.hpp"
23#include "security/command-interest-signer.hpp"
24#include "security/v2/validation-policy.hpp"
25#include "security/v2/validation-state.hpp"
26
27#include "boost-test.hpp"
28#include "common.hpp"
29#include "identity-management-fixture.hpp"
30#include "../validator-fixture.hpp"
31
32namespace ndn {
33namespace security {
34namespace v2 {
35namespace validator_config {
36namespace tests {
37
38using namespace ndn::tests;
39using namespace ndn::security::v2::tests;
40
41BOOST_AUTO_TEST_SUITE(Security)
42BOOST_AUTO_TEST_SUITE(V2)
43BOOST_AUTO_TEST_SUITE(ValidatorConfig)
44
45class CheckerFixture : public IdentityManagementFixture
46{
47public:
48 CheckerFixture()
49 {
50 names.push_back("/foo/bar");
51 names.push_back("/foo/bar/bar");
52 names.push_back("/foo");
53 names.push_back("/other/prefix");
54 }
55
56 Name
57 makeSignedInterestName(const Name& name)
58 {
59 return Name(name).append("SignatureInfo").append("SignatureValue");
60 }
61
62 Name
63 makeKeyLocatorName(const Name& name)
64 {
65 return Name(name).append("KEY").append("v=1");
66 }
67
68public:
69 std::vector<Name> names;
70};
71
72BOOST_FIXTURE_TEST_SUITE(TestChecker, CheckerFixture)
73
74class NameRelationEqual : public CheckerFixture
75{
76public:
77 NameRelationEqual()
78 : checker("/foo/bar", NameRelation::EQUAL)
79 {
80 }
81
82public:
83 NameRelationChecker checker;
84 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
85 {true, false, false, false},
86 {true, false, false, false},
87 {true, false, false, false}};
88};
89
90class NameRelationIsPrefixOf : public CheckerFixture
91{
92public:
93 NameRelationIsPrefixOf()
94 : checker("/foo/bar", NameRelation::IS_PREFIX_OF)
95 {
96 }
97
98public:
99 NameRelationChecker checker;
100 std::vector<std::vector<bool>> outcomes = {{true, true, false, false},
101 {true, true, false, false},
102 {true, true, false, false},
103 {true, true, false, false}};
104};
105
106class NameRelationIsStrictPrefixOf : public CheckerFixture
107{
108public:
109 NameRelationIsStrictPrefixOf()
110 : checker("/foo/bar", NameRelation::IS_STRICT_PREFIX_OF)
111 {
112 }
113
114public:
115 NameRelationChecker checker;
116 std::vector<std::vector<bool>> outcomes = {{false, true, false, false},
117 {false, true, false, false},
118 {false, true, false, false},
119 {false, true, false, false}};
120};
121
122class RegexEqual : public CheckerFixture
123{
124public:
125 RegexEqual()
126 : checker(Regex("^<foo><bar>$"))
127 {
128 }
129
130public:
131 RegexChecker checker;
132 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
133 {true, false, false, false},
134 {true, false, false, false},
135 {true, false, false, false}};
136};
137
138class RegexIsPrefixOf : public CheckerFixture
139{
140public:
141 RegexIsPrefixOf()
142 : checker(Regex("^<foo><bar><>*$"))
143 {
144 }
145
146public:
147 RegexChecker checker;
148 std::vector<std::vector<bool>> outcomes = {{true, true, false, false},
149 {true, true, false, false},
150 {true, true, false, false},
151 {true, true, false, false}};
152};
153
154class RegexIsStrictPrefixOf : public CheckerFixture
155{
156public:
157 RegexIsStrictPrefixOf()
158 : checker(Regex("^<foo><bar><>+$"))
159 {
160 }
161
162public:
163 RegexChecker checker;
164 std::vector<std::vector<bool>> outcomes = {{false, true, false, false},
165 {false, true, false, false},
166 {false, true, false, false},
167 {false, true, false, false}};
168};
169
170class HyperRelationEqual : public CheckerFixture
171{
172public:
173 HyperRelationEqual()
174 : checker("^(<>+)$", "\\1", "^(<>+)<KEY><>$", "\\1", NameRelation::EQUAL)
175 {
176 }
177
178public:
179 HyperRelationChecker checker;
180 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
181 {false, true, false, false},
182 {false, false, true, false},
183 {false, false, false, true}};
184};
185
186class HyperRelationIsPrefixOf : public CheckerFixture
187{
188public:
189 HyperRelationIsPrefixOf()
190 : checker("^(<>+)$", "\\1", "^(<>+)<KEY><>$", "\\1", NameRelation::IS_PREFIX_OF)
191 {
192 }
193
194public:
195 HyperRelationChecker checker;
196 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
197 {true, true, true, false},
198 {false, false, true, false},
199 {false, false, false, true}};
200};
201
202class HyperRelationIsStrictPrefixOf : public CheckerFixture
203{
204public:
205 HyperRelationIsStrictPrefixOf()
206 : checker("^(<>+)$", "\\1", "^(<>+)<KEY><>$", "\\1", NameRelation::IS_STRICT_PREFIX_OF)
207 {
208 }
209
210public:
211 HyperRelationChecker checker;
212 std::vector<std::vector<bool>> outcomes = {{false, false, true, false},
213 {true, false, true, false},
214 {false, false, false, false},
215 {false, false, false, false}};
216};
217
218class Hierarchical : public CheckerFixture
219{
220public:
221 Hierarchical()
222 : checkerPtr(Checker::create(makeSection(R"CONF(
223 type hierarchical
224 sig-type rsa-sha256
225 )CONF"), "test-config"))
226 , checker(*checkerPtr)
227 {
228 }
229
230public:
231 std::unique_ptr<Checker> checkerPtr;
232 Checker& checker;
233
234 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
235 {true, true, true, false},
236 {false, false, true, false},
237 {false, false, false, true}};
238};
239
240class CustomizedNameRelation : public CheckerFixture
241{
242public:
243 CustomizedNameRelation()
244 : checkerPtr(Checker::create(makeSection(R"CONF(
245 type customized
246 sig-type rsa-sha256
247 key-locator
248 {
249 type name
250 name /foo/bar
251 relation equal
252 }
253 )CONF"), "test-config"))
254 , checker(*checkerPtr)
255 {
256 }
257
258public:
259 std::unique_ptr<Checker> checkerPtr;
260 Checker& checker;
261
262 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
263 {true, false, false, false},
264 {true, false, false, false},
265 {true, false, false, false}};
266};
267
268class CustomizedRegex : public CheckerFixture
269{
270public:
271 CustomizedRegex()
272 : checkerPtr(Checker::create(makeSection(R"CONF(
273 type customized
274 sig-type rsa-sha256
275 key-locator
276 {
277 type name
278 regex ^<foo><bar>$
279 }
280 )CONF"), "test-config"))
281 , checker(*checkerPtr)
282 {
283 }
284
285public:
286 std::unique_ptr<Checker> checkerPtr;
287 Checker& checker;
288
289 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
290 {true, false, false, false},
291 {true, false, false, false},
292 {true, false, false, false}};
293};
294
295class CustomizedHyperRelation : public CheckerFixture
296{
297public:
298 CustomizedHyperRelation()
299 : checkerPtr(Checker::create(makeSection(R"CONF(
300 type customized
301 sig-type rsa-sha256
302 key-locator
303 {
304 type name
305 hyper-relation
306 {
307 k-regex ^(<>+)<KEY><>$
308 k-expand \\1
309 h-relation is-prefix-of
310 p-regex ^(<>+)$
311 p-expand \\1
312 }
313 }
314 )CONF"), "test-config"))
315 , checker(*checkerPtr)
316 {
317 }
318
319public:
320 std::unique_ptr<Checker> checkerPtr;
321 Checker& checker;
322
323 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
324 {true, true, true, false},
325 {false, false, true, false},
326 {false, false, false, true}};
327};
328
329
330using Tests = boost::mpl::vector<NameRelationEqual, NameRelationIsPrefixOf, NameRelationIsStrictPrefixOf,
331 RegexEqual, RegexIsPrefixOf, RegexIsStrictPrefixOf,
332 HyperRelationEqual, HyperRelationIsPrefixOf, HyperRelationIsStrictPrefixOf,
333 Hierarchical,
334 CustomizedNameRelation, CustomizedRegex, CustomizedHyperRelation>;
335
336
337BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checks, T, Tests, T)
338{
339 BOOST_ASSERT(this->outcomes.size() == this->names.size());
340 for (size_t i = 0; i < this->names.size(); ++i) {
341 BOOST_ASSERT(this->outcomes[i].size() == this->names.size());
342 for (size_t j = 0; j < this->names.size(); ++j) {
343 const Name& pktName = this->names[i];
344 Name klName = this->makeKeyLocatorName(this->names[j]);
345 bool expectedOutcome = this->outcomes[i][j];
346
347 auto dataState = make_shared<DummyValidationState>();
348 BOOST_CHECK_EQUAL(this->checker.check(tlv::Data, pktName, klName, dataState), expectedOutcome);
349 BOOST_CHECK_EQUAL(boost::logic::indeterminate(dataState->getOutcome()), expectedOutcome);
350 if (boost::logic::indeterminate(dataState->getOutcome()) == !expectedOutcome) {
351 BOOST_CHECK_EQUAL(dataState->getOutcome(), !expectedOutcome);
352 }
353
354 auto interestState = make_shared<DummyValidationState>();
355 BOOST_CHECK_EQUAL(this->checker.check(tlv::Interest, this->makeSignedInterestName(pktName),
356 klName, interestState), expectedOutcome);
357 BOOST_CHECK_EQUAL(boost::logic::indeterminate(interestState->getOutcome()), expectedOutcome);
358 if (boost::logic::indeterminate(interestState->getOutcome()) == !expectedOutcome) {
359 BOOST_CHECK_EQUAL(interestState->getOutcome(), !expectedOutcome);
360 }
361 }
362 }
363}
364
365BOOST_AUTO_TEST_SUITE_END() // TestChecker
366BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
367BOOST_AUTO_TEST_SUITE_END() // V2
368BOOST_AUTO_TEST_SUITE_END() // Security
369
370} // namespace tests
371} // namespace validator_config
372} // namespace v2
373} // namespace security
374} // namespace ndn