Add the first GYP-based bots to MB (blink android trybots).
[chromium-blink-merge.git] / net / proxy / proxy_script_fetcher_impl_unittest.cc
blob5046db367b35b784d84d579fb40ed271810d50a0
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/strings/utf_string_conversions.h"
13 #include "net/base/filename_util.h"
14 #include "net/base/load_flags.h"
15 #include "net/base/network_delegate_impl.h"
16 #include "net/base/test_completion_callback.h"
17 #include "net/cert/mock_cert_verifier.h"
18 #include "net/disk_cache/disk_cache.h"
19 #include "net/dns/mock_host_resolver.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/http/transport_security_state.h"
24 #include "net/ssl/ssl_config_service_defaults.h"
25 #include "net/test/spawned_test_server/spawned_test_server.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"
33 #if !defined(DISABLE_FILE_SUPPORT)
34 #include "net/url_request/file_protocol_handler.h"
35 #endif
37 using base::ASCIIToUTF16;
39 // TODO(eroman):
40 // - Test canceling an outstanding request.
41 // - Test deleting ProxyScriptFetcher while a request is in progress.
43 namespace net {
45 namespace {
47 const base::FilePath::CharType kDocRoot[] =
48 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest");
50 struct FetchResult {
51 int code;
52 base::string16 text;
55 // A non-mock URL request which can access http:// and file:// urls, in the case
56 // the tests were built with file support.
57 class RequestContext : public URLRequestContext {
58 public:
59 RequestContext() : storage_(this) {
60 ProxyConfig no_proxy;
61 storage_.set_host_resolver(scoped_ptr<HostResolver>(new MockHostResolver));
62 storage_.set_cert_verifier(new MockCertVerifier);
63 storage_.set_transport_security_state(new TransportSecurityState);
64 storage_.set_proxy_service(ProxyService::CreateFixed(no_proxy));
65 storage_.set_ssl_config_service(new SSLConfigServiceDefaults);
66 storage_.set_http_server_properties(
67 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
69 HttpNetworkSession::Params params;
70 params.host_resolver = host_resolver();
71 params.cert_verifier = cert_verifier();
72 params.transport_security_state = transport_security_state();
73 params.proxy_service = proxy_service();
74 params.ssl_config_service = ssl_config_service();
75 params.http_server_properties = http_server_properties();
76 scoped_refptr<HttpNetworkSession> network_session(
77 new HttpNetworkSession(params));
78 storage_.set_http_transaction_factory(new HttpCache(
79 network_session.get(), HttpCache::DefaultBackend::InMemory(0)));
80 URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl();
81 #if !defined(DISABLE_FILE_SUPPORT)
82 job_factory->SetProtocolHandler(
83 "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
84 #endif
85 storage_.set_job_factory(job_factory);
88 ~RequestContext() override { AssertNoURLRequests(); }
90 private:
91 URLRequestContextStorage storage_;
94 #if !defined(DISABLE_FILE_SUPPORT)
95 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest.
96 GURL GetTestFileUrl(const std::string& relpath) {
97 base::FilePath path;
98 PathService::Get(base::DIR_SOURCE_ROOT, &path);
99 path = path.AppendASCII("net");
100 path = path.AppendASCII("data");
101 path = path.AppendASCII("proxy_script_fetcher_unittest");
102 GURL base_url = FilePathToFileURL(path);
103 return GURL(base_url.spec() + "/" + relpath);
105 #endif // !defined(DISABLE_FILE_SUPPORT)
107 // Really simple NetworkDelegate so we can allow local file access on ChromeOS
108 // without introducing layering violations. Also causes a test failure if a
109 // request is seen that doesn't set a load flag to bypass revocation checking.
111 class BasicNetworkDelegate : public NetworkDelegateImpl {
112 public:
113 BasicNetworkDelegate() {}
114 ~BasicNetworkDelegate() override {}
116 private:
117 int OnBeforeURLRequest(URLRequest* request,
118 const CompletionCallback& callback,
119 GURL* new_url) override {
120 EXPECT_TRUE(request->load_flags() & LOAD_DISABLE_CERT_REVOCATION_CHECKING);
121 return OK;
124 int OnBeforeSendHeaders(URLRequest* request,
125 const CompletionCallback& callback,
126 HttpRequestHeaders* headers) override {
127 return OK;
130 void OnSendHeaders(URLRequest* request,
131 const HttpRequestHeaders& headers) override {}
133 int OnHeadersReceived(
134 URLRequest* request,
135 const CompletionCallback& callback,
136 const HttpResponseHeaders* original_response_headers,
137 scoped_refptr<HttpResponseHeaders>* override_response_headers,
138 GURL* allowed_unsafe_redirect_url) override {
139 return OK;
142 void OnBeforeRedirect(URLRequest* request,
143 const GURL& new_location) override {}
145 void OnResponseStarted(URLRequest* request) override {}
147 void OnRawBytesRead(const URLRequest& request, int bytes_read) override {}
149 void OnCompleted(URLRequest* request, bool started) override {}
151 void OnURLRequestDestroyed(URLRequest* request) override {}
153 void OnPACScriptError(int line_number, const base::string16& error) override {
156 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
157 URLRequest* request,
158 const AuthChallengeInfo& auth_info,
159 const AuthCallback& callback,
160 AuthCredentials* credentials) override {
161 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
164 bool OnCanGetCookies(const URLRequest& request,
165 const CookieList& cookie_list) override {
166 return true;
169 bool OnCanSetCookie(const URLRequest& request,
170 const std::string& cookie_line,
171 CookieOptions* options) override {
172 return true;
175 bool OnCanAccessFile(const URLRequest& request,
176 const base::FilePath& path) const override {
177 return true;
179 bool OnCanThrottleRequest(const URLRequest& request) const override {
180 return false;
183 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
186 class ProxyScriptFetcherImplTest : public PlatformTest {
187 public:
188 ProxyScriptFetcherImplTest()
189 : test_server_(SpawnedTestServer::TYPE_HTTP,
190 SpawnedTestServer::kLocalhost,
191 base::FilePath(kDocRoot)) {
192 context_.set_network_delegate(&network_delegate_);
195 protected:
196 SpawnedTestServer test_server_;
197 BasicNetworkDelegate network_delegate_;
198 RequestContext context_;
201 #if !defined(DISABLE_FILE_SUPPORT)
202 TEST_F(ProxyScriptFetcherImplTest, FileUrl) {
203 ProxyScriptFetcherImpl pac_fetcher(&context_);
205 { // Fetch a non-existent file.
206 base::string16 text;
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.
215 base::string16 text;
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);
224 #endif // !defined(DISABLE_FILE_SUPPORT)
226 // Note that all mime types are allowed for PAC file, to be consistent
227 // with other browsers.
228 TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) {
229 ASSERT_TRUE(test_server_.Start());
231 ProxyScriptFetcherImpl pac_fetcher(&context_);
233 { // Fetch a PAC with mime type "text/plain"
234 GURL url(test_server_.GetURL("files/pac.txt"));
235 base::string16 text;
236 TestCompletionCallback callback;
237 int result = pac_fetcher.Fetch(url, &text, callback.callback());
238 EXPECT_EQ(ERR_IO_PENDING, result);
239 EXPECT_EQ(OK, callback.WaitForResult());
240 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text);
242 { // Fetch a PAC with mime type "text/html"
243 GURL url(test_server_.GetURL("files/pac.html"));
244 base::string16 text;
245 TestCompletionCallback callback;
246 int result = pac_fetcher.Fetch(url, &text, callback.callback());
247 EXPECT_EQ(ERR_IO_PENDING, result);
248 EXPECT_EQ(OK, callback.WaitForResult());
249 EXPECT_EQ(ASCIIToUTF16("-pac.html-\n"), text);
251 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig"
252 GURL url(test_server_.GetURL("files/pac.nsproxy"));
253 base::string16 text;
254 TestCompletionCallback callback;
255 int result = pac_fetcher.Fetch(url, &text, callback.callback());
256 EXPECT_EQ(ERR_IO_PENDING, result);
257 EXPECT_EQ(OK, callback.WaitForResult());
258 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
262 TEST_F(ProxyScriptFetcherImplTest, HttpStatusCode) {
263 ASSERT_TRUE(test_server_.Start());
265 ProxyScriptFetcherImpl pac_fetcher(&context_);
267 { // Fetch a PAC which gives a 500 -- FAIL
268 GURL url(test_server_.GetURL("files/500.pac"));
269 base::string16 text;
270 TestCompletionCallback callback;
271 int result = pac_fetcher.Fetch(url, &text, callback.callback());
272 EXPECT_EQ(ERR_IO_PENDING, result);
273 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult());
274 EXPECT_TRUE(text.empty());
276 { // Fetch a PAC which gives a 404 -- FAIL
277 GURL url(test_server_.GetURL("files/404.pac"));
278 base::string16 text;
279 TestCompletionCallback callback;
280 int result = pac_fetcher.Fetch(url, &text, callback.callback());
281 EXPECT_EQ(ERR_IO_PENDING, result);
282 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult());
283 EXPECT_TRUE(text.empty());
287 TEST_F(ProxyScriptFetcherImplTest, ContentDisposition) {
288 ASSERT_TRUE(test_server_.Start());
290 ProxyScriptFetcherImpl pac_fetcher(&context_);
292 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should
293 // have no effect.
294 GURL url(test_server_.GetURL("files/downloadable.pac"));
295 base::string16 text;
296 TestCompletionCallback callback;
297 int result = pac_fetcher.Fetch(url, &text, callback.callback());
298 EXPECT_EQ(ERR_IO_PENDING, result);
299 EXPECT_EQ(OK, callback.WaitForResult());
300 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text);
303 // Verifies that PAC scripts are not being cached.
304 TEST_F(ProxyScriptFetcherImplTest, NoCache) {
305 ASSERT_TRUE(test_server_.Start());
307 ProxyScriptFetcherImpl pac_fetcher(&context_);
309 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour.
310 GURL url(test_server_.GetURL("files/cacheable_1hr.pac"));
312 base::string16 text;
313 TestCompletionCallback callback;
314 int result = pac_fetcher.Fetch(url, &text, callback.callback());
315 EXPECT_EQ(ERR_IO_PENDING, result);
316 EXPECT_EQ(OK, callback.WaitForResult());
317 EXPECT_EQ(ASCIIToUTF16("-cacheable_1hr.pac-\n"), text);
320 // Kill the HTTP server.
321 ASSERT_TRUE(test_server_.Stop());
323 // Try to fetch the file again. Since the server is not running anymore, the
324 // call should fail, thus indicating that the file was not fetched from the
325 // local cache.
327 base::string16 text;
328 TestCompletionCallback callback;
329 int result = pac_fetcher.Fetch(url, &text, callback.callback());
330 EXPECT_EQ(ERR_IO_PENDING, result);
332 // Expect any error. The exact error varies by platform.
333 EXPECT_NE(OK, callback.WaitForResult());
337 TEST_F(ProxyScriptFetcherImplTest, TooLarge) {
338 ASSERT_TRUE(test_server_.Start());
340 ProxyScriptFetcherImpl pac_fetcher(&context_);
342 // Set the maximum response size to 50 bytes.
343 int prev_size = pac_fetcher.SetSizeConstraint(50);
345 // These two URLs are the same file, but are http:// vs file://
346 GURL urls[] = {
347 test_server_.GetURL("files/large-pac.nsproxy"),
348 #if !defined(DISABLE_FILE_SUPPORT)
349 GetTestFileUrl("large-pac.nsproxy")
350 #endif
353 // Try fetching URLs that are 101 bytes large. We should abort the request
354 // after 50 bytes have been read, and fail with a too large error.
355 for (size_t i = 0; i < arraysize(urls); ++i) {
356 const GURL& url = urls[i];
357 base::string16 text;
358 TestCompletionCallback callback;
359 int result = pac_fetcher.Fetch(url, &text, callback.callback());
360 EXPECT_EQ(ERR_IO_PENDING, result);
361 EXPECT_EQ(ERR_FILE_TOO_BIG, callback.WaitForResult());
362 EXPECT_TRUE(text.empty());
365 // Restore the original size bound.
366 pac_fetcher.SetSizeConstraint(prev_size);
368 { // Make sure we can still fetch regular URLs.
369 GURL url(test_server_.GetURL("files/pac.nsproxy"));
370 base::string16 text;
371 TestCompletionCallback callback;
372 int result = pac_fetcher.Fetch(url, &text, callback.callback());
373 EXPECT_EQ(ERR_IO_PENDING, result);
374 EXPECT_EQ(OK, callback.WaitForResult());
375 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
379 TEST_F(ProxyScriptFetcherImplTest, Hang) {
380 ASSERT_TRUE(test_server_.Start());
382 ProxyScriptFetcherImpl pac_fetcher(&context_);
384 // Set the timeout period to 0.5 seconds.
385 base::TimeDelta prev_timeout = pac_fetcher.SetTimeoutConstraint(
386 base::TimeDelta::FromMilliseconds(500));
388 // Try fetching a URL which takes 1.2 seconds. We should abort the request
389 // after 500 ms, and fail with a timeout error.
391 GURL url(test_server_.GetURL("slow/proxy.pac?1.2"));
392 base::string16 text;
393 TestCompletionCallback callback;
394 int result = pac_fetcher.Fetch(url, &text, callback.callback());
395 EXPECT_EQ(ERR_IO_PENDING, result);
396 EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult());
397 EXPECT_TRUE(text.empty());
400 // Restore the original timeout period.
401 pac_fetcher.SetTimeoutConstraint(prev_timeout);
403 { // Make sure we can still fetch regular URLs.
404 GURL url(test_server_.GetURL("files/pac.nsproxy"));
405 base::string16 text;
406 TestCompletionCallback callback;
407 int result = pac_fetcher.Fetch(url, &text, callback.callback());
408 EXPECT_EQ(ERR_IO_PENDING, result);
409 EXPECT_EQ(OK, callback.WaitForResult());
410 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
414 // The ProxyScriptFetcher should decode any content-codings
415 // (like gzip, bzip, etc.), and apply any charset conversions to yield
416 // UTF8.
417 TEST_F(ProxyScriptFetcherImplTest, Encodings) {
418 ASSERT_TRUE(test_server_.Start());
420 ProxyScriptFetcherImpl pac_fetcher(&context_);
422 // Test a response that is gzip-encoded -- should get inflated.
424 GURL url(test_server_.GetURL("files/gzipped_pac"));
425 base::string16 text;
426 TestCompletionCallback callback;
427 int result = pac_fetcher.Fetch(url, &text, callback.callback());
428 EXPECT_EQ(ERR_IO_PENDING, result);
429 EXPECT_EQ(OK, callback.WaitForResult());
430 EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text);
433 // Test a response that was served as UTF-16 (BE). It should
434 // be converted to UTF8.
436 GURL url(test_server_.GetURL("files/utf16be_pac"));
437 base::string16 text;
438 TestCompletionCallback callback;
439 int result = pac_fetcher.Fetch(url, &text, callback.callback());
440 EXPECT_EQ(ERR_IO_PENDING, result);
441 EXPECT_EQ(OK, callback.WaitForResult());
442 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text);
446 TEST_F(ProxyScriptFetcherImplTest, DataURLs) {
447 ProxyScriptFetcherImpl pac_fetcher(&context_);
449 const char kEncodedUrl[] =
450 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
451 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
452 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
453 const char kPacScript[] =
454 "function FindProxyForURL(url, host) {\n"
455 " if (host == 'foobar.com')\n"
456 " return 'PROXY blackhole:80';\n"
457 " return 'DIRECT';\n"
458 "}";
460 // Test fetching a "data:"-url containing a base64 encoded PAC script.
462 GURL url(kEncodedUrl);
463 base::string16 text;
464 TestCompletionCallback callback;
465 int result = pac_fetcher.Fetch(url, &text, callback.callback());
466 EXPECT_EQ(OK, result);
467 EXPECT_EQ(ASCIIToUTF16(kPacScript), text);
470 const char kEncodedUrlBroken[] =
471 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R";
473 // Test a broken "data:"-url containing a base64 encoded PAC script.
475 GURL url(kEncodedUrlBroken);
476 base::string16 text;
477 TestCompletionCallback callback;
478 int result = pac_fetcher.Fetch(url, &text, callback.callback());
479 EXPECT_EQ(ERR_FAILED, result);
483 } // namespace
485 } // namespace net