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