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 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 42 | template<class Packet> |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame^] | 43 | class RuleFixture : public KeyChainFixture |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 44 | { |
| 45 | public: |
| 46 | RuleFixture() |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 47 | : rule(ruleId, Packet::getType()) |
| 48 | , pktName(Packet::makeName("/foo/bar", m_keyChain)) |
| 49 | , state(Packet::makeState()) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 50 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public: |
| 54 | const std::string ruleId = "rule-id"; |
| 55 | Rule rule; |
| 56 | Name pktName; |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 57 | shared_ptr<ValidationState> state; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 60 | using PktTypes = boost::mpl::vector<DataPkt, InterestV02Pkt, InterestV03Pkt>; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 61 | |
| 62 | BOOST_AUTO_TEST_SUITE(TestRule) |
| 63 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 64 | BOOST_FIXTURE_TEST_CASE(Errors, RuleFixture<DataPkt>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 65 | { |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 66 | BOOST_CHECK_THROW(rule.match(tlv::Interest, this->pktName, state), Error); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 67 | BOOST_CHECK_THROW(rule.check(tlv::Interest, this->pktName, "/foo/bar", state), Error); |
| 68 | } |
| 69 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 70 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Constructor, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 71 | { |
| 72 | BOOST_CHECK_EQUAL(this->rule.getId(), this->ruleId); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(this->rule.getPktType(), PktType::getType()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 76 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(EmptyRule, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 77 | { |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 78 | 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 Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 82 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Filters, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 83 | { |
| 84 | this->rule.addFilter(make_unique<RegexNameFilter>(Regex("^<foo><bar>$"))); |
| 85 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 86 | 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 Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 88 | |
| 89 | this->rule.addFilter(make_unique<RegexNameFilter>(Regex("^<not><foo><bar>$"))); |
| 90 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 91 | 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 Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 93 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/foo/bar", this->state), false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 97 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checkers, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 98 | { |
| 99 | this->rule.addChecker(make_unique<HyperRelationChecker>("^(<>+)$", "\\1", |
| 100 | "^<not>?(<>+)$", "\\1", |
| 101 | NameRelation::EQUAL)); |
| 102 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/foo/bar", this->state), true); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 104 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 105 | this->state = PktType::makeState(); // reset state |
| 106 | BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/not/foo/bar", this->state), true); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 107 | |
| 108 | this->rule.addChecker(make_unique<HyperRelationChecker>("^(<>+)$", "\\1", |
| 109 | "^(<>+)$", "\\1", |
| 110 | NameRelation::EQUAL)); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 111 | this->state = PktType::makeState(); // reset state |
| 112 | BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/foo/bar", this->state), true); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 113 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 114 | this->state = PktType::makeState(); // reset state |
| 115 | BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), this->pktName, "/not/foo/bar", this->state), false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_SUITE(Create) |
| 119 | |
| 120 | BOOST_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 Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 149 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(FilterAndChecker, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 150 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 151 | std::string config = R"CONF( |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 152 | id rule-id |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 153 | for )CONF" + (PktType::getType() == tlv::Data ? "data"s : "interest"s) + R"CONF( |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 154 | 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 Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 179 | 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 Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 181 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 182 | BOOST_CHECK_EQUAL(rule->check(PktType::getType(), this->pktName, "/foo/bar", this->state), true); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 183 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 184 | this->state = PktType::makeState(); // reset state |
| 185 | BOOST_CHECK_EQUAL(rule->check(PktType::getType(), this->pktName, "/not/foo/bar", this->state), false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | BOOST_AUTO_TEST_SUITE_END() // Create |
| 189 | |
| 190 | BOOST_AUTO_TEST_SUITE_END() // TestRule |
| 191 | BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 192 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 193 | |
| 194 | } // namespace tests |
| 195 | } // namespace validator_config |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 196 | } // inline namespace v2 |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 197 | } // namespace security |
| 198 | } // namespace ndn |