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
3 // found in the LICENSE file.
5 #include "net/cert/mock_cert_verifier.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/strings/string_util.h"
9 #include "net/base/net_errors.h"
10 #include "net/cert/cert_status_flags.h"
11 #include "net/cert/cert_verify_result.h"
12 #include "net/cert/x509_certificate.h"
16 struct MockCertVerifier::Rule
{
17 Rule(X509Certificate
* cert
,
18 const std::string
& hostname
,
19 const CertVerifyResult
& result
,
26 DCHECK(result
.verified_cert
.get());
29 scoped_refptr
<X509Certificate
> cert
;
31 CertVerifyResult result
;
35 MockCertVerifier::MockCertVerifier() : default_result_(ERR_CERT_INVALID
) {}
37 MockCertVerifier::~MockCertVerifier() {}
39 int MockCertVerifier::Verify(X509Certificate
* cert
,
40 const std::string
& hostname
,
41 const std::string
& ocsp_response
,
44 CertVerifyResult
* verify_result
,
45 const CompletionCallback
& callback
,
46 scoped_ptr
<Request
>* out_req
,
47 const BoundNetLog
& net_log
) {
48 RuleList::const_iterator it
;
49 for (it
= rules_
.begin(); it
!= rules_
.end(); ++it
) {
50 // Check just the server cert. Intermediates will be ignored.
51 if (!it
->cert
->Equals(cert
))
53 if (!MatchPattern(hostname
, it
->hostname
))
55 *verify_result
= it
->result
;
59 // Fall through to the default.
60 verify_result
->verified_cert
= cert
;
61 verify_result
->cert_status
= MapNetErrorToCertStatus(default_result_
);
62 return default_result_
;
65 void MockCertVerifier::AddResultForCert(X509Certificate
* cert
,
66 const CertVerifyResult
& verify_result
,
68 AddResultForCertAndHost(cert
, "*", verify_result
, rv
);
71 void MockCertVerifier::AddResultForCertAndHost(
72 X509Certificate
* cert
,
73 const std::string
& host_pattern
,
74 const CertVerifyResult
& verify_result
,
76 Rule
rule(cert
, host_pattern
, verify_result
, rv
);
77 rules_
.push_back(rule
);