blob: fb7edda51379a5dd592f704edbae0183f97364b6 [file] [log] [blame]
Alexander Afanasyev7e721412017-01-11 13:36:08 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * 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 Afanasyev7e721412017-01-11 13:36:08 -080045 , cache(time::days(100))
46 {
47 processInterest = [this] (const Interest& interest) {
48 auto cert = cache.find(interest);
49 if (cert != nullptr) {
50 face.receive(*cert);
51 }
52 };
53 }
54
55 virtual
56 ~ValidatorFixture() = default;
57
58 template<class Packet>
59 void
60 validate(const Packet& packet, const std::string& msg, bool expectSuccess, int line)
61 {
62 std::string detailedInfo = msg + " on line " + to_string(line);
63 size_t nCallbacks = 0;
64 this->validator.validate(packet,
Alexander Afanasyev93338872017-01-30 22:37:00 -080065 [&] (const Packet&) {
66 ++nCallbacks;
67 BOOST_CHECK_MESSAGE(expectSuccess,
68 (expectSuccess ? "OK: " : "FAILED: ") + detailedInfo);
69 },
70 [&] (const Packet&, const ValidationError& error) {
71 ++nCallbacks;
72 BOOST_CHECK_MESSAGE(!expectSuccess,
73 (!expectSuccess ? "OK: " : "FAILED: ") + detailedInfo +
74 (expectSuccess ? " (" + boost::lexical_cast<std::string>(error) + ")" : ""));
75 });
Alexander Afanasyev7e721412017-01-11 13:36:08 -080076
77 mockNetworkOperations();
78 BOOST_CHECK_EQUAL(nCallbacks, 1);
79 }
80
81 void
82 mockNetworkOperations()
83 {
84 util::signal::ScopedConnection connection = face.onSendInterest.connect([this] (const Interest& interest) {
85 if (processInterest != nullptr) {
86 io.post(bind(processInterest, interest));
87 }
88 });
Alexander Afanasyev93338872017-01-30 22:37:00 -080089 advanceClocks(time::milliseconds(s_mockPeriod), s_mockTimes);
90 }
91
92 /** \brief undo clock advancement of mockNetworkOperations
93 */
94 void
95 rewindClockAfterValidation()
96 {
97 this->systemClock->advance(time::milliseconds(s_mockPeriod * s_mockTimes * -1));
Alexander Afanasyev7e721412017-01-11 13:36:08 -080098 }
99
100public:
101 util::DummyClientFace face;
102 std::function<void(const Interest& interest)> processInterest;
103 Validator validator;
104
105 CertificateCache cache;
Alexander Afanasyev93338872017-01-30 22:37:00 -0800106
107private:
108 const static int s_mockPeriod;
109 const static int s_mockTimes;
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800110};
111
Alexander Afanasyev93338872017-01-30 22:37:00 -0800112template<class ValidationPolicy, class CertificateFetcher>
113const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockPeriod = 250;
114
115template<class ValidationPolicy, class CertificateFetcher>
116const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockTimes = 200;
117
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800118template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork>
119class HierarchicalValidatorFixture : public ValidatorFixture<ValidationPolicy, CertificateFetcher>
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800120{
121public:
122 HierarchicalValidatorFixture()
123 {
124 identity = this->addIdentity("/Security/V2/ValidatorFixture");
125 subIdentity = this->addSubCertificate("/Security/V2/ValidatorFixture/Sub1", identity);
126 subSelfSignedIdentity = this->addIdentity("/Security/V2/ValidatorFixture/Sub1/Sub2");
127 otherIdentity = this->addIdentity("/Security/V2/OtherIdentity");
128
129 this->validator.loadAnchor("", Certificate(identity.getDefaultKey().getDefaultCertificate()));
130
131 this->cache.insert(identity.getDefaultKey().getDefaultCertificate());
132 this->cache.insert(subIdentity.getDefaultKey().getDefaultCertificate());
133 this->cache.insert(subSelfSignedIdentity.getDefaultKey().getDefaultCertificate());
134 this->cache.insert(otherIdentity.getDefaultKey().getDefaultCertificate());
135 }
136
137public:
138 Identity identity;
139 Identity subIdentity;
140 Identity subSelfSignedIdentity;
141 Identity otherIdentity;
142};
143
144#define VALIDATE_SUCCESS(packet, message) this->template validate(packet, message, true, __LINE__)
145#define VALIDATE_FAILURE(packet, message) this->template validate(packet, message, false, __LINE__)
146
147} // namespace tests
148} // namespace v2
149} // namespace security
150} // namespace ndn
151
152#endif // NDN_TESTS_SECURITY_V2_VALIDATOR_FIXTURE_HPP