blob: 2485a8236df63dbfd4332a6e5aa73b69f3bf0475 [file] [log] [blame]
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shi5dc75602021-02-19 11:33:00 -07003 * Copyright (c) 2013-2021 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;
Junxiao Shi5dc75602021-02-19 11:33:00 -070038using namespace ndn::security::tests;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080039
40BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080041BOOST_AUTO_TEST_SUITE(ValidatorConfig)
Junxiao Shi5dc75602021-02-19 11:33:00 -070042BOOST_AUTO_TEST_SUITE(TestChecker)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080043
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050044class CheckerFixture : public KeyChainFixture
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080045{
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
Davide Pesavento5437aa22019-03-24 14:02:37 -040055 static Name
Junxiao Shi5dc75602021-02-19 11:33:00 -070056 makeKeyLocatorKeyName(const Name& name)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080057 {
Junxiao Shi5dc75602021-02-19 11:33:00 -070058 static PartialName suffix("KEY/keyid");
59 return Name(name).append(suffix);
60 }
61
62 static Name
63 makeKeyLocatorCertName(const Name& name)
64 {
65 static PartialName suffix("KEY/keyid/issuer/v=1");
66 return Name(name).append(suffix);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080067 }
68
69public:
70 std::vector<Name> names;
71};
72
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080073class NameRelationEqual : public CheckerFixture
74{
75public:
Junxiao Shi5dc75602021-02-19 11:33:00 -070076 NameRelationChecker checker{"/foo/bar", NameRelation::EQUAL};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080077 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
78 {true, false, false, false},
79 {true, false, false, false},
80 {true, false, false, false}};
81};
82
83class NameRelationIsPrefixOf : public CheckerFixture
84{
85public:
Junxiao Shi5dc75602021-02-19 11:33:00 -070086 NameRelationChecker checker{"/foo/bar", NameRelation::IS_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080087 std::vector<std::vector<bool>> outcomes = {{true, true, false, false},
88 {true, true, false, false},
89 {true, true, false, false},
90 {true, true, false, false}};
91};
92
93class NameRelationIsStrictPrefixOf : public CheckerFixture
94{
95public:
Junxiao Shi5dc75602021-02-19 11:33:00 -070096 NameRelationChecker checker{"/foo/bar", NameRelation::IS_STRICT_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080097 std::vector<std::vector<bool>> outcomes = {{false, true, false, false},
98 {false, true, false, false},
99 {false, true, false, false},
100 {false, true, false, false}};
101};
102
103class RegexEqual : public CheckerFixture
104{
105public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700106 RegexChecker checker{Regex("^<foo><bar><KEY><>{1,3}$")};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800107 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
108 {true, false, false, false},
109 {true, false, false, false},
110 {true, false, false, false}};
111};
112
113class RegexIsPrefixOf : public CheckerFixture
114{
115public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700116 RegexChecker checker{Regex("^<foo><bar><>*<KEY><>{1,3}$")};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800117 std::vector<std::vector<bool>> outcomes = {{true, true, false, false},
118 {true, true, false, false},
119 {true, true, false, false},
120 {true, true, false, false}};
121};
122
123class RegexIsStrictPrefixOf : public CheckerFixture
124{
125public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700126 RegexChecker checker{Regex("^<foo><bar><>+<KEY><>{1,3}$")};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800127 std::vector<std::vector<bool>> outcomes = {{false, true, false, false},
128 {false, true, false, false},
129 {false, true, false, false},
130 {false, true, false, false}};
131};
132
133class HyperRelationEqual : public CheckerFixture
134{
135public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700136 HyperRelationChecker checker{"^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::EQUAL};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800137 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
138 {false, true, false, false},
139 {false, false, true, false},
140 {false, false, false, true}};
141};
142
143class HyperRelationIsPrefixOf : public CheckerFixture
144{
145public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700146 HyperRelationChecker checker{"^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800147 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
148 {true, true, true, false},
149 {false, false, true, false},
150 {false, false, false, true}};
151};
152
153class HyperRelationIsStrictPrefixOf : public CheckerFixture
154{
155public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700156 HyperRelationChecker checker{"^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_STRICT_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800157 std::vector<std::vector<bool>> outcomes = {{false, false, true, false},
158 {true, false, true, false},
159 {false, false, false, false},
160 {false, false, false, false}};
161};
162
163class Hierarchical : public CheckerFixture
164{
165public:
166 Hierarchical()
167 : checkerPtr(Checker::create(makeSection(R"CONF(
168 type hierarchical
169 sig-type rsa-sha256
170 )CONF"), "test-config"))
171 , checker(*checkerPtr)
172 {
173 }
174
175public:
176 std::unique_ptr<Checker> checkerPtr;
177 Checker& checker;
178
179 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
180 {true, true, true, false},
181 {false, false, true, false},
182 {false, false, false, true}};
183};
184
185class CustomizedNameRelation : public CheckerFixture
186{
187public:
188 CustomizedNameRelation()
189 : checkerPtr(Checker::create(makeSection(R"CONF(
190 type customized
191 sig-type rsa-sha256
192 key-locator
193 {
194 type name
195 name /foo/bar
196 relation equal
197 }
198 )CONF"), "test-config"))
199 , checker(*checkerPtr)
200 {
201 }
202
203public:
204 std::unique_ptr<Checker> checkerPtr;
205 Checker& checker;
206
207 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
208 {true, false, false, false},
209 {true, false, false, false},
210 {true, false, false, false}};
211};
212
213class CustomizedRegex : public CheckerFixture
214{
215public:
216 CustomizedRegex()
217 : checkerPtr(Checker::create(makeSection(R"CONF(
218 type customized
219 sig-type rsa-sha256
220 key-locator
221 {
222 type name
Junxiao Shi5dc75602021-02-19 11:33:00 -0700223 regex ^<foo><bar><KEY><>{1,3}$
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800224 }
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, false, false},
235 {true, false, false, false},
236 {true, false, false, false},
237 {true, false, false, false}};
238};
239
240class CustomizedHyperRelation : public CheckerFixture
241{
242public:
243 CustomizedHyperRelation()
244 : checkerPtr(Checker::create(makeSection(R"CONF(
245 type customized
246 sig-type rsa-sha256
247 key-locator
248 {
249 type name
250 hyper-relation
251 {
Junxiao Shi5dc75602021-02-19 11:33:00 -0700252 k-regex ^(<>+)<KEY><>{1,3}$
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800253 k-expand \\1
254 h-relation is-prefix-of
255 p-regex ^(<>+)$
256 p-expand \\1
257 }
258 }
259 )CONF"), "test-config"))
260 , checker(*checkerPtr)
261 {
262 }
263
264public:
265 std::unique_ptr<Checker> checkerPtr;
266 Checker& checker;
267
268 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
269 {true, true, true, false},
270 {false, false, true, false},
271 {false, false, false, true}};
272};
273
Junxiao Shi5dc75602021-02-19 11:33:00 -0700274using CheckerFixtures = boost::mpl::vector<
275 NameRelationEqual,
276 NameRelationIsPrefixOf,
277 NameRelationIsStrictPrefixOf,
278 RegexEqual,
279 RegexIsPrefixOf,
280 RegexIsStrictPrefixOf,
281 HyperRelationEqual,
282 HyperRelationIsPrefixOf,
283 HyperRelationIsStrictPrefixOf,
284 Hierarchical,
285 CustomizedNameRelation,
286 CustomizedRegex,
287 CustomizedHyperRelation
288>;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800289
Junxiao Shi5dc75602021-02-19 11:33:00 -0700290// Cartesian product of [DataPkt, InterestV02Pkt, InterestV03Pkt] and CheckerFixtures.
291// Each element is a boost::mpl::pair<PktType, CheckerFixture>.
292using Tests = boost::mpl::fold<
293 CheckerFixtures,
294 boost::mpl::vector<>,
295 boost::mpl::push_back<boost::mpl::push_back<boost::mpl::push_back<boost::mpl::_1,
296 boost::mpl::pair<DataPkt, boost::mpl::_2>>,
297 boost::mpl::pair<InterestV02Pkt, boost::mpl::_2>>,
298 boost::mpl::pair<InterestV03Pkt, boost::mpl::_2>>
299>::type;
300
301BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checks, T, Tests, T::second)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800302{
Junxiao Shi5dc75602021-02-19 11:33:00 -0700303 using PktType = typename T::first;
Davide Pesavento5437aa22019-03-24 14:02:37 -0400304
305 BOOST_REQUIRE_EQUAL(this->outcomes.size(), this->names.size());
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800306 for (size_t i = 0; i < this->names.size(); ++i) {
Davide Pesavento5437aa22019-03-24 14:02:37 -0400307 BOOST_REQUIRE_EQUAL(this->outcomes[i].size(), this->names.size());
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800308 for (size_t j = 0; j < this->names.size(); ++j) {
Eric Newberry17d7c472020-06-18 21:29:22 -0700309 auto pktName = PktType::makeName(this->names[i], this->m_keyChain);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800310 bool expectedOutcome = this->outcomes[i][j];
311
Junxiao Shi5dc75602021-02-19 11:33:00 -0700312 {
313 auto klName = this->makeKeyLocatorKeyName(this->names[j]);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800314
Junxiao Shi5dc75602021-02-19 11:33:00 -0700315 auto state = PktType::makeState();
316 BOOST_CHECK_EQUAL(this->checker.check(PktType::getType(), pktName, klName, state), expectedOutcome);
317 BOOST_CHECK_EQUAL(boost::logic::indeterminate(state->getOutcome()), expectedOutcome);
318 BOOST_CHECK_EQUAL(bool(state->getOutcome()), false);
319 }
Eric Newberry17d7c472020-06-18 21:29:22 -0700320
Junxiao Shi5dc75602021-02-19 11:33:00 -0700321 {
322 auto klName = this->makeKeyLocatorCertName(this->names[j]);
Eric Newberry17d7c472020-06-18 21:29:22 -0700323
Junxiao Shi5dc75602021-02-19 11:33:00 -0700324 auto state = PktType::makeState();
325 BOOST_CHECK_EQUAL(this->checker.check(PktType::getType(), pktName, klName, state), expectedOutcome);
326 BOOST_CHECK_EQUAL(boost::logic::indeterminate(state->getOutcome()), expectedOutcome);
327 BOOST_CHECK_EQUAL(bool(state->getOutcome()), false);
328 }
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800329 }
330 }
331}
332
333BOOST_AUTO_TEST_SUITE_END() // TestChecker
334BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800335BOOST_AUTO_TEST_SUITE_END() // Security
336
337} // namespace tests
338} // namespace validator_config
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400339} // inline namespace v2
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800340} // namespace security
341} // namespace ndn