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