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 | #ifndef UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_ |
| 18 | #define UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 20 | #include <map> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 21 | #include <string> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | #include <vector> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 23 | |
| 24 | #include <base/logging.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 25 | #include <brillo/message_loops/message_loop.h> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 26 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/http_fetcher.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | |
| 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 | |
| 34 | namespace 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. |
| 39 | const size_t kMockHttpFetcherChunkSize(65536); |
| 40 | |
| 41 | class 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 Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 45 | MockHttpFetcher(const uint8_t* data, |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 46 | size_t size, |
| 47 | ProxyResolver* proxy_resolver) |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 48 | : HttpFetcher(proxy_resolver), |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 49 | sent_offset_(0), |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 50 | timeout_id_(brillo::MessageLoop::kTaskIdNull), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 51 | paused_(false), |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 52 | fail_transfer_(false), |
Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 53 | never_use_(false) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | data_.insert(data_.end(), data, data + size); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 57 | // Constructor overload for string data. |
| 58 | MockHttpFetcher(const char* data, size_t size, ProxyResolver* proxy_resolver) |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 59 | : MockHttpFetcher( |
| 60 | reinterpret_cast<const uint8_t*>(data), size, proxy_resolver) {} |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 61 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 62 | // Cleans up all internal state. Does not notify delegate |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 63 | ~MockHttpFetcher() override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 65 | // Ignores this. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 66 | void SetOffset(off_t offset) override { |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 67 | sent_offset_ = offset; |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 68 | if (delegate_) |
| 69 | delegate_->SeekToOffset(offset); |
| 70 | } |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 71 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 72 | // Do nothing. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 73 | 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 Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 78 | |
Tianjie | e283ce4 | 2020-07-29 11:37:51 -0700 | [diff] [blame] | 79 | // No bytes were downloaded in the mock class. |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 80 | size_t GetBytesDownloaded() override { return bytes_sent_; } |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 81 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 82 | // Begins the transfer if it hasn't already begun. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 83 | void BeginTransfer(const std::string& url) override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | |
| 85 | // If the transfer is in progress, aborts the transfer early. |
| 86 | // The transfer cannot be resumed. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 87 | void TerminateTransfer() override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 89 | void SetHeader(const std::string& header_name, |
| 90 | const std::string& header_value) override; |
| 91 | |
Jae Hoon Kim | 0ae8fe1 | 2019-06-26 14:32:50 -0700 | [diff] [blame] | 92 | bool GetHeader(const std::string& header_name, |
| 93 | std::string* header_value) const override { |
| 94 | header_value->clear(); |
| 95 | return false; |
| 96 | } |
| 97 | |
Alex Deymo | 14ad88e | 2016-06-29 12:30:14 -0700 | [diff] [blame] | 98 | // Return the value of the header |header_name| or the empty string if not |
| 99 | // set. |
| 100 | std::string GetHeader(const std::string& header_name) const; |
| 101 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 102 | // Suspend the mock transfer. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 103 | void Pause() override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 104 | |
| 105 | // Resume the mock transfer. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 106 | void Unpause() override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 107 | |
| 108 | // Fail the transfer. This simulates a network failure. |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 109 | void FailTransfer(int http_response_code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 111 | // If set to true, this will EXPECT fail on BeginTransfer |
| 112 | void set_never_use(bool never_use) { never_use_ = never_use; } |
| 113 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 114 | const brillo::Blob& post_data() const { return post_data_; } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 115 | |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 116 | void set_delay(bool delay) { delay_ = delay; } |
| 117 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 118 | private: |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 119 | // Sends data to the delegate and sets up a timeout callback if needed. There |
| 120 | // must be a delegate. If |skip_delivery| is true, no bytes will be delivered, |
| 121 | // but the callbacks still be set if needed. |
| 122 | void SendData(bool skip_delivery); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 123 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 124 | // Callback for when our message loop timeout expires. |
| 125 | void TimeoutCallback(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 126 | |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 127 | // Sets the HTTP response code and signals to the delegate that the transfer |
| 128 | // is complete. |
| 129 | void SignalTransferComplete(); |
| 130 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 131 | // A full copy of the data we'll return to the delegate |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 132 | brillo::Blob data_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 133 | |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 134 | // The current offset, marks the first byte that will be sent next |
Kelvin Zhang | f693d8d | 2020-08-04 10:17:50 -0400 | [diff] [blame] | 135 | size_t sent_offset_{0}; |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 136 | |
| 137 | // Total number of bytes transferred |
Kelvin Zhang | f693d8d | 2020-08-04 10:17:50 -0400 | [diff] [blame] | 138 | size_t bytes_sent_{0}; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 139 | |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 140 | // The extra headers set. |
| 141 | std::map<std::string, std::string> extra_headers_; |
| 142 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 143 | // The TaskId of the timeout callback. After each chunk of data sent, we |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 144 | // time out for 0s just to make sure that run loop services other clients. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 145 | brillo::MessageLoop::TaskId timeout_id_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 146 | |
| 147 | // True iff the fetcher is paused. |
Kelvin Zhang | f693d8d | 2020-08-04 10:17:50 -0400 | [diff] [blame] | 148 | bool paused_{false}; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 149 | |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 150 | // Set to true if the transfer should fail. |
Kelvin Zhang | f693d8d | 2020-08-04 10:17:50 -0400 | [diff] [blame] | 151 | bool fail_transfer_{false}; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 152 | |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 153 | // Set to true if BeginTransfer should EXPECT fail. |
Kelvin Zhang | f693d8d | 2020-08-04 10:17:50 -0400 | [diff] [blame] | 154 | bool never_use_{false}; |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 155 | |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 156 | // Whether it should wait for 10ms before sending data to delegates |
| 157 | bool delay_{true}; |
| 158 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 159 | DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher); |
| 160 | }; |
| 161 | |
| 162 | } // namespace chromeos_update_engine |
| 163 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 164 | #endif // UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_ |