| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.
h" | 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.
h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" | 13 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" |
| 14 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
| 15 #include "net/base/host_port_pair.h" | 15 #include "net/base/host_port_pair.h" |
| 16 #include "net/base/network_delegate.h" | 16 #include "net/base/network_delegate.h" |
| 17 #include "net/cert/cert_verifier.h" | |
| 18 #include "net/cert/mock_cert_verifier.h" | |
| 19 #include "net/dns/mock_host_resolver.h" | |
| 20 #include "net/http/http_network_layer.h" | |
| 21 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 22 #include "net/http/http_server_properties_impl.h" | |
| 23 #include "net/http/http_transaction_test_util.h" | 18 #include "net/http/http_transaction_test_util.h" |
| 24 #include "net/socket/socket_test_util.h" | 19 #include "net/socket/socket_test_util.h" |
| 25 #include "net/ssl/ssl_config_service.h" | 20 #include "net/url_request/static_http_user_agent_settings.h" |
| 26 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
| 27 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
| 28 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 25 |
| 31 using net::HttpNetworkLayer; | |
| 32 using net::HttpNetworkSession; | |
| 33 using net::HttpResponseHeaders; | 26 using net::HttpResponseHeaders; |
| 34 using net::HostPortPair; | 27 using net::HostPortPair; |
| 35 using net::MockRead; | 28 using net::MockRead; |
| 36 using net::MockWrite; | 29 using net::MockWrite; |
| 37 using net::ProxyRetryInfoMap; | 30 using net::ProxyRetryInfoMap; |
| 38 using net::ProxyService; | 31 using net::ProxyService; |
| 39 using net::StaticSocketDataProvider; | 32 using net::StaticSocketDataProvider; |
| 40 using net::TestDelegate; | 33 using net::TestDelegate; |
| 41 using net::TransportSecurityState; | |
| 42 using net::URLRequest; | 34 using net::URLRequest; |
| 43 using net::URLRequestContext; | 35 using net::TestURLRequestContext; |
| 44 | 36 |
| 45 | 37 |
| 46 namespace { | 38 namespace { |
| 47 // Transform "normal"-looking headers (\n-separated) to the appropriate | 39 // Transform "normal"-looking headers (\n-separated) to the appropriate |
| 48 // input format for ParseRawHeaders (\0-separated). | 40 // input format for ParseRawHeaders (\0-separated). |
| 49 void HeadersToRaw(std::string* headers) { | 41 void HeadersToRaw(std::string* headers) { |
| 50 std::replace(headers->begin(), headers->end(), '\n', '\0'); | 42 std::replace(headers->begin(), headers->end(), '\n', '\0'); |
| 51 if (!headers->empty()) | 43 if (!headers->empty()) |
| 52 *headers += '\0'; | 44 *headers += '\0'; |
| 53 } | 45 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 test_data_reduction_proxy_params_, | 104 test_data_reduction_proxy_params_, |
| 113 request, | 105 request, |
| 114 original_response_headers, | 106 original_response_headers, |
| 115 override_response_headers); | 107 override_response_headers); |
| 116 return net::OK; | 108 return net::OK; |
| 117 } | 109 } |
| 118 | 110 |
| 119 TestDataReductionProxyParams* test_data_reduction_proxy_params_; | 111 TestDataReductionProxyParams* test_data_reduction_proxy_params_; |
| 120 }; | 112 }; |
| 121 | 113 |
| 122 // Constructs a |URLRequestContext| that uses a |MockSocketFactory| to simulate | 114 // Constructs a |TestURLRequestContext| that uses a |MockSocketFactory| to |
| 123 // requests and responses. | 115 // simulate requests and responses. |
| 124 class DataReductionProxyProtocolTest : public testing::Test { | 116 class DataReductionProxyProtocolTest : public testing::Test { |
| 125 public: | 117 public: |
| 126 DataReductionProxyProtocolTest() | 118 DataReductionProxyProtocolTest() : http_user_agent_settings_("", "") {} |
| 127 : ssl_config_service_(new net::SSLConfigServiceDefaults) {} | |
| 128 | 119 |
| 129 // Sets up the |URLRequestContext| with the provided |ProxyService|. | 120 // Sets up the |TestURLRequestContext| with the provided |ProxyService|. |
| 130 void ConfigureTestDependencies(ProxyService* proxy_service) { | 121 void ConfigureTestDependencies(ProxyService* proxy_service) { |
| 131 cert_verifier_.reset(new net::MockCertVerifier); | 122 // Create a context with delayed initialization. |
| 132 transport_security_state_.reset(new TransportSecurityState); | 123 context_.reset(new TestURLRequestContext(true)); |
| 124 |
| 133 proxy_service_.reset(proxy_service); | 125 proxy_service_.reset(proxy_service); |
| 134 HttpNetworkSession::Params session_params; | |
| 135 session_params.client_socket_factory = &mock_socket_factory_; | |
| 136 session_params.host_resolver = &host_resolver_; | |
| 137 session_params.cert_verifier = cert_verifier_.get(); | |
| 138 session_params.transport_security_state = transport_security_state_.get(); | |
| 139 session_params.proxy_service = proxy_service_.get(); | |
| 140 session_params.ssl_config_service = ssl_config_service_.get(); | |
| 141 session_params.http_server_properties = | |
| 142 http_server_properties_.GetWeakPtr(); | |
| 143 network_session_ = new HttpNetworkSession(session_params); | |
| 144 factory_.reset(new HttpNetworkLayer(network_session_.get())); | |
| 145 | |
| 146 proxy_params_.reset(new TestDataReductionProxyParams()); | 126 proxy_params_.reset(new TestDataReductionProxyParams()); |
| 147 network_delegate_.reset(new TestDataReductionProxyNetworkDelegate( | 127 network_delegate_.reset(new TestDataReductionProxyNetworkDelegate( |
| 148 proxy_params_.get())); | 128 proxy_params_.get())); |
| 149 | 129 |
| 150 context_.reset(new URLRequestContext()); | 130 context_->set_client_socket_factory(&mock_socket_factory_); |
| 151 context_->set_host_resolver(&host_resolver_); | |
| 152 context_->set_cert_verifier(cert_verifier_.get()); | |
| 153 context_->set_proxy_service(proxy_service_.get()); | 131 context_->set_proxy_service(proxy_service_.get()); |
| 154 context_->set_ssl_config_service(ssl_config_service_.get()); | |
| 155 context_->set_http_transaction_factory(factory_.get()); | |
| 156 context_->set_http_server_properties(http_server_properties_.GetWeakPtr()); | |
| 157 context_->set_transport_security_state(transport_security_state_.get()); | |
| 158 context_->set_network_delegate(network_delegate_.get()); | 132 context_->set_network_delegate(network_delegate_.get()); |
| 133 // This is needed to prevent the test context from adding language headers |
| 134 // to requests. |
| 135 context_->set_http_user_agent_settings(&http_user_agent_settings_); |
| 136 |
| 137 context_->Init(); |
| 159 } | 138 } |
| 160 | 139 |
| 161 // Simulates a request to a data reduction proxy that may result in bypassing | 140 // Simulates a request to a data reduction proxy that may result in bypassing |
| 162 // the proxy and retrying the the request. | 141 // the proxy and retrying the the request. |
| 163 // Runs a test with the given request |method| that expects the first response | 142 // Runs a test with the given request |method| that expects the first response |
| 164 // from the server to be |first_response|. If |expected_retry|, the test | 143 // from the server to be |first_response|. If |expected_retry|, the test |
| 165 // will expect a retry of the request. A response body will be expected | 144 // will expect a retry of the request. A response body will be expected |
| 166 // if |expect_response_body|. | 145 // if |expect_response_body|. |
| 167 void TestProxyFallback(const char* method, | 146 void TestProxyFallback(const char* method, |
| 168 const char* first_response, | 147 const char* first_response, |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 ASSERT_TRUE(i != retry_info.end()); | 284 ASSERT_TRUE(i != retry_info.end()); |
| 306 EXPECT_TRUE(expected_min_duration <= (*i).second.current_delay); | 285 EXPECT_TRUE(expected_min_duration <= (*i).second.current_delay); |
| 307 EXPECT_TRUE((*i).second.current_delay <= expected_max_duration); | 286 EXPECT_TRUE((*i).second.current_delay <= expected_max_duration); |
| 308 } | 287 } |
| 309 } | 288 } |
| 310 | 289 |
| 311 protected: | 290 protected: |
| 312 base::MessageLoopForIO loop_; | 291 base::MessageLoopForIO loop_; |
| 313 | 292 |
| 314 net::MockClientSocketFactory mock_socket_factory_; | 293 net::MockClientSocketFactory mock_socket_factory_; |
| 315 net::MockHostResolver host_resolver_; | |
| 316 scoped_ptr<net::CertVerifier> cert_verifier_; | |
| 317 scoped_ptr<TransportSecurityState> transport_security_state_; | |
| 318 scoped_ptr<ProxyService> proxy_service_; | 294 scoped_ptr<ProxyService> proxy_service_; |
| 319 const scoped_refptr<net::SSLConfigService> ssl_config_service_; | |
| 320 scoped_refptr<HttpNetworkSession> network_session_; | |
| 321 scoped_ptr<HttpNetworkLayer> factory_; | |
| 322 net::HttpServerPropertiesImpl http_server_properties_; | |
| 323 scoped_ptr<TestDataReductionProxyParams> proxy_params_; | 295 scoped_ptr<TestDataReductionProxyParams> proxy_params_; |
| 324 scoped_ptr<TestDataReductionProxyNetworkDelegate> network_delegate_; | 296 scoped_ptr<TestDataReductionProxyNetworkDelegate> network_delegate_; |
| 297 net::StaticHttpUserAgentSettings http_user_agent_settings_; |
| 325 | 298 |
| 326 scoped_ptr<URLRequestContext> context_; | 299 scoped_ptr<TestURLRequestContext> context_; |
| 327 }; | 300 }; |
| 328 | 301 |
| 329 // Tests that request are deemed idempotent or not according to the method used. | 302 // Tests that request are deemed idempotent or not according to the method used. |
| 330 TEST_F(DataReductionProxyProtocolTest, TestIdempotency) { | 303 TEST_F(DataReductionProxyProtocolTest, TestIdempotency) { |
| 331 net::TestURLRequestContext context; | 304 net::TestURLRequestContext context; |
| 332 const struct { | 305 const struct { |
| 333 const char* method; | 306 const char* method; |
| 334 bool expected_result; | 307 bool expected_result; |
| 335 } tests[] = { | 308 } tests[] = { |
| 336 { "GET", true }, | 309 { "GET", true }, |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 EXPECT_EQ(net::URLRequestStatus::SUCCESS, r.status().status()); | 634 EXPECT_EQ(net::URLRequestStatus::SUCCESS, r.status().status()); |
| 662 EXPECT_EQ(net::OK, r.status().error()); | 635 EXPECT_EQ(net::OK, r.status().error()); |
| 663 | 636 |
| 664 EXPECT_EQ("Bypass message", d.data_received()); | 637 EXPECT_EQ("Bypass message", d.data_received()); |
| 665 | 638 |
| 666 // We should have no entries in our bad proxy list. | 639 // We should have no entries in our bad proxy list. |
| 667 TestBadProxies(0, -1, "", ""); | 640 TestBadProxies(0, -1, "", ""); |
| 668 } | 641 } |
| 669 | 642 |
| 670 } // namespace data_reduction_proxy | 643 } // namespace data_reduction_proxy |
| OLD | NEW |