1 // Copyright 2021 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_NET_FETCHER_H_ 6 #define NET_CERT_MOCK_CERT_NET_FETCHER_H_ 7 8 #include "net/cert/cert_net_fetcher.h" 9 10 #include "testing/gmock/include/gmock/gmock.h" 11 #include "third_party/boringssl/src/include/openssl/base.h" 12 13 namespace net { 14 15 // MockCertNetFetcher is an implementation of CertNetFetcher for testing. 16 class MockCertNetFetcher : public CertNetFetcher { 17 public: 18 MockCertNetFetcher(); 19 20 MOCK_METHOD0(Shutdown, void()); 21 MOCK_METHOD3(FetchCaIssuers, 22 std::unique_ptr<Request>(const GURL& url, 23 int timeout_milliseconds, 24 int max_response_bytes)); 25 MOCK_METHOD3(FetchCrl, 26 std::unique_ptr<Request>(const GURL& url, 27 int timeout_milliseconds, 28 int max_response_bytes)); 29 30 MOCK_METHOD3(FetchOcsp, 31 std::unique_ptr<Request>(const GURL& url, 32 int timeout_milliseconds, 33 int max_response_bytes)); 34 35 protected: 36 // Protected since CertNetFetcher is refcounted. 37 ~MockCertNetFetcher() override; 38 }; 39 40 // MockCertNetFetcherRequest gives back the indicated error and bytes. 41 class MockCertNetFetcherRequest : public CertNetFetcher::Request { 42 public: 43 MockCertNetFetcherRequest(Error error, std::vector<uint8_t> bytes); 44 ~MockCertNetFetcherRequest() override; 45 46 // Creates a CertNetFetcher::Request that completes with an error. 47 static std::unique_ptr<CertNetFetcher::Request> Create(Error error); 48 49 // Creates a CertNetFetcher::Request that completes with OK error code and 50 // the specified bytes. 51 static std::unique_ptr<CertNetFetcher::Request> Create( 52 std::vector<uint8_t> bytes); 53 54 // Creates a CertNetFetcher::Request that completes with OK error code and 55 // the specified CRYPTO_BUFFER data. 56 static std::unique_ptr<CertNetFetcher::Request> Create( 57 const CRYPTO_BUFFER* buffer); 58 59 void WaitForResult(Error* error, std::vector<uint8_t>* bytes) override; 60 61 private: 62 Error error_; 63 std::vector<uint8_t> bytes_; 64 bool did_consume_result_ = false; 65 }; 66 67 } // namespace net 68 69 #endif // NET_CERT_MOCK_CERT_NET_FETCHER_H_ 70