blob: dbc81bf39ce3bd4320cfdf61e7ce1319129e0f7a [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/rule.hpp"
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040025#include "tests/unit/security/validator-fixture.hpp"
26#include "tests/unit/security/validator-config/common.hpp"
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080027
28#include <boost/mpl/vector_c.hpp>
29
30namespace ndn {
31namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040032inline namespace v2 {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080033namespace validator_config {
34namespace tests {
35
36using namespace ndn::tests;
37using namespace ndn::security::v2::tests;
38
39BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080040BOOST_AUTO_TEST_SUITE(ValidatorConfig)
41
42template<uint32_t PktType>
43class RuleFixture : public IdentityManagementFixture
44{
45public:
46 RuleFixture()
47 : rule(ruleId, PktType)
48 , pktName("/foo/bar")
49 {
50 if (PktType == tlv::Interest) {
51 pktName = Name("/foo/bar/SigInfo/SigValue");
52 }
53 }
54
55public:
56 const std::string ruleId = "rule-id";
57 Rule rule;
58 Name pktName;
59};
60
61using PktTypes = boost::mpl::vector_c<uint32_t, tlv::Data, tlv::Interest>;
62
63BOOST_AUTO_TEST_SUITE(TestRule)
64
65BOOST_FIXTURE_TEST_CASE(Errors, RuleFixture<tlv::Data>)
66{
67 BOOST_CHECK_THROW(rule.match(tlv::Interest, this->pktName), Error);
68
69 auto state = make_shared<DummyValidationState>();
70 BOOST_CHECK_THROW(rule.check(tlv::Interest, this->pktName, "/foo/bar", state), Error);
71}
72
73BOOST_FIXTURE_TEST_CASE_TEMPLATE(Constructor, PktType, PktTypes, RuleFixture<PktType::value>)
74{
75 BOOST_CHECK_EQUAL(this->rule.getId(), this->ruleId);
76 BOOST_CHECK_EQUAL(this->rule.getPktType(), PktType::value);
77}
78
79BOOST_FIXTURE_TEST_CASE_TEMPLATE(EmptyRule, PktType, PktTypes, RuleFixture<PktType::value>)
80{
81 BOOST_CHECK_EQUAL(this->rule.match(PktType::value, this->pktName), true);
82
83 auto state = make_shared<DummyValidationState>();
84 BOOST_CHECK_EQUAL(this->rule.check(PktType::value, this->pktName, "/foo/bar", state), false);
85}
86
87BOOST_FIXTURE_TEST_CASE_TEMPLATE(Filters, PktType, PktTypes, RuleFixture<PktType::value>)
88{
89 this->rule.addFilter(make_unique<RegexNameFilter>(Regex("^<foo><bar>$")));
90
91 BOOST_CHECK_EQUAL(this->rule.match(PktType::value, this->pktName), true);
92 BOOST_CHECK_EQUAL(this->rule.match(PktType::value, "/not" + this->pktName.toUri()), false);
93
94 this->rule.addFilter(make_unique<RegexNameFilter>(Regex("^<not><foo><bar>$")));
95
96 BOOST_CHECK_EQUAL(this->rule.match(PktType::value, this->pktName), true);
97 BOOST_CHECK_EQUAL(this->rule.match(PktType::value, "/not" + this->pktName.toUri()), true);
98
99 auto state = make_shared<DummyValidationState>();
100 BOOST_CHECK_EQUAL(this->rule.check(PktType::value, this->pktName, "/foo/bar", state), false);
101}
102
103BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checkers, PktType, PktTypes, RuleFixture<PktType::value>)
104{
105 this->rule.addChecker(make_unique<HyperRelationChecker>("^(<>+)$", "\\1",
106 "^<not>?(<>+)$", "\\1",
107 NameRelation::EQUAL));
108
109 auto state = make_shared<DummyValidationState>();
110 BOOST_CHECK_EQUAL(this->rule.check(PktType::value, this->pktName, "/foo/bar", state), true);
111
112 state = make_shared<DummyValidationState>();
113 BOOST_CHECK_EQUAL(this->rule.check(PktType::value, this->pktName, "/not/foo/bar", state), true);
114
115 this->rule.addChecker(make_unique<HyperRelationChecker>("^(<>+)$", "\\1",
116 "^(<>+)$", "\\1",
117 NameRelation::EQUAL));
118 state = make_shared<DummyValidationState>();
119 BOOST_CHECK_EQUAL(this->rule.check(PktType::value, this->pktName, "/foo/bar", state), true);
120
121 state = make_shared<DummyValidationState>();
122 BOOST_CHECK_EQUAL(this->rule.check(PktType::value, this->pktName, "/not/foo/bar", state), false);
123}
124
125BOOST_AUTO_TEST_SUITE(Create)
126
127BOOST_AUTO_TEST_CASE(Errors)
128{
129 BOOST_CHECK_THROW(Rule::create(makeSection(""), "test-config"), Error);
130
131 std::string config = R"CONF(
132 id rule-id
133 for something
134 )CONF";
135 BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error);
136
137 config = R"CONF(
138 id rule-id
139 for data
140 )CONF";
141 BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error); // at least one checker required
142
143 config = R"CONF(
144 id rule-id
145 for data
146 checker
147 {
148 type hierarchical
149 sig-type rsa-sha256
150 }
151 other stuff
152 )CONF";
153 BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error);
154}
155
156BOOST_FIXTURE_TEST_CASE_TEMPLATE(FilterAndChecker, PktType, PktTypes, RuleFixture<PktType::value>)
157{
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400158 std::string config = R"CONF(
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800159 id rule-id
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400160 for )CONF" + (PktType::value == tlv::Data ? "data"s : "interest"s) + R"CONF(
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800161 filter
162 {
163 type name
164 regex ^<foo><bar>$
165 }
166 checker
167 {
168 type customized
169 sig-type rsa-sha256
170 key-locator
171 {
172 type name
173 hyper-relation
174 {
175 k-regex ^(<>+)$
176 k-expand \\1
177 h-relation equal
178 p-regex ^(<>+)$
179 p-expand \\1
180 }
181 }
182 }
183 )CONF";
184 auto rule = Rule::create(makeSection(config), "test-config");
185
186 BOOST_CHECK_EQUAL(rule->match(PktType::value, this->pktName), true);
187 BOOST_CHECK_EQUAL(rule->match(PktType::value, "/not" + this->pktName.toUri()), false);
188
189 auto state = make_shared<DummyValidationState>();
190 BOOST_CHECK_EQUAL(rule->check(PktType::value, this->pktName, "/foo/bar", state), true);
191
192 state = make_shared<DummyValidationState>();
193 BOOST_CHECK_EQUAL(rule->check(PktType::value, this->pktName, "/not/foo/bar", state), false);
194}
195
196BOOST_AUTO_TEST_SUITE_END() // Create
197
198BOOST_AUTO_TEST_SUITE_END() // TestRule
199BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800200BOOST_AUTO_TEST_SUITE_END() // Security
201
202} // namespace tests
203} // namespace validator_config
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400204} // inline namespace v2
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800205} // namespace security
206} // namespace ndn