Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [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 | |
| 22 | #ifndef NDN_TESTS_DUMMY_VALIDATOR_HPP |
| 23 | #define NDN_TESTS_DUMMY_VALIDATOR_HPP |
| 24 | |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 25 | #include "security/v2/validator.hpp" |
| 26 | #include "security/v2/validation-policy.hpp" |
| 27 | #include "security/v2/certificate-fetcher-offline.hpp" |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace tests { |
| 31 | |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 32 | /** \brief A validation policy for unit testing |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 33 | */ |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 34 | class DummyValidationPolicy : public security::v2::ValidationPolicy |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 35 | { |
| 36 | public: |
| 37 | /** \brief constructor |
| 38 | * \param shouldAccept whether to accept or reject all validation requests |
| 39 | */ |
| 40 | explicit |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 41 | DummyValidationPolicy(bool shouldAccept = true) |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 42 | { |
| 43 | this->setResult(shouldAccept); |
| 44 | } |
| 45 | |
| 46 | /** \brief change the validation result |
| 47 | * \param shouldAccept whether to accept or reject all validation requests |
| 48 | */ |
| 49 | void |
| 50 | setResult(bool shouldAccept) |
| 51 | { |
| 52 | m_decide = [shouldAccept] (const Name&) { return shouldAccept; }; |
| 53 | } |
| 54 | |
| 55 | /** \brief set a callback for validation |
| 56 | * \param cb a callback which receives the Interest/Data name for each validation request; |
| 57 | * its return value determines the validation result |
| 58 | */ |
| 59 | void |
| 60 | setResultCallback(const function<bool(const Name&)>& cb) |
| 61 | { |
| 62 | m_decide = cb; |
| 63 | } |
| 64 | |
| 65 | protected: |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 66 | void |
| 67 | checkPolicy(const Data& data, const shared_ptr<security::v2::ValidationState>& state, |
| 68 | const ValidationContinuation& continueValidation) override |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 69 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 70 | if (m_decide(data.getName())) { |
| 71 | continueValidation(nullptr, state); |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 72 | } |
| 73 | else { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 74 | state->fail(security::v2::ValidationError::NO_ERROR); |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 78 | void |
| 79 | checkPolicy(const Interest& interest, const shared_ptr<security::v2::ValidationState>& state, |
| 80 | const ValidationContinuation& continueValidation) override |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 81 | { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 82 | if (m_decide(interest.getName())) { |
| 83 | continueValidation(nullptr, state); |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 84 | } |
| 85 | else { |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 86 | state->fail(security::v2::ValidationError::NO_ERROR); |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | function<bool(const Name&)> m_decide; |
| 92 | }; |
| 93 | |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 94 | |
| 95 | class DummyValidator : public security::v2::Validator |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 96 | { |
| 97 | public: |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 98 | DummyValidator(bool shouldAccept = true) |
| 99 | : security::v2::Validator(make_unique<DummyValidationPolicy>(shouldAccept), |
| 100 | make_unique<security::v2::CertificateFetcherOffline>()) |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 101 | { |
| 102 | } |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 103 | |
| 104 | DummyValidationPolicy& |
| 105 | getPolicy() |
| 106 | { |
| 107 | return static_cast<DummyValidationPolicy&>(security::v2::Validator::getPolicy()); |
| 108 | } |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace tests |
| 112 | } // namespace ndn |
| 113 | |
| 114 | #endif // NDN_TESTS_DUMMY_VALIDATOR_HPP |