rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 5 | #include "update_engine/mock_http_fetcher.h" |
| 6 | |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
| 9 | #include <base/logging.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 12 | // This is a mock implementation of HttpFetcher which is useful for testing. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 13 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 14 | using chromeos::MessageLoop; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 15 | using std::min; |
| 16 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | MockHttpFetcher::~MockHttpFetcher() { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 20 | CHECK(timeout_id_ == MessageLoop::kTaskIdNull) << |
| 21 | "Call TerminateTransfer() before dtor."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | void MockHttpFetcher::BeginTransfer(const std::string& url) { |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 25 | EXPECT_FALSE(never_use_); |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 26 | if (fail_transfer_ || data_.empty()) { |
| 27 | // No data to send, just notify of completion.. |
| 28 | SignalTransferComplete(); |
| 29 | return; |
| 30 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | if (sent_size_ < data_.size()) |
| 32 | SendData(true); |
| 33 | } |
| 34 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 35 | // Returns false on one condition: If timeout_id_ was already set |
| 36 | // and it needs to be deleted by the caller. If timeout_id_ is null |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 37 | // when this function is called, this function will always return true. |
| 38 | bool MockHttpFetcher::SendData(bool skip_delivery) { |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 39 | if (fail_transfer_) { |
| 40 | SignalTransferComplete(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 41 | return timeout_id_ != MessageLoop::kTaskIdNull; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 42 | } |
| 43 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 44 | CHECK_LT(sent_size_, data_.size()); |
| 45 | if (!skip_delivery) { |
| 46 | const size_t chunk_size = min(kMockHttpFetcherChunkSize, |
| 47 | data_.size() - sent_size_); |
| 48 | CHECK(delegate_); |
| 49 | delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 50 | // We may get terminated in the callback. |
| 51 | if (sent_size_ == data_.size()) { |
| 52 | LOG(INFO) << "Terminated in the ReceivedBytes callback."; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 53 | return timeout_id_ != MessageLoop::kTaskIdNull; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 54 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 | sent_size_ += chunk_size; |
| 56 | CHECK_LE(sent_size_, data_.size()); |
| 57 | if (sent_size_ == data_.size()) { |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 58 | // We've sent all the data. Notify of success. |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 59 | SignalTransferComplete(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | if (paused_) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 64 | // If we're paused, we should return true if timeout_id_ is set, |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 65 | // since we need the caller to delete it. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 66 | return timeout_id_ != MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 69 | if (timeout_id_ != MessageLoop::kTaskIdNull) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | // we still need a timeout if there's more data to send |
| 71 | return sent_size_ < data_.size(); |
| 72 | } else if (sent_size_ < data_.size()) { |
| 73 | // we don't have a timeout source and we need one |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 74 | timeout_id_ = MessageLoop::current()->PostDelayedTask( |
| 75 | FROM_HERE, |
| 76 | base::Bind(&MockHttpFetcher::TimeoutCallback, base::Unretained(this)), |
| 77 | base::TimeDelta::FromMilliseconds(10)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 82 | void MockHttpFetcher::TimeoutCallback() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 83 | CHECK(!paused_); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 84 | if (SendData(false)) { |
| 85 | // We need to re-schedule the timeout. |
| 86 | timeout_id_ = MessageLoop::current()->PostDelayedTask( |
| 87 | FROM_HERE, |
| 88 | base::Bind(&MockHttpFetcher::TimeoutCallback, base::Unretained(this)), |
| 89 | base::TimeDelta::FromMilliseconds(10)); |
| 90 | } else { |
| 91 | timeout_id_ = MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 92 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // If the transfer is in progress, aborts the transfer early. |
| 96 | // The transfer cannot be resumed. |
| 97 | void MockHttpFetcher::TerminateTransfer() { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 98 | LOG(INFO) << "Terminating transfer."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 99 | sent_size_ = data_.size(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 100 | // Kill any timeout, it is ok to call with kTaskIdNull. |
| 101 | MessageLoop::current()->CancelTask(timeout_id_); |
| 102 | timeout_id_ = MessageLoop::kTaskIdNull; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 103 | delegate_->TransferTerminated(this); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void MockHttpFetcher::Pause() { |
| 107 | CHECK(!paused_); |
| 108 | paused_ = true; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame^] | 109 | MessageLoop::current()->CancelTask(timeout_id_); |
| 110 | timeout_id_ = MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void MockHttpFetcher::Unpause() { |
| 114 | CHECK(paused_) << "You must pause before unpause."; |
| 115 | paused_ = false; |
| 116 | if (sent_size_ < data_.size()) { |
| 117 | SendData(false); |
| 118 | } |
| 119 | } |
| 120 | |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 121 | void MockHttpFetcher::FailTransfer(int http_response_code) { |
| 122 | fail_transfer_ = true; |
| 123 | http_response_code_ = http_response_code; |
| 124 | } |
| 125 | |
| 126 | void MockHttpFetcher::SignalTransferComplete() { |
| 127 | // If the transfer has been failed, the HTTP response code should be set |
| 128 | // already. |
| 129 | if (!fail_transfer_) { |
| 130 | http_response_code_ = 200; |
| 131 | } |
| 132 | delegate_->TransferComplete(this, !fail_transfer_); |
| 133 | } |
| 134 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 135 | } // namespace chromeos_update_engine |