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 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_MOCK_HTTP_FETCHER_H_ |
| 18 | #define UPDATE_ENGINE_MOCK_HTTP_FETCHER_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 20 | #include <string> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 21 | #include <vector> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 22 | |
| 23 | #include <base/logging.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 24 | #include <chromeos/message_loops/message_loop.h> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 25 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 26 | #include "update_engine/fake_system_state.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | #include "update_engine/http_fetcher.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 28 | #include "update_engine/mock_connection_manager.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 29 | |
| 30 | // This is a mock implementation of HttpFetcher which is useful for testing. |
| 31 | // All data must be passed into the ctor. When started, MockHttpFetcher will |
| 32 | // deliver the data in chunks of size kMockHttpFetcherChunkSize. To simulate |
| 33 | // a network failure, you can call FailTransfer(). |
| 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
| 37 | // MockHttpFetcher will send a chunk of data down in each call to BeginTransfer |
| 38 | // and Unpause. For the other chunks of data, a callback is put on the run |
| 39 | // loop and when that's called, another chunk is sent down. |
| 40 | const size_t kMockHttpFetcherChunkSize(65536); |
| 41 | |
| 42 | class MockHttpFetcher : public HttpFetcher { |
| 43 | public: |
| 44 | // The data passed in here is copied and then passed to the delegate after |
| 45 | // the transfer begins. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 46 | MockHttpFetcher(const uint8_t* data, |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 47 | size_t size, |
| 48 | ProxyResolver* proxy_resolver) |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 49 | : HttpFetcher(proxy_resolver, &fake_system_state_), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 50 | sent_size_(0), |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 51 | timeout_id_(chromeos::MessageLoop::kTaskIdNull), |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 52 | paused_(false), |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 53 | fail_transfer_(false), |
Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 54 | never_use_(false) { |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 55 | fake_system_state_.set_connection_manager(&mock_connection_manager_); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 56 | data_.insert(data_.end(), data, data + size); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 59 | // Constructor overload for string data. |
| 60 | MockHttpFetcher(const char* data, size_t size, ProxyResolver* proxy_resolver) |
| 61 | : MockHttpFetcher(reinterpret_cast<const uint8_t*>(data), size, |
| 62 | proxy_resolver) {} |
| 63 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | // Cleans up all internal state. Does not notify delegate |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 65 | ~MockHttpFetcher() override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 66 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 67 | // Ignores this. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 68 | void SetOffset(off_t offset) override { |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 69 | sent_size_ = offset; |
| 70 | if (delegate_) |
| 71 | delegate_->SeekToOffset(offset); |
| 72 | } |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 73 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 74 | // Do nothing. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 75 | void SetLength(size_t length) override {} |
| 76 | void UnsetLength() override {} |
| 77 | void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {} |
| 78 | void set_connect_timeout(int connect_timeout_seconds) override {} |
| 79 | void set_max_retry_count(int max_retry_count) override {} |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 80 | |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 81 | // Dummy: no bytes were downloaded. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 82 | size_t GetBytesDownloaded() override { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 83 | return sent_size_; |
| 84 | } |
| 85 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 86 | // Begins the transfer if it hasn't already begun. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 87 | void BeginTransfer(const std::string& url) override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | |
| 89 | // If the transfer is in progress, aborts the transfer early. |
| 90 | // The transfer cannot be resumed. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 91 | void TerminateTransfer() override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 92 | |
| 93 | // Suspend the mock transfer. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 94 | void Pause() override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 | |
| 96 | // Resume the mock transfer. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 97 | void Unpause() override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 98 | |
| 99 | // Fail the transfer. This simulates a network failure. |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 100 | void FailTransfer(int http_response_code); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 101 | |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 102 | // If set to true, this will EXPECT fail on BeginTransfer |
| 103 | void set_never_use(bool never_use) { never_use_ = never_use; } |
| 104 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 105 | const chromeos::Blob& post_data() const { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 106 | return post_data_; |
| 107 | } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 108 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 109 | private: |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 110 | // Sends data to the delegate and sets up a timeout callback if needed. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 | // There must be a delegate and there must be data to send. If there is |
| 112 | // already a timeout callback, and it should be deleted by the caller, |
| 113 | // this will return false; otherwise true is returned. |
| 114 | // If skip_delivery is true, no bytes will be delivered, but the callbacks |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 115 | // still be set if needed. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 116 | bool SendData(bool skip_delivery); |
| 117 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 118 | // Callback for when our message loop timeout expires. |
| 119 | void TimeoutCallback(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 120 | |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 121 | // Sets the HTTP response code and signals to the delegate that the transfer |
| 122 | // is complete. |
| 123 | void SignalTransferComplete(); |
| 124 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 125 | // A full copy of the data we'll return to the delegate |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 126 | chromeos::Blob data_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 127 | |
| 128 | // The number of bytes we've sent so far |
| 129 | size_t sent_size_; |
| 130 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 131 | // 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] | 132 | // time out for 0s just to make sure that run loop services other clients. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 133 | chromeos::MessageLoop::TaskId timeout_id_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 134 | |
| 135 | // True iff the fetcher is paused. |
| 136 | bool paused_; |
| 137 | |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 138 | // Set to true if the transfer should fail. |
| 139 | bool fail_transfer_; |
| 140 | |
Andrew de los Reyes | 173e63c | 2011-04-04 17:19:57 -0700 | [diff] [blame] | 141 | // Set to true if BeginTransfer should EXPECT fail. |
| 142 | bool never_use_; |
| 143 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 144 | FakeSystemState fake_system_state_; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 145 | MockConnectionManager mock_connection_manager_; |
| 146 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 147 | DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher); |
| 148 | }; |
| 149 | |
| 150 | } // namespace chromeos_update_engine |
| 151 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 152 | #endif // UPDATE_ENGINE_MOCK_HTTP_FETCHER_H_ |