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