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.
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.
9 #ifndef NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_
10 #define NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_
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/log/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"
27 class ClientSocketHandle
;
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
);
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
);
40 // Set |address| to 1.1.1.1:80
41 void SetIPv4Address(IPEndPoint
* address
);
43 // Set |address| to [1:abcd::3:4:ff]:80
44 void SetIPv6Address(IPEndPoint
* address
);
46 // A ClientSocketFactory that produces sockets with the specified connection
48 class MockTransportClientSocketFactory
: public ClientSocketFactory
{
50 enum ClientSocketType
{
51 // Connects successfully, synchronously.
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 stalled socket that never connects at all, but returns a failing
66 // ConnectionAttempt in |GetConnectionAttempts|.
67 MOCK_STALLED_FAILING_CLIENT_SOCKET
,
68 // A socket that can be triggered to connect explicitly, asynchronously.
69 MOCK_TRIGGERABLE_CLIENT_SOCKET
,
72 explicit MockTransportClientSocketFactory(NetLog
* net_log
);
73 ~MockTransportClientSocketFactory() override
;
75 scoped_ptr
<DatagramClientSocket
> CreateDatagramClientSocket(
76 DatagramSocket::BindType bind_type
,
77 const RandIntCallback
& rand_int_cb
,
79 const NetLog::Source
& source
) override
;
81 scoped_ptr
<StreamSocket
> CreateTransportClientSocket(
82 const AddressList
& addresses
,
83 NetLog
* /* net_log */,
84 const NetLog::Source
& /* source */) override
;
86 scoped_ptr
<SSLClientSocket
> CreateSSLClientSocket(
87 scoped_ptr
<ClientSocketHandle
> transport_socket
,
88 const HostPortPair
& host_and_port
,
89 const SSLConfig
& ssl_config
,
90 const SSLClientSocketContext
& context
) override
;
92 void ClearSSLSessionCache() override
;
94 int allocation_count() const { return allocation_count_
; }
96 // Set the default ClientSocketType.
97 void set_default_client_socket_type(ClientSocketType type
) {
98 client_socket_type_
= type
;
101 // Set a list of ClientSocketTypes to be used.
102 void set_client_socket_types(ClientSocketType
* type_list
, int num_types
);
104 void set_delay(base::TimeDelta delay
) { delay_
= delay
; }
106 // If one or more MOCK_TRIGGERABLE_CLIENT_SOCKETs has already been created,
107 // then returns a Closure that can be called to cause the first
108 // not-yet-connected one to connect. If no MOCK_TRIGGERABLE_CLIENT_SOCKETs
109 // have been created yet, wait for one to be created before returning the
110 // Closure. This method should be called the same number of times as
111 // MOCK_TRIGGERABLE_CLIENT_SOCKETs are created in the test.
112 base::Closure
WaitForTriggerableSocketCreation();
116 int allocation_count_
;
117 ClientSocketType client_socket_type_
;
118 ClientSocketType
* client_socket_types_
;
119 int client_socket_index_
;
120 int client_socket_index_max_
;
121 base::TimeDelta delay_
;
122 std::queue
<base::Closure
> triggerable_sockets_
;
123 base::Closure run_loop_quit_closure_
;
125 DISALLOW_COPY_AND_ASSIGN(MockTransportClientSocketFactory
);
130 #endif // NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_