OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cert/mock_cert_verifier.h" | 5 #include "net/cert/mock_cert_verifier.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
10 #include "base/strings/pattern.h" | 11 #include "base/strings/pattern.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "net/cert/cert_status_flags.h" | 14 #include "net/cert/cert_status_flags.h" |
14 #include "net/cert/cert_verify_result.h" | 15 #include "net/cert/cert_verify_result.h" |
15 #include "net/cert/x509_certificate.h" | 16 #include "net/cert/x509_certificate.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
19 struct MockCertVerifier::Rule { | 20 struct MockCertVerifier::Rule { |
20 Rule(X509Certificate* cert, | 21 Rule(scoped_refptr<X509Certificate> cert_arg, |
21 const std::string& hostname, | 22 const std::string& hostname_arg, |
22 const CertVerifyResult& result, | 23 const CertVerifyResult& result_arg, |
23 int rv) | 24 int rv_arg) |
24 : cert(cert), | 25 : cert(std::move(cert_arg)), |
25 hostname(hostname), | 26 hostname(hostname_arg), |
26 result(result), | 27 result(result_arg), |
27 rv(rv) { | 28 rv(rv_arg) { |
28 DCHECK(cert); | 29 DCHECK(cert); |
29 DCHECK(result.verified_cert.get()); | 30 DCHECK(result.verified_cert); |
30 } | 31 } |
31 | 32 |
32 scoped_refptr<X509Certificate> cert; | 33 scoped_refptr<X509Certificate> cert; |
33 std::string hostname; | 34 std::string hostname; |
34 CertVerifyResult result; | 35 CertVerifyResult result; |
35 int rv; | 36 int rv; |
36 }; | 37 }; |
37 | 38 |
38 MockCertVerifier::MockCertVerifier() : default_result_(ERR_CERT_INVALID) {} | 39 MockCertVerifier::MockCertVerifier() : default_result_(ERR_CERT_INVALID) {} |
39 | 40 |
40 MockCertVerifier::~MockCertVerifier() {} | 41 MockCertVerifier::~MockCertVerifier() {} |
41 | 42 |
42 int MockCertVerifier::Verify(X509Certificate* cert, | 43 int MockCertVerifier::Verify(const RequestParams& params, |
43 const std::string& hostname, | |
44 const std::string& ocsp_response, | |
45 int flags, | |
46 CRLSet* crl_set, | 44 CRLSet* crl_set, |
47 CertVerifyResult* verify_result, | 45 CertVerifyResult* verify_result, |
48 const CompletionCallback& callback, | 46 const CompletionCallback& callback, |
49 std::unique_ptr<Request>* out_req, | 47 std::unique_ptr<Request>* out_req, |
50 const BoundNetLog& net_log) { | 48 const BoundNetLog& net_log) { |
51 RuleList::const_iterator it; | 49 RuleList::const_iterator it; |
52 for (it = rules_.begin(); it != rules_.end(); ++it) { | 50 for (it = rules_.begin(); it != rules_.end(); ++it) { |
53 // Check just the server cert. Intermediates will be ignored. | 51 // Check just the server cert. Intermediates will be ignored. |
54 if (!it->cert->Equals(cert)) | 52 if (!it->cert->Equals(params.certificate().get())) |
55 continue; | 53 continue; |
56 if (!base::MatchPattern(hostname, it->hostname)) | 54 if (!base::MatchPattern(params.hostname(), it->hostname)) |
57 continue; | 55 continue; |
58 *verify_result = it->result; | 56 *verify_result = it->result; |
59 return it->rv; | 57 return it->rv; |
60 } | 58 } |
61 | 59 |
62 // Fall through to the default. | 60 // Fall through to the default. |
63 verify_result->verified_cert = cert; | 61 verify_result->verified_cert = params.certificate(); |
64 verify_result->cert_status = MapNetErrorToCertStatus(default_result_); | 62 verify_result->cert_status = MapNetErrorToCertStatus(default_result_); |
65 return default_result_; | 63 return default_result_; |
66 } | 64 } |
67 | 65 |
68 void MockCertVerifier::AddResultForCert(X509Certificate* cert, | 66 void MockCertVerifier::AddResultForCert(scoped_refptr<X509Certificate> cert, |
69 const CertVerifyResult& verify_result, | 67 const CertVerifyResult& verify_result, |
70 int rv) { | 68 int rv) { |
71 AddResultForCertAndHost(cert, "*", verify_result, rv); | 69 AddResultForCertAndHost(std::move(cert), "*", verify_result, rv); |
72 } | 70 } |
73 | 71 |
74 void MockCertVerifier::AddResultForCertAndHost( | 72 void MockCertVerifier::AddResultForCertAndHost( |
75 X509Certificate* cert, | 73 scoped_refptr<X509Certificate> cert, |
76 const std::string& host_pattern, | 74 const std::string& host_pattern, |
77 const CertVerifyResult& verify_result, | 75 const CertVerifyResult& verify_result, |
78 int rv) { | 76 int rv) { |
79 Rule rule(cert, host_pattern, verify_result, rv); | 77 rules_.push_back(Rule(std::move(cert), host_pattern, verify_result, rv)); |
80 rules_.push_back(rule); | |
81 } | 78 } |
82 | 79 |
83 } // namespace net | 80 } // namespace net |
OLD | NEW |