blob: be0adb347c693ad0dfe185fd60dbfc2aa7c3fb30 [file] [log] [blame]
Junxiao Shi0f3f0b42016-07-14 13:26:37 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -08002/*
Alexander Afanasyev09236c22020-06-03 13:42:38 -04003 * Copyright (c) 2013-2020 Regents of the University of California.
Junxiao Shi0f3f0b42016-07-14 13:26:37 +00004 *
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 Pesavento4c1ad4c2020-11-16 21:12:02 -050022#ifndef NDN_CXX_TESTS_UNIT_DUMMY_VALIDATOR_HPP
23#define NDN_CXX_TESTS_UNIT_DUMMY_VALIDATOR_HPP
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000024
Alexander Afanasyev09236c22020-06-03 13:42:38 -040025#include "ndn-cxx/security/validator.hpp"
26#include "ndn-cxx/security/validation-policy.hpp"
27#include "ndn-cxx/security/certificate-fetcher-offline.hpp"
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000028
29namespace ndn {
30namespace tests {
31
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080032/** \brief A validation policy for unit testing
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000033 */
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050034class DummyValidationPolicy : public security::ValidationPolicy
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000035{
36public:
37 /** \brief constructor
38 * \param shouldAccept whether to accept or reject all validation requests
39 */
40 explicit
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080041 DummyValidationPolicy(bool shouldAccept = true)
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000042 {
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
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050060 setResultCallback(std::function<bool(const Name&)> cb)
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000061 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050062 m_decide = std::move(cb);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000063 }
64
65protected:
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080066 void
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050067 checkPolicy(const Data& data, const shared_ptr<security::ValidationState>& state,
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080068 const ValidationContinuation& continueValidation) override
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000069 {
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080070 if (m_decide(data.getName())) {
71 continueValidation(nullptr, state);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000072 }
73 else {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050074 state->fail(security::ValidationError::NO_ERROR);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000075 }
76 }
77
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080078 void
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050079 checkPolicy(const Interest& interest, const shared_ptr<security::ValidationState>& state,
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080080 const ValidationContinuation& continueValidation) override
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000081 {
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080082 if (m_decide(interest.getName())) {
83 continueValidation(nullptr, state);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000084 }
85 else {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050086 state->fail(security::ValidationError::NO_ERROR);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000087 }
88 }
89
90private:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050091 std::function<bool(const Name&)> m_decide;
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000092};
93
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050094class DummyValidator : public security::Validator
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000095{
96public:
Davide Pesavento7e780642018-11-24 15:51:34 -050097 explicit
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080098 DummyValidator(bool shouldAccept = true)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050099 : security::Validator(make_unique<DummyValidationPolicy>(shouldAccept),
100 make_unique<security::CertificateFetcherOffline>())
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000101 {
102 }
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800103
104 DummyValidationPolicy&
105 getPolicy()
106 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500107 return static_cast<DummyValidationPolicy&>(security::Validator::getPolicy());
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800108 }
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000109};
110
111} // namespace tests
112} // namespace ndn
113
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500114#endif // NDN_CXX_TESTS_UNIT_DUMMY_VALIDATOR_HPP