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/strings/utf_string_conversions.h"
13 #include "net/base/load_flags.h"
14 #include "net/base/net_util.h"
15 #include "net/base/test_completion_callback.h"
16 #include "net/cert/mock_cert_verifier.h"
17 #include "net/disk_cache/disk_cache.h"
18 #include "net/dns/mock_host_resolver.h"
19 #include "net/http/http_cache.h"
20 #include "net/http/http_network_session.h"
21 #include "net/http/http_server_properties_impl.h"
22 #include "net/http/transport_security_state.h"
23 #include "net/ssl/ssl_config_service_defaults.h"
24 #include "net/test/spawned_test_server/spawned_test_server.h"
25 #include "net/url_request/file_protocol_handler.h"
26 #include "net/url_request/url_request_context_storage.h"
27 #include "net/url_request/url_request_file_job.h"
28 #include "net/url_request/url_request_job_factory_impl.h"
29 #include "net/url_request/url_request_test_util.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "testing/platform_test.h"
36 // - Test canceling an outstanding request.
37 // - Test deleting ProxyScriptFetcher while a request is in progress.
41 const base::FilePath::CharType kDocRoot
[] =
42 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest");
49 // A non-mock URL request which can access http:// and file:// urls.
50 class RequestContext
: public URLRequestContext
{
52 RequestContext() : storage_(this) {
54 storage_
.set_host_resolver(scoped_ptr
<HostResolver
>(new MockHostResolver
));
55 storage_
.set_cert_verifier(new MockCertVerifier
);
56 storage_
.set_transport_security_state(new TransportSecurityState
);
57 storage_
.set_proxy_service(ProxyService::CreateFixed(no_proxy
));
58 storage_
.set_ssl_config_service(new SSLConfigServiceDefaults
);
59 storage_
.set_http_server_properties(new HttpServerPropertiesImpl
);
61 HttpNetworkSession::Params params
;
62 params
.host_resolver
= host_resolver();
63 params
.cert_verifier
= cert_verifier();
64 params
.transport_security_state
= transport_security_state();
65 params
.proxy_service
= proxy_service();
66 params
.ssl_config_service
= ssl_config_service();
67 params
.http_server_properties
= http_server_properties();
68 scoped_refptr
<HttpNetworkSession
> network_session(
69 new HttpNetworkSession(params
));
70 storage_
.set_http_transaction_factory(new HttpCache(
71 network_session
.get(), HttpCache::DefaultBackend::InMemory(0)));
72 URLRequestJobFactoryImpl
* job_factory
= new URLRequestJobFactoryImpl();
73 job_factory
->SetProtocolHandler("file", new FileProtocolHandler());
74 storage_
.set_job_factory(job_factory
);
77 virtual ~RequestContext() {
81 URLRequestContextStorage storage_
;
84 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest.
85 GURL
GetTestFileUrl(const std::string
& relpath
) {
87 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
88 path
= path
.AppendASCII("net");
89 path
= path
.AppendASCII("data");
90 path
= path
.AppendASCII("proxy_script_fetcher_unittest");
91 GURL base_url
= FilePathToFileURL(path
);
92 return GURL(base_url
.spec() + "/" + relpath
);
95 // Really simple NetworkDelegate so we can allow local file access on ChromeOS
96 // without introducing layering violations. Also causes a test failure if a
97 // request is seen that doesn't set a load flag to bypass revocation checking.
99 class BasicNetworkDelegate
: public NetworkDelegate
{
101 BasicNetworkDelegate() {}
102 virtual ~BasicNetworkDelegate() {}
105 virtual int OnBeforeURLRequest(URLRequest
* request
,
106 const CompletionCallback
& callback
,
107 GURL
* new_url
) OVERRIDE
{
108 EXPECT_TRUE(request
->load_flags() & LOAD_DISABLE_CERT_REVOCATION_CHECKING
);
112 virtual int OnBeforeSendHeaders(URLRequest
* request
,
113 const CompletionCallback
& callback
,
114 HttpRequestHeaders
* headers
) OVERRIDE
{
118 virtual void OnSendHeaders(URLRequest
* request
,
119 const HttpRequestHeaders
& headers
) OVERRIDE
{}
121 virtual int OnHeadersReceived(
123 const CompletionCallback
& callback
,
124 const HttpResponseHeaders
* original_response_headers
,
125 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
)
130 virtual void OnBeforeRedirect(URLRequest
* request
,
131 const GURL
& new_location
) OVERRIDE
{}
133 virtual void OnResponseStarted(URLRequest
* request
) OVERRIDE
{}
135 virtual void OnRawBytesRead(const URLRequest
& request
,
136 int bytes_read
) OVERRIDE
{}
138 virtual void OnCompleted(URLRequest
* request
, bool started
) OVERRIDE
{}
140 virtual void OnURLRequestDestroyed(URLRequest
* request
) OVERRIDE
{}
142 virtual void OnPACScriptError(int line_number
,
143 const base::string16
& error
) OVERRIDE
{}
145 virtual NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
147 const AuthChallengeInfo
& auth_info
,
148 const AuthCallback
& callback
,
149 AuthCredentials
* credentials
) OVERRIDE
{
150 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
153 virtual bool OnCanGetCookies(const URLRequest
& request
,
154 const CookieList
& cookie_list
) OVERRIDE
{
158 virtual bool OnCanSetCookie(const URLRequest
& request
,
159 const std::string
& cookie_line
,
160 CookieOptions
* options
) OVERRIDE
{
164 virtual bool OnCanAccessFile(const net::URLRequest
& request
,
165 const base::FilePath
& path
) const OVERRIDE
{
168 virtual bool OnCanThrottleRequest(const URLRequest
& request
) const OVERRIDE
{
172 virtual int OnBeforeSocketStreamConnect(
173 SocketStream
* stream
,
174 const CompletionCallback
& callback
) OVERRIDE
{
178 virtual void OnRequestWaitStateChange(const net::URLRequest
& request
,
179 RequestWaitState state
) OVERRIDE
{
182 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate
);
187 class ProxyScriptFetcherImplTest
: public PlatformTest
{
189 ProxyScriptFetcherImplTest()
190 : test_server_(SpawnedTestServer::TYPE_HTTP
,
191 net::SpawnedTestServer::kLocalhost
,
192 base::FilePath(kDocRoot
)) {
193 context_
.set_network_delegate(&network_delegate_
);
197 SpawnedTestServer test_server_
;
198 BasicNetworkDelegate network_delegate_
;
199 RequestContext context_
;
202 TEST_F(ProxyScriptFetcherImplTest
, FileUrl
) {
203 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
205 { // Fetch a non-existent file.
207 TestCompletionCallback callback
;
208 int result
= pac_fetcher
.Fetch(GetTestFileUrl("does-not-exist"),
209 &text
, callback
.callback());
210 EXPECT_EQ(ERR_IO_PENDING
, result
);
211 EXPECT_EQ(ERR_FILE_NOT_FOUND
, callback
.WaitForResult());
212 EXPECT_TRUE(text
.empty());
214 { // Fetch a file that exists.
216 TestCompletionCallback callback
;
217 int result
= pac_fetcher
.Fetch(GetTestFileUrl("pac.txt"),
218 &text
, callback
.callback());
219 EXPECT_EQ(ERR_IO_PENDING
, result
);
220 EXPECT_EQ(OK
, callback
.WaitForResult());
221 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text
);
225 // Note that all mime types are allowed for PAC file, to be consistent
226 // with other browsers.
227 TEST_F(ProxyScriptFetcherImplTest
, HttpMimeType
) {
228 ASSERT_TRUE(test_server_
.Start());
230 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
232 { // Fetch a PAC with mime type "text/plain"
233 GURL
url(test_server_
.GetURL("files/pac.txt"));
235 TestCompletionCallback callback
;
236 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
237 EXPECT_EQ(ERR_IO_PENDING
, result
);
238 EXPECT_EQ(OK
, callback
.WaitForResult());
239 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text
);
241 { // Fetch a PAC with mime type "text/html"
242 GURL
url(test_server_
.GetURL("files/pac.html"));
244 TestCompletionCallback callback
;
245 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
246 EXPECT_EQ(ERR_IO_PENDING
, result
);
247 EXPECT_EQ(OK
, callback
.WaitForResult());
248 EXPECT_EQ(ASCIIToUTF16("-pac.html-\n"), text
);
250 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig"
251 GURL
url(test_server_
.GetURL("files/pac.nsproxy"));
253 TestCompletionCallback callback
;
254 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
255 EXPECT_EQ(ERR_IO_PENDING
, result
);
256 EXPECT_EQ(OK
, callback
.WaitForResult());
257 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text
);
261 TEST_F(ProxyScriptFetcherImplTest
, HttpStatusCode
) {
262 ASSERT_TRUE(test_server_
.Start());
264 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
266 { // Fetch a PAC which gives a 500 -- FAIL
267 GURL
url(test_server_
.GetURL("files/500.pac"));
269 TestCompletionCallback callback
;
270 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
271 EXPECT_EQ(ERR_IO_PENDING
, result
);
272 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK
, callback
.WaitForResult());
273 EXPECT_TRUE(text
.empty());
275 { // Fetch a PAC which gives a 404 -- FAIL
276 GURL
url(test_server_
.GetURL("files/404.pac"));
278 TestCompletionCallback callback
;
279 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
280 EXPECT_EQ(ERR_IO_PENDING
, result
);
281 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK
, callback
.WaitForResult());
282 EXPECT_TRUE(text
.empty());
286 TEST_F(ProxyScriptFetcherImplTest
, ContentDisposition
) {
287 ASSERT_TRUE(test_server_
.Start());
289 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
291 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should
293 GURL
url(test_server_
.GetURL("files/downloadable.pac"));
295 TestCompletionCallback callback
;
296 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
297 EXPECT_EQ(ERR_IO_PENDING
, result
);
298 EXPECT_EQ(OK
, callback
.WaitForResult());
299 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text
);
302 // Verifies that PAC scripts are not being cached.
303 TEST_F(ProxyScriptFetcherImplTest
, NoCache
) {
304 ASSERT_TRUE(test_server_
.Start());
306 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
308 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour.
309 GURL
url(test_server_
.GetURL("files/cacheable_1hr.pac"));
312 TestCompletionCallback callback
;
313 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
314 EXPECT_EQ(ERR_IO_PENDING
, result
);
315 EXPECT_EQ(OK
, callback
.WaitForResult());
316 EXPECT_EQ(ASCIIToUTF16("-cacheable_1hr.pac-\n"), text
);
319 // Kill the HTTP server.
320 ASSERT_TRUE(test_server_
.Stop());
322 // Try to fetch the file again. Since the server is not running anymore, the
323 // call should fail, thus indicating that the file was not fetched from the
327 TestCompletionCallback callback
;
328 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
329 EXPECT_EQ(ERR_IO_PENDING
, result
);
331 #if defined(OS_ANDROID)
332 // On Android platform, the tests are run on the device while the server
333 // runs on the host machine. After killing the server, port forwarder
334 // running on the device is still active, which produces error message
335 // "Connection reset by peer" rather than "Connection refused".
336 EXPECT_EQ(ERR_CONNECTION_RESET
, callback
.WaitForResult());
338 EXPECT_EQ(ERR_CONNECTION_REFUSED
, callback
.WaitForResult());
343 TEST_F(ProxyScriptFetcherImplTest
, TooLarge
) {
344 ASSERT_TRUE(test_server_
.Start());
346 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
348 // Set the maximum response size to 50 bytes.
349 int prev_size
= pac_fetcher
.SetSizeConstraint(50);
351 // These two URLs are the same file, but are http:// vs file://
353 test_server_
.GetURL("files/large-pac.nsproxy"),
354 GetTestFileUrl("large-pac.nsproxy")
357 // Try fetching URLs that are 101 bytes large. We should abort the request
358 // after 50 bytes have been read, and fail with a too large error.
359 for (size_t i
= 0; i
< arraysize(urls
); ++i
) {
360 const GURL
& url
= urls
[i
];
362 TestCompletionCallback callback
;
363 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
364 EXPECT_EQ(ERR_IO_PENDING
, result
);
365 EXPECT_EQ(ERR_FILE_TOO_BIG
, callback
.WaitForResult());
366 EXPECT_TRUE(text
.empty());
369 // Restore the original size bound.
370 pac_fetcher
.SetSizeConstraint(prev_size
);
372 { // Make sure we can still fetch regular URLs.
373 GURL
url(test_server_
.GetURL("files/pac.nsproxy"));
375 TestCompletionCallback callback
;
376 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
377 EXPECT_EQ(ERR_IO_PENDING
, result
);
378 EXPECT_EQ(OK
, callback
.WaitForResult());
379 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text
);
383 TEST_F(ProxyScriptFetcherImplTest
, Hang
) {
384 ASSERT_TRUE(test_server_
.Start());
386 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
388 // Set the timeout period to 0.5 seconds.
389 base::TimeDelta prev_timeout
= pac_fetcher
.SetTimeoutConstraint(
390 base::TimeDelta::FromMilliseconds(500));
392 // Try fetching a URL which takes 1.2 seconds. We should abort the request
393 // after 500 ms, and fail with a timeout error.
395 GURL
url(test_server_
.GetURL("slow/proxy.pac?1.2"));
397 TestCompletionCallback callback
;
398 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
399 EXPECT_EQ(ERR_IO_PENDING
, result
);
400 EXPECT_EQ(ERR_TIMED_OUT
, callback
.WaitForResult());
401 EXPECT_TRUE(text
.empty());
404 // Restore the original timeout period.
405 pac_fetcher
.SetTimeoutConstraint(prev_timeout
);
407 { // Make sure we can still fetch regular URLs.
408 GURL
url(test_server_
.GetURL("files/pac.nsproxy"));
410 TestCompletionCallback callback
;
411 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
412 EXPECT_EQ(ERR_IO_PENDING
, result
);
413 EXPECT_EQ(OK
, callback
.WaitForResult());
414 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text
);
418 // The ProxyScriptFetcher should decode any content-codings
419 // (like gzip, bzip, etc.), and apply any charset conversions to yield
421 TEST_F(ProxyScriptFetcherImplTest
, Encodings
) {
422 ASSERT_TRUE(test_server_
.Start());
424 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
426 // Test a response that is gzip-encoded -- should get inflated.
428 GURL
url(test_server_
.GetURL("files/gzipped_pac"));
430 TestCompletionCallback callback
;
431 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
432 EXPECT_EQ(ERR_IO_PENDING
, result
);
433 EXPECT_EQ(OK
, callback
.WaitForResult());
434 EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text
);
437 // Test a response that was served as UTF-16 (BE). It should
438 // be converted to UTF8.
440 GURL
url(test_server_
.GetURL("files/utf16be_pac"));
442 TestCompletionCallback callback
;
443 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
444 EXPECT_EQ(ERR_IO_PENDING
, result
);
445 EXPECT_EQ(OK
, callback
.WaitForResult());
446 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text
);
450 TEST_F(ProxyScriptFetcherImplTest
, DataURLs
) {
451 ProxyScriptFetcherImpl
pac_fetcher(&context_
);
453 const char kEncodedUrl
[] =
454 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
455 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
456 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
457 const char kPacScript
[] =
458 "function FindProxyForURL(url, host) {\n"
459 " if (host == 'foobar.com')\n"
460 " return 'PROXY blackhole:80';\n"
461 " return 'DIRECT';\n"
464 // Test fetching a "data:"-url containing a base64 encoded PAC script.
466 GURL
url(kEncodedUrl
);
468 TestCompletionCallback callback
;
469 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
470 EXPECT_EQ(OK
, result
);
471 EXPECT_EQ(ASCIIToUTF16(kPacScript
), text
);
474 const char kEncodedUrlBroken
[] =
475 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R";
477 // Test a broken "data:"-url containing a base64 encoded PAC script.
479 GURL
url(kEncodedUrlBroken
);
481 TestCompletionCallback callback
;
482 int result
= pac_fetcher
.Fetch(url
, &text
, callback
.callback());
483 EXPECT_EQ(ERR_FAILED
, result
);