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