blob: da36306ab0972d3a9a373f41059f8b312d1a657f [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Jay Srinivasan08fce042012-06-07 16:31:01 -070016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_CROS_FAKE_SYSTEM_STATE_H_
18#define UPDATE_ENGINE_CROS_FAKE_SYSTEM_STATE_H_
Jay Srinivasan08fce042012-06-07 16:31:01 -070019
Amin Hassaniddd68222020-11-30 16:45:59 -080020#include <memory>
21
Gilad Arnold1f847232014-04-07 12:07:49 -070022#include <base/logging.h>
Jay Srinivasan08fce042012-06-07 16:31:01 -070023#include <gmock/gmock.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070024#include <policy/mock_device_policy.h>
Jay Srinivasanf0572052012-10-23 18:12:56 -070025
Gilad Arnold1f847232014-04-07 12:07:49 -070026#include "metrics/metrics_library_mock.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/fake_boot_control.h"
28#include "update_engine/common/fake_clock.h"
29#include "update_engine/common/fake_hardware.h"
Amin Hassani90e9f192020-11-18 14:20:56 -080030#include "update_engine/common/fake_prefs.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070031#include "update_engine/common/mock_metrics_reporter.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/common/mock_prefs.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070033#include "update_engine/common/system_state.h"
34#include "update_engine/cros/mock_connection_manager.h"
35#include "update_engine/cros/mock_omaha_request_params.h"
36#include "update_engine/cros/mock_p2p_manager.h"
37#include "update_engine/cros/mock_payload_state.h"
38#include "update_engine/cros/mock_power_manager.h"
39#include "update_engine/cros/mock_update_attempter.h"
Alex Deymo63784a52014-05-28 10:46:14 -070040#include "update_engine/update_manager/fake_update_manager.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070041
42namespace chromeos_update_engine {
43
44// Mock the SystemStateInterface so that we could lie that
45// OOBE is completed even when there's no such marker file, etc.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070046class FakeSystemState : public SystemState {
Jay Srinivasan43488792012-06-19 00:25:31 -070047 public:
Amin Hassaniddd68222020-11-30 16:45:59 -080048 static void CreateInstance() {
49 static std::unique_ptr<FakeSystemState> system_state;
50 system_state.reset(new FakeSystemState());
51 g_pointer_ = system_state.get();
52 }
Amin Hassani538bd592020-11-04 20:46:08 -080053
54 static FakeSystemState* Get() {
Amin Hassaniddd68222020-11-30 16:45:59 -080055 return reinterpret_cast<FakeSystemState*>(g_pointer_);
Amin Hassani538bd592020-11-04 20:46:08 -080056 }
Jay Srinivasan55f50c22013-01-10 19:24:35 -080057
Gilad Arnold1f847232014-04-07 12:07:49 -070058 // Base class overrides. All getters return the current implementation of
59 // various members, either the default (fake/mock) or the one set to override
60 // it by client code.
Jay Srinivasan43488792012-06-19 00:25:31 -070061
Alex Deymo763e7db2015-08-27 21:08:08 -070062 BootControlInterface* boot_control() override { return boot_control_; }
63
Alex Vakulenko157fe302014-08-11 15:59:58 -070064 inline ClockInterface* clock() override { return clock_; }
David Zeuthenf413fe52013-04-22 14:04:39 -070065
Alex Vakulenko157fe302014-08-11 15:59:58 -070066 inline void set_device_policy(
Gilad Arnold1f847232014-04-07 12:07:49 -070067 const policy::DevicePolicy* device_policy) override {
68 device_policy_ = device_policy;
69 }
70
Alex Vakulenko157fe302014-08-11 15:59:58 -070071 inline const policy::DevicePolicy* device_policy() override {
Gilad Arnold1f847232014-04-07 12:07:49 -070072 return device_policy_;
73 }
74
Alex Deymof6ee0162015-07-31 12:35:22 -070075 inline ConnectionManagerInterface* connection_manager() override {
Jay Srinivasan43488792012-06-19 00:25:31 -070076 return connection_manager_;
77 }
78
Alex Vakulenko157fe302014-08-11 15:59:58 -070079 inline HardwareInterface* hardware() override { return hardware_; }
Alex Deymo42432912013-07-12 20:21:15 -070080
Tianjie Xu282aa1f2017-09-05 13:42:45 -070081 inline MetricsReporterInterface* metrics_reporter() override {
82 CHECK(metrics_reporter_ != nullptr);
83 return metrics_reporter_;
Jay Srinivasanf0572052012-10-23 18:12:56 -070084 }
85
Alex Vakulenko157fe302014-08-11 15:59:58 -070086 inline PrefsInterface* prefs() override { return prefs_; }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080087
Alex Vakulenko157fe302014-08-11 15:59:58 -070088 inline PrefsInterface* powerwash_safe_prefs() override {
Chris Sosaaa18e162013-06-20 13:20:30 -070089 return powerwash_safe_prefs_;
90 }
91
Alex Vakulenko157fe302014-08-11 15:59:58 -070092 inline PayloadStateInterface* payload_state() override {
David Zeuthen526cb582013-08-06 12:26:18 -070093 return payload_state_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080094 }
95
Alex Vakulenko157fe302014-08-11 15:59:58 -070096 inline UpdateAttempter* update_attempter() override {
Gilad Arnold1f847232014-04-07 12:07:49 -070097 return update_attempter_;
Gilad Arnold76b2b482014-04-01 13:32:43 -070098 }
Jay Srinivasan55f50c22013-01-10 19:24:35 -080099
Alex Vakulenko157fe302014-08-11 15:59:58 -0700100 inline OmahaRequestParams* request_params() override {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700101 return request_params_;
102 }
103
Alex Vakulenko157fe302014-08-11 15:59:58 -0700104 inline P2PManager* p2p_manager() override { return p2p_manager_; }
David Zeuthen526cb582013-08-06 12:26:18 -0700105
Alex Vakulenko157fe302014-08-11 15:59:58 -0700106 inline chromeos_update_manager::UpdateManager* update_manager() override {
Alex Deymo63784a52014-05-28 10:46:14 -0700107 return update_manager_;
Alex Deymo94c06162014-03-21 20:34:46 -0700108 }
109
Sen Jiangb8c6a8f2016-06-07 17:33:17 -0700110 inline PowerManagerInterface* power_manager() override {
111 return power_manager_;
Alex Deymo30534502015-07-20 15:06:33 -0700112 }
113
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800114 inline DlcServiceInterface* dlcservice() override { return dlcservice_; }
115
Alex Vakulenko157fe302014-08-11 15:59:58 -0700116 inline bool system_rebooted() override { return fake_system_rebooted_; }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800117
Gilad Arnold1f847232014-04-07 12:07:49 -0700118 // Setters for the various members, can be used for overriding the default
119 // implementations. For convenience, setting to a null pointer will restore
120 // the default implementation.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800121
Alex Deymo763e7db2015-08-27 21:08:08 -0700122 void set_boot_control(BootControlInterface* boot_control) {
123 boot_control_ = boot_control ? boot_control : &fake_boot_control_;
124 }
125
David Zeuthenf413fe52013-04-22 14:04:39 -0700126 inline void set_clock(ClockInterface* clock) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700127 clock_ = clock ? clock : &fake_clock_;
128 }
129
Alex Deymof6ee0162015-07-31 12:35:22 -0700130 inline void set_connection_manager(
131 ConnectionManagerInterface* connection_manager) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800132 connection_manager_ =
133 (connection_manager ? connection_manager : &mock_connection_manager_);
David Zeuthenf413fe52013-04-22 14:04:39 -0700134 }
135
Alex Deymo42432912013-07-12 20:21:15 -0700136 inline void set_hardware(HardwareInterface* hardware) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700137 hardware_ = hardware ? hardware : &fake_hardware_;
Alex Deymo42432912013-07-12 20:21:15 -0700138 }
139
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700140 inline void set_metrics_reporter(MetricsReporterInterface* metrics_reporter) {
141 metrics_reporter_ =
142 metrics_reporter ? metrics_reporter : &mock_metrics_reporter_;
J. Richard Barnette4da2cc12013-10-28 16:11:10 -0700143 }
144
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800145 inline void set_prefs(PrefsInterface* prefs) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700146 prefs_ = prefs ? prefs : &mock_prefs_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800147 }
148
Gilad Arnold1f847232014-04-07 12:07:49 -0700149 inline void set_powerwash_safe_prefs(PrefsInterface* powerwash_safe_prefs) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800150 powerwash_safe_prefs_ =
151 (powerwash_safe_prefs ? powerwash_safe_prefs
152 : &mock_powerwash_safe_prefs_);
Chris Sosaaa18e162013-06-20 13:20:30 -0700153 }
154
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800155 inline void set_payload_state(PayloadStateInterface* payload_state) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700156 payload_state_ = payload_state ? payload_state : &mock_payload_state_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800157 }
158
Gilad Arnold1f847232014-04-07 12:07:49 -0700159 inline void set_update_attempter(UpdateAttempter* update_attempter) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800160 update_attempter_ =
161 (update_attempter ? update_attempter : &mock_update_attempter_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800162 }
Jay Srinivasanf0572052012-10-23 18:12:56 -0700163
Gilad Arnold1f847232014-04-07 12:07:49 -0700164 inline void set_request_params(OmahaRequestParams* request_params) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800165 request_params_ = (request_params ? request_params : &mock_request_params_);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700166 }
167
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800168 inline void set_p2p_manager(P2PManager* p2p_manager) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700169 p2p_manager_ = p2p_manager ? p2p_manager : &mock_p2p_manager_;
David Zeuthen526cb582013-08-06 12:26:18 -0700170 }
171
Alex Deymo63784a52014-05-28 10:46:14 -0700172 inline void set_update_manager(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800173 chromeos_update_manager::UpdateManager* update_manager) {
Alex Deymo63784a52014-05-28 10:46:14 -0700174 update_manager_ = update_manager ? update_manager : &fake_update_manager_;
Alex Deymo94c06162014-03-21 20:34:46 -0700175 }
176
Gilad Arnold1f847232014-04-07 12:07:49 -0700177 inline void set_system_rebooted(bool system_rebooted) {
178 fake_system_rebooted_ = system_rebooted;
179 }
180
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800181 inline void set_dlcservice(DlcServiceInterface* dlcservice) {
182 dlcservice_ = dlcservice;
183 }
184
Gilad Arnold1f847232014-04-07 12:07:49 -0700185 // Getters for the built-in default implementations. These return the actual
186 // concrete type of each implementation. For additional safety, they will fail
187 // whenever the requested default was overridden by a different
188 // implementation.
189
Alex Deymo763e7db2015-08-27 21:08:08 -0700190 inline FakeBootControl* fake_boot_control() {
191 CHECK(boot_control_ == &fake_boot_control_);
192 return &fake_boot_control_;
193 }
194
Gilad Arnold1f847232014-04-07 12:07:49 -0700195 inline FakeClock* fake_clock() {
196 CHECK(clock_ == &fake_clock_);
197 return &fake_clock_;
198 }
199
200 inline testing::NiceMock<MockConnectionManager>* mock_connection_manager() {
201 CHECK(connection_manager_ == &mock_connection_manager_);
202 return &mock_connection_manager_;
203 }
204
205 inline FakeHardware* fake_hardware() {
206 CHECK(hardware_ == &fake_hardware_);
207 return &fake_hardware_;
208 }
209
Amin Hassani90e9f192020-11-18 14:20:56 -0800210 inline FakePrefs* fake_prefs() {
211 CHECK(prefs_ == &fake_prefs_);
212 return &fake_prefs_;
213 }
214
215 inline FakePrefs* fake_powerwash_safe_prefs() {
216 CHECK(powerwash_safe_prefs_ == &fake_powerwash_safe_prefs_);
217 return &fake_powerwash_safe_prefs_;
218 }
219
Tianjie Xu1f93d092017-10-09 12:13:29 -0700220 inline testing::NiceMock<MockMetricsReporter>* mock_metrics_reporter() {
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700221 CHECK(metrics_reporter_ == &mock_metrics_reporter_);
222 return &mock_metrics_reporter_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700223 }
224
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800225 inline testing::NiceMock<MockPrefs>* mock_prefs() {
Gilad Arnold1f847232014-04-07 12:07:49 -0700226 CHECK(prefs_ == &mock_prefs_);
227 return &mock_prefs_;
228 }
229
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800230 inline testing::NiceMock<MockPrefs>* mock_powerwash_safe_prefs() {
Gilad Arnold1f847232014-04-07 12:07:49 -0700231 CHECK(powerwash_safe_prefs_ == &mock_powerwash_safe_prefs_);
232 return &mock_powerwash_safe_prefs_;
233 }
234
235 inline testing::NiceMock<MockPayloadState>* mock_payload_state() {
236 CHECK(payload_state_ == &mock_payload_state_);
237 return &mock_payload_state_;
238 }
239
Alex Deymo560ae1d2014-10-28 02:17:54 -0700240 inline testing::NiceMock<MockUpdateAttempter>* mock_update_attempter() {
Gilad Arnold1f847232014-04-07 12:07:49 -0700241 CHECK(update_attempter_ == &mock_update_attempter_);
242 return &mock_update_attempter_;
243 }
244
Alex Deymo560ae1d2014-10-28 02:17:54 -0700245 inline testing::NiceMock<MockOmahaRequestParams>* mock_request_params() {
246 CHECK(request_params_ == &mock_request_params_);
247 return &mock_request_params_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700248 }
249
250 inline testing::NiceMock<MockP2PManager>* mock_p2p_manager() {
251 CHECK(p2p_manager_ == &mock_p2p_manager_);
252 return &mock_p2p_manager_;
253 }
254
Alex Deymo63784a52014-05-28 10:46:14 -0700255 inline chromeos_update_manager::FakeUpdateManager* fake_update_manager() {
256 CHECK(update_manager_ == &fake_update_manager_);
257 return &fake_update_manager_;
David Zeuthen526cb582013-08-06 12:26:18 -0700258 }
259
Jay Srinivasan43488792012-06-19 00:25:31 -0700260 private:
Amin Hassani538bd592020-11-04 20:46:08 -0800261 // Don't allow for direct initialization of this class.
262 FakeSystemState();
263
Gilad Arnold1f847232014-04-07 12:07:49 -0700264 // Default mock/fake implementations (owned).
Amin Hassani90e9f192020-11-18 14:20:56 -0800265 chromeos_update_manager::FakeUpdateManager fake_update_manager_;
Alex Deymo763e7db2015-08-27 21:08:08 -0700266 FakeBootControl fake_boot_control_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700267 FakeClock fake_clock_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700268 FakeHardware fake_hardware_;
Amin Hassani90e9f192020-11-18 14:20:56 -0800269 FakePrefs fake_prefs_;
270 FakePrefs fake_powerwash_safe_prefs_;
271
272 testing::NiceMock<MockConnectionManager> mock_connection_manager_;
Tianjie Xu1f93d092017-10-09 12:13:29 -0700273 testing::NiceMock<MockMetricsReporter> mock_metrics_reporter_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800274 testing::NiceMock<MockPrefs> mock_prefs_;
275 testing::NiceMock<MockPrefs> mock_powerwash_safe_prefs_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800276 testing::NiceMock<MockPayloadState> mock_payload_state_;
Alex Deymo560ae1d2014-10-28 02:17:54 -0700277 testing::NiceMock<MockUpdateAttempter> mock_update_attempter_;
278 testing::NiceMock<MockOmahaRequestParams> mock_request_params_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700279 testing::NiceMock<MockP2PManager> mock_p2p_manager_;
Sen Jiangb8c6a8f2016-06-07 17:33:17 -0700280 testing::NiceMock<MockPowerManager> mock_power_manager_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700281
Gilad Arnold1f847232014-04-07 12:07:49 -0700282 // Pointers to objects that client code can override. They are initialized to
283 // the default implementations above.
Alex Deymo763e7db2015-08-27 21:08:08 -0700284 BootControlInterface* boot_control_{&fake_boot_control_};
David Zeuthenf413fe52013-04-22 14:04:39 -0700285 ClockInterface* clock_;
Alex Deymof6ee0162015-07-31 12:35:22 -0700286 ConnectionManagerInterface* connection_manager_;
Alex Deymo42432912013-07-12 20:21:15 -0700287 HardwareInterface* hardware_;
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700288 MetricsReporterInterface* metrics_reporter_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800289 PrefsInterface* prefs_;
Chris Sosaaa18e162013-06-20 13:20:30 -0700290 PrefsInterface* powerwash_safe_prefs_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700291 PayloadStateInterface* payload_state_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700292 UpdateAttempter* update_attempter_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700293 OmahaRequestParams* request_params_;
Alex Deymo94c06162014-03-21 20:34:46 -0700294 P2PManager* p2p_manager_;
Alex Deymo63784a52014-05-28 10:46:14 -0700295 chromeos_update_manager::UpdateManager* update_manager_;
Sen Jiangb8c6a8f2016-06-07 17:33:17 -0700296 PowerManagerInterface* power_manager_{&mock_power_manager_};
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800297 DlcServiceInterface* dlcservice_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700298
299 // Other object pointers (not preinitialized).
300 const policy::DevicePolicy* device_policy_;
301
302 // Other data members.
Gilad Arnold1f847232014-04-07 12:07:49 -0700303 bool fake_system_rebooted_;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700304};
305
Alex Deymo94c06162014-03-21 20:34:46 -0700306} // namespace chromeos_update_engine
Jay Srinivasan08fce042012-06-07 16:31:01 -0700307
Amin Hassaniec7bc112020-10-29 16:47:58 -0700308#endif // UPDATE_ENGINE_CROS_FAKE_SYSTEM_STATE_H_