| 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, |
| 21 const std::string& hostname, | 22 const std::string& hostname, |
| 22 const CertVerifyResult& result, | 23 const CertVerifyResult& result, |
| 23 int rv) | 24 int rv) |
| 24 : cert(cert), | 25 : cert(std::move(cert)), hostname(hostname), result(result), rv(rv) { |
| 25 hostname(hostname), | |
| 26 result(result), | |
| 27 rv(rv) { | |
| 28 DCHECK(cert); | 26 DCHECK(cert); |
| 29 DCHECK(result.verified_cert.get()); | 27 DCHECK(result.verified_cert); |
| 30 } | 28 } |
| 31 | 29 |
| 32 scoped_refptr<X509Certificate> cert; | 30 scoped_refptr<X509Certificate> cert; |
| 33 std::string hostname; | 31 std::string hostname; |
| 34 CertVerifyResult result; | 32 CertVerifyResult result; |
| 35 int rv; | 33 int rv; |
| 36 }; | 34 }; |
| 37 | 35 |
| 38 MockCertVerifier::MockCertVerifier() : default_result_(ERR_CERT_INVALID) {} | 36 MockCertVerifier::MockCertVerifier() : default_result_(ERR_CERT_INVALID) {} |
| 39 | 37 |
| 40 MockCertVerifier::~MockCertVerifier() {} | 38 MockCertVerifier::~MockCertVerifier() {} |
| 41 | 39 |
| 42 int MockCertVerifier::Verify(X509Certificate* cert, | 40 int MockCertVerifier::Verify(const CertVerifier::RequestParams& params, |
| 43 const std::string& hostname, | |
| 44 const std::string& ocsp_response, | |
| 45 int flags, | |
| 46 CRLSet* crl_set, | 41 CRLSet* crl_set, |
| 47 CertVerifyResult* verify_result, | 42 CertVerifyResult* verify_result, |
| 48 const CompletionCallback& callback, | 43 const CompletionCallback& callback, |
| 49 std::unique_ptr<Request>* out_req, | 44 std::unique_ptr<Request>* out_req, |
| 50 const BoundNetLog& net_log) { | 45 const BoundNetLog& net_log) { |
| 51 RuleList::const_iterator it; | 46 RuleList::const_iterator it; |
| 52 for (it = rules_.begin(); it != rules_.end(); ++it) { | 47 for (it = rules_.begin(); it != rules_.end(); ++it) { |
| 53 // Check just the server cert. Intermediates will be ignored. | 48 // Check just the server cert. Intermediates will be ignored. |
| 54 if (!it->cert->Equals(cert)) | 49 if (!it->cert->Equals(params.certificate().get())) |
| 55 continue; | 50 continue; |
| 56 if (!base::MatchPattern(hostname, it->hostname)) | 51 if (!base::MatchPattern(params.hostname(), it->hostname)) |
| 57 continue; | 52 continue; |
| 58 *verify_result = it->result; | 53 *verify_result = it->result; |
| 59 return it->rv; | 54 return it->rv; |
| 60 } | 55 } |
| 61 | 56 |
| 62 // Fall through to the default. | 57 // Fall through to the default. |
| 63 verify_result->verified_cert = cert; | 58 verify_result->verified_cert = params.certificate(); |
| 64 verify_result->cert_status = MapNetErrorToCertStatus(default_result_); | 59 verify_result->cert_status = MapNetErrorToCertStatus(default_result_); |
| 65 return default_result_; | 60 return default_result_; |
| 66 } | 61 } |
| 67 | 62 |
| 68 void MockCertVerifier::AddResultForCert(X509Certificate* cert, | 63 void MockCertVerifier::AddResultForCert(scoped_refptr<X509Certificate> cert, |
| 69 const CertVerifyResult& verify_result, | 64 const CertVerifyResult& verify_result, |
| 70 int rv) { | 65 int rv) { |
| 71 AddResultForCertAndHost(cert, "*", verify_result, rv); | 66 AddResultForCertAndHost(std::move(cert), "*", verify_result, rv); |
| 72 } | 67 } |
| 73 | 68 |
| 74 void MockCertVerifier::AddResultForCertAndHost( | 69 void MockCertVerifier::AddResultForCertAndHost( |
| 75 X509Certificate* cert, | 70 scoped_refptr<X509Certificate> cert, |
| 76 const std::string& host_pattern, | 71 const std::string& host_pattern, |
| 77 const CertVerifyResult& verify_result, | 72 const CertVerifyResult& verify_result, |
| 78 int rv) { | 73 int rv) { |
| 79 Rule rule(cert, host_pattern, verify_result, rv); | 74 rules_.push_back(Rule(std::move(cert), host_pattern, verify_result, rv)); |
| 80 rules_.push_back(rule); | |
| 81 } | 75 } |
| 82 | 76 |
| 83 } // namespace net | 77 } // namespace net |
| OLD | NEW |