blob: 8fac880bb351f90932a44fab227f07dd2986d815 [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"
26#include "util/dummy-client-face.hpp"
27
28#include "../../identity-management-time-fixture.hpp"
29
30#include <boost/lexical_cast.hpp>
31
32namespace ndn {
33namespace security {
34namespace v2 {
35namespace tests {
36
37template<class ValidationPolicy>
38class ValidatorFixture : public ndn::tests::IdentityManagementTimeFixture
39{
40public:
41 ValidatorFixture()
42 : face(io, {true, true})
43 , validator(make_unique<ValidationPolicy>(), &face)
44 , cache(time::days(100))
45 {
46 processInterest = [this] (const Interest& interest) {
47 auto cert = cache.find(interest);
48 if (cert != nullptr) {
49 face.receive(*cert);
50 }
51 };
52 }
53
54 virtual
55 ~ValidatorFixture() = default;
56
57 template<class Packet>
58 void
59 validate(const Packet& packet, const std::string& msg, bool expectSuccess, int line)
60 {
61 std::string detailedInfo = msg + " on line " + to_string(line);
62 size_t nCallbacks = 0;
63 this->validator.validate(packet,
64 [&] (const Packet&) {
65 ++nCallbacks;
66 BOOST_CHECK_MESSAGE(expectSuccess,
67 (expectSuccess ? "OK: " : "FAILED: ") + detailedInfo);
68 },
69 [&] (const Packet&, const ValidationError& error) {
70 ++nCallbacks;
71 BOOST_CHECK_MESSAGE(!expectSuccess,
72 (!expectSuccess ? "OK: " : "FAILED: ") + detailedInfo +
73 (expectSuccess ? " (" + boost::lexical_cast<std::string>(error) + ")" : ""));
74 });
75
76 mockNetworkOperations();
77 BOOST_CHECK_EQUAL(nCallbacks, 1);
78 }
79
80 void
81 mockNetworkOperations()
82 {
83 util::signal::ScopedConnection connection = face.onSendInterest.connect([this] (const Interest& interest) {
84 if (processInterest != nullptr) {
85 io.post(bind(processInterest, interest));
86 }
87 });
88 advanceClocks(time::milliseconds(250), 200);
89 }
90
91public:
92 util::DummyClientFace face;
93 std::function<void(const Interest& interest)> processInterest;
94 Validator validator;
95
96 CertificateCache cache;
97};
98
99template<class ValidationPolicy>
100class HierarchicalValidatorFixture : public ValidatorFixture<ValidationPolicy>
101{
102public:
103 HierarchicalValidatorFixture()
104 {
105 identity = this->addIdentity("/Security/V2/ValidatorFixture");
106 subIdentity = this->addSubCertificate("/Security/V2/ValidatorFixture/Sub1", identity);
107 subSelfSignedIdentity = this->addIdentity("/Security/V2/ValidatorFixture/Sub1/Sub2");
108 otherIdentity = this->addIdentity("/Security/V2/OtherIdentity");
109
110 this->validator.loadAnchor("", Certificate(identity.getDefaultKey().getDefaultCertificate()));
111
112 this->cache.insert(identity.getDefaultKey().getDefaultCertificate());
113 this->cache.insert(subIdentity.getDefaultKey().getDefaultCertificate());
114 this->cache.insert(subSelfSignedIdentity.getDefaultKey().getDefaultCertificate());
115 this->cache.insert(otherIdentity.getDefaultKey().getDefaultCertificate());
116 }
117
118public:
119 Identity identity;
120 Identity subIdentity;
121 Identity subSelfSignedIdentity;
122 Identity otherIdentity;
123};
124
125#define VALIDATE_SUCCESS(packet, message) this->template validate(packet, message, true, __LINE__)
126#define VALIDATE_FAILURE(packet, message) this->template validate(packet, message, false, __LINE__)
127
128} // namespace tests
129} // namespace v2
130} // namespace security
131} // namespace ndn
132
133#endif // NDN_TESTS_SECURITY_V2_VALIDATOR_FIXTURE_HPP