Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -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 | #ifndef NDN_TESTS_UNIT_SECURITY_V2_VALIDATOR_FIXTURE_HPP |
| 23 | #define NDN_TESTS_UNIT_SECURITY_V2_VALIDATOR_FIXTURE_HPP |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/v2/validator.hpp" |
| 26 | #include "ndn-cxx/security/v2/certificate-fetcher-from-network.hpp" |
| 27 | #include "ndn-cxx/util/dummy-client-face.hpp" |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -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/identity-management-time-fixture.hpp" |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 31 | |
| 32 | #include <boost/lexical_cast.hpp> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace security { |
| 36 | namespace v2 { |
| 37 | namespace tests { |
| 38 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 39 | template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork> |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 40 | class ValidatorFixture : public ndn::tests::IdentityManagementTimeFixture |
| 41 | { |
| 42 | public: |
| 43 | ValidatorFixture() |
| 44 | : face(io, {true, true}) |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 45 | , validator(make_unique<ValidationPolicy>(), make_unique<CertificateFetcher>(face)) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 46 | , policy(static_cast<ValidationPolicy&>(validator.getPolicy())) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 47 | , cache(100_days) |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 48 | , lastError(ValidationError::Code::NO_ERROR) |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 49 | { |
| 50 | processInterest = [this] (const Interest& interest) { |
| 51 | auto cert = cache.find(interest); |
| 52 | if (cert != nullptr) { |
| 53 | face.receive(*cert); |
| 54 | } |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | virtual |
| 59 | ~ValidatorFixture() = default; |
| 60 | |
| 61 | template<class Packet> |
| 62 | void |
| 63 | validate(const Packet& packet, const std::string& msg, bool expectSuccess, int line) |
| 64 | { |
| 65 | std::string detailedInfo = msg + " on line " + to_string(line); |
| 66 | size_t nCallbacks = 0; |
| 67 | this->validator.validate(packet, |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 68 | [&] (const Packet&) { |
| 69 | ++nCallbacks; |
| 70 | BOOST_CHECK_MESSAGE(expectSuccess, |
| 71 | (expectSuccess ? "OK: " : "FAILED: ") + detailedInfo); |
| 72 | }, |
| 73 | [&] (const Packet&, const ValidationError& error) { |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 74 | lastError = error; |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 75 | ++nCallbacks; |
| 76 | BOOST_CHECK_MESSAGE(!expectSuccess, |
| 77 | (!expectSuccess ? "OK: " : "FAILED: ") + detailedInfo + |
| 78 | (expectSuccess ? " (" + boost::lexical_cast<std::string>(error) + ")" : "")); |
| 79 | }); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 80 | |
| 81 | mockNetworkOperations(); |
| 82 | BOOST_CHECK_EQUAL(nCallbacks, 1); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | mockNetworkOperations() |
| 87 | { |
| 88 | util::signal::ScopedConnection connection = face.onSendInterest.connect([this] (const Interest& interest) { |
| 89 | if (processInterest != nullptr) { |
| 90 | io.post(bind(processInterest, interest)); |
| 91 | } |
| 92 | }); |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 93 | advanceClocks(time::milliseconds(s_mockPeriod), s_mockTimes); |
| 94 | } |
| 95 | |
| 96 | /** \brief undo clock advancement of mockNetworkOperations |
| 97 | */ |
| 98 | void |
| 99 | rewindClockAfterValidation() |
| 100 | { |
| 101 | this->systemClock->advance(time::milliseconds(s_mockPeriod * s_mockTimes * -1)); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | public: |
| 105 | util::DummyClientFace face; |
| 106 | std::function<void(const Interest& interest)> processInterest; |
| 107 | Validator validator; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 108 | ValidationPolicy& policy; |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 109 | |
| 110 | CertificateCache cache; |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 111 | ValidationError lastError; |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | const static int s_mockPeriod; |
| 115 | const static int s_mockTimes; |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 118 | template<class ValidationPolicy, class CertificateFetcher> |
| 119 | const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockPeriod = 250; |
| 120 | |
| 121 | template<class ValidationPolicy, class CertificateFetcher> |
| 122 | const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockTimes = 200; |
| 123 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 124 | template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork> |
| 125 | class HierarchicalValidatorFixture : public ValidatorFixture<ValidationPolicy, CertificateFetcher> |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 126 | { |
| 127 | public: |
| 128 | HierarchicalValidatorFixture() |
| 129 | { |
| 130 | identity = this->addIdentity("/Security/V2/ValidatorFixture"); |
| 131 | subIdentity = this->addSubCertificate("/Security/V2/ValidatorFixture/Sub1", identity); |
| 132 | subSelfSignedIdentity = this->addIdentity("/Security/V2/ValidatorFixture/Sub1/Sub2"); |
| 133 | otherIdentity = this->addIdentity("/Security/V2/OtherIdentity"); |
| 134 | |
| 135 | this->validator.loadAnchor("", Certificate(identity.getDefaultKey().getDefaultCertificate())); |
| 136 | |
| 137 | this->cache.insert(identity.getDefaultKey().getDefaultCertificate()); |
| 138 | this->cache.insert(subIdentity.getDefaultKey().getDefaultCertificate()); |
| 139 | this->cache.insert(subSelfSignedIdentity.getDefaultKey().getDefaultCertificate()); |
| 140 | this->cache.insert(otherIdentity.getDefaultKey().getDefaultCertificate()); |
| 141 | } |
| 142 | |
| 143 | public: |
| 144 | Identity identity; |
| 145 | Identity subIdentity; |
| 146 | Identity subSelfSignedIdentity; |
| 147 | Identity otherIdentity; |
| 148 | }; |
| 149 | |
| 150 | #define VALIDATE_SUCCESS(packet, message) this->template validate(packet, message, true, __LINE__) |
| 151 | #define VALIDATE_FAILURE(packet, message) this->template validate(packet, message, false, __LINE__) |
| 152 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 153 | class DummyValidationState : public ValidationState |
| 154 | { |
| 155 | public: |
| 156 | ~DummyValidationState() |
| 157 | { |
| 158 | m_outcome = false; |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | fail(const ValidationError& error) override |
| 163 | { |
| 164 | // BOOST_TEST_MESSAGE(error); |
| 165 | m_outcome = false; |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | void |
| 170 | verifyOriginalPacket(const Certificate& trustedCert) override |
| 171 | { |
| 172 | // do nothing |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | bypassValidation() override |
| 177 | { |
| 178 | // do nothing |
| 179 | } |
| 180 | }; |
| 181 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 182 | } // namespace tests |
| 183 | } // namespace v2 |
| 184 | } // namespace security |
| 185 | } // namespace ndn |
| 186 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 187 | #endif // NDN_TESTS_UNIT_SECURITY_V2_VALIDATOR_FIXTURE_HPP |