Remove expectation for rebaselined test.
[chromium-blink-merge.git] / net / proxy / proxy_script_fetcher_impl_unittest.cc
blob1c89b3caaf3b91df4e379e1a9b510b56179ae644
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/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"
33 namespace net {
35 // TODO(eroman):
36 // - Test canceling an outstanding request.
37 // - Test deleting ProxyScriptFetcher while a request is in progress.
39 namespace {
41 const base::FilePath::CharType kDocRoot[] =
42 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest");
44 struct FetchResult {
45 int code;
46 base::string16 text;
49 // A non-mock URL request which can access http:// and file:// urls.
50 class RequestContext : public URLRequestContext {
51 public:
52 RequestContext() : storage_(this) {
53 ProxyConfig no_proxy;
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() {
80 private:
81 URLRequestContextStorage storage_;
84 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest.
85 GURL GetTestFileUrl(const std::string& relpath) {
86 base::FilePath path;
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 {
100 public:
101 BasicNetworkDelegate() {}
102 virtual ~BasicNetworkDelegate() {}
104 private:
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);
109 return OK;
112 virtual int OnBeforeSendHeaders(URLRequest* request,
113 const CompletionCallback& callback,
114 HttpRequestHeaders* headers) OVERRIDE {
115 return OK;
118 virtual void OnSendHeaders(URLRequest* request,
119 const HttpRequestHeaders& headers) OVERRIDE {}
121 virtual int OnHeadersReceived(
122 URLRequest* request,
123 const CompletionCallback& callback,
124 const HttpResponseHeaders* original_response_headers,
125 scoped_refptr<HttpResponseHeaders>* override_response_headers)
126 OVERRIDE {
127 return OK;
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(
146 URLRequest* request,
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 {
155 return true;
158 virtual bool OnCanSetCookie(const URLRequest& request,
159 const std::string& cookie_line,
160 CookieOptions* options) OVERRIDE {
161 return true;
164 virtual bool OnCanAccessFile(const net::URLRequest& request,
165 const base::FilePath& path) const OVERRIDE {
166 return true;
168 virtual bool OnCanThrottleRequest(const URLRequest& request) const OVERRIDE {
169 return false;
172 virtual int OnBeforeSocketStreamConnect(
173 SocketStream* stream,
174 const CompletionCallback& callback) OVERRIDE {
175 return OK;
178 virtual void OnRequestWaitStateChange(const net::URLRequest& request,
179 RequestWaitState state) OVERRIDE {
182 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
185 } // namespace
187 class ProxyScriptFetcherImplTest : public PlatformTest {
188 public:
189 ProxyScriptFetcherImplTest()
190 : test_server_(SpawnedTestServer::TYPE_HTTP,
191 net::SpawnedTestServer::kLocalhost,
192 base::FilePath(kDocRoot)) {
193 context_.set_network_delegate(&network_delegate_);
196 protected:
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.
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);
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"));
234 base::string16 text;
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"));
243 base::string16 text;
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"));
252 base::string16 text;
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"));
268 base::string16 text;
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"));
277 base::string16 text;
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
292 // have no effect.
293 GURL url(test_server_.GetURL("files/downloadable.pac"));
294 base::string16 text;
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"));
311 base::string16 text;
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
324 // local cache.
326 base::string16 text;
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());
337 #else
338 EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult());
339 #endif
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://
352 GURL urls[] = {
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];
361 base::string16 text;
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"));
374 base::string16 text;
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"));
396 base::string16 text;
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"));
409 base::string16 text;
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
420 // UTF8.
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"));
429 base::string16 text;
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"));
441 base::string16 text;
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"
462 "}";
464 // Test fetching a "data:"-url containing a base64 encoded PAC script.
466 GURL url(kEncodedUrl);
467 base::string16 text;
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);
480 base::string16 text;
481 TestCompletionCallback callback;
482 int result = pac_fetcher.Fetch(url, &text, callback.callback());
483 EXPECT_EQ(ERR_FAILED, result);
487 } // namespace net