mock_proxy_host_resolver.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2019 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 SERVICES_PROXY_RESOLVER_MOCK_PROXY_HOST_RESOLVER_H_
  5. #define SERVICES_PROXY_RESOLVER_MOCK_PROXY_HOST_RESOLVER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <tuple>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/callback_forward.h"
  13. #include "net/base/ip_address.h"
  14. #include "net/proxy_resolution/proxy_resolve_dns_operation.h"
  15. #include "services/proxy_resolver/proxy_host_resolver.h"
  16. namespace net {
  17. class NetworkIsolationKey;
  18. } // namespace net
  19. namespace proxy_resolver {
  20. // Mock of ProxyHostResolver that resolves by default to 127.0.0.1, except for
  21. // hostnames with more specific results set using SetError() or SetResult().
  22. // Also allows returning failure for all results with FailAll().
  23. class MockProxyHostResolver : public ProxyHostResolver {
  24. public:
  25. // If |synchronous_mode| set to |true|, all results will be returned
  26. // synchronously. Otherwise, all results will be asynchronous.
  27. explicit MockProxyHostResolver(bool synchronous_mode = false);
  28. ~MockProxyHostResolver() override;
  29. std::unique_ptr<Request> CreateRequest(
  30. const std::string& hostname,
  31. net::ProxyResolveDnsOperation operation,
  32. const net::NetworkIsolationKey& network_isolation_key) override;
  33. void SetError(const std::string& hostname,
  34. net::ProxyResolveDnsOperation operation,
  35. const net::NetworkIsolationKey& network_isolation_key);
  36. void SetResult(const std::string& hostname,
  37. net::ProxyResolveDnsOperation operation,
  38. const net::NetworkIsolationKey& network_isolation_key,
  39. std::vector<net::IPAddress> result);
  40. void FailAll();
  41. unsigned num_resolve() const { return num_resolve_; }
  42. private:
  43. using ResultKey = std::tuple<std::string,
  44. net::ProxyResolveDnsOperation,
  45. net::NetworkIsolationKey>;
  46. class RequestImpl;
  47. // Any entry with an empty value signifies an ERR_NAME_NOT_RESOLVED result.
  48. std::map<ResultKey, std::vector<net::IPAddress>> results_;
  49. unsigned num_resolve_;
  50. bool fail_all_;
  51. bool synchronous_mode_;
  52. };
  53. // Mock of ProxyHostResolver that always hangs until cancelled.
  54. class HangingProxyHostResolver : public ProxyHostResolver {
  55. public:
  56. // If not null, |hang_callback| will be invoked whenever a request is started.
  57. explicit HangingProxyHostResolver(
  58. base::RepeatingClosure hang_callback = base::RepeatingClosure());
  59. ~HangingProxyHostResolver() override;
  60. std::unique_ptr<Request> CreateRequest(
  61. const std::string& hostname,
  62. net::ProxyResolveDnsOperation operation,
  63. const net::NetworkIsolationKey& network_isolation_key) override;
  64. int num_cancelled_requests() const { return num_cancelled_requests_; }
  65. void set_hang_callback(base::RepeatingClosure hang_callback) {
  66. hang_callback_ = hang_callback;
  67. }
  68. private:
  69. class RequestImpl;
  70. int num_cancelled_requests_;
  71. base::RepeatingClosure hang_callback_;
  72. };
  73. } // namespace proxy_resolver
  74. #endif // SERVICES_PROXY_RESOLVER_MOCK_PROXY_HOST_RESOLVER_H_