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

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: mmenke comments Created 6 years, 9 months 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/basictypes.h"
7 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/file_util.h"
8 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
9 #include "base/memory/scoped_vector.h" 11 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
11 #include "base/pickle.h" 13 #include "base/pickle.h"
12 #include "base/run_loop.h" 14 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h" 16 #include "base/strings/string_split.h"
15 #include "content/browser/browser_thread_impl.h" 17 #include "content/browser/browser_thread_impl.h"
16 #include "content/browser/child_process_security_policy_impl.h" 18 #include "content/browser/child_process_security_policy_impl.h"
17 #include "content/browser/loader/cross_site_resource_handler.h" 19 #include "content/browser/loader/cross_site_resource_handler.h"
18 #include "content/browser/loader/detachable_resource_handler.h" 20 #include "content/browser/loader/detachable_resource_handler.h"
19 #include "content/browser/loader/resource_dispatcher_host_impl.h" 21 #include "content/browser/loader/resource_dispatcher_host_impl.h"
20 #include "content/browser/loader/resource_loader.h" 22 #include "content/browser/loader/resource_loader.h"
21 #include "content/browser/loader/resource_message_filter.h" 23 #include "content/browser/loader/resource_message_filter.h"
22 #include "content/browser/loader/resource_request_info_impl.h" 24 #include "content/browser/loader/resource_request_info_impl.h"
23 #include "content/browser/worker_host/worker_service_impl.h" 25 #include "content/browser/worker_host/worker_service_impl.h"
24 #include "content/common/child_process_host_impl.h" 26 #include "content/common/child_process_host_impl.h"
25 #include "content/common/resource_messages.h" 27 #include "content/common/resource_messages.h"
26 #include "content/common/view_messages.h" 28 #include "content/common/view_messages.h"
27 #include "content/public/browser/global_request_id.h" 29 #include "content/public/browser/global_request_id.h"
28 #include "content/public/browser/resource_context.h" 30 #include "content/public/browser/resource_context.h"
29 #include "content/public/browser/resource_dispatcher_host_delegate.h" 31 #include "content/public/browser/resource_dispatcher_host_delegate.h"
30 #include "content/public/browser/resource_request_info.h" 32 #include "content/public/browser/resource_request_info.h"
31 #include "content/public/browser/resource_throttle.h" 33 #include "content/public/browser/resource_throttle.h"
32 #include "content/public/common/process_type.h" 34 #include "content/public/common/process_type.h"
33 #include "content/public/common/resource_response.h" 35 #include "content/public/common/resource_response.h"
34 #include "content/public/test/test_browser_context.h" 36 #include "content/public/test/test_browser_context.h"
37 #include "content/public/test/test_browser_thread_bundle.h"
35 #include "content/test/test_content_browser_client.h" 38 #include "content/test/test_content_browser_client.h"
36 #include "net/base/net_errors.h" 39 #include "net/base/net_errors.h"
37 #include "net/base/request_priority.h" 40 #include "net/base/request_priority.h"
38 #include "net/base/upload_bytes_element_reader.h" 41 #include "net/base/upload_bytes_element_reader.h"
39 #include "net/base/upload_data_stream.h" 42 #include "net/base/upload_data_stream.h"
40 #include "net/http/http_util.h" 43 #include "net/http/http_util.h"
41 #include "net/url_request/url_request.h" 44 #include "net/url_request/url_request.h"
42 #include "net/url_request/url_request_context.h" 45 #include "net/url_request/url_request_context.h"
43 #include "net/url_request/url_request_job.h" 46 #include "net/url_request/url_request_job.h"
44 #include "net/url_request/url_request_simple_job.h" 47 #include "net/url_request/url_request_simple_job.h"
45 #include "net/url_request/url_request_test_job.h" 48 #include "net/url_request/url_request_test_job.h"
46 #include "net/url_request/url_request_test_util.h" 49 #include "net/url_request/url_request_test_util.h"
47 #include "testing/gtest/include/gtest/gtest.h" 50 #include "testing/gtest/include/gtest/gtest.h"
48 #include "webkit/common/appcache/appcache_interfaces.h" 51 #include "webkit/common/appcache/appcache_interfaces.h"
52 #include "webkit/common/blob/shareable_file_reference.h"
49 53
50 // TODO(eroman): Write unit tests for SafeBrowsing that exercise 54 // TODO(eroman): Write unit tests for SafeBrowsing that exercise
51 // SafeBrowsingResourceHandler. 55 // SafeBrowsingResourceHandler.
52 56
57 using webkit_blob::ShareableFileReference;
58
53 namespace content { 59 namespace content {
54 60
55 namespace { 61 namespace {
56 62
57 // Returns the resource response header structure for this request. 63 // Returns the resource response header structure for this request.
58 void GetResponseHead(const std::vector<IPC::Message>& messages, 64 void GetResponseHead(const std::vector<IPC::Message>& messages,
59 ResourceResponseHead* response_head) { 65 ResourceResponseHead* response_head) {
60 ASSERT_GE(messages.size(), 2U); 66 ASSERT_GE(messages.size(), 2U);
61 67
62 // The first messages should be received response. 68 // The first messages should be received response.
(...skipping 16 matching lines...) Expand all
79 } // namespace 85 } // namespace
80 86
81 static int RequestIDForMessage(const IPC::Message& msg) { 87 static int RequestIDForMessage(const IPC::Message& msg) {
82 int request_id = -1; 88 int request_id = -1;
83 switch (msg.type()) { 89 switch (msg.type()) {
84 case ResourceMsg_UploadProgress::ID: 90 case ResourceMsg_UploadProgress::ID:
85 case ResourceMsg_ReceivedResponse::ID: 91 case ResourceMsg_ReceivedResponse::ID:
86 case ResourceMsg_ReceivedRedirect::ID: 92 case ResourceMsg_ReceivedRedirect::ID:
87 case ResourceMsg_SetDataBuffer::ID: 93 case ResourceMsg_SetDataBuffer::ID:
88 case ResourceMsg_DataReceived::ID: 94 case ResourceMsg_DataReceived::ID:
95 case ResourceMsg_DataDownloaded::ID:
89 case ResourceMsg_RequestComplete::ID: { 96 case ResourceMsg_RequestComplete::ID: {
90 bool result = PickleIterator(msg).ReadInt(&request_id); 97 bool result = PickleIterator(msg).ReadInt(&request_id);
91 DCHECK(result); 98 DCHECK(result);
92 break; 99 break;
93 } 100 }
94 } 101 }
95 return request_id; 102 return request_id;
96 } 103 }
97 104
98 static ResourceHostMsg_Request CreateResourceRequest( 105 static ResourceHostMsg_Request CreateResourceRequest(
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 public: 182 public:
176 explicit ForwardingFilter(IPC::Sender* dest, 183 explicit ForwardingFilter(IPC::Sender* dest,
177 ResourceContext* resource_context) 184 ResourceContext* resource_context)
178 : ResourceMessageFilter( 185 : ResourceMessageFilter(
179 ChildProcessHostImpl::GenerateChildProcessUniqueId(), 186 ChildProcessHostImpl::GenerateChildProcessUniqueId(),
180 PROCESS_TYPE_RENDERER, NULL, NULL, NULL, 187 PROCESS_TYPE_RENDERER, NULL, NULL, NULL,
181 base::Bind(&ForwardingFilter::GetContexts, 188 base::Bind(&ForwardingFilter::GetContexts,
182 base::Unretained(this))), 189 base::Unretained(this))),
183 dest_(dest), 190 dest_(dest),
184 resource_context_(resource_context) { 191 resource_context_(resource_context) {
192 ChildProcessSecurityPolicyImpl::GetInstance()->Add(child_id());
185 set_peer_pid_for_testing(base::GetCurrentProcId()); 193 set_peer_pid_for_testing(base::GetCurrentProcId());
186 } 194 }
187 195
188 // ResourceMessageFilter override 196 // ResourceMessageFilter override
189 virtual bool Send(IPC::Message* msg) OVERRIDE { 197 virtual bool Send(IPC::Message* msg) OVERRIDE {
190 if (!dest_) 198 if (!dest_)
191 return false; 199 return false;
192 return dest_->Send(msg); 200 return dest_->Send(msg);
193 } 201 }
194 202
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 } 561 }
554 } 562 }
555 563
556 private: 564 private:
557 bool create_two_throttles_; 565 bool create_two_throttles_;
558 int flags_; 566 int flags_;
559 int error_code_for_cancellation_; 567 int error_code_for_cancellation_;
560 scoped_ptr<base::SupportsUserData::Data> user_data_; 568 scoped_ptr<base::SupportsUserData::Data> user_data_;
561 }; 569 };
562 570
571 // Waits for a ShareableFileReference to be released.
572 class ShareableFileReleaseWaiter {
573 public:
574 ShareableFileReleaseWaiter(const base::FilePath& path) {
575 scoped_refptr<ShareableFileReference> file =
576 ShareableFileReference::Get(path);
577 file->AddFinalReleaseCallback(
578 base::Bind(&ShareableFileReleaseWaiter::Released,
579 base::Unretained(this)));
580 }
581
582 void Wait() {
583 loop_.Run();
584 }
585
586 private:
587 void Released(const base::FilePath& path) {
588 loop_.Quit();
589 }
590
591 base::RunLoop loop_;
592 DISALLOW_COPY_AND_ASSIGN(ShareableFileReleaseWaiter);
mmenke 2014/03/03 21:10:11 nit: Blank line before DISALLOW_COPY_AND_ASSIGN
davidben 2014/03/03 23:20:18 Done.
593 };
594
563 class ResourceDispatcherHostTest : public testing::Test, 595 class ResourceDispatcherHostTest : public testing::Test,
564 public IPC::Sender { 596 public IPC::Sender {
565 public: 597 public:
566 ResourceDispatcherHostTest() 598 ResourceDispatcherHostTest()
567 : ui_thread_(BrowserThread::UI, &message_loop_), 599 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
568 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_),
569 cache_thread_(BrowserThread::CACHE, &message_loop_),
570 io_thread_(BrowserThread::IO, &message_loop_),
571 old_factory_(NULL), 600 old_factory_(NULL),
572 send_data_received_acks_(false) { 601 send_data_received_acks_(false) {
573 browser_context_.reset(new TestBrowserContext()); 602 browser_context_.reset(new TestBrowserContext());
574 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); 603 BrowserContext::EnsureResourceContextInitialized(browser_context_.get());
575 message_loop_.RunUntilIdle(); 604 base::RunLoop().RunUntilIdle();
576 ResourceContext* resource_context = browser_context_->GetResourceContext(); 605 ResourceContext* resource_context = browser_context_->GetResourceContext();
577 filter_ = new ForwardingFilter(this, resource_context); 606 filter_ = new ForwardingFilter(this, resource_context);
578 resource_context->GetRequestContext()->set_network_delegate( 607 resource_context->GetRequestContext()->set_network_delegate(
579 &network_delegate_); 608 &network_delegate_);
580 } 609 }
581 610
582 virtual ~ResourceDispatcherHostTest() {
583 for (std::set<int>::iterator it = child_ids_.begin();
584 it != child_ids_.end(); ++it) {
585 host_.CancelRequestsForProcess(*it);
586 }
587 }
588
589 // IPC::Sender implementation 611 // IPC::Sender implementation
590 virtual bool Send(IPC::Message* msg) OVERRIDE { 612 virtual bool Send(IPC::Message* msg) OVERRIDE {
591 accum_.AddMessage(*msg); 613 accum_.AddMessage(*msg);
592 614
593 if (send_data_received_acks_ && 615 if (send_data_received_acks_ &&
594 msg->type() == ResourceMsg_DataReceived::ID) { 616 msg->type() == ResourceMsg_DataReceived::ID) {
595 GenerateDataReceivedACK(*msg); 617 GenerateDataReceivedACK(*msg);
596 } 618 }
597 619
620 if (wait_for_request_complete_loop_ &&
621 msg->type() == ResourceMsg_RequestComplete::ID) {
622 wait_for_request_complete_loop_->Quit();
623 }
624
598 delete msg; 625 delete msg;
599 return true; 626 return true;
600 } 627 }
601 628
602 protected: 629 protected:
603 // testing::Test 630 // testing::Test
604 virtual void SetUp() { 631 virtual void SetUp() OVERRIDE {
605 DCHECK(!test_fixture_); 632 DCHECK(!test_fixture_);
606 test_fixture_ = this; 633 test_fixture_ = this;
607 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); 634 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0);
608 net::URLRequest::Deprecated::RegisterProtocolFactory( 635 net::URLRequest::Deprecated::RegisterProtocolFactory(
609 "test", 636 "test",
610 &ResourceDispatcherHostTest::Factory); 637 &ResourceDispatcherHostTest::Factory);
611 EnsureTestSchemeIsAllowed(); 638 EnsureTestSchemeIsAllowed();
612 delay_start_ = false; 639 delay_start_ = false;
613 delay_complete_ = false; 640 delay_complete_ = false;
614 network_start_notification_ = false; 641 network_start_notification_ = false;
615 url_request_jobs_created_count_ = 0; 642 url_request_jobs_created_count_ = 0;
616 } 643 }
617 644
618 virtual void TearDown() { 645 virtual void TearDown() {
619 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL); 646 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL);
620 if (!scheme_.empty()) 647 if (!scheme_.empty())
621 net::URLRequest::Deprecated::RegisterProtocolFactory( 648 net::URLRequest::Deprecated::RegisterProtocolFactory(
622 scheme_, old_factory_); 649 scheme_, old_factory_);
623 650
624 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); 651 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty());
625 URLRequestTestDelayedStartJob::ClearQueue(); 652 URLRequestTestDelayedStartJob::ClearQueue();
626 653
627 DCHECK(test_fixture_ == this); 654 DCHECK(test_fixture_ == this);
628 test_fixture_ = NULL; 655 test_fixture_ = NULL;
629 656
657 for (std::set<int>::iterator it = child_ids_.begin();
658 it != child_ids_.end(); ++it) {
659 host_.CancelRequestsForProcess(*it);
660 }
661
630 host_.Shutdown(); 662 host_.Shutdown();
631 663
632 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0); 664 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0);
633 665
634 // Flush the message loop to make application verifiers happy. 666 // Flush the message loop to make application verifiers happy.
635 if (ResourceDispatcherHostImpl::Get()) 667 if (ResourceDispatcherHostImpl::Get())
636 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext( 668 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext(
637 browser_context_->GetResourceContext()); 669 browser_context_->GetResourceContext());
638 670
639 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting(); 671 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting();
640 672
641 browser_context_.reset(); 673 browser_context_.reset();
642 message_loop_.RunUntilIdle(); 674 base::RunLoop().RunUntilIdle();
643 } 675 }
644 676
645 // Creates a request using the current test object as the filter and 677 // Creates a request using the current test object as the filter and
646 // SubResource as the resource type. 678 // SubResource as the resource type.
647 void MakeTestRequest(int render_view_id, 679 void MakeTestRequest(int render_view_id,
648 int request_id, 680 int request_id,
649 const GURL& url); 681 const GURL& url);
650 682
651 // Generates a request using the given filter and resource type. 683 // Generates a request using the given filter and resource type.
652 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter, 684 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter,
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
768 } 800 }
769 801
770 // Setting filters for testing renderer messages. 802 // Setting filters for testing renderer messages.
771 // Returns the previous filter. 803 // Returns the previous filter.
772 ResourceMessageFilter* SetFilter(ResourceMessageFilter* new_filter) { 804 ResourceMessageFilter* SetFilter(ResourceMessageFilter* new_filter) {
773 ResourceMessageFilter* old_filter = host_.filter_; 805 ResourceMessageFilter* old_filter = host_.filter_;
774 host_.filter_ = new_filter; 806 host_.filter_ = new_filter;
775 return old_filter; 807 return old_filter;
776 } 808 }
777 809
778 base::MessageLoopForIO message_loop_; 810 void WaitForRequestComplete() {
779 BrowserThreadImpl ui_thread_; 811 DCHECK(!wait_for_request_complete_loop_);
780 BrowserThreadImpl file_thread_; 812 wait_for_request_complete_loop_.reset(new base::RunLoop);
781 BrowserThreadImpl cache_thread_; 813 wait_for_request_complete_loop_->Run();
782 BrowserThreadImpl io_thread_; 814 wait_for_request_complete_loop_.reset();
815 }
816
817 content::TestBrowserThreadBundle thread_bundle_;
783 scoped_ptr<TestBrowserContext> browser_context_; 818 scoped_ptr<TestBrowserContext> browser_context_;
784 scoped_refptr<ForwardingFilter> filter_; 819 scoped_refptr<ForwardingFilter> filter_;
785 net::TestNetworkDelegate network_delegate_; 820 net::TestNetworkDelegate network_delegate_;
786 ResourceDispatcherHostImpl host_; 821 ResourceDispatcherHostImpl host_;
787 ResourceIPCAccumulator accum_; 822 ResourceIPCAccumulator accum_;
788 std::string response_headers_; 823 std::string response_headers_;
789 std::string response_data_; 824 std::string response_data_;
790 std::string scheme_; 825 std::string scheme_;
791 net::URLRequest::ProtocolFactory* old_factory_; 826 net::URLRequest::ProtocolFactory* old_factory_;
792 bool send_data_received_acks_; 827 bool send_data_received_acks_;
793 std::set<int> child_ids_; 828 std::set<int> child_ids_;
829 scoped_ptr<base::RunLoop> wait_for_request_complete_loop_;
794 static ResourceDispatcherHostTest* test_fixture_; 830 static ResourceDispatcherHostTest* test_fixture_;
795 static bool delay_start_; 831 static bool delay_start_;
796 static bool delay_complete_; 832 static bool delay_complete_;
797 static bool network_start_notification_; 833 static bool network_start_notification_;
798 static int url_request_jobs_created_count_; 834 static int url_request_jobs_created_count_;
799 }; 835 };
800 // Static. 836 // Static.
801 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; 837 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL;
802 bool ResourceDispatcherHostTest::delay_start_ = false; 838 bool ResourceDispatcherHostTest::delay_start_ = false;
803 bool ResourceDispatcherHostTest::delay_complete_ = false; 839 bool ResourceDispatcherHostTest::delay_complete_ = false;
(...skipping 1902 matching lines...) Expand 10 before | Expand all | Expand 10 after
2706 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok); 2742 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok);
2707 } 2743 }
2708 2744
2709 base::MessageLoop::current()->RunUntilIdle(); 2745 base::MessageLoop::current()->RunUntilIdle();
2710 2746
2711 msgs.clear(); 2747 msgs.clear();
2712 accum_.GetClassifiedMessages(&msgs); 2748 accum_.GetClassifiedMessages(&msgs);
2713 } 2749 }
2714 } 2750 }
2715 2751
2752 // Tests the dispatcher host's temporary file management.
2753 TEST_F(ResourceDispatcherHostTest, RegisterDownloadedTempFile) {
2754 const int kRequestID = 1;
2755
2756 // Create a temporary file.
2757 base::FilePath file_path;
2758 ASSERT_TRUE(base::CreateTemporaryFile(&file_path));
2759 scoped_refptr<ShareableFileReference> deletable_file =
2760 ShareableFileReference::GetOrCreate(
2761 file_path,
2762 ShareableFileReference::DELETE_ON_FINAL_RELEASE,
2763 BrowserThread::GetMessageLoopProxyForThread(
2764 BrowserThread::FILE).get());
2765
2766 // Not readable.
2767 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
2768 filter_->child_id(), file_path));
2769
2770 // Register it for a resource request.
2771 host_.RegisterDownloadedTempFile(filter_->child_id(), kRequestID, file_path);
2772
2773 // Should be readable now.
2774 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
2775 filter_->child_id(), file_path));
2776
2777 // The child releases from the request.
2778 bool msg_was_ok = true;
2779 ResourceHostMsg_ReleaseDownloadedFile release_msg(kRequestID);
2780 host_.OnMessageReceived(release_msg, filter_, &msg_was_ok);
2781 ASSERT_TRUE(msg_was_ok);
2782
2783 // Still readable because there is another reference to the file. (The child
2784 // may take additional blob references.)
2785 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
2786 filter_->child_id(), file_path));
2787
2788 // Release extra references.
2789 deletable_file = NULL;
2790 base::MessageLoop::current()->RunUntilIdle();
mmenke 2014/03/03 21:10:11 Think it's worth mentioning that this will wait fo
mmenke 2014/03/03 21:10:11 base::RunLoop().RunUntilIdle();
davidben 2014/03/03 23:20:18 Done.
davidben 2014/03/03 23:20:18 Done. (Well, it's still async. Just on the same th
2791
2792 // The file is no longer readable to the child and has been deleted.
2793 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
2794 filter_->child_id(), file_path));
2795 EXPECT_FALSE(base::PathExists(file_path));
2796 }
2797
2798 // Tests that temporary files held on behalf of child processes are released
2799 // when the child process dies.
2800 TEST_F(ResourceDispatcherHostTest, ReleaseTemporiesOnProcessExit) {
2801 const int kRequestID = 1;
2802
2803 // Create a temporary file.
2804 base::FilePath file_path;
2805 ASSERT_TRUE(base::CreateTemporaryFile(&file_path));
2806 scoped_refptr<ShareableFileReference> deletable_file =
2807 ShareableFileReference::GetOrCreate(
2808 file_path,
2809 ShareableFileReference::DELETE_ON_FINAL_RELEASE,
2810 BrowserThread::GetMessageLoopProxyForThread(
2811 BrowserThread::FILE).get());
2812
2813 // Register it for a resource request.
2814 host_.RegisterDownloadedTempFile(filter_->child_id(), kRequestID, file_path);
2815 deletable_file = NULL;
2816
2817 // Should be readable now.
2818 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
2819 filter_->child_id(), file_path));
2820
2821 // Let the process die.
2822 filter_->OnChannelClosing();
2823 base::MessageLoop::current()->RunUntilIdle();
mmenke 2014/03/03 21:10:11 base::RunLoop().RunUntilIdle();
davidben 2014/03/03 23:20:18 Done.
2824
2825 // The file is no longer readable to the child and has been deleted.
2826 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
2827 filter_->child_id(), file_path));
2828 EXPECT_FALSE(base::PathExists(file_path));
2829 }
2830
2831 TEST_F(ResourceDispatcherHostTest, DownloadToFile) {
2832 // Make a request which downloads to file.
2833 ResourceHostMsg_Request request = CreateResourceRequest(
2834 "GET", ResourceType::SUB_RESOURCE, net::URLRequestTestJob::test_url_1());
2835 request.download_to_file = true;
2836 ResourceHostMsg_RequestResource request_msg(0, 1, request);
2837 bool msg_was_ok;
2838 host_.OnMessageReceived(request_msg, filter_, &msg_was_ok);
2839 ASSERT_TRUE(msg_was_ok);
2840
2841 // Running the message loop until idle does not work because
2842 // RedirectToFileResourceHandler posts things to base::WorkerPool. Instead,
2843 // wait for the ResourceMsg_RequestComplete to go out. Then run the event loop
2844 // until idle so the loader is gone.
2845 WaitForRequestComplete();
2846 base::MessageLoop::current()->RunUntilIdle();
mmenke 2014/03/03 21:10:11 base::RunLoop().RunUntilIdle();
davidben 2014/03/03 23:20:18 Done.
2847 EXPECT_EQ(0, host_.pending_requests());
2848
2849 ResourceIPCAccumulator::ClassifiedMessages msgs;
2850 accum_.GetClassifiedMessages(&msgs);
2851
2852 ASSERT_EQ(1U, msgs.size());
2853 const std::vector<IPC::Message>& messages = msgs[0];
2854
2855 // The request should contain the following messages:
2856 // ReceivedResponse (indicates headers received and filename)
2857 // DataDownloaded* (bytes downloaded and total length)
2858 // RequestComplete (request is done)
2859
2860 // ReceivedResponse
2861 ResourceResponseHead response_head;
2862 GetResponseHead(messages, &response_head);
2863 ASSERT_FALSE(response_head.download_file_path.empty());
2864
2865 // DataDownloaded
2866 size_t total_len = 0;
2867 for (size_t i = 1; i < messages.size() - 1; i++) {
2868 ASSERT_EQ(ResourceMsg_DataDownloaded::ID, messages[i].type());
2869 PickleIterator iter(messages[i]);
2870 int request_id, data_len;
2871 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &request_id));
2872 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &data_len));
2873 total_len += data_len;
2874 }
2875 EXPECT_EQ(net::URLRequestTestJob::test_data_1().size(), total_len);
2876
2877 // RequestComplete
2878 CheckRequestCompleteErrorCode(messages.back(), net::OK);
2879
2880 // Verify that the data ended up in the temporary file.
2881 std::string contents;
2882 ASSERT_TRUE(base::ReadFileToString(response_head.download_file_path,
2883 &contents));
2884 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), contents);
2885
2886 // The file should be readable by the child.
2887 EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
2888 filter_->child_id(), response_head.download_file_path));
2889
2890 // When the renderer releases the file, it should be deleted. Again,
2891 // RunUntilIdle doesn't work because base::WorkerPool is involved.
2892 ShareableFileReleaseWaiter waiter(response_head.download_file_path);
2893 ResourceHostMsg_ReleaseDownloadedFile release_msg(1);
2894 host_.OnMessageReceived(release_msg, filter_, &msg_was_ok);
2895 ASSERT_TRUE(msg_was_ok);
2896 waiter.Wait();
2897 // The release callback runs before the delete is scheduled, so pump the
2898 // message loop for the delete itself.
mmenke 2014/03/03 21:10:11 May be worth mentioning this depends on DidCreateT
davidben 2014/03/03 23:20:18 Done.
2899 base::MessageLoop::current()->RunUntilIdle();
mmenke 2014/03/03 21:10:11 base::RunLoop().RunUntilIdle();
davidben 2014/03/03 23:20:18 Done.
2900
2901 EXPECT_FALSE(base::PathExists(response_head.download_file_path));
2902 EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile(
2903 filter_->child_id(), response_head.download_file_path));
2904 }
2905
2716 } // namespace content 2906 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698