blob: 84901ec9fd7dcb68fbbfce1ede9d669d532cc4b4 [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
Junxiao Shi58b9e0f2021-03-18 15:54:07 -060069 template<typename PktType, typename C>
70 static void
71 testChecker(C& checker, const Name& pktName, const Name& klName, bool expectedOutcome)
72 {
73 BOOST_TEST_CONTEXT("pkt=" << pktName << " kl=" << klName) {
74 auto state = PktType::makeState();
75 auto result = checker.check(PktType::getType(), pktName, klName, *state);
76 BOOST_CHECK_EQUAL(bool(result), expectedOutcome);
77 BOOST_CHECK(boost::logic::indeterminate(state->getOutcome()));
78 if (!result) {
79 BOOST_CHECK_NE(result.getErrorMessage(), "");
80 }
81 }
82 }
83
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080084public:
85 std::vector<Name> names;
86};
87
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080088class NameRelationEqual : public CheckerFixture
89{
90public:
Junxiao Shi5dc75602021-02-19 11:33:00 -070091 NameRelationChecker checker{"/foo/bar", NameRelation::EQUAL};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080092 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
93 {true, false, false, false},
94 {true, false, false, false},
95 {true, false, false, false}};
96};
97
98class NameRelationIsPrefixOf : public CheckerFixture
99{
100public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700101 NameRelationChecker checker{"/foo/bar", NameRelation::IS_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800102 std::vector<std::vector<bool>> outcomes = {{true, true, false, false},
103 {true, true, false, false},
104 {true, true, false, false},
105 {true, true, false, false}};
106};
107
108class NameRelationIsStrictPrefixOf : public CheckerFixture
109{
110public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700111 NameRelationChecker checker{"/foo/bar", NameRelation::IS_STRICT_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800112 std::vector<std::vector<bool>> outcomes = {{false, true, false, false},
113 {false, true, false, false},
114 {false, true, false, false},
115 {false, true, false, false}};
116};
117
118class RegexEqual : public CheckerFixture
119{
120public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700121 RegexChecker checker{Regex("^<foo><bar><KEY><>{1,3}$")};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800122 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
123 {true, false, false, false},
124 {true, false, false, false},
125 {true, false, false, false}};
126};
127
128class RegexIsPrefixOf : public CheckerFixture
129{
130public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700131 RegexChecker checker{Regex("^<foo><bar><>*<KEY><>{1,3}$")};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800132 std::vector<std::vector<bool>> outcomes = {{true, true, false, false},
133 {true, true, false, false},
134 {true, true, false, false},
135 {true, true, false, false}};
136};
137
138class RegexIsStrictPrefixOf : public CheckerFixture
139{
140public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700141 RegexChecker checker{Regex("^<foo><bar><>+<KEY><>{1,3}$")};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800142 std::vector<std::vector<bool>> outcomes = {{false, true, false, false},
143 {false, true, false, false},
144 {false, true, false, false},
145 {false, true, false, false}};
146};
147
148class HyperRelationEqual : public CheckerFixture
149{
150public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700151 HyperRelationChecker checker{"^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::EQUAL};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800152 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
153 {false, true, false, false},
154 {false, false, true, false},
155 {false, false, false, true}};
156};
157
158class HyperRelationIsPrefixOf : public CheckerFixture
159{
160public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700161 HyperRelationChecker checker{"^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800162 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
163 {true, true, true, false},
164 {false, false, true, false},
165 {false, false, false, true}};
166};
167
168class HyperRelationIsStrictPrefixOf : public CheckerFixture
169{
170public:
Junxiao Shi5dc75602021-02-19 11:33:00 -0700171 HyperRelationChecker checker{"^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_STRICT_PREFIX_OF};
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800172 std::vector<std::vector<bool>> outcomes = {{false, false, true, false},
173 {true, false, true, false},
174 {false, false, false, false},
175 {false, false, false, false}};
176};
177
178class Hierarchical : public CheckerFixture
179{
180public:
181 Hierarchical()
182 : checkerPtr(Checker::create(makeSection(R"CONF(
183 type hierarchical
184 sig-type rsa-sha256
185 )CONF"), "test-config"))
186 , checker(*checkerPtr)
187 {
188 }
189
190public:
191 std::unique_ptr<Checker> checkerPtr;
192 Checker& checker;
193
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 CustomizedNameRelation : public CheckerFixture
201{
202public:
203 CustomizedNameRelation()
204 : checkerPtr(Checker::create(makeSection(R"CONF(
205 type customized
206 sig-type rsa-sha256
207 key-locator
208 {
209 type name
210 name /foo/bar
211 relation equal
212 }
213 )CONF"), "test-config"))
214 , checker(*checkerPtr)
215 {
216 }
217
218public:
219 std::unique_ptr<Checker> checkerPtr;
220 Checker& checker;
221
222 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
223 {true, false, false, false},
224 {true, false, false, false},
225 {true, false, false, false}};
226};
227
228class CustomizedRegex : public CheckerFixture
229{
230public:
231 CustomizedRegex()
232 : checkerPtr(Checker::create(makeSection(R"CONF(
233 type customized
234 sig-type rsa-sha256
235 key-locator
236 {
237 type name
Junxiao Shi5dc75602021-02-19 11:33:00 -0700238 regex ^<foo><bar><KEY><>{1,3}$
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800239 }
240 )CONF"), "test-config"))
241 , checker(*checkerPtr)
242 {
243 }
244
245public:
246 std::unique_ptr<Checker> checkerPtr;
247 Checker& checker;
248
249 std::vector<std::vector<bool>> outcomes = {{true, false, false, false},
250 {true, false, false, false},
251 {true, false, false, false},
252 {true, false, false, false}};
253};
254
255class CustomizedHyperRelation : public CheckerFixture
256{
257public:
258 CustomizedHyperRelation()
259 : checkerPtr(Checker::create(makeSection(R"CONF(
260 type customized
261 sig-type rsa-sha256
262 key-locator
263 {
264 type name
265 hyper-relation
266 {
Junxiao Shi5dc75602021-02-19 11:33:00 -0700267 k-regex ^(<>+)<KEY><>{1,3}$
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800268 k-expand \\1
269 h-relation is-prefix-of
270 p-regex ^(<>+)$
271 p-expand \\1
272 }
273 }
274 )CONF"), "test-config"))
275 , checker(*checkerPtr)
276 {
277 }
278
279public:
280 std::unique_ptr<Checker> checkerPtr;
281 Checker& checker;
282
283 std::vector<std::vector<bool>> outcomes = {{true, false, true, false},
284 {true, true, true, false},
285 {false, false, true, false},
286 {false, false, false, true}};
287};
288
Junxiao Shi5dc75602021-02-19 11:33:00 -0700289using CheckerFixtures = boost::mpl::vector<
290 NameRelationEqual,
291 NameRelationIsPrefixOf,
292 NameRelationIsStrictPrefixOf,
293 RegexEqual,
294 RegexIsPrefixOf,
295 RegexIsStrictPrefixOf,
296 HyperRelationEqual,
297 HyperRelationIsPrefixOf,
298 HyperRelationIsStrictPrefixOf,
299 Hierarchical,
300 CustomizedNameRelation,
301 CustomizedRegex,
302 CustomizedHyperRelation
303>;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800304
Junxiao Shi5dc75602021-02-19 11:33:00 -0700305// Cartesian product of [DataPkt, InterestV02Pkt, InterestV03Pkt] and CheckerFixtures.
306// Each element is a boost::mpl::pair<PktType, CheckerFixture>.
307using Tests = boost::mpl::fold<
308 CheckerFixtures,
309 boost::mpl::vector<>,
310 boost::mpl::push_back<boost::mpl::push_back<boost::mpl::push_back<boost::mpl::_1,
311 boost::mpl::pair<DataPkt, boost::mpl::_2>>,
312 boost::mpl::pair<InterestV02Pkt, boost::mpl::_2>>,
313 boost::mpl::pair<InterestV03Pkt, boost::mpl::_2>>
314>::type;
315
316BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checks, T, Tests, T::second)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800317{
Junxiao Shi5dc75602021-02-19 11:33:00 -0700318 using PktType = typename T::first;
Davide Pesavento5437aa22019-03-24 14:02:37 -0400319
320 BOOST_REQUIRE_EQUAL(this->outcomes.size(), this->names.size());
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800321 for (size_t i = 0; i < this->names.size(); ++i) {
Davide Pesavento5437aa22019-03-24 14:02:37 -0400322 BOOST_REQUIRE_EQUAL(this->outcomes[i].size(), this->names.size());
Junxiao Shi58b9e0f2021-03-18 15:54:07 -0600323
324 auto pktName = PktType::makeName(this->names[i], this->m_keyChain);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800325 for (size_t j = 0; j < this->names.size(); ++j) {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800326 bool expectedOutcome = this->outcomes[i][j];
327
Junxiao Shi58b9e0f2021-03-18 15:54:07 -0600328 auto klName = this->makeKeyLocatorKeyName(this->names[j]);
329 this->template testChecker<PktType>(this->checker, pktName, klName, expectedOutcome);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800330
Junxiao Shi58b9e0f2021-03-18 15:54:07 -0600331 klName = this->makeKeyLocatorCertName(this->names[j]);
332 this->template testChecker<PktType>(this->checker, pktName, klName, expectedOutcome);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800333 }
334 }
335}
336
337BOOST_AUTO_TEST_SUITE_END() // TestChecker
338BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800339BOOST_AUTO_TEST_SUITE_END() // Security
340
341} // namespace tests
342} // namespace validator_config
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400343} // inline namespace v2
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800344} // namespace security
345} // namespace ndn