blob: 5bd195eb883f010ea83ac89ab2d21a1f9392d25e [file] [log] [blame]
Alexander Afanasyev7e721412017-01-11 13:36:08 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08002/*
Davide Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev7e721412017-01-11 13:36:08 -08004 *
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_SECURITY_V2_VALIDATOR_FIXTURE_HPP
23#define NDN_TESTS_UNIT_SECURITY_V2_VALIDATOR_FIXTURE_HPP
Alexander Afanasyev7e721412017-01-11 13:36:08 -080024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/security/v2/validator.hpp"
26#include "ndn-cxx/security/v2/certificate-fetcher-from-network.hpp"
27#include "ndn-cxx/util/dummy-client-face.hpp"
Alexander Afanasyev7e721412017-01-11 13:36:08 -080028
Davide Pesavento7e780642018-11-24 15:51:34 -050029#include "tests/boost-test.hpp"
30#include "tests/unit/identity-management-time-fixture.hpp"
Alexander Afanasyev7e721412017-01-11 13:36:08 -080031
32#include <boost/lexical_cast.hpp>
33
34namespace ndn {
35namespace security {
36namespace v2 {
37namespace tests {
38
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080039template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork>
Alexander Afanasyev7e721412017-01-11 13:36:08 -080040class ValidatorFixture : public ndn::tests::IdentityManagementTimeFixture
41{
42public:
43 ValidatorFixture()
44 : face(io, {true, true})
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080045 , validator(make_unique<ValidationPolicy>(), make_unique<CertificateFetcher>(face))
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080046 , policy(static_cast<ValidationPolicy&>(validator.getPolicy()))
Davide Pesavento0f830802018-01-16 23:58:58 -050047 , cache(100_days)
Alexander Afanasyev7e721412017-01-11 13:36:08 -080048 {
49 processInterest = [this] (const Interest& interest) {
50 auto cert = cache.find(interest);
51 if (cert != nullptr) {
52 face.receive(*cert);
53 }
54 };
55 }
56
57 virtual
58 ~ValidatorFixture() = default;
59
60 template<class Packet>
61 void
62 validate(const Packet& packet, const std::string& msg, bool expectSuccess, int line)
63 {
64 std::string detailedInfo = msg + " on line " + to_string(line);
65 size_t nCallbacks = 0;
66 this->validator.validate(packet,
Alexander Afanasyev93338872017-01-30 22:37:00 -080067 [&] (const Packet&) {
68 ++nCallbacks;
69 BOOST_CHECK_MESSAGE(expectSuccess,
70 (expectSuccess ? "OK: " : "FAILED: ") + detailedInfo);
71 },
72 [&] (const Packet&, const ValidationError& error) {
73 ++nCallbacks;
74 BOOST_CHECK_MESSAGE(!expectSuccess,
75 (!expectSuccess ? "OK: " : "FAILED: ") + detailedInfo +
76 (expectSuccess ? " (" + boost::lexical_cast<std::string>(error) + ")" : ""));
77 });
Alexander Afanasyev7e721412017-01-11 13:36:08 -080078
79 mockNetworkOperations();
80 BOOST_CHECK_EQUAL(nCallbacks, 1);
81 }
82
83 void
84 mockNetworkOperations()
85 {
86 util::signal::ScopedConnection connection = face.onSendInterest.connect([this] (const Interest& interest) {
87 if (processInterest != nullptr) {
88 io.post(bind(processInterest, interest));
89 }
90 });
Alexander Afanasyev93338872017-01-30 22:37:00 -080091 advanceClocks(time::milliseconds(s_mockPeriod), s_mockTimes);
92 }
93
94 /** \brief undo clock advancement of mockNetworkOperations
95 */
96 void
97 rewindClockAfterValidation()
98 {
99 this->systemClock->advance(time::milliseconds(s_mockPeriod * s_mockTimes * -1));
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800100 }
101
102public:
103 util::DummyClientFace face;
104 std::function<void(const Interest& interest)> processInterest;
105 Validator validator;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800106 ValidationPolicy& policy;
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800107
108 CertificateCache cache;
Alexander Afanasyev93338872017-01-30 22:37:00 -0800109
110private:
111 const static int s_mockPeriod;
112 const static int s_mockTimes;
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800113};
114
Alexander Afanasyev93338872017-01-30 22:37:00 -0800115template<class ValidationPolicy, class CertificateFetcher>
116const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockPeriod = 250;
117
118template<class ValidationPolicy, class CertificateFetcher>
119const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockTimes = 200;
120
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800121template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork>
122class HierarchicalValidatorFixture : public ValidatorFixture<ValidationPolicy, CertificateFetcher>
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800123{
124public:
125 HierarchicalValidatorFixture()
126 {
127 identity = this->addIdentity("/Security/V2/ValidatorFixture");
128 subIdentity = this->addSubCertificate("/Security/V2/ValidatorFixture/Sub1", identity);
129 subSelfSignedIdentity = this->addIdentity("/Security/V2/ValidatorFixture/Sub1/Sub2");
130 otherIdentity = this->addIdentity("/Security/V2/OtherIdentity");
131
132 this->validator.loadAnchor("", Certificate(identity.getDefaultKey().getDefaultCertificate()));
133
134 this->cache.insert(identity.getDefaultKey().getDefaultCertificate());
135 this->cache.insert(subIdentity.getDefaultKey().getDefaultCertificate());
136 this->cache.insert(subSelfSignedIdentity.getDefaultKey().getDefaultCertificate());
137 this->cache.insert(otherIdentity.getDefaultKey().getDefaultCertificate());
138 }
139
140public:
141 Identity identity;
142 Identity subIdentity;
143 Identity subSelfSignedIdentity;
144 Identity otherIdentity;
145};
146
147#define VALIDATE_SUCCESS(packet, message) this->template validate(packet, message, true, __LINE__)
148#define VALIDATE_FAILURE(packet, message) this->template validate(packet, message, false, __LINE__)
149
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800150class DummyValidationState : public ValidationState
151{
152public:
153 ~DummyValidationState()
154 {
155 m_outcome = false;
156 }
157
158 void
159 fail(const ValidationError& error) override
160 {
161 // BOOST_TEST_MESSAGE(error);
162 m_outcome = false;
163 }
164
165private:
166 void
167 verifyOriginalPacket(const Certificate& trustedCert) override
168 {
169 // do nothing
170 }
171
172 void
173 bypassValidation() override
174 {
175 // do nothing
176 }
177};
178
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800179} // namespace tests
180} // namespace v2
181} // namespace security
182} // namespace ndn
183
Davide Pesavento7e780642018-11-24 15:51:34 -0500184#endif // NDN_TESTS_UNIT_SECURITY_V2_VALIDATOR_FIXTURE_HPP