blob: bb051676ba8aaa0477b4c9c7b087c73a2b712bf4 [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 Pesavento7e780642018-11-24 15:51:34 -050022#ifndef NDN_TESTS_UNIT_DUMMY_VALIDATOR_HPP
23#define NDN_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 */
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080034class DummyValidationPolicy : public security::v2::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
60 setResultCallback(const function<bool(const Name&)>& cb)
61 {
62 m_decide = cb;
63 }
64
65protected:
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080066 void
67 checkPolicy(const Data& data, const shared_ptr<security::v2::ValidationState>& state,
68 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 {
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080074 state->fail(security::v2::ValidationError::NO_ERROR);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000075 }
76 }
77
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080078 void
79 checkPolicy(const Interest& interest, const shared_ptr<security::v2::ValidationState>& state,
80 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 {
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080086 state->fail(security::v2::ValidationError::NO_ERROR);
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000087 }
88 }
89
90private:
91 function<bool(const Name&)> m_decide;
92};
93
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080094class DummyValidator : public security::v2::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)
99 : security::v2::Validator(make_unique<DummyValidationPolicy>(shouldAccept),
100 make_unique<security::v2::CertificateFetcherOffline>())
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000101 {
102 }
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -0800103
104 DummyValidationPolicy&
105 getPolicy()
106 {
107 return static_cast<DummyValidationPolicy&>(security::v2::Validator::getPolicy());
108 }
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000109};
110
111} // namespace tests
112} // namespace ndn
113
Davide Pesavento7e780642018-11-24 15:51:34 -0500114#endif // NDN_TESTS_UNIT_DUMMY_VALIDATOR_HPP