1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_CERT_MOCK_CERT_VERIFIER_H_ 6 #define NET_CERT_MOCK_CERT_VERIFIER_H_ 7 8 #include <list> 9 #include <memory> 10 11 #include "base/callback_list.h" 12 #include "base/observer_list.h" 13 #include "base/scoped_observation.h" 14 #include "net/base/completion_once_callback.h" 15 #include "net/cert/cert_verifier.h" 16 #include "net/cert/cert_verify_result.h" 17 18 namespace net { 19 20 class MockCertVerifier : public CertVerifier { 21 public: 22 // Creates a new MockCertVerifier. By default, any call to Verify() will 23 // result in the cert status being flagged as CERT_STATUS_INVALID and return 24 // an ERR_CERT_INVALID network error code. This behaviour can be overridden 25 // by calling set_default_result() to change the default return value for 26 // Verify() or by calling one of the AddResult*() methods to specifically 27 // handle a certificate or certificate and host. 28 MockCertVerifier(); 29 30 ~MockCertVerifier() override; 31 32 // CertVerifier implementation 33 int Verify(const RequestParams& params, 34 CertVerifyResult* verify_result, 35 CompletionOnceCallback callback, 36 std::unique_ptr<Request>* out_req, 37 const NetLogWithSource& net_log) override; SetConfig(const Config & config)38 void SetConfig(const Config& config) override {} 39 void AddObserver(Observer* observer) override; 40 void RemoveObserver(Observer* observer) override; 41 42 // Sets the default return value for Verify() for certificates/hosts that do 43 // not have explicit results added via the AddResult*() methods. set_default_result(int default_result)44 void set_default_result(int default_result) { 45 default_result_ = default_result; 46 } 47 48 // Sets whether Verify() returns a result asynchronously. set_async(bool async)49 void set_async(bool async) { async_ = async; } 50 51 // Adds a rule that will cause any call to Verify() for |cert| to return rv, 52 // copying |verify_result| into the verified result. 53 // Note: Only the primary certificate of |cert| is checked. Any intermediate 54 // certificates will be ignored. 55 void AddResultForCert(scoped_refptr<X509Certificate> cert, 56 const CertVerifyResult& verify_result, 57 int rv); 58 59 // Same as AddResultForCert(), but further restricts it to only return for 60 // hostnames that match |host_pattern|. 61 void AddResultForCertAndHost(scoped_refptr<X509Certificate> cert, 62 const std::string& host_pattern, 63 const CertVerifyResult& verify_result, 64 int rv); 65 66 // Clear all existing rules. 67 void ClearRules(); 68 69 // Notify any registered observers of an OnCertVerifierChanged event. 70 void SimulateOnCertVerifierChanged(); 71 72 private: 73 struct Rule; 74 using RuleList = std::list<Rule>; 75 class MockRequest; 76 friend class MockRequest; 77 78 int VerifyImpl(const RequestParams& params, CertVerifyResult* verify_result); 79 80 int default_result_ = ERR_CERT_INVALID; 81 RuleList rules_; 82 bool async_ = false; 83 84 base::OnceClosureList request_list_; 85 base::ObserverList<Observer> observers_; 86 }; 87 88 // A MockCertVerifier that also records the RequestParams received for each 89 // verification attempt. 90 class ParamRecordingMockCertVerifier : public MockCertVerifier { 91 public: 92 ParamRecordingMockCertVerifier(); 93 ~ParamRecordingMockCertVerifier() override; 94 95 int Verify(const RequestParams& params, 96 CertVerifyResult* verify_result, 97 CompletionOnceCallback callback, 98 std::unique_ptr<Request>* out_req, 99 const NetLogWithSource& net_log) override; 100 GetVerifyParams()101 const std::vector<RequestParams>& GetVerifyParams() const { return params_; } 102 103 private: 104 std::vector<RequestParams> params_; 105 }; 106 107 class CertVerifierObserverCounter : public CertVerifier::Observer { 108 public: 109 explicit CertVerifierObserverCounter(CertVerifier* verifier); 110 ~CertVerifierObserverCounter() override; 111 112 // CertVerifier::Observer implementation: 113 void OnCertVerifierChanged() override; 114 change_count()115 unsigned change_count() const { return change_count_; } 116 117 private: 118 base::ScopedObservation<CertVerifier, CertVerifier::Observer> obs_{this}; 119 120 unsigned change_count_ = 0; 121 }; 122 123 } // namespace net 124 125 #endif // NET_CERT_MOCK_CERT_VERIFIER_H_ 126