/src/mozilla-central/security/pkix/test/gtest/pkixgtest.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This code is made available to you under your choice of the following sets |
4 | | * of licensing terms: |
5 | | */ |
6 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
9 | | */ |
10 | | /* Copyright 2014 Mozilla Contributors |
11 | | * |
12 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
13 | | * you may not use this file except in compliance with the License. |
14 | | * You may obtain a copy of the License at |
15 | | * |
16 | | * http://www.apache.org/licenses/LICENSE-2.0 |
17 | | * |
18 | | * Unless required by applicable law or agreed to in writing, software |
19 | | * distributed under the License is distributed on an "AS IS" BASIS, |
20 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
21 | | * See the License for the specific language governing permissions and |
22 | | * limitations under the License. |
23 | | */ |
24 | | #ifndef mozilla_pkix_pkixgtest_h |
25 | | #define mozilla_pkix_pkixgtest_h |
26 | | |
27 | | #include <ostream> |
28 | | |
29 | | #if defined(__clang__) |
30 | | #pragma clang diagnostic push |
31 | | #pragma clang diagnostic ignored "-Wdeprecated" |
32 | | #pragma clang diagnostic ignored "-Wmissing-noreturn" |
33 | | #pragma clang diagnostic ignored "-Wshift-sign-overflow" |
34 | | #pragma clang diagnostic ignored "-Wsign-conversion" |
35 | | #pragma clang diagnostic ignored "-Wundef" |
36 | | #elif defined(__GNUC__) |
37 | | #pragma GCC diagnostic push |
38 | | #pragma GCC diagnostic ignored "-Wextra" |
39 | | #elif defined(_MSC_VER) |
40 | | #pragma warning(push, 3) |
41 | | // C4224: Nonstandard extension used: formal parameter 'X' was previously |
42 | | // defined as a type. |
43 | | #pragma warning(disable: 4224) |
44 | | // C4826: Conversion from 'type1 ' to 'type_2' is sign - extended. This may |
45 | | // cause unexpected runtime behavior. |
46 | | #pragma warning(disable: 4826) |
47 | | #endif |
48 | | |
49 | | #include "gtest/gtest.h" |
50 | | |
51 | | #if defined(__clang__) |
52 | | #pragma clang diagnostic pop |
53 | | #elif defined(__GNUC__) |
54 | | #pragma GCC diagnostic pop |
55 | | #elif defined(_MSC_VER) |
56 | | #pragma warning(pop) |
57 | | #endif |
58 | | |
59 | | #include "pkix/pkix.h" |
60 | | #include "pkixtestutil.h" |
61 | | |
62 | | // PrintTo must be in the same namespace as the type we're overloading it for. |
63 | | namespace mozilla { namespace pkix { |
64 | | |
65 | | inline void |
66 | | PrintTo(const Result& result, ::std::ostream* os) |
67 | 0 | { |
68 | 0 | const char* stringified = MapResultToName(result); |
69 | 0 | if (stringified) { |
70 | 0 | *os << stringified; |
71 | 0 | } else { |
72 | 0 | *os << "mozilla::pkix::Result(" << static_cast<unsigned int>(result) << ")"; |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | } } // namespace mozilla::pkix |
77 | | |
78 | | namespace mozilla { namespace pkix { namespace test { |
79 | | |
80 | | extern const std::time_t oneDayBeforeNow; |
81 | | extern const std::time_t oneDayAfterNow; |
82 | | extern const std::time_t twoDaysBeforeNow; |
83 | | extern const std::time_t twoDaysAfterNow; |
84 | | extern const std::time_t tenDaysBeforeNow; |
85 | | extern const std::time_t tenDaysAfterNow; |
86 | | |
87 | | |
88 | | class EverythingFailsByDefaultTrustDomain : public TrustDomain |
89 | | { |
90 | | public: |
91 | | Result GetCertTrust(EndEntityOrCA, const CertPolicyId&, |
92 | | Input, /*out*/ TrustLevel&) override |
93 | 0 | { |
94 | 0 | ADD_FAILURE(); |
95 | 0 | return NotReached("GetCertTrust should not be called", |
96 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
97 | 0 | } |
98 | | |
99 | | Result FindIssuer(Input, IssuerChecker&, Time) override |
100 | 0 | { |
101 | 0 | ADD_FAILURE(); |
102 | 0 | return NotReached("FindIssuer should not be called", |
103 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
104 | 0 | } |
105 | | |
106 | | Result CheckRevocation(EndEntityOrCA, const CertID&, Time, Duration, |
107 | | /*optional*/ const Input*, |
108 | | /*optional*/ const Input*) override |
109 | 0 | { |
110 | 0 | ADD_FAILURE(); |
111 | 0 | return NotReached("CheckRevocation should not be called", |
112 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
113 | 0 | } |
114 | | |
115 | | Result IsChainValid(const DERArray&, Time, const CertPolicyId&) override |
116 | 0 | { |
117 | 0 | ADD_FAILURE(); |
118 | 0 | return NotReached("IsChainValid should not be called", |
119 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
120 | 0 | } |
121 | | |
122 | | Result DigestBuf(Input, DigestAlgorithm, /*out*/ uint8_t*, size_t) override |
123 | 0 | { |
124 | 0 | ADD_FAILURE(); |
125 | 0 | return NotReached("DigestBuf should not be called", |
126 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
127 | 0 | } |
128 | | |
129 | | Result CheckSignatureDigestAlgorithm(DigestAlgorithm, |
130 | | EndEntityOrCA, |
131 | | Time) override |
132 | 0 | { |
133 | 0 | ADD_FAILURE(); |
134 | 0 | return NotReached("CheckSignatureDigestAlgorithm should not be called", |
135 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
136 | 0 | } |
137 | | |
138 | | Result CheckECDSACurveIsAcceptable(EndEntityOrCA, NamedCurve) override |
139 | 0 | { |
140 | 0 | ADD_FAILURE(); |
141 | 0 | return NotReached("CheckECDSACurveIsAcceptable should not be called", |
142 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
143 | 0 | } |
144 | | |
145 | | Result VerifyECDSASignedDigest(const SignedDigest&, Input) override |
146 | 0 | { |
147 | 0 | ADD_FAILURE(); |
148 | 0 | return NotReached("VerifyECDSASignedDigest should not be called", |
149 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
150 | 0 | } |
151 | | |
152 | | Result CheckRSAPublicKeyModulusSizeInBits(EndEntityOrCA, unsigned int) |
153 | | override |
154 | 0 | { |
155 | 0 | ADD_FAILURE(); |
156 | 0 | return NotReached("CheckRSAPublicKeyModulusSizeInBits should not be called", |
157 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
158 | 0 | } |
159 | | |
160 | | Result VerifyRSAPKCS1SignedDigest(const SignedDigest&, Input) override |
161 | 0 | { |
162 | 0 | ADD_FAILURE(); |
163 | 0 | return NotReached("VerifyRSAPKCS1SignedDigest should not be called", |
164 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
165 | 0 | } |
166 | | |
167 | | Result CheckValidityIsAcceptable(Time, Time, EndEntityOrCA, KeyPurposeId) |
168 | | override |
169 | 0 | { |
170 | 0 | ADD_FAILURE(); |
171 | 0 | return NotReached("CheckValidityIsAcceptable should not be called", |
172 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
173 | 0 | } |
174 | | |
175 | | Result NetscapeStepUpMatchesServerAuth(Time, bool&) override |
176 | 0 | { |
177 | 0 | ADD_FAILURE(); |
178 | 0 | return NotReached("NetscapeStepUpMatchesServerAuth should not be called", |
179 | 0 | Result::FATAL_ERROR_LIBRARY_FAILURE); |
180 | 0 | } |
181 | | |
182 | | virtual void NoteAuxiliaryExtension(AuxiliaryExtension, Input) override |
183 | 0 | { |
184 | 0 | ADD_FAILURE(); |
185 | 0 | } |
186 | | }; |
187 | | |
188 | | class DefaultCryptoTrustDomain : public EverythingFailsByDefaultTrustDomain |
189 | | { |
190 | | Result DigestBuf(Input item, DigestAlgorithm digestAlg, |
191 | | /*out*/ uint8_t* digestBuf, size_t digestBufLen) override |
192 | 0 | { |
193 | 0 | return TestDigestBuf(item, digestAlg, digestBuf, digestBufLen); |
194 | 0 | } |
195 | | |
196 | | Result CheckSignatureDigestAlgorithm(DigestAlgorithm, EndEntityOrCA, Time) |
197 | | override |
198 | 0 | { |
199 | 0 | return Success; |
200 | 0 | } |
201 | | |
202 | | Result CheckECDSACurveIsAcceptable(EndEntityOrCA, NamedCurve) override |
203 | 0 | { |
204 | 0 | return Success; |
205 | 0 | } |
206 | | |
207 | | Result VerifyECDSASignedDigest(const SignedDigest& signedDigest, |
208 | | Input subjectPublicKeyInfo) override |
209 | 0 | { |
210 | 0 | return TestVerifyECDSASignedDigest(signedDigest, subjectPublicKeyInfo); |
211 | 0 | } |
212 | | |
213 | | Result CheckRSAPublicKeyModulusSizeInBits(EndEntityOrCA, unsigned int) |
214 | | override |
215 | 0 | { |
216 | 0 | return Success; |
217 | 0 | } |
218 | | |
219 | | Result VerifyRSAPKCS1SignedDigest(const SignedDigest& signedDigest, |
220 | | Input subjectPublicKeyInfo) override |
221 | 0 | { |
222 | 0 | return TestVerifyRSAPKCS1SignedDigest(signedDigest, subjectPublicKeyInfo); |
223 | 0 | } |
224 | | |
225 | | Result CheckValidityIsAcceptable(Time, Time, EndEntityOrCA, KeyPurposeId) |
226 | | override |
227 | 0 | { |
228 | 0 | return Success; |
229 | 0 | } |
230 | | |
231 | | Result NetscapeStepUpMatchesServerAuth(Time, /*out*/ bool& matches) override |
232 | 0 | { |
233 | 0 | matches = true; |
234 | 0 | return Success; |
235 | 0 | } |
236 | | |
237 | | void NoteAuxiliaryExtension(AuxiliaryExtension, Input) override |
238 | 0 | { |
239 | 0 | } |
240 | | }; |
241 | | |
242 | | class DefaultNameMatchingPolicy : public NameMatchingPolicy |
243 | | { |
244 | | public: |
245 | | virtual Result FallBackToCommonName( |
246 | | Time, /*out*/ FallBackToSearchWithinSubject& fallBackToCommonName) override |
247 | 0 | { |
248 | 0 | fallBackToCommonName = FallBackToSearchWithinSubject::Yes; |
249 | 0 | return Success; |
250 | 0 | } |
251 | | }; |
252 | | |
253 | | } } } // namespace mozilla::pkix::test |
254 | | |
255 | | #endif // mozilla_pkix_pkixgtest_h |