blob: db528c7194365f2e3646b05576fb8b46a0e92bad [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
adlr@google.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_MOCK_HTTP_FETCHER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_MOCK_HTTP_FETCHER_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
8#include <vector>
Andrew de los Reyes45168102010-11-22 11:13:50 -08009
10#include <base/logging.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000011#include <glib.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080012
rspangler@google.com49fdf182009-10-10 00:57:34 +000013#include "update_engine/http_fetcher.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070014#include "update_engine/mock_connection_manager.h"
15#include "update_engine/mock_system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
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
22namespace 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.
27const size_t kMockHttpFetcherChunkSize(65536);
28
29class 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 Reyes45168102010-11-22 11:13:50 -080033 MockHttpFetcher(const char* data,
34 size_t size,
35 ProxyResolver* proxy_resolver)
Jay Srinivasan43488792012-06-19 00:25:31 -070036 : HttpFetcher(proxy_resolver, &mock_system_state_),
Andrew de los Reyes45168102010-11-22 11:13:50 -080037 sent_size_(0),
Darin Petkovedc522e2010-11-05 09:35:17 -070038 timeout_source_(NULL),
39 timout_tag_(0),
40 paused_(false),
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070041 fail_transfer_(false),
Jay Srinivasan43488792012-06-19 00:25:31 -070042 never_use_(false),
43 mock_connection_manager_(&mock_system_state_) {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080044 mock_system_state_.set_connection_manager(&mock_connection_manager_);
rspangler@google.com49fdf182009-10-10 00:57:34 +000045 data_.insert(data_.end(), data, data + size);
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 }
47
48 // Cleans up all internal state. Does not notify delegate
49 ~MockHttpFetcher();
50
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070051 // Ignores this.
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070052 virtual void SetOffset(off_t offset) {
53 sent_size_ = offset;
54 if (delegate_)
55 delegate_->SeekToOffset(offset);
56 }
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070057
Gilad Arnolde4ad2502011-12-29 17:08:54 -080058 // Do nothing.
59 virtual void SetLength(size_t length) {}
60 virtual void UnsetLength() {}
61
Gilad Arnold48085ba2011-11-16 09:36:08 -080062 // Dummy: no bytes were downloaded.
63 virtual size_t GetBytesDownloaded() {
64 return sent_size_;
65 }
66
rspangler@google.com49fdf182009-10-10 00:57:34 +000067 // Begins the transfer if it hasn't already begun.
68 virtual void BeginTransfer(const std::string& url);
69
70 // If the transfer is in progress, aborts the transfer early.
71 // The transfer cannot be resumed.
72 virtual void TerminateTransfer();
73
74 // Suspend the mock transfer.
75 virtual void Pause();
76
77 // Resume the mock transfer.
78 virtual void Unpause();
79
80 // Fail the transfer. This simulates a network failure.
Darin Petkovedc522e2010-11-05 09:35:17 -070081 void FailTransfer(int http_response_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000082
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070083 // If set to true, this will EXPECT fail on BeginTransfer
84 void set_never_use(bool never_use) { never_use_ = never_use; }
85
rspangler@google.com49fdf182009-10-10 00:57:34 +000086 const std::vector<char>& post_data() const {
87 return post_data_;
88 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000089
rspangler@google.com49fdf182009-10-10 00:57:34 +000090 private:
91 // Sends data to the delegate and sets up a glib timeout callback if needed.
92 // There must be a delegate and there must be data to send. If there is
93 // already a timeout callback, and it should be deleted by the caller,
94 // this will return false; otherwise true is returned.
95 // If skip_delivery is true, no bytes will be delivered, but the callbacks
96 // still still be set if needed
97 bool SendData(bool skip_delivery);
98
99 // Callback for when our glib main loop callback is called
100 bool TimeoutCallback();
101 static gboolean StaticTimeoutCallback(gpointer data) {
102 return reinterpret_cast<MockHttpFetcher*>(data)->TimeoutCallback();
103 }
104
Darin Petkovedc522e2010-11-05 09:35:17 -0700105 // Sets the HTTP response code and signals to the delegate that the transfer
106 // is complete.
107 void SignalTransferComplete();
108
rspangler@google.com49fdf182009-10-10 00:57:34 +0000109 // A full copy of the data we'll return to the delegate
110 std::vector<char> data_;
111
112 // The number of bytes we've sent so far
113 size_t sent_size_;
114
115 // The glib main loop timeout source. After each chunk of data sent, we
116 // time out for 0s just to make sure that run loop services other clients.
117 GSource* timeout_source_;
118
119 // ID of the timeout source, valid only if timeout_source_ != NULL
120 guint timout_tag_;
121
122 // True iff the fetcher is paused.
123 bool paused_;
124
Darin Petkovedc522e2010-11-05 09:35:17 -0700125 // Set to true if the transfer should fail.
126 bool fail_transfer_;
127
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700128 // Set to true if BeginTransfer should EXPECT fail.
129 bool never_use_;
130
Jay Srinivasan43488792012-06-19 00:25:31 -0700131 MockSystemState mock_system_state_;
132 MockConnectionManager mock_connection_manager_;
133
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134 DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
135};
136
137} // namespace chromeos_update_engine
138
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000139#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MOCK_HTTP_FETCHER_H__