blob: 89f5d21cd1a5c600bcaa3d8f16ddb28f92e3c29a [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>
Alex Deymo60ca1a72015-06-18 18:19:15 -070012#include <chromeos/message_loops/message_loop.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000013#include <glib.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080014
Gilad Arnold5bb4c902014-04-10 12:32:13 -070015#include "update_engine/fake_system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000016#include "update_engine/http_fetcher.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070017#include "update_engine/mock_connection_manager.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000018
19// This is a mock implementation of HttpFetcher which is useful for testing.
20// All data must be passed into the ctor. When started, MockHttpFetcher will
21// deliver the data in chunks of size kMockHttpFetcherChunkSize. To simulate
22// a network failure, you can call FailTransfer().
23
24namespace chromeos_update_engine {
25
26// MockHttpFetcher will send a chunk of data down in each call to BeginTransfer
27// and Unpause. For the other chunks of data, a callback is put on the run
28// loop and when that's called, another chunk is sent down.
29const size_t kMockHttpFetcherChunkSize(65536);
30
31class MockHttpFetcher : public HttpFetcher {
32 public:
33 // The data passed in here is copied and then passed to the delegate after
34 // the transfer begins.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080035 MockHttpFetcher(const uint8_t* data,
Andrew de los Reyes45168102010-11-22 11:13:50 -080036 size_t size,
37 ProxyResolver* proxy_resolver)
Gilad Arnold5bb4c902014-04-10 12:32:13 -070038 : HttpFetcher(proxy_resolver, &fake_system_state_),
Andrew de los Reyes45168102010-11-22 11:13:50 -080039 sent_size_(0),
Alex Deymo60ca1a72015-06-18 18:19:15 -070040 timeout_id_(chromeos::MessageLoop::kTaskIdNull),
Darin Petkovedc522e2010-11-05 09:35:17 -070041 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
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080049 // Constructor overload for string data.
50 MockHttpFetcher(const char* data, size_t size, ProxyResolver* proxy_resolver)
51 : MockHttpFetcher(reinterpret_cast<const uint8_t*>(data), size,
52 proxy_resolver) {}
53
rspangler@google.com49fdf182009-10-10 00:57:34 +000054 // Cleans up all internal state. Does not notify delegate
Alex Deymo610277e2014-11-11 21:18:11 -080055 ~MockHttpFetcher() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000056
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070057 // Ignores this.
Alex Deymo610277e2014-11-11 21:18:11 -080058 void SetOffset(off_t offset) override {
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070059 sent_size_ = offset;
60 if (delegate_)
61 delegate_->SeekToOffset(offset);
62 }
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070063
Gilad Arnolde4ad2502011-12-29 17:08:54 -080064 // Do nothing.
Alex Deymo610277e2014-11-11 21:18:11 -080065 void SetLength(size_t length) override {}
66 void UnsetLength() override {}
67 void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {}
68 void set_connect_timeout(int connect_timeout_seconds) override {}
69 void set_max_retry_count(int max_retry_count) override {}
Gilad Arnolde4ad2502011-12-29 17:08:54 -080070
Gilad Arnold48085ba2011-11-16 09:36:08 -080071 // Dummy: no bytes were downloaded.
Alex Deymo610277e2014-11-11 21:18:11 -080072 size_t GetBytesDownloaded() override {
Gilad Arnold48085ba2011-11-16 09:36:08 -080073 return sent_size_;
74 }
75
rspangler@google.com49fdf182009-10-10 00:57:34 +000076 // Begins the transfer if it hasn't already begun.
Alex Deymo610277e2014-11-11 21:18:11 -080077 void BeginTransfer(const std::string& url) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000078
79 // If the transfer is in progress, aborts the transfer early.
80 // The transfer cannot be resumed.
Alex Deymo610277e2014-11-11 21:18:11 -080081 void TerminateTransfer() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000082
83 // Suspend the mock transfer.
Alex Deymo610277e2014-11-11 21:18:11 -080084 void Pause() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000085
86 // Resume the mock transfer.
Alex Deymo610277e2014-11-11 21:18:11 -080087 void Unpause() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000088
89 // Fail the transfer. This simulates a network failure.
Darin Petkovedc522e2010-11-05 09:35:17 -070090 void FailTransfer(int http_response_code);
rspangler@google.com49fdf182009-10-10 00:57:34 +000091
Andrew de los Reyes173e63c2011-04-04 17:19:57 -070092 // If set to true, this will EXPECT fail on BeginTransfer
93 void set_never_use(bool never_use) { never_use_ = never_use; }
94
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080095 const chromeos::Blob& post_data() const {
rspangler@google.com49fdf182009-10-10 00:57:34 +000096 return post_data_;
97 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +000098
rspangler@google.com49fdf182009-10-10 00:57:34 +000099 private:
Alex Deymo60ca1a72015-06-18 18:19:15 -0700100 // Sends data to the delegate and sets up a timeout callback if needed.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 // There must be a delegate and there must be data to send. If there is
102 // already a timeout callback, and it should be deleted by the caller,
103 // this will return false; otherwise true is returned.
104 // If skip_delivery is true, no bytes will be delivered, but the callbacks
Alex Vakulenko072359c2014-07-18 11:41:07 -0700105 // still be set if needed.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106 bool SendData(bool skip_delivery);
107
Alex Deymo60ca1a72015-06-18 18:19:15 -0700108 // Callback for when our message loop timeout expires.
109 void TimeoutCallback();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110
Darin Petkovedc522e2010-11-05 09:35:17 -0700111 // Sets the HTTP response code and signals to the delegate that the transfer
112 // is complete.
113 void SignalTransferComplete();
114
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115 // A full copy of the data we'll return to the delegate
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800116 chromeos::Blob data_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117
118 // The number of bytes we've sent so far
119 size_t sent_size_;
120
Alex Deymo60ca1a72015-06-18 18:19:15 -0700121 // The TaskId of the timeout callback. After each chunk of data sent, we
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122 // time out for 0s just to make sure that run loop services other clients.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700123 chromeos::MessageLoop::TaskId timeout_id_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000124
125 // True iff the fetcher is paused.
126 bool paused_;
127
Darin Petkovedc522e2010-11-05 09:35:17 -0700128 // Set to true if the transfer should fail.
129 bool fail_transfer_;
130
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700131 // Set to true if BeginTransfer should EXPECT fail.
132 bool never_use_;
133
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700134 FakeSystemState fake_system_state_;
Jay Srinivasan43488792012-06-19 00:25:31 -0700135 MockConnectionManager mock_connection_manager_;
136
rspangler@google.com49fdf182009-10-10 00:57:34 +0000137 DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
138};
139
140} // namespace chromeos_update_engine
141
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700142#endif // UPDATE_ENGINE_MOCK_HTTP_FETCHER_H_