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