blob: 40e4bcbe6511db72f06548b13b14af8231ff5bb0 [file] [log] [blame]
Junxiao Shi0f3f0b42016-07-14 13:26:37 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 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_DUMMY_VALIDATOR_HPP
23#define NDN_TESTS_DUMMY_VALIDATOR_HPP
24
25#include "security/validator.hpp"
26
27namespace ndn {
28namespace tests {
29
30/** \brief a Validator for unit testing
31 */
32class DummyValidator : public Validator
33{
34public:
35 /** \brief constructor
36 * \param shouldAccept whether to accept or reject all validation requests
37 */
38 explicit
39 DummyValidator(bool shouldAccept = true)
40 {
41 this->setResult(shouldAccept);
42 }
43
44 /** \brief change the validation result
45 * \param shouldAccept whether to accept or reject all validation requests
46 */
47 void
48 setResult(bool shouldAccept)
49 {
50 m_decide = [shouldAccept] (const Name&) { return shouldAccept; };
51 }
52
53 /** \brief set a callback for validation
54 * \param cb a callback which receives the Interest/Data name for each validation request;
55 * its return value determines the validation result
56 */
57 void
58 setResultCallback(const function<bool(const Name&)>& cb)
59 {
60 m_decide = cb;
61 }
62
63protected:
64 virtual void
65 checkPolicy(const Interest& interest, int nSteps,
66 const OnInterestValidated& accept, const OnInterestValidationFailed& reject,
67 std::vector<shared_ptr<ValidationRequest>>&) override
68 {
69 if (m_decide(interest.getName())) {
70 accept(interest.shared_from_this());
71 }
72 else {
73 reject(interest.shared_from_this(), "");
74 }
75 }
76
77 virtual void
78 checkPolicy(const Data& data, int nSteps,
79 const OnDataValidated& accept, const OnDataValidationFailed& reject,
80 std::vector<shared_ptr<ValidationRequest>>&) override
81 {
82 if (m_decide(data.getName())) {
83 accept(data.shared_from_this());
84 }
85 else {
86 reject(data.shared_from_this(), "");
87 }
88 }
89
90private:
91 function<bool(const Name&)> m_decide;
92};
93
94/** \brief a DummyValidator initialized to reject all requests
95 */
96class DummyRejectValidator : public DummyValidator
97{
98public:
99 DummyRejectValidator()
100 : DummyValidator(false)
101 {
102 }
103};
104
105} // namespace tests
106} // namespace ndn
107
108#endif // NDN_TESTS_DUMMY_VALIDATOR_HPP