Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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 | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 45 | , cache(time::days(100)) |
| 46 | { |
| 47 | processInterest = [this] (const Interest& interest) { |
| 48 | auto cert = cache.find(interest); |
| 49 | if (cert != nullptr) { |
| 50 | face.receive(*cert); |
| 51 | } |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | virtual |
| 56 | ~ValidatorFixture() = default; |
| 57 | |
| 58 | template<class Packet> |
| 59 | void |
| 60 | validate(const Packet& packet, const std::string& msg, bool expectSuccess, int line) |
| 61 | { |
| 62 | std::string detailedInfo = msg + " on line " + to_string(line); |
| 63 | size_t nCallbacks = 0; |
| 64 | this->validator.validate(packet, |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame^] | 65 | [&] (const Packet&) { |
| 66 | ++nCallbacks; |
| 67 | BOOST_CHECK_MESSAGE(expectSuccess, |
| 68 | (expectSuccess ? "OK: " : "FAILED: ") + detailedInfo); |
| 69 | }, |
| 70 | [&] (const Packet&, const ValidationError& error) { |
| 71 | ++nCallbacks; |
| 72 | BOOST_CHECK_MESSAGE(!expectSuccess, |
| 73 | (!expectSuccess ? "OK: " : "FAILED: ") + detailedInfo + |
| 74 | (expectSuccess ? " (" + boost::lexical_cast<std::string>(error) + ")" : "")); |
| 75 | }); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 76 | |
| 77 | mockNetworkOperations(); |
| 78 | BOOST_CHECK_EQUAL(nCallbacks, 1); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | mockNetworkOperations() |
| 83 | { |
| 84 | util::signal::ScopedConnection connection = face.onSendInterest.connect([this] (const Interest& interest) { |
| 85 | if (processInterest != nullptr) { |
| 86 | io.post(bind(processInterest, interest)); |
| 87 | } |
| 88 | }); |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame^] | 89 | advanceClocks(time::milliseconds(s_mockPeriod), s_mockTimes); |
| 90 | } |
| 91 | |
| 92 | /** \brief undo clock advancement of mockNetworkOperations |
| 93 | */ |
| 94 | void |
| 95 | rewindClockAfterValidation() |
| 96 | { |
| 97 | this->systemClock->advance(time::milliseconds(s_mockPeriod * s_mockTimes * -1)); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public: |
| 101 | util::DummyClientFace face; |
| 102 | std::function<void(const Interest& interest)> processInterest; |
| 103 | Validator validator; |
| 104 | |
| 105 | CertificateCache cache; |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame^] | 106 | |
| 107 | private: |
| 108 | const static int s_mockPeriod; |
| 109 | const static int s_mockTimes; |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame^] | 112 | template<class ValidationPolicy, class CertificateFetcher> |
| 113 | const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockPeriod = 250; |
| 114 | |
| 115 | template<class ValidationPolicy, class CertificateFetcher> |
| 116 | const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockTimes = 200; |
| 117 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 118 | template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork> |
| 119 | class HierarchicalValidatorFixture : public ValidatorFixture<ValidationPolicy, CertificateFetcher> |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 120 | { |
| 121 | public: |
| 122 | HierarchicalValidatorFixture() |
| 123 | { |
| 124 | identity = this->addIdentity("/Security/V2/ValidatorFixture"); |
| 125 | subIdentity = this->addSubCertificate("/Security/V2/ValidatorFixture/Sub1", identity); |
| 126 | subSelfSignedIdentity = this->addIdentity("/Security/V2/ValidatorFixture/Sub1/Sub2"); |
| 127 | otherIdentity = this->addIdentity("/Security/V2/OtherIdentity"); |
| 128 | |
| 129 | this->validator.loadAnchor("", Certificate(identity.getDefaultKey().getDefaultCertificate())); |
| 130 | |
| 131 | this->cache.insert(identity.getDefaultKey().getDefaultCertificate()); |
| 132 | this->cache.insert(subIdentity.getDefaultKey().getDefaultCertificate()); |
| 133 | this->cache.insert(subSelfSignedIdentity.getDefaultKey().getDefaultCertificate()); |
| 134 | this->cache.insert(otherIdentity.getDefaultKey().getDefaultCertificate()); |
| 135 | } |
| 136 | |
| 137 | public: |
| 138 | Identity identity; |
| 139 | Identity subIdentity; |
| 140 | Identity subSelfSignedIdentity; |
| 141 | Identity otherIdentity; |
| 142 | }; |
| 143 | |
| 144 | #define VALIDATE_SUCCESS(packet, message) this->template validate(packet, message, true, __LINE__) |
| 145 | #define VALIDATE_FAILURE(packet, message) this->template validate(packet, message, false, __LINE__) |
| 146 | |
| 147 | } // namespace tests |
| 148 | } // namespace v2 |
| 149 | } // namespace security |
| 150 | } // namespace ndn |
| 151 | |
| 152 | #endif // NDN_TESTS_SECURITY_V2_VALIDATOR_FIXTURE_HPP |