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/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; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 38 | |
| 39 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(ValidatorConfig) |
| 41 | |
| 42 | class CheckerFixture : public IdentityManagementFixture |
| 43 | { |
| 44 | public: |
| 45 | CheckerFixture() |
| 46 | { |
| 47 | names.push_back("/foo/bar"); |
| 48 | names.push_back("/foo/bar/bar"); |
| 49 | names.push_back("/foo"); |
| 50 | names.push_back("/other/prefix"); |
| 51 | } |
| 52 | |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 53 | static Name |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 54 | makeSignedInterestName(const Name& name) |
| 55 | { |
| 56 | return Name(name).append("SignatureInfo").append("SignatureValue"); |
| 57 | } |
| 58 | |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 59 | static Name |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 60 | makeKeyLocatorName(const Name& name) |
| 61 | { |
| 62 | return Name(name).append("KEY").append("v=1"); |
| 63 | } |
| 64 | |
| 65 | public: |
| 66 | std::vector<Name> names; |
| 67 | }; |
| 68 | |
| 69 | BOOST_FIXTURE_TEST_SUITE(TestChecker, CheckerFixture) |
| 70 | |
| 71 | class NameRelationEqual : public CheckerFixture |
| 72 | { |
| 73 | public: |
| 74 | NameRelationEqual() |
| 75 | : checker("/foo/bar", NameRelation::EQUAL) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | public: |
| 80 | NameRelationChecker checker; |
| 81 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 82 | {true, false, false, false}, |
| 83 | {true, false, false, false}, |
| 84 | {true, false, false, false}}; |
| 85 | }; |
| 86 | |
| 87 | class NameRelationIsPrefixOf : public CheckerFixture |
| 88 | { |
| 89 | public: |
| 90 | NameRelationIsPrefixOf() |
| 91 | : checker("/foo/bar", NameRelation::IS_PREFIX_OF) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | public: |
| 96 | NameRelationChecker checker; |
| 97 | std::vector<std::vector<bool>> outcomes = {{true, true, false, false}, |
| 98 | {true, true, false, false}, |
| 99 | {true, true, false, false}, |
| 100 | {true, true, false, false}}; |
| 101 | }; |
| 102 | |
| 103 | class NameRelationIsStrictPrefixOf : public CheckerFixture |
| 104 | { |
| 105 | public: |
| 106 | NameRelationIsStrictPrefixOf() |
| 107 | : checker("/foo/bar", NameRelation::IS_STRICT_PREFIX_OF) |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | public: |
| 112 | NameRelationChecker checker; |
| 113 | std::vector<std::vector<bool>> outcomes = {{false, true, false, false}, |
| 114 | {false, true, false, false}, |
| 115 | {false, true, false, false}, |
| 116 | {false, true, false, false}}; |
| 117 | }; |
| 118 | |
| 119 | class RegexEqual : public CheckerFixture |
| 120 | { |
| 121 | public: |
| 122 | RegexEqual() |
Zhiyi Zhang | c4a0176 | 2017-10-11 12:07:25 -0700 | [diff] [blame] | 123 | : checker(Regex("^<foo><bar><KEY><>$")) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 124 | { |
| 125 | } |
| 126 | |
| 127 | public: |
| 128 | RegexChecker checker; |
| 129 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 130 | {true, false, false, false}, |
| 131 | {true, false, false, false}, |
| 132 | {true, false, false, false}}; |
| 133 | }; |
| 134 | |
| 135 | class RegexIsPrefixOf : public CheckerFixture |
| 136 | { |
| 137 | public: |
| 138 | RegexIsPrefixOf() |
Zhiyi Zhang | c4a0176 | 2017-10-11 12:07:25 -0700 | [diff] [blame] | 139 | : checker(Regex("^<foo><bar><>*<KEY><>$")) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 140 | { |
| 141 | } |
| 142 | |
| 143 | public: |
| 144 | RegexChecker checker; |
| 145 | std::vector<std::vector<bool>> outcomes = {{true, true, false, false}, |
| 146 | {true, true, false, false}, |
| 147 | {true, true, false, false}, |
| 148 | {true, true, false, false}}; |
| 149 | }; |
| 150 | |
| 151 | class RegexIsStrictPrefixOf : public CheckerFixture |
| 152 | { |
| 153 | public: |
| 154 | RegexIsStrictPrefixOf() |
Zhiyi Zhang | c4a0176 | 2017-10-11 12:07:25 -0700 | [diff] [blame] | 155 | : checker(Regex("^<foo><bar><>+<KEY><>$")) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 156 | { |
| 157 | } |
| 158 | |
| 159 | public: |
| 160 | RegexChecker checker; |
| 161 | std::vector<std::vector<bool>> outcomes = {{false, true, false, false}, |
| 162 | {false, true, false, false}, |
| 163 | {false, true, false, false}, |
| 164 | {false, true, false, false}}; |
| 165 | }; |
| 166 | |
| 167 | class HyperRelationEqual : public CheckerFixture |
| 168 | { |
| 169 | public: |
| 170 | HyperRelationEqual() |
| 171 | : checker("^(<>+)$", "\\1", "^(<>+)<KEY><>$", "\\1", NameRelation::EQUAL) |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | public: |
| 176 | HyperRelationChecker checker; |
| 177 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 178 | {false, true, false, false}, |
| 179 | {false, false, true, false}, |
| 180 | {false, false, false, true}}; |
| 181 | }; |
| 182 | |
| 183 | class HyperRelationIsPrefixOf : public CheckerFixture |
| 184 | { |
| 185 | public: |
| 186 | HyperRelationIsPrefixOf() |
| 187 | : checker("^(<>+)$", "\\1", "^(<>+)<KEY><>$", "\\1", NameRelation::IS_PREFIX_OF) |
| 188 | { |
| 189 | } |
| 190 | |
| 191 | public: |
| 192 | HyperRelationChecker checker; |
| 193 | std::vector<std::vector<bool>> outcomes = {{true, false, true, false}, |
| 194 | {true, true, true, false}, |
| 195 | {false, false, true, false}, |
| 196 | {false, false, false, true}}; |
| 197 | }; |
| 198 | |
| 199 | class HyperRelationIsStrictPrefixOf : public CheckerFixture |
| 200 | { |
| 201 | public: |
| 202 | HyperRelationIsStrictPrefixOf() |
| 203 | : checker("^(<>+)$", "\\1", "^(<>+)<KEY><>$", "\\1", NameRelation::IS_STRICT_PREFIX_OF) |
| 204 | { |
| 205 | } |
| 206 | |
| 207 | public: |
| 208 | HyperRelationChecker checker; |
| 209 | std::vector<std::vector<bool>> outcomes = {{false, false, true, false}, |
| 210 | {true, false, true, false}, |
| 211 | {false, false, false, false}, |
| 212 | {false, false, false, false}}; |
| 213 | }; |
| 214 | |
| 215 | class Hierarchical : public CheckerFixture |
| 216 | { |
| 217 | public: |
| 218 | Hierarchical() |
| 219 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 220 | type hierarchical |
| 221 | sig-type rsa-sha256 |
| 222 | )CONF"), "test-config")) |
| 223 | , checker(*checkerPtr) |
| 224 | { |
| 225 | } |
| 226 | |
| 227 | public: |
| 228 | std::unique_ptr<Checker> checkerPtr; |
| 229 | Checker& checker; |
| 230 | |
| 231 | std::vector<std::vector<bool>> outcomes = {{true, false, true, false}, |
| 232 | {true, true, true, false}, |
| 233 | {false, false, true, false}, |
| 234 | {false, false, false, true}}; |
| 235 | }; |
| 236 | |
| 237 | class CustomizedNameRelation : public CheckerFixture |
| 238 | { |
| 239 | public: |
| 240 | CustomizedNameRelation() |
| 241 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 242 | type customized |
| 243 | sig-type rsa-sha256 |
| 244 | key-locator |
| 245 | { |
| 246 | type name |
| 247 | name /foo/bar |
| 248 | relation equal |
| 249 | } |
| 250 | )CONF"), "test-config")) |
| 251 | , checker(*checkerPtr) |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | public: |
| 256 | std::unique_ptr<Checker> checkerPtr; |
| 257 | Checker& checker; |
| 258 | |
| 259 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 260 | {true, false, false, false}, |
| 261 | {true, false, false, false}, |
| 262 | {true, false, false, false}}; |
| 263 | }; |
| 264 | |
| 265 | class CustomizedRegex : public CheckerFixture |
| 266 | { |
| 267 | public: |
| 268 | CustomizedRegex() |
| 269 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 270 | type customized |
| 271 | sig-type rsa-sha256 |
| 272 | key-locator |
| 273 | { |
| 274 | type name |
Zhiyi Zhang | c4a0176 | 2017-10-11 12:07:25 -0700 | [diff] [blame] | 275 | regex ^<foo><bar><KEY><>$ |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 276 | } |
| 277 | )CONF"), "test-config")) |
| 278 | , checker(*checkerPtr) |
| 279 | { |
| 280 | } |
| 281 | |
| 282 | public: |
| 283 | std::unique_ptr<Checker> checkerPtr; |
| 284 | Checker& checker; |
| 285 | |
| 286 | std::vector<std::vector<bool>> outcomes = {{true, false, false, false}, |
| 287 | {true, false, false, false}, |
| 288 | {true, false, false, false}, |
| 289 | {true, false, false, false}}; |
| 290 | }; |
| 291 | |
| 292 | class CustomizedHyperRelation : public CheckerFixture |
| 293 | { |
| 294 | public: |
| 295 | CustomizedHyperRelation() |
| 296 | : checkerPtr(Checker::create(makeSection(R"CONF( |
| 297 | type customized |
| 298 | sig-type rsa-sha256 |
| 299 | key-locator |
| 300 | { |
| 301 | type name |
| 302 | hyper-relation |
| 303 | { |
| 304 | k-regex ^(<>+)<KEY><>$ |
| 305 | k-expand \\1 |
| 306 | h-relation is-prefix-of |
| 307 | p-regex ^(<>+)$ |
| 308 | p-expand \\1 |
| 309 | } |
| 310 | } |
| 311 | )CONF"), "test-config")) |
| 312 | , checker(*checkerPtr) |
| 313 | { |
| 314 | } |
| 315 | |
| 316 | public: |
| 317 | std::unique_ptr<Checker> checkerPtr; |
| 318 | Checker& checker; |
| 319 | |
| 320 | std::vector<std::vector<bool>> outcomes = {{true, false, true, false}, |
| 321 | {true, true, true, false}, |
| 322 | {false, false, true, false}, |
| 323 | {false, false, false, true}}; |
| 324 | }; |
| 325 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 326 | using Tests = boost::mpl::vector<NameRelationEqual, NameRelationIsPrefixOf, NameRelationIsStrictPrefixOf, |
| 327 | RegexEqual, RegexIsPrefixOf, RegexIsStrictPrefixOf, |
| 328 | HyperRelationEqual, HyperRelationIsPrefixOf, HyperRelationIsStrictPrefixOf, |
| 329 | Hierarchical, |
| 330 | CustomizedNameRelation, CustomizedRegex, CustomizedHyperRelation>; |
| 331 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 332 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Checks, T, Tests, T) |
| 333 | { |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 334 | using namespace ndn::security::v2::tests; |
| 335 | |
| 336 | BOOST_REQUIRE_EQUAL(this->outcomes.size(), this->names.size()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 337 | for (size_t i = 0; i < this->names.size(); ++i) { |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 338 | BOOST_REQUIRE_EQUAL(this->outcomes[i].size(), this->names.size()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 339 | for (size_t j = 0; j < this->names.size(); ++j) { |
| 340 | const Name& pktName = this->names[i]; |
| 341 | Name klName = this->makeKeyLocatorName(this->names[j]); |
| 342 | bool expectedOutcome = this->outcomes[i][j]; |
| 343 | |
| 344 | auto dataState = make_shared<DummyValidationState>(); |
| 345 | BOOST_CHECK_EQUAL(this->checker.check(tlv::Data, pktName, klName, dataState), expectedOutcome); |
| 346 | BOOST_CHECK_EQUAL(boost::logic::indeterminate(dataState->getOutcome()), expectedOutcome); |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 347 | BOOST_CHECK_EQUAL(bool(dataState->getOutcome()), false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 348 | |
| 349 | auto interestState = make_shared<DummyValidationState>(); |
| 350 | BOOST_CHECK_EQUAL(this->checker.check(tlv::Interest, this->makeSignedInterestName(pktName), |
| 351 | klName, interestState), expectedOutcome); |
| 352 | BOOST_CHECK_EQUAL(boost::logic::indeterminate(interestState->getOutcome()), expectedOutcome); |
Davide Pesavento | 5437aa2 | 2019-03-24 14:02:37 -0400 | [diff] [blame] | 353 | BOOST_CHECK_EQUAL(bool(interestState->getOutcome()), false); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | BOOST_AUTO_TEST_SUITE_END() // TestChecker |
| 359 | BOOST_AUTO_TEST_SUITE_END() // ValidatorConfig |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 360 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 361 | |
| 362 | } // namespace tests |
| 363 | } // namespace validator_config |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 364 | } // inline namespace v2 |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 365 | } // namespace security |
| 366 | } // namespace ndn |