blob: 748acf10ac4c5465ad2384507ca528b83da79e40 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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 Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_MOCK_HTTP_FETCHER_H_
6#define UPDATE_ENGINE_MOCK_HTTP_FETCHER_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
Alex Vakulenkod2779df2014-06-16 13:19:00 -07008#include <string>
rspangler@google.com49fdf182009-10-10 00:57:34 +00009#include <vector>
Andrew de los Reyes45168102010-11-22 11:13:50 -080010
11#include <base/logging.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000012#include <glib.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080013
Gilad Arnold5bb4c902014-04-10 12:32:13 -070014#include "update_engine/fake_system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include "update_engine/http_fetcher.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070016#include "update_engine/mock_connection_manager.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000017
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
23namespace 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.
28const size_t kMockHttpFetcherChunkSize(65536);
29
30class MockHttpFetcher : public HttpFetcher {
31 public:
32 // The data passed in here is copied and then passed to the delegate after
33 // the transfer begins.
Andrew de los Reyes45168102010-11-22 11:13:50 -080034 MockHttpFetcher(const char* data,
35 size_t size,
36 ProxyResolver* proxy_resolver)
Gilad Arnold5bb4c902014-04-10 12:32:13 -070037 : HttpFetcher(proxy_resolver, &fake_system_state_),
Andrew de los Reyes45168102010-11-22 11:13:50 -080038 sent_size_(0),
Darin Petkovedc522e2010-11-05 09:35:17 -070039 timeout_source_(NULL),
40 timout_tag_(0),
41 paused_(false),
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070042 fail_transfer_(false),
Jay Srinivasan43488792012-06-19 00:25:31 -070043 never_use_(false),
Gilad Arnold5bb4c902014-04-10 12:32:13 -070044 mock_connection_manager_(&fake_system_state_) {
45 fake_system_state_.set_connection_manager(&mock_connection_manager_);
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 data_.insert(data_.end(), data, data + size);
rspangler@google.com49fdf182009-10-10 00:57:34 +000047 }
48
49 // Cleans up all internal state. Does not notify delegate
50 ~MockHttpFetcher();
51
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070052 // Ignores this.
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070053 virtual void SetOffset(off_t offset) {
54 sent_size_ = offset;
55 if (delegate_)
56 delegate_->SeekToOffset(offset);
57 }
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070058
Gilad Arnolde4ad2502011-12-29 17:08:54 -080059 // Do nothing.
60 virtual void SetLength(size_t length) {}
61 virtual void UnsetLength() {}
David Zeuthen34135a92013-08-06 11:16:16 -070062 virtual void set_low_speed_limit(int low_speed_bps, int low_speed_sec) {}
63 virtual void set_connect_timeout(int connect_timeout_seconds) {}
64 virtual void set_max_retry_count(int max_retry_count) {}
Gilad Arnolde4ad2502011-12-29 17:08:54 -080065
Gilad Arnold48085ba2011-11-16 09:36:08 -080066 // Dummy: no bytes were downloaded.
67 virtual size_t GetBytesDownloaded() {
68 return sent_size_;
69 }
70
rspangler@google.com49fdf182009-10-10 00:57:34 +000071 // Begins the transfer if it hasn't already begun.
72 virtual void BeginTransfer(const std::string& url);
73
74 // If the transfer is in progress, aborts the transfer early.
75 // The transfer cannot be resumed.
76 virtual void TerminateTransfer();
77
78 // Suspend the mock transfer.
79 virtual void Pause();
80
81 // Resume the mock transfer.
82 virtual void Unpause();
83
84 // Fail the transfer. This simulates a network failure.
Darin Petkovedc522e2010-11-05 09:35:17 -070085 void FailTransfer(int http_response_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000086
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070087 // If set to true, this will EXPECT fail on BeginTransfer
88 void set_never_use(bool never_use) { never_use_ = never_use; }
89
rspangler@google.com49fdf182009-10-10 00:57:34 +000090 const std::vector<char>& post_data() const {
91 return post_data_;
92 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000093
rspangler@google.com49fdf182009-10-10 00:57:34 +000094 private:
95 // Sends data to the delegate and sets up a glib timeout callback if needed.
96 // There must be a delegate and there must be data to send. If there is
97 // already a timeout callback, and it should be deleted by the caller,
98 // this will return false; otherwise true is returned.
99 // If skip_delivery is true, no bytes will be delivered, but the callbacks
Alex Vakulenko072359c2014-07-18 11:41:07 -0700100 // still be set if needed.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 bool SendData(bool skip_delivery);
102
103 // Callback for when our glib main loop callback is called
104 bool TimeoutCallback();
105 static gboolean StaticTimeoutCallback(gpointer data) {
106 return reinterpret_cast<MockHttpFetcher*>(data)->TimeoutCallback();
107 }
108
Darin Petkovedc522e2010-11-05 09:35:17 -0700109 // Sets the HTTP response code and signals to the delegate that the transfer
110 // is complete.
111 void SignalTransferComplete();
112
rspangler@google.com49fdf182009-10-10 00:57:34 +0000113 // A full copy of the data we'll return to the delegate
114 std::vector<char> data_;
115
116 // The number of bytes we've sent so far
117 size_t sent_size_;
118
119 // The glib main loop timeout source. After each chunk of data sent, we
120 // time out for 0s just to make sure that run loop services other clients.
121 GSource* timeout_source_;
122
123 // ID of the timeout source, valid only if timeout_source_ != NULL
124 guint timout_tag_;
125
126 // True iff the fetcher is paused.
127 bool paused_;
128
Darin Petkovedc522e2010-11-05 09:35:17 -0700129 // Set to true if the transfer should fail.
130 bool fail_transfer_;
131
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700132 // Set to true if BeginTransfer should EXPECT fail.
133 bool never_use_;
134
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700135 FakeSystemState fake_system_state_;
Jay Srinivasan43488792012-06-19 00:25:31 -0700136 MockConnectionManager mock_connection_manager_;
137
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138 DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
139};
140
141} // namespace chromeos_update_engine
142
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700143#endif // UPDATE_ENGINE_MOCK_HTTP_FETCHER_H_