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 | 0a6456c | 2019-11-14 00:33:11 -0500 | [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/validation-policy-config.hpp" |
| 23 | #include "ndn-cxx/security/transform/base64-encode.hpp" |
| 24 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 25 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
| 26 | #include "ndn-cxx/util/logger.hpp" |
| 27 | #include "ndn-cxx/util/io.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 28 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "tests/boost-test.hpp" |
| 30 | #include "tests/unit/security/v2/validator-config/common.hpp" |
| 31 | #include "tests/unit/security/v2/validator-fixture.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 34 | namespace security { |
| 35 | namespace v2 { |
| 36 | namespace validator_config { |
| 37 | namespace tests { |
| 38 | |
| 39 | using namespace ndn::tests; |
| 40 | using namespace ndn::security::v2::tests; |
| 41 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 42 | BOOST_AUTO_TEST_SUITE(Security) |
| 43 | BOOST_AUTO_TEST_SUITE(V2) |
| 44 | BOOST_AUTO_TEST_SUITE(TestValidationPolicyConfig) |
| 45 | |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 46 | BOOST_FIXTURE_TEST_CASE(EmptyConfig, HierarchicalValidatorFixture<ValidationPolicyConfig>) |
| 47 | { |
| 48 | this->policy.load(ConfigSection{}, "<empty>"); |
| 49 | |
| 50 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 51 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 52 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0); |
| 53 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0); |
| 54 | |
| 55 | Data d("/Security/V2/ValidationPolicyConfig/D"); |
| 56 | this->m_keyChain.sign(d, signingByIdentity(this->identity)); |
| 57 | VALIDATE_FAILURE(d, "Empty policy should reject everything"); |
| 58 | |
| 59 | Interest i("/Security/V2/ValidationPolicyConfig/I"); |
| 60 | this->m_keyChain.sign(i, signingByIdentity(this->identity)); |
| 61 | VALIDATE_FAILURE(i, "Empty policy should reject everything"); |
| 62 | } |
| 63 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 64 | template<typename Packet> |
| 65 | class PacketName; |
| 66 | |
| 67 | template<> |
| 68 | class PacketName<Interest> |
| 69 | { |
| 70 | public: |
| 71 | static std::string |
| 72 | getName() |
| 73 | { |
| 74 | return "interest"; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | template<> |
| 79 | class PacketName<Data> |
| 80 | { |
| 81 | public: |
| 82 | static std::string |
| 83 | getName() |
| 84 | { |
| 85 | return "data"; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | template<typename PacketType> |
| 90 | class ValidationPolicyConfigFixture : public HierarchicalValidatorFixture<ValidationPolicyConfig> |
| 91 | { |
| 92 | public: |
| 93 | ValidationPolicyConfigFixture() |
| 94 | : path(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "security" / "v2" / "validation-policy-config") |
| 95 | { |
| 96 | boost::filesystem::create_directories(path); |
| 97 | baseConfig = R"CONF( |
| 98 | rule |
| 99 | { |
| 100 | id test-rule-id |
| 101 | for )CONF" + PacketName<Packet>::getName() + R"CONF( |
| 102 | filter |
| 103 | { |
| 104 | type name |
| 105 | name )CONF" + identity.getName().toUri() + R"CONF( |
| 106 | relation is-prefix-of |
| 107 | } |
| 108 | checker |
| 109 | { |
| 110 | type hierarchical |
| 111 | sig-type rsa-sha256 |
| 112 | } |
| 113 | } |
| 114 | )CONF"; |
| 115 | } |
| 116 | |
| 117 | ~ValidationPolicyConfigFixture() |
| 118 | { |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 119 | boost::system::error_code ec; |
| 120 | boost::filesystem::remove_all(path, ec); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 123 | protected: |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 124 | using Packet = PacketType; |
| 125 | |
| 126 | const boost::filesystem::path path; |
| 127 | std::string baseConfig; |
| 128 | }; |
| 129 | |
| 130 | template<typename PacketType> |
| 131 | class LoadStringWithFileAnchor : public ValidationPolicyConfigFixture<PacketType> |
| 132 | { |
| 133 | public: |
| 134 | LoadStringWithFileAnchor() |
| 135 | { |
| 136 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 137 | |
| 138 | this->saveCertificate(this->identity, (this->path / "identity.ndncert").string()); |
| 139 | this->policy.load(this->baseConfig + R"CONF( |
| 140 | trust-anchor |
| 141 | { |
| 142 | type file |
| 143 | file-name "trust-anchor.ndncert" |
| 144 | } |
| 145 | )CONF", (this->path / "test-config").string()); |
| 146 | |
| 147 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 148 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | template<typename PacketType> |
| 153 | class LoadFileWithFileAnchor : public ValidationPolicyConfigFixture<PacketType> |
| 154 | { |
| 155 | public: |
| 156 | LoadFileWithFileAnchor() |
| 157 | { |
| 158 | std::string configFile = (this->path / "config.conf").string(); |
| 159 | { |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 160 | std::ofstream config(configFile); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 161 | config << this->baseConfig << R"CONF( |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 162 | trust-anchor |
| 163 | { |
| 164 | type file |
| 165 | file-name "trust-anchor.ndncert" |
| 166 | } |
| 167 | )CONF"; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 168 | } |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 169 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 170 | this->saveCertificate(this->identity, (this->path / "identity.ndncert").string()); |
| 171 | |
| 172 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 173 | |
| 174 | this->policy.load(configFile); |
| 175 | |
| 176 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 177 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | template<typename PacketType> |
| 182 | class LoadSectionWithFileAnchor : public ValidationPolicyConfigFixture<PacketType> |
| 183 | { |
| 184 | public: |
| 185 | LoadSectionWithFileAnchor() |
| 186 | { |
| 187 | auto section = makeSection(this->baseConfig + R"CONF( |
| 188 | trust-anchor |
| 189 | { |
| 190 | type file |
| 191 | file-name "trust-anchor.ndncert" |
| 192 | } |
| 193 | )CONF"); |
| 194 | |
| 195 | this->saveCertificate(this->identity, (this->path / "identity.ndncert").string()); |
| 196 | |
| 197 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 198 | |
| 199 | this->policy.load(section, (this->path / "test-config").string()); |
| 200 | |
| 201 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 202 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | template<typename PacketType> |
| 207 | class LoadStringWithBase64Anchor : public ValidationPolicyConfigFixture<PacketType> |
| 208 | { |
| 209 | public: |
| 210 | LoadStringWithBase64Anchor() |
| 211 | { |
| 212 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 213 | |
| 214 | std::ostringstream os; |
Davide Pesavento | 0a6456c | 2019-11-14 00:33:11 -0500 | [diff] [blame] | 215 | { |
| 216 | using namespace ndn::security::transform; |
| 217 | const auto& cert = this->identity.getDefaultKey().getDefaultCertificate().wireEncode(); |
| 218 | bufferSource(cert.wire(), cert.size()) >> base64Encode(false) >> streamSink(os); |
| 219 | } |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 220 | |
| 221 | this->policy.load(this->baseConfig + R"CONF( |
| 222 | trust-anchor |
| 223 | { |
| 224 | type base64 |
| 225 | base64-string ")CONF" + os.str() + R"CONF(" |
| 226 | } |
| 227 | )CONF", (this->path / "test-config").string()); |
| 228 | |
| 229 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 230 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 231 | } |
| 232 | }; |
| 233 | |
| 234 | class NoRefresh |
| 235 | { |
| 236 | public: |
| 237 | static std::string |
| 238 | getRefreshString() |
| 239 | { |
| 240 | return ""; |
| 241 | } |
| 242 | }; |
| 243 | |
| 244 | class Refresh1h |
| 245 | { |
| 246 | public: |
| 247 | static std::string |
| 248 | getRefreshString() |
| 249 | { |
| 250 | return "refresh 1h"; |
| 251 | } |
| 252 | |
| 253 | static time::milliseconds |
| 254 | getRefreshTime() |
| 255 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 256 | return 1_h; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 257 | } |
| 258 | }; |
| 259 | |
| 260 | class Refresh1m |
| 261 | { |
| 262 | public: |
| 263 | static std::string |
| 264 | getRefreshString() |
| 265 | { |
| 266 | return "refresh 1m"; |
| 267 | } |
| 268 | |
| 269 | static time::milliseconds |
| 270 | getRefreshTime() |
| 271 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 272 | return 1_min; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 273 | } |
| 274 | }; |
| 275 | |
| 276 | class Refresh1s |
| 277 | { |
| 278 | public: |
| 279 | static std::string |
| 280 | getRefreshString() |
| 281 | { |
| 282 | return "refresh 1s"; |
| 283 | } |
| 284 | |
| 285 | static time::milliseconds |
| 286 | getRefreshTime() |
| 287 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 288 | return 1_s; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 289 | } |
| 290 | }; |
| 291 | |
| 292 | template<typename PacketType, typename Refresh = NoRefresh> |
| 293 | class LoadStringWithDirAnchor : public ValidationPolicyConfigFixture<PacketType> |
| 294 | { |
| 295 | public: |
| 296 | LoadStringWithDirAnchor() |
| 297 | { |
| 298 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 299 | |
| 300 | boost::filesystem::create_directories(this->path / "keys"); |
| 301 | this->saveCertificate(this->identity, (this->path / "keys" / "identity.ndncert").string()); |
| 302 | |
| 303 | this->policy.load(this->baseConfig + R"CONF( |
| 304 | trust-anchor |
| 305 | { |
| 306 | type dir |
| 307 | dir keys |
| 308 | )CONF" + Refresh::getRefreshString() + R"CONF( |
| 309 | } |
| 310 | )CONF", (this->path / "test-config").string()); |
| 311 | |
| 312 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 313 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 314 | } |
| 315 | }; |
| 316 | |
| 317 | using DataPolicies = boost::mpl::vector<LoadStringWithFileAnchor<Data>, |
| 318 | LoadFileWithFileAnchor<Data>, |
| 319 | LoadSectionWithFileAnchor<Data>, |
| 320 | LoadStringWithBase64Anchor<Data>, |
| 321 | LoadStringWithDirAnchor<Data>, |
| 322 | LoadStringWithDirAnchor<Data, Refresh1h>, |
| 323 | LoadStringWithDirAnchor<Data, Refresh1m>, |
| 324 | LoadStringWithDirAnchor<Data, Refresh1s> |
| 325 | >; |
| 326 | |
| 327 | using InterestPolicies = boost::mpl::vector<LoadStringWithFileAnchor<Interest>, |
| 328 | LoadFileWithFileAnchor<Interest>, |
| 329 | LoadSectionWithFileAnchor<Interest>, |
| 330 | LoadStringWithBase64Anchor<Interest>, |
| 331 | LoadStringWithDirAnchor<Interest>, |
| 332 | LoadStringWithDirAnchor<Interest, Refresh1h>, |
| 333 | LoadStringWithDirAnchor<Interest, Refresh1m>, |
| 334 | LoadStringWithDirAnchor<Interest, Refresh1s> |
| 335 | >; |
| 336 | |
| 337 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateData, Policy, DataPolicies, Policy) |
| 338 | { |
| 339 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 1); |
| 340 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0); |
| 341 | |
| 342 | using Packet = typename Policy::Packet; |
| 343 | Packet unsignedPacket("/Security/V2/ValidatorFixture/Sub1/Sub2/Packet"); |
| 344 | |
| 345 | Packet packet = unsignedPacket; |
| 346 | VALIDATE_FAILURE(packet, "Unsigned"); |
| 347 | |
| 348 | packet = unsignedPacket; |
| 349 | this->m_keyChain.sign(packet, signingWithSha256()); |
| 350 | VALIDATE_FAILURE(packet, "Policy doesn't accept Sha256Digest signature"); |
| 351 | |
| 352 | packet = unsignedPacket; |
| 353 | this->m_keyChain.sign(packet, signingByIdentity(this->identity)); |
| 354 | VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the anchor"); |
| 355 | |
| 356 | packet = unsignedPacket; |
| 357 | this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity)); |
| 358 | VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the policy-compliant cert"); |
| 359 | |
| 360 | packet = unsignedPacket; |
| 361 | this->m_keyChain.sign(packet, signingByIdentity(this->otherIdentity)); |
| 362 | VALIDATE_FAILURE(packet, "Should fail, as signed by the policy-violating cert"); |
| 363 | |
| 364 | packet = unsignedPacket; |
| 365 | this->m_keyChain.sign(packet, signingByIdentity(this->subSelfSignedIdentity)); |
| 366 | VALIDATE_FAILURE(packet, "Should fail, because subSelfSignedIdentity is not a trust anchor"); |
| 367 | } |
| 368 | |
| 369 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateInterest, Policy, InterestPolicies, Policy) |
| 370 | { |
| 371 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0); |
| 372 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 1); |
| 373 | |
| 374 | using Packet = typename Policy::Packet; |
| 375 | Packet unsignedPacket("/Security/V2/ValidatorFixture/Sub1/Sub2/Packet"); |
| 376 | |
| 377 | Packet packet = unsignedPacket; |
| 378 | VALIDATE_FAILURE(packet, "Unsigned"); |
| 379 | |
| 380 | packet = unsignedPacket; |
| 381 | this->m_keyChain.sign(packet, signingWithSha256()); |
| 382 | VALIDATE_FAILURE(packet, "Policy doesn't accept Sha256Digest signature"); |
| 383 | |
| 384 | packet = unsignedPacket; |
| 385 | this->m_keyChain.sign(packet, signingByIdentity(this->identity)); |
| 386 | VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the anchor"); |
| 387 | |
| 388 | packet = unsignedPacket; |
| 389 | this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity)); |
| 390 | VALIDATE_FAILURE(packet, "Should fail, as there is no matching rule for data"); |
| 391 | |
| 392 | packet = unsignedPacket; |
| 393 | this->m_keyChain.sign(packet, signingByIdentity(this->otherIdentity)); |
| 394 | VALIDATE_FAILURE(packet, "Should fail, as signed by the policy-violating cert"); |
| 395 | |
| 396 | packet = unsignedPacket; |
| 397 | this->m_keyChain.sign(packet, signingByIdentity(this->subSelfSignedIdentity)); |
| 398 | VALIDATE_FAILURE(packet, "Should fail, because subSelfSignedIdentity is not a trust anchor"); |
| 399 | } |
| 400 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 401 | BOOST_FIXTURE_TEST_CASE(Reload, HierarchicalValidatorFixture<ValidationPolicyConfig>) |
| 402 | { |
| 403 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, false); |
| 404 | this->policy.load(R"CONF( |
| 405 | rule |
| 406 | { |
| 407 | id test-rule-data-id |
| 408 | for data |
| 409 | filter |
| 410 | { |
| 411 | type name |
| 412 | name /foo/bar |
| 413 | relation is-prefix-of |
| 414 | } |
| 415 | checker |
| 416 | { |
| 417 | type hierarchical |
| 418 | sig-type rsa-sha256 |
| 419 | } |
| 420 | } |
| 421 | rule |
| 422 | { |
| 423 | id test-rule-interest-id |
| 424 | for interest |
| 425 | filter |
| 426 | { |
| 427 | type name |
| 428 | name /foo/bar |
| 429 | relation is-prefix-of |
| 430 | } |
| 431 | checker |
| 432 | { |
| 433 | type hierarchical |
| 434 | sig-type rsa-sha256 |
| 435 | } |
| 436 | } |
| 437 | trust-anchor |
| 438 | { |
| 439 | type dir |
| 440 | dir keys |
| 441 | refresh 1h |
| 442 | } |
| 443 | )CONF", "test-config"); |
| 444 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 445 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, false); |
| 446 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 1); |
| 447 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 1); |
| 448 | |
| 449 | this->policy.load(R"CONF( |
| 450 | trust-anchor |
| 451 | { |
| 452 | type any |
| 453 | } |
| 454 | )CONF", "test-config"); |
| 455 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 456 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, true); |
| 457 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0); |
| 458 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0); |
| 459 | } |
| 460 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 461 | using Packets = boost::mpl::vector<Interest, Data>; |
| 462 | |
| 463 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(TrustAnchorWildcard, Packet, Packets, ValidationPolicyConfigFixture<Packet>) |
| 464 | { |
| 465 | this->policy.load(R"CONF( |
| 466 | trust-anchor |
| 467 | { |
| 468 | type any |
| 469 | } |
| 470 | )CONF", "test-config"); |
| 471 | |
| 472 | BOOST_CHECK_EQUAL(this->policy.m_isConfigured, true); |
| 473 | BOOST_CHECK_EQUAL(this->policy.m_shouldBypass, true); |
| 474 | BOOST_CHECK_EQUAL(this->policy.m_dataRules.size(), 0); |
| 475 | BOOST_CHECK_EQUAL(this->policy.m_interestRules.size(), 0); |
| 476 | |
| 477 | Packet unsignedPacket("/Security/V2/ValidatorFixture/Sub1/Sub2/Packet"); |
| 478 | |
| 479 | Packet packet = unsignedPacket; |
| 480 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 481 | |
| 482 | packet = unsignedPacket; |
| 483 | this->m_keyChain.sign(packet, signingWithSha256()); |
| 484 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 485 | |
| 486 | packet = unsignedPacket; |
| 487 | this->m_keyChain.sign(packet, signingByIdentity(this->identity)); |
| 488 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 489 | |
| 490 | packet = unsignedPacket; |
| 491 | this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity)); |
| 492 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 493 | |
| 494 | packet = unsignedPacket; |
| 495 | this->m_keyChain.sign(packet, signingByIdentity(this->otherIdentity)); |
| 496 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 497 | |
| 498 | packet = unsignedPacket; |
| 499 | this->m_keyChain.sign(packet, signingByIdentity(this->subSelfSignedIdentity)); |
| 500 | VALIDATE_SUCCESS(packet, "Policy should accept everything"); |
| 501 | } |
| 502 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 503 | using RefreshPolicies = boost::mpl::vector<Refresh1h, Refresh1m, Refresh1s>; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 504 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 505 | template<typename RefreshPolicy> |
| 506 | class RefreshPolicyFixture : public LoadStringWithDirAnchor<Data, RefreshPolicy> |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 507 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 508 | }; |
| 509 | |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 510 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateRefresh, Refresh, RefreshPolicies, RefreshPolicyFixture<Refresh>) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 511 | { |
| 512 | using Packet = Data; |
| 513 | Packet unsignedPacket("/Security/V2/ValidatorFixture/Sub1/Sub2/Packet"); |
| 514 | |
| 515 | boost::filesystem::remove(this->path / "keys" / "identity.ndncert"); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 516 | this->advanceClocks(Refresh::getRefreshTime(), 3); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 517 | |
| 518 | Packet packet = unsignedPacket; |
| 519 | this->m_keyChain.sign(packet, signingByIdentity(this->identity)); |
| 520 | VALIDATE_FAILURE(packet, "Should fail, as the trust anchor should no longer exist"); |
| 521 | |
| 522 | packet = unsignedPacket; |
| 523 | this->m_keyChain.sign(packet, signingByIdentity(this->subIdentity)); |
| 524 | VALIDATE_FAILURE(packet, "Should fail, as the trust anchor should no longer exist"); |
| 525 | } |
| 526 | |
Alexander Afanasyev | 7b11246 | 2018-10-17 11:51:52 -0400 | [diff] [blame] | 527 | BOOST_FIXTURE_TEST_CASE(OrphanedPolicyLoad, HierarchicalValidatorFixture<ValidationPolicyConfig>) // Bug #4758 |
| 528 | { |
| 529 | ValidationPolicyConfig policy1; |
| 530 | BOOST_CHECK_THROW(policy1.load("trust-anchor { type any }", "test-config"), Error); |
| 531 | |
| 532 | // Reloading would have triggered a segfault |
| 533 | BOOST_CHECK_THROW(policy1.load("trust-anchor { type any }", "test-config"), Error); |
| 534 | |
| 535 | ValidationPolicyConfig policy2; |
| 536 | |
| 537 | std::string config = R"CONF( |
| 538 | trust-anchor |
| 539 | { |
| 540 | type dir |
| 541 | dir keys |
| 542 | refresh 1h |
| 543 | } |
| 544 | )CONF"; |
| 545 | |
| 546 | // Inserting trust anchor would have triggered a segfault |
| 547 | BOOST_CHECK_THROW(policy2.load(config, "test-config"), Error); |
| 548 | } |
| 549 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 550 | BOOST_AUTO_TEST_SUITE_END() // TestValidationPolicyConfig |
| 551 | BOOST_AUTO_TEST_SUITE_END() // V2 |
| 552 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 553 | |
| 554 | } // namespace tests |
| 555 | } // namespace validator_config |
| 556 | } // namespace v2 |
| 557 | } // namespace security |
| 558 | } // namespace ndn |