Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / net / proxy / proxy_script_fetcher_impl_unittest.cc
blob0ffe0c0f2c37d72626557f83dc6d46ae62f78660
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/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"
37 #endif
39 using base::ASCIIToUTF16;
41 // TODO(eroman):
42 // - Test canceling an outstanding request.
43 // - Test deleting ProxyScriptFetcher while a request is in progress.
45 namespace net {
47 namespace {
49 const base::FilePath::CharType kDocRoot[] =
50 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest");
52 struct FetchResult {
53 int code;
54 base::string16 text;
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 {
60 public:
61 RequestContext() : storage_(this) {
62 ProxyConfig no_proxy;
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("file",
85 make_scoped_ptr(new FileProtocolHandler(
86 base::ThreadTaskRunnerHandle::Get())));
87 #endif
88 storage_.set_job_factory(job_factory);
91 ~RequestContext() override { AssertNoURLRequests(); }
93 private:
94 URLRequestContextStorage storage_;
97 #if !defined(DISABLE_FILE_SUPPORT)
98 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest.
99 GURL GetTestFileUrl(const std::string& relpath) {
100 base::FilePath path;
101 PathService::Get(base::DIR_SOURCE_ROOT, &path);
102 path = path.AppendASCII("net");
103 path = path.AppendASCII("data");
104 path = path.AppendASCII("proxy_script_fetcher_unittest");
105 GURL base_url = FilePathToFileURL(path);
106 return GURL(base_url.spec() + "/" + relpath);
108 #endif // !defined(DISABLE_FILE_SUPPORT)
110 // Really simple NetworkDelegate so we can allow local file access on ChromeOS
111 // without introducing layering violations. Also causes a test failure if a
112 // request is seen that doesn't set a load flag to bypass revocation checking.
114 class BasicNetworkDelegate : public NetworkDelegateImpl {
115 public:
116 BasicNetworkDelegate() {}
117 ~BasicNetworkDelegate() override {}
119 private:
120 int OnBeforeURLRequest(URLRequest* request,
121 const CompletionCallback& callback,
122 GURL* new_url) override {
123 EXPECT_TRUE(request->load_flags() & LOAD_DISABLE_CERT_REVOCATION_CHECKING);
124 return OK;
127 int OnBeforeSendHeaders(URLRequest* request,
128 const CompletionCallback& callback,
129 HttpRequestHeaders* headers) override {
130 return OK;
133 void OnSendHeaders(URLRequest* request,
134 const HttpRequestHeaders& headers) override {}
136 int OnHeadersReceived(
137 URLRequest* request,
138 const CompletionCallback& callback,
139 const HttpResponseHeaders* original_response_headers,
140 scoped_refptr<HttpResponseHeaders>* override_response_headers,
141 GURL* allowed_unsafe_redirect_url) override {
142 return OK;
145 void OnBeforeRedirect(URLRequest* request,
146 const GURL& new_location) override {}
148 void OnResponseStarted(URLRequest* request) override {}
150 void OnCompleted(URLRequest* request, bool started) override {}
152 void OnURLRequestDestroyed(URLRequest* request) override {}
154 void OnPACScriptError(int line_number, const base::string16& error) override {
157 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
158 URLRequest* request,
159 const AuthChallengeInfo& auth_info,
160 const AuthCallback& callback,
161 AuthCredentials* credentials) override {
162 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
165 bool OnCanGetCookies(const URLRequest& request,
166 const CookieList& cookie_list) override {
167 return true;
170 bool OnCanSetCookie(const URLRequest& request,
171 const std::string& cookie_line,
172 CookieOptions* options) override {
173 return true;
176 bool OnCanAccessFile(const URLRequest& request,
177 const base::FilePath& path) const override {
178 return true;
181 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
184 class ProxyScriptFetcherImplTest : public PlatformTest {
185 public:
186 ProxyScriptFetcherImplTest()
187 : test_server_(SpawnedTestServer::TYPE_HTTP,
188 SpawnedTestServer::kLocalhost,
189 base::FilePath(kDocRoot)) {
190 context_.set_network_delegate(&network_delegate_);
193 protected:
194 SpawnedTestServer test_server_;
195 BasicNetworkDelegate network_delegate_;
196 RequestContext context_;
199 #if !defined(DISABLE_FILE_SUPPORT)
200 TEST_F(ProxyScriptFetcherImplTest, FileUrl) {
201 ProxyScriptFetcherImpl pac_fetcher(&context_);
203 { // Fetch a non-existent file.
204 base::string16 text;
205 TestCompletionCallback callback;
206 int result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist"),
207 &text, callback.callback());
208 EXPECT_EQ(ERR_IO_PENDING, result);
209 EXPECT_EQ(ERR_FILE_NOT_FOUND, callback.WaitForResult());
210 EXPECT_TRUE(text.empty());
212 { // Fetch a file that exists.
213 base::string16 text;
214 TestCompletionCallback callback;
215 int result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt"),
216 &text, callback.callback());
217 EXPECT_EQ(ERR_IO_PENDING, result);
218 EXPECT_EQ(OK, callback.WaitForResult());
219 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text);
222 #endif // !defined(DISABLE_FILE_SUPPORT)
224 // Note that all mime types are allowed for PAC file, to be consistent
225 // with other browsers.
226 TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) {
227 ASSERT_TRUE(test_server_.Start());
229 ProxyScriptFetcherImpl pac_fetcher(&context_);
231 { // Fetch a PAC with mime type "text/plain"
232 GURL url(test_server_.GetURL("files/pac.txt"));
233 base::string16 text;
234 TestCompletionCallback callback;
235 int result = pac_fetcher.Fetch(url, &text, callback.callback());
236 EXPECT_EQ(ERR_IO_PENDING, result);
237 EXPECT_EQ(OK, callback.WaitForResult());
238 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text);
240 { // Fetch a PAC with mime type "text/html"
241 GURL url(test_server_.GetURL("files/pac.html"));
242 base::string16 text;
243 TestCompletionCallback callback;
244 int result = pac_fetcher.Fetch(url, &text, callback.callback());
245 EXPECT_EQ(ERR_IO_PENDING, result);
246 EXPECT_EQ(OK, callback.WaitForResult());
247 EXPECT_EQ(ASCIIToUTF16("-pac.html-\n"), text);
249 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig"
250 GURL url(test_server_.GetURL("files/pac.nsproxy"));
251 base::string16 text;
252 TestCompletionCallback callback;
253 int result = pac_fetcher.Fetch(url, &text, callback.callback());
254 EXPECT_EQ(ERR_IO_PENDING, result);
255 EXPECT_EQ(OK, callback.WaitForResult());
256 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
260 TEST_F(ProxyScriptFetcherImplTest, HttpStatusCode) {
261 ASSERT_TRUE(test_server_.Start());
263 ProxyScriptFetcherImpl pac_fetcher(&context_);
265 { // Fetch a PAC which gives a 500 -- FAIL
266 GURL url(test_server_.GetURL("files/500.pac"));
267 base::string16 text;
268 TestCompletionCallback callback;
269 int result = pac_fetcher.Fetch(url, &text, callback.callback());
270 EXPECT_EQ(ERR_IO_PENDING, result);
271 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult());
272 EXPECT_TRUE(text.empty());
274 { // Fetch a PAC which gives a 404 -- FAIL
275 GURL url(test_server_.GetURL("files/404.pac"));
276 base::string16 text;
277 TestCompletionCallback callback;
278 int result = pac_fetcher.Fetch(url, &text, callback.callback());
279 EXPECT_EQ(ERR_IO_PENDING, result);
280 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult());
281 EXPECT_TRUE(text.empty());
285 TEST_F(ProxyScriptFetcherImplTest, ContentDisposition) {
286 ASSERT_TRUE(test_server_.Start());
288 ProxyScriptFetcherImpl pac_fetcher(&context_);
290 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should
291 // have no effect.
292 GURL url(test_server_.GetURL("files/downloadable.pac"));
293 base::string16 text;
294 TestCompletionCallback callback;
295 int result = pac_fetcher.Fetch(url, &text, callback.callback());
296 EXPECT_EQ(ERR_IO_PENDING, result);
297 EXPECT_EQ(OK, callback.WaitForResult());
298 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text);
301 // Verifies that PAC scripts are not being cached.
302 TEST_F(ProxyScriptFetcherImplTest, NoCache) {
303 ASSERT_TRUE(test_server_.Start());
305 ProxyScriptFetcherImpl pac_fetcher(&context_);
307 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour.
308 GURL url(test_server_.GetURL("files/cacheable_1hr.pac"));
310 base::string16 text;
311 TestCompletionCallback callback;
312 int result = pac_fetcher.Fetch(url, &text, callback.callback());
313 EXPECT_EQ(ERR_IO_PENDING, result);
314 EXPECT_EQ(OK, callback.WaitForResult());
315 EXPECT_EQ(ASCIIToUTF16("-cacheable_1hr.pac-\n"), text);
318 // Kill the HTTP server.
319 ASSERT_TRUE(test_server_.Stop());
321 // Try to fetch the file again. Since the server is not running anymore, the
322 // call should fail, thus indicating that the file was not fetched from the
323 // local cache.
325 base::string16 text;
326 TestCompletionCallback callback;
327 int result = pac_fetcher.Fetch(url, &text, callback.callback());
328 EXPECT_EQ(ERR_IO_PENDING, result);
330 // Expect any error. The exact error varies by platform.
331 EXPECT_NE(OK, callback.WaitForResult());
335 TEST_F(ProxyScriptFetcherImplTest, TooLarge) {
336 ASSERT_TRUE(test_server_.Start());
338 ProxyScriptFetcherImpl pac_fetcher(&context_);
340 // Set the maximum response size to 50 bytes.
341 int prev_size = pac_fetcher.SetSizeConstraint(50);
343 // These two URLs are the same file, but are http:// vs file://
344 GURL urls[] = {
345 test_server_.GetURL("files/large-pac.nsproxy"),
346 #if !defined(DISABLE_FILE_SUPPORT)
347 GetTestFileUrl("large-pac.nsproxy")
348 #endif
351 // Try fetching URLs that are 101 bytes large. We should abort the request
352 // after 50 bytes have been read, and fail with a too large error.
353 for (size_t i = 0; i < arraysize(urls); ++i) {
354 const GURL& url = urls[i];
355 base::string16 text;
356 TestCompletionCallback callback;
357 int result = pac_fetcher.Fetch(url, &text, callback.callback());
358 EXPECT_EQ(ERR_IO_PENDING, result);
359 EXPECT_EQ(ERR_FILE_TOO_BIG, callback.WaitForResult());
360 EXPECT_TRUE(text.empty());
363 // Restore the original size bound.
364 pac_fetcher.SetSizeConstraint(prev_size);
366 { // Make sure we can still fetch regular URLs.
367 GURL url(test_server_.GetURL("files/pac.nsproxy"));
368 base::string16 text;
369 TestCompletionCallback callback;
370 int result = pac_fetcher.Fetch(url, &text, callback.callback());
371 EXPECT_EQ(ERR_IO_PENDING, result);
372 EXPECT_EQ(OK, callback.WaitForResult());
373 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
377 TEST_F(ProxyScriptFetcherImplTest, Hang) {
378 ASSERT_TRUE(test_server_.Start());
380 ProxyScriptFetcherImpl pac_fetcher(&context_);
382 // Set the timeout period to 0.5 seconds.
383 base::TimeDelta prev_timeout = pac_fetcher.SetTimeoutConstraint(
384 base::TimeDelta::FromMilliseconds(500));
386 // Try fetching a URL which takes 1.2 seconds. We should abort the request
387 // after 500 ms, and fail with a timeout error.
389 GURL url(test_server_.GetURL("slow/proxy.pac?1.2"));
390 base::string16 text;
391 TestCompletionCallback callback;
392 int result = pac_fetcher.Fetch(url, &text, callback.callback());
393 EXPECT_EQ(ERR_IO_PENDING, result);
394 EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult());
395 EXPECT_TRUE(text.empty());
398 // Restore the original timeout period.
399 pac_fetcher.SetTimeoutConstraint(prev_timeout);
401 { // Make sure we can still fetch regular URLs.
402 GURL url(test_server_.GetURL("files/pac.nsproxy"));
403 base::string16 text;
404 TestCompletionCallback callback;
405 int result = pac_fetcher.Fetch(url, &text, callback.callback());
406 EXPECT_EQ(ERR_IO_PENDING, result);
407 EXPECT_EQ(OK, callback.WaitForResult());
408 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
412 // The ProxyScriptFetcher should decode any content-codings
413 // (like gzip, bzip, etc.), and apply any charset conversions to yield
414 // UTF8.
415 TEST_F(ProxyScriptFetcherImplTest, Encodings) {
416 ASSERT_TRUE(test_server_.Start());
418 ProxyScriptFetcherImpl pac_fetcher(&context_);
420 // Test a response that is gzip-encoded -- should get inflated.
422 GURL url(test_server_.GetURL("files/gzipped_pac"));
423 base::string16 text;
424 TestCompletionCallback callback;
425 int result = pac_fetcher.Fetch(url, &text, callback.callback());
426 EXPECT_EQ(ERR_IO_PENDING, result);
427 EXPECT_EQ(OK, callback.WaitForResult());
428 EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text);
431 // Test a response that was served as UTF-16 (BE). It should
432 // be converted to UTF8.
434 GURL url(test_server_.GetURL("files/utf16be_pac"));
435 base::string16 text;
436 TestCompletionCallback callback;
437 int result = pac_fetcher.Fetch(url, &text, callback.callback());
438 EXPECT_EQ(ERR_IO_PENDING, result);
439 EXPECT_EQ(OK, callback.WaitForResult());
440 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text);
444 TEST_F(ProxyScriptFetcherImplTest, DataURLs) {
445 ProxyScriptFetcherImpl pac_fetcher(&context_);
447 const char kEncodedUrl[] =
448 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
449 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
450 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
451 const char kPacScript[] =
452 "function FindProxyForURL(url, host) {\n"
453 " if (host == 'foobar.com')\n"
454 " return 'PROXY blackhole:80';\n"
455 " return 'DIRECT';\n"
456 "}";
458 // Test fetching a "data:"-url containing a base64 encoded PAC script.
460 GURL url(kEncodedUrl);
461 base::string16 text;
462 TestCompletionCallback callback;
463 int result = pac_fetcher.Fetch(url, &text, callback.callback());
464 EXPECT_EQ(OK, result);
465 EXPECT_EQ(ASCIIToUTF16(kPacScript), text);
468 const char kEncodedUrlBroken[] =
469 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R";
471 // Test a broken "data:"-url containing a base64 encoded PAC script.
473 GURL url(kEncodedUrlBroken);
474 base::string16 text;
475 TestCompletionCallback callback;
476 int result = pac_fetcher.Fetch(url, &text, callback.callback());
477 EXPECT_EQ(ERR_FAILED, result);
481 } // namespace
483 } // namespace net