mock_cert_verifier.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.
  4. #ifndef NET_CERT_MOCK_CERT_VERIFIER_H_
  5. #define NET_CERT_MOCK_CERT_VERIFIER_H_
  6. #include <list>
  7. #include <memory>
  8. #include "base/callback_list.h"
  9. #include "net/base/completion_once_callback.h"
  10. #include "net/cert/cert_verifier.h"
  11. #include "net/cert/cert_verify_result.h"
  12. namespace net {
  13. class MockCertVerifier : public CertVerifier {
  14. public:
  15. // Creates a new MockCertVerifier. By default, any call to Verify() will
  16. // result in the cert status being flagged as CERT_STATUS_INVALID and return
  17. // an ERR_CERT_INVALID network error code. This behaviour can be overridden
  18. // by calling set_default_result() to change the default return value for
  19. // Verify() or by calling one of the AddResult*() methods to specifically
  20. // handle a certificate or certificate and host.
  21. MockCertVerifier();
  22. ~MockCertVerifier() override;
  23. // CertVerifier implementation
  24. int Verify(const RequestParams& params,
  25. CertVerifyResult* verify_result,
  26. CompletionOnceCallback callback,
  27. std::unique_ptr<Request>* out_req,
  28. const NetLogWithSource& net_log) override;
  29. void SetConfig(const Config& config) override {}
  30. // Sets the default return value for Verify() for certificates/hosts that do
  31. // not have explicit results added via the AddResult*() methods.
  32. void set_default_result(int default_result) {
  33. default_result_ = default_result;
  34. }
  35. // Sets whether Verify() returns a result asynchronously.
  36. void set_async(bool async) { async_ = async; }
  37. // Adds a rule that will cause any call to Verify() for |cert| to return rv,
  38. // copying |verify_result| into the verified result.
  39. // Note: Only the primary certificate of |cert| is checked. Any intermediate
  40. // certificates will be ignored.
  41. void AddResultForCert(scoped_refptr<X509Certificate> cert,
  42. const CertVerifyResult& verify_result,
  43. int rv);
  44. // Same as AddResultForCert(), but further restricts it to only return for
  45. // hostnames that match |host_pattern|.
  46. void AddResultForCertAndHost(scoped_refptr<X509Certificate> cert,
  47. const std::string& host_pattern,
  48. const CertVerifyResult& verify_result,
  49. int rv);
  50. // Clear all existing rules.
  51. void ClearRules();
  52. private:
  53. struct Rule;
  54. using RuleList = std::list<Rule>;
  55. class MockRequest;
  56. friend class MockRequest;
  57. int VerifyImpl(const RequestParams& params, CertVerifyResult* verify_result);
  58. int default_result_ = ERR_CERT_INVALID;
  59. RuleList rules_;
  60. bool async_ = false;
  61. base::OnceClosureList request_list_;
  62. };
  63. } // namespace net
  64. #endif // NET_CERT_MOCK_CERT_VERIFIER_H_