Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / socket / ssl_client_socket_pool_unittest.cc
blobf2792a7dc50b003f1ec785f6a431ec15816fcc7b
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"
38 namespace net {
40 namespace {
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> {
81 protected:
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(HostPortPair("host", 443),
91 false,
92 false,
93 OnHostResolutionCallback())),
94 transport_histograms_("MockTCP"),
95 transport_socket_pool_(kMaxSockets,
96 kMaxSocketsPerGroup,
97 &transport_histograms_,
98 &socket_factory_),
99 proxy_transport_socket_params_(
100 new TransportSocketParams(HostPortPair("proxy", 443),
101 false,
102 false,
103 OnHostResolutionCallback())),
104 socks_socket_params_(
105 new SOCKSSocketParams(proxy_transport_socket_params_,
106 true,
107 HostPortPair("sockshost", 443))),
108 socks_histograms_("MockSOCKS"),
109 socks_socket_pool_(kMaxSockets,
110 kMaxSocketsPerGroup,
111 &socks_histograms_,
112 &transport_socket_pool_),
113 http_proxy_socket_params_(
114 new HttpProxySocketParams(proxy_transport_socket_params_,
115 NULL,
116 GURL("http://host"),
117 std::string(),
118 HostPortPair("host", 80),
119 session_->http_auth_cache(),
120 session_->http_auth_handler_factory(),
121 session_->spdy_session_pool(),
122 true)),
123 http_proxy_histograms_("MockHttpProxy"),
124 http_proxy_socket_pool_(kMaxSockets,
125 kMaxSocketsPerGroup,
126 &http_proxy_histograms_,
127 &host_resolver_,
128 &transport_socket_pool_,
129 NULL,
130 NULL),
131 enable_ssl_connect_job_waiting_(false) {
132 scoped_refptr<SSLConfigService> ssl_config_service(
133 new SSLConfigServiceDefaults);
134 ssl_config_service->GetSSLConfig(&ssl_config_);
137 void CreatePool(bool transport_pool, bool http_proxy_pool, bool socks_pool) {
138 ssl_histograms_.reset(new ClientSocketPoolHistograms("SSLUnitTest"));
139 pool_.reset(new SSLClientSocketPool(
140 kMaxSockets,
141 kMaxSocketsPerGroup,
142 ssl_histograms_.get(),
143 NULL /* host_resolver */,
144 NULL /* cert_verifier */,
145 NULL /* channel_id_service */,
146 NULL /* transport_security_state */,
147 NULL /* cert_transparency_verifier */,
148 std::string() /* ssl_session_cache_shard */,
149 &socket_factory_,
150 transport_pool ? &transport_socket_pool_ : NULL,
151 socks_pool ? &socks_socket_pool_ : NULL,
152 http_proxy_pool ? &http_proxy_socket_pool_ : NULL,
153 NULL,
154 enable_ssl_connect_job_waiting_,
155 NULL));
158 scoped_refptr<SSLSocketParams> SSLParams(ProxyServer::Scheme proxy,
159 bool want_spdy_over_npn) {
160 return make_scoped_refptr(new SSLSocketParams(
161 proxy == ProxyServer::SCHEME_DIRECT ? direct_transport_socket_params_
162 : NULL,
163 proxy == ProxyServer::SCHEME_SOCKS5 ? socks_socket_params_ : NULL,
164 proxy == ProxyServer::SCHEME_HTTP ? http_proxy_socket_params_ : NULL,
165 HostPortPair("host", 443),
166 ssl_config_,
167 PRIVACY_MODE_DISABLED,
169 false,
170 want_spdy_over_npn));
173 void AddAuthToCache() {
174 const base::string16 kFoo(base::ASCIIToUTF16("foo"));
175 const base::string16 kBar(base::ASCIIToUTF16("bar"));
176 session_->http_auth_cache()->Add(GURL("http://proxy:443/"),
177 "MyRealm1",
178 HttpAuth::AUTH_SCHEME_BASIC,
179 "Basic realm=MyRealm1",
180 AuthCredentials(kFoo, kBar),
181 "/");
184 HttpNetworkSession* CreateNetworkSession() {
185 HttpNetworkSession::Params params;
186 params.host_resolver = &host_resolver_;
187 params.cert_verifier = cert_verifier_.get();
188 params.transport_security_state = transport_security_state_.get();
189 params.proxy_service = proxy_service_.get();
190 params.client_socket_factory = &socket_factory_;
191 params.ssl_config_service = ssl_config_service_.get();
192 params.http_auth_handler_factory = http_auth_handler_factory_.get();
193 params.http_server_properties =
194 http_server_properties_.GetWeakPtr();
195 params.enable_spdy_compression = false;
196 params.spdy_default_protocol = GetParam();
197 return new HttpNetworkSession(params);
200 void TestIPPoolingDisabled(SSLSocketDataProvider* ssl);
202 MockClientSocketFactory socket_factory_;
203 MockCachingHostResolver host_resolver_;
204 scoped_ptr<CertVerifier> cert_verifier_;
205 scoped_ptr<TransportSecurityState> transport_security_state_;
206 const scoped_ptr<ProxyService> proxy_service_;
207 const scoped_refptr<SSLConfigService> ssl_config_service_;
208 const scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory_;
209 HttpServerPropertiesImpl http_server_properties_;
210 const scoped_refptr<HttpNetworkSession> session_;
212 scoped_refptr<TransportSocketParams> direct_transport_socket_params_;
213 ClientSocketPoolHistograms transport_histograms_;
214 MockTransportClientSocketPool transport_socket_pool_;
216 scoped_refptr<TransportSocketParams> proxy_transport_socket_params_;
218 scoped_refptr<SOCKSSocketParams> socks_socket_params_;
219 ClientSocketPoolHistograms socks_histograms_;
220 MockSOCKSClientSocketPool socks_socket_pool_;
222 scoped_refptr<HttpProxySocketParams> http_proxy_socket_params_;
223 ClientSocketPoolHistograms http_proxy_histograms_;
224 HttpProxyClientSocketPool http_proxy_socket_pool_;
226 SSLConfig ssl_config_;
227 scoped_ptr<ClientSocketPoolHistograms> ssl_histograms_;
228 scoped_ptr<SSLClientSocketPool> pool_;
230 bool enable_ssl_connect_job_waiting_;
233 INSTANTIATE_TEST_CASE_P(
234 NextProto,
235 SSLClientSocketPoolTest,
236 testing::Values(kProtoDeprecatedSPDY2,
237 kProtoSPDY3, kProtoSPDY31, kProtoSPDY4));
239 // Tests that the final socket will connect even if all sockets
240 // prior to it fail.
242 // All sockets should wait for the first socket to attempt to
243 // connect. Once it fails to connect, all other sockets should
244 // attempt to connect. All should fail, except the final socket.
245 TEST_P(SSLClientSocketPoolTest, AllSocketsFailButLast) {
246 // Although we request four sockets, the first three socket connect
247 // failures cause the socket pool to create three more sockets because
248 // there are pending requests.
249 StaticSocketDataProvider data1;
250 StaticSocketDataProvider data2;
251 StaticSocketDataProvider data3;
252 StaticSocketDataProvider data4;
253 StaticSocketDataProvider data5;
254 StaticSocketDataProvider data6;
255 StaticSocketDataProvider data7;
256 socket_factory_.AddSocketDataProvider(&data1);
257 socket_factory_.AddSocketDataProvider(&data2);
258 socket_factory_.AddSocketDataProvider(&data3);
259 socket_factory_.AddSocketDataProvider(&data4);
260 socket_factory_.AddSocketDataProvider(&data5);
261 socket_factory_.AddSocketDataProvider(&data6);
262 socket_factory_.AddSocketDataProvider(&data7);
263 SSLSocketDataProvider ssl(ASYNC, ERR_SSL_PROTOCOL_ERROR);
264 ssl.is_in_session_cache = false;
265 SSLSocketDataProvider ssl2(ASYNC, ERR_SSL_PROTOCOL_ERROR);
266 ssl2.is_in_session_cache = false;
267 SSLSocketDataProvider ssl3(ASYNC, ERR_SSL_PROTOCOL_ERROR);
268 ssl3.is_in_session_cache = false;
269 SSLSocketDataProvider ssl4(ASYNC, OK);
270 ssl4.is_in_session_cache = false;
271 SSLSocketDataProvider ssl5(ASYNC, OK);
272 ssl5.is_in_session_cache = false;
273 SSLSocketDataProvider ssl6(ASYNC, OK);
274 ssl6.is_in_session_cache = false;
275 SSLSocketDataProvider ssl7(ASYNC, OK);
276 ssl7.is_in_session_cache = false;
278 socket_factory_.AddSSLSocketDataProvider(&ssl);
279 socket_factory_.AddSSLSocketDataProvider(&ssl2);
280 socket_factory_.AddSSLSocketDataProvider(&ssl3);
281 socket_factory_.AddSSLSocketDataProvider(&ssl4);
282 socket_factory_.AddSSLSocketDataProvider(&ssl5);
283 socket_factory_.AddSSLSocketDataProvider(&ssl6);
284 socket_factory_.AddSSLSocketDataProvider(&ssl7);
286 enable_ssl_connect_job_waiting_ = true;
287 CreatePool(true, false, false);
289 scoped_refptr<SSLSocketParams> params1 =
290 SSLParams(ProxyServer::SCHEME_DIRECT, false);
291 scoped_refptr<SSLSocketParams> params2 =
292 SSLParams(ProxyServer::SCHEME_DIRECT, false);
293 scoped_refptr<SSLSocketParams> params3 =
294 SSLParams(ProxyServer::SCHEME_DIRECT, false);
295 scoped_refptr<SSLSocketParams> params4 =
296 SSLParams(ProxyServer::SCHEME_DIRECT, false);
297 ClientSocketHandle handle1;
298 ClientSocketHandle handle2;
299 ClientSocketHandle handle3;
300 ClientSocketHandle handle4;
301 TestCompletionCallback callback1;
302 TestCompletionCallback callback2;
303 TestCompletionCallback callback3;
304 TestCompletionCallback callback4;
306 handle1.Init(
307 "b", params1, MEDIUM, callback1.callback(), pool_.get(), BoundNetLog());
308 handle2.Init(
309 "b", params2, MEDIUM, callback2.callback(), pool_.get(), BoundNetLog());
310 handle3.Init(
311 "b", params3, MEDIUM, callback3.callback(), pool_.get(), BoundNetLog());
312 handle4.Init(
313 "b", params4, MEDIUM, callback4.callback(), pool_.get(), BoundNetLog());
315 base::RunLoop().RunUntilIdle();
317 // Only the last socket should have connected.
318 EXPECT_FALSE(handle1.socket());
319 EXPECT_FALSE(handle2.socket());
320 EXPECT_FALSE(handle3.socket());
321 EXPECT_TRUE(handle4.socket()->IsConnected());
324 // Tests that sockets will still connect in parallel if the
325 // EnableSSLConnectJobWaiting flag is not enabled.
326 TEST_P(SSLClientSocketPoolTest, SocketsConnectWithoutFlag) {
327 StaticSocketDataProvider data1;
328 StaticSocketDataProvider data2;
329 StaticSocketDataProvider data3;
330 socket_factory_.AddSocketDataProvider(&data1);
331 socket_factory_.AddSocketDataProvider(&data2);
332 socket_factory_.AddSocketDataProvider(&data3);
334 SSLSocketDataProvider ssl(ASYNC, OK);
335 ssl.is_in_session_cache = false;
336 ssl.should_pause_on_connect = true;
337 SSLSocketDataProvider ssl2(ASYNC, OK);
338 ssl2.is_in_session_cache = false;
339 ssl2.should_pause_on_connect = true;
340 SSLSocketDataProvider ssl3(ASYNC, OK);
341 ssl3.is_in_session_cache = false;
342 ssl3.should_pause_on_connect = true;
343 socket_factory_.AddSSLSocketDataProvider(&ssl);
344 socket_factory_.AddSSLSocketDataProvider(&ssl2);
345 socket_factory_.AddSSLSocketDataProvider(&ssl3);
347 CreatePool(true, false, false);
349 scoped_refptr<SSLSocketParams> params1 =
350 SSLParams(ProxyServer::SCHEME_DIRECT, false);
351 scoped_refptr<SSLSocketParams> params2 =
352 SSLParams(ProxyServer::SCHEME_DIRECT, false);
353 scoped_refptr<SSLSocketParams> params3 =
354 SSLParams(ProxyServer::SCHEME_DIRECT, false);
355 ClientSocketHandle handle1;
356 ClientSocketHandle handle2;
357 ClientSocketHandle handle3;
358 TestCompletionCallback callback1;
359 TestCompletionCallback callback2;
360 TestCompletionCallback callback3;
362 handle1.Init(
363 "b", params1, MEDIUM, callback1.callback(), pool_.get(), BoundNetLog());
364 handle2.Init(
365 "b", params2, MEDIUM, callback2.callback(), pool_.get(), BoundNetLog());
366 handle3.Init(
367 "b", params3, MEDIUM, callback3.callback(), pool_.get(), BoundNetLog());
369 base::RunLoop().RunUntilIdle();
371 std::vector<MockSSLClientSocket*> sockets =
372 socket_factory_.ssl_client_sockets();
374 // All sockets should have started their connections.
375 for (std::vector<MockSSLClientSocket*>::iterator it = sockets.begin();
376 it != sockets.end();
377 ++it) {
378 EXPECT_TRUE((*it)->reached_connect());
381 // Resume connecting all of the sockets.
382 for (std::vector<MockSSLClientSocket*>::iterator it = sockets.begin();
383 it != sockets.end();
384 ++it) {
385 (*it)->RestartPausedConnect();
388 callback1.WaitForResult();
389 callback2.WaitForResult();
390 callback3.WaitForResult();
392 EXPECT_TRUE(handle1.socket()->IsConnected());
393 EXPECT_TRUE(handle2.socket()->IsConnected());
394 EXPECT_TRUE(handle3.socket()->IsConnected());
397 // Tests that the pool deleting an SSLConnectJob will not cause a crash,
398 // or prevent pending sockets from connecting.
399 TEST_P(SSLClientSocketPoolTest, DeletedSSLConnectJob) {
400 StaticSocketDataProvider data1;
401 StaticSocketDataProvider data2;
402 StaticSocketDataProvider data3;
403 socket_factory_.AddSocketDataProvider(&data1);
404 socket_factory_.AddSocketDataProvider(&data2);
405 socket_factory_.AddSocketDataProvider(&data3);
407 SSLSocketDataProvider ssl(ASYNC, OK);
408 ssl.is_in_session_cache = false;
409 ssl.should_pause_on_connect = true;
410 SSLSocketDataProvider ssl2(ASYNC, OK);
411 ssl2.is_in_session_cache = false;
412 SSLSocketDataProvider ssl3(ASYNC, OK);
413 ssl3.is_in_session_cache = false;
414 socket_factory_.AddSSLSocketDataProvider(&ssl);
415 socket_factory_.AddSSLSocketDataProvider(&ssl2);
416 socket_factory_.AddSSLSocketDataProvider(&ssl3);
418 enable_ssl_connect_job_waiting_ = true;
419 CreatePool(true, false, false);
421 scoped_refptr<SSLSocketParams> params1 =
422 SSLParams(ProxyServer::SCHEME_DIRECT, false);
423 scoped_refptr<SSLSocketParams> params2 =
424 SSLParams(ProxyServer::SCHEME_DIRECT, false);
425 scoped_refptr<SSLSocketParams> params3 =
426 SSLParams(ProxyServer::SCHEME_DIRECT, false);
427 ClientSocketHandle handle1;
428 ClientSocketHandle handle2;
429 ClientSocketHandle handle3;
430 TestCompletionCallback callback1;
431 TestCompletionCallback callback2;
432 TestCompletionCallback callback3;
434 handle1.Init(
435 "b", params1, MEDIUM, callback1.callback(), pool_.get(), BoundNetLog());
436 handle2.Init(
437 "b", params2, MEDIUM, callback2.callback(), pool_.get(), BoundNetLog());
438 handle3.Init(
439 "b", params3, MEDIUM, callback3.callback(), pool_.get(), BoundNetLog());
441 // Allow the connections to proceed until the first socket has started
442 // connecting.
443 base::RunLoop().RunUntilIdle();
445 std::vector<MockSSLClientSocket*> sockets =
446 socket_factory_.ssl_client_sockets();
448 pool_->CancelRequest("b", &handle2);
450 sockets[0]->RestartPausedConnect();
452 callback1.WaitForResult();
453 callback3.WaitForResult();
455 EXPECT_TRUE(handle1.socket()->IsConnected());
456 EXPECT_FALSE(handle2.socket());
457 EXPECT_TRUE(handle3.socket()->IsConnected());
460 // Tests that all pending sockets still connect when the pool deletes a pending
461 // SSLConnectJob which immediately followed a failed leading connection.
462 TEST_P(SSLClientSocketPoolTest, DeletedSocketAfterFail) {
463 StaticSocketDataProvider data1;
464 StaticSocketDataProvider data2;
465 StaticSocketDataProvider data3;
466 StaticSocketDataProvider data4;
467 socket_factory_.AddSocketDataProvider(&data1);
468 socket_factory_.AddSocketDataProvider(&data2);
469 socket_factory_.AddSocketDataProvider(&data3);
470 socket_factory_.AddSocketDataProvider(&data4);
472 SSLSocketDataProvider ssl(ASYNC, ERR_SSL_PROTOCOL_ERROR);
473 ssl.is_in_session_cache = false;
474 ssl.should_pause_on_connect = true;
475 SSLSocketDataProvider ssl2(ASYNC, OK);
476 ssl2.is_in_session_cache = false;
477 SSLSocketDataProvider ssl3(ASYNC, OK);
478 ssl3.is_in_session_cache = false;
479 SSLSocketDataProvider ssl4(ASYNC, OK);
480 ssl4.is_in_session_cache = false;
481 socket_factory_.AddSSLSocketDataProvider(&ssl);
482 socket_factory_.AddSSLSocketDataProvider(&ssl2);
483 socket_factory_.AddSSLSocketDataProvider(&ssl3);
484 socket_factory_.AddSSLSocketDataProvider(&ssl4);
486 enable_ssl_connect_job_waiting_ = true;
487 CreatePool(true, false, false);
489 scoped_refptr<SSLSocketParams> params1 =
490 SSLParams(ProxyServer::SCHEME_DIRECT, false);
491 scoped_refptr<SSLSocketParams> params2 =
492 SSLParams(ProxyServer::SCHEME_DIRECT, false);
493 scoped_refptr<SSLSocketParams> params3 =
494 SSLParams(ProxyServer::SCHEME_DIRECT, false);
495 ClientSocketHandle handle1;
496 ClientSocketHandle handle2;
497 ClientSocketHandle handle3;
498 TestCompletionCallback callback1;
499 TestCompletionCallback callback2;
500 TestCompletionCallback callback3;
502 handle1.Init(
503 "b", params1, MEDIUM, callback1.callback(), pool_.get(), BoundNetLog());
504 handle2.Init(
505 "b", params2, MEDIUM, callback2.callback(), pool_.get(), BoundNetLog());
506 handle3.Init(
507 "b", params3, MEDIUM, callback3.callback(), pool_.get(), BoundNetLog());
509 // Allow the connections to proceed until the first socket has started
510 // connecting.
511 base::RunLoop().RunUntilIdle();
513 std::vector<MockSSLClientSocket*> sockets =
514 socket_factory_.ssl_client_sockets();
516 EXPECT_EQ(3u, sockets.size());
517 EXPECT_TRUE(sockets[0]->reached_connect());
518 EXPECT_FALSE(handle1.socket());
520 pool_->CancelRequest("b", &handle2);
522 sockets[0]->RestartPausedConnect();
524 callback1.WaitForResult();
525 callback3.WaitForResult();
527 EXPECT_FALSE(handle1.socket());
528 EXPECT_FALSE(handle2.socket());
529 EXPECT_TRUE(handle3.socket()->IsConnected());
532 // Make sure that sockets still connect after the leader socket's
533 // connection fails.
534 TEST_P(SSLClientSocketPoolTest, SimultaneousConnectJobsFail) {
535 StaticSocketDataProvider data1;
536 StaticSocketDataProvider data2;
537 StaticSocketDataProvider data3;
538 StaticSocketDataProvider data4;
539 StaticSocketDataProvider data5;
540 socket_factory_.AddSocketDataProvider(&data1);
541 socket_factory_.AddSocketDataProvider(&data2);
542 socket_factory_.AddSocketDataProvider(&data3);
543 socket_factory_.AddSocketDataProvider(&data4);
544 socket_factory_.AddSocketDataProvider(&data5);
545 SSLSocketDataProvider ssl(ASYNC, ERR_SSL_PROTOCOL_ERROR);
546 ssl.is_in_session_cache = false;
547 ssl.should_pause_on_connect = true;
548 SSLSocketDataProvider ssl2(ASYNC, OK);
549 ssl2.is_in_session_cache = false;
550 ssl2.should_pause_on_connect = true;
551 SSLSocketDataProvider ssl3(ASYNC, OK);
552 ssl3.is_in_session_cache = false;
553 SSLSocketDataProvider ssl4(ASYNC, OK);
554 ssl4.is_in_session_cache = false;
555 SSLSocketDataProvider ssl5(ASYNC, OK);
556 ssl5.is_in_session_cache = false;
558 socket_factory_.AddSSLSocketDataProvider(&ssl);
559 socket_factory_.AddSSLSocketDataProvider(&ssl2);
560 socket_factory_.AddSSLSocketDataProvider(&ssl3);
561 socket_factory_.AddSSLSocketDataProvider(&ssl4);
562 socket_factory_.AddSSLSocketDataProvider(&ssl5);
564 enable_ssl_connect_job_waiting_ = true;
565 CreatePool(true, false, false);
566 scoped_refptr<SSLSocketParams> params1 =
567 SSLParams(ProxyServer::SCHEME_DIRECT, false);
568 scoped_refptr<SSLSocketParams> params2 =
569 SSLParams(ProxyServer::SCHEME_DIRECT, false);
570 scoped_refptr<SSLSocketParams> params3 =
571 SSLParams(ProxyServer::SCHEME_DIRECT, false);
572 scoped_refptr<SSLSocketParams> params4 =
573 SSLParams(ProxyServer::SCHEME_DIRECT, false);
574 ClientSocketHandle handle1;
575 ClientSocketHandle handle2;
576 ClientSocketHandle handle3;
577 ClientSocketHandle handle4;
578 TestCompletionCallback callback1;
579 TestCompletionCallback callback2;
580 TestCompletionCallback callback3;
581 TestCompletionCallback callback4;
582 handle1.Init(
583 "b", params1, MEDIUM, callback1.callback(), pool_.get(), BoundNetLog());
584 handle2.Init(
585 "b", params2, MEDIUM, callback2.callback(), pool_.get(), BoundNetLog());
586 handle3.Init(
587 "b", params3, MEDIUM, callback3.callback(), pool_.get(), BoundNetLog());
588 handle4.Init(
589 "b", params4, MEDIUM, callback4.callback(), pool_.get(), BoundNetLog());
591 base::RunLoop().RunUntilIdle();
593 std::vector<MockSSLClientSocket*> sockets =
594 socket_factory_.ssl_client_sockets();
596 std::vector<MockSSLClientSocket*>::const_iterator it = sockets.begin();
598 // The first socket should have had Connect called on it.
599 EXPECT_TRUE((*it)->reached_connect());
600 ++it;
602 // No other socket should have reached connect yet.
603 for (; it != sockets.end(); ++it)
604 EXPECT_FALSE((*it)->reached_connect());
606 // Allow the first socket to resume it's connection process.
607 sockets[0]->RestartPausedConnect();
609 base::RunLoop().RunUntilIdle();
611 // The second socket should have reached connect.
612 EXPECT_TRUE(sockets[1]->reached_connect());
614 // Allow the second socket to continue its connection.
615 sockets[1]->RestartPausedConnect();
617 base::RunLoop().RunUntilIdle();
619 EXPECT_FALSE(handle1.socket());
620 EXPECT_TRUE(handle2.socket()->IsConnected());
621 EXPECT_TRUE(handle3.socket()->IsConnected());
622 EXPECT_TRUE(handle4.socket()->IsConnected());
625 // Make sure that no sockets connect before the "leader" socket,
626 // given that the leader has a successful connection.
627 TEST_P(SSLClientSocketPoolTest, SimultaneousConnectJobsSuccess) {
628 StaticSocketDataProvider data1;
629 StaticSocketDataProvider data2;
630 StaticSocketDataProvider data3;
631 socket_factory_.AddSocketDataProvider(&data1);
632 socket_factory_.AddSocketDataProvider(&data2);
633 socket_factory_.AddSocketDataProvider(&data3);
635 SSLSocketDataProvider ssl(ASYNC, OK);
636 ssl.is_in_session_cache = false;
637 ssl.should_pause_on_connect = true;
638 SSLSocketDataProvider ssl2(ASYNC, OK);
639 ssl2.is_in_session_cache = false;
640 SSLSocketDataProvider ssl3(ASYNC, OK);
641 ssl3.is_in_session_cache = false;
642 socket_factory_.AddSSLSocketDataProvider(&ssl);
643 socket_factory_.AddSSLSocketDataProvider(&ssl2);
644 socket_factory_.AddSSLSocketDataProvider(&ssl3);
646 enable_ssl_connect_job_waiting_ = true;
647 CreatePool(true, false, false);
649 scoped_refptr<SSLSocketParams> params1 =
650 SSLParams(ProxyServer::SCHEME_DIRECT, false);
651 scoped_refptr<SSLSocketParams> params2 =
652 SSLParams(ProxyServer::SCHEME_DIRECT, false);
653 scoped_refptr<SSLSocketParams> params3 =
654 SSLParams(ProxyServer::SCHEME_DIRECT, false);
655 ClientSocketHandle handle1;
656 ClientSocketHandle handle2;
657 ClientSocketHandle handle3;
658 TestCompletionCallback callback1;
659 TestCompletionCallback callback2;
660 TestCompletionCallback callback3;
662 handle1.Init(
663 "b", params1, MEDIUM, callback1.callback(), pool_.get(), BoundNetLog());
664 handle2.Init(
665 "b", params2, MEDIUM, callback2.callback(), pool_.get(), BoundNetLog());
666 handle3.Init(
667 "b", params3, MEDIUM, callback3.callback(), pool_.get(), BoundNetLog());
669 // Allow the connections to proceed until the first socket has finished
670 // connecting.
671 base::RunLoop().RunUntilIdle();
673 std::vector<MockSSLClientSocket*> sockets =
674 socket_factory_.ssl_client_sockets();
676 std::vector<MockSSLClientSocket*>::const_iterator it = sockets.begin();
677 // The first socket should have reached connect.
678 EXPECT_TRUE((*it)->reached_connect());
679 ++it;
680 // No other socket should have reached connect yet.
681 for (; it != sockets.end(); ++it)
682 EXPECT_FALSE((*it)->reached_connect());
684 sockets[0]->RestartPausedConnect();
686 callback1.WaitForResult();
687 callback2.WaitForResult();
688 callback3.WaitForResult();
690 EXPECT_TRUE(handle1.socket()->IsConnected());
691 EXPECT_TRUE(handle2.socket()->IsConnected());
692 EXPECT_TRUE(handle3.socket()->IsConnected());
695 TEST_P(SSLClientSocketPoolTest, TCPFail) {
696 StaticSocketDataProvider data;
697 data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_CONNECTION_FAILED));
698 socket_factory_.AddSocketDataProvider(&data);
700 CreatePool(true /* tcp pool */, false, false);
701 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
702 false);
704 ClientSocketHandle handle;
705 int rv = handle.Init("a", params, MEDIUM, CompletionCallback(), pool_.get(),
706 BoundNetLog());
707 EXPECT_EQ(ERR_CONNECTION_FAILED, rv);
708 EXPECT_FALSE(handle.is_initialized());
709 EXPECT_FALSE(handle.socket());
710 EXPECT_FALSE(handle.is_ssl_error());
713 TEST_P(SSLClientSocketPoolTest, TCPFailAsync) {
714 StaticSocketDataProvider data;
715 data.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_FAILED));
716 socket_factory_.AddSocketDataProvider(&data);
718 CreatePool(true /* tcp pool */, false, false);
719 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
720 false);
722 ClientSocketHandle handle;
723 TestCompletionCallback callback;
724 int rv = handle.Init(
725 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
726 EXPECT_EQ(ERR_IO_PENDING, rv);
727 EXPECT_FALSE(handle.is_initialized());
728 EXPECT_FALSE(handle.socket());
730 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
731 EXPECT_FALSE(handle.is_initialized());
732 EXPECT_FALSE(handle.socket());
733 EXPECT_FALSE(handle.is_ssl_error());
736 TEST_P(SSLClientSocketPoolTest, BasicDirect) {
737 StaticSocketDataProvider data;
738 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
739 socket_factory_.AddSocketDataProvider(&data);
740 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
741 socket_factory_.AddSSLSocketDataProvider(&ssl);
743 CreatePool(true /* tcp pool */, false, false);
744 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
745 false);
747 ClientSocketHandle handle;
748 TestCompletionCallback callback;
749 int rv = handle.Init(
750 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
751 EXPECT_EQ(OK, rv);
752 EXPECT_TRUE(handle.is_initialized());
753 EXPECT_TRUE(handle.socket());
754 TestLoadTimingInfo(handle);
757 // Make sure that SSLConnectJob passes on its priority to its
758 // socket request on Init (for the DIRECT case).
759 TEST_P(SSLClientSocketPoolTest, SetSocketRequestPriorityOnInitDirect) {
760 CreatePool(true /* tcp pool */, false, false);
761 scoped_refptr<SSLSocketParams> params =
762 SSLParams(ProxyServer::SCHEME_DIRECT, false);
764 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) {
765 RequestPriority priority = static_cast<RequestPriority>(i);
766 StaticSocketDataProvider data;
767 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
768 socket_factory_.AddSocketDataProvider(&data);
769 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
770 socket_factory_.AddSSLSocketDataProvider(&ssl);
772 ClientSocketHandle handle;
773 TestCompletionCallback callback;
774 EXPECT_EQ(OK, handle.Init("a", params, priority, callback.callback(),
775 pool_.get(), BoundNetLog()));
776 EXPECT_EQ(priority, transport_socket_pool_.last_request_priority());
777 handle.socket()->Disconnect();
781 TEST_P(SSLClientSocketPoolTest, BasicDirectAsync) {
782 StaticSocketDataProvider data;
783 socket_factory_.AddSocketDataProvider(&data);
784 SSLSocketDataProvider ssl(ASYNC, OK);
785 socket_factory_.AddSSLSocketDataProvider(&ssl);
787 CreatePool(true /* tcp pool */, false, false);
788 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
789 false);
791 ClientSocketHandle handle;
792 TestCompletionCallback callback;
793 int rv = handle.Init(
794 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
795 EXPECT_EQ(ERR_IO_PENDING, rv);
796 EXPECT_FALSE(handle.is_initialized());
797 EXPECT_FALSE(handle.socket());
799 EXPECT_EQ(OK, callback.WaitForResult());
800 EXPECT_TRUE(handle.is_initialized());
801 EXPECT_TRUE(handle.socket());
802 TestLoadTimingInfo(handle);
805 TEST_P(SSLClientSocketPoolTest, DirectCertError) {
806 StaticSocketDataProvider data;
807 socket_factory_.AddSocketDataProvider(&data);
808 SSLSocketDataProvider ssl(ASYNC, ERR_CERT_COMMON_NAME_INVALID);
809 socket_factory_.AddSSLSocketDataProvider(&ssl);
811 CreatePool(true /* tcp pool */, false, false);
812 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
813 false);
815 ClientSocketHandle handle;
816 TestCompletionCallback callback;
817 int rv = handle.Init(
818 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
819 EXPECT_EQ(ERR_IO_PENDING, rv);
820 EXPECT_FALSE(handle.is_initialized());
821 EXPECT_FALSE(handle.socket());
823 EXPECT_EQ(ERR_CERT_COMMON_NAME_INVALID, callback.WaitForResult());
824 EXPECT_TRUE(handle.is_initialized());
825 EXPECT_TRUE(handle.socket());
826 TestLoadTimingInfo(handle);
829 TEST_P(SSLClientSocketPoolTest, DirectSSLError) {
830 StaticSocketDataProvider data;
831 socket_factory_.AddSocketDataProvider(&data);
832 SSLSocketDataProvider ssl(ASYNC, ERR_SSL_PROTOCOL_ERROR);
833 socket_factory_.AddSSLSocketDataProvider(&ssl);
835 CreatePool(true /* tcp pool */, false, false);
836 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
837 false);
839 ClientSocketHandle handle;
840 TestCompletionCallback callback;
841 int rv = handle.Init(
842 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
843 EXPECT_EQ(ERR_IO_PENDING, rv);
844 EXPECT_FALSE(handle.is_initialized());
845 EXPECT_FALSE(handle.socket());
847 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, callback.WaitForResult());
848 EXPECT_FALSE(handle.is_initialized());
849 EXPECT_FALSE(handle.socket());
850 EXPECT_TRUE(handle.is_ssl_error());
853 TEST_P(SSLClientSocketPoolTest, DirectWithNPN) {
854 StaticSocketDataProvider data;
855 socket_factory_.AddSocketDataProvider(&data);
856 SSLSocketDataProvider ssl(ASYNC, OK);
857 ssl.SetNextProto(kProtoHTTP11);
858 socket_factory_.AddSSLSocketDataProvider(&ssl);
860 CreatePool(true /* tcp pool */, false, false);
861 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
862 false);
864 ClientSocketHandle handle;
865 TestCompletionCallback callback;
866 int rv = handle.Init(
867 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
868 EXPECT_EQ(ERR_IO_PENDING, rv);
869 EXPECT_FALSE(handle.is_initialized());
870 EXPECT_FALSE(handle.socket());
872 EXPECT_EQ(OK, callback.WaitForResult());
873 EXPECT_TRUE(handle.is_initialized());
874 EXPECT_TRUE(handle.socket());
875 TestLoadTimingInfo(handle);
876 SSLClientSocket* ssl_socket = static_cast<SSLClientSocket*>(handle.socket());
877 EXPECT_TRUE(ssl_socket->WasNpnNegotiated());
880 TEST_P(SSLClientSocketPoolTest, DirectNoSPDY) {
881 StaticSocketDataProvider data;
882 socket_factory_.AddSocketDataProvider(&data);
883 SSLSocketDataProvider ssl(ASYNC, OK);
884 ssl.SetNextProto(kProtoHTTP11);
885 socket_factory_.AddSSLSocketDataProvider(&ssl);
887 CreatePool(true /* tcp pool */, false, false);
888 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
889 true);
891 ClientSocketHandle handle;
892 TestCompletionCallback callback;
893 int rv = handle.Init(
894 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
895 EXPECT_EQ(ERR_IO_PENDING, rv);
896 EXPECT_FALSE(handle.is_initialized());
897 EXPECT_FALSE(handle.socket());
899 EXPECT_EQ(ERR_NPN_NEGOTIATION_FAILED, callback.WaitForResult());
900 EXPECT_FALSE(handle.is_initialized());
901 EXPECT_FALSE(handle.socket());
902 EXPECT_TRUE(handle.is_ssl_error());
905 TEST_P(SSLClientSocketPoolTest, DirectGotSPDY) {
906 StaticSocketDataProvider data;
907 socket_factory_.AddSocketDataProvider(&data);
908 SSLSocketDataProvider ssl(ASYNC, OK);
909 ssl.SetNextProto(GetParam());
910 socket_factory_.AddSSLSocketDataProvider(&ssl);
912 CreatePool(true /* tcp pool */, false, false);
913 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
914 true);
916 ClientSocketHandle handle;
917 TestCompletionCallback callback;
918 int rv = handle.Init(
919 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
920 EXPECT_EQ(ERR_IO_PENDING, rv);
921 EXPECT_FALSE(handle.is_initialized());
922 EXPECT_FALSE(handle.socket());
924 EXPECT_EQ(OK, callback.WaitForResult());
925 EXPECT_TRUE(handle.is_initialized());
926 EXPECT_TRUE(handle.socket());
927 TestLoadTimingInfo(handle);
929 SSLClientSocket* ssl_socket = static_cast<SSLClientSocket*>(handle.socket());
930 EXPECT_TRUE(ssl_socket->WasNpnNegotiated());
931 std::string proto;
932 ssl_socket->GetNextProto(&proto);
933 EXPECT_EQ(GetParam(), SSLClientSocket::NextProtoFromString(proto));
936 TEST_P(SSLClientSocketPoolTest, DirectGotBonusSPDY) {
937 StaticSocketDataProvider data;
938 socket_factory_.AddSocketDataProvider(&data);
939 SSLSocketDataProvider ssl(ASYNC, OK);
940 ssl.SetNextProto(GetParam());
941 socket_factory_.AddSSLSocketDataProvider(&ssl);
943 CreatePool(true /* tcp pool */, false, false);
944 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
945 true);
947 ClientSocketHandle handle;
948 TestCompletionCallback callback;
949 int rv = handle.Init(
950 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
951 EXPECT_EQ(ERR_IO_PENDING, rv);
952 EXPECT_FALSE(handle.is_initialized());
953 EXPECT_FALSE(handle.socket());
955 EXPECT_EQ(OK, callback.WaitForResult());
956 EXPECT_TRUE(handle.is_initialized());
957 EXPECT_TRUE(handle.socket());
958 TestLoadTimingInfo(handle);
960 SSLClientSocket* ssl_socket = static_cast<SSLClientSocket*>(handle.socket());
961 EXPECT_TRUE(ssl_socket->WasNpnNegotiated());
962 std::string proto;
963 ssl_socket->GetNextProto(&proto);
964 EXPECT_EQ(GetParam(), SSLClientSocket::NextProtoFromString(proto));
967 TEST_P(SSLClientSocketPoolTest, SOCKSFail) {
968 StaticSocketDataProvider data;
969 data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_CONNECTION_FAILED));
970 socket_factory_.AddSocketDataProvider(&data);
972 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
973 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_SOCKS5,
974 false);
976 ClientSocketHandle handle;
977 TestCompletionCallback callback;
978 int rv = handle.Init(
979 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
980 EXPECT_EQ(ERR_CONNECTION_FAILED, rv);
981 EXPECT_FALSE(handle.is_initialized());
982 EXPECT_FALSE(handle.socket());
983 EXPECT_FALSE(handle.is_ssl_error());
986 TEST_P(SSLClientSocketPoolTest, SOCKSFailAsync) {
987 StaticSocketDataProvider data;
988 data.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_FAILED));
989 socket_factory_.AddSocketDataProvider(&data);
991 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
992 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_SOCKS5,
993 false);
995 ClientSocketHandle handle;
996 TestCompletionCallback callback;
997 int rv = handle.Init(
998 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
999 EXPECT_EQ(ERR_IO_PENDING, rv);
1000 EXPECT_FALSE(handle.is_initialized());
1001 EXPECT_FALSE(handle.socket());
1003 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
1004 EXPECT_FALSE(handle.is_initialized());
1005 EXPECT_FALSE(handle.socket());
1006 EXPECT_FALSE(handle.is_ssl_error());
1009 TEST_P(SSLClientSocketPoolTest, SOCKSBasic) {
1010 StaticSocketDataProvider data;
1011 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
1012 socket_factory_.AddSocketDataProvider(&data);
1013 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
1014 socket_factory_.AddSSLSocketDataProvider(&ssl);
1016 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1017 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_SOCKS5,
1018 false);
1020 ClientSocketHandle handle;
1021 TestCompletionCallback callback;
1022 int rv = handle.Init(
1023 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
1024 EXPECT_EQ(OK, rv);
1025 EXPECT_TRUE(handle.is_initialized());
1026 EXPECT_TRUE(handle.socket());
1027 // SOCKS5 generally has no DNS times, but the mock SOCKS5 sockets used here
1028 // don't go through the real logic, unlike in the HTTP proxy tests.
1029 TestLoadTimingInfo(handle);
1032 // Make sure that SSLConnectJob passes on its priority to its
1033 // transport socket on Init (for the SOCKS_PROXY case).
1034 TEST_P(SSLClientSocketPoolTest, SetTransportPriorityOnInitSOCKS) {
1035 StaticSocketDataProvider data;
1036 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
1037 socket_factory_.AddSocketDataProvider(&data);
1038 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
1039 socket_factory_.AddSSLSocketDataProvider(&ssl);
1041 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1042 scoped_refptr<SSLSocketParams> params =
1043 SSLParams(ProxyServer::SCHEME_SOCKS5, false);
1045 ClientSocketHandle handle;
1046 TestCompletionCallback callback;
1047 EXPECT_EQ(OK, handle.Init("a", params, HIGHEST, callback.callback(),
1048 pool_.get(), BoundNetLog()));
1049 EXPECT_EQ(HIGHEST, transport_socket_pool_.last_request_priority());
1052 TEST_P(SSLClientSocketPoolTest, SOCKSBasicAsync) {
1053 StaticSocketDataProvider data;
1054 socket_factory_.AddSocketDataProvider(&data);
1055 SSLSocketDataProvider ssl(ASYNC, OK);
1056 socket_factory_.AddSSLSocketDataProvider(&ssl);
1058 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1059 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_SOCKS5,
1060 false);
1062 ClientSocketHandle handle;
1063 TestCompletionCallback callback;
1064 int rv = handle.Init(
1065 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
1066 EXPECT_EQ(ERR_IO_PENDING, rv);
1067 EXPECT_FALSE(handle.is_initialized());
1068 EXPECT_FALSE(handle.socket());
1070 EXPECT_EQ(OK, callback.WaitForResult());
1071 EXPECT_TRUE(handle.is_initialized());
1072 EXPECT_TRUE(handle.socket());
1073 // SOCKS5 generally has no DNS times, but the mock SOCKS5 sockets used here
1074 // don't go through the real logic, unlike in the HTTP proxy tests.
1075 TestLoadTimingInfo(handle);
1078 TEST_P(SSLClientSocketPoolTest, HttpProxyFail) {
1079 StaticSocketDataProvider data;
1080 data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_CONNECTION_FAILED));
1081 socket_factory_.AddSocketDataProvider(&data);
1083 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1084 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
1085 false);
1087 ClientSocketHandle handle;
1088 TestCompletionCallback callback;
1089 int rv = handle.Init(
1090 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
1091 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, rv);
1092 EXPECT_FALSE(handle.is_initialized());
1093 EXPECT_FALSE(handle.socket());
1094 EXPECT_FALSE(handle.is_ssl_error());
1097 TEST_P(SSLClientSocketPoolTest, HttpProxyFailAsync) {
1098 StaticSocketDataProvider data;
1099 data.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_FAILED));
1100 socket_factory_.AddSocketDataProvider(&data);
1102 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1103 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
1104 false);
1106 ClientSocketHandle handle;
1107 TestCompletionCallback callback;
1108 int rv = handle.Init(
1109 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
1110 EXPECT_EQ(ERR_IO_PENDING, rv);
1111 EXPECT_FALSE(handle.is_initialized());
1112 EXPECT_FALSE(handle.socket());
1114 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, callback.WaitForResult());
1115 EXPECT_FALSE(handle.is_initialized());
1116 EXPECT_FALSE(handle.socket());
1117 EXPECT_FALSE(handle.is_ssl_error());
1120 TEST_P(SSLClientSocketPoolTest, HttpProxyBasic) {
1121 MockWrite writes[] = {
1122 MockWrite(SYNCHRONOUS,
1123 "CONNECT host:80 HTTP/1.1\r\n"
1124 "Host: host\r\n"
1125 "Proxy-Connection: keep-alive\r\n"
1126 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
1128 MockRead reads[] = {
1129 MockRead(SYNCHRONOUS, "HTTP/1.1 200 Connection Established\r\n\r\n"),
1131 StaticSocketDataProvider data(reads, arraysize(reads), writes,
1132 arraysize(writes));
1133 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
1134 socket_factory_.AddSocketDataProvider(&data);
1135 AddAuthToCache();
1136 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
1137 socket_factory_.AddSSLSocketDataProvider(&ssl);
1139 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1140 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
1141 false);
1143 ClientSocketHandle handle;
1144 TestCompletionCallback callback;
1145 int rv = handle.Init(
1146 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
1147 EXPECT_EQ(OK, rv);
1148 EXPECT_TRUE(handle.is_initialized());
1149 EXPECT_TRUE(handle.socket());
1150 TestLoadTimingInfoNoDns(handle);
1153 // Make sure that SSLConnectJob passes on its priority to its
1154 // transport socket on Init (for the HTTP_PROXY case).
1155 TEST_P(SSLClientSocketPoolTest, SetTransportPriorityOnInitHTTP) {
1156 MockWrite writes[] = {
1157 MockWrite(SYNCHRONOUS,
1158 "CONNECT host:80 HTTP/1.1\r\n"
1159 "Host: host\r\n"
1160 "Proxy-Connection: keep-alive\r\n"
1161 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
1163 MockRead reads[] = {
1164 MockRead(SYNCHRONOUS, "HTTP/1.1 200 Connection Established\r\n\r\n"),
1166 StaticSocketDataProvider data(reads, arraysize(reads), writes,
1167 arraysize(writes));
1168 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
1169 socket_factory_.AddSocketDataProvider(&data);
1170 AddAuthToCache();
1171 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
1172 socket_factory_.AddSSLSocketDataProvider(&ssl);
1174 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1175 scoped_refptr<SSLSocketParams> params =
1176 SSLParams(ProxyServer::SCHEME_HTTP, false);
1178 ClientSocketHandle handle;
1179 TestCompletionCallback callback;
1180 EXPECT_EQ(OK, handle.Init("a", params, HIGHEST, callback.callback(),
1181 pool_.get(), BoundNetLog()));
1182 EXPECT_EQ(HIGHEST, transport_socket_pool_.last_request_priority());
1185 TEST_P(SSLClientSocketPoolTest, HttpProxyBasicAsync) {
1186 MockWrite writes[] = {
1187 MockWrite("CONNECT host:80 HTTP/1.1\r\n"
1188 "Host: host\r\n"
1189 "Proxy-Connection: keep-alive\r\n"
1190 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
1192 MockRead reads[] = {
1193 MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"),
1195 StaticSocketDataProvider data(reads, arraysize(reads), writes,
1196 arraysize(writes));
1197 socket_factory_.AddSocketDataProvider(&data);
1198 AddAuthToCache();
1199 SSLSocketDataProvider ssl(ASYNC, OK);
1200 socket_factory_.AddSSLSocketDataProvider(&ssl);
1202 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1203 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
1204 false);
1206 ClientSocketHandle handle;
1207 TestCompletionCallback callback;
1208 int rv = handle.Init(
1209 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
1210 EXPECT_EQ(ERR_IO_PENDING, rv);
1211 EXPECT_FALSE(handle.is_initialized());
1212 EXPECT_FALSE(handle.socket());
1214 EXPECT_EQ(OK, callback.WaitForResult());
1215 EXPECT_TRUE(handle.is_initialized());
1216 EXPECT_TRUE(handle.socket());
1217 TestLoadTimingInfoNoDns(handle);
1220 TEST_P(SSLClientSocketPoolTest, NeedProxyAuth) {
1221 MockWrite writes[] = {
1222 MockWrite("CONNECT host:80 HTTP/1.1\r\n"
1223 "Host: host\r\n"
1224 "Proxy-Connection: keep-alive\r\n\r\n"),
1226 MockRead reads[] = {
1227 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"),
1228 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"),
1229 MockRead("Content-Length: 10\r\n\r\n"),
1230 MockRead("0123456789"),
1232 StaticSocketDataProvider data(reads, arraysize(reads), writes,
1233 arraysize(writes));
1234 socket_factory_.AddSocketDataProvider(&data);
1235 SSLSocketDataProvider ssl(ASYNC, OK);
1236 socket_factory_.AddSSLSocketDataProvider(&ssl);
1238 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
1239 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
1240 false);
1242 ClientSocketHandle handle;
1243 TestCompletionCallback callback;
1244 int rv = handle.Init(
1245 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
1246 EXPECT_EQ(ERR_IO_PENDING, rv);
1247 EXPECT_FALSE(handle.is_initialized());
1248 EXPECT_FALSE(handle.socket());
1250 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED, callback.WaitForResult());
1251 EXPECT_FALSE(handle.is_initialized());
1252 EXPECT_FALSE(handle.socket());
1253 EXPECT_FALSE(handle.is_ssl_error());
1254 const HttpResponseInfo& tunnel_info = handle.ssl_error_response_info();
1255 EXPECT_EQ(tunnel_info.headers->response_code(), 407);
1256 scoped_ptr<ClientSocketHandle> tunnel_handle(
1257 handle.release_pending_http_proxy_connection());
1258 EXPECT_TRUE(tunnel_handle->socket());
1259 EXPECT_FALSE(tunnel_handle->socket()->IsConnected());
1262 TEST_P(SSLClientSocketPoolTest, IPPooling) {
1263 const int kTestPort = 80;
1264 struct TestHosts {
1265 std::string name;
1266 std::string iplist;
1267 SpdySessionKey key;
1268 AddressList addresses;
1269 } test_hosts[] = {
1270 { "www.webkit.org", "192.0.2.33,192.168.0.1,192.168.0.5" },
1271 { "code.google.com", "192.168.0.2,192.168.0.3,192.168.0.5" },
1272 { "js.webkit.org", "192.168.0.4,192.168.0.1,192.0.2.33" },
1275 host_resolver_.set_synchronous_mode(true);
1276 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_hosts); i++) {
1277 host_resolver_.rules()->AddIPLiteralRule(
1278 test_hosts[i].name, test_hosts[i].iplist, std::string());
1280 // This test requires that the HostResolver cache be populated. Normal
1281 // code would have done this already, but we do it manually.
1282 HostResolver::RequestInfo info(HostPortPair(test_hosts[i].name, kTestPort));
1283 host_resolver_.Resolve(info,
1284 DEFAULT_PRIORITY,
1285 &test_hosts[i].addresses,
1286 CompletionCallback(),
1287 NULL,
1288 BoundNetLog());
1290 // Setup a SpdySessionKey
1291 test_hosts[i].key = SpdySessionKey(
1292 HostPortPair(test_hosts[i].name, kTestPort), ProxyServer::Direct(),
1293 PRIVACY_MODE_DISABLED);
1296 MockRead reads[] = {
1297 MockRead(ASYNC, ERR_IO_PENDING),
1299 StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0);
1300 socket_factory_.AddSocketDataProvider(&data);
1301 SSLSocketDataProvider ssl(ASYNC, OK);
1302 ssl.cert = X509Certificate::CreateFromBytes(
1303 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der));
1304 ssl.SetNextProto(GetParam());
1305 socket_factory_.AddSSLSocketDataProvider(&ssl);
1307 CreatePool(true /* tcp pool */, false, false);
1308 base::WeakPtr<SpdySession> spdy_session =
1309 CreateSecureSpdySession(session_, test_hosts[0].key, BoundNetLog());
1311 EXPECT_TRUE(
1312 HasSpdySession(session_->spdy_session_pool(), test_hosts[0].key));
1313 EXPECT_FALSE(
1314 HasSpdySession(session_->spdy_session_pool(), test_hosts[1].key));
1315 EXPECT_TRUE(
1316 HasSpdySession(session_->spdy_session_pool(), test_hosts[2].key));
1318 session_->spdy_session_pool()->CloseAllSessions();
1321 void SSLClientSocketPoolTest::TestIPPoolingDisabled(
1322 SSLSocketDataProvider* ssl) {
1323 const int kTestPort = 80;
1324 struct TestHosts {
1325 std::string name;
1326 std::string iplist;
1327 SpdySessionKey key;
1328 AddressList addresses;
1329 } test_hosts[] = {
1330 { "www.webkit.org", "192.0.2.33,192.168.0.1,192.168.0.5" },
1331 { "js.webkit.com", "192.168.0.4,192.168.0.1,192.0.2.33" },
1334 TestCompletionCallback callback;
1335 int rv;
1336 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_hosts); i++) {
1337 host_resolver_.rules()->AddIPLiteralRule(
1338 test_hosts[i].name, test_hosts[i].iplist, std::string());
1340 // This test requires that the HostResolver cache be populated. Normal
1341 // code would have done this already, but we do it manually.
1342 HostResolver::RequestInfo info(HostPortPair(test_hosts[i].name, kTestPort));
1343 rv = host_resolver_.Resolve(info,
1344 DEFAULT_PRIORITY,
1345 &test_hosts[i].addresses,
1346 callback.callback(),
1347 NULL,
1348 BoundNetLog());
1349 EXPECT_EQ(OK, callback.GetResult(rv));
1351 // Setup a SpdySessionKey
1352 test_hosts[i].key = SpdySessionKey(
1353 HostPortPair(test_hosts[i].name, kTestPort), ProxyServer::Direct(),
1354 PRIVACY_MODE_DISABLED);
1357 MockRead reads[] = {
1358 MockRead(ASYNC, ERR_IO_PENDING),
1360 StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0);
1361 socket_factory_.AddSocketDataProvider(&data);
1362 socket_factory_.AddSSLSocketDataProvider(ssl);
1364 CreatePool(true /* tcp pool */, false, false);
1365 base::WeakPtr<SpdySession> spdy_session =
1366 CreateSecureSpdySession(session_, test_hosts[0].key, BoundNetLog());
1368 EXPECT_TRUE(
1369 HasSpdySession(session_->spdy_session_pool(), test_hosts[0].key));
1370 EXPECT_FALSE(
1371 HasSpdySession(session_->spdy_session_pool(), test_hosts[1].key));
1373 session_->spdy_session_pool()->CloseAllSessions();
1376 // Verifies that an SSL connection with client authentication disables SPDY IP
1377 // pooling.
1378 TEST_P(SSLClientSocketPoolTest, IPPoolingClientCert) {
1379 SSLSocketDataProvider ssl(ASYNC, OK);
1380 ssl.cert = X509Certificate::CreateFromBytes(
1381 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der));
1382 ssl.client_cert_sent = true;
1383 ssl.SetNextProto(GetParam());
1384 TestIPPoolingDisabled(&ssl);
1387 // Verifies that an SSL connection with channel ID disables SPDY IP pooling.
1388 TEST_P(SSLClientSocketPoolTest, IPPoolingChannelID) {
1389 SSLSocketDataProvider ssl(ASYNC, OK);
1390 ssl.channel_id_sent = true;
1391 ssl.SetNextProto(GetParam());
1392 TestIPPoolingDisabled(&ssl);
1395 // It would be nice to also test the timeouts in SSLClientSocketPool.
1397 } // namespace
1399 } // namespace net