Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / net / socket / ssl_client_socket_pool_unittest.cc
blob19b77138c8b8217595fe8584fc4c57bc8487b2c6
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/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "net/base/auth.h"
13 #include "net/base/load_timing_info.h"
14 #include "net/base/load_timing_info_test_util.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/test_completion_callback.h"
17 #include "net/cert/cert_verifier.h"
18 #include "net/dns/mock_host_resolver.h"
19 #include "net/http/http_auth_handler_factory.h"
20 #include "net/http/http_network_session.h"
21 #include "net/http/http_request_headers.h"
22 #include "net/http/http_response_headers.h"
23 #include "net/http/http_server_properties_impl.h"
24 #include "net/http/transport_security_state.h"
25 #include "net/proxy/proxy_service.h"
26 #include "net/socket/client_socket_handle.h"
27 #include "net/socket/next_proto.h"
28 #include "net/socket/socket_test_util.h"
29 #include "net/spdy/spdy_session.h"
30 #include "net/spdy/spdy_session_pool.h"
31 #include "net/spdy/spdy_test_util_common.h"
32 #include "net/ssl/ssl_config_service_defaults.h"
33 #include "net/test/test_certificate_data.h"
34 #include "testing/gtest/include/gtest/gtest.h"
36 namespace net {
38 namespace {
40 const int kMaxSockets = 32;
41 const int kMaxSocketsPerGroup = 6;
43 // Make sure |handle|'s load times are set correctly. DNS and connect start
44 // times comes from mock client sockets in these tests, so primarily serves to
45 // check those times were copied, and ssl times / connect end are set correctly.
46 void TestLoadTimingInfo(const ClientSocketHandle& handle) {
47 LoadTimingInfo load_timing_info;
48 EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info));
50 EXPECT_FALSE(load_timing_info.socket_reused);
51 // None of these tests use a NetLog.
52 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
54 ExpectConnectTimingHasTimes(
55 load_timing_info.connect_timing,
56 CONNECT_TIMING_HAS_SSL_TIMES | CONNECT_TIMING_HAS_DNS_TIMES);
57 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info);
60 // Just like TestLoadTimingInfo, except DNS times are expected to be null, for
61 // tests over proxies that do DNS lookups themselves.
62 void TestLoadTimingInfoNoDns(const ClientSocketHandle& handle) {
63 LoadTimingInfo load_timing_info;
64 EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info));
66 // None of these tests use a NetLog.
67 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
69 EXPECT_FALSE(load_timing_info.socket_reused);
71 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
72 CONNECT_TIMING_HAS_SSL_TIMES);
73 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info);
76 class SSLClientSocketPoolTest
77 : public testing::Test,
78 public ::testing::WithParamInterface<NextProto> {
79 protected:
80 SSLClientSocketPoolTest()
81 : transport_security_state_(new TransportSecurityState),
82 proxy_service_(ProxyService::CreateDirect()),
83 ssl_config_service_(new SSLConfigServiceDefaults),
84 http_auth_handler_factory_(
85 HttpAuthHandlerFactory::CreateDefault(&host_resolver_)),
86 session_(CreateNetworkSession()),
87 direct_transport_socket_params_(new TransportSocketParams(
88 HostPortPair("host", 443),
89 false,
90 false,
91 OnHostResolutionCallback(),
92 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)),
93 transport_socket_pool_(kMaxSockets,
94 kMaxSocketsPerGroup,
95 &socket_factory_),
96 proxy_transport_socket_params_(new TransportSocketParams(
97 HostPortPair("proxy", 443),
98 false,
99 false,
100 OnHostResolutionCallback(),
101 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)),
102 socks_socket_params_(
103 new SOCKSSocketParams(proxy_transport_socket_params_,
104 true,
105 HostPortPair("sockshost", 443))),
106 socks_socket_pool_(kMaxSockets,
107 kMaxSocketsPerGroup,
108 &transport_socket_pool_),
109 http_proxy_socket_params_(
110 new HttpProxySocketParams(proxy_transport_socket_params_,
111 NULL,
112 std::string(),
113 HostPortPair("host", 80),
114 session_->http_auth_cache(),
115 session_->http_auth_handler_factory(),
116 session_->spdy_session_pool(),
117 true,
118 NULL)),
119 http_proxy_socket_pool_(kMaxSockets,
120 kMaxSocketsPerGroup,
121 &transport_socket_pool_,
122 NULL,
123 NULL) {
124 scoped_refptr<SSLConfigService> ssl_config_service(
125 new SSLConfigServiceDefaults);
126 ssl_config_service->GetSSLConfig(&ssl_config_);
129 void CreatePool(bool transport_pool, bool http_proxy_pool, bool socks_pool) {
130 pool_.reset(new SSLClientSocketPool(
131 kMaxSockets, kMaxSocketsPerGroup, NULL /* cert_verifier */,
132 NULL /* channel_id_service */, NULL /* transport_security_state */,
133 NULL /* cert_transparency_verifier */, NULL /* cert_policy_enforcer */,
134 std::string() /* ssl_session_cache_shard */, &socket_factory_,
135 transport_pool ? &transport_socket_pool_ : NULL,
136 socks_pool ? &socks_socket_pool_ : NULL,
137 http_proxy_pool ? &http_proxy_socket_pool_ : NULL, NULL, NULL));
140 scoped_refptr<SSLSocketParams> SSLParams(ProxyServer::Scheme proxy,
141 bool want_spdy_over_npn) {
142 return make_scoped_refptr(new SSLSocketParams(
143 proxy == ProxyServer::SCHEME_DIRECT ? direct_transport_socket_params_
144 : NULL,
145 proxy == ProxyServer::SCHEME_SOCKS5 ? socks_socket_params_ : NULL,
146 proxy == ProxyServer::SCHEME_HTTP ? http_proxy_socket_params_ : NULL,
147 HostPortPair("host", 443),
148 ssl_config_,
149 PRIVACY_MODE_DISABLED,
151 want_spdy_over_npn));
154 void AddAuthToCache() {
155 const base::string16 kFoo(base::ASCIIToUTF16("foo"));
156 const base::string16 kBar(base::ASCIIToUTF16("bar"));
157 session_->http_auth_cache()->Add(GURL("http://proxy:443/"),
158 "MyRealm1",
159 HttpAuth::AUTH_SCHEME_BASIC,
160 "Basic realm=MyRealm1",
161 AuthCredentials(kFoo, kBar),
162 "/");
165 HttpNetworkSession* CreateNetworkSession() {
166 HttpNetworkSession::Params params;
167 params.host_resolver = &host_resolver_;
168 params.cert_verifier = cert_verifier_.get();
169 params.transport_security_state = transport_security_state_.get();
170 params.proxy_service = proxy_service_.get();
171 params.client_socket_factory = &socket_factory_;
172 params.ssl_config_service = ssl_config_service_.get();
173 params.http_auth_handler_factory = http_auth_handler_factory_.get();
174 params.http_server_properties =
175 http_server_properties_.GetWeakPtr();
176 params.enable_spdy_compression = false;
177 params.spdy_default_protocol = GetParam();
178 return new HttpNetworkSession(params);
181 void TestIPPoolingDisabled(SSLSocketDataProvider* ssl);
183 MockClientSocketFactory socket_factory_;
184 MockCachingHostResolver host_resolver_;
185 scoped_ptr<CertVerifier> cert_verifier_;
186 scoped_ptr<TransportSecurityState> transport_security_state_;
187 const scoped_ptr<ProxyService> proxy_service_;
188 const scoped_refptr<SSLConfigService> ssl_config_service_;
189 const scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory_;
190 HttpServerPropertiesImpl http_server_properties_;
191 const scoped_refptr<HttpNetworkSession> session_;
193 scoped_refptr<TransportSocketParams> direct_transport_socket_params_;
194 MockTransportClientSocketPool transport_socket_pool_;
196 scoped_refptr<TransportSocketParams> proxy_transport_socket_params_;
198 scoped_refptr<SOCKSSocketParams> socks_socket_params_;
199 MockSOCKSClientSocketPool socks_socket_pool_;
201 scoped_refptr<HttpProxySocketParams> http_proxy_socket_params_;
202 HttpProxyClientSocketPool http_proxy_socket_pool_;
204 SSLConfig ssl_config_;
205 scoped_ptr<SSLClientSocketPool> pool_;
208 INSTANTIATE_TEST_CASE_P(NextProto,
209 SSLClientSocketPoolTest,
210 testing::Values(kProtoSPDY31,
211 kProtoSPDY4_14,
212 kProtoSPDY4));
214 TEST_P(SSLClientSocketPoolTest, TCPFail) {
215 StaticSocketDataProvider data;
216 data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_CONNECTION_FAILED));
217 socket_factory_.AddSocketDataProvider(&data);
219 CreatePool(true /* tcp pool */, false, false);
220 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
221 false);
223 ClientSocketHandle handle;
224 int rv = handle.Init("a", params, MEDIUM, CompletionCallback(), pool_.get(),
225 BoundNetLog());
226 EXPECT_EQ(ERR_CONNECTION_FAILED, rv);
227 EXPECT_FALSE(handle.is_initialized());
228 EXPECT_FALSE(handle.socket());
229 EXPECT_FALSE(handle.is_ssl_error());
232 TEST_P(SSLClientSocketPoolTest, TCPFailAsync) {
233 StaticSocketDataProvider data;
234 data.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_FAILED));
235 socket_factory_.AddSocketDataProvider(&data);
237 CreatePool(true /* tcp pool */, false, false);
238 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
239 false);
241 ClientSocketHandle handle;
242 TestCompletionCallback callback;
243 int rv = handle.Init(
244 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
245 EXPECT_EQ(ERR_IO_PENDING, rv);
246 EXPECT_FALSE(handle.is_initialized());
247 EXPECT_FALSE(handle.socket());
249 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
250 EXPECT_FALSE(handle.is_initialized());
251 EXPECT_FALSE(handle.socket());
252 EXPECT_FALSE(handle.is_ssl_error());
255 TEST_P(SSLClientSocketPoolTest, BasicDirect) {
256 StaticSocketDataProvider data;
257 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
258 socket_factory_.AddSocketDataProvider(&data);
259 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
260 socket_factory_.AddSSLSocketDataProvider(&ssl);
262 CreatePool(true /* tcp pool */, false, false);
263 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
264 false);
266 ClientSocketHandle handle;
267 TestCompletionCallback callback;
268 int rv = handle.Init(
269 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
270 EXPECT_EQ(OK, rv);
271 EXPECT_TRUE(handle.is_initialized());
272 EXPECT_TRUE(handle.socket());
273 TestLoadTimingInfo(handle);
276 // Make sure that SSLConnectJob passes on its priority to its
277 // socket request on Init (for the DIRECT case).
278 TEST_P(SSLClientSocketPoolTest, SetSocketRequestPriorityOnInitDirect) {
279 CreatePool(true /* tcp pool */, false, false);
280 scoped_refptr<SSLSocketParams> params =
281 SSLParams(ProxyServer::SCHEME_DIRECT, false);
283 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) {
284 RequestPriority priority = static_cast<RequestPriority>(i);
285 StaticSocketDataProvider data;
286 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
287 socket_factory_.AddSocketDataProvider(&data);
288 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
289 socket_factory_.AddSSLSocketDataProvider(&ssl);
291 ClientSocketHandle handle;
292 TestCompletionCallback callback;
293 EXPECT_EQ(OK, handle.Init("a", params, priority, callback.callback(),
294 pool_.get(), BoundNetLog()));
295 EXPECT_EQ(priority, transport_socket_pool_.last_request_priority());
296 handle.socket()->Disconnect();
300 TEST_P(SSLClientSocketPoolTest, BasicDirectAsync) {
301 StaticSocketDataProvider data;
302 socket_factory_.AddSocketDataProvider(&data);
303 SSLSocketDataProvider ssl(ASYNC, OK);
304 socket_factory_.AddSSLSocketDataProvider(&ssl);
306 CreatePool(true /* tcp pool */, false, false);
307 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
308 false);
310 ClientSocketHandle handle;
311 TestCompletionCallback callback;
312 int rv = handle.Init(
313 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
314 EXPECT_EQ(ERR_IO_PENDING, rv);
315 EXPECT_FALSE(handle.is_initialized());
316 EXPECT_FALSE(handle.socket());
318 EXPECT_EQ(OK, callback.WaitForResult());
319 EXPECT_TRUE(handle.is_initialized());
320 EXPECT_TRUE(handle.socket());
321 TestLoadTimingInfo(handle);
324 TEST_P(SSLClientSocketPoolTest, DirectCertError) {
325 StaticSocketDataProvider data;
326 socket_factory_.AddSocketDataProvider(&data);
327 SSLSocketDataProvider ssl(ASYNC, ERR_CERT_COMMON_NAME_INVALID);
328 socket_factory_.AddSSLSocketDataProvider(&ssl);
330 CreatePool(true /* tcp pool */, false, false);
331 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
332 false);
334 ClientSocketHandle handle;
335 TestCompletionCallback callback;
336 int rv = handle.Init(
337 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
338 EXPECT_EQ(ERR_IO_PENDING, rv);
339 EXPECT_FALSE(handle.is_initialized());
340 EXPECT_FALSE(handle.socket());
342 EXPECT_EQ(ERR_CERT_COMMON_NAME_INVALID, callback.WaitForResult());
343 EXPECT_TRUE(handle.is_initialized());
344 EXPECT_TRUE(handle.socket());
345 TestLoadTimingInfo(handle);
348 TEST_P(SSLClientSocketPoolTest, DirectSSLError) {
349 StaticSocketDataProvider data;
350 socket_factory_.AddSocketDataProvider(&data);
351 SSLSocketDataProvider ssl(ASYNC, ERR_SSL_PROTOCOL_ERROR);
352 socket_factory_.AddSSLSocketDataProvider(&ssl);
354 CreatePool(true /* tcp pool */, false, false);
355 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
356 false);
358 ClientSocketHandle handle;
359 TestCompletionCallback callback;
360 int rv = handle.Init(
361 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
362 EXPECT_EQ(ERR_IO_PENDING, rv);
363 EXPECT_FALSE(handle.is_initialized());
364 EXPECT_FALSE(handle.socket());
366 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, callback.WaitForResult());
367 EXPECT_FALSE(handle.is_initialized());
368 EXPECT_FALSE(handle.socket());
369 EXPECT_TRUE(handle.is_ssl_error());
372 TEST_P(SSLClientSocketPoolTest, DirectWithNPN) {
373 StaticSocketDataProvider data;
374 socket_factory_.AddSocketDataProvider(&data);
375 SSLSocketDataProvider ssl(ASYNC, OK);
376 ssl.SetNextProto(kProtoHTTP11);
377 socket_factory_.AddSSLSocketDataProvider(&ssl);
379 CreatePool(true /* tcp pool */, false, false);
380 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
381 false);
383 ClientSocketHandle handle;
384 TestCompletionCallback callback;
385 int rv = handle.Init(
386 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
387 EXPECT_EQ(ERR_IO_PENDING, rv);
388 EXPECT_FALSE(handle.is_initialized());
389 EXPECT_FALSE(handle.socket());
391 EXPECT_EQ(OK, callback.WaitForResult());
392 EXPECT_TRUE(handle.is_initialized());
393 EXPECT_TRUE(handle.socket());
394 TestLoadTimingInfo(handle);
395 SSLClientSocket* ssl_socket = static_cast<SSLClientSocket*>(handle.socket());
396 EXPECT_TRUE(ssl_socket->WasNpnNegotiated());
399 TEST_P(SSLClientSocketPoolTest, DirectNoSPDY) {
400 StaticSocketDataProvider data;
401 socket_factory_.AddSocketDataProvider(&data);
402 SSLSocketDataProvider ssl(ASYNC, OK);
403 ssl.SetNextProto(kProtoHTTP11);
404 socket_factory_.AddSSLSocketDataProvider(&ssl);
406 CreatePool(true /* tcp pool */, false, false);
407 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
408 true);
410 ClientSocketHandle handle;
411 TestCompletionCallback callback;
412 int rv = handle.Init(
413 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
414 EXPECT_EQ(ERR_IO_PENDING, rv);
415 EXPECT_FALSE(handle.is_initialized());
416 EXPECT_FALSE(handle.socket());
418 EXPECT_EQ(ERR_NPN_NEGOTIATION_FAILED, callback.WaitForResult());
419 EXPECT_FALSE(handle.is_initialized());
420 EXPECT_FALSE(handle.socket());
421 EXPECT_TRUE(handle.is_ssl_error());
424 TEST_P(SSLClientSocketPoolTest, DirectGotSPDY) {
425 StaticSocketDataProvider data;
426 socket_factory_.AddSocketDataProvider(&data);
427 SSLSocketDataProvider ssl(ASYNC, OK);
428 ssl.SetNextProto(GetParam());
429 socket_factory_.AddSSLSocketDataProvider(&ssl);
431 CreatePool(true /* tcp pool */, false, false);
432 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
433 true);
435 ClientSocketHandle handle;
436 TestCompletionCallback callback;
437 int rv = handle.Init(
438 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
439 EXPECT_EQ(ERR_IO_PENDING, rv);
440 EXPECT_FALSE(handle.is_initialized());
441 EXPECT_FALSE(handle.socket());
443 EXPECT_EQ(OK, callback.WaitForResult());
444 EXPECT_TRUE(handle.is_initialized());
445 EXPECT_TRUE(handle.socket());
446 TestLoadTimingInfo(handle);
448 SSLClientSocket* ssl_socket = static_cast<SSLClientSocket*>(handle.socket());
449 EXPECT_TRUE(ssl_socket->WasNpnNegotiated());
450 std::string proto;
451 ssl_socket->GetNextProto(&proto);
452 EXPECT_EQ(GetParam(), SSLClientSocket::NextProtoFromString(proto));
455 TEST_P(SSLClientSocketPoolTest, DirectGotBonusSPDY) {
456 StaticSocketDataProvider data;
457 socket_factory_.AddSocketDataProvider(&data);
458 SSLSocketDataProvider ssl(ASYNC, OK);
459 ssl.SetNextProto(GetParam());
460 socket_factory_.AddSSLSocketDataProvider(&ssl);
462 CreatePool(true /* tcp pool */, false, false);
463 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_DIRECT,
464 true);
466 ClientSocketHandle handle;
467 TestCompletionCallback callback;
468 int rv = handle.Init(
469 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
470 EXPECT_EQ(ERR_IO_PENDING, rv);
471 EXPECT_FALSE(handle.is_initialized());
472 EXPECT_FALSE(handle.socket());
474 EXPECT_EQ(OK, callback.WaitForResult());
475 EXPECT_TRUE(handle.is_initialized());
476 EXPECT_TRUE(handle.socket());
477 TestLoadTimingInfo(handle);
479 SSLClientSocket* ssl_socket = static_cast<SSLClientSocket*>(handle.socket());
480 EXPECT_TRUE(ssl_socket->WasNpnNegotiated());
481 std::string proto;
482 ssl_socket->GetNextProto(&proto);
483 EXPECT_EQ(GetParam(), SSLClientSocket::NextProtoFromString(proto));
486 TEST_P(SSLClientSocketPoolTest, SOCKSFail) {
487 StaticSocketDataProvider data;
488 data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_CONNECTION_FAILED));
489 socket_factory_.AddSocketDataProvider(&data);
491 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
492 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_SOCKS5,
493 false);
495 ClientSocketHandle handle;
496 TestCompletionCallback callback;
497 int rv = handle.Init(
498 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
499 EXPECT_EQ(ERR_CONNECTION_FAILED, rv);
500 EXPECT_FALSE(handle.is_initialized());
501 EXPECT_FALSE(handle.socket());
502 EXPECT_FALSE(handle.is_ssl_error());
505 TEST_P(SSLClientSocketPoolTest, SOCKSFailAsync) {
506 StaticSocketDataProvider data;
507 data.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_FAILED));
508 socket_factory_.AddSocketDataProvider(&data);
510 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
511 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_SOCKS5,
512 false);
514 ClientSocketHandle handle;
515 TestCompletionCallback callback;
516 int rv = handle.Init(
517 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
518 EXPECT_EQ(ERR_IO_PENDING, rv);
519 EXPECT_FALSE(handle.is_initialized());
520 EXPECT_FALSE(handle.socket());
522 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
523 EXPECT_FALSE(handle.is_initialized());
524 EXPECT_FALSE(handle.socket());
525 EXPECT_FALSE(handle.is_ssl_error());
528 TEST_P(SSLClientSocketPoolTest, SOCKSBasic) {
529 StaticSocketDataProvider data;
530 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
531 socket_factory_.AddSocketDataProvider(&data);
532 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
533 socket_factory_.AddSSLSocketDataProvider(&ssl);
535 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
536 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_SOCKS5,
537 false);
539 ClientSocketHandle handle;
540 TestCompletionCallback callback;
541 int rv = handle.Init(
542 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
543 EXPECT_EQ(OK, rv);
544 EXPECT_TRUE(handle.is_initialized());
545 EXPECT_TRUE(handle.socket());
546 // SOCKS5 generally has no DNS times, but the mock SOCKS5 sockets used here
547 // don't go through the real logic, unlike in the HTTP proxy tests.
548 TestLoadTimingInfo(handle);
551 // Make sure that SSLConnectJob passes on its priority to its
552 // transport socket on Init (for the SOCKS_PROXY case).
553 TEST_P(SSLClientSocketPoolTest, SetTransportPriorityOnInitSOCKS) {
554 StaticSocketDataProvider data;
555 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
556 socket_factory_.AddSocketDataProvider(&data);
557 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
558 socket_factory_.AddSSLSocketDataProvider(&ssl);
560 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
561 scoped_refptr<SSLSocketParams> params =
562 SSLParams(ProxyServer::SCHEME_SOCKS5, false);
564 ClientSocketHandle handle;
565 TestCompletionCallback callback;
566 EXPECT_EQ(OK, handle.Init("a", params, HIGHEST, callback.callback(),
567 pool_.get(), BoundNetLog()));
568 EXPECT_EQ(HIGHEST, transport_socket_pool_.last_request_priority());
571 TEST_P(SSLClientSocketPoolTest, SOCKSBasicAsync) {
572 StaticSocketDataProvider data;
573 socket_factory_.AddSocketDataProvider(&data);
574 SSLSocketDataProvider ssl(ASYNC, OK);
575 socket_factory_.AddSSLSocketDataProvider(&ssl);
577 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
578 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_SOCKS5,
579 false);
581 ClientSocketHandle handle;
582 TestCompletionCallback callback;
583 int rv = handle.Init(
584 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
585 EXPECT_EQ(ERR_IO_PENDING, rv);
586 EXPECT_FALSE(handle.is_initialized());
587 EXPECT_FALSE(handle.socket());
589 EXPECT_EQ(OK, callback.WaitForResult());
590 EXPECT_TRUE(handle.is_initialized());
591 EXPECT_TRUE(handle.socket());
592 // SOCKS5 generally has no DNS times, but the mock SOCKS5 sockets used here
593 // don't go through the real logic, unlike in the HTTP proxy tests.
594 TestLoadTimingInfo(handle);
597 TEST_P(SSLClientSocketPoolTest, HttpProxyFail) {
598 StaticSocketDataProvider data;
599 data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_CONNECTION_FAILED));
600 socket_factory_.AddSocketDataProvider(&data);
602 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
603 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
604 false);
606 ClientSocketHandle handle;
607 TestCompletionCallback callback;
608 int rv = handle.Init(
609 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
610 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, rv);
611 EXPECT_FALSE(handle.is_initialized());
612 EXPECT_FALSE(handle.socket());
613 EXPECT_FALSE(handle.is_ssl_error());
616 TEST_P(SSLClientSocketPoolTest, HttpProxyFailAsync) {
617 StaticSocketDataProvider data;
618 data.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_FAILED));
619 socket_factory_.AddSocketDataProvider(&data);
621 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
622 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
623 false);
625 ClientSocketHandle handle;
626 TestCompletionCallback callback;
627 int rv = handle.Init(
628 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
629 EXPECT_EQ(ERR_IO_PENDING, rv);
630 EXPECT_FALSE(handle.is_initialized());
631 EXPECT_FALSE(handle.socket());
633 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, callback.WaitForResult());
634 EXPECT_FALSE(handle.is_initialized());
635 EXPECT_FALSE(handle.socket());
636 EXPECT_FALSE(handle.is_ssl_error());
639 TEST_P(SSLClientSocketPoolTest, HttpProxyBasic) {
640 MockWrite writes[] = {
641 MockWrite(SYNCHRONOUS,
642 "CONNECT host:80 HTTP/1.1\r\n"
643 "Host: host:80\r\n"
644 "Proxy-Connection: keep-alive\r\n"
645 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
647 MockRead reads[] = {
648 MockRead(SYNCHRONOUS, "HTTP/1.1 200 Connection Established\r\n\r\n"),
650 StaticSocketDataProvider data(reads, arraysize(reads), writes,
651 arraysize(writes));
652 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
653 socket_factory_.AddSocketDataProvider(&data);
654 AddAuthToCache();
655 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
656 socket_factory_.AddSSLSocketDataProvider(&ssl);
658 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
659 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
660 false);
662 ClientSocketHandle handle;
663 TestCompletionCallback callback;
664 int rv = handle.Init(
665 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
666 EXPECT_EQ(OK, rv);
667 EXPECT_TRUE(handle.is_initialized());
668 EXPECT_TRUE(handle.socket());
669 TestLoadTimingInfoNoDns(handle);
672 // Make sure that SSLConnectJob passes on its priority to its
673 // transport socket on Init (for the HTTP_PROXY case).
674 TEST_P(SSLClientSocketPoolTest, SetTransportPriorityOnInitHTTP) {
675 MockWrite writes[] = {
676 MockWrite(SYNCHRONOUS,
677 "CONNECT host:80 HTTP/1.1\r\n"
678 "Host: host:80\r\n"
679 "Proxy-Connection: keep-alive\r\n"
680 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
682 MockRead reads[] = {
683 MockRead(SYNCHRONOUS, "HTTP/1.1 200 Connection Established\r\n\r\n"),
685 StaticSocketDataProvider data(reads, arraysize(reads), writes,
686 arraysize(writes));
687 data.set_connect_data(MockConnect(SYNCHRONOUS, OK));
688 socket_factory_.AddSocketDataProvider(&data);
689 AddAuthToCache();
690 SSLSocketDataProvider ssl(SYNCHRONOUS, OK);
691 socket_factory_.AddSSLSocketDataProvider(&ssl);
693 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
694 scoped_refptr<SSLSocketParams> params =
695 SSLParams(ProxyServer::SCHEME_HTTP, false);
697 ClientSocketHandle handle;
698 TestCompletionCallback callback;
699 EXPECT_EQ(OK, handle.Init("a", params, HIGHEST, callback.callback(),
700 pool_.get(), BoundNetLog()));
701 EXPECT_EQ(HIGHEST, transport_socket_pool_.last_request_priority());
704 TEST_P(SSLClientSocketPoolTest, HttpProxyBasicAsync) {
705 MockWrite writes[] = {
706 MockWrite(
707 "CONNECT host:80 HTTP/1.1\r\n"
708 "Host: host:80\r\n"
709 "Proxy-Connection: keep-alive\r\n"
710 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
712 MockRead reads[] = {
713 MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"),
715 StaticSocketDataProvider data(reads, arraysize(reads), writes,
716 arraysize(writes));
717 socket_factory_.AddSocketDataProvider(&data);
718 AddAuthToCache();
719 SSLSocketDataProvider ssl(ASYNC, OK);
720 socket_factory_.AddSSLSocketDataProvider(&ssl);
722 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
723 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
724 false);
726 ClientSocketHandle handle;
727 TestCompletionCallback callback;
728 int rv = handle.Init(
729 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
730 EXPECT_EQ(ERR_IO_PENDING, rv);
731 EXPECT_FALSE(handle.is_initialized());
732 EXPECT_FALSE(handle.socket());
734 EXPECT_EQ(OK, callback.WaitForResult());
735 EXPECT_TRUE(handle.is_initialized());
736 EXPECT_TRUE(handle.socket());
737 TestLoadTimingInfoNoDns(handle);
740 TEST_P(SSLClientSocketPoolTest, NeedProxyAuth) {
741 MockWrite writes[] = {
742 MockWrite(
743 "CONNECT host:80 HTTP/1.1\r\n"
744 "Host: host:80\r\n"
745 "Proxy-Connection: keep-alive\r\n\r\n"),
747 MockRead reads[] = {
748 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"),
749 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"),
750 MockRead("Content-Length: 10\r\n\r\n"),
751 MockRead("0123456789"),
753 StaticSocketDataProvider data(reads, arraysize(reads), writes,
754 arraysize(writes));
755 socket_factory_.AddSocketDataProvider(&data);
756 SSLSocketDataProvider ssl(ASYNC, OK);
757 socket_factory_.AddSSLSocketDataProvider(&ssl);
759 CreatePool(false, true /* http proxy pool */, true /* socks pool */);
760 scoped_refptr<SSLSocketParams> params = SSLParams(ProxyServer::SCHEME_HTTP,
761 false);
763 ClientSocketHandle handle;
764 TestCompletionCallback callback;
765 int rv = handle.Init(
766 "a", params, MEDIUM, callback.callback(), pool_.get(), BoundNetLog());
767 EXPECT_EQ(ERR_IO_PENDING, rv);
768 EXPECT_FALSE(handle.is_initialized());
769 EXPECT_FALSE(handle.socket());
771 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED, callback.WaitForResult());
772 EXPECT_FALSE(handle.is_initialized());
773 EXPECT_FALSE(handle.socket());
774 EXPECT_FALSE(handle.is_ssl_error());
775 const HttpResponseInfo& tunnel_info = handle.ssl_error_response_info();
776 EXPECT_EQ(tunnel_info.headers->response_code(), 407);
777 scoped_ptr<ClientSocketHandle> tunnel_handle(
778 handle.release_pending_http_proxy_connection());
779 EXPECT_TRUE(tunnel_handle->socket());
780 EXPECT_FALSE(tunnel_handle->socket()->IsConnected());
783 TEST_P(SSLClientSocketPoolTest, IPPooling) {
784 const int kTestPort = 80;
785 struct TestHosts {
786 std::string name;
787 std::string iplist;
788 SpdySessionKey key;
789 AddressList addresses;
790 } test_hosts[] = {
791 { "www.webkit.org", "192.0.2.33,192.168.0.1,192.168.0.5" },
792 { "code.google.com", "192.168.0.2,192.168.0.3,192.168.0.5" },
793 { "js.webkit.org", "192.168.0.4,192.168.0.1,192.0.2.33" },
796 host_resolver_.set_synchronous_mode(true);
797 for (size_t i = 0; i < arraysize(test_hosts); i++) {
798 host_resolver_.rules()->AddIPLiteralRule(
799 test_hosts[i].name, test_hosts[i].iplist, std::string());
801 // This test requires that the HostResolver cache be populated. Normal
802 // code would have done this already, but we do it manually.
803 HostResolver::RequestInfo info(HostPortPair(test_hosts[i].name, kTestPort));
804 host_resolver_.Resolve(info,
805 DEFAULT_PRIORITY,
806 &test_hosts[i].addresses,
807 CompletionCallback(),
808 NULL,
809 BoundNetLog());
811 // Setup a SpdySessionKey
812 test_hosts[i].key = SpdySessionKey(
813 HostPortPair(test_hosts[i].name, kTestPort), ProxyServer::Direct(),
814 PRIVACY_MODE_DISABLED);
817 MockRead reads[] = {
818 MockRead(ASYNC, ERR_IO_PENDING),
820 StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0);
821 socket_factory_.AddSocketDataProvider(&data);
822 SSLSocketDataProvider ssl(ASYNC, OK);
823 ssl.cert = X509Certificate::CreateFromBytes(
824 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der));
825 ssl.SetNextProto(GetParam());
826 socket_factory_.AddSSLSocketDataProvider(&ssl);
828 CreatePool(true /* tcp pool */, false, false);
829 base::WeakPtr<SpdySession> spdy_session =
830 CreateSecureSpdySession(session_, test_hosts[0].key, BoundNetLog());
832 EXPECT_TRUE(
833 HasSpdySession(session_->spdy_session_pool(), test_hosts[0].key));
834 EXPECT_FALSE(
835 HasSpdySession(session_->spdy_session_pool(), test_hosts[1].key));
836 EXPECT_TRUE(
837 HasSpdySession(session_->spdy_session_pool(), test_hosts[2].key));
839 session_->spdy_session_pool()->CloseAllSessions();
842 void SSLClientSocketPoolTest::TestIPPoolingDisabled(
843 SSLSocketDataProvider* ssl) {
844 const int kTestPort = 80;
845 struct TestHosts {
846 std::string name;
847 std::string iplist;
848 SpdySessionKey key;
849 AddressList addresses;
850 } test_hosts[] = {
851 { "www.webkit.org", "192.0.2.33,192.168.0.1,192.168.0.5" },
852 { "js.webkit.com", "192.168.0.4,192.168.0.1,192.0.2.33" },
855 TestCompletionCallback callback;
856 int rv;
857 for (size_t i = 0; i < arraysize(test_hosts); i++) {
858 host_resolver_.rules()->AddIPLiteralRule(
859 test_hosts[i].name, test_hosts[i].iplist, std::string());
861 // This test requires that the HostResolver cache be populated. Normal
862 // code would have done this already, but we do it manually.
863 HostResolver::RequestInfo info(HostPortPair(test_hosts[i].name, kTestPort));
864 rv = host_resolver_.Resolve(info,
865 DEFAULT_PRIORITY,
866 &test_hosts[i].addresses,
867 callback.callback(),
868 NULL,
869 BoundNetLog());
870 EXPECT_EQ(OK, callback.GetResult(rv));
872 // Setup a SpdySessionKey
873 test_hosts[i].key = SpdySessionKey(
874 HostPortPair(test_hosts[i].name, kTestPort), ProxyServer::Direct(),
875 PRIVACY_MODE_DISABLED);
878 MockRead reads[] = {
879 MockRead(ASYNC, ERR_IO_PENDING),
881 StaticSocketDataProvider data(reads, arraysize(reads), NULL, 0);
882 socket_factory_.AddSocketDataProvider(&data);
883 socket_factory_.AddSSLSocketDataProvider(ssl);
885 CreatePool(true /* tcp pool */, false, false);
886 base::WeakPtr<SpdySession> spdy_session =
887 CreateSecureSpdySession(session_, test_hosts[0].key, BoundNetLog());
889 EXPECT_TRUE(
890 HasSpdySession(session_->spdy_session_pool(), test_hosts[0].key));
891 EXPECT_FALSE(
892 HasSpdySession(session_->spdy_session_pool(), test_hosts[1].key));
894 session_->spdy_session_pool()->CloseAllSessions();
897 // Verifies that an SSL connection with client authentication disables SPDY IP
898 // pooling.
899 TEST_P(SSLClientSocketPoolTest, IPPoolingClientCert) {
900 SSLSocketDataProvider ssl(ASYNC, OK);
901 ssl.cert = X509Certificate::CreateFromBytes(
902 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der));
903 ssl.client_cert_sent = true;
904 ssl.SetNextProto(GetParam());
905 TestIPPoolingDisabled(&ssl);
908 // Verifies that an SSL connection with channel ID disables SPDY IP pooling.
909 TEST_P(SSLClientSocketPoolTest, IPPoolingChannelID) {
910 SSLSocketDataProvider ssl(ASYNC, OK);
911 ssl.channel_id_sent = true;
912 ssl.SetNextProto(GetParam());
913 TestIPPoolingDisabled(&ssl);
916 // It would be nice to also test the timeouts in SSLClientSocketPool.
918 } // namespace
920 } // namespace net