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