blob: 7f52048918c5eef954681d2b19bae9b7ad65d9c8 [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/*
Alexander Afanasyev7e721412017-01-11 13:36:08 -08003 * Copyright (c) 2013-2017 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_SECURITY_V2_VALIDATOR_FIXTURE_HPP
23#define NDN_TESTS_SECURITY_V2_VALIDATOR_FIXTURE_HPP
24
25#include "security/v2/validator.hpp"
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080026#include "security/v2/certificate-fetcher-from-network.hpp"
Alexander Afanasyev7e721412017-01-11 13:36:08 -080027#include "util/dummy-client-face.hpp"
28
29#include "../../identity-management-time-fixture.hpp"
30
31#include <boost/lexical_cast.hpp>
32
33namespace ndn {
34namespace security {
35namespace v2 {
36namespace tests {
37
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080038template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork>
Alexander Afanasyev7e721412017-01-11 13:36:08 -080039class ValidatorFixture : public ndn::tests::IdentityManagementTimeFixture
40{
41public:
42 ValidatorFixture()
43 : face(io, {true, true})
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080044 , validator(make_unique<ValidationPolicy>(), make_unique<CertificateFetcher>(face))
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080045 , policy(static_cast<ValidationPolicy&>(validator.getPolicy()))
Alexander Afanasyev7e721412017-01-11 13:36:08 -080046 , cache(time::days(100))
47 {
48 processInterest = [this] (const Interest& interest) {
49 auto cert = cache.find(interest);
50 if (cert != nullptr) {
51 face.receive(*cert);
52 }
53 };
54 }
55
56 virtual
57 ~ValidatorFixture() = default;
58
59 template<class Packet>
60 void
61 validate(const Packet& packet, const std::string& msg, bool expectSuccess, int line)
62 {
63 std::string detailedInfo = msg + " on line " + to_string(line);
64 size_t nCallbacks = 0;
65 this->validator.validate(packet,
Alexander Afanasyev93338872017-01-30 22:37:00 -080066 [&] (const Packet&) {
67 ++nCallbacks;
68 BOOST_CHECK_MESSAGE(expectSuccess,
69 (expectSuccess ? "OK: " : "FAILED: ") + detailedInfo);
70 },
71 [&] (const Packet&, const ValidationError& error) {
72 ++nCallbacks;
73 BOOST_CHECK_MESSAGE(!expectSuccess,
74 (!expectSuccess ? "OK: " : "FAILED: ") + detailedInfo +
75 (expectSuccess ? " (" + boost::lexical_cast<std::string>(error) + ")" : ""));
76 });
Alexander Afanasyev7e721412017-01-11 13:36:08 -080077
78 mockNetworkOperations();
79 BOOST_CHECK_EQUAL(nCallbacks, 1);
80 }
81
82 void
83 mockNetworkOperations()
84 {
85 util::signal::ScopedConnection connection = face.onSendInterest.connect([this] (const Interest& interest) {
86 if (processInterest != nullptr) {
87 io.post(bind(processInterest, interest));
88 }
89 });
Alexander Afanasyev93338872017-01-30 22:37:00 -080090 advanceClocks(time::milliseconds(s_mockPeriod), s_mockTimes);
91 }
92
93 /** \brief undo clock advancement of mockNetworkOperations
94 */
95 void
96 rewindClockAfterValidation()
97 {
98 this->systemClock->advance(time::milliseconds(s_mockPeriod * s_mockTimes * -1));
Alexander Afanasyev7e721412017-01-11 13:36:08 -080099 }
100
101public:
102 util::DummyClientFace face;
103 std::function<void(const Interest& interest)> processInterest;
104 Validator validator;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800105 ValidationPolicy& policy;
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800106
107 CertificateCache cache;
Alexander Afanasyev93338872017-01-30 22:37:00 -0800108
109private:
110 const static int s_mockPeriod;
111 const static int s_mockTimes;
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800112};
113
Alexander Afanasyev93338872017-01-30 22:37:00 -0800114template<class ValidationPolicy, class CertificateFetcher>
115const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockPeriod = 250;
116
117template<class ValidationPolicy, class CertificateFetcher>
118const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockTimes = 200;
119
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800120template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork>
121class HierarchicalValidatorFixture : public ValidatorFixture<ValidationPolicy, CertificateFetcher>
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800122{
123public:
124 HierarchicalValidatorFixture()
125 {
126 identity = this->addIdentity("/Security/V2/ValidatorFixture");
127 subIdentity = this->addSubCertificate("/Security/V2/ValidatorFixture/Sub1", identity);
128 subSelfSignedIdentity = this->addIdentity("/Security/V2/ValidatorFixture/Sub1/Sub2");
129 otherIdentity = this->addIdentity("/Security/V2/OtherIdentity");
130
131 this->validator.loadAnchor("", Certificate(identity.getDefaultKey().getDefaultCertificate()));
132
133 this->cache.insert(identity.getDefaultKey().getDefaultCertificate());
134 this->cache.insert(subIdentity.getDefaultKey().getDefaultCertificate());
135 this->cache.insert(subSelfSignedIdentity.getDefaultKey().getDefaultCertificate());
136 this->cache.insert(otherIdentity.getDefaultKey().getDefaultCertificate());
137 }
138
139public:
140 Identity identity;
141 Identity subIdentity;
142 Identity subSelfSignedIdentity;
143 Identity otherIdentity;
144};
145
146#define VALIDATE_SUCCESS(packet, message) this->template validate(packet, message, true, __LINE__)
147#define VALIDATE_FAILURE(packet, message) this->template validate(packet, message, false, __LINE__)
148
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800149class DummyValidationState : public ValidationState
150{
151public:
152 ~DummyValidationState()
153 {
154 m_outcome = false;
155 }
156
157 void
158 fail(const ValidationError& error) override
159 {
160 // BOOST_TEST_MESSAGE(error);
161 m_outcome = false;
162 }
163
164private:
165 void
166 verifyOriginalPacket(const Certificate& trustedCert) override
167 {
168 // do nothing
169 }
170
171 void
172 bypassValidation() override
173 {
174 // do nothing
175 }
176};
177
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800178} // namespace tests
179} // namespace v2
180} // namespace security
181} // namespace ndn
182
183#endif // NDN_TESTS_SECURITY_V2_VALIDATOR_FIXTURE_HPP