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