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/base/mock_cert_verifier.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/string_util.h"
9 #include "net/base/cert_status_flags.h"
10 #include "net/base/cert_verify_result.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/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
);
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
,
43 CertVerifyResult
* verify_result
,
44 const CompletionCallback
& callback
,
45 RequestHandle
* out_req
,
46 const BoundNetLog
& net_log
) {
47 RuleList::const_iterator it
;
48 for (it
= rules_
.begin(); it
!= rules_
.end(); ++it
) {
49 // Check just the server cert. Intermediates will be ignored.
50 if (!it
->cert
->Equals(cert
))
52 if (!MatchPattern(hostname
, it
->hostname
))
54 *verify_result
= it
->result
;
58 // Fall through to the default.
59 verify_result
->verified_cert
= cert
;
60 verify_result
->cert_status
= MapNetErrorToCertStatus(default_result_
);
61 return default_result_
;
64 void MockCertVerifier::CancelRequest(RequestHandle req
) {
68 void MockCertVerifier::AddResultForCert(X509Certificate
* cert
,
69 const CertVerifyResult
& verify_result
,
71 AddResultForCertAndHost(cert
, "*", verify_result
, rv
);
74 void MockCertVerifier::AddResultForCertAndHost(
75 X509Certificate
* cert
,
76 const std::string
& host_pattern
,
77 const CertVerifyResult
& verify_result
,
79 Rule
rule(cert
, host_pattern
, verify_result
, rv
);
80 rules_
.push_back(rule
);