| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_proxy_client_socket_pool.h" | 5 #include "net/http/http_proxy_client_socket_pool.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/test/histogram_tester.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 #include "net/base/proxy_delegate.h" | 15 #include "net/base/proxy_delegate.h" |
| 15 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
| 16 #include "net/base/test_proxy_delegate.h" | 17 #include "net/base/test_proxy_delegate.h" |
| 17 #include "net/http/http_network_session.h" | 18 #include "net/http/http_network_session.h" |
| 18 #include "net/http/http_proxy_client_socket.h" | 19 #include "net/http/http_proxy_client_socket.h" |
| 19 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
| 20 #include "net/log/net_log_with_source.h" | 21 #include "net/log/net_log_with_source.h" |
| 21 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
| 22 #include "net/socket/next_proto.h" | 23 #include "net/socket/next_proto.h" |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 void InitializeSpdySsl() { ssl_data_->next_proto = kProtoHTTP2; } | 185 void InitializeSpdySsl() { ssl_data_->next_proto = kProtoHTTP2; } |
| 185 | 186 |
| 186 std::unique_ptr<HttpNetworkSession> CreateNetworkSession() { | 187 std::unique_ptr<HttpNetworkSession> CreateNetworkSession() { |
| 187 return SpdySessionDependencies::SpdyCreateSession(&session_deps_); | 188 return SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
| 188 } | 189 } |
| 189 | 190 |
| 190 RequestPriority GetLastTransportRequestPriority() const { | 191 RequestPriority GetLastTransportRequestPriority() const { |
| 191 return transport_socket_pool_.last_request_priority(); | 192 return transport_socket_pool_.last_request_priority(); |
| 192 } | 193 } |
| 193 | 194 |
| 195 const base::HistogramTester& histogram_tester() { return histogram_tester_; } |
| 196 |
| 194 private: | 197 private: |
| 195 SpdySessionDependencies session_deps_; | 198 SpdySessionDependencies session_deps_; |
| 196 | 199 |
| 197 MockTransportClientSocketPool transport_socket_pool_; | 200 MockTransportClientSocketPool transport_socket_pool_; |
| 198 MockHostResolver host_resolver_; | 201 MockHostResolver host_resolver_; |
| 199 std::unique_ptr<CertVerifier> cert_verifier_; | 202 std::unique_ptr<CertVerifier> cert_verifier_; |
| 200 SSLClientSocketPool ssl_socket_pool_; | 203 SSLClientSocketPool ssl_socket_pool_; |
| 201 | 204 |
| 202 std::unique_ptr<HttpNetworkSession> session_; | 205 std::unique_ptr<HttpNetworkSession> session_; |
| 203 | 206 |
| 207 base::HistogramTester histogram_tester_; |
| 208 |
| 204 protected: | 209 protected: |
| 205 SpdyTestUtil spdy_util_; | 210 SpdyTestUtil spdy_util_; |
| 206 std::unique_ptr<SSLSocketDataProvider> ssl_data_; | 211 std::unique_ptr<SSLSocketDataProvider> ssl_data_; |
| 207 std::unique_ptr<SequencedSocketData> data_; | 212 std::unique_ptr<SequencedSocketData> data_; |
| 208 HttpProxyClientSocketPool pool_; | 213 HttpProxyClientSocketPool pool_; |
| 209 ClientSocketHandle handle_; | 214 ClientSocketHandle handle_; |
| 210 TestCompletionCallback callback_; | 215 TestCompletionCallback callback_; |
| 211 }; | 216 }; |
| 212 | 217 |
| 213 // All tests are run with three different proxy types: HTTP, HTTPS (non-SPDY) | 218 // All tests are run with three different proxy types: HTTP, HTTPS (non-SPDY) |
| 214 // and SPDY. | 219 // and SPDY. |
| 215 INSTANTIATE_TEST_CASE_P(HttpProxyType, | 220 INSTANTIATE_TEST_CASE_P(HttpProxyType, |
| 216 HttpProxyClientSocketPoolTest, | 221 HttpProxyClientSocketPoolTest, |
| 217 ::testing::Values(HTTP, HTTPS, SPDY)); | 222 ::testing::Values(HTTP, HTTPS, SPDY)); |
| 218 | 223 |
| 219 TEST_P(HttpProxyClientSocketPoolTest, NoTunnel) { | 224 TEST_P(HttpProxyClientSocketPoolTest, NoTunnel) { |
| 220 Initialize(NULL, 0, NULL, 0, NULL, 0, NULL, 0); | 225 Initialize(NULL, 0, NULL, 0, NULL, 0, NULL, 0); |
| 221 | 226 |
| 222 std::unique_ptr<TestProxyDelegate> proxy_delegate(new TestProxyDelegate()); | 227 std::unique_ptr<TestProxyDelegate> proxy_delegate(new TestProxyDelegate()); |
| 223 int rv = handle_.Init("a", CreateNoTunnelParams(proxy_delegate.get()), LOW, | 228 int rv = handle_.Init("a", CreateNoTunnelParams(proxy_delegate.get()), LOW, |
| 224 ClientSocketPool::RespectLimits::ENABLED, | 229 ClientSocketPool::RespectLimits::ENABLED, |
| 225 CompletionCallback(), &pool_, NetLogWithSource()); | 230 CompletionCallback(), &pool_, NetLogWithSource()); |
| 226 EXPECT_THAT(rv, IsOk()); | 231 EXPECT_THAT(rv, IsOk()); |
| 227 EXPECT_TRUE(handle_.is_initialized()); | 232 EXPECT_TRUE(handle_.is_initialized()); |
| 228 ASSERT_TRUE(handle_.socket()); | 233 ASSERT_TRUE(handle_.socket()); |
| 229 EXPECT_TRUE(handle_.socket()->IsConnected()); | 234 EXPECT_TRUE(handle_.socket()->IsConnected()); |
| 230 EXPECT_FALSE(proxy_delegate->on_before_tunnel_request_called()); | 235 EXPECT_FALSE(proxy_delegate->on_before_tunnel_request_called()); |
| 231 EXPECT_FALSE(proxy_delegate->on_tunnel_headers_received_called()); | 236 EXPECT_FALSE(proxy_delegate->on_tunnel_headers_received_called()); |
| 232 EXPECT_TRUE(proxy_delegate->on_tunnel_request_completed_called()); | 237 EXPECT_TRUE(proxy_delegate->on_tunnel_request_completed_called()); |
| 238 |
| 239 bool is_secure_proxy = GetParam() == HTTPS || GetParam() == SPDY; |
| 240 histogram_tester().ExpectTotalCount( |
| 241 "Net.HttpProxy.ConnectLatency.Insecure.Success", is_secure_proxy ? 0 : 1); |
| 242 histogram_tester().ExpectTotalCount( |
| 243 "Net.HttpProxy.ConnectLatency.Secure.Success", is_secure_proxy ? 1 : 0); |
| 233 } | 244 } |
| 234 | 245 |
| 235 // Make sure that HttpProxyConnectJob passes on its priority to its | 246 // Make sure that HttpProxyConnectJob passes on its priority to its |
| 236 // (non-SSL) socket request on Init. | 247 // (non-SSL) socket request on Init. |
| 237 TEST_P(HttpProxyClientSocketPoolTest, SetSocketRequestPriorityOnInit) { | 248 TEST_P(HttpProxyClientSocketPoolTest, SetSocketRequestPriorityOnInit) { |
| 238 Initialize(NULL, 0, NULL, 0, NULL, 0, NULL, 0); | 249 Initialize(NULL, 0, NULL, 0, NULL, 0, NULL, 0); |
| 239 EXPECT_EQ(OK, handle_.Init("a", CreateNoTunnelParams(NULL), HIGHEST, | 250 EXPECT_EQ(OK, handle_.Init("a", CreateNoTunnelParams(NULL), HIGHEST, |
| 240 ClientSocketPool::RespectLimits::ENABLED, | 251 ClientSocketPool::RespectLimits::ENABLED, |
| 241 CompletionCallback(), &pool_, NetLogWithSource())); | 252 CompletionCallback(), &pool_, NetLogWithSource())); |
| 242 EXPECT_EQ(HIGHEST, GetLastTransportRequestPriority()); | 253 EXPECT_EQ(HIGHEST, GetLastTransportRequestPriority()); |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 ClientSocketPool::RespectLimits::ENABLED, | 444 ClientSocketPool::RespectLimits::ENABLED, |
| 434 callback_.callback(), &pool_, NetLogWithSource()); | 445 callback_.callback(), &pool_, NetLogWithSource()); |
| 435 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 446 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 436 EXPECT_FALSE(handle_.is_initialized()); | 447 EXPECT_FALSE(handle_.is_initialized()); |
| 437 EXPECT_FALSE(handle_.socket()); | 448 EXPECT_FALSE(handle_.socket()); |
| 438 | 449 |
| 439 EXPECT_THAT(callback_.WaitForResult(), IsError(ERR_PROXY_CONNECTION_FAILED)); | 450 EXPECT_THAT(callback_.WaitForResult(), IsError(ERR_PROXY_CONNECTION_FAILED)); |
| 440 | 451 |
| 441 EXPECT_FALSE(handle_.is_initialized()); | 452 EXPECT_FALSE(handle_.is_initialized()); |
| 442 EXPECT_FALSE(handle_.socket()); | 453 EXPECT_FALSE(handle_.socket()); |
| 454 |
| 455 bool is_secure_proxy = GetParam() == HTTPS; |
| 456 histogram_tester().ExpectTotalCount( |
| 457 "Net.HttpProxy.ConnectLatency.Insecure.Error", is_secure_proxy ? 0 : 1); |
| 458 histogram_tester().ExpectTotalCount( |
| 459 "Net.HttpProxy.ConnectLatency.Secure.Error", is_secure_proxy ? 1 : 0); |
| 443 } | 460 } |
| 444 | 461 |
| 445 TEST_P(HttpProxyClientSocketPoolTest, SSLError) { | 462 TEST_P(HttpProxyClientSocketPoolTest, SSLError) { |
| 446 if (GetParam() == HTTP) | 463 if (GetParam() == HTTP) |
| 447 return; | 464 return; |
| 448 data_.reset(new SequencedSocketData(NULL, 0, NULL, 0)); | 465 data_.reset(new SequencedSocketData(NULL, 0, NULL, 0)); |
| 449 data_->set_connect_data(MockConnect(ASYNC, OK)); | 466 data_->set_connect_data(MockConnect(ASYNC, OK)); |
| 450 socket_factory()->AddSocketDataProvider(data_.get()); | 467 socket_factory()->AddSocketDataProvider(data_.get()); |
| 451 | 468 |
| 452 ssl_data_.reset(new SSLSocketDataProvider(ASYNC, | 469 ssl_data_.reset(new SSLSocketDataProvider(ASYNC, |
| 453 ERR_CERT_AUTHORITY_INVALID)); | 470 ERR_CERT_AUTHORITY_INVALID)); |
| 454 if (GetParam() == SPDY) { | 471 if (GetParam() == SPDY) { |
| 455 InitializeSpdySsl(); | 472 InitializeSpdySsl(); |
| 456 } | 473 } |
| 457 socket_factory()->AddSSLSocketDataProvider(ssl_data_.get()); | 474 socket_factory()->AddSSLSocketDataProvider(ssl_data_.get()); |
| 458 | 475 |
| 459 int rv = handle_.Init("a", CreateTunnelParams(NULL), LOW, | 476 int rv = handle_.Init("a", CreateTunnelParams(NULL), LOW, |
| 460 ClientSocketPool::RespectLimits::ENABLED, | 477 ClientSocketPool::RespectLimits::ENABLED, |
| 461 callback_.callback(), &pool_, NetLogWithSource()); | 478 callback_.callback(), &pool_, NetLogWithSource()); |
| 462 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 479 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 463 EXPECT_FALSE(handle_.is_initialized()); | 480 EXPECT_FALSE(handle_.is_initialized()); |
| 464 EXPECT_FALSE(handle_.socket()); | 481 EXPECT_FALSE(handle_.socket()); |
| 465 | 482 |
| 466 EXPECT_THAT(callback_.WaitForResult(), | 483 EXPECT_THAT(callback_.WaitForResult(), |
| 467 IsError(ERR_PROXY_CERTIFICATE_INVALID)); | 484 IsError(ERR_PROXY_CERTIFICATE_INVALID)); |
| 468 | 485 |
| 469 EXPECT_FALSE(handle_.is_initialized()); | 486 EXPECT_FALSE(handle_.is_initialized()); |
| 470 EXPECT_FALSE(handle_.socket()); | 487 EXPECT_FALSE(handle_.socket()); |
| 488 histogram_tester().ExpectTotalCount( |
| 489 "Net.HttpProxy.ConnectLatency.Secure.Error", 1); |
| 490 histogram_tester().ExpectTotalCount( |
| 491 "Net.HttpProxy.ConnectLatency.Insecure.Error", 0); |
| 471 } | 492 } |
| 472 | 493 |
| 473 TEST_P(HttpProxyClientSocketPoolTest, SslClientAuth) { | 494 TEST_P(HttpProxyClientSocketPoolTest, SslClientAuth) { |
| 474 if (GetParam() == HTTP) | 495 if (GetParam() == HTTP) |
| 475 return; | 496 return; |
| 476 data_.reset(new SequencedSocketData(NULL, 0, NULL, 0)); | 497 data_.reset(new SequencedSocketData(NULL, 0, NULL, 0)); |
| 477 data_->set_connect_data(MockConnect(ASYNC, OK)); | 498 data_->set_connect_data(MockConnect(ASYNC, OK)); |
| 478 socket_factory()->AddSocketDataProvider(data_.get()); | 499 socket_factory()->AddSocketDataProvider(data_.get()); |
| 479 | 500 |
| 480 ssl_data_.reset(new SSLSocketDataProvider(ASYNC, | 501 ssl_data_.reset(new SSLSocketDataProvider(ASYNC, |
| 481 ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); | 502 ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 482 if (GetParam() == SPDY) { | 503 if (GetParam() == SPDY) { |
| 483 InitializeSpdySsl(); | 504 InitializeSpdySsl(); |
| 484 } | 505 } |
| 485 socket_factory()->AddSSLSocketDataProvider(ssl_data_.get()); | 506 socket_factory()->AddSSLSocketDataProvider(ssl_data_.get()); |
| 486 | 507 |
| 487 int rv = handle_.Init("a", CreateTunnelParams(NULL), LOW, | 508 int rv = handle_.Init("a", CreateTunnelParams(NULL), LOW, |
| 488 ClientSocketPool::RespectLimits::ENABLED, | 509 ClientSocketPool::RespectLimits::ENABLED, |
| 489 callback_.callback(), &pool_, NetLogWithSource()); | 510 callback_.callback(), &pool_, NetLogWithSource()); |
| 490 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 511 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 491 EXPECT_FALSE(handle_.is_initialized()); | 512 EXPECT_FALSE(handle_.is_initialized()); |
| 492 EXPECT_FALSE(handle_.socket()); | 513 EXPECT_FALSE(handle_.socket()); |
| 493 | 514 |
| 494 EXPECT_THAT(callback_.WaitForResult(), | 515 EXPECT_THAT(callback_.WaitForResult(), |
| 495 IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); | 516 IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 496 | 517 |
| 497 EXPECT_FALSE(handle_.is_initialized()); | 518 EXPECT_FALSE(handle_.is_initialized()); |
| 498 EXPECT_FALSE(handle_.socket()); | 519 EXPECT_FALSE(handle_.socket()); |
| 520 histogram_tester().ExpectTotalCount( |
| 521 "Net.HttpProxy.ConnectLatency.Secure.Error", 1); |
| 522 histogram_tester().ExpectTotalCount( |
| 523 "Net.HttpProxy.ConnectLatency.Insecure.Error", 0); |
| 499 } | 524 } |
| 500 | 525 |
| 501 TEST_P(HttpProxyClientSocketPoolTest, TunnelUnexpectedClose) { | 526 TEST_P(HttpProxyClientSocketPoolTest, TunnelUnexpectedClose) { |
| 502 MockWrite writes[] = { | 527 MockWrite writes[] = { |
| 503 MockWrite(ASYNC, 0, | 528 MockWrite(ASYNC, 0, |
| 504 "CONNECT www.google.com:443 HTTP/1.1\r\n" | 529 "CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 505 "Host: www.google.com:443\r\n" | 530 "Host: www.google.com:443\r\n" |
| 506 "Proxy-Connection: keep-alive\r\n" | 531 "Proxy-Connection: keep-alive\r\n" |
| 507 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | 532 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 508 }; | 533 }; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 // Make sure Location header was included and correct. | 718 // Make sure Location header was included and correct. |
| 694 std::string location; | 719 std::string location; |
| 695 EXPECT_TRUE(headers->IsRedirect(&location)); | 720 EXPECT_TRUE(headers->IsRedirect(&location)); |
| 696 EXPECT_EQ(location, redirectTarget); | 721 EXPECT_EQ(location, redirectTarget); |
| 697 } | 722 } |
| 698 } | 723 } |
| 699 | 724 |
| 700 // It would be nice to also test the timeouts in HttpProxyClientSocketPool. | 725 // It would be nice to also test the timeouts in HttpProxyClientSocketPool. |
| 701 | 726 |
| 702 } // namespace net | 727 } // namespace net |
| OLD | NEW |