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/pattern.h"
9 #include "base/strings/string_util.h"
10 #include "net/base/net_errors.h"
11 #include "net/cert/cert_status_flags.h"
12 #include "net/cert/cert_verify_result.h"
13 #include "net/cert/x509_certificate.h"
17 struct MockCertVerifier::Rule
{
18 Rule(X509Certificate
* cert
,
19 const std::string
& hostname
,
20 const CertVerifyResult
& result
,
27 DCHECK(result
.verified_cert
.get());
30 scoped_refptr
<X509Certificate
> cert
;
32 CertVerifyResult result
;
36 MockCertVerifier::MockCertVerifier() : default_result_(ERR_CERT_INVALID
) {}
38 MockCertVerifier::~MockCertVerifier() {}
40 int MockCertVerifier::Verify(X509Certificate
* cert
,
41 const std::string
& hostname
,
42 const std::string
& ocsp_response
,
45 CertVerifyResult
* verify_result
,
46 const CompletionCallback
& callback
,
47 scoped_ptr
<Request
>* out_req
,
48 const BoundNetLog
& net_log
) {
49 RuleList::const_iterator it
;
50 for (it
= rules_
.begin(); it
!= rules_
.end(); ++it
) {
51 // Check just the server cert. Intermediates will be ignored.
52 if (!it
->cert
->Equals(cert
))
54 if (!base::MatchPattern(hostname
, it
->hostname
))
56 *verify_result
= it
->result
;
60 // Fall through to the default.
61 verify_result
->verified_cert
= cert
;
62 verify_result
->cert_status
= MapNetErrorToCertStatus(default_result_
);
63 return default_result_
;
66 void MockCertVerifier::AddResultForCert(X509Certificate
* cert
,
67 const CertVerifyResult
& verify_result
,
69 AddResultForCertAndHost(cert
, "*", verify_result
, rv
);
72 void MockCertVerifier::AddResultForCertAndHost(
73 X509Certificate
* cert
,
74 const std::string
& host_pattern
,
75 const CertVerifyResult
& verify_result
,
77 Rule
rule(cert
, host_pattern
, verify_result
, rv
);
78 rules_
.push_back(rule
);