Chromium Code Reviews| Index: net/http/http_network_transaction_ssl_unittest.cc |
| diff --git a/net/http/http_network_transaction_ssl_unittest.cc b/net/http/http_network_transaction_ssl_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..662fc497a0a9bc5915dc7525b899704922be12fb |
| --- /dev/null |
| +++ b/net/http/http_network_transaction_ssl_unittest.cc |
| @@ -0,0 +1,206 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "net/base/net_util.h" |
| +#include "net/base/request_priority.h" |
| +#include "net/dns/mock_host_resolver.h" |
| +#include "net/http/http_auth_handler_mock.h" |
| +#include "net/http/http_network_session.h" |
| +#include "net/http/http_network_transaction.h" |
| +#include "net/http/http_request_info.h" |
| +#include "net/http/http_server_properties_impl.h" |
| +#include "net/http/transport_security_state.h" |
| +#include "net/proxy/proxy_service.h" |
| +#include "net/socket/socket_test_util.h" |
| +#include "net/ssl/ssl_config_service_defaults.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| + |
| +class HttpNetworkTransactionSSLTest : public testing::Test { |
| + protected: |
| + virtual void SetUp() { |
| + ssl_config_ = new SSLConfigServiceDefaults; |
| + session_params_.ssl_config_service = ssl_config_.get(); |
| + |
| + auth_handler_factory_.reset(new HttpAuthHandlerMock::Factory()); |
| + session_params_.http_auth_handler_factory = auth_handler_factory_.get(); |
| + |
| + proxy_service_.reset(ProxyService::CreateDirect()); |
| + session_params_.proxy_service = proxy_service_.get(); |
| + |
| + session_params_.host_resolver = &mock_resolver_; |
| + session_params_.http_server_properties = &http_server_properties_; |
| + session_params_.transport_security_state = &transport_security_state_; |
| + } |
| + |
| + HttpRequestInfo* GetRequestInfo(std::string url) { |
| + HttpRequestInfo* request_info = new HttpRequestInfo; |
| + request_info->url = GURL(url); |
| + request_info->method = "GET"; |
| + return request_info; |
| + } |
| + |
| + SSLConfig& GetServerSSLConfig(HttpNetworkTransaction* trans) { |
| + return trans->server_ssl_config_; |
| + } |
| + |
| + scoped_refptr<SSLConfigService> ssl_config_; |
| + scoped_ptr<HttpAuthHandlerMock::Factory> auth_handler_factory_; |
| + scoped_ptr<ProxyService> proxy_service_; |
| + |
| + MockHostResolver mock_resolver_; |
| + HttpServerPropertiesImpl http_server_properties_; |
| + TransportSecurityState transport_security_state_; |
| + HttpNetworkSession::Params session_params_; |
| +}; |
| + |
| +TEST_F(HttpNetworkTransactionSSLTest, Google_SSLVersionMinPreloadedEnabled) { |
| + MockClientSocketFactory mock_socket_factory; |
|
Ryan Sleevi
2013/04/17 01:16:36
Seems like all your tests just end up creating a m
thaidn_google
2013/04/17 04:34:47
Nice tip. Thanks!
On 2013/04/17 01:16:36, Ryan Sl
|
| + |
| + // |ssl_data| contains the data for the first TLS 1.0 handshake that will |
| + // return |ERR_SSL_PROTOCOL_ERROR| to trigger the SSL 3.0 fallback logic in |
| + // |HttpNetworkTransaction|. |
|
Ryan Sleevi
2013/04/17 01:16:36
comment style nit: While the || notation isn't for
thaidn_google
2013/04/17 04:34:47
Good to know. Thanks!
On 2013/04/17 01:16:36, Rya
|
| + |
| + // Note: When a connection to a server fails during a handshake, |
| + // |HttpNetworkTransaction| will attempt to fallback to SSL 3.0 if the |
| + // previous connection was attempted with TLS 1.0. Since that behavior is |
|
Ryan Sleevi
2013/04/17 01:16:36
comment nit: Two bits:
1) This is explaining a con
thaidn_google
2013/04/17 04:34:47
I hope the modified comments LGTY :-).
On 2013/04
|
| + // disabled for Google properties, there is only one handshake data here. |
| + SSLSocketDataProvider ssl_data(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
| + mock_socket_factory.AddSSLSocketDataProvider(&ssl_data); |
| + net::StaticSocketDataProvider data(NULL, 0, NULL, 0); |
|
Ryan Sleevi
2013/04/17 01:16:36
style: net:: namespace issues still
thaidn_google
2013/04/17 04:34:47
Done.
|
| + mock_socket_factory.AddSocketDataProvider(&data); |
| + |
| + session_params_.client_socket_factory = &mock_socket_factory; |
| + |
| + scoped_refptr<HttpNetworkSession> session( |
| + new HttpNetworkSession(session_params_)); |
| + scoped_ptr<HttpNetworkTransaction> trans( |
| + new HttpNetworkTransaction(DEFAULT_PRIORITY, session)); |
| + TestCompletionCallback callback; |
|
Ryan Sleevi
2013/04/17 01:16:36
style nit: Move the TestCompletionCallback declara
thaidn_google
2013/04/17 04:34:47
Done.
|
| + |
| + SSLConfig& ssl_config = GetServerSSLConfig(trans.get()); |
| + // Explicitly configures the maximum supported SSL version to TLS 1.0. |
| + ssl_config.version_max = SSL_PROTOCOL_VERSION_TLS1; |
|
Ryan Sleevi
2013/04/17 01:16:36
Seems like this could be done in the test harness
thaidn_google
2013/04/17 04:34:47
Done.
|
| + |
| + // This will consume |ssl_data|. |
| + int rv = callback.GetResult( |
| + trans->Start(GetRequestInfo("https://www.google.com/"), |
| + callback.callback(), BoundNetLog())); |
| + EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv); |
| + |
| + // |version_max| never fallbacks to SSLv3 for Google properties. |
| + EXPECT_EQ(SSL_PROTOCOL_VERSION_TLS1, |
| + ssl_config.version_max); |
| + EXPECT_FALSE(ssl_config.version_fallback); |
| +} |
| + |
| +TEST_F(HttpNetworkTransactionSSLTest, Google_SSLVersionMinPreloadedDisabled) { |
| + MockClientSocketFactory mock_socket_factory; |
| + |
| + // |ssl_data1| contains the data for the first TLS 1.0 handshake that will |
| + // return |ERR_SSL_PROTOCOL_ERROR| to trigger the SSL 3.0 fallback logic in |
| + // |HttpNetworkTransaction|. |
| + SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
| + mock_socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| + net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| + mock_socket_factory.AddSocketDataProvider(&data1); |
| + |
| + // |ssl_data2| contains the data for the second SSL 3.0 handshake. When a |
| + // connection to a server fails during a handshake, |
| + // |HttpNetworkTransaction| will attempt to fallback to SSL 3.0 if the |
| + // previous connection was attempted with TLS 1.0. This is transparent to the |
| + // caller of the |HttpNetworkTransaction|. |
|
Ryan Sleevi
2013/04/17 01:16:36
With the comments from line 72 incorporated (eg: i
thaidn_google
2013/04/17 04:34:47
Done.
|
| + |
| + // Note: if |ssl_data2| is omitted (like in |
| + // |Google_SSLVersionMinPreloadedEnabled|) then this test will crash, because |
| + // |HttpNetworkTransaction| will attempt to handshake two times, but there is |
| + // only one handshake data. |
|
Ryan Sleevi
2013/04/17 01:16:36
comment nit: I'm generally not fond of inter-test
thaidn_google
2013/04/17 04:34:47
I agree.
On 2013/04/17 01:16:36, Ryan Sleevi wrot
|
| + SSLSocketDataProvider ssl_data2(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
| + mock_socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| + net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); |
| + mock_socket_factory.AddSocketDataProvider(&data2); |
| + |
| + session_params_.client_socket_factory = &mock_socket_factory; |
| + |
| + scoped_refptr<HttpNetworkSession> session( |
| + new HttpNetworkSession(session_params_)); |
| + scoped_ptr<HttpNetworkTransaction> trans( |
| + new HttpNetworkTransaction(DEFAULT_PRIORITY, session)); |
| + TestCompletionCallback callback; |
| + |
| + SSLConfig& ssl_config = GetServerSSLConfig(trans.get()); |
| + // Explicitly configures the maximum supported SSL version to TLS 1.0. |
| + ssl_config.version_max = SSL_PROTOCOL_VERSION_TLS1; |
| + |
| + ssl_config.ssl_version_min_preloaded_disabled = true; |
| + |
| + // This will consume |ssl_data1| and |ssl_data2|. |
| + int rv = callback.GetResult( |
| + trans->Start(GetRequestInfo("https://www.google.com/"), |
| + callback.callback(), BoundNetLog())); |
| + EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv); |
| + |
| + // |version_max| fallbacks to SSL 3.0 for Google properties when |
| + // |ssl_version_min_preloaded_disabled| is true. |
| + EXPECT_EQ(SSL_PROTOCOL_VERSION_SSL3, |
| + ssl_config.version_max); |
| + EXPECT_TRUE(ssl_config.version_fallback); |
| +} |
| + |
| +TEST_F(HttpNetworkTransactionSSLTest, PayPal_SSLVersionMinPreloadedDisabled) { |
| + MockClientSocketFactory mock_socket_factory; |
| + // |ssl_data1| contains the data for the first TLS 1.0 handshake that will |
| + // return |ERR_SSL_PROTOCOL_ERROR| to trigger the SSL 3.0 fallback logic in |
| + // |HttpNetworkTransaction|. |
| + SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
| + mock_socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| + net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| + mock_socket_factory.AddSocketDataProvider(&data1); |
| + |
| + // |ssl_data2| contains the data for the second SSL 3.0 handshake. When a |
| + // connection to a server fails during a handshake, |
| + // |HttpNetworkTransaction| will attempt to fallback to SSL 3.0 if the |
| + // previous connection was attempted with TLS 1.0. This is transparent to the |
| + // caller of the |HttpNetworkTransaction|. |
| + |
| + // Note: if |ssl_data2| is omitted (like in |
| + // |Google_SSLVersionMinPreloadedEnabled|) then this test will crash, because |
| + // |HttpNetworkTransaction| will attempt to handshake two times, but there is |
| + // only one handshake data. |
| + SSLSocketDataProvider ssl_data2(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
| + mock_socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| + net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); |
| + mock_socket_factory.AddSocketDataProvider(&data2); |
| + |
| + session_params_.client_socket_factory = &mock_socket_factory; |
| + |
| + scoped_refptr<HttpNetworkSession> session( |
| + new HttpNetworkSession(session_params_)); |
| + scoped_ptr<HttpNetworkTransaction> trans( |
| + new HttpNetworkTransaction(DEFAULT_PRIORITY, session)); |
| + TestCompletionCallback callback; |
| + |
| + SSLConfig& ssl_config = GetServerSSLConfig(trans.get()); |
| + // Explicitly configures the maximum supported SSL version to TLS 1.0. |
| + ssl_config.version_max = SSL_PROTOCOL_VERSION_TLS1; |
| + |
| + // This will consume |ssl_data1| and |ssl_data2|. |
| + int rv = callback.GetResult( |
| + trans->Start(GetRequestInfo("https://www.paypal.com/"), |
| + callback.callback(), BoundNetLog())); |
| + EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv); |
| + |
| + // |version_max| fallbacks to SSL 3.0. |
| + EXPECT_EQ(SSL_PROTOCOL_VERSION_SSL3, |
| + ssl_config.version_max); |
|
Ryan Sleevi
2013/04/17 01:16:36
Is wrapping really necessary here?
thaidn_google
2013/04/17 04:34:47
Done.
|
| + EXPECT_TRUE(ssl_config.version_fallback); |
| +} |
| + |
| +} // namespace net |
| + |