Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: content/browser/loader/resource_dispatcher_host_unittest.cc

Issue 82273002: Fix various issues in RedirectToFileResourceHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: UUUUNIT TESTS Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <vector> 5 #include <vector>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h"
8 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
9 #include "base/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
11 #include "base/pickle.h" 12 #include "base/pickle.h"
12 #include "base/run_loop.h" 13 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
15 #include "content/browser/browser_thread_impl.h" 16 #include "content/browser/browser_thread_impl.h"
16 #include "content/browser/child_process_security_policy_impl.h" 17 #include "content/browser/child_process_security_policy_impl.h"
17 #include "content/browser/loader/detachable_resource_handler.h" 18 #include "content/browser/loader/detachable_resource_handler.h"
18 #include "content/browser/loader/resource_dispatcher_host_impl.h" 19 #include "content/browser/loader/resource_dispatcher_host_impl.h"
19 #include "content/browser/loader/resource_loader.h" 20 #include "content/browser/loader/resource_loader.h"
20 #include "content/browser/loader/resource_message_filter.h" 21 #include "content/browser/loader/resource_message_filter.h"
21 #include "content/browser/loader/resource_request_info_impl.h" 22 #include "content/browser/loader/resource_request_info_impl.h"
22 #include "content/browser/worker_host/worker_service_impl.h" 23 #include "content/browser/worker_host/worker_service_impl.h"
23 #include "content/common/child_process_host_impl.h" 24 #include "content/common/child_process_host_impl.h"
24 #include "content/common/resource_messages.h" 25 #include "content/common/resource_messages.h"
25 #include "content/common/view_messages.h" 26 #include "content/common/view_messages.h"
26 #include "content/public/browser/global_request_id.h" 27 #include "content/public/browser/global_request_id.h"
27 #include "content/public/browser/resource_context.h" 28 #include "content/public/browser/resource_context.h"
28 #include "content/public/browser/resource_dispatcher_host_delegate.h" 29 #include "content/public/browser/resource_dispatcher_host_delegate.h"
29 #include "content/public/browser/resource_request_info.h" 30 #include "content/public/browser/resource_request_info.h"
30 #include "content/public/browser/resource_throttle.h" 31 #include "content/public/browser/resource_throttle.h"
31 #include "content/public/common/process_type.h" 32 #include "content/public/common/process_type.h"
32 #include "content/public/common/resource_response.h" 33 #include "content/public/common/resource_response.h"
33 #include "content/public/test/test_browser_context.h" 34 #include "content/public/test/test_browser_context.h"
35 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "content/test/test_content_browser_client.h" 36 #include "content/test/test_content_browser_client.h"
35 #include "net/base/net_errors.h" 37 #include "net/base/net_errors.h"
36 #include "net/base/request_priority.h" 38 #include "net/base/request_priority.h"
37 #include "net/base/upload_bytes_element_reader.h" 39 #include "net/base/upload_bytes_element_reader.h"
38 #include "net/base/upload_data_stream.h" 40 #include "net/base/upload_data_stream.h"
39 #include "net/http/http_util.h" 41 #include "net/http/http_util.h"
40 #include "net/url_request/url_request.h" 42 #include "net/url_request/url_request.h"
41 #include "net/url_request/url_request_context.h" 43 #include "net/url_request/url_request_context.h"
42 #include "net/url_request/url_request_job.h" 44 #include "net/url_request/url_request_job.h"
43 #include "net/url_request/url_request_simple_job.h" 45 #include "net/url_request/url_request_simple_job.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 } // namespace 80 } // namespace
79 81
80 static int RequestIDForMessage(const IPC::Message& msg) { 82 static int RequestIDForMessage(const IPC::Message& msg) {
81 int request_id = -1; 83 int request_id = -1;
82 switch (msg.type()) { 84 switch (msg.type()) {
83 case ResourceMsg_UploadProgress::ID: 85 case ResourceMsg_UploadProgress::ID:
84 case ResourceMsg_ReceivedResponse::ID: 86 case ResourceMsg_ReceivedResponse::ID:
85 case ResourceMsg_ReceivedRedirect::ID: 87 case ResourceMsg_ReceivedRedirect::ID:
86 case ResourceMsg_SetDataBuffer::ID: 88 case ResourceMsg_SetDataBuffer::ID:
87 case ResourceMsg_DataReceived::ID: 89 case ResourceMsg_DataReceived::ID:
90 case ResourceMsg_DataDownloaded::ID:
88 case ResourceMsg_RequestComplete::ID: { 91 case ResourceMsg_RequestComplete::ID: {
89 bool result = PickleIterator(msg).ReadInt(&request_id); 92 bool result = PickleIterator(msg).ReadInt(&request_id);
90 DCHECK(result); 93 DCHECK(result);
91 break; 94 break;
92 } 95 }
93 } 96 }
94 return request_id; 97 return request_id;
95 } 98 }
96 99
97 static ResourceHostMsg_Request CreateResourceRequest( 100 static ResourceHostMsg_Request CreateResourceRequest(
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 bool create_two_throttles_; 518 bool create_two_throttles_;
516 int flags_; 519 int flags_;
517 int error_code_for_cancellation_; 520 int error_code_for_cancellation_;
518 scoped_ptr<base::SupportsUserData::Data> user_data_; 521 scoped_ptr<base::SupportsUserData::Data> user_data_;
519 }; 522 };
520 523
521 class ResourceDispatcherHostTest : public testing::Test, 524 class ResourceDispatcherHostTest : public testing::Test,
522 public IPC::Sender { 525 public IPC::Sender {
523 public: 526 public:
524 ResourceDispatcherHostTest() 527 ResourceDispatcherHostTest()
525 : ui_thread_(BrowserThread::UI, &message_loop_), 528 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
526 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_),
527 cache_thread_(BrowserThread::CACHE, &message_loop_),
528 io_thread_(BrowserThread::IO, &message_loop_),
529 old_factory_(NULL), 529 old_factory_(NULL),
530 send_data_received_acks_(false) { 530 send_data_received_acks_(false) {
531 browser_context_.reset(new TestBrowserContext()); 531 browser_context_.reset(new TestBrowserContext());
532 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); 532 BrowserContext::EnsureResourceContextInitialized(browser_context_.get());
533 message_loop_.RunUntilIdle(); 533 base::RunLoop().RunUntilIdle();
534 ResourceContext* resource_context = browser_context_->GetResourceContext(); 534 ResourceContext* resource_context = browser_context_->GetResourceContext();
535 filter_ = new ForwardingFilter(this, resource_context); 535 filter_ = new ForwardingFilter(this, resource_context);
536 resource_context->GetRequestContext()->set_network_delegate( 536 resource_context->GetRequestContext()->set_network_delegate(
537 &network_delegate_); 537 &network_delegate_);
538 } 538 }
539 539
540 virtual ~ResourceDispatcherHostTest() {
541 for (std::set<int>::iterator it = child_ids_.begin();
542 it != child_ids_.end(); ++it) {
543 host_.CancelRequestsForProcess(*it);
544 }
545 }
546
547 // IPC::Sender implementation 540 // IPC::Sender implementation
548 virtual bool Send(IPC::Message* msg) OVERRIDE { 541 virtual bool Send(IPC::Message* msg) OVERRIDE {
549 accum_.AddMessage(*msg); 542 accum_.AddMessage(*msg);
550 543
551 if (send_data_received_acks_ && 544 if (send_data_received_acks_ &&
552 msg->type() == ResourceMsg_DataReceived::ID) { 545 msg->type() == ResourceMsg_DataReceived::ID) {
553 GenerateDataReceivedACK(*msg); 546 GenerateDataReceivedACK(*msg);
554 } 547 }
555 548
549 if (wait_for_request_complete_ &&
550 msg->type() == ResourceMsg_RequestComplete::ID) {
551 wait_for_request_complete_->Quit();
552 }
553
556 delete msg; 554 delete msg;
557 return true; 555 return true;
558 } 556 }
559 557
560 protected: 558 protected:
561 // testing::Test 559 // testing::Test
562 virtual void SetUp() { 560 virtual void SetUp() {
563 DCHECK(!test_fixture_); 561 DCHECK(!test_fixture_);
564 test_fixture_ = this; 562 test_fixture_ = this;
565 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); 563 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0);
(...skipping 11 matching lines...) Expand all
577 if (!scheme_.empty()) 575 if (!scheme_.empty())
578 net::URLRequest::Deprecated::RegisterProtocolFactory( 576 net::URLRequest::Deprecated::RegisterProtocolFactory(
579 scheme_, old_factory_); 577 scheme_, old_factory_);
580 578
581 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); 579 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty());
582 URLRequestTestDelayedStartJob::ClearQueue(); 580 URLRequestTestDelayedStartJob::ClearQueue();
583 581
584 DCHECK(test_fixture_ == this); 582 DCHECK(test_fixture_ == this);
585 test_fixture_ = NULL; 583 test_fixture_ = NULL;
586 584
585 for (std::set<int>::iterator it = child_ids_.begin();
586 it != child_ids_.end(); ++it) {
587 host_.CancelRequestsForProcess(*it);
588 }
589
587 host_.Shutdown(); 590 host_.Shutdown();
588 591
589 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0); 592 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0);
590 593
591 // Flush the message loop to make application verifiers happy. 594 // Flush the message loop to make application verifiers happy.
592 if (ResourceDispatcherHostImpl::Get()) 595 if (ResourceDispatcherHostImpl::Get())
593 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext( 596 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext(
594 browser_context_->GetResourceContext()); 597 browser_context_->GetResourceContext());
595 598
596 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting(); 599 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting();
597 600
598 browser_context_.reset(); 601 browser_context_.reset();
599 message_loop_.RunUntilIdle(); 602 base::RunLoop().RunUntilIdle();
600 } 603 }
601 604
602 // Creates a request using the current test object as the filter and 605 // Creates a request using the current test object as the filter and
603 // SubResource as the resource type. 606 // SubResource as the resource type.
604 void MakeTestRequest(int render_view_id, 607 void MakeTestRequest(int render_view_id,
605 int request_id, 608 int request_id,
606 const GURL& url); 609 const GURL& url);
607 610
608 // Generates a request using the given filter and resource type. 611 // Generates a request using the given filter and resource type.
609 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter, 612 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter,
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 bool result = PickleIterator(msg).ReadInt(&request_id); 709 bool result = PickleIterator(msg).ReadInt(&request_id);
707 DCHECK(result); 710 DCHECK(result);
708 scoped_ptr<IPC::Message> ack( 711 scoped_ptr<IPC::Message> ack(
709 new ResourceHostMsg_DataReceived_ACK(request_id)); 712 new ResourceHostMsg_DataReceived_ACK(request_id));
710 713
711 base::MessageLoop::current()->PostTask( 714 base::MessageLoop::current()->PostTask(
712 FROM_HERE, 715 FROM_HERE,
713 base::Bind(&GenerateIPCMessage, filter_, base::Passed(&ack))); 716 base::Bind(&GenerateIPCMessage, filter_, base::Passed(&ack)));
714 } 717 }
715 718
716 base::MessageLoopForIO message_loop_; 719 void WaitForRequestComplete() {
717 BrowserThreadImpl ui_thread_; 720 DCHECK(!wait_for_request_complete_);
718 BrowserThreadImpl file_thread_; 721 wait_for_request_complete_.reset(new base::RunLoop);
719 BrowserThreadImpl cache_thread_; 722 wait_for_request_complete_->Run();
720 BrowserThreadImpl io_thread_; 723 wait_for_request_complete_.reset();
724 }
725
726 content::TestBrowserThreadBundle thread_bundle_;
721 scoped_ptr<TestBrowserContext> browser_context_; 727 scoped_ptr<TestBrowserContext> browser_context_;
722 scoped_refptr<ForwardingFilter> filter_; 728 scoped_refptr<ForwardingFilter> filter_;
723 net::TestNetworkDelegate network_delegate_; 729 net::TestNetworkDelegate network_delegate_;
724 ResourceDispatcherHostImpl host_; 730 ResourceDispatcherHostImpl host_;
725 ResourceIPCAccumulator accum_; 731 ResourceIPCAccumulator accum_;
726 std::string response_headers_; 732 std::string response_headers_;
727 std::string response_data_; 733 std::string response_data_;
728 std::string scheme_; 734 std::string scheme_;
729 net::URLRequest::ProtocolFactory* old_factory_; 735 net::URLRequest::ProtocolFactory* old_factory_;
730 bool send_data_received_acks_; 736 bool send_data_received_acks_;
731 std::set<int> child_ids_; 737 std::set<int> child_ids_;
738 scoped_ptr<base::RunLoop> wait_for_request_complete_;
732 static ResourceDispatcherHostTest* test_fixture_; 739 static ResourceDispatcherHostTest* test_fixture_;
733 static bool delay_start_; 740 static bool delay_start_;
734 static bool delay_complete_; 741 static bool delay_complete_;
735 static int url_request_jobs_created_count_; 742 static int url_request_jobs_created_count_;
736 }; 743 };
737 // Static. 744 // Static.
738 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; 745 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL;
739 bool ResourceDispatcherHostTest::delay_start_ = false; 746 bool ResourceDispatcherHostTest::delay_start_ = false;
740 bool ResourceDispatcherHostTest::delay_complete_ = false; 747 bool ResourceDispatcherHostTest::delay_complete_ = false;
741 int ResourceDispatcherHostTest::url_request_jobs_created_count_ = 0; 748 int ResourceDispatcherHostTest::url_request_jobs_created_count_ = 0;
(...skipping 1856 matching lines...) Expand 10 before | Expand all | Expand 10 after
2598 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok); 2605 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok);
2599 } 2606 }
2600 2607
2601 base::MessageLoop::current()->RunUntilIdle(); 2608 base::MessageLoop::current()->RunUntilIdle();
2602 2609
2603 msgs.clear(); 2610 msgs.clear();
2604 accum_.GetClassifiedMessages(&msgs); 2611 accum_.GetClassifiedMessages(&msgs);
2605 } 2612 }
2606 } 2613 }
2607 2614
2615 TEST_F(ResourceDispatcherHostTest, DownloadToFile) {
2616 // Make a request which downloads to file.
2617 ResourceHostMsg_Request request = CreateResourceRequest(
2618 "GET", ResourceType::SUB_RESOURCE, net::URLRequestTestJob::test_url_1());
2619 request.download_to_file = true;
2620 ResourceHostMsg_RequestResource request_msg(0, 1, request);
2621 bool msg_was_ok;
2622 host_.OnMessageReceived(request_msg, filter_, &msg_was_ok);
2623 ASSERT_TRUE(msg_was_ok);
2624
2625 // Running the message loop until idle does not work because
2626 // RedirectToFileResourceHandler posts things to base::WorkerPool. Instead,
2627 // wait for the ResourceMsg_RequestComplete to go out. Then run the event loop
2628 // until idle so the loader is gone.
2629 WaitForRequestComplete();
2630 base::MessageLoop::current()->RunUntilIdle();
2631 EXPECT_EQ(0, host_.pending_requests());
2632
2633 ResourceIPCAccumulator::ClassifiedMessages msgs;
2634 accum_.GetClassifiedMessages(&msgs);
2635
2636 ASSERT_EQ(1U, msgs.size());
2637 const std::vector<IPC::Message>& messages = msgs[0];
2638
2639 // The request should contain the following messages:
2640 // ReceivedResponse (indicates headers received and filename)
2641 // DataDownloaded* (bytes downloaded and total length)
2642 // RequestComplete (request is done)
2643
2644 // ReceivedResponse
2645 ResourceResponseHead response_head;
2646 GetResponseHead(messages, &response_head);
2647 ASSERT_FALSE(response_head.download_file_path.empty());
2648
2649 // DataDownloaded
2650 size_t total_len = 0;
2651 for (size_t i = 1; i < messages.size() - 1; i++) {
2652 ASSERT_EQ(ResourceMsg_DataDownloaded::ID, messages[i].type());
2653 PickleIterator iter(messages[i]);
2654 int request_id, data_len;
2655 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &request_id));
2656 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &data_len));
2657 total_len += data_len;
2658 }
2659 EXPECT_EQ(net::URLRequestTestJob::test_data_1().size(), total_len);
2660
2661 // RequestComplete
2662 CheckRequestCompleteErrorCode(messages.back(), net::OK);
2663
2664 // Verify that the data ended up in the temporary file.
2665 std::string contents;
2666 ASSERT_TRUE(base::ReadFileToString(response_head.download_file_path,
2667 &contents));
2668 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), contents);
2669
2670 // When the renderer releases the file, it should be deleted.
2671 ResourceHostMsg_ReleaseDownloadedFile release_msg(1);
2672 host_.OnMessageReceived(release_msg, filter_, &msg_was_ok);
2673 ASSERT_TRUE(msg_was_ok);
2674 base::MessageLoop::current()->RunUntilIdle();
2675
2676 EXPECT_FALSE(base::PathExists(response_head.download_file_path));
2677 }
2678
2608 } // namespace content 2679 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698