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