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