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