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