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/filter.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 23 | #include "ndn-cxx/security/command-interest-signer.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
| 26 | #include "tests/identity-management-fixture.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame^] | 27 | #include "tests/unit/security/validator-config/common.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame^] | 31 | inline namespace v2 { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 32 | namespace validator_config { |
| 33 | namespace tests { |
| 34 | |
| 35 | using namespace ndn::tests; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(ValidatorConfig) |
| 39 | |
| 40 | class FilterFixture : public IdentityManagementFixture |
| 41 | { |
| 42 | public: |
| 43 | Interest |
| 44 | makeSignedInterest(const Name& name) |
| 45 | { |
| 46 | Interest interest(name); |
| 47 | m_keyChain.sign(interest); |
| 48 | return interest; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | BOOST_FIXTURE_TEST_SUITE(TestFilter, FilterFixture) |
| 53 | |
| 54 | #define CHECK_FOR_MATCHES(filter, same, longer, shorter, different) \ |
| 55 | BOOST_CHECK_EQUAL(filter.match(tlv::Interest, makeSignedInterest("/foo/bar").getName()), same); \ |
| 56 | BOOST_CHECK_EQUAL(filter.match(tlv::Data, Data("/foo/bar").getName()), same); \ |
| 57 | BOOST_CHECK_EQUAL(filter.match(tlv::Interest, makeSignedInterest("/foo/bar/bar").getName()), longer); \ |
| 58 | BOOST_CHECK_EQUAL(filter.match(tlv::Data, Data("/foo/bar/bar").getName()), longer); \ |
| 59 | BOOST_CHECK_EQUAL(filter.match(tlv::Interest, makeSignedInterest("/foo").getName()), shorter); \ |
| 60 | BOOST_CHECK_EQUAL(filter.match(tlv::Data, Data("/foo").getName()), shorter); \ |
| 61 | BOOST_CHECK_EQUAL(filter.match(tlv::Interest, makeSignedInterest("/other/prefix").getName()), different); \ |
| 62 | BOOST_CHECK_EQUAL(filter.match(tlv::Data, Data("/other/prefix").getName()), different); |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(RelationName) |
| 65 | { |
| 66 | RelationNameFilter f1("/foo/bar", NameRelation::EQUAL); |
| 67 | CHECK_FOR_MATCHES(f1, true, false, false, false); |
| 68 | |
| 69 | RelationNameFilter f2("/foo/bar", NameRelation::IS_PREFIX_OF); |
| 70 | CHECK_FOR_MATCHES(f2, true, true, false, false); |
| 71 | |
| 72 | RelationNameFilter f3("/foo/bar", NameRelation::IS_STRICT_PREFIX_OF); |
| 73 | CHECK_FOR_MATCHES(f3, false, true, false, false); |
| 74 | } |
| 75 | |
| 76 | BOOST_AUTO_TEST_CASE(RegexName) |
| 77 | { |
| 78 | RegexNameFilter f1(Regex("^<foo><bar>$")); |
| 79 | CHECK_FOR_MATCHES(f1, true, false, false, false); |
| 80 | |
| 81 | RegexNameFilter f2(Regex("^<foo><bar><>*$")); |
| 82 | CHECK_FOR_MATCHES(f2, true, true, false, false); |
| 83 | |
| 84 | RegexNameFilter f3(Regex("^<foo><bar><>+$")); |
| 85 | CHECK_FOR_MATCHES(f3, false, true, false, false); |
| 86 | } |
| 87 | |
| 88 | BOOST_FIXTURE_TEST_SUITE(Create, FilterFixture) |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE(Errors) |
| 91 | { |
| 92 | BOOST_CHECK_THROW(Filter::create(makeSection(""), "test-config"), Error); |
| 93 | BOOST_CHECK_THROW(Filter::create(makeSection("type unknown"), "test-config"), Error); |
| 94 | BOOST_CHECK_THROW(Filter::create(makeSection("type name"), "test-config"), Error); |
| 95 | |
| 96 | std::string config = R"CONF( |
| 97 | type name |
| 98 | not-name-or-regex stuff |
| 99 | )CONF"; |
| 100 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 101 | |
| 102 | config = R"CONF( |
| 103 | type name |
| 104 | name /foo/bar |
| 105 | )CONF"; |
| 106 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 107 | |
| 108 | config = R"CONF( |
| 109 | type name |
| 110 | name /foo/bar |
| 111 | not-relation stuff |
| 112 | )CONF"; |
| 113 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 114 | |
| 115 | config = R"CONF( |
| 116 | type name |
| 117 | name /foo/bar |
| 118 | relation equal |
| 119 | not-end stuff |
| 120 | )CONF"; |
| 121 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 122 | |
| 123 | config = R"CONF( |
| 124 | type name |
| 125 | regex ^<foo><bar>$ |
| 126 | not-end stuff |
| 127 | )CONF"; |
| 128 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(NameFilter) |
| 132 | { |
| 133 | std::string config = R"CONF( |
| 134 | type name |
| 135 | name /foo/bar |
| 136 | relation equal |
| 137 | )CONF"; |
| 138 | auto f1 = Filter::create(makeSection(config), "test-config"); |
| 139 | CHECK_FOR_MATCHES((*f1), true, false, false, false); |
| 140 | |
| 141 | config = R"CONF( |
| 142 | type name |
| 143 | name /foo/bar |
| 144 | relation is-prefix-of |
| 145 | )CONF"; |
| 146 | auto f2 = Filter::create(makeSection(config), "test-config"); |
| 147 | CHECK_FOR_MATCHES((*f2), true, true, false, false); |
| 148 | |
| 149 | config = R"CONF( |
| 150 | type name |
| 151 | name /foo/bar |
| 152 | relation is-strict-prefix-of |
| 153 | )CONF"; |
| 154 | auto f3 = Filter::create(makeSection(config), "test-config"); |
| 155 | CHECK_FOR_MATCHES((*f3), false, true, false, false); |
| 156 | } |
| 157 | |
| 158 | BOOST_AUTO_TEST_CASE(RegexFilter) |
| 159 | { |
| 160 | std::string config = R"CONF( |
| 161 | type name |
| 162 | regex ^<foo><bar>$ |
| 163 | )CONF"; |
| 164 | auto f1 = Filter::create(makeSection(config), "test-config"); |
| 165 | CHECK_FOR_MATCHES((*f1), true, false, false, false); |
| 166 | |
| 167 | config = R"CONF( |
| 168 | type name |
| 169 | regex ^<foo><bar><>*$ |
| 170 | )CONF"; |
| 171 | auto f2 = Filter::create(makeSection(config), "test-config"); |
| 172 | CHECK_FOR_MATCHES((*f2), true, true, false, false); |
| 173 | |
| 174 | config = R"CONF( |
| 175 | type name |
| 176 | regex ^<foo><bar><>+$ |
| 177 | )CONF"; |
| 178 | auto f3 = Filter::create(makeSection(config), "test-config"); |
| 179 | CHECK_FOR_MATCHES((*f3), false, true, false, false); |
| 180 | |
| 181 | config = R"CONF( |
| 182 | type name |
| 183 | regex ^<>*$ |
| 184 | )CONF"; |
| 185 | auto f4 = Filter::create(makeSection(config), "test-config"); |
| 186 | CHECK_FOR_MATCHES((*f4), true, true, true, true); |
| 187 | } |
| 188 | |
| 189 | BOOST_AUTO_TEST_SUITE_END() // Create |
| 190 | |
| 191 | BOOST_AUTO_TEST_SUITE_END() // TestFilter |
| 192 | BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 193 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 194 | |
| 195 | } // namespace tests |
| 196 | } // namespace validator_config |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame^] | 197 | } // inline namespace v2 |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 198 | } // namespace security |
| 199 | } // namespace ndn |