blob: 0410d9fdb1cced71975bf32ce936c5360bf56786 [file] [log] [blame]
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shi58b9e0f2021-03-18 15:54:07 -06003 * 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/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>
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050043class RuleFixture : public KeyChainFixture
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080044{
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{
Junxiao Shi58b9e0f2021-03-18 15:54:07 -060099 auto testChecker = [this] (const Name& klName, bool expectedOutcome) {
100 BOOST_TEST_CONTEXT(klName << " expected=" << expectedOutcome) {
101 this->state = PktType::makeState(); // reset state
102 BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, klName, this->state),
103 expectedOutcome);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800104
Junxiao Shi58b9e0f2021-03-18 15:54:07 -0600105 auto outcome = this->state->getOutcome();
106 if (expectedOutcome) {
107 BOOST_CHECK(boost::logic::indeterminate(outcome));
108 }
109 else {
110 BOOST_CHECK(!boost::logic::indeterminate(outcome));
111 BOOST_CHECK(!bool(outcome));
112 }
113 }
114 };
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800115
116 this->rule.addChecker(make_unique<HyperRelationChecker>("^(<>+)$", "\\1",
Junxiao Shi58b9e0f2021-03-18 15:54:07 -0600117 "^<always>(<>+)$", "\\1",
118 NameRelation::EQUAL));
119 testChecker("/always/foo/bar", true);
120 testChecker("/seldomly/foo/bar", false);
121 testChecker("/never/foo/bar", false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800122
Junxiao Shi58b9e0f2021-03-18 15:54:07 -0600123 this->rule.addChecker(make_unique<HyperRelationChecker>("^(<>+)$", "\\1",
124 "^<seldomly>(<>+)$", "\\1",
125 NameRelation::EQUAL));
126 testChecker("/always/foo/bar", true);
127 testChecker("/seldomly/foo/bar", true);
128 testChecker("/never/foo/bar", false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800129}
130
131BOOST_AUTO_TEST_SUITE(Create)
132
133BOOST_AUTO_TEST_CASE(Errors)
134{
135 BOOST_CHECK_THROW(Rule::create(makeSection(""), "test-config"), Error);
136
137 std::string config = R"CONF(
138 id rule-id
139 for something
140 )CONF";
141 BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error);
142
143 config = R"CONF(
144 id rule-id
145 for data
146 )CONF";
147 BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error); // at least one checker required
148
149 config = R"CONF(
150 id rule-id
151 for data
152 checker
153 {
154 type hierarchical
155 sig-type rsa-sha256
156 }
157 other stuff
158 )CONF";
159 BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error);
160}
161
Eric Newberry17d7c472020-06-18 21:29:22 -0700162BOOST_FIXTURE_TEST_CASE_TEMPLATE(FilterAndChecker, PktType, PktTypes, RuleFixture<PktType>)
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800163{
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400164 std::string config = R"CONF(
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800165 id rule-id
Eric Newberry17d7c472020-06-18 21:29:22 -0700166 for )CONF" + (PktType::getType() == tlv::Data ? "data"s : "interest"s) + R"CONF(
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800167 filter
168 {
169 type name
170 regex ^<foo><bar>$
171 }
172 checker
173 {
174 type customized
175 sig-type rsa-sha256
176 key-locator
177 {
178 type name
179 hyper-relation
180 {
181 k-regex ^(<>+)$
182 k-expand \\1
183 h-relation equal
184 p-regex ^(<>+)$
185 p-expand \\1
186 }
187 }
188 }
189 )CONF";
190 auto rule = Rule::create(makeSection(config), "test-config");
191
Eric Newberry17d7c472020-06-18 21:29:22 -0700192 BOOST_CHECK_EQUAL(rule->match(PktType::getType(), this->pktName, this->state), true);
193 BOOST_CHECK_EQUAL(rule->match(PktType::getType(), "/not" + this->pktName.toUri(), this->state), false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800194
Eric Newberry17d7c472020-06-18 21:29:22 -0700195 BOOST_CHECK_EQUAL(rule->check(PktType::getType(), this->pktName, "/foo/bar", this->state), true);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800196
Eric Newberry17d7c472020-06-18 21:29:22 -0700197 this->state = PktType::makeState(); // reset state
198 BOOST_CHECK_EQUAL(rule->check(PktType::getType(), this->pktName, "/not/foo/bar", this->state), false);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800199}
200
201BOOST_AUTO_TEST_SUITE_END() // Create
202
203BOOST_AUTO_TEST_SUITE_END() // TestRule
204BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800205BOOST_AUTO_TEST_SUITE_END() // Security
206
207} // namespace tests
208} // namespace validator_config
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400209} // inline namespace v2
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800210} // namespace security
211} // namespace ndn