url_request_unittest.cc revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
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 "build/build_config.h"
6
7#if defined(OS_WIN)
8#include <windows.h>
9#include <shlobj.h>
10#endif
11
12#include <algorithm>
13#include <string>
14
15#include "base/basictypes.h"
16#include "base/bind.h"
17#include "base/compiler_specific.h"
18#include "base/file_util.h"
19#include "base/format_macros.h"
20#include "base/memory/weak_ptr.h"
21#include "base/message_loop/message_loop.h"
22#include "base/path_service.h"
23#include "base/process_util.h"
24#include "base/strings/string_number_conversions.h"
25#include "base/strings/string_piece.h"
26#include "base/strings/string_split.h"
27#include "base/strings/string_util.h"
28#include "base/strings/stringprintf.h"
29#include "base/strings/utf_string_conversions.h"
30#include "net/base/capturing_net_log.h"
31#include "net/base/load_flags.h"
32#include "net/base/load_timing_info.h"
33#include "net/base/load_timing_info_test_util.h"
34#include "net/base/net_errors.h"
35#include "net/base/net_log.h"
36#include "net/base/net_log_unittest.h"
37#include "net/base/net_module.h"
38#include "net/base/net_util.h"
39#include "net/base/test_data_directory.h"
40#include "net/base/upload_bytes_element_reader.h"
41#include "net/base/upload_data_stream.h"
42#include "net/base/upload_file_element_reader.h"
43#include "net/cert/ev_root_ca_metadata.h"
44#include "net/cert/test_root_certs.h"
45#include "net/cookies/cookie_monster.h"
46#include "net/cookies/cookie_store_test_helpers.h"
47#include "net/disk_cache/disk_cache.h"
48#include "net/dns/mock_host_resolver.h"
49#include "net/ftp/ftp_network_layer.h"
50#include "net/http/http_cache.h"
51#include "net/http/http_network_layer.h"
52#include "net/http/http_network_session.h"
53#include "net/http/http_request_headers.h"
54#include "net/http/http_response_headers.h"
55#include "net/ocsp/nss_ocsp.h"
56#include "net/proxy/proxy_service.h"
57#include "net/socket/ssl_client_socket.h"
58#include "net/ssl/ssl_connection_status_flags.h"
59#include "net/test/cert_test_util.h"
60#include "net/test/spawned_test_server/spawned_test_server.h"
61#include "net/url_request/data_protocol_handler.h"
62#include "net/url_request/file_protocol_handler.h"
63#include "net/url_request/ftp_protocol_handler.h"
64#include "net/url_request/static_http_user_agent_settings.h"
65#include "net/url_request/url_request.h"
66#include "net/url_request/url_request_file_dir_job.h"
67#include "net/url_request/url_request_http_job.h"
68#include "net/url_request/url_request_job_factory_impl.h"
69#include "net/url_request/url_request_redirect_job.h"
70#include "net/url_request/url_request_test_job.h"
71#include "net/url_request/url_request_test_util.h"
72#include "testing/gtest/include/gtest/gtest.h"
73#include "testing/platform_test.h"
74
75#if defined(OS_WIN)
76#include "base/win/scoped_com_initializer.h"
77#include "base/win/scoped_comptr.h"
78#include "base/win/windows_version.h"
79#endif
80
81using base::Time;
82
83namespace net {
84
85namespace {
86
87const base::string16 kChrome(ASCIIToUTF16("chrome"));
88const base::string16 kSecret(ASCIIToUTF16("secret"));
89const base::string16 kUser(ASCIIToUTF16("user"));
90
91// Tests load timing information in the case a fresh connection was used, with
92// no proxy.
93void TestLoadTimingNotReused(const net::LoadTimingInfo& load_timing_info,
94                             int connect_timing_flags) {
95  EXPECT_FALSE(load_timing_info.socket_reused);
96  EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
97
98  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
99  EXPECT_FALSE(load_timing_info.request_start.is_null());
100
101  EXPECT_LE(load_timing_info.request_start,
102            load_timing_info.connect_timing.connect_start);
103  ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
104                              connect_timing_flags);
105  EXPECT_LE(load_timing_info.connect_timing.connect_end,
106            load_timing_info.send_start);
107  EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
108  EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
109
110  EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
111  EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
112}
113
114// Same as above, but with proxy times.
115void TestLoadTimingNotReusedWithProxy(
116    const net::LoadTimingInfo& load_timing_info,
117    int connect_timing_flags) {
118  EXPECT_FALSE(load_timing_info.socket_reused);
119  EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
120
121  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
122  EXPECT_FALSE(load_timing_info.request_start.is_null());
123
124  EXPECT_LE(load_timing_info.request_start,
125            load_timing_info.proxy_resolve_start);
126  EXPECT_LE(load_timing_info.proxy_resolve_start,
127            load_timing_info.proxy_resolve_end);
128  EXPECT_LE(load_timing_info.proxy_resolve_end,
129            load_timing_info.connect_timing.connect_start);
130  ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
131                              connect_timing_flags);
132  EXPECT_LE(load_timing_info.connect_timing.connect_end,
133            load_timing_info.send_start);
134  EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
135  EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
136}
137
138// Same as above, but with a reused socket and proxy times.
139void TestLoadTimingReusedWithProxy(
140    const net::LoadTimingInfo& load_timing_info) {
141  EXPECT_TRUE(load_timing_info.socket_reused);
142  EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
143
144  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
145  EXPECT_FALSE(load_timing_info.request_start.is_null());
146
147  ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
148
149  EXPECT_LE(load_timing_info.request_start,
150            load_timing_info.proxy_resolve_start);
151  EXPECT_LE(load_timing_info.proxy_resolve_start,
152            load_timing_info.proxy_resolve_end);
153  EXPECT_LE(load_timing_info.proxy_resolve_end,
154            load_timing_info.send_start);
155  EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
156  EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
157}
158
159// Tests load timing information in the case of a cache hit, when no cache
160// validation request was sent over the wire.
161void TestLoadTimingCacheHitNoNetwork(
162    const net::LoadTimingInfo& load_timing_info) {
163  EXPECT_FALSE(load_timing_info.socket_reused);
164  EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
165
166  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
167  EXPECT_FALSE(load_timing_info.request_start.is_null());
168
169  ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
170  EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start);
171  EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
172  EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
173
174  EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
175  EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
176}
177
178// Tests load timing in the case that there is no HTTP response.  This can be
179// used to test in the case of errors or non-HTTP requests.
180void TestLoadTimingNoHttpResponse(
181    const net::LoadTimingInfo& load_timing_info) {
182  EXPECT_FALSE(load_timing_info.socket_reused);
183  EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
184
185  // Only the request times should be non-null.
186  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
187  EXPECT_FALSE(load_timing_info.request_start.is_null());
188
189  ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
190
191  EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
192  EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
193  EXPECT_TRUE(load_timing_info.send_start.is_null());
194  EXPECT_TRUE(load_timing_info.send_end.is_null());
195  EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
196}
197
198base::StringPiece TestNetResourceProvider(int key) {
199  return "header";
200}
201
202// Do a case-insensitive search through |haystack| for |needle|.
203bool ContainsString(const std::string& haystack, const char* needle) {
204  std::string::const_iterator it =
205      std::search(haystack.begin(),
206                  haystack.end(),
207                  needle,
208                  needle + strlen(needle),
209                  base::CaseInsensitiveCompare<char>());
210  return it != haystack.end();
211}
212
213void FillBuffer(char* buffer, size_t len) {
214  static bool called = false;
215  if (!called) {
216    called = true;
217    int seed = static_cast<int>(Time::Now().ToInternalValue());
218    srand(seed);
219  }
220
221  for (size_t i = 0; i < len; i++) {
222    buffer[i] = static_cast<char>(rand());
223    if (!buffer[i])
224      buffer[i] = 'g';
225  }
226}
227
228UploadDataStream* CreateSimpleUploadData(const char* data) {
229  scoped_ptr<UploadElementReader> reader(
230      new UploadBytesElementReader(data, strlen(data)));
231  return UploadDataStream::CreateWithReader(reader.Pass(), 0);
232}
233
234// Verify that the SSLInfo of a successful SSL connection has valid values.
235void CheckSSLInfo(const SSLInfo& ssl_info) {
236  // Allow ChromeFrame fake SSLInfo to get through.
237  if (ssl_info.cert.get() &&
238      ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") {
239    // -1 means unknown.
240    EXPECT_EQ(ssl_info.security_bits, -1);
241    return;
242  }
243
244  // -1 means unknown.  0 means no encryption.
245  EXPECT_GT(ssl_info.security_bits, 0);
246
247  // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
248  int cipher_suite = SSLConnectionStatusToCipherSuite(
249      ssl_info.connection_status);
250  EXPECT_NE(0, cipher_suite);
251}
252
253void CheckFullRequestHeaders(const HttpRequestHeaders& headers,
254                             const GURL& host_url) {
255  std::string sent_value;
256
257  EXPECT_TRUE(headers.GetHeader("Host", &sent_value));
258  EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value);
259
260  EXPECT_TRUE(headers.GetHeader("Connection", &sent_value));
261  EXPECT_EQ("keep-alive", sent_value);
262}
263
264bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
265  size_t size = a.size();
266
267  if (size != b.size())
268    return false;
269
270  for (size_t i = 0; i < size; ++i) {
271    if (!a[i].Equals(b[i]))
272      return false;
273  }
274
275  return true;
276}
277
278// A network delegate that allows the user to choose a subset of request stages
279// to block in. When blocking, the delegate can do one of the following:
280//  * synchronously return a pre-specified error code, or
281//  * asynchronously return that value via an automatically called callback,
282//    or
283//  * block and wait for the user to do a callback.
284// Additionally, the user may also specify a redirect URL -- then each request
285// with the current URL different from the redirect target will be redirected
286// to that target, in the on-before-URL-request stage, independent of whether
287// the delegate blocks in ON_BEFORE_URL_REQUEST or not.
288class BlockingNetworkDelegate : public TestNetworkDelegate {
289 public:
290  // Stages in which the delegate can block.
291  enum Stage {
292    NOT_BLOCKED = 0,
293    ON_BEFORE_URL_REQUEST = 1 << 0,
294    ON_BEFORE_SEND_HEADERS = 1 << 1,
295    ON_HEADERS_RECEIVED = 1 << 2,
296    ON_AUTH_REQUIRED = 1 << 3
297  };
298
299  // Behavior during blocked stages.  During other stages, just
300  // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
301  enum BlockMode {
302    SYNCHRONOUS,    // No callback, returns specified return values.
303    AUTO_CALLBACK,  // |this| posts a task to run the callback using the
304                    // specified return codes.
305    USER_CALLBACK,  // User takes care of doing a callback.  |retval_| and
306                    // |auth_retval_| are ignored. In every blocking stage the
307                    // message loop is quit.
308  };
309
310  // Creates a delegate which does not block at all.
311  explicit BlockingNetworkDelegate(BlockMode block_mode);
312
313  // For users to trigger a callback returning |response|.
314  // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
315  // Only call if |block_mode_| == USER_CALLBACK.
316  void DoCallback(int response);
317  void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
318
319  // Setters.
320  void set_retval(int retval) {
321    ASSERT_NE(USER_CALLBACK, block_mode_);
322    ASSERT_NE(ERR_IO_PENDING, retval);
323    ASSERT_NE(OK, retval);
324    retval_ = retval;
325  }
326
327  // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
328  // |auth_credentials_| will be passed with the response.
329  void set_auth_retval(AuthRequiredResponse auth_retval) {
330    ASSERT_NE(USER_CALLBACK, block_mode_);
331    ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
332    auth_retval_ = auth_retval;
333  }
334  void set_auth_credentials(const AuthCredentials& auth_credentials) {
335    auth_credentials_ = auth_credentials;
336  }
337
338  void set_redirect_url(const GURL& url) {
339    redirect_url_ = url;
340  }
341
342  void set_block_on(int block_on) {
343    block_on_ = block_on;
344  }
345
346  // Allows the user to check in which state did we block.
347  Stage stage_blocked_for_callback() const {
348    EXPECT_EQ(USER_CALLBACK, block_mode_);
349    return stage_blocked_for_callback_;
350  }
351
352 private:
353  void RunCallback(int response, const CompletionCallback& callback);
354  void RunAuthCallback(AuthRequiredResponse response,
355                       const AuthCallback& callback);
356
357  // TestNetworkDelegate implementation.
358  virtual int OnBeforeURLRequest(URLRequest* request,
359                                 const CompletionCallback& callback,
360                                 GURL* new_url) OVERRIDE;
361
362  virtual int OnBeforeSendHeaders(URLRequest* request,
363                                  const CompletionCallback& callback,
364                                  HttpRequestHeaders* headers) OVERRIDE;
365
366  virtual int OnHeadersReceived(
367      URLRequest* request,
368      const CompletionCallback& callback,
369      const HttpResponseHeaders* original_response_headers,
370      scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE;
371
372  virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
373      URLRequest* request,
374      const AuthChallengeInfo& auth_info,
375      const AuthCallback& callback,
376      AuthCredentials* credentials) OVERRIDE;
377
378  // Resets the callbacks and |stage_blocked_for_callback_|.
379  void Reset();
380
381  // Checks whether we should block in |stage|. If yes, returns an error code
382  // and optionally sets up callback based on |block_mode_|. If no, returns OK.
383  int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
384
385  // Configuration parameters, can be adjusted by public methods:
386  const BlockMode block_mode_;
387
388  // Values returned on blocking stages when mode is SYNCHRONOUS or
389  // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
390  int retval_;  // To be returned in non-auth stages.
391  AuthRequiredResponse auth_retval_;
392
393  GURL redirect_url_;  // Used if non-empty.
394  int block_on_;  // Bit mask: in which stages to block.
395
396  // |auth_credentials_| will be copied to |*target_auth_credential_| on
397  // callback.
398  AuthCredentials auth_credentials_;
399  AuthCredentials* target_auth_credentials_;
400
401  // Internal variables, not set by not the user:
402  // Last blocked stage waiting for user callback (unused if |block_mode_| !=
403  // USER_CALLBACK).
404  Stage stage_blocked_for_callback_;
405
406  // Callback objects stored during blocking stages.
407  CompletionCallback callback_;
408  AuthCallback auth_callback_;
409
410  base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
411
412  DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
413};
414
415BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
416    : block_mode_(block_mode),
417      retval_(OK),
418      auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
419      block_on_(0),
420      target_auth_credentials_(NULL),
421      stage_blocked_for_callback_(NOT_BLOCKED),
422      weak_factory_(this) {
423}
424
425void BlockingNetworkDelegate::DoCallback(int response) {
426  ASSERT_EQ(USER_CALLBACK, block_mode_);
427  ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
428  ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
429  CompletionCallback callback = callback_;
430  Reset();
431  RunCallback(response, callback);
432}
433
434void BlockingNetworkDelegate::DoAuthCallback(
435    NetworkDelegate::AuthRequiredResponse response) {
436  ASSERT_EQ(USER_CALLBACK, block_mode_);
437  ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
438  AuthCallback auth_callback = auth_callback_;
439  Reset();
440  RunAuthCallback(response, auth_callback);
441}
442
443void BlockingNetworkDelegate::RunCallback(int response,
444                                          const CompletionCallback& callback) {
445  callback.Run(response);
446}
447
448void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
449                                              const AuthCallback& callback) {
450  if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
451    ASSERT_TRUE(target_auth_credentials_ != NULL);
452    *target_auth_credentials_ = auth_credentials_;
453  }
454  callback.Run(response);
455}
456
457int BlockingNetworkDelegate::OnBeforeURLRequest(
458    URLRequest* request,
459    const CompletionCallback& callback,
460    GURL* new_url) {
461  if (redirect_url_ == request->url())
462    return OK;  // We've already seen this request and redirected elsewhere.
463
464  TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
465
466  if (!redirect_url_.is_empty())
467    *new_url = redirect_url_;
468
469  return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
470}
471
472int BlockingNetworkDelegate::OnBeforeSendHeaders(
473    URLRequest* request,
474    const CompletionCallback& callback,
475    HttpRequestHeaders* headers) {
476  TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
477
478  return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
479}
480
481int BlockingNetworkDelegate::OnHeadersReceived(
482    URLRequest* request,
483    const CompletionCallback& callback,
484    const HttpResponseHeaders* original_response_headers,
485    scoped_refptr<HttpResponseHeaders>* override_response_headers) {
486  TestNetworkDelegate::OnHeadersReceived(
487      request, callback, original_response_headers,
488      override_response_headers);
489
490  return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
491}
492
493NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
494    URLRequest* request,
495    const AuthChallengeInfo& auth_info,
496    const AuthCallback& callback,
497    AuthCredentials* credentials) {
498  TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
499                                      credentials);
500  // Check that the user has provided callback for the previous blocked stage.
501  EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
502
503  if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
504    return AUTH_REQUIRED_RESPONSE_NO_ACTION;
505  }
506
507  target_auth_credentials_ = credentials;
508
509  switch (block_mode_) {
510    case SYNCHRONOUS:
511      if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
512        *target_auth_credentials_ = auth_credentials_;
513      return auth_retval_;
514
515    case AUTO_CALLBACK:
516      base::MessageLoop::current()->PostTask(
517          FROM_HERE,
518          base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
519                     weak_factory_.GetWeakPtr(), auth_retval_, callback));
520      return AUTH_REQUIRED_RESPONSE_IO_PENDING;
521
522    case USER_CALLBACK:
523      auth_callback_ = callback;
524      stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
525      base::MessageLoop::current()->PostTask(FROM_HERE,
526                                             base::MessageLoop::QuitClosure());
527      return AUTH_REQUIRED_RESPONSE_IO_PENDING;
528  }
529  NOTREACHED();
530  return AUTH_REQUIRED_RESPONSE_NO_ACTION;  // Dummy value.
531}
532
533void BlockingNetworkDelegate::Reset() {
534  EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
535  stage_blocked_for_callback_ = NOT_BLOCKED;
536  callback_.Reset();
537  auth_callback_.Reset();
538}
539
540int BlockingNetworkDelegate::MaybeBlockStage(
541    BlockingNetworkDelegate::Stage stage,
542    const CompletionCallback& callback) {
543  // Check that the user has provided callback for the previous blocked stage.
544  EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
545
546  if ((block_on_ & stage) == 0) {
547    return OK;
548  }
549
550  switch (block_mode_) {
551    case SYNCHRONOUS:
552      EXPECT_NE(OK, retval_);
553      return retval_;
554
555    case AUTO_CALLBACK:
556      base::MessageLoop::current()->PostTask(
557          FROM_HERE,
558          base::Bind(&BlockingNetworkDelegate::RunCallback,
559                     weak_factory_.GetWeakPtr(), retval_, callback));
560      return ERR_IO_PENDING;
561
562    case USER_CALLBACK:
563      callback_ = callback;
564      stage_blocked_for_callback_ = stage;
565      base::MessageLoop::current()->PostTask(FROM_HERE,
566                                             base::MessageLoop::QuitClosure());
567      return ERR_IO_PENDING;
568  }
569  NOTREACHED();
570  return 0;
571}
572
573class TestURLRequestContextWithProxy : public TestURLRequestContext {
574 public:
575  // Does not own |delegate|.
576  TestURLRequestContextWithProxy(const std::string& proxy,
577                                 NetworkDelegate* delegate)
578      : TestURLRequestContext(true) {
579    context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
580    set_network_delegate(delegate);
581    Init();
582  }
583  virtual ~TestURLRequestContextWithProxy() {}
584};
585
586}  // namespace
587
588// Inherit PlatformTest since we require the autorelease pool on Mac OS X.
589class URLRequestTest : public PlatformTest {
590 public:
591  URLRequestTest() : default_context_(true) {
592    default_context_.set_network_delegate(&default_network_delegate_);
593    default_context_.set_net_log(&net_log_);
594    job_factory_.SetProtocolHandler("data", new DataProtocolHandler);
595    job_factory_.SetProtocolHandler("file", new FileProtocolHandler);
596    default_context_.set_job_factory(&job_factory_);
597    default_context_.Init();
598  }
599  virtual ~URLRequestTest() {}
600
601  // Adds the TestJobInterceptor to the default context.
602  TestJobInterceptor* AddTestInterceptor() {
603    TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
604    job_factory_.SetProtocolHandler("http", NULL);
605    job_factory_.SetProtocolHandler("http", protocol_handler_);
606    return protocol_handler_;
607  }
608
609 protected:
610  CapturingNetLog net_log_;
611  TestNetworkDelegate default_network_delegate_;  // Must outlive URLRequest.
612  URLRequestJobFactoryImpl job_factory_;
613  TestURLRequestContext default_context_;
614};
615
616TEST_F(URLRequestTest, AboutBlankTest) {
617  TestDelegate d;
618  {
619    URLRequest r(GURL("about:blank"), &d, &default_context_);
620
621    r.Start();
622    EXPECT_TRUE(r.is_pending());
623
624    base::MessageLoop::current()->Run();
625
626    EXPECT_TRUE(!r.is_pending());
627    EXPECT_FALSE(d.received_data_before_response());
628    EXPECT_EQ(d.bytes_received(), 0);
629    EXPECT_EQ("", r.GetSocketAddress().host());
630    EXPECT_EQ(0, r.GetSocketAddress().port());
631
632    HttpRequestHeaders headers;
633    EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
634  }
635}
636
637TEST_F(URLRequestTest, DataURLImageTest) {
638  TestDelegate d;
639  {
640    // Use our nice little Chrome logo.
641    URLRequest r(GURL(
642        "data:image/png;base64,"
643        "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
644        "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
645        "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
646        "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
647        "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
648        "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
649        "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
650        "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
651        "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
652        "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
653        "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
654        "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
655        "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
656        "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
657        "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
658        "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
659        "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
660        "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
661        &d,
662        &default_context_);
663
664    r.Start();
665    EXPECT_TRUE(r.is_pending());
666
667    base::MessageLoop::current()->Run();
668
669    EXPECT_TRUE(!r.is_pending());
670    EXPECT_FALSE(d.received_data_before_response());
671    EXPECT_EQ(d.bytes_received(), 911);
672    EXPECT_EQ("", r.GetSocketAddress().host());
673    EXPECT_EQ(0, r.GetSocketAddress().port());
674
675    HttpRequestHeaders headers;
676    EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
677  }
678}
679
680TEST_F(URLRequestTest, FileTest) {
681  base::FilePath app_path;
682  PathService::Get(base::FILE_EXE, &app_path);
683  GURL app_url = FilePathToFileURL(app_path);
684
685  TestDelegate d;
686  {
687    URLRequest r(app_url, &d, &default_context_);
688
689    r.Start();
690    EXPECT_TRUE(r.is_pending());
691
692    base::MessageLoop::current()->Run();
693
694    int64 file_size = -1;
695    EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
696
697    EXPECT_TRUE(!r.is_pending());
698    EXPECT_EQ(1, d.response_started_count());
699    EXPECT_FALSE(d.received_data_before_response());
700    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
701    EXPECT_EQ("", r.GetSocketAddress().host());
702    EXPECT_EQ(0, r.GetSocketAddress().port());
703
704    HttpRequestHeaders headers;
705    EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
706  }
707}
708
709TEST_F(URLRequestTest, FileTestCancel) {
710  base::FilePath app_path;
711  PathService::Get(base::FILE_EXE, &app_path);
712  GURL app_url = FilePathToFileURL(app_path);
713
714  TestDelegate d;
715  {
716    URLRequest r(app_url, &d, &default_context_);
717
718    r.Start();
719    EXPECT_TRUE(r.is_pending());
720    r.Cancel();
721  }
722  // Async cancellation should be safe even when URLRequest has been already
723  // destroyed.
724  base::MessageLoop::current()->RunUntilIdle();
725}
726
727TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
728  const size_t buffer_size = 4000;
729  scoped_ptr<char[]> buffer(new char[buffer_size]);
730  FillBuffer(buffer.get(), buffer_size);
731
732  base::FilePath temp_path;
733  EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
734  GURL temp_url = FilePathToFileURL(temp_path);
735  EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
736
737  int64 file_size;
738  EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
739
740  const size_t first_byte_position = 500;
741  const size_t last_byte_position = buffer_size - first_byte_position;
742  const size_t content_length = last_byte_position - first_byte_position + 1;
743  std::string partial_buffer_string(buffer.get() + first_byte_position,
744                                    buffer.get() + last_byte_position + 1);
745
746  TestDelegate d;
747  {
748    URLRequest r(temp_url, &d, &default_context_);
749
750    HttpRequestHeaders headers;
751    headers.SetHeader(HttpRequestHeaders::kRange,
752                      base::StringPrintf(
753                           "bytes=%" PRIuS "-%" PRIuS,
754                           first_byte_position, last_byte_position));
755    r.SetExtraRequestHeaders(headers);
756    r.Start();
757    EXPECT_TRUE(r.is_pending());
758
759    base::MessageLoop::current()->Run();
760    EXPECT_TRUE(!r.is_pending());
761    EXPECT_EQ(1, d.response_started_count());
762    EXPECT_FALSE(d.received_data_before_response());
763    EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
764    // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
765    EXPECT_TRUE(partial_buffer_string == d.data_received());
766  }
767
768  EXPECT_TRUE(base::DeleteFile(temp_path, false));
769}
770
771TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
772  const size_t buffer_size = 4000;
773  scoped_ptr<char[]> buffer(new char[buffer_size]);
774  FillBuffer(buffer.get(), buffer_size);
775
776  base::FilePath temp_path;
777  EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
778  GURL temp_url = FilePathToFileURL(temp_path);
779  EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
780
781  int64 file_size;
782  EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
783
784  const size_t first_byte_position = 500;
785  const size_t last_byte_position = buffer_size - 1;
786  const size_t content_length = last_byte_position - first_byte_position + 1;
787  std::string partial_buffer_string(buffer.get() + first_byte_position,
788                                    buffer.get() + last_byte_position + 1);
789
790  TestDelegate d;
791  {
792    URLRequest r(temp_url, &d, &default_context_);
793
794    HttpRequestHeaders headers;
795    headers.SetHeader(HttpRequestHeaders::kRange,
796                      base::StringPrintf("bytes=%" PRIuS "-",
797                                         first_byte_position));
798    r.SetExtraRequestHeaders(headers);
799    r.Start();
800    EXPECT_TRUE(r.is_pending());
801
802    base::MessageLoop::current()->Run();
803    EXPECT_TRUE(!r.is_pending());
804    EXPECT_EQ(1, d.response_started_count());
805    EXPECT_FALSE(d.received_data_before_response());
806    EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
807    // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
808    EXPECT_TRUE(partial_buffer_string == d.data_received());
809  }
810
811  EXPECT_TRUE(base::DeleteFile(temp_path, false));
812}
813
814TEST_F(URLRequestTest, FileTestMultipleRanges) {
815  const size_t buffer_size = 400000;
816  scoped_ptr<char[]> buffer(new char[buffer_size]);
817  FillBuffer(buffer.get(), buffer_size);
818
819  base::FilePath temp_path;
820  EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
821  GURL temp_url = FilePathToFileURL(temp_path);
822  EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
823
824  int64 file_size;
825  EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
826
827  TestDelegate d;
828  {
829    URLRequest r(temp_url, &d, &default_context_);
830
831    HttpRequestHeaders headers;
832    headers.SetHeader(HttpRequestHeaders::kRange,
833                      "bytes=0-0,10-200,200-300");
834    r.SetExtraRequestHeaders(headers);
835    r.Start();
836    EXPECT_TRUE(r.is_pending());
837
838    base::MessageLoop::current()->Run();
839    EXPECT_TRUE(d.request_failed());
840  }
841
842  EXPECT_TRUE(base::DeleteFile(temp_path, false));
843}
844
845TEST_F(URLRequestTest, InvalidUrlTest) {
846  TestDelegate d;
847  {
848    URLRequest r(GURL("invalid url"), &d, &default_context_);
849
850    r.Start();
851    EXPECT_TRUE(r.is_pending());
852
853    base::MessageLoop::current()->Run();
854    EXPECT_TRUE(d.request_failed());
855  }
856}
857
858#if defined(OS_WIN)
859TEST_F(URLRequestTest, ResolveShortcutTest) {
860  base::FilePath app_path;
861  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
862  app_path = app_path.AppendASCII("net");
863  app_path = app_path.AppendASCII("data");
864  app_path = app_path.AppendASCII("url_request_unittest");
865  app_path = app_path.AppendASCII("with-headers.html");
866
867  std::wstring lnk_path = app_path.value() + L".lnk";
868
869  base::win::ScopedCOMInitializer com_initializer;
870
871  // Temporarily create a shortcut for test
872  {
873    base::win::ScopedComPtr<IShellLink> shell;
874    ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL,
875                                               CLSCTX_INPROC_SERVER)));
876    base::win::ScopedComPtr<IPersistFile> persist;
877    ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive())));
878    EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
879    EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
880    EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
881  }
882
883  TestDelegate d;
884  {
885    URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)), &d,
886                 &default_context_);
887
888    r.Start();
889    EXPECT_TRUE(r.is_pending());
890
891    base::MessageLoop::current()->Run();
892
893    WIN32_FILE_ATTRIBUTE_DATA data;
894    GetFileAttributesEx(app_path.value().c_str(),
895                        GetFileExInfoStandard, &data);
896    HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
897                             FILE_SHARE_READ, NULL, OPEN_EXISTING,
898                             FILE_ATTRIBUTE_NORMAL, NULL);
899    EXPECT_NE(INVALID_HANDLE_VALUE, file);
900    scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]);
901    DWORD read_size;
902    BOOL result;
903    result = ReadFile(file, buffer.get(), data.nFileSizeLow,
904                      &read_size, NULL);
905    std::string content(buffer.get(), read_size);
906    CloseHandle(file);
907
908    EXPECT_TRUE(!r.is_pending());
909    EXPECT_EQ(1, d.received_redirect_count());
910    EXPECT_EQ(content, d.data_received());
911  }
912
913  // Clean the shortcut
914  DeleteFile(lnk_path.c_str());
915}
916#endif  // defined(OS_WIN)
917
918TEST_F(URLRequestTest, FileDirCancelTest) {
919  // Put in mock resource provider.
920  NetModule::SetResourceProvider(TestNetResourceProvider);
921
922  TestDelegate d;
923  {
924    base::FilePath file_path;
925    PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
926    file_path = file_path.Append(FILE_PATH_LITERAL("net"));
927    file_path = file_path.Append(FILE_PATH_LITERAL("data"));
928
929    URLRequest req(FilePathToFileURL(file_path), &d, &default_context_);
930    req.Start();
931    EXPECT_TRUE(req.is_pending());
932
933    d.set_cancel_in_received_data_pending(true);
934
935    base::MessageLoop::current()->Run();
936  }
937
938  // Take out mock resource provider.
939  NetModule::SetResourceProvider(NULL);
940}
941
942TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
943  // There is an implicit redirect when loading a file path that matches a
944  // directory and does not end with a slash.  Ensure that following such
945  // redirects does not crash.  See http://crbug.com/18686.
946
947  base::FilePath path;
948  PathService::Get(base::DIR_SOURCE_ROOT, &path);
949  path = path.Append(FILE_PATH_LITERAL("net"));
950  path = path.Append(FILE_PATH_LITERAL("data"));
951  path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
952
953  TestDelegate d;
954  URLRequest req(FilePathToFileURL(path), &d, &default_context_);
955  req.Start();
956  base::MessageLoop::current()->Run();
957
958  ASSERT_EQ(1, d.received_redirect_count());
959  ASSERT_LT(0, d.bytes_received());
960  ASSERT_FALSE(d.request_failed());
961  ASSERT_TRUE(req.status().is_success());
962}
963
964#if defined(OS_WIN)
965// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
966TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
967  TestDelegate d;
968  URLRequest req(GURL("file:///"), &d, &default_context_);
969  req.Start();
970  base::MessageLoop::current()->Run();
971
972  ASSERT_EQ(1, d.received_redirect_count());
973  ASSERT_FALSE(req.status().is_success());
974}
975#endif
976
977// Custom URLRequestJobs for use with interceptor tests
978class RestartTestJob : public URLRequestTestJob {
979 public:
980  RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
981    : URLRequestTestJob(request, network_delegate, true) {}
982 protected:
983  virtual void StartAsync() OVERRIDE {
984    this->NotifyRestartRequired();
985  }
986 private:
987  virtual ~RestartTestJob() {}
988};
989
990class CancelTestJob : public URLRequestTestJob {
991 public:
992  explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
993    : URLRequestTestJob(request, network_delegate, true) {}
994 protected:
995  virtual void StartAsync() OVERRIDE {
996    request_->Cancel();
997  }
998 private:
999  virtual ~CancelTestJob() {}
1000};
1001
1002class CancelThenRestartTestJob : public URLRequestTestJob {
1003 public:
1004  explicit CancelThenRestartTestJob(URLRequest* request,
1005                                    NetworkDelegate* network_delegate)
1006      : URLRequestTestJob(request, network_delegate, true) {
1007  }
1008 protected:
1009  virtual void StartAsync() OVERRIDE {
1010    request_->Cancel();
1011    this->NotifyRestartRequired();
1012  }
1013 private:
1014  virtual ~CancelThenRestartTestJob() {}
1015};
1016
1017// An Interceptor for use with interceptor tests
1018class TestInterceptor : URLRequest::Interceptor {
1019 public:
1020  TestInterceptor()
1021      : intercept_main_request_(false), restart_main_request_(false),
1022        cancel_main_request_(false), cancel_then_restart_main_request_(false),
1023        simulate_main_network_error_(false),
1024        intercept_redirect_(false), cancel_redirect_request_(false),
1025        intercept_final_response_(false), cancel_final_request_(false),
1026        did_intercept_main_(false), did_restart_main_(false),
1027        did_cancel_main_(false), did_cancel_then_restart_main_(false),
1028        did_simulate_error_main_(false),
1029        did_intercept_redirect_(false), did_cancel_redirect_(false),
1030        did_intercept_final_(false), did_cancel_final_(false) {
1031    URLRequest::Deprecated::RegisterRequestInterceptor(this);
1032  }
1033
1034  virtual ~TestInterceptor() {
1035    URLRequest::Deprecated::UnregisterRequestInterceptor(this);
1036  }
1037
1038  virtual URLRequestJob* MaybeIntercept(
1039      URLRequest* request,
1040      NetworkDelegate* network_delegate) OVERRIDE {
1041    if (restart_main_request_) {
1042      restart_main_request_ = false;
1043      did_restart_main_ = true;
1044      return new RestartTestJob(request, network_delegate);
1045    }
1046    if (cancel_main_request_) {
1047      cancel_main_request_ = false;
1048      did_cancel_main_ = true;
1049      return new CancelTestJob(request, network_delegate);
1050    }
1051    if (cancel_then_restart_main_request_) {
1052      cancel_then_restart_main_request_ = false;
1053      did_cancel_then_restart_main_ = true;
1054      return new CancelThenRestartTestJob(request, network_delegate);
1055    }
1056    if (simulate_main_network_error_) {
1057      simulate_main_network_error_ = false;
1058      did_simulate_error_main_ = true;
1059      // will error since the requeted url is not one of its canned urls
1060      return new URLRequestTestJob(request, network_delegate, true);
1061    }
1062    if (!intercept_main_request_)
1063      return NULL;
1064    intercept_main_request_ = false;
1065    did_intercept_main_ = true;
1066    URLRequestTestJob* job =  new URLRequestTestJob(request,
1067                                                    network_delegate,
1068                                                    main_headers_,
1069                                                    main_data_,
1070                                                    true);
1071    job->set_load_timing_info(main_request_load_timing_info_);
1072    return job;
1073  }
1074
1075  virtual URLRequestJob* MaybeInterceptRedirect(
1076      URLRequest* request,
1077      NetworkDelegate* network_delegate,
1078      const GURL& location) OVERRIDE {
1079    if (cancel_redirect_request_) {
1080      cancel_redirect_request_ = false;
1081      did_cancel_redirect_ = true;
1082      return new CancelTestJob(request, network_delegate);
1083    }
1084    if (!intercept_redirect_)
1085      return NULL;
1086    intercept_redirect_ = false;
1087    did_intercept_redirect_ = true;
1088    return new URLRequestTestJob(request,
1089                                 network_delegate,
1090                                 redirect_headers_,
1091                                 redirect_data_,
1092                                 true);
1093  }
1094
1095  virtual URLRequestJob* MaybeInterceptResponse(
1096      URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE {
1097    if (cancel_final_request_) {
1098      cancel_final_request_ = false;
1099      did_cancel_final_ = true;
1100      return new CancelTestJob(request, network_delegate);
1101    }
1102    if (!intercept_final_response_)
1103      return NULL;
1104    intercept_final_response_ = false;
1105    did_intercept_final_ = true;
1106    return new URLRequestTestJob(request,
1107                                 network_delegate,
1108                                 final_headers_,
1109                                 final_data_,
1110                                 true);
1111  }
1112
1113  // Whether to intercept the main request, and if so the response to return and
1114  // the LoadTimingInfo to use.
1115  bool intercept_main_request_;
1116  std::string main_headers_;
1117  std::string main_data_;
1118  LoadTimingInfo main_request_load_timing_info_;
1119
1120  // Other actions we take at MaybeIntercept time
1121  bool restart_main_request_;
1122  bool cancel_main_request_;
1123  bool cancel_then_restart_main_request_;
1124  bool simulate_main_network_error_;
1125
1126  // Whether to intercept redirects, and if so the response to return.
1127  bool intercept_redirect_;
1128  std::string redirect_headers_;
1129  std::string redirect_data_;
1130
1131  // Other actions we can take at MaybeInterceptRedirect time
1132  bool cancel_redirect_request_;
1133
1134  // Whether to intercept final response, and if so the response to return.
1135  bool intercept_final_response_;
1136  std::string final_headers_;
1137  std::string final_data_;
1138
1139  // Other actions we can take at MaybeInterceptResponse time
1140  bool cancel_final_request_;
1141
1142  // If we did something or not
1143  bool did_intercept_main_;
1144  bool did_restart_main_;
1145  bool did_cancel_main_;
1146  bool did_cancel_then_restart_main_;
1147  bool did_simulate_error_main_;
1148  bool did_intercept_redirect_;
1149  bool did_cancel_redirect_;
1150  bool did_intercept_final_;
1151  bool did_cancel_final_;
1152
1153  // Static getters for canned response header and data strings
1154
1155  static std::string ok_data() {
1156    return URLRequestTestJob::test_data_1();
1157  }
1158
1159  static std::string ok_headers() {
1160    return URLRequestTestJob::test_headers();
1161  }
1162
1163  static std::string redirect_data() {
1164    return std::string();
1165  }
1166
1167  static std::string redirect_headers() {
1168    return URLRequestTestJob::test_redirect_headers();
1169  }
1170
1171  static std::string error_data() {
1172    return std::string("ohhh nooooo mr. bill!");
1173  }
1174
1175  static std::string error_headers() {
1176    return URLRequestTestJob::test_error_headers();
1177  }
1178};
1179
1180TEST_F(URLRequestTest, Intercept) {
1181  TestInterceptor interceptor;
1182
1183  // intercept the main request and respond with a simple response
1184  interceptor.intercept_main_request_ = true;
1185  interceptor.main_headers_ = TestInterceptor::ok_headers();
1186  interceptor.main_data_ = TestInterceptor::ok_data();
1187
1188  TestDelegate d;
1189  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1190  base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1191  base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1192  base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
1193  req.SetUserData(NULL, user_data0);
1194  req.SetUserData(&user_data1, user_data1);
1195  req.SetUserData(&user_data2, user_data2);
1196  req.set_method("GET");
1197  req.Start();
1198  base::MessageLoop::current()->Run();
1199
1200  // Make sure we can retrieve our specific user data
1201  EXPECT_EQ(user_data0, req.GetUserData(NULL));
1202  EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
1203  EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
1204
1205  // Check the interceptor got called as expected
1206  EXPECT_TRUE(interceptor.did_intercept_main_);
1207
1208  // Check we got one good response
1209  EXPECT_TRUE(req.status().is_success());
1210  EXPECT_EQ(200, req.response_headers()->response_code());
1211  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1212  EXPECT_EQ(1, d.response_started_count());
1213  EXPECT_EQ(0, d.received_redirect_count());
1214}
1215
1216TEST_F(URLRequestTest, InterceptRedirect) {
1217  TestInterceptor interceptor;
1218
1219  // intercept the main request and respond with a redirect
1220  interceptor.intercept_main_request_ = true;
1221  interceptor.main_headers_ = TestInterceptor::redirect_headers();
1222  interceptor.main_data_ = TestInterceptor::redirect_data();
1223
1224  // intercept that redirect and respond a final OK response
1225  interceptor.intercept_redirect_ = true;
1226  interceptor.redirect_headers_ =  TestInterceptor::ok_headers();
1227  interceptor.redirect_data_ = TestInterceptor::ok_data();
1228
1229  TestDelegate d;
1230  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1231  req.set_method("GET");
1232  req.Start();
1233  base::MessageLoop::current()->Run();
1234
1235  // Check the interceptor got called as expected
1236  EXPECT_TRUE(interceptor.did_intercept_main_);
1237  EXPECT_TRUE(interceptor.did_intercept_redirect_);
1238
1239  // Check we got one good response
1240  EXPECT_TRUE(req.status().is_success());
1241  if (req.status().is_success()) {
1242    EXPECT_EQ(200, req.response_headers()->response_code());
1243  }
1244  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1245  EXPECT_EQ(1, d.response_started_count());
1246  EXPECT_EQ(0, d.received_redirect_count());
1247}
1248
1249TEST_F(URLRequestTest, InterceptServerError) {
1250  TestInterceptor interceptor;
1251
1252  // intercept the main request to generate a server error response
1253  interceptor.intercept_main_request_ = true;
1254  interceptor.main_headers_ = TestInterceptor::error_headers();
1255  interceptor.main_data_ = TestInterceptor::error_data();
1256
1257  // intercept that error and respond with an OK response
1258  interceptor.intercept_final_response_ = true;
1259  interceptor.final_headers_ = TestInterceptor::ok_headers();
1260  interceptor.final_data_ = TestInterceptor::ok_data();
1261
1262  TestDelegate d;
1263  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1264  req.set_method("GET");
1265  req.Start();
1266  base::MessageLoop::current()->Run();
1267
1268  // Check the interceptor got called as expected
1269  EXPECT_TRUE(interceptor.did_intercept_main_);
1270  EXPECT_TRUE(interceptor.did_intercept_final_);
1271
1272  // Check we got one good response
1273  EXPECT_TRUE(req.status().is_success());
1274  EXPECT_EQ(200, req.response_headers()->response_code());
1275  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1276  EXPECT_EQ(1, d.response_started_count());
1277  EXPECT_EQ(0, d.received_redirect_count());
1278}
1279
1280TEST_F(URLRequestTest, InterceptNetworkError) {
1281  TestInterceptor interceptor;
1282
1283  // intercept the main request to simulate a network error
1284  interceptor.simulate_main_network_error_ = true;
1285
1286  // intercept that error and respond with an OK response
1287  interceptor.intercept_final_response_ = true;
1288  interceptor.final_headers_ = TestInterceptor::ok_headers();
1289  interceptor.final_data_ = TestInterceptor::ok_data();
1290
1291  TestDelegate d;
1292  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1293  req.set_method("GET");
1294  req.Start();
1295  base::MessageLoop::current()->Run();
1296
1297  // Check the interceptor got called as expected
1298  EXPECT_TRUE(interceptor.did_simulate_error_main_);
1299  EXPECT_TRUE(interceptor.did_intercept_final_);
1300
1301  // Check we received one good response
1302  EXPECT_TRUE(req.status().is_success());
1303  EXPECT_EQ(200, req.response_headers()->response_code());
1304  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1305  EXPECT_EQ(1, d.response_started_count());
1306  EXPECT_EQ(0, d.received_redirect_count());
1307}
1308
1309TEST_F(URLRequestTest, InterceptRestartRequired) {
1310  TestInterceptor interceptor;
1311
1312  // restart the main request
1313  interceptor.restart_main_request_ = true;
1314
1315  // then intercept the new main request and respond with an OK response
1316  interceptor.intercept_main_request_ = true;
1317  interceptor.main_headers_ = TestInterceptor::ok_headers();
1318  interceptor.main_data_ = TestInterceptor::ok_data();
1319
1320  TestDelegate d;
1321  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1322  req.set_method("GET");
1323  req.Start();
1324  base::MessageLoop::current()->Run();
1325
1326  // Check the interceptor got called as expected
1327  EXPECT_TRUE(interceptor.did_restart_main_);
1328  EXPECT_TRUE(interceptor.did_intercept_main_);
1329
1330  // Check we received one good response
1331  EXPECT_TRUE(req.status().is_success());
1332  if (req.status().is_success()) {
1333    EXPECT_EQ(200, req.response_headers()->response_code());
1334  }
1335  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1336  EXPECT_EQ(1, d.response_started_count());
1337  EXPECT_EQ(0, d.received_redirect_count());
1338}
1339
1340TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1341  TestInterceptor interceptor;
1342
1343  // intercept the main request and cancel from within the restarted job
1344  interceptor.cancel_main_request_ = true;
1345
1346  // setup to intercept final response and override it with an OK response
1347  interceptor.intercept_final_response_ = true;
1348  interceptor.final_headers_ = TestInterceptor::ok_headers();
1349  interceptor.final_data_ = TestInterceptor::ok_data();
1350
1351  TestDelegate d;
1352  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1353  req.set_method("GET");
1354  req.Start();
1355  base::MessageLoop::current()->Run();
1356
1357  // Check the interceptor got called as expected
1358  EXPECT_TRUE(interceptor.did_cancel_main_);
1359  EXPECT_FALSE(interceptor.did_intercept_final_);
1360
1361  // Check we see a canceled request
1362  EXPECT_FALSE(req.status().is_success());
1363  EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1364}
1365
1366TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1367  TestInterceptor interceptor;
1368
1369  // intercept the main request and respond with a redirect
1370  interceptor.intercept_main_request_ = true;
1371  interceptor.main_headers_ = TestInterceptor::redirect_headers();
1372  interceptor.main_data_ = TestInterceptor::redirect_data();
1373
1374  // intercept the redirect and cancel from within that job
1375  interceptor.cancel_redirect_request_ = true;
1376
1377  // setup to intercept final response and override it with an OK response
1378  interceptor.intercept_final_response_ = true;
1379  interceptor.final_headers_ = TestInterceptor::ok_headers();
1380  interceptor.final_data_ = TestInterceptor::ok_data();
1381
1382  TestDelegate d;
1383  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1384  req.set_method("GET");
1385  req.Start();
1386  base::MessageLoop::current()->Run();
1387
1388  // Check the interceptor got called as expected
1389  EXPECT_TRUE(interceptor.did_intercept_main_);
1390  EXPECT_TRUE(interceptor.did_cancel_redirect_);
1391  EXPECT_FALSE(interceptor.did_intercept_final_);
1392
1393  // Check we see a canceled request
1394  EXPECT_FALSE(req.status().is_success());
1395  EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1396}
1397
1398TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1399  TestInterceptor interceptor;
1400
1401  // intercept the main request to simulate a network error
1402  interceptor.simulate_main_network_error_ = true;
1403
1404  // setup to intercept final response and cancel from within that job
1405  interceptor.cancel_final_request_ = true;
1406
1407  TestDelegate d;
1408  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1409  req.set_method("GET");
1410  req.Start();
1411  base::MessageLoop::current()->Run();
1412
1413  // Check the interceptor got called as expected
1414  EXPECT_TRUE(interceptor.did_simulate_error_main_);
1415  EXPECT_TRUE(interceptor.did_cancel_final_);
1416
1417  // Check we see a canceled request
1418  EXPECT_FALSE(req.status().is_success());
1419  EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1420}
1421
1422TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1423  TestInterceptor interceptor;
1424
1425  // intercept the main request and cancel then restart from within that job
1426  interceptor.cancel_then_restart_main_request_ = true;
1427
1428  // setup to intercept final response and override it with an OK response
1429  interceptor.intercept_final_response_ = true;
1430  interceptor.final_headers_ = TestInterceptor::ok_headers();
1431  interceptor.final_data_ = TestInterceptor::ok_data();
1432
1433  TestDelegate d;
1434  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1435  req.set_method("GET");
1436  req.Start();
1437  base::MessageLoop::current()->Run();
1438
1439  // Check the interceptor got called as expected
1440  EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1441  EXPECT_FALSE(interceptor.did_intercept_final_);
1442
1443  // Check we see a canceled request
1444  EXPECT_FALSE(req.status().is_success());
1445  EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1446}
1447
1448LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing,
1449                                 URLRequestContext* context) {
1450  TestInterceptor interceptor;
1451  interceptor.intercept_main_request_ = true;
1452  interceptor.main_request_load_timing_info_ = job_load_timing;
1453  TestDelegate d;
1454  URLRequest req(GURL("http://test_intercept/foo"), &d, context);
1455  req.Start();
1456  base::MessageLoop::current()->Run();
1457
1458  LoadTimingInfo resulting_load_timing;
1459  req.GetLoadTimingInfo(&resulting_load_timing);
1460
1461  // None of these should be modified by the URLRequest.
1462  EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused);
1463  EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id);
1464  EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start);
1465  EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end);
1466  EXPECT_EQ(job_load_timing.receive_headers_end,
1467            resulting_load_timing.receive_headers_end);
1468
1469  return resulting_load_timing;
1470}
1471
1472// "Normal" LoadTimingInfo as returned by a job.  Everything is in order, not
1473// reused.  |connect_time_flags| is used to indicate if there should be dns
1474// or SSL times, and |used_proxy| is used for proxy times.
1475LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now,
1476                                    int connect_time_flags,
1477                                    bool used_proxy) {
1478  LoadTimingInfo load_timing;
1479  load_timing.socket_log_id = 1;
1480
1481  if (used_proxy) {
1482    load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1483    load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1484  }
1485
1486  LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing;
1487  if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) {
1488    connect_timing.dns_start = now + base::TimeDelta::FromDays(3);
1489    connect_timing.dns_end = now + base::TimeDelta::FromDays(4);
1490  }
1491  connect_timing.connect_start = now + base::TimeDelta::FromDays(5);
1492  if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) {
1493    connect_timing.ssl_start = now + base::TimeDelta::FromDays(6);
1494    connect_timing.ssl_end = now + base::TimeDelta::FromDays(7);
1495  }
1496  connect_timing.connect_end = now + base::TimeDelta::FromDays(8);
1497
1498  load_timing.send_start = now + base::TimeDelta::FromDays(9);
1499  load_timing.send_end = now + base::TimeDelta::FromDays(10);
1500  load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1501  return load_timing;
1502}
1503
1504// Same as above, but in the case of a reused socket.
1505LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now,
1506                                          bool used_proxy) {
1507  LoadTimingInfo load_timing;
1508  load_timing.socket_log_id = 1;
1509  load_timing.socket_reused = true;
1510
1511  if (used_proxy) {
1512    load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1513    load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1514  }
1515
1516  load_timing.send_start = now + base::TimeDelta::FromDays(9);
1517  load_timing.send_end = now + base::TimeDelta::FromDays(10);
1518  load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1519  return load_timing;
1520}
1521
1522// Basic test that the intercept + load timing tests work.
1523TEST_F(URLRequestTest, InterceptLoadTiming) {
1524  base::TimeTicks now = base::TimeTicks::Now();
1525  LoadTimingInfo job_load_timing =
1526      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false);
1527
1528  LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1529                                                        &default_context_);
1530
1531  // Nothing should have been changed by the URLRequest.
1532  EXPECT_EQ(job_load_timing.proxy_resolve_start,
1533            load_timing_result.proxy_resolve_start);
1534  EXPECT_EQ(job_load_timing.proxy_resolve_end,
1535            load_timing_result.proxy_resolve_end);
1536  EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1537            load_timing_result.connect_timing.dns_start);
1538  EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1539            load_timing_result.connect_timing.dns_end);
1540  EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1541            load_timing_result.connect_timing.connect_start);
1542  EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1543            load_timing_result.connect_timing.connect_end);
1544  EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1545            load_timing_result.connect_timing.ssl_start);
1546  EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1547            load_timing_result.connect_timing.ssl_end);
1548
1549  // Redundant sanity check.
1550  TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES);
1551}
1552
1553// Another basic test, with proxy and SSL times, but no DNS times.
1554TEST_F(URLRequestTest, InterceptLoadTimingProxy) {
1555  base::TimeTicks now = base::TimeTicks::Now();
1556  LoadTimingInfo job_load_timing =
1557      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true);
1558
1559  LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1560                                                        &default_context_);
1561
1562  // Nothing should have been changed by the URLRequest.
1563  EXPECT_EQ(job_load_timing.proxy_resolve_start,
1564            load_timing_result.proxy_resolve_start);
1565  EXPECT_EQ(job_load_timing.proxy_resolve_end,
1566            load_timing_result.proxy_resolve_end);
1567  EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1568            load_timing_result.connect_timing.dns_start);
1569  EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1570            load_timing_result.connect_timing.dns_end);
1571  EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1572            load_timing_result.connect_timing.connect_start);
1573  EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1574            load_timing_result.connect_timing.connect_end);
1575  EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1576            load_timing_result.connect_timing.ssl_start);
1577  EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1578            load_timing_result.connect_timing.ssl_end);
1579
1580  // Redundant sanity check.
1581  TestLoadTimingNotReusedWithProxy(load_timing_result,
1582                                   CONNECT_TIMING_HAS_SSL_TIMES);
1583}
1584
1585// Make sure that URLRequest correctly adjusts proxy times when they're before
1586// |request_start|, due to already having a connected socket.  This happens in
1587// the case of reusing a SPDY session or HTTP pipeline.  The connected socket is
1588// not considered reused in this test (May be a preconnect).
1589//
1590// To mix things up from the test above, assumes DNS times but no SSL times.
1591TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) {
1592  base::TimeTicks now = base::TimeTicks::Now();
1593  LoadTimingInfo job_load_timing =
1594      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true);
1595  job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6);
1596  job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5);
1597  job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4);
1598  job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3);
1599  job_load_timing.connect_timing.connect_start =
1600      now - base::TimeDelta::FromDays(2);
1601  job_load_timing.connect_timing.connect_end =
1602      now - base::TimeDelta::FromDays(1);
1603
1604  LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1605                                                        &default_context_);
1606
1607  // Proxy times, connect times, and DNS times should all be replaced with
1608  // request_start.
1609  EXPECT_EQ(load_timing_result.request_start,
1610            load_timing_result.proxy_resolve_start);
1611  EXPECT_EQ(load_timing_result.request_start,
1612            load_timing_result.proxy_resolve_end);
1613  EXPECT_EQ(load_timing_result.request_start,
1614            load_timing_result.connect_timing.dns_start);
1615  EXPECT_EQ(load_timing_result.request_start,
1616            load_timing_result.connect_timing.dns_end);
1617  EXPECT_EQ(load_timing_result.request_start,
1618            load_timing_result.connect_timing.connect_start);
1619  EXPECT_EQ(load_timing_result.request_start,
1620            load_timing_result.connect_timing.connect_end);
1621
1622  // Other times should have been left null.
1623  TestLoadTimingNotReusedWithProxy(load_timing_result,
1624                                   CONNECT_TIMING_HAS_DNS_TIMES);
1625}
1626
1627// Same as above, but in the reused case.
1628TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) {
1629  base::TimeTicks now = base::TimeTicks::Now();
1630  LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true);
1631  job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4);
1632  job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3);
1633
1634  LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1635                                                        &default_context_);
1636
1637  // Proxy times and connect times should all be replaced with request_start.
1638  EXPECT_EQ(load_timing_result.request_start,
1639            load_timing_result.proxy_resolve_start);
1640  EXPECT_EQ(load_timing_result.request_start,
1641            load_timing_result.proxy_resolve_end);
1642
1643  // Other times should have been left null.
1644  TestLoadTimingReusedWithProxy(load_timing_result);
1645}
1646
1647// Make sure that URLRequest correctly adjusts connect times when they're before
1648// |request_start|, due to reusing a connected socket.  The connected socket is
1649// not considered reused in this test (May be a preconnect).
1650//
1651// To mix things up, the request has SSL times, but no DNS times.
1652TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) {
1653  base::TimeTicks now = base::TimeTicks::Now();
1654  LoadTimingInfo job_load_timing =
1655      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false);
1656  job_load_timing.connect_timing.connect_start =
1657      now - base::TimeDelta::FromDays(1);
1658  job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2);
1659  job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3);
1660  job_load_timing.connect_timing.connect_end =
1661      now - base::TimeDelta::FromDays(4);
1662
1663  LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1664                                                        &default_context_);
1665
1666  // Connect times, and SSL times should be replaced with request_start.
1667  EXPECT_EQ(load_timing_result.request_start,
1668            load_timing_result.connect_timing.connect_start);
1669  EXPECT_EQ(load_timing_result.request_start,
1670            load_timing_result.connect_timing.ssl_start);
1671  EXPECT_EQ(load_timing_result.request_start,
1672            load_timing_result.connect_timing.ssl_end);
1673  EXPECT_EQ(load_timing_result.request_start,
1674            load_timing_result.connect_timing.connect_end);
1675
1676  // Other times should have been left null.
1677  TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES);
1678}
1679
1680// Make sure that URLRequest correctly adjusts connect times when they're before
1681// |request_start|, due to reusing a connected socket in the case that there
1682// are also proxy times.  The connected socket is not considered reused in this
1683// test (May be a preconnect).
1684//
1685// In this test, there are no SSL or DNS times.
1686TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) {
1687  base::TimeTicks now = base::TimeTicks::Now();
1688  LoadTimingInfo job_load_timing =
1689      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true);
1690  job_load_timing.connect_timing.connect_start =
1691      now - base::TimeDelta::FromDays(1);
1692  job_load_timing.connect_timing.connect_end =
1693      now - base::TimeDelta::FromDays(2);
1694
1695  LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1696                                                        &default_context_);
1697
1698  // Connect times should be replaced with proxy_resolve_end.
1699  EXPECT_EQ(load_timing_result.proxy_resolve_end,
1700            load_timing_result.connect_timing.connect_start);
1701  EXPECT_EQ(load_timing_result.proxy_resolve_end,
1702            load_timing_result.connect_timing.connect_end);
1703
1704  // Other times should have been left null.
1705  TestLoadTimingNotReusedWithProxy(load_timing_result,
1706                                   CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
1707}
1708
1709// Check that two different URL requests have different identifiers.
1710TEST_F(URLRequestTest, Identifiers) {
1711  TestDelegate d;
1712  TestURLRequestContext context;
1713  TestURLRequest req(GURL("http://example.com"), &d, &context, NULL);
1714  TestURLRequest other_req(GURL("http://example.com"), &d, &context, NULL);
1715
1716  ASSERT_NE(req.identifier(), other_req.identifier());
1717}
1718
1719// Check that a failure to connect to the proxy is reported to the network
1720// delegate.
1721TEST_F(URLRequestTest, NetworkDelegateProxyError) {
1722  MockHostResolver host_resolver;
1723  host_resolver.rules()->AddSimulatedFailure("*");
1724
1725  TestNetworkDelegate network_delegate;  // Must outlive URLRequests.
1726  TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
1727
1728  TestDelegate d;
1729  URLRequest req(GURL("http://example.com"), &d, &context);
1730  req.set_method("GET");
1731
1732  req.Start();
1733  base::MessageLoop::current()->Run();
1734
1735  // Check we see a failed request.
1736  EXPECT_FALSE(req.status().is_success());
1737  EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1738  EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error());
1739
1740  EXPECT_EQ(1, network_delegate.error_count());
1741  EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
1742  EXPECT_EQ(1, network_delegate.completed_requests());
1743}
1744
1745// Make sure that net::NetworkDelegate::NotifyCompleted is called if
1746// content is empty.
1747TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
1748  TestDelegate d;
1749  URLRequest req(GURL("data:,"), &d, &default_context_);
1750  req.Start();
1751  base::MessageLoop::current()->Run();
1752  EXPECT_EQ("", d.data_received());
1753  EXPECT_EQ(1, default_network_delegate_.completed_requests());
1754}
1755
1756// Make sure that SetPriority actually sets the URLRequest's priority
1757// correctly, both before and after start.
1758TEST_F(URLRequestTest, SetPriorityBasic) {
1759  TestDelegate d;
1760  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1761  EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1762
1763  req.SetPriority(LOW);
1764  EXPECT_EQ(LOW, req.priority());
1765
1766  req.Start();
1767  EXPECT_EQ(LOW, req.priority());
1768
1769  req.SetPriority(MEDIUM);
1770  EXPECT_EQ(MEDIUM, req.priority());
1771}
1772
1773// Make sure that URLRequest calls SetPriority on a job before calling
1774// Start on it.
1775TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) {
1776  TestDelegate d;
1777  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1778  EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1779
1780  scoped_refptr<URLRequestTestJob> job =
1781      new URLRequestTestJob(&req, &default_network_delegate_);
1782  AddTestInterceptor()->set_main_intercept_job(job.get());
1783  EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
1784
1785  req.SetPriority(LOW);
1786
1787  req.Start();
1788  EXPECT_EQ(LOW, job->priority());
1789}
1790
1791// Make sure that URLRequest passes on its priority updates to its
1792// job.
1793TEST_F(URLRequestTest, SetJobPriority) {
1794  TestDelegate d;
1795  URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1796
1797  scoped_refptr<URLRequestTestJob> job =
1798      new URLRequestTestJob(&req, &default_network_delegate_);
1799  AddTestInterceptor()->set_main_intercept_job(job.get());
1800
1801  req.SetPriority(LOW);
1802  req.Start();
1803  EXPECT_EQ(LOW, job->priority());
1804
1805  req.SetPriority(MEDIUM);
1806  EXPECT_EQ(MEDIUM, req.priority());
1807  EXPECT_EQ(MEDIUM, job->priority());
1808}
1809
1810// TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
1811#if !defined(OS_IOS)
1812// A subclass of SpawnedTestServer that uses a statically-configured hostname.
1813// This is to work around mysterious failures in chrome_frame_net_tests. See:
1814// http://crbug.com/114369
1815class LocalHttpTestServer : public SpawnedTestServer {
1816 public:
1817  explicit LocalHttpTestServer(const base::FilePath& document_root)
1818      : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1819                          ScopedCustomUrlRequestTestHttpHost::value(),
1820                          document_root) {}
1821  LocalHttpTestServer()
1822      : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1823                          ScopedCustomUrlRequestTestHttpHost::value(),
1824                          base::FilePath()) {}
1825};
1826
1827TEST_F(URLRequestTest, DelayedCookieCallback) {
1828  LocalHttpTestServer test_server;
1829  ASSERT_TRUE(test_server.Start());
1830
1831  TestURLRequestContext context;
1832  scoped_refptr<DelayedCookieMonster> delayed_cm =
1833      new DelayedCookieMonster();
1834  scoped_refptr<CookieStore> cookie_store = delayed_cm;
1835  context.set_cookie_store(delayed_cm.get());
1836
1837  // Set up a cookie.
1838  {
1839    TestNetworkDelegate network_delegate;
1840    context.set_network_delegate(&network_delegate);
1841    TestDelegate d;
1842    URLRequest req(
1843        test_server.GetURL("set-cookie?CookieToNotSend=1"), &d, &context);
1844    req.Start();
1845    base::MessageLoop::current()->Run();
1846    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1847    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1848    EXPECT_EQ(1, network_delegate.set_cookie_count());
1849  }
1850
1851  // Verify that the cookie is set.
1852  {
1853    TestNetworkDelegate network_delegate;
1854    context.set_network_delegate(&network_delegate);
1855    TestDelegate d;
1856    URLRequest req(test_server.GetURL("echoheader?Cookie"), &d, &context);
1857    req.Start();
1858    base::MessageLoop::current()->Run();
1859
1860    EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1861                != std::string::npos);
1862    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1863    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1864  }
1865}
1866
1867TEST_F(URLRequestTest, DoNotSendCookies) {
1868  LocalHttpTestServer test_server;
1869  ASSERT_TRUE(test_server.Start());
1870
1871  // Set up a cookie.
1872  {
1873    TestNetworkDelegate network_delegate;
1874    default_context_.set_network_delegate(&network_delegate);
1875    TestDelegate d;
1876    URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
1877                   &d,
1878                   &default_context_);
1879    req.Start();
1880    base::MessageLoop::current()->Run();
1881    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1882    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1883  }
1884
1885  // Verify that the cookie is set.
1886  {
1887    TestNetworkDelegate network_delegate;
1888    default_context_.set_network_delegate(&network_delegate);
1889    TestDelegate d;
1890    URLRequest req(
1891        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1892    req.Start();
1893    base::MessageLoop::current()->Run();
1894
1895    EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1896                != std::string::npos);
1897    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1898    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1899  }
1900
1901  // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
1902  {
1903    TestNetworkDelegate network_delegate;
1904    default_context_.set_network_delegate(&network_delegate);
1905    TestDelegate d;
1906    URLRequest req(
1907        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1908    req.set_load_flags(LOAD_DO_NOT_SEND_COOKIES);
1909    req.Start();
1910    base::MessageLoop::current()->Run();
1911
1912    EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1913                == std::string::npos);
1914
1915    // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
1916    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1917    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1918  }
1919}
1920
1921TEST_F(URLRequestTest, DoNotSaveCookies) {
1922  LocalHttpTestServer test_server;
1923  ASSERT_TRUE(test_server.Start());
1924
1925  // Set up a cookie.
1926  {
1927    TestNetworkDelegate network_delegate;
1928    default_context_.set_network_delegate(&network_delegate);
1929    TestDelegate d;
1930    URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
1931                   &d,
1932                   &default_context_);
1933    req.Start();
1934    base::MessageLoop::current()->Run();
1935
1936    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1937    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1938    EXPECT_EQ(1, network_delegate.set_cookie_count());
1939  }
1940
1941  // Try to set-up another cookie and update the previous cookie.
1942  {
1943    TestNetworkDelegate network_delegate;
1944    default_context_.set_network_delegate(&network_delegate);
1945    TestDelegate d;
1946    URLRequest req(
1947        test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
1948        &d,
1949        &default_context_);
1950    req.set_load_flags(LOAD_DO_NOT_SAVE_COOKIES);
1951    req.Start();
1952
1953    base::MessageLoop::current()->Run();
1954
1955    // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
1956    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1957    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1958    EXPECT_EQ(0, network_delegate.set_cookie_count());
1959  }
1960
1961  // Verify the cookies weren't saved or updated.
1962  {
1963    TestNetworkDelegate network_delegate;
1964    default_context_.set_network_delegate(&network_delegate);
1965    TestDelegate d;
1966    URLRequest req(
1967        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1968    req.Start();
1969    base::MessageLoop::current()->Run();
1970
1971    EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1972                == std::string::npos);
1973    EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1974                != std::string::npos);
1975
1976    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1977    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1978    EXPECT_EQ(0, network_delegate.set_cookie_count());
1979  }
1980}
1981
1982TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
1983  LocalHttpTestServer test_server;
1984  ASSERT_TRUE(test_server.Start());
1985
1986  // Set up a cookie.
1987  {
1988    TestNetworkDelegate network_delegate;
1989    default_context_.set_network_delegate(&network_delegate);
1990    TestDelegate d;
1991    URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
1992                   &d,
1993                   &default_context_);
1994    req.Start();
1995    base::MessageLoop::current()->Run();
1996
1997    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1998    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1999  }
2000
2001  // Verify that the cookie is set.
2002  {
2003    TestNetworkDelegate network_delegate;
2004    default_context_.set_network_delegate(&network_delegate);
2005    TestDelegate d;
2006    URLRequest req(
2007        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2008    req.Start();
2009    base::MessageLoop::current()->Run();
2010
2011    EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2012                != std::string::npos);
2013
2014    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2015    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2016  }
2017
2018  // Verify that the cookie isn't sent.
2019  {
2020    TestNetworkDelegate network_delegate;
2021    default_context_.set_network_delegate(&network_delegate);
2022    TestDelegate d;
2023    network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
2024    URLRequest req(
2025        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2026    req.Start();
2027    base::MessageLoop::current()->Run();
2028
2029    EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2030                == std::string::npos);
2031
2032    EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2033    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2034  }
2035}
2036
2037TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
2038  LocalHttpTestServer test_server;
2039  ASSERT_TRUE(test_server.Start());
2040
2041  // Set up a cookie.
2042  {
2043    TestNetworkDelegate network_delegate;
2044    default_context_.set_network_delegate(&network_delegate);
2045    TestDelegate d;
2046    URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2047                   &d,
2048                   &default_context_);
2049    req.Start();
2050    base::MessageLoop::current()->Run();
2051
2052    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2053    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2054  }
2055
2056  // Try to set-up another cookie and update the previous cookie.
2057  {
2058    TestNetworkDelegate network_delegate;
2059    default_context_.set_network_delegate(&network_delegate);
2060    TestDelegate d;
2061    network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2062    URLRequest req(
2063        test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2064        &d,
2065        &default_context_);
2066    req.Start();
2067
2068    base::MessageLoop::current()->Run();
2069
2070    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2071    EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2072  }
2073
2074  // Verify the cookies weren't saved or updated.
2075  {
2076    TestNetworkDelegate network_delegate;
2077    default_context_.set_network_delegate(&network_delegate);
2078    TestDelegate d;
2079    URLRequest req(
2080        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2081    req.Start();
2082    base::MessageLoop::current()->Run();
2083
2084    EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2085                == std::string::npos);
2086    EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2087                != std::string::npos);
2088
2089    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2090    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2091  }
2092}
2093
2094TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
2095  LocalHttpTestServer test_server;
2096  ASSERT_TRUE(test_server.Start());
2097
2098  // Set up an empty cookie.
2099  {
2100    TestNetworkDelegate network_delegate;
2101    default_context_.set_network_delegate(&network_delegate);
2102    TestDelegate d;
2103    URLRequest req(test_server.GetURL("set-cookie"), &d, &default_context_);
2104    req.Start();
2105    base::MessageLoop::current()->Run();
2106
2107    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2108    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2109    EXPECT_EQ(0, network_delegate.set_cookie_count());
2110  }
2111}
2112
2113TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
2114  LocalHttpTestServer test_server;
2115  ASSERT_TRUE(test_server.Start());
2116
2117  // Set up a cookie.
2118  {
2119    TestNetworkDelegate network_delegate;
2120    default_context_.set_network_delegate(&network_delegate);
2121    TestDelegate d;
2122    URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2123                   &d,
2124                   &default_context_);
2125    req.Start();
2126    base::MessageLoop::current()->Run();
2127
2128    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2129    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2130  }
2131
2132  // Verify that the cookie is set.
2133  {
2134    TestNetworkDelegate network_delegate;
2135    default_context_.set_network_delegate(&network_delegate);
2136    TestDelegate d;
2137    URLRequest req(
2138        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2139    req.Start();
2140    base::MessageLoop::current()->Run();
2141
2142    EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2143                != std::string::npos);
2144
2145    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2146    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2147  }
2148
2149  // Verify that the cookie isn't sent.
2150  {
2151    TestNetworkDelegate network_delegate;
2152    default_context_.set_network_delegate(&network_delegate);
2153    TestDelegate d;
2154    network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
2155    URLRequest req(
2156        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2157    req.Start();
2158    base::MessageLoop::current()->Run();
2159
2160    EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2161                == std::string::npos);
2162
2163    EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2164    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2165  }
2166}
2167
2168TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
2169  LocalHttpTestServer test_server;
2170  ASSERT_TRUE(test_server.Start());
2171
2172  // Set up a cookie.
2173  {
2174    TestNetworkDelegate network_delegate;
2175    default_context_.set_network_delegate(&network_delegate);
2176    TestDelegate d;
2177    URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2178                   &d,
2179                   &default_context_);
2180    req.Start();
2181    base::MessageLoop::current()->Run();
2182
2183    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2184    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2185  }
2186
2187  // Try to set-up another cookie and update the previous cookie.
2188  {
2189    TestNetworkDelegate network_delegate;
2190    default_context_.set_network_delegate(&network_delegate);
2191    TestDelegate d;
2192    network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2193    URLRequest req(
2194        test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2195        &d,
2196        &default_context_);
2197    req.Start();
2198
2199    base::MessageLoop::current()->Run();
2200
2201    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2202    EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2203  }
2204
2205  // Verify the cookies weren't saved or updated.
2206  {
2207    TestNetworkDelegate network_delegate;
2208    default_context_.set_network_delegate(&network_delegate);
2209    TestDelegate d;
2210    URLRequest req(
2211        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2212    req.Start();
2213    base::MessageLoop::current()->Run();
2214
2215    EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2216                == std::string::npos);
2217    EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2218                != std::string::npos);
2219
2220    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2221    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2222  }
2223}
2224
2225// FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2226// value for the |fixed_date| argument given to the constructor.
2227class FixedDateNetworkDelegate : public TestNetworkDelegate {
2228 public:
2229  explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2230      : fixed_date_(fixed_date) {}
2231  virtual ~FixedDateNetworkDelegate() {}
2232
2233  // net::NetworkDelegate implementation
2234  virtual int OnHeadersReceived(
2235      net::URLRequest* request,
2236      const net::CompletionCallback& callback,
2237      const net::HttpResponseHeaders* original_response_headers,
2238      scoped_refptr<net::HttpResponseHeaders>* override_response_headers)
2239      OVERRIDE;
2240
2241 private:
2242  std::string fixed_date_;
2243
2244  DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2245};
2246
2247int FixedDateNetworkDelegate::OnHeadersReceived(
2248    net::URLRequest* request,
2249    const net::CompletionCallback& callback,
2250    const net::HttpResponseHeaders* original_response_headers,
2251    scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
2252  net::HttpResponseHeaders* new_response_headers =
2253      new net::HttpResponseHeaders(original_response_headers->raw_headers());
2254
2255  new_response_headers->RemoveHeader("Date");
2256  new_response_headers->AddHeader("Date: " + fixed_date_);
2257
2258  *override_response_headers = new_response_headers;
2259  return TestNetworkDelegate::OnHeadersReceived(request,
2260                                                callback,
2261                                                original_response_headers,
2262                                                override_response_headers);
2263}
2264
2265// Test that cookie expiration times are adjusted for server/client clock
2266// skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2267// headers by defaulting to GMT. (crbug.com/135131)
2268TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2269  LocalHttpTestServer test_server;
2270  ASSERT_TRUE(test_server.Start());
2271
2272  // Set up an expired cookie.
2273  {
2274    TestNetworkDelegate network_delegate;
2275    default_context_.set_network_delegate(&network_delegate);
2276    TestDelegate d;
2277    URLRequest req(test_server.GetURL(
2278        "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2279        &d,
2280        &default_context_);
2281    req.Start();
2282    base::MessageLoop::current()->Run();
2283  }
2284  // Verify that the cookie is not set.
2285  {
2286    TestNetworkDelegate network_delegate;
2287    default_context_.set_network_delegate(&network_delegate);
2288    TestDelegate d;
2289    URLRequest req(
2290        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2291    req.Start();
2292    base::MessageLoop::current()->Run();
2293
2294    EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
2295  }
2296  // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2297  {
2298    FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
2299    default_context_.set_network_delegate(&network_delegate);
2300    TestDelegate d;
2301    URLRequest req(test_server.GetURL(
2302        "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2303        &d,
2304        &default_context_);
2305    req.Start();
2306    base::MessageLoop::current()->Run();
2307  }
2308  // Verify that the cookie is set.
2309  {
2310    TestNetworkDelegate network_delegate;
2311    default_context_.set_network_delegate(&network_delegate);
2312    TestDelegate d;
2313    URLRequest req(
2314        test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2315    req.Start();
2316    base::MessageLoop::current()->Run();
2317
2318    EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
2319  }
2320}
2321
2322
2323// Check that it is impossible to change the referrer in the extra headers of
2324// an URLRequest.
2325TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2326  LocalHttpTestServer test_server;
2327  ASSERT_TRUE(test_server.Start());
2328
2329  // If extra headers contain referer and the request contains a referer,
2330  // only the latter shall be respected.
2331  {
2332    TestDelegate d;
2333    URLRequest req(
2334        test_server.GetURL("echoheader?Referer"), &d, &default_context_);
2335    req.SetReferrer("http://foo.com/");
2336
2337    HttpRequestHeaders headers;
2338    headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2339    req.SetExtraRequestHeaders(headers);
2340
2341    req.Start();
2342    base::MessageLoop::current()->Run();
2343
2344    EXPECT_EQ("http://foo.com/", d.data_received());
2345  }
2346
2347  // If extra headers contain a referer but the request does not, no referer
2348  // shall be sent in the header.
2349  {
2350    TestDelegate d;
2351    URLRequest req(
2352        test_server.GetURL("echoheader?Referer"), &d, &default_context_);
2353
2354    HttpRequestHeaders headers;
2355    headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2356    req.SetExtraRequestHeaders(headers);
2357    req.set_load_flags(LOAD_VALIDATE_CACHE);
2358
2359    req.Start();
2360    base::MessageLoop::current()->Run();
2361
2362    EXPECT_EQ("None", d.data_received());
2363  }
2364}
2365
2366class URLRequestTestHTTP : public URLRequestTest {
2367 public:
2368  URLRequestTestHTTP()
2369      : test_server_(base::FilePath(FILE_PATH_LITERAL(
2370                                  "net/data/url_request_unittest"))) {
2371  }
2372
2373 protected:
2374  // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2375  // |request_method| is the method to use for the initial request.
2376  // |redirect_method| is the method that is expected to be used for the second
2377  // request, after redirection.
2378  // If |include_data| is true, data is uploaded with the request.  The
2379  // response body is expected to match it exactly, if and only if
2380  // |request_method| == |redirect_method|.
2381  void HTTPRedirectMethodTest(const GURL& redirect_url,
2382                              const std::string& request_method,
2383                              const std::string& redirect_method,
2384                              bool include_data) {
2385    static const char kData[] = "hello world";
2386    TestDelegate d;
2387    URLRequest req(redirect_url, &d, &default_context_);
2388    req.set_method(request_method);
2389    if (include_data) {
2390      req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
2391      HttpRequestHeaders headers;
2392      headers.SetHeader(HttpRequestHeaders::kContentLength,
2393                        base::UintToString(arraysize(kData) - 1));
2394      req.SetExtraRequestHeaders(headers);
2395    }
2396    req.Start();
2397    base::MessageLoop::current()->Run();
2398    EXPECT_EQ(redirect_method, req.method());
2399    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
2400    EXPECT_EQ(OK, req.status().error());
2401    if (include_data) {
2402      if (request_method == redirect_method) {
2403        EXPECT_EQ(kData, d.data_received());
2404      } else {
2405        EXPECT_NE(kData, d.data_received());
2406      }
2407    }
2408    if (HasFailure())
2409      LOG(WARNING) << "Request method was: " << request_method;
2410  }
2411
2412  void HTTPUploadDataOperationTest(const std::string& method) {
2413    const int kMsgSize = 20000;  // multiple of 10
2414    const int kIterations = 50;
2415    char* uploadBytes = new char[kMsgSize+1];
2416    char* ptr = uploadBytes;
2417    char marker = 'a';
2418    for (int idx = 0; idx < kMsgSize/10; idx++) {
2419      memcpy(ptr, "----------", 10);
2420      ptr += 10;
2421      if (idx % 100 == 0) {
2422        ptr--;
2423        *ptr++ = marker;
2424        if (++marker > 'z')
2425          marker = 'a';
2426      }
2427    }
2428    uploadBytes[kMsgSize] = '\0';
2429
2430    for (int i = 0; i < kIterations; ++i) {
2431      TestDelegate d;
2432      URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
2433      r.set_method(method.c_str());
2434
2435      r.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes)));
2436
2437      r.Start();
2438      EXPECT_TRUE(r.is_pending());
2439
2440      base::MessageLoop::current()->Run();
2441
2442      ASSERT_EQ(1, d.response_started_count())
2443          << "request failed: " << r.status().status()
2444          << ", os error: " << r.status().error();
2445
2446      EXPECT_FALSE(d.received_data_before_response());
2447      EXPECT_EQ(uploadBytes, d.data_received());
2448    }
2449    delete[] uploadBytes;
2450  }
2451
2452  void AddChunksToUpload(URLRequest* r) {
2453    r->AppendChunkToUpload("a", 1, false);
2454    r->AppendChunkToUpload("bcd", 3, false);
2455    r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2456    r->AppendChunkToUpload("\r\n\r\n", 4, false);
2457    r->AppendChunkToUpload("0", 1, false);
2458    r->AppendChunkToUpload("2323", 4, true);
2459  }
2460
2461  void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
2462    // This should match the chunks sent by AddChunksToUpload().
2463    const std::string expected_data =
2464        "abcdthis is a longer chunk than before.\r\n\r\n02323";
2465
2466    ASSERT_EQ(1, d->response_started_count())
2467        << "request failed: " << r->status().status()
2468        << ", os error: " << r->status().error();
2469
2470    EXPECT_FALSE(d->received_data_before_response());
2471
2472    EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2473    EXPECT_EQ(expected_data, d->data_received());
2474  }
2475
2476  bool DoManyCookiesRequest(int num_cookies) {
2477    TestDelegate d;
2478    URLRequest r(test_server_.GetURL("set-many-cookies?" +
2479                                     base::IntToString(num_cookies)),
2480                                     &d,
2481                                     &default_context_);
2482
2483    r.Start();
2484    EXPECT_TRUE(r.is_pending());
2485
2486    base::MessageLoop::current()->Run();
2487
2488    bool is_success = r.status().is_success();
2489
2490    if (!is_success) {
2491      // Requests handled by ChromeFrame send a less precise error message,
2492      // ERR_CONNECTION_ABORTED.
2493      EXPECT_TRUE(r.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG ||
2494                  r.status().error() == ERR_CONNECTION_ABORTED);
2495      // The test server appears to be unable to handle subsequent requests
2496      // after this error is triggered. Force it to restart.
2497      EXPECT_TRUE(test_server_.Stop());
2498      EXPECT_TRUE(test_server_.Start());
2499    }
2500
2501    return is_success;
2502  }
2503
2504  LocalHttpTestServer test_server_;
2505};
2506
2507// In this unit test, we're using the HTTPTestServer as a proxy server and
2508// issuing a CONNECT request with the magic host name "www.redirect.com".
2509// The HTTPTestServer will return a 302 response, which we should not
2510// follow.
2511TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
2512  ASSERT_TRUE(test_server_.Start());
2513
2514  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
2515  TestURLRequestContextWithProxy context(
2516      test_server_.host_port_pair().ToString(),
2517      &network_delegate);
2518
2519  TestDelegate d;
2520  {
2521    URLRequest r(GURL("https://www.redirect.com/"), &d, &context);
2522    r.Start();
2523    EXPECT_TRUE(r.is_pending());
2524
2525    base::MessageLoop::current()->Run();
2526
2527    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2528    EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
2529    EXPECT_EQ(1, d.response_started_count());
2530    // We should not have followed the redirect.
2531    EXPECT_EQ(0, d.received_redirect_count());
2532  }
2533}
2534
2535// This is the same as the previous test, but checks that the network delegate
2536// registers the error.
2537TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
2538  ASSERT_TRUE(test_server_.Start());
2539
2540  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
2541  TestURLRequestContextWithProxy context(
2542      test_server_.host_port_pair().ToString(),
2543      &network_delegate);
2544
2545  TestDelegate d;
2546  {
2547    URLRequest r(GURL("https://www.redirect.com/"), &d, &context);
2548    r.Start();
2549    EXPECT_TRUE(r.is_pending());
2550
2551    base::MessageLoop::current()->Run();
2552
2553    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2554    EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
2555    EXPECT_EQ(1, d.response_started_count());
2556    // We should not have followed the redirect.
2557    EXPECT_EQ(0, d.received_redirect_count());
2558
2559    EXPECT_EQ(1, network_delegate.error_count());
2560    EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
2561  }
2562}
2563
2564// Tests that we can block and asynchronously return OK in various stages.
2565TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
2566  static const BlockingNetworkDelegate::Stage blocking_stages[] = {
2567    BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2568    BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2569    BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2570  };
2571  static const size_t blocking_stages_length = arraysize(blocking_stages);
2572
2573  ASSERT_TRUE(test_server_.Start());
2574
2575  TestDelegate d;
2576  BlockingNetworkDelegate network_delegate(
2577      BlockingNetworkDelegate::USER_CALLBACK);
2578  network_delegate.set_block_on(
2579      BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
2580      BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
2581      BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2582
2583  TestURLRequestContext context(true);
2584  context.set_network_delegate(&network_delegate);
2585  context.Init();
2586
2587  {
2588    URLRequest r(test_server_.GetURL("empty.html"), &d, &context);
2589
2590    r.Start();
2591    for (size_t i = 0; i < blocking_stages_length; ++i) {
2592      base::MessageLoop::current()->Run();
2593      EXPECT_EQ(blocking_stages[i],
2594                network_delegate.stage_blocked_for_callback());
2595      network_delegate.DoCallback(OK);
2596    }
2597    base::MessageLoop::current()->Run();
2598    EXPECT_EQ(200, r.GetResponseCode());
2599    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2600    EXPECT_EQ(1, network_delegate.created_requests());
2601    EXPECT_EQ(0, network_delegate.destroyed_requests());
2602  }
2603  EXPECT_EQ(1, network_delegate.destroyed_requests());
2604}
2605
2606// Tests that the network delegate can block and cancel a request.
2607TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
2608  ASSERT_TRUE(test_server_.Start());
2609
2610  TestDelegate d;
2611  BlockingNetworkDelegate network_delegate(
2612      BlockingNetworkDelegate::AUTO_CALLBACK);
2613  network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2614  network_delegate.set_retval(ERR_EMPTY_RESPONSE);
2615
2616  TestURLRequestContextWithProxy context(
2617      test_server_.host_port_pair().ToString(),
2618      &network_delegate);
2619
2620  {
2621    URLRequest r(test_server_.GetURL(std::string()), &d, &context);
2622
2623    r.Start();
2624    base::MessageLoop::current()->Run();
2625
2626    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2627    EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
2628    EXPECT_EQ(1, network_delegate.created_requests());
2629    EXPECT_EQ(0, network_delegate.destroyed_requests());
2630  }
2631  EXPECT_EQ(1, network_delegate.destroyed_requests());
2632}
2633
2634// Helper function for NetworkDelegateCancelRequestAsynchronously and
2635// NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
2636// delegate operating in |block_mode| and a request for |url|. It blocks the
2637// request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
2638void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
2639                                  BlockingNetworkDelegate::Stage stage,
2640                                  const GURL& url) {
2641  TestDelegate d;
2642  BlockingNetworkDelegate network_delegate(block_mode);
2643  network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
2644  network_delegate.set_block_on(stage);
2645
2646  TestURLRequestContext context(true);
2647  context.set_network_delegate(&network_delegate);
2648  context.Init();
2649
2650  {
2651    URLRequest r(url, &d, &context);
2652
2653    r.Start();
2654    base::MessageLoop::current()->Run();
2655
2656    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2657    EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r.status().error());
2658    EXPECT_EQ(1, network_delegate.created_requests());
2659    EXPECT_EQ(0, network_delegate.destroyed_requests());
2660  }
2661  EXPECT_EQ(1, network_delegate.destroyed_requests());
2662}
2663
2664// The following 3 tests check that the network delegate can cancel a request
2665// synchronously in various stages of the request.
2666TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
2667  ASSERT_TRUE(test_server_.Start());
2668  NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2669                               BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2670                               test_server_.GetURL(std::string()));
2671}
2672
2673TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
2674  ASSERT_TRUE(test_server_.Start());
2675  NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2676                               BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2677                               test_server_.GetURL(std::string()));
2678}
2679
2680TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
2681  ASSERT_TRUE(test_server_.Start());
2682  NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2683                               BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2684                               test_server_.GetURL(std::string()));
2685}
2686
2687// The following 3 tests check that the network delegate can cancel a request
2688// asynchronously in various stages of the request.
2689TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
2690  ASSERT_TRUE(test_server_.Start());
2691  NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2692                               BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2693                               test_server_.GetURL(std::string()));
2694}
2695
2696TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
2697  ASSERT_TRUE(test_server_.Start());
2698  NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2699                               BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2700                               test_server_.GetURL(std::string()));
2701}
2702
2703TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
2704  ASSERT_TRUE(test_server_.Start());
2705  NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2706                               BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2707                               test_server_.GetURL(std::string()));
2708}
2709
2710// Tests that the network delegate can block and redirect a request to a new
2711// URL.
2712TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
2713  ASSERT_TRUE(test_server_.Start());
2714
2715  TestDelegate d;
2716  BlockingNetworkDelegate network_delegate(
2717      BlockingNetworkDelegate::AUTO_CALLBACK);
2718  network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2719  GURL redirect_url(test_server_.GetURL("simple.html"));
2720  network_delegate.set_redirect_url(redirect_url);
2721
2722  TestURLRequestContextWithProxy context(
2723      test_server_.host_port_pair().ToString(),
2724      &network_delegate);
2725
2726  {
2727    GURL original_url(test_server_.GetURL("empty.html"));
2728    URLRequest r(original_url, &d, &context);
2729
2730    r.Start();
2731    base::MessageLoop::current()->Run();
2732
2733    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2734    EXPECT_EQ(0, r.status().error());
2735    EXPECT_EQ(redirect_url, r.url());
2736    EXPECT_EQ(original_url, r.original_url());
2737    EXPECT_EQ(2U, r.url_chain().size());
2738    EXPECT_EQ(1, network_delegate.created_requests());
2739    EXPECT_EQ(0, network_delegate.destroyed_requests());
2740  }
2741  EXPECT_EQ(1, network_delegate.destroyed_requests());
2742}
2743
2744// Tests that the network delegate can block and redirect a request to a new
2745// URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2746TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
2747  ASSERT_TRUE(test_server_.Start());
2748
2749  TestDelegate d;
2750  BlockingNetworkDelegate network_delegate(
2751      BlockingNetworkDelegate::SYNCHRONOUS);
2752  GURL redirect_url(test_server_.GetURL("simple.html"));
2753  network_delegate.set_redirect_url(redirect_url);
2754
2755  TestURLRequestContextWithProxy context(
2756      test_server_.host_port_pair().ToString(),
2757      &network_delegate);
2758
2759  {
2760    GURL original_url(test_server_.GetURL("empty.html"));
2761    URLRequest r(original_url, &d, &context);
2762
2763    r.Start();
2764    base::MessageLoop::current()->Run();
2765
2766    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2767    EXPECT_EQ(0, r.status().error());
2768    EXPECT_EQ(redirect_url, r.url());
2769    EXPECT_EQ(original_url, r.original_url());
2770    EXPECT_EQ(2U, r.url_chain().size());
2771    EXPECT_EQ(1, network_delegate.created_requests());
2772    EXPECT_EQ(0, network_delegate.destroyed_requests());
2773  }
2774  EXPECT_EQ(1, network_delegate.destroyed_requests());
2775}
2776
2777// Tests that redirects caused by the network delegate preserve POST data.
2778TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
2779  ASSERT_TRUE(test_server_.Start());
2780
2781  const char kData[] = "hello world";
2782
2783  TestDelegate d;
2784  BlockingNetworkDelegate network_delegate(
2785      BlockingNetworkDelegate::AUTO_CALLBACK);
2786  network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2787  GURL redirect_url(test_server_.GetURL("echo"));
2788  network_delegate.set_redirect_url(redirect_url);
2789
2790  TestURLRequestContext context(true);
2791  context.set_network_delegate(&network_delegate);
2792  context.Init();
2793
2794  {
2795    GURL original_url(test_server_.GetURL("empty.html"));
2796    URLRequest r(original_url, &d, &context);
2797    r.set_method("POST");
2798    r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
2799    HttpRequestHeaders headers;
2800    headers.SetHeader(HttpRequestHeaders::kContentLength,
2801                      base::UintToString(arraysize(kData) - 1));
2802    r.SetExtraRequestHeaders(headers);
2803    r.Start();
2804    base::MessageLoop::current()->Run();
2805
2806    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2807    EXPECT_EQ(0, r.status().error());
2808    EXPECT_EQ(redirect_url, r.url());
2809    EXPECT_EQ(original_url, r.original_url());
2810    EXPECT_EQ(2U, r.url_chain().size());
2811    EXPECT_EQ(1, network_delegate.created_requests());
2812    EXPECT_EQ(0, network_delegate.destroyed_requests());
2813    EXPECT_EQ("POST", r.method());
2814    EXPECT_EQ(kData, d.data_received());
2815  }
2816  EXPECT_EQ(1, network_delegate.destroyed_requests());
2817}
2818
2819// Tests that the network delegate can synchronously complete OnAuthRequired
2820// by taking no action. This indicates that the NetworkDelegate does not want to
2821// handle the challenge, and is passing the buck along to the
2822// URLRequest::Delegate.
2823TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
2824  ASSERT_TRUE(test_server_.Start());
2825
2826  TestDelegate d;
2827  BlockingNetworkDelegate network_delegate(
2828      BlockingNetworkDelegate::SYNCHRONOUS);
2829
2830  TestURLRequestContext context(true);
2831  context.set_network_delegate(&network_delegate);
2832  context.Init();
2833
2834  d.set_credentials(AuthCredentials(kUser, kSecret));
2835
2836  {
2837    GURL url(test_server_.GetURL("auth-basic"));
2838    URLRequest r(url, &d, &context);
2839    r.Start();
2840
2841    base::MessageLoop::current()->Run();
2842
2843    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2844    EXPECT_EQ(0, r.status().error());
2845    EXPECT_EQ(200, r.GetResponseCode());
2846    EXPECT_TRUE(d.auth_required_called());
2847    EXPECT_EQ(1, network_delegate.created_requests());
2848    EXPECT_EQ(0, network_delegate.destroyed_requests());
2849  }
2850  EXPECT_EQ(1, network_delegate.destroyed_requests());
2851}
2852
2853TEST_F(URLRequestTestHTTP,
2854    NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) {
2855  ASSERT_TRUE(test_server_.Start());
2856
2857  TestDelegate d;
2858  BlockingNetworkDelegate network_delegate(
2859      BlockingNetworkDelegate::SYNCHRONOUS);
2860
2861  TestURLRequestContext context(true);
2862  context.set_network_delegate(&network_delegate);
2863  context.Init();
2864
2865  d.set_credentials(AuthCredentials(kUser, kSecret));
2866
2867  {
2868    GURL url(test_server_.GetURL("auth-basic"));
2869    URLRequest r(url, &d, &context);
2870    r.Start();
2871
2872    {
2873      HttpRequestHeaders headers;
2874      EXPECT_TRUE(r.GetFullRequestHeaders(&headers));
2875      EXPECT_FALSE(headers.HasHeader("Authorization"));
2876    }
2877
2878    base::MessageLoop::current()->Run();
2879
2880    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2881    EXPECT_EQ(0, r.status().error());
2882    EXPECT_EQ(200, r.GetResponseCode());
2883    EXPECT_TRUE(d.auth_required_called());
2884    EXPECT_EQ(1, network_delegate.created_requests());
2885    EXPECT_EQ(0, network_delegate.destroyed_requests());
2886  }
2887  EXPECT_EQ(1, network_delegate.destroyed_requests());
2888}
2889
2890// Tests that the network delegate can synchronously complete OnAuthRequired
2891// by setting credentials.
2892TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
2893  ASSERT_TRUE(test_server_.Start());
2894
2895  TestDelegate d;
2896  BlockingNetworkDelegate network_delegate(
2897      BlockingNetworkDelegate::SYNCHRONOUS);
2898  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2899  network_delegate.set_auth_retval(
2900      NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2901
2902  network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
2903
2904  TestURLRequestContext context(true);
2905  context.set_network_delegate(&network_delegate);
2906  context.Init();
2907
2908  {
2909    GURL url(test_server_.GetURL("auth-basic"));
2910    URLRequest r(url, &d, &context);
2911    r.Start();
2912    base::MessageLoop::current()->Run();
2913
2914    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2915    EXPECT_EQ(0, r.status().error());
2916    EXPECT_EQ(200, r.GetResponseCode());
2917    EXPECT_FALSE(d.auth_required_called());
2918    EXPECT_EQ(1, network_delegate.created_requests());
2919    EXPECT_EQ(0, network_delegate.destroyed_requests());
2920  }
2921  EXPECT_EQ(1, network_delegate.destroyed_requests());
2922}
2923
2924// Same as above, but also tests that GetFullRequestHeaders returns the proper
2925// headers (for the first or second request) when called at the proper times.
2926TEST_F(URLRequestTestHTTP,
2927    NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) {
2928  ASSERT_TRUE(test_server_.Start());
2929
2930  TestDelegate d;
2931  BlockingNetworkDelegate network_delegate(
2932      BlockingNetworkDelegate::SYNCHRONOUS);
2933  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2934  network_delegate.set_auth_retval(
2935      NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2936
2937  network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
2938
2939  TestURLRequestContext context(true);
2940  context.set_network_delegate(&network_delegate);
2941  context.Init();
2942
2943  {
2944    GURL url(test_server_.GetURL("auth-basic"));
2945    URLRequest r(url, &d, &context);
2946    r.Start();
2947    base::MessageLoop::current()->Run();
2948
2949    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2950    EXPECT_EQ(0, r.status().error());
2951    EXPECT_EQ(200, r.GetResponseCode());
2952    EXPECT_FALSE(d.auth_required_called());
2953    EXPECT_EQ(1, network_delegate.created_requests());
2954    EXPECT_EQ(0, network_delegate.destroyed_requests());
2955
2956    {
2957      HttpRequestHeaders headers;
2958      EXPECT_TRUE(r.GetFullRequestHeaders(&headers));
2959      EXPECT_TRUE(headers.HasHeader("Authorization"));
2960    }
2961  }
2962  EXPECT_EQ(1, network_delegate.destroyed_requests());
2963}
2964
2965// Tests that the network delegate can synchronously complete OnAuthRequired
2966// by cancelling authentication.
2967TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
2968  ASSERT_TRUE(test_server_.Start());
2969
2970  TestDelegate d;
2971  BlockingNetworkDelegate network_delegate(
2972      BlockingNetworkDelegate::SYNCHRONOUS);
2973  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2974  network_delegate.set_auth_retval(
2975      NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2976
2977  TestURLRequestContext context(true);
2978  context.set_network_delegate(&network_delegate);
2979  context.Init();
2980
2981  {
2982    GURL url(test_server_.GetURL("auth-basic"));
2983    URLRequest r(url, &d, &context);
2984    r.Start();
2985    base::MessageLoop::current()->Run();
2986
2987    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2988    EXPECT_EQ(OK, r.status().error());
2989    EXPECT_EQ(401, r.GetResponseCode());
2990    EXPECT_FALSE(d.auth_required_called());
2991    EXPECT_EQ(1, network_delegate.created_requests());
2992    EXPECT_EQ(0, network_delegate.destroyed_requests());
2993  }
2994  EXPECT_EQ(1, network_delegate.destroyed_requests());
2995}
2996
2997// Tests that the network delegate can asynchronously complete OnAuthRequired
2998// by taking no action. This indicates that the NetworkDelegate does not want
2999// to handle the challenge, and is passing the buck along to the
3000// URLRequest::Delegate.
3001TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
3002  ASSERT_TRUE(test_server_.Start());
3003
3004  TestDelegate d;
3005  BlockingNetworkDelegate network_delegate(
3006      BlockingNetworkDelegate::AUTO_CALLBACK);
3007  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3008
3009  TestURLRequestContext context(true);
3010  context.set_network_delegate(&network_delegate);
3011  context.Init();
3012
3013  d.set_credentials(AuthCredentials(kUser, kSecret));
3014
3015  {
3016    GURL url(test_server_.GetURL("auth-basic"));
3017    URLRequest r(url, &d, &context);
3018    r.Start();
3019    base::MessageLoop::current()->Run();
3020
3021    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3022    EXPECT_EQ(0, r.status().error());
3023    EXPECT_EQ(200, r.GetResponseCode());
3024    EXPECT_TRUE(d.auth_required_called());
3025    EXPECT_EQ(1, network_delegate.created_requests());
3026    EXPECT_EQ(0, network_delegate.destroyed_requests());
3027  }
3028  EXPECT_EQ(1, network_delegate.destroyed_requests());
3029}
3030
3031// Tests that the network delegate can asynchronously complete OnAuthRequired
3032// by setting credentials.
3033TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
3034  ASSERT_TRUE(test_server_.Start());
3035
3036  TestDelegate d;
3037  BlockingNetworkDelegate network_delegate(
3038      BlockingNetworkDelegate::AUTO_CALLBACK);
3039  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3040  network_delegate.set_auth_retval(
3041      NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3042
3043  AuthCredentials auth_credentials(kUser, kSecret);
3044  network_delegate.set_auth_credentials(auth_credentials);
3045
3046  TestURLRequestContext context(true);
3047  context.set_network_delegate(&network_delegate);
3048  context.Init();
3049
3050  {
3051    GURL url(test_server_.GetURL("auth-basic"));
3052    URLRequest r(url, &d, &context);
3053    r.Start();
3054    base::MessageLoop::current()->Run();
3055
3056    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3057    EXPECT_EQ(0, r.status().error());
3058
3059    EXPECT_EQ(200, r.GetResponseCode());
3060    EXPECT_FALSE(d.auth_required_called());
3061    EXPECT_EQ(1, network_delegate.created_requests());
3062    EXPECT_EQ(0, network_delegate.destroyed_requests());
3063  }
3064  EXPECT_EQ(1, network_delegate.destroyed_requests());
3065}
3066
3067// Tests that the network delegate can asynchronously complete OnAuthRequired
3068// by cancelling authentication.
3069TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
3070  ASSERT_TRUE(test_server_.Start());
3071
3072  TestDelegate d;
3073  BlockingNetworkDelegate network_delegate(
3074      BlockingNetworkDelegate::AUTO_CALLBACK);
3075  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3076  network_delegate.set_auth_retval(
3077      NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3078
3079  TestURLRequestContext context(true);
3080  context.set_network_delegate(&network_delegate);
3081  context.Init();
3082
3083  {
3084    GURL url(test_server_.GetURL("auth-basic"));
3085    URLRequest r(url, &d, &context);
3086    r.Start();
3087    base::MessageLoop::current()->Run();
3088
3089    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3090    EXPECT_EQ(OK, r.status().error());
3091    EXPECT_EQ(401, r.GetResponseCode());
3092    EXPECT_FALSE(d.auth_required_called());
3093    EXPECT_EQ(1, network_delegate.created_requests());
3094    EXPECT_EQ(0, network_delegate.destroyed_requests());
3095  }
3096  EXPECT_EQ(1, network_delegate.destroyed_requests());
3097}
3098
3099// Tests that we can handle when a network request was canceled while we were
3100// waiting for the network delegate.
3101// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3102TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
3103  ASSERT_TRUE(test_server_.Start());
3104
3105  TestDelegate d;
3106  BlockingNetworkDelegate network_delegate(
3107      BlockingNetworkDelegate::USER_CALLBACK);
3108  network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3109
3110  TestURLRequestContext context(true);
3111  context.set_network_delegate(&network_delegate);
3112  context.Init();
3113
3114  {
3115    URLRequest r(test_server_.GetURL(std::string()), &d, &context);
3116
3117    r.Start();
3118    base::MessageLoop::current()->Run();
3119    EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3120              network_delegate.stage_blocked_for_callback());
3121    EXPECT_EQ(0, network_delegate.completed_requests());
3122    // Cancel before callback.
3123    r.Cancel();
3124    // Ensure that network delegate is notified.
3125    EXPECT_EQ(1, network_delegate.completed_requests());
3126    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3127    EXPECT_EQ(ERR_ABORTED, r.status().error());
3128    EXPECT_EQ(1, network_delegate.created_requests());
3129    EXPECT_EQ(0, network_delegate.destroyed_requests());
3130  }
3131  EXPECT_EQ(1, network_delegate.destroyed_requests());
3132}
3133
3134// Tests that we can handle when a network request was canceled while we were
3135// waiting for the network delegate.
3136// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3137TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
3138  ASSERT_TRUE(test_server_.Start());
3139
3140  TestDelegate d;
3141  BlockingNetworkDelegate network_delegate(
3142      BlockingNetworkDelegate::USER_CALLBACK);
3143  network_delegate.set_block_on(
3144      BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
3145
3146  TestURLRequestContext context(true);
3147  context.set_network_delegate(&network_delegate);
3148  context.Init();
3149
3150  {
3151    URLRequest r(test_server_.GetURL(std::string()), &d, &context);
3152
3153    r.Start();
3154    base::MessageLoop::current()->Run();
3155    EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3156              network_delegate.stage_blocked_for_callback());
3157    EXPECT_EQ(0, network_delegate.completed_requests());
3158    // Cancel before callback.
3159    r.Cancel();
3160    // Ensure that network delegate is notified.
3161    EXPECT_EQ(1, network_delegate.completed_requests());
3162    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3163    EXPECT_EQ(ERR_ABORTED, r.status().error());
3164    EXPECT_EQ(1, network_delegate.created_requests());
3165    EXPECT_EQ(0, network_delegate.destroyed_requests());
3166  }
3167  EXPECT_EQ(1, network_delegate.destroyed_requests());
3168}
3169
3170// Tests that we can handle when a network request was canceled while we were
3171// waiting for the network delegate.
3172// Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3173TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
3174  ASSERT_TRUE(test_server_.Start());
3175
3176  TestDelegate d;
3177  BlockingNetworkDelegate network_delegate(
3178      BlockingNetworkDelegate::USER_CALLBACK);
3179  network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3180
3181  TestURLRequestContext context(true);
3182  context.set_network_delegate(&network_delegate);
3183  context.Init();
3184
3185  {
3186    URLRequest r(test_server_.GetURL(std::string()), &d, &context);
3187
3188    r.Start();
3189    base::MessageLoop::current()->Run();
3190    EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3191              network_delegate.stage_blocked_for_callback());
3192    EXPECT_EQ(0, network_delegate.completed_requests());
3193    // Cancel before callback.
3194    r.Cancel();
3195    // Ensure that network delegate is notified.
3196    EXPECT_EQ(1, network_delegate.completed_requests());
3197    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3198    EXPECT_EQ(ERR_ABORTED, r.status().error());
3199    EXPECT_EQ(1, network_delegate.created_requests());
3200    EXPECT_EQ(0, network_delegate.destroyed_requests());
3201  }
3202  EXPECT_EQ(1, network_delegate.destroyed_requests());
3203}
3204
3205// Tests that we can handle when a network request was canceled while we were
3206// waiting for the network delegate.
3207// Part 4: Request is cancelled while waiting for OnAuthRequired callback.
3208TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
3209  ASSERT_TRUE(test_server_.Start());
3210
3211  TestDelegate d;
3212  BlockingNetworkDelegate network_delegate(
3213      BlockingNetworkDelegate::USER_CALLBACK);
3214  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3215
3216  TestURLRequestContext context(true);
3217  context.set_network_delegate(&network_delegate);
3218  context.Init();
3219
3220  {
3221    URLRequest r(test_server_.GetURL("auth-basic"), &d, &context);
3222
3223    r.Start();
3224    base::MessageLoop::current()->Run();
3225    EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
3226              network_delegate.stage_blocked_for_callback());
3227    EXPECT_EQ(0, network_delegate.completed_requests());
3228    // Cancel before callback.
3229    r.Cancel();
3230    // Ensure that network delegate is notified.
3231    EXPECT_EQ(1, network_delegate.completed_requests());
3232    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3233    EXPECT_EQ(ERR_ABORTED, r.status().error());
3234    EXPECT_EQ(1, network_delegate.created_requests());
3235    EXPECT_EQ(0, network_delegate.destroyed_requests());
3236  }
3237  EXPECT_EQ(1, network_delegate.destroyed_requests());
3238}
3239
3240// In this unit test, we're using the HTTPTestServer as a proxy server and
3241// issuing a CONNECT request with the magic host name "www.server-auth.com".
3242// The HTTPTestServer will return a 401 response, which we should balk at.
3243TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
3244  ASSERT_TRUE(test_server_.Start());
3245
3246  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
3247  TestURLRequestContextWithProxy context(
3248      test_server_.host_port_pair().ToString(),
3249      &network_delegate);
3250
3251  TestDelegate d;
3252  {
3253    URLRequest r(GURL("https://www.server-auth.com/"), &d, &context);
3254
3255    r.Start();
3256    EXPECT_TRUE(r.is_pending());
3257
3258    base::MessageLoop::current()->Run();
3259
3260    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
3261    EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
3262  }
3263}
3264
3265TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
3266  ASSERT_TRUE(test_server_.Start());
3267
3268  TestDelegate d;
3269  {
3270    URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
3271
3272    r.Start();
3273    EXPECT_TRUE(r.is_pending());
3274
3275    base::MessageLoop::current()->Run();
3276
3277    EXPECT_EQ(1, d.response_started_count());
3278    EXPECT_FALSE(d.received_data_before_response());
3279    EXPECT_NE(0, d.bytes_received());
3280    EXPECT_EQ(test_server_.host_port_pair().host(),
3281              r.GetSocketAddress().host());
3282    EXPECT_EQ(test_server_.host_port_pair().port(),
3283              r.GetSocketAddress().port());
3284
3285    // TODO(eroman): Add back the NetLog tests...
3286  }
3287}
3288
3289// This test has the server send a large number of cookies to the client.
3290// To ensure that no number of cookies causes a crash, a galloping binary
3291// search is used to estimate that maximum number of cookies that are accepted
3292// by the browser. Beyond the maximum number, the request will fail with
3293// ERR_RESPONSE_HEADERS_TOO_BIG.
3294#if defined(OS_WIN)
3295// http://crbug.com/177916
3296#define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3297#else
3298#define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3299#endif  // defined(OS_WIN)
3300TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) {
3301  ASSERT_TRUE(test_server_.Start());
3302
3303  int lower_bound = 0;
3304  int upper_bound = 1;
3305
3306  // Double the number of cookies until the response header limits are
3307  // exceeded.
3308  while (DoManyCookiesRequest(upper_bound)) {
3309    lower_bound = upper_bound;
3310    upper_bound *= 2;
3311    ASSERT_LT(upper_bound, 1000000);
3312  }
3313
3314  int tolerance = upper_bound * 0.005;
3315  if (tolerance < 2)
3316    tolerance = 2;
3317
3318  // Perform a binary search to find the highest possible number of cookies,
3319  // within the desired tolerance.
3320  while (upper_bound - lower_bound >= tolerance) {
3321    int num_cookies = (lower_bound + upper_bound) / 2;
3322
3323    if (DoManyCookiesRequest(num_cookies))
3324      lower_bound = num_cookies;
3325    else
3326      upper_bound = num_cookies;
3327  }
3328  // Success: the test did not crash.
3329}
3330
3331TEST_F(URLRequestTestHTTP, GetTest) {
3332  ASSERT_TRUE(test_server_.Start());
3333
3334  TestDelegate d;
3335  {
3336    URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
3337
3338    r.Start();
3339    EXPECT_TRUE(r.is_pending());
3340
3341    base::MessageLoop::current()->Run();
3342
3343    EXPECT_EQ(1, d.response_started_count());
3344    EXPECT_FALSE(d.received_data_before_response());
3345    EXPECT_NE(0, d.bytes_received());
3346    EXPECT_EQ(test_server_.host_port_pair().host(),
3347              r.GetSocketAddress().host());
3348    EXPECT_EQ(test_server_.host_port_pair().port(),
3349              r.GetSocketAddress().port());
3350  }
3351}
3352
3353TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) {
3354  ASSERT_TRUE(test_server_.Start());
3355
3356  TestDelegate d;
3357  {
3358    GURL test_url(test_server_.GetURL(std::string()));
3359    URLRequest r(test_url, &d, &default_context_);
3360
3361    HttpRequestHeaders headers;
3362    EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
3363
3364    r.Start();
3365    EXPECT_TRUE(r.is_pending());
3366
3367    base::MessageLoop::current()->Run();
3368
3369    EXPECT_EQ(1, d.response_started_count());
3370    EXPECT_FALSE(d.received_data_before_response());
3371    EXPECT_NE(0, d.bytes_received());
3372    EXPECT_EQ(test_server_.host_port_pair().host(),
3373              r.GetSocketAddress().host());
3374    EXPECT_EQ(test_server_.host_port_pair().port(),
3375              r.GetSocketAddress().port());
3376
3377    EXPECT_TRUE(d.have_full_request_headers());
3378    CheckFullRequestHeaders(d.full_request_headers(), test_url);
3379  }
3380}
3381
3382TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
3383  ASSERT_TRUE(test_server_.Start());
3384
3385  TestDelegate d;
3386  {
3387    URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
3388
3389    r.Start();
3390    EXPECT_TRUE(r.is_pending());
3391
3392    base::MessageLoop::current()->Run();
3393
3394    LoadTimingInfo load_timing_info;
3395    r.GetLoadTimingInfo(&load_timing_info);
3396    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3397
3398    EXPECT_EQ(1, d.response_started_count());
3399    EXPECT_FALSE(d.received_data_before_response());
3400    EXPECT_NE(0, d.bytes_received());
3401    EXPECT_EQ(test_server_.host_port_pair().host(),
3402              r.GetSocketAddress().host());
3403    EXPECT_EQ(test_server_.host_port_pair().port(),
3404              r.GetSocketAddress().port());
3405  }
3406}
3407
3408TEST_F(URLRequestTestHTTP, GetZippedTest) {
3409  ASSERT_TRUE(test_server_.Start());
3410
3411  // Parameter that specifies the Content-Length field in the response:
3412  // C - Compressed length.
3413  // U - Uncompressed length.
3414  // L - Large length (larger than both C & U).
3415  // M - Medium length (between C & U).
3416  // S - Small length (smaller than both C & U).
3417  const char test_parameters[] = "CULMS";
3418  const int num_tests = arraysize(test_parameters)- 1;  // Skip NULL.
3419  // C & U should be OK.
3420  // L & M are larger than the data sent, and show an error.
3421  // S has too little data, but we seem to accept it.
3422  const bool test_expect_success[num_tests] =
3423      { true, true, false, false, true };
3424
3425  for (int i = 0; i < num_tests ; i++) {
3426    TestDelegate d;
3427    {
3428      std::string test_file =
3429          base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
3430                             test_parameters[i]);
3431
3432      TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
3433      TestURLRequestContext context(true);
3434      context.set_network_delegate(&network_delegate);
3435      context.Init();
3436
3437      URLRequest r(test_server_.GetURL(test_file), &d, &context);
3438      r.Start();
3439      EXPECT_TRUE(r.is_pending());
3440
3441      base::MessageLoop::current()->Run();
3442
3443      EXPECT_EQ(1, d.response_started_count());
3444      EXPECT_FALSE(d.received_data_before_response());
3445      VLOG(1) << " Received " << d.bytes_received() << " bytes"
3446              << " status = " << r.status().status()
3447              << " error = " << r.status().error();
3448      if (test_expect_success[i]) {
3449        EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status())
3450            << " Parameter = \"" << test_file << "\"";
3451      } else {
3452        EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
3453        EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r.status().error())
3454            << " Parameter = \"" << test_file << "\"";
3455      }
3456    }
3457  }
3458}
3459
3460TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
3461  ASSERT_TRUE(test_server_.Start());
3462
3463  SpawnedTestServer https_test_server(
3464      SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost,
3465      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
3466  ASSERT_TRUE(https_test_server.Start());
3467
3468  // An https server is sent a request with an https referer,
3469  // and responds with a redirect to an http url. The http
3470  // server should not be sent the referer.
3471  GURL http_destination = test_server_.GetURL(std::string());
3472  TestDelegate d;
3473  URLRequest req(https_test_server.GetURL(
3474      "server-redirect?" + http_destination.spec()), &d, &default_context_);
3475  req.SetReferrer("https://www.referrer.com/");
3476  req.Start();
3477  base::MessageLoop::current()->Run();
3478
3479  EXPECT_EQ(1, d.response_started_count());
3480  EXPECT_EQ(1, d.received_redirect_count());
3481  EXPECT_EQ(http_destination, req.url());
3482  EXPECT_EQ(std::string(), req.referrer());
3483}
3484
3485TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
3486  ASSERT_TRUE(test_server_.Start());
3487
3488  GURL destination_url = test_server_.GetURL(std::string());
3489  GURL original_url =
3490      test_server_.GetURL("server-redirect?" + destination_url.spec());
3491  TestDelegate d;
3492  URLRequest req(original_url, &d, &default_context_);
3493  req.Start();
3494  base::MessageLoop::current()->Run();
3495
3496  EXPECT_EQ(1, d.response_started_count());
3497  EXPECT_EQ(1, d.received_redirect_count());
3498  EXPECT_EQ(destination_url, req.url());
3499  EXPECT_EQ(original_url, req.original_url());
3500  ASSERT_EQ(2U, req.url_chain().size());
3501  EXPECT_EQ(original_url, req.url_chain()[0]);
3502  EXPECT_EQ(destination_url, req.url_chain()[1]);
3503
3504  LoadTimingInfo load_timing_info_before_redirect;
3505  EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
3506      &load_timing_info_before_redirect));
3507  TestLoadTimingNotReused(load_timing_info_before_redirect,
3508                          CONNECT_TIMING_HAS_DNS_TIMES);
3509
3510  LoadTimingInfo load_timing_info;
3511  req.GetLoadTimingInfo(&load_timing_info);
3512  TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3513
3514  // Check that a new socket was used on redirect, since the server does not
3515  // supposed keep-alive sockets, and that the times before the redirect are
3516  // before the ones recorded for the second request.
3517  EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
3518            load_timing_info.socket_log_id);
3519  EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
3520            load_timing_info.connect_timing.connect_start);
3521}
3522
3523TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
3524  ASSERT_TRUE(test_server_.Start());
3525
3526  GURL destination_url = test_server_.GetURL(std::string());
3527  GURL middle_redirect_url =
3528      test_server_.GetURL("server-redirect?" + destination_url.spec());
3529  GURL original_url = test_server_.GetURL(
3530      "server-redirect?" + middle_redirect_url.spec());
3531  TestDelegate d;
3532  URLRequest req(original_url, &d, &default_context_);
3533  req.Start();
3534  base::MessageLoop::current()->Run();
3535
3536  EXPECT_EQ(1, d.response_started_count());
3537  EXPECT_EQ(2, d.received_redirect_count());
3538  EXPECT_EQ(destination_url, req.url());
3539  EXPECT_EQ(original_url, req.original_url());
3540  ASSERT_EQ(3U, req.url_chain().size());
3541  EXPECT_EQ(original_url, req.url_chain()[0]);
3542  EXPECT_EQ(middle_redirect_url, req.url_chain()[1]);
3543  EXPECT_EQ(destination_url, req.url_chain()[2]);
3544}
3545
3546namespace {
3547
3548const char kExtraHeader[] = "Allow-Snafu";
3549const char kExtraValue[] = "fubar";
3550
3551class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
3552  virtual void OnReceivedRedirect(net::URLRequest* request,
3553                                  const GURL& new_url,
3554                                  bool* defer_redirect) OVERRIDE {
3555    TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
3556    request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
3557  }
3558};
3559
3560}  // namespace
3561
3562TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
3563  ASSERT_TRUE(test_server_.Start());
3564
3565  GURL destination_url = test_server_.GetURL(
3566      "echoheader?" + std::string(kExtraHeader));
3567  GURL original_url = test_server_.GetURL(
3568      "server-redirect?" + destination_url.spec());
3569  RedirectWithAdditionalHeadersDelegate d;
3570  URLRequest req(original_url, &d, &default_context_);
3571  req.Start();
3572  base::MessageLoop::current()->Run();
3573
3574  std::string value;
3575  const HttpRequestHeaders& headers = req.extra_request_headers();
3576  EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
3577  EXPECT_EQ(kExtraValue, value);
3578  EXPECT_FALSE(req.is_pending());
3579  EXPECT_FALSE(req.is_redirecting());
3580  EXPECT_EQ(kExtraValue, d.data_received());
3581}
3582
3583namespace {
3584
3585const char kExtraHeaderToRemove[] = "To-Be-Removed";
3586
3587class RedirectWithHeaderRemovalDelegate : public TestDelegate {
3588  virtual void OnReceivedRedirect(net::URLRequest* request,
3589                          const GURL& new_url,
3590                          bool* defer_redirect) OVERRIDE {
3591    TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
3592    request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
3593  }
3594};
3595
3596}  // namespace
3597
3598TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
3599  ASSERT_TRUE(test_server_.Start());
3600
3601  GURL destination_url = test_server_.GetURL(
3602      "echoheader?" + std::string(kExtraHeaderToRemove));
3603  GURL original_url = test_server_.GetURL(
3604      "server-redirect?" + destination_url.spec());
3605  RedirectWithHeaderRemovalDelegate d;
3606  URLRequest req(original_url, &d, &default_context_);
3607  req.SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
3608  req.Start();
3609  base::MessageLoop::current()->Run();
3610
3611  std::string value;
3612  const HttpRequestHeaders& headers = req.extra_request_headers();
3613  EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
3614  EXPECT_FALSE(req.is_pending());
3615  EXPECT_FALSE(req.is_redirecting());
3616  EXPECT_EQ("None", d.data_received());
3617}
3618
3619TEST_F(URLRequestTestHTTP, CancelTest) {
3620  TestDelegate d;
3621  {
3622    URLRequest r(GURL("http://www.google.com/"), &d, &default_context_);
3623
3624    r.Start();
3625    EXPECT_TRUE(r.is_pending());
3626
3627    r.Cancel();
3628
3629    base::MessageLoop::current()->Run();
3630
3631    // We expect to receive OnResponseStarted even though the request has been
3632    // cancelled.
3633    EXPECT_EQ(1, d.response_started_count());
3634    EXPECT_EQ(0, d.bytes_received());
3635    EXPECT_FALSE(d.received_data_before_response());
3636  }
3637}
3638
3639TEST_F(URLRequestTestHTTP, CancelTest2) {
3640  ASSERT_TRUE(test_server_.Start());
3641
3642  TestDelegate d;
3643  {
3644    URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
3645
3646    d.set_cancel_in_response_started(true);
3647
3648    r.Start();
3649    EXPECT_TRUE(r.is_pending());
3650
3651    base::MessageLoop::current()->Run();
3652
3653    EXPECT_EQ(1, d.response_started_count());
3654    EXPECT_EQ(0, d.bytes_received());
3655    EXPECT_FALSE(d.received_data_before_response());
3656    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3657  }
3658}
3659
3660TEST_F(URLRequestTestHTTP, CancelTest3) {
3661  ASSERT_TRUE(test_server_.Start());
3662
3663  TestDelegate d;
3664  {
3665    URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
3666
3667    d.set_cancel_in_received_data(true);
3668
3669    r.Start();
3670    EXPECT_TRUE(r.is_pending());
3671
3672    base::MessageLoop::current()->Run();
3673
3674    EXPECT_EQ(1, d.response_started_count());
3675    // There is no guarantee about how much data was received
3676    // before the cancel was issued.  It could have been 0 bytes,
3677    // or it could have been all the bytes.
3678    // EXPECT_EQ(0, d.bytes_received());
3679    EXPECT_FALSE(d.received_data_before_response());
3680    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3681  }
3682}
3683
3684TEST_F(URLRequestTestHTTP, CancelTest4) {
3685  ASSERT_TRUE(test_server_.Start());
3686
3687  TestDelegate d;
3688  {
3689    URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
3690
3691    r.Start();
3692    EXPECT_TRUE(r.is_pending());
3693
3694    // The request will be implicitly canceled when it is destroyed. The
3695    // test delegate must not post a quit message when this happens because
3696    // this test doesn't actually have a message loop. The quit message would
3697    // get put on this thread's message queue and the next test would exit
3698    // early, causing problems.
3699    d.set_quit_on_complete(false);
3700  }
3701  // expect things to just cleanup properly.
3702
3703  // we won't actually get a received reponse here because we've never run the
3704  // message loop
3705  EXPECT_FALSE(d.received_data_before_response());
3706  EXPECT_EQ(0, d.bytes_received());
3707}
3708
3709TEST_F(URLRequestTestHTTP, CancelTest5) {
3710  ASSERT_TRUE(test_server_.Start());
3711
3712  // populate cache
3713  {
3714    TestDelegate d;
3715    URLRequest r(test_server_.GetURL("cachetime"), &d, &default_context_);
3716    r.Start();
3717    base::MessageLoop::current()->Run();
3718    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3719  }
3720
3721  // cancel read from cache (see bug 990242)
3722  {
3723    TestDelegate d;
3724    URLRequest r(test_server_.GetURL("cachetime"), &d, &default_context_);
3725    r.Start();
3726    r.Cancel();
3727    base::MessageLoop::current()->Run();
3728
3729    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3730    EXPECT_EQ(1, d.response_started_count());
3731    EXPECT_EQ(0, d.bytes_received());
3732    EXPECT_FALSE(d.received_data_before_response());
3733  }
3734}
3735
3736TEST_F(URLRequestTestHTTP, PostTest) {
3737  ASSERT_TRUE(test_server_.Start());
3738  HTTPUploadDataOperationTest("POST");
3739}
3740
3741TEST_F(URLRequestTestHTTP, PutTest) {
3742  ASSERT_TRUE(test_server_.Start());
3743  HTTPUploadDataOperationTest("PUT");
3744}
3745
3746TEST_F(URLRequestTestHTTP, PostEmptyTest) {
3747  ASSERT_TRUE(test_server_.Start());
3748
3749  TestDelegate d;
3750  {
3751    URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3752    r.set_method("POST");
3753
3754    r.Start();
3755    EXPECT_TRUE(r.is_pending());
3756
3757    base::MessageLoop::current()->Run();
3758
3759    ASSERT_EQ(1, d.response_started_count())
3760        << "request failed: " << r.status().status()
3761        << ", error: " << r.status().error();
3762
3763    EXPECT_FALSE(d.received_data_before_response());
3764    EXPECT_TRUE(d.data_received().empty());
3765  }
3766}
3767
3768TEST_F(URLRequestTestHTTP, PostFileTest) {
3769  ASSERT_TRUE(test_server_.Start());
3770
3771  TestDelegate d;
3772  {
3773    URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3774    r.set_method("POST");
3775
3776    base::FilePath dir;
3777    PathService::Get(base::DIR_EXE, &dir);
3778    file_util::SetCurrentDirectory(dir);
3779
3780    ScopedVector<UploadElementReader> element_readers;
3781
3782    base::FilePath path;
3783    PathService::Get(base::DIR_SOURCE_ROOT, &path);
3784    path = path.Append(FILE_PATH_LITERAL("net"));
3785    path = path.Append(FILE_PATH_LITERAL("data"));
3786    path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
3787    path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
3788    element_readers.push_back(
3789        new UploadFileElementReader(base::MessageLoopProxy::current().get(),
3790                                    path,
3791                                    0,
3792                                    kuint64max,
3793                                    base::Time()));
3794
3795    // This file should just be ignored in the upload stream.
3796    element_readers.push_back(new UploadFileElementReader(
3797        base::MessageLoopProxy::current().get(),
3798        base::FilePath(FILE_PATH_LITERAL(
3799            "c:\\path\\to\\non\\existant\\file.randomness.12345")),
3800        0,
3801        kuint64max,
3802        base::Time()));
3803    r.set_upload(make_scoped_ptr(new UploadDataStream(&element_readers, 0)));
3804
3805    r.Start();
3806    EXPECT_TRUE(r.is_pending());
3807
3808    base::MessageLoop::current()->Run();
3809
3810    int64 size = 0;
3811    ASSERT_EQ(true, file_util::GetFileSize(path, &size));
3812    scoped_ptr<char[]> buf(new char[size]);
3813
3814    ASSERT_EQ(size, file_util::ReadFile(path, buf.get(), size));
3815
3816    ASSERT_EQ(1, d.response_started_count())
3817        << "request failed: " << r.status().status()
3818        << ", error: " << r.status().error();
3819
3820    EXPECT_FALSE(d.received_data_before_response());
3821
3822    EXPECT_EQ(size, d.bytes_received());
3823    EXPECT_EQ(std::string(&buf[0], size), d.data_received());
3824  }
3825}
3826
3827TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
3828  ASSERT_TRUE(test_server_.Start());
3829
3830  TestDelegate d;
3831  {
3832    URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3833    r.EnableChunkedUpload();
3834    r.set_method("POST");
3835    AddChunksToUpload(&r);
3836    r.Start();
3837    EXPECT_TRUE(r.is_pending());
3838
3839    base::MessageLoop::current()->Run();
3840
3841    VerifyReceivedDataMatchesChunks(&r, &d);
3842  }
3843}
3844
3845TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
3846  ASSERT_TRUE(test_server_.Start());
3847
3848  TestDelegate d;
3849  {
3850    URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3851    r.EnableChunkedUpload();
3852    r.set_method("POST");
3853    r.Start();
3854    EXPECT_TRUE(r.is_pending());
3855    AddChunksToUpload(&r);
3856    base::MessageLoop::current()->Run();
3857
3858    VerifyReceivedDataMatchesChunks(&r, &d);
3859  }
3860}
3861
3862TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
3863  ASSERT_TRUE(test_server_.Start());
3864
3865  TestDelegate d;
3866  {
3867    URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3868    r.EnableChunkedUpload();
3869    r.set_method("POST");
3870    r.Start();
3871    EXPECT_TRUE(r.is_pending());
3872
3873    base::MessageLoop::current()->RunUntilIdle();
3874    AddChunksToUpload(&r);
3875    base::MessageLoop::current()->Run();
3876
3877    VerifyReceivedDataMatchesChunks(&r, &d);
3878  }
3879}
3880
3881TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
3882  ASSERT_TRUE(test_server_.Start());
3883
3884  TestDelegate d;
3885  URLRequest req(
3886      test_server_.GetURL("files/with-headers.html"), &d, &default_context_);
3887  req.Start();
3888  base::MessageLoop::current()->Run();
3889
3890  const HttpResponseHeaders* headers = req.response_headers();
3891
3892  // Simple sanity check that response_info() accesses the same data.
3893  EXPECT_EQ(headers, req.response_info().headers.get());
3894
3895  std::string header;
3896  EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
3897  EXPECT_EQ("private", header);
3898
3899  header.clear();
3900  EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
3901  EXPECT_EQ("text/html; charset=ISO-8859-1", header);
3902
3903  // The response has two "X-Multiple-Entries" headers.
3904  // This verfies our output has them concatenated together.
3905  header.clear();
3906  EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
3907  EXPECT_EQ("a, b", header);
3908}
3909
3910TEST_F(URLRequestTestHTTP, ProcessSTS) {
3911  SpawnedTestServer::SSLOptions ssl_options;
3912  SpawnedTestServer https_test_server(
3913      SpawnedTestServer::TYPE_HTTPS,
3914      ssl_options,
3915      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
3916  ASSERT_TRUE(https_test_server.Start());
3917
3918  TestDelegate d;
3919  URLRequest request(
3920      https_test_server.GetURL("files/hsts-headers.html"),
3921      &d,
3922      &default_context_);
3923  request.Start();
3924  base::MessageLoop::current()->Run();
3925
3926  TransportSecurityState* security_state =
3927      default_context_.transport_security_state();
3928  bool sni_available = true;
3929  TransportSecurityState::DomainState domain_state;
3930  EXPECT_TRUE(security_state->GetDomainState(
3931      SpawnedTestServer::kLocalhost, sni_available, &domain_state));
3932  EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
3933            domain_state.upgrade_mode);
3934  EXPECT_TRUE(domain_state.sts_include_subdomains);
3935  EXPECT_FALSE(domain_state.pkp_include_subdomains);
3936#if defined(OS_ANDROID)
3937  // Android's CertVerifyProc does not (yet) handle pins.
3938#else
3939  EXPECT_FALSE(domain_state.HasPublicKeyPins());
3940#endif
3941}
3942
3943// Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
3944// reject HPKP headers, and a test setting only HPKP headers will fail (no
3945// DomainState present because header rejected).
3946#if defined(OS_ANDROID)
3947#define MAYBE_ProcessPKP DISABLED_ProcessPKP
3948#else
3949#define MAYBE_ProcessPKP ProcessPKP
3950#endif
3951
3952// Tests that enabling HPKP on a domain does not affect the HSTS
3953// validity/expiration.
3954TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) {
3955  SpawnedTestServer::SSLOptions ssl_options;
3956  SpawnedTestServer https_test_server(
3957      SpawnedTestServer::TYPE_HTTPS,
3958      ssl_options,
3959      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
3960  ASSERT_TRUE(https_test_server.Start());
3961
3962  TestDelegate d;
3963  URLRequest request(
3964      https_test_server.GetURL("files/hpkp-headers.html"),
3965      &d,
3966      &default_context_);
3967  request.Start();
3968  base::MessageLoop::current()->Run();
3969
3970  TransportSecurityState* security_state =
3971      default_context_.transport_security_state();
3972  bool sni_available = true;
3973  TransportSecurityState::DomainState domain_state;
3974  EXPECT_TRUE(security_state->GetDomainState(
3975      SpawnedTestServer::kLocalhost, sni_available, &domain_state));
3976  EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT,
3977            domain_state.upgrade_mode);
3978  EXPECT_FALSE(domain_state.sts_include_subdomains);
3979  EXPECT_FALSE(domain_state.pkp_include_subdomains);
3980  EXPECT_TRUE(domain_state.HasPublicKeyPins());
3981  EXPECT_NE(domain_state.upgrade_expiry,
3982            domain_state.dynamic_spki_hashes_expiry);
3983}
3984
3985TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
3986  SpawnedTestServer::SSLOptions ssl_options;
3987  SpawnedTestServer https_test_server(
3988      SpawnedTestServer::TYPE_HTTPS,
3989      ssl_options,
3990      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
3991  ASSERT_TRUE(https_test_server.Start());
3992
3993  TestDelegate d;
3994  URLRequest request(
3995      https_test_server.GetURL("files/hsts-multiple-headers.html"),
3996      &d,
3997      &default_context_);
3998  request.Start();
3999  base::MessageLoop::current()->Run();
4000
4001  // We should have set parameters from the first header, not the second.
4002  TransportSecurityState* security_state =
4003      default_context_.transport_security_state();
4004  bool sni_available = true;
4005  TransportSecurityState::DomainState domain_state;
4006  EXPECT_TRUE(security_state->GetDomainState(
4007      SpawnedTestServer::kLocalhost, sni_available, &domain_state));
4008  EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
4009            domain_state.upgrade_mode);
4010  EXPECT_FALSE(domain_state.sts_include_subdomains);
4011  EXPECT_FALSE(domain_state.pkp_include_subdomains);
4012}
4013
4014TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) {
4015  SpawnedTestServer::SSLOptions ssl_options;
4016  SpawnedTestServer https_test_server(
4017      SpawnedTestServer::TYPE_HTTPS,
4018      ssl_options,
4019      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
4020  ASSERT_TRUE(https_test_server.Start());
4021
4022  TestDelegate d;
4023  URLRequest request(
4024      https_test_server.GetURL("files/hsts-and-hpkp-headers.html"),
4025      &d,
4026      &default_context_);
4027  request.Start();
4028  base::MessageLoop::current()->Run();
4029
4030  // We should have set parameters from the first header, not the second.
4031  TransportSecurityState* security_state =
4032      default_context_.transport_security_state();
4033  bool sni_available = true;
4034  TransportSecurityState::DomainState domain_state;
4035  EXPECT_TRUE(security_state->GetDomainState(
4036      SpawnedTestServer::kLocalhost, sni_available, &domain_state));
4037  EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
4038            domain_state.upgrade_mode);
4039#if defined(OS_ANDROID)
4040  // Android's CertVerifyProc does not (yet) handle pins.
4041#else
4042  EXPECT_TRUE(domain_state.HasPublicKeyPins());
4043#endif
4044  EXPECT_NE(domain_state.upgrade_expiry,
4045            domain_state.dynamic_spki_hashes_expiry);
4046
4047  // Even though there is an HSTS header asserting includeSubdomains, it is
4048  // the *second* such header, and we MUST process only the first.
4049  EXPECT_FALSE(domain_state.sts_include_subdomains);
4050  // includeSubdomains does not occur in the test HPKP header.
4051  EXPECT_FALSE(domain_state.pkp_include_subdomains);
4052}
4053
4054// Tests that when multiple HPKP headers are present, asserting different
4055// policies, that only the first such policy is processed.
4056TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) {
4057  SpawnedTestServer::SSLOptions ssl_options;
4058  SpawnedTestServer https_test_server(
4059      SpawnedTestServer::TYPE_HTTPS,
4060      ssl_options,
4061      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
4062  ASSERT_TRUE(https_test_server.Start());
4063
4064  TestDelegate d;
4065  URLRequest request(
4066      https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"),
4067      &d,
4068      &default_context_);
4069  request.Start();
4070  base::MessageLoop::current()->Run();
4071
4072  TransportSecurityState* security_state =
4073      default_context_.transport_security_state();
4074  bool sni_available = true;
4075  TransportSecurityState::DomainState domain_state;
4076  EXPECT_TRUE(security_state->GetDomainState(
4077      SpawnedTestServer::kLocalhost, sni_available, &domain_state));
4078  EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
4079            domain_state.upgrade_mode);
4080#if defined(OS_ANDROID)
4081  // Android's CertVerifyProc does not (yet) handle pins.
4082#else
4083  EXPECT_TRUE(domain_state.HasPublicKeyPins());
4084#endif
4085  EXPECT_NE(domain_state.upgrade_expiry,
4086            domain_state.dynamic_spki_hashes_expiry);
4087
4088  EXPECT_TRUE(domain_state.sts_include_subdomains);
4089  EXPECT_FALSE(domain_state.pkp_include_subdomains);
4090}
4091
4092TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
4093  ASSERT_TRUE(test_server_.Start());
4094
4095  TestDelegate d;
4096  URLRequest req(test_server_.GetURL(
4097      "files/content-type-normalization.html"), &d, &default_context_);
4098  req.Start();
4099  base::MessageLoop::current()->Run();
4100
4101  std::string mime_type;
4102  req.GetMimeType(&mime_type);
4103  EXPECT_EQ("text/html", mime_type);
4104
4105  std::string charset;
4106  req.GetCharset(&charset);
4107  EXPECT_EQ("utf-8", charset);
4108  req.Cancel();
4109}
4110
4111TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictRedirects) {
4112  // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
4113  GURL file_url("file:///foo.txt");
4114  GURL data_url("data:,foo");
4115  FileProtocolHandler file_protocol_handler;
4116  EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url));
4117  DataProtocolHandler data_protocol_handler;
4118  EXPECT_TRUE(data_protocol_handler.IsSafeRedirectTarget(data_url));
4119
4120  // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
4121  EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url));
4122  EXPECT_TRUE(job_factory_.IsSafeRedirectTarget(data_url));
4123}
4124
4125TEST_F(URLRequestTestHTTP, RestrictRedirects) {
4126  ASSERT_TRUE(test_server_.Start());
4127
4128  TestDelegate d;
4129  URLRequest req(test_server_.GetURL(
4130      "files/redirect-to-file.html"), &d, &default_context_);
4131  req.Start();
4132  base::MessageLoop::current()->Run();
4133
4134  EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
4135  EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
4136}
4137
4138TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
4139  ASSERT_TRUE(test_server_.Start());
4140
4141  TestDelegate d;
4142  URLRequest req(test_server_.GetURL(
4143      "files/redirect-to-invalid-url.html"), &d, &default_context_);
4144  req.Start();
4145  base::MessageLoop::current()->Run();
4146
4147  EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
4148  EXPECT_EQ(ERR_INVALID_URL, req.status().error());
4149}
4150
4151TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
4152  ASSERT_TRUE(test_server_.Start());
4153
4154  TestDelegate d;
4155  URLRequest req(
4156      test_server_.GetURL("echoheader?Referer"), &d, &default_context_);
4157  req.SetReferrer("http://user:pass@foo.com/");
4158  req.Start();
4159  base::MessageLoop::current()->Run();
4160
4161  EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
4162}
4163
4164TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) {
4165  ASSERT_TRUE(test_server_.Start());
4166
4167  TestDelegate d;
4168  URLRequest req(
4169      test_server_.GetURL("echoheader?Referer"), &d, &default_context_);
4170  req.SetReferrer("http://foo.com/test#fragment");
4171  req.Start();
4172  base::MessageLoop::current()->Run();
4173
4174  EXPECT_EQ(std::string("http://foo.com/test"), d.data_received());
4175}
4176
4177TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) {
4178  ASSERT_TRUE(test_server_.Start());
4179
4180  TestDelegate d;
4181  URLRequest req(
4182      test_server_.GetURL("echoheader?Referer"), &d, &default_context_);
4183  req.SetReferrer("http://foo.com/test#fragment");
4184  req.SetReferrer("");
4185  req.Start();
4186  base::MessageLoop::current()->Run();
4187
4188  EXPECT_EQ(std::string("None"), d.data_received());
4189}
4190
4191TEST_F(URLRequestTestHTTP, CancelRedirect) {
4192  ASSERT_TRUE(test_server_.Start());
4193
4194  TestDelegate d;
4195  {
4196    d.set_cancel_in_received_redirect(true);
4197    URLRequest req(
4198        test_server_.GetURL("files/redirect-test.html"), &d, &default_context_);
4199    req.Start();
4200    base::MessageLoop::current()->Run();
4201
4202    EXPECT_EQ(1, d.response_started_count());
4203    EXPECT_EQ(0, d.bytes_received());
4204    EXPECT_FALSE(d.received_data_before_response());
4205    EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
4206  }
4207}
4208
4209TEST_F(URLRequestTestHTTP, DeferredRedirect) {
4210  ASSERT_TRUE(test_server_.Start());
4211
4212  TestDelegate d;
4213  {
4214    d.set_quit_on_redirect(true);
4215    GURL test_url(test_server_.GetURL("files/redirect-test.html"));
4216    URLRequest req(test_url, &d, &default_context_);
4217
4218    req.Start();
4219    base::MessageLoop::current()->Run();
4220
4221    EXPECT_EQ(1, d.received_redirect_count());
4222
4223    req.FollowDeferredRedirect();
4224    base::MessageLoop::current()->Run();
4225
4226    EXPECT_EQ(1, d.response_started_count());
4227    EXPECT_FALSE(d.received_data_before_response());
4228    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
4229
4230    base::FilePath path;
4231    PathService::Get(base::DIR_SOURCE_ROOT, &path);
4232    path = path.Append(FILE_PATH_LITERAL("net"));
4233    path = path.Append(FILE_PATH_LITERAL("data"));
4234    path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
4235    path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
4236
4237    std::string contents;
4238    EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
4239    EXPECT_EQ(contents, d.data_received());
4240  }
4241}
4242
4243TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) {
4244  ASSERT_TRUE(test_server_.Start());
4245
4246  TestDelegate d;
4247  {
4248    d.set_quit_on_redirect(true);
4249    GURL test_url(test_server_.GetURL("files/redirect-test.html"));
4250    URLRequest req(test_url, &d, &default_context_);
4251
4252    EXPECT_FALSE(d.have_full_request_headers());
4253
4254    req.Start();
4255    base::MessageLoop::current()->Run();
4256
4257    EXPECT_EQ(1, d.received_redirect_count());
4258    EXPECT_TRUE(d.have_full_request_headers());
4259    CheckFullRequestHeaders(d.full_request_headers(), test_url);
4260    d.ClearFullRequestHeaders();
4261
4262    req.FollowDeferredRedirect();
4263    base::MessageLoop::current()->Run();
4264
4265    GURL target_url(test_server_.GetURL("files/with-headers.html"));
4266    EXPECT_EQ(1, d.response_started_count());
4267    EXPECT_TRUE(d.have_full_request_headers());
4268    CheckFullRequestHeaders(d.full_request_headers(), target_url);
4269    EXPECT_FALSE(d.received_data_before_response());
4270    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
4271
4272    base::FilePath path;
4273    PathService::Get(base::DIR_SOURCE_ROOT, &path);
4274    path = path.Append(FILE_PATH_LITERAL("net"));
4275    path = path.Append(FILE_PATH_LITERAL("data"));
4276    path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
4277    path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
4278
4279    std::string contents;
4280    EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
4281    EXPECT_EQ(contents, d.data_received());
4282  }
4283}
4284
4285TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
4286  ASSERT_TRUE(test_server_.Start());
4287
4288  TestDelegate d;
4289  {
4290    d.set_quit_on_redirect(true);
4291    URLRequest req(
4292        test_server_.GetURL("files/redirect-test.html"), &d, &default_context_);
4293    req.Start();
4294    base::MessageLoop::current()->Run();
4295
4296    EXPECT_EQ(1, d.received_redirect_count());
4297
4298    req.Cancel();
4299    base::MessageLoop::current()->Run();
4300
4301    EXPECT_EQ(1, d.response_started_count());
4302    EXPECT_EQ(0, d.bytes_received());
4303    EXPECT_FALSE(d.received_data_before_response());
4304    EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
4305  }
4306}
4307
4308TEST_F(URLRequestTestHTTP, VaryHeader) {
4309  ASSERT_TRUE(test_server_.Start());
4310
4311  // Populate the cache.
4312  {
4313    TestDelegate d;
4314    URLRequest req(
4315        test_server_.GetURL("echoheadercache?foo"), &d, &default_context_);
4316    HttpRequestHeaders headers;
4317    headers.SetHeader("foo", "1");
4318    req.SetExtraRequestHeaders(headers);
4319    req.Start();
4320    base::MessageLoop::current()->Run();
4321
4322    LoadTimingInfo load_timing_info;
4323    req.GetLoadTimingInfo(&load_timing_info);
4324    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
4325  }
4326
4327  // Expect a cache hit.
4328  {
4329    TestDelegate d;
4330    URLRequest req(
4331        test_server_.GetURL("echoheadercache?foo"), &d, &default_context_);
4332    HttpRequestHeaders headers;
4333    headers.SetHeader("foo", "1");
4334    req.SetExtraRequestHeaders(headers);
4335    req.Start();
4336    base::MessageLoop::current()->Run();
4337
4338    EXPECT_TRUE(req.was_cached());
4339
4340    LoadTimingInfo load_timing_info;
4341    req.GetLoadTimingInfo(&load_timing_info);
4342    TestLoadTimingCacheHitNoNetwork(load_timing_info);
4343  }
4344
4345  // Expect a cache miss.
4346  {
4347    TestDelegate d;
4348    URLRequest req(
4349        test_server_.GetURL("echoheadercache?foo"), &d, &default_context_);
4350    HttpRequestHeaders headers;
4351    headers.SetHeader("foo", "2");
4352    req.SetExtraRequestHeaders(headers);
4353    req.Start();
4354    base::MessageLoop::current()->Run();
4355
4356    EXPECT_FALSE(req.was_cached());
4357
4358    LoadTimingInfo load_timing_info;
4359    req.GetLoadTimingInfo(&load_timing_info);
4360    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
4361  }
4362}
4363
4364TEST_F(URLRequestTestHTTP, BasicAuth) {
4365  ASSERT_TRUE(test_server_.Start());
4366
4367  // populate the cache
4368  {
4369    TestDelegate d;
4370    d.set_credentials(AuthCredentials(kUser, kSecret));
4371
4372    URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
4373    r.Start();
4374
4375    base::MessageLoop::current()->Run();
4376
4377    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4378  }
4379
4380  // repeat request with end-to-end validation.  since auth-basic results in a
4381  // cachable page, we expect this test to result in a 304.  in which case, the
4382  // response should be fetched from the cache.
4383  {
4384    TestDelegate d;
4385    d.set_credentials(AuthCredentials(kUser, kSecret));
4386
4387    URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
4388    r.set_load_flags(LOAD_VALIDATE_CACHE);
4389    r.Start();
4390
4391    base::MessageLoop::current()->Run();
4392
4393    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4394
4395    // Should be the same cached document.
4396    EXPECT_TRUE(r.was_cached());
4397  }
4398}
4399
4400// Check that Set-Cookie headers in 401 responses are respected.
4401// http://crbug.com/6450
4402TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
4403  ASSERT_TRUE(test_server_.Start());
4404
4405  GURL url_requiring_auth =
4406      test_server_.GetURL("auth-basic?set-cookie-if-challenged");
4407
4408  // Request a page that will give a 401 containing a Set-Cookie header.
4409  // Verify that when the transaction is restarted, it includes the new cookie.
4410  {
4411    TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
4412    TestURLRequestContext context(true);
4413    context.set_network_delegate(&network_delegate);
4414    context.Init();
4415
4416    TestDelegate d;
4417    d.set_credentials(AuthCredentials(kUser, kSecret));
4418
4419    URLRequest r(url_requiring_auth, &d, &context);
4420    r.Start();
4421
4422    base::MessageLoop::current()->Run();
4423
4424    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4425
4426    // Make sure we sent the cookie in the restarted transaction.
4427    EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
4428        != std::string::npos);
4429  }
4430
4431  // Same test as above, except this time the restart is initiated earlier
4432  // (without user intervention since identity is embedded in the URL).
4433  {
4434    TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
4435    TestURLRequestContext context(true);
4436    context.set_network_delegate(&network_delegate);
4437    context.Init();
4438
4439    TestDelegate d;
4440
4441    GURL::Replacements replacements;
4442    std::string username("user2");
4443    std::string password("secret");
4444    replacements.SetUsernameStr(username);
4445    replacements.SetPasswordStr(password);
4446    GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
4447
4448    URLRequest r(url_with_identity, &d, &context);
4449    r.Start();
4450
4451    base::MessageLoop::current()->Run();
4452
4453    EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
4454
4455    // Make sure we sent the cookie in the restarted transaction.
4456    EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
4457        != std::string::npos);
4458  }
4459}
4460
4461// Tests that load timing works as expected with auth and the cache.
4462TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
4463  ASSERT_TRUE(test_server_.Start());
4464
4465  // populate the cache
4466  {
4467    TestDelegate d;
4468    d.set_credentials(AuthCredentials(kUser, kSecret));
4469
4470    URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
4471    r.Start();
4472
4473    base::MessageLoop::current()->Run();
4474
4475    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4476
4477    LoadTimingInfo load_timing_info_before_auth;
4478    EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
4479        &load_timing_info_before_auth));
4480    TestLoadTimingNotReused(load_timing_info_before_auth,
4481                            CONNECT_TIMING_HAS_DNS_TIMES);
4482
4483    LoadTimingInfo load_timing_info;
4484    r.GetLoadTimingInfo(&load_timing_info);
4485    // The test server does not support keep alive sockets, so the second
4486    // request with auth should use a new socket.
4487    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
4488    EXPECT_NE(load_timing_info_before_auth.socket_log_id,
4489              load_timing_info.socket_log_id);
4490    EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
4491              load_timing_info.connect_timing.connect_start);
4492  }
4493
4494  // Repeat request with end-to-end validation.  Since auth-basic results in a
4495  // cachable page, we expect this test to result in a 304.  In which case, the
4496  // response should be fetched from the cache.
4497  {
4498    TestDelegate d;
4499    d.set_credentials(AuthCredentials(kUser, kSecret));
4500
4501    URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
4502    r.set_load_flags(LOAD_VALIDATE_CACHE);
4503    r.Start();
4504
4505    base::MessageLoop::current()->Run();
4506
4507    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4508
4509    // Should be the same cached document.
4510    EXPECT_TRUE(r.was_cached());
4511
4512    // Since there was a request that went over the wire, the load timing
4513    // information should include connection times.
4514    LoadTimingInfo load_timing_info;
4515    r.GetLoadTimingInfo(&load_timing_info);
4516    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
4517  }
4518}
4519
4520// In this test, we do a POST which the server will 302 redirect.
4521// The subsequent transaction should use GET, and should not send the
4522// Content-Type header.
4523// http://code.google.com/p/chromium/issues/detail?id=843
4524TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
4525  ASSERT_TRUE(test_server_.Start());
4526
4527  const char kData[] = "hello world";
4528
4529  TestDelegate d;
4530  URLRequest req(
4531      test_server_.GetURL("files/redirect-to-echoall"), &d, &default_context_);
4532  req.set_method("POST");
4533  req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
4534
4535  // Set headers (some of which are specific to the POST).
4536  HttpRequestHeaders headers;
4537  headers.AddHeadersFromString(
4538    "Content-Type: multipart/form-data; "
4539    "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
4540    "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
4541    "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
4542    "Accept-Language: en-US,en\r\n"
4543    "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
4544    "Content-Length: 11\r\n"
4545    "Origin: http://localhost:1337/");
4546  req.SetExtraRequestHeaders(headers);
4547  req.Start();
4548  base::MessageLoop::current()->Run();
4549
4550  std::string mime_type;
4551  req.GetMimeType(&mime_type);
4552  EXPECT_EQ("text/html", mime_type);
4553
4554  const std::string& data = d.data_received();
4555
4556  // Check that the post-specific headers were stripped:
4557  EXPECT_FALSE(ContainsString(data, "Content-Length:"));
4558  EXPECT_FALSE(ContainsString(data, "Content-Type:"));
4559  EXPECT_FALSE(ContainsString(data, "Origin:"));
4560
4561  // These extra request headers should not have been stripped.
4562  EXPECT_TRUE(ContainsString(data, "Accept:"));
4563  EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
4564  EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
4565}
4566
4567// The following tests check that we handle mutating the request method for
4568// HTTP redirects as expected.
4569// See http://crbug.com/56373 and http://crbug.com/102130.
4570
4571TEST_F(URLRequestTestHTTP, Redirect301Tests) {
4572  ASSERT_TRUE(test_server_.Start());
4573
4574  const GURL url = test_server_.GetURL("files/redirect301-to-echo");
4575
4576  HTTPRedirectMethodTest(url, "POST", "GET", true);
4577  HTTPRedirectMethodTest(url, "PUT", "PUT", true);
4578  HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
4579}
4580
4581TEST_F(URLRequestTestHTTP, Redirect302Tests) {
4582  ASSERT_TRUE(test_server_.Start());
4583
4584  const GURL url = test_server_.GetURL("files/redirect302-to-echo");
4585
4586  HTTPRedirectMethodTest(url, "POST", "GET", true);
4587  HTTPRedirectMethodTest(url, "PUT", "PUT", true);
4588  HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
4589}
4590
4591TEST_F(URLRequestTestHTTP, Redirect303Tests) {
4592  ASSERT_TRUE(test_server_.Start());
4593
4594  const GURL url = test_server_.GetURL("files/redirect303-to-echo");
4595
4596  HTTPRedirectMethodTest(url, "POST", "GET", true);
4597  HTTPRedirectMethodTest(url, "PUT", "GET", true);
4598  HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
4599}
4600
4601TEST_F(URLRequestTestHTTP, Redirect307Tests) {
4602  ASSERT_TRUE(test_server_.Start());
4603
4604  const GURL url = test_server_.GetURL("files/redirect307-to-echo");
4605
4606  HTTPRedirectMethodTest(url, "POST", "POST", true);
4607  HTTPRedirectMethodTest(url, "PUT", "PUT", true);
4608  HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
4609}
4610
4611TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
4612  ASSERT_TRUE(test_server_.Start());
4613
4614  const char kData[] = "hello world";
4615
4616  TestDelegate d;
4617  URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_);
4618  req.set_method("POST");
4619  req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
4620  HttpRequestHeaders headers;
4621  headers.SetHeader(HttpRequestHeaders::kContentLength,
4622                    base::UintToString(arraysize(kData) - 1));
4623  req.SetExtraRequestHeaders(headers);
4624
4625  URLRequestRedirectJob* job = new URLRequestRedirectJob(
4626      &req, &default_network_delegate_, test_server_.GetURL("echo"),
4627      URLRequestRedirectJob::REDIRECT_302_FOUND);
4628  AddTestInterceptor()->set_main_intercept_job(job);
4629
4630  req.Start();
4631  base::MessageLoop::current()->Run();
4632  EXPECT_EQ("GET", req.method());
4633}
4634
4635TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
4636  ASSERT_TRUE(test_server_.Start());
4637
4638  const char kData[] = "hello world";
4639
4640  TestDelegate d;
4641  URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_);
4642  req.set_method("POST");
4643  req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
4644  HttpRequestHeaders headers;
4645  headers.SetHeader(HttpRequestHeaders::kContentLength,
4646                    base::UintToString(arraysize(kData) - 1));
4647  req.SetExtraRequestHeaders(headers);
4648
4649  URLRequestRedirectJob* job = new URLRequestRedirectJob(
4650      &req, &default_network_delegate_, test_server_.GetURL("echo"),
4651      URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT);
4652  AddTestInterceptor()->set_main_intercept_job(job);
4653
4654  req.Start();
4655  base::MessageLoop::current()->Run();
4656  EXPECT_EQ("POST", req.method());
4657  EXPECT_EQ(kData, d.data_received());
4658}
4659
4660// Check that default A-L header is sent.
4661TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
4662  ASSERT_TRUE(test_server_.Start());
4663
4664  StaticHttpUserAgentSettings settings("en", EmptyString());
4665  TestNetworkDelegate network_delegate;  // Must outlive URLRequests.
4666  TestURLRequestContext context(true);
4667  context.set_network_delegate(&network_delegate);
4668  context.set_http_user_agent_settings(&settings);
4669  context.Init();
4670
4671  TestDelegate d;
4672  URLRequest req(
4673      test_server_.GetURL("echoheader?Accept-Language"), &d, &context);
4674  req.Start();
4675  base::MessageLoop::current()->Run();
4676  EXPECT_EQ("en", d.data_received());
4677}
4678
4679// Check that an empty A-L header is not sent. http://crbug.com/77365.
4680TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
4681  ASSERT_TRUE(test_server_.Start());
4682
4683  StaticHttpUserAgentSettings settings(EmptyString(), EmptyString());
4684  TestNetworkDelegate network_delegate;  // Must outlive URLRequests.
4685  TestURLRequestContext context(true);
4686  context.set_network_delegate(&network_delegate);
4687  context.Init();
4688  // We override the language after initialization because empty entries
4689  // get overridden by Init().
4690  context.set_http_user_agent_settings(&settings);
4691
4692  TestDelegate d;
4693  URLRequest req(
4694      test_server_.GetURL("echoheader?Accept-Language"), &d, &context);
4695  req.Start();
4696  base::MessageLoop::current()->Run();
4697  EXPECT_EQ("None", d.data_received());
4698}
4699
4700// Check that if request overrides the A-L header, the default is not appended.
4701// See http://crbug.com/20894
4702TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
4703  ASSERT_TRUE(test_server_.Start());
4704
4705  TestDelegate d;
4706  URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
4707                 &d,
4708                 &default_context_);
4709  HttpRequestHeaders headers;
4710  headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
4711  req.SetExtraRequestHeaders(headers);
4712  req.Start();
4713  base::MessageLoop::current()->Run();
4714  EXPECT_EQ(std::string("ru"), d.data_received());
4715}
4716
4717// Check that default A-E header is sent.
4718TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
4719  ASSERT_TRUE(test_server_.Start());
4720
4721  TestDelegate d;
4722  URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
4723                 &d,
4724                 &default_context_);
4725  HttpRequestHeaders headers;
4726  req.SetExtraRequestHeaders(headers);
4727  req.Start();
4728  base::MessageLoop::current()->Run();
4729  EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
4730}
4731
4732// Check that if request overrides the A-E header, the default is not appended.
4733// See http://crbug.com/47381
4734TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
4735  ASSERT_TRUE(test_server_.Start());
4736
4737  TestDelegate d;
4738  URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
4739                 &d,
4740                 &default_context_);
4741  HttpRequestHeaders headers;
4742  headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
4743  req.SetExtraRequestHeaders(headers);
4744  req.Start();
4745  base::MessageLoop::current()->Run();
4746  EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
4747  EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
4748}
4749
4750// Check that setting the A-C header sends the proper header.
4751TEST_F(URLRequestTestHTTP, SetAcceptCharset) {
4752  ASSERT_TRUE(test_server_.Start());
4753
4754  TestDelegate d;
4755  URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"),
4756                 &d,
4757                 &default_context_);
4758  HttpRequestHeaders headers;
4759  headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
4760  req.SetExtraRequestHeaders(headers);
4761  req.Start();
4762  base::MessageLoop::current()->Run();
4763  EXPECT_EQ(std::string("koi-8r"), d.data_received());
4764}
4765
4766// Check that default User-Agent header is sent.
4767TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
4768  ASSERT_TRUE(test_server_.Start());
4769
4770  TestDelegate d;
4771  URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
4772                 &d,
4773                 &default_context_);
4774  req.Start();
4775  base::MessageLoop::current()->Run();
4776  EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received());
4777}
4778
4779// Check that if request overrides the User-Agent header,
4780// the default is not appended.
4781TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
4782  ASSERT_TRUE(test_server_.Start());
4783
4784  TestDelegate d;
4785  URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
4786                 &d,
4787                 &default_context_);
4788  HttpRequestHeaders headers;
4789  headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
4790  req.SetExtraRequestHeaders(headers);
4791  req.Start();
4792  base::MessageLoop::current()->Run();
4793  // If the net tests are being run with ChromeFrame then we need to allow for
4794  // the 'chromeframe' suffix which is added to the user agent before the
4795  // closing parentheses.
4796  EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true));
4797}
4798
4799// Check that a NULL HttpUserAgentSettings causes the corresponding empty
4800// User-Agent header to be sent but does not send the Accept-Language and
4801// Accept-Charset headers.
4802TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
4803  ASSERT_TRUE(test_server_.Start());
4804
4805  TestNetworkDelegate network_delegate;  // Must outlive URLRequests.
4806  TestURLRequestContext context(true);
4807  context.set_network_delegate(&network_delegate);
4808  context.Init();
4809  // We override the HttpUserAgentSettings after initialization because empty
4810  // entries get overridden by Init().
4811  context.set_http_user_agent_settings(NULL);
4812
4813  struct {
4814    const char* request;
4815    const char* expected_response;
4816  } tests[] = { { "echoheader?Accept-Language", "None" },
4817                { "echoheader?Accept-Charset", "None" },
4818                { "echoheader?User-Agent", "" } };
4819
4820  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
4821    TestDelegate d;
4822    URLRequest req(test_server_.GetURL(tests[i].request), &d, &context);
4823    req.Start();
4824    base::MessageLoop::current()->Run();
4825    EXPECT_EQ(tests[i].expected_response, d.data_received())
4826        << " Request = \"" << tests[i].request << "\"";
4827  }
4828}
4829
4830// Make sure that URLRequest passes on its priority updates to
4831// newly-created jobs after the first one.
4832TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) {
4833  ASSERT_TRUE(test_server_.Start());
4834
4835  TestDelegate d;
4836  URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_);
4837  EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
4838
4839  scoped_refptr<URLRequestRedirectJob> redirect_job =
4840      new URLRequestRedirectJob(
4841          &req, &default_network_delegate_, test_server_.GetURL("echo"),
4842          URLRequestRedirectJob::REDIRECT_302_FOUND);
4843  AddTestInterceptor()->set_main_intercept_job(redirect_job.get());
4844
4845  req.SetPriority(LOW);
4846  req.Start();
4847  EXPECT_TRUE(req.is_pending());
4848
4849  scoped_refptr<URLRequestTestJob> job =
4850      new URLRequestTestJob(&req, &default_network_delegate_);
4851  AddTestInterceptor()->set_main_intercept_job(job.get());
4852
4853  // Should trigger |job| to be started.
4854  base::MessageLoop::current()->Run();
4855  EXPECT_EQ(LOW, job->priority());
4856}
4857
4858class HTTPSRequestTest : public testing::Test {
4859 public:
4860  HTTPSRequestTest() : default_context_(true) {
4861    default_context_.set_network_delegate(&default_network_delegate_);
4862    default_context_.Init();
4863  }
4864  virtual ~HTTPSRequestTest() {}
4865
4866 protected:
4867  TestNetworkDelegate default_network_delegate_;  // Must outlive URLRequest.
4868  TestURLRequestContext default_context_;
4869};
4870
4871TEST_F(HTTPSRequestTest, HTTPSGetTest) {
4872  SpawnedTestServer test_server(
4873      SpawnedTestServer::TYPE_HTTPS,
4874      SpawnedTestServer::kLocalhost,
4875      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
4876  ASSERT_TRUE(test_server.Start());
4877
4878  TestDelegate d;
4879  {
4880    URLRequest r(test_server.GetURL(std::string()), &d, &default_context_);
4881    r.Start();
4882    EXPECT_TRUE(r.is_pending());
4883
4884    base::MessageLoop::current()->Run();
4885
4886    EXPECT_EQ(1, d.response_started_count());
4887    EXPECT_FALSE(d.received_data_before_response());
4888    EXPECT_NE(0, d.bytes_received());
4889    CheckSSLInfo(r.ssl_info());
4890    EXPECT_EQ(test_server.host_port_pair().host(),
4891              r.GetSocketAddress().host());
4892    EXPECT_EQ(test_server.host_port_pair().port(),
4893              r.GetSocketAddress().port());
4894  }
4895}
4896
4897TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
4898  SpawnedTestServer::SSLOptions ssl_options(
4899      SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
4900  SpawnedTestServer test_server(
4901      SpawnedTestServer::TYPE_HTTPS,
4902      ssl_options,
4903      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
4904  ASSERT_TRUE(test_server.Start());
4905
4906  bool err_allowed = true;
4907  for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
4908    TestDelegate d;
4909    {
4910      d.set_allow_certificate_errors(err_allowed);
4911      URLRequest r(test_server.GetURL(std::string()), &d, &default_context_);
4912
4913      r.Start();
4914      EXPECT_TRUE(r.is_pending());
4915
4916      base::MessageLoop::current()->Run();
4917
4918      EXPECT_EQ(1, d.response_started_count());
4919      EXPECT_FALSE(d.received_data_before_response());
4920      EXPECT_TRUE(d.have_certificate_errors());
4921      if (err_allowed) {
4922        EXPECT_NE(0, d.bytes_received());
4923        CheckSSLInfo(r.ssl_info());
4924      } else {
4925        EXPECT_EQ(0, d.bytes_received());
4926      }
4927    }
4928  }
4929}
4930
4931TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
4932  SpawnedTestServer::SSLOptions ssl_options(
4933      SpawnedTestServer::SSLOptions::CERT_EXPIRED);
4934  SpawnedTestServer test_server(
4935      SpawnedTestServer::TYPE_HTTPS,
4936      ssl_options,
4937      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
4938  ASSERT_TRUE(test_server.Start());
4939
4940  // Iterate from false to true, just so that we do the opposite of the
4941  // previous test in order to increase test coverage.
4942  bool err_allowed = false;
4943  for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
4944    TestDelegate d;
4945    {
4946      d.set_allow_certificate_errors(err_allowed);
4947      URLRequest r(test_server.GetURL(std::string()), &d, &default_context_);
4948
4949      r.Start();
4950      EXPECT_TRUE(r.is_pending());
4951
4952      base::MessageLoop::current()->Run();
4953
4954      EXPECT_EQ(1, d.response_started_count());
4955      EXPECT_FALSE(d.received_data_before_response());
4956      EXPECT_TRUE(d.have_certificate_errors());
4957      if (err_allowed) {
4958        EXPECT_NE(0, d.bytes_received());
4959        CheckSSLInfo(r.ssl_info());
4960      } else {
4961        EXPECT_EQ(0, d.bytes_received());
4962      }
4963    }
4964  }
4965}
4966
4967// Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
4968// than necessary.
4969TEST_F(HTTPSRequestTest, TLSv1Fallback) {
4970  uint16 default_version_max = SSLConfigService::default_version_max();
4971  // The OpenSSL library in use may not support TLS 1.1.
4972#if !defined(USE_OPENSSL)
4973  EXPECT_GT(default_version_max, SSL_PROTOCOL_VERSION_TLS1);
4974#endif
4975  if (default_version_max <= SSL_PROTOCOL_VERSION_TLS1)
4976    return;
4977
4978  SpawnedTestServer::SSLOptions ssl_options(
4979      SpawnedTestServer::SSLOptions::CERT_OK);
4980  ssl_options.tls_intolerant =
4981      SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
4982  SpawnedTestServer test_server(
4983      SpawnedTestServer::TYPE_HTTPS,
4984      ssl_options,
4985      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
4986  ASSERT_TRUE(test_server.Start());
4987
4988  TestDelegate d;
4989  TestURLRequestContext context(true);
4990  context.Init();
4991  d.set_allow_certificate_errors(true);
4992  URLRequest r(test_server.GetURL(std::string()), &d, &context);
4993  r.Start();
4994
4995  base::MessageLoop::current()->Run();
4996
4997  EXPECT_EQ(1, d.response_started_count());
4998  EXPECT_NE(0, d.bytes_received());
4999  EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_TLS1),
5000            SSLConnectionStatusToVersion(r.ssl_info().connection_status));
5001  EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK);
5002}
5003
5004// This tests that a load of www.google.com with a certificate error sets
5005// the |certificate_errors_are_fatal| flag correctly. This flag will cause
5006// the interstitial to be fatal.
5007TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
5008  SpawnedTestServer::SSLOptions ssl_options(
5009      SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
5010  SpawnedTestServer test_server(
5011      SpawnedTestServer::TYPE_HTTPS,
5012      ssl_options,
5013      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
5014  ASSERT_TRUE(test_server.Start());
5015
5016  // We require that the URL be www.google.com in order to pick up the
5017  // preloaded HSTS entries in the TransportSecurityState. This means that we
5018  // have to use a MockHostResolver in order to direct www.google.com to the
5019  // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
5020
5021  MockHostResolver host_resolver;
5022  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
5023  TestURLRequestContext context(true);
5024  context.set_network_delegate(&network_delegate);
5025  context.set_host_resolver(&host_resolver);
5026  TransportSecurityState transport_security_state;
5027  context.set_transport_security_state(&transport_security_state);
5028  context.Init();
5029
5030  TestDelegate d;
5031  URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
5032                                       test_server.host_port_pair().port())),
5033               &d,
5034               &context);
5035
5036  r.Start();
5037  EXPECT_TRUE(r.is_pending());
5038
5039  base::MessageLoop::current()->Run();
5040
5041  EXPECT_EQ(1, d.response_started_count());
5042  EXPECT_FALSE(d.received_data_before_response());
5043  EXPECT_TRUE(d.have_certificate_errors());
5044  EXPECT_TRUE(d.certificate_errors_are_fatal());
5045}
5046
5047// This tests that cached HTTPS page loads do not cause any updates to the
5048// TransportSecurityState.
5049TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
5050  // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
5051  // matter. It just has to be any error.
5052  SpawnedTestServer::SSLOptions ssl_options(
5053      SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
5054  SpawnedTestServer test_server(
5055      SpawnedTestServer::TYPE_HTTPS,
5056      ssl_options,
5057      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
5058  ASSERT_TRUE(test_server.Start());
5059
5060  // We require that the URL be www.google.com in order to pick up the
5061  // preloaded and dynamic HSTS and public key pin entries in the
5062  // TransportSecurityState. This means that we have to use a
5063  // MockHostResolver in order to direct www.google.com to the testserver.
5064  // By default, MockHostResolver maps all hosts to 127.0.0.1.
5065
5066  MockHostResolver host_resolver;
5067  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
5068  TestURLRequestContext context(true);
5069  context.set_network_delegate(&network_delegate);
5070  context.set_host_resolver(&host_resolver);
5071  TransportSecurityState transport_security_state;
5072  TransportSecurityState::DomainState domain_state;
5073  EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true,
5074                                                      &domain_state));
5075  context.set_transport_security_state(&transport_security_state);
5076  context.Init();
5077
5078  TestDelegate d;
5079  URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
5080                                       test_server.host_port_pair().port())),
5081               &d,
5082               &context);
5083
5084  r.Start();
5085  EXPECT_TRUE(r.is_pending());
5086
5087  base::MessageLoop::current()->Run();
5088
5089  EXPECT_EQ(1, d.response_started_count());
5090  EXPECT_FALSE(d.received_data_before_response());
5091  EXPECT_TRUE(d.have_certificate_errors());
5092  EXPECT_TRUE(d.certificate_errors_are_fatal());
5093
5094  // Get a fresh copy of the state, and check that it hasn't been updated.
5095  TransportSecurityState::DomainState new_domain_state;
5096  EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true,
5097                                                      &new_domain_state));
5098  EXPECT_EQ(new_domain_state.upgrade_mode, domain_state.upgrade_mode);
5099  EXPECT_EQ(new_domain_state.sts_include_subdomains,
5100            domain_state.sts_include_subdomains);
5101  EXPECT_EQ(new_domain_state.pkp_include_subdomains,
5102            domain_state.pkp_include_subdomains);
5103  EXPECT_TRUE(FingerprintsEqual(new_domain_state.static_spki_hashes,
5104                                domain_state.static_spki_hashes));
5105  EXPECT_TRUE(FingerprintsEqual(new_domain_state.dynamic_spki_hashes,
5106                                domain_state.dynamic_spki_hashes));
5107  EXPECT_TRUE(FingerprintsEqual(new_domain_state.bad_static_spki_hashes,
5108                                domain_state.bad_static_spki_hashes));
5109}
5110
5111// Make sure HSTS preserves a POST request's method and body.
5112TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
5113  static const char kData[] = "hello world";
5114
5115  SpawnedTestServer::SSLOptions ssl_options(
5116      SpawnedTestServer::SSLOptions::CERT_OK);
5117  SpawnedTestServer test_server(
5118      SpawnedTestServer::TYPE_HTTPS,
5119      ssl_options,
5120      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
5121  ASSERT_TRUE(test_server.Start());
5122
5123
5124  // Per spec, TransportSecurityState expects a domain name, rather than an IP
5125  // address, so a MockHostResolver is needed to redirect www.somewhere.com to
5126  // the SpawnedTestServer.  By default, MockHostResolver maps all hosts
5127  // to 127.0.0.1.
5128  MockHostResolver host_resolver;
5129
5130  // Force https for www.somewhere.com.
5131  TransportSecurityState transport_security_state;
5132  base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000);
5133  bool include_subdomains = false;
5134  transport_security_state.AddHSTS("www.somewhere.com", expiry,
5135                                   include_subdomains);
5136
5137  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
5138
5139  TestURLRequestContext context(true);
5140  context.set_host_resolver(&host_resolver);
5141  context.set_transport_security_state(&transport_security_state);
5142  context.set_network_delegate(&network_delegate);
5143  context.Init();
5144
5145  TestDelegate d;
5146  // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
5147  // cause a certificate error.  Ignore the error.
5148  d.set_allow_certificate_errors(true);
5149
5150  URLRequest req(GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
5151                                         test_server.host_port_pair().port())),
5152                 &d,
5153                 &context);
5154  req.set_method("POST");
5155  req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
5156
5157  req.Start();
5158  base::MessageLoop::current()->Run();
5159
5160  EXPECT_EQ("https", req.url().scheme());
5161  EXPECT_EQ("POST", req.method());
5162  EXPECT_EQ(kData, d.data_received());
5163
5164  LoadTimingInfo load_timing_info;
5165  network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info);
5166  // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
5167  TestLoadTimingCacheHitNoNetwork(load_timing_info);
5168}
5169
5170TEST_F(HTTPSRequestTest, SSLv3Fallback) {
5171  SpawnedTestServer::SSLOptions ssl_options(
5172      SpawnedTestServer::SSLOptions::CERT_OK);
5173  ssl_options.tls_intolerant =
5174      SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
5175  SpawnedTestServer test_server(
5176      SpawnedTestServer::TYPE_HTTPS,
5177      ssl_options,
5178      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
5179  ASSERT_TRUE(test_server.Start());
5180
5181  TestDelegate d;
5182  TestURLRequestContext context(true);
5183  context.Init();
5184  d.set_allow_certificate_errors(true);
5185  URLRequest r(test_server.GetURL(std::string()), &d, &context);
5186  r.Start();
5187
5188  base::MessageLoop::current()->Run();
5189
5190  EXPECT_EQ(1, d.response_started_count());
5191  EXPECT_NE(0, d.bytes_received());
5192  EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_SSL3),
5193            SSLConnectionStatusToVersion(r.ssl_info().connection_status));
5194  EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK);
5195}
5196
5197namespace {
5198
5199class SSLClientAuthTestDelegate : public TestDelegate {
5200 public:
5201  SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
5202  }
5203  virtual void OnCertificateRequested(
5204      URLRequest* request,
5205      SSLCertRequestInfo* cert_request_info) OVERRIDE {
5206    on_certificate_requested_count_++;
5207    base::MessageLoop::current()->Quit();
5208  }
5209  int on_certificate_requested_count() {
5210    return on_certificate_requested_count_;
5211  }
5212 private:
5213  int on_certificate_requested_count_;
5214};
5215
5216}  // namespace
5217
5218// TODO(davidben): Test the rest of the code. Specifically,
5219// - Filtering which certificates to select.
5220// - Sending a certificate back.
5221// - Getting a certificate request in an SSL renegotiation sending the
5222//   HTTP request.
5223TEST_F(HTTPSRequestTest, ClientAuthTest) {
5224  SpawnedTestServer::SSLOptions ssl_options;
5225  ssl_options.request_client_certificate = true;
5226  SpawnedTestServer test_server(
5227      SpawnedTestServer::TYPE_HTTPS,
5228      ssl_options,
5229      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
5230  ASSERT_TRUE(test_server.Start());
5231
5232  SSLClientAuthTestDelegate d;
5233  {
5234    URLRequest r(test_server.GetURL(std::string()), &d, &default_context_);
5235
5236    r.Start();
5237    EXPECT_TRUE(r.is_pending());
5238
5239    base::MessageLoop::current()->Run();
5240
5241    EXPECT_EQ(1, d.on_certificate_requested_count());
5242    EXPECT_FALSE(d.received_data_before_response());
5243    EXPECT_EQ(0, d.bytes_received());
5244
5245    // Send no certificate.
5246    // TODO(davidben): Get temporary client cert import (with keys) working on
5247    // all platforms so we can test sending a cert as well.
5248    r.ContinueWithCertificate(NULL);
5249
5250    base::MessageLoop::current()->Run();
5251
5252    EXPECT_EQ(1, d.response_started_count());
5253    EXPECT_FALSE(d.received_data_before_response());
5254    EXPECT_NE(0, d.bytes_received());
5255  }
5256}
5257
5258TEST_F(HTTPSRequestTest, ResumeTest) {
5259  // Test that we attempt a session resume when making two connections to the
5260  // same host.
5261  SpawnedTestServer::SSLOptions ssl_options;
5262  ssl_options.record_resume = true;
5263  SpawnedTestServer test_server(
5264      SpawnedTestServer::TYPE_HTTPS,
5265      ssl_options,
5266      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
5267  ASSERT_TRUE(test_server.Start());
5268
5269  SSLClientSocket::ClearSessionCache();
5270
5271  {
5272    TestDelegate d;
5273    URLRequest r(
5274        test_server.GetURL("ssl-session-cache"), &d, &default_context_);
5275
5276    r.Start();
5277    EXPECT_TRUE(r.is_pending());
5278
5279    base::MessageLoop::current()->Run();
5280
5281    EXPECT_EQ(1, d.response_started_count());
5282  }
5283
5284  reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
5285    CloseAllConnections();
5286
5287  {
5288    TestDelegate d;
5289    URLRequest r(
5290        test_server.GetURL("ssl-session-cache"), &d, &default_context_);
5291
5292    r.Start();
5293    EXPECT_TRUE(r.is_pending());
5294
5295    base::MessageLoop::current()->Run();
5296
5297    // The response will look like;
5298    //   insert abc
5299    //   lookup abc
5300    //   insert xyz
5301    //
5302    // With a newline at the end which makes the split think that there are
5303    // four lines.
5304
5305    EXPECT_EQ(1, d.response_started_count());
5306    std::vector<std::string> lines;
5307    base::SplitString(d.data_received(), '\n', &lines);
5308    ASSERT_EQ(4u, lines.size()) << d.data_received();
5309
5310    std::string session_id;
5311
5312    for (size_t i = 0; i < 2; i++) {
5313      std::vector<std::string> parts;
5314      base::SplitString(lines[i], '\t', &parts);
5315      ASSERT_EQ(2u, parts.size());
5316      if (i == 0) {
5317        EXPECT_EQ("insert", parts[0]);
5318        session_id = parts[1];
5319      } else {
5320        EXPECT_EQ("lookup", parts[0]);
5321        EXPECT_EQ(session_id, parts[1]);
5322      }
5323    }
5324  }
5325}
5326
5327TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
5328  // Test that sessions aren't resumed when the value of ssl_session_cache_shard
5329  // differs.
5330  SpawnedTestServer::SSLOptions ssl_options;
5331  ssl_options.record_resume = true;
5332  SpawnedTestServer test_server(
5333      SpawnedTestServer::TYPE_HTTPS,
5334      ssl_options,
5335      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
5336  ASSERT_TRUE(test_server.Start());
5337
5338  SSLClientSocket::ClearSessionCache();
5339
5340  {
5341    TestDelegate d;
5342    URLRequest r(
5343        test_server.GetURL("ssl-session-cache"), &d, &default_context_);
5344
5345    r.Start();
5346    EXPECT_TRUE(r.is_pending());
5347
5348    base::MessageLoop::current()->Run();
5349
5350    EXPECT_EQ(1, d.response_started_count());
5351  }
5352
5353  // Now create a new HttpCache with a different ssl_session_cache_shard value.
5354  HttpNetworkSession::Params params;
5355  params.host_resolver = default_context_.host_resolver();
5356  params.cert_verifier = default_context_.cert_verifier();
5357  params.transport_security_state = default_context_.transport_security_state();
5358  params.proxy_service = default_context_.proxy_service();
5359  params.ssl_config_service = default_context_.ssl_config_service();
5360  params.http_auth_handler_factory =
5361      default_context_.http_auth_handler_factory();
5362  params.network_delegate = &default_network_delegate_;
5363  params.http_server_properties = default_context_.http_server_properties();
5364  params.ssl_session_cache_shard = "alternate";
5365
5366  scoped_ptr<net::HttpCache> cache(new net::HttpCache(
5367      new net::HttpNetworkSession(params),
5368      net::HttpCache::DefaultBackend::InMemory(0)));
5369
5370  default_context_.set_http_transaction_factory(cache.get());
5371
5372  {
5373    TestDelegate d;
5374    URLRequest r(
5375        test_server.GetURL("ssl-session-cache"), &d, &default_context_);
5376
5377    r.Start();
5378    EXPECT_TRUE(r.is_pending());
5379
5380    base::MessageLoop::current()->Run();
5381
5382    // The response will look like;
5383    //   insert abc
5384    //   insert xyz
5385    //
5386    // With a newline at the end which makes the split think that there are
5387    // three lines.
5388
5389    EXPECT_EQ(1, d.response_started_count());
5390    std::vector<std::string> lines;
5391    base::SplitString(d.data_received(), '\n', &lines);
5392    ASSERT_EQ(3u, lines.size());
5393
5394    std::string session_id;
5395    for (size_t i = 0; i < 2; i++) {
5396      std::vector<std::string> parts;
5397      base::SplitString(lines[i], '\t', &parts);
5398      ASSERT_EQ(2u, parts.size());
5399      EXPECT_EQ("insert", parts[0]);
5400      if (i == 0) {
5401        session_id = parts[1];
5402      } else {
5403        EXPECT_NE(session_id, parts[1]);
5404      }
5405    }
5406  }
5407}
5408
5409class TestSSLConfigService : public SSLConfigService {
5410 public:
5411  TestSSLConfigService(bool ev_enabled, bool online_rev_checking)
5412      : ev_enabled_(ev_enabled),
5413        online_rev_checking_(online_rev_checking) {
5414  }
5415
5416  // SSLConfigService:
5417  virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
5418    *config = SSLConfig();
5419    config->rev_checking_enabled = online_rev_checking_;
5420    config->verify_ev_cert = ev_enabled_;
5421  }
5422
5423 protected:
5424  virtual ~TestSSLConfigService() {}
5425
5426 private:
5427  const bool ev_enabled_;
5428  const bool online_rev_checking_;
5429};
5430
5431// This the fingerprint of the "Testing CA" certificate used by the testserver.
5432// See net/data/ssl/certificates/ocsp-test-root.pem.
5433static const SHA1HashValue kOCSPTestCertFingerprint =
5434  { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
5435      0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
5436
5437// This is the policy OID contained in the certificates that testserver
5438// generates.
5439static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
5440
5441class HTTPSOCSPTest : public HTTPSRequestTest {
5442 public:
5443  HTTPSOCSPTest()
5444      : context_(true),
5445        ev_test_policy_(
5446            new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
5447                                   kOCSPTestCertFingerprint,
5448                                   kOCSPTestCertPolicy)) {
5449  }
5450
5451  virtual void SetUp() OVERRIDE {
5452    SetupContext(&context_);
5453    context_.Init();
5454
5455    scoped_refptr<net::X509Certificate> root_cert =
5456      ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
5457    CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert);
5458    test_root_.reset(new ScopedTestRoot(root_cert.get()));
5459
5460#if defined(USE_NSS) || defined(OS_IOS)
5461    SetURLRequestContextForNSSHttpIO(&context_);
5462    EnsureNSSHttpIOInit();
5463#endif
5464  }
5465
5466  void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options,
5467                    CertStatus* out_cert_status) {
5468    // We always overwrite out_cert_status.
5469    *out_cert_status = 0;
5470    SpawnedTestServer test_server(
5471        SpawnedTestServer::TYPE_HTTPS,
5472        ssl_options,
5473        base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
5474    ASSERT_TRUE(test_server.Start());
5475
5476    TestDelegate d;
5477    d.set_allow_certificate_errors(true);
5478    URLRequest r(test_server.GetURL(std::string()), &d, &context_);
5479    r.Start();
5480
5481    base::MessageLoop::current()->Run();
5482
5483    EXPECT_EQ(1, d.response_started_count());
5484    *out_cert_status = r.ssl_info().cert_status;
5485  }
5486
5487  virtual ~HTTPSOCSPTest() {
5488#if defined(USE_NSS) || defined(OS_IOS)
5489    ShutdownNSSHttpIO();
5490#endif
5491  }
5492
5493 protected:
5494  // SetupContext configures the URLRequestContext that will be used for making
5495  // connetions to testserver. This can be overridden in test subclasses for
5496  // different behaviour.
5497  virtual void SetupContext(URLRequestContext* context) {
5498    context->set_ssl_config_service(
5499        new TestSSLConfigService(true /* check for EV */,
5500                                 true /* online revocation checking */));
5501  }
5502
5503  scoped_ptr<ScopedTestRoot> test_root_;
5504  TestURLRequestContext context_;
5505  scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
5506};
5507
5508static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
5509#if defined(OS_WIN)
5510  // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
5511  // have that ability on other platforms.
5512  return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
5513#else
5514  return 0;
5515#endif
5516}
5517
5518// SystemUsesChromiumEVMetadata returns true iff the current operating system
5519// uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
5520// several tests are effected because our testing EV certificate won't be
5521// recognised as EV.
5522static bool SystemUsesChromiumEVMetadata() {
5523#if defined(USE_OPENSSL)
5524  // http://crbug.com/117478 - OpenSSL does not support EV validation.
5525  return false;
5526#elif defined(OS_MACOSX) && !defined(OS_IOS)
5527  // On OS X, we use the system to tell us whether a certificate is EV or not
5528  // and the system won't recognise our testing root.
5529  return false;
5530#else
5531  return true;
5532#endif
5533}
5534
5535static bool SystemSupportsOCSP() {
5536#if defined(USE_OPENSSL)
5537  // http://crbug.com/117478 - OpenSSL does not support OCSP.
5538  return false;
5539#elif defined(OS_WIN)
5540  return base::win::GetVersion() >= base::win::VERSION_VISTA;
5541#elif defined(OS_ANDROID)
5542  // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
5543  return false;
5544#else
5545  return true;
5546#endif
5547}
5548
5549TEST_F(HTTPSOCSPTest, Valid) {
5550  if (!SystemSupportsOCSP()) {
5551    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5552    return;
5553  }
5554
5555  SpawnedTestServer::SSLOptions ssl_options(
5556      SpawnedTestServer::SSLOptions::CERT_AUTO);
5557  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
5558
5559  CertStatus cert_status;
5560  DoConnection(ssl_options, &cert_status);
5561
5562  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5563
5564  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5565            static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
5566
5567  EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5568}
5569
5570TEST_F(HTTPSOCSPTest, Revoked) {
5571  if (!SystemSupportsOCSP()) {
5572    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5573    return;
5574  }
5575
5576  SpawnedTestServer::SSLOptions ssl_options(
5577      SpawnedTestServer::SSLOptions::CERT_AUTO);
5578  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
5579
5580  CertStatus cert_status;
5581  DoConnection(ssl_options, &cert_status);
5582
5583#if !(defined(OS_MACOSX) && !defined(OS_IOS))
5584  // Doesn't pass on OS X yet for reasons that need to be investigated.
5585  EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
5586#endif
5587  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5588  EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5589}
5590
5591TEST_F(HTTPSOCSPTest, Invalid) {
5592  if (!SystemSupportsOCSP()) {
5593    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5594    return;
5595  }
5596
5597  SpawnedTestServer::SSLOptions ssl_options(
5598      SpawnedTestServer::SSLOptions::CERT_AUTO);
5599  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
5600
5601  CertStatus cert_status;
5602  DoConnection(ssl_options, &cert_status);
5603
5604  EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
5605            cert_status & CERT_STATUS_ALL_ERRORS);
5606
5607  // Without a positive OCSP response, we shouldn't show the EV status.
5608  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5609  EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5610}
5611
5612class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
5613 protected:
5614  virtual void SetupContext(URLRequestContext* context) OVERRIDE {
5615    context->set_ssl_config_service(
5616        new TestSSLConfigService(true /* check for EV */,
5617                                 false /* online revocation checking */));
5618  }
5619};
5620
5621TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
5622  if (!SystemSupportsOCSP()) {
5623    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5624    return;
5625  }
5626
5627  SpawnedTestServer::SSLOptions ssl_options(
5628      SpawnedTestServer::SSLOptions::CERT_AUTO);
5629  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
5630  SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
5631
5632  CertStatus cert_status;
5633  DoConnection(ssl_options, &cert_status);
5634
5635  EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
5636            cert_status & CERT_STATUS_ALL_ERRORS);
5637
5638  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5639  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5640            static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
5641}
5642
5643TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
5644  if (!SystemSupportsOCSP()) {
5645    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5646    return;
5647  }
5648
5649  SpawnedTestServer::SSLOptions ssl_options(
5650      SpawnedTestServer::SSLOptions::CERT_AUTO);
5651  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
5652  SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
5653
5654  CertStatus cert_status;
5655  DoConnection(ssl_options, &cert_status);
5656
5657  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5658
5659  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5660            static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
5661  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5662            static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
5663}
5664
5665TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
5666  if (!SystemSupportsOCSP()) {
5667    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5668    return;
5669  }
5670
5671  SpawnedTestServer::SSLOptions ssl_options(
5672      SpawnedTestServer::SSLOptions::CERT_AUTO);
5673  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
5674  SSLConfigService::SetCRLSet(
5675      scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
5676
5677  CertStatus cert_status;
5678  DoConnection(ssl_options, &cert_status);
5679
5680  EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
5681            cert_status & CERT_STATUS_ALL_ERRORS);
5682
5683  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5684  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5685            static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
5686}
5687
5688TEST_F(HTTPSEVCRLSetTest, FreshCRLSet) {
5689  SpawnedTestServer::SSLOptions ssl_options(
5690      SpawnedTestServer::SSLOptions::CERT_AUTO);
5691  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
5692  SSLConfigService::SetCRLSet(
5693      scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
5694
5695  CertStatus cert_status;
5696  DoConnection(ssl_options, &cert_status);
5697
5698  // With a valid, fresh CRLSet the bad OCSP response shouldn't matter because
5699  // we wont check it.
5700  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5701
5702  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5703            static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
5704
5705  EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5706}
5707
5708TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
5709  // Test that when EV verification is requested, but online revocation
5710  // checking is disabled, and the leaf certificate is not in fact EV, that
5711  // no revocation checking actually happens.
5712  if (!SystemSupportsOCSP()) {
5713    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5714    return;
5715  }
5716
5717  // Unmark the certificate's OID as EV, which should disable revocation
5718  // checking (as per the user preference)
5719  ev_test_policy_.reset();
5720
5721  SpawnedTestServer::SSLOptions ssl_options(
5722      SpawnedTestServer::SSLOptions::CERT_AUTO);
5723  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
5724  SSLConfigService::SetCRLSet(
5725      scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
5726
5727  CertStatus cert_status;
5728  DoConnection(ssl_options, &cert_status);
5729
5730  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5731
5732  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5733  EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5734}
5735
5736class HTTPSCRLSetTest : public HTTPSOCSPTest {
5737 protected:
5738  virtual void SetupContext(URLRequestContext* context) OVERRIDE {
5739    context->set_ssl_config_service(
5740        new TestSSLConfigService(false /* check for EV */,
5741                                 false /* online revocation checking */));
5742  }
5743};
5744
5745TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
5746  SpawnedTestServer::SSLOptions ssl_options(
5747      SpawnedTestServer::SSLOptions::CERT_AUTO);
5748  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
5749  SSLConfigService::SetCRLSet(
5750      scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
5751
5752  CertStatus cert_status;
5753  DoConnection(ssl_options, &cert_status);
5754
5755  // If we're not trying EV verification then, even if the CRLSet has expired,
5756  // we don't fall back to online revocation checks.
5757  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5758  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5759  EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5760}
5761#endif  // !defined(OS_IOS)
5762
5763#if !defined(DISABLE_FTP_SUPPORT)
5764class URLRequestTestFTP : public URLRequestTest {
5765 public:
5766  URLRequestTestFTP()
5767      : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost,
5768                     base::FilePath()) {
5769  }
5770
5771 protected:
5772  SpawnedTestServer test_server_;
5773};
5774
5775// Make sure an FTP request using an unsafe ports fails.
5776TEST_F(URLRequestTestFTP, UnsafePort) {
5777  ASSERT_TRUE(test_server_.Start());
5778
5779  URLRequestJobFactoryImpl job_factory;
5780  FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver());
5781
5782  GURL url("ftp://127.0.0.1:7");
5783  job_factory.SetProtocolHandler(
5784      "ftp",
5785      new FtpProtocolHandler(&ftp_transaction_factory));
5786  default_context_.set_job_factory(&job_factory);
5787
5788  TestDelegate d;
5789  {
5790    URLRequest r(url, &d, &default_context_);
5791    r.Start();
5792    EXPECT_TRUE(r.is_pending());
5793
5794    base::MessageLoop::current()->Run();
5795
5796    EXPECT_FALSE(r.is_pending());
5797    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
5798    EXPECT_EQ(ERR_UNSAFE_PORT, r.status().error());
5799  }
5800}
5801
5802// Flaky, see http://crbug.com/25045.
5803TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
5804  ASSERT_TRUE(test_server_.Start());
5805
5806  TestDelegate d;
5807  {
5808    URLRequest r(test_server_.GetURL("/"), &d, &default_context_);
5809    r.Start();
5810    EXPECT_TRUE(r.is_pending());
5811
5812    base::MessageLoop::current()->Run();
5813
5814    EXPECT_FALSE(r.is_pending());
5815    EXPECT_EQ(1, d.response_started_count());
5816    EXPECT_FALSE(d.received_data_before_response());
5817    EXPECT_LT(0, d.bytes_received());
5818    EXPECT_EQ(test_server_.host_port_pair().host(),
5819              r.GetSocketAddress().host());
5820    EXPECT_EQ(test_server_.host_port_pair().port(),
5821              r.GetSocketAddress().port());
5822  }
5823}
5824
5825// Flaky, see http://crbug.com/25045.
5826TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
5827  ASSERT_TRUE(test_server_.Start());
5828
5829  base::FilePath app_path;
5830  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5831  app_path = app_path.AppendASCII("LICENSE");
5832  TestDelegate d;
5833  {
5834    URLRequest r(test_server_.GetURL("/LICENSE"), &d, &default_context_);
5835    r.Start();
5836    EXPECT_TRUE(r.is_pending());
5837
5838    base::MessageLoop::current()->Run();
5839
5840    int64 file_size = 0;
5841    file_util::GetFileSize(app_path, &file_size);
5842
5843    EXPECT_FALSE(r.is_pending());
5844    EXPECT_EQ(1, d.response_started_count());
5845    EXPECT_FALSE(d.received_data_before_response());
5846    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
5847    EXPECT_EQ(test_server_.host_port_pair().host(),
5848              r.GetSocketAddress().host());
5849    EXPECT_EQ(test_server_.host_port_pair().port(),
5850              r.GetSocketAddress().port());
5851  }
5852}
5853
5854// Flaky, see http://crbug.com/25045.
5855TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
5856  ASSERT_TRUE(test_server_.Start());
5857
5858  base::FilePath app_path;
5859  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5860  app_path = app_path.AppendASCII("LICENSE");
5861  TestDelegate d;
5862  {
5863    URLRequest r(
5864        test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
5865        &d,
5866        &default_context_);
5867    r.Start();
5868    EXPECT_TRUE(r.is_pending());
5869
5870    base::MessageLoop::current()->Run();
5871
5872    int64 file_size = 0;
5873    file_util::GetFileSize(app_path, &file_size);
5874
5875    EXPECT_FALSE(r.is_pending());
5876    EXPECT_EQ(test_server_.host_port_pair().host(),
5877              r.GetSocketAddress().host());
5878    EXPECT_EQ(test_server_.host_port_pair().port(),
5879              r.GetSocketAddress().port());
5880    EXPECT_EQ(1, d.response_started_count());
5881    EXPECT_FALSE(d.received_data_before_response());
5882    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
5883
5884    LoadTimingInfo load_timing_info;
5885    r.GetLoadTimingInfo(&load_timing_info);
5886    TestLoadTimingNoHttpResponse(load_timing_info);
5887  }
5888}
5889
5890// Flaky, see http://crbug.com/25045.
5891TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
5892  ASSERT_TRUE(test_server_.Start());
5893
5894  base::FilePath app_path;
5895  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5896  app_path = app_path.AppendASCII("LICENSE");
5897  TestDelegate d;
5898  {
5899    URLRequest r(
5900        test_server_.GetURLWithUserAndPassword("/LICENSE",
5901                                               "chrome",
5902                                               "wrong_password"),
5903        &d,
5904        &default_context_);
5905    r.Start();
5906    EXPECT_TRUE(r.is_pending());
5907
5908    base::MessageLoop::current()->Run();
5909
5910    int64 file_size = 0;
5911    file_util::GetFileSize(app_path, &file_size);
5912
5913    EXPECT_FALSE(r.is_pending());
5914    EXPECT_EQ(1, d.response_started_count());
5915    EXPECT_FALSE(d.received_data_before_response());
5916    EXPECT_EQ(d.bytes_received(), 0);
5917  }
5918}
5919
5920// Flaky, see http://crbug.com/25045.
5921TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
5922  ASSERT_TRUE(test_server_.Start());
5923
5924  base::FilePath app_path;
5925  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5926  app_path = app_path.AppendASCII("LICENSE");
5927  TestDelegate d;
5928  // Set correct login credentials. The delegate will be asked for them when
5929  // the initial login with wrong credentials will fail.
5930  d.set_credentials(AuthCredentials(kChrome, kChrome));
5931  {
5932    URLRequest r(
5933        test_server_.GetURLWithUserAndPassword("/LICENSE",
5934                                               "chrome",
5935                                               "wrong_password"),
5936        &d,
5937        &default_context_);
5938    r.Start();
5939    EXPECT_TRUE(r.is_pending());
5940
5941    base::MessageLoop::current()->Run();
5942
5943    int64 file_size = 0;
5944    file_util::GetFileSize(app_path, &file_size);
5945
5946    EXPECT_FALSE(r.is_pending());
5947    EXPECT_EQ(1, d.response_started_count());
5948    EXPECT_FALSE(d.received_data_before_response());
5949    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
5950  }
5951}
5952
5953// Flaky, see http://crbug.com/25045.
5954TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
5955  ASSERT_TRUE(test_server_.Start());
5956
5957  base::FilePath app_path;
5958  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5959  app_path = app_path.AppendASCII("LICENSE");
5960  TestDelegate d;
5961  {
5962    URLRequest r(
5963        test_server_.GetURLWithUserAndPassword("/LICENSE",
5964                                               "wrong_user",
5965                                               "chrome"),
5966        &d,
5967        &default_context_);
5968    r.Start();
5969    EXPECT_TRUE(r.is_pending());
5970
5971    base::MessageLoop::current()->Run();
5972
5973    int64 file_size = 0;
5974    file_util::GetFileSize(app_path, &file_size);
5975
5976    EXPECT_FALSE(r.is_pending());
5977    EXPECT_EQ(1, d.response_started_count());
5978    EXPECT_FALSE(d.received_data_before_response());
5979    EXPECT_EQ(d.bytes_received(), 0);
5980  }
5981}
5982
5983// Flaky, see http://crbug.com/25045.
5984TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
5985  ASSERT_TRUE(test_server_.Start());
5986
5987  base::FilePath app_path;
5988  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5989  app_path = app_path.AppendASCII("LICENSE");
5990  TestDelegate d;
5991  // Set correct login credentials. The delegate will be asked for them when
5992  // the initial login with wrong credentials will fail.
5993  d.set_credentials(AuthCredentials(kChrome, kChrome));
5994  {
5995    URLRequest r(
5996        test_server_.GetURLWithUserAndPassword("/LICENSE",
5997                                               "wrong_user",
5998                                               "chrome"),
5999        &d,
6000        &default_context_);
6001    r.Start();
6002    EXPECT_TRUE(r.is_pending());
6003
6004    base::MessageLoop::current()->Run();
6005
6006    int64 file_size = 0;
6007    file_util::GetFileSize(app_path, &file_size);
6008
6009    EXPECT_FALSE(r.is_pending());
6010    EXPECT_EQ(1, d.response_started_count());
6011    EXPECT_FALSE(d.received_data_before_response());
6012    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
6013  }
6014}
6015
6016// Flaky, see http://crbug.com/25045.
6017TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
6018  ASSERT_TRUE(test_server_.Start());
6019
6020  base::FilePath app_path;
6021  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
6022  app_path = app_path.AppendASCII("LICENSE");
6023
6024  scoped_ptr<TestDelegate> d(new TestDelegate);
6025  {
6026    // Pass correct login identity in the URL.
6027    URLRequest r(
6028        test_server_.GetURLWithUserAndPassword("/LICENSE",
6029                                               "chrome",
6030                                               "chrome"),
6031        d.get(),
6032        &default_context_);
6033    r.Start();
6034    EXPECT_TRUE(r.is_pending());
6035
6036    base::MessageLoop::current()->Run();
6037
6038    int64 file_size = 0;
6039    file_util::GetFileSize(app_path, &file_size);
6040
6041    EXPECT_FALSE(r.is_pending());
6042    EXPECT_EQ(1, d->response_started_count());
6043    EXPECT_FALSE(d->received_data_before_response());
6044    EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
6045  }
6046
6047  d.reset(new TestDelegate);
6048  {
6049    // This request should use cached identity from previous request.
6050    URLRequest r(test_server_.GetURL("/LICENSE"), d.get(), &default_context_);
6051    r.Start();
6052    EXPECT_TRUE(r.is_pending());
6053
6054    base::MessageLoop::current()->Run();
6055
6056    int64 file_size = 0;
6057    file_util::GetFileSize(app_path, &file_size);
6058
6059    EXPECT_FALSE(r.is_pending());
6060    EXPECT_EQ(1, d->response_started_count());
6061    EXPECT_FALSE(d->received_data_before_response());
6062    EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
6063  }
6064}
6065
6066// Flaky, see http://crbug.com/25045.
6067TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
6068  ASSERT_TRUE(test_server_.Start());
6069
6070  base::FilePath app_path;
6071  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
6072  app_path = app_path.AppendASCII("LICENSE");
6073
6074  scoped_ptr<TestDelegate> d(new TestDelegate);
6075  // Set correct login credentials. The delegate will be asked for them when
6076  // the initial login with wrong credentials will fail.
6077  d->set_credentials(AuthCredentials(kChrome, kChrome));
6078  {
6079    URLRequest r(
6080        test_server_.GetURLWithUserAndPassword("/LICENSE",
6081                                               "chrome",
6082                                               "wrong_password"),
6083        d.get(),
6084        &default_context_);
6085    r.Start();
6086    EXPECT_TRUE(r.is_pending());
6087
6088    base::MessageLoop::current()->Run();
6089
6090    int64 file_size = 0;
6091    file_util::GetFileSize(app_path, &file_size);
6092
6093    EXPECT_FALSE(r.is_pending());
6094    EXPECT_EQ(1, d->response_started_count());
6095    EXPECT_FALSE(d->received_data_before_response());
6096    EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
6097  }
6098
6099  // Use a new delegate without explicit credentials. The cached ones should be
6100  // used.
6101  d.reset(new TestDelegate);
6102  {
6103    // Don't pass wrong credentials in the URL, they would override valid cached
6104    // ones.
6105    URLRequest r(test_server_.GetURL("/LICENSE"), d.get(), &default_context_);
6106    r.Start();
6107    EXPECT_TRUE(r.is_pending());
6108
6109    base::MessageLoop::current()->Run();
6110
6111    int64 file_size = 0;
6112    file_util::GetFileSize(app_path, &file_size);
6113
6114    EXPECT_FALSE(r.is_pending());
6115    EXPECT_EQ(1, d->response_started_count());
6116    EXPECT_FALSE(d->received_data_before_response());
6117    EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
6118  }
6119}
6120#endif  // !defined(DISABLE_FTP_SUPPORT)
6121
6122}  // namespace net
6123