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/proxy/proxy_script_fetcher_impl.h"
9 #include "base/compiler_specific.h"
10 #include "base/files/file_path.h"
11 #include "base/path_service.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "net/base/filename_util.h"
16 #include "net/base/load_flags.h"
17 #include "net/base/network_delegate_impl.h"
18 #include "net/base/test_completion_callback.h"
19 #include "net/cert/mock_cert_verifier.h"
20 #include "net/disk_cache/disk_cache.h"
21 #include "net/dns/mock_host_resolver.h"
22 #include "net/http/http_cache.h"
23 #include "net/http/http_network_session.h"
24 #include "net/http/http_server_properties_impl.h"
25 #include "net/http/transport_security_state.h"
26 #include "net/ssl/ssl_config_service_defaults.h"
27 #include "net/test/spawned_test_server/spawned_test_server.h"
28 #include "net/url_request/url_request_context_storage.h"
29 #include "net/url_request/url_request_file_job.h"
30 #include "net/url_request/url_request_job_factory_impl.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "testing/platform_test.h"
35 #if !defined(DISABLE_FILE_SUPPORT)
36 #include "net/url_request/file_protocol_handler.h"
39 using base::ASCIIToUTF16
;
42 // - Test canceling an outstanding request.
43 // - Test deleting ProxyScriptFetcher while a request is in progress.
49 const base::FilePath::CharType kDocRoot
[] =
50 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest");
57 // A non-mock URL request which can access http:// and file:// urls, in the case
58 // the tests were built with file support.
59 class RequestContext
: public URLRequestContext
{
61 RequestContext() : storage_(this) {
63 storage_
.set_host_resolver(scoped_ptr
<HostResolver
>(new MockHostResolver
));
64 storage_
.set_cert_verifier(new MockCertVerifier
);
65 storage_
.set_transport_security_state(new TransportSecurityState
);
66 storage_
.set_proxy_service(ProxyService::CreateFixed(no_proxy
));
67 storage_
.set_ssl_config_service(new SSLConfigServiceDefaults
);
68 storage_
.set_http_server_properties(
69 scoped_ptr
<HttpServerProperties
>(new HttpServerPropertiesImpl()));
71 HttpNetworkSession::Params params
;
72 params
.host_resolver
= host_resolver();
73 params
.cert_verifier
= cert_verifier();
74 params
.transport_security_state
= transport_security_state();
75 params
.proxy_service
= proxy_service();
76 params
.ssl_config_service
= ssl_config_service();
77 params
.http_server_properties
= http_server_properties();
78 scoped_refptr
<HttpNetworkSession
> network_session(
79 new HttpNetworkSession(params
));
80 storage_
.set_http_transaction_factory(new HttpCache(
81 network_session
.get(), HttpCache::DefaultBackend::InMemory(0)));
82 URLRequestJobFactoryImpl
* job_factory
= new URLRequestJobFactoryImpl();
83 #if !defined(DISABLE_FILE_SUPPORT)
84 job_factory
->SetProtocolHandler(
85 "file", new FileProtocolHandler(base::ThreadTaskRunnerHandle::Get()));
87 storage_
.set_job_factory(job_factory
);
90 ~RequestContext() override
{ AssertNoURLRequests(); }
93 URLRequestContextStorage storage_
;
96 #if !defined(DISABLE_FILE_SUPPORT)
97 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest.
98 GURL
GetTestFileUrl(const std::string
& relpath
) {
100 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
101 path
= path
.AppendASCII("net");
102 path
= path
.AppendASCII("data");
103 path
= path
.AppendASCII("proxy_script_fetcher_unittest");
104 GURL base_url
= FilePathToFileURL(path
);
105 return GURL(base_url
.spec() + "/" + relpath
);
107 #endif // !defined(DISABLE_FILE_SUPPORT)
109 // Really simple NetworkDelegate so we can allow local file access on ChromeOS
110 // without introducing layering violations. Also causes a test failure if a
111 // request is seen that doesn't set a load flag to bypass revocation checking.
113 class BasicNetworkDelegate
: public NetworkDelegateImpl
{
115 BasicNetworkDelegate() {}
116 ~BasicNetworkDelegate() override
{}
119 int OnBeforeURLRequest(URLRequest
* request
,
120 const CompletionCallback
& callback
,
121 GURL
* new_url
) override
{
122 EXPECT_TRUE(request
->load_flags() & LOAD_DISABLE_CERT_REVOCATION_CHECKING
);
126 int OnBeforeSendHeaders(URLRequest
* request
,
127 const CompletionCallback
& callback
,
128 HttpRequestHeaders
* headers
) override
{
132 void OnSendHeaders(URLRequest
* request
,
133 const HttpRequestHeaders
& headers
) override
{}
135 int OnHeadersReceived(
137 const CompletionCallback
& callback
,
138 const HttpResponseHeaders
* original_response_headers
,
139 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
140 GURL
* allowed_unsafe_redirect_url
) override
{
144 void OnBeforeRedirect(URLRequest
* request
,
145 const GURL
& new_location
) override
{}
147 void OnResponseStarted(URLRequest
* request
) override
{}
149 void OnRawBytesRead(const URLRequest
& request
, int bytes_read
) override
{}
151 void OnCompleted(URLRequest
* request
, bool started
) override
{}
153 void OnURLRequestDestroyed(URLRequest
* request
) override
{}
155 void OnPACScriptError(int line_number
, const base::string16
& error
) override
{
158 NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
160 const AuthChallengeInfo
& auth_info
,
161 const AuthCallback
& callback
,
162 AuthCredentials
* credentials
) override
{
163 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
166 bool OnCanGetCookies(const URLRequest
& request
,
167 const CookieList
& cookie_list
) override
{
171 bool OnCanSetCookie(const URLRequest
& request
,
172 const std::string
& cookie_line
,
173 CookieOptions
* options
) override
{
177 bool OnCanAccessFile(const URLRequest
& request
,
178 const base::FilePath
& path
) const override
{
181 bool OnCanThrottleRequest(const URLRequest
& request
) const override
{
185 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate
);
188 class ProxyScriptFetcherImplTest
: public PlatformTest
{
190 ProxyScriptFetcherImplTest()
191 : test_server_(SpawnedTestServer::TYPE_HTTP
,
192 SpawnedTestServer::kLocalhost
,
193 base::FilePath(kDocRoot
)) {
194 context_
.set_network_delegate(&network_delegate_
);
198 SpawnedTestServer test_server_
;
199 BasicNetworkDelegate network_delegate_
;
200 RequestContext context_
;
203 #if !defined(DISABLE_FILE_SUPPORT)
204 TEST_F(ProxyScriptFetcherImplTest
, FileUrl
) {
205 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
207 { // Fetch a non-existent file.
209 TestCompletionCallback callback
;
210 int result
= pac_fetcher
.Fetch(GetTestFileUrl("does-not-exist"),
211 &text
, callback
.callback());
212 EXPECT_EQ(ERR_IO_PENDING
, result
);
213 EXPECT_EQ(ERR_FILE_NOT_FOUND
, callback
.WaitForResult());
214 EXPECT_TRUE(text
.empty());
216 { // Fetch a file that exists.
218 TestCompletionCallback callback
;
219 int result
= pac_fetcher
.Fetch(GetTestFileUrl("pac.txt"),
220 &text
, callback
.callback());
221 EXPECT_EQ(ERR_IO_PENDING
, result
);
222 EXPECT_EQ(OK
, callback
.WaitForResult());
223 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text
);
226 #endif // !defined(DISABLE_FILE_SUPPORT)
228 // Note that all mime types are allowed for PAC file, to be consistent
229 // with other browsers.
230 TEST_F(ProxyScriptFetcherImplTest
, HttpMimeType
) {
231 ASSERT_TRUE(test_server_
.Start());
233 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
235 { // Fetch a PAC with mime type "text/plain"
236 GURL
url(test_server_
.GetURL("files/pac.txt"));
238 TestCompletionCallback callback
;
239 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
240 EXPECT_EQ(ERR_IO_PENDING
, result
);
241 EXPECT_EQ(OK
, callback
.WaitForResult());
242 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text
);
244 { // Fetch a PAC with mime type "text/html"
245 GURL
url(test_server_
.GetURL("files/pac.html"));
247 TestCompletionCallback callback
;
248 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
249 EXPECT_EQ(ERR_IO_PENDING
, result
);
250 EXPECT_EQ(OK
, callback
.WaitForResult());
251 EXPECT_EQ(ASCIIToUTF16("-pac.html-\n"), text
);
253 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig"
254 GURL
url(test_server_
.GetURL("files/pac.nsproxy"));
256 TestCompletionCallback callback
;
257 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
258 EXPECT_EQ(ERR_IO_PENDING
, result
);
259 EXPECT_EQ(OK
, callback
.WaitForResult());
260 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text
);
264 TEST_F(ProxyScriptFetcherImplTest
, HttpStatusCode
) {
265 ASSERT_TRUE(test_server_
.Start());
267 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
269 { // Fetch a PAC which gives a 500 -- FAIL
270 GURL
url(test_server_
.GetURL("files/500.pac"));
272 TestCompletionCallback callback
;
273 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
274 EXPECT_EQ(ERR_IO_PENDING
, result
);
275 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK
, callback
.WaitForResult());
276 EXPECT_TRUE(text
.empty());
278 { // Fetch a PAC which gives a 404 -- FAIL
279 GURL
url(test_server_
.GetURL("files/404.pac"));
281 TestCompletionCallback callback
;
282 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
283 EXPECT_EQ(ERR_IO_PENDING
, result
);
284 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK
, callback
.WaitForResult());
285 EXPECT_TRUE(text
.empty());
289 TEST_F(ProxyScriptFetcherImplTest
, ContentDisposition
) {
290 ASSERT_TRUE(test_server_
.Start());
292 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
294 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should
296 GURL
url(test_server_
.GetURL("files/downloadable.pac"));
298 TestCompletionCallback callback
;
299 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
300 EXPECT_EQ(ERR_IO_PENDING
, result
);
301 EXPECT_EQ(OK
, callback
.WaitForResult());
302 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text
);
305 // Verifies that PAC scripts are not being cached.
306 TEST_F(ProxyScriptFetcherImplTest
, NoCache
) {
307 ASSERT_TRUE(test_server_
.Start());
309 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
311 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour.
312 GURL
url(test_server_
.GetURL("files/cacheable_1hr.pac"));
315 TestCompletionCallback callback
;
316 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
317 EXPECT_EQ(ERR_IO_PENDING
, result
);
318 EXPECT_EQ(OK
, callback
.WaitForResult());
319 EXPECT_EQ(ASCIIToUTF16("-cacheable_1hr.pac-\n"), text
);
322 // Kill the HTTP server.
323 ASSERT_TRUE(test_server_
.Stop());
325 // Try to fetch the file again. Since the server is not running anymore, the
326 // call should fail, thus indicating that the file was not fetched from the
330 TestCompletionCallback callback
;
331 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
332 EXPECT_EQ(ERR_IO_PENDING
, result
);
334 // Expect any error. The exact error varies by platform.
335 EXPECT_NE(OK
, callback
.WaitForResult());
339 TEST_F(ProxyScriptFetcherImplTest
, TooLarge
) {
340 ASSERT_TRUE(test_server_
.Start());
342 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
344 // Set the maximum response size to 50 bytes.
345 int prev_size
= pac_fetcher
.SetSizeConstraint(50);
347 // These two URLs are the same file, but are http:// vs file://
349 test_server_
.GetURL("files/large-pac.nsproxy"),
350 #if !defined(DISABLE_FILE_SUPPORT)
351 GetTestFileUrl("large-pac.nsproxy")
355 // Try fetching URLs that are 101 bytes large. We should abort the request
356 // after 50 bytes have been read, and fail with a too large error.
357 for (size_t i
= 0; i
< arraysize(urls
); ++i
) {
358 const GURL
& url
= urls
[i
];
360 TestCompletionCallback callback
;
361 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
362 EXPECT_EQ(ERR_IO_PENDING
, result
);
363 EXPECT_EQ(ERR_FILE_TOO_BIG
, callback
.WaitForResult());
364 EXPECT_TRUE(text
.empty());
367 // Restore the original size bound.
368 pac_fetcher
.SetSizeConstraint(prev_size
);
370 { // Make sure we can still fetch regular URLs.
371 GURL
url(test_server_
.GetURL("files/pac.nsproxy"));
373 TestCompletionCallback callback
;
374 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
375 EXPECT_EQ(ERR_IO_PENDING
, result
);
376 EXPECT_EQ(OK
, callback
.WaitForResult());
377 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text
);
381 TEST_F(ProxyScriptFetcherImplTest
, Hang
) {
382 ASSERT_TRUE(test_server_
.Start());
384 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
386 // Set the timeout period to 0.5 seconds.
387 base::TimeDelta prev_timeout
= pac_fetcher
.SetTimeoutConstraint(
388 base::TimeDelta::FromMilliseconds(500));
390 // Try fetching a URL which takes 1.2 seconds. We should abort the request
391 // after 500 ms, and fail with a timeout error.
393 GURL
url(test_server_
.GetURL("slow/proxy.pac?1.2"));
395 TestCompletionCallback callback
;
396 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
397 EXPECT_EQ(ERR_IO_PENDING
, result
);
398 EXPECT_EQ(ERR_TIMED_OUT
, callback
.WaitForResult());
399 EXPECT_TRUE(text
.empty());
402 // Restore the original timeout period.
403 pac_fetcher
.SetTimeoutConstraint(prev_timeout
);
405 { // Make sure we can still fetch regular URLs.
406 GURL
url(test_server_
.GetURL("files/pac.nsproxy"));
408 TestCompletionCallback callback
;
409 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
410 EXPECT_EQ(ERR_IO_PENDING
, result
);
411 EXPECT_EQ(OK
, callback
.WaitForResult());
412 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text
);
416 // The ProxyScriptFetcher should decode any content-codings
417 // (like gzip, bzip, etc.), and apply any charset conversions to yield
419 TEST_F(ProxyScriptFetcherImplTest
, Encodings
) {
420 ASSERT_TRUE(test_server_
.Start());
422 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
424 // Test a response that is gzip-encoded -- should get inflated.
426 GURL
url(test_server_
.GetURL("files/gzipped_pac"));
428 TestCompletionCallback callback
;
429 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
430 EXPECT_EQ(ERR_IO_PENDING
, result
);
431 EXPECT_EQ(OK
, callback
.WaitForResult());
432 EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text
);
435 // Test a response that was served as UTF-16 (BE). It should
436 // be converted to UTF8.
438 GURL
url(test_server_
.GetURL("files/utf16be_pac"));
440 TestCompletionCallback callback
;
441 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
442 EXPECT_EQ(ERR_IO_PENDING
, result
);
443 EXPECT_EQ(OK
, callback
.WaitForResult());
444 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text
);
448 TEST_F(ProxyScriptFetcherImplTest
, DataURLs
) {
449 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
451 const char kEncodedUrl
[] =
452 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
453 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
454 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
455 const char kPacScript
[] =
456 "function FindProxyForURL(url, host) {\n"
457 " if (host == 'foobar.com')\n"
458 " return 'PROXY blackhole:80';\n"
459 " return 'DIRECT';\n"
462 // Test fetching a "data:"-url containing a base64 encoded PAC script.
464 GURL
url(kEncodedUrl
);
466 TestCompletionCallback callback
;
467 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
468 EXPECT_EQ(OK
, result
);
469 EXPECT_EQ(ASCIIToUTF16(kPacScript
), text
);
472 const char kEncodedUrlBroken
[] =
473 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R";
475 // Test a broken "data:"-url containing a base64 encoded PAC script.
477 GURL
url(kEncodedUrlBroken
);
479 TestCompletionCallback callback
;
480 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
481 EXPECT_EQ(ERR_FAILED
, result
);