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