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 | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 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 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 28 | #include <boost/mp11/list.hpp> |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 30 | namespace ndn::tests { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 32 | using namespace ndn::security::validator_config; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 33 | |
| 34 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(ValidatorConfig) |
| 36 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 37 | template<class Packet> |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 38 | class RuleFixture : public KeyChainFixture |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 39 | { |
| 40 | public: |
| 41 | RuleFixture() |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 42 | : rule(ruleId, Packet::getType()) |
| 43 | , pktName(Packet::makeName("/foo/bar", m_keyChain)) |
| 44 | , state(Packet::makeState()) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 45 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | public: |
| 49 | const std::string ruleId = "rule-id"; |
| 50 | Rule rule; |
| 51 | Name pktName; |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 52 | shared_ptr<security::ValidationState> state; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 55 | using PktTypes = boost::mp11::mp_list<DataPkt, InterestV02Pkt, InterestV03Pkt>; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 56 | |
| 57 | BOOST_AUTO_TEST_SUITE(TestRule) |
| 58 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 59 | BOOST_FIXTURE_TEST_CASE(Errors, RuleFixture<DataPkt>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 60 | { |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 61 | BOOST_CHECK_THROW(rule.match(tlv::Interest, this->pktName, state), Error); |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 62 | BOOST_CHECK_THROW(rule.check(tlv::Interest, tlv::SignatureSha256WithRsa, |
| 63 | this->pktName, "/foo/bar", state), Error); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 66 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Constructor, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 67 | { |
| 68 | BOOST_CHECK_EQUAL(this->rule.getId(), this->ruleId); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(this->rule.getPktType(), PktType::getType()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 72 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(EmptyRule, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 73 | { |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(this->rule.match(PktType::getType(), this->pktName, this->state), true); |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), tlv::SignatureSha256WithRsa, |
| 76 | this->pktName, "/foo/bar", this->state), false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 79 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Filters, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 80 | { |
| 81 | this->rule.addFilter(make_unique<RegexNameFilter>(Regex("^<foo><bar>$"))); |
| 82 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(this->rule.match(PktType::getType(), this->pktName, this->state), true); |
| 84 | 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] | 85 | |
| 86 | this->rule.addFilter(make_unique<RegexNameFilter>(Regex("^<not><foo><bar>$"))); |
| 87 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(this->rule.match(PktType::getType(), this->pktName, this->state), true); |
| 89 | 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] | 90 | |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), tlv::SignatureSha256WithRsa, |
| 92 | this->pktName, "/foo/bar", this->state), false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 95 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checkers, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 96 | { |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 97 | auto testChecker = [this] (const Name& klName, bool expectedOutcome) { |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 98 | BOOST_TEST_INFO_SCOPE("Name = " << klName); |
| 99 | BOOST_TEST_INFO_SCOPE("Expected = " << expectedOutcome); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 100 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 101 | this->state = PktType::makeState(); // reset state |
| 102 | BOOST_CHECK_EQUAL(this->rule.check(PktType::getType(), tlv::SignatureSha256WithRsa, |
| 103 | this->pktName, klName, this->state), |
| 104 | expectedOutcome); |
| 105 | |
| 106 | auto outcome = this->state->getOutcome(); |
| 107 | if (expectedOutcome) { |
| 108 | BOOST_CHECK(boost::logic::indeterminate(outcome)); |
| 109 | } |
| 110 | else { |
| 111 | BOOST_CHECK(!boost::logic::indeterminate(outcome)); |
| 112 | BOOST_CHECK(!bool(outcome)); |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 113 | } |
| 114 | }; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 116 | this->rule.addChecker(make_unique<HyperRelationChecker>(tlv::SignatureSha256WithRsa, |
| 117 | "^(<>+)$", "\\1", |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 118 | "^<always>(<>+)$", "\\1", |
| 119 | NameRelation::EQUAL)); |
| 120 | testChecker("/always/foo/bar", true); |
| 121 | testChecker("/seldomly/foo/bar", false); |
| 122 | testChecker("/never/foo/bar", false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 123 | |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 124 | this->rule.addChecker(make_unique<HyperRelationChecker>(tlv::SignatureSha256WithRsa, |
| 125 | "^(<>+)$", "\\1", |
Junxiao Shi | 58b9e0f | 2021-03-18 15:54:07 -0600 | [diff] [blame] | 126 | "^<seldomly>(<>+)$", "\\1", |
| 127 | NameRelation::EQUAL)); |
| 128 | testChecker("/always/foo/bar", true); |
| 129 | testChecker("/seldomly/foo/bar", true); |
| 130 | testChecker("/never/foo/bar", false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE(Create) |
| 134 | |
| 135 | BOOST_AUTO_TEST_CASE(Errors) |
| 136 | { |
| 137 | BOOST_CHECK_THROW(Rule::create(makeSection(""), "test-config"), Error); |
| 138 | |
| 139 | std::string config = R"CONF( |
| 140 | id rule-id |
| 141 | for something |
| 142 | )CONF"; |
| 143 | BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error); |
| 144 | |
| 145 | config = R"CONF( |
| 146 | id rule-id |
| 147 | for data |
| 148 | )CONF"; |
| 149 | BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error); // at least one checker required |
| 150 | |
| 151 | config = R"CONF( |
| 152 | id rule-id |
| 153 | for data |
| 154 | checker |
| 155 | { |
| 156 | type hierarchical |
| 157 | sig-type rsa-sha256 |
| 158 | } |
| 159 | other stuff |
| 160 | )CONF"; |
| 161 | BOOST_CHECK_THROW(Rule::create(makeSection(config), "test-config"), Error); |
| 162 | } |
| 163 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 164 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(FilterAndChecker, PktType, PktTypes, RuleFixture<PktType>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 165 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 166 | std::string config = R"CONF( |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 167 | id rule-id |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 168 | for )CONF" + (PktType::getType() == tlv::Data ? "data"s : "interest"s) + R"CONF( |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 169 | filter |
| 170 | { |
| 171 | type name |
| 172 | regex ^<foo><bar>$ |
| 173 | } |
| 174 | checker |
| 175 | { |
| 176 | type customized |
| 177 | sig-type rsa-sha256 |
| 178 | key-locator |
| 179 | { |
| 180 | type name |
| 181 | hyper-relation |
| 182 | { |
| 183 | k-regex ^(<>+)$ |
| 184 | k-expand \\1 |
| 185 | h-relation equal |
| 186 | p-regex ^(<>+)$ |
| 187 | p-expand \\1 |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | )CONF"; |
| 192 | auto rule = Rule::create(makeSection(config), "test-config"); |
| 193 | |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 194 | BOOST_CHECK(rule->match(PktType::getType(), this->pktName, this->state)); |
| 195 | BOOST_CHECK(!rule->match(PktType::getType(), "/not" + this->pktName.toUri(), this->state)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 196 | |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 197 | BOOST_CHECK(rule->check(PktType::getType(), tlv::SignatureSha256WithRsa, this->pktName, "/foo/bar", this->state)); |
| 198 | BOOST_CHECK(!rule->check(PktType::getType(), tlv::SignatureSha256WithEcdsa, this->pktName, "/foo/bar", this->state)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 199 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 200 | this->state = PktType::makeState(); // reset state |
Alexander Afanasyev | 17d4b93 | 2021-03-17 17:58:40 -0400 | [diff] [blame] | 201 | BOOST_CHECK(!rule->check(PktType::getType(), tlv::SignatureSha256WithRsa, this->pktName, "/not/foo/bar", this->state)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | BOOST_AUTO_TEST_SUITE_END() // Create |
| 205 | |
| 206 | BOOST_AUTO_TEST_SUITE_END() // TestRule |
| 207 | BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 208 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 209 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 210 | } // namespace ndn::tests |