blob: 0fd953735c7235136a1a88a3d635245504b1159f [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"
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
20namespace 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.
25const size_t kMockHttpFetcherChunkSize(65536);
26
27class 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 Reyes45168102010-11-22 11:13:50 -080031 MockHttpFetcher(const char* data,
32 size_t size,
33 ProxyResolver* proxy_resolver)
34 : HttpFetcher(proxy_resolver),
35 sent_size_(0),
Darin Petkovedc522e2010-11-05 09:35:17 -070036 timeout_source_(NULL),
37 timout_tag_(0),
38 paused_(false),
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070039 fail_transfer_(false),
40 never_use_(false) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000041 data_.insert(data_.end(), data, data + size);
rspangler@google.com49fdf182009-10-10 00:57:34 +000042 }
43
44 // Cleans up all internal state. Does not notify delegate
45 ~MockHttpFetcher();
46
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070047 // Ignores this.
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070048 virtual void SetOffset(off_t offset) {
49 sent_size_ = offset;
50 if (delegate_)
51 delegate_->SeekToOffset(offset);
52 }
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070053
Gilad Arnold48085ba2011-11-16 09:36:08 -080054 // Dummy: no bytes were downloaded.
55 virtual size_t GetBytesDownloaded() {
56 return sent_size_;
57 }
58
rspangler@google.com49fdf182009-10-10 00:57:34 +000059 // Begins the transfer if it hasn't already begun.
60 virtual void BeginTransfer(const std::string& url);
61
62 // If the transfer is in progress, aborts the transfer early.
63 // The transfer cannot be resumed.
64 virtual void TerminateTransfer();
65
66 // Suspend the mock transfer.
67 virtual void Pause();
68
69 // Resume the mock transfer.
70 virtual void Unpause();
71
72 // Fail the transfer. This simulates a network failure.
Darin Petkovedc522e2010-11-05 09:35:17 -070073 void FailTransfer(int http_response_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000074
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070075 // If set to true, this will EXPECT fail on BeginTransfer
76 void set_never_use(bool never_use) { never_use_ = never_use; }
77
rspangler@google.com49fdf182009-10-10 00:57:34 +000078 const std::vector<char>& post_data() const {
79 return post_data_;
80 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000081
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 private:
83 // Sends data to the delegate and sets up a glib timeout callback if needed.
84 // There must be a delegate and there must be data to send. If there is
85 // already a timeout callback, and it should be deleted by the caller,
86 // this will return false; otherwise true is returned.
87 // If skip_delivery is true, no bytes will be delivered, but the callbacks
88 // still still be set if needed
89 bool SendData(bool skip_delivery);
90
91 // Callback for when our glib main loop callback is called
92 bool TimeoutCallback();
93 static gboolean StaticTimeoutCallback(gpointer data) {
94 return reinterpret_cast<MockHttpFetcher*>(data)->TimeoutCallback();
95 }
96
Darin Petkovedc522e2010-11-05 09:35:17 -070097 // Sets the HTTP response code and signals to the delegate that the transfer
98 // is complete.
99 void SignalTransferComplete();
100
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 // A full copy of the data we'll return to the delegate
102 std::vector<char> data_;
103
104 // The number of bytes we've sent so far
105 size_t sent_size_;
106
107 // The glib main loop timeout source. After each chunk of data sent, we
108 // time out for 0s just to make sure that run loop services other clients.
109 GSource* timeout_source_;
110
111 // ID of the timeout source, valid only if timeout_source_ != NULL
112 guint timout_tag_;
113
114 // True iff the fetcher is paused.
115 bool paused_;
116
Darin Petkovedc522e2010-11-05 09:35:17 -0700117 // Set to true if the transfer should fail.
118 bool fail_transfer_;
119
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700120 // Set to true if BeginTransfer should EXPECT fail.
121 bool never_use_;
122
rspangler@google.com49fdf182009-10-10 00:57:34 +0000123 DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
124};
125
126} // namespace chromeos_update_engine
127
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000128#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MOCK_HTTP_FETCHER_H__