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