1 // Copyright (c) 2012 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 #include "net/socket/transport_client_socket_pool.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/threading/platform_thread.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/load_timing_info.h"
14 #include "net/base/load_timing_info_test_util.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/net_util.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/dns/mock_host_resolver.h"
19 #include "net/log/test_net_log.h"
20 #include "net/socket/client_socket_handle.h"
21 #include "net/socket/socket_test_util.h"
22 #include "net/socket/stream_socket.h"
23 #include "net/socket/transport_client_socket_pool_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
28 using internal::ClientSocketPoolBaseHelper
;
32 const int kMaxSockets
= 32;
33 const int kMaxSocketsPerGroup
= 6;
34 const RequestPriority kDefaultPriority
= LOW
;
36 class TransportClientSocketPoolTest
: public testing::Test
{
38 TransportClientSocketPoolTest()
39 : connect_backup_jobs_enabled_(
40 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true)),
42 new TransportSocketParams(
43 HostPortPair("www.google.com", 80),
46 OnHostResolutionCallback(),
47 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
)),
48 host_resolver_(new MockHostResolver
),
49 client_socket_factory_(&net_log_
),
53 &client_socket_factory_
,
57 ~TransportClientSocketPoolTest() override
{
58 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
59 connect_backup_jobs_enabled_
);
62 scoped_refptr
<TransportSocketParams
> CreateParamsForTCPFastOpen() {
63 return new TransportSocketParams(HostPortPair("www.google.com", 80),
64 false, false, OnHostResolutionCallback(),
65 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED
);
68 int StartRequest(const std::string
& group_name
, RequestPriority priority
) {
69 scoped_refptr
<TransportSocketParams
> params(new TransportSocketParams(
70 HostPortPair("www.google.com", 80), false, false,
71 OnHostResolutionCallback(),
72 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
73 return test_base_
.StartRequestUsingPool(
74 &pool_
, group_name
, priority
, params
);
77 int GetOrderOfRequest(size_t index
) {
78 return test_base_
.GetOrderOfRequest(index
);
81 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive
) {
82 return test_base_
.ReleaseOneConnection(keep_alive
);
85 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive
) {
86 test_base_
.ReleaseAllConnections(keep_alive
);
89 ScopedVector
<TestSocketRequest
>* requests() { return test_base_
.requests(); }
90 size_t completion_count() const { return test_base_
.completion_count(); }
92 bool connect_backup_jobs_enabled_
;
94 scoped_refptr
<TransportSocketParams
> params_
;
95 scoped_ptr
<MockHostResolver
> host_resolver_
;
96 MockTransportClientSocketFactory client_socket_factory_
;
97 TransportClientSocketPool pool_
;
98 ClientSocketPoolTest test_base_
;
101 DISALLOW_COPY_AND_ASSIGN(TransportClientSocketPoolTest
);
104 TEST(TransportConnectJobTest
, MakeAddrListStartWithIPv4
) {
105 IPAddressNumber ip_number
;
106 ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.1", &ip_number
));
107 IPEndPoint
addrlist_v4_1(ip_number
, 80);
108 ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.2", &ip_number
));
109 IPEndPoint
addrlist_v4_2(ip_number
, 80);
110 ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::64", &ip_number
));
111 IPEndPoint
addrlist_v6_1(ip_number
, 80);
112 ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::66", &ip_number
));
113 IPEndPoint
addrlist_v6_2(ip_number
, 80);
115 AddressList addrlist
;
117 // Test 1: IPv4 only. Expect no change.
119 addrlist
.push_back(addrlist_v4_1
);
120 addrlist
.push_back(addrlist_v4_2
);
121 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
122 ASSERT_EQ(2u, addrlist
.size());
123 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[0].GetFamily());
124 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[1].GetFamily());
126 // Test 2: IPv6 only. Expect no change.
128 addrlist
.push_back(addrlist_v6_1
);
129 addrlist
.push_back(addrlist_v6_2
);
130 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
131 ASSERT_EQ(2u, addrlist
.size());
132 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[0].GetFamily());
133 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[1].GetFamily());
135 // Test 3: IPv4 then IPv6. Expect no change.
137 addrlist
.push_back(addrlist_v4_1
);
138 addrlist
.push_back(addrlist_v4_2
);
139 addrlist
.push_back(addrlist_v6_1
);
140 addrlist
.push_back(addrlist_v6_2
);
141 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
142 ASSERT_EQ(4u, addrlist
.size());
143 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[0].GetFamily());
144 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[1].GetFamily());
145 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[2].GetFamily());
146 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[3].GetFamily());
148 // Test 4: IPv6, IPv4, IPv6, IPv4. Expect first IPv6 moved to the end.
150 addrlist
.push_back(addrlist_v6_1
);
151 addrlist
.push_back(addrlist_v4_1
);
152 addrlist
.push_back(addrlist_v6_2
);
153 addrlist
.push_back(addrlist_v4_2
);
154 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
155 ASSERT_EQ(4u, addrlist
.size());
156 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[0].GetFamily());
157 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[1].GetFamily());
158 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[2].GetFamily());
159 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[3].GetFamily());
161 // Test 5: IPv6, IPv6, IPv4, IPv4. Expect first two IPv6's moved to the end.
163 addrlist
.push_back(addrlist_v6_1
);
164 addrlist
.push_back(addrlist_v6_2
);
165 addrlist
.push_back(addrlist_v4_1
);
166 addrlist
.push_back(addrlist_v4_2
);
167 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
168 ASSERT_EQ(4u, addrlist
.size());
169 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[0].GetFamily());
170 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[1].GetFamily());
171 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[2].GetFamily());
172 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[3].GetFamily());
175 TEST_F(TransportClientSocketPoolTest
, Basic
) {
176 TestCompletionCallback callback
;
177 ClientSocketHandle handle
;
178 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool_
,
180 EXPECT_EQ(ERR_IO_PENDING
, rv
);
181 EXPECT_FALSE(handle
.is_initialized());
182 EXPECT_FALSE(handle
.socket());
184 EXPECT_EQ(OK
, callback
.WaitForResult());
185 EXPECT_TRUE(handle
.is_initialized());
186 EXPECT_TRUE(handle
.socket());
187 TestLoadTimingInfoConnectedNotReused(handle
);
188 EXPECT_EQ(0u, handle
.connection_attempts().size());
191 // Make sure that TransportConnectJob passes on its priority to its
192 // HostResolver request on Init.
193 TEST_F(TransportClientSocketPoolTest
, SetResolvePriorityOnInit
) {
194 for (int i
= MINIMUM_PRIORITY
; i
<= MAXIMUM_PRIORITY
; ++i
) {
195 RequestPriority priority
= static_cast<RequestPriority
>(i
);
196 TestCompletionCallback callback
;
197 ClientSocketHandle handle
;
198 EXPECT_EQ(ERR_IO_PENDING
,
199 handle
.Init("a", params_
, priority
, callback
.callback(), &pool_
,
201 EXPECT_EQ(priority
, host_resolver_
->last_request_priority());
205 TEST_F(TransportClientSocketPoolTest
, InitHostResolutionFailure
) {
206 host_resolver_
->rules()->AddSimulatedFailure("unresolvable.host.name");
207 TestCompletionCallback callback
;
208 ClientSocketHandle handle
;
209 HostPortPair
host_port_pair("unresolvable.host.name", 80);
210 scoped_refptr
<TransportSocketParams
> dest(new TransportSocketParams(
211 host_port_pair
, false, false, OnHostResolutionCallback(),
212 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
213 EXPECT_EQ(ERR_IO_PENDING
,
214 handle
.Init("a", dest
, kDefaultPriority
, callback
.callback(),
215 &pool_
, BoundNetLog()));
216 EXPECT_EQ(ERR_NAME_NOT_RESOLVED
, callback
.WaitForResult());
217 ASSERT_EQ(1u, handle
.connection_attempts().size());
218 EXPECT_TRUE(handle
.connection_attempts()[0].endpoint
.address().empty());
219 EXPECT_EQ(ERR_NAME_NOT_RESOLVED
, handle
.connection_attempts()[0].result
);
222 TEST_F(TransportClientSocketPoolTest
, InitConnectionFailure
) {
223 client_socket_factory_
.set_default_client_socket_type(
224 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
);
225 TestCompletionCallback callback
;
226 ClientSocketHandle handle
;
227 EXPECT_EQ(ERR_IO_PENDING
,
228 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
229 &pool_
, BoundNetLog()));
230 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
231 ASSERT_EQ(1u, handle
.connection_attempts().size());
232 EXPECT_EQ("127.0.0.1:80",
233 handle
.connection_attempts()[0].endpoint
.ToString());
234 EXPECT_EQ(ERR_CONNECTION_FAILED
, handle
.connection_attempts()[0].result
);
236 // Make the host resolutions complete synchronously this time.
237 host_resolver_
->set_synchronous_mode(true);
238 EXPECT_EQ(ERR_CONNECTION_FAILED
,
239 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
240 &pool_
, BoundNetLog()));
241 ASSERT_EQ(1u, handle
.connection_attempts().size());
242 EXPECT_EQ("127.0.0.1:80",
243 handle
.connection_attempts()[0].endpoint
.ToString());
244 EXPECT_EQ(ERR_CONNECTION_FAILED
, handle
.connection_attempts()[0].result
);
247 TEST_F(TransportClientSocketPoolTest
, PendingRequests
) {
248 // First request finishes asynchronously.
249 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
250 EXPECT_EQ(OK
, (*requests())[0]->WaitForResult());
252 // Make all subsequent host resolutions complete synchronously.
253 host_resolver_
->set_synchronous_mode(true);
255 // Rest of them finish synchronously, until we reach the per-group limit.
256 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
257 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
258 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
259 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
260 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
262 // The rest are pending since we've used all active sockets.
263 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
264 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
265 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
266 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
267 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
268 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
269 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
270 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
271 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
272 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
274 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE
);
276 EXPECT_EQ(kMaxSocketsPerGroup
, client_socket_factory_
.allocation_count());
278 // One initial asynchronous request and then 10 pending requests.
279 EXPECT_EQ(11U, completion_count());
281 // First part of requests, all with the same priority, finishes in FIFO order.
282 EXPECT_EQ(1, GetOrderOfRequest(1));
283 EXPECT_EQ(2, GetOrderOfRequest(2));
284 EXPECT_EQ(3, GetOrderOfRequest(3));
285 EXPECT_EQ(4, GetOrderOfRequest(4));
286 EXPECT_EQ(5, GetOrderOfRequest(5));
287 EXPECT_EQ(6, GetOrderOfRequest(6));
289 // Make sure that rest of the requests complete in the order of priority.
290 EXPECT_EQ(7, GetOrderOfRequest(7));
291 EXPECT_EQ(14, GetOrderOfRequest(8));
292 EXPECT_EQ(15, GetOrderOfRequest(9));
293 EXPECT_EQ(10, GetOrderOfRequest(10));
294 EXPECT_EQ(13, GetOrderOfRequest(11));
295 EXPECT_EQ(8, GetOrderOfRequest(12));
296 EXPECT_EQ(16, GetOrderOfRequest(13));
297 EXPECT_EQ(11, GetOrderOfRequest(14));
298 EXPECT_EQ(12, GetOrderOfRequest(15));
299 EXPECT_EQ(9, GetOrderOfRequest(16));
301 // Make sure we test order of all requests made.
302 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(17));
305 TEST_F(TransportClientSocketPoolTest
, PendingRequests_NoKeepAlive
) {
306 // First request finishes asynchronously.
307 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
308 EXPECT_EQ(OK
, (*requests())[0]->WaitForResult());
310 // Make all subsequent host resolutions complete synchronously.
311 host_resolver_
->set_synchronous_mode(true);
313 // Rest of them finish synchronously, until we reach the per-group limit.
314 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
315 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
316 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
317 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
318 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
320 // The rest are pending since we've used all active sockets.
321 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
322 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
323 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
324 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
325 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
327 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
329 // The pending requests should finish successfully.
330 EXPECT_EQ(OK
, (*requests())[6]->WaitForResult());
331 EXPECT_EQ(OK
, (*requests())[7]->WaitForResult());
332 EXPECT_EQ(OK
, (*requests())[8]->WaitForResult());
333 EXPECT_EQ(OK
, (*requests())[9]->WaitForResult());
334 EXPECT_EQ(OK
, (*requests())[10]->WaitForResult());
336 EXPECT_EQ(static_cast<int>(requests()->size()),
337 client_socket_factory_
.allocation_count());
339 // First asynchronous request, and then last 5 pending requests.
340 EXPECT_EQ(6U, completion_count());
343 // This test will start up a RequestSocket() and then immediately Cancel() it.
344 // The pending host resolution will eventually complete, and destroy the
345 // ClientSocketPool which will crash if the group was not cleared properly.
346 TEST_F(TransportClientSocketPoolTest
, CancelRequestClearGroup
) {
347 TestCompletionCallback callback
;
348 ClientSocketHandle handle
;
349 EXPECT_EQ(ERR_IO_PENDING
,
350 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
351 &pool_
, BoundNetLog()));
355 TEST_F(TransportClientSocketPoolTest
, TwoRequestsCancelOne
) {
356 ClientSocketHandle handle
;
357 TestCompletionCallback callback
;
358 ClientSocketHandle handle2
;
359 TestCompletionCallback callback2
;
361 EXPECT_EQ(ERR_IO_PENDING
,
362 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
363 &pool_
, BoundNetLog()));
364 EXPECT_EQ(ERR_IO_PENDING
,
365 handle2
.Init("a", params_
, kDefaultPriority
, callback2
.callback(),
366 &pool_
, BoundNetLog()));
370 EXPECT_EQ(OK
, callback2
.WaitForResult());
374 TEST_F(TransportClientSocketPoolTest
, ConnectCancelConnect
) {
375 client_socket_factory_
.set_default_client_socket_type(
376 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
);
377 ClientSocketHandle handle
;
378 TestCompletionCallback callback
;
379 EXPECT_EQ(ERR_IO_PENDING
,
380 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
381 &pool_
, BoundNetLog()));
385 TestCompletionCallback callback2
;
386 EXPECT_EQ(ERR_IO_PENDING
,
387 handle
.Init("a", params_
, kDefaultPriority
, callback2
.callback(),
388 &pool_
, BoundNetLog()));
390 host_resolver_
->set_synchronous_mode(true);
391 // At this point, handle has two ConnectingSockets out for it. Due to the
392 // setting the mock resolver into synchronous mode, the host resolution for
393 // both will return in the same loop of the MessageLoop. The client socket
394 // is a pending socket, so the Connect() will asynchronously complete on the
395 // next loop of the MessageLoop. That means that the first
396 // ConnectingSocket will enter OnIOComplete, and then the second one will.
397 // If the first one is not cancelled, it will advance the load state, and
398 // then the second one will crash.
400 EXPECT_EQ(OK
, callback2
.WaitForResult());
401 EXPECT_FALSE(callback
.have_result());
406 TEST_F(TransportClientSocketPoolTest
, CancelRequest
) {
407 // First request finishes asynchronously.
408 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
409 EXPECT_EQ(OK
, (*requests())[0]->WaitForResult());
411 // Make all subsequent host resolutions complete synchronously.
412 host_resolver_
->set_synchronous_mode(true);
414 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
415 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
416 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
417 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
418 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
420 // Reached per-group limit, queue up requests.
421 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
422 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
423 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
424 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
425 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
426 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
427 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
428 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
429 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
430 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
433 size_t index_to_cancel
= kMaxSocketsPerGroup
+ 2;
434 EXPECT_FALSE((*requests())[index_to_cancel
]->handle()->is_initialized());
435 (*requests())[index_to_cancel
]->handle()->Reset();
437 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE
);
439 EXPECT_EQ(kMaxSocketsPerGroup
,
440 client_socket_factory_
.allocation_count());
441 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup
, completion_count());
443 EXPECT_EQ(1, GetOrderOfRequest(1));
444 EXPECT_EQ(2, GetOrderOfRequest(2));
445 EXPECT_EQ(3, GetOrderOfRequest(3));
446 EXPECT_EQ(4, GetOrderOfRequest(4));
447 EXPECT_EQ(5, GetOrderOfRequest(5));
448 EXPECT_EQ(6, GetOrderOfRequest(6));
449 EXPECT_EQ(14, GetOrderOfRequest(7));
450 EXPECT_EQ(7, GetOrderOfRequest(8));
451 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound
,
452 GetOrderOfRequest(9)); // Canceled request.
453 EXPECT_EQ(9, GetOrderOfRequest(10));
454 EXPECT_EQ(10, GetOrderOfRequest(11));
455 EXPECT_EQ(11, GetOrderOfRequest(12));
456 EXPECT_EQ(8, GetOrderOfRequest(13));
457 EXPECT_EQ(12, GetOrderOfRequest(14));
458 EXPECT_EQ(13, GetOrderOfRequest(15));
459 EXPECT_EQ(15, GetOrderOfRequest(16));
461 // Make sure we test order of all requests made.
462 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(17));
465 class RequestSocketCallback
: public TestCompletionCallbackBase
{
467 RequestSocketCallback(ClientSocketHandle
* handle
,
468 TransportClientSocketPool
* pool
)
471 within_callback_(false),
472 callback_(base::Bind(&RequestSocketCallback::OnComplete
,
473 base::Unretained(this))) {
476 ~RequestSocketCallback() override
{}
478 const CompletionCallback
& callback() const { return callback_
; }
481 void OnComplete(int result
) {
483 ASSERT_EQ(OK
, result
);
485 if (!within_callback_
) {
486 // Don't allow reuse of the socket. Disconnect it and then release it and
487 // run through the MessageLoop once to get it completely released.
488 handle_
->socket()->Disconnect();
491 base::MessageLoop::ScopedNestableTaskAllower
allow(
492 base::MessageLoop::current());
493 base::MessageLoop::current()->RunUntilIdle();
495 within_callback_
= true;
496 scoped_refptr
<TransportSocketParams
> dest(new TransportSocketParams(
497 HostPortPair("www.google.com", 80), false, false,
498 OnHostResolutionCallback(),
499 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
500 int rv
= handle_
->Init("a", dest
, LOWEST
, callback(), pool_
,
506 ClientSocketHandle
* const handle_
;
507 TransportClientSocketPool
* const pool_
;
508 bool within_callback_
;
509 CompletionCallback callback_
;
511 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback
);
514 TEST_F(TransportClientSocketPoolTest
, RequestTwice
) {
515 ClientSocketHandle handle
;
516 RequestSocketCallback
callback(&handle
, &pool_
);
517 scoped_refptr
<TransportSocketParams
> dest(new TransportSocketParams(
518 HostPortPair("www.google.com", 80), false, false,
519 OnHostResolutionCallback(),
520 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
521 int rv
= handle
.Init("a", dest
, LOWEST
, callback
.callback(), &pool_
,
523 ASSERT_EQ(ERR_IO_PENDING
, rv
);
525 // The callback is going to request "www.google.com". We want it to complete
526 // synchronously this time.
527 host_resolver_
->set_synchronous_mode(true);
529 EXPECT_EQ(OK
, callback
.WaitForResult());
534 // Make sure that pending requests get serviced after active requests get
536 TEST_F(TransportClientSocketPoolTest
, CancelActiveRequestWithPendingRequests
) {
537 client_socket_factory_
.set_default_client_socket_type(
538 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
);
540 // Queue up all the requests
541 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
542 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
543 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
544 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
545 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
546 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
547 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
548 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
549 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
551 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them.
552 ASSERT_LE(kMaxSocketsPerGroup
, static_cast<int>(requests()->size()));
553 for (int i
= 0; i
< kMaxSocketsPerGroup
; i
++)
554 (*requests())[i
]->handle()->Reset();
556 // Let's wait for the rest to complete now.
557 for (size_t i
= kMaxSocketsPerGroup
; i
< requests()->size(); ++i
) {
558 EXPECT_EQ(OK
, (*requests())[i
]->WaitForResult());
559 (*requests())[i
]->handle()->Reset();
562 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup
, completion_count());
565 // Make sure that pending requests get serviced after active requests fail.
566 TEST_F(TransportClientSocketPoolTest
, FailingActiveRequestWithPendingRequests
) {
567 client_socket_factory_
.set_default_client_socket_type(
568 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET
);
570 const int kNumRequests
= 2 * kMaxSocketsPerGroup
+ 1;
571 ASSERT_LE(kNumRequests
, kMaxSockets
); // Otherwise the test will hang.
573 // Queue up all the requests
574 for (int i
= 0; i
< kNumRequests
; i
++)
575 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
577 for (int i
= 0; i
< kNumRequests
; i
++)
578 EXPECT_EQ(ERR_CONNECTION_FAILED
, (*requests())[i
]->WaitForResult());
581 TEST_F(TransportClientSocketPoolTest
, IdleSocketLoadTiming
) {
582 TestCompletionCallback callback
;
583 ClientSocketHandle handle
;
584 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool_
,
586 EXPECT_EQ(ERR_IO_PENDING
, rv
);
587 EXPECT_FALSE(handle
.is_initialized());
588 EXPECT_FALSE(handle
.socket());
590 EXPECT_EQ(OK
, callback
.WaitForResult());
591 EXPECT_TRUE(handle
.is_initialized());
592 EXPECT_TRUE(handle
.socket());
593 TestLoadTimingInfoConnectedNotReused(handle
);
596 // Need to run all pending to release the socket back to the pool.
597 base::MessageLoop::current()->RunUntilIdle();
599 // Now we should have 1 idle socket.
600 EXPECT_EQ(1, pool_
.IdleSocketCount());
602 rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool_
,
605 EXPECT_EQ(0, pool_
.IdleSocketCount());
606 TestLoadTimingInfoConnectedReused(handle
);
609 TEST_F(TransportClientSocketPoolTest
, ResetIdleSocketsOnIPAddressChange
) {
610 TestCompletionCallback callback
;
611 ClientSocketHandle handle
;
612 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool_
,
614 EXPECT_EQ(ERR_IO_PENDING
, rv
);
615 EXPECT_FALSE(handle
.is_initialized());
616 EXPECT_FALSE(handle
.socket());
618 EXPECT_EQ(OK
, callback
.WaitForResult());
619 EXPECT_TRUE(handle
.is_initialized());
620 EXPECT_TRUE(handle
.socket());
624 // Need to run all pending to release the socket back to the pool.
625 base::MessageLoop::current()->RunUntilIdle();
627 // Now we should have 1 idle socket.
628 EXPECT_EQ(1, pool_
.IdleSocketCount());
630 // After an IP address change, we should have 0 idle sockets.
631 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
632 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async.
634 EXPECT_EQ(0, pool_
.IdleSocketCount());
637 TEST_F(TransportClientSocketPoolTest
, BackupSocketConnect
) {
638 // Case 1 tests the first socket stalling, and the backup connecting.
639 MockTransportClientSocketFactory::ClientSocketType case1_types
[] = {
640 // The first socket will not connect.
641 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
,
642 // The second socket will connect more quickly.
643 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET
646 // Case 2 tests the first socket being slow, so that we start the
647 // second connect, but the second connect stalls, and we still
648 // complete the first.
649 MockTransportClientSocketFactory::ClientSocketType case2_types
[] = {
650 // The first socket will connect, although delayed.
651 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
,
652 // The second socket will not connect.
653 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
656 MockTransportClientSocketFactory::ClientSocketType
* cases
[2] = {
661 for (size_t index
= 0; index
< arraysize(cases
); ++index
) {
662 client_socket_factory_
.set_client_socket_types(cases
[index
], 2);
664 EXPECT_EQ(0, pool_
.IdleSocketCount());
666 TestCompletionCallback callback
;
667 ClientSocketHandle handle
;
668 int rv
= handle
.Init("b", params_
, LOW
, callback
.callback(), &pool_
,
670 EXPECT_EQ(ERR_IO_PENDING
, rv
);
671 EXPECT_FALSE(handle
.is_initialized());
672 EXPECT_FALSE(handle
.socket());
674 // Create the first socket, set the timer.
675 base::MessageLoop::current()->RunUntilIdle();
677 // Wait for the backup socket timer to fire.
678 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
679 ClientSocketPool::kMaxConnectRetryIntervalMs
+ 50));
681 // Let the appropriate socket connect.
682 base::MessageLoop::current()->RunUntilIdle();
684 EXPECT_EQ(OK
, callback
.WaitForResult());
685 EXPECT_TRUE(handle
.is_initialized());
686 EXPECT_TRUE(handle
.socket());
688 // One socket is stalled, the other is active.
689 EXPECT_EQ(0, pool_
.IdleSocketCount());
692 // Close all pending connect jobs and existing sockets.
693 pool_
.FlushWithError(ERR_NETWORK_CHANGED
);
697 // Test the case where a socket took long enough to start the creation
698 // of the backup socket, but then we cancelled the request after that.
699 TEST_F(TransportClientSocketPoolTest
, BackupSocketCancel
) {
700 client_socket_factory_
.set_default_client_socket_type(
701 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
);
703 enum { CANCEL_BEFORE_WAIT
, CANCEL_AFTER_WAIT
};
705 for (int index
= CANCEL_BEFORE_WAIT
; index
< CANCEL_AFTER_WAIT
; ++index
) {
706 EXPECT_EQ(0, pool_
.IdleSocketCount());
708 TestCompletionCallback callback
;
709 ClientSocketHandle handle
;
710 int rv
= handle
.Init("c", params_
, LOW
, callback
.callback(), &pool_
,
712 EXPECT_EQ(ERR_IO_PENDING
, rv
);
713 EXPECT_FALSE(handle
.is_initialized());
714 EXPECT_FALSE(handle
.socket());
716 // Create the first socket, set the timer.
717 base::MessageLoop::current()->RunUntilIdle();
719 if (index
== CANCEL_AFTER_WAIT
) {
720 // Wait for the backup socket timer to fire.
721 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
722 ClientSocketPool::kMaxConnectRetryIntervalMs
));
725 // Let the appropriate socket connect.
726 base::MessageLoop::current()->RunUntilIdle();
730 EXPECT_FALSE(callback
.have_result());
731 EXPECT_FALSE(handle
.is_initialized());
732 EXPECT_FALSE(handle
.socket());
734 // One socket is stalled, the other is active.
735 EXPECT_EQ(0, pool_
.IdleSocketCount());
739 // Test the case where a socket took long enough to start the creation
740 // of the backup socket and never completes, and then the backup
742 TEST_F(TransportClientSocketPoolTest
, BackupSocketFailAfterStall
) {
743 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
744 // The first socket will not connect.
745 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
,
746 // The second socket will fail immediately.
747 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
750 client_socket_factory_
.set_client_socket_types(case_types
, 2);
752 EXPECT_EQ(0, pool_
.IdleSocketCount());
754 TestCompletionCallback callback
;
755 ClientSocketHandle handle
;
756 int rv
= handle
.Init("b", params_
, LOW
, callback
.callback(), &pool_
,
758 EXPECT_EQ(ERR_IO_PENDING
, rv
);
759 EXPECT_FALSE(handle
.is_initialized());
760 EXPECT_FALSE(handle
.socket());
762 // Create the first socket, set the timer.
763 base::MessageLoop::current()->RunUntilIdle();
765 // Wait for the backup socket timer to fire.
766 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
767 ClientSocketPool::kMaxConnectRetryIntervalMs
));
769 // Let the second connect be synchronous. Otherwise, the emulated
770 // host resolution takes an extra trip through the message loop.
771 host_resolver_
->set_synchronous_mode(true);
773 // Let the appropriate socket connect.
774 base::MessageLoop::current()->RunUntilIdle();
776 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
777 EXPECT_FALSE(handle
.is_initialized());
778 EXPECT_FALSE(handle
.socket());
779 ASSERT_EQ(1u, handle
.connection_attempts().size());
780 EXPECT_EQ(ERR_CONNECTION_FAILED
, handle
.connection_attempts()[0].result
);
781 EXPECT_EQ(0, pool_
.IdleSocketCount());
784 // Reset for the next case.
785 host_resolver_
->set_synchronous_mode(false);
788 // Test the case where a socket took long enough to start the creation
789 // of the backup socket and eventually completes, but the backup socket
791 TEST_F(TransportClientSocketPoolTest
, BackupSocketFailAfterDelay
) {
792 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
793 // The first socket will connect, although delayed.
794 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
,
795 // The second socket will not connect.
796 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
799 client_socket_factory_
.set_client_socket_types(case_types
, 2);
800 client_socket_factory_
.set_delay(base::TimeDelta::FromSeconds(5));
802 EXPECT_EQ(0, pool_
.IdleSocketCount());
804 TestCompletionCallback callback
;
805 ClientSocketHandle handle
;
806 int rv
= handle
.Init("b", params_
, LOW
, callback
.callback(), &pool_
,
808 EXPECT_EQ(ERR_IO_PENDING
, rv
);
809 EXPECT_FALSE(handle
.is_initialized());
810 EXPECT_FALSE(handle
.socket());
812 // Create the first socket, set the timer.
813 base::MessageLoop::current()->RunUntilIdle();
815 // Wait for the backup socket timer to fire.
816 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
817 ClientSocketPool::kMaxConnectRetryIntervalMs
));
819 // Let the second connect be synchronous. Otherwise, the emulated
820 // host resolution takes an extra trip through the message loop.
821 host_resolver_
->set_synchronous_mode(true);
823 // Let the appropriate socket connect.
824 base::MessageLoop::current()->RunUntilIdle();
826 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
827 EXPECT_FALSE(handle
.is_initialized());
828 EXPECT_FALSE(handle
.socket());
829 ASSERT_EQ(1u, handle
.connection_attempts().size());
830 EXPECT_EQ(ERR_CONNECTION_FAILED
, handle
.connection_attempts()[0].result
);
833 // Reset for the next case.
834 host_resolver_
->set_synchronous_mode(false);
837 // Test the case of the IPv6 address stalling, and falling back to the IPv4
838 // socket which finishes first.
839 TEST_F(TransportClientSocketPoolTest
, IPv6FallbackSocketIPv4FinishesFirst
) {
840 // Create a pool without backup jobs.
841 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
842 TransportClientSocketPool
pool(kMaxSockets
,
844 host_resolver_
.get(),
845 &client_socket_factory_
,
848 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
849 // This is the IPv6 socket. It stalls, but presents one failed connection
850 // attempt on GetConnectionAttempts.
851 MockTransportClientSocketFactory::MOCK_STALLED_FAILING_CLIENT_SOCKET
,
852 // This is the IPv4 socket.
853 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
};
855 client_socket_factory_
.set_client_socket_types(case_types
, 2);
857 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
858 host_resolver_
->rules()
859 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
861 TestCompletionCallback callback
;
862 ClientSocketHandle handle
;
863 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool
,
865 EXPECT_EQ(ERR_IO_PENDING
, rv
);
866 EXPECT_FALSE(handle
.is_initialized());
867 EXPECT_FALSE(handle
.socket());
869 EXPECT_EQ(OK
, callback
.WaitForResult());
870 EXPECT_TRUE(handle
.is_initialized());
871 EXPECT_TRUE(handle
.socket());
873 handle
.socket()->GetLocalAddress(&endpoint
);
874 EXPECT_EQ(kIPv4AddressSize
, endpoint
.address().size());
876 // Check that the failed connection attempt on the main socket is collected.
877 ConnectionAttempts attempts
;
878 handle
.socket()->GetConnectionAttempts(&attempts
);
879 ASSERT_EQ(1u, attempts
.size());
880 EXPECT_EQ(ERR_CONNECTION_FAILED
, attempts
[0].result
);
881 EXPECT_EQ(kIPv6AddressSize
, attempts
[0].endpoint
.address().size());
883 EXPECT_EQ(2, client_socket_factory_
.allocation_count());
886 // Test the case of the IPv6 address being slow, thus falling back to trying to
887 // connect to the IPv4 address, but having the connect to the IPv6 address
889 TEST_F(TransportClientSocketPoolTest
, IPv6FallbackSocketIPv6FinishesFirst
) {
890 // Create a pool without backup jobs.
891 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
892 TransportClientSocketPool
pool(kMaxSockets
,
894 host_resolver_
.get(),
895 &client_socket_factory_
,
898 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
899 // This is the IPv6 socket.
900 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
,
901 // This is the IPv4 socket. It stalls, but presents one failed connection
902 // attempt on GetConnectionATtempts.
903 MockTransportClientSocketFactory::MOCK_STALLED_FAILING_CLIENT_SOCKET
};
905 client_socket_factory_
.set_client_socket_types(case_types
, 2);
906 client_socket_factory_
.set_delay(base::TimeDelta::FromMilliseconds(
907 TransportConnectJobHelper::kIPv6FallbackTimerInMs
+ 50));
909 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
910 host_resolver_
->rules()
911 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
913 TestCompletionCallback callback
;
914 ClientSocketHandle handle
;
915 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool
,
917 EXPECT_EQ(ERR_IO_PENDING
, rv
);
918 EXPECT_FALSE(handle
.is_initialized());
919 EXPECT_FALSE(handle
.socket());
921 EXPECT_EQ(OK
, callback
.WaitForResult());
922 EXPECT_TRUE(handle
.is_initialized());
923 EXPECT_TRUE(handle
.socket());
925 handle
.socket()->GetLocalAddress(&endpoint
);
926 EXPECT_EQ(kIPv6AddressSize
, endpoint
.address().size());
928 // Check that the failed connection attempt on the fallback socket is
930 ConnectionAttempts attempts
;
931 handle
.socket()->GetConnectionAttempts(&attempts
);
932 ASSERT_EQ(1u, attempts
.size());
933 EXPECT_EQ(ERR_CONNECTION_FAILED
, attempts
[0].result
);
934 EXPECT_EQ(kIPv4AddressSize
, attempts
[0].endpoint
.address().size());
936 EXPECT_EQ(2, client_socket_factory_
.allocation_count());
939 TEST_F(TransportClientSocketPoolTest
, IPv6NoIPv4AddressesToFallbackTo
) {
940 // Create a pool without backup jobs.
941 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
942 TransportClientSocketPool
pool(kMaxSockets
,
944 host_resolver_
.get(),
945 &client_socket_factory_
,
948 client_socket_factory_
.set_default_client_socket_type(
949 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
);
951 // Resolve an AddressList with only IPv6 addresses.
952 host_resolver_
->rules()
953 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
955 TestCompletionCallback callback
;
956 ClientSocketHandle handle
;
957 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool
,
959 EXPECT_EQ(ERR_IO_PENDING
, rv
);
960 EXPECT_FALSE(handle
.is_initialized());
961 EXPECT_FALSE(handle
.socket());
963 EXPECT_EQ(OK
, callback
.WaitForResult());
964 EXPECT_TRUE(handle
.is_initialized());
965 EXPECT_TRUE(handle
.socket());
967 handle
.socket()->GetLocalAddress(&endpoint
);
968 EXPECT_EQ(kIPv6AddressSize
, endpoint
.address().size());
969 EXPECT_EQ(0u, handle
.connection_attempts().size());
970 EXPECT_EQ(1, client_socket_factory_
.allocation_count());
973 TEST_F(TransportClientSocketPoolTest
, IPv4HasNoFallback
) {
974 // Create a pool without backup jobs.
975 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
976 TransportClientSocketPool
pool(kMaxSockets
,
978 host_resolver_
.get(),
979 &client_socket_factory_
,
982 client_socket_factory_
.set_default_client_socket_type(
983 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
);
985 // Resolve an AddressList with only IPv4 addresses.
986 host_resolver_
->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
988 TestCompletionCallback callback
;
989 ClientSocketHandle handle
;
990 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool
,
992 EXPECT_EQ(ERR_IO_PENDING
, rv
);
993 EXPECT_FALSE(handle
.is_initialized());
994 EXPECT_FALSE(handle
.socket());
996 EXPECT_EQ(OK
, callback
.WaitForResult());
997 EXPECT_TRUE(handle
.is_initialized());
998 EXPECT_TRUE(handle
.socket());
1000 handle
.socket()->GetLocalAddress(&endpoint
);
1001 EXPECT_EQ(kIPv4AddressSize
, endpoint
.address().size());
1002 EXPECT_EQ(0u, handle
.connection_attempts().size());
1003 EXPECT_EQ(1, client_socket_factory_
.allocation_count());
1006 // Test that if TCP FastOpen is enabled, it is set on the socket
1007 // when we have only an IPv4 address.
1008 TEST_F(TransportClientSocketPoolTest
, TCPFastOpenOnIPv4WithNoFallback
) {
1009 // Create a pool without backup jobs.
1010 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
1011 TransportClientSocketPool
pool(kMaxSockets
,
1012 kMaxSocketsPerGroup
,
1013 host_resolver_
.get(),
1014 &client_socket_factory_
,
1016 client_socket_factory_
.set_default_client_socket_type(
1017 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
);
1018 // Resolve an AddressList with only IPv4 addresses.
1019 host_resolver_
->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
1021 TestCompletionCallback callback
;
1022 ClientSocketHandle handle
;
1023 // Enable TCP FastOpen in TransportSocketParams.
1024 scoped_refptr
<TransportSocketParams
> params
= CreateParamsForTCPFastOpen();
1025 handle
.Init("a", params
, LOW
, callback
.callback(), &pool
, BoundNetLog());
1026 EXPECT_EQ(OK
, callback
.WaitForResult());
1027 EXPECT_TRUE(handle
.socket()->UsingTCPFastOpen());
1030 // Test that if TCP FastOpen is enabled, it is set on the socket
1031 // when we have only IPv6 addresses.
1032 TEST_F(TransportClientSocketPoolTest
, TCPFastOpenOnIPv6WithNoFallback
) {
1033 // Create a pool without backup jobs.
1034 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
1035 TransportClientSocketPool
pool(kMaxSockets
,
1036 kMaxSocketsPerGroup
,
1037 host_resolver_
.get(),
1038 &client_socket_factory_
,
1040 client_socket_factory_
.set_default_client_socket_type(
1041 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
);
1042 // Resolve an AddressList with only IPv6 addresses.
1043 host_resolver_
->rules()
1044 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
1046 TestCompletionCallback callback
;
1047 ClientSocketHandle handle
;
1048 // Enable TCP FastOpen in TransportSocketParams.
1049 scoped_refptr
<TransportSocketParams
> params
= CreateParamsForTCPFastOpen();
1050 handle
.Init("a", params
, LOW
, callback
.callback(), &pool
, BoundNetLog());
1051 EXPECT_EQ(OK
, callback
.WaitForResult());
1052 EXPECT_TRUE(handle
.socket()->UsingTCPFastOpen());
1055 // Test that if TCP FastOpen is enabled, it does not do anything when there
1056 // is a IPv6 address with fallback to an IPv4 address. This test tests the case
1057 // when the IPv6 connect fails and the IPv4 one succeeds.
1058 TEST_F(TransportClientSocketPoolTest
,
1059 NoTCPFastOpenOnIPv6FailureWithIPv4Fallback
) {
1060 // Create a pool without backup jobs.
1061 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
1062 TransportClientSocketPool
pool(kMaxSockets
,
1063 kMaxSocketsPerGroup
,
1064 host_resolver_
.get(),
1065 &client_socket_factory_
,
1068 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
1069 // This is the IPv6 socket.
1070 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
,
1071 // This is the IPv4 socket.
1072 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
1074 client_socket_factory_
.set_client_socket_types(case_types
, 2);
1075 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
1076 host_resolver_
->rules()
1077 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
1079 TestCompletionCallback callback
;
1080 ClientSocketHandle handle
;
1081 // Enable TCP FastOpen in TransportSocketParams.
1082 scoped_refptr
<TransportSocketParams
> params
= CreateParamsForTCPFastOpen();
1083 handle
.Init("a", params
, LOW
, callback
.callback(), &pool
, BoundNetLog());
1084 EXPECT_EQ(OK
, callback
.WaitForResult());
1085 // Verify that the socket used is connected to the fallback IPv4 address.
1086 IPEndPoint endpoint
;
1087 handle
.socket()->GetLocalAddress(&endpoint
);
1088 EXPECT_EQ(kIPv4AddressSize
, endpoint
.address().size());
1089 EXPECT_EQ(2, client_socket_factory_
.allocation_count());
1090 // Verify that TCP FastOpen was not turned on for the socket.
1091 EXPECT_FALSE(handle
.socket()->UsingTCPFastOpen());
1094 // Test that if TCP FastOpen is enabled, it does not do anything when there
1095 // is a IPv6 address with fallback to an IPv4 address. This test tests the case
1096 // when the IPv6 connect succeeds.
1097 TEST_F(TransportClientSocketPoolTest
,
1098 NoTCPFastOpenOnIPv6SuccessWithIPv4Fallback
) {
1099 // Create a pool without backup jobs.
1100 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
1101 TransportClientSocketPool
pool(kMaxSockets
,
1102 kMaxSocketsPerGroup
,
1103 host_resolver_
.get(),
1104 &client_socket_factory_
,
1107 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
1108 // This is the IPv6 socket.
1109 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
,
1110 // This is the IPv4 socket.
1111 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
1113 client_socket_factory_
.set_client_socket_types(case_types
, 2);
1114 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
1115 host_resolver_
->rules()
1116 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
1118 TestCompletionCallback callback
;
1119 ClientSocketHandle handle
;
1120 // Enable TCP FastOpen in TransportSocketParams.
1121 scoped_refptr
<TransportSocketParams
> params
= CreateParamsForTCPFastOpen();
1122 handle
.Init("a", params
, LOW
, callback
.callback(), &pool
, BoundNetLog());
1123 EXPECT_EQ(OK
, callback
.WaitForResult());
1124 // Verify that the socket used is connected to the IPv6 address.
1125 IPEndPoint endpoint
;
1126 handle
.socket()->GetLocalAddress(&endpoint
);
1127 EXPECT_EQ(kIPv6AddressSize
, endpoint
.address().size());
1128 EXPECT_EQ(1, client_socket_factory_
.allocation_count());
1129 // Verify that TCP FastOpen was not turned on for the socket.
1130 EXPECT_FALSE(handle
.socket()->UsingTCPFastOpen());