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