Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 17 | #include "update_engine/mock_http_fetcher.h" |
| 18 | |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
| 21 | #include <base/logging.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 24 | // 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] | 25 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 26 | using brillo::MessageLoop; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 27 | using std::min; |
| 28 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 29 | namespace chromeos_update_engine { |
| 30 | |
| 31 | MockHttpFetcher::~MockHttpFetcher() { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 32 | CHECK(timeout_id_ == MessageLoop::kTaskIdNull) << |
| 33 | "Call TerminateTransfer() before dtor."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void MockHttpFetcher::BeginTransfer(const std::string& url) { |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 37 | EXPECT_FALSE(never_use_); |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 38 | if (fail_transfer_ || data_.empty()) { |
| 39 | // No data to send, just notify of completion.. |
| 40 | SignalTransferComplete(); |
| 41 | return; |
| 42 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 43 | if (sent_size_ < data_.size()) |
| 44 | SendData(true); |
| 45 | } |
| 46 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 47 | // Returns false on one condition: If timeout_id_ was already set |
| 48 | // 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] | 49 | // when this function is called, this function will always return true. |
| 50 | bool MockHttpFetcher::SendData(bool skip_delivery) { |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 51 | if (fail_transfer_) { |
| 52 | SignalTransferComplete(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 53 | return timeout_id_ != MessageLoop::kTaskIdNull; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 54 | } |
| 55 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 56 | CHECK_LT(sent_size_, data_.size()); |
| 57 | if (!skip_delivery) { |
| 58 | const size_t chunk_size = min(kMockHttpFetcherChunkSize, |
| 59 | data_.size() - sent_size_); |
| 60 | CHECK(delegate_); |
| 61 | delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 62 | // We may get terminated in the callback. |
| 63 | if (sent_size_ == data_.size()) { |
| 64 | LOG(INFO) << "Terminated in the ReceivedBytes callback."; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 65 | return timeout_id_ != MessageLoop::kTaskIdNull; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 66 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 67 | sent_size_ += chunk_size; |
| 68 | CHECK_LE(sent_size_, data_.size()); |
| 69 | if (sent_size_ == data_.size()) { |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 70 | // We've sent all the data. Notify of success. |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 71 | SignalTransferComplete(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | if (paused_) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 76 | // 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] | 77 | // since we need the caller to delete it. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 78 | return timeout_id_ != MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 81 | if (timeout_id_ != MessageLoop::kTaskIdNull) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 82 | // we still need a timeout if there's more data to send |
| 83 | return sent_size_ < data_.size(); |
| 84 | } else if (sent_size_ < data_.size()) { |
| 85 | // we don't have a timeout source and we need one |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 86 | timeout_id_ = MessageLoop::current()->PostDelayedTask( |
| 87 | FROM_HERE, |
| 88 | base::Bind(&MockHttpFetcher::TimeoutCallback, base::Unretained(this)), |
| 89 | base::TimeDelta::FromMilliseconds(10)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 94 | void MockHttpFetcher::TimeoutCallback() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 | CHECK(!paused_); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 96 | if (SendData(false)) { |
| 97 | // We need to re-schedule the timeout. |
| 98 | timeout_id_ = MessageLoop::current()->PostDelayedTask( |
| 99 | FROM_HERE, |
| 100 | base::Bind(&MockHttpFetcher::TimeoutCallback, base::Unretained(this)), |
| 101 | base::TimeDelta::FromMilliseconds(10)); |
| 102 | } else { |
| 103 | timeout_id_ = MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 104 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // If the transfer is in progress, aborts the transfer early. |
| 108 | // The transfer cannot be resumed. |
| 109 | void MockHttpFetcher::TerminateTransfer() { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 110 | LOG(INFO) << "Terminating transfer."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 | sent_size_ = data_.size(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 112 | // Kill any timeout, it is ok to call with kTaskIdNull. |
| 113 | MessageLoop::current()->CancelTask(timeout_id_); |
| 114 | timeout_id_ = MessageLoop::kTaskIdNull; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 115 | delegate_->TransferTerminated(this); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void MockHttpFetcher::Pause() { |
| 119 | CHECK(!paused_); |
| 120 | paused_ = true; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 121 | MessageLoop::current()->CancelTask(timeout_id_); |
| 122 | timeout_id_ = MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void MockHttpFetcher::Unpause() { |
| 126 | CHECK(paused_) << "You must pause before unpause."; |
| 127 | paused_ = false; |
| 128 | if (sent_size_ < data_.size()) { |
| 129 | SendData(false); |
| 130 | } |
| 131 | } |
| 132 | |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 133 | void MockHttpFetcher::FailTransfer(int http_response_code) { |
| 134 | fail_transfer_ = true; |
| 135 | http_response_code_ = http_response_code; |
| 136 | } |
| 137 | |
| 138 | void MockHttpFetcher::SignalTransferComplete() { |
| 139 | // If the transfer has been failed, the HTTP response code should be set |
| 140 | // already. |
| 141 | if (!fail_transfer_) { |
| 142 | http_response_code_ = 200; |
| 143 | } |
| 144 | delegate_->TransferComplete(this, !fail_transfer_); |
| 145 | } |
| 146 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 147 | } // namespace chromeos_update_engine |