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