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" |
| 25 | #include "tests/identity-management-fixture.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 26 | #include "tests/unit/security/validator-config/common.hpp" |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame^] | 27 | #include "tests/unit/security/validator-fixture.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; |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame^] | 36 | using namespace ndn::security::v2::tests; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(ValidatorConfig) |
| 40 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame^] | 41 | BOOST_FIXTURE_TEST_SUITE(TestFilter, IdentityManagementFixture) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 42 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame^] | 43 | #define CHECK_FOR_MATCHES(filter, same, longer, shorter, different) \ |
| 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); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 68 | |
| 69 | BOOST_AUTO_TEST_CASE(RelationName) |
| 70 | { |
| 71 | RelationNameFilter f1("/foo/bar", NameRelation::EQUAL); |
| 72 | CHECK_FOR_MATCHES(f1, true, false, false, false); |
| 73 | |
| 74 | RelationNameFilter f2("/foo/bar", NameRelation::IS_PREFIX_OF); |
| 75 | CHECK_FOR_MATCHES(f2, true, true, false, false); |
| 76 | |
| 77 | RelationNameFilter f3("/foo/bar", NameRelation::IS_STRICT_PREFIX_OF); |
| 78 | CHECK_FOR_MATCHES(f3, false, true, false, false); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(RegexName) |
| 82 | { |
| 83 | RegexNameFilter f1(Regex("^<foo><bar>$")); |
| 84 | CHECK_FOR_MATCHES(f1, true, false, false, false); |
| 85 | |
| 86 | RegexNameFilter f2(Regex("^<foo><bar><>*$")); |
| 87 | CHECK_FOR_MATCHES(f2, true, true, false, false); |
| 88 | |
| 89 | RegexNameFilter f3(Regex("^<foo><bar><>+$")); |
| 90 | CHECK_FOR_MATCHES(f3, false, true, false, false); |
| 91 | } |
| 92 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame^] | 93 | BOOST_FIXTURE_TEST_SUITE(Create, IdentityManagementFixture) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 94 | |
| 95 | BOOST_AUTO_TEST_CASE(Errors) |
| 96 | { |
| 97 | BOOST_CHECK_THROW(Filter::create(makeSection(""), "test-config"), Error); |
| 98 | BOOST_CHECK_THROW(Filter::create(makeSection("type unknown"), "test-config"), Error); |
| 99 | BOOST_CHECK_THROW(Filter::create(makeSection("type name"), "test-config"), Error); |
| 100 | |
| 101 | std::string config = R"CONF( |
| 102 | type name |
| 103 | not-name-or-regex stuff |
| 104 | )CONF"; |
| 105 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 106 | |
| 107 | config = R"CONF( |
| 108 | type name |
| 109 | name /foo/bar |
| 110 | )CONF"; |
| 111 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 112 | |
| 113 | config = R"CONF( |
| 114 | type name |
| 115 | name /foo/bar |
| 116 | not-relation stuff |
| 117 | )CONF"; |
| 118 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 119 | |
| 120 | config = R"CONF( |
| 121 | type name |
| 122 | name /foo/bar |
| 123 | relation equal |
| 124 | not-end stuff |
| 125 | )CONF"; |
| 126 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 127 | |
| 128 | config = R"CONF( |
| 129 | type name |
| 130 | regex ^<foo><bar>$ |
| 131 | not-end stuff |
| 132 | )CONF"; |
| 133 | BOOST_CHECK_THROW(Filter::create(makeSection(config), "test-config"), Error); |
| 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(NameFilter) |
| 137 | { |
| 138 | std::string config = R"CONF( |
| 139 | type name |
| 140 | name /foo/bar |
| 141 | relation equal |
| 142 | )CONF"; |
| 143 | auto f1 = Filter::create(makeSection(config), "test-config"); |
| 144 | CHECK_FOR_MATCHES((*f1), true, false, false, false); |
| 145 | |
| 146 | config = R"CONF( |
| 147 | type name |
| 148 | name /foo/bar |
| 149 | relation is-prefix-of |
| 150 | )CONF"; |
| 151 | auto f2 = Filter::create(makeSection(config), "test-config"); |
| 152 | CHECK_FOR_MATCHES((*f2), true, true, false, false); |
| 153 | |
| 154 | config = R"CONF( |
| 155 | type name |
| 156 | name /foo/bar |
| 157 | relation is-strict-prefix-of |
| 158 | )CONF"; |
| 159 | auto f3 = Filter::create(makeSection(config), "test-config"); |
| 160 | CHECK_FOR_MATCHES((*f3), false, true, false, false); |
| 161 | } |
| 162 | |
| 163 | BOOST_AUTO_TEST_CASE(RegexFilter) |
| 164 | { |
| 165 | std::string config = R"CONF( |
| 166 | type name |
| 167 | regex ^<foo><bar>$ |
| 168 | )CONF"; |
| 169 | auto f1 = Filter::create(makeSection(config), "test-config"); |
| 170 | CHECK_FOR_MATCHES((*f1), true, false, false, false); |
| 171 | |
| 172 | config = R"CONF( |
| 173 | type name |
| 174 | regex ^<foo><bar><>*$ |
| 175 | )CONF"; |
| 176 | auto f2 = Filter::create(makeSection(config), "test-config"); |
| 177 | CHECK_FOR_MATCHES((*f2), true, true, false, false); |
| 178 | |
| 179 | config = R"CONF( |
| 180 | type name |
| 181 | regex ^<foo><bar><>+$ |
| 182 | )CONF"; |
| 183 | auto f3 = Filter::create(makeSection(config), "test-config"); |
| 184 | CHECK_FOR_MATCHES((*f3), false, true, false, false); |
| 185 | |
| 186 | config = R"CONF( |
| 187 | type name |
| 188 | regex ^<>*$ |
| 189 | )CONF"; |
| 190 | auto f4 = Filter::create(makeSection(config), "test-config"); |
| 191 | CHECK_FOR_MATCHES((*f4), true, true, true, true); |
| 192 | } |
| 193 | |
| 194 | BOOST_AUTO_TEST_SUITE_END() // Create |
| 195 | |
| 196 | BOOST_AUTO_TEST_SUITE_END() // TestFilter |
| 197 | BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 198 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 199 | |
| 200 | } // namespace tests |
| 201 | } // namespace validator_config |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 202 | } // inline namespace v2 |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 203 | } // namespace security |
| 204 | } // namespace ndn |