transport_client_socket_pool_test_util.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2014 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. // Test methods and classes common to transport_client_socket_pool_unittest.cc
  5. // and websocket_transport_client_socket_pool_unittest.cc. If you find you need
  6. // to use these for another purpose, consider moving them to socket_test_util.h.
  7. #ifndef NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_
  8. #define NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_
  9. #include <memory>
  10. #include <string>
  11. #include "base/callback.h"
  12. #include "base/compiler_specific.h"
  13. #include "base/containers/queue.h"
  14. #include "base/containers/span.h"
  15. #include "base/memory/raw_ptr.h"
  16. #include "base/time/time.h"
  17. #include "net/base/address_list.h"
  18. #include "net/socket/client_socket_factory.h"
  19. #include "net/socket/client_socket_handle.h"
  20. #include "net/socket/socket_performance_watcher.h"
  21. #include "net/socket/stream_socket.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. namespace net {
  24. class ClientSocketHandle;
  25. class IPEndPoint;
  26. class NetLog;
  27. // Make sure |handle| sets load times correctly when it has been assigned a
  28. // reused socket. Uses gtest expectations.
  29. void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle);
  30. // Make sure |handle| sets load times correctly when it has been assigned a
  31. // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner
  32. // of a connection where |is_reused| is false may consider the connection
  33. // reused. Uses gtest expectations.
  34. void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle);
  35. // Set |address| to 1.1.1.1:80
  36. void SetIPv4Address(IPEndPoint* address);
  37. // Set |address| to [1:abcd::3:4:ff]:80
  38. void SetIPv6Address(IPEndPoint* address);
  39. // A ClientSocketFactory that produces sockets with the specified connection
  40. // behaviours.
  41. class MockTransportClientSocketFactory : public ClientSocketFactory {
  42. public:
  43. // The type of socket to create.
  44. enum class Type {
  45. // An unexpected socket. Causes a test failure if run.
  46. kUnexpected,
  47. // Connects successfully, synchronously.
  48. kSynchronous,
  49. // Fails to connect, synchronously.
  50. kFailing,
  51. // Connects successfully, asynchronously.
  52. kPending,
  53. // Fails to connect, asynchronously.
  54. kPendingFailing,
  55. // A delayed socket will pause before connecting through the message loop.
  56. kDelayed,
  57. // A delayed socket that fails.
  58. kDelayedFailing,
  59. // A stalled socket that never connects at all.
  60. kStalled,
  61. // A socket that can be triggered to connect explicitly, asynchronously.
  62. kTriggerable,
  63. };
  64. // A rule describing a mock `TransportClientSocket` to create.
  65. struct Rule {
  66. explicit Rule(Type type,
  67. absl::optional<std::vector<IPEndPoint>> expected_addresses =
  68. absl::nullopt,
  69. Error connect_error = ERR_CONNECTION_FAILED);
  70. ~Rule();
  71. Rule(const Rule&);
  72. Rule& operator=(const Rule&);
  73. Type type;
  74. // If specified, the addresses that should be passed into
  75. // `CreateTransportClientSocket`.
  76. absl::optional<std::vector<IPEndPoint>> expected_addresses;
  77. // The error to use if `type` specifies a failing connection. Ignored
  78. // otherwise.
  79. Error connect_error;
  80. };
  81. explicit MockTransportClientSocketFactory(NetLog* net_log);
  82. MockTransportClientSocketFactory(const MockTransportClientSocketFactory&) =
  83. delete;
  84. MockTransportClientSocketFactory& operator=(
  85. const MockTransportClientSocketFactory&) = delete;
  86. ~MockTransportClientSocketFactory() override;
  87. std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket(
  88. DatagramSocket::BindType bind_type,
  89. NetLog* net_log,
  90. const NetLogSource& source) override;
  91. std::unique_ptr<TransportClientSocket> CreateTransportClientSocket(
  92. const AddressList& addresses,
  93. std::unique_ptr<
  94. SocketPerformanceWatcher> /* socket_performance_watcher */,
  95. NetworkQualityEstimator* /* network_quality_estimator */,
  96. NetLog* /* net_log */,
  97. const NetLogSource& /* source */) override;
  98. std::unique_ptr<SSLClientSocket> CreateSSLClientSocket(
  99. SSLClientContext* context,
  100. std::unique_ptr<StreamSocket> nested_socket,
  101. const HostPortPair& host_and_port,
  102. const SSLConfig& ssl_config) override;
  103. int allocation_count() const { return allocation_count_; }
  104. // Set the default type for `CreateTransportClientSocket` calls, if all rules
  105. // (see `SetRules`) are consumed.
  106. void set_default_client_socket_type(Type type) { client_socket_type_ = type; }
  107. // Configures a list of rules for `CreateTransportClientSocket`. `rules` must
  108. // outlive the `MockTransportClientSocketFactory`. If
  109. // `CreateTransportClientSocket` is called more than `rules.size()` times,
  110. // excess calls will be treated as test failures, but this can be changed by
  111. // calling `set_default_client_socket_type` after this method.
  112. void SetRules(base::span<const Rule> rules);
  113. void set_delay(base::TimeDelta delay) { delay_ = delay; }
  114. // If one or more `kTriggerable` socket has already been created, then returns
  115. // a `OnceClosure` that can be called to cause the first not-yet-connected one
  116. // to connect. If no `kTriggerable` sockets have been created yet, wait for
  117. // one to be created before returning the `OnceClosure`. This method should be
  118. // called the same number of times as `kTriggerable` sockets are created in
  119. // the test.
  120. base::OnceClosure WaitForTriggerableSocketCreation();
  121. private:
  122. raw_ptr<NetLog> net_log_;
  123. int allocation_count_ = 0;
  124. Type client_socket_type_ = Type::kSynchronous;
  125. base::span<const Rule> rules_;
  126. base::TimeDelta delay_;
  127. base::queue<base::OnceClosure> triggerable_sockets_;
  128. base::OnceClosure run_loop_quit_closure_;
  129. };
  130. } // namespace net
  131. #endif // NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_