blob: 0d4b130cdddcc84a34cc4044e140ed65e36856db [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.
Kelvin Zhang7e5b4052022-03-22 12:17:59 -070039static constexpr size_t kMockHttpFetcherChunkSize(65536);
rspangler@google.com49fdf182009-10-10 00:57:34 +000040
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.
Kelvin Zhangc7a1d1f2022-07-29 13:36:29 -070045 MockHttpFetcher(const uint8_t* data, size_t size)
46 : HttpFetcher(),
Kelvin Zhang9dd93052020-07-21 17:31:19 -040047 sent_offset_(0),
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070048 timeout_id_(brillo::MessageLoop::kTaskIdNull),
Darin Petkovedc522e2010-11-05 09:35:17 -070049 paused_(false),
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070050 fail_transfer_(false),
Alex Deymof6ee0162015-07-31 12:35:22 -070051 never_use_(false) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000052 data_.insert(data_.end(), data, data + size);
rspangler@google.com49fdf182009-10-10 00:57:34 +000053 }
54
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080055 // Constructor overload for string data.
Kelvin Zhangc7a1d1f2022-07-29 13:36:29 -070056 MockHttpFetcher(const char* data, size_t size)
57 : MockHttpFetcher(reinterpret_cast<const uint8_t*>(data), size) {}
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080058
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 // Cleans up all internal state. Does not notify delegate
Alex Deymo610277e2014-11-11 21:18:11 -080060 ~MockHttpFetcher() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000061
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070062 // Ignores this.
Alex Deymo610277e2014-11-11 21:18:11 -080063 void SetOffset(off_t offset) override {
Kelvin Zhang9dd93052020-07-21 17:31:19 -040064 sent_offset_ = offset;
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070065 if (delegate_)
66 delegate_->SeekToOffset(offset);
67 }
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070068
Gilad Arnolde4ad2502011-12-29 17:08:54 -080069 // Do nothing.
Alex Deymo610277e2014-11-11 21:18:11 -080070 void SetLength(size_t length) override {}
71 void UnsetLength() override {}
72 void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {}
73 void set_connect_timeout(int connect_timeout_seconds) override {}
74 void set_max_retry_count(int max_retry_count) override {}
Gilad Arnolde4ad2502011-12-29 17:08:54 -080075
Tianjiee283ce42020-07-29 11:37:51 -070076 // No bytes were downloaded in the mock class.
Kelvin Zhang9dd93052020-07-21 17:31:19 -040077 size_t GetBytesDownloaded() override { return bytes_sent_; }
Gilad Arnold48085ba2011-11-16 09:36:08 -080078
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 // Begins the transfer if it hasn't already begun.
Alex Deymo610277e2014-11-11 21:18:11 -080080 void BeginTransfer(const std::string& url) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000081
82 // If the transfer is in progress, aborts the transfer early.
83 // The transfer cannot be resumed.
Alex Deymo610277e2014-11-11 21:18:11 -080084 void TerminateTransfer() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000085
Alex Deymofdd6dec2016-03-03 22:35:43 -080086 void SetHeader(const std::string& header_name,
87 const std::string& header_value) override;
88
Jae Hoon Kim0ae8fe12019-06-26 14:32:50 -070089 bool GetHeader(const std::string& header_name,
90 std::string* header_value) const override {
91 header_value->clear();
92 return false;
93 }
94
Alex Deymo14ad88e2016-06-29 12:30:14 -070095 // Return the value of the header |header_name| or the empty string if not
96 // set.
97 std::string GetHeader(const std::string& header_name) const;
98
rspangler@google.com49fdf182009-10-10 00:57:34 +000099 // Suspend the mock transfer.
Alex Deymo610277e2014-11-11 21:18:11 -0800100 void Pause() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101
102 // Resume the mock transfer.
Alex Deymo610277e2014-11-11 21:18:11 -0800103 void Unpause() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104
105 // Fail the transfer. This simulates a network failure.
Darin Petkovedc522e2010-11-05 09:35:17 -0700106 void FailTransfer(int http_response_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700108 // If set to true, this will EXPECT fail on BeginTransfer
109 void set_never_use(bool never_use) { never_use_ = never_use; }
110
Amin Hassanib2689592019-01-13 17:04:28 -0800111 const brillo::Blob& post_data() const { return post_data_; }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000112
Kelvin Zhang9dd93052020-07-21 17:31:19 -0400113 void set_delay(bool delay) { delay_ = delay; }
114
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115 private:
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700116 // Sends data to the delegate and sets up a timeout callback if needed. There
117 // must be a delegate. If |skip_delivery| is true, no bytes will be delivered,
118 // but the callbacks still be set if needed.
119 void SendData(bool skip_delivery);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120
Alex Deymo60ca1a72015-06-18 18:19:15 -0700121 // Callback for when our message loop timeout expires.
122 void TimeoutCallback();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000123
Darin Petkovedc522e2010-11-05 09:35:17 -0700124 // Sets the HTTP response code and signals to the delegate that the transfer
125 // is complete.
126 void SignalTransferComplete();
127
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128 // A full copy of the data we'll return to the delegate
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700129 brillo::Blob data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130
Kelvin Zhang9dd93052020-07-21 17:31:19 -0400131 // The current offset, marks the first byte that will be sent next
Kelvin Zhangf693d8d2020-08-04 10:17:50 -0400132 size_t sent_offset_{0};
Kelvin Zhang9dd93052020-07-21 17:31:19 -0400133
134 // Total number of bytes transferred
Kelvin Zhangf693d8d2020-08-04 10:17:50 -0400135 size_t bytes_sent_{0};
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136
Alex Deymofdd6dec2016-03-03 22:35:43 -0800137 // The extra headers set.
138 std::map<std::string, std::string> extra_headers_;
139
Alex Deymo60ca1a72015-06-18 18:19:15 -0700140 // The TaskId of the timeout callback. After each chunk of data sent, we
rspangler@google.com49fdf182009-10-10 00:57:34 +0000141 // time out for 0s just to make sure that run loop services other clients.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700142 brillo::MessageLoop::TaskId timeout_id_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143
144 // True iff the fetcher is paused.
Kelvin Zhangf693d8d2020-08-04 10:17:50 -0400145 bool paused_{false};
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146
Darin Petkovedc522e2010-11-05 09:35:17 -0700147 // Set to true if the transfer should fail.
Kelvin Zhangf693d8d2020-08-04 10:17:50 -0400148 bool fail_transfer_{false};
Darin Petkovedc522e2010-11-05 09:35:17 -0700149
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700150 // Set to true if BeginTransfer should EXPECT fail.
Kelvin Zhangf693d8d2020-08-04 10:17:50 -0400151 bool never_use_{false};
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700152
Kelvin Zhang9dd93052020-07-21 17:31:19 -0400153 // Whether it should wait for 10ms before sending data to delegates
154 bool delay_{true};
155
rspangler@google.com49fdf182009-10-10 00:57:34 +0000156 DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
157};
158
159} // namespace chromeos_update_engine
160
Alex Deymo39910dc2015-11-09 17:04:30 -0800161#endif // UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_