Enables compositing support for webview.
[chromium-blink-merge.git] / net / proxy / proxy_script_fetcher_impl_unittest.cc
blobf7ca2b42aba440c96591da4312643493597a4905
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"
7 #include <string>
9 #include "base/file_path.h"
10 #include "base/compiler_specific.h"
11 #include "base/path_service.h"
12 #include "base/utf_string_conversions.h"
13 #include "net/base/mock_cert_verifier.h"
14 #include "net/base/mock_host_resolver.h"
15 #include "net/base/net_util.h"
16 #include "net/base/load_flags.h"
17 #include "net/base/ssl_config_service_defaults.h"
18 #include "net/base/test_completion_callback.h"
19 #include "net/disk_cache/disk_cache.h"
20 #include "net/http/http_cache.h"
21 #include "net/http/http_network_session.h"
22 #include "net/http/http_server_properties_impl.h"
23 #include "net/test/test_server.h"
24 #include "net/url_request/url_request_context_storage.h"
25 #include "net/url_request/url_request_file_job.h"
26 #include "net/url_request/url_request_job_factory_impl.h"
27 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "testing/platform_test.h"
31 namespace net {
33 // TODO(eroman):
34 // - Test canceling an outstanding request.
35 // - Test deleting ProxyScriptFetcher while a request is in progress.
37 namespace {
39 const FilePath::CharType kDocRoot[] =
40 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest");
42 struct FetchResult {
43 int code;
44 string16 text;
47 // A non-mock URL request which can access http:// and file:// urls.
48 class RequestContext : public URLRequestContext {
49 public:
50 RequestContext() : ALLOW_THIS_IN_INITIALIZER_LIST(storage_(this)) {
51 ProxyConfig no_proxy;
52 storage_.set_host_resolver(scoped_ptr<HostResolver>(new MockHostResolver));
53 storage_.set_cert_verifier(new MockCertVerifier);
54 storage_.set_proxy_service(ProxyService::CreateFixed(no_proxy));
55 storage_.set_ssl_config_service(new SSLConfigServiceDefaults);
56 storage_.set_http_server_properties(new HttpServerPropertiesImpl);
58 HttpNetworkSession::Params params;
59 params.host_resolver = host_resolver();
60 params.cert_verifier = cert_verifier();
61 params.proxy_service = proxy_service();
62 params.ssl_config_service = ssl_config_service();
63 params.http_server_properties = http_server_properties();
64 scoped_refptr<HttpNetworkSession> network_session(
65 new HttpNetworkSession(params));
66 storage_.set_http_transaction_factory(new HttpCache(
67 network_session,
68 HttpCache::DefaultBackend::InMemory(0)));
69 storage_.set_job_factory(new URLRequestJobFactoryImpl());
72 virtual ~RequestContext() {
75 private:
76 URLRequestContextStorage storage_;
79 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest.
80 GURL GetTestFileUrl(const std::string& relpath) {
81 FilePath path;
82 PathService::Get(base::DIR_SOURCE_ROOT, &path);
83 path = path.AppendASCII("net");
84 path = path.AppendASCII("data");
85 path = path.AppendASCII("proxy_script_fetcher_unittest");
86 GURL base_url = FilePathToFileURL(path);
87 return GURL(base_url.spec() + "/" + relpath);
90 // Really simple NetworkDelegate so we can allow local file access on ChromeOS
91 // without introducing layering violations. Also causes a test failure if a
92 // request is seen that doesn't set a load flag to bypass revocation checking.
94 class BasicNetworkDelegate : public NetworkDelegate {
95 public:
96 BasicNetworkDelegate() {}
97 virtual ~BasicNetworkDelegate() {}
99 private:
100 virtual int OnBeforeURLRequest(URLRequest* request,
101 const CompletionCallback& callback,
102 GURL* new_url) OVERRIDE {
103 EXPECT_TRUE(request->load_flags() & LOAD_DISABLE_CERT_REVOCATION_CHECKING);
104 return OK;
107 virtual int OnBeforeSendHeaders(URLRequest* request,
108 const CompletionCallback& callback,
109 HttpRequestHeaders* headers) OVERRIDE {
110 return OK;
113 virtual void OnSendHeaders(URLRequest* request,
114 const HttpRequestHeaders& headers) OVERRIDE {}
116 virtual int OnHeadersReceived(
117 URLRequest* request,
118 const CompletionCallback& callback,
119 const HttpResponseHeaders* original_response_headers,
120 scoped_refptr<HttpResponseHeaders>* override_response_headers)
121 OVERRIDE {
122 return OK;
125 virtual void OnBeforeRedirect(URLRequest* request,
126 const GURL& new_location) OVERRIDE {}
128 virtual void OnResponseStarted(URLRequest* request) OVERRIDE {}
130 virtual void OnRawBytesRead(const URLRequest& request,
131 int bytes_read) OVERRIDE {}
133 virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE {}
135 virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE {}
137 virtual void OnPACScriptError(int line_number,
138 const string16& error) OVERRIDE {}
140 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
141 URLRequest* request,
142 const AuthChallengeInfo& auth_info,
143 const AuthCallback& callback,
144 AuthCredentials* credentials) OVERRIDE {
145 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
148 virtual bool OnCanGetCookies(const URLRequest& request,
149 const CookieList& cookie_list) OVERRIDE {
150 return true;
153 virtual bool OnCanSetCookie(const URLRequest& request,
154 const std::string& cookie_line,
155 CookieOptions* options) OVERRIDE {
156 return true;
159 virtual bool OnCanAccessFile(const net::URLRequest& request,
160 const FilePath& path) const OVERRIDE {
161 return true;
163 virtual bool OnCanThrottleRequest(const URLRequest& request) const OVERRIDE {
164 return false;
167 virtual int OnBeforeSocketStreamConnect(
168 SocketStream* stream,
169 const CompletionCallback& callback) OVERRIDE {
170 return OK;
173 virtual void OnRequestWaitStateChange(const net::URLRequest& request,
174 RequestWaitState state) OVERRIDE {
177 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
180 } // namespace
182 class ProxyScriptFetcherImplTest : public PlatformTest {
183 public:
184 ProxyScriptFetcherImplTest()
185 : test_server_(TestServer::TYPE_HTTP,
186 net::TestServer::kLocalhost,
187 FilePath(kDocRoot)) {
188 context_.set_network_delegate(&network_delegate_);
191 protected:
192 TestServer test_server_;
193 BasicNetworkDelegate network_delegate_;
194 RequestContext context_;
197 TEST_F(ProxyScriptFetcherImplTest, FileUrl) {
198 ProxyScriptFetcherImpl pac_fetcher(&context_);
200 { // Fetch a non-existent file.
201 string16 text;
202 TestCompletionCallback callback;
203 int result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist"),
204 &text, callback.callback());
205 EXPECT_EQ(ERR_IO_PENDING, result);
206 EXPECT_EQ(ERR_FILE_NOT_FOUND, callback.WaitForResult());
207 EXPECT_TRUE(text.empty());
209 { // Fetch a file that exists.
210 string16 text;
211 TestCompletionCallback callback;
212 int result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt"),
213 &text, callback.callback());
214 EXPECT_EQ(ERR_IO_PENDING, result);
215 EXPECT_EQ(OK, callback.WaitForResult());
216 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text);
220 // Note that all mime types are allowed for PAC file, to be consistent
221 // with other browsers.
222 TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) {
223 ASSERT_TRUE(test_server_.Start());
225 ProxyScriptFetcherImpl pac_fetcher(&context_);
227 { // Fetch a PAC with mime type "text/plain"
228 GURL url(test_server_.GetURL("files/pac.txt"));
229 string16 text;
230 TestCompletionCallback callback;
231 int result = pac_fetcher.Fetch(url, &text, callback.callback());
232 EXPECT_EQ(ERR_IO_PENDING, result);
233 EXPECT_EQ(OK, callback.WaitForResult());
234 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text);
236 { // Fetch a PAC with mime type "text/html"
237 GURL url(test_server_.GetURL("files/pac.html"));
238 string16 text;
239 TestCompletionCallback callback;
240 int result = pac_fetcher.Fetch(url, &text, callback.callback());
241 EXPECT_EQ(ERR_IO_PENDING, result);
242 EXPECT_EQ(OK, callback.WaitForResult());
243 EXPECT_EQ(ASCIIToUTF16("-pac.html-\n"), text);
245 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig"
246 GURL url(test_server_.GetURL("files/pac.nsproxy"));
247 string16 text;
248 TestCompletionCallback callback;
249 int result = pac_fetcher.Fetch(url, &text, callback.callback());
250 EXPECT_EQ(ERR_IO_PENDING, result);
251 EXPECT_EQ(OK, callback.WaitForResult());
252 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
256 TEST_F(ProxyScriptFetcherImplTest, HttpStatusCode) {
257 ASSERT_TRUE(test_server_.Start());
259 ProxyScriptFetcherImpl pac_fetcher(&context_);
261 { // Fetch a PAC which gives a 500 -- FAIL
262 GURL url(test_server_.GetURL("files/500.pac"));
263 string16 text;
264 TestCompletionCallback callback;
265 int result = pac_fetcher.Fetch(url, &text, callback.callback());
266 EXPECT_EQ(ERR_IO_PENDING, result);
267 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult());
268 EXPECT_TRUE(text.empty());
270 { // Fetch a PAC which gives a 404 -- FAIL
271 GURL url(test_server_.GetURL("files/404.pac"));
272 string16 text;
273 TestCompletionCallback callback;
274 int result = pac_fetcher.Fetch(url, &text, callback.callback());
275 EXPECT_EQ(ERR_IO_PENDING, result);
276 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult());
277 EXPECT_TRUE(text.empty());
281 TEST_F(ProxyScriptFetcherImplTest, ContentDisposition) {
282 ASSERT_TRUE(test_server_.Start());
284 ProxyScriptFetcherImpl pac_fetcher(&context_);
286 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should
287 // have no effect.
288 GURL url(test_server_.GetURL("files/downloadable.pac"));
289 string16 text;
290 TestCompletionCallback callback;
291 int result = pac_fetcher.Fetch(url, &text, callback.callback());
292 EXPECT_EQ(ERR_IO_PENDING, result);
293 EXPECT_EQ(OK, callback.WaitForResult());
294 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text);
297 // Verifies that PAC scripts are not being cached.
298 TEST_F(ProxyScriptFetcherImplTest, NoCache) {
299 ASSERT_TRUE(test_server_.Start());
301 ProxyScriptFetcherImpl pac_fetcher(&context_);
303 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour.
304 GURL url(test_server_.GetURL("files/cacheable_1hr.pac"));
306 string16 text;
307 TestCompletionCallback callback;
308 int result = pac_fetcher.Fetch(url, &text, callback.callback());
309 EXPECT_EQ(ERR_IO_PENDING, result);
310 EXPECT_EQ(OK, callback.WaitForResult());
311 EXPECT_EQ(ASCIIToUTF16("-cacheable_1hr.pac-\n"), text);
314 // Kill the HTTP server.
315 ASSERT_TRUE(test_server_.Stop());
317 // Try to fetch the file again. Since the server is not running anymore, the
318 // call should fail, thus indicating that the file was not fetched from the
319 // local cache.
321 string16 text;
322 TestCompletionCallback callback;
323 int result = pac_fetcher.Fetch(url, &text, callback.callback());
324 EXPECT_EQ(ERR_IO_PENDING, result);
326 #if defined(OS_ANDROID)
327 // On Android platform, the tests are run on the device while the server
328 // runs on the host machine. After killing the server, port forwarder
329 // running on the device is still active, which produces error message
330 // "Connection reset by peer" rather than "Connection refused".
331 EXPECT_EQ(ERR_CONNECTION_RESET, callback.WaitForResult());
332 #else
333 EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult());
334 #endif
338 TEST_F(ProxyScriptFetcherImplTest, TooLarge) {
339 ASSERT_TRUE(test_server_.Start());
341 ProxyScriptFetcherImpl pac_fetcher(&context_);
343 // Set the maximum response size to 50 bytes.
344 int prev_size = pac_fetcher.SetSizeConstraint(50);
346 // These two URLs are the same file, but are http:// vs file://
347 GURL urls[] = {
348 test_server_.GetURL("files/large-pac.nsproxy"),
349 GetTestFileUrl("large-pac.nsproxy")
352 // Try fetching URLs that are 101 bytes large. We should abort the request
353 // after 50 bytes have been read, and fail with a too large error.
354 for (size_t i = 0; i < arraysize(urls); ++i) {
355 const GURL& url = urls[i];
356 string16 text;
357 TestCompletionCallback callback;
358 int result = pac_fetcher.Fetch(url, &text, callback.callback());
359 EXPECT_EQ(ERR_IO_PENDING, result);
360 EXPECT_EQ(ERR_FILE_TOO_BIG, callback.WaitForResult());
361 EXPECT_TRUE(text.empty());
364 // Restore the original size bound.
365 pac_fetcher.SetSizeConstraint(prev_size);
367 { // Make sure we can still fetch regular URLs.
368 GURL url(test_server_.GetURL("files/pac.nsproxy"));
369 string16 text;
370 TestCompletionCallback callback;
371 int result = pac_fetcher.Fetch(url, &text, callback.callback());
372 EXPECT_EQ(ERR_IO_PENDING, result);
373 EXPECT_EQ(OK, callback.WaitForResult());
374 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
378 TEST_F(ProxyScriptFetcherImplTest, Hang) {
379 ASSERT_TRUE(test_server_.Start());
381 ProxyScriptFetcherImpl pac_fetcher(&context_);
383 // Set the timeout period to 0.5 seconds.
384 base::TimeDelta prev_timeout = pac_fetcher.SetTimeoutConstraint(
385 base::TimeDelta::FromMilliseconds(500));
387 // Try fetching a URL which takes 1.2 seconds. We should abort the request
388 // after 500 ms, and fail with a timeout error.
390 GURL url(test_server_.GetURL("slow/proxy.pac?1.2"));
391 string16 text;
392 TestCompletionCallback callback;
393 int result = pac_fetcher.Fetch(url, &text, callback.callback());
394 EXPECT_EQ(ERR_IO_PENDING, result);
395 EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult());
396 EXPECT_TRUE(text.empty());
399 // Restore the original timeout period.
400 pac_fetcher.SetTimeoutConstraint(prev_timeout);
402 { // Make sure we can still fetch regular URLs.
403 GURL url(test_server_.GetURL("files/pac.nsproxy"));
404 string16 text;
405 TestCompletionCallback callback;
406 int result = pac_fetcher.Fetch(url, &text, callback.callback());
407 EXPECT_EQ(ERR_IO_PENDING, result);
408 EXPECT_EQ(OK, callback.WaitForResult());
409 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
413 // The ProxyScriptFetcher should decode any content-codings
414 // (like gzip, bzip, etc.), and apply any charset conversions to yield
415 // UTF8.
416 TEST_F(ProxyScriptFetcherImplTest, Encodings) {
417 ASSERT_TRUE(test_server_.Start());
419 ProxyScriptFetcherImpl pac_fetcher(&context_);
421 // Test a response that is gzip-encoded -- should get inflated.
423 GURL url(test_server_.GetURL("files/gzipped_pac"));
424 string16 text;
425 TestCompletionCallback callback;
426 int result = pac_fetcher.Fetch(url, &text, callback.callback());
427 EXPECT_EQ(ERR_IO_PENDING, result);
428 EXPECT_EQ(OK, callback.WaitForResult());
429 EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text);
432 // Test a response that was served as UTF-16 (BE). It should
433 // be converted to UTF8.
435 GURL url(test_server_.GetURL("files/utf16be_pac"));
436 string16 text;
437 TestCompletionCallback callback;
438 int result = pac_fetcher.Fetch(url, &text, callback.callback());
439 EXPECT_EQ(ERR_IO_PENDING, result);
440 EXPECT_EQ(OK, callback.WaitForResult());
441 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text);
445 TEST_F(ProxyScriptFetcherImplTest, DataURLs) {
446 ProxyScriptFetcherImpl pac_fetcher(&context_);
448 const char kEncodedUrl[] =
449 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
450 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
451 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
452 const char kPacScript[] =
453 "function FindProxyForURL(url, host) {\n"
454 " if (host == 'foobar.com')\n"
455 " return 'PROXY blackhole:80';\n"
456 " return 'DIRECT';\n"
457 "}";
459 // Test fetching a "data:"-url containing a base64 encoded PAC script.
461 GURL url(kEncodedUrl);
462 string16 text;
463 TestCompletionCallback callback;
464 int result = pac_fetcher.Fetch(url, &text, callback.callback());
465 EXPECT_EQ(OK, result);
466 EXPECT_EQ(ASCIIToUTF16(kPacScript), text);
469 const char kEncodedUrlBroken[] =
470 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R";
472 // Test a broken "data:"-url containing a base64 encoded PAC script.
474 GURL url(kEncodedUrlBroken);
475 string16 text;
476 TestCompletionCallback callback;
477 int result = pac_fetcher.Fetch(url, &text, callback.callback());
478 EXPECT_EQ(ERR_FAILED, result);
482 } // namespace net