1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/url_request/url_request_test_util.h" 6 7 #include "base/compiler_specific.h" 8 #include "base/logging.h" 9 #include "base/message_loop/message_loop.h" 10 #include "base/threading/thread.h" 11 #include "base/threading/worker_pool.h" 12 #include "net/base/host_port_pair.h" 13 #include "net/cert/cert_verifier.h" 14 #include "net/dns/mock_host_resolver.h" 15 #include "net/http/http_network_session.h" 16 #include "net/http/http_server_properties_impl.h" 17 #include "net/http/transport_security_state.h" 18 #include "net/ssl/default_server_bound_cert_store.h" 19 #include "net/ssl/server_bound_cert_service.h" 20 #include "net/url_request/static_http_user_agent_settings.h" 21 #include "net/url_request/url_request_job_factory_impl.h" 22 #include "testing/gtest/include/gtest/gtest.h" 23 24 namespace net { 25 26 namespace { 27 28 // These constants put the NetworkDelegate events of TestNetworkDelegate 29 // into an order. They are used in conjunction with 30 // |TestNetworkDelegate::next_states_| to check that we do not send 31 // events in the wrong order. 32 const int kStageBeforeURLRequest = 1 << 0; 33 const int kStageBeforeSendHeaders = 1 << 1; 34 const int kStageSendHeaders = 1 << 2; 35 const int kStageHeadersReceived = 1 << 3; 36 const int kStageAuthRequired = 1 << 4; 37 const int kStageBeforeRedirect = 1 << 5; 38 const int kStageResponseStarted = 1 << 6; 39 const int kStageCompletedSuccess = 1 << 7; 40 const int kStageCompletedError = 1 << 8; 41 const int kStageURLRequestDestroyed = 1 << 9; 42 const int kStageDestruction = 1 << 10; 43 44 } // namespace 45 46 TestURLRequestContext::TestURLRequestContext() 47 : initialized_(false), 48 client_socket_factory_(NULL), 49 context_storage_(this) { 50 Init(); 51 } 52 53 TestURLRequestContext::TestURLRequestContext(bool delay_initialization) 54 : initialized_(false), 55 client_socket_factory_(NULL), 56 context_storage_(this) { 57 if (!delay_initialization) 58 Init(); 59 } 60 61 TestURLRequestContext::~TestURLRequestContext() { 62 DCHECK(initialized_); 63 } 64 65 void TestURLRequestContext::Init() { 66 DCHECK(!initialized_); 67 initialized_ = true; 68 69 if (!host_resolver()) 70 context_storage_.set_host_resolver( 71 scoped_ptr<HostResolver>(new MockCachingHostResolver())); 72 if (!proxy_service()) 73 context_storage_.set_proxy_service(ProxyService::CreateDirect()); 74 if (!cert_verifier()) 75 context_storage_.set_cert_verifier(CertVerifier::CreateDefault()); 76 if (!transport_security_state()) 77 context_storage_.set_transport_security_state(new TransportSecurityState); 78 if (!ssl_config_service()) 79 context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults); 80 if (!http_auth_handler_factory()) { 81 context_storage_.set_http_auth_handler_factory( 82 HttpAuthHandlerFactory::CreateDefault(host_resolver())); 83 } 84 if (!http_server_properties()) { 85 context_storage_.set_http_server_properties( 86 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl())); 87 } 88 if (!transport_security_state()) { 89 context_storage_.set_transport_security_state( 90 new TransportSecurityState()); 91 } 92 if (http_transaction_factory()) { 93 // Make sure we haven't been passed an object we're not going to use. 94 EXPECT_FALSE(client_socket_factory_); 95 } else { 96 HttpNetworkSession::Params params; 97 params.client_socket_factory = client_socket_factory(); 98 params.host_resolver = host_resolver(); 99 params.cert_verifier = cert_verifier(); 100 params.transport_security_state = transport_security_state(); 101 params.proxy_service = proxy_service(); 102 params.ssl_config_service = ssl_config_service(); 103 params.http_auth_handler_factory = http_auth_handler_factory(); 104 params.network_delegate = network_delegate(); 105 params.http_server_properties = http_server_properties(); 106 params.net_log = net_log(); 107 context_storage_.set_http_transaction_factory(new HttpCache( 108 new HttpNetworkSession(params), 109 HttpCache::DefaultBackend::InMemory(0))); 110 } 111 // In-memory cookie store. 112 if (!cookie_store()) 113 context_storage_.set_cookie_store(new CookieMonster(NULL, NULL)); 114 // In-memory origin bound cert service. 115 if (!server_bound_cert_service()) { 116 context_storage_.set_server_bound_cert_service( 117 new ServerBoundCertService( 118 new DefaultServerBoundCertStore(NULL), 119 base::WorkerPool::GetTaskRunner(true))); 120 } 121 if (!http_user_agent_settings()) { 122 context_storage_.set_http_user_agent_settings( 123 new StaticHttpUserAgentSettings("en-us,fr", std::string())); 124 } 125 if (!job_factory()) 126 context_storage_.set_job_factory(new URLRequestJobFactoryImpl); 127 } 128 129 TestURLRequest::TestURLRequest(const GURL& url, 130 RequestPriority priority, 131 Delegate* delegate, 132 TestURLRequestContext* context) 133 : URLRequest(url, priority, delegate, context) {} 134 135 TestURLRequest::~TestURLRequest() { 136 } 137 138 TestURLRequestContextGetter::TestURLRequestContextGetter( 139 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) 140 : network_task_runner_(network_task_runner) { 141 DCHECK(network_task_runner_.get()); 142 } 143 144 TestURLRequestContextGetter::TestURLRequestContextGetter( 145 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner, 146 scoped_ptr<TestURLRequestContext> context) 147 : network_task_runner_(network_task_runner), context_(context.Pass()) { 148 DCHECK(network_task_runner_.get()); 149 } 150 151 TestURLRequestContextGetter::~TestURLRequestContextGetter() {} 152 153 TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() { 154 if (!context_.get()) 155 context_.reset(new TestURLRequestContext); 156 return context_.get(); 157 } 158 159 scoped_refptr<base::SingleThreadTaskRunner> 160 TestURLRequestContextGetter::GetNetworkTaskRunner() const { 161 return network_task_runner_; 162 } 163 164 TestDelegate::TestDelegate() 165 : cancel_in_rr_(false), 166 cancel_in_rs_(false), 167 cancel_in_rd_(false), 168 cancel_in_rd_pending_(false), 169 quit_on_complete_(true), 170 quit_on_redirect_(false), 171 allow_certificate_errors_(false), 172 response_started_count_(0), 173 received_bytes_count_(0), 174 received_redirect_count_(0), 175 received_data_before_response_(false), 176 request_failed_(false), 177 have_certificate_errors_(false), 178 certificate_errors_are_fatal_(false), 179 auth_required_(false), 180 have_full_request_headers_(false), 181 buf_(new IOBuffer(kBufferSize)) { 182 } 183 184 TestDelegate::~TestDelegate() {} 185 186 void TestDelegate::ClearFullRequestHeaders() { 187 full_request_headers_.Clear(); 188 have_full_request_headers_ = false; 189 } 190 191 void TestDelegate::OnReceivedRedirect(URLRequest* request, 192 const GURL& new_url, 193 bool* defer_redirect) { 194 EXPECT_TRUE(request->is_redirecting()); 195 196 have_full_request_headers_ = 197 request->GetFullRequestHeaders(&full_request_headers_); 198 199 received_redirect_count_++; 200 if (quit_on_redirect_) { 201 *defer_redirect = true; 202 base::MessageLoop::current()->PostTask(FROM_HERE, 203 base::MessageLoop::QuitClosure()); 204 } else if (cancel_in_rr_) { 205 request->Cancel(); 206 } 207 } 208 209 void TestDelegate::OnAuthRequired(URLRequest* request, 210 AuthChallengeInfo* auth_info) { 211 auth_required_ = true; 212 if (!credentials_.Empty()) { 213 request->SetAuth(credentials_); 214 } else { 215 request->CancelAuth(); 216 } 217 } 218 219 void TestDelegate::OnSSLCertificateError(URLRequest* request, 220 const SSLInfo& ssl_info, 221 bool fatal) { 222 // The caller can control whether it needs all SSL requests to go through, 223 // independent of any possible errors, or whether it wants SSL errors to 224 // cancel the request. 225 have_certificate_errors_ = true; 226 certificate_errors_are_fatal_ = fatal; 227 if (allow_certificate_errors_) 228 request->ContinueDespiteLastError(); 229 else 230 request->Cancel(); 231 } 232 233 void TestDelegate::OnResponseStarted(URLRequest* request) { 234 // It doesn't make sense for the request to have IO pending at this point. 235 DCHECK(!request->status().is_io_pending()); 236 EXPECT_FALSE(request->is_redirecting()); 237 238 have_full_request_headers_ = 239 request->GetFullRequestHeaders(&full_request_headers_); 240 241 response_started_count_++; 242 if (cancel_in_rs_) { 243 request->Cancel(); 244 OnResponseCompleted(request); 245 } else if (!request->status().is_success()) { 246 DCHECK(request->status().status() == URLRequestStatus::FAILED || 247 request->status().status() == URLRequestStatus::CANCELED); 248 request_failed_ = true; 249 OnResponseCompleted(request); 250 } else { 251 // Initiate the first read. 252 int bytes_read = 0; 253 if (request->Read(buf_.get(), kBufferSize, &bytes_read)) 254 OnReadCompleted(request, bytes_read); 255 else if (!request->status().is_io_pending()) 256 OnResponseCompleted(request); 257 } 258 } 259 260 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) { 261 // It doesn't make sense for the request to have IO pending at this point. 262 DCHECK(!request->status().is_io_pending()); 263 264 if (response_started_count_ == 0) 265 received_data_before_response_ = true; 266 267 if (cancel_in_rd_) 268 request->Cancel(); 269 270 if (bytes_read >= 0) { 271 // There is data to read. 272 received_bytes_count_ += bytes_read; 273 274 // consume the data 275 data_received_.append(buf_->data(), bytes_read); 276 } 277 278 // If it was not end of stream, request to read more. 279 if (request->status().is_success() && bytes_read > 0) { 280 bytes_read = 0; 281 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) { 282 if (bytes_read > 0) { 283 data_received_.append(buf_->data(), bytes_read); 284 received_bytes_count_ += bytes_read; 285 } else { 286 break; 287 } 288 } 289 } 290 if (!request->status().is_io_pending()) 291 OnResponseCompleted(request); 292 else if (cancel_in_rd_pending_) 293 request->Cancel(); 294 } 295 296 void TestDelegate::OnResponseCompleted(URLRequest* request) { 297 if (quit_on_complete_) 298 base::MessageLoop::current()->PostTask(FROM_HERE, 299 base::MessageLoop::QuitClosure()); 300 } 301 302 TestNetworkDelegate::TestNetworkDelegate() 303 : last_error_(0), 304 error_count_(0), 305 created_requests_(0), 306 destroyed_requests_(0), 307 completed_requests_(0), 308 canceled_requests_(0), 309 cookie_options_bit_mask_(0), 310 blocked_get_cookies_count_(0), 311 blocked_set_cookie_count_(0), 312 set_cookie_count_(0), 313 has_load_timing_info_before_redirect_(false), 314 has_load_timing_info_before_auth_(false), 315 can_access_files_(true), 316 can_throttle_requests_(true) { 317 } 318 319 TestNetworkDelegate::~TestNetworkDelegate() { 320 for (std::map<int, int>::iterator i = next_states_.begin(); 321 i != next_states_.end(); ++i) { 322 event_order_[i->first] += "~TestNetworkDelegate\n"; 323 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first]; 324 } 325 } 326 327 bool TestNetworkDelegate::GetLoadTimingInfoBeforeRedirect( 328 LoadTimingInfo* load_timing_info_before_redirect) const { 329 *load_timing_info_before_redirect = load_timing_info_before_redirect_; 330 return has_load_timing_info_before_redirect_; 331 } 332 333 bool TestNetworkDelegate::GetLoadTimingInfoBeforeAuth( 334 LoadTimingInfo* load_timing_info_before_auth) const { 335 *load_timing_info_before_auth = load_timing_info_before_auth_; 336 return has_load_timing_info_before_auth_; 337 } 338 339 void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) { 340 if (next_states_.find(request_id) == next_states_.end()) { 341 // TODO(davidben): Although the URLRequest documentation does not allow 342 // calling Cancel() before Start(), the ResourceLoader does so. URLRequest's 343 // destructor also calls Cancel. Either officially support this or fix the 344 // ResourceLoader code. 345 next_states_[request_id] = kStageBeforeURLRequest | kStageCompletedError; 346 event_order_[request_id] = ""; 347 } 348 } 349 350 int TestNetworkDelegate::OnBeforeURLRequest( 351 URLRequest* request, 352 const CompletionCallback& callback, 353 GURL* new_url ) { 354 int req_id = request->identifier(); 355 InitRequestStatesIfNew(req_id); 356 event_order_[req_id] += "OnBeforeURLRequest\n"; 357 EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) << 358 event_order_[req_id]; 359 next_states_[req_id] = 360 kStageBeforeSendHeaders | 361 kStageResponseStarted | // data: URLs do not trigger sending headers 362 kStageBeforeRedirect | // a delegate can trigger a redirection 363 kStageCompletedError | // request canceled by delegate 364 kStageAuthRequired; // Auth can come next for FTP requests 365 created_requests_++; 366 return OK; 367 } 368 369 int TestNetworkDelegate::OnBeforeSendHeaders( 370 URLRequest* request, 371 const CompletionCallback& callback, 372 HttpRequestHeaders* headers) { 373 int req_id = request->identifier(); 374 InitRequestStatesIfNew(req_id); 375 event_order_[req_id] += "OnBeforeSendHeaders\n"; 376 EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) << 377 event_order_[req_id]; 378 next_states_[req_id] = 379 kStageSendHeaders | 380 kStageCompletedError; // request canceled by delegate 381 382 return OK; 383 } 384 385 void TestNetworkDelegate::OnSendHeaders( 386 URLRequest* request, 387 const HttpRequestHeaders& headers) { 388 int req_id = request->identifier(); 389 InitRequestStatesIfNew(req_id); 390 event_order_[req_id] += "OnSendHeaders\n"; 391 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) << 392 event_order_[req_id]; 393 next_states_[req_id] = 394 kStageHeadersReceived | 395 kStageCompletedError; 396 } 397 398 int TestNetworkDelegate::OnHeadersReceived( 399 URLRequest* request, 400 const CompletionCallback& callback, 401 const HttpResponseHeaders* original_response_headers, 402 scoped_refptr<HttpResponseHeaders>* override_response_headers) { 403 int req_id = request->identifier(); 404 event_order_[req_id] += "OnHeadersReceived\n"; 405 InitRequestStatesIfNew(req_id); 406 EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) << 407 event_order_[req_id]; 408 next_states_[req_id] = 409 kStageBeforeRedirect | 410 kStageResponseStarted | 411 kStageAuthRequired | 412 kStageCompletedError; // e.g. proxy resolution problem 413 414 // Basic authentication sends a second request from the URLRequestHttpJob 415 // layer before the URLRequest reports that a response has started. 416 next_states_[req_id] |= kStageBeforeSendHeaders; 417 418 return OK; 419 } 420 421 void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request, 422 const GURL& new_location) { 423 load_timing_info_before_redirect_ = LoadTimingInfo(); 424 request->GetLoadTimingInfo(&load_timing_info_before_redirect_); 425 has_load_timing_info_before_redirect_ = true; 426 EXPECT_FALSE(load_timing_info_before_redirect_.request_start_time.is_null()); 427 EXPECT_FALSE(load_timing_info_before_redirect_.request_start.is_null()); 428 429 int req_id = request->identifier(); 430 InitRequestStatesIfNew(req_id); 431 event_order_[req_id] += "OnBeforeRedirect\n"; 432 EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) << 433 event_order_[req_id]; 434 next_states_[req_id] = 435 kStageBeforeURLRequest | // HTTP redirects trigger this. 436 kStageBeforeSendHeaders | // Redirects from the network delegate do not 437 // trigger onBeforeURLRequest. 438 kStageCompletedError; 439 440 // A redirect can lead to a file or a data URL. In this case, we do not send 441 // headers. 442 next_states_[req_id] |= kStageResponseStarted; 443 } 444 445 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) { 446 LoadTimingInfo load_timing_info; 447 request->GetLoadTimingInfo(&load_timing_info); 448 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); 449 EXPECT_FALSE(load_timing_info.request_start.is_null()); 450 451 int req_id = request->identifier(); 452 InitRequestStatesIfNew(req_id); 453 event_order_[req_id] += "OnResponseStarted\n"; 454 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) << 455 event_order_[req_id]; 456 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; 457 if (request->status().status() == URLRequestStatus::FAILED) { 458 error_count_++; 459 last_error_ = request->status().error(); 460 } 461 } 462 463 void TestNetworkDelegate::OnRawBytesRead(const URLRequest& request, 464 int bytes_read) { 465 } 466 467 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) { 468 int req_id = request->identifier(); 469 InitRequestStatesIfNew(req_id); 470 event_order_[req_id] += "OnCompleted\n"; 471 // Expect "Success -> (next_states_ & kStageCompletedSuccess)" 472 // is logically identical to 473 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" 474 EXPECT_TRUE(!request->status().is_success() || 475 (next_states_[req_id] & kStageCompletedSuccess)) << 476 event_order_[req_id]; 477 EXPECT_TRUE(request->status().is_success() || 478 (next_states_[req_id] & kStageCompletedError)) << 479 event_order_[req_id]; 480 next_states_[req_id] = kStageURLRequestDestroyed; 481 completed_requests_++; 482 if (request->status().status() == URLRequestStatus::FAILED) { 483 error_count_++; 484 last_error_ = request->status().error(); 485 } else if (request->status().status() == URLRequestStatus::CANCELED) { 486 canceled_requests_++; 487 } else { 488 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status()); 489 } 490 } 491 492 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) { 493 int req_id = request->identifier(); 494 InitRequestStatesIfNew(req_id); 495 event_order_[req_id] += "OnURLRequestDestroyed\n"; 496 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << 497 event_order_[req_id]; 498 next_states_[req_id] = kStageDestruction; 499 destroyed_requests_++; 500 } 501 502 void TestNetworkDelegate::OnPACScriptError(int line_number, 503 const base::string16& error) { 504 } 505 506 NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired( 507 URLRequest* request, 508 const AuthChallengeInfo& auth_info, 509 const AuthCallback& callback, 510 AuthCredentials* credentials) { 511 load_timing_info_before_auth_ = LoadTimingInfo(); 512 request->GetLoadTimingInfo(&load_timing_info_before_auth_); 513 has_load_timing_info_before_auth_ = true; 514 EXPECT_FALSE(load_timing_info_before_auth_.request_start_time.is_null()); 515 EXPECT_FALSE(load_timing_info_before_auth_.request_start.is_null()); 516 517 int req_id = request->identifier(); 518 InitRequestStatesIfNew(req_id); 519 event_order_[req_id] += "OnAuthRequired\n"; 520 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) << 521 event_order_[req_id]; 522 next_states_[req_id] = kStageBeforeSendHeaders | 523 kStageAuthRequired | // For example, proxy auth followed by server auth. 524 kStageHeadersReceived | // Request canceled by delegate simulates empty 525 // response. 526 kStageResponseStarted | // data: URLs do not trigger sending headers 527 kStageBeforeRedirect | // a delegate can trigger a redirection 528 kStageCompletedError; // request cancelled before callback 529 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; 530 } 531 532 bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request, 533 const CookieList& cookie_list) { 534 bool allow = true; 535 if (cookie_options_bit_mask_ & NO_GET_COOKIES) 536 allow = false; 537 538 if (!allow) { 539 blocked_get_cookies_count_++; 540 } 541 542 return allow; 543 } 544 545 bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request, 546 const std::string& cookie_line, 547 CookieOptions* options) { 548 bool allow = true; 549 if (cookie_options_bit_mask_ & NO_SET_COOKIE) 550 allow = false; 551 552 if (!allow) { 553 blocked_set_cookie_count_++; 554 } else { 555 set_cookie_count_++; 556 } 557 558 return allow; 559 } 560 561 bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request, 562 const base::FilePath& path) const { 563 return can_access_files_; 564 } 565 566 bool TestNetworkDelegate::OnCanThrottleRequest( 567 const URLRequest& request) const { 568 return can_throttle_requests_; 569 } 570 571 int TestNetworkDelegate::OnBeforeSocketStreamConnect( 572 SocketStream* socket, 573 const CompletionCallback& callback) { 574 return OK; 575 } 576 577 void TestNetworkDelegate::OnRequestWaitStateChange( 578 const URLRequest& request, 579 RequestWaitState state) { 580 } 581 582 // static 583 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1"); 584 585 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost( 586 const std::string& new_value) 587 : old_value_(value_), 588 new_value_(new_value) { 589 value_ = new_value_; 590 } 591 592 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() { 593 DCHECK_EQ(value_, new_value_); 594 value_ = old_value_; 595 } 596 597 // static 598 const std::string& ScopedCustomUrlRequestTestHttpHost::value() { 599 return value_; 600 } 601 602 TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) { 603 } 604 605 URLRequestJob* TestJobInterceptor::MaybeCreateJob( 606 URLRequest* request, 607 NetworkDelegate* network_delegate) const { 608 URLRequestJob* job = main_intercept_job_; 609 main_intercept_job_ = NULL; 610 return job; 611 } 612 613 void TestJobInterceptor::set_main_intercept_job(URLRequestJob* job) { 614 main_intercept_job_ = job; 615 } 616 617 } // namespace net 618