| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/appcache/appcache_url_request_job.h" | 5 #include "content/browser/appcache/appcache_url_request_job.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 scoped_refptr<AppCacheResponseInfo> loaded_info_; | 145 scoped_refptr<AppCacheResponseInfo> loaded_info_; |
| 146 int64_t loaded_info_id_; | 146 int64_t loaded_info_id_; |
| 147 AppCacheURLRequestJobTest* test_; | 147 AppCacheURLRequestJobTest* test_; |
| 148 }; | 148 }; |
| 149 | 149 |
| 150 class MockURLRequestDelegate : public net::URLRequest::Delegate { | 150 class MockURLRequestDelegate : public net::URLRequest::Delegate { |
| 151 public: | 151 public: |
| 152 explicit MockURLRequestDelegate(AppCacheURLRequestJobTest* test) | 152 explicit MockURLRequestDelegate(AppCacheURLRequestJobTest* test) |
| 153 : test_(test), | 153 : test_(test), |
| 154 received_data_(new net::IOBuffer(kNumBlocks * kBlockSize)), | 154 received_data_(new net::IOBuffer(kNumBlocks * kBlockSize)), |
| 155 did_receive_headers_(false), amount_received_(0), | 155 did_receive_headers_(false), |
| 156 kill_after_amount_received_(0), kill_with_io_pending_(false) { | 156 amount_received_(0), |
| 157 } | 157 kill_after_amount_received_(0), |
| 158 kill_with_io_pending_(false), |
| 159 request_status_(net::ERR_IO_PENDING) {} |
| 158 | 160 |
| 159 void OnResponseStarted(net::URLRequest* request) override { | 161 void OnResponseStarted(net::URLRequest* request, int net_error) override { |
| 162 DCHECK_NE(net::ERR_IO_PENDING, net_error); |
| 160 amount_received_ = 0; | 163 amount_received_ = 0; |
| 161 did_receive_headers_ = false; | 164 did_receive_headers_ = false; |
| 162 if (request->status().is_success()) { | 165 request_status_ = net_error; |
| 166 if (net_error == net::OK) { |
| 163 EXPECT_TRUE(request->response_headers()); | 167 EXPECT_TRUE(request->response_headers()); |
| 164 did_receive_headers_ = true; | 168 did_receive_headers_ = true; |
| 165 received_info_ = request->response_info(); | 169 received_info_ = request->response_info(); |
| 166 ReadSome(request); | 170 ReadSome(request); |
| 167 } else { | 171 } else { |
| 168 RequestComplete(); | 172 RequestComplete(); |
| 169 } | 173 } |
| 170 } | 174 } |
| 171 | 175 |
| 172 void OnReadCompleted(net::URLRequest* request, int bytes_read) override { | 176 void OnReadCompleted(net::URLRequest* request, int bytes_read) override { |
| 173 if (bytes_read > 0) { | 177 if (bytes_read > 0) { |
| 174 amount_received_ += bytes_read; | 178 amount_received_ += bytes_read; |
| 179 request_status_ = net::OK; |
| 175 | 180 |
| 176 if (kill_after_amount_received_ && !kill_with_io_pending_) { | 181 if (kill_after_amount_received_ && !kill_with_io_pending_) { |
| 177 if (amount_received_ >= kill_after_amount_received_) { | 182 if (amount_received_ >= kill_after_amount_received_) { |
| 178 request->Cancel(); | 183 request->Cancel(); |
| 179 return; | 184 return; |
| 180 } | 185 } |
| 181 } | 186 } |
| 182 | 187 |
| 183 ReadSome(request); | 188 ReadSome(request); |
| 184 | 189 |
| 185 if (kill_after_amount_received_ && kill_with_io_pending_) { | 190 if (kill_after_amount_received_ && kill_with_io_pending_) { |
| 186 if (amount_received_ >= kill_after_amount_received_) { | 191 if (amount_received_ >= kill_after_amount_received_) { |
| 187 request->Cancel(); | 192 request->Cancel(); |
| 188 return; | 193 return; |
| 189 } | 194 } |
| 190 } | 195 } |
| 191 } else { | 196 } else { |
| 197 request_status_ = bytes_read; |
| 192 RequestComplete(); | 198 RequestComplete(); |
| 193 } | 199 } |
| 194 } | 200 } |
| 195 | 201 |
| 196 void ReadSome(net::URLRequest* request) { | 202 void ReadSome(net::URLRequest* request) { |
| 197 DCHECK(amount_received_ + kBlockSize <= kNumBlocks * kBlockSize); | 203 DCHECK(amount_received_ + kBlockSize <= kNumBlocks * kBlockSize); |
| 198 scoped_refptr<IOBuffer> wrapped_buffer( | 204 scoped_refptr<IOBuffer> wrapped_buffer( |
| 199 new net::WrappedIOBuffer(received_data_->data() + amount_received_)); | 205 new net::WrappedIOBuffer(received_data_->data() + amount_received_)); |
| 200 int bytes_read = 0; | 206 EXPECT_EQ(net::ERR_IO_PENDING, |
| 201 EXPECT_FALSE( | 207 request->Read(wrapped_buffer.get(), kBlockSize)); |
| 202 request->Read(wrapped_buffer.get(), kBlockSize, &bytes_read)); | |
| 203 EXPECT_EQ(0, bytes_read); | |
| 204 } | 208 } |
| 205 | 209 |
| 206 void RequestComplete() { | 210 void RequestComplete() { |
| 207 test_->ScheduleNextTask(); | 211 test_->ScheduleNextTask(); |
| 208 } | 212 } |
| 209 | 213 |
| 214 int request_status() { return request_status_; } |
| 215 |
| 210 AppCacheURLRequestJobTest* test_; | 216 AppCacheURLRequestJobTest* test_; |
| 211 net::HttpResponseInfo received_info_; | 217 net::HttpResponseInfo received_info_; |
| 212 scoped_refptr<net::IOBuffer> received_data_; | 218 scoped_refptr<net::IOBuffer> received_data_; |
| 213 bool did_receive_headers_; | 219 bool did_receive_headers_; |
| 214 int amount_received_; | 220 int amount_received_; |
| 215 int kill_after_amount_received_; | 221 int kill_after_amount_received_; |
| 216 bool kill_with_io_pending_; | 222 bool kill_with_io_pending_; |
| 223 int request_status_; |
| 217 }; | 224 }; |
| 218 | 225 |
| 219 // Helper callback to run a test on our io_thread. The io_thread is spun up | 226 // Helper callback to run a test on our io_thread. The io_thread is spun up |
| 220 // once and reused for all tests. | 227 // once and reused for all tests. |
| 221 template <class Method> | 228 template <class Method> |
| 222 void MethodWrapper(Method method) { | 229 void MethodWrapper(Method method) { |
| 223 SetUpTest(); | 230 SetUpTest(); |
| 224 (this->*method)(); | 231 (this->*method)(); |
| 225 } | 232 } |
| 226 | 233 |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 | 549 |
| 543 // Start the request. | 550 // Start the request. |
| 544 request_->Start(); | 551 request_->Start(); |
| 545 | 552 |
| 546 // The job should have been picked up. | 553 // The job should have been picked up. |
| 547 EXPECT_FALSE(job_factory_->has_job()); | 554 EXPECT_FALSE(job_factory_->has_job()); |
| 548 // Completion is async. | 555 // Completion is async. |
| 549 } | 556 } |
| 550 | 557 |
| 551 void VerifyDeliverNetworkResponse() { | 558 void VerifyDeliverNetworkResponse() { |
| 552 EXPECT_EQ(request_->status().error(), | 559 EXPECT_EQ(net::ERR_INTERNET_DISCONNECTED, |
| 553 net::ERR_INTERNET_DISCONNECTED); | 560 url_request_delegate_->request_status()); |
| 554 EXPECT_TRUE(restart_callback_invoked_); | 561 EXPECT_TRUE(restart_callback_invoked_); |
| 555 TestFinished(); | 562 TestFinished(); |
| 556 } | 563 } |
| 557 | 564 |
| 558 // DeliverErrorResponse -------------------------------------------------- | 565 // DeliverErrorResponse -------------------------------------------------- |
| 559 | 566 |
| 560 void DeliverErrorResponse() { | 567 void DeliverErrorResponse() { |
| 561 // This test has async steps. | 568 // This test has async steps. |
| 562 PushNextTask( | 569 PushNextTask( |
| 563 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, | 570 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 580 | 587 |
| 581 // Start the request. | 588 // Start the request. |
| 582 request_->Start(); | 589 request_->Start(); |
| 583 | 590 |
| 584 // The job should have been picked up. | 591 // The job should have been picked up. |
| 585 EXPECT_FALSE(job_factory_->has_job()); | 592 EXPECT_FALSE(job_factory_->has_job()); |
| 586 // Completion is async. | 593 // Completion is async. |
| 587 } | 594 } |
| 588 | 595 |
| 589 void VerifyDeliverErrorResponse() { | 596 void VerifyDeliverErrorResponse() { |
| 590 EXPECT_EQ(request_->status().error(), net::ERR_FAILED); | 597 EXPECT_EQ(net::ERR_FAILED, url_request_delegate_->request_status()); |
| 591 TestFinished(); | 598 TestFinished(); |
| 592 } | 599 } |
| 593 | 600 |
| 594 // DeliverSmallAppCachedResponse -------------------------------------- | 601 // DeliverSmallAppCachedResponse -------------------------------------- |
| 595 // "Small" being small enough to read completely in a single | 602 // "Small" being small enough to read completely in a single |
| 596 // request->Read call. | 603 // request->Read call. |
| 597 | 604 |
| 598 void DeliverSmallAppCachedResponse() { | 605 void DeliverSmallAppCachedResponse() { |
| 599 // This test has several async steps. | 606 // This test has several async steps. |
| 600 // 1. Write a small response to response storage. | 607 // 1. Write a small response to response storage. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 GURL(), 111, | 654 GURL(), 111, |
| 648 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), false); | 655 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), false); |
| 649 ASSERT_TRUE(weak_job); | 656 ASSERT_TRUE(weak_job); |
| 650 EXPECT_TRUE(weak_job->is_delivering_appcache_response()); | 657 EXPECT_TRUE(weak_job->is_delivering_appcache_response()); |
| 651 } | 658 } |
| 652 | 659 |
| 653 // Completion is async. | 660 // Completion is async. |
| 654 } | 661 } |
| 655 | 662 |
| 656 void VerifyDeliverSmallAppCachedResponse() { | 663 void VerifyDeliverSmallAppCachedResponse() { |
| 657 EXPECT_TRUE(request_->status().is_success()); | 664 EXPECT_EQ(net::OK, url_request_delegate_->request_status()); |
| 658 EXPECT_TRUE(CompareHttpResponseInfos( | 665 EXPECT_TRUE(CompareHttpResponseInfos( |
| 659 write_info_buffer_->http_info.get(), | 666 write_info_buffer_->http_info.get(), |
| 660 &url_request_delegate_->received_info_)); | 667 &url_request_delegate_->received_info_)); |
| 661 EXPECT_EQ(5, url_request_delegate_->amount_received_); | 668 EXPECT_EQ(5, url_request_delegate_->amount_received_); |
| 662 EXPECT_EQ(0, memcmp(kHttpBasicBody, | 669 EXPECT_EQ(0, memcmp(kHttpBasicBody, |
| 663 url_request_delegate_->received_data_->data(), | 670 url_request_delegate_->received_data_->data(), |
| 664 strlen(kHttpBasicBody))); | 671 strlen(kHttpBasicBody))); |
| 665 TestFinished(); | 672 TestFinished(); |
| 666 } | 673 } |
| 667 | 674 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 694 scoped_refptr<IOBuffer> body(new IOBuffer(kBlockSize * 3)); | 701 scoped_refptr<IOBuffer> body(new IOBuffer(kBlockSize * 3)); |
| 695 char* p = body->data(); | 702 char* p = body->data(); |
| 696 for (int i = 0; i < 3; ++i, p += kBlockSize) | 703 for (int i = 0; i < 3; ++i, p += kBlockSize) |
| 697 FillData(i + 1, p, kBlockSize); | 704 FillData(i + 1, p, kBlockSize); |
| 698 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); | 705 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| 699 WriteResponse( | 706 WriteResponse( |
| 700 MakeHttpResponseInfo(raw_headers), body.get(), kBlockSize * 3); | 707 MakeHttpResponseInfo(raw_headers), body.get(), kBlockSize * 3); |
| 701 } | 708 } |
| 702 | 709 |
| 703 void VerifyDeliverLargeAppCachedResponse() { | 710 void VerifyDeliverLargeAppCachedResponse() { |
| 704 EXPECT_TRUE(request_->status().is_success()); | 711 EXPECT_EQ(net::OK, url_request_delegate_->request_status()); |
| 705 EXPECT_TRUE(CompareHttpResponseInfos( | 712 EXPECT_TRUE(CompareHttpResponseInfos( |
| 706 write_info_buffer_->http_info.get(), | 713 write_info_buffer_->http_info.get(), |
| 707 &url_request_delegate_->received_info_)); | 714 &url_request_delegate_->received_info_)); |
| 708 EXPECT_EQ(3072, url_request_delegate_->amount_received_); | 715 EXPECT_EQ(3072, url_request_delegate_->amount_received_); |
| 709 char* p = url_request_delegate_->received_data_->data(); | 716 char* p = url_request_delegate_->received_data_->data(); |
| 710 for (int i = 0; i < 3; ++i, p += kBlockSize) | 717 for (int i = 0; i < 3; ++i, p += kBlockSize) |
| 711 EXPECT_TRUE(CheckData(i + 1, p, kBlockSize)); | 718 EXPECT_TRUE(CheckData(i + 1, p, kBlockSize)); |
| 712 TestFinished(); | 719 TestFinished(); |
| 713 } | 720 } |
| 714 | 721 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 | 759 |
| 753 // Start the request. | 760 // Start the request. |
| 754 EXPECT_FALSE(job->has_been_started()); | 761 EXPECT_FALSE(job->has_been_started()); |
| 755 job_factory_->SetJob(std::move(job)); | 762 job_factory_->SetJob(std::move(job)); |
| 756 request_->Start(); | 763 request_->Start(); |
| 757 EXPECT_FALSE(job_factory_->has_job()); | 764 EXPECT_FALSE(job_factory_->has_job()); |
| 758 // Completion is async. | 765 // Completion is async. |
| 759 } | 766 } |
| 760 | 767 |
| 761 void VerifyDeliverPartialResponse() { | 768 void VerifyDeliverPartialResponse() { |
| 762 EXPECT_TRUE(request_->status().is_success()); | 769 EXPECT_EQ(net::OK, url_request_delegate_->request_status()); |
| 763 EXPECT_EQ(3, url_request_delegate_->amount_received_); | 770 EXPECT_EQ(3, url_request_delegate_->amount_received_); |
| 764 EXPECT_EQ(0, memcmp(kHttpBasicBody + 1, | 771 EXPECT_EQ(0, memcmp(kHttpBasicBody + 1, |
| 765 url_request_delegate_->received_data_->data(), | 772 url_request_delegate_->received_data_->data(), |
| 766 3)); | 773 3)); |
| 767 net::HttpResponseHeaders* headers = | 774 net::HttpResponseHeaders* headers = |
| 768 url_request_delegate_->received_info_.headers.get(); | 775 url_request_delegate_->received_info_.headers.get(); |
| 769 EXPECT_EQ(206, headers->response_code()); | 776 EXPECT_EQ(206, headers->response_code()); |
| 770 EXPECT_EQ(3, headers->GetContentLength()); | 777 EXPECT_EQ(3, headers->GetContentLength()); |
| 771 int64_t range_start, range_end, object_size; | 778 int64_t range_start, range_end, object_size; |
| 772 EXPECT_TRUE( | 779 EXPECT_TRUE( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 794 writer_.reset(service_->storage()->CreateResponseWriter(GURL())); | 801 writer_.reset(service_->storage()->CreateResponseWriter(GURL())); |
| 795 written_response_id_ = writer_->response_id(); | 802 written_response_id_ = writer_->response_id(); |
| 796 WriteLargeResponse(); | 803 WriteLargeResponse(); |
| 797 | 804 |
| 798 url_request_delegate_->kill_after_amount_received_ = kBlockSize; | 805 url_request_delegate_->kill_after_amount_received_ = kBlockSize; |
| 799 url_request_delegate_->kill_with_io_pending_ = false; | 806 url_request_delegate_->kill_with_io_pending_ = false; |
| 800 // Continues async | 807 // Continues async |
| 801 } | 808 } |
| 802 | 809 |
| 803 void VerifyCancel() { | 810 void VerifyCancel() { |
| 804 EXPECT_EQ(net::URLRequestStatus::CANCELED, | 811 EXPECT_EQ(net::ERR_ABORTED, url_request_delegate_->request_status()); |
| 805 request_->status().status()); | |
| 806 TestFinished(); | 812 TestFinished(); |
| 807 } | 813 } |
| 808 | 814 |
| 809 // CancelRequestWithIOPending -------------------------------------- | 815 // CancelRequestWithIOPending -------------------------------------- |
| 810 | 816 |
| 811 void CancelRequestWithIOPending() { | 817 void CancelRequestWithIOPending() { |
| 812 // This test has several async steps. | 818 // This test has several async steps. |
| 813 // 1. Write a large response to response storage. | 819 // 1. Write a large response to response storage. |
| 814 // 2. Use net::URLRequest to retrieve it. | 820 // 2. Use net::URLRequest to retrieve it. |
| 815 // 3. Cancel the request after data starts coming in. | 821 // 3. Cancel the request after data starts coming in. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 893 | 899 |
| 894 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { | 900 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { |
| 895 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); | 901 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); |
| 896 } | 902 } |
| 897 | 903 |
| 898 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { | 904 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { |
| 899 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); | 905 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); |
| 900 } | 906 } |
| 901 | 907 |
| 902 } // namespace content | 908 } // namespace content |
| OLD | NEW |