File: mock_cert_verifier.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (132 lines) | stat: -rw-r--r-- 4,564 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_CERT_MOCK_CERT_VERIFIER_H_
#define NET_CERT_MOCK_CERT_VERIFIER_H_

#include <list>
#include <memory>

#include "base/callback_list.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "net/base/completion_once_callback.h"
#include "net/cert/cert_verifier.h"
#include "net/cert/cert_verify_result.h"

namespace net {

class MockCertVerifier : public CertVerifier {
 public:
  // Creates a new MockCertVerifier. By default, any call to Verify() will
  // result in the cert status being flagged as CERT_STATUS_INVALID and return
  // an ERR_CERT_INVALID network error code. This behaviour can be overridden
  // by calling set_default_result() to change the default return value for
  // Verify() or by calling one of the AddResult*() methods to specifically
  // handle a certificate or certificate and host.
  MockCertVerifier();

  ~MockCertVerifier() override;

  // CertVerifier implementation
  int Verify(const RequestParams& params,
             CertVerifyResult* verify_result,
             CompletionOnceCallback callback,
             std::unique_ptr<Request>* out_req,
             const NetLogWithSource& net_log) override;
  void Verify2QwacBinding(
      const std::string& binding,
      const std::string& hostname,
      const scoped_refptr<net::X509Certificate>& tls_cert,
      base::OnceCallback<void(const scoped_refptr<net::X509Certificate>&)>
          callback,
      const net::NetLogWithSource& net_log) override;
  void SetConfig(const Config& config) override {}
  void AddObserver(Observer* observer) override;
  void RemoveObserver(Observer* observer) override;

  // Sets the default return value for Verify() for certificates/hosts that do
  // not have explicit results added via the AddResult*() methods.
  void set_default_result(int default_result) {
    default_result_ = default_result;
  }

  // Sets whether Verify() returns a result asynchronously.
  void set_async(bool async) { async_ = async; }

  // Adds a rule that will cause any call to Verify() for |cert| to return rv,
  // copying |verify_result| into the verified result.
  // Note: Only the primary certificate of |cert| is checked. Any intermediate
  // certificates will be ignored.
  void AddResultForCert(scoped_refptr<X509Certificate> cert,
                        const CertVerifyResult& verify_result,
                        int rv);

  // Same as AddResultForCert(), but further restricts it to only return for
  // hostnames that match |host_pattern|.
  void AddResultForCertAndHost(scoped_refptr<X509Certificate> cert,
                               const std::string& host_pattern,
                               const CertVerifyResult& verify_result,
                               int rv);

  // Clear all existing rules.
  void ClearRules();

  // Notify any registered observers of an OnCertVerifierChanged event.
  void SimulateOnCertVerifierChanged();

 private:
  struct Rule;
  using RuleList = std::list<Rule>;
  class MockRequest;
  friend class MockRequest;

  int VerifyImpl(const RequestParams& params, CertVerifyResult* verify_result);

  int default_result_ = ERR_CERT_INVALID;
  RuleList rules_;
  bool async_ = false;

  base::OnceClosureList request_list_;
  base::ObserverList<Observer> observers_;
};

// A MockCertVerifier that also records the RequestParams received for each
// verification attempt.
class ParamRecordingMockCertVerifier : public MockCertVerifier {
 public:
  ParamRecordingMockCertVerifier();
  ~ParamRecordingMockCertVerifier() override;

  int Verify(const RequestParams& params,
             CertVerifyResult* verify_result,
             CompletionOnceCallback callback,
             std::unique_ptr<Request>* out_req,
             const NetLogWithSource& net_log) override;

  const std::vector<RequestParams>& GetVerifyParams() const { return params_; }

 private:
  std::vector<RequestParams> params_;
};

class CertVerifierObserverCounter : public CertVerifier::Observer {
 public:
  explicit CertVerifierObserverCounter(CertVerifier* verifier);
  ~CertVerifierObserverCounter() override;

  // CertVerifier::Observer implementation:
  void OnCertVerifierChanged() override;

  unsigned change_count() const { return change_count_; }

 private:
  base::ScopedObservation<CertVerifier, CertVerifier::Observer> obs_{this};

  unsigned change_count_ = 0;
};

}  // namespace net

#endif  // NET_CERT_MOCK_CERT_VERIFIER_H_