xref: /aosp_15_r20/system/update_engine/common/mock_http_fetcher.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2009 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <map>
21*5a923131SAndroid Build Coastguard Worker #include <string>
22*5a923131SAndroid Build Coastguard Worker #include <vector>
23*5a923131SAndroid Build Coastguard Worker 
24*5a923131SAndroid Build Coastguard Worker #include <base/logging.h>
25*5a923131SAndroid Build Coastguard Worker #include <brillo/message_loops/message_loop.h>
26*5a923131SAndroid Build Coastguard Worker 
27*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/http_fetcher.h"
28*5a923131SAndroid Build Coastguard Worker 
29*5a923131SAndroid Build Coastguard Worker // This is a mock implementation of HttpFetcher which is useful for testing.
30*5a923131SAndroid Build Coastguard Worker // All data must be passed into the ctor. When started, MockHttpFetcher will
31*5a923131SAndroid Build Coastguard Worker // deliver the data in chunks of size kMockHttpFetcherChunkSize. To simulate
32*5a923131SAndroid Build Coastguard Worker // a network failure, you can call FailTransfer().
33*5a923131SAndroid Build Coastguard Worker 
34*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
35*5a923131SAndroid Build Coastguard Worker 
36*5a923131SAndroid Build Coastguard Worker // MockHttpFetcher will send a chunk of data down in each call to BeginTransfer
37*5a923131SAndroid Build Coastguard Worker // and Unpause. For the other chunks of data, a callback is put on the run
38*5a923131SAndroid Build Coastguard Worker // loop and when that's called, another chunk is sent down.
39*5a923131SAndroid Build Coastguard Worker static constexpr size_t kMockHttpFetcherChunkSize(65536);
40*5a923131SAndroid Build Coastguard Worker 
41*5a923131SAndroid Build Coastguard Worker class MockHttpFetcher : public HttpFetcher {
42*5a923131SAndroid Build Coastguard Worker  public:
43*5a923131SAndroid Build Coastguard Worker   // The data passed in here is copied and then passed to the delegate after
44*5a923131SAndroid Build Coastguard Worker   // the transfer begins.
MockHttpFetcher(const uint8_t * data,size_t size)45*5a923131SAndroid Build Coastguard Worker   MockHttpFetcher(const uint8_t* data, size_t size)
46*5a923131SAndroid Build Coastguard Worker       : HttpFetcher(),
47*5a923131SAndroid Build Coastguard Worker         sent_offset_(0),
48*5a923131SAndroid Build Coastguard Worker         timeout_id_(brillo::MessageLoop::kTaskIdNull),
49*5a923131SAndroid Build Coastguard Worker         paused_(false),
50*5a923131SAndroid Build Coastguard Worker         fail_transfer_(false),
51*5a923131SAndroid Build Coastguard Worker         never_use_(false) {
52*5a923131SAndroid Build Coastguard Worker     data_.insert(data_.end(), data, data + size);
53*5a923131SAndroid Build Coastguard Worker   }
54*5a923131SAndroid Build Coastguard Worker 
55*5a923131SAndroid Build Coastguard Worker   // Constructor overload for string data.
MockHttpFetcher(const char * data,size_t size)56*5a923131SAndroid Build Coastguard Worker   MockHttpFetcher(const char* data, size_t size)
57*5a923131SAndroid Build Coastguard Worker       : MockHttpFetcher(reinterpret_cast<const uint8_t*>(data), size) {}
58*5a923131SAndroid Build Coastguard Worker 
59*5a923131SAndroid Build Coastguard Worker   // Cleans up all internal state. Does not notify delegate
60*5a923131SAndroid Build Coastguard Worker   ~MockHttpFetcher() override;
61*5a923131SAndroid Build Coastguard Worker 
62*5a923131SAndroid Build Coastguard Worker   // Ignores this.
SetOffset(off_t offset)63*5a923131SAndroid Build Coastguard Worker   void SetOffset(off_t offset) override {
64*5a923131SAndroid Build Coastguard Worker     sent_offset_ = offset;
65*5a923131SAndroid Build Coastguard Worker     if (delegate_)
66*5a923131SAndroid Build Coastguard Worker       delegate_->SeekToOffset(offset);
67*5a923131SAndroid Build Coastguard Worker   }
68*5a923131SAndroid Build Coastguard Worker 
69*5a923131SAndroid Build Coastguard Worker   // Do nothing.
SetLength(size_t length)70*5a923131SAndroid Build Coastguard Worker   void SetLength(size_t length) override {}
UnsetLength()71*5a923131SAndroid Build Coastguard Worker   void UnsetLength() override {}
set_low_speed_limit(int low_speed_bps,int low_speed_sec)72*5a923131SAndroid Build Coastguard Worker   void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {}
set_connect_timeout(int connect_timeout_seconds)73*5a923131SAndroid Build Coastguard Worker   void set_connect_timeout(int connect_timeout_seconds) override {}
set_max_retry_count(int max_retry_count)74*5a923131SAndroid Build Coastguard Worker   void set_max_retry_count(int max_retry_count) override {}
75*5a923131SAndroid Build Coastguard Worker 
76*5a923131SAndroid Build Coastguard Worker   // No bytes were downloaded in the mock class.
GetBytesDownloaded()77*5a923131SAndroid Build Coastguard Worker   size_t GetBytesDownloaded() override { return bytes_sent_; }
78*5a923131SAndroid Build Coastguard Worker 
79*5a923131SAndroid Build Coastguard Worker   // Begins the transfer if it hasn't already begun.
80*5a923131SAndroid Build Coastguard Worker   void BeginTransfer(const std::string& url) override;
81*5a923131SAndroid Build Coastguard Worker 
82*5a923131SAndroid Build Coastguard Worker   // If the transfer is in progress, aborts the transfer early.
83*5a923131SAndroid Build Coastguard Worker   // The transfer cannot be resumed.
84*5a923131SAndroid Build Coastguard Worker   void TerminateTransfer() override;
85*5a923131SAndroid Build Coastguard Worker 
86*5a923131SAndroid Build Coastguard Worker   void SetHeader(const std::string& header_name,
87*5a923131SAndroid Build Coastguard Worker                  const std::string& header_value) override;
88*5a923131SAndroid Build Coastguard Worker 
GetHeader(const std::string & header_name,std::string * header_value)89*5a923131SAndroid Build Coastguard Worker   bool GetHeader(const std::string& header_name,
90*5a923131SAndroid Build Coastguard Worker                  std::string* header_value) const override {
91*5a923131SAndroid Build Coastguard Worker     header_value->clear();
92*5a923131SAndroid Build Coastguard Worker     return false;
93*5a923131SAndroid Build Coastguard Worker   }
94*5a923131SAndroid Build Coastguard Worker 
95*5a923131SAndroid Build Coastguard Worker   // Return the value of the header |header_name| or the empty string if not
96*5a923131SAndroid Build Coastguard Worker   // set.
97*5a923131SAndroid Build Coastguard Worker   std::string GetHeader(const std::string& header_name) const;
98*5a923131SAndroid Build Coastguard Worker 
99*5a923131SAndroid Build Coastguard Worker   // Suspend the mock transfer.
100*5a923131SAndroid Build Coastguard Worker   void Pause() override;
101*5a923131SAndroid Build Coastguard Worker 
102*5a923131SAndroid Build Coastguard Worker   // Resume the mock transfer.
103*5a923131SAndroid Build Coastguard Worker   void Unpause() override;
104*5a923131SAndroid Build Coastguard Worker 
105*5a923131SAndroid Build Coastguard Worker   // Fail the transfer. This simulates a network failure.
106*5a923131SAndroid Build Coastguard Worker   void FailTransfer(int http_response_code);
107*5a923131SAndroid Build Coastguard Worker 
108*5a923131SAndroid Build Coastguard Worker   // If set to true, this will EXPECT fail on BeginTransfer
set_never_use(bool never_use)109*5a923131SAndroid Build Coastguard Worker   void set_never_use(bool never_use) { never_use_ = never_use; }
110*5a923131SAndroid Build Coastguard Worker 
post_data()111*5a923131SAndroid Build Coastguard Worker   const brillo::Blob& post_data() const { return post_data_; }
112*5a923131SAndroid Build Coastguard Worker 
set_delay(bool delay)113*5a923131SAndroid Build Coastguard Worker   void set_delay(bool delay) { delay_ = delay; }
114*5a923131SAndroid Build Coastguard Worker 
115*5a923131SAndroid Build Coastguard Worker  private:
116*5a923131SAndroid Build Coastguard Worker   // Sends data to the delegate and sets up a timeout callback if needed. There
117*5a923131SAndroid Build Coastguard Worker   // must be a delegate. If |skip_delivery| is true, no bytes will be delivered,
118*5a923131SAndroid Build Coastguard Worker   // but the callbacks still be set if needed.
119*5a923131SAndroid Build Coastguard Worker   void SendData(bool skip_delivery);
120*5a923131SAndroid Build Coastguard Worker 
121*5a923131SAndroid Build Coastguard Worker   // Callback for when our message loop timeout expires.
122*5a923131SAndroid Build Coastguard Worker   void TimeoutCallback();
123*5a923131SAndroid Build Coastguard Worker 
124*5a923131SAndroid Build Coastguard Worker   // Sets the HTTP response code and signals to the delegate that the transfer
125*5a923131SAndroid Build Coastguard Worker   // is complete.
126*5a923131SAndroid Build Coastguard Worker   void SignalTransferComplete();
127*5a923131SAndroid Build Coastguard Worker 
128*5a923131SAndroid Build Coastguard Worker   // A full copy of the data we'll return to the delegate
129*5a923131SAndroid Build Coastguard Worker   brillo::Blob data_;
130*5a923131SAndroid Build Coastguard Worker 
131*5a923131SAndroid Build Coastguard Worker   // The current offset, marks the first byte that will be sent next
132*5a923131SAndroid Build Coastguard Worker   size_t sent_offset_{0};
133*5a923131SAndroid Build Coastguard Worker 
134*5a923131SAndroid Build Coastguard Worker   // Total number of bytes transferred
135*5a923131SAndroid Build Coastguard Worker   size_t bytes_sent_{0};
136*5a923131SAndroid Build Coastguard Worker 
137*5a923131SAndroid Build Coastguard Worker   // The extra headers set.
138*5a923131SAndroid Build Coastguard Worker   std::map<std::string, std::string> extra_headers_;
139*5a923131SAndroid Build Coastguard Worker 
140*5a923131SAndroid Build Coastguard Worker   // The TaskId of the timeout callback. After each chunk of data sent, we
141*5a923131SAndroid Build Coastguard Worker   // time out for 0s just to make sure that run loop services other clients.
142*5a923131SAndroid Build Coastguard Worker   brillo::MessageLoop::TaskId timeout_id_;
143*5a923131SAndroid Build Coastguard Worker 
144*5a923131SAndroid Build Coastguard Worker   // True iff the fetcher is paused.
145*5a923131SAndroid Build Coastguard Worker   bool paused_{false};
146*5a923131SAndroid Build Coastguard Worker 
147*5a923131SAndroid Build Coastguard Worker   // Set to true if the transfer should fail.
148*5a923131SAndroid Build Coastguard Worker   bool fail_transfer_{false};
149*5a923131SAndroid Build Coastguard Worker 
150*5a923131SAndroid Build Coastguard Worker   // Set to true if BeginTransfer should EXPECT fail.
151*5a923131SAndroid Build Coastguard Worker   bool never_use_{false};
152*5a923131SAndroid Build Coastguard Worker 
153*5a923131SAndroid Build Coastguard Worker   // Whether it should wait for 10ms before sending data to delegates
154*5a923131SAndroid Build Coastguard Worker   bool delay_{true};
155*5a923131SAndroid Build Coastguard Worker 
156*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
157*5a923131SAndroid Build Coastguard Worker };
158*5a923131SAndroid Build Coastguard Worker 
159*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
160*5a923131SAndroid Build Coastguard Worker 
161*5a923131SAndroid Build Coastguard Worker #endif  // UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_
162