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

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: Comment 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/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/detachable_resource_handler.h" 19 #include "content/browser/loader/detachable_resource_handler.h"
18 #include "content/browser/loader/resource_dispatcher_host_impl.h" 20 #include "content/browser/loader/resource_dispatcher_host_impl.h"
19 #include "content/browser/loader/resource_loader.h" 21 #include "content/browser/loader/resource_loader.h"
20 #include "content/browser/loader/resource_message_filter.h" 22 #include "content/browser/loader/resource_message_filter.h"
21 #include "content/browser/loader/resource_request_info_impl.h" 23 #include "content/browser/loader/resource_request_info_impl.h"
22 #include "content/browser/worker_host/worker_service_impl.h" 24 #include "content/browser/worker_host/worker_service_impl.h"
23 #include "content/common/child_process_host_impl.h" 25 #include "content/common/child_process_host_impl.h"
24 #include "content/common/resource_messages.h" 26 #include "content/common/resource_messages.h"
25 #include "content/common/view_messages.h" 27 #include "content/common/view_messages.h"
26 #include "content/public/browser/global_request_id.h" 28 #include "content/public/browser/global_request_id.h"
27 #include "content/public/browser/resource_context.h" 29 #include "content/public/browser/resource_context.h"
28 #include "content/public/browser/resource_dispatcher_host_delegate.h" 30 #include "content/public/browser/resource_dispatcher_host_delegate.h"
29 #include "content/public/browser/resource_request_info.h" 31 #include "content/public/browser/resource_request_info.h"
30 #include "content/public/browser/resource_throttle.h" 32 #include "content/public/browser/resource_throttle.h"
31 #include "content/public/common/process_type.h" 33 #include "content/public/common/process_type.h"
32 #include "content/public/common/resource_response.h" 34 #include "content/public/common/resource_response.h"
33 #include "content/public/test/test_browser_context.h" 35 #include "content/public/test/test_browser_context.h"
36 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "content/test/test_content_browser_client.h" 37 #include "content/test/test_content_browser_client.h"
35 #include "net/base/net_errors.h" 38 #include "net/base/net_errors.h"
36 #include "net/base/request_priority.h" 39 #include "net/base/request_priority.h"
37 #include "net/base/upload_bytes_element_reader.h" 40 #include "net/base/upload_bytes_element_reader.h"
38 #include "net/base/upload_data_stream.h" 41 #include "net/base/upload_data_stream.h"
39 #include "net/http/http_util.h" 42 #include "net/http/http_util.h"
40 #include "net/url_request/url_request.h" 43 #include "net/url_request/url_request.h"
41 #include "net/url_request/url_request_context.h" 44 #include "net/url_request/url_request_context.h"
42 #include "net/url_request/url_request_job.h" 45 #include "net/url_request/url_request_job.h"
43 #include "net/url_request/url_request_simple_job.h" 46 #include "net/url_request/url_request_simple_job.h"
44 #include "net/url_request/url_request_test_job.h" 47 #include "net/url_request/url_request_test_job.h"
45 #include "net/url_request/url_request_test_util.h" 48 #include "net/url_request/url_request_test_util.h"
46 #include "testing/gtest/include/gtest/gtest.h" 49 #include "testing/gtest/include/gtest/gtest.h"
47 #include "webkit/common/appcache/appcache_interfaces.h" 50 #include "webkit/common/appcache/appcache_interfaces.h"
51 #include "webkit/common/blob/shareable_file_reference.h"
48 52
49 // TODO(eroman): Write unit tests for SafeBrowsing that exercise 53 // TODO(eroman): Write unit tests for SafeBrowsing that exercise
50 // SafeBrowsingResourceHandler. 54 // SafeBrowsingResourceHandler.
51 55
56 using webkit_blob::ShareableFileReference;
57
52 namespace content { 58 namespace content {
53 59
54 namespace { 60 namespace {
55 61
56 // Returns the resource response header structure for this request. 62 // Returns the resource response header structure for this request.
57 void GetResponseHead(const std::vector<IPC::Message>& messages, 63 void GetResponseHead(const std::vector<IPC::Message>& messages,
58 ResourceResponseHead* response_head) { 64 ResourceResponseHead* response_head) {
59 ASSERT_GE(messages.size(), 2U); 65 ASSERT_GE(messages.size(), 2U);
60 66
61 // The first messages should be received response. 67 // The first messages should be received response.
(...skipping 16 matching lines...) Expand all
78 } // namespace 84 } // namespace
79 85
80 static int RequestIDForMessage(const IPC::Message& msg) { 86 static int RequestIDForMessage(const IPC::Message& msg) {
81 int request_id = -1; 87 int request_id = -1;
82 switch (msg.type()) { 88 switch (msg.type()) {
83 case ResourceMsg_UploadProgress::ID: 89 case ResourceMsg_UploadProgress::ID:
84 case ResourceMsg_ReceivedResponse::ID: 90 case ResourceMsg_ReceivedResponse::ID:
85 case ResourceMsg_ReceivedRedirect::ID: 91 case ResourceMsg_ReceivedRedirect::ID:
86 case ResourceMsg_SetDataBuffer::ID: 92 case ResourceMsg_SetDataBuffer::ID:
87 case ResourceMsg_DataReceived::ID: 93 case ResourceMsg_DataReceived::ID:
94 case ResourceMsg_DataDownloaded::ID:
88 case ResourceMsg_RequestComplete::ID: { 95 case ResourceMsg_RequestComplete::ID: {
89 bool result = PickleIterator(msg).ReadInt(&request_id); 96 bool result = PickleIterator(msg).ReadInt(&request_id);
90 DCHECK(result); 97 DCHECK(result);
91 break; 98 break;
92 } 99 }
93 } 100 }
94 return request_id; 101 return request_id;
95 } 102 }
96 103
97 static ResourceHostMsg_Request CreateResourceRequest( 104 static ResourceHostMsg_Request CreateResourceRequest(
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 } 518 }
512 } 519 }
513 520
514 private: 521 private:
515 bool create_two_throttles_; 522 bool create_two_throttles_;
516 int flags_; 523 int flags_;
517 int error_code_for_cancellation_; 524 int error_code_for_cancellation_;
518 scoped_ptr<base::SupportsUserData::Data> user_data_; 525 scoped_ptr<base::SupportsUserData::Data> user_data_;
519 }; 526 };
520 527
528 class ShareableFileReferenceWaiter {
529 public:
530 ShareableFileReferenceWaiter(const base::FilePath& path) {
531 scoped_refptr<ShareableFileReference> file =
532 ShareableFileReference::Get(path);
533 file->AddFinalReleaseCallback(
534 base::Bind(&ShareableFileReferenceWaiter::Released,
535 base::Unretained(this)));
536 }
537
538 void Wait() {
539 loop_.Run();
540 }
541
542 private:
543 void Released(const base::FilePath& path) {
544 loop_.Quit();
545 }
546
547 base::RunLoop loop_;
548 DISALLOW_COPY_AND_ASSIGN(ShareableFileReferenceWaiter);
549 };
550
521 class ResourceDispatcherHostTest : public testing::Test, 551 class ResourceDispatcherHostTest : public testing::Test,
522 public IPC::Sender { 552 public IPC::Sender {
523 public: 553 public:
524 ResourceDispatcherHostTest() 554 ResourceDispatcherHostTest()
525 : ui_thread_(BrowserThread::UI, &message_loop_), 555 : 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), 556 old_factory_(NULL),
530 send_data_received_acks_(false) { 557 send_data_received_acks_(false) {
531 browser_context_.reset(new TestBrowserContext()); 558 browser_context_.reset(new TestBrowserContext());
532 BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); 559 BrowserContext::EnsureResourceContextInitialized(browser_context_.get());
533 message_loop_.RunUntilIdle(); 560 base::RunLoop().RunUntilIdle();
534 ResourceContext* resource_context = browser_context_->GetResourceContext(); 561 ResourceContext* resource_context = browser_context_->GetResourceContext();
535 filter_ = new ForwardingFilter(this, resource_context); 562 filter_ = new ForwardingFilter(this, resource_context);
536 resource_context->GetRequestContext()->set_network_delegate( 563 resource_context->GetRequestContext()->set_network_delegate(
537 &network_delegate_); 564 &network_delegate_);
538 } 565 }
539 566
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 567 // IPC::Sender implementation
548 virtual bool Send(IPC::Message* msg) OVERRIDE { 568 virtual bool Send(IPC::Message* msg) OVERRIDE {
549 accum_.AddMessage(*msg); 569 accum_.AddMessage(*msg);
550 570
551 if (send_data_received_acks_ && 571 if (send_data_received_acks_ &&
552 msg->type() == ResourceMsg_DataReceived::ID) { 572 msg->type() == ResourceMsg_DataReceived::ID) {
553 GenerateDataReceivedACK(*msg); 573 GenerateDataReceivedACK(*msg);
554 } 574 }
555 575
576 if (wait_for_request_complete_loop_ &&
577 msg->type() == ResourceMsg_RequestComplete::ID) {
578 wait_for_request_complete_loop_->Quit();
579 }
580
556 delete msg; 581 delete msg;
557 return true; 582 return true;
558 } 583 }
559 584
560 protected: 585 protected:
561 // testing::Test 586 // testing::Test
562 virtual void SetUp() { 587 virtual void SetUp() {
563 DCHECK(!test_fixture_); 588 DCHECK(!test_fixture_);
564 test_fixture_ = this; 589 test_fixture_ = this;
565 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); 590 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0);
(...skipping 11 matching lines...) Expand all
577 if (!scheme_.empty()) 602 if (!scheme_.empty())
578 net::URLRequest::Deprecated::RegisterProtocolFactory( 603 net::URLRequest::Deprecated::RegisterProtocolFactory(
579 scheme_, old_factory_); 604 scheme_, old_factory_);
580 605
581 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); 606 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty());
582 URLRequestTestDelayedStartJob::ClearQueue(); 607 URLRequestTestDelayedStartJob::ClearQueue();
583 608
584 DCHECK(test_fixture_ == this); 609 DCHECK(test_fixture_ == this);
585 test_fixture_ = NULL; 610 test_fixture_ = NULL;
586 611
612 for (std::set<int>::iterator it = child_ids_.begin();
613 it != child_ids_.end(); ++it) {
614 host_.CancelRequestsForProcess(*it);
615 }
616
587 host_.Shutdown(); 617 host_.Shutdown();
588 618
589 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0); 619 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0);
590 620
591 // Flush the message loop to make application verifiers happy. 621 // Flush the message loop to make application verifiers happy.
592 if (ResourceDispatcherHostImpl::Get()) 622 if (ResourceDispatcherHostImpl::Get())
593 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext( 623 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext(
594 browser_context_->GetResourceContext()); 624 browser_context_->GetResourceContext());
595 625
596 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting(); 626 WorkerServiceImpl::GetInstance()->PerformTeardownForTesting();
597 627
598 browser_context_.reset(); 628 browser_context_.reset();
599 message_loop_.RunUntilIdle(); 629 base::RunLoop().RunUntilIdle();
600 } 630 }
601 631
602 // Creates a request using the current test object as the filter and 632 // Creates a request using the current test object as the filter and
603 // SubResource as the resource type. 633 // SubResource as the resource type.
604 void MakeTestRequest(int render_view_id, 634 void MakeTestRequest(int render_view_id,
605 int request_id, 635 int request_id,
606 const GURL& url); 636 const GURL& url);
607 637
608 // Generates a request using the given filter and resource type. 638 // Generates a request using the given filter and resource type.
609 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter, 639 void MakeTestRequestWithResourceType(ResourceMessageFilter* filter,
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 bool result = PickleIterator(msg).ReadInt(&request_id); 736 bool result = PickleIterator(msg).ReadInt(&request_id);
707 DCHECK(result); 737 DCHECK(result);
708 scoped_ptr<IPC::Message> ack( 738 scoped_ptr<IPC::Message> ack(
709 new ResourceHostMsg_DataReceived_ACK(request_id)); 739 new ResourceHostMsg_DataReceived_ACK(request_id));
710 740
711 base::MessageLoop::current()->PostTask( 741 base::MessageLoop::current()->PostTask(
712 FROM_HERE, 742 FROM_HERE,
713 base::Bind(&GenerateIPCMessage, filter_, base::Passed(&ack))); 743 base::Bind(&GenerateIPCMessage, filter_, base::Passed(&ack)));
714 } 744 }
715 745
716 base::MessageLoopForIO message_loop_; 746 void WaitForRequestComplete() {
717 BrowserThreadImpl ui_thread_; 747 DCHECK(!wait_for_request_complete_loop_);
718 BrowserThreadImpl file_thread_; 748 wait_for_request_complete_loop_.reset(new base::RunLoop);
719 BrowserThreadImpl cache_thread_; 749 wait_for_request_complete_loop_->Run();
720 BrowserThreadImpl io_thread_; 750 wait_for_request_complete_loop_.reset();
751 }
752
753 content::TestBrowserThreadBundle thread_bundle_;
721 scoped_ptr<TestBrowserContext> browser_context_; 754 scoped_ptr<TestBrowserContext> browser_context_;
722 scoped_refptr<ForwardingFilter> filter_; 755 scoped_refptr<ForwardingFilter> filter_;
723 net::TestNetworkDelegate network_delegate_; 756 net::TestNetworkDelegate network_delegate_;
724 ResourceDispatcherHostImpl host_; 757 ResourceDispatcherHostImpl host_;
725 ResourceIPCAccumulator accum_; 758 ResourceIPCAccumulator accum_;
726 std::string response_headers_; 759 std::string response_headers_;
727 std::string response_data_; 760 std::string response_data_;
728 std::string scheme_; 761 std::string scheme_;
729 net::URLRequest::ProtocolFactory* old_factory_; 762 net::URLRequest::ProtocolFactory* old_factory_;
730 bool send_data_received_acks_; 763 bool send_data_received_acks_;
731 std::set<int> child_ids_; 764 std::set<int> child_ids_;
765 scoped_ptr<base::RunLoop> wait_for_request_complete_loop_;
732 static ResourceDispatcherHostTest* test_fixture_; 766 static ResourceDispatcherHostTest* test_fixture_;
733 static bool delay_start_; 767 static bool delay_start_;
734 static bool delay_complete_; 768 static bool delay_complete_;
735 static int url_request_jobs_created_count_; 769 static int url_request_jobs_created_count_;
736 }; 770 };
737 // Static. 771 // Static.
738 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; 772 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL;
739 bool ResourceDispatcherHostTest::delay_start_ = false; 773 bool ResourceDispatcherHostTest::delay_start_ = false;
740 bool ResourceDispatcherHostTest::delay_complete_ = false; 774 bool ResourceDispatcherHostTest::delay_complete_ = false;
741 int ResourceDispatcherHostTest::url_request_jobs_created_count_ = 0; 775 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); 2632 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok);
2599 } 2633 }
2600 2634
2601 base::MessageLoop::current()->RunUntilIdle(); 2635 base::MessageLoop::current()->RunUntilIdle();
2602 2636
2603 msgs.clear(); 2637 msgs.clear();
2604 accum_.GetClassifiedMessages(&msgs); 2638 accum_.GetClassifiedMessages(&msgs);
2605 } 2639 }
2606 } 2640 }
2607 2641
2642 TEST_F(ResourceDispatcherHostTest, DownloadToFile) {
2643 // Make a request which downloads to file.
2644 ResourceHostMsg_Request request = CreateResourceRequest(
2645 "GET", ResourceType::SUB_RESOURCE, net::URLRequestTestJob::test_url_1());
2646 request.download_to_file = true;
2647 ResourceHostMsg_RequestResource request_msg(0, 1, request);
2648 bool msg_was_ok;
2649 host_.OnMessageReceived(request_msg, filter_, &msg_was_ok);
2650 ASSERT_TRUE(msg_was_ok);
2651
2652 // Running the message loop until idle does not work because
2653 // RedirectToFileResourceHandler posts things to base::WorkerPool. Instead,
2654 // wait for the ResourceMsg_RequestComplete to go out. Then run the event loop
2655 // until idle so the loader is gone.
2656 WaitForRequestComplete();
2657 base::MessageLoop::current()->RunUntilIdle();
2658 EXPECT_EQ(0, host_.pending_requests());
2659
2660 ResourceIPCAccumulator::ClassifiedMessages msgs;
2661 accum_.GetClassifiedMessages(&msgs);
2662
2663 ASSERT_EQ(1U, msgs.size());
2664 const std::vector<IPC::Message>& messages = msgs[0];
2665
2666 // The request should contain the following messages:
2667 // ReceivedResponse (indicates headers received and filename)
2668 // DataDownloaded* (bytes downloaded and total length)
2669 // RequestComplete (request is done)
2670
2671 // ReceivedResponse
2672 ResourceResponseHead response_head;
2673 GetResponseHead(messages, &response_head);
2674 ASSERT_FALSE(response_head.download_file_path.empty());
2675
2676 // DataDownloaded
2677 size_t total_len = 0;
2678 for (size_t i = 1; i < messages.size() - 1; i++) {
2679 ASSERT_EQ(ResourceMsg_DataDownloaded::ID, messages[i].type());
2680 PickleIterator iter(messages[i]);
2681 int request_id, data_len;
2682 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &request_id));
2683 ASSERT_TRUE(IPC::ReadParam(&messages[i], &iter, &data_len));
2684 total_len += data_len;
2685 }
2686 EXPECT_EQ(net::URLRequestTestJob::test_data_1().size(), total_len);
2687
2688 // RequestComplete
2689 CheckRequestCompleteErrorCode(messages.back(), net::OK);
2690
2691 // Verify that the data ended up in the temporary file.
2692 std::string contents;
2693 ASSERT_TRUE(base::ReadFileToString(response_head.download_file_path,
2694 &contents));
2695 EXPECT_EQ(net::URLRequestTestJob::test_data_1(), contents);
2696
2697 // When the renderer releases the file, it should be deleted. Again,
2698 // RunUntilIdle doesn't work because base::WorkerPool is involved.
2699 ShareableFileReferenceWaiter waiter(response_head.download_file_path);
2700 ResourceHostMsg_ReleaseDownloadedFile release_msg(1);
2701 host_.OnMessageReceived(release_msg, filter_, &msg_was_ok);
2702 ASSERT_TRUE(msg_was_ok);
2703 waiter.Wait();
2704 // The release callback runs before the delete is scheduled, so pump the
2705 // message loop for the delete itself.
2706 base::MessageLoop::current()->RunUntilIdle();
2707
2708 EXPECT_FALSE(base::PathExists(response_head.download_file_path));
2709 }
2710
2608 } // namespace content 2711 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698