blob: 75eb6dce2dc3d513afc57664b97c279b423c36eb [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
Eric Newberry17d7c472020-06-18 21:29:22 -070042template<class Packet>
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080043class RuleFixture : public IdentityManagementFixture
44{
45public:
46 RuleFixture()
Eric Newberry17d7c472020-06-18 21:29:22 -070047 : rule(ruleId, Packet::getType())
48 , pktName(Packet::makeName("/foo/bar", m_keyChain))
49 , state(Packet::makeState())
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080050 {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080051 }
52
53public:
54 const std::string ruleId = "rule-id";
55 Rule rule;
56 Name pktName;
Eric Newberry17d7c472020-06-18 21:29:22 -070057 shared_ptr<ValidationState> state;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080058};
59
Eric Newberry17d7c472020-06-18 21:29:22 -070060using PktTypes = boost::mpl::vector<DataPkt, InterestV02Pkt, InterestV03Pkt>;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080061
62BOOST_AUTO_TEST_SUITE(TestRule)
63
Eric Newberry17d7c472020-06-18 21:29:22 -070064BOOST_FIXTURE_TEST_CASE(Errors, RuleFixture<DataPkt>)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080065{
Eric Newberry17d7c472020-06-18 21:29:22 -070066 BOOST_CHECK_THROW(rule.match(tlv::Interest, this->pktName, state), Error);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080067 BOOST_CHECK_THROW(rule.check(tlv::Interest, this->pktName, "/foo/bar", state), Error);
68}
69
Eric Newberry17d7c472020-06-18 21:29:22 -070070BOOST_FIXTURE_TEST_CASE_TEMPLATE(Constructor, PktType, PktTypes, RuleFixture<PktType>)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080071{
72 BOOST_CHECK_EQUAL(this->rule.getId(), this->ruleId);
Eric Newberry17d7c472020-06-18 21:29:22 -070073 BOOST_CHECK_EQUAL(this->rule.getPktType(), PktType::getType());
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080074}
75
Eric Newberry17d7c472020-06-18 21:29:22 -070076BOOST_FIXTURE_TEST_CASE_TEMPLATE(EmptyRule, PktType, PktTypes, RuleFixture<PktType>)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080077{
Eric Newberry17d7c472020-06-18 21:29:22 -070078 BOOST_CHECK_EQUAL(this->rule.match(PktType::getType(), this->pktName, this->state), true);
79 BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/foo/bar", this->state), false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080080}
81
Eric Newberry17d7c472020-06-18 21:29:22 -070082BOOST_FIXTURE_TEST_CASE_TEMPLATE(Filters, PktType, PktTypes, RuleFixture<PktType>)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080083{
84 this->rule.addFilter(make_unique<RegexNameFilter>(Regex("^<foo><bar>$")));
85
Eric Newberry17d7c472020-06-18 21:29:22 -070086 BOOST_CHECK_EQUAL(this->rule.match(PktType::getType(), this->pktName, this->state), true);
87 BOOST_CHECK_EQUAL(this->rule.match(PktType::getType(), "/not" + this->pktName.toUri(), this->state), false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080088
89 this->rule.addFilter(make_unique<RegexNameFilter>(Regex("^<not><foo><bar>$")));
90
Eric Newberry17d7c472020-06-18 21:29:22 -070091 BOOST_CHECK_EQUAL(this->rule.match(PktType::getType(), this->pktName, this->state), true);
92 BOOST_CHECK_EQUAL(this->rule.match(PktType::getType(), "/not" + this->pktName.toUri(), this->state), true);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080093
Eric Newberry17d7c472020-06-18 21:29:22 -070094 BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/foo/bar", this->state), false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080095}
96
Eric Newberry17d7c472020-06-18 21:29:22 -070097BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checkers, PktType, PktTypes, RuleFixture<PktType>)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080098{
99 this->rule.addChecker(make_unique<HyperRelationChecker>("^(<>+)$", "\\1",
100 "^<not>?(<>+)$", "\\1",
101 NameRelation::EQUAL));
102
Eric Newberry17d7c472020-06-18 21:29:22 -0700103 BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/foo/bar", this->state), true);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800104
Eric Newberry17d7c472020-06-18 21:29:22 -0700105 this->state = PktType::makeState(); // reset state
106 BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/not/foo/bar", this->state), true);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800107
108 this->rule.addChecker(make_unique<HyperRelationChecker>("^(<>+)$", "\\1",
109 "^(<>+)$", "\\1",
110 NameRelation::EQUAL));
Eric Newberry17d7c472020-06-18 21:29:22 -0700111 this->state = PktType::makeState(); // reset state
112 BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/foo/bar", this->state), true);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800113
Eric Newberry17d7c472020-06-18 21:29:22 -0700114 this->state = PktType::makeState(); // reset state
115 BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/not/foo/bar", this->state), false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800116}
117
118BOOST_AUTO_TEST_SUITE(Create)
119
120BOOST_AUTO_TEST_CASE(Errors)
121{
122 BOOST_CHECK_THROW(Rule::create(makeSection(""), "test-config"), Error);
123
124 std::string config = R"CONF(
125 id rule-id
126 for something
127 )CONF";
128 BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error);
129
130 config = R"CONF(
131 id rule-id
132 for data
133 )CONF";
134 BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error); // at least one checker required
135
136 config = R"CONF(
137 id rule-id
138 for data
139 checker
140 {
141 type hierarchical
142 sig-type rsa-sha256
143 }
144 other stuff
145 )CONF";
146 BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error);
147}
148
Eric Newberry17d7c472020-06-18 21:29:22 -0700149BOOST_FIXTURE_TEST_CASE_TEMPLATE(FilterAndChecker, PktType, PktTypes, RuleFixture<PktType>)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800150{
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400151 std::string config = R"CONF(
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800152 id rule-id
Eric Newberry17d7c472020-06-18 21:29:22 -0700153 for )CONF" + (PktType::getType() == tlv::Data ? "data"s : "interest"s) + R"CONF(
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800154 filter
155 {
156 type name
157 regex ^<foo><bar>$
158 }
159 checker
160 {
161 type customized
162 sig-type rsa-sha256
163 key-locator
164 {
165 type name
166 hyper-relation
167 {
168 k-regex ^(<>+)$
169 k-expand \\1
170 h-relation equal
171 p-regex ^(<>+)$
172 p-expand \\1
173 }
174 }
175 }
176 )CONF";
177 auto rule = Rule::create(makeSection(config), "test-config");
178
Eric Newberry17d7c472020-06-18 21:29:22 -0700179 BOOST_CHECK_EQUAL(rule->match(PktType::getType(), this->pktName, this->state), true);
180 BOOST_CHECK_EQUAL(rule->match(PktType::getType(), "/not" + this->pktName.toUri(), this->state), false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800181
Eric Newberry17d7c472020-06-18 21:29:22 -0700182 BOOST_CHECK_EQUAL(rule->check(PktType::getType(), this->pktName, "/foo/bar", this->state), true);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800183
Eric Newberry17d7c472020-06-18 21:29:22 -0700184 this->state = PktType::makeState(); // reset state
185 BOOST_CHECK_EQUAL(rule->check(PktType::getType(), this->pktName, "/not/foo/bar", this->state), false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800186}
187
188BOOST_AUTO_TEST_SUITE_END() // Create
189
190BOOST_AUTO_TEST_SUITE_END() // TestRule
191BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800192BOOST_AUTO_TEST_SUITE_END() // Security
193
194} // namespace tests
195} // namespace validator_config
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400196} // inline namespace v2
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800197} // namespace security
198} // namespace ndn