blob: f1ae72aba53ecd370ffc9db42ad56a6eb26c29ba [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/mock_http_fetcher.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000018
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070019#include <algorithm>
20
Alex Deymoc1c17b42015-11-23 03:53:15 -030021#include <base/bind.h>
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070022#include <base/logging.h>
Alex Deymofdd6dec2016-03-03 22:35:43 -080023#include <base/strings/string_util.h>
Alex Deymoc1c17b42015-11-23 03:53:15 -030024#include <base/time/time.h>
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070025#include <gtest/gtest.h>
26
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070027// This is a mock implementation of HttpFetcher which is useful for testing.
rspangler@google.com49fdf182009-10-10 00:57:34 +000028
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070029using brillo::MessageLoop;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000030using std::min;
31
rspangler@google.com49fdf182009-10-10 00:57:34 +000032namespace chromeos_update_engine {
33
34MockHttpFetcher::~MockHttpFetcher() {
Alex Deymo60ca1a72015-06-18 18:19:15 -070035 CHECK(timeout_id_ == MessageLoop::kTaskIdNull) <<
36 "Call TerminateTransfer() before dtor.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000037}
38
39void MockHttpFetcher::BeginTransfer(const std::string& url) {
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070040 EXPECT_FALSE(never_use_);
Darin Petkovedc522e2010-11-05 09:35:17 -070041 if (fail_transfer_ || data_.empty()) {
42 // No data to send, just notify of completion..
43 SignalTransferComplete();
44 return;
45 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 if (sent_size_ < data_.size())
47 SendData(true);
48}
49
Alex Deymo60ca1a72015-06-18 18:19:15 -070050// Returns false on one condition: If timeout_id_ was already set
51// and it needs to be deleted by the caller. If timeout_id_ is null
rspangler@google.com49fdf182009-10-10 00:57:34 +000052// when this function is called, this function will always return true.
53bool MockHttpFetcher::SendData(bool skip_delivery) {
Darin Petkovedc522e2010-11-05 09:35:17 -070054 if (fail_transfer_) {
55 SignalTransferComplete();
Alex Deymo60ca1a72015-06-18 18:19:15 -070056 return timeout_id_ != MessageLoop::kTaskIdNull;
Darin Petkovedc522e2010-11-05 09:35:17 -070057 }
58
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 CHECK_LT(sent_size_, data_.size());
60 if (!skip_delivery) {
61 const size_t chunk_size = min(kMockHttpFetcherChunkSize,
62 data_.size() - sent_size_);
63 CHECK(delegate_);
64 delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size);
Darin Petkov9ce452b2010-11-17 14:33:28 -080065 // We may get terminated in the callback.
66 if (sent_size_ == data_.size()) {
67 LOG(INFO) << "Terminated in the ReceivedBytes callback.";
Alex Deymo60ca1a72015-06-18 18:19:15 -070068 return timeout_id_ != MessageLoop::kTaskIdNull;
Darin Petkov9ce452b2010-11-17 14:33:28 -080069 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 sent_size_ += chunk_size;
71 CHECK_LE(sent_size_, data_.size());
72 if (sent_size_ == data_.size()) {
Darin Petkovcb466212010-08-26 09:40:11 -070073 // We've sent all the data. Notify of success.
Darin Petkovedc522e2010-11-05 09:35:17 -070074 SignalTransferComplete();
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 }
76 }
77
78 if (paused_) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070079 // If we're paused, we should return true if timeout_id_ is set,
rspangler@google.com49fdf182009-10-10 00:57:34 +000080 // since we need the caller to delete it.
Alex Deymo60ca1a72015-06-18 18:19:15 -070081 return timeout_id_ != MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 }
83
Alex Deymo60ca1a72015-06-18 18:19:15 -070084 if (timeout_id_ != MessageLoop::kTaskIdNull) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000085 // we still need a timeout if there's more data to send
86 return sent_size_ < data_.size();
87 } else if (sent_size_ < data_.size()) {
88 // we don't have a timeout source and we need one
Alex Deymo60ca1a72015-06-18 18:19:15 -070089 timeout_id_ = MessageLoop::current()->PostDelayedTask(
90 FROM_HERE,
91 base::Bind(&MockHttpFetcher::TimeoutCallback, base::Unretained(this)),
92 base::TimeDelta::FromMilliseconds(10));
rspangler@google.com49fdf182009-10-10 00:57:34 +000093 }
94 return true;
95}
96
Alex Deymo60ca1a72015-06-18 18:19:15 -070097void MockHttpFetcher::TimeoutCallback() {
rspangler@google.com49fdf182009-10-10 00:57:34 +000098 CHECK(!paused_);
Alex Deymo60ca1a72015-06-18 18:19:15 -070099 if (SendData(false)) {
100 // We need to re-schedule the timeout.
101 timeout_id_ = MessageLoop::current()->PostDelayedTask(
102 FROM_HERE,
103 base::Bind(&MockHttpFetcher::TimeoutCallback, base::Unretained(this)),
104 base::TimeDelta::FromMilliseconds(10));
105 } else {
106 timeout_id_ = MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000108}
109
110// If the transfer is in progress, aborts the transfer early.
111// The transfer cannot be resumed.
112void MockHttpFetcher::TerminateTransfer() {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800113 LOG(INFO) << "Terminating transfer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000114 sent_size_ = data_.size();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700115 // Kill any timeout, it is ok to call with kTaskIdNull.
116 MessageLoop::current()->CancelTask(timeout_id_);
117 timeout_id_ = MessageLoop::kTaskIdNull;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800118 delegate_->TransferTerminated(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119}
120
Alex Deymofdd6dec2016-03-03 22:35:43 -0800121void MockHttpFetcher::SetHeader(const std::string& header_name,
122 const std::string& header_value) {
123 extra_headers_[base::ToLowerASCII(header_name)] = header_value;
124}
125
Alex Deymo14ad88e2016-06-29 12:30:14 -0700126std::string MockHttpFetcher::GetHeader(const std::string& header_name) const {
127 const auto it = extra_headers_.find(base::ToLowerASCII(header_name));
128 if (it == extra_headers_.end())
129 return "";
130 return it->second;
131}
132
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133void MockHttpFetcher::Pause() {
134 CHECK(!paused_);
135 paused_ = true;
Alex Deymo60ca1a72015-06-18 18:19:15 -0700136 MessageLoop::current()->CancelTask(timeout_id_);
137 timeout_id_ = MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138}
139
140void MockHttpFetcher::Unpause() {
141 CHECK(paused_) << "You must pause before unpause.";
142 paused_ = false;
143 if (sent_size_ < data_.size()) {
144 SendData(false);
145 }
146}
147
Darin Petkovedc522e2010-11-05 09:35:17 -0700148void MockHttpFetcher::FailTransfer(int http_response_code) {
149 fail_transfer_ = true;
150 http_response_code_ = http_response_code;
151}
152
153void MockHttpFetcher::SignalTransferComplete() {
154 // If the transfer has been failed, the HTTP response code should be set
155 // already.
156 if (!fail_transfer_) {
157 http_response_code_ = 200;
158 }
159 delegate_->TransferComplete(this, !fail_transfer_);
160}
161
rspangler@google.com49fdf182009-10-10 00:57:34 +0000162} // namespace chromeos_update_engine