Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/v2/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" |
| 25 | #include "tests/unit/security/v2/validator-fixture.hpp" |
| 26 | #include "tests/unit/security/v2/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 { |
| 32 | namespace v2 { |
| 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) |
| 40 | BOOST_AUTO_TEST_SUITE(V2) |
| 41 | BOOST_AUTO_TEST_SUITE(ValidatorConfig) |
| 42 | |
| 43 | template<uint32_t PktType> |
| 44 | class RuleFixture : public IdentityManagementFixture |
| 45 | { |
| 46 | public: |
| 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 | |
| 56 | public: |
| 57 | const std::string ruleId = "rule-id"; |
| 58 | Rule rule; |
| 59 | Name pktName; |
| 60 | }; |
| 61 | |
| 62 | using PktTypes = boost::mpl::vector_c<uint32_t, tlv::Data, tlv::Interest>; |
| 63 | |
| 64 | BOOST_AUTO_TEST_SUITE(TestRule) |
| 65 | |
| 66 | BOOST_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 | |
| 74 | BOOST_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 | |
| 80 | BOOST_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 | |
| 88 | BOOST_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 | |
| 104 | BOOST_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 | |
| 126 | BOOST_AUTO_TEST_SUITE(Create) |
| 127 | |
| 128 | BOOST_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 | |
| 157 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(FilterAndChecker, PktType, PktTypes, RuleFixture<PktType::value>) |
| 158 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 159 | std::string config = R"CONF( |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 160 | id rule-id |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 161 | for )CONF" + (PktType::value == tlv::Data ? "data"s : "interest"s) + R"CONF( |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 162 | 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 | |
| 197 | BOOST_AUTO_TEST_SUITE_END() // Create |
| 198 | |
| 199 | BOOST_AUTO_TEST_SUITE_END() // TestRule |
| 200 | BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig |
| 201 | BOOST_AUTO_TEST_SUITE_END() // V2 |
| 202 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 203 | |
| 204 | } // namespace tests |
| 205 | } // namespace validator_config |
| 206 | } // namespace v2 |
| 207 | } // namespace security |
| 208 | } // namespace ndn |