blob: f3fa70dbc97386fe10135761cde5adf12497c1d6 [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 Deymoc1c17b42015-11-23 03:53:15 -030023#include <base/time/time.h>
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070024#include <gtest/gtest.h>
25
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070026// This is a mock implementation of HttpFetcher which is useful for testing.
rspangler@google.com49fdf182009-10-10 00:57:34 +000027
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070028using brillo::MessageLoop;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000029using std::min;
30
rspangler@google.com49fdf182009-10-10 00:57:34 +000031namespace chromeos_update_engine {
32
33MockHttpFetcher::~MockHttpFetcher() {
Alex Deymo60ca1a72015-06-18 18:19:15 -070034 CHECK(timeout_id_ == MessageLoop::kTaskIdNull) <<
35 "Call TerminateTransfer() before dtor.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000036}
37
38void MockHttpFetcher::BeginTransfer(const std::string& url) {
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070039 EXPECT_FALSE(never_use_);
Darin Petkovedc522e2010-11-05 09:35:17 -070040 if (fail_transfer_ || data_.empty()) {
41 // No data to send, just notify of completion..
42 SignalTransferComplete();
43 return;
44 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000045 if (sent_size_ < data_.size())
46 SendData(true);
47}
48
Alex Deymo60ca1a72015-06-18 18:19:15 -070049// 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.com49fdf182009-10-10 00:57:34 +000051// when this function is called, this function will always return true.
52bool MockHttpFetcher::SendData(bool skip_delivery) {
Darin Petkovedc522e2010-11-05 09:35:17 -070053 if (fail_transfer_) {
54 SignalTransferComplete();
Alex Deymo60ca1a72015-06-18 18:19:15 -070055 return timeout_id_ != MessageLoop::kTaskIdNull;
Darin Petkovedc522e2010-11-05 09:35:17 -070056 }
57
rspangler@google.com49fdf182009-10-10 00:57:34 +000058 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 Petkov9ce452b2010-11-17 14:33:28 -080064 // We may get terminated in the callback.
65 if (sent_size_ == data_.size()) {
66 LOG(INFO) << "Terminated in the ReceivedBytes callback.";
Alex Deymo60ca1a72015-06-18 18:19:15 -070067 return timeout_id_ != MessageLoop::kTaskIdNull;
Darin Petkov9ce452b2010-11-17 14:33:28 -080068 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000069 sent_size_ += chunk_size;
70 CHECK_LE(sent_size_, data_.size());
71 if (sent_size_ == data_.size()) {
Darin Petkovcb466212010-08-26 09:40:11 -070072 // We've sent all the data. Notify of success.
Darin Petkovedc522e2010-11-05 09:35:17 -070073 SignalTransferComplete();
rspangler@google.com49fdf182009-10-10 00:57:34 +000074 }
75 }
76
77 if (paused_) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070078 // If we're paused, we should return true if timeout_id_ is set,
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 // since we need the caller to delete it.
Alex Deymo60ca1a72015-06-18 18:19:15 -070080 return timeout_id_ != MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +000081 }
82
Alex Deymo60ca1a72015-06-18 18:19:15 -070083 if (timeout_id_ != MessageLoop::kTaskIdNull) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000084 // 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 Deymo60ca1a72015-06-18 18:19:15 -070088 timeout_id_ = MessageLoop::current()->PostDelayedTask(
89 FROM_HERE,
90 base::Bind(&MockHttpFetcher::TimeoutCallback, base::Unretained(this)),
91 base::TimeDelta::FromMilliseconds(10));
rspangler@google.com49fdf182009-10-10 00:57:34 +000092 }
93 return true;
94}
95
Alex Deymo60ca1a72015-06-18 18:19:15 -070096void MockHttpFetcher::TimeoutCallback() {
rspangler@google.com49fdf182009-10-10 00:57:34 +000097 CHECK(!paused_);
Alex Deymo60ca1a72015-06-18 18:19:15 -070098 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.com49fdf182009-10-10 00:57:34 +0000106 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107}
108
109// If the transfer is in progress, aborts the transfer early.
110// The transfer cannot be resumed.
111void MockHttpFetcher::TerminateTransfer() {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800112 LOG(INFO) << "Terminating transfer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000113 sent_size_ = data_.size();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700114 // Kill any timeout, it is ok to call with kTaskIdNull.
115 MessageLoop::current()->CancelTask(timeout_id_);
116 timeout_id_ = MessageLoop::kTaskIdNull;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800117 delegate_->TransferTerminated(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000118}
119
120void MockHttpFetcher::Pause() {
121 CHECK(!paused_);
122 paused_ = true;
Alex Deymo60ca1a72015-06-18 18:19:15 -0700123 MessageLoop::current()->CancelTask(timeout_id_);
124 timeout_id_ = MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125}
126
127void 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 Petkovedc522e2010-11-05 09:35:17 -0700135void MockHttpFetcher::FailTransfer(int http_response_code) {
136 fail_transfer_ = true;
137 http_response_code_ = http_response_code;
138}
139
140void 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.com49fdf182009-10-10 00:57:34 +0000149} // namespace chromeos_update_engine