blob: aa512eaff96d07896cbb74af01c5bebaddaa1010 [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,
65 [&] (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 });
76
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 });
89 advanceClocks(time::milliseconds(250), 200);
90 }
91
92public:
93 util::DummyClientFace face;
94 std::function<void(const Interest& interest)> processInterest;
95 Validator validator;
96
97 CertificateCache cache;
98};
99
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800100template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork>
101class HierarchicalValidatorFixture : public ValidatorFixture<ValidationPolicy, CertificateFetcher>
Alexander Afanasyev7e721412017-01-11 13:36:08 -0800102{
103public:
104 HierarchicalValidatorFixture()
105 {
106 identity = this->addIdentity("/Security/V2/ValidatorFixture");
107 subIdentity = this->addSubCertificate("/Security/V2/ValidatorFixture/Sub1", identity);
108 subSelfSignedIdentity = this->addIdentity("/Security/V2/ValidatorFixture/Sub1/Sub2");
109 otherIdentity = this->addIdentity("/Security/V2/OtherIdentity");
110
111 this->validator.loadAnchor("", Certificate(identity.getDefaultKey().getDefaultCertificate()));
112
113 this->cache.insert(identity.getDefaultKey().getDefaultCertificate());
114 this->cache.insert(subIdentity.getDefaultKey().getDefaultCertificate());
115 this->cache.insert(subSelfSignedIdentity.getDefaultKey().getDefaultCertificate());
116 this->cache.insert(otherIdentity.getDefaultKey().getDefaultCertificate());
117 }
118
119public:
120 Identity identity;
121 Identity subIdentity;
122 Identity subSelfSignedIdentity;
123 Identity otherIdentity;
124};
125
126#define VALIDATE_SUCCESS(packet, message) this->template validate(packet, message, true, __LINE__)
127#define VALIDATE_FAILURE(packet, message) this->template validate(packet, message, false, __LINE__)
128
129} // namespace tests
130} // namespace v2
131} // namespace security
132} // namespace ndn
133
134#endif // NDN_TESTS_SECURITY_V2_VALIDATOR_FIXTURE_HPP