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