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/http/http_proxy_client_socket_pool.h"
7 #include "base/callback.h"
8 #include "base/compiler_specific.h"
9 #include "base/run_loop.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "net/base/auth.h"
14 #include "net/base/load_timing_info.h"
15 #include "net/base/load_timing_info_test_util.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/cert/cert_verifier.h"
19 #include "net/dns/mock_host_resolver.h"
20 #include "net/http/http_auth_handler_factory.h"
21 #include "net/http/http_network_session.h"
22 #include "net/http/http_request_headers.h"
23 #include "net/http/http_response_headers.h"
24 #include "net/http/http_server_properties_impl.h"
25 #include "net/http/transport_security_state.h"
26 #include "net/proxy/proxy_service.h"
27 #include "net/socket/client_socket_handle.h"
28 #include "net/socket/client_socket_pool_histograms.h"
29 #include "net/socket/next_proto.h"
30 #include "net/socket/socket_test_util.h"
31 #include "net/spdy/spdy_session.h"
32 #include "net/spdy/spdy_session_pool.h"
33 #include "net/spdy/spdy_test_util_common.h"
34 #include "net/ssl/ssl_config_service_defaults.h"
35 #include "net/test/test_certificate_data.h"
36 #include "testing/gtest/include/gtest/gtest.h"
42 const int kMaxSockets
= 32;
43 const int kMaxSocketsPerGroup
= 6;
45 // Make sure |handle|'s load times are set correctly. DNS and connect start
46 // times comes from mock client sockets in these tests, so primarily serves to
47 // check those times were copied, and ssl times / connect end are set correctly.
48 void TestLoadTimingInfo(const ClientSocketHandle
& handle
) {
49 LoadTimingInfo load_timing_info
;
50 EXPECT_TRUE(handle
.GetLoadTimingInfo(false, &load_timing_info
));
52 EXPECT_FALSE(load_timing_info
.socket_reused
);
53 // None of these tests use a NetLog.
54 EXPECT_EQ(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
56 ExpectConnectTimingHasTimes(
57 load_timing_info
.connect_timing
,
58 CONNECT_TIMING_HAS_SSL_TIMES
| CONNECT_TIMING_HAS_DNS_TIMES
);
59 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info
);
62 // Just like TestLoadTimingInfo, except DNS times are expected to be null, for
63 // tests over proxies that do DNS lookups themselves.
64 void TestLoadTimingInfoNoDns(const ClientSocketHandle
& handle
) {
65 LoadTimingInfo load_timing_info
;
66 EXPECT_TRUE(handle
.GetLoadTimingInfo(false, &load_timing_info
));
68 // None of these tests use a NetLog.
69 EXPECT_EQ(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
71 EXPECT_FALSE(load_timing_info
.socket_reused
);
73 ExpectConnectTimingHasTimes(load_timing_info
.connect_timing
,
74 CONNECT_TIMING_HAS_SSL_TIMES
);
75 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info
);
78 class SSLClientSocketPoolTest
79 : public testing::Test
,
80 public ::testing::WithParamInterface
<NextProto
> {
82 SSLClientSocketPoolTest()
83 : transport_security_state_(new TransportSecurityState
),
84 proxy_service_(ProxyService::CreateDirect()),
85 ssl_config_service_(new SSLConfigServiceDefaults
),
86 http_auth_handler_factory_(
87 HttpAuthHandlerFactory::CreateDefault(&host_resolver_
)),
88 session_(CreateNetworkSession()),
89 direct_transport_socket_params_(
90 new TransportSocketParams(
91 HostPortPair("host", 443),
94 OnHostResolutionCallback(),
95 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
)),
96 transport_histograms_("MockTCP"),
97 transport_socket_pool_(kMaxSockets
,
99 &transport_histograms_
,
101 proxy_transport_socket_params_(
102 new TransportSocketParams(
103 HostPortPair("proxy", 443),
106 OnHostResolutionCallback(),
107 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
)),
108 socks_socket_params_(
109 new SOCKSSocketParams(proxy_transport_socket_params_
,
111 HostPortPair("sockshost", 443))),
112 socks_histograms_("MockSOCKS"),
113 socks_socket_pool_(kMaxSockets
,
116 &transport_socket_pool_
),
117 http_proxy_socket_params_(
118 new HttpProxySocketParams(proxy_transport_socket_params_
,
122 HostPortPair("host", 80),
123 session_
->http_auth_cache(),
124 session_
->http_auth_handler_factory(),
125 session_
->spdy_session_pool(),
128 http_proxy_histograms_("MockHttpProxy"),
129 http_proxy_socket_pool_(kMaxSockets
,
131 &http_proxy_histograms_
,
132 &transport_socket_pool_
,
135 enable_ssl_connect_job_waiting_(false) {
136 scoped_refptr
<SSLConfigService
> ssl_config_service(
137 new SSLConfigServiceDefaults
);
138 ssl_config_service
->GetSSLConfig(&ssl_config_
);
141 void CreatePool(bool transport_pool
, bool http_proxy_pool
, bool socks_pool
) {
142 ssl_histograms_
.reset(new ClientSocketPoolHistograms("SSLUnitTest"));
143 pool_
.reset(new SSLClientSocketPool(
144 kMaxSockets
, kMaxSocketsPerGroup
, ssl_histograms_
.get(),
145 NULL
/* cert_verifier */, NULL
/* channel_id_service */,
146 NULL
/* transport_security_state */,
147 NULL
/* cert_transparency_verifier */, NULL
/* cert_policy_enforcer */,
148 std::string() /* ssl_session_cache_shard */, &socket_factory_
,
149 transport_pool
? &transport_socket_pool_
: NULL
,
150 socks_pool
? &socks_socket_pool_
: NULL
,
151 http_proxy_pool
? &http_proxy_socket_pool_
: NULL
, NULL
,
152 enable_ssl_connect_job_waiting_
, NULL
));
155 scoped_refptr
<SSLSocketParams
> SSLParams(ProxyServer::Scheme proxy
,
156 bool want_spdy_over_npn
) {
157 return make_scoped_refptr(new SSLSocketParams(
158 proxy
== ProxyServer::SCHEME_DIRECT
? direct_transport_socket_params_
160 proxy
== ProxyServer::SCHEME_SOCKS5
? socks_socket_params_
: NULL
,
161 proxy
== ProxyServer::SCHEME_HTTP
? http_proxy_socket_params_
: NULL
,
162 HostPortPair("host", 443),
164 PRIVACY_MODE_DISABLED
,
167 want_spdy_over_npn
));
170 void AddAuthToCache() {
171 const base::string16
kFoo(base::ASCIIToUTF16("foo"));
172 const base::string16
kBar(base::ASCIIToUTF16("bar"));
173 session_
->http_auth_cache()->Add(GURL("http://proxy:443/"),
175 HttpAuth::AUTH_SCHEME_BASIC
,
176 "Basic realm=MyRealm1",
177 AuthCredentials(kFoo
, kBar
),
181 HttpNetworkSession
* CreateNetworkSession() {
182 HttpNetworkSession::Params params
;
183 params
.host_resolver
= &host_resolver_
;
184 params
.cert_verifier
= cert_verifier_
.get();
185 params
.transport_security_state
= transport_security_state_
.get();
186 params
.proxy_service
= proxy_service_
.get();
187 params
.client_socket_factory
= &socket_factory_
;
188 params
.ssl_config_service
= ssl_config_service_
.get();
189 params
.http_auth_handler_factory
= http_auth_handler_factory_
.get();
190 params
.http_server_properties
=
191 http_server_properties_
.GetWeakPtr();
192 params
.enable_spdy_compression
= false;
193 params
.spdy_default_protocol
= GetParam();
194 return new HttpNetworkSession(params
);
197 void TestIPPoolingDisabled(SSLSocketDataProvider
* ssl
);
199 MockClientSocketFactory socket_factory_
;
200 MockCachingHostResolver host_resolver_
;
201 scoped_ptr
<CertVerifier
> cert_verifier_
;
202 scoped_ptr
<TransportSecurityState
> transport_security_state_
;
203 const scoped_ptr
<ProxyService
> proxy_service_
;
204 const scoped_refptr
<SSLConfigService
> ssl_config_service_
;
205 const scoped_ptr
<HttpAuthHandlerFactory
> http_auth_handler_factory_
;
206 HttpServerPropertiesImpl http_server_properties_
;
207 const scoped_refptr
<HttpNetworkSession
> session_
;
209 scoped_refptr
<TransportSocketParams
> direct_transport_socket_params_
;
210 ClientSocketPoolHistograms transport_histograms_
;
211 MockTransportClientSocketPool transport_socket_pool_
;
213 scoped_refptr
<TransportSocketParams
> proxy_transport_socket_params_
;
215 scoped_refptr
<SOCKSSocketParams
> socks_socket_params_
;
216 ClientSocketPoolHistograms socks_histograms_
;
217 MockSOCKSClientSocketPool socks_socket_pool_
;
219 scoped_refptr
<HttpProxySocketParams
> http_proxy_socket_params_
;
220 ClientSocketPoolHistograms http_proxy_histograms_
;
221 HttpProxyClientSocketPool http_proxy_socket_pool_
;
223 SSLConfig ssl_config_
;
224 scoped_ptr
<ClientSocketPoolHistograms
> ssl_histograms_
;
225 scoped_ptr
<SSLClientSocketPool
> pool_
;
227 bool enable_ssl_connect_job_waiting_
;
230 INSTANTIATE_TEST_CASE_P(
232 SSLClientSocketPoolTest
,
233 testing::Values(kProtoSPDY31
, kProtoSPDY4_14
, kProtoSPDY4_15
));
235 // Tests that the final socket will connect even if all sockets
238 // All sockets should wait for the first socket to attempt to
239 // connect. Once it fails to connect, all other sockets should
240 // attempt to connect. All should fail, except the final socket.
241 TEST_P(SSLClientSocketPoolTest
, AllSocketsFailButLast
) {
242 // Although we request four sockets, the first three socket connect
243 // failures cause the socket pool to create three more sockets because
244 // there are pending requests.
245 StaticSocketDataProvider data1
;
246 StaticSocketDataProvider data2
;
247 StaticSocketDataProvider data3
;
248 StaticSocketDataProvider data4
;
249 StaticSocketDataProvider data5
;
250 StaticSocketDataProvider data6
;
251 StaticSocketDataProvider data7
;
252 socket_factory_
.AddSocketDataProvider(&data1
);
253 socket_factory_
.AddSocketDataProvider(&data2
);
254 socket_factory_
.AddSocketDataProvider(&data3
);
255 socket_factory_
.AddSocketDataProvider(&data4
);
256 socket_factory_
.AddSocketDataProvider(&data5
);
257 socket_factory_
.AddSocketDataProvider(&data6
);
258 socket_factory_
.AddSocketDataProvider(&data7
);
259 SSLSocketDataProvider
ssl(ASYNC
, ERR_SSL_PROTOCOL_ERROR
);
260 ssl
.is_in_session_cache
= false;
261 SSLSocketDataProvider
ssl2(ASYNC
, ERR_SSL_PROTOCOL_ERROR
);
262 ssl2
.is_in_session_cache
= false;
263 SSLSocketDataProvider
ssl3(ASYNC
, ERR_SSL_PROTOCOL_ERROR
);
264 ssl3
.is_in_session_cache
= false;
265 SSLSocketDataProvider
ssl4(ASYNC
, OK
);
266 ssl4
.is_in_session_cache
= false;
267 SSLSocketDataProvider
ssl5(ASYNC
, OK
);
268 ssl5
.is_in_session_cache
= false;
269 SSLSocketDataProvider
ssl6(ASYNC
, OK
);
270 ssl6
.is_in_session_cache
= false;
271 SSLSocketDataProvider
ssl7(ASYNC
, OK
);
272 ssl7
.is_in_session_cache
= false;
274 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
275 socket_factory_
.AddSSLSocketDataProvider(&ssl2
);
276 socket_factory_
.AddSSLSocketDataProvider(&ssl3
);
277 socket_factory_
.AddSSLSocketDataProvider(&ssl4
);
278 socket_factory_
.AddSSLSocketDataProvider(&ssl5
);
279 socket_factory_
.AddSSLSocketDataProvider(&ssl6
);
280 socket_factory_
.AddSSLSocketDataProvider(&ssl7
);
282 enable_ssl_connect_job_waiting_
= true;
283 CreatePool(true, false, false);
285 scoped_refptr
<SSLSocketParams
> params1
=
286 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
287 scoped_refptr
<SSLSocketParams
> params2
=
288 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
289 scoped_refptr
<SSLSocketParams
> params3
=
290 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
291 scoped_refptr
<SSLSocketParams
> params4
=
292 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
293 ClientSocketHandle handle1
;
294 ClientSocketHandle handle2
;
295 ClientSocketHandle handle3
;
296 ClientSocketHandle handle4
;
297 TestCompletionCallback callback1
;
298 TestCompletionCallback callback2
;
299 TestCompletionCallback callback3
;
300 TestCompletionCallback callback4
;
303 "b", params1
, MEDIUM
, callback1
.callback(), pool_
.get(), BoundNetLog());
305 "b", params2
, MEDIUM
, callback2
.callback(), pool_
.get(), BoundNetLog());
307 "b", params3
, MEDIUM
, callback3
.callback(), pool_
.get(), BoundNetLog());
309 "b", params4
, MEDIUM
, callback4
.callback(), pool_
.get(), BoundNetLog());
311 base::RunLoop().RunUntilIdle();
313 // Only the last socket should have connected.
314 EXPECT_FALSE(handle1
.socket());
315 EXPECT_FALSE(handle2
.socket());
316 EXPECT_FALSE(handle3
.socket());
317 EXPECT_TRUE(handle4
.socket()->IsConnected());
320 // Tests that sockets will still connect in parallel if the
321 // EnableSSLConnectJobWaiting flag is not enabled.
322 TEST_P(SSLClientSocketPoolTest
, SocketsConnectWithoutFlag
) {
323 StaticSocketDataProvider data1
;
324 StaticSocketDataProvider data2
;
325 StaticSocketDataProvider data3
;
326 socket_factory_
.AddSocketDataProvider(&data1
);
327 socket_factory_
.AddSocketDataProvider(&data2
);
328 socket_factory_
.AddSocketDataProvider(&data3
);
330 SSLSocketDataProvider
ssl(ASYNC
, OK
);
331 ssl
.is_in_session_cache
= false;
332 ssl
.should_pause_on_connect
= true;
333 SSLSocketDataProvider
ssl2(ASYNC
, OK
);
334 ssl2
.is_in_session_cache
= false;
335 ssl2
.should_pause_on_connect
= true;
336 SSLSocketDataProvider
ssl3(ASYNC
, OK
);
337 ssl3
.is_in_session_cache
= false;
338 ssl3
.should_pause_on_connect
= true;
339 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
340 socket_factory_
.AddSSLSocketDataProvider(&ssl2
);
341 socket_factory_
.AddSSLSocketDataProvider(&ssl3
);
343 CreatePool(true, false, false);
345 scoped_refptr
<SSLSocketParams
> params1
=
346 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
347 scoped_refptr
<SSLSocketParams
> params2
=
348 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
349 scoped_refptr
<SSLSocketParams
> params3
=
350 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
351 ClientSocketHandle handle1
;
352 ClientSocketHandle handle2
;
353 ClientSocketHandle handle3
;
354 TestCompletionCallback callback1
;
355 TestCompletionCallback callback2
;
356 TestCompletionCallback callback3
;
359 "b", params1
, MEDIUM
, callback1
.callback(), pool_
.get(), BoundNetLog());
361 "b", params2
, MEDIUM
, callback2
.callback(), pool_
.get(), BoundNetLog());
363 "b", params3
, MEDIUM
, callback3
.callback(), pool_
.get(), BoundNetLog());
365 base::RunLoop().RunUntilIdle();
367 std::vector
<MockSSLClientSocket
*> sockets
=
368 socket_factory_
.ssl_client_sockets();
370 // All sockets should have started their connections.
371 for (std::vector
<MockSSLClientSocket
*>::iterator it
= sockets
.begin();
374 EXPECT_TRUE((*it
)->reached_connect());
377 // Resume connecting all of the sockets.
378 for (std::vector
<MockSSLClientSocket
*>::iterator it
= sockets
.begin();
381 (*it
)->RestartPausedConnect();
384 callback1
.WaitForResult();
385 callback2
.WaitForResult();
386 callback3
.WaitForResult();
388 EXPECT_TRUE(handle1
.socket()->IsConnected());
389 EXPECT_TRUE(handle2
.socket()->IsConnected());
390 EXPECT_TRUE(handle3
.socket()->IsConnected());
393 // Tests that the pool deleting an SSLConnectJob will not cause a crash,
394 // or prevent pending sockets from connecting.
395 TEST_P(SSLClientSocketPoolTest
, DeletedSSLConnectJob
) {
396 StaticSocketDataProvider data1
;
397 StaticSocketDataProvider data2
;
398 StaticSocketDataProvider data3
;
399 socket_factory_
.AddSocketDataProvider(&data1
);
400 socket_factory_
.AddSocketDataProvider(&data2
);
401 socket_factory_
.AddSocketDataProvider(&data3
);
403 SSLSocketDataProvider
ssl(ASYNC
, OK
);
404 ssl
.is_in_session_cache
= false;
405 ssl
.should_pause_on_connect
= true;
406 SSLSocketDataProvider
ssl2(ASYNC
, OK
);
407 ssl2
.is_in_session_cache
= false;
408 SSLSocketDataProvider
ssl3(ASYNC
, OK
);
409 ssl3
.is_in_session_cache
= false;
410 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
411 socket_factory_
.AddSSLSocketDataProvider(&ssl2
);
412 socket_factory_
.AddSSLSocketDataProvider(&ssl3
);
414 enable_ssl_connect_job_waiting_
= true;
415 CreatePool(true, false, false);
417 scoped_refptr
<SSLSocketParams
> params1
=
418 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
419 scoped_refptr
<SSLSocketParams
> params2
=
420 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
421 scoped_refptr
<SSLSocketParams
> params3
=
422 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
423 ClientSocketHandle handle1
;
424 ClientSocketHandle handle2
;
425 ClientSocketHandle handle3
;
426 TestCompletionCallback callback1
;
427 TestCompletionCallback callback2
;
428 TestCompletionCallback callback3
;
431 "b", params1
, MEDIUM
, callback1
.callback(), pool_
.get(), BoundNetLog());
433 "b", params2
, MEDIUM
, callback2
.callback(), pool_
.get(), BoundNetLog());
435 "b", params3
, MEDIUM
, callback3
.callback(), pool_
.get(), BoundNetLog());
437 // Allow the connections to proceed until the first socket has started
439 base::RunLoop().RunUntilIdle();
441 std::vector
<MockSSLClientSocket
*> sockets
=
442 socket_factory_
.ssl_client_sockets();
444 pool_
->CancelRequest("b", &handle2
);
446 sockets
[0]->RestartPausedConnect();
448 callback1
.WaitForResult();
449 callback3
.WaitForResult();
451 EXPECT_TRUE(handle1
.socket()->IsConnected());
452 EXPECT_FALSE(handle2
.socket());
453 EXPECT_TRUE(handle3
.socket()->IsConnected());
456 // Tests that all pending sockets still connect when the pool deletes a pending
457 // SSLConnectJob which immediately followed a failed leading connection.
458 TEST_P(SSLClientSocketPoolTest
, DeletedSocketAfterFail
) {
459 StaticSocketDataProvider data1
;
460 StaticSocketDataProvider data2
;
461 StaticSocketDataProvider data3
;
462 StaticSocketDataProvider data4
;
463 socket_factory_
.AddSocketDataProvider(&data1
);
464 socket_factory_
.AddSocketDataProvider(&data2
);
465 socket_factory_
.AddSocketDataProvider(&data3
);
466 socket_factory_
.AddSocketDataProvider(&data4
);
468 SSLSocketDataProvider
ssl(ASYNC
, ERR_SSL_PROTOCOL_ERROR
);
469 ssl
.is_in_session_cache
= false;
470 ssl
.should_pause_on_connect
= true;
471 SSLSocketDataProvider
ssl2(ASYNC
, OK
);
472 ssl2
.is_in_session_cache
= false;
473 SSLSocketDataProvider
ssl3(ASYNC
, OK
);
474 ssl3
.is_in_session_cache
= false;
475 SSLSocketDataProvider
ssl4(ASYNC
, OK
);
476 ssl4
.is_in_session_cache
= false;
477 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
478 socket_factory_
.AddSSLSocketDataProvider(&ssl2
);
479 socket_factory_
.AddSSLSocketDataProvider(&ssl3
);
480 socket_factory_
.AddSSLSocketDataProvider(&ssl4
);
482 enable_ssl_connect_job_waiting_
= true;
483 CreatePool(true, false, false);
485 scoped_refptr
<SSLSocketParams
> params1
=
486 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
487 scoped_refptr
<SSLSocketParams
> params2
=
488 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
489 scoped_refptr
<SSLSocketParams
> params3
=
490 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
491 ClientSocketHandle handle1
;
492 ClientSocketHandle handle2
;
493 ClientSocketHandle handle3
;
494 TestCompletionCallback callback1
;
495 TestCompletionCallback callback2
;
496 TestCompletionCallback callback3
;
499 "b", params1
, MEDIUM
, callback1
.callback(), pool_
.get(), BoundNetLog());
501 "b", params2
, MEDIUM
, callback2
.callback(), pool_
.get(), BoundNetLog());
503 "b", params3
, MEDIUM
, callback3
.callback(), pool_
.get(), BoundNetLog());
505 // Allow the connections to proceed until the first socket has started
507 base::RunLoop().RunUntilIdle();
509 std::vector
<MockSSLClientSocket
*> sockets
=
510 socket_factory_
.ssl_client_sockets();
512 EXPECT_EQ(3u, sockets
.size());
513 EXPECT_TRUE(sockets
[0]->reached_connect());
514 EXPECT_FALSE(handle1
.socket());
516 pool_
->CancelRequest("b", &handle2
);
518 sockets
[0]->RestartPausedConnect();
520 callback1
.WaitForResult();
521 callback3
.WaitForResult();
523 EXPECT_FALSE(handle1
.socket());
524 EXPECT_FALSE(handle2
.socket());
525 EXPECT_TRUE(handle3
.socket()->IsConnected());
528 // Make sure that sockets still connect after the leader socket's
530 TEST_P(SSLClientSocketPoolTest
, SimultaneousConnectJobsFail
) {
531 StaticSocketDataProvider data1
;
532 StaticSocketDataProvider data2
;
533 StaticSocketDataProvider data3
;
534 StaticSocketDataProvider data4
;
535 StaticSocketDataProvider data5
;
536 socket_factory_
.AddSocketDataProvider(&data1
);
537 socket_factory_
.AddSocketDataProvider(&data2
);
538 socket_factory_
.AddSocketDataProvider(&data3
);
539 socket_factory_
.AddSocketDataProvider(&data4
);
540 socket_factory_
.AddSocketDataProvider(&data5
);
541 SSLSocketDataProvider
ssl(ASYNC
, ERR_SSL_PROTOCOL_ERROR
);
542 ssl
.is_in_session_cache
= false;
543 ssl
.should_pause_on_connect
= true;
544 SSLSocketDataProvider
ssl2(ASYNC
, OK
);
545 ssl2
.is_in_session_cache
= false;
546 ssl2
.should_pause_on_connect
= true;
547 SSLSocketDataProvider
ssl3(ASYNC
, OK
);
548 ssl3
.is_in_session_cache
= false;
549 SSLSocketDataProvider
ssl4(ASYNC
, OK
);
550 ssl4
.is_in_session_cache
= false;
551 SSLSocketDataProvider
ssl5(ASYNC
, OK
);
552 ssl5
.is_in_session_cache
= false;
554 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
555 socket_factory_
.AddSSLSocketDataProvider(&ssl2
);
556 socket_factory_
.AddSSLSocketDataProvider(&ssl3
);
557 socket_factory_
.AddSSLSocketDataProvider(&ssl4
);
558 socket_factory_
.AddSSLSocketDataProvider(&ssl5
);
560 enable_ssl_connect_job_waiting_
= true;
561 CreatePool(true, false, false);
562 scoped_refptr
<SSLSocketParams
> params1
=
563 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
564 scoped_refptr
<SSLSocketParams
> params2
=
565 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
566 scoped_refptr
<SSLSocketParams
> params3
=
567 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
568 scoped_refptr
<SSLSocketParams
> params4
=
569 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
570 ClientSocketHandle handle1
;
571 ClientSocketHandle handle2
;
572 ClientSocketHandle handle3
;
573 ClientSocketHandle handle4
;
574 TestCompletionCallback callback1
;
575 TestCompletionCallback callback2
;
576 TestCompletionCallback callback3
;
577 TestCompletionCallback callback4
;
579 "b", params1
, MEDIUM
, callback1
.callback(), pool_
.get(), BoundNetLog());
581 "b", params2
, MEDIUM
, callback2
.callback(), pool_
.get(), BoundNetLog());
583 "b", params3
, MEDIUM
, callback3
.callback(), pool_
.get(), BoundNetLog());
585 "b", params4
, MEDIUM
, callback4
.callback(), pool_
.get(), BoundNetLog());
587 base::RunLoop().RunUntilIdle();
589 std::vector
<MockSSLClientSocket
*> sockets
=
590 socket_factory_
.ssl_client_sockets();
592 std::vector
<MockSSLClientSocket
*>::const_iterator it
= sockets
.begin();
594 // The first socket should have had Connect called on it.
595 EXPECT_TRUE((*it
)->reached_connect());
598 // No other socket should have reached connect yet.
599 for (; it
!= sockets
.end(); ++it
)
600 EXPECT_FALSE((*it
)->reached_connect());
602 // Allow the first socket to resume it's connection process.
603 sockets
[0]->RestartPausedConnect();
605 base::RunLoop().RunUntilIdle();
607 // The second socket should have reached connect.
608 EXPECT_TRUE(sockets
[1]->reached_connect());
610 // Allow the second socket to continue its connection.
611 sockets
[1]->RestartPausedConnect();
613 base::RunLoop().RunUntilIdle();
615 EXPECT_FALSE(handle1
.socket());
616 EXPECT_TRUE(handle2
.socket()->IsConnected());
617 EXPECT_TRUE(handle3
.socket()->IsConnected());
618 EXPECT_TRUE(handle4
.socket()->IsConnected());
621 // Make sure that no sockets connect before the "leader" socket,
622 // given that the leader has a successful connection.
623 TEST_P(SSLClientSocketPoolTest
, SimultaneousConnectJobsSuccess
) {
624 StaticSocketDataProvider data1
;
625 StaticSocketDataProvider data2
;
626 StaticSocketDataProvider data3
;
627 socket_factory_
.AddSocketDataProvider(&data1
);
628 socket_factory_
.AddSocketDataProvider(&data2
);
629 socket_factory_
.AddSocketDataProvider(&data3
);
631 SSLSocketDataProvider
ssl(ASYNC
, OK
);
632 ssl
.is_in_session_cache
= false;
633 ssl
.should_pause_on_connect
= true;
634 SSLSocketDataProvider
ssl2(ASYNC
, OK
);
635 ssl2
.is_in_session_cache
= false;
636 SSLSocketDataProvider
ssl3(ASYNC
, OK
);
637 ssl3
.is_in_session_cache
= false;
638 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
639 socket_factory_
.AddSSLSocketDataProvider(&ssl2
);
640 socket_factory_
.AddSSLSocketDataProvider(&ssl3
);
642 enable_ssl_connect_job_waiting_
= true;
643 CreatePool(true, false, false);
645 scoped_refptr
<SSLSocketParams
> params1
=
646 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
647 scoped_refptr
<SSLSocketParams
> params2
=
648 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
649 scoped_refptr
<SSLSocketParams
> params3
=
650 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
651 ClientSocketHandle handle1
;
652 ClientSocketHandle handle2
;
653 ClientSocketHandle handle3
;
654 TestCompletionCallback callback1
;
655 TestCompletionCallback callback2
;
656 TestCompletionCallback callback3
;
659 "b", params1
, MEDIUM
, callback1
.callback(), pool_
.get(), BoundNetLog());
661 "b", params2
, MEDIUM
, callback2
.callback(), pool_
.get(), BoundNetLog());
663 "b", params3
, MEDIUM
, callback3
.callback(), pool_
.get(), BoundNetLog());
665 // Allow the connections to proceed until the first socket has finished
667 base::RunLoop().RunUntilIdle();
669 std::vector
<MockSSLClientSocket
*> sockets
=
670 socket_factory_
.ssl_client_sockets();
672 std::vector
<MockSSLClientSocket
*>::const_iterator it
= sockets
.begin();
673 // The first socket should have reached connect.
674 EXPECT_TRUE((*it
)->reached_connect());
676 // No other socket should have reached connect yet.
677 for (; it
!= sockets
.end(); ++it
)
678 EXPECT_FALSE((*it
)->reached_connect());
680 sockets
[0]->RestartPausedConnect();
682 callback1
.WaitForResult();
683 callback2
.WaitForResult();
684 callback3
.WaitForResult();
686 EXPECT_TRUE(handle1
.socket()->IsConnected());
687 EXPECT_TRUE(handle2
.socket()->IsConnected());
688 EXPECT_TRUE(handle3
.socket()->IsConnected());
691 TEST_P(SSLClientSocketPoolTest
, TCPFail
) {
692 StaticSocketDataProvider data
;
693 data
.set_connect_data(MockConnect(SYNCHRONOUS
, ERR_CONNECTION_FAILED
));
694 socket_factory_
.AddSocketDataProvider(&data
);
696 CreatePool(true /* tcp pool */, false, false);
697 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
700 ClientSocketHandle handle
;
701 int rv
= handle
.Init("a", params
, MEDIUM
, CompletionCallback(), pool_
.get(),
703 EXPECT_EQ(ERR_CONNECTION_FAILED
, rv
);
704 EXPECT_FALSE(handle
.is_initialized());
705 EXPECT_FALSE(handle
.socket());
706 EXPECT_FALSE(handle
.is_ssl_error());
709 TEST_P(SSLClientSocketPoolTest
, TCPFailAsync
) {
710 StaticSocketDataProvider data
;
711 data
.set_connect_data(MockConnect(ASYNC
, ERR_CONNECTION_FAILED
));
712 socket_factory_
.AddSocketDataProvider(&data
);
714 CreatePool(true /* tcp pool */, false, false);
715 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
718 ClientSocketHandle handle
;
719 TestCompletionCallback callback
;
720 int rv
= handle
.Init(
721 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
722 EXPECT_EQ(ERR_IO_PENDING
, rv
);
723 EXPECT_FALSE(handle
.is_initialized());
724 EXPECT_FALSE(handle
.socket());
726 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
727 EXPECT_FALSE(handle
.is_initialized());
728 EXPECT_FALSE(handle
.socket());
729 EXPECT_FALSE(handle
.is_ssl_error());
732 TEST_P(SSLClientSocketPoolTest
, BasicDirect
) {
733 StaticSocketDataProvider data
;
734 data
.set_connect_data(MockConnect(SYNCHRONOUS
, OK
));
735 socket_factory_
.AddSocketDataProvider(&data
);
736 SSLSocketDataProvider
ssl(SYNCHRONOUS
, OK
);
737 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
739 CreatePool(true /* tcp pool */, false, false);
740 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
743 ClientSocketHandle handle
;
744 TestCompletionCallback callback
;
745 int rv
= handle
.Init(
746 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
748 EXPECT_TRUE(handle
.is_initialized());
749 EXPECT_TRUE(handle
.socket());
750 TestLoadTimingInfo(handle
);
753 // Make sure that SSLConnectJob passes on its priority to its
754 // socket request on Init (for the DIRECT case).
755 TEST_P(SSLClientSocketPoolTest
, SetSocketRequestPriorityOnInitDirect
) {
756 CreatePool(true /* tcp pool */, false, false);
757 scoped_refptr
<SSLSocketParams
> params
=
758 SSLParams(ProxyServer::SCHEME_DIRECT
, false);
760 for (int i
= MINIMUM_PRIORITY
; i
<= MAXIMUM_PRIORITY
; ++i
) {
761 RequestPriority priority
= static_cast<RequestPriority
>(i
);
762 StaticSocketDataProvider data
;
763 data
.set_connect_data(MockConnect(SYNCHRONOUS
, OK
));
764 socket_factory_
.AddSocketDataProvider(&data
);
765 SSLSocketDataProvider
ssl(SYNCHRONOUS
, OK
);
766 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
768 ClientSocketHandle handle
;
769 TestCompletionCallback callback
;
770 EXPECT_EQ(OK
, handle
.Init("a", params
, priority
, callback
.callback(),
771 pool_
.get(), BoundNetLog()));
772 EXPECT_EQ(priority
, transport_socket_pool_
.last_request_priority());
773 handle
.socket()->Disconnect();
777 TEST_P(SSLClientSocketPoolTest
, BasicDirectAsync
) {
778 StaticSocketDataProvider data
;
779 socket_factory_
.AddSocketDataProvider(&data
);
780 SSLSocketDataProvider
ssl(ASYNC
, OK
);
781 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
783 CreatePool(true /* tcp pool */, false, false);
784 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
787 ClientSocketHandle handle
;
788 TestCompletionCallback callback
;
789 int rv
= handle
.Init(
790 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
791 EXPECT_EQ(ERR_IO_PENDING
, rv
);
792 EXPECT_FALSE(handle
.is_initialized());
793 EXPECT_FALSE(handle
.socket());
795 EXPECT_EQ(OK
, callback
.WaitForResult());
796 EXPECT_TRUE(handle
.is_initialized());
797 EXPECT_TRUE(handle
.socket());
798 TestLoadTimingInfo(handle
);
801 TEST_P(SSLClientSocketPoolTest
, DirectCertError
) {
802 StaticSocketDataProvider data
;
803 socket_factory_
.AddSocketDataProvider(&data
);
804 SSLSocketDataProvider
ssl(ASYNC
, ERR_CERT_COMMON_NAME_INVALID
);
805 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
807 CreatePool(true /* tcp pool */, false, false);
808 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
811 ClientSocketHandle handle
;
812 TestCompletionCallback callback
;
813 int rv
= handle
.Init(
814 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
815 EXPECT_EQ(ERR_IO_PENDING
, rv
);
816 EXPECT_FALSE(handle
.is_initialized());
817 EXPECT_FALSE(handle
.socket());
819 EXPECT_EQ(ERR_CERT_COMMON_NAME_INVALID
, callback
.WaitForResult());
820 EXPECT_TRUE(handle
.is_initialized());
821 EXPECT_TRUE(handle
.socket());
822 TestLoadTimingInfo(handle
);
825 TEST_P(SSLClientSocketPoolTest
, DirectSSLError
) {
826 StaticSocketDataProvider data
;
827 socket_factory_
.AddSocketDataProvider(&data
);
828 SSLSocketDataProvider
ssl(ASYNC
, ERR_SSL_PROTOCOL_ERROR
);
829 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
831 CreatePool(true /* tcp pool */, false, false);
832 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
835 ClientSocketHandle handle
;
836 TestCompletionCallback callback
;
837 int rv
= handle
.Init(
838 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
839 EXPECT_EQ(ERR_IO_PENDING
, rv
);
840 EXPECT_FALSE(handle
.is_initialized());
841 EXPECT_FALSE(handle
.socket());
843 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR
, callback
.WaitForResult());
844 EXPECT_FALSE(handle
.is_initialized());
845 EXPECT_FALSE(handle
.socket());
846 EXPECT_TRUE(handle
.is_ssl_error());
849 TEST_P(SSLClientSocketPoolTest
, DirectWithNPN
) {
850 StaticSocketDataProvider data
;
851 socket_factory_
.AddSocketDataProvider(&data
);
852 SSLSocketDataProvider
ssl(ASYNC
, OK
);
853 ssl
.SetNextProto(kProtoHTTP11
);
854 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
856 CreatePool(true /* tcp pool */, false, false);
857 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
860 ClientSocketHandle handle
;
861 TestCompletionCallback callback
;
862 int rv
= handle
.Init(
863 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
864 EXPECT_EQ(ERR_IO_PENDING
, rv
);
865 EXPECT_FALSE(handle
.is_initialized());
866 EXPECT_FALSE(handle
.socket());
868 EXPECT_EQ(OK
, callback
.WaitForResult());
869 EXPECT_TRUE(handle
.is_initialized());
870 EXPECT_TRUE(handle
.socket());
871 TestLoadTimingInfo(handle
);
872 SSLClientSocket
* ssl_socket
= static_cast<SSLClientSocket
*>(handle
.socket());
873 EXPECT_TRUE(ssl_socket
->WasNpnNegotiated());
876 TEST_P(SSLClientSocketPoolTest
, DirectNoSPDY
) {
877 StaticSocketDataProvider data
;
878 socket_factory_
.AddSocketDataProvider(&data
);
879 SSLSocketDataProvider
ssl(ASYNC
, OK
);
880 ssl
.SetNextProto(kProtoHTTP11
);
881 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
883 CreatePool(true /* tcp pool */, false, false);
884 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
887 ClientSocketHandle handle
;
888 TestCompletionCallback callback
;
889 int rv
= handle
.Init(
890 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
891 EXPECT_EQ(ERR_IO_PENDING
, rv
);
892 EXPECT_FALSE(handle
.is_initialized());
893 EXPECT_FALSE(handle
.socket());
895 EXPECT_EQ(ERR_NPN_NEGOTIATION_FAILED
, callback
.WaitForResult());
896 EXPECT_FALSE(handle
.is_initialized());
897 EXPECT_FALSE(handle
.socket());
898 EXPECT_TRUE(handle
.is_ssl_error());
901 TEST_P(SSLClientSocketPoolTest
, DirectGotSPDY
) {
902 StaticSocketDataProvider data
;
903 socket_factory_
.AddSocketDataProvider(&data
);
904 SSLSocketDataProvider
ssl(ASYNC
, OK
);
905 ssl
.SetNextProto(GetParam());
906 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
908 CreatePool(true /* tcp pool */, false, false);
909 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
912 ClientSocketHandle handle
;
913 TestCompletionCallback callback
;
914 int rv
= handle
.Init(
915 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
916 EXPECT_EQ(ERR_IO_PENDING
, rv
);
917 EXPECT_FALSE(handle
.is_initialized());
918 EXPECT_FALSE(handle
.socket());
920 EXPECT_EQ(OK
, callback
.WaitForResult());
921 EXPECT_TRUE(handle
.is_initialized());
922 EXPECT_TRUE(handle
.socket());
923 TestLoadTimingInfo(handle
);
925 SSLClientSocket
* ssl_socket
= static_cast<SSLClientSocket
*>(handle
.socket());
926 EXPECT_TRUE(ssl_socket
->WasNpnNegotiated());
928 ssl_socket
->GetNextProto(&proto
);
929 EXPECT_EQ(GetParam(), SSLClientSocket::NextProtoFromString(proto
));
932 TEST_P(SSLClientSocketPoolTest
, DirectGotBonusSPDY
) {
933 StaticSocketDataProvider data
;
934 socket_factory_
.AddSocketDataProvider(&data
);
935 SSLSocketDataProvider
ssl(ASYNC
, OK
);
936 ssl
.SetNextProto(GetParam());
937 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
939 CreatePool(true /* tcp pool */, false, false);
940 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_DIRECT
,
943 ClientSocketHandle handle
;
944 TestCompletionCallback callback
;
945 int rv
= handle
.Init(
946 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
947 EXPECT_EQ(ERR_IO_PENDING
, rv
);
948 EXPECT_FALSE(handle
.is_initialized());
949 EXPECT_FALSE(handle
.socket());
951 EXPECT_EQ(OK
, callback
.WaitForResult());
952 EXPECT_TRUE(handle
.is_initialized());
953 EXPECT_TRUE(handle
.socket());
954 TestLoadTimingInfo(handle
);
956 SSLClientSocket
* ssl_socket
= static_cast<SSLClientSocket
*>(handle
.socket());
957 EXPECT_TRUE(ssl_socket
->WasNpnNegotiated());
959 ssl_socket
->GetNextProto(&proto
);
960 EXPECT_EQ(GetParam(), SSLClientSocket::NextProtoFromString(proto
));
963 TEST_P(SSLClientSocketPoolTest
, SOCKSFail
) {
964 StaticSocketDataProvider data
;
965 data
.set_connect_data(MockConnect(SYNCHRONOUS
, ERR_CONNECTION_FAILED
));
966 socket_factory_
.AddSocketDataProvider(&data
);
968 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
969 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_SOCKS5
,
972 ClientSocketHandle handle
;
973 TestCompletionCallback callback
;
974 int rv
= handle
.Init(
975 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
976 EXPECT_EQ(ERR_CONNECTION_FAILED
, rv
);
977 EXPECT_FALSE(handle
.is_initialized());
978 EXPECT_FALSE(handle
.socket());
979 EXPECT_FALSE(handle
.is_ssl_error());
982 TEST_P(SSLClientSocketPoolTest
, SOCKSFailAsync
) {
983 StaticSocketDataProvider data
;
984 data
.set_connect_data(MockConnect(ASYNC
, ERR_CONNECTION_FAILED
));
985 socket_factory_
.AddSocketDataProvider(&data
);
987 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
988 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_SOCKS5
,
991 ClientSocketHandle handle
;
992 TestCompletionCallback callback
;
993 int rv
= handle
.Init(
994 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
995 EXPECT_EQ(ERR_IO_PENDING
, rv
);
996 EXPECT_FALSE(handle
.is_initialized());
997 EXPECT_FALSE(handle
.socket());
999 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
1000 EXPECT_FALSE(handle
.is_initialized());
1001 EXPECT_FALSE(handle
.socket());
1002 EXPECT_FALSE(handle
.is_ssl_error());
1005 TEST_P(SSLClientSocketPoolTest
, SOCKSBasic
) {
1006 StaticSocketDataProvider data
;
1007 data
.set_connect_data(MockConnect(SYNCHRONOUS
, OK
));
1008 socket_factory_
.AddSocketDataProvider(&data
);
1009 SSLSocketDataProvider
ssl(SYNCHRONOUS
, OK
);
1010 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
1012 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1013 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_SOCKS5
,
1016 ClientSocketHandle handle
;
1017 TestCompletionCallback callback
;
1018 int rv
= handle
.Init(
1019 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
1021 EXPECT_TRUE(handle
.is_initialized());
1022 EXPECT_TRUE(handle
.socket());
1023 // SOCKS5 generally has no DNS times, but the mock SOCKS5 sockets used here
1024 // don't go through the real logic, unlike in the HTTP proxy tests.
1025 TestLoadTimingInfo(handle
);
1028 // Make sure that SSLConnectJob passes on its priority to its
1029 // transport socket on Init (for the SOCKS_PROXY case).
1030 TEST_P(SSLClientSocketPoolTest
, SetTransportPriorityOnInitSOCKS
) {
1031 StaticSocketDataProvider data
;
1032 data
.set_connect_data(MockConnect(SYNCHRONOUS
, OK
));
1033 socket_factory_
.AddSocketDataProvider(&data
);
1034 SSLSocketDataProvider
ssl(SYNCHRONOUS
, OK
);
1035 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
1037 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1038 scoped_refptr
<SSLSocketParams
> params
=
1039 SSLParams(ProxyServer::SCHEME_SOCKS5
, false);
1041 ClientSocketHandle handle
;
1042 TestCompletionCallback callback
;
1043 EXPECT_EQ(OK
, handle
.Init("a", params
, HIGHEST
, callback
.callback(),
1044 pool_
.get(), BoundNetLog()));
1045 EXPECT_EQ(HIGHEST
, transport_socket_pool_
.last_request_priority());
1048 TEST_P(SSLClientSocketPoolTest
, SOCKSBasicAsync
) {
1049 StaticSocketDataProvider data
;
1050 socket_factory_
.AddSocketDataProvider(&data
);
1051 SSLSocketDataProvider
ssl(ASYNC
, OK
);
1052 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
1054 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1055 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_SOCKS5
,
1058 ClientSocketHandle handle
;
1059 TestCompletionCallback callback
;
1060 int rv
= handle
.Init(
1061 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
1062 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1063 EXPECT_FALSE(handle
.is_initialized());
1064 EXPECT_FALSE(handle
.socket());
1066 EXPECT_EQ(OK
, callback
.WaitForResult());
1067 EXPECT_TRUE(handle
.is_initialized());
1068 EXPECT_TRUE(handle
.socket());
1069 // SOCKS5 generally has no DNS times, but the mock SOCKS5 sockets used here
1070 // don't go through the real logic, unlike in the HTTP proxy tests.
1071 TestLoadTimingInfo(handle
);
1074 TEST_P(SSLClientSocketPoolTest
, HttpProxyFail
) {
1075 StaticSocketDataProvider data
;
1076 data
.set_connect_data(MockConnect(SYNCHRONOUS
, ERR_CONNECTION_FAILED
));
1077 socket_factory_
.AddSocketDataProvider(&data
);
1079 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1080 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_HTTP
,
1083 ClientSocketHandle handle
;
1084 TestCompletionCallback callback
;
1085 int rv
= handle
.Init(
1086 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
1087 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED
, rv
);
1088 EXPECT_FALSE(handle
.is_initialized());
1089 EXPECT_FALSE(handle
.socket());
1090 EXPECT_FALSE(handle
.is_ssl_error());
1093 TEST_P(SSLClientSocketPoolTest
, HttpProxyFailAsync
) {
1094 StaticSocketDataProvider data
;
1095 data
.set_connect_data(MockConnect(ASYNC
, ERR_CONNECTION_FAILED
));
1096 socket_factory_
.AddSocketDataProvider(&data
);
1098 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1099 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_HTTP
,
1102 ClientSocketHandle handle
;
1103 TestCompletionCallback callback
;
1104 int rv
= handle
.Init(
1105 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
1106 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1107 EXPECT_FALSE(handle
.is_initialized());
1108 EXPECT_FALSE(handle
.socket());
1110 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED
, callback
.WaitForResult());
1111 EXPECT_FALSE(handle
.is_initialized());
1112 EXPECT_FALSE(handle
.socket());
1113 EXPECT_FALSE(handle
.is_ssl_error());
1116 TEST_P(SSLClientSocketPoolTest
, HttpProxyBasic
) {
1117 MockWrite writes
[] = {
1118 MockWrite(SYNCHRONOUS
,
1119 "CONNECT host:80 HTTP/1.1\r\n"
1121 "Proxy-Connection: keep-alive\r\n"
1122 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
1124 MockRead reads
[] = {
1125 MockRead(SYNCHRONOUS
, "HTTP/1.1 200 Connection Established\r\n\r\n"),
1127 StaticSocketDataProvider
data(reads
, arraysize(reads
), writes
,
1129 data
.set_connect_data(MockConnect(SYNCHRONOUS
, OK
));
1130 socket_factory_
.AddSocketDataProvider(&data
);
1132 SSLSocketDataProvider
ssl(SYNCHRONOUS
, OK
);
1133 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
1135 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1136 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_HTTP
,
1139 ClientSocketHandle handle
;
1140 TestCompletionCallback callback
;
1141 int rv
= handle
.Init(
1142 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
1144 EXPECT_TRUE(handle
.is_initialized());
1145 EXPECT_TRUE(handle
.socket());
1146 TestLoadTimingInfoNoDns(handle
);
1149 // Make sure that SSLConnectJob passes on its priority to its
1150 // transport socket on Init (for the HTTP_PROXY case).
1151 TEST_P(SSLClientSocketPoolTest
, SetTransportPriorityOnInitHTTP
) {
1152 MockWrite writes
[] = {
1153 MockWrite(SYNCHRONOUS
,
1154 "CONNECT host:80 HTTP/1.1\r\n"
1156 "Proxy-Connection: keep-alive\r\n"
1157 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
1159 MockRead reads
[] = {
1160 MockRead(SYNCHRONOUS
, "HTTP/1.1 200 Connection Established\r\n\r\n"),
1162 StaticSocketDataProvider
data(reads
, arraysize(reads
), writes
,
1164 data
.set_connect_data(MockConnect(SYNCHRONOUS
, OK
));
1165 socket_factory_
.AddSocketDataProvider(&data
);
1167 SSLSocketDataProvider
ssl(SYNCHRONOUS
, OK
);
1168 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
1170 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1171 scoped_refptr
<SSLSocketParams
> params
=
1172 SSLParams(ProxyServer::SCHEME_HTTP
, false);
1174 ClientSocketHandle handle
;
1175 TestCompletionCallback callback
;
1176 EXPECT_EQ(OK
, handle
.Init("a", params
, HIGHEST
, callback
.callback(),
1177 pool_
.get(), BoundNetLog()));
1178 EXPECT_EQ(HIGHEST
, transport_socket_pool_
.last_request_priority());
1181 TEST_P(SSLClientSocketPoolTest
, HttpProxyBasicAsync
) {
1182 MockWrite writes
[] = {
1183 MockWrite("CONNECT host:80 HTTP/1.1\r\n"
1185 "Proxy-Connection: keep-alive\r\n"
1186 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
1188 MockRead reads
[] = {
1189 MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"),
1191 StaticSocketDataProvider
data(reads
, arraysize(reads
), writes
,
1193 socket_factory_
.AddSocketDataProvider(&data
);
1195 SSLSocketDataProvider
ssl(ASYNC
, OK
);
1196 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
1198 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1199 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_HTTP
,
1202 ClientSocketHandle handle
;
1203 TestCompletionCallback callback
;
1204 int rv
= handle
.Init(
1205 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
1206 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1207 EXPECT_FALSE(handle
.is_initialized());
1208 EXPECT_FALSE(handle
.socket());
1210 EXPECT_EQ(OK
, callback
.WaitForResult());
1211 EXPECT_TRUE(handle
.is_initialized());
1212 EXPECT_TRUE(handle
.socket());
1213 TestLoadTimingInfoNoDns(handle
);
1216 TEST_P(SSLClientSocketPoolTest
, NeedProxyAuth
) {
1217 MockWrite writes
[] = {
1218 MockWrite("CONNECT host:80 HTTP/1.1\r\n"
1220 "Proxy-Connection: keep-alive\r\n\r\n"),
1222 MockRead reads
[] = {
1223 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"),
1224 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"),
1225 MockRead("Content-Length: 10\r\n\r\n"),
1226 MockRead("0123456789"),
1228 StaticSocketDataProvider
data(reads
, arraysize(reads
), writes
,
1230 socket_factory_
.AddSocketDataProvider(&data
);
1231 SSLSocketDataProvider
ssl(ASYNC
, OK
);
1232 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
1234 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1235 scoped_refptr
<SSLSocketParams
> params
= SSLParams(ProxyServer::SCHEME_HTTP
,
1238 ClientSocketHandle handle
;
1239 TestCompletionCallback callback
;
1240 int rv
= handle
.Init(
1241 "a", params
, MEDIUM
, callback
.callback(), pool_
.get(), BoundNetLog());
1242 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1243 EXPECT_FALSE(handle
.is_initialized());
1244 EXPECT_FALSE(handle
.socket());
1246 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED
, callback
.WaitForResult());
1247 EXPECT_FALSE(handle
.is_initialized());
1248 EXPECT_FALSE(handle
.socket());
1249 EXPECT_FALSE(handle
.is_ssl_error());
1250 const HttpResponseInfo
& tunnel_info
= handle
.ssl_error_response_info();
1251 EXPECT_EQ(tunnel_info
.headers
->response_code(), 407);
1252 scoped_ptr
<ClientSocketHandle
> tunnel_handle(
1253 handle
.release_pending_http_proxy_connection());
1254 EXPECT_TRUE(tunnel_handle
->socket());
1255 EXPECT_FALSE(tunnel_handle
->socket()->IsConnected());
1258 TEST_P(SSLClientSocketPoolTest
, IPPooling
) {
1259 const int kTestPort
= 80;
1264 AddressList addresses
;
1266 { "www.webkit.org", "192.0.2.33,192.168.0.1,192.168.0.5" },
1267 { "code.google.com", "192.168.0.2,192.168.0.3,192.168.0.5" },
1268 { "js.webkit.org", "192.168.0.4,192.168.0.1,192.0.2.33" },
1271 host_resolver_
.set_synchronous_mode(true);
1272 for (size_t i
= 0; i
< arraysize(test_hosts
); i
++) {
1273 host_resolver_
.rules()->AddIPLiteralRule(
1274 test_hosts
[i
].name
, test_hosts
[i
].iplist
, std::string());
1276 // This test requires that the HostResolver cache be populated. Normal
1277 // code would have done this already, but we do it manually.
1278 HostResolver::RequestInfo
info(HostPortPair(test_hosts
[i
].name
, kTestPort
));
1279 host_resolver_
.Resolve(info
,
1281 &test_hosts
[i
].addresses
,
1282 CompletionCallback(),
1286 // Setup a SpdySessionKey
1287 test_hosts
[i
].key
= SpdySessionKey(
1288 HostPortPair(test_hosts
[i
].name
, kTestPort
), ProxyServer::Direct(),
1289 PRIVACY_MODE_DISABLED
);
1292 MockRead reads
[] = {
1293 MockRead(ASYNC
, ERR_IO_PENDING
),
1295 StaticSocketDataProvider
data(reads
, arraysize(reads
), NULL
, 0);
1296 socket_factory_
.AddSocketDataProvider(&data
);
1297 SSLSocketDataProvider
ssl(ASYNC
, OK
);
1298 ssl
.cert
= X509Certificate::CreateFromBytes(
1299 reinterpret_cast<const char*>(webkit_der
), sizeof(webkit_der
));
1300 ssl
.SetNextProto(GetParam());
1301 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
1303 CreatePool(true /* tcp pool */, false, false);
1304 base::WeakPtr
<SpdySession
> spdy_session
=
1305 CreateSecureSpdySession(session_
, test_hosts
[0].key
, BoundNetLog());
1308 HasSpdySession(session_
->spdy_session_pool(), test_hosts
[0].key
));
1310 HasSpdySession(session_
->spdy_session_pool(), test_hosts
[1].key
));
1312 HasSpdySession(session_
->spdy_session_pool(), test_hosts
[2].key
));
1314 session_
->spdy_session_pool()->CloseAllSessions();
1317 void SSLClientSocketPoolTest::TestIPPoolingDisabled(
1318 SSLSocketDataProvider
* ssl
) {
1319 const int kTestPort
= 80;
1324 AddressList addresses
;
1326 { "www.webkit.org", "192.0.2.33,192.168.0.1,192.168.0.5" },
1327 { "js.webkit.com", "192.168.0.4,192.168.0.1,192.0.2.33" },
1330 TestCompletionCallback callback
;
1332 for (size_t i
= 0; i
< arraysize(test_hosts
); i
++) {
1333 host_resolver_
.rules()->AddIPLiteralRule(
1334 test_hosts
[i
].name
, test_hosts
[i
].iplist
, std::string());
1336 // This test requires that the HostResolver cache be populated. Normal
1337 // code would have done this already, but we do it manually.
1338 HostResolver::RequestInfo
info(HostPortPair(test_hosts
[i
].name
, kTestPort
));
1339 rv
= host_resolver_
.Resolve(info
,
1341 &test_hosts
[i
].addresses
,
1342 callback
.callback(),
1345 EXPECT_EQ(OK
, callback
.GetResult(rv
));
1347 // Setup a SpdySessionKey
1348 test_hosts
[i
].key
= SpdySessionKey(
1349 HostPortPair(test_hosts
[i
].name
, kTestPort
), ProxyServer::Direct(),
1350 PRIVACY_MODE_DISABLED
);
1353 MockRead reads
[] = {
1354 MockRead(ASYNC
, ERR_IO_PENDING
),
1356 StaticSocketDataProvider
data(reads
, arraysize(reads
), NULL
, 0);
1357 socket_factory_
.AddSocketDataProvider(&data
);
1358 socket_factory_
.AddSSLSocketDataProvider(ssl
);
1360 CreatePool(true /* tcp pool */, false, false);
1361 base::WeakPtr
<SpdySession
> spdy_session
=
1362 CreateSecureSpdySession(session_
, test_hosts
[0].key
, BoundNetLog());
1365 HasSpdySession(session_
->spdy_session_pool(), test_hosts
[0].key
));
1367 HasSpdySession(session_
->spdy_session_pool(), test_hosts
[1].key
));
1369 session_
->spdy_session_pool()->CloseAllSessions();
1372 // Verifies that an SSL connection with client authentication disables SPDY IP
1374 TEST_P(SSLClientSocketPoolTest
, IPPoolingClientCert
) {
1375 SSLSocketDataProvider
ssl(ASYNC
, OK
);
1376 ssl
.cert
= X509Certificate::CreateFromBytes(
1377 reinterpret_cast<const char*>(webkit_der
), sizeof(webkit_der
));
1378 ssl
.client_cert_sent
= true;
1379 ssl
.SetNextProto(GetParam());
1380 TestIPPoolingDisabled(&ssl
);
1383 // Verifies that an SSL connection with channel ID disables SPDY IP pooling.
1384 TEST_P(SSLClientSocketPoolTest
, IPPoolingChannelID
) {
1385 SSLSocketDataProvider
ssl(ASYNC
, OK
);
1386 ssl
.channel_id_sent
= true;
1387 ssl
.SetNextProto(GetParam());
1388 TestIPPoolingDisabled(&ssl
);
1391 // It would be nice to also test the timeouts in SSLClientSocketPool.