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 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 <string> 14 15 #include "base/callback.h" 16 #include "base/compiler_specific.h" 17 #include "base/containers/queue.h" 18 #include "base/macros.h" 19 #include "base/time/time.h" 20 #include "net/base/address_list.h" 21 #include "net/socket/client_socket_factory.h" 22 #include "net/socket/client_socket_handle.h" 23 #include "net/socket/socket_performance_watcher.h" 24 #include "net/socket/stream_socket.h" 25 26 namespace net { 27 28 class ClientSocketHandle; 29 class IPEndPoint; 30 class NetLog; 31 32 // Make sure |handle| sets load times correctly when it has been assigned a 33 // reused socket. Uses gtest expectations. 34 void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle); 35 36 // Make sure |handle| sets load times correctly when it has been assigned a 37 // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner 38 // of a connection where |is_reused| is false may consider the connection 39 // reused. Uses gtest expectations. 40 void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle); 41 42 // Set |address| to 1.1.1.1:80 43 void SetIPv4Address(IPEndPoint* address); 44 45 // Set |address| to [1:abcd::3:4:ff]:80 46 void SetIPv6Address(IPEndPoint* address); 47 48 // A ClientSocketFactory that produces sockets with the specified connection 49 // behaviours. 50 class MockTransportClientSocketFactory : public ClientSocketFactory { 51 public: 52 enum ClientSocketType { 53 // Connects successfully, synchronously. 54 MOCK_CLIENT_SOCKET, 55 // Fails to connect, synchronously. 56 MOCK_FAILING_CLIENT_SOCKET, 57 // Connects successfully, asynchronously. 58 MOCK_PENDING_CLIENT_SOCKET, 59 // Fails to connect, asynchronously. 60 MOCK_PENDING_FAILING_CLIENT_SOCKET, 61 // A delayed socket will pause before connecting through the message loop. 62 MOCK_DELAYED_CLIENT_SOCKET, 63 // A delayed socket that fails. 64 MOCK_DELAYED_FAILING_CLIENT_SOCKET, 65 // A stalled socket that never connects at all. 66 MOCK_STALLED_CLIENT_SOCKET, 67 // A stalled socket that never connects at all, but returns a failing 68 // ConnectionAttempt in |GetConnectionAttempts|. 69 MOCK_STALLED_FAILING_CLIENT_SOCKET, 70 // A socket that can be triggered to connect explicitly, asynchronously. 71 MOCK_TRIGGERABLE_CLIENT_SOCKET, 72 }; 73 74 explicit MockTransportClientSocketFactory(NetLog* net_log); 75 ~MockTransportClientSocketFactory() override; 76 77 std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( 78 DatagramSocket::BindType bind_type, 79 NetLog* net_log, 80 const NetLogSource& source) override; 81 82 std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( 83 const AddressList& addresses, 84 std::unique_ptr< 85 SocketPerformanceWatcher> /* socket_performance_watcher */, 86 NetworkQualityEstimator* /* network_quality_estimator */, 87 NetLog* /* net_log */, 88 const NetLogSource& /* source */) override; 89 90 std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( 91 SSLClientContext* context, 92 std::unique_ptr<StreamSocket> nested_socket, 93 const HostPortPair& host_and_port, 94 const SSLConfig& ssl_config) override; 95 96 std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( 97 std::unique_ptr<StreamSocket> stream_socket, 98 const std::string& user_agent, 99 const HostPortPair& endpoint, 100 const ProxyServer& proxy_server, 101 HttpAuthController* http_auth_controller, 102 bool tunnel, 103 bool using_spdy, 104 NextProto negotiated_protocol, 105 ProxyDelegate* proxy_delegate, 106 const NetworkTrafficAnnotationTag& traffic_annotation) override; 107 allocation_count()108 int allocation_count() const { return allocation_count_; } 109 110 // Set the default ClientSocketType. set_default_client_socket_type(ClientSocketType type)111 void set_default_client_socket_type(ClientSocketType type) { 112 client_socket_type_ = type; 113 } 114 115 // Set a list of ClientSocketTypes to be used. 116 void set_client_socket_types(ClientSocketType* type_list, int num_types); 117 set_delay(base::TimeDelta delay)118 void set_delay(base::TimeDelta delay) { delay_ = delay; } 119 120 // If one or more MOCK_TRIGGERABLE_CLIENT_SOCKETs has already been created, 121 // then returns a Closure that can be called to cause the first 122 // not-yet-connected one to connect. If no MOCK_TRIGGERABLE_CLIENT_SOCKETs 123 // have been created yet, wait for one to be created before returning the 124 // Closure. This method should be called the same number of times as 125 // MOCK_TRIGGERABLE_CLIENT_SOCKETs are created in the test. 126 base::OnceClosure WaitForTriggerableSocketCreation(); 127 128 private: 129 NetLog* net_log_; 130 int allocation_count_; 131 ClientSocketType client_socket_type_; 132 ClientSocketType* client_socket_types_; 133 int client_socket_index_; 134 int client_socket_index_max_; 135 base::TimeDelta delay_; 136 base::queue<base::OnceClosure> triggerable_sockets_; 137 base::OnceClosure run_loop_quit_closure_; 138 139 DISALLOW_COPY_AND_ASSIGN(MockTransportClientSocketFactory); 140 }; 141 142 } // namespace net 143 144 #endif // NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_ 145