blob: 63b755613812e14f82ca3e21a7beff99f829f882 [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/*
Davide Pesaventoc860bd12022-10-04 03:23:43 -04003 * Copyright (c) 2013-2022 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
Davide Pesaventoc860bd12022-10-04 03:23:43 -040032/**
33 * \brief A dummy validation policy for unit testing.
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000034 */
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050035class DummyValidationPolicy : public security::ValidationPolicy
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000036{
37public:
Davide Pesaventoc860bd12022-10-04 03:23:43 -040038 /** \brief Constructor.
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000039 * \param shouldAccept whether to accept or reject all validation requests
40 */
41 explicit
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080042 DummyValidationPolicy(bool shouldAccept = true)
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000043 {
44 this->setResult(shouldAccept);
45 }
46
Davide Pesaventoc860bd12022-10-04 03:23:43 -040047 /** \brief Change the validation result.
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000048 * \param shouldAccept whether to accept or reject all validation requests
49 */
50 void
51 setResult(bool shouldAccept)
52 {
53 m_decide = [shouldAccept] (const Name&) { return shouldAccept; };
54 }
55
Davide Pesaventoc860bd12022-10-04 03:23:43 -040056 /** \brief Set a callback for validation.
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000057 * \param cb a callback which receives the Interest/Data name for each validation request;
58 * its return value determines the validation result
59 */
60 void
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050061 setResultCallback(std::function<bool(const Name&)> cb)
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000062 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050063 m_decide = std::move(cb);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000064 }
65
66protected:
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080067 void
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050068 checkPolicy(const Data& data, const shared_ptr<security::ValidationState>& state,
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080069 const ValidationContinuation& continueValidation) override
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000070 {
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080071 if (m_decide(data.getName())) {
72 continueValidation(nullptr, state);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000073 }
74 else {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050075 state->fail(security::ValidationError::NO_ERROR);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000076 }
77 }
78
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080079 void
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050080 checkPolicy(const Interest& interest, const shared_ptr<security::ValidationState>& state,
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080081 const ValidationContinuation& continueValidation) override
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000082 {
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080083 if (m_decide(interest.getName())) {
84 continueValidation(nullptr, state);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000085 }
86 else {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050087 state->fail(security::ValidationError::NO_ERROR);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000088 }
89 }
90
91private:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050092 std::function<bool(const Name&)> m_decide;
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000093};
94
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050095class DummyValidator : public security::Validator
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000096{
97public:
Davide Pesavento7e780642018-11-24 15:51:34 -050098 explicit
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080099 DummyValidator(bool shouldAccept = true)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500100 : security::Validator(make_unique<DummyValidationPolicy>(shouldAccept),
101 make_unique<security::CertificateFetcherOffline>())
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000102 {
103 }
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800104
105 DummyValidationPolicy&
106 getPolicy()
107 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500108 return static_cast<DummyValidationPolicy&>(security::Validator::getPolicy());
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800109 }
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000110};
111
112} // namespace tests
113} // namespace ndn
114
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500115#endif // NDN_CXX_TESTS_UNIT_DUMMY_VALIDATOR_HPP