blob: e56318e3ab4505c42c6e41f617e42908ad84c85b [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#ifndef UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_
18#define UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
Alex Deymofdd6dec2016-03-03 22:35:43 -080020#include <map>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070021#include <string>
rspangler@google.com49fdf182009-10-10 00:57:34 +000022#include <vector>
Andrew de los Reyes45168102010-11-22 11:13:50 -080023
24#include <base/logging.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025#include <brillo/message_loops/message_loop.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/http_fetcher.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000028
29// This is a mock implementation of HttpFetcher which is useful for testing.
30// All data must be passed into the ctor. When started, MockHttpFetcher will
31// deliver the data in chunks of size kMockHttpFetcherChunkSize. To simulate
32// a network failure, you can call FailTransfer().
33
34namespace chromeos_update_engine {
35
36// MockHttpFetcher will send a chunk of data down in each call to BeginTransfer
37// and Unpause. For the other chunks of data, a callback is put on the run
38// loop and when that's called, another chunk is sent down.
39const size_t kMockHttpFetcherChunkSize(65536);
40
41class MockHttpFetcher : public HttpFetcher {
42 public:
43 // The data passed in here is copied and then passed to the delegate after
44 // the transfer begins.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080045 MockHttpFetcher(const uint8_t* data,
Andrew de los Reyes45168102010-11-22 11:13:50 -080046 size_t size,
47 ProxyResolver* proxy_resolver)
Alex Deymoc1c17b42015-11-23 03:53:15 -030048 : HttpFetcher(proxy_resolver),
Andrew de los Reyes45168102010-11-22 11:13:50 -080049 sent_size_(0),
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070050 timeout_id_(brillo::MessageLoop::kTaskIdNull),
Darin Petkovedc522e2010-11-05 09:35:17 -070051 paused_(false),
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070052 fail_transfer_(false),
Alex Deymof6ee0162015-07-31 12:35:22 -070053 never_use_(false) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000054 data_.insert(data_.end(), data, data + size);
rspangler@google.com49fdf182009-10-10 00:57:34 +000055 }
56
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080057 // Constructor overload for string data.
58 MockHttpFetcher(const char* data, size_t size, ProxyResolver* proxy_resolver)
59 : MockHttpFetcher(reinterpret_cast<const uint8_t*>(data), size,
60 proxy_resolver) {}
61
rspangler@google.com49fdf182009-10-10 00:57:34 +000062 // Cleans up all internal state. Does not notify delegate
Alex Deymo610277e2014-11-11 21:18:11 -080063 ~MockHttpFetcher() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000064
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070065 // Ignores this.
Alex Deymo610277e2014-11-11 21:18:11 -080066 void SetOffset(off_t offset) override {
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070067 sent_size_ = offset;
68 if (delegate_)
69 delegate_->SeekToOffset(offset);
70 }
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070071
Gilad Arnolde4ad2502011-12-29 17:08:54 -080072 // Do nothing.
Alex Deymo610277e2014-11-11 21:18:11 -080073 void SetLength(size_t length) override {}
74 void UnsetLength() override {}
75 void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {}
76 void set_connect_timeout(int connect_timeout_seconds) override {}
77 void set_max_retry_count(int max_retry_count) override {}
Gilad Arnolde4ad2502011-12-29 17:08:54 -080078
Gilad Arnold48085ba2011-11-16 09:36:08 -080079 // Dummy: no bytes were downloaded.
Alex Deymo610277e2014-11-11 21:18:11 -080080 size_t GetBytesDownloaded() override {
Gilad Arnold48085ba2011-11-16 09:36:08 -080081 return sent_size_;
82 }
83
rspangler@google.com49fdf182009-10-10 00:57:34 +000084 // Begins the transfer if it hasn't already begun.
Alex Deymo610277e2014-11-11 21:18:11 -080085 void BeginTransfer(const std::string& url) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000086
87 // If the transfer is in progress, aborts the transfer early.
88 // The transfer cannot be resumed.
Alex Deymo610277e2014-11-11 21:18:11 -080089 void TerminateTransfer() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000090
Alex Deymofdd6dec2016-03-03 22:35:43 -080091 void SetHeader(const std::string& header_name,
92 const std::string& header_value) override;
93
rspangler@google.com49fdf182009-10-10 00:57:34 +000094 // Suspend the mock transfer.
Alex Deymo610277e2014-11-11 21:18:11 -080095 void Pause() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000096
97 // Resume the mock transfer.
Alex Deymo610277e2014-11-11 21:18:11 -080098 void Unpause() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000099
100 // Fail the transfer. This simulates a network failure.
Darin Petkovedc522e2010-11-05 09:35:17 -0700101 void FailTransfer(int http_response_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700103 // If set to true, this will EXPECT fail on BeginTransfer
104 void set_never_use(bool never_use) { never_use_ = never_use; }
105
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700106 const brillo::Blob& post_data() const {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107 return post_data_;
108 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000109
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110 private:
Alex Deymo60ca1a72015-06-18 18:19:15 -0700111 // Sends data to the delegate and sets up a timeout callback if needed.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000112 // There must be a delegate and there must be data to send. If there is
113 // already a timeout callback, and it should be deleted by the caller,
114 // this will return false; otherwise true is returned.
115 // If skip_delivery is true, no bytes will be delivered, but the callbacks
Alex Vakulenko072359c2014-07-18 11:41:07 -0700116 // still be set if needed.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117 bool SendData(bool skip_delivery);
118
Alex Deymo60ca1a72015-06-18 18:19:15 -0700119 // Callback for when our message loop timeout expires.
120 void TimeoutCallback();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000121
Darin Petkovedc522e2010-11-05 09:35:17 -0700122 // Sets the HTTP response code and signals to the delegate that the transfer
123 // is complete.
124 void SignalTransferComplete();
125
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126 // A full copy of the data we'll return to the delegate
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700127 brillo::Blob data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128
129 // The number of bytes we've sent so far
130 size_t sent_size_;
131
Alex Deymofdd6dec2016-03-03 22:35:43 -0800132 // The extra headers set.
133 std::map<std::string, std::string> extra_headers_;
134
Alex Deymo60ca1a72015-06-18 18:19:15 -0700135 // The TaskId of the timeout callback. After each chunk of data sent, we
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136 // time out for 0s just to make sure that run loop services other clients.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700137 brillo::MessageLoop::TaskId timeout_id_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138
139 // True iff the fetcher is paused.
140 bool paused_;
141
Darin Petkovedc522e2010-11-05 09:35:17 -0700142 // Set to true if the transfer should fail.
143 bool fail_transfer_;
144
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700145 // Set to true if BeginTransfer should EXPECT fail.
146 bool never_use_;
147
rspangler@google.com49fdf182009-10-10 00:57:34 +0000148 DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
149};
150
151} // namespace chromeos_update_engine
152
Alex Deymo39910dc2015-11-09 17:04:30 -0800153#endif // UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_