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