url_request_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/files/scoped_temp_dir.h"
20#include "base/format_macros.h"
21#include "base/memory/weak_ptr.h"
22#include "base/message_loop/message_loop.h"
23#include "base/message_loop/message_loop_proxy.h"
24#include "base/path_service.h"
25#include "base/run_loop.h"
26#include "base/strings/string_number_conversions.h"
27#include "base/strings/string_piece.h"
28#include "base/strings/string_split.h"
29#include "base/strings/string_util.h"
30#include "base/strings/stringprintf.h"
31#include "base/strings/utf_string_conversions.h"
32#include "net/base/capturing_net_log.h"
33#include "net/base/load_flags.h"
34#include "net/base/load_timing_info.h"
35#include "net/base/load_timing_info_test_util.h"
36#include "net/base/net_errors.h"
37#include "net/base/net_log.h"
38#include "net/base/net_log_unittest.h"
39#include "net/base/net_module.h"
40#include "net/base/net_util.h"
41#include "net/base/request_priority.h"
42#include "net/base/test_data_directory.h"
43#include "net/base/upload_bytes_element_reader.h"
44#include "net/base/upload_data_stream.h"
45#include "net/base/upload_file_element_reader.h"
46#include "net/cert/ev_root_ca_metadata.h"
47#include "net/cert/mock_cert_verifier.h"
48#include "net/cert/test_root_certs.h"
49#include "net/cookies/cookie_monster.h"
50#include "net/cookies/cookie_store_test_helpers.h"
51#include "net/disk_cache/disk_cache.h"
52#include "net/dns/mock_host_resolver.h"
53#include "net/ftp/ftp_network_layer.h"
54#include "net/http/http_byte_range.h"
55#include "net/http/http_cache.h"
56#include "net/http/http_network_layer.h"
57#include "net/http/http_network_session.h"
58#include "net/http/http_request_headers.h"
59#include "net/http/http_response_headers.h"
60#include "net/http/http_util.h"
61#include "net/ocsp/nss_ocsp.h"
62#include "net/proxy/proxy_service.h"
63#include "net/socket/ssl_client_socket.h"
64#include "net/ssl/ssl_connection_status_flags.h"
65#include "net/test/cert_test_util.h"
66#include "net/test/spawned_test_server/spawned_test_server.h"
67#include "net/url_request/data_protocol_handler.h"
68#include "net/url_request/static_http_user_agent_settings.h"
69#include "net/url_request/url_request.h"
70#include "net/url_request/url_request_http_job.h"
71#include "net/url_request/url_request_job_factory_impl.h"
72#include "net/url_request/url_request_redirect_job.h"
73#include "net/url_request/url_request_test_job.h"
74#include "net/url_request/url_request_test_util.h"
75#include "testing/gtest/include/gtest/gtest.h"
76#include "testing/platform_test.h"
77
78#if !defined(DISABLE_FILE_SUPPORT)
79#include "net/base/filename_util.h"
80#include "net/url_request/file_protocol_handler.h"
81#include "net/url_request/url_request_file_dir_job.h"
82#endif
83
84#if !defined(DISABLE_FTP_SUPPORT)
85#include "net/url_request/ftp_protocol_handler.h"
86#endif
87
88#if defined(OS_WIN)
89#include "base/win/scoped_com_initializer.h"
90#include "base/win/scoped_comptr.h"
91#include "base/win/windows_version.h"
92#endif
93
94using base::ASCIIToUTF16;
95using base::Time;
96
97namespace net {
98
99namespace {
100
101const base::string16 kChrome(ASCIIToUTF16("chrome"));
102const base::string16 kSecret(ASCIIToUTF16("secret"));
103const base::string16 kUser(ASCIIToUTF16("user"));
104
105// Tests load timing information in the case a fresh connection was used, with
106// no proxy.
107void TestLoadTimingNotReused(const net::LoadTimingInfo& load_timing_info,
108                             int connect_timing_flags) {
109  EXPECT_FALSE(load_timing_info.socket_reused);
110  EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
111
112  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
113  EXPECT_FALSE(load_timing_info.request_start.is_null());
114
115  EXPECT_LE(load_timing_info.request_start,
116            load_timing_info.connect_timing.connect_start);
117  ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
118                              connect_timing_flags);
119  EXPECT_LE(load_timing_info.connect_timing.connect_end,
120            load_timing_info.send_start);
121  EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
122  EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
123
124  EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
125  EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
126}
127
128// Same as above, but with proxy times.
129void TestLoadTimingNotReusedWithProxy(
130    const net::LoadTimingInfo& load_timing_info,
131    int connect_timing_flags) {
132  EXPECT_FALSE(load_timing_info.socket_reused);
133  EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
134
135  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
136  EXPECT_FALSE(load_timing_info.request_start.is_null());
137
138  EXPECT_LE(load_timing_info.request_start,
139            load_timing_info.proxy_resolve_start);
140  EXPECT_LE(load_timing_info.proxy_resolve_start,
141            load_timing_info.proxy_resolve_end);
142  EXPECT_LE(load_timing_info.proxy_resolve_end,
143            load_timing_info.connect_timing.connect_start);
144  ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
145                              connect_timing_flags);
146  EXPECT_LE(load_timing_info.connect_timing.connect_end,
147            load_timing_info.send_start);
148  EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
149  EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
150}
151
152// Same as above, but with a reused socket and proxy times.
153void TestLoadTimingReusedWithProxy(
154    const net::LoadTimingInfo& load_timing_info) {
155  EXPECT_TRUE(load_timing_info.socket_reused);
156  EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
157
158  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
159  EXPECT_FALSE(load_timing_info.request_start.is_null());
160
161  ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
162
163  EXPECT_LE(load_timing_info.request_start,
164            load_timing_info.proxy_resolve_start);
165  EXPECT_LE(load_timing_info.proxy_resolve_start,
166            load_timing_info.proxy_resolve_end);
167  EXPECT_LE(load_timing_info.proxy_resolve_end,
168            load_timing_info.send_start);
169  EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
170  EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
171}
172
173// Tests load timing information in the case of a cache hit, when no cache
174// validation request was sent over the wire.
175base::StringPiece TestNetResourceProvider(int key) {
176  return "header";
177}
178
179void FillBuffer(char* buffer, size_t len) {
180  static bool called = false;
181  if (!called) {
182    called = true;
183    int seed = static_cast<int>(Time::Now().ToInternalValue());
184    srand(seed);
185  }
186
187  for (size_t i = 0; i < len; i++) {
188    buffer[i] = static_cast<char>(rand());
189    if (!buffer[i])
190      buffer[i] = 'g';
191  }
192}
193
194#if !defined(OS_IOS)
195void TestLoadTimingCacheHitNoNetwork(
196    const net::LoadTimingInfo& load_timing_info) {
197  EXPECT_FALSE(load_timing_info.socket_reused);
198  EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
199
200  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
201  EXPECT_FALSE(load_timing_info.request_start.is_null());
202
203  ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
204  EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start);
205  EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
206  EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
207
208  EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
209  EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
210}
211
212// Tests load timing in the case that there is no HTTP response.  This can be
213// used to test in the case of errors or non-HTTP requests.
214void TestLoadTimingNoHttpResponse(
215    const net::LoadTimingInfo& load_timing_info) {
216  EXPECT_FALSE(load_timing_info.socket_reused);
217  EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
218
219  // Only the request times should be non-null.
220  EXPECT_FALSE(load_timing_info.request_start_time.is_null());
221  EXPECT_FALSE(load_timing_info.request_start.is_null());
222
223  ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
224
225  EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
226  EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
227  EXPECT_TRUE(load_timing_info.send_start.is_null());
228  EXPECT_TRUE(load_timing_info.send_end.is_null());
229  EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
230}
231
232// Do a case-insensitive search through |haystack| for |needle|.
233bool ContainsString(const std::string& haystack, const char* needle) {
234  std::string::const_iterator it =
235      std::search(haystack.begin(),
236                  haystack.end(),
237                  needle,
238                  needle + strlen(needle),
239                  base::CaseInsensitiveCompare<char>());
240  return it != haystack.end();
241}
242
243UploadDataStream* CreateSimpleUploadData(const char* data) {
244  scoped_ptr<UploadElementReader> reader(
245      new UploadBytesElementReader(data, strlen(data)));
246  return UploadDataStream::CreateWithReader(reader.Pass(), 0);
247}
248
249// Verify that the SSLInfo of a successful SSL connection has valid values.
250void CheckSSLInfo(const SSLInfo& ssl_info) {
251  // -1 means unknown.  0 means no encryption.
252  EXPECT_GT(ssl_info.security_bits, 0);
253
254  // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
255  int cipher_suite = SSLConnectionStatusToCipherSuite(
256      ssl_info.connection_status);
257  EXPECT_NE(0, cipher_suite);
258}
259
260void CheckFullRequestHeaders(const HttpRequestHeaders& headers,
261                             const GURL& host_url) {
262  std::string sent_value;
263
264  EXPECT_TRUE(headers.GetHeader("Host", &sent_value));
265  EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value);
266
267  EXPECT_TRUE(headers.GetHeader("Connection", &sent_value));
268  EXPECT_EQ("keep-alive", sent_value);
269}
270
271bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
272  size_t size = a.size();
273
274  if (size != b.size())
275    return false;
276
277  for (size_t i = 0; i < size; ++i) {
278    if (!a[i].Equals(b[i]))
279      return false;
280  }
281
282  return true;
283}
284#endif  // !defined(OS_IOS)
285
286// A network delegate that allows the user to choose a subset of request stages
287// to block in. When blocking, the delegate can do one of the following:
288//  * synchronously return a pre-specified error code, or
289//  * asynchronously return that value via an automatically called callback,
290//    or
291//  * block and wait for the user to do a callback.
292// Additionally, the user may also specify a redirect URL -- then each request
293// with the current URL different from the redirect target will be redirected
294// to that target, in the on-before-URL-request stage, independent of whether
295// the delegate blocks in ON_BEFORE_URL_REQUEST or not.
296class BlockingNetworkDelegate : public TestNetworkDelegate {
297 public:
298  // Stages in which the delegate can block.
299  enum Stage {
300    NOT_BLOCKED = 0,
301    ON_BEFORE_URL_REQUEST = 1 << 0,
302    ON_BEFORE_SEND_HEADERS = 1 << 1,
303    ON_HEADERS_RECEIVED = 1 << 2,
304    ON_AUTH_REQUIRED = 1 << 3
305  };
306
307  // Behavior during blocked stages.  During other stages, just
308  // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
309  enum BlockMode {
310    SYNCHRONOUS,    // No callback, returns specified return values.
311    AUTO_CALLBACK,  // |this| posts a task to run the callback using the
312                    // specified return codes.
313    USER_CALLBACK,  // User takes care of doing a callback.  |retval_| and
314                    // |auth_retval_| are ignored. In every blocking stage the
315                    // message loop is quit.
316  };
317
318  // Creates a delegate which does not block at all.
319  explicit BlockingNetworkDelegate(BlockMode block_mode);
320
321  // For users to trigger a callback returning |response|.
322  // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
323  // Only call if |block_mode_| == USER_CALLBACK.
324  void DoCallback(int response);
325  void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
326
327  // Setters.
328  void set_retval(int retval) {
329    ASSERT_NE(USER_CALLBACK, block_mode_);
330    ASSERT_NE(ERR_IO_PENDING, retval);
331    ASSERT_NE(OK, retval);
332    retval_ = retval;
333  }
334
335  // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
336  // |auth_credentials_| will be passed with the response.
337  void set_auth_retval(AuthRequiredResponse auth_retval) {
338    ASSERT_NE(USER_CALLBACK, block_mode_);
339    ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
340    auth_retval_ = auth_retval;
341  }
342  void set_auth_credentials(const AuthCredentials& auth_credentials) {
343    auth_credentials_ = auth_credentials;
344  }
345
346  void set_redirect_url(const GURL& url) {
347    redirect_url_ = url;
348  }
349
350  void set_block_on(int block_on) {
351    block_on_ = block_on;
352  }
353
354  // Allows the user to check in which state did we block.
355  Stage stage_blocked_for_callback() const {
356    EXPECT_EQ(USER_CALLBACK, block_mode_);
357    return stage_blocked_for_callback_;
358  }
359
360 private:
361  void RunCallback(int response, const CompletionCallback& callback);
362  void RunAuthCallback(AuthRequiredResponse response,
363                       const AuthCallback& callback);
364
365  // TestNetworkDelegate implementation.
366  virtual int OnBeforeURLRequest(URLRequest* request,
367                                 const CompletionCallback& callback,
368                                 GURL* new_url) OVERRIDE;
369
370  virtual int OnBeforeSendHeaders(URLRequest* request,
371                                  const CompletionCallback& callback,
372                                  HttpRequestHeaders* headers) OVERRIDE;
373
374  virtual int OnHeadersReceived(
375      URLRequest* request,
376      const CompletionCallback& callback,
377      const HttpResponseHeaders* original_response_headers,
378      scoped_refptr<HttpResponseHeaders>* override_response_headers,
379      GURL* allowed_unsafe_redirect_url) OVERRIDE;
380
381  virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
382      URLRequest* request,
383      const AuthChallengeInfo& auth_info,
384      const AuthCallback& callback,
385      AuthCredentials* credentials) OVERRIDE;
386
387  // Resets the callbacks and |stage_blocked_for_callback_|.
388  void Reset();
389
390  // Checks whether we should block in |stage|. If yes, returns an error code
391  // and optionally sets up callback based on |block_mode_|. If no, returns OK.
392  int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
393
394  // Configuration parameters, can be adjusted by public methods:
395  const BlockMode block_mode_;
396
397  // Values returned on blocking stages when mode is SYNCHRONOUS or
398  // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
399  int retval_;  // To be returned in non-auth stages.
400  AuthRequiredResponse auth_retval_;
401
402  GURL redirect_url_;  // Used if non-empty during OnBeforeURLRequest.
403  int block_on_;  // Bit mask: in which stages to block.
404
405  // |auth_credentials_| will be copied to |*target_auth_credential_| on
406  // callback.
407  AuthCredentials auth_credentials_;
408  AuthCredentials* target_auth_credentials_;
409
410  // Internal variables, not set by not the user:
411  // Last blocked stage waiting for user callback (unused if |block_mode_| !=
412  // USER_CALLBACK).
413  Stage stage_blocked_for_callback_;
414
415  // Callback objects stored during blocking stages.
416  CompletionCallback callback_;
417  AuthCallback auth_callback_;
418
419  base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
420
421  DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
422};
423
424BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
425    : block_mode_(block_mode),
426      retval_(OK),
427      auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
428      block_on_(0),
429      target_auth_credentials_(NULL),
430      stage_blocked_for_callback_(NOT_BLOCKED),
431      weak_factory_(this) {
432}
433
434void BlockingNetworkDelegate::DoCallback(int response) {
435  ASSERT_EQ(USER_CALLBACK, block_mode_);
436  ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
437  ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
438  CompletionCallback callback = callback_;
439  Reset();
440  RunCallback(response, callback);
441}
442
443void BlockingNetworkDelegate::DoAuthCallback(
444    NetworkDelegate::AuthRequiredResponse response) {
445  ASSERT_EQ(USER_CALLBACK, block_mode_);
446  ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
447  AuthCallback auth_callback = auth_callback_;
448  Reset();
449  RunAuthCallback(response, auth_callback);
450}
451
452void BlockingNetworkDelegate::RunCallback(int response,
453                                          const CompletionCallback& callback) {
454  callback.Run(response);
455}
456
457void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
458                                              const AuthCallback& callback) {
459  if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
460    ASSERT_TRUE(target_auth_credentials_ != NULL);
461    *target_auth_credentials_ = auth_credentials_;
462  }
463  callback.Run(response);
464}
465
466int BlockingNetworkDelegate::OnBeforeURLRequest(
467    URLRequest* request,
468    const CompletionCallback& callback,
469    GURL* new_url) {
470  if (redirect_url_ == request->url())
471    return OK;  // We've already seen this request and redirected elsewhere.
472
473  TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
474
475  if (!redirect_url_.is_empty())
476    *new_url = redirect_url_;
477
478  return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
479}
480
481int BlockingNetworkDelegate::OnBeforeSendHeaders(
482    URLRequest* request,
483    const CompletionCallback& callback,
484    HttpRequestHeaders* headers) {
485  TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
486
487  return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
488}
489
490int BlockingNetworkDelegate::OnHeadersReceived(
491    URLRequest* request,
492    const CompletionCallback& callback,
493    const HttpResponseHeaders* original_response_headers,
494    scoped_refptr<HttpResponseHeaders>* override_response_headers,
495    GURL* allowed_unsafe_redirect_url) {
496  TestNetworkDelegate::OnHeadersReceived(request,
497                                         callback,
498                                         original_response_headers,
499                                         override_response_headers,
500                                         allowed_unsafe_redirect_url);
501
502  return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
503}
504
505NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
506    URLRequest* request,
507    const AuthChallengeInfo& auth_info,
508    const AuthCallback& callback,
509    AuthCredentials* credentials) {
510  TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
511                                      credentials);
512  // Check that the user has provided callback for the previous blocked stage.
513  EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
514
515  if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
516    return AUTH_REQUIRED_RESPONSE_NO_ACTION;
517  }
518
519  target_auth_credentials_ = credentials;
520
521  switch (block_mode_) {
522    case SYNCHRONOUS:
523      if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
524        *target_auth_credentials_ = auth_credentials_;
525      return auth_retval_;
526
527    case AUTO_CALLBACK:
528      base::MessageLoop::current()->PostTask(
529          FROM_HERE,
530          base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
531                     weak_factory_.GetWeakPtr(), auth_retval_, callback));
532      return AUTH_REQUIRED_RESPONSE_IO_PENDING;
533
534    case USER_CALLBACK:
535      auth_callback_ = callback;
536      stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
537      base::MessageLoop::current()->PostTask(FROM_HERE,
538                                             base::MessageLoop::QuitClosure());
539      return AUTH_REQUIRED_RESPONSE_IO_PENDING;
540  }
541  NOTREACHED();
542  return AUTH_REQUIRED_RESPONSE_NO_ACTION;  // Dummy value.
543}
544
545void BlockingNetworkDelegate::Reset() {
546  EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
547  stage_blocked_for_callback_ = NOT_BLOCKED;
548  callback_.Reset();
549  auth_callback_.Reset();
550}
551
552int BlockingNetworkDelegate::MaybeBlockStage(
553    BlockingNetworkDelegate::Stage stage,
554    const CompletionCallback& callback) {
555  // Check that the user has provided callback for the previous blocked stage.
556  EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
557
558  if ((block_on_ & stage) == 0) {
559    return OK;
560  }
561
562  switch (block_mode_) {
563    case SYNCHRONOUS:
564      EXPECT_NE(OK, retval_);
565      return retval_;
566
567    case AUTO_CALLBACK:
568      base::MessageLoop::current()->PostTask(
569          FROM_HERE,
570          base::Bind(&BlockingNetworkDelegate::RunCallback,
571                     weak_factory_.GetWeakPtr(), retval_, callback));
572      return ERR_IO_PENDING;
573
574    case USER_CALLBACK:
575      callback_ = callback;
576      stage_blocked_for_callback_ = stage;
577      base::MessageLoop::current()->PostTask(FROM_HERE,
578                                             base::MessageLoop::QuitClosure());
579      return ERR_IO_PENDING;
580  }
581  NOTREACHED();
582  return 0;
583}
584
585class TestURLRequestContextWithProxy : public TestURLRequestContext {
586 public:
587  // Does not own |delegate|.
588  TestURLRequestContextWithProxy(const std::string& proxy,
589                                 NetworkDelegate* delegate)
590      : TestURLRequestContext(true) {
591    context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
592    set_network_delegate(delegate);
593    Init();
594  }
595  virtual ~TestURLRequestContextWithProxy() {}
596};
597
598}  // namespace
599
600// Inherit PlatformTest since we require the autorelease pool on Mac OS X.
601class URLRequestTest : public PlatformTest {
602 public:
603  URLRequestTest() : default_context_(true) {
604    default_context_.set_network_delegate(&default_network_delegate_);
605    default_context_.set_net_log(&net_log_);
606    job_factory_.SetProtocolHandler("data", new DataProtocolHandler);
607#if !defined(DISABLE_FILE_SUPPORT)
608    job_factory_.SetProtocolHandler(
609        "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
610#endif
611    default_context_.set_job_factory(&job_factory_);
612    default_context_.Init();
613  }
614  virtual ~URLRequestTest() {
615    // URLRequestJobs may post clean-up tasks on destruction.
616    base::RunLoop().RunUntilIdle();
617  }
618
619  // Adds the TestJobInterceptor to the default context.
620  TestJobInterceptor* AddTestInterceptor() {
621    TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
622    job_factory_.SetProtocolHandler("http", NULL);
623    job_factory_.SetProtocolHandler("http", protocol_handler_);
624    return protocol_handler_;
625  }
626
627 protected:
628  CapturingNetLog net_log_;
629  TestNetworkDelegate default_network_delegate_;  // Must outlive URLRequest.
630  URLRequestJobFactoryImpl job_factory_;
631  TestURLRequestContext default_context_;
632};
633
634TEST_F(URLRequestTest, AboutBlankTest) {
635  TestDelegate d;
636  {
637    URLRequest r(GURL("about:blank"), DEFAULT_PRIORITY, &d, &default_context_);
638
639    r.Start();
640    EXPECT_TRUE(r.is_pending());
641
642    base::RunLoop().Run();
643
644    EXPECT_TRUE(!r.is_pending());
645    EXPECT_FALSE(d.received_data_before_response());
646    EXPECT_EQ(d.bytes_received(), 0);
647    EXPECT_EQ("", r.GetSocketAddress().host());
648    EXPECT_EQ(0, r.GetSocketAddress().port());
649
650    HttpRequestHeaders headers;
651    EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
652  }
653}
654
655TEST_F(URLRequestTest, DataURLImageTest) {
656  TestDelegate d;
657  {
658    // Use our nice little Chrome logo.
659    URLRequest r(
660        GURL(
661        "data:image/png;base64,"
662        "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
663        "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
664        "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
665        "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
666        "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
667        "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
668        "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
669        "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
670        "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
671        "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
672        "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
673        "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
674        "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
675        "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
676        "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
677        "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
678        "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
679        "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
680        DEFAULT_PRIORITY,
681        &d,
682        &default_context_);
683
684    r.Start();
685    EXPECT_TRUE(r.is_pending());
686
687    base::RunLoop().Run();
688
689    EXPECT_TRUE(!r.is_pending());
690    EXPECT_FALSE(d.received_data_before_response());
691    EXPECT_EQ(d.bytes_received(), 911);
692    EXPECT_EQ("", r.GetSocketAddress().host());
693    EXPECT_EQ(0, r.GetSocketAddress().port());
694
695    HttpRequestHeaders headers;
696    EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
697  }
698}
699
700#if !defined(DISABLE_FILE_SUPPORT)
701TEST_F(URLRequestTest, FileTest) {
702  base::FilePath app_path;
703  PathService::Get(base::FILE_EXE, &app_path);
704  GURL app_url = FilePathToFileURL(app_path);
705
706  TestDelegate d;
707  {
708    URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_);
709
710    r.Start();
711    EXPECT_TRUE(r.is_pending());
712
713    base::RunLoop().Run();
714
715    int64 file_size = -1;
716    EXPECT_TRUE(base::GetFileSize(app_path, &file_size));
717
718    EXPECT_TRUE(!r.is_pending());
719    EXPECT_EQ(1, d.response_started_count());
720    EXPECT_FALSE(d.received_data_before_response());
721    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
722    EXPECT_EQ("", r.GetSocketAddress().host());
723    EXPECT_EQ(0, r.GetSocketAddress().port());
724
725    HttpRequestHeaders headers;
726    EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
727  }
728}
729
730TEST_F(URLRequestTest, FileTestCancel) {
731  base::FilePath app_path;
732  PathService::Get(base::FILE_EXE, &app_path);
733  GURL app_url = FilePathToFileURL(app_path);
734
735  TestDelegate d;
736  {
737    URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_);
738
739    r.Start();
740    EXPECT_TRUE(r.is_pending());
741    r.Cancel();
742  }
743  // Async cancellation should be safe even when URLRequest has been already
744  // destroyed.
745  base::RunLoop().RunUntilIdle();
746}
747
748TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
749  const size_t buffer_size = 4000;
750  scoped_ptr<char[]> buffer(new char[buffer_size]);
751  FillBuffer(buffer.get(), buffer_size);
752
753  base::FilePath temp_path;
754  EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
755  GURL temp_url = FilePathToFileURL(temp_path);
756  EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
757
758  int64 file_size;
759  EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
760
761  const size_t first_byte_position = 500;
762  const size_t last_byte_position = buffer_size - first_byte_position;
763  const size_t content_length = last_byte_position - first_byte_position + 1;
764  std::string partial_buffer_string(buffer.get() + first_byte_position,
765                                    buffer.get() + last_byte_position + 1);
766
767  TestDelegate d;
768  {
769    URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_);
770
771    HttpRequestHeaders headers;
772    headers.SetHeader(
773        HttpRequestHeaders::kRange,
774        net::HttpByteRange::Bounded(
775            first_byte_position, last_byte_position).GetHeaderValue());
776    r.SetExtraRequestHeaders(headers);
777    r.Start();
778    EXPECT_TRUE(r.is_pending());
779
780    base::RunLoop().Run();
781    EXPECT_TRUE(!r.is_pending());
782    EXPECT_EQ(1, d.response_started_count());
783    EXPECT_FALSE(d.received_data_before_response());
784    EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
785    // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
786    EXPECT_TRUE(partial_buffer_string == d.data_received());
787  }
788
789  EXPECT_TRUE(base::DeleteFile(temp_path, false));
790}
791
792TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
793  const size_t buffer_size = 4000;
794  scoped_ptr<char[]> buffer(new char[buffer_size]);
795  FillBuffer(buffer.get(), buffer_size);
796
797  base::FilePath temp_path;
798  EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
799  GURL temp_url = FilePathToFileURL(temp_path);
800  EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
801
802  int64 file_size;
803  EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
804
805  const size_t first_byte_position = 500;
806  const size_t last_byte_position = buffer_size - 1;
807  const size_t content_length = last_byte_position - first_byte_position + 1;
808  std::string partial_buffer_string(buffer.get() + first_byte_position,
809                                    buffer.get() + last_byte_position + 1);
810
811  TestDelegate d;
812  {
813    URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_);
814
815    HttpRequestHeaders headers;
816    headers.SetHeader(HttpRequestHeaders::kRange,
817                      net::HttpByteRange::RightUnbounded(
818                          first_byte_position).GetHeaderValue());
819    r.SetExtraRequestHeaders(headers);
820    r.Start();
821    EXPECT_TRUE(r.is_pending());
822
823    base::RunLoop().Run();
824    EXPECT_TRUE(!r.is_pending());
825    EXPECT_EQ(1, d.response_started_count());
826    EXPECT_FALSE(d.received_data_before_response());
827    EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
828    // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
829    EXPECT_TRUE(partial_buffer_string == d.data_received());
830  }
831
832  EXPECT_TRUE(base::DeleteFile(temp_path, false));
833}
834
835TEST_F(URLRequestTest, FileTestMultipleRanges) {
836  const size_t buffer_size = 400000;
837  scoped_ptr<char[]> buffer(new char[buffer_size]);
838  FillBuffer(buffer.get(), buffer_size);
839
840  base::FilePath temp_path;
841  EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
842  GURL temp_url = FilePathToFileURL(temp_path);
843  EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
844
845  int64 file_size;
846  EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
847
848  TestDelegate d;
849  {
850    URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_);
851
852    HttpRequestHeaders headers;
853    headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300");
854    r.SetExtraRequestHeaders(headers);
855    r.Start();
856    EXPECT_TRUE(r.is_pending());
857
858    base::RunLoop().Run();
859    EXPECT_TRUE(d.request_failed());
860  }
861
862  EXPECT_TRUE(base::DeleteFile(temp_path, false));
863}
864
865TEST_F(URLRequestTest, AllowFileURLs) {
866  base::ScopedTempDir temp_dir;
867  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
868  base::FilePath test_file;
869  ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file));
870  std::string test_data("monkey");
871  base::WriteFile(test_file, test_data.data(), test_data.size());
872  GURL test_file_url = net::FilePathToFileURL(test_file);
873
874  {
875    TestDelegate d;
876    TestNetworkDelegate network_delegate;
877    network_delegate.set_can_access_files(true);
878    default_context_.set_network_delegate(&network_delegate);
879    URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_);
880    r.Start();
881    base::RunLoop().Run();
882    EXPECT_FALSE(d.request_failed());
883    EXPECT_EQ(test_data, d.data_received());
884  }
885
886  {
887    TestDelegate d;
888    TestNetworkDelegate network_delegate;
889    network_delegate.set_can_access_files(false);
890    default_context_.set_network_delegate(&network_delegate);
891    URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_);
892    r.Start();
893    base::RunLoop().Run();
894    EXPECT_TRUE(d.request_failed());
895    EXPECT_EQ("", d.data_received());
896  }
897}
898
899
900TEST_F(URLRequestTest, FileDirCancelTest) {
901  // Put in mock resource provider.
902  NetModule::SetResourceProvider(TestNetResourceProvider);
903
904  TestDelegate d;
905  {
906    base::FilePath file_path;
907    PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
908    file_path = file_path.Append(FILE_PATH_LITERAL("net"));
909    file_path = file_path.Append(FILE_PATH_LITERAL("data"));
910
911    URLRequest req(
912        FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, &default_context_);
913    req.Start();
914    EXPECT_TRUE(req.is_pending());
915
916    d.set_cancel_in_received_data_pending(true);
917
918    base::RunLoop().Run();
919  }
920
921  // Take out mock resource provider.
922  NetModule::SetResourceProvider(NULL);
923}
924
925TEST_F(URLRequestTest, FileDirOutputSanity) {
926  // Verify the general sanity of the the output of the file:
927  // directory lister by checking for the output of a known existing
928  // file.
929  const char sentinel_name[] = "filedir-sentinel";
930
931  base::FilePath path;
932  PathService::Get(base::DIR_SOURCE_ROOT, &path);
933  path = path.Append(FILE_PATH_LITERAL("net"));
934  path = path.Append(FILE_PATH_LITERAL("data"));
935  path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
936
937  TestDelegate d;
938  URLRequest req(
939      FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_);
940  req.Start();
941  base::RunLoop().Run();
942
943  // Generate entry for the sentinel file.
944  base::FilePath sentinel_path = path.AppendASCII(sentinel_name);
945  base::File::Info info;
946  EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info));
947  EXPECT_GT(info.size, 0);
948  std::string sentinel_output = GetDirectoryListingEntry(
949      base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)),
950      std::string(sentinel_name),
951      false /* is_dir */,
952      info.size,
953      info.last_modified);
954
955  ASSERT_LT(0, d.bytes_received());
956  ASSERT_FALSE(d.request_failed());
957  ASSERT_TRUE(req.status().is_success());
958  // Check for the entry generated for the "sentinel" file.
959  const std::string& data = d.data_received();
960  ASSERT_NE(data.find(sentinel_output), std::string::npos);
961}
962
963TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
964  // There is an implicit redirect when loading a file path that matches a
965  // directory and does not end with a slash.  Ensure that following such
966  // redirects does not crash.  See http://crbug.com/18686.
967
968  base::FilePath path;
969  PathService::Get(base::DIR_SOURCE_ROOT, &path);
970  path = path.Append(FILE_PATH_LITERAL("net"));
971  path = path.Append(FILE_PATH_LITERAL("data"));
972  path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
973
974  TestDelegate d;
975  URLRequest req(
976      FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_);
977  req.Start();
978  base::RunLoop().Run();
979
980  ASSERT_EQ(1, d.received_redirect_count());
981  ASSERT_LT(0, d.bytes_received());
982  ASSERT_FALSE(d.request_failed());
983  ASSERT_TRUE(req.status().is_success());
984}
985
986#if defined(OS_WIN)
987// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
988TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
989  TestDelegate d;
990  URLRequest req(GURL("file:///"), DEFAULT_PRIORITY, &d, &default_context_);
991  req.Start();
992  base::RunLoop().Run();
993
994  ASSERT_EQ(1, d.received_redirect_count());
995  ASSERT_FALSE(req.status().is_success());
996}
997#endif  // defined(OS_WIN)
998
999#endif  // !defined(DISABLE_FILE_SUPPORT)
1000
1001TEST_F(URLRequestTest, InvalidUrlTest) {
1002  TestDelegate d;
1003  {
1004    URLRequest r(GURL("invalid url"), DEFAULT_PRIORITY, &d, &default_context_);
1005
1006    r.Start();
1007    EXPECT_TRUE(r.is_pending());
1008
1009    base::RunLoop().Run();
1010    EXPECT_TRUE(d.request_failed());
1011  }
1012}
1013
1014#if defined(OS_WIN)
1015TEST_F(URLRequestTest, ResolveShortcutTest) {
1016  base::FilePath app_path;
1017  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1018  app_path = app_path.AppendASCII("net");
1019  app_path = app_path.AppendASCII("data");
1020  app_path = app_path.AppendASCII("url_request_unittest");
1021  app_path = app_path.AppendASCII("with-headers.html");
1022
1023  std::wstring lnk_path = app_path.value() + L".lnk";
1024
1025  base::win::ScopedCOMInitializer com_initializer;
1026
1027  // Temporarily create a shortcut for test
1028  {
1029    base::win::ScopedComPtr<IShellLink> shell;
1030    ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL,
1031                                               CLSCTX_INPROC_SERVER)));
1032    base::win::ScopedComPtr<IPersistFile> persist;
1033    ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive())));
1034    EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
1035    EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
1036    EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
1037  }
1038
1039  TestDelegate d;
1040  {
1041    URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)),
1042                 DEFAULT_PRIORITY,
1043                 &d,
1044                 &default_context_);
1045
1046    r.Start();
1047    EXPECT_TRUE(r.is_pending());
1048
1049    base::RunLoop().Run();
1050
1051    WIN32_FILE_ATTRIBUTE_DATA data;
1052    GetFileAttributesEx(app_path.value().c_str(),
1053                        GetFileExInfoStandard, &data);
1054    HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
1055                             FILE_SHARE_READ, NULL, OPEN_EXISTING,
1056                             FILE_ATTRIBUTE_NORMAL, NULL);
1057    EXPECT_NE(INVALID_HANDLE_VALUE, file);
1058    scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]);
1059    DWORD read_size;
1060    BOOL result;
1061    result = ReadFile(file, buffer.get(), data.nFileSizeLow,
1062                      &read_size, NULL);
1063    std::string content(buffer.get(), read_size);
1064    CloseHandle(file);
1065
1066    EXPECT_TRUE(!r.is_pending());
1067    EXPECT_EQ(1, d.received_redirect_count());
1068    EXPECT_EQ(content, d.data_received());
1069  }
1070
1071  // Clean the shortcut
1072  DeleteFile(lnk_path.c_str());
1073}
1074#endif  // defined(OS_WIN)
1075
1076// Custom URLRequestJobs for use with interceptor tests
1077class RestartTestJob : public URLRequestTestJob {
1078 public:
1079  RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1080    : URLRequestTestJob(request, network_delegate, true) {}
1081 protected:
1082  virtual void StartAsync() OVERRIDE {
1083    this->NotifyRestartRequired();
1084  }
1085 private:
1086  virtual ~RestartTestJob() {}
1087};
1088
1089class CancelTestJob : public URLRequestTestJob {
1090 public:
1091  explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1092    : URLRequestTestJob(request, network_delegate, true) {}
1093 protected:
1094  virtual void StartAsync() OVERRIDE {
1095    request_->Cancel();
1096  }
1097 private:
1098  virtual ~CancelTestJob() {}
1099};
1100
1101class CancelThenRestartTestJob : public URLRequestTestJob {
1102 public:
1103  explicit CancelThenRestartTestJob(URLRequest* request,
1104                                    NetworkDelegate* network_delegate)
1105      : URLRequestTestJob(request, network_delegate, true) {
1106  }
1107 protected:
1108  virtual void StartAsync() OVERRIDE {
1109    request_->Cancel();
1110    this->NotifyRestartRequired();
1111  }
1112 private:
1113  virtual ~CancelThenRestartTestJob() {}
1114};
1115
1116// An Interceptor for use with interceptor tests
1117class TestInterceptor : URLRequest::Interceptor {
1118 public:
1119  TestInterceptor()
1120      : intercept_main_request_(false), restart_main_request_(false),
1121        cancel_main_request_(false), cancel_then_restart_main_request_(false),
1122        simulate_main_network_error_(false),
1123        intercept_redirect_(false), cancel_redirect_request_(false),
1124        intercept_final_response_(false), cancel_final_request_(false),
1125        did_intercept_main_(false), did_restart_main_(false),
1126        did_cancel_main_(false), did_cancel_then_restart_main_(false),
1127        did_simulate_error_main_(false),
1128        did_intercept_redirect_(false), did_cancel_redirect_(false),
1129        did_intercept_final_(false), did_cancel_final_(false) {
1130    URLRequest::Deprecated::RegisterRequestInterceptor(this);
1131  }
1132
1133  virtual ~TestInterceptor() {
1134    URLRequest::Deprecated::UnregisterRequestInterceptor(this);
1135  }
1136
1137  virtual URLRequestJob* MaybeIntercept(
1138      URLRequest* request,
1139      NetworkDelegate* network_delegate) OVERRIDE {
1140    if (restart_main_request_) {
1141      restart_main_request_ = false;
1142      did_restart_main_ = true;
1143      return new RestartTestJob(request, network_delegate);
1144    }
1145    if (cancel_main_request_) {
1146      cancel_main_request_ = false;
1147      did_cancel_main_ = true;
1148      return new CancelTestJob(request, network_delegate);
1149    }
1150    if (cancel_then_restart_main_request_) {
1151      cancel_then_restart_main_request_ = false;
1152      did_cancel_then_restart_main_ = true;
1153      return new CancelThenRestartTestJob(request, network_delegate);
1154    }
1155    if (simulate_main_network_error_) {
1156      simulate_main_network_error_ = false;
1157      did_simulate_error_main_ = true;
1158      // will error since the requeted url is not one of its canned urls
1159      return new URLRequestTestJob(request, network_delegate, true);
1160    }
1161    if (!intercept_main_request_)
1162      return NULL;
1163    intercept_main_request_ = false;
1164    did_intercept_main_ = true;
1165    URLRequestTestJob* job =  new URLRequestTestJob(request,
1166                                                    network_delegate,
1167                                                    main_headers_,
1168                                                    main_data_,
1169                                                    true);
1170    job->set_load_timing_info(main_request_load_timing_info_);
1171    return job;
1172  }
1173
1174  virtual URLRequestJob* MaybeInterceptRedirect(
1175      URLRequest* request,
1176      NetworkDelegate* network_delegate,
1177      const GURL& location) OVERRIDE {
1178    if (cancel_redirect_request_) {
1179      cancel_redirect_request_ = false;
1180      did_cancel_redirect_ = true;
1181      return new CancelTestJob(request, network_delegate);
1182    }
1183    if (!intercept_redirect_)
1184      return NULL;
1185    intercept_redirect_ = false;
1186    did_intercept_redirect_ = true;
1187    return new URLRequestTestJob(request,
1188                                 network_delegate,
1189                                 redirect_headers_,
1190                                 redirect_data_,
1191                                 true);
1192  }
1193
1194  virtual URLRequestJob* MaybeInterceptResponse(
1195      URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE {
1196    if (cancel_final_request_) {
1197      cancel_final_request_ = false;
1198      did_cancel_final_ = true;
1199      return new CancelTestJob(request, network_delegate);
1200    }
1201    if (!intercept_final_response_)
1202      return NULL;
1203    intercept_final_response_ = false;
1204    did_intercept_final_ = true;
1205    return new URLRequestTestJob(request,
1206                                 network_delegate,
1207                                 final_headers_,
1208                                 final_data_,
1209                                 true);
1210  }
1211
1212  // Whether to intercept the main request, and if so the response to return and
1213  // the LoadTimingInfo to use.
1214  bool intercept_main_request_;
1215  std::string main_headers_;
1216  std::string main_data_;
1217  LoadTimingInfo main_request_load_timing_info_;
1218
1219  // Other actions we take at MaybeIntercept time
1220  bool restart_main_request_;
1221  bool cancel_main_request_;
1222  bool cancel_then_restart_main_request_;
1223  bool simulate_main_network_error_;
1224
1225  // Whether to intercept redirects, and if so the response to return.
1226  bool intercept_redirect_;
1227  std::string redirect_headers_;
1228  std::string redirect_data_;
1229
1230  // Other actions we can take at MaybeInterceptRedirect time
1231  bool cancel_redirect_request_;
1232
1233  // Whether to intercept final response, and if so the response to return.
1234  bool intercept_final_response_;
1235  std::string final_headers_;
1236  std::string final_data_;
1237
1238  // Other actions we can take at MaybeInterceptResponse time
1239  bool cancel_final_request_;
1240
1241  // If we did something or not
1242  bool did_intercept_main_;
1243  bool did_restart_main_;
1244  bool did_cancel_main_;
1245  bool did_cancel_then_restart_main_;
1246  bool did_simulate_error_main_;
1247  bool did_intercept_redirect_;
1248  bool did_cancel_redirect_;
1249  bool did_intercept_final_;
1250  bool did_cancel_final_;
1251
1252  // Static getters for canned response header and data strings
1253
1254  static std::string ok_data() {
1255    return URLRequestTestJob::test_data_1();
1256  }
1257
1258  static std::string ok_headers() {
1259    return URLRequestTestJob::test_headers();
1260  }
1261
1262  static std::string redirect_data() {
1263    return std::string();
1264  }
1265
1266  static std::string redirect_headers() {
1267    return URLRequestTestJob::test_redirect_headers();
1268  }
1269
1270  static std::string error_data() {
1271    return std::string("ohhh nooooo mr. bill!");
1272  }
1273
1274  static std::string error_headers() {
1275    return URLRequestTestJob::test_error_headers();
1276  }
1277};
1278
1279TEST_F(URLRequestTest, Intercept) {
1280  TestInterceptor interceptor;
1281
1282  // intercept the main request and respond with a simple response
1283  interceptor.intercept_main_request_ = true;
1284  interceptor.main_headers_ = TestInterceptor::ok_headers();
1285  interceptor.main_data_ = TestInterceptor::ok_data();
1286
1287  TestDelegate d;
1288  URLRequest req(GURL("http://test_intercept/foo"),
1289                 DEFAULT_PRIORITY,
1290                 &d,
1291                 &default_context_);
1292  base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1293  base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1294  base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
1295  req.SetUserData(NULL, user_data0);
1296  req.SetUserData(&user_data1, user_data1);
1297  req.SetUserData(&user_data2, user_data2);
1298  req.set_method("GET");
1299  req.Start();
1300  base::RunLoop().Run();
1301
1302  // Make sure we can retrieve our specific user data
1303  EXPECT_EQ(user_data0, req.GetUserData(NULL));
1304  EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
1305  EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
1306
1307  // Check the interceptor got called as expected
1308  EXPECT_TRUE(interceptor.did_intercept_main_);
1309
1310  // Check we got one good response
1311  EXPECT_TRUE(req.status().is_success());
1312  EXPECT_EQ(200, req.response_headers()->response_code());
1313  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1314  EXPECT_EQ(1, d.response_started_count());
1315  EXPECT_EQ(0, d.received_redirect_count());
1316}
1317
1318TEST_F(URLRequestTest, InterceptRedirect) {
1319  TestInterceptor interceptor;
1320
1321  // intercept the main request and respond with a redirect
1322  interceptor.intercept_main_request_ = true;
1323  interceptor.main_headers_ = TestInterceptor::redirect_headers();
1324  interceptor.main_data_ = TestInterceptor::redirect_data();
1325
1326  // intercept that redirect and respond a final OK response
1327  interceptor.intercept_redirect_ = true;
1328  interceptor.redirect_headers_ =  TestInterceptor::ok_headers();
1329  interceptor.redirect_data_ = TestInterceptor::ok_data();
1330
1331  TestDelegate d;
1332  URLRequest req(GURL("http://test_intercept/foo"),
1333                 DEFAULT_PRIORITY,
1334                 &d,
1335                 &default_context_);
1336  req.set_method("GET");
1337  req.Start();
1338  base::RunLoop().Run();
1339
1340  // Check the interceptor got called as expected
1341  EXPECT_TRUE(interceptor.did_intercept_main_);
1342  EXPECT_TRUE(interceptor.did_intercept_redirect_);
1343
1344  // Check we got one good response
1345  EXPECT_TRUE(req.status().is_success());
1346  if (req.status().is_success()) {
1347    EXPECT_EQ(200, req.response_headers()->response_code());
1348  }
1349  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1350  EXPECT_EQ(1, d.response_started_count());
1351  EXPECT_EQ(0, d.received_redirect_count());
1352}
1353
1354TEST_F(URLRequestTest, InterceptServerError) {
1355  TestInterceptor interceptor;
1356
1357  // intercept the main request to generate a server error response
1358  interceptor.intercept_main_request_ = true;
1359  interceptor.main_headers_ = TestInterceptor::error_headers();
1360  interceptor.main_data_ = TestInterceptor::error_data();
1361
1362  // intercept that error and respond with an OK response
1363  interceptor.intercept_final_response_ = true;
1364  interceptor.final_headers_ = TestInterceptor::ok_headers();
1365  interceptor.final_data_ = TestInterceptor::ok_data();
1366
1367  TestDelegate d;
1368  URLRequest req(GURL("http://test_intercept/foo"),
1369                 DEFAULT_PRIORITY,
1370                 &d,
1371                 &default_context_);
1372  req.set_method("GET");
1373  req.Start();
1374  base::RunLoop().Run();
1375
1376  // Check the interceptor got called as expected
1377  EXPECT_TRUE(interceptor.did_intercept_main_);
1378  EXPECT_TRUE(interceptor.did_intercept_final_);
1379
1380  // Check we got one good response
1381  EXPECT_TRUE(req.status().is_success());
1382  EXPECT_EQ(200, req.response_headers()->response_code());
1383  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1384  EXPECT_EQ(1, d.response_started_count());
1385  EXPECT_EQ(0, d.received_redirect_count());
1386}
1387
1388TEST_F(URLRequestTest, InterceptNetworkError) {
1389  TestInterceptor interceptor;
1390
1391  // intercept the main request to simulate a network error
1392  interceptor.simulate_main_network_error_ = true;
1393
1394  // intercept that error and respond with an OK response
1395  interceptor.intercept_final_response_ = true;
1396  interceptor.final_headers_ = TestInterceptor::ok_headers();
1397  interceptor.final_data_ = TestInterceptor::ok_data();
1398
1399  TestDelegate d;
1400  URLRequest req(GURL("http://test_intercept/foo"),
1401                 DEFAULT_PRIORITY,
1402                 &d,
1403                 &default_context_);
1404  req.set_method("GET");
1405  req.Start();
1406  base::RunLoop().Run();
1407
1408  // Check the interceptor got called as expected
1409  EXPECT_TRUE(interceptor.did_simulate_error_main_);
1410  EXPECT_TRUE(interceptor.did_intercept_final_);
1411
1412  // Check we received one good response
1413  EXPECT_TRUE(req.status().is_success());
1414  EXPECT_EQ(200, req.response_headers()->response_code());
1415  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1416  EXPECT_EQ(1, d.response_started_count());
1417  EXPECT_EQ(0, d.received_redirect_count());
1418}
1419
1420TEST_F(URLRequestTest, InterceptRestartRequired) {
1421  TestInterceptor interceptor;
1422
1423  // restart the main request
1424  interceptor.restart_main_request_ = true;
1425
1426  // then intercept the new main request and respond with an OK response
1427  interceptor.intercept_main_request_ = true;
1428  interceptor.main_headers_ = TestInterceptor::ok_headers();
1429  interceptor.main_data_ = TestInterceptor::ok_data();
1430
1431  TestDelegate d;
1432  URLRequest req(GURL("http://test_intercept/foo"),
1433                 DEFAULT_PRIORITY,
1434                 &d,
1435                 &default_context_);
1436  req.set_method("GET");
1437  req.Start();
1438  base::RunLoop().Run();
1439
1440  // Check the interceptor got called as expected
1441  EXPECT_TRUE(interceptor.did_restart_main_);
1442  EXPECT_TRUE(interceptor.did_intercept_main_);
1443
1444  // Check we received one good response
1445  EXPECT_TRUE(req.status().is_success());
1446  if (req.status().is_success()) {
1447    EXPECT_EQ(200, req.response_headers()->response_code());
1448  }
1449  EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1450  EXPECT_EQ(1, d.response_started_count());
1451  EXPECT_EQ(0, d.received_redirect_count());
1452}
1453
1454TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1455  TestInterceptor interceptor;
1456
1457  // intercept the main request and cancel from within the restarted job
1458  interceptor.cancel_main_request_ = true;
1459
1460  // setup to intercept final response and override it with an OK response
1461  interceptor.intercept_final_response_ = true;
1462  interceptor.final_headers_ = TestInterceptor::ok_headers();
1463  interceptor.final_data_ = TestInterceptor::ok_data();
1464
1465  TestDelegate d;
1466  URLRequest req(GURL("http://test_intercept/foo"),
1467                 DEFAULT_PRIORITY,
1468                 &d,
1469                 &default_context_);
1470  req.set_method("GET");
1471  req.Start();
1472  base::RunLoop().Run();
1473
1474  // Check the interceptor got called as expected
1475  EXPECT_TRUE(interceptor.did_cancel_main_);
1476  EXPECT_FALSE(interceptor.did_intercept_final_);
1477
1478  // Check we see a canceled request
1479  EXPECT_FALSE(req.status().is_success());
1480  EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1481}
1482
1483TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1484  TestInterceptor interceptor;
1485
1486  // intercept the main request and respond with a redirect
1487  interceptor.intercept_main_request_ = true;
1488  interceptor.main_headers_ = TestInterceptor::redirect_headers();
1489  interceptor.main_data_ = TestInterceptor::redirect_data();
1490
1491  // intercept the redirect and cancel from within that job
1492  interceptor.cancel_redirect_request_ = true;
1493
1494  // setup to intercept final response and override it with an OK response
1495  interceptor.intercept_final_response_ = true;
1496  interceptor.final_headers_ = TestInterceptor::ok_headers();
1497  interceptor.final_data_ = TestInterceptor::ok_data();
1498
1499  TestDelegate d;
1500  URLRequest req(GURL("http://test_intercept/foo"),
1501                 DEFAULT_PRIORITY,
1502                 &d,
1503                 &default_context_);
1504  req.set_method("GET");
1505  req.Start();
1506  base::RunLoop().Run();
1507
1508  // Check the interceptor got called as expected
1509  EXPECT_TRUE(interceptor.did_intercept_main_);
1510  EXPECT_TRUE(interceptor.did_cancel_redirect_);
1511  EXPECT_FALSE(interceptor.did_intercept_final_);
1512
1513  // Check we see a canceled request
1514  EXPECT_FALSE(req.status().is_success());
1515  EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1516}
1517
1518TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1519  TestInterceptor interceptor;
1520
1521  // intercept the main request to simulate a network error
1522  interceptor.simulate_main_network_error_ = true;
1523
1524  // setup to intercept final response and cancel from within that job
1525  interceptor.cancel_final_request_ = true;
1526
1527  TestDelegate d;
1528  URLRequest req(GURL("http://test_intercept/foo"),
1529                 DEFAULT_PRIORITY,
1530                 &d,
1531                 &default_context_);
1532  req.set_method("GET");
1533  req.Start();
1534  base::RunLoop().Run();
1535
1536  // Check the interceptor got called as expected
1537  EXPECT_TRUE(interceptor.did_simulate_error_main_);
1538  EXPECT_TRUE(interceptor.did_cancel_final_);
1539
1540  // Check we see a canceled request
1541  EXPECT_FALSE(req.status().is_success());
1542  EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1543}
1544
1545TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1546  TestInterceptor interceptor;
1547
1548  // intercept the main request and cancel then restart from within that job
1549  interceptor.cancel_then_restart_main_request_ = true;
1550
1551  // setup to intercept final response and override it with an OK response
1552  interceptor.intercept_final_response_ = true;
1553  interceptor.final_headers_ = TestInterceptor::ok_headers();
1554  interceptor.final_data_ = TestInterceptor::ok_data();
1555
1556  TestDelegate d;
1557  URLRequest req(GURL("http://test_intercept/foo"),
1558                 DEFAULT_PRIORITY,
1559                 &d,
1560                 &default_context_);
1561  req.set_method("GET");
1562  req.Start();
1563  base::RunLoop().Run();
1564
1565  // Check the interceptor got called as expected
1566  EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1567  EXPECT_FALSE(interceptor.did_intercept_final_);
1568
1569  // Check we see a canceled request
1570  EXPECT_FALSE(req.status().is_success());
1571  EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1572}
1573
1574LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing,
1575                                 URLRequestContext* context) {
1576  TestInterceptor interceptor;
1577  interceptor.intercept_main_request_ = true;
1578  interceptor.main_request_load_timing_info_ = job_load_timing;
1579  TestDelegate d;
1580  URLRequest req(
1581      GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, context);
1582  req.Start();
1583  base::RunLoop().Run();
1584
1585  LoadTimingInfo resulting_load_timing;
1586  req.GetLoadTimingInfo(&resulting_load_timing);
1587
1588  // None of these should be modified by the URLRequest.
1589  EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused);
1590  EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id);
1591  EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start);
1592  EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end);
1593  EXPECT_EQ(job_load_timing.receive_headers_end,
1594            resulting_load_timing.receive_headers_end);
1595
1596  return resulting_load_timing;
1597}
1598
1599// "Normal" LoadTimingInfo as returned by a job.  Everything is in order, not
1600// reused.  |connect_time_flags| is used to indicate if there should be dns
1601// or SSL times, and |used_proxy| is used for proxy times.
1602LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now,
1603                                    int connect_time_flags,
1604                                    bool used_proxy) {
1605  LoadTimingInfo load_timing;
1606  load_timing.socket_log_id = 1;
1607
1608  if (used_proxy) {
1609    load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1610    load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1611  }
1612
1613  LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing;
1614  if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) {
1615    connect_timing.dns_start = now + base::TimeDelta::FromDays(3);
1616    connect_timing.dns_end = now + base::TimeDelta::FromDays(4);
1617  }
1618  connect_timing.connect_start = now + base::TimeDelta::FromDays(5);
1619  if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) {
1620    connect_timing.ssl_start = now + base::TimeDelta::FromDays(6);
1621    connect_timing.ssl_end = now + base::TimeDelta::FromDays(7);
1622  }
1623  connect_timing.connect_end = now + base::TimeDelta::FromDays(8);
1624
1625  load_timing.send_start = now + base::TimeDelta::FromDays(9);
1626  load_timing.send_end = now + base::TimeDelta::FromDays(10);
1627  load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1628  return load_timing;
1629}
1630
1631// Same as above, but in the case of a reused socket.
1632LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now,
1633                                          bool used_proxy) {
1634  LoadTimingInfo load_timing;
1635  load_timing.socket_log_id = 1;
1636  load_timing.socket_reused = true;
1637
1638  if (used_proxy) {
1639    load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1640    load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1641  }
1642
1643  load_timing.send_start = now + base::TimeDelta::FromDays(9);
1644  load_timing.send_end = now + base::TimeDelta::FromDays(10);
1645  load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1646  return load_timing;
1647}
1648
1649// Basic test that the intercept + load timing tests work.
1650TEST_F(URLRequestTest, InterceptLoadTiming) {
1651  base::TimeTicks now = base::TimeTicks::Now();
1652  LoadTimingInfo job_load_timing =
1653      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false);
1654
1655  LoadTimingInfo load_timing_result =
1656      RunLoadTimingTest(job_load_timing, &default_context_);
1657
1658  // Nothing should have been changed by the URLRequest.
1659  EXPECT_EQ(job_load_timing.proxy_resolve_start,
1660            load_timing_result.proxy_resolve_start);
1661  EXPECT_EQ(job_load_timing.proxy_resolve_end,
1662            load_timing_result.proxy_resolve_end);
1663  EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1664            load_timing_result.connect_timing.dns_start);
1665  EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1666            load_timing_result.connect_timing.dns_end);
1667  EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1668            load_timing_result.connect_timing.connect_start);
1669  EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1670            load_timing_result.connect_timing.connect_end);
1671  EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1672            load_timing_result.connect_timing.ssl_start);
1673  EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1674            load_timing_result.connect_timing.ssl_end);
1675
1676  // Redundant sanity check.
1677  TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES);
1678}
1679
1680// Another basic test, with proxy and SSL times, but no DNS times.
1681TEST_F(URLRequestTest, InterceptLoadTimingProxy) {
1682  base::TimeTicks now = base::TimeTicks::Now();
1683  LoadTimingInfo job_load_timing =
1684      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true);
1685
1686  LoadTimingInfo load_timing_result =
1687      RunLoadTimingTest(job_load_timing, &default_context_);
1688
1689  // Nothing should have been changed by the URLRequest.
1690  EXPECT_EQ(job_load_timing.proxy_resolve_start,
1691            load_timing_result.proxy_resolve_start);
1692  EXPECT_EQ(job_load_timing.proxy_resolve_end,
1693            load_timing_result.proxy_resolve_end);
1694  EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1695            load_timing_result.connect_timing.dns_start);
1696  EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1697            load_timing_result.connect_timing.dns_end);
1698  EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1699            load_timing_result.connect_timing.connect_start);
1700  EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1701            load_timing_result.connect_timing.connect_end);
1702  EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1703            load_timing_result.connect_timing.ssl_start);
1704  EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1705            load_timing_result.connect_timing.ssl_end);
1706
1707  // Redundant sanity check.
1708  TestLoadTimingNotReusedWithProxy(load_timing_result,
1709                                   CONNECT_TIMING_HAS_SSL_TIMES);
1710}
1711
1712// Make sure that URLRequest correctly adjusts proxy times when they're before
1713// |request_start|, due to already having a connected socket.  This happens in
1714// the case of reusing a SPDY session.  The connected socket is not considered
1715// reused in this test (May be a preconnect).
1716//
1717// To mix things up from the test above, assumes DNS times but no SSL times.
1718TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) {
1719  base::TimeTicks now = base::TimeTicks::Now();
1720  LoadTimingInfo job_load_timing =
1721      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true);
1722  job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6);
1723  job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5);
1724  job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4);
1725  job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3);
1726  job_load_timing.connect_timing.connect_start =
1727      now - base::TimeDelta::FromDays(2);
1728  job_load_timing.connect_timing.connect_end =
1729      now - base::TimeDelta::FromDays(1);
1730
1731  LoadTimingInfo load_timing_result =
1732      RunLoadTimingTest(job_load_timing, &default_context_);
1733
1734  // Proxy times, connect times, and DNS times should all be replaced with
1735  // request_start.
1736  EXPECT_EQ(load_timing_result.request_start,
1737            load_timing_result.proxy_resolve_start);
1738  EXPECT_EQ(load_timing_result.request_start,
1739            load_timing_result.proxy_resolve_end);
1740  EXPECT_EQ(load_timing_result.request_start,
1741            load_timing_result.connect_timing.dns_start);
1742  EXPECT_EQ(load_timing_result.request_start,
1743            load_timing_result.connect_timing.dns_end);
1744  EXPECT_EQ(load_timing_result.request_start,
1745            load_timing_result.connect_timing.connect_start);
1746  EXPECT_EQ(load_timing_result.request_start,
1747            load_timing_result.connect_timing.connect_end);
1748
1749  // Other times should have been left null.
1750  TestLoadTimingNotReusedWithProxy(load_timing_result,
1751                                   CONNECT_TIMING_HAS_DNS_TIMES);
1752}
1753
1754// Same as above, but in the reused case.
1755TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) {
1756  base::TimeTicks now = base::TimeTicks::Now();
1757  LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true);
1758  job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4);
1759  job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3);
1760
1761  LoadTimingInfo load_timing_result =
1762      RunLoadTimingTest(job_load_timing, &default_context_);
1763
1764  // Proxy times and connect times should all be replaced with request_start.
1765  EXPECT_EQ(load_timing_result.request_start,
1766            load_timing_result.proxy_resolve_start);
1767  EXPECT_EQ(load_timing_result.request_start,
1768            load_timing_result.proxy_resolve_end);
1769
1770  // Other times should have been left null.
1771  TestLoadTimingReusedWithProxy(load_timing_result);
1772}
1773
1774// Make sure that URLRequest correctly adjusts connect times when they're before
1775// |request_start|, due to reusing a connected socket.  The connected socket is
1776// not considered reused in this test (May be a preconnect).
1777//
1778// To mix things up, the request has SSL times, but no DNS times.
1779TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) {
1780  base::TimeTicks now = base::TimeTicks::Now();
1781  LoadTimingInfo job_load_timing =
1782      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false);
1783  job_load_timing.connect_timing.connect_start =
1784      now - base::TimeDelta::FromDays(1);
1785  job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2);
1786  job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3);
1787  job_load_timing.connect_timing.connect_end =
1788      now - base::TimeDelta::FromDays(4);
1789
1790  LoadTimingInfo load_timing_result =
1791      RunLoadTimingTest(job_load_timing, &default_context_);
1792
1793  // Connect times, and SSL times should be replaced with request_start.
1794  EXPECT_EQ(load_timing_result.request_start,
1795            load_timing_result.connect_timing.connect_start);
1796  EXPECT_EQ(load_timing_result.request_start,
1797            load_timing_result.connect_timing.ssl_start);
1798  EXPECT_EQ(load_timing_result.request_start,
1799            load_timing_result.connect_timing.ssl_end);
1800  EXPECT_EQ(load_timing_result.request_start,
1801            load_timing_result.connect_timing.connect_end);
1802
1803  // Other times should have been left null.
1804  TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES);
1805}
1806
1807// Make sure that URLRequest correctly adjusts connect times when they're before
1808// |request_start|, due to reusing a connected socket in the case that there
1809// are also proxy times.  The connected socket is not considered reused in this
1810// test (May be a preconnect).
1811//
1812// In this test, there are no SSL or DNS times.
1813TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) {
1814  base::TimeTicks now = base::TimeTicks::Now();
1815  LoadTimingInfo job_load_timing =
1816      NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true);
1817  job_load_timing.connect_timing.connect_start =
1818      now - base::TimeDelta::FromDays(1);
1819  job_load_timing.connect_timing.connect_end =
1820      now - base::TimeDelta::FromDays(2);
1821
1822  LoadTimingInfo load_timing_result =
1823      RunLoadTimingTest(job_load_timing, &default_context_);
1824
1825  // Connect times should be replaced with proxy_resolve_end.
1826  EXPECT_EQ(load_timing_result.proxy_resolve_end,
1827            load_timing_result.connect_timing.connect_start);
1828  EXPECT_EQ(load_timing_result.proxy_resolve_end,
1829            load_timing_result.connect_timing.connect_end);
1830
1831  // Other times should have been left null.
1832  TestLoadTimingNotReusedWithProxy(load_timing_result,
1833                                   CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
1834}
1835
1836// Check that two different URL requests have different identifiers.
1837TEST_F(URLRequestTest, Identifiers) {
1838  TestDelegate d;
1839  TestURLRequestContext context;
1840  TestURLRequest req(
1841      GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
1842  TestURLRequest other_req(
1843      GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
1844
1845  ASSERT_NE(req.identifier(), other_req.identifier());
1846}
1847
1848// Check that a failure to connect to the proxy is reported to the network
1849// delegate.
1850TEST_F(URLRequestTest, NetworkDelegateProxyError) {
1851  MockHostResolver host_resolver;
1852  host_resolver.rules()->AddSimulatedFailure("*");
1853
1854  TestNetworkDelegate network_delegate;  // Must outlive URLRequests.
1855  TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
1856
1857  TestDelegate d;
1858  URLRequest req(GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
1859  req.set_method("GET");
1860
1861  req.Start();
1862  base::RunLoop().Run();
1863
1864  // Check we see a failed request.
1865  EXPECT_FALSE(req.status().is_success());
1866  // The proxy server is not set before failure.
1867  EXPECT_TRUE(req.proxy_server().IsEmpty());
1868  EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1869  EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error());
1870
1871  EXPECT_EQ(1, network_delegate.error_count());
1872  EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
1873  EXPECT_EQ(1, network_delegate.completed_requests());
1874}
1875
1876// Make sure that net::NetworkDelegate::NotifyCompleted is called if
1877// content is empty.
1878TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
1879  TestDelegate d;
1880  URLRequest req(GURL("data:,"), DEFAULT_PRIORITY, &d, &default_context_);
1881  req.Start();
1882  base::RunLoop().Run();
1883  EXPECT_EQ("", d.data_received());
1884  EXPECT_EQ(1, default_network_delegate_.completed_requests());
1885}
1886
1887// Make sure that SetPriority actually sets the URLRequest's priority
1888// correctly, both before and after start.
1889TEST_F(URLRequestTest, SetPriorityBasic) {
1890  TestDelegate d;
1891  URLRequest req(GURL("http://test_intercept/foo"),
1892                 DEFAULT_PRIORITY,
1893                 &d,
1894                 &default_context_);
1895  EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1896
1897  req.SetPriority(LOW);
1898  EXPECT_EQ(LOW, req.priority());
1899
1900  req.Start();
1901  EXPECT_EQ(LOW, req.priority());
1902
1903  req.SetPriority(MEDIUM);
1904  EXPECT_EQ(MEDIUM, req.priority());
1905}
1906
1907// Make sure that URLRequest calls SetPriority on a job before calling
1908// Start on it.
1909TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) {
1910  TestDelegate d;
1911  URLRequest req(GURL("http://test_intercept/foo"),
1912                 DEFAULT_PRIORITY,
1913                 &d,
1914                 &default_context_);
1915  EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1916
1917  scoped_refptr<URLRequestTestJob> job =
1918      new URLRequestTestJob(&req, &default_network_delegate_);
1919  AddTestInterceptor()->set_main_intercept_job(job.get());
1920  EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
1921
1922  req.SetPriority(LOW);
1923
1924  req.Start();
1925  EXPECT_EQ(LOW, job->priority());
1926}
1927
1928// Make sure that URLRequest passes on its priority updates to its
1929// job.
1930TEST_F(URLRequestTest, SetJobPriority) {
1931  TestDelegate d;
1932  URLRequest req(GURL("http://test_intercept/foo"),
1933                 DEFAULT_PRIORITY,
1934                 &d,
1935                 &default_context_);
1936
1937  scoped_refptr<URLRequestTestJob> job =
1938      new URLRequestTestJob(&req, &default_network_delegate_);
1939  AddTestInterceptor()->set_main_intercept_job(job.get());
1940
1941  req.SetPriority(LOW);
1942  req.Start();
1943  EXPECT_EQ(LOW, job->priority());
1944
1945  req.SetPriority(MEDIUM);
1946  EXPECT_EQ(MEDIUM, req.priority());
1947  EXPECT_EQ(MEDIUM, job->priority());
1948}
1949
1950// Setting the IGNORE_LIMITS load flag should be okay if the priority
1951// is MAXIMUM_PRIORITY.
1952TEST_F(URLRequestTest, PriorityIgnoreLimits) {
1953  TestDelegate d;
1954  URLRequest req(GURL("http://test_intercept/foo"),
1955                 MAXIMUM_PRIORITY,
1956                 &d,
1957                 &default_context_);
1958  EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1959
1960  scoped_refptr<URLRequestTestJob> job =
1961      new URLRequestTestJob(&req, &default_network_delegate_);
1962  AddTestInterceptor()->set_main_intercept_job(job.get());
1963
1964  req.SetLoadFlags(LOAD_IGNORE_LIMITS);
1965  EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1966
1967  req.SetPriority(MAXIMUM_PRIORITY);
1968  EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1969
1970  req.Start();
1971  EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1972  EXPECT_EQ(MAXIMUM_PRIORITY, job->priority());
1973}
1974
1975// TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
1976#if !defined(OS_IOS)
1977// A subclass of SpawnedTestServer that uses a statically-configured hostname.
1978// This is to work around mysterious failures in chrome_frame_net_tests. See:
1979// http://crbug.com/114369
1980// TODO(erikwright): remove or update as needed; see http://crbug.com/334634.
1981class LocalHttpTestServer : public SpawnedTestServer {
1982 public:
1983  explicit LocalHttpTestServer(const base::FilePath& document_root)
1984      : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1985                          ScopedCustomUrlRequestTestHttpHost::value(),
1986                          document_root) {}
1987  LocalHttpTestServer()
1988      : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1989                          ScopedCustomUrlRequestTestHttpHost::value(),
1990                          base::FilePath()) {}
1991};
1992
1993TEST_F(URLRequestTest, DelayedCookieCallback) {
1994  LocalHttpTestServer test_server;
1995  ASSERT_TRUE(test_server.Start());
1996
1997  TestURLRequestContext context;
1998  scoped_refptr<DelayedCookieMonster> delayed_cm =
1999      new DelayedCookieMonster();
2000  scoped_refptr<CookieStore> cookie_store = delayed_cm;
2001  context.set_cookie_store(delayed_cm.get());
2002
2003  // Set up a cookie.
2004  {
2005    TestNetworkDelegate network_delegate;
2006    context.set_network_delegate(&network_delegate);
2007    TestDelegate d;
2008    URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2009                   DEFAULT_PRIORITY,
2010                   &d,
2011                   &context);
2012    req.Start();
2013    base::RunLoop().Run();
2014    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2015    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2016    EXPECT_EQ(1, network_delegate.set_cookie_count());
2017  }
2018
2019  // Verify that the cookie is set.
2020  {
2021    TestNetworkDelegate network_delegate;
2022    context.set_network_delegate(&network_delegate);
2023    TestDelegate d;
2024    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2025                   DEFAULT_PRIORITY,
2026                   &d,
2027                   &context);
2028    req.Start();
2029    base::RunLoop().Run();
2030
2031    EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2032                != std::string::npos);
2033    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2034    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2035  }
2036}
2037
2038TEST_F(URLRequestTest, DoNotSendCookies) {
2039  LocalHttpTestServer test_server;
2040  ASSERT_TRUE(test_server.Start());
2041
2042  // Set up a cookie.
2043  {
2044    TestNetworkDelegate network_delegate;
2045    default_context_.set_network_delegate(&network_delegate);
2046    TestDelegate d;
2047    URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2048                   DEFAULT_PRIORITY,
2049                   &d,
2050                   &default_context_);
2051    req.Start();
2052    base::RunLoop().Run();
2053    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2054    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2055  }
2056
2057  // Verify that the cookie is set.
2058  {
2059    TestNetworkDelegate network_delegate;
2060    default_context_.set_network_delegate(&network_delegate);
2061    TestDelegate d;
2062    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2063                   DEFAULT_PRIORITY,
2064                   &d,
2065                   &default_context_);
2066    req.Start();
2067    base::RunLoop().Run();
2068
2069    EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2070                != std::string::npos);
2071    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2072    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2073  }
2074
2075  // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2076  {
2077    TestNetworkDelegate network_delegate;
2078    default_context_.set_network_delegate(&network_delegate);
2079    TestDelegate d;
2080    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2081                   DEFAULT_PRIORITY,
2082                   &d,
2083                   &default_context_);
2084    req.SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES);
2085    req.Start();
2086    base::RunLoop().Run();
2087
2088    EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2089                == std::string::npos);
2090
2091    // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
2092    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2093    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2094  }
2095}
2096
2097TEST_F(URLRequestTest, DoNotSaveCookies) {
2098  LocalHttpTestServer test_server;
2099  ASSERT_TRUE(test_server.Start());
2100
2101  // Set up a cookie.
2102  {
2103    TestNetworkDelegate network_delegate;
2104    default_context_.set_network_delegate(&network_delegate);
2105    TestDelegate d;
2106    URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2107                   DEFAULT_PRIORITY,
2108                   &d,
2109                   &default_context_);
2110    req.Start();
2111    base::RunLoop().Run();
2112
2113    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2114    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2115    EXPECT_EQ(1, network_delegate.set_cookie_count());
2116  }
2117
2118  // Try to set-up another cookie and update the previous cookie.
2119  {
2120    TestNetworkDelegate network_delegate;
2121    default_context_.set_network_delegate(&network_delegate);
2122    TestDelegate d;
2123    URLRequest req(
2124        test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2125        DEFAULT_PRIORITY,
2126        &d,
2127        &default_context_);
2128    req.SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES);
2129    req.Start();
2130
2131    base::RunLoop().Run();
2132
2133    // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
2134    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2135    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2136    EXPECT_EQ(0, network_delegate.set_cookie_count());
2137  }
2138
2139  // Verify the cookies weren't saved or updated.
2140  {
2141    TestNetworkDelegate network_delegate;
2142    default_context_.set_network_delegate(&network_delegate);
2143    TestDelegate d;
2144    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2145                   DEFAULT_PRIORITY,
2146                   &d,
2147                   &default_context_);
2148    req.Start();
2149    base::RunLoop().Run();
2150
2151    EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2152                == std::string::npos);
2153    EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2154                != std::string::npos);
2155
2156    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2157    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2158    EXPECT_EQ(0, network_delegate.set_cookie_count());
2159  }
2160}
2161
2162TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
2163  LocalHttpTestServer test_server;
2164  ASSERT_TRUE(test_server.Start());
2165
2166  // Set up a cookie.
2167  {
2168    TestNetworkDelegate network_delegate;
2169    default_context_.set_network_delegate(&network_delegate);
2170    TestDelegate d;
2171    URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2172                   DEFAULT_PRIORITY,
2173                   &d,
2174                   &default_context_);
2175    req.Start();
2176    base::RunLoop().Run();
2177
2178    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2179    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2180  }
2181
2182  // Verify that the cookie is set.
2183  {
2184    TestNetworkDelegate network_delegate;
2185    default_context_.set_network_delegate(&network_delegate);
2186    TestDelegate d;
2187    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2188                   DEFAULT_PRIORITY,
2189                   &d,
2190                   &default_context_);
2191    req.Start();
2192    base::RunLoop().Run();
2193
2194    EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2195                != std::string::npos);
2196
2197    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2198    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2199  }
2200
2201  // Verify that the cookie isn't sent.
2202  {
2203    TestNetworkDelegate network_delegate;
2204    default_context_.set_network_delegate(&network_delegate);
2205    TestDelegate d;
2206    network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
2207    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2208                   DEFAULT_PRIORITY,
2209                   &d,
2210                   &default_context_);
2211    req.Start();
2212    base::RunLoop().Run();
2213
2214    EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2215                == std::string::npos);
2216
2217    EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2218    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2219  }
2220}
2221
2222TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
2223  LocalHttpTestServer test_server;
2224  ASSERT_TRUE(test_server.Start());
2225
2226  // Set up a cookie.
2227  {
2228    TestNetworkDelegate network_delegate;
2229    default_context_.set_network_delegate(&network_delegate);
2230    TestDelegate d;
2231    URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2232                   DEFAULT_PRIORITY,
2233                   &d,
2234                   &default_context_);
2235    req.Start();
2236    base::RunLoop().Run();
2237
2238    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2239    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2240  }
2241
2242  // Try to set-up another cookie and update the previous cookie.
2243  {
2244    TestNetworkDelegate network_delegate;
2245    default_context_.set_network_delegate(&network_delegate);
2246    TestDelegate d;
2247    network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2248    URLRequest req(
2249        test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2250        DEFAULT_PRIORITY,
2251        &d,
2252        &default_context_);
2253    req.Start();
2254
2255    base::RunLoop().Run();
2256
2257    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2258    EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2259  }
2260
2261  // Verify the cookies weren't saved or updated.
2262  {
2263    TestNetworkDelegate network_delegate;
2264    default_context_.set_network_delegate(&network_delegate);
2265    TestDelegate d;
2266    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2267                   DEFAULT_PRIORITY,
2268                   &d,
2269                   &default_context_);
2270    req.Start();
2271    base::RunLoop().Run();
2272
2273    EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2274                == std::string::npos);
2275    EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2276                != std::string::npos);
2277
2278    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2279    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2280  }
2281}
2282
2283TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
2284  LocalHttpTestServer test_server;
2285  ASSERT_TRUE(test_server.Start());
2286
2287  // Set up an empty cookie.
2288  {
2289    TestNetworkDelegate network_delegate;
2290    default_context_.set_network_delegate(&network_delegate);
2291    TestDelegate d;
2292    URLRequest req(test_server.GetURL("set-cookie"),
2293                   DEFAULT_PRIORITY,
2294                   &d,
2295                   &default_context_);
2296    req.Start();
2297    base::RunLoop().Run();
2298
2299    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2300    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2301    EXPECT_EQ(0, network_delegate.set_cookie_count());
2302  }
2303}
2304
2305TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
2306  LocalHttpTestServer test_server;
2307  ASSERT_TRUE(test_server.Start());
2308
2309  // Set up a cookie.
2310  {
2311    TestNetworkDelegate network_delegate;
2312    default_context_.set_network_delegate(&network_delegate);
2313    TestDelegate d;
2314    URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2315                   DEFAULT_PRIORITY,
2316                   &d,
2317                   &default_context_);
2318    req.Start();
2319    base::RunLoop().Run();
2320
2321    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2322    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2323  }
2324
2325  // Verify that the cookie is set.
2326  {
2327    TestNetworkDelegate network_delegate;
2328    default_context_.set_network_delegate(&network_delegate);
2329    TestDelegate d;
2330    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2331                   DEFAULT_PRIORITY,
2332                   &d,
2333                   &default_context_);
2334    req.Start();
2335    base::RunLoop().Run();
2336
2337    EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2338                != std::string::npos);
2339
2340    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2341    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2342  }
2343
2344  // Verify that the cookie isn't sent.
2345  {
2346    TestNetworkDelegate network_delegate;
2347    default_context_.set_network_delegate(&network_delegate);
2348    TestDelegate d;
2349    network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
2350    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2351                   DEFAULT_PRIORITY,
2352                   &d,
2353                   &default_context_);
2354    req.Start();
2355    base::RunLoop().Run();
2356
2357    EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2358                == std::string::npos);
2359
2360    EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2361    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2362  }
2363}
2364
2365TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
2366  LocalHttpTestServer test_server;
2367  ASSERT_TRUE(test_server.Start());
2368
2369  // Set up a cookie.
2370  {
2371    TestNetworkDelegate network_delegate;
2372    default_context_.set_network_delegate(&network_delegate);
2373    TestDelegate d;
2374    URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2375                   DEFAULT_PRIORITY,
2376                   &d,
2377                   &default_context_);
2378    req.Start();
2379    base::RunLoop().Run();
2380
2381    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2382    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2383  }
2384
2385  // Try to set-up another cookie and update the previous cookie.
2386  {
2387    TestNetworkDelegate network_delegate;
2388    default_context_.set_network_delegate(&network_delegate);
2389    TestDelegate d;
2390    network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2391    URLRequest req(
2392        test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2393        DEFAULT_PRIORITY,
2394        &d,
2395        &default_context_);
2396    req.Start();
2397
2398    base::RunLoop().Run();
2399
2400    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2401    EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2402  }
2403
2404  // Verify the cookies weren't saved or updated.
2405  {
2406    TestNetworkDelegate network_delegate;
2407    default_context_.set_network_delegate(&network_delegate);
2408    TestDelegate d;
2409    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2410                   DEFAULT_PRIORITY,
2411                   &d,
2412                   &default_context_);
2413    req.Start();
2414    base::RunLoop().Run();
2415
2416    EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2417                == std::string::npos);
2418    EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2419                != std::string::npos);
2420
2421    EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2422    EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2423  }
2424}
2425
2426// FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2427// value for the |fixed_date| argument given to the constructor.
2428class FixedDateNetworkDelegate : public TestNetworkDelegate {
2429 public:
2430  explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2431      : fixed_date_(fixed_date) {}
2432  virtual ~FixedDateNetworkDelegate() {}
2433
2434  // net::NetworkDelegate implementation
2435  virtual int OnHeadersReceived(
2436      net::URLRequest* request,
2437      const net::CompletionCallback& callback,
2438      const net::HttpResponseHeaders* original_response_headers,
2439      scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
2440      GURL* allowed_unsafe_redirect_url) OVERRIDE;
2441
2442 private:
2443  std::string fixed_date_;
2444
2445  DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2446};
2447
2448int FixedDateNetworkDelegate::OnHeadersReceived(
2449    net::URLRequest* request,
2450    const net::CompletionCallback& callback,
2451    const net::HttpResponseHeaders* original_response_headers,
2452    scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
2453    GURL* allowed_unsafe_redirect_url) {
2454  net::HttpResponseHeaders* new_response_headers =
2455      new net::HttpResponseHeaders(original_response_headers->raw_headers());
2456
2457  new_response_headers->RemoveHeader("Date");
2458  new_response_headers->AddHeader("Date: " + fixed_date_);
2459
2460  *override_response_headers = new_response_headers;
2461  return TestNetworkDelegate::OnHeadersReceived(request,
2462                                                callback,
2463                                                original_response_headers,
2464                                                override_response_headers,
2465                                                allowed_unsafe_redirect_url);
2466}
2467
2468// Test that cookie expiration times are adjusted for server/client clock
2469// skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2470// headers by defaulting to GMT. (crbug.com/135131)
2471TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2472  LocalHttpTestServer test_server;
2473  ASSERT_TRUE(test_server.Start());
2474
2475  // Set up an expired cookie.
2476  {
2477    TestNetworkDelegate network_delegate;
2478    default_context_.set_network_delegate(&network_delegate);
2479    TestDelegate d;
2480    URLRequest req(
2481        test_server.GetURL(
2482            "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2483        DEFAULT_PRIORITY,
2484        &d,
2485        &default_context_);
2486    req.Start();
2487    base::RunLoop().Run();
2488  }
2489  // Verify that the cookie is not set.
2490  {
2491    TestNetworkDelegate network_delegate;
2492    default_context_.set_network_delegate(&network_delegate);
2493    TestDelegate d;
2494    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2495                   DEFAULT_PRIORITY,
2496                   &d,
2497                   &default_context_);
2498    req.Start();
2499    base::RunLoop().Run();
2500
2501    EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
2502  }
2503  // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2504  {
2505    FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
2506    default_context_.set_network_delegate(&network_delegate);
2507    TestDelegate d;
2508    URLRequest req(
2509        test_server.GetURL(
2510            "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2511        DEFAULT_PRIORITY,
2512        &d,
2513        &default_context_);
2514    req.Start();
2515    base::RunLoop().Run();
2516  }
2517  // Verify that the cookie is set.
2518  {
2519    TestNetworkDelegate network_delegate;
2520    default_context_.set_network_delegate(&network_delegate);
2521    TestDelegate d;
2522    URLRequest req(test_server.GetURL("echoheader?Cookie"),
2523                   DEFAULT_PRIORITY,
2524                   &d,
2525                   &default_context_);
2526    req.Start();
2527    base::RunLoop().Run();
2528
2529    EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
2530  }
2531}
2532
2533
2534// Check that it is impossible to change the referrer in the extra headers of
2535// an URLRequest.
2536TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2537  LocalHttpTestServer test_server;
2538  ASSERT_TRUE(test_server.Start());
2539
2540  // If extra headers contain referer and the request contains a referer,
2541  // only the latter shall be respected.
2542  {
2543    TestDelegate d;
2544    URLRequest req(test_server.GetURL("echoheader?Referer"),
2545                   DEFAULT_PRIORITY,
2546                   &d,
2547                   &default_context_);
2548    req.SetReferrer("http://foo.com/");
2549
2550    HttpRequestHeaders headers;
2551    headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2552    req.SetExtraRequestHeaders(headers);
2553
2554    req.Start();
2555    base::RunLoop().Run();
2556
2557    EXPECT_EQ("http://foo.com/", d.data_received());
2558  }
2559
2560  // If extra headers contain a referer but the request does not, no referer
2561  // shall be sent in the header.
2562  {
2563    TestDelegate d;
2564    URLRequest req(test_server.GetURL("echoheader?Referer"),
2565                   DEFAULT_PRIORITY,
2566                   &d,
2567                   &default_context_);
2568
2569    HttpRequestHeaders headers;
2570    headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2571    req.SetExtraRequestHeaders(headers);
2572    req.SetLoadFlags(LOAD_VALIDATE_CACHE);
2573
2574    req.Start();
2575    base::RunLoop().Run();
2576
2577    EXPECT_EQ("None", d.data_received());
2578  }
2579}
2580
2581class URLRequestTestHTTP : public URLRequestTest {
2582 public:
2583  URLRequestTestHTTP()
2584      : test_server_(base::FilePath(FILE_PATH_LITERAL(
2585                                  "net/data/url_request_unittest"))) {
2586  }
2587
2588 protected:
2589  // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2590  // |request_method| is the method to use for the initial request.
2591  // |redirect_method| is the method that is expected to be used for the second
2592  // request, after redirection.
2593  // If |include_data| is true, data is uploaded with the request.  The
2594  // response body is expected to match it exactly, if and only if
2595  // |request_method| == |redirect_method|.
2596  void HTTPRedirectMethodTest(const GURL& redirect_url,
2597                              const std::string& request_method,
2598                              const std::string& redirect_method,
2599                              bool include_data) {
2600    static const char kData[] = "hello world";
2601    TestDelegate d;
2602    URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_);
2603    req.set_method(request_method);
2604    if (include_data) {
2605      req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
2606      HttpRequestHeaders headers;
2607      headers.SetHeader(HttpRequestHeaders::kContentLength,
2608                        base::UintToString(arraysize(kData) - 1));
2609      req.SetExtraRequestHeaders(headers);
2610    }
2611    req.Start();
2612    base::RunLoop().Run();
2613    EXPECT_EQ(redirect_method, req.method());
2614    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
2615    EXPECT_EQ(OK, req.status().error());
2616    if (include_data) {
2617      if (request_method == redirect_method) {
2618        EXPECT_EQ(kData, d.data_received());
2619      } else {
2620        EXPECT_NE(kData, d.data_received());
2621      }
2622    }
2623    if (HasFailure())
2624      LOG(WARNING) << "Request method was: " << request_method;
2625  }
2626
2627  void HTTPUploadDataOperationTest(const std::string& method) {
2628    const int kMsgSize = 20000;  // multiple of 10
2629    const int kIterations = 50;
2630    char* uploadBytes = new char[kMsgSize+1];
2631    char* ptr = uploadBytes;
2632    char marker = 'a';
2633    for (int idx = 0; idx < kMsgSize/10; idx++) {
2634      memcpy(ptr, "----------", 10);
2635      ptr += 10;
2636      if (idx % 100 == 0) {
2637        ptr--;
2638        *ptr++ = marker;
2639        if (++marker > 'z')
2640          marker = 'a';
2641      }
2642    }
2643    uploadBytes[kMsgSize] = '\0';
2644
2645    for (int i = 0; i < kIterations; ++i) {
2646      TestDelegate d;
2647      URLRequest r(
2648          test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
2649      r.set_method(method.c_str());
2650
2651      r.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes)));
2652
2653      r.Start();
2654      EXPECT_TRUE(r.is_pending());
2655
2656      base::RunLoop().Run();
2657
2658      ASSERT_EQ(1, d.response_started_count())
2659          << "request failed: " << r.status().status()
2660          << ", os error: " << r.status().error();
2661
2662      EXPECT_FALSE(d.received_data_before_response());
2663      EXPECT_EQ(uploadBytes, d.data_received());
2664    }
2665    delete[] uploadBytes;
2666  }
2667
2668  void AddChunksToUpload(URLRequest* r) {
2669    r->AppendChunkToUpload("a", 1, false);
2670    r->AppendChunkToUpload("bcd", 3, false);
2671    r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2672    r->AppendChunkToUpload("\r\n\r\n", 4, false);
2673    r->AppendChunkToUpload("0", 1, false);
2674    r->AppendChunkToUpload("2323", 4, true);
2675  }
2676
2677  void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
2678    // This should match the chunks sent by AddChunksToUpload().
2679    const std::string expected_data =
2680        "abcdthis is a longer chunk than before.\r\n\r\n02323";
2681
2682    ASSERT_EQ(1, d->response_started_count())
2683        << "request failed: " << r->status().status()
2684        << ", os error: " << r->status().error();
2685
2686    EXPECT_FALSE(d->received_data_before_response());
2687
2688    EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2689    EXPECT_EQ(expected_data, d->data_received());
2690  }
2691
2692  bool DoManyCookiesRequest(int num_cookies) {
2693    TestDelegate d;
2694    URLRequest r(test_server_.GetURL("set-many-cookies?" +
2695                                     base::IntToString(num_cookies)),
2696                 DEFAULT_PRIORITY,
2697                 &d,
2698                 &default_context_);
2699
2700    r.Start();
2701    EXPECT_TRUE(r.is_pending());
2702
2703    base::RunLoop().Run();
2704
2705    bool is_success = r.status().is_success();
2706
2707    if (!is_success) {
2708      EXPECT_TRUE(r.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG);
2709      // The test server appears to be unable to handle subsequent requests
2710      // after this error is triggered. Force it to restart.
2711      EXPECT_TRUE(test_server_.Stop());
2712      EXPECT_TRUE(test_server_.Start());
2713    }
2714
2715    return is_success;
2716  }
2717
2718  LocalHttpTestServer test_server_;
2719};
2720
2721// In this unit test, we're using the HTTPTestServer as a proxy server and
2722// issuing a CONNECT request with the magic host name "www.redirect.com".
2723// The HTTPTestServer will return a 302 response, which we should not
2724// follow.
2725TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
2726  ASSERT_TRUE(test_server_.Start());
2727
2728  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
2729  TestURLRequestContextWithProxy context(
2730      test_server_.host_port_pair().ToString(), &network_delegate);
2731
2732  TestDelegate d;
2733  {
2734    URLRequest r(
2735        GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context);
2736    r.Start();
2737    EXPECT_TRUE(r.is_pending());
2738
2739    base::RunLoop().Run();
2740
2741    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2742    // The proxy server is not set before failure.
2743    EXPECT_TRUE(r.proxy_server().IsEmpty());
2744    EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
2745    EXPECT_EQ(1, d.response_started_count());
2746    // We should not have followed the redirect.
2747    EXPECT_EQ(0, d.received_redirect_count());
2748  }
2749}
2750
2751// This is the same as the previous test, but checks that the network delegate
2752// registers the error.
2753TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
2754  ASSERT_TRUE(test_server_.Start());
2755
2756  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
2757  TestURLRequestContextWithProxy context(
2758      test_server_.host_port_pair().ToString(), &network_delegate);
2759
2760  TestDelegate d;
2761  {
2762    URLRequest r(
2763        GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context);
2764    r.Start();
2765    EXPECT_TRUE(r.is_pending());
2766
2767    base::RunLoop().Run();
2768
2769    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2770    // The proxy server is not set before failure.
2771    EXPECT_TRUE(r.proxy_server().IsEmpty());
2772    EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
2773    EXPECT_EQ(1, d.response_started_count());
2774    // We should not have followed the redirect.
2775    EXPECT_EQ(0, d.received_redirect_count());
2776
2777    EXPECT_EQ(1, network_delegate.error_count());
2778    EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
2779  }
2780}
2781
2782// Tests that we can block and asynchronously return OK in various stages.
2783TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
2784  static const BlockingNetworkDelegate::Stage blocking_stages[] = {
2785    BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2786    BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2787    BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2788  };
2789  static const size_t blocking_stages_length = arraysize(blocking_stages);
2790
2791  ASSERT_TRUE(test_server_.Start());
2792
2793  TestDelegate d;
2794  BlockingNetworkDelegate network_delegate(
2795      BlockingNetworkDelegate::USER_CALLBACK);
2796  network_delegate.set_block_on(
2797      BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
2798      BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
2799      BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2800
2801  TestURLRequestContext context(true);
2802  context.set_network_delegate(&network_delegate);
2803  context.Init();
2804
2805  {
2806    URLRequest r(
2807        test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, &context);
2808
2809    r.Start();
2810    for (size_t i = 0; i < blocking_stages_length; ++i) {
2811      base::RunLoop().Run();
2812      EXPECT_EQ(blocking_stages[i],
2813                network_delegate.stage_blocked_for_callback());
2814      network_delegate.DoCallback(OK);
2815    }
2816    base::RunLoop().Run();
2817    EXPECT_EQ(200, r.GetResponseCode());
2818    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2819    EXPECT_EQ(1, network_delegate.created_requests());
2820    EXPECT_EQ(0, network_delegate.destroyed_requests());
2821  }
2822  EXPECT_EQ(1, network_delegate.destroyed_requests());
2823}
2824
2825// Tests that the network delegate can block and cancel a request.
2826TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
2827  ASSERT_TRUE(test_server_.Start());
2828
2829  TestDelegate d;
2830  BlockingNetworkDelegate network_delegate(
2831      BlockingNetworkDelegate::AUTO_CALLBACK);
2832  network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2833  network_delegate.set_retval(ERR_EMPTY_RESPONSE);
2834
2835  TestURLRequestContextWithProxy context(
2836      test_server_.host_port_pair().ToString(), &network_delegate);
2837
2838  {
2839    URLRequest r(
2840        test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
2841
2842    r.Start();
2843    base::RunLoop().Run();
2844
2845    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2846    // The proxy server is not set before cancellation.
2847    EXPECT_TRUE(r.proxy_server().IsEmpty());
2848    EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
2849    EXPECT_EQ(1, network_delegate.created_requests());
2850    EXPECT_EQ(0, network_delegate.destroyed_requests());
2851  }
2852  EXPECT_EQ(1, network_delegate.destroyed_requests());
2853}
2854
2855// Helper function for NetworkDelegateCancelRequestAsynchronously and
2856// NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
2857// delegate operating in |block_mode| and a request for |url|. It blocks the
2858// request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
2859void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
2860                                  BlockingNetworkDelegate::Stage stage,
2861                                  const GURL& url) {
2862  TestDelegate d;
2863  BlockingNetworkDelegate network_delegate(block_mode);
2864  network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
2865  network_delegate.set_block_on(stage);
2866
2867  TestURLRequestContext context(true);
2868  context.set_network_delegate(&network_delegate);
2869  context.Init();
2870
2871  {
2872    URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
2873
2874    r.Start();
2875    base::RunLoop().Run();
2876
2877    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2878    // The proxy server is not set before cancellation.
2879    EXPECT_TRUE(r.proxy_server().IsEmpty());
2880    EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r.status().error());
2881    EXPECT_EQ(1, network_delegate.created_requests());
2882    EXPECT_EQ(0, network_delegate.destroyed_requests());
2883  }
2884  EXPECT_EQ(1, network_delegate.destroyed_requests());
2885}
2886
2887// The following 3 tests check that the network delegate can cancel a request
2888// synchronously in various stages of the request.
2889TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
2890  ASSERT_TRUE(test_server_.Start());
2891  NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2892                               BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2893                               test_server_.GetURL(std::string()));
2894}
2895
2896TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
2897  ASSERT_TRUE(test_server_.Start());
2898  NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2899                               BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2900                               test_server_.GetURL(std::string()));
2901}
2902
2903TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
2904  ASSERT_TRUE(test_server_.Start());
2905  NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2906                               BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2907                               test_server_.GetURL(std::string()));
2908}
2909
2910// The following 3 tests check that the network delegate can cancel a request
2911// asynchronously in various stages of the request.
2912TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
2913  ASSERT_TRUE(test_server_.Start());
2914  NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2915                               BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2916                               test_server_.GetURL(std::string()));
2917}
2918
2919TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
2920  ASSERT_TRUE(test_server_.Start());
2921  NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2922                               BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2923                               test_server_.GetURL(std::string()));
2924}
2925
2926TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
2927  ASSERT_TRUE(test_server_.Start());
2928  NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2929                               BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2930                               test_server_.GetURL(std::string()));
2931}
2932
2933// Tests that the network delegate can block and redirect a request to a new
2934// URL.
2935TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
2936  ASSERT_TRUE(test_server_.Start());
2937
2938  TestDelegate d;
2939  BlockingNetworkDelegate network_delegate(
2940      BlockingNetworkDelegate::AUTO_CALLBACK);
2941  network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2942  GURL redirect_url(test_server_.GetURL("simple.html"));
2943  network_delegate.set_redirect_url(redirect_url);
2944
2945  TestURLRequestContextWithProxy context(
2946      test_server_.host_port_pair().ToString(), &network_delegate);
2947
2948  {
2949    GURL original_url(test_server_.GetURL("empty.html"));
2950    URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
2951
2952    // Quit after hitting the redirect, so can check the headers.
2953    d.set_quit_on_redirect(true);
2954    r.Start();
2955    base::RunLoop().Run();
2956
2957    // Check headers from URLRequestJob.
2958    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2959    EXPECT_EQ(307, r.GetResponseCode());
2960    EXPECT_EQ(307, r.response_headers()->response_code());
2961    std::string location;
2962    ASSERT_TRUE(r.response_headers()->EnumerateHeader(NULL, "Location",
2963                                                      &location));
2964    EXPECT_EQ(redirect_url, GURL(location));
2965
2966    // Let the request finish.
2967    r.FollowDeferredRedirect();
2968    base::RunLoop().Run();
2969    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2970    EXPECT_TRUE(r.proxy_server().Equals(test_server_.host_port_pair()));
2971    EXPECT_EQ(
2972        1, network_delegate.observed_before_proxy_headers_sent_callbacks());
2973    EXPECT_TRUE(
2974        network_delegate.last_observed_proxy().Equals(
2975            test_server_.host_port_pair()));
2976
2977    EXPECT_EQ(0, r.status().error());
2978    EXPECT_EQ(redirect_url, r.url());
2979    EXPECT_EQ(original_url, r.original_url());
2980    EXPECT_EQ(2U, r.url_chain().size());
2981    EXPECT_EQ(1, network_delegate.created_requests());
2982    EXPECT_EQ(0, network_delegate.destroyed_requests());
2983  }
2984  EXPECT_EQ(1, network_delegate.destroyed_requests());
2985}
2986
2987// Tests that the network delegate can block and redirect a request to a new
2988// URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2989TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
2990  ASSERT_TRUE(test_server_.Start());
2991
2992  TestDelegate d;
2993  BlockingNetworkDelegate network_delegate(
2994      BlockingNetworkDelegate::SYNCHRONOUS);
2995  GURL redirect_url(test_server_.GetURL("simple.html"));
2996  network_delegate.set_redirect_url(redirect_url);
2997
2998  TestURLRequestContextWithProxy context(
2999      test_server_.host_port_pair().ToString(), &network_delegate);
3000
3001  {
3002    GURL original_url(test_server_.GetURL("empty.html"));
3003    URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
3004
3005    // Quit after hitting the redirect, so can check the headers.
3006    d.set_quit_on_redirect(true);
3007    r.Start();
3008    base::RunLoop().Run();
3009
3010    // Check headers from URLRequestJob.
3011    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3012    EXPECT_EQ(307, r.GetResponseCode());
3013    EXPECT_EQ(307, r.response_headers()->response_code());
3014    std::string location;
3015    ASSERT_TRUE(r.response_headers()->EnumerateHeader(NULL, "Location",
3016                                                      &location));
3017    EXPECT_EQ(redirect_url, GURL(location));
3018
3019    // Let the request finish.
3020    r.FollowDeferredRedirect();
3021    base::RunLoop().Run();
3022
3023    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3024    EXPECT_TRUE(r.proxy_server().Equals(test_server_.host_port_pair()));
3025    EXPECT_EQ(
3026        1, network_delegate.observed_before_proxy_headers_sent_callbacks());
3027    EXPECT_TRUE(
3028        network_delegate.last_observed_proxy().Equals(
3029            test_server_.host_port_pair()));
3030    EXPECT_EQ(0, r.status().error());
3031    EXPECT_EQ(redirect_url, r.url());
3032    EXPECT_EQ(original_url, r.original_url());
3033    EXPECT_EQ(2U, r.url_chain().size());
3034    EXPECT_EQ(1, network_delegate.created_requests());
3035    EXPECT_EQ(0, network_delegate.destroyed_requests());
3036  }
3037  EXPECT_EQ(1, network_delegate.destroyed_requests());
3038}
3039
3040// Tests that redirects caused by the network delegate preserve POST data.
3041TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
3042  ASSERT_TRUE(test_server_.Start());
3043
3044  const char kData[] = "hello world";
3045
3046  TestDelegate d;
3047  BlockingNetworkDelegate network_delegate(
3048      BlockingNetworkDelegate::AUTO_CALLBACK);
3049  network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3050  GURL redirect_url(test_server_.GetURL("echo"));
3051  network_delegate.set_redirect_url(redirect_url);
3052
3053  TestURLRequestContext context(true);
3054  context.set_network_delegate(&network_delegate);
3055  context.Init();
3056
3057  {
3058    GURL original_url(test_server_.GetURL("empty.html"));
3059    URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
3060    r.set_method("POST");
3061    r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
3062    HttpRequestHeaders headers;
3063    headers.SetHeader(HttpRequestHeaders::kContentLength,
3064                      base::UintToString(arraysize(kData) - 1));
3065    r.SetExtraRequestHeaders(headers);
3066
3067    // Quit after hitting the redirect, so can check the headers.
3068    d.set_quit_on_redirect(true);
3069    r.Start();
3070    base::RunLoop().Run();
3071
3072    // Check headers from URLRequestJob.
3073    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3074    EXPECT_EQ(307, r.GetResponseCode());
3075    EXPECT_EQ(307, r.response_headers()->response_code());
3076    std::string location;
3077    ASSERT_TRUE(r.response_headers()->EnumerateHeader(NULL, "Location",
3078                                                      &location));
3079    EXPECT_EQ(redirect_url, GURL(location));
3080
3081    // Let the request finish.
3082    r.FollowDeferredRedirect();
3083    base::RunLoop().Run();
3084
3085    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3086    EXPECT_EQ(0, r.status().error());
3087    EXPECT_EQ(redirect_url, r.url());
3088    EXPECT_EQ(original_url, r.original_url());
3089    EXPECT_EQ(2U, r.url_chain().size());
3090    EXPECT_EQ(1, network_delegate.created_requests());
3091    EXPECT_EQ(0, network_delegate.destroyed_requests());
3092    EXPECT_EQ("POST", r.method());
3093    EXPECT_EQ(kData, d.data_received());
3094  }
3095  EXPECT_EQ(1, network_delegate.destroyed_requests());
3096}
3097
3098// Tests that the network delegate can block and redirect a request to a new
3099// URL during OnHeadersReceived.
3100TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) {
3101  ASSERT_TRUE(test_server_.Start());
3102
3103  TestDelegate d;
3104  BlockingNetworkDelegate network_delegate(
3105      BlockingNetworkDelegate::AUTO_CALLBACK);
3106  network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3107  GURL redirect_url(test_server_.GetURL("simple.html"));
3108  network_delegate.set_redirect_on_headers_received_url(redirect_url);
3109
3110  TestURLRequestContextWithProxy context(
3111      test_server_.host_port_pair().ToString(), &network_delegate);
3112
3113  {
3114    GURL original_url(test_server_.GetURL("empty.html"));
3115    URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
3116
3117    r.Start();
3118    base::RunLoop().Run();
3119
3120    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3121    EXPECT_TRUE(r.proxy_server().Equals(test_server_.host_port_pair()));
3122    EXPECT_EQ(
3123        2, network_delegate.observed_before_proxy_headers_sent_callbacks());
3124    EXPECT_TRUE(
3125        network_delegate.last_observed_proxy().Equals(
3126            test_server_.host_port_pair()));
3127    EXPECT_EQ(net::OK, r.status().error());
3128    EXPECT_EQ(redirect_url, r.url());
3129    EXPECT_EQ(original_url, r.original_url());
3130    EXPECT_EQ(2U, r.url_chain().size());
3131    EXPECT_EQ(2, network_delegate.created_requests());
3132    EXPECT_EQ(0, network_delegate.destroyed_requests());
3133  }
3134  EXPECT_EQ(1, network_delegate.destroyed_requests());
3135}
3136
3137// Tests that the network delegate can synchronously complete OnAuthRequired
3138// by taking no action. This indicates that the NetworkDelegate does not want to
3139// handle the challenge, and is passing the buck along to the
3140// URLRequest::Delegate.
3141TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
3142  ASSERT_TRUE(test_server_.Start());
3143
3144  TestDelegate d;
3145  BlockingNetworkDelegate network_delegate(
3146      BlockingNetworkDelegate::SYNCHRONOUS);
3147
3148  TestURLRequestContext context(true);
3149  context.set_network_delegate(&network_delegate);
3150  context.Init();
3151
3152  d.set_credentials(AuthCredentials(kUser, kSecret));
3153
3154  {
3155    GURL url(test_server_.GetURL("auth-basic"));
3156    URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3157    r.Start();
3158
3159    base::RunLoop().Run();
3160
3161    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3162    EXPECT_EQ(0, r.status().error());
3163    EXPECT_EQ(200, r.GetResponseCode());
3164    EXPECT_TRUE(d.auth_required_called());
3165    EXPECT_EQ(1, network_delegate.created_requests());
3166    EXPECT_EQ(0, network_delegate.destroyed_requests());
3167  }
3168  EXPECT_EQ(1, network_delegate.destroyed_requests());
3169}
3170
3171TEST_F(URLRequestTestHTTP,
3172    NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) {
3173  ASSERT_TRUE(test_server_.Start());
3174
3175  TestDelegate d;
3176  BlockingNetworkDelegate network_delegate(
3177      BlockingNetworkDelegate::SYNCHRONOUS);
3178
3179  TestURLRequestContext context(true);
3180  context.set_network_delegate(&network_delegate);
3181  context.Init();
3182
3183  d.set_credentials(AuthCredentials(kUser, kSecret));
3184
3185  {
3186    GURL url(test_server_.GetURL("auth-basic"));
3187    URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3188    r.Start();
3189
3190    {
3191      HttpRequestHeaders headers;
3192      EXPECT_TRUE(r.GetFullRequestHeaders(&headers));
3193      EXPECT_FALSE(headers.HasHeader("Authorization"));
3194    }
3195
3196    base::RunLoop().Run();
3197
3198    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3199    EXPECT_EQ(0, r.status().error());
3200    EXPECT_EQ(200, r.GetResponseCode());
3201    EXPECT_TRUE(d.auth_required_called());
3202    EXPECT_EQ(1, network_delegate.created_requests());
3203    EXPECT_EQ(0, network_delegate.destroyed_requests());
3204  }
3205  EXPECT_EQ(1, network_delegate.destroyed_requests());
3206}
3207
3208// Tests that the network delegate can synchronously complete OnAuthRequired
3209// by setting credentials.
3210TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
3211  ASSERT_TRUE(test_server_.Start());
3212
3213  TestDelegate d;
3214  BlockingNetworkDelegate network_delegate(
3215      BlockingNetworkDelegate::SYNCHRONOUS);
3216  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3217  network_delegate.set_auth_retval(
3218      NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3219
3220  network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3221
3222  TestURLRequestContext context(true);
3223  context.set_network_delegate(&network_delegate);
3224  context.Init();
3225
3226  {
3227    GURL url(test_server_.GetURL("auth-basic"));
3228    URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3229    r.Start();
3230    base::RunLoop().Run();
3231
3232    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3233    EXPECT_EQ(0, r.status().error());
3234    EXPECT_EQ(200, r.GetResponseCode());
3235    EXPECT_FALSE(d.auth_required_called());
3236    EXPECT_EQ(1, network_delegate.created_requests());
3237    EXPECT_EQ(0, network_delegate.destroyed_requests());
3238  }
3239  EXPECT_EQ(1, network_delegate.destroyed_requests());
3240}
3241
3242// Same as above, but also tests that GetFullRequestHeaders returns the proper
3243// headers (for the first or second request) when called at the proper times.
3244TEST_F(URLRequestTestHTTP,
3245    NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) {
3246  ASSERT_TRUE(test_server_.Start());
3247
3248  TestDelegate d;
3249  BlockingNetworkDelegate network_delegate(
3250      BlockingNetworkDelegate::SYNCHRONOUS);
3251  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3252  network_delegate.set_auth_retval(
3253      NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3254
3255  network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3256
3257  TestURLRequestContext context(true);
3258  context.set_network_delegate(&network_delegate);
3259  context.Init();
3260
3261  {
3262    GURL url(test_server_.GetURL("auth-basic"));
3263    URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3264    r.Start();
3265    base::RunLoop().Run();
3266
3267    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3268    EXPECT_EQ(0, r.status().error());
3269    EXPECT_EQ(200, r.GetResponseCode());
3270    EXPECT_FALSE(d.auth_required_called());
3271    EXPECT_EQ(1, network_delegate.created_requests());
3272    EXPECT_EQ(0, network_delegate.destroyed_requests());
3273
3274    {
3275      HttpRequestHeaders headers;
3276      EXPECT_TRUE(r.GetFullRequestHeaders(&headers));
3277      EXPECT_TRUE(headers.HasHeader("Authorization"));
3278    }
3279  }
3280  EXPECT_EQ(1, network_delegate.destroyed_requests());
3281}
3282
3283// Tests that the network delegate can synchronously complete OnAuthRequired
3284// by cancelling authentication.
3285TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
3286  ASSERT_TRUE(test_server_.Start());
3287
3288  TestDelegate d;
3289  BlockingNetworkDelegate network_delegate(
3290      BlockingNetworkDelegate::SYNCHRONOUS);
3291  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3292  network_delegate.set_auth_retval(
3293      NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3294
3295  TestURLRequestContext context(true);
3296  context.set_network_delegate(&network_delegate);
3297  context.Init();
3298
3299  {
3300    GURL url(test_server_.GetURL("auth-basic"));
3301    URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3302    r.Start();
3303    base::RunLoop().Run();
3304
3305    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3306    EXPECT_EQ(OK, r.status().error());
3307    EXPECT_EQ(401, r.GetResponseCode());
3308    EXPECT_FALSE(d.auth_required_called());
3309    EXPECT_EQ(1, network_delegate.created_requests());
3310    EXPECT_EQ(0, network_delegate.destroyed_requests());
3311  }
3312  EXPECT_EQ(1, network_delegate.destroyed_requests());
3313}
3314
3315// Tests that the network delegate can asynchronously complete OnAuthRequired
3316// by taking no action. This indicates that the NetworkDelegate does not want
3317// to handle the challenge, and is passing the buck along to the
3318// URLRequest::Delegate.
3319TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
3320  ASSERT_TRUE(test_server_.Start());
3321
3322  TestDelegate d;
3323  BlockingNetworkDelegate network_delegate(
3324      BlockingNetworkDelegate::AUTO_CALLBACK);
3325  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3326
3327  TestURLRequestContext context(true);
3328  context.set_network_delegate(&network_delegate);
3329  context.Init();
3330
3331  d.set_credentials(AuthCredentials(kUser, kSecret));
3332
3333  {
3334    GURL url(test_server_.GetURL("auth-basic"));
3335    URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3336    r.Start();
3337    base::RunLoop().Run();
3338
3339    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3340    EXPECT_EQ(0, r.status().error());
3341    EXPECT_EQ(200, r.GetResponseCode());
3342    EXPECT_TRUE(d.auth_required_called());
3343    EXPECT_EQ(1, network_delegate.created_requests());
3344    EXPECT_EQ(0, network_delegate.destroyed_requests());
3345  }
3346  EXPECT_EQ(1, network_delegate.destroyed_requests());
3347}
3348
3349// Tests that the network delegate can asynchronously complete OnAuthRequired
3350// by setting credentials.
3351TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
3352  ASSERT_TRUE(test_server_.Start());
3353
3354  TestDelegate d;
3355  BlockingNetworkDelegate network_delegate(
3356      BlockingNetworkDelegate::AUTO_CALLBACK);
3357  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3358  network_delegate.set_auth_retval(
3359      NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3360
3361  AuthCredentials auth_credentials(kUser, kSecret);
3362  network_delegate.set_auth_credentials(auth_credentials);
3363
3364  TestURLRequestContext context(true);
3365  context.set_network_delegate(&network_delegate);
3366  context.Init();
3367
3368  {
3369    GURL url(test_server_.GetURL("auth-basic"));
3370    URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3371    r.Start();
3372    base::RunLoop().Run();
3373
3374    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3375    EXPECT_EQ(0, r.status().error());
3376
3377    EXPECT_EQ(200, r.GetResponseCode());
3378    EXPECT_FALSE(d.auth_required_called());
3379    EXPECT_EQ(1, network_delegate.created_requests());
3380    EXPECT_EQ(0, network_delegate.destroyed_requests());
3381  }
3382  EXPECT_EQ(1, network_delegate.destroyed_requests());
3383}
3384
3385// Tests that the network delegate can asynchronously complete OnAuthRequired
3386// by cancelling authentication.
3387TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
3388  ASSERT_TRUE(test_server_.Start());
3389
3390  TestDelegate d;
3391  BlockingNetworkDelegate network_delegate(
3392      BlockingNetworkDelegate::AUTO_CALLBACK);
3393  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3394  network_delegate.set_auth_retval(
3395      NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3396
3397  TestURLRequestContext context(true);
3398  context.set_network_delegate(&network_delegate);
3399  context.Init();
3400
3401  {
3402    GURL url(test_server_.GetURL("auth-basic"));
3403    URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3404    r.Start();
3405    base::RunLoop().Run();
3406
3407    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3408    EXPECT_EQ(OK, r.status().error());
3409    EXPECT_EQ(401, r.GetResponseCode());
3410    EXPECT_FALSE(d.auth_required_called());
3411    EXPECT_EQ(1, network_delegate.created_requests());
3412    EXPECT_EQ(0, network_delegate.destroyed_requests());
3413  }
3414  EXPECT_EQ(1, network_delegate.destroyed_requests());
3415}
3416
3417// Tests that we can handle when a network request was canceled while we were
3418// waiting for the network delegate.
3419// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3420TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
3421  ASSERT_TRUE(test_server_.Start());
3422
3423  TestDelegate d;
3424  BlockingNetworkDelegate network_delegate(
3425      BlockingNetworkDelegate::USER_CALLBACK);
3426  network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3427
3428  TestURLRequestContext context(true);
3429  context.set_network_delegate(&network_delegate);
3430  context.Init();
3431
3432  {
3433    URLRequest r(
3434        test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
3435
3436    r.Start();
3437    base::RunLoop().Run();
3438    EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3439              network_delegate.stage_blocked_for_callback());
3440    EXPECT_EQ(0, network_delegate.completed_requests());
3441    // Cancel before callback.
3442    r.Cancel();
3443    // Ensure that network delegate is notified.
3444    EXPECT_EQ(1, network_delegate.completed_requests());
3445    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3446    EXPECT_EQ(ERR_ABORTED, r.status().error());
3447    EXPECT_EQ(1, network_delegate.created_requests());
3448    EXPECT_EQ(0, network_delegate.destroyed_requests());
3449  }
3450  EXPECT_EQ(1, network_delegate.destroyed_requests());
3451}
3452
3453// Tests that we can handle when a network request was canceled while we were
3454// waiting for the network delegate.
3455// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3456TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
3457  ASSERT_TRUE(test_server_.Start());
3458
3459  TestDelegate d;
3460  BlockingNetworkDelegate network_delegate(
3461      BlockingNetworkDelegate::USER_CALLBACK);
3462  network_delegate.set_block_on(
3463      BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
3464
3465  TestURLRequestContext context(true);
3466  context.set_network_delegate(&network_delegate);
3467  context.Init();
3468
3469  {
3470    URLRequest r(
3471        test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
3472
3473    r.Start();
3474    base::RunLoop().Run();
3475    EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3476              network_delegate.stage_blocked_for_callback());
3477    EXPECT_EQ(0, network_delegate.completed_requests());
3478    // Cancel before callback.
3479    r.Cancel();
3480    // Ensure that network delegate is notified.
3481    EXPECT_EQ(1, network_delegate.completed_requests());
3482    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3483    EXPECT_EQ(ERR_ABORTED, r.status().error());
3484    EXPECT_EQ(1, network_delegate.created_requests());
3485    EXPECT_EQ(0, network_delegate.destroyed_requests());
3486  }
3487  EXPECT_EQ(1, network_delegate.destroyed_requests());
3488}
3489
3490// Tests that we can handle when a network request was canceled while we were
3491// waiting for the network delegate.
3492// Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3493TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
3494  ASSERT_TRUE(test_server_.Start());
3495
3496  TestDelegate d;
3497  BlockingNetworkDelegate network_delegate(
3498      BlockingNetworkDelegate::USER_CALLBACK);
3499  network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3500
3501  TestURLRequestContext context(true);
3502  context.set_network_delegate(&network_delegate);
3503  context.Init();
3504
3505  {
3506    URLRequest r(
3507        test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
3508
3509    r.Start();
3510    base::RunLoop().Run();
3511    EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3512              network_delegate.stage_blocked_for_callback());
3513    EXPECT_EQ(0, network_delegate.completed_requests());
3514    // Cancel before callback.
3515    r.Cancel();
3516    // Ensure that network delegate is notified.
3517    EXPECT_EQ(1, network_delegate.completed_requests());
3518    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3519    EXPECT_EQ(ERR_ABORTED, r.status().error());
3520    EXPECT_EQ(1, network_delegate.created_requests());
3521    EXPECT_EQ(0, network_delegate.destroyed_requests());
3522  }
3523  EXPECT_EQ(1, network_delegate.destroyed_requests());
3524}
3525
3526// Tests that we can handle when a network request was canceled while we were
3527// waiting for the network delegate.
3528// Part 4: Request is cancelled while waiting for OnAuthRequired callback.
3529TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
3530  ASSERT_TRUE(test_server_.Start());
3531
3532  TestDelegate d;
3533  BlockingNetworkDelegate network_delegate(
3534      BlockingNetworkDelegate::USER_CALLBACK);
3535  network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3536
3537  TestURLRequestContext context(true);
3538  context.set_network_delegate(&network_delegate);
3539  context.Init();
3540
3541  {
3542    URLRequest r(
3543        test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, &context);
3544
3545    r.Start();
3546    base::RunLoop().Run();
3547    EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
3548              network_delegate.stage_blocked_for_callback());
3549    EXPECT_EQ(0, network_delegate.completed_requests());
3550    // Cancel before callback.
3551    r.Cancel();
3552    // Ensure that network delegate is notified.
3553    EXPECT_EQ(1, network_delegate.completed_requests());
3554    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3555    EXPECT_EQ(ERR_ABORTED, r.status().error());
3556    EXPECT_EQ(1, network_delegate.created_requests());
3557    EXPECT_EQ(0, network_delegate.destroyed_requests());
3558  }
3559  EXPECT_EQ(1, network_delegate.destroyed_requests());
3560}
3561
3562// In this unit test, we're using the HTTPTestServer as a proxy server and
3563// issuing a CONNECT request with the magic host name "www.server-auth.com".
3564// The HTTPTestServer will return a 401 response, which we should balk at.
3565TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
3566  ASSERT_TRUE(test_server_.Start());
3567
3568  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
3569  TestURLRequestContextWithProxy context(
3570      test_server_.host_port_pair().ToString(), &network_delegate);
3571
3572  TestDelegate d;
3573  {
3574    URLRequest r(
3575        GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, &context);
3576
3577    r.Start();
3578    EXPECT_TRUE(r.is_pending());
3579
3580    base::RunLoop().Run();
3581
3582    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
3583    // The proxy server is not set before failure.
3584    EXPECT_TRUE(r.proxy_server().IsEmpty());
3585    EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
3586  }
3587}
3588
3589TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
3590  ASSERT_TRUE(test_server_.Start());
3591
3592  TestDelegate d;
3593  {
3594    URLRequest r(test_server_.GetURL(std::string()),
3595                 DEFAULT_PRIORITY,
3596                 &d,
3597                 &default_context_);
3598
3599    r.Start();
3600    EXPECT_TRUE(r.is_pending());
3601
3602    base::RunLoop().Run();
3603
3604    EXPECT_EQ(1, d.response_started_count());
3605    EXPECT_FALSE(d.received_data_before_response());
3606    EXPECT_NE(0, d.bytes_received());
3607    EXPECT_EQ(test_server_.host_port_pair().host(),
3608              r.GetSocketAddress().host());
3609    EXPECT_EQ(test_server_.host_port_pair().port(),
3610              r.GetSocketAddress().port());
3611
3612    // TODO(eroman): Add back the NetLog tests...
3613  }
3614}
3615
3616// This test has the server send a large number of cookies to the client.
3617// To ensure that no number of cookies causes a crash, a galloping binary
3618// search is used to estimate that maximum number of cookies that are accepted
3619// by the browser. Beyond the maximum number, the request will fail with
3620// ERR_RESPONSE_HEADERS_TOO_BIG.
3621#if defined(OS_WIN)
3622// http://crbug.com/177916
3623#define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3624#else
3625#define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3626#endif  // defined(OS_WIN)
3627TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) {
3628  ASSERT_TRUE(test_server_.Start());
3629
3630  int lower_bound = 0;
3631  int upper_bound = 1;
3632
3633  // Double the number of cookies until the response header limits are
3634  // exceeded.
3635  while (DoManyCookiesRequest(upper_bound)) {
3636    lower_bound = upper_bound;
3637    upper_bound *= 2;
3638    ASSERT_LT(upper_bound, 1000000);
3639  }
3640
3641  int tolerance = upper_bound * 0.005;
3642  if (tolerance < 2)
3643    tolerance = 2;
3644
3645  // Perform a binary search to find the highest possible number of cookies,
3646  // within the desired tolerance.
3647  while (upper_bound - lower_bound >= tolerance) {
3648    int num_cookies = (lower_bound + upper_bound) / 2;
3649
3650    if (DoManyCookiesRequest(num_cookies))
3651      lower_bound = num_cookies;
3652    else
3653      upper_bound = num_cookies;
3654  }
3655  // Success: the test did not crash.
3656}
3657
3658TEST_F(URLRequestTestHTTP, GetTest) {
3659  ASSERT_TRUE(test_server_.Start());
3660
3661  TestDelegate d;
3662  {
3663    URLRequest r(test_server_.GetURL(std::string()),
3664                 DEFAULT_PRIORITY,
3665                 &d,
3666                 &default_context_);
3667
3668    r.Start();
3669    EXPECT_TRUE(r.is_pending());
3670
3671    base::RunLoop().Run();
3672
3673    EXPECT_EQ(1, d.response_started_count());
3674    EXPECT_FALSE(d.received_data_before_response());
3675    EXPECT_NE(0, d.bytes_received());
3676    EXPECT_EQ(test_server_.host_port_pair().host(),
3677              r.GetSocketAddress().host());
3678    EXPECT_EQ(test_server_.host_port_pair().port(),
3679              r.GetSocketAddress().port());
3680  }
3681}
3682
3683TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) {
3684  ASSERT_TRUE(test_server_.Start());
3685
3686  TestDelegate d;
3687  {
3688    GURL test_url(test_server_.GetURL(std::string()));
3689    URLRequest r(test_url, DEFAULT_PRIORITY, &d, &default_context_);
3690
3691    HttpRequestHeaders headers;
3692    EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
3693
3694    r.Start();
3695    EXPECT_TRUE(r.is_pending());
3696
3697    base::RunLoop().Run();
3698
3699    EXPECT_EQ(1, d.response_started_count());
3700    EXPECT_FALSE(d.received_data_before_response());
3701    EXPECT_NE(0, d.bytes_received());
3702    EXPECT_EQ(test_server_.host_port_pair().host(),
3703              r.GetSocketAddress().host());
3704    EXPECT_EQ(test_server_.host_port_pair().port(),
3705              r.GetSocketAddress().port());
3706
3707    EXPECT_TRUE(d.have_full_request_headers());
3708    CheckFullRequestHeaders(d.full_request_headers(), test_url);
3709  }
3710}
3711
3712TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
3713  ASSERT_TRUE(test_server_.Start());
3714
3715  TestDelegate d;
3716  {
3717    URLRequest r(test_server_.GetURL(std::string()),
3718                 DEFAULT_PRIORITY,
3719                 &d,
3720                 &default_context_);
3721
3722    r.Start();
3723    EXPECT_TRUE(r.is_pending());
3724
3725    base::RunLoop().Run();
3726
3727    LoadTimingInfo load_timing_info;
3728    r.GetLoadTimingInfo(&load_timing_info);
3729    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3730
3731    EXPECT_EQ(1, d.response_started_count());
3732    EXPECT_FALSE(d.received_data_before_response());
3733    EXPECT_NE(0, d.bytes_received());
3734    EXPECT_EQ(test_server_.host_port_pair().host(),
3735              r.GetSocketAddress().host());
3736    EXPECT_EQ(test_server_.host_port_pair().port(),
3737              r.GetSocketAddress().port());
3738  }
3739}
3740
3741TEST_F(URLRequestTestHTTP, GetZippedTest) {
3742  ASSERT_TRUE(test_server_.Start());
3743
3744  // Parameter that specifies the Content-Length field in the response:
3745  // C - Compressed length.
3746  // U - Uncompressed length.
3747  // L - Large length (larger than both C & U).
3748  // M - Medium length (between C & U).
3749  // S - Small length (smaller than both C & U).
3750  const char test_parameters[] = "CULMS";
3751  const int num_tests = arraysize(test_parameters)- 1;  // Skip NULL.
3752  // C & U should be OK.
3753  // L & M are larger than the data sent, and show an error.
3754  // S has too little data, but we seem to accept it.
3755  const bool test_expect_success[num_tests] =
3756      { true, true, false, false, true };
3757
3758  for (int i = 0; i < num_tests ; i++) {
3759    TestDelegate d;
3760    {
3761      std::string test_file =
3762          base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
3763                             test_parameters[i]);
3764
3765      TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
3766      TestURLRequestContext context(true);
3767      context.set_network_delegate(&network_delegate);
3768      context.Init();
3769
3770      URLRequest r(
3771          test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d, &context);
3772      r.Start();
3773      EXPECT_TRUE(r.is_pending());
3774
3775      base::RunLoop().Run();
3776
3777      EXPECT_EQ(1, d.response_started_count());
3778      EXPECT_FALSE(d.received_data_before_response());
3779      VLOG(1) << " Received " << d.bytes_received() << " bytes"
3780              << " status = " << r.status().status()
3781              << " error = " << r.status().error();
3782      if (test_expect_success[i]) {
3783        EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status())
3784            << " Parameter = \"" << test_file << "\"";
3785      } else {
3786        EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
3787        EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r.status().error())
3788            << " Parameter = \"" << test_file << "\"";
3789      }
3790    }
3791  }
3792}
3793
3794TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
3795  ASSERT_TRUE(test_server_.Start());
3796
3797  SpawnedTestServer https_test_server(
3798      SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost,
3799      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
3800  ASSERT_TRUE(https_test_server.Start());
3801
3802  // An https server is sent a request with an https referer,
3803  // and responds with a redirect to an http url. The http
3804  // server should not be sent the referer.
3805  GURL http_destination = test_server_.GetURL(std::string());
3806  TestDelegate d;
3807  URLRequest req(
3808      https_test_server.GetURL("server-redirect?" + http_destination.spec()),
3809      DEFAULT_PRIORITY,
3810      &d,
3811      &default_context_);
3812  req.SetReferrer("https://www.referrer.com/");
3813  req.Start();
3814  base::RunLoop().Run();
3815
3816  EXPECT_EQ(1, d.response_started_count());
3817  EXPECT_EQ(1, d.received_redirect_count());
3818  EXPECT_EQ(http_destination, req.url());
3819  EXPECT_EQ(std::string(), req.referrer());
3820}
3821
3822TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
3823  ASSERT_TRUE(test_server_.Start());
3824
3825  GURL destination_url = test_server_.GetURL(std::string());
3826  GURL original_url =
3827      test_server_.GetURL("server-redirect?" + destination_url.spec());
3828  TestDelegate d;
3829  URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
3830  req.Start();
3831  base::RunLoop().Run();
3832
3833  EXPECT_EQ(1, d.response_started_count());
3834  EXPECT_EQ(1, d.received_redirect_count());
3835  EXPECT_EQ(destination_url, req.url());
3836  EXPECT_EQ(original_url, req.original_url());
3837  ASSERT_EQ(2U, req.url_chain().size());
3838  EXPECT_EQ(original_url, req.url_chain()[0]);
3839  EXPECT_EQ(destination_url, req.url_chain()[1]);
3840
3841  LoadTimingInfo load_timing_info_before_redirect;
3842  EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
3843      &load_timing_info_before_redirect));
3844  TestLoadTimingNotReused(load_timing_info_before_redirect,
3845                          CONNECT_TIMING_HAS_DNS_TIMES);
3846
3847  LoadTimingInfo load_timing_info;
3848  req.GetLoadTimingInfo(&load_timing_info);
3849  TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3850
3851  // Check that a new socket was used on redirect, since the server does not
3852  // supposed keep-alive sockets, and that the times before the redirect are
3853  // before the ones recorded for the second request.
3854  EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
3855            load_timing_info.socket_log_id);
3856  EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
3857            load_timing_info.connect_timing.connect_start);
3858}
3859
3860TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
3861  ASSERT_TRUE(test_server_.Start());
3862
3863  GURL destination_url = test_server_.GetURL(std::string());
3864  GURL middle_redirect_url =
3865      test_server_.GetURL("server-redirect?" + destination_url.spec());
3866  GURL original_url = test_server_.GetURL(
3867      "server-redirect?" + middle_redirect_url.spec());
3868  TestDelegate d;
3869  URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
3870  req.Start();
3871  base::RunLoop().Run();
3872
3873  EXPECT_EQ(1, d.response_started_count());
3874  EXPECT_EQ(2, d.received_redirect_count());
3875  EXPECT_EQ(destination_url, req.url());
3876  EXPECT_EQ(original_url, req.original_url());
3877  ASSERT_EQ(3U, req.url_chain().size());
3878  EXPECT_EQ(original_url, req.url_chain()[0]);
3879  EXPECT_EQ(middle_redirect_url, req.url_chain()[1]);
3880  EXPECT_EQ(destination_url, req.url_chain()[2]);
3881}
3882
3883// First and second pieces of information logged by delegates to URLRequests.
3884const char kFirstDelegateInfo[] = "Wonderful delegate";
3885const char kSecondDelegateInfo[] = "Exciting delegate";
3886
3887// Logs delegate information to a URLRequest.  The first string is logged
3888// synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY.  The second is
3889// logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER.  Then
3890// another asynchronous call is used to clear the delegate information
3891// before calling a callback.  The object then deletes itself.
3892class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> {
3893 public:
3894  typedef base::Callback<void()> Callback;
3895
3896  // Each time delegate information is added to the URLRequest, the resulting
3897  // load state is checked.  The expected load state after each request is
3898  // passed in as an argument.
3899  static void Run(URLRequest* url_request,
3900                  LoadState expected_first_load_state,
3901                  LoadState expected_second_load_state,
3902                  LoadState expected_third_load_state,
3903                  const Callback& callback) {
3904    AsyncDelegateLogger* logger = new AsyncDelegateLogger(
3905        url_request,
3906        expected_first_load_state,
3907        expected_second_load_state,
3908        expected_third_load_state,
3909        callback);
3910    logger->Start();
3911  }
3912
3913  // Checks that the log entries, starting with log_position, contain the
3914  // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have
3915  // recorded.  Returns the index of entry after the expected number of
3916  // events this logged, or entries.size() if there aren't enough entries.
3917  static size_t CheckDelegateInfo(
3918      const CapturingNetLog::CapturedEntryList& entries, size_t log_position) {
3919    // There should be 4 DELEGATE_INFO events: Two begins and two ends.
3920    if (log_position + 3 >= entries.size()) {
3921      ADD_FAILURE() << "Not enough log entries";
3922      return entries.size();
3923    }
3924    std::string delegate_info;
3925    EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3926    EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3927    EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3928                                                     &delegate_info));
3929    EXPECT_EQ(kFirstDelegateInfo, delegate_info);
3930
3931    ++log_position;
3932    EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3933    EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3934
3935    ++log_position;
3936    EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3937    EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3938    EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3939                                                     &delegate_info));
3940    EXPECT_EQ(kSecondDelegateInfo, delegate_info);
3941
3942    ++log_position;
3943    EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3944    EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3945
3946    return log_position + 1;
3947  }
3948
3949  // Find delegate request begin and end messages for OnBeforeNetworkStart.
3950  // Returns the position of the end message.
3951  static size_t ExpectBeforeNetworkEvents(
3952      const CapturingNetLog::CapturedEntryList& entries,
3953      size_t log_position) {
3954    log_position =
3955        ExpectLogContainsSomewhereAfter(entries,
3956                                        log_position,
3957                                        NetLog::TYPE_URL_REQUEST_DELEGATE,
3958                                        NetLog::PHASE_BEGIN);
3959    EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE,
3960              entries[log_position + 1].type);
3961    EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase);
3962    return log_position + 1;
3963  }
3964
3965 private:
3966  friend class base::RefCounted<AsyncDelegateLogger>;
3967
3968  AsyncDelegateLogger(URLRequest* url_request,
3969                      LoadState expected_first_load_state,
3970                      LoadState expected_second_load_state,
3971                      LoadState expected_third_load_state,
3972                      const Callback& callback)
3973      : url_request_(url_request),
3974        expected_first_load_state_(expected_first_load_state),
3975        expected_second_load_state_(expected_second_load_state),
3976        expected_third_load_state_(expected_third_load_state),
3977        callback_(callback) {
3978  }
3979
3980  ~AsyncDelegateLogger() {}
3981
3982  void Start() {
3983    url_request_->LogBlockedBy(kFirstDelegateInfo);
3984    LoadStateWithParam load_state = url_request_->GetLoadState();
3985    EXPECT_EQ(expected_first_load_state_, load_state.state);
3986    EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param);
3987    base::MessageLoop::current()->PostTask(
3988        FROM_HERE,
3989        base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this));
3990  }
3991
3992  void LogSecondDelegate() {
3993    url_request_->LogAndReportBlockedBy(kSecondDelegateInfo);
3994    LoadStateWithParam load_state = url_request_->GetLoadState();
3995    EXPECT_EQ(expected_second_load_state_, load_state.state);
3996    if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) {
3997      EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
3998    } else {
3999      EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
4000    }
4001    base::MessageLoop::current()->PostTask(
4002        FROM_HERE,
4003        base::Bind(&AsyncDelegateLogger::LogComplete, this));
4004  }
4005
4006  void LogComplete() {
4007    url_request_->LogUnblocked();
4008    LoadStateWithParam load_state = url_request_->GetLoadState();
4009    EXPECT_EQ(expected_third_load_state_, load_state.state);
4010    if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE)
4011      EXPECT_EQ(base::string16(), load_state.param);
4012    callback_.Run();
4013  }
4014
4015  URLRequest* url_request_;
4016  const int expected_first_load_state_;
4017  const int expected_second_load_state_;
4018  const int expected_third_load_state_;
4019  const Callback callback_;
4020
4021  DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger);
4022};
4023
4024// NetworkDelegate that logs delegate information before a request is started,
4025// before headers are sent, when headers are read, and when auth information
4026// is requested.  Uses AsyncDelegateLogger.
4027class AsyncLoggingNetworkDelegate : public TestNetworkDelegate {
4028 public:
4029  AsyncLoggingNetworkDelegate() {}
4030  virtual ~AsyncLoggingNetworkDelegate() {}
4031
4032  // NetworkDelegate implementation.
4033  virtual int OnBeforeURLRequest(URLRequest* request,
4034                                 const CompletionCallback& callback,
4035                                 GURL* new_url) OVERRIDE {
4036    TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
4037    return RunCallbackAsynchronously(request, callback);
4038  }
4039
4040  virtual int OnBeforeSendHeaders(URLRequest* request,
4041                                  const CompletionCallback& callback,
4042                                  HttpRequestHeaders* headers) OVERRIDE {
4043    TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
4044    return RunCallbackAsynchronously(request, callback);
4045  }
4046
4047  virtual int OnHeadersReceived(
4048      URLRequest* request,
4049      const CompletionCallback& callback,
4050      const HttpResponseHeaders* original_response_headers,
4051      scoped_refptr<HttpResponseHeaders>* override_response_headers,
4052      GURL* allowed_unsafe_redirect_url) OVERRIDE {
4053    TestNetworkDelegate::OnHeadersReceived(request,
4054                                           callback,
4055                                           original_response_headers,
4056                                           override_response_headers,
4057                                           allowed_unsafe_redirect_url);
4058    return RunCallbackAsynchronously(request, callback);
4059  }
4060
4061  virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
4062      URLRequest* request,
4063      const AuthChallengeInfo& auth_info,
4064      const AuthCallback& callback,
4065      AuthCredentials* credentials) OVERRIDE {
4066    AsyncDelegateLogger::Run(
4067        request,
4068        LOAD_STATE_WAITING_FOR_DELEGATE,
4069        LOAD_STATE_WAITING_FOR_DELEGATE,
4070        LOAD_STATE_WAITING_FOR_DELEGATE,
4071        base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume,
4072                   callback, credentials));
4073    return AUTH_REQUIRED_RESPONSE_IO_PENDING;
4074  }
4075
4076 private:
4077  static int RunCallbackAsynchronously(
4078      URLRequest* request,
4079      const CompletionCallback& callback) {
4080    AsyncDelegateLogger::Run(
4081        request,
4082        LOAD_STATE_WAITING_FOR_DELEGATE,
4083        LOAD_STATE_WAITING_FOR_DELEGATE,
4084        LOAD_STATE_WAITING_FOR_DELEGATE,
4085        base::Bind(callback, OK));
4086    return ERR_IO_PENDING;
4087  }
4088
4089  static void SetAuthAndResume(const AuthCallback& callback,
4090                               AuthCredentials* credentials) {
4091    *credentials = AuthCredentials(kUser, kSecret);
4092    callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
4093  }
4094
4095  DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate);
4096};
4097
4098// URLRequest::Delegate that logs delegate information when the headers
4099// are received, when each read completes, and during redirects.  Uses
4100// AsyncDelegateLogger.  Can optionally cancel a request in any phase.
4101//
4102// Inherits from TestDelegate to reuse the TestDelegate code to handle
4103// advancing to the next step in most cases, as well as cancellation.
4104class AsyncLoggingUrlRequestDelegate : public TestDelegate {
4105 public:
4106  enum CancelStage {
4107    NO_CANCEL = 0,
4108    CANCEL_ON_RECEIVED_REDIRECT,
4109    CANCEL_ON_RESPONSE_STARTED,
4110    CANCEL_ON_READ_COMPLETED
4111  };
4112
4113  explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage)
4114      : cancel_stage_(cancel_stage) {
4115    if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT)
4116      set_cancel_in_received_redirect(true);
4117    else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED)
4118      set_cancel_in_response_started(true);
4119    else if (cancel_stage == CANCEL_ON_READ_COMPLETED)
4120      set_cancel_in_received_data(true);
4121  }
4122  virtual ~AsyncLoggingUrlRequestDelegate() {}
4123
4124  // URLRequest::Delegate implementation:
4125  void virtual OnReceivedRedirect(URLRequest* request,
4126                                  const GURL& new_url,
4127                                  bool* defer_redirect) OVERRIDE {
4128    *defer_redirect = true;
4129    AsyncDelegateLogger::Run(
4130        request,
4131        LOAD_STATE_WAITING_FOR_DELEGATE,
4132        LOAD_STATE_WAITING_FOR_DELEGATE,
4133        LOAD_STATE_WAITING_FOR_DELEGATE,
4134        base::Bind(
4135            &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete,
4136            base::Unretained(this), request, new_url));
4137  }
4138
4139  virtual void OnResponseStarted(URLRequest* request) OVERRIDE {
4140    AsyncDelegateLogger::Run(
4141      request,
4142      LOAD_STATE_WAITING_FOR_DELEGATE,
4143      LOAD_STATE_WAITING_FOR_DELEGATE,
4144      LOAD_STATE_WAITING_FOR_DELEGATE,
4145      base::Bind(
4146          &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete,
4147          base::Unretained(this), request));
4148  }
4149
4150  virtual void OnReadCompleted(URLRequest* request,
4151                               int bytes_read) OVERRIDE {
4152    AsyncDelegateLogger::Run(
4153        request,
4154        LOAD_STATE_IDLE,
4155        LOAD_STATE_IDLE,
4156        LOAD_STATE_IDLE,
4157        base::Bind(
4158            &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete,
4159            base::Unretained(this), request, bytes_read));
4160  }
4161
4162 private:
4163  void OnReceivedRedirectLoggingComplete(URLRequest* request,
4164                                         const GURL& new_url) {
4165    bool defer_redirect = false;
4166    TestDelegate::OnReceivedRedirect(request, new_url, &defer_redirect);
4167    // FollowDeferredRedirect should not be called after cancellation.
4168    if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT)
4169      return;
4170    if (!defer_redirect)
4171      request->FollowDeferredRedirect();
4172  }
4173
4174  void OnResponseStartedLoggingComplete(URLRequest* request) {
4175    // The parent class continues the request.
4176    TestDelegate::OnResponseStarted(request);
4177  }
4178
4179  void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) {
4180    // The parent class continues the request.
4181    TestDelegate::OnReadCompleted(request, bytes_read);
4182  }
4183
4184  const CancelStage cancel_stage_;
4185
4186  DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate);
4187};
4188
4189// Tests handling of delegate info before a request starts.
4190TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) {
4191  ASSERT_TRUE(test_server_.Start());
4192
4193  TestDelegate request_delegate;
4194  TestURLRequestContext context(true);
4195  context.set_network_delegate(NULL);
4196  context.set_net_log(&net_log_);
4197  context.Init();
4198
4199  {
4200    URLRequest r(test_server_.GetURL("empty.html"),
4201                 DEFAULT_PRIORITY,
4202                 &request_delegate,
4203                 &context);
4204    LoadStateWithParam load_state = r.GetLoadState();
4205    EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4206    EXPECT_EQ(base::string16(), load_state.param);
4207
4208    AsyncDelegateLogger::Run(
4209        &r,
4210        LOAD_STATE_WAITING_FOR_DELEGATE,
4211        LOAD_STATE_WAITING_FOR_DELEGATE,
4212        LOAD_STATE_IDLE,
4213        base::Bind(&URLRequest::Start, base::Unretained(&r)));
4214
4215    base::RunLoop().Run();
4216
4217    EXPECT_EQ(200, r.GetResponseCode());
4218    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4219  }
4220
4221  CapturingNetLog::CapturedEntryList entries;
4222  net_log_.GetEntries(&entries);
4223  size_t log_position = ExpectLogContainsSomewhereAfter(
4224      entries,
4225      0,
4226      NetLog::TYPE_DELEGATE_INFO,
4227      NetLog::PHASE_BEGIN);
4228
4229  log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position);
4230
4231  // Nothing else should add any delegate info to the request.
4232  EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4233      entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4234}
4235
4236// Tests handling of delegate info from a network delegate.
4237TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) {
4238  ASSERT_TRUE(test_server_.Start());
4239
4240  TestDelegate request_delegate;
4241  AsyncLoggingNetworkDelegate network_delegate;
4242  TestURLRequestContext context(true);
4243  context.set_network_delegate(&network_delegate);
4244  context.set_net_log(&net_log_);
4245  context.Init();
4246
4247  {
4248    URLRequest r(test_server_.GetURL("simple.html"),
4249                 DEFAULT_PRIORITY,
4250                 &request_delegate,
4251                 &context);
4252    LoadStateWithParam load_state = r.GetLoadState();
4253    EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4254    EXPECT_EQ(base::string16(), load_state.param);
4255
4256    r.Start();
4257    base::RunLoop().Run();
4258
4259    EXPECT_EQ(200, r.GetResponseCode());
4260    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4261    EXPECT_EQ(1, network_delegate.created_requests());
4262    EXPECT_EQ(0, network_delegate.destroyed_requests());
4263  }
4264  EXPECT_EQ(1, network_delegate.destroyed_requests());
4265
4266  size_t log_position = 0;
4267  CapturingNetLog::CapturedEntryList entries;
4268  net_log_.GetEntries(&entries);
4269  for (size_t i = 0; i < 3; ++i) {
4270    log_position = ExpectLogContainsSomewhereAfter(
4271        entries,
4272        log_position + 1,
4273        NetLog::TYPE_URL_REQUEST_DELEGATE,
4274        NetLog::PHASE_BEGIN);
4275
4276    log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4277                                                          log_position + 1);
4278
4279    ASSERT_LT(log_position, entries.size());
4280    EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4281    EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4282
4283    if (i == 1) {
4284      log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4285          entries, log_position + 1);
4286    }
4287  }
4288
4289  EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4290      entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4291}
4292
4293// Tests handling of delegate info from a network delegate in the case of an
4294// HTTP redirect.
4295TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) {
4296  ASSERT_TRUE(test_server_.Start());
4297
4298  TestDelegate request_delegate;
4299  AsyncLoggingNetworkDelegate network_delegate;
4300  TestURLRequestContext context(true);
4301  context.set_network_delegate(&network_delegate);
4302  context.set_net_log(&net_log_);
4303  context.Init();
4304
4305  {
4306    URLRequest r(test_server_.GetURL("server-redirect?simple.html"),
4307                 DEFAULT_PRIORITY,
4308                 &request_delegate,
4309                 &context);
4310    LoadStateWithParam load_state = r.GetLoadState();
4311    EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4312    EXPECT_EQ(base::string16(), load_state.param);
4313
4314    r.Start();
4315    base::RunLoop().Run();
4316
4317    EXPECT_EQ(200, r.GetResponseCode());
4318    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4319    EXPECT_EQ(2, network_delegate.created_requests());
4320    EXPECT_EQ(0, network_delegate.destroyed_requests());
4321  }
4322  EXPECT_EQ(1, network_delegate.destroyed_requests());
4323
4324  size_t log_position = 0;
4325  CapturingNetLog::CapturedEntryList entries;
4326  net_log_.GetEntries(&entries);
4327  // The NetworkDelegate logged information in OnBeforeURLRequest,
4328  // OnBeforeSendHeaders, and OnHeadersReceived.
4329  for (size_t i = 0; i < 3; ++i) {
4330    log_position = ExpectLogContainsSomewhereAfter(
4331        entries,
4332        log_position + 1,
4333        NetLog::TYPE_URL_REQUEST_DELEGATE,
4334        NetLog::PHASE_BEGIN);
4335
4336    log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4337                                                          log_position + 1);
4338
4339    ASSERT_LT(log_position, entries.size());
4340    EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4341    EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4342
4343    if (i == 1) {
4344      log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4345          entries, log_position + 1);
4346    }
4347  }
4348
4349  // The URLRequest::Delegate then gets informed about the redirect.
4350  log_position = ExpectLogContainsSomewhereAfter(
4351      entries,
4352      log_position + 1,
4353      NetLog::TYPE_URL_REQUEST_DELEGATE,
4354      NetLog::PHASE_BEGIN);
4355
4356  // The NetworkDelegate logged information in the same three events as before.
4357  for (size_t i = 0; i < 3; ++i) {
4358    log_position = ExpectLogContainsSomewhereAfter(
4359        entries,
4360        log_position + 1,
4361        NetLog::TYPE_URL_REQUEST_DELEGATE,
4362        NetLog::PHASE_BEGIN);
4363
4364    log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4365                                                          log_position + 1);
4366
4367    ASSERT_LT(log_position, entries.size());
4368    EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4369    EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4370  }
4371
4372  EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4373      entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4374}
4375
4376// Tests handling of delegate info from a network delegate in the case of HTTP
4377// AUTH.
4378TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) {
4379  ASSERT_TRUE(test_server_.Start());
4380
4381  TestDelegate request_delegate;
4382  AsyncLoggingNetworkDelegate network_delegate;
4383  TestURLRequestContext context(true);
4384  context.set_network_delegate(&network_delegate);
4385  context.set_net_log(&net_log_);
4386  context.Init();
4387
4388  {
4389    URLRequest r(test_server_.GetURL("auth-basic"),
4390                 DEFAULT_PRIORITY,
4391                 &request_delegate,
4392                 &context);
4393    LoadStateWithParam load_state = r.GetLoadState();
4394    EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4395    EXPECT_EQ(base::string16(), load_state.param);
4396
4397    r.Start();
4398    base::RunLoop().Run();
4399
4400    EXPECT_EQ(200, r.GetResponseCode());
4401    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4402    EXPECT_EQ(1, network_delegate.created_requests());
4403    EXPECT_EQ(0, network_delegate.destroyed_requests());
4404  }
4405  EXPECT_EQ(1, network_delegate.destroyed_requests());
4406
4407  size_t log_position = 0;
4408  CapturingNetLog::CapturedEntryList entries;
4409  net_log_.GetEntries(&entries);
4410  // The NetworkDelegate should have logged information in OnBeforeURLRequest,
4411  // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in
4412  // OnBeforeURLRequest and OnBeforeSendHeaders.
4413  for (size_t i = 0; i < 6; ++i) {
4414    log_position = ExpectLogContainsSomewhereAfter(
4415        entries,
4416        log_position + 1,
4417        NetLog::TYPE_URL_REQUEST_DELEGATE,
4418        NetLog::PHASE_BEGIN);
4419
4420    log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4421                                                          log_position + 1);
4422
4423    ASSERT_LT(log_position, entries.size());
4424    EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4425    EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4426
4427    if (i == 1) {
4428      log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4429          entries, log_position + 1);
4430    }
4431  }
4432
4433  EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4434      entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4435}
4436
4437// Tests handling of delegate info from a URLRequest::Delegate.
4438TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
4439  ASSERT_TRUE(test_server_.Start());
4440
4441  AsyncLoggingUrlRequestDelegate request_delegate(
4442      AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4443  TestURLRequestContext context(true);
4444  context.set_network_delegate(NULL);
4445  context.set_net_log(&net_log_);
4446  context.Init();
4447
4448  {
4449    // A chunked response with delays between chunks is used to make sure that
4450    // attempts by the URLRequest delegate to log information while reading the
4451    // body are ignored.  Since they are ignored, this test is robust against
4452    // the possibility of multiple reads being combined in the unlikely event
4453    // that it occurs.
4454    URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"),
4455                 DEFAULT_PRIORITY,
4456                 &request_delegate,
4457                 &context);
4458    LoadStateWithParam load_state = r.GetLoadState();
4459    r.Start();
4460    base::RunLoop().Run();
4461
4462    EXPECT_EQ(200, r.GetResponseCode());
4463    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4464  }
4465
4466  CapturingNetLog::CapturedEntryList entries;
4467  net_log_.GetEntries(&entries);
4468
4469  size_t log_position = 0;
4470
4471  log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4472      entries, log_position);
4473
4474  // The delegate info should only have been logged on header complete.  Other
4475  // times it should silently be ignored.
4476  log_position =
4477      ExpectLogContainsSomewhereAfter(entries,
4478                                      log_position + 1,
4479                                      NetLog::TYPE_URL_REQUEST_DELEGATE,
4480                                      NetLog::PHASE_BEGIN);
4481
4482  log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4483                                                        log_position + 1);
4484
4485  ASSERT_LT(log_position, entries.size());
4486  EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4487  EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4488
4489  EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4490      entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4491  EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4492      entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4493}
4494
4495// Tests handling of delegate info from a URLRequest::Delegate in the case of
4496// an HTTP redirect.
4497TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) {
4498  ASSERT_TRUE(test_server_.Start());
4499
4500  AsyncLoggingUrlRequestDelegate request_delegate(
4501      AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4502  TestURLRequestContext context(true);
4503  context.set_network_delegate(NULL);
4504  context.set_net_log(&net_log_);
4505  context.Init();
4506
4507  {
4508    URLRequest r(test_server_.GetURL("server-redirect?simple.html"),
4509                 DEFAULT_PRIORITY,
4510                 &request_delegate,
4511                 &context);
4512    LoadStateWithParam load_state = r.GetLoadState();
4513    r.Start();
4514    base::RunLoop().Run();
4515
4516    EXPECT_EQ(200, r.GetResponseCode());
4517    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4518  }
4519
4520  CapturingNetLog::CapturedEntryList entries;
4521  net_log_.GetEntries(&entries);
4522
4523  // Delegate info should only have been logged in OnReceivedRedirect and
4524  // OnResponseStarted.
4525  size_t log_position = 0;
4526  for (int i = 0; i < 2; ++i) {
4527    if (i == 0) {
4528      log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4529                         entries, log_position) + 1;
4530    }
4531
4532    log_position = ExpectLogContainsSomewhereAfter(
4533            entries,
4534            log_position,
4535            NetLog::TYPE_URL_REQUEST_DELEGATE,
4536            NetLog::PHASE_BEGIN);
4537
4538    log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4539                                                          log_position + 1);
4540
4541    ASSERT_LT(log_position, entries.size());
4542    EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4543    EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4544  }
4545
4546  EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4547      entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4548  EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4549      entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4550}
4551
4552// Tests handling of delegate info from a URLRequest::Delegate in the case of
4553// an HTTP redirect, with cancellation at various points.
4554TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) {
4555  ASSERT_TRUE(test_server_.Start());
4556
4557  const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = {
4558    AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT,
4559    AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED,
4560    AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED,
4561  };
4562
4563  for (size_t test_case = 0; test_case < arraysize(kCancelStages);
4564       ++test_case) {
4565    AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]);
4566    TestURLRequestContext context(true);
4567    CapturingNetLog net_log;
4568    context.set_network_delegate(NULL);
4569    context.set_net_log(&net_log);
4570    context.Init();
4571
4572    {
4573      URLRequest r(test_server_.GetURL("server-redirect?simple.html"),
4574                   DEFAULT_PRIORITY,
4575                   &request_delegate,
4576                   &context);
4577      LoadStateWithParam load_state = r.GetLoadState();
4578      r.Start();
4579      base::RunLoop().Run();
4580      EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4581    }
4582
4583    CapturingNetLog::CapturedEntryList entries;
4584    net_log.GetEntries(&entries);
4585
4586    // Delegate info is always logged in both OnReceivedRedirect and
4587    // OnResponseStarted.  In the CANCEL_ON_RECEIVED_REDIRECT, the
4588    // OnResponseStarted delegate call is after cancellation, but logging is
4589    // still currently supported in that call.
4590    size_t log_position = 0;
4591    for (int i = 0; i < 2; ++i) {
4592      if (i == 0) {
4593        log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4594                           entries, log_position) + 1;
4595      }
4596
4597      log_position = ExpectLogContainsSomewhereAfter(
4598              entries,
4599              log_position,
4600              NetLog::TYPE_URL_REQUEST_DELEGATE,
4601              NetLog::PHASE_BEGIN);
4602
4603      log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4604                                                            log_position + 1);
4605
4606      ASSERT_LT(log_position, entries.size());
4607      EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4608      EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4609    }
4610
4611    EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4612        entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4613    EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4614        entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4615  }
4616}
4617
4618namespace {
4619
4620const char kExtraHeader[] = "Allow-Snafu";
4621const char kExtraValue[] = "fubar";
4622
4623class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
4624  virtual void OnReceivedRedirect(net::URLRequest* request,
4625                                  const GURL& new_url,
4626                                  bool* defer_redirect) OVERRIDE {
4627    TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
4628    request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
4629  }
4630};
4631
4632}  // namespace
4633
4634TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
4635  ASSERT_TRUE(test_server_.Start());
4636
4637  GURL destination_url = test_server_.GetURL(
4638      "echoheader?" + std::string(kExtraHeader));
4639  GURL original_url = test_server_.GetURL(
4640      "server-redirect?" + destination_url.spec());
4641  RedirectWithAdditionalHeadersDelegate d;
4642  URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
4643  req.Start();
4644  base::RunLoop().Run();
4645
4646  std::string value;
4647  const HttpRequestHeaders& headers = req.extra_request_headers();
4648  EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
4649  EXPECT_EQ(kExtraValue, value);
4650  EXPECT_FALSE(req.is_pending());
4651  EXPECT_FALSE(req.is_redirecting());
4652  EXPECT_EQ(kExtraValue, d.data_received());
4653}
4654
4655namespace {
4656
4657const char kExtraHeaderToRemove[] = "To-Be-Removed";
4658
4659class RedirectWithHeaderRemovalDelegate : public TestDelegate {
4660  virtual void OnReceivedRedirect(net::URLRequest* request,
4661                          const GURL& new_url,
4662                          bool* defer_redirect) OVERRIDE {
4663    TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
4664    request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
4665  }
4666};
4667
4668}  // namespace
4669
4670TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
4671  ASSERT_TRUE(test_server_.Start());
4672
4673  GURL destination_url = test_server_.GetURL(
4674      "echoheader?" + std::string(kExtraHeaderToRemove));
4675  GURL original_url = test_server_.GetURL(
4676      "server-redirect?" + destination_url.spec());
4677  RedirectWithHeaderRemovalDelegate d;
4678  URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
4679  req.SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
4680  req.Start();
4681  base::RunLoop().Run();
4682
4683  std::string value;
4684  const HttpRequestHeaders& headers = req.extra_request_headers();
4685  EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
4686  EXPECT_FALSE(req.is_pending());
4687  EXPECT_FALSE(req.is_redirecting());
4688  EXPECT_EQ("None", d.data_received());
4689}
4690
4691TEST_F(URLRequestTestHTTP, CancelTest) {
4692  TestDelegate d;
4693  {
4694    URLRequest r(GURL("http://www.google.com/"),
4695                 DEFAULT_PRIORITY,
4696                 &d,
4697                 &default_context_);
4698
4699    r.Start();
4700    EXPECT_TRUE(r.is_pending());
4701
4702    r.Cancel();
4703
4704    base::RunLoop().Run();
4705
4706    // We expect to receive OnResponseStarted even though the request has been
4707    // cancelled.
4708    EXPECT_EQ(1, d.response_started_count());
4709    EXPECT_EQ(0, d.bytes_received());
4710    EXPECT_FALSE(d.received_data_before_response());
4711  }
4712}
4713
4714TEST_F(URLRequestTestHTTP, CancelTest2) {
4715  ASSERT_TRUE(test_server_.Start());
4716
4717  TestDelegate d;
4718  {
4719    URLRequest r(test_server_.GetURL(std::string()),
4720                 DEFAULT_PRIORITY,
4721                 &d,
4722                 &default_context_);
4723
4724    d.set_cancel_in_response_started(true);
4725
4726    r.Start();
4727    EXPECT_TRUE(r.is_pending());
4728
4729    base::RunLoop().Run();
4730
4731    EXPECT_EQ(1, d.response_started_count());
4732    EXPECT_EQ(0, d.bytes_received());
4733    EXPECT_FALSE(d.received_data_before_response());
4734    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4735  }
4736}
4737
4738TEST_F(URLRequestTestHTTP, CancelTest3) {
4739  ASSERT_TRUE(test_server_.Start());
4740
4741  TestDelegate d;
4742  {
4743    URLRequest r(test_server_.GetURL(std::string()),
4744                 DEFAULT_PRIORITY,
4745                 &d,
4746                 &default_context_);
4747
4748    d.set_cancel_in_received_data(true);
4749
4750    r.Start();
4751    EXPECT_TRUE(r.is_pending());
4752
4753    base::RunLoop().Run();
4754
4755    EXPECT_EQ(1, d.response_started_count());
4756    // There is no guarantee about how much data was received
4757    // before the cancel was issued.  It could have been 0 bytes,
4758    // or it could have been all the bytes.
4759    // EXPECT_EQ(0, d.bytes_received());
4760    EXPECT_FALSE(d.received_data_before_response());
4761    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4762  }
4763}
4764
4765TEST_F(URLRequestTestHTTP, CancelTest4) {
4766  ASSERT_TRUE(test_server_.Start());
4767
4768  TestDelegate d;
4769  {
4770    URLRequest r(test_server_.GetURL(std::string()),
4771                 DEFAULT_PRIORITY,
4772                 &d,
4773                 &default_context_);
4774
4775    r.Start();
4776    EXPECT_TRUE(r.is_pending());
4777
4778    // The request will be implicitly canceled when it is destroyed. The
4779    // test delegate must not post a quit message when this happens because
4780    // this test doesn't actually have a message loop. The quit message would
4781    // get put on this thread's message queue and the next test would exit
4782    // early, causing problems.
4783    d.set_quit_on_complete(false);
4784  }
4785  // expect things to just cleanup properly.
4786
4787  // we won't actually get a received reponse here because we've never run the
4788  // message loop
4789  EXPECT_FALSE(d.received_data_before_response());
4790  EXPECT_EQ(0, d.bytes_received());
4791}
4792
4793TEST_F(URLRequestTestHTTP, CancelTest5) {
4794  ASSERT_TRUE(test_server_.Start());
4795
4796  // populate cache
4797  {
4798    TestDelegate d;
4799    URLRequest r(test_server_.GetURL("cachetime"),
4800                 DEFAULT_PRIORITY,
4801                 &d,
4802                 &default_context_);
4803    r.Start();
4804    base::RunLoop().Run();
4805    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4806  }
4807
4808  // cancel read from cache (see bug 990242)
4809  {
4810    TestDelegate d;
4811    URLRequest r(test_server_.GetURL("cachetime"),
4812                 DEFAULT_PRIORITY,
4813                 &d,
4814                 &default_context_);
4815    r.Start();
4816    r.Cancel();
4817    base::RunLoop().Run();
4818
4819    EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4820    EXPECT_EQ(1, d.response_started_count());
4821    EXPECT_EQ(0, d.bytes_received());
4822    EXPECT_FALSE(d.received_data_before_response());
4823  }
4824}
4825
4826TEST_F(URLRequestTestHTTP, PostTest) {
4827  ASSERT_TRUE(test_server_.Start());
4828  HTTPUploadDataOperationTest("POST");
4829}
4830
4831TEST_F(URLRequestTestHTTP, PutTest) {
4832  ASSERT_TRUE(test_server_.Start());
4833  HTTPUploadDataOperationTest("PUT");
4834}
4835
4836TEST_F(URLRequestTestHTTP, PostEmptyTest) {
4837  ASSERT_TRUE(test_server_.Start());
4838
4839  TestDelegate d;
4840  {
4841    URLRequest r(
4842        test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4843    r.set_method("POST");
4844
4845    r.Start();
4846    EXPECT_TRUE(r.is_pending());
4847
4848    base::RunLoop().Run();
4849
4850    ASSERT_EQ(1, d.response_started_count())
4851        << "request failed: " << r.status().status()
4852        << ", error: " << r.status().error();
4853
4854    EXPECT_FALSE(d.received_data_before_response());
4855    EXPECT_TRUE(d.data_received().empty());
4856  }
4857}
4858
4859TEST_F(URLRequestTestHTTP, PostFileTest) {
4860  ASSERT_TRUE(test_server_.Start());
4861
4862  TestDelegate d;
4863  {
4864    URLRequest r(
4865        test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4866    r.set_method("POST");
4867
4868    base::FilePath dir;
4869    PathService::Get(base::DIR_EXE, &dir);
4870    base::SetCurrentDirectory(dir);
4871
4872    ScopedVector<UploadElementReader> element_readers;
4873
4874    base::FilePath path;
4875    PathService::Get(base::DIR_SOURCE_ROOT, &path);
4876    path = path.Append(FILE_PATH_LITERAL("net"));
4877    path = path.Append(FILE_PATH_LITERAL("data"));
4878    path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
4879    path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
4880    element_readers.push_back(
4881        new UploadFileElementReader(base::MessageLoopProxy::current().get(),
4882                                    path,
4883                                    0,
4884                                    kuint64max,
4885                                    base::Time()));
4886    r.set_upload(make_scoped_ptr(
4887        new UploadDataStream(element_readers.Pass(), 0)));
4888
4889    r.Start();
4890    EXPECT_TRUE(r.is_pending());
4891
4892    base::RunLoop().Run();
4893
4894    int64 size = 0;
4895    ASSERT_EQ(true, base::GetFileSize(path, &size));
4896    scoped_ptr<char[]> buf(new char[size]);
4897
4898    ASSERT_EQ(size, base::ReadFile(path, buf.get(), size));
4899
4900    ASSERT_EQ(1, d.response_started_count())
4901        << "request failed: " << r.status().status()
4902        << ", error: " << r.status().error();
4903
4904    EXPECT_FALSE(d.received_data_before_response());
4905
4906    EXPECT_EQ(size, d.bytes_received());
4907    EXPECT_EQ(std::string(&buf[0], size), d.data_received());
4908  }
4909}
4910
4911TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) {
4912  ASSERT_TRUE(test_server_.Start());
4913
4914  TestDelegate d;
4915  {
4916    URLRequest r(test_server_.GetURL("echo"), DEFAULT_PRIORITY,
4917                 &d, &default_context_);
4918    r.set_method("POST");
4919
4920    ScopedVector<UploadElementReader> element_readers;
4921
4922    element_readers.push_back(new UploadFileElementReader(
4923        base::MessageLoopProxy::current().get(),
4924        base::FilePath(FILE_PATH_LITERAL(
4925            "c:\\path\\to\\non\\existant\\file.randomness.12345")),
4926        0,
4927        kuint64max,
4928        base::Time()));
4929    r.set_upload(make_scoped_ptr(
4930        new UploadDataStream(element_readers.Pass(), 0)));
4931
4932    r.Start();
4933    EXPECT_TRUE(r.is_pending());
4934
4935    base::RunLoop().Run();
4936
4937    EXPECT_TRUE(d.request_failed());
4938    EXPECT_FALSE(d.received_data_before_response());
4939    EXPECT_EQ(0, d.bytes_received());
4940    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
4941    EXPECT_EQ(ERR_FILE_NOT_FOUND, r.status().error());
4942  }
4943}
4944
4945TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
4946  ASSERT_TRUE(test_server_.Start());
4947
4948  TestDelegate d;
4949  {
4950    URLRequest r(
4951        test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4952    r.EnableChunkedUpload();
4953    r.set_method("POST");
4954    AddChunksToUpload(&r);
4955    r.Start();
4956    EXPECT_TRUE(r.is_pending());
4957
4958    base::RunLoop().Run();
4959
4960    VerifyReceivedDataMatchesChunks(&r, &d);
4961  }
4962}
4963
4964TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
4965  ASSERT_TRUE(test_server_.Start());
4966
4967  TestDelegate d;
4968  {
4969    URLRequest r(
4970        test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4971    r.EnableChunkedUpload();
4972    r.set_method("POST");
4973    r.Start();
4974    EXPECT_TRUE(r.is_pending());
4975    AddChunksToUpload(&r);
4976    base::RunLoop().Run();
4977
4978    VerifyReceivedDataMatchesChunks(&r, &d);
4979  }
4980}
4981
4982TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
4983  ASSERT_TRUE(test_server_.Start());
4984
4985  TestDelegate d;
4986  {
4987    URLRequest r(
4988        test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4989    r.EnableChunkedUpload();
4990    r.set_method("POST");
4991    r.Start();
4992    EXPECT_TRUE(r.is_pending());
4993
4994    base::RunLoop().RunUntilIdle();
4995    AddChunksToUpload(&r);
4996    base::RunLoop().Run();
4997
4998    VerifyReceivedDataMatchesChunks(&r, &d);
4999  }
5000}
5001
5002TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
5003  ASSERT_TRUE(test_server_.Start());
5004
5005  TestDelegate d;
5006  URLRequest req(test_server_.GetURL("files/with-headers.html"),
5007                 DEFAULT_PRIORITY,
5008                 &d,
5009                 &default_context_);
5010  req.Start();
5011  base::RunLoop().Run();
5012
5013  const HttpResponseHeaders* headers = req.response_headers();
5014
5015  // Simple sanity check that response_info() accesses the same data.
5016  EXPECT_EQ(headers, req.response_info().headers.get());
5017
5018  std::string header;
5019  EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
5020  EXPECT_EQ("private", header);
5021
5022  header.clear();
5023  EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
5024  EXPECT_EQ("text/html; charset=ISO-8859-1", header);
5025
5026  // The response has two "X-Multiple-Entries" headers.
5027  // This verfies our output has them concatenated together.
5028  header.clear();
5029  EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
5030  EXPECT_EQ("a, b", header);
5031}
5032
5033TEST_F(URLRequestTestHTTP, ProcessSTS) {
5034  SpawnedTestServer::SSLOptions ssl_options;
5035  SpawnedTestServer https_test_server(
5036      SpawnedTestServer::TYPE_HTTPS,
5037      ssl_options,
5038      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5039  ASSERT_TRUE(https_test_server.Start());
5040
5041  TestDelegate d;
5042  URLRequest request(https_test_server.GetURL("files/hsts-headers.html"),
5043                     DEFAULT_PRIORITY,
5044                     &d,
5045                     &default_context_);
5046  request.Start();
5047  base::RunLoop().Run();
5048
5049  TransportSecurityState* security_state =
5050      default_context_.transport_security_state();
5051  TransportSecurityState::DomainState domain_state;
5052  EXPECT_TRUE(security_state->GetDynamicDomainState(
5053      SpawnedTestServer::kLocalhost, &domain_state));
5054  EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5055            domain_state.sts.upgrade_mode);
5056  EXPECT_TRUE(domain_state.sts.include_subdomains);
5057  EXPECT_FALSE(domain_state.pkp.include_subdomains);
5058#if defined(OS_ANDROID)
5059  // Android's CertVerifyProc does not (yet) handle pins.
5060#else
5061  EXPECT_FALSE(domain_state.HasPublicKeyPins());
5062#endif
5063}
5064
5065// Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
5066// reject HPKP headers, and a test setting only HPKP headers will fail (no
5067// DomainState present because header rejected).
5068#if defined(OS_ANDROID)
5069#define MAYBE_ProcessPKP DISABLED_ProcessPKP
5070#else
5071#define MAYBE_ProcessPKP ProcessPKP
5072#endif
5073
5074// Tests that enabling HPKP on a domain does not affect the HSTS
5075// validity/expiration.
5076TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) {
5077  SpawnedTestServer::SSLOptions ssl_options;
5078  SpawnedTestServer https_test_server(
5079      SpawnedTestServer::TYPE_HTTPS,
5080      ssl_options,
5081      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5082  ASSERT_TRUE(https_test_server.Start());
5083
5084  TestDelegate d;
5085  URLRequest request(https_test_server.GetURL("files/hpkp-headers.html"),
5086                     DEFAULT_PRIORITY,
5087                     &d,
5088                     &default_context_);
5089  request.Start();
5090  base::RunLoop().Run();
5091
5092  TransportSecurityState* security_state =
5093      default_context_.transport_security_state();
5094  TransportSecurityState::DomainState domain_state;
5095  EXPECT_TRUE(security_state->GetDynamicDomainState(
5096      SpawnedTestServer::kLocalhost, &domain_state));
5097  EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT,
5098            domain_state.sts.upgrade_mode);
5099  EXPECT_FALSE(domain_state.sts.include_subdomains);
5100  EXPECT_FALSE(domain_state.pkp.include_subdomains);
5101  EXPECT_TRUE(domain_state.HasPublicKeyPins());
5102  EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
5103}
5104
5105TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
5106  SpawnedTestServer::SSLOptions ssl_options;
5107  SpawnedTestServer https_test_server(
5108      SpawnedTestServer::TYPE_HTTPS,
5109      ssl_options,
5110      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5111  ASSERT_TRUE(https_test_server.Start());
5112
5113  TestDelegate d;
5114  URLRequest request(
5115      https_test_server.GetURL("files/hsts-multiple-headers.html"),
5116      DEFAULT_PRIORITY,
5117      &d,
5118      &default_context_);
5119  request.Start();
5120  base::RunLoop().Run();
5121
5122  // We should have set parameters from the first header, not the second.
5123  TransportSecurityState* security_state =
5124      default_context_.transport_security_state();
5125  TransportSecurityState::DomainState domain_state;
5126  EXPECT_TRUE(security_state->GetDynamicDomainState(
5127      SpawnedTestServer::kLocalhost, &domain_state));
5128  EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5129            domain_state.sts.upgrade_mode);
5130  EXPECT_FALSE(domain_state.sts.include_subdomains);
5131  EXPECT_FALSE(domain_state.pkp.include_subdomains);
5132}
5133
5134TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) {
5135  SpawnedTestServer::SSLOptions ssl_options;
5136  SpawnedTestServer https_test_server(
5137      SpawnedTestServer::TYPE_HTTPS,
5138      ssl_options,
5139      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5140  ASSERT_TRUE(https_test_server.Start());
5141
5142  TestDelegate d;
5143  URLRequest request(
5144      https_test_server.GetURL("files/hsts-and-hpkp-headers.html"),
5145      DEFAULT_PRIORITY,
5146      &d,
5147      &default_context_);
5148  request.Start();
5149  base::RunLoop().Run();
5150
5151  // We should have set parameters from the first header, not the second.
5152  TransportSecurityState* security_state =
5153      default_context_.transport_security_state();
5154  TransportSecurityState::DomainState domain_state;
5155  EXPECT_TRUE(security_state->GetDynamicDomainState(
5156      SpawnedTestServer::kLocalhost, &domain_state));
5157  EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5158            domain_state.sts.upgrade_mode);
5159#if defined(OS_ANDROID)
5160  // Android's CertVerifyProc does not (yet) handle pins.
5161#else
5162  EXPECT_TRUE(domain_state.HasPublicKeyPins());
5163#endif
5164  EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
5165
5166  // Even though there is an HSTS header asserting includeSubdomains, it is
5167  // the *second* such header, and we MUST process only the first.
5168  EXPECT_FALSE(domain_state.sts.include_subdomains);
5169  // includeSubdomains does not occur in the test HPKP header.
5170  EXPECT_FALSE(domain_state.pkp.include_subdomains);
5171}
5172
5173// Tests that when multiple HPKP headers are present, asserting different
5174// policies, that only the first such policy is processed.
5175TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) {
5176  SpawnedTestServer::SSLOptions ssl_options;
5177  SpawnedTestServer https_test_server(
5178      SpawnedTestServer::TYPE_HTTPS,
5179      ssl_options,
5180      base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5181  ASSERT_TRUE(https_test_server.Start());
5182
5183  TestDelegate d;
5184  URLRequest request(
5185      https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"),
5186      DEFAULT_PRIORITY,
5187      &d,
5188      &default_context_);
5189  request.Start();
5190  base::RunLoop().Run();
5191
5192  TransportSecurityState* security_state =
5193      default_context_.transport_security_state();
5194  TransportSecurityState::DomainState domain_state;
5195  EXPECT_TRUE(security_state->GetDynamicDomainState(
5196      SpawnedTestServer::kLocalhost, &domain_state));
5197  EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5198            domain_state.sts.upgrade_mode);
5199#if defined(OS_ANDROID)
5200  // Android's CertVerifyProc does not (yet) handle pins.
5201#else
5202  EXPECT_TRUE(domain_state.HasPublicKeyPins());
5203#endif
5204  EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
5205
5206  EXPECT_TRUE(domain_state.sts.include_subdomains);
5207  EXPECT_FALSE(domain_state.pkp.include_subdomains);
5208}
5209
5210TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
5211  ASSERT_TRUE(test_server_.Start());
5212
5213  TestDelegate d;
5214  URLRequest req(test_server_.GetURL("files/content-type-normalization.html"),
5215                 DEFAULT_PRIORITY,
5216                 &d,
5217                 &default_context_);
5218  req.Start();
5219  base::RunLoop().Run();
5220
5221  std::string mime_type;
5222  req.GetMimeType(&mime_type);
5223  EXPECT_EQ("text/html", mime_type);
5224
5225  std::string charset;
5226  req.GetCharset(&charset);
5227  EXPECT_EQ("utf-8", charset);
5228  req.Cancel();
5229}
5230
5231TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) {
5232  // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5233  GURL data_url("data:,foo");
5234  DataProtocolHandler data_protocol_handler;
5235  EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url));
5236
5237  // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5238  EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(data_url));
5239}
5240
5241#if !defined(DISABLE_FILE_SUPPORT)
5242TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) {
5243  // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5244  GURL file_url("file:///foo.txt");
5245  FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current());
5246  EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url));
5247
5248  // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5249  EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url));
5250}
5251
5252TEST_F(URLRequestTestHTTP, RestrictFileRedirects) {
5253  ASSERT_TRUE(test_server_.Start());
5254
5255  TestDelegate d;
5256  URLRequest req(test_server_.GetURL("files/redirect-to-file.html"),
5257                 DEFAULT_PRIORITY,
5258                 &d,
5259                 &default_context_);
5260  req.Start();
5261  base::RunLoop().Run();
5262
5263  EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
5264  EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
5265}
5266#endif  // !defined(DISABLE_FILE_SUPPORT)
5267
5268TEST_F(URLRequestTestHTTP, RestrictDataRedirects) {
5269  ASSERT_TRUE(test_server_.Start());
5270
5271  TestDelegate d;
5272  URLRequest req(test_server_.GetURL("files/redirect-to-data.html"),
5273                 DEFAULT_PRIORITY,
5274                 &d,
5275                 &default_context_);
5276  req.Start();
5277  base::MessageLoop::current()->Run();
5278
5279  EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
5280  EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
5281}
5282
5283TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
5284  ASSERT_TRUE(test_server_.Start());
5285
5286  TestDelegate d;
5287  URLRequest req(test_server_.GetURL("files/redirect-to-invalid-url.html"),
5288                 DEFAULT_PRIORITY,
5289                 &d,
5290                 &default_context_);
5291  req.Start();
5292  base::RunLoop().Run();
5293
5294  EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
5295  EXPECT_EQ(ERR_INVALID_URL, req.status().error());
5296}
5297
5298// Make sure redirects are cached, despite not reading their bodies.
5299TEST_F(URLRequestTestHTTP, CacheRedirect) {
5300  ASSERT_TRUE(test_server_.Start());
5301  GURL redirect_url =
5302      test_server_.GetURL("files/redirect302-to-echo-cacheable");
5303
5304  {
5305    TestDelegate d;
5306    URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_);
5307    req.Start();
5308    base::RunLoop().Run();
5309    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5310    EXPECT_EQ(1, d.received_redirect_count());
5311    EXPECT_EQ(test_server_.GetURL("echo"), req.url());
5312  }
5313
5314  {
5315    TestDelegate d;
5316    d.set_quit_on_redirect(true);
5317    URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_);
5318    req.Start();
5319    base::RunLoop().Run();
5320
5321    EXPECT_EQ(1, d.received_redirect_count());
5322    EXPECT_EQ(0, d.response_started_count());
5323    EXPECT_TRUE(req.was_cached());
5324
5325    req.FollowDeferredRedirect();
5326    base::RunLoop().Run();
5327    EXPECT_EQ(1, d.received_redirect_count());
5328    EXPECT_EQ(1, d.response_started_count());
5329    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5330    EXPECT_EQ(test_server_.GetURL("echo"), req.url());
5331  }
5332}
5333
5334// Make sure a request isn't cached when a NetworkDelegate forces a redirect
5335// when the headers are read, since the body won't have been read.
5336TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) {
5337  ASSERT_TRUE(test_server_.Start());
5338  // URL that is normally cached.
5339  GURL initial_url = test_server_.GetURL("cachetime");
5340
5341  {
5342    // Set up the TestNetworkDelegate tp force a redirect.
5343    GURL redirect_to_url = test_server_.GetURL("echo");
5344    default_network_delegate_.set_redirect_on_headers_received_url(
5345        redirect_to_url);
5346
5347    TestDelegate d;
5348    URLRequest req(initial_url, DEFAULT_PRIORITY, &d, &default_context_);
5349    req.Start();
5350    base::RunLoop().Run();
5351    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5352    EXPECT_EQ(1, d.received_redirect_count());
5353    EXPECT_EQ(redirect_to_url, req.url());
5354  }
5355
5356  {
5357    TestDelegate d;
5358    URLRequest req(initial_url, DEFAULT_PRIORITY, &d, &default_context_);
5359    req.Start();
5360    base::RunLoop().Run();
5361
5362    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5363    EXPECT_FALSE(req.was_cached());
5364    EXPECT_EQ(0, d.received_redirect_count());
5365    EXPECT_EQ(initial_url, req.url());
5366  }
5367}
5368
5369// Tests that redirection to an unsafe URL is allowed when it has been marked as
5370// safe.
5371TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) {
5372  ASSERT_TRUE(test_server_.Start());
5373
5374  GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url");
5375  default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5376  default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5377
5378  TestDelegate d;
5379  {
5380    URLRequest r(test_server_.GetURL("whatever"),
5381                 DEFAULT_PRIORITY,
5382                 &d,
5383                 &default_context_);
5384
5385    r.Start();
5386    base::RunLoop().Run();
5387
5388    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5389
5390    EXPECT_EQ(2U, r.url_chain().size());
5391    EXPECT_EQ(net::OK, r.status().error());
5392    EXPECT_EQ(unsafe_url, r.url());
5393    EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received());
5394  }
5395}
5396
5397// Tests that a redirect to a different unsafe URL is blocked, even after adding
5398// some other URL to the whitelist.
5399TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) {
5400  ASSERT_TRUE(test_server_.Start());
5401
5402  GURL unsafe_url("data:text/html,something");
5403  GURL different_unsafe_url("data:text/html,something-else");
5404  default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5405  default_network_delegate_.set_allowed_unsafe_redirect_url(
5406      different_unsafe_url);
5407
5408  TestDelegate d;
5409  {
5410    URLRequest r(test_server_.GetURL("whatever"),
5411                 DEFAULT_PRIORITY,
5412                 &d,
5413                 &default_context_);
5414
5415    r.Start();
5416    base::RunLoop().Run();
5417
5418    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
5419    EXPECT_EQ(ERR_UNSAFE_REDIRECT, r.status().error());
5420  }
5421}
5422
5423// Redirects from an URL with fragment to an unsafe URL with fragment should
5424// be allowed, and the reference fragment of the target URL should be preserved.
5425TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) {
5426  ASSERT_TRUE(test_server_.Start());
5427
5428  GURL original_url(test_server_.GetURL("original#fragment1"));
5429  GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5430  GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5431
5432  default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5433  default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5434
5435  TestDelegate d;
5436  {
5437    URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
5438
5439    r.Start();
5440    base::RunLoop().Run();
5441
5442    EXPECT_EQ(2U, r.url_chain().size());
5443    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5444    EXPECT_EQ(net::OK, r.status().error());
5445    EXPECT_EQ(original_url, r.original_url());
5446    EXPECT_EQ(expected_url, r.url());
5447  }
5448}
5449
5450// When a delegate has specified a safe redirect URL, but it does not match the
5451// redirect target, then do not prevent the reference fragment from being added.
5452TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) {
5453  ASSERT_TRUE(test_server_.Start());
5454
5455  GURL original_url(test_server_.GetURL("original#expected-fragment"));
5456  GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url");
5457  GURL redirect_url(test_server_.GetURL("target"));
5458  GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment"));
5459
5460  default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5461  default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5462
5463  TestDelegate d;
5464  {
5465    URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
5466
5467    r.Start();
5468    base::RunLoop().Run();
5469
5470    EXPECT_EQ(2U, r.url_chain().size());
5471    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5472    EXPECT_EQ(net::OK, r.status().error());
5473    EXPECT_EQ(original_url, r.original_url());
5474    EXPECT_EQ(expected_redirect_url, r.url());
5475  }
5476}
5477
5478// When a delegate has specified a safe redirect URL, assume that the redirect
5479// URL should not be changed. In particular, the reference fragment should not
5480// be modified.
5481TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) {
5482  ASSERT_TRUE(test_server_.Start());
5483
5484  GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5485  GURL redirect_url("data:text/html,expect-no-reference-fragment");
5486
5487  default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5488  default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url);
5489
5490  TestDelegate d;
5491  {
5492    URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
5493
5494    r.Start();
5495    base::RunLoop().Run();
5496
5497    EXPECT_EQ(2U, r.url_chain().size());
5498    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5499    EXPECT_EQ(net::OK, r.status().error());
5500    EXPECT_EQ(original_url, r.original_url());
5501    EXPECT_EQ(redirect_url, r.url());
5502  }
5503}
5504
5505// When a URLRequestRedirectJob is created, the redirection must be followed and
5506// the reference fragment of the target URL must not be modified.
5507TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) {
5508  ASSERT_TRUE(test_server_.Start());
5509
5510  GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5511  GURL redirect_url(test_server_.GetURL("echo"));
5512
5513  TestDelegate d;
5514  URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
5515
5516  URLRequestRedirectJob* job = new URLRequestRedirectJob(
5517      &r, &default_network_delegate_, redirect_url,
5518      URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
5519  AddTestInterceptor()->set_main_intercept_job(job);
5520
5521  r.Start();
5522  base::RunLoop().Run();
5523
5524  EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5525  EXPECT_EQ(net::OK, r.status().error());
5526  EXPECT_EQ(original_url, r.original_url());
5527  EXPECT_EQ(redirect_url, r.url());
5528}
5529
5530TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
5531  ASSERT_TRUE(test_server_.Start());
5532
5533  TestDelegate d;
5534  URLRequest req(test_server_.GetURL("echoheader?Referer"),
5535                 DEFAULT_PRIORITY,
5536                 &d,
5537                 &default_context_);
5538  req.SetReferrer("http://user:pass@foo.com/");
5539  req.Start();
5540  base::RunLoop().Run();
5541
5542  EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
5543}
5544
5545TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) {
5546  ASSERT_TRUE(test_server_.Start());
5547
5548  TestDelegate d;
5549  URLRequest req(test_server_.GetURL("echoheader?Referer"),
5550                 DEFAULT_PRIORITY,
5551                 &d,
5552                 &default_context_);
5553  req.SetReferrer("http://foo.com/test#fragment");
5554  req.Start();
5555  base::RunLoop().Run();
5556
5557  EXPECT_EQ(std::string("http://foo.com/test"), d.data_received());
5558}
5559
5560TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) {
5561  ASSERT_TRUE(test_server_.Start());
5562
5563  TestDelegate d;
5564  URLRequest req(test_server_.GetURL("echoheader?Referer"),
5565                 DEFAULT_PRIORITY,
5566                 &d,
5567                 &default_context_);
5568  req.SetReferrer("http://foo.com/test#fragment");
5569  req.SetReferrer("");
5570  req.Start();
5571  base::RunLoop().Run();
5572
5573  EXPECT_EQ(std::string("None"), d.data_received());
5574}
5575
5576// Defer network start and then resume, checking that the request was a success
5577// and bytes were received.
5578TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) {
5579  ASSERT_TRUE(test_server_.Start());
5580
5581  TestDelegate d;
5582  {
5583    d.set_quit_on_network_start(true);
5584    GURL test_url(test_server_.GetURL("echo"));
5585    URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5586
5587    req.Start();
5588    base::RunLoop().Run();
5589
5590    EXPECT_EQ(1, d.received_before_network_start_count());
5591    EXPECT_EQ(0, d.response_started_count());
5592
5593    req.ResumeNetworkStart();
5594    base::RunLoop().Run();
5595
5596    EXPECT_EQ(1, d.response_started_count());
5597    EXPECT_NE(0, d.bytes_received());
5598    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5599  }
5600}
5601
5602// Check that OnBeforeNetworkStart is only called once even if there is a
5603// redirect.
5604TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) {
5605  ASSERT_TRUE(test_server_.Start());
5606
5607  TestDelegate d;
5608  {
5609    d.set_quit_on_redirect(true);
5610    d.set_quit_on_network_start(true);
5611    URLRequest req(test_server_.GetURL("server-redirect?echo"),
5612                   DEFAULT_PRIORITY,
5613                   &d,
5614                   &default_context_);
5615
5616    req.Start();
5617    base::RunLoop().Run();
5618
5619    EXPECT_EQ(1, d.received_before_network_start_count());
5620    EXPECT_EQ(0, d.response_started_count());
5621    EXPECT_EQ(0, d.received_redirect_count());
5622
5623    req.ResumeNetworkStart();
5624    base::RunLoop().Run();
5625
5626    EXPECT_EQ(1, d.received_redirect_count());
5627    req.FollowDeferredRedirect();
5628    base::RunLoop().Run();
5629
5630    // Check that the redirect's new network transaction does not get propagated
5631    // to a second OnBeforeNetworkStart() notification.
5632    EXPECT_EQ(1, d.received_before_network_start_count());
5633
5634    EXPECT_EQ(1, d.response_started_count());
5635    EXPECT_NE(0, d.bytes_received());
5636    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5637  }
5638}
5639
5640// Cancel the request after learning that the request would use the network.
5641TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) {
5642  ASSERT_TRUE(test_server_.Start());
5643
5644  TestDelegate d;
5645  {
5646    d.set_quit_on_network_start(true);
5647    GURL test_url(test_server_.GetURL("echo"));
5648    URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5649
5650    req.Start();
5651    base::RunLoop().Run();
5652
5653    EXPECT_EQ(1, d.received_before_network_start_count());
5654    EXPECT_EQ(0, d.response_started_count());
5655
5656    req.Cancel();
5657    base::RunLoop().Run();
5658
5659    EXPECT_EQ(1, d.response_started_count());
5660    EXPECT_EQ(0, d.bytes_received());
5661    EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5662  }
5663}
5664
5665TEST_F(URLRequestTestHTTP, CancelRedirect) {
5666  ASSERT_TRUE(test_server_.Start());
5667
5668  TestDelegate d;
5669  {
5670    d.set_cancel_in_received_redirect(true);
5671    URLRequest req(test_server_.GetURL("files/redirect-test.html"),
5672                   DEFAULT_PRIORITY,
5673                   &d,
5674                   &default_context_);
5675    req.Start();
5676    base::RunLoop().Run();
5677
5678    EXPECT_EQ(1, d.response_started_count());
5679    EXPECT_EQ(0, d.bytes_received());
5680    EXPECT_FALSE(d.received_data_before_response());
5681    EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5682  }
5683}
5684
5685TEST_F(URLRequestTestHTTP, DeferredRedirect) {
5686  ASSERT_TRUE(test_server_.Start());
5687
5688  TestDelegate d;
5689  {
5690    d.set_quit_on_redirect(true);
5691    GURL test_url(test_server_.GetURL("files/redirect-test.html"));
5692    URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5693
5694    req.Start();
5695    base::RunLoop().Run();
5696
5697    EXPECT_EQ(1, d.received_redirect_count());
5698
5699    req.FollowDeferredRedirect();
5700    base::RunLoop().Run();
5701
5702    EXPECT_EQ(1, d.response_started_count());
5703    EXPECT_FALSE(d.received_data_before_response());
5704    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5705
5706    base::FilePath path;
5707    PathService::Get(base::DIR_SOURCE_ROOT, &path);
5708    path = path.Append(FILE_PATH_LITERAL("net"));
5709    path = path.Append(FILE_PATH_LITERAL("data"));
5710    path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5711    path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5712
5713    std::string contents;
5714    EXPECT_TRUE(base::ReadFileToString(path, &contents));
5715    EXPECT_EQ(contents, d.data_received());
5716  }
5717}
5718
5719TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) {
5720  ASSERT_TRUE(test_server_.Start());
5721
5722  TestDelegate d;
5723  {
5724    d.set_quit_on_redirect(true);
5725    GURL test_url(test_server_.GetURL("files/redirect-test.html"));
5726    URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5727
5728    EXPECT_FALSE(d.have_full_request_headers());
5729
5730    req.Start();
5731    base::RunLoop().Run();
5732
5733    EXPECT_EQ(1, d.received_redirect_count());
5734    EXPECT_TRUE(d.have_full_request_headers());
5735    CheckFullRequestHeaders(d.full_request_headers(), test_url);
5736    d.ClearFullRequestHeaders();
5737
5738    req.FollowDeferredRedirect();
5739    base::RunLoop().Run();
5740
5741    GURL target_url(test_server_.GetURL("files/with-headers.html"));
5742    EXPECT_EQ(1, d.response_started_count());
5743    EXPECT_TRUE(d.have_full_request_headers());
5744    CheckFullRequestHeaders(d.full_request_headers(), target_url);
5745    EXPECT_FALSE(d.received_data_before_response());
5746    EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5747
5748    base::FilePath path;
5749    PathService::Get(base::DIR_SOURCE_ROOT, &path);
5750    path = path.Append(FILE_PATH_LITERAL("net"));
5751    path = path.Append(FILE_PATH_LITERAL("data"));
5752    path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5753    path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5754
5755    std::string contents;
5756    EXPECT_TRUE(base::ReadFileToString(path, &contents));
5757    EXPECT_EQ(contents, d.data_received());
5758  }
5759}
5760
5761TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
5762  ASSERT_TRUE(test_server_.Start());
5763
5764  TestDelegate d;
5765  {
5766    d.set_quit_on_redirect(true);
5767    URLRequest req(test_server_.GetURL("files/redirect-test.html"),
5768                   DEFAULT_PRIORITY,
5769                   &d,
5770                   &default_context_);
5771    req.Start();
5772    base::RunLoop().Run();
5773
5774    EXPECT_EQ(1, d.received_redirect_count());
5775
5776    req.Cancel();
5777    base::RunLoop().Run();
5778
5779    EXPECT_EQ(1, d.response_started_count());
5780    EXPECT_EQ(0, d.bytes_received());
5781    EXPECT_FALSE(d.received_data_before_response());
5782    EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5783  }
5784}
5785
5786TEST_F(URLRequestTestHTTP, VaryHeader) {
5787  ASSERT_TRUE(test_server_.Start());
5788
5789  // Populate the cache.
5790  {
5791    TestDelegate d;
5792    URLRequest req(test_server_.GetURL("echoheadercache?foo"),
5793                   DEFAULT_PRIORITY,
5794                   &d,
5795                   &default_context_);
5796    HttpRequestHeaders headers;
5797    headers.SetHeader("foo", "1");
5798    req.SetExtraRequestHeaders(headers);
5799    req.Start();
5800    base::RunLoop().Run();
5801
5802    LoadTimingInfo load_timing_info;
5803    req.GetLoadTimingInfo(&load_timing_info);
5804    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
5805  }
5806
5807  // Expect a cache hit.
5808  {
5809    TestDelegate d;
5810    URLRequest req(test_server_.GetURL("echoheadercache?foo"),
5811                   DEFAULT_PRIORITY,
5812                   &d,
5813                   &default_context_);
5814    HttpRequestHeaders headers;
5815    headers.SetHeader("foo", "1");
5816    req.SetExtraRequestHeaders(headers);
5817    req.Start();
5818    base::RunLoop().Run();
5819
5820    EXPECT_TRUE(req.was_cached());
5821
5822    LoadTimingInfo load_timing_info;
5823    req.GetLoadTimingInfo(&load_timing_info);
5824    TestLoadTimingCacheHitNoNetwork(load_timing_info);
5825  }
5826
5827  // Expect a cache miss.
5828  {
5829    TestDelegate d;
5830    URLRequest req(test_server_.GetURL("echoheadercache?foo"),
5831                   DEFAULT_PRIORITY,
5832                   &d,
5833                   &default_context_);
5834    HttpRequestHeaders headers;
5835    headers.SetHeader("foo", "2");
5836    req.SetExtraRequestHeaders(headers);
5837    req.Start();
5838    base::RunLoop().Run();
5839
5840    EXPECT_FALSE(req.was_cached());
5841
5842    LoadTimingInfo load_timing_info;
5843    req.GetLoadTimingInfo(&load_timing_info);
5844    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
5845  }
5846}
5847
5848TEST_F(URLRequestTestHTTP, BasicAuth) {
5849  ASSERT_TRUE(test_server_.Start());
5850
5851  // populate the cache
5852  {
5853    TestDelegate d;
5854    d.set_credentials(AuthCredentials(kUser, kSecret));
5855
5856    URLRequest r(test_server_.GetURL("auth-basic"),
5857                 DEFAULT_PRIORITY,
5858                 &d,
5859                 &default_context_);
5860    r.Start();
5861
5862    base::RunLoop().Run();
5863
5864    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5865  }
5866
5867  // repeat request with end-to-end validation.  since auth-basic results in a
5868  // cachable page, we expect this test to result in a 304.  in which case, the
5869  // response should be fetched from the cache.
5870  {
5871    TestDelegate d;
5872    d.set_credentials(AuthCredentials(kUser, kSecret));
5873
5874    URLRequest r(test_server_.GetURL("auth-basic"),
5875                 DEFAULT_PRIORITY,
5876                 &d,
5877                 &default_context_);
5878    r.SetLoadFlags(LOAD_VALIDATE_CACHE);
5879    r.Start();
5880
5881    base::RunLoop().Run();
5882
5883    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5884
5885    // Should be the same cached document.
5886    EXPECT_TRUE(r.was_cached());
5887  }
5888}
5889
5890// Check that Set-Cookie headers in 401 responses are respected.
5891// http://crbug.com/6450
5892TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
5893  ASSERT_TRUE(test_server_.Start());
5894
5895  GURL url_requiring_auth =
5896      test_server_.GetURL("auth-basic?set-cookie-if-challenged");
5897
5898  // Request a page that will give a 401 containing a Set-Cookie header.
5899  // Verify that when the transaction is restarted, it includes the new cookie.
5900  {
5901    TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
5902    TestURLRequestContext context(true);
5903    context.set_network_delegate(&network_delegate);
5904    context.Init();
5905
5906    TestDelegate d;
5907    d.set_credentials(AuthCredentials(kUser, kSecret));
5908
5909    URLRequest r(url_requiring_auth, DEFAULT_PRIORITY, &d, &context);
5910    r.Start();
5911
5912    base::RunLoop().Run();
5913
5914    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5915
5916    // Make sure we sent the cookie in the restarted transaction.
5917    EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5918        != std::string::npos);
5919  }
5920
5921  // Same test as above, except this time the restart is initiated earlier
5922  // (without user intervention since identity is embedded in the URL).
5923  {
5924    TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
5925    TestURLRequestContext context(true);
5926    context.set_network_delegate(&network_delegate);
5927    context.Init();
5928
5929    TestDelegate d;
5930
5931    GURL::Replacements replacements;
5932    std::string username("user2");
5933    std::string password("secret");
5934    replacements.SetUsernameStr(username);
5935    replacements.SetPasswordStr(password);
5936    GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
5937
5938    URLRequest r(url_with_identity, DEFAULT_PRIORITY, &d, &context);
5939    r.Start();
5940
5941    base::RunLoop().Run();
5942
5943    EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
5944
5945    // Make sure we sent the cookie in the restarted transaction.
5946    EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5947        != std::string::npos);
5948  }
5949}
5950
5951// Tests that load timing works as expected with auth and the cache.
5952TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
5953  ASSERT_TRUE(test_server_.Start());
5954
5955  // populate the cache
5956  {
5957    TestDelegate d;
5958    d.set_credentials(AuthCredentials(kUser, kSecret));
5959
5960    URLRequest r(test_server_.GetURL("auth-basic"),
5961                 DEFAULT_PRIORITY,
5962                 &d,
5963                 &default_context_);
5964    r.Start();
5965
5966    base::RunLoop().Run();
5967
5968    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5969
5970    LoadTimingInfo load_timing_info_before_auth;
5971    EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
5972        &load_timing_info_before_auth));
5973    TestLoadTimingNotReused(load_timing_info_before_auth,
5974                            CONNECT_TIMING_HAS_DNS_TIMES);
5975
5976    LoadTimingInfo load_timing_info;
5977    r.GetLoadTimingInfo(&load_timing_info);
5978    // The test server does not support keep alive sockets, so the second
5979    // request with auth should use a new socket.
5980    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
5981    EXPECT_NE(load_timing_info_before_auth.socket_log_id,
5982              load_timing_info.socket_log_id);
5983    EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
5984              load_timing_info.connect_timing.connect_start);
5985  }
5986
5987  // Repeat request with end-to-end validation.  Since auth-basic results in a
5988  // cachable page, we expect this test to result in a 304.  In which case, the
5989  // response should be fetched from the cache.
5990  {
5991    TestDelegate d;
5992    d.set_credentials(AuthCredentials(kUser, kSecret));
5993
5994    URLRequest r(test_server_.GetURL("auth-basic"),
5995                 DEFAULT_PRIORITY,
5996                 &d,
5997                 &default_context_);
5998    r.SetLoadFlags(LOAD_VALIDATE_CACHE);
5999    r.Start();
6000
6001    base::RunLoop().Run();
6002
6003    EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6004
6005    // Should be the same cached document.
6006    EXPECT_TRUE(r.was_cached());
6007
6008    // Since there was a request that went over the wire, the load timing
6009    // information should include connection times.
6010    LoadTimingInfo load_timing_info;
6011    r.GetLoadTimingInfo(&load_timing_info);
6012    TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
6013  }
6014}
6015
6016// In this test, we do a POST which the server will 302 redirect.
6017// The subsequent transaction should use GET, and should not send the
6018// Content-Type header.
6019// http://code.google.com/p/chromium/issues/detail?id=843
6020TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
6021  ASSERT_TRUE(test_server_.Start());
6022
6023  const char kData[] = "hello world";
6024
6025  TestDelegate d;
6026  URLRequest req(test_server_.GetURL("files/redirect-to-echoall"),
6027                 DEFAULT_PRIORITY,
6028                 &d,
6029                 &default_context_);
6030  req.set_method("POST");
6031  req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
6032
6033  // Set headers (some of which are specific to the POST).
6034  HttpRequestHeaders headers;
6035  headers.AddHeadersFromString(
6036    "Content-Type: multipart/form-data; "
6037    "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
6038    "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
6039    "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
6040    "Accept-Language: en-US,en\r\n"
6041    "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
6042    "Content-Length: 11\r\n"
6043    "Origin: http://localhost:1337/");
6044  req.SetExtraRequestHeaders(headers);
6045  req.Start();
6046  base::RunLoop().Run();
6047
6048  std::string mime_type;
6049  req.GetMimeType(&mime_type);
6050  EXPECT_EQ("text/html", mime_type);
6051
6052  const std::string& data = d.data_received();
6053
6054  // Check that the post-specific headers were stripped:
6055  EXPECT_FALSE(ContainsString(data, "Content-Length:"));
6056  EXPECT_FALSE(ContainsString(data, "Content-Type:"));
6057  EXPECT_FALSE(ContainsString(data, "Origin:"));
6058
6059  // These extra request headers should not have been stripped.
6060  EXPECT_TRUE(ContainsString(data, "Accept:"));
6061  EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
6062  EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
6063}
6064
6065// The following tests check that we handle mutating the request method for
6066// HTTP redirects as expected.
6067// See http://crbug.com/56373 and http://crbug.com/102130.
6068
6069TEST_F(URLRequestTestHTTP, Redirect301Tests) {
6070  ASSERT_TRUE(test_server_.Start());
6071
6072  const GURL url = test_server_.GetURL("files/redirect301-to-echo");
6073
6074  HTTPRedirectMethodTest(url, "POST", "GET", true);
6075  HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6076  HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6077}
6078
6079TEST_F(URLRequestTestHTTP, Redirect302Tests) {
6080  ASSERT_TRUE(test_server_.Start());
6081
6082  const GURL url = test_server_.GetURL("files/redirect302-to-echo");
6083
6084  HTTPRedirectMethodTest(url, "POST", "GET", true);
6085  HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6086  HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6087}
6088
6089TEST_F(URLRequestTestHTTP, Redirect303Tests) {
6090  ASSERT_TRUE(test_server_.Start());
6091
6092  const GURL url = test_server_.GetURL("files/redirect303-to-echo");
6093
6094  HTTPRedirectMethodTest(url, "POST", "GET", true);
6095  HTTPRedirectMethodTest(url, "PUT", "GET", true);
6096  HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6097}
6098
6099TEST_F(URLRequestTestHTTP, Redirect307Tests) {
6100  ASSERT_TRUE(test_server_.Start());
6101
6102  const GURL url = test_server_.GetURL("files/redirect307-to-echo");
6103
6104  HTTPRedirectMethodTest(url, "POST", "POST", true);
6105  HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6106  HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6107}
6108
6109TEST_F(URLRequestTestHTTP, Redirect308Tests) {
6110  ASSERT_TRUE(test_server_.Start());
6111
6112  const GURL url = test_server_.GetURL("files/redirect308-to-echo");
6113
6114  HTTPRedirectMethodTest(url, "POST", "POST", true);
6115  HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6116  HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6117}
6118
6119// Make sure that 308 responses without bodies are not treated as redirects.
6120// Certain legacy apis that pre-date the response code expect this behavior
6121// (Like Google Drive).
6122TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) {
6123  ASSERT_TRUE(test_server_.Start());
6124
6125  TestDelegate d;
6126  const GURL url = test_server_.GetURL("files/308-without-location-header");
6127
6128  URLRequest request(url, DEFAULT_PRIORITY, &d, &default_context_);
6129
6130  request.Start();
6131  base::RunLoop().Run();
6132  EXPECT_EQ(URLRequestStatus::SUCCESS, request.status().status());
6133  EXPECT_EQ(OK, request.status().error());
6134  EXPECT_EQ(0, d.received_redirect_count());
6135  EXPECT_EQ(308, request.response_headers()->response_code());
6136  EXPECT_EQ("This is not a redirect.", d.data_received());
6137}
6138
6139TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) {
6140  ASSERT_TRUE(test_server_.Start());
6141
6142  GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment"));
6143  GURL expected_url(test_server_.GetURL("echo#fragment"));
6144
6145  TestDelegate d;
6146  {
6147    URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
6148
6149    r.Start();
6150    base::RunLoop().Run();
6151
6152    EXPECT_EQ(2U, r.url_chain().size());
6153    EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
6154    EXPECT_EQ(net::OK, r.status().error());
6155    EXPECT_EQ(original_url, r.original_url());
6156    EXPECT_EQ(expected_url, r.url());
6157  }
6158}
6159
6160TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
6161  ASSERT_TRUE(test_server_.Start());
6162
6163  const char kData[] = "hello world";
6164
6165  TestDelegate d;
6166  URLRequest req(test_server_.GetURL("empty.html"),
6167                 DEFAULT_PRIORITY,
6168                 &d,
6169                 &default_context_);
6170  req.set_method("POST");
6171  req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
6172  HttpRequestHeaders headers;
6173  headers.SetHeader(HttpRequestHeaders::kContentLength,
6174                    base::UintToString(arraysize(kData) - 1));
6175  req.SetExtraRequestHeaders(headers);
6176
6177  URLRequestRedirectJob* job = new URLRequestRedirectJob(
6178      &req, &default_network_delegate_, test_server_.GetURL("echo"),
6179      URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
6180  AddTestInterceptor()->set_main_intercept_job(job);
6181
6182  req.Start();
6183  base::RunLoop().Run();
6184  EXPECT_EQ("GET", req.method());
6185}
6186
6187TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
6188  ASSERT_TRUE(test_server_.Start());
6189
6190  const char kData[] = "hello world";
6191
6192  TestDelegate d;
6193  URLRequest req(test_server_.GetURL("empty.html"),
6194                 DEFAULT_PRIORITY,
6195                 &d,
6196                 &default_context_);
6197  req.set_method("POST");
6198  req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
6199  HttpRequestHeaders headers;
6200  headers.SetHeader(HttpRequestHeaders::kContentLength,
6201                    base::UintToString(arraysize(kData) - 1));
6202  req.SetExtraRequestHeaders(headers);
6203
6204  URLRequestRedirectJob* job = new URLRequestRedirectJob(
6205      &req, &default_network_delegate_, test_server_.GetURL("echo"),
6206      URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT,
6207      "Very Good Reason");
6208  AddTestInterceptor()->set_main_intercept_job(job);
6209
6210  req.Start();
6211  base::RunLoop().Run();
6212  EXPECT_EQ("POST", req.method());
6213  EXPECT_EQ(kData, d.data_received());
6214}
6215
6216// Check that default A-L header is sent.
6217TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
6218  ASSERT_TRUE(test_server_.Start());
6219
6220  StaticHttpUserAgentSettings settings("en", std::string());
6221  TestNetworkDelegate network_delegate;  // Must outlive URLRequests.
6222  TestURLRequestContext context(true);
6223  context.set_network_delegate(&network_delegate);
6224  context.set_http_user_agent_settings(&settings);
6225  context.Init();
6226
6227  TestDelegate d;
6228  URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
6229                 DEFAULT_PRIORITY,
6230                 &d,
6231                 &context);
6232  req.Start();
6233  base::RunLoop().Run();
6234  EXPECT_EQ("en", d.data_received());
6235}
6236
6237// Check that an empty A-L header is not sent. http://crbug.com/77365.
6238TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
6239  ASSERT_TRUE(test_server_.Start());
6240
6241  std::string empty_string;  // Avoid most vexing parse on line below.
6242  StaticHttpUserAgentSettings settings(empty_string, empty_string);
6243  TestNetworkDelegate network_delegate;  // Must outlive URLRequests.
6244  TestURLRequestContext context(true);
6245  context.set_network_delegate(&network_delegate);
6246  context.Init();
6247  // We override the language after initialization because empty entries
6248  // get overridden by Init().
6249  context.set_http_user_agent_settings(&settings);
6250
6251  TestDelegate d;
6252  URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
6253                 DEFAULT_PRIORITY,
6254                 &d,
6255                 &context);
6256  req.Start();
6257  base::RunLoop().Run();
6258  EXPECT_EQ("None", d.data_received());
6259}
6260
6261// Check that if request overrides the A-L header, the default is not appended.
6262// See http://crbug.com/20894
6263TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
6264  ASSERT_TRUE(test_server_.Start());
6265
6266  TestDelegate d;
6267  URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
6268                 DEFAULT_PRIORITY,
6269                 &d,
6270                 &default_context_);
6271  HttpRequestHeaders headers;
6272  headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
6273  req.SetExtraRequestHeaders(headers);
6274  req.Start();
6275  base::RunLoop().Run();
6276  EXPECT_EQ(std::string("ru"), d.data_received());
6277}
6278
6279// Check that default A-E header is sent.
6280TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
6281  ASSERT_TRUE(test_server_.Start());
6282
6283  TestDelegate d;
6284  URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
6285                 DEFAULT_PRIORITY,
6286                 &d,
6287                 &default_context_);
6288  HttpRequestHeaders headers;
6289  req.SetExtraRequestHeaders(headers);
6290  req.Start();
6291  base::RunLoop().Run();
6292  EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
6293}
6294
6295// Check that if request overrides the A-E header, the default is not appended.
6296// See http://crbug.com/47381
6297TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
6298  ASSERT_TRUE(test_server_.Start());
6299
6300  TestDelegate d;
6301  URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
6302                 DEFAULT_PRIORITY,
6303                 &d,
6304                 &default_context_);
6305  HttpRequestHeaders headers;
6306  headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
6307  req.SetExtraRequestHeaders(headers);
6308  req.Start();
6309  base::RunLoop().Run();
6310  EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
6311  EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
6312}
6313
6314// Check that setting the A-C header sends the proper header.
6315TEST_F(URLRequestTestHTTP, SetAcceptCharset) {
6316  ASSERT_TRUE(test_server_.Start());
6317
6318  TestDelegate d;
6319  URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"),
6320                 DEFAULT_PRIORITY,
6321                 &d,
6322                 &default_context_);
6323  HttpRequestHeaders headers;
6324  headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
6325  req.SetExtraRequestHeaders(headers);
6326  req.Start();
6327  base::RunLoop().Run();
6328  EXPECT_EQ(std::string("koi-8r"), d.data_received());
6329}
6330
6331// Check that default User-Agent header is sent.
6332TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
6333  ASSERT_TRUE(test_server_.Start());
6334
6335  TestDelegate d;
6336  URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
6337                 DEFAULT_PRIORITY,
6338                 &d,
6339                 &default_context_);
6340  req.Start();
6341  base::RunLoop().Run();
6342  EXPECT_EQ(req.context()->http_user_agent_settings()->GetUserAgent(),
6343            d.data_received());
6344}
6345
6346// Check that if request overrides the User-Agent header,
6347// the default is not appended.
6348TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
6349  ASSERT_TRUE(test_server_.Start());
6350
6351  TestDelegate d;
6352  URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
6353                 DEFAULT_PRIORITY,
6354                 &d,
6355                 &default_context_);
6356  HttpRequestHeaders headers;
6357  headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
6358  req.SetExtraRequestHeaders(headers);
6359  req.Start();
6360  base::RunLoop().Run();
6361  EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received());
6362}
6363
6364// Check that a NULL HttpUserAgentSettings causes the corresponding empty
6365// User-Agent header to be sent but does not send the Accept-Language and
6366// Accept-Charset headers.
6367TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
6368  ASSERT_TRUE(test_server_.Start());
6369
6370  TestNetworkDelegate network_delegate;  // Must outlive URLRequests.
6371  TestURLRequestContext context(true);
6372  context.set_network_delegate(&network_delegate);
6373  context.Init();
6374  // We override the HttpUserAgentSettings after initialization because empty
6375  // entries get overridden by Init().
6376  context.set_http_user_agent_settings(NULL);
6377
6378  struct {
6379    const char* request;
6380    const char* expected_response;
6381  } tests[] = { { "echoheader?Accept-Language", "None" },
6382                { "echoheader?Accept-Charset", "None" },
6383                { "echoheader?User-Agent", "" } };
6384
6385  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
6386    TestDelegate d;
6387    URLRequest req(
6388        test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d, &context);
6389    req.Start();
6390    base::RunLoop().Run();
6391    EXPECT_EQ(tests[i].expected_response, d.data_received())
6392        << " Request = \"" << tests[i].request << "\"";
6393  }
6394}
6395
6396// Make sure that URLRequest passes on its priority updates to
6397// newly-created jobs after the first one.
6398TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) {
6399  ASSERT_TRUE(test_server_.Start());
6400
6401  TestDelegate d;
6402  URLRequest req(test_server_.GetURL("empty.html"),
6403                 DEFAULT_PRIORITY,
6404                 &d,
6405                 &default_context_);
6406  EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
6407
6408  scoped_refptr<URLRequestRedirectJob> redirect_job =
6409      new URLRequestRedirectJob(
6410          &req, &default_network_delegate_, test_server_.GetURL("echo"),
6411          URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
6412  AddTestInterceptor()->set_main_intercept_job(redirect_job.get());
6413
6414  req.SetPriority(LOW);
6415  req.Start();
6416  EXPECT_TRUE(req.is_pending());
6417
6418  scoped_refptr<URLRequestTestJob> job =
6419      new URLRequestTestJob(&req, &default_network_delegate_);
6420  AddTestInterceptor()->set_main_intercept_job(job.get());
6421
6422  // Should trigger |job| to be started.
6423  base::RunLoop().Run();
6424  EXPECT_EQ(LOW, job->priority());
6425}
6426
6427// Check that creating a network request while entering/exiting suspend mode
6428// fails as it should.  This is the only case where an HttpTransactionFactory
6429// does not return an HttpTransaction.
6430TEST_F(URLRequestTestHTTP, NetworkSuspendTest) {
6431  // Create a new HttpNetworkLayer that thinks it's suspended.
6432  HttpNetworkSession::Params params;
6433  params.host_resolver = default_context_.host_resolver();
6434  params.cert_verifier = default_context_.cert_verifier();
6435  params.transport_security_state = default_context_.transport_security_state();
6436  params.proxy_service = default_context_.proxy_service();
6437  params.ssl_config_service = default_context_.ssl_config_service();
6438  params.http_auth_handler_factory =
6439      default_context_.http_auth_handler_factory();
6440  params.network_delegate = &default_network_delegate_;
6441  params.http_server_properties = default_context_.http_server_properties();
6442  scoped_ptr<HttpNetworkLayer> network_layer(
6443      new HttpNetworkLayer(new HttpNetworkSession(params)));
6444  network_layer->OnSuspend();
6445
6446  HttpCache http_cache(network_layer.release(), default_context_.net_log(),
6447                       HttpCache::DefaultBackend::InMemory(0));
6448
6449  TestURLRequestContext context(true);
6450  context.set_http_transaction_factory(&http_cache);
6451  context.Init();
6452
6453  TestDelegate d;
6454  URLRequest req(GURL("http://127.0.0.1/"),
6455                 DEFAULT_PRIORITY,
6456                 &d,
6457                 &context);
6458  req.Start();
6459  base::RunLoop().Run();
6460
6461  EXPECT_TRUE(d.request_failed());
6462  EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
6463  EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req.status().error());
6464}
6465
6466// Check that creating a network request while entering/exiting suspend mode
6467// fails as it should in the case there is no cache.  This is the only case
6468// where an HttpTransactionFactory does not return an HttpTransaction.
6469TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) {
6470  // Create a new HttpNetworkLayer that thinks it's suspended.
6471  HttpNetworkSession::Params params;
6472  params.host_resolver = default_context_.host_resolver();
6473  params.cert_verifier = default_context_.cert_verifier();
6474  params.transport_security_state = default_context_.transport_security_state();
6475  params.proxy_service = default_context_.proxy_service();
6476  params.ssl_config_service = default_context_.ssl_config_service();
6477  params.http_auth_handler_factory =
6478      default_context_.http_auth_handler_factory();
6479  params.network_delegate = &default_network_delegate_;
6480  params.http_server_properties = default_context_.http_server_properties();
6481  HttpNetworkLayer network_layer(new HttpNetworkSession(params));
6482  network_layer.OnSuspend();
6483
6484  TestURLRequestContext context(true);
6485  context.set_http_transaction_factory(&network_layer);
6486  context.Init();
6487
6488  TestDelegate d;
6489  URLRequest req(GURL("http://127.0.0.1/"),
6490                 DEFAULT_PRIORITY,
6491                 &d,
6492                 &context);
6493  req.Start();
6494  base::RunLoop().Run();
6495
6496  EXPECT_TRUE(d.request_failed());
6497  EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
6498  EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req.status().error());
6499}
6500
6501class HTTPSRequestTest : public testing::Test {
6502 public:
6503  HTTPSRequestTest() : default_context_(true) {
6504    default_context_.set_network_delegate(&default_network_delegate_);
6505    default_context_.Init();
6506  }
6507  virtual ~HTTPSRequestTest() {}
6508
6509 protected:
6510  TestNetworkDelegate default_network_delegate_;  // Must outlive URLRequest.
6511  TestURLRequestContext default_context_;
6512};
6513
6514TEST_F(HTTPSRequestTest, HTTPSGetTest) {
6515  SpawnedTestServer test_server(
6516      SpawnedTestServer::TYPE_HTTPS,
6517      SpawnedTestServer::kLocalhost,
6518      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6519  ASSERT_TRUE(test_server.Start());
6520
6521  TestDelegate d;
6522  {
6523    URLRequest r(test_server.GetURL(std::string()),
6524                 DEFAULT_PRIORITY,
6525                 &d,
6526                 &default_context_);
6527    r.Start();
6528    EXPECT_TRUE(r.is_pending());
6529
6530    base::RunLoop().Run();
6531
6532    EXPECT_EQ(1, d.response_started_count());
6533    EXPECT_FALSE(d.received_data_before_response());
6534    EXPECT_NE(0, d.bytes_received());
6535    CheckSSLInfo(r.ssl_info());
6536    EXPECT_EQ(test_server.host_port_pair().host(),
6537              r.GetSocketAddress().host());
6538    EXPECT_EQ(test_server.host_port_pair().port(),
6539              r.GetSocketAddress().port());
6540  }
6541}
6542
6543TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
6544  SpawnedTestServer::SSLOptions ssl_options(
6545      SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6546  SpawnedTestServer test_server(
6547      SpawnedTestServer::TYPE_HTTPS,
6548      ssl_options,
6549      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6550  ASSERT_TRUE(test_server.Start());
6551
6552  bool err_allowed = true;
6553  for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6554    TestDelegate d;
6555    {
6556      d.set_allow_certificate_errors(err_allowed);
6557      URLRequest r(test_server.GetURL(std::string()),
6558                   DEFAULT_PRIORITY,
6559                   &d,
6560                   &default_context_);
6561
6562      r.Start();
6563      EXPECT_TRUE(r.is_pending());
6564
6565      base::RunLoop().Run();
6566
6567      EXPECT_EQ(1, d.response_started_count());
6568      EXPECT_FALSE(d.received_data_before_response());
6569      EXPECT_TRUE(d.have_certificate_errors());
6570      if (err_allowed) {
6571        EXPECT_NE(0, d.bytes_received());
6572        CheckSSLInfo(r.ssl_info());
6573      } else {
6574        EXPECT_EQ(0, d.bytes_received());
6575      }
6576    }
6577  }
6578}
6579
6580TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
6581  SpawnedTestServer::SSLOptions ssl_options(
6582      SpawnedTestServer::SSLOptions::CERT_EXPIRED);
6583  SpawnedTestServer test_server(
6584      SpawnedTestServer::TYPE_HTTPS,
6585      ssl_options,
6586      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6587  ASSERT_TRUE(test_server.Start());
6588
6589  // Iterate from false to true, just so that we do the opposite of the
6590  // previous test in order to increase test coverage.
6591  bool err_allowed = false;
6592  for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6593    TestDelegate d;
6594    {
6595      d.set_allow_certificate_errors(err_allowed);
6596      URLRequest r(test_server.GetURL(std::string()),
6597                   DEFAULT_PRIORITY,
6598                   &d,
6599                   &default_context_);
6600
6601      r.Start();
6602      EXPECT_TRUE(r.is_pending());
6603
6604      base::RunLoop().Run();
6605
6606      EXPECT_EQ(1, d.response_started_count());
6607      EXPECT_FALSE(d.received_data_before_response());
6608      EXPECT_TRUE(d.have_certificate_errors());
6609      if (err_allowed) {
6610        EXPECT_NE(0, d.bytes_received());
6611        CheckSSLInfo(r.ssl_info());
6612      } else {
6613        EXPECT_EQ(0, d.bytes_received());
6614      }
6615    }
6616  }
6617}
6618
6619// This tests that a load of www.google.com with a certificate error sets
6620// the |certificate_errors_are_fatal| flag correctly. This flag will cause
6621// the interstitial to be fatal.
6622TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
6623  SpawnedTestServer::SSLOptions ssl_options(
6624      SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6625  SpawnedTestServer test_server(
6626      SpawnedTestServer::TYPE_HTTPS,
6627      ssl_options,
6628      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6629  ASSERT_TRUE(test_server.Start());
6630
6631  // We require that the URL be www.google.com in order to pick up the
6632  // preloaded HSTS entries in the TransportSecurityState. This means that we
6633  // have to use a MockHostResolver in order to direct www.google.com to the
6634  // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
6635
6636  MockHostResolver host_resolver;
6637  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
6638  TestURLRequestContext context(true);
6639  context.set_network_delegate(&network_delegate);
6640  context.set_host_resolver(&host_resolver);
6641  TransportSecurityState transport_security_state;
6642  context.set_transport_security_state(&transport_security_state);
6643  context.Init();
6644
6645  TestDelegate d;
6646  URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
6647                                       test_server.host_port_pair().port())),
6648               DEFAULT_PRIORITY,
6649               &d,
6650               &context);
6651
6652  r.Start();
6653  EXPECT_TRUE(r.is_pending());
6654
6655  base::RunLoop().Run();
6656
6657  EXPECT_EQ(1, d.response_started_count());
6658  EXPECT_FALSE(d.received_data_before_response());
6659  EXPECT_TRUE(d.have_certificate_errors());
6660  EXPECT_TRUE(d.certificate_errors_are_fatal());
6661}
6662
6663// This tests that cached HTTPS page loads do not cause any updates to the
6664// TransportSecurityState.
6665TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
6666  // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
6667  // matter. It just has to be any error.
6668  SpawnedTestServer::SSLOptions ssl_options(
6669      SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6670  SpawnedTestServer test_server(
6671      SpawnedTestServer::TYPE_HTTPS,
6672      ssl_options,
6673      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6674  ASSERT_TRUE(test_server.Start());
6675
6676  // We require that the URL be www.google.com in order to pick up the static
6677  // and dynamic STS and PKP entries in the TransportSecurityState. This means
6678  // that we have to use a MockHostResolver in order to direct www.google.com to
6679  // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
6680
6681  MockHostResolver host_resolver;
6682  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
6683  TestURLRequestContext context(true);
6684  context.set_network_delegate(&network_delegate);
6685  context.set_host_resolver(&host_resolver);
6686  TransportSecurityState transport_security_state;
6687
6688  TransportSecurityState::DomainState static_domain_state;
6689  EXPECT_TRUE(transport_security_state.GetStaticDomainState(
6690      "www.google.com", true, &static_domain_state));
6691  context.set_transport_security_state(&transport_security_state);
6692  context.Init();
6693
6694  TransportSecurityState::DomainState dynamic_domain_state;
6695  EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
6696      "www.google.com", &dynamic_domain_state));
6697
6698  TestDelegate d;
6699  URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
6700                                       test_server.host_port_pair().port())),
6701               DEFAULT_PRIORITY,
6702               &d,
6703               &context);
6704
6705  r.Start();
6706  EXPECT_TRUE(r.is_pending());
6707
6708  base::RunLoop().Run();
6709
6710  EXPECT_EQ(1, d.response_started_count());
6711  EXPECT_FALSE(d.received_data_before_response());
6712  EXPECT_TRUE(d.have_certificate_errors());
6713  EXPECT_TRUE(d.certificate_errors_are_fatal());
6714
6715  // Get a fresh copy of the states, and check that they haven't changed.
6716  TransportSecurityState::DomainState new_static_domain_state;
6717  EXPECT_TRUE(transport_security_state.GetStaticDomainState(
6718      "www.google.com", true, &new_static_domain_state));
6719  TransportSecurityState::DomainState new_dynamic_domain_state;
6720  EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
6721      "www.google.com", &new_dynamic_domain_state));
6722
6723  EXPECT_EQ(new_static_domain_state.sts.upgrade_mode,
6724            static_domain_state.sts.upgrade_mode);
6725  EXPECT_EQ(new_static_domain_state.sts.include_subdomains,
6726            static_domain_state.sts.include_subdomains);
6727  EXPECT_EQ(new_static_domain_state.pkp.include_subdomains,
6728            static_domain_state.pkp.include_subdomains);
6729  EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes,
6730                                static_domain_state.pkp.spki_hashes));
6731  EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes,
6732                                static_domain_state.pkp.bad_spki_hashes));
6733}
6734
6735// Make sure HSTS preserves a POST request's method and body.
6736TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
6737  static const char kData[] = "hello world";
6738
6739  SpawnedTestServer::SSLOptions ssl_options(
6740      SpawnedTestServer::SSLOptions::CERT_OK);
6741  SpawnedTestServer test_server(
6742      SpawnedTestServer::TYPE_HTTPS,
6743      ssl_options,
6744      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6745  ASSERT_TRUE(test_server.Start());
6746
6747
6748  // Per spec, TransportSecurityState expects a domain name, rather than an IP
6749  // address, so a MockHostResolver is needed to redirect www.somewhere.com to
6750  // the SpawnedTestServer.  By default, MockHostResolver maps all hosts
6751  // to 127.0.0.1.
6752  MockHostResolver host_resolver;
6753
6754  // Force https for www.somewhere.com.
6755  TransportSecurityState transport_security_state;
6756  base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000);
6757  bool include_subdomains = false;
6758  transport_security_state.AddHSTS("www.somewhere.com", expiry,
6759                                   include_subdomains);
6760
6761  TestNetworkDelegate network_delegate;  // Must outlive URLRequest.
6762
6763  TestURLRequestContext context(true);
6764  context.set_host_resolver(&host_resolver);
6765  context.set_transport_security_state(&transport_security_state);
6766  context.set_network_delegate(&network_delegate);
6767  context.Init();
6768
6769  TestDelegate d;
6770  // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
6771  // cause a certificate error.  Ignore the error.
6772  d.set_allow_certificate_errors(true);
6773
6774  URLRequest req(GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
6775                                         test_server.host_port_pair().port())),
6776                 DEFAULT_PRIORITY,
6777                 &d,
6778                 &context);
6779  req.set_method("POST");
6780  req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
6781
6782  req.Start();
6783  base::RunLoop().Run();
6784
6785  EXPECT_EQ("https", req.url().scheme());
6786  EXPECT_EQ("POST", req.method());
6787  EXPECT_EQ(kData, d.data_received());
6788
6789  LoadTimingInfo load_timing_info;
6790  network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info);
6791  // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
6792  TestLoadTimingCacheHitNoNetwork(load_timing_info);
6793}
6794
6795namespace {
6796
6797class SSLClientAuthTestDelegate : public TestDelegate {
6798 public:
6799  SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
6800  }
6801  virtual void OnCertificateRequested(
6802      URLRequest* request,
6803      SSLCertRequestInfo* cert_request_info) OVERRIDE {
6804    on_certificate_requested_count_++;
6805    base::MessageLoop::current()->Quit();
6806  }
6807  int on_certificate_requested_count() {
6808    return on_certificate_requested_count_;
6809  }
6810 private:
6811  int on_certificate_requested_count_;
6812};
6813
6814}  // namespace
6815
6816// TODO(davidben): Test the rest of the code. Specifically,
6817// - Filtering which certificates to select.
6818// - Sending a certificate back.
6819// - Getting a certificate request in an SSL renegotiation sending the
6820//   HTTP request.
6821TEST_F(HTTPSRequestTest, ClientAuthTest) {
6822  SpawnedTestServer::SSLOptions ssl_options;
6823  ssl_options.request_client_certificate = true;
6824  SpawnedTestServer test_server(
6825      SpawnedTestServer::TYPE_HTTPS,
6826      ssl_options,
6827      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6828  ASSERT_TRUE(test_server.Start());
6829
6830  SSLClientAuthTestDelegate d;
6831  {
6832    URLRequest r(test_server.GetURL(std::string()),
6833                 DEFAULT_PRIORITY,
6834                 &d,
6835                 &default_context_);
6836
6837    r.Start();
6838    EXPECT_TRUE(r.is_pending());
6839
6840    base::RunLoop().Run();
6841
6842    EXPECT_EQ(1, d.on_certificate_requested_count());
6843    EXPECT_FALSE(d.received_data_before_response());
6844    EXPECT_EQ(0, d.bytes_received());
6845
6846    // Send no certificate.
6847    // TODO(davidben): Get temporary client cert import (with keys) working on
6848    // all platforms so we can test sending a cert as well.
6849    r.ContinueWithCertificate(NULL);
6850
6851    base::RunLoop().Run();
6852
6853    EXPECT_EQ(1, d.response_started_count());
6854    EXPECT_FALSE(d.received_data_before_response());
6855    EXPECT_NE(0, d.bytes_received());
6856  }
6857}
6858
6859TEST_F(HTTPSRequestTest, ResumeTest) {
6860  // Test that we attempt a session resume when making two connections to the
6861  // same host.
6862  SpawnedTestServer::SSLOptions ssl_options;
6863  ssl_options.record_resume = true;
6864  SpawnedTestServer test_server(
6865      SpawnedTestServer::TYPE_HTTPS,
6866      ssl_options,
6867      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6868  ASSERT_TRUE(test_server.Start());
6869
6870  SSLClientSocket::ClearSessionCache();
6871
6872  {
6873    TestDelegate d;
6874    URLRequest r(test_server.GetURL("ssl-session-cache"),
6875                 DEFAULT_PRIORITY,
6876                 &d,
6877                 &default_context_);
6878
6879    r.Start();
6880    EXPECT_TRUE(r.is_pending());
6881
6882    base::RunLoop().Run();
6883
6884    EXPECT_EQ(1, d.response_started_count());
6885  }
6886
6887  reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
6888    CloseAllConnections();
6889
6890  {
6891    TestDelegate d;
6892    URLRequest r(test_server.GetURL("ssl-session-cache"),
6893                 DEFAULT_PRIORITY,
6894                 &d,
6895                 &default_context_);
6896
6897    r.Start();
6898    EXPECT_TRUE(r.is_pending());
6899
6900    base::RunLoop().Run();
6901
6902    // The response will look like;
6903    //   insert abc
6904    //   lookup abc
6905    //   insert xyz
6906    //
6907    // With a newline at the end which makes the split think that there are
6908    // four lines.
6909
6910    EXPECT_EQ(1, d.response_started_count());
6911    std::vector<std::string> lines;
6912    base::SplitString(d.data_received(), '\n', &lines);
6913    ASSERT_EQ(4u, lines.size()) << d.data_received();
6914
6915    std::string session_id;
6916
6917    for (size_t i = 0; i < 2; i++) {
6918      std::vector<std::string> parts;
6919      base::SplitString(lines[i], '\t', &parts);
6920      ASSERT_EQ(2u, parts.size());
6921      if (i == 0) {
6922        EXPECT_EQ("insert", parts[0]);
6923        session_id = parts[1];
6924      } else {
6925        EXPECT_EQ("lookup", parts[0]);
6926        EXPECT_EQ(session_id, parts[1]);
6927      }
6928    }
6929  }
6930}
6931
6932TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
6933  // Test that sessions aren't resumed when the value of ssl_session_cache_shard
6934  // differs.
6935  SpawnedTestServer::SSLOptions ssl_options;
6936  ssl_options.record_resume = true;
6937  SpawnedTestServer test_server(
6938      SpawnedTestServer::TYPE_HTTPS,
6939      ssl_options,
6940      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6941  ASSERT_TRUE(test_server.Start());
6942
6943  SSLClientSocket::ClearSessionCache();
6944
6945  {
6946    TestDelegate d;
6947    URLRequest r(test_server.GetURL("ssl-session-cache"),
6948                 DEFAULT_PRIORITY,
6949                 &d,
6950                 &default_context_);
6951
6952    r.Start();
6953    EXPECT_TRUE(r.is_pending());
6954
6955    base::RunLoop().Run();
6956
6957    EXPECT_EQ(1, d.response_started_count());
6958  }
6959
6960  // Now create a new HttpCache with a different ssl_session_cache_shard value.
6961  HttpNetworkSession::Params params;
6962  params.host_resolver = default_context_.host_resolver();
6963  params.cert_verifier = default_context_.cert_verifier();
6964  params.transport_security_state = default_context_.transport_security_state();
6965  params.proxy_service = default_context_.proxy_service();
6966  params.ssl_config_service = default_context_.ssl_config_service();
6967  params.http_auth_handler_factory =
6968      default_context_.http_auth_handler_factory();
6969  params.network_delegate = &default_network_delegate_;
6970  params.http_server_properties = default_context_.http_server_properties();
6971  params.ssl_session_cache_shard = "alternate";
6972
6973  scoped_ptr<net::HttpCache> cache(new net::HttpCache(
6974      new net::HttpNetworkSession(params),
6975      net::HttpCache::DefaultBackend::InMemory(0)));
6976
6977  default_context_.set_http_transaction_factory(cache.get());
6978
6979  {
6980    TestDelegate d;
6981    URLRequest r(test_server.GetURL("ssl-session-cache"),
6982                 DEFAULT_PRIORITY,
6983                 &d,
6984                 &default_context_);
6985
6986    r.Start();
6987    EXPECT_TRUE(r.is_pending());
6988
6989    base::RunLoop().Run();
6990
6991    // The response will look like;
6992    //   insert abc
6993    //   insert xyz
6994    //
6995    // With a newline at the end which makes the split think that there are
6996    // three lines.
6997
6998    EXPECT_EQ(1, d.response_started_count());
6999    std::vector<std::string> lines;
7000    base::SplitString(d.data_received(), '\n', &lines);
7001    ASSERT_EQ(3u, lines.size());
7002
7003    std::string session_id;
7004    for (size_t i = 0; i < 2; i++) {
7005      std::vector<std::string> parts;
7006      base::SplitString(lines[i], '\t', &parts);
7007      ASSERT_EQ(2u, parts.size());
7008      EXPECT_EQ("insert", parts[0]);
7009      if (i == 0) {
7010        session_id = parts[1];
7011      } else {
7012        EXPECT_NE(session_id, parts[1]);
7013      }
7014    }
7015  }
7016}
7017
7018class HTTPSFallbackTest : public testing::Test {
7019 public:
7020  HTTPSFallbackTest() : context_(true) {
7021    context_.Init();
7022    delegate_.set_allow_certificate_errors(true);
7023  }
7024  virtual ~HTTPSFallbackTest() {}
7025
7026 protected:
7027  void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) {
7028    DCHECK(!request_);
7029    SpawnedTestServer test_server(
7030        SpawnedTestServer::TYPE_HTTPS,
7031        ssl_options,
7032        base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7033    ASSERT_TRUE(test_server.Start());
7034
7035    request_.reset(new URLRequest(
7036        test_server.GetURL(std::string()), DEFAULT_PRIORITY,
7037        &delegate_, &context_));
7038    request_->Start();
7039
7040    base::RunLoop().Run();
7041  }
7042
7043  void ExpectConnection(int version) {
7044    EXPECT_EQ(1, delegate_.response_started_count());
7045    EXPECT_NE(0, delegate_.bytes_received());
7046    EXPECT_EQ(version, SSLConnectionStatusToVersion(
7047        request_->ssl_info().connection_status));
7048    EXPECT_TRUE(request_->ssl_info().connection_status &
7049                SSL_CONNECTION_VERSION_FALLBACK);
7050  }
7051
7052  void ExpectFailure(int error) {
7053    EXPECT_EQ(1, delegate_.response_started_count());
7054    EXPECT_FALSE(request_->status().is_success());
7055    EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status());
7056    EXPECT_EQ(error, request_->status().error());
7057  }
7058
7059 private:
7060  TestDelegate delegate_;
7061  TestURLRequestContext context_;
7062  scoped_ptr<URLRequest> request_;
7063};
7064
7065// Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
7066// than necessary.
7067TEST_F(HTTPSFallbackTest, TLSv1Fallback) {
7068  SpawnedTestServer::SSLOptions ssl_options(
7069      SpawnedTestServer::SSLOptions::CERT_OK);
7070  ssl_options.tls_intolerant =
7071      SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7072
7073  ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7074  ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7075}
7076
7077// This test is disabled on Android because the remote test server doesn't cause
7078// a TCP reset.
7079#if !defined(OS_ANDROID)
7080// Tests fallback to TLS 1.0 on connection reset.
7081TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) {
7082  SpawnedTestServer::SSLOptions ssl_options(
7083      SpawnedTestServer::SSLOptions::CERT_OK);
7084  ssl_options.tls_intolerant =
7085      SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7086  ssl_options.tls_intolerance_type =
7087      SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7088
7089  ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7090  ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7091}
7092#endif  // !OS_ANDROID
7093
7094// Tests that we don't fallback on handshake failure with servers that implement
7095// TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
7096TEST_F(HTTPSFallbackTest, FallbackSCSV) {
7097  SpawnedTestServer::SSLOptions ssl_options(
7098      SpawnedTestServer::SSLOptions::CERT_OK);
7099  // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
7100  // a version fallback.
7101  ssl_options.tls_intolerant =
7102      SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7103  // Have the server process TLS_FALLBACK_SCSV so that version fallback
7104  // connections are rejected.
7105  ssl_options.fallback_scsv_enabled = true;
7106
7107  ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7108
7109  // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version
7110  // intolerance. If the fallback SCSV is processed when the original error
7111  // that caused the fallback should be returned, which should be
7112  // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
7113  ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH);
7114}
7115
7116// Tests that we don't fallback on connection closed with servers that implement
7117// TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
7118TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) {
7119  SpawnedTestServer::SSLOptions ssl_options(
7120      SpawnedTestServer::SSLOptions::CERT_OK);
7121  // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
7122  // a version fallback.
7123  ssl_options.tls_intolerant =
7124      SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7125  ssl_options.tls_intolerance_type =
7126      SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
7127  // Have the server process TLS_FALLBACK_SCSV so that version fallback
7128  // connections are rejected.
7129  ssl_options.fallback_scsv_enabled = true;
7130
7131  ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7132
7133  // The original error should be replayed on rejected fallback.
7134  ExpectFailure(ERR_CONNECTION_CLOSED);
7135}
7136
7137// Tests that the SSLv3 fallback triggers on alert.
7138TEST_F(HTTPSFallbackTest, SSLv3Fallback) {
7139  SpawnedTestServer::SSLOptions ssl_options(
7140      SpawnedTestServer::SSLOptions::CERT_OK);
7141  ssl_options.tls_intolerant =
7142      SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7143
7144  ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7145  ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
7146}
7147
7148// Tests that the SSLv3 fallback triggers on closed connections.
7149TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) {
7150  SpawnedTestServer::SSLOptions ssl_options(
7151      SpawnedTestServer::SSLOptions::CERT_OK);
7152  ssl_options.tls_intolerant =
7153      SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7154  ssl_options.tls_intolerance_type =
7155      SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
7156
7157  ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7158  ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
7159}
7160
7161// This test is disabled on Android because the remote test server doesn't cause
7162// a TCP reset.
7163#if !defined(OS_ANDROID)
7164// Tests that a reset connection does not fallback down to SSL3.
7165TEST_F(HTTPSFallbackTest, SSLv3NoFallbackReset) {
7166  SpawnedTestServer::SSLOptions ssl_options(
7167      SpawnedTestServer::SSLOptions::CERT_OK);
7168  ssl_options.tls_intolerant =
7169      SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7170  ssl_options.tls_intolerance_type =
7171      SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7172
7173  ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7174  ExpectFailure(ERR_CONNECTION_RESET);
7175}
7176#endif  // !OS_ANDROID
7177
7178class HTTPSSessionTest : public testing::Test {
7179 public:
7180  HTTPSSessionTest() : default_context_(true) {
7181    cert_verifier_.set_default_result(net::OK);
7182
7183    default_context_.set_network_delegate(&default_network_delegate_);
7184    default_context_.set_cert_verifier(&cert_verifier_);
7185    default_context_.Init();
7186  }
7187  virtual ~HTTPSSessionTest() {}
7188
7189 protected:
7190  MockCertVerifier cert_verifier_;
7191  TestNetworkDelegate default_network_delegate_;  // Must outlive URLRequest.
7192  TestURLRequestContext default_context_;
7193};
7194
7195// Tests that session resumption is not attempted if an invalid certificate
7196// is presented.
7197TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) {
7198  SpawnedTestServer::SSLOptions ssl_options;
7199  ssl_options.record_resume = true;
7200  SpawnedTestServer test_server(
7201      SpawnedTestServer::TYPE_HTTPS,
7202      ssl_options,
7203      base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7204  ASSERT_TRUE(test_server.Start());
7205
7206  SSLClientSocket::ClearSessionCache();
7207
7208  // Simulate the certificate being expired and attempt a connection.
7209  cert_verifier_.set_default_result(net::ERR_CERT_DATE_INVALID);
7210  {
7211    TestDelegate d;
7212    URLRequest r(test_server.GetURL("ssl-session-cache"),
7213                 DEFAULT_PRIORITY,
7214                 &d,
7215                 &default_context_);
7216
7217    r.Start();
7218    EXPECT_TRUE(r.is_pending());
7219
7220    base::RunLoop().Run();
7221
7222    EXPECT_EQ(1, d.response_started_count());
7223  }
7224
7225  reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
7226    CloseAllConnections();
7227
7228  // Now change the certificate to be acceptable (so that the response is
7229  // loaded), and ensure that no session id is presented to the peer.
7230  cert_verifier_.set_default_result(net::OK);
7231  {
7232    TestDelegate d;
7233    URLRequest r(test_server.GetURL("ssl-session-cache"),
7234                 DEFAULT_PRIORITY,
7235                 &d,
7236                 &default_context_);
7237
7238    r.Start();
7239    EXPECT_TRUE(r.is_pending());
7240
7241    base::RunLoop().Run();
7242
7243    // The response will look like;
7244    //   insert abc
7245    //   insert xyz
7246    //
7247    // With a newline at the end which makes the split think that there are
7248    // three lines.
7249    //
7250    // If a session was presented (eg: a bug), then the response would look
7251    // like;
7252    //   insert abc
7253    //   lookup abc
7254    //   insert xyz
7255
7256    EXPECT_EQ(1, d.response_started_count());
7257    std::vector<std::string> lines;
7258    base::SplitString(d.data_received(), '\n', &lines);
7259    ASSERT_EQ(3u, lines.size()) << d.data_received();
7260
7261    std::string session_id;
7262    for (size_t i = 0; i < 2; i++) {
7263      std::vector<std::string> parts;
7264      base::SplitString(lines[i], '\t', &parts);
7265      ASSERT_EQ(2u, parts.size());
7266      EXPECT_EQ("insert", parts[0]);
7267      if (i == 0) {
7268        session_id = parts[1];
7269      } else {
7270        EXPECT_NE(session_id, parts[1]);
7271      }
7272    }
7273  }
7274}
7275
7276class TestSSLConfigService : public SSLConfigService {
7277 public:
7278  TestSSLConfigService(bool ev_enabled,
7279                       bool online_rev_checking,
7280                       bool rev_checking_required_local_anchors)
7281      : ev_enabled_(ev_enabled),
7282        online_rev_checking_(online_rev_checking),
7283        rev_checking_required_local_anchors_(
7284            rev_checking_required_local_anchors) {}
7285
7286  // SSLConfigService:
7287  virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
7288    *config = SSLConfig();
7289    config->rev_checking_enabled = online_rev_checking_;
7290    config->verify_ev_cert = ev_enabled_;
7291    config->rev_checking_required_local_anchors =
7292        rev_checking_required_local_anchors_;
7293  }
7294
7295 protected:
7296  virtual ~TestSSLConfigService() {}
7297
7298 private:
7299  const bool ev_enabled_;
7300  const bool online_rev_checking_;
7301  const bool rev_checking_required_local_anchors_;
7302};
7303
7304// This the fingerprint of the "Testing CA" certificate used by the testserver.
7305// See net/data/ssl/certificates/ocsp-test-root.pem.
7306static const SHA1HashValue kOCSPTestCertFingerprint =
7307  { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
7308      0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
7309
7310// This is the SHA256, SPKI hash of the "Testing CA" certificate used by the
7311// testserver.
7312static const SHA256HashValue kOCSPTestCertSPKI = { {
7313  0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e,
7314  0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49,
7315  0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74,
7316  0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28,
7317} };
7318
7319// This is the policy OID contained in the certificates that testserver
7320// generates.
7321static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
7322
7323class HTTPSOCSPTest : public HTTPSRequestTest {
7324 public:
7325  HTTPSOCSPTest()
7326      : context_(true),
7327        ev_test_policy_(
7328            new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
7329                                   kOCSPTestCertFingerprint,
7330                                   kOCSPTestCertPolicy)) {
7331  }
7332
7333  virtual void SetUp() OVERRIDE {
7334    SetupContext(&context_);
7335    context_.Init();
7336
7337    scoped_refptr<net::X509Certificate> root_cert =
7338        ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
7339    CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert);
7340    test_root_.reset(new ScopedTestRoot(root_cert.get()));
7341
7342#if defined(USE_NSS) || defined(OS_IOS)
7343    SetURLRequestContextForNSSHttpIO(&context_);
7344    EnsureNSSHttpIOInit();
7345#endif
7346  }
7347
7348  void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options,
7349                    CertStatus* out_cert_status) {
7350    // We always overwrite out_cert_status.
7351    *out_cert_status = 0;
7352    SpawnedTestServer test_server(
7353        SpawnedTestServer::TYPE_HTTPS,
7354        ssl_options,
7355        base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7356    ASSERT_TRUE(test_server.Start());
7357
7358    TestDelegate d;
7359    d.set_allow_certificate_errors(true);
7360    URLRequest r(
7361        test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context_);
7362    r.Start();
7363
7364    base::RunLoop().Run();
7365
7366    EXPECT_EQ(1, d.response_started_count());
7367    *out_cert_status = r.ssl_info().cert_status;
7368  }
7369
7370  virtual ~HTTPSOCSPTest() {
7371#if defined(USE_NSS) || defined(OS_IOS)
7372    ShutdownNSSHttpIO();
7373#endif
7374  }
7375
7376 protected:
7377  // SetupContext configures the URLRequestContext that will be used for making
7378  // connetions to testserver. This can be overridden in test subclasses for
7379  // different behaviour.
7380  virtual void SetupContext(URLRequestContext* context) {
7381    context->set_ssl_config_service(
7382        new TestSSLConfigService(true /* check for EV */,
7383                                 true /* online revocation checking */,
7384                                 false /* require rev. checking for local
7385                                          anchors */));
7386  }
7387
7388  scoped_ptr<ScopedTestRoot> test_root_;
7389  TestURLRequestContext context_;
7390  scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
7391};
7392
7393static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
7394#if defined(OS_WIN)
7395  // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
7396  // have that ability on other platforms.
7397  return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
7398#else
7399  return 0;
7400#endif
7401}
7402
7403// SystemSupportsHardFailRevocationChecking returns true iff the current
7404// operating system supports revocation checking and can distinguish between
7405// situations where a given certificate lacks any revocation information (eg:
7406// no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when
7407// revocation information cannot be obtained (eg: the CRL was unreachable).
7408// If it does not, then tests which rely on 'hard fail' behaviour should be
7409// skipped.
7410static bool SystemSupportsHardFailRevocationChecking() {
7411#if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS)
7412  return true;
7413#else
7414  return false;
7415#endif
7416}
7417
7418// SystemUsesChromiumEVMetadata returns true iff the current operating system
7419// uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
7420// several tests are effected because our testing EV certificate won't be
7421// recognised as EV.
7422static bool SystemUsesChromiumEVMetadata() {
7423#if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID)
7424  // http://crbug.com/117478 - OpenSSL does not support EV validation.
7425  return false;
7426#elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
7427  // On OS X and Android, we use the system to tell us whether a certificate is
7428  // EV or not and the system won't recognise our testing root.
7429  return false;
7430#else
7431  return true;
7432#endif
7433}
7434
7435static bool SystemSupportsOCSP() {
7436#if defined(USE_OPENSSL)
7437  // http://crbug.com/117478 - OpenSSL does not support OCSP.
7438  return false;
7439#elif defined(OS_WIN)
7440  return base::win::GetVersion() >= base::win::VERSION_VISTA;
7441#elif defined(OS_ANDROID)
7442  // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
7443  return false;
7444#else
7445  return true;
7446#endif
7447}
7448
7449TEST_F(HTTPSOCSPTest, Valid) {
7450  if (!SystemSupportsOCSP()) {
7451    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7452    return;
7453  }
7454
7455  SpawnedTestServer::SSLOptions ssl_options(
7456      SpawnedTestServer::SSLOptions::CERT_AUTO);
7457  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
7458
7459  CertStatus cert_status;
7460  DoConnection(ssl_options, &cert_status);
7461
7462  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7463
7464  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7465            static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
7466
7467  EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7468}
7469
7470TEST_F(HTTPSOCSPTest, Revoked) {
7471  if (!SystemSupportsOCSP()) {
7472    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7473    return;
7474  }
7475
7476  SpawnedTestServer::SSLOptions ssl_options(
7477      SpawnedTestServer::SSLOptions::CERT_AUTO);
7478  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
7479
7480  CertStatus cert_status;
7481  DoConnection(ssl_options, &cert_status);
7482
7483#if !(defined(OS_MACOSX) && !defined(OS_IOS))
7484  // Doesn't pass on OS X yet for reasons that need to be investigated.
7485  EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7486#endif
7487  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7488  EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7489}
7490
7491TEST_F(HTTPSOCSPTest, Invalid) {
7492  if (!SystemSupportsOCSP()) {
7493    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7494    return;
7495  }
7496
7497  SpawnedTestServer::SSLOptions ssl_options(
7498      SpawnedTestServer::SSLOptions::CERT_AUTO);
7499  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7500
7501  CertStatus cert_status;
7502  DoConnection(ssl_options, &cert_status);
7503
7504  EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7505            cert_status & CERT_STATUS_ALL_ERRORS);
7506
7507  // Without a positive OCSP response, we shouldn't show the EV status.
7508  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7509  EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7510}
7511
7512class HTTPSHardFailTest : public HTTPSOCSPTest {
7513 protected:
7514  virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7515    context->set_ssl_config_service(
7516        new TestSSLConfigService(false /* check for EV */,
7517                                 false /* online revocation checking */,
7518                                 true /* require rev. checking for local
7519                                         anchors */));
7520  }
7521};
7522
7523
7524TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) {
7525  if (!SystemSupportsOCSP()) {
7526    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7527    return;
7528  }
7529
7530  if (!SystemSupportsHardFailRevocationChecking()) {
7531    LOG(WARNING) << "Skipping test because system doesn't support hard fail "
7532                 << "revocation checking";
7533    return;
7534  }
7535
7536  SpawnedTestServer::SSLOptions ssl_options(
7537      SpawnedTestServer::SSLOptions::CERT_AUTO);
7538  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7539
7540  CertStatus cert_status;
7541  DoConnection(ssl_options, &cert_status);
7542
7543  EXPECT_EQ(CERT_STATUS_REVOKED,
7544            cert_status & CERT_STATUS_REVOKED);
7545
7546  // Without a positive OCSP response, we shouldn't show the EV status.
7547  EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7548}
7549
7550class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
7551 protected:
7552  virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7553    context->set_ssl_config_service(
7554        new TestSSLConfigService(true /* check for EV */,
7555                                 false /* online revocation checking */,
7556                                 false /* require rev. checking for local
7557                                          anchors */));
7558  }
7559};
7560
7561TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
7562  if (!SystemSupportsOCSP()) {
7563    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7564    return;
7565  }
7566
7567  SpawnedTestServer::SSLOptions ssl_options(
7568      SpawnedTestServer::SSLOptions::CERT_AUTO);
7569  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7570  SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7571
7572  CertStatus cert_status;
7573  DoConnection(ssl_options, &cert_status);
7574
7575  EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7576            cert_status & CERT_STATUS_ALL_ERRORS);
7577
7578  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7579  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7580            static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7581}
7582
7583TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) {
7584  if (!SystemSupportsOCSP()) {
7585    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7586    return;
7587  }
7588
7589  SpawnedTestServer::SSLOptions ssl_options(
7590      SpawnedTestServer::SSLOptions::CERT_AUTO);
7591  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
7592  SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7593
7594  CertStatus cert_status;
7595  DoConnection(ssl_options, &cert_status);
7596
7597  // Currently only works for Windows. When using NSS or OS X, it's not
7598  // possible to determine whether the check failed because of actual
7599  // revocation or because there was an OCSP failure.
7600#if defined(OS_WIN)
7601  EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7602#else
7603  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7604#endif
7605
7606  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7607  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7608            static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7609}
7610
7611TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
7612  if (!SystemSupportsOCSP()) {
7613    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7614    return;
7615  }
7616
7617  SpawnedTestServer::SSLOptions ssl_options(
7618      SpawnedTestServer::SSLOptions::CERT_AUTO);
7619  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
7620  SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7621
7622  CertStatus cert_status;
7623  DoConnection(ssl_options, &cert_status);
7624
7625  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7626
7627  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7628            static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
7629  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7630            static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7631}
7632
7633TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
7634  if (!SystemSupportsOCSP()) {
7635    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7636    return;
7637  }
7638
7639  SpawnedTestServer::SSLOptions ssl_options(
7640      SpawnedTestServer::SSLOptions::CERT_AUTO);
7641  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7642  SSLConfigService::SetCRLSet(
7643      scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7644
7645  CertStatus cert_status;
7646  DoConnection(ssl_options, &cert_status);
7647
7648  EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7649            cert_status & CERT_STATUS_ALL_ERRORS);
7650
7651  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7652  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7653            static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7654}
7655
7656TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) {
7657  if (!SystemSupportsOCSP()) {
7658    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7659    return;
7660  }
7661
7662  SpawnedTestServer::SSLOptions ssl_options(
7663      SpawnedTestServer::SSLOptions::CERT_AUTO);
7664  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7665  SSLConfigService::SetCRLSet(
7666      scoped_refptr<CRLSet>(CRLSet::ForTesting(
7667          false, &kOCSPTestCertSPKI, "")));
7668
7669  CertStatus cert_status;
7670  DoConnection(ssl_options, &cert_status);
7671
7672  // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a
7673  // revocation check for EV.
7674  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7675  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7676            static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
7677  EXPECT_FALSE(
7678      static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7679}
7680
7681TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) {
7682  if (!SystemSupportsOCSP()) {
7683    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7684    return;
7685  }
7686
7687  SpawnedTestServer::SSLOptions ssl_options(
7688      SpawnedTestServer::SSLOptions::CERT_AUTO);
7689  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7690  SSLConfigService::SetCRLSet(
7691      scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
7692
7693  CertStatus cert_status = 0;
7694  DoConnection(ssl_options, &cert_status);
7695
7696  // Even with a fresh CRLSet, we should still do online revocation checks when
7697  // the certificate chain isn't covered by the CRLSet, which it isn't in this
7698  // test.
7699  EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7700            cert_status & CERT_STATUS_ALL_ERRORS);
7701
7702  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7703  EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7704            static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7705}
7706
7707TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
7708  // Test that when EV verification is requested, but online revocation
7709  // checking is disabled, and the leaf certificate is not in fact EV, that
7710  // no revocation checking actually happens.
7711  if (!SystemSupportsOCSP()) {
7712    LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7713    return;
7714  }
7715
7716  // Unmark the certificate's OID as EV, which should disable revocation
7717  // checking (as per the user preference)
7718  ev_test_policy_.reset();
7719
7720  SpawnedTestServer::SSLOptions ssl_options(
7721      SpawnedTestServer::SSLOptions::CERT_AUTO);
7722  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
7723  SSLConfigService::SetCRLSet(
7724      scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7725
7726  CertStatus cert_status;
7727  DoConnection(ssl_options, &cert_status);
7728
7729  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7730
7731  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7732  EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7733}
7734
7735class HTTPSCRLSetTest : public HTTPSOCSPTest {
7736 protected:
7737  virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7738    context->set_ssl_config_service(
7739        new TestSSLConfigService(false /* check for EV */,
7740                                 false /* online revocation checking */,
7741                                 false /* require rev. checking for local
7742                                          anchors */));
7743  }
7744};
7745
7746TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
7747  SpawnedTestServer::SSLOptions ssl_options(
7748      SpawnedTestServer::SSLOptions::CERT_AUTO);
7749  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7750  SSLConfigService::SetCRLSet(
7751      scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7752
7753  CertStatus cert_status;
7754  DoConnection(ssl_options, &cert_status);
7755
7756  // If we're not trying EV verification then, even if the CRLSet has expired,
7757  // we don't fall back to online revocation checks.
7758  EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7759  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7760  EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7761}
7762
7763TEST_F(HTTPSCRLSetTest, CRLSetRevoked) {
7764#if defined(USE_OPENSSL)
7765  LOG(WARNING) << "Skipping test because system doesn't support CRLSets";
7766  return;
7767#endif
7768
7769  SpawnedTestServer::SSLOptions ssl_options(
7770      SpawnedTestServer::SSLOptions::CERT_AUTO);
7771  ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
7772  ssl_options.cert_serial = 10;
7773  SSLConfigService::SetCRLSet(
7774      scoped_refptr<CRLSet>(CRLSet::ForTesting(
7775          false, &kOCSPTestCertSPKI, "\x0a")));
7776
7777  CertStatus cert_status = 0;
7778  DoConnection(ssl_options, &cert_status);
7779
7780  // If the certificate is recorded as revoked in the CRLSet, that should be
7781  // reflected without online revocation checking.
7782  EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7783  EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7784  EXPECT_FALSE(
7785      static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7786}
7787#endif  // !defined(OS_IOS)
7788
7789#if !defined(DISABLE_FTP_SUPPORT)
7790class URLRequestTestFTP : public URLRequestTest {
7791 public:
7792  URLRequestTestFTP()
7793      : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost,
7794                     base::FilePath()) {
7795  }
7796
7797 protected:
7798  SpawnedTestServer test_server_;
7799};
7800
7801// Make sure an FTP request using an unsafe ports fails.
7802TEST_F(URLRequestTestFTP, UnsafePort) {
7803  ASSERT_TRUE(test_server_.Start());
7804
7805  URLRequestJobFactoryImpl job_factory;
7806  FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver());
7807
7808  GURL url("ftp://127.0.0.1:7");
7809  job_factory.SetProtocolHandler(
7810      "ftp",
7811      new FtpProtocolHandler(&ftp_transaction_factory));
7812  default_context_.set_job_factory(&job_factory);
7813
7814  TestDelegate d;
7815  {
7816    URLRequest r(url, DEFAULT_PRIORITY, &d, &default_context_);
7817    r.Start();
7818    EXPECT_TRUE(r.is_pending());
7819
7820    base::RunLoop().Run();
7821
7822    EXPECT_FALSE(r.is_pending());
7823    EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
7824    EXPECT_EQ(ERR_UNSAFE_PORT, r.status().error());
7825  }
7826}
7827
7828// Flaky, see http://crbug.com/25045.
7829TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
7830  ASSERT_TRUE(test_server_.Start());
7831
7832  TestDelegate d;
7833  {
7834    URLRequest r(
7835        test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, &default_context_);
7836    r.Start();
7837    EXPECT_TRUE(r.is_pending());
7838
7839    base::RunLoop().Run();
7840
7841    EXPECT_FALSE(r.is_pending());
7842    EXPECT_EQ(1, d.response_started_count());
7843    EXPECT_FALSE(d.received_data_before_response());
7844    EXPECT_LT(0, d.bytes_received());
7845    EXPECT_EQ(test_server_.host_port_pair().host(),
7846              r.GetSocketAddress().host());
7847    EXPECT_EQ(test_server_.host_port_pair().port(),
7848              r.GetSocketAddress().port());
7849  }
7850}
7851
7852// Flaky, see http://crbug.com/25045.
7853TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
7854  ASSERT_TRUE(test_server_.Start());
7855
7856  base::FilePath app_path;
7857  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7858  app_path = app_path.AppendASCII("LICENSE");
7859  TestDelegate d;
7860  {
7861    URLRequest r(test_server_.GetURL("/LICENSE"),
7862                 DEFAULT_PRIORITY,
7863                 &d,
7864                 &default_context_);
7865    r.Start();
7866    EXPECT_TRUE(r.is_pending());
7867
7868    base::RunLoop().Run();
7869
7870    int64 file_size = 0;
7871    base::GetFileSize(app_path, &file_size);
7872
7873    EXPECT_FALSE(r.is_pending());
7874    EXPECT_EQ(1, d.response_started_count());
7875    EXPECT_FALSE(d.received_data_before_response());
7876    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
7877    EXPECT_EQ(test_server_.host_port_pair().host(),
7878              r.GetSocketAddress().host());
7879    EXPECT_EQ(test_server_.host_port_pair().port(),
7880              r.GetSocketAddress().port());
7881  }
7882}
7883
7884// Flaky, see http://crbug.com/25045.
7885TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
7886  ASSERT_TRUE(test_server_.Start());
7887
7888  base::FilePath app_path;
7889  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7890  app_path = app_path.AppendASCII("LICENSE");
7891  TestDelegate d;
7892  {
7893    URLRequest r(
7894        test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
7895        DEFAULT_PRIORITY,
7896        &d,
7897        &default_context_);
7898    r.Start();
7899    EXPECT_TRUE(r.is_pending());
7900
7901    base::RunLoop().Run();
7902
7903    int64 file_size = 0;
7904    base::GetFileSize(app_path, &file_size);
7905
7906    EXPECT_FALSE(r.is_pending());
7907    EXPECT_EQ(test_server_.host_port_pair().host(),
7908              r.GetSocketAddress().host());
7909    EXPECT_EQ(test_server_.host_port_pair().port(),
7910              r.GetSocketAddress().port());
7911    EXPECT_EQ(1, d.response_started_count());
7912    EXPECT_FALSE(d.received_data_before_response());
7913    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
7914
7915    LoadTimingInfo load_timing_info;
7916    r.GetLoadTimingInfo(&load_timing_info);
7917    TestLoadTimingNoHttpResponse(load_timing_info);
7918  }
7919}
7920
7921// Flaky, see http://crbug.com/25045.
7922TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
7923  ASSERT_TRUE(test_server_.Start());
7924
7925  base::FilePath app_path;
7926  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7927  app_path = app_path.AppendASCII("LICENSE");
7928  TestDelegate d;
7929  {
7930    URLRequest r(test_server_.GetURLWithUserAndPassword(
7931                     "/LICENSE", "chrome", "wrong_password"),
7932                 DEFAULT_PRIORITY,
7933                 &d,
7934                 &default_context_);
7935    r.Start();
7936    EXPECT_TRUE(r.is_pending());
7937
7938    base::RunLoop().Run();
7939
7940    int64 file_size = 0;
7941    base::GetFileSize(app_path, &file_size);
7942
7943    EXPECT_FALSE(r.is_pending());
7944    EXPECT_EQ(1, d.response_started_count());
7945    EXPECT_FALSE(d.received_data_before_response());
7946    EXPECT_EQ(d.bytes_received(), 0);
7947  }
7948}
7949
7950// Flaky, see http://crbug.com/25045.
7951TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
7952  ASSERT_TRUE(test_server_.Start());
7953
7954  base::FilePath app_path;
7955  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7956  app_path = app_path.AppendASCII("LICENSE");
7957  TestDelegate d;
7958  // Set correct login credentials. The delegate will be asked for them when
7959  // the initial login with wrong credentials will fail.
7960  d.set_credentials(AuthCredentials(kChrome, kChrome));
7961  {
7962    URLRequest r(test_server_.GetURLWithUserAndPassword(
7963                     "/LICENSE", "chrome", "wrong_password"),
7964                 DEFAULT_PRIORITY,
7965                 &d,
7966                 &default_context_);
7967    r.Start();
7968    EXPECT_TRUE(r.is_pending());
7969
7970    base::RunLoop().Run();
7971
7972    int64 file_size = 0;
7973    base::GetFileSize(app_path, &file_size);
7974
7975    EXPECT_FALSE(r.is_pending());
7976    EXPECT_EQ(1, d.response_started_count());
7977    EXPECT_FALSE(d.received_data_before_response());
7978    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
7979  }
7980}
7981
7982// Flaky, see http://crbug.com/25045.
7983TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
7984  ASSERT_TRUE(test_server_.Start());
7985
7986  base::FilePath app_path;
7987  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7988  app_path = app_path.AppendASCII("LICENSE");
7989  TestDelegate d;
7990  {
7991    URLRequest r(test_server_.GetURLWithUserAndPassword(
7992                     "/LICENSE", "wrong_user", "chrome"),
7993                 DEFAULT_PRIORITY,
7994                 &d,
7995                 &default_context_);
7996    r.Start();
7997    EXPECT_TRUE(r.is_pending());
7998
7999    base::RunLoop().Run();
8000
8001    int64 file_size = 0;
8002    base::GetFileSize(app_path, &file_size);
8003
8004    EXPECT_FALSE(r.is_pending());
8005    EXPECT_EQ(1, d.response_started_count());
8006    EXPECT_FALSE(d.received_data_before_response());
8007    EXPECT_EQ(d.bytes_received(), 0);
8008  }
8009}
8010
8011// Flaky, see http://crbug.com/25045.
8012TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
8013  ASSERT_TRUE(test_server_.Start());
8014
8015  base::FilePath app_path;
8016  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8017  app_path = app_path.AppendASCII("LICENSE");
8018  TestDelegate d;
8019  // Set correct login credentials. The delegate will be asked for them when
8020  // the initial login with wrong credentials will fail.
8021  d.set_credentials(AuthCredentials(kChrome, kChrome));
8022  {
8023    URLRequest r(test_server_.GetURLWithUserAndPassword(
8024                     "/LICENSE", "wrong_user", "chrome"),
8025                 DEFAULT_PRIORITY,
8026                 &d,
8027                 &default_context_);
8028    r.Start();
8029    EXPECT_TRUE(r.is_pending());
8030
8031    base::RunLoop().Run();
8032
8033    int64 file_size = 0;
8034    base::GetFileSize(app_path, &file_size);
8035
8036    EXPECT_FALSE(r.is_pending());
8037    EXPECT_EQ(1, d.response_started_count());
8038    EXPECT_FALSE(d.received_data_before_response());
8039    EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
8040  }
8041}
8042
8043// Flaky, see http://crbug.com/25045.
8044TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
8045  ASSERT_TRUE(test_server_.Start());
8046
8047  base::FilePath app_path;
8048  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8049  app_path = app_path.AppendASCII("LICENSE");
8050
8051  scoped_ptr<TestDelegate> d(new TestDelegate);
8052  {
8053    // Pass correct login identity in the URL.
8054    URLRequest r(
8055        test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
8056        DEFAULT_PRIORITY,
8057        d.get(),
8058        &default_context_);
8059    r.Start();
8060    EXPECT_TRUE(r.is_pending());
8061
8062    base::RunLoop().Run();
8063
8064    int64 file_size = 0;
8065    base::GetFileSize(app_path, &file_size);
8066
8067    EXPECT_FALSE(r.is_pending());
8068    EXPECT_EQ(1, d->response_started_count());
8069    EXPECT_FALSE(d->received_data_before_response());
8070    EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8071  }
8072
8073  d.reset(new TestDelegate);
8074  {
8075    // This request should use cached identity from previous request.
8076    URLRequest r(test_server_.GetURL("/LICENSE"),
8077                 DEFAULT_PRIORITY,
8078                 d.get(),
8079                 &default_context_);
8080    r.Start();
8081    EXPECT_TRUE(r.is_pending());
8082
8083    base::RunLoop().Run();
8084
8085    int64 file_size = 0;
8086    base::GetFileSize(app_path, &file_size);
8087
8088    EXPECT_FALSE(r.is_pending());
8089    EXPECT_EQ(1, d->response_started_count());
8090    EXPECT_FALSE(d->received_data_before_response());
8091    EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8092  }
8093}
8094
8095// Flaky, see http://crbug.com/25045.
8096TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
8097  ASSERT_TRUE(test_server_.Start());
8098
8099  base::FilePath app_path;
8100  PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8101  app_path = app_path.AppendASCII("LICENSE");
8102
8103  scoped_ptr<TestDelegate> d(new TestDelegate);
8104  // Set correct login credentials. The delegate will be asked for them when
8105  // the initial login with wrong credentials will fail.
8106  d->set_credentials(AuthCredentials(kChrome, kChrome));
8107  {
8108    URLRequest r(test_server_.GetURLWithUserAndPassword(
8109                     "/LICENSE", "chrome", "wrong_password"),
8110                 DEFAULT_PRIORITY,
8111                 d.get(),
8112                 &default_context_);
8113    r.Start();
8114    EXPECT_TRUE(r.is_pending());
8115
8116    base::RunLoop().Run();
8117
8118    int64 file_size = 0;
8119    base::GetFileSize(app_path, &file_size);
8120
8121    EXPECT_FALSE(r.is_pending());
8122    EXPECT_EQ(1, d->response_started_count());
8123    EXPECT_FALSE(d->received_data_before_response());
8124    EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8125  }
8126
8127  // Use a new delegate without explicit credentials. The cached ones should be
8128  // used.
8129  d.reset(new TestDelegate);
8130  {
8131    // Don't pass wrong credentials in the URL, they would override valid cached
8132    // ones.
8133    URLRequest r(test_server_.GetURL("/LICENSE"),
8134                 DEFAULT_PRIORITY,
8135                 d.get(),
8136                 &default_context_);
8137    r.Start();
8138    EXPECT_TRUE(r.is_pending());
8139
8140    base::RunLoop().Run();
8141
8142    int64 file_size = 0;
8143    base::GetFileSize(app_path, &file_size);
8144
8145    EXPECT_FALSE(r.is_pending());
8146    EXPECT_EQ(1, d->response_started_count());
8147    EXPECT_FALSE(d->received_data_before_response());
8148    EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8149  }
8150}
8151#endif  // !defined(DISABLE_FTP_SUPPORT)
8152
8153}  // namespace net
8154