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