mock_cert_net_fetcher.h 2.4 KB

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