Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / cert / mock_cert_verifier.cc
blob6ee23d6483c1c885cfaf9036d15af117666145f7
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"
15 namespace net {
17 struct MockCertVerifier::Rule {
18 Rule(X509Certificate* cert,
19 const std::string& hostname,
20 const CertVerifyResult& result,
21 int rv)
22 : cert(cert),
23 hostname(hostname),
24 result(result),
25 rv(rv) {
26 DCHECK(cert);
27 DCHECK(result.verified_cert.get());
30 scoped_refptr<X509Certificate> cert;
31 std::string hostname;
32 CertVerifyResult result;
33 int rv;
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,
43 int flags,
44 CRLSet* crl_set,
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))
53 continue;
54 if (!base::MatchPattern(hostname, it->hostname))
55 continue;
56 *verify_result = it->result;
57 return it->rv;
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,
68 int rv) {
69 AddResultForCertAndHost(cert, "*", verify_result, rv);
72 void MockCertVerifier::AddResultForCertAndHost(
73 X509Certificate* cert,
74 const std::string& host_pattern,
75 const CertVerifyResult& verify_result,
76 int rv) {
77 Rule rule(cert, host_pattern, verify_result, rv);
78 rules_.push_back(rule);
81 } // namespace net