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 "chrome/browser/net/connection_tester.h"
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/cookie_store_factory.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/net_util.h"
22 #include "net/base/request_priority.h"
23 #include "net/cert/cert_verifier.h"
24 #include "net/dns/host_resolver.h"
25 #include "net/http/http_auth_handler_factory.h"
26 #include "net/http/http_cache.h"
27 #include "net/http/http_network_session.h"
28 #include "net/http/http_server_properties_impl.h"
29 #include "net/http/transport_security_state.h"
30 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
31 #include "net/proxy/proxy_config_service_fixed.h"
32 #include "net/proxy/proxy_script_fetcher_impl.h"
33 #include "net/proxy/proxy_service.h"
34 #include "net/proxy/proxy_service_v8.h"
35 #include "net/ssl/ssl_config_service_defaults.h"
36 #include "net/url_request/url_request.h"
37 #include "net/url_request/url_request_context.h"
38 #include "net/url_request/url_request_context_storage.h"
39 #include "net/url_request/url_request_job_factory_impl.h"
41 #if !defined(OS_ANDROID) && !defined(OS_IOS)
42 #include "chrome/browser/net/firefox_proxy_settings.h"
47 // ExperimentURLRequestContext ------------------------------------------------
49 // An instance of ExperimentURLRequestContext is created for each experiment
50 // run by ConnectionTester. The class initializes network dependencies according
51 // to the specified "experiment".
52 class ExperimentURLRequestContext
: public net::URLRequestContext
{
54 explicit ExperimentURLRequestContext(
55 net::URLRequestContext
* proxy_request_context
) :
57 proxy_request_context_(proxy_request_context
),
60 weak_factory_(this) {}
62 ~ExperimentURLRequestContext() override
{ AssertNoURLRequests(); }
64 // Creates a proxy config service for |experiment|. On success returns net::OK
65 // and fills |config_service| with a new pointer. Otherwise returns a network
67 int CreateProxyConfigService(
68 ConnectionTester::ProxySettingsExperiment experiment
,
69 scoped_ptr
<net::ProxyConfigService
>* config_service
,
70 base::Callback
<void(int)> callback
) {
72 case ConnectionTester::PROXY_EXPERIMENT_USE_SYSTEM_SETTINGS
:
73 return CreateSystemProxyConfigService(config_service
);
74 case ConnectionTester::PROXY_EXPERIMENT_USE_FIREFOX_SETTINGS
:
75 return CreateFirefoxProxyConfigService(config_service
, callback
);
76 case ConnectionTester::PROXY_EXPERIMENT_USE_AUTO_DETECT
:
77 config_service
->reset(new net::ProxyConfigServiceFixed(
78 net::ProxyConfig::CreateAutoDetect()));
80 case ConnectionTester::PROXY_EXPERIMENT_USE_DIRECT
:
81 config_service
->reset(new net::ProxyConfigServiceFixed(
82 net::ProxyConfig::CreateDirect()));
86 return net::ERR_UNEXPECTED
;
90 int Init(const ConnectionTester::Experiment
& experiment
,
91 scoped_ptr
<net::ProxyConfigService
>* proxy_config_service
,
92 net::NetLog
* net_log
) {
95 // Create a custom HostResolver for this experiment.
96 scoped_ptr
<net::HostResolver
> host_resolver_tmp
;
97 rv
= CreateHostResolver(experiment
.host_resolver_experiment
,
100 return rv
; // Failure.
101 storage_
.set_host_resolver(host_resolver_tmp
.Pass());
103 // Create a custom ProxyService for this this experiment.
104 scoped_ptr
<net::ProxyService
> experiment_proxy_service
;
105 rv
= CreateProxyService(experiment
.proxy_settings_experiment
,
106 proxy_config_service
, &experiment_proxy_service
);
108 return rv
; // Failure.
109 storage_
.set_proxy_service(experiment_proxy_service
.release());
111 // The rest of the dependencies are standard, and don't depend on the
112 // experiment being run.
113 storage_
.set_cert_verifier(net::CertVerifier::CreateDefault());
114 storage_
.set_transport_security_state(new net::TransportSecurityState
);
115 storage_
.set_ssl_config_service(new net::SSLConfigServiceDefaults
);
116 storage_
.set_http_auth_handler_factory(
117 net::HttpAuthHandlerFactory::CreateDefault(host_resolver()));
118 storage_
.set_http_server_properties(
119 scoped_ptr
<net::HttpServerProperties
>(
120 new net::HttpServerPropertiesImpl()));
122 net::HttpNetworkSession::Params session_params
;
123 session_params
.host_resolver
= host_resolver();
124 session_params
.cert_verifier
= cert_verifier();
125 session_params
.transport_security_state
= transport_security_state();
126 session_params
.proxy_service
= proxy_service();
127 session_params
.ssl_config_service
= ssl_config_service();
128 session_params
.http_auth_handler_factory
= http_auth_handler_factory();
129 session_params
.http_server_properties
= http_server_properties();
130 session_params
.net_log
= net_log
;
131 scoped_refptr
<net::HttpNetworkSession
> network_session(
132 new net::HttpNetworkSession(session_params
));
133 storage_
.set_http_transaction_factory(new net::HttpCache(
134 network_session
.get(), net::HttpCache::DefaultBackend::InMemory(0)));
135 // In-memory cookie store.
136 storage_
.set_cookie_store(
137 content::CreateCookieStore(content::CookieStoreConfig()));
138 // Creating a new job factory avoids added ProtocolHandlers and
139 // layered URLRequestInterceptingJobFactories.
140 storage_
.set_job_factory(new net::URLRequestJobFactoryImpl());
146 // Creates a host resolver for |experiment|. On success returns net::OK and
147 // fills |host_resolver| with a new pointer. Otherwise returns a network
149 int CreateHostResolver(
150 ConnectionTester::HostResolverExperiment experiment
,
151 scoped_ptr
<net::HostResolver
>* host_resolver
) {
152 // Create a vanilla HostResolver that disables caching.
153 const size_t kMaxJobs
= 50u;
154 const size_t kMaxRetryAttempts
= 4u;
155 net::HostResolver::Options options
;
156 options
.max_concurrent_resolves
= kMaxJobs
;
157 options
.max_retry_attempts
= kMaxRetryAttempts
;
158 options
.enable_caching
= false;
159 scoped_ptr
<net::HostResolver
> resolver(
160 net::HostResolver::CreateSystemResolver(options
, NULL
/* NetLog */));
162 // Modify it slightly based on the experiment being run.
163 switch (experiment
) {
164 case ConnectionTester::HOST_RESOLVER_EXPERIMENT_PLAIN
:
166 case ConnectionTester::HOST_RESOLVER_EXPERIMENT_DISABLE_IPV6
:
167 resolver
->SetDefaultAddressFamily(net::ADDRESS_FAMILY_IPV4
);
169 case ConnectionTester::HOST_RESOLVER_EXPERIMENT_IPV6_PROBE
: {
170 // The system HostResolver will probe by default.
175 return net::ERR_UNEXPECTED
;
177 host_resolver
->swap(resolver
);
181 // Creates a proxy service for |experiment|. On success returns net::OK
182 // and fills |experiment_proxy_service| with a new pointer. Otherwise returns
183 // a network error code.
184 int CreateProxyService(
185 ConnectionTester::ProxySettingsExperiment experiment
,
186 scoped_ptr
<net::ProxyConfigService
>* proxy_config_service
,
187 scoped_ptr
<net::ProxyService
>* experiment_proxy_service
) {
188 if (CommandLine::ForCurrentProcess()->HasSwitch(
189 switches::kSingleProcess
)) {
190 // We can't create a standard proxy resolver in single-process mode.
191 // Rather than falling-back to some other implementation, fail.
192 return net::ERR_NOT_IMPLEMENTED
;
195 net::DhcpProxyScriptFetcherFactory dhcp_factory
;
198 experiment_proxy_service
->reset(
199 net::ProxyService::CreateUsingSystemProxyResolver(
200 proxy_config_service
->release(), 0u, NULL
));
202 experiment_proxy_service
->reset(
203 net::CreateProxyServiceUsingV8ProxyResolver(
204 proxy_config_service
->release(),
205 new net::ProxyScriptFetcherImpl(proxy_request_context_
),
206 dhcp_factory
.Create(proxy_request_context_
),
215 // Creates a proxy config service that pulls from the system proxy settings.
216 // On success returns net::OK and fills |config_service| with a new pointer.
217 // Otherwise returns a network error code.
218 int CreateSystemProxyConfigService(
219 scoped_ptr
<net::ProxyConfigService
>* config_service
) {
220 #if defined(OS_LINUX) || defined(OS_OPENBSD)
221 // TODO(eroman): This is not supported on Linux yet, because of how
222 // construction needs ot happen on the UI thread.
223 return net::ERR_NOT_IMPLEMENTED
;
225 config_service
->reset(net::ProxyService::CreateSystemProxyConfigService(
226 base::ThreadTaskRunnerHandle::Get().get(), NULL
));
231 #if !defined(OS_ANDROID) && !defined(OS_IOS)
232 static int FirefoxProxySettingsTask(
233 FirefoxProxySettings
* firefox_settings
) {
234 if (!FirefoxProxySettings::GetSettings(firefox_settings
))
235 return net::ERR_FILE_NOT_FOUND
;
239 void FirefoxProxySettingsReply(
240 scoped_ptr
<net::ProxyConfigService
>* config_service
,
241 FirefoxProxySettings
* firefox_settings
,
242 base::Callback
<void(int)> callback
,
245 if (FirefoxProxySettings::SYSTEM
== firefox_settings
->config_type()) {
246 rv
= CreateSystemProxyConfigService(config_service
);
248 net::ProxyConfig config
;
249 if (firefox_settings
->ToProxyConfig(&config
))
250 config_service
->reset(new net::ProxyConfigServiceFixed(config
));
252 rv
= net::ERR_FAILED
;
259 // Creates a fixed proxy config service that is initialized using Firefox's
260 // current proxy settings. On success returns net::OK and fills
261 // |config_service| with a new pointer. Otherwise returns a network error
263 int CreateFirefoxProxyConfigService(
264 scoped_ptr
<net::ProxyConfigService
>* config_service
,
265 base::Callback
<void(int)> callback
) {
266 #if defined(OS_ANDROID) || defined(OS_IOS)
267 // Chrome on Android and iOS do not support Firefox settings.
268 return net::ERR_NOT_IMPLEMENTED
;
270 // Fetch Firefox's proxy settings (can fail if Firefox is not installed).
271 FirefoxProxySettings
* ff_settings
= new FirefoxProxySettings();
272 base::Callback
<int(void)> task
= base::Bind(
273 &FirefoxProxySettingsTask
, ff_settings
);
274 base::Callback
<void(int)> reply
= base::Bind(
275 &ExperimentURLRequestContext::FirefoxProxySettingsReply
,
276 weak_factory_
.GetWeakPtr(), config_service
,
277 base::Owned(ff_settings
), callback
);
278 if (!content::BrowserThread::PostTaskAndReplyWithResult
<int>(
279 content::BrowserThread::FILE, FROM_HERE
, task
, reply
))
280 return net::ERR_FAILED
;
281 return net::ERR_IO_PENDING
;
286 net::URLRequestContext
* const proxy_request_context_
;
288 net::URLRequestContextStorage storage_
;
289 base::WeakPtrFactory
<ExperimentURLRequestContext
> weak_factory_
;
294 // ConnectionTester::TestRunner ----------------------------------------------
296 // TestRunner is a helper class for running an individual experiment. It can
297 // be deleted any time after it is started, and this will abort the request.
298 class ConnectionTester::TestRunner
: public net::URLRequest::Delegate
{
300 // |tester| must remain alive throughout the TestRunner's lifetime.
301 // |tester| will be notified of completion.
302 TestRunner(ConnectionTester
* tester
, net::NetLog
* net_log
)
305 weak_factory_(this) {}
307 // Finish running |experiment| once a ProxyConfigService has been created.
308 // In the case of a FirefoxProxyConfigService, this will be called back
309 // after disk access has completed.
310 void ProxyConfigServiceCreated(
311 const Experiment
& experiment
,
312 scoped_ptr
<net::ProxyConfigService
>* proxy_config_service
, int status
);
314 // Starts running |experiment|. Notifies tester->OnExperimentCompleted() when
316 void Run(const Experiment
& experiment
);
318 // Overridden from net::URLRequest::Delegate:
319 void OnResponseStarted(net::URLRequest
* request
) override
;
320 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
;
321 // TODO(eroman): handle cases requiring authentication.
324 // The number of bytes to read each response body chunk.
325 static const int kReadBufferSize
= 1024;
327 // Starts reading the response's body (and keeps reading until an error or
329 void ReadBody(net::URLRequest
* request
);
331 // Called when the request has completed (for both success and failure).
332 void OnResponseCompleted(net::URLRequest
* request
);
333 void OnExperimentCompletedWithResult(int result
);
335 ConnectionTester
* tester_
;
336 scoped_ptr
<ExperimentURLRequestContext
> request_context_
;
337 scoped_ptr
<net::URLRequest
> request_
;
338 net::NetLog
* net_log_
;
340 base::WeakPtrFactory
<TestRunner
> weak_factory_
;
342 DISALLOW_COPY_AND_ASSIGN(TestRunner
);
345 void ConnectionTester::TestRunner::OnResponseStarted(net::URLRequest
* request
) {
346 if (!request
->status().is_success()) {
347 OnResponseCompleted(request
);
351 // Start reading the body.
355 void ConnectionTester::TestRunner::OnReadCompleted(net::URLRequest
* request
,
357 if (bytes_read
<= 0) {
358 OnResponseCompleted(request
);
362 // Keep reading until the stream is closed. Throw the data read away.
366 void ConnectionTester::TestRunner::ReadBody(net::URLRequest
* request
) {
367 // Read the response body |kReadBufferSize| bytes at a time.
368 scoped_refptr
<net::IOBuffer
> unused_buffer(
369 new net::IOBuffer(kReadBufferSize
));
371 if (request
->Read(unused_buffer
.get(), kReadBufferSize
, &num_bytes
)) {
372 OnReadCompleted(request
, num_bytes
);
373 } else if (!request
->status().is_io_pending()) {
374 // Read failed synchronously.
375 OnResponseCompleted(request
);
379 void ConnectionTester::TestRunner::OnResponseCompleted(
380 net::URLRequest
* request
) {
381 int result
= net::OK
;
382 if (!request
->status().is_success()) {
383 DCHECK_NE(net::ERR_IO_PENDING
, request
->status().error());
384 result
= request
->status().error();
387 // Post a task to notify the parent rather than handling it right away,
388 // to avoid re-entrancy problems with URLRequest. (Don't want the caller
389 // to end up deleting the URLRequest while in the middle of processing).
390 base::MessageLoop::current()->PostTask(
392 base::Bind(&TestRunner::OnExperimentCompletedWithResult
,
393 weak_factory_
.GetWeakPtr(), result
));
396 void ConnectionTester::TestRunner::OnExperimentCompletedWithResult(int result
) {
397 tester_
->OnExperimentCompleted(result
);
400 void ConnectionTester::TestRunner::ProxyConfigServiceCreated(
401 const Experiment
& experiment
,
402 scoped_ptr
<net::ProxyConfigService
>* proxy_config_service
,
404 if (status
== net::OK
)
405 status
= request_context_
->Init(experiment
,
406 proxy_config_service
,
408 if (status
!= net::OK
) {
409 tester_
->OnExperimentCompleted(status
);
412 // Fetch a request using the experimental context.
413 request_
= request_context_
->CreateRequest(
414 experiment
.url
, net::DEFAULT_PRIORITY
, this, NULL
);
418 void ConnectionTester::TestRunner::Run(const Experiment
& experiment
) {
419 // Try to create a net::URLRequestContext for this experiment.
420 request_context_
.reset(
421 new ExperimentURLRequestContext(tester_
->proxy_request_context_
));
422 scoped_ptr
<net::ProxyConfigService
>* proxy_config_service
=
423 new scoped_ptr
<net::ProxyConfigService
>();
424 base::Callback
<void(int)> config_service_callback
=
426 &TestRunner::ProxyConfigServiceCreated
, weak_factory_
.GetWeakPtr(),
427 experiment
, base::Owned(proxy_config_service
));
428 int rv
= request_context_
->CreateProxyConfigService(
429 experiment
.proxy_settings_experiment
,
430 proxy_config_service
, config_service_callback
);
431 if (rv
!= net::ERR_IO_PENDING
)
432 ProxyConfigServiceCreated(experiment
, proxy_config_service
, rv
);
435 // ConnectionTester ----------------------------------------------------------
437 ConnectionTester::ConnectionTester(
439 net::URLRequestContext
* proxy_request_context
,
440 net::NetLog
* net_log
)
441 : delegate_(delegate
),
442 proxy_request_context_(proxy_request_context
),
445 DCHECK(proxy_request_context
);
448 ConnectionTester::~ConnectionTester() {
449 // Cancellation happens automatically by deleting test_runner_.
452 void ConnectionTester::RunAllTests(const GURL
& url
) {
453 // Select all possible experiments to run. (In no particular order).
454 // It is possible that some of these experiments are actually duplicates.
455 GetAllPossibleExperimentCombinations(url
, &remaining_experiments_
);
457 delegate_
->OnStartConnectionTestSuite();
458 StartNextExperiment();
462 base::string16
ConnectionTester::ProxySettingsExperimentDescription(
463 ProxySettingsExperiment experiment
) {
464 // TODO(eroman): Use proper string resources.
465 switch (experiment
) {
466 case PROXY_EXPERIMENT_USE_DIRECT
:
467 return base::ASCIIToUTF16("Don't use any proxy");
468 case PROXY_EXPERIMENT_USE_SYSTEM_SETTINGS
:
469 return base::ASCIIToUTF16("Use system proxy settings");
470 case PROXY_EXPERIMENT_USE_FIREFOX_SETTINGS
:
471 return base::ASCIIToUTF16("Use Firefox's proxy settings");
472 case PROXY_EXPERIMENT_USE_AUTO_DETECT
:
473 return base::ASCIIToUTF16("Auto-detect proxy settings");
476 return base::string16();
481 base::string16
ConnectionTester::HostResolverExperimentDescription(
482 HostResolverExperiment experiment
) {
483 // TODO(eroman): Use proper string resources.
484 switch (experiment
) {
485 case HOST_RESOLVER_EXPERIMENT_PLAIN
:
486 return base::string16();
487 case HOST_RESOLVER_EXPERIMENT_DISABLE_IPV6
:
488 return base::ASCIIToUTF16("Disable IPv6 host resolving");
489 case HOST_RESOLVER_EXPERIMENT_IPV6_PROBE
:
490 return base::ASCIIToUTF16("Probe for IPv6 host resolving");
493 return base::string16();
498 void ConnectionTester::GetAllPossibleExperimentCombinations(
500 ConnectionTester::ExperimentList
* list
) {
502 for (size_t resolver_experiment
= 0;
503 resolver_experiment
< HOST_RESOLVER_EXPERIMENT_COUNT
;
504 ++resolver_experiment
) {
505 for (size_t proxy_experiment
= 0;
506 proxy_experiment
< PROXY_EXPERIMENT_COUNT
;
507 ++proxy_experiment
) {
508 Experiment
experiment(
510 static_cast<ProxySettingsExperiment
>(proxy_experiment
),
511 static_cast<HostResolverExperiment
>(resolver_experiment
));
512 list
->push_back(experiment
);
517 void ConnectionTester::StartNextExperiment() {
518 DCHECK(!remaining_experiments_
.empty());
519 DCHECK(!current_test_runner_
.get());
521 delegate_
->OnStartConnectionTestExperiment(current_experiment());
523 current_test_runner_
.reset(new TestRunner(this, net_log_
));
524 current_test_runner_
->Run(current_experiment());
527 void ConnectionTester::OnExperimentCompleted(int result
) {
528 Experiment current
= current_experiment();
530 // Advance to the next experiment.
531 remaining_experiments_
.erase(remaining_experiments_
.begin());
532 current_test_runner_
.reset();
534 // Notify the delegate of completion.
535 delegate_
->OnCompletedConnectionTestExperiment(current
, result
);
537 if (remaining_experiments_
.empty()) {
538 delegate_
->OnCompletedConnectionTestSuite();
540 StartNextExperiment();