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