Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 Regents of the University of California. |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 4 | * |
| 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 Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 22 | #include "ndn-cxx/security/validator-config/rule.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 25 | #include "tests/unit/security/validator-fixture.hpp" |
| 26 | #include "tests/unit/security/validator-config/common.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 27 | |
| 28 | #include <boost/mpl/vector_c.hpp> |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 32 | inline namespace v2 { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 33 | namespace validator_config { |
| 34 | namespace tests { |
| 35 | |
| 36 | using namespace ndn::tests; |
| 37 | using namespace ndn::security::v2::tests; |
| 38 | |
| 39 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(ValidatorConfig) |
| 41 | |
| 42 | template<uint32_t PktType> |
| 43 | class RuleFixture : public IdentityManagementFixture |
| 44 | { |
| 45 | public: |
| 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 | |
| 55 | public: |
| 56 | const std::string ruleId = "rule-id"; |
| 57 | Rule rule; |
| 58 | Name pktName; |
| 59 | }; |
| 60 | |
| 61 | using PktTypes = boost::mpl::vector_c<uint32_t, tlv::Data, tlv::Interest>; |
| 62 | |
| 63 | BOOST_AUTO_TEST_SUITE(TestRule) |
| 64 | |
| 65 | BOOST_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 | |
| 73 | BOOST_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 | |
| 79 | BOOST_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 | |
| 87 | BOOST_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 | |
| 103 | BOOST_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 | |
| 125 | BOOST_AUTO_TEST_SUITE(Create) |
| 126 | |
| 127 | BOOST_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 | |
| 156 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(FilterAndChecker, PktType, PktTypes, RuleFixture<PktType::value>) |
| 157 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 158 | std::string config = R"CONF( |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 159 | id rule-id |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 160 | for )CONF" + (PktType::value == tlv::Data ? "data"s : "interest"s) + R"CONF( |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 161 | 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 | |
| 196 | BOOST_AUTO_TEST_SUITE_END() // Create |
| 197 | |
| 198 | BOOST_AUTO_TEST_SUITE_END() // TestRule |
| 199 | BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 200 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 201 | |
| 202 | } // namespace tests |
| 203 | } // namespace validator_config |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 204 | } // inline namespace v2 |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 205 | } // namespace security |
| 206 | } // namespace ndn |