blob: dfe61a73cd836146fe491290b93f2cdf07c54d7b [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>
9#include <glib.h>
Chris Masone790e62e2010-08-12 10:41:18 -070010#include "base/logging.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000011#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)
Darin Petkovedc522e2010-11-05 09:35:17 -070030 : sent_size_(0),
31 timeout_source_(NULL),
32 timout_tag_(0),
33 paused_(false),
34 fail_transfer_(false) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000035 data_.insert(data_.end(), data, data + size);
rspangler@google.com49fdf182009-10-10 00:57:34 +000036 }
37
38 // Cleans up all internal state. Does not notify delegate
39 ~MockHttpFetcher();
40
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070041 // Ignores this.
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070042 virtual void SetOffset(off_t offset) {
43 sent_size_ = offset;
44 if (delegate_)
45 delegate_->SeekToOffset(offset);
46 }
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070047
rspangler@google.com49fdf182009-10-10 00:57:34 +000048 // Begins the transfer if it hasn't already begun.
49 virtual void BeginTransfer(const std::string& url);
50
51 // If the transfer is in progress, aborts the transfer early.
52 // The transfer cannot be resumed.
53 virtual void TerminateTransfer();
54
55 // Suspend the mock transfer.
56 virtual void Pause();
57
58 // Resume the mock transfer.
59 virtual void Unpause();
60
61 // Fail the transfer. This simulates a network failure.
Darin Petkovedc522e2010-11-05 09:35:17 -070062 void FailTransfer(int http_response_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000063
64 const std::vector<char>& post_data() const {
65 return post_data_;
66 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000067
rspangler@google.com49fdf182009-10-10 00:57:34 +000068 private:
69 // Sends data to the delegate and sets up a glib timeout callback if needed.
70 // There must be a delegate and there must be data to send. If there is
71 // already a timeout callback, and it should be deleted by the caller,
72 // this will return false; otherwise true is returned.
73 // If skip_delivery is true, no bytes will be delivered, but the callbacks
74 // still still be set if needed
75 bool SendData(bool skip_delivery);
76
77 // Callback for when our glib main loop callback is called
78 bool TimeoutCallback();
79 static gboolean StaticTimeoutCallback(gpointer data) {
80 return reinterpret_cast<MockHttpFetcher*>(data)->TimeoutCallback();
81 }
82
Darin Petkovedc522e2010-11-05 09:35:17 -070083 // Sets the HTTP response code and signals to the delegate that the transfer
84 // is complete.
85 void SignalTransferComplete();
86
rspangler@google.com49fdf182009-10-10 00:57:34 +000087 // A full copy of the data we'll return to the delegate
88 std::vector<char> data_;
89
90 // The number of bytes we've sent so far
91 size_t sent_size_;
92
93 // The glib main loop timeout source. After each chunk of data sent, we
94 // time out for 0s just to make sure that run loop services other clients.
95 GSource* timeout_source_;
96
97 // ID of the timeout source, valid only if timeout_source_ != NULL
98 guint timout_tag_;
99
100 // True iff the fetcher is paused.
101 bool paused_;
102
Darin Petkovedc522e2010-11-05 09:35:17 -0700103 // Set to true if the transfer should fail.
104 bool fail_transfer_;
105
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106 DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
107};
108
109} // namespace chromeos_update_engine
110
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000111#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MOCK_HTTP_FETCHER_H__