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