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