proxy_script_fetcher_impl_unittest.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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.
4
5#include "net/proxy/proxy_script_fetcher_impl.h"
6
7#include <string>
8
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/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"
32
33using base::ASCIIToUTF16;
34
35namespace net {
36
37// TODO(eroman):
38//   - Test canceling an outstanding request.
39//   - Test deleting ProxyScriptFetcher while a request is in progress.
40
41namespace {
42
43const base::FilePath::CharType kDocRoot[] =
44    FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest");
45
46struct FetchResult {
47  int code;
48  base::string16 text;
49};
50
51// A non-mock URL request which can access http:// and file:// urls.
52class RequestContext : public URLRequestContext {
53 public:
54  RequestContext() : storage_(this) {
55    ProxyConfig no_proxy;
56    storage_.set_host_resolver(scoped_ptr<HostResolver>(new MockHostResolver));
57    storage_.set_cert_verifier(new MockCertVerifier);
58    storage_.set_transport_security_state(new TransportSecurityState);
59    storage_.set_proxy_service(ProxyService::CreateFixed(no_proxy));
60    storage_.set_ssl_config_service(new SSLConfigServiceDefaults);
61    storage_.set_http_server_properties(
62        scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
63
64    HttpNetworkSession::Params params;
65    params.host_resolver = host_resolver();
66    params.cert_verifier = cert_verifier();
67    params.transport_security_state = transport_security_state();
68    params.proxy_service = proxy_service();
69    params.ssl_config_service = ssl_config_service();
70    params.http_server_properties = http_server_properties();
71    scoped_refptr<HttpNetworkSession> network_session(
72        new HttpNetworkSession(params));
73    storage_.set_http_transaction_factory(new HttpCache(
74        network_session.get(), HttpCache::DefaultBackend::InMemory(0)));
75    URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl();
76    job_factory->SetProtocolHandler(
77        "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
78    storage_.set_job_factory(job_factory);
79  }
80
81  virtual ~RequestContext() {
82  }
83
84 private:
85  URLRequestContextStorage storage_;
86};
87
88// Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest.
89GURL GetTestFileUrl(const std::string& relpath) {
90  base::FilePath path;
91  PathService::Get(base::DIR_SOURCE_ROOT, &path);
92  path = path.AppendASCII("net");
93  path = path.AppendASCII("data");
94  path = path.AppendASCII("proxy_script_fetcher_unittest");
95  GURL base_url = FilePathToFileURL(path);
96  return GURL(base_url.spec() + "/" + relpath);
97}
98
99// Really simple NetworkDelegate so we can allow local file access on ChromeOS
100// without introducing layering violations.  Also causes a test failure if a
101// request is seen that doesn't set a load flag to bypass revocation checking.
102
103class BasicNetworkDelegate : public NetworkDelegate {
104 public:
105  BasicNetworkDelegate() {}
106  virtual ~BasicNetworkDelegate() {}
107
108 private:
109  virtual int OnBeforeURLRequest(URLRequest* request,
110                                 const CompletionCallback& callback,
111                                 GURL* new_url) OVERRIDE {
112    EXPECT_TRUE(request->load_flags() & LOAD_DISABLE_CERT_REVOCATION_CHECKING);
113    return OK;
114  }
115
116  virtual int OnBeforeSendHeaders(URLRequest* request,
117                                  const CompletionCallback& callback,
118                                  HttpRequestHeaders* headers) OVERRIDE {
119    return OK;
120  }
121
122  virtual void OnSendHeaders(URLRequest* request,
123                             const HttpRequestHeaders& headers) OVERRIDE {}
124
125  virtual int OnHeadersReceived(
126      URLRequest* request,
127      const CompletionCallback& callback,
128      const HttpResponseHeaders* original_response_headers,
129      scoped_refptr<HttpResponseHeaders>* override_response_headers,
130      GURL* allowed_unsafe_redirect_url) OVERRIDE {
131    return OK;
132  }
133
134  virtual void OnBeforeRedirect(URLRequest* request,
135                                const GURL& new_location) OVERRIDE {}
136
137  virtual void OnResponseStarted(URLRequest* request) OVERRIDE {}
138
139  virtual void OnRawBytesRead(const URLRequest& request,
140                              int bytes_read) OVERRIDE {}
141
142  virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE {}
143
144  virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE {}
145
146  virtual void OnPACScriptError(int line_number,
147                                const base::string16& error) OVERRIDE {}
148
149  virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
150      URLRequest* request,
151      const AuthChallengeInfo& auth_info,
152      const AuthCallback& callback,
153      AuthCredentials* credentials) OVERRIDE {
154    return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
155  }
156
157  virtual bool OnCanGetCookies(const URLRequest& request,
158                               const CookieList& cookie_list) OVERRIDE {
159    return true;
160  }
161
162  virtual bool OnCanSetCookie(const URLRequest& request,
163                              const std::string& cookie_line,
164                              CookieOptions* options) OVERRIDE {
165    return true;
166  }
167
168  virtual bool OnCanAccessFile(const net::URLRequest& request,
169                               const base::FilePath& path) const OVERRIDE {
170    return true;
171  }
172  virtual bool OnCanThrottleRequest(const URLRequest& request) const OVERRIDE {
173    return false;
174  }
175
176  virtual int OnBeforeSocketStreamConnect(
177      SocketStream* stream,
178      const CompletionCallback& callback) OVERRIDE {
179    return OK;
180  }
181
182  DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
183};
184
185}  // namespace
186
187class 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_);
194  }
195
196 protected:
197  SpawnedTestServer test_server_;
198  BasicNetworkDelegate network_delegate_;
199  RequestContext context_;
200};
201
202TEST_F(ProxyScriptFetcherImplTest, FileUrl) {
203  ProxyScriptFetcherImpl pac_fetcher(&context_);
204
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());
213  }
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);
222  }
223}
224
225// Note that all mime types are allowed for PAC file, to be consistent
226// with other browsers.
227TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) {
228  ASSERT_TRUE(test_server_.Start());
229
230  ProxyScriptFetcherImpl pac_fetcher(&context_);
231
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);
240  }
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);
249  }
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);
258  }
259}
260
261TEST_F(ProxyScriptFetcherImplTest, HttpStatusCode) {
262  ASSERT_TRUE(test_server_.Start());
263
264  ProxyScriptFetcherImpl pac_fetcher(&context_);
265
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());
274  }
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());
283  }
284}
285
286TEST_F(ProxyScriptFetcherImplTest, ContentDisposition) {
287  ASSERT_TRUE(test_server_.Start());
288
289  ProxyScriptFetcherImpl pac_fetcher(&context_);
290
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);
300}
301
302// Verifies that PAC scripts are not being cached.
303TEST_F(ProxyScriptFetcherImplTest, NoCache) {
304  ASSERT_TRUE(test_server_.Start());
305
306  ProxyScriptFetcherImpl pac_fetcher(&context_);
307
308  // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour.
309  GURL url(test_server_.GetURL("files/cacheable_1hr.pac"));
310  {
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);
317  }
318
319  // Kill the HTTP server.
320  ASSERT_TRUE(test_server_.Stop());
321
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.
325  {
326    base::string16 text;
327    TestCompletionCallback callback;
328    int result = pac_fetcher.Fetch(url, &text, callback.callback());
329    EXPECT_EQ(ERR_IO_PENDING, result);
330
331    // Expect any error. The exact error varies by platform.
332    EXPECT_NE(OK, callback.WaitForResult());
333  }
334}
335
336TEST_F(ProxyScriptFetcherImplTest, TooLarge) {
337  ASSERT_TRUE(test_server_.Start());
338
339  ProxyScriptFetcherImpl pac_fetcher(&context_);
340
341  // Set the maximum response size to 50 bytes.
342  int prev_size = pac_fetcher.SetSizeConstraint(50);
343
344  // These two URLs are the same file, but are http:// vs file://
345  GURL urls[] = {
346    test_server_.GetURL("files/large-pac.nsproxy"),
347    GetTestFileUrl("large-pac.nsproxy")
348  };
349
350  // Try fetching URLs that are 101 bytes large. We should abort the request
351  // after 50 bytes have been read, and fail with a too large error.
352  for (size_t i = 0; i < arraysize(urls); ++i) {
353    const GURL& url = urls[i];
354    base::string16 text;
355    TestCompletionCallback callback;
356    int result = pac_fetcher.Fetch(url, &text, callback.callback());
357    EXPECT_EQ(ERR_IO_PENDING, result);
358    EXPECT_EQ(ERR_FILE_TOO_BIG, callback.WaitForResult());
359    EXPECT_TRUE(text.empty());
360  }
361
362  // Restore the original size bound.
363  pac_fetcher.SetSizeConstraint(prev_size);
364
365  { // Make sure we can still fetch regular URLs.
366    GURL url(test_server_.GetURL("files/pac.nsproxy"));
367    base::string16 text;
368    TestCompletionCallback callback;
369    int result = pac_fetcher.Fetch(url, &text, callback.callback());
370    EXPECT_EQ(ERR_IO_PENDING, result);
371    EXPECT_EQ(OK, callback.WaitForResult());
372    EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
373  }
374}
375
376TEST_F(ProxyScriptFetcherImplTest, Hang) {
377  ASSERT_TRUE(test_server_.Start());
378
379  ProxyScriptFetcherImpl pac_fetcher(&context_);
380
381  // Set the timeout period to 0.5 seconds.
382  base::TimeDelta prev_timeout = pac_fetcher.SetTimeoutConstraint(
383      base::TimeDelta::FromMilliseconds(500));
384
385  // Try fetching a URL which takes 1.2 seconds. We should abort the request
386  // after 500 ms, and fail with a timeout error.
387  {
388    GURL url(test_server_.GetURL("slow/proxy.pac?1.2"));
389    base::string16 text;
390    TestCompletionCallback callback;
391    int result = pac_fetcher.Fetch(url, &text, callback.callback());
392    EXPECT_EQ(ERR_IO_PENDING, result);
393    EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult());
394    EXPECT_TRUE(text.empty());
395  }
396
397  // Restore the original timeout period.
398  pac_fetcher.SetTimeoutConstraint(prev_timeout);
399
400  { // Make sure we can still fetch regular URLs.
401    GURL url(test_server_.GetURL("files/pac.nsproxy"));
402    base::string16 text;
403    TestCompletionCallback callback;
404    int result = pac_fetcher.Fetch(url, &text, callback.callback());
405    EXPECT_EQ(ERR_IO_PENDING, result);
406    EXPECT_EQ(OK, callback.WaitForResult());
407    EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text);
408  }
409}
410
411// The ProxyScriptFetcher should decode any content-codings
412// (like gzip, bzip, etc.), and apply any charset conversions to yield
413// UTF8.
414TEST_F(ProxyScriptFetcherImplTest, Encodings) {
415  ASSERT_TRUE(test_server_.Start());
416
417  ProxyScriptFetcherImpl pac_fetcher(&context_);
418
419  // Test a response that is gzip-encoded -- should get inflated.
420  {
421    GURL url(test_server_.GetURL("files/gzipped_pac"));
422    base::string16 text;
423    TestCompletionCallback callback;
424    int result = pac_fetcher.Fetch(url, &text, callback.callback());
425    EXPECT_EQ(ERR_IO_PENDING, result);
426    EXPECT_EQ(OK, callback.WaitForResult());
427    EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text);
428  }
429
430  // Test a response that was served as UTF-16 (BE). It should
431  // be converted to UTF8.
432  {
433    GURL url(test_server_.GetURL("files/utf16be_pac"));
434    base::string16 text;
435    TestCompletionCallback callback;
436    int result = pac_fetcher.Fetch(url, &text, callback.callback());
437    EXPECT_EQ(ERR_IO_PENDING, result);
438    EXPECT_EQ(OK, callback.WaitForResult());
439    EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text);
440  }
441}
442
443TEST_F(ProxyScriptFetcherImplTest, DataURLs) {
444  ProxyScriptFetcherImpl pac_fetcher(&context_);
445
446  const char kEncodedUrl[] =
447      "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
448      "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
449      "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
450  const char kPacScript[] =
451      "function FindProxyForURL(url, host) {\n"
452      "  if (host == 'foobar.com')\n"
453      "    return 'PROXY blackhole:80';\n"
454      "  return 'DIRECT';\n"
455      "}";
456
457  // Test fetching a "data:"-url containing a base64 encoded PAC script.
458  {
459    GURL url(kEncodedUrl);
460    base::string16 text;
461    TestCompletionCallback callback;
462    int result = pac_fetcher.Fetch(url, &text, callback.callback());
463    EXPECT_EQ(OK, result);
464    EXPECT_EQ(ASCIIToUTF16(kPacScript), text);
465  }
466
467  const char kEncodedUrlBroken[] =
468      "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R";
469
470  // Test a broken "data:"-url containing a base64 encoded PAC script.
471  {
472    GURL url(kEncodedUrlBroken);
473    base::string16 text;
474    TestCompletionCallback callback;
475    int result = pac_fetcher.Fetch(url, &text, callback.callback());
476    EXPECT_EQ(ERR_FAILED, result);
477  }
478}
479
480}  // namespace net
481