Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2021 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/checker.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 23 | #include "ndn-cxx/security/command-interest-signer.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 24 | #include "ndn-cxx/security/validation-policy.hpp" |
| 25 | #include "ndn-cxx/security/validation-state.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 28 | #include "tests/unit/security/validator-fixture.hpp" |
| 29 | #include "tests/unit/security/validator-config/common.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 33 | inline namespace v2 { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 34 | namespace validator_config { |
| 35 | namespace tests { |
| 36 | |
| 37 | using namespace ndn::tests; |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 38 | using namespace ndn::security::tests; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(ValidatorConfig) |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 42 | BOOST_AUTO_TEST_SUITE(TestChecker) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 43 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 44 | class CheckerFixture : public KeyChainFixture |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 45 | { |
| 46 | public: |
| 47 | CheckerFixture() |
| 48 | { |
| 49 | names.push_back("/foo/bar"); |
| 50 | names.push_back("/foo/bar/bar"); |
| 51 | names.push_back("/foo"); |
| 52 | names.push_back("/other/prefix"); |
| 53 | } |
| 54 | |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 55 | static Name |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 56 | makeKeyLocatorKeyName(const Name& name) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 57 | { |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 58 | static PartialName suffix("KEY/keyid"); |
| 59 | return Name(name).append(suffix); |
| 60 | } |
| 61 | |
| 62 | static Name |
| 63 | makeKeyLocatorCertName(const Name& name) |
| 64 | { |
| 65 | static PartialName suffix("KEY/keyid/issuer/v=1"); |
| 66 | return Name(name).append(suffix); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | public: |
| 70 | std::vector<Name> names; |
| 71 | }; |
| 72 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 73 | class NameRelationEqual : public CheckerFixture |
| 74 | { |
| 75 | public: |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 76 | NameRelationChecker checker{"/foo/bar", NameRelation::EQUAL}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 77 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 78 | {true, false, false, false}, |
| 79 | {true, false, false, false}, |
| 80 | {true, false, false, false}}; |
| 81 | }; |
| 82 | |
| 83 | class NameRelationIsPrefixOf : public CheckerFixture |
| 84 | { |
| 85 | public: |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 86 | NameRelationChecker checker{"/foo/bar", NameRelation::IS_PREFIX_OF}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 87 | std::vector<std::vector<bool>> outcomes = {{true, true, false, false}, |
| 88 | {true, true, false, false}, |
| 89 | {true, true, false, false}, |
| 90 | {true, true, false, false}}; |
| 91 | }; |
| 92 | |
| 93 | class NameRelationIsStrictPrefixOf : public CheckerFixture |
| 94 | { |
| 95 | public: |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 96 | NameRelationChecker checker{"/foo/bar", NameRelation::IS_STRICT_PREFIX_OF}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 97 | std::vector<std::vector<bool>> outcomes = {{false, true, false, false}, |
| 98 | {false, true, false, false}, |
| 99 | {false, true, false, false}, |
| 100 | {false, true, false, false}}; |
| 101 | }; |
| 102 | |
| 103 | class RegexEqual : public CheckerFixture |
| 104 | { |
| 105 | public: |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 106 | RegexChecker checker{Regex("^<foo><bar><KEY><>{1,3}$")}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 107 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 108 | {true, false, false, false}, |
| 109 | {true, false, false, false}, |
| 110 | {true, false, false, false}}; |
| 111 | }; |
| 112 | |
| 113 | class RegexIsPrefixOf : public CheckerFixture |
| 114 | { |
| 115 | public: |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 116 | RegexChecker checker{Regex("^<foo><bar><>*<KEY><>{1,3}$")}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 117 | std::vector<std::vector<bool>> outcomes = {{true, true, false, false}, |
| 118 | {true, true, false, false}, |
| 119 | {true, true, false, false}, |
| 120 | {true, true, false, false}}; |
| 121 | }; |
| 122 | |
| 123 | class RegexIsStrictPrefixOf : public CheckerFixture |
| 124 | { |
| 125 | public: |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 126 | RegexChecker checker{Regex("^<foo><bar><>+<KEY><>{1,3}$")}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 127 | std::vector<std::vector<bool>> outcomes = {{false, true, false, false}, |
| 128 | {false, true, false, false}, |
| 129 | {false, true, false, false}, |
| 130 | {false, true, false, false}}; |
| 131 | }; |
| 132 | |
| 133 | class HyperRelationEqual : public CheckerFixture |
| 134 | { |
| 135 | public: |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 136 | HyperRelationChecker checker{"^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::EQUAL}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 137 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 138 | {false, true, false, false}, |
| 139 | {false, false, true, false}, |
| 140 | {false, false, false, true}}; |
| 141 | }; |
| 142 | |
| 143 | class HyperRelationIsPrefixOf : public CheckerFixture |
| 144 | { |
| 145 | public: |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 146 | HyperRelationChecker checker{"^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_PREFIX_OF}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 147 | std::vector<std::vector<bool>> outcomes = {{true, false, true, false}, |
| 148 | {true, true, true, false}, |
| 149 | {false, false, true, false}, |
| 150 | {false, false, false, true}}; |
| 151 | }; |
| 152 | |
| 153 | class HyperRelationIsStrictPrefixOf : public CheckerFixture |
| 154 | { |
| 155 | public: |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 156 | HyperRelationChecker checker{"^(<>+)$", "\\1", "^(<>+)<KEY><>{1,3}$", "\\1", NameRelation::IS_STRICT_PREFIX_OF}; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 157 | std::vector<std::vector<bool>> outcomes = {{false, false, true, false}, |
| 158 | {true, false, true, false}, |
| 159 | {false, false, false, false}, |
| 160 | {false, false, false, false}}; |
| 161 | }; |
| 162 | |
| 163 | class Hierarchical : public CheckerFixture |
| 164 | { |
| 165 | public: |
| 166 | Hierarchical() |
| 167 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 168 | type hierarchical |
| 169 | sig-type rsa-sha256 |
| 170 | )CONF"), "test-config")) |
| 171 | , checker(*checkerPtr) |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | public: |
| 176 | std::unique_ptr<Checker> checkerPtr; |
| 177 | Checker& checker; |
| 178 | |
| 179 | std::vector<std::vector<bool>> outcomes = {{true, false, true, false}, |
| 180 | {true, true, true, false}, |
| 181 | {false, false, true, false}, |
| 182 | {false, false, false, true}}; |
| 183 | }; |
| 184 | |
| 185 | class CustomizedNameRelation : public CheckerFixture |
| 186 | { |
| 187 | public: |
| 188 | CustomizedNameRelation() |
| 189 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 190 | type customized |
| 191 | sig-type rsa-sha256 |
| 192 | key-locator |
| 193 | { |
| 194 | type name |
| 195 | name /foo/bar |
| 196 | relation equal |
| 197 | } |
| 198 | )CONF"), "test-config")) |
| 199 | , checker(*checkerPtr) |
| 200 | { |
| 201 | } |
| 202 | |
| 203 | public: |
| 204 | std::unique_ptr<Checker> checkerPtr; |
| 205 | Checker& checker; |
| 206 | |
| 207 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 208 | {true, false, false, false}, |
| 209 | {true, false, false, false}, |
| 210 | {true, false, false, false}}; |
| 211 | }; |
| 212 | |
| 213 | class CustomizedRegex : public CheckerFixture |
| 214 | { |
| 215 | public: |
| 216 | CustomizedRegex() |
| 217 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 218 | type customized |
| 219 | sig-type rsa-sha256 |
| 220 | key-locator |
| 221 | { |
| 222 | type name |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 223 | regex ^<foo><bar><KEY><>{1,3}$ |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 224 | } |
| 225 | )CONF"), "test-config")) |
| 226 | , checker(*checkerPtr) |
| 227 | { |
| 228 | } |
| 229 | |
| 230 | public: |
| 231 | std::unique_ptr<Checker> checkerPtr; |
| 232 | Checker& checker; |
| 233 | |
| 234 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 235 | {true, false, false, false}, |
| 236 | {true, false, false, false}, |
| 237 | {true, false, false, false}}; |
| 238 | }; |
| 239 | |
| 240 | class CustomizedHyperRelation : public CheckerFixture |
| 241 | { |
| 242 | public: |
| 243 | CustomizedHyperRelation() |
| 244 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 245 | type customized |
| 246 | sig-type rsa-sha256 |
| 247 | key-locator |
| 248 | { |
| 249 | type name |
| 250 | hyper-relation |
| 251 | { |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 252 | k-regex ^(<>+)<KEY><>{1,3}$ |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 253 | k-expand \\1 |
| 254 | h-relation is-prefix-of |
| 255 | p-regex ^(<>+)$ |
| 256 | p-expand \\1 |
| 257 | } |
| 258 | } |
| 259 | )CONF"), "test-config")) |
| 260 | , checker(*checkerPtr) |
| 261 | { |
| 262 | } |
| 263 | |
| 264 | public: |
| 265 | std::unique_ptr<Checker> checkerPtr; |
| 266 | Checker& checker; |
| 267 | |
| 268 | std::vector<std::vector<bool>> outcomes = {{true, false, true, false}, |
| 269 | {true, true, true, false}, |
| 270 | {false, false, true, false}, |
| 271 | {false, false, false, true}}; |
| 272 | }; |
| 273 | |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 274 | using CheckerFixtures = boost::mpl::vector< |
| 275 | NameRelationEqual, |
| 276 | NameRelationIsPrefixOf, |
| 277 | NameRelationIsStrictPrefixOf, |
| 278 | RegexEqual, |
| 279 | RegexIsPrefixOf, |
| 280 | RegexIsStrictPrefixOf, |
| 281 | HyperRelationEqual, |
| 282 | HyperRelationIsPrefixOf, |
| 283 | HyperRelationIsStrictPrefixOf, |
| 284 | Hierarchical, |
| 285 | CustomizedNameRelation, |
| 286 | CustomizedRegex, |
| 287 | CustomizedHyperRelation |
| 288 | >; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 289 | |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 290 | // Cartesian product of [DataPkt, InterestV02Pkt, InterestV03Pkt] and CheckerFixtures. |
| 291 | // Each element is a boost::mpl::pair<PktType, CheckerFixture>. |
| 292 | using Tests = boost::mpl::fold< |
| 293 | CheckerFixtures, |
| 294 | boost::mpl::vector<>, |
| 295 | boost::mpl::push_back<boost::mpl::push_back<boost::mpl::push_back<boost::mpl::_1, |
| 296 | boost::mpl::pair<DataPkt, boost::mpl::_2>>, |
| 297 | boost::mpl::pair<InterestV02Pkt, boost::mpl::_2>>, |
| 298 | boost::mpl::pair<InterestV03Pkt, boost::mpl::_2>> |
| 299 | >::type; |
| 300 | |
| 301 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checks, T, Tests, T::second) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 302 | { |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 303 | using PktType = typename T::first; |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 304 | |
| 305 | BOOST_REQUIRE_EQUAL(this->outcomes.size(), this->names.size()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 306 | for (size_t i = 0; i < this->names.size(); ++i) { |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 307 | BOOST_REQUIRE_EQUAL(this->outcomes[i].size(), this->names.size()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 308 | for (size_t j = 0; j < this->names.size(); ++j) { |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 309 | auto pktName = PktType::makeName(this->names[i], this->m_keyChain); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 310 | bool expectedOutcome = this->outcomes[i][j]; |
| 311 | |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 312 | { |
| 313 | auto klName = this->makeKeyLocatorKeyName(this->names[j]); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 314 | |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 315 | auto state = PktType::makeState(); |
| 316 | BOOST_CHECK_EQUAL(this->checker.check(PktType::getType(), pktName, klName, state), expectedOutcome); |
| 317 | BOOST_CHECK_EQUAL(boost::logic::indeterminate(state->getOutcome()), expectedOutcome); |
| 318 | BOOST_CHECK_EQUAL(bool(state->getOutcome()), false); |
| 319 | } |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 320 | |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 321 | { |
| 322 | auto klName = this->makeKeyLocatorCertName(this->names[j]); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 323 | |
Junxiao Shi | 5dc7560 | 2021-02-19 11:33:00 -0700 | [diff] [blame^] | 324 | auto state = PktType::makeState(); |
| 325 | BOOST_CHECK_EQUAL(this->checker.check(PktType::getType(), pktName, klName, state), expectedOutcome); |
| 326 | BOOST_CHECK_EQUAL(boost::logic::indeterminate(state->getOutcome()), expectedOutcome); |
| 327 | BOOST_CHECK_EQUAL(bool(state->getOutcome()), false); |
| 328 | } |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | BOOST_AUTO_TEST_SUITE_END() // TestChecker |
| 334 | BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 335 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 336 | |
| 337 | } // namespace tests |
| 338 | } // namespace validator_config |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 339 | } // inline namespace v2 |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 340 | } // namespace security |
| 341 | } // namespace ndn |