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