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