blob: 3dc064588a7d5f53096665adee4f0477d785833f [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
5#ifndef UPDATE_ENGINE_MOCK_HTTP_FETCHER_H__
6#define UPDATE_ENGINE_MOCK_HTTP_FETCHER_H__
7
8#include <vector>
9#include <glib.h>
10#include <glog/logging.h>
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
18namespace 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.
23const size_t kMockHttpFetcherChunkSize(65536);
24
25class 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);
32 LOG(INFO) << "timeout_source_ = " << (int)timeout_source_;
33 }
34
35 // Cleans up all internal state. Does not notify delegate
36 ~MockHttpFetcher();
37
38 // Begins the transfer if it hasn't already begun.
39 virtual void BeginTransfer(const std::string& url);
40
41 // If the transfer is in progress, aborts the transfer early.
42 // The transfer cannot be resumed.
43 virtual void TerminateTransfer();
44
45 // Suspend the mock transfer.
46 virtual void Pause();
47
48 // Resume the mock transfer.
49 virtual void Unpause();
50
51 // Fail the transfer. This simulates a network failure.
52 void FailTransfer();
53
54 const std::vector<char>& post_data() const {
55 return post_data_;
56 }
57
58 private:
59 // Sends data to the delegate and sets up a glib timeout callback if needed.
60 // There must be a delegate and there must be data to send. If there is
61 // already a timeout callback, and it should be deleted by the caller,
62 // this will return false; otherwise true is returned.
63 // If skip_delivery is true, no bytes will be delivered, but the callbacks
64 // still still be set if needed
65 bool SendData(bool skip_delivery);
66
67 // Callback for when our glib main loop callback is called
68 bool TimeoutCallback();
69 static gboolean StaticTimeoutCallback(gpointer data) {
70 return reinterpret_cast<MockHttpFetcher*>(data)->TimeoutCallback();
71 }
72
73 // A full copy of the data we'll return to the delegate
74 std::vector<char> data_;
75
76 // The number of bytes we've sent so far
77 size_t sent_size_;
78
79 // The glib main loop timeout source. After each chunk of data sent, we
80 // time out for 0s just to make sure that run loop services other clients.
81 GSource* timeout_source_;
82
83 // ID of the timeout source, valid only if timeout_source_ != NULL
84 guint timout_tag_;
85
86 // True iff the fetcher is paused.
87 bool paused_;
88
89 DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
90};
91
92} // namespace chromeos_update_engine
93
94#endif // UPDATE_ENGINE_MOCK_HTTP_FETCHER_H__