blob: b1d5952dab755aec061d15e70178b45d42775c7e [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
Gilad Arnold1f847232014-04-07 12:07:49 -070020#include <base/logging.h>
Jay Srinivasan08fce042012-06-07 16:31:01 -070021#include <gmock/gmock.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070022#include <policy/mock_device_policy.h>
Jay Srinivasanf0572052012-10-23 18:12:56 -070023
Gilad Arnold1f847232014-04-07 12:07:49 -070024#include "metrics/metrics_library_mock.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/fake_boot_control.h"
26#include "update_engine/common/fake_clock.h"
27#include "update_engine/common/fake_hardware.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070028#include "update_engine/common/mock_metrics_reporter.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/mock_prefs.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070030#include "update_engine/common/system_state.h"
31#include "update_engine/cros/mock_connection_manager.h"
32#include "update_engine/cros/mock_omaha_request_params.h"
33#include "update_engine/cros/mock_p2p_manager.h"
34#include "update_engine/cros/mock_payload_state.h"
35#include "update_engine/cros/mock_power_manager.h"
36#include "update_engine/cros/mock_update_attempter.h"
Alex Deymo63784a52014-05-28 10:46:14 -070037#include "update_engine/update_manager/fake_update_manager.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070038
39namespace chromeos_update_engine {
40
41// Mock the SystemStateInterface so that we could lie that
42// OOBE is completed even when there's no such marker file, etc.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070043class FakeSystemState : public SystemState {
Jay Srinivasan43488792012-06-19 00:25:31 -070044 public:
Amin Hassani538bd592020-11-04 20:46:08 -080045 static void CreateInstance() { g_instance_.reset(new FakeSystemState()); }
46
47 static FakeSystemState* Get() {
48 return reinterpret_cast<FakeSystemState*>(g_instance_.get());
49 }
Jay Srinivasan55f50c22013-01-10 19:24:35 -080050
Gilad Arnold1f847232014-04-07 12:07:49 -070051 // Base class overrides. All getters return the current implementation of
52 // various members, either the default (fake/mock) or the one set to override
53 // it by client code.
Jay Srinivasan43488792012-06-19 00:25:31 -070054
Alex Deymo763e7db2015-08-27 21:08:08 -070055 BootControlInterface* boot_control() override { return boot_control_; }
56
Alex Vakulenko157fe302014-08-11 15:59:58 -070057 inline ClockInterface* clock() override { return clock_; }
David Zeuthenf413fe52013-04-22 14:04:39 -070058
Alex Vakulenko157fe302014-08-11 15:59:58 -070059 inline void set_device_policy(
Gilad Arnold1f847232014-04-07 12:07:49 -070060 const policy::DevicePolicy* device_policy) override {
61 device_policy_ = device_policy;
62 }
63
Alex Vakulenko157fe302014-08-11 15:59:58 -070064 inline const policy::DevicePolicy* device_policy() override {
Gilad Arnold1f847232014-04-07 12:07:49 -070065 return device_policy_;
66 }
67
Alex Deymof6ee0162015-07-31 12:35:22 -070068 inline ConnectionManagerInterface* connection_manager() override {
Jay Srinivasan43488792012-06-19 00:25:31 -070069 return connection_manager_;
70 }
71
Alex Vakulenko157fe302014-08-11 15:59:58 -070072 inline HardwareInterface* hardware() override { return hardware_; }
Alex Deymo42432912013-07-12 20:21:15 -070073
Tianjie Xu282aa1f2017-09-05 13:42:45 -070074 inline MetricsReporterInterface* metrics_reporter() override {
75 CHECK(metrics_reporter_ != nullptr);
76 return metrics_reporter_;
Jay Srinivasanf0572052012-10-23 18:12:56 -070077 }
78
Alex Vakulenko157fe302014-08-11 15:59:58 -070079 inline PrefsInterface* prefs() override { return prefs_; }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080080
Alex Vakulenko157fe302014-08-11 15:59:58 -070081 inline PrefsInterface* powerwash_safe_prefs() override {
Chris Sosaaa18e162013-06-20 13:20:30 -070082 return powerwash_safe_prefs_;
83 }
84
Alex Vakulenko157fe302014-08-11 15:59:58 -070085 inline PayloadStateInterface* payload_state() override {
David Zeuthen526cb582013-08-06 12:26:18 -070086 return payload_state_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080087 }
88
Alex Vakulenko157fe302014-08-11 15:59:58 -070089 inline UpdateAttempter* update_attempter() override {
Gilad Arnold1f847232014-04-07 12:07:49 -070090 return update_attempter_;
Gilad Arnold76b2b482014-04-01 13:32:43 -070091 }
Jay Srinivasan55f50c22013-01-10 19:24:35 -080092
Alex Vakulenko157fe302014-08-11 15:59:58 -070093 inline OmahaRequestParams* request_params() override {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070094 return request_params_;
95 }
96
Alex Vakulenko157fe302014-08-11 15:59:58 -070097 inline P2PManager* p2p_manager() override { return p2p_manager_; }
David Zeuthen526cb582013-08-06 12:26:18 -070098
Alex Vakulenko157fe302014-08-11 15:59:58 -070099 inline chromeos_update_manager::UpdateManager* update_manager() override {
Alex Deymo63784a52014-05-28 10:46:14 -0700100 return update_manager_;
Alex Deymo94c06162014-03-21 20:34:46 -0700101 }
102
Sen Jiangb8c6a8f2016-06-07 17:33:17 -0700103 inline PowerManagerInterface* power_manager() override {
104 return power_manager_;
Alex Deymo30534502015-07-20 15:06:33 -0700105 }
106
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800107 inline DlcServiceInterface* dlcservice() override { return dlcservice_; }
108
Alex Vakulenko157fe302014-08-11 15:59:58 -0700109 inline bool system_rebooted() override { return fake_system_rebooted_; }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800110
Gilad Arnold1f847232014-04-07 12:07:49 -0700111 // Setters for the various members, can be used for overriding the default
112 // implementations. For convenience, setting to a null pointer will restore
113 // the default implementation.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800114
Alex Deymo763e7db2015-08-27 21:08:08 -0700115 void set_boot_control(BootControlInterface* boot_control) {
116 boot_control_ = boot_control ? boot_control : &fake_boot_control_;
117 }
118
David Zeuthenf413fe52013-04-22 14:04:39 -0700119 inline void set_clock(ClockInterface* clock) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700120 clock_ = clock ? clock : &fake_clock_;
121 }
122
Alex Deymof6ee0162015-07-31 12:35:22 -0700123 inline void set_connection_manager(
124 ConnectionManagerInterface* connection_manager) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800125 connection_manager_ =
126 (connection_manager ? connection_manager : &mock_connection_manager_);
David Zeuthenf413fe52013-04-22 14:04:39 -0700127 }
128
Alex Deymo42432912013-07-12 20:21:15 -0700129 inline void set_hardware(HardwareInterface* hardware) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700130 hardware_ = hardware ? hardware : &fake_hardware_;
Alex Deymo42432912013-07-12 20:21:15 -0700131 }
132
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700133 inline void set_metrics_reporter(MetricsReporterInterface* metrics_reporter) {
134 metrics_reporter_ =
135 metrics_reporter ? metrics_reporter : &mock_metrics_reporter_;
J. Richard Barnette4da2cc12013-10-28 16:11:10 -0700136 }
137
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800138 inline void set_prefs(PrefsInterface* prefs) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700139 prefs_ = prefs ? prefs : &mock_prefs_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800140 }
141
Gilad Arnold1f847232014-04-07 12:07:49 -0700142 inline void set_powerwash_safe_prefs(PrefsInterface* powerwash_safe_prefs) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800143 powerwash_safe_prefs_ =
144 (powerwash_safe_prefs ? powerwash_safe_prefs
145 : &mock_powerwash_safe_prefs_);
Chris Sosaaa18e162013-06-20 13:20:30 -0700146 }
147
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800148 inline void set_payload_state(PayloadStateInterface* payload_state) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700149 payload_state_ = payload_state ? payload_state : &mock_payload_state_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800150 }
151
Gilad Arnold1f847232014-04-07 12:07:49 -0700152 inline void set_update_attempter(UpdateAttempter* update_attempter) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800153 update_attempter_ =
154 (update_attempter ? update_attempter : &mock_update_attempter_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800155 }
Jay Srinivasanf0572052012-10-23 18:12:56 -0700156
Gilad Arnold1f847232014-04-07 12:07:49 -0700157 inline void set_request_params(OmahaRequestParams* request_params) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800158 request_params_ = (request_params ? request_params : &mock_request_params_);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700159 }
160
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800161 inline void set_p2p_manager(P2PManager* p2p_manager) {
Gilad Arnold1f847232014-04-07 12:07:49 -0700162 p2p_manager_ = p2p_manager ? p2p_manager : &mock_p2p_manager_;
David Zeuthen526cb582013-08-06 12:26:18 -0700163 }
164
Alex Deymo63784a52014-05-28 10:46:14 -0700165 inline void set_update_manager(
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800166 chromeos_update_manager::UpdateManager* update_manager) {
Alex Deymo63784a52014-05-28 10:46:14 -0700167 update_manager_ = update_manager ? update_manager : &fake_update_manager_;
Alex Deymo94c06162014-03-21 20:34:46 -0700168 }
169
Gilad Arnold1f847232014-04-07 12:07:49 -0700170 inline void set_system_rebooted(bool system_rebooted) {
171 fake_system_rebooted_ = system_rebooted;
172 }
173
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800174 inline void set_dlcservice(DlcServiceInterface* dlcservice) {
175 dlcservice_ = dlcservice;
176 }
177
Gilad Arnold1f847232014-04-07 12:07:49 -0700178 // Getters for the built-in default implementations. These return the actual
179 // concrete type of each implementation. For additional safety, they will fail
180 // whenever the requested default was overridden by a different
181 // implementation.
182
Alex Deymo763e7db2015-08-27 21:08:08 -0700183 inline FakeBootControl* fake_boot_control() {
184 CHECK(boot_control_ == &fake_boot_control_);
185 return &fake_boot_control_;
186 }
187
Gilad Arnold1f847232014-04-07 12:07:49 -0700188 inline FakeClock* fake_clock() {
189 CHECK(clock_ == &fake_clock_);
190 return &fake_clock_;
191 }
192
193 inline testing::NiceMock<MockConnectionManager>* mock_connection_manager() {
194 CHECK(connection_manager_ == &mock_connection_manager_);
195 return &mock_connection_manager_;
196 }
197
198 inline FakeHardware* fake_hardware() {
199 CHECK(hardware_ == &fake_hardware_);
200 return &fake_hardware_;
201 }
202
Tianjie Xu1f93d092017-10-09 12:13:29 -0700203 inline testing::NiceMock<MockMetricsReporter>* mock_metrics_reporter() {
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700204 CHECK(metrics_reporter_ == &mock_metrics_reporter_);
205 return &mock_metrics_reporter_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700206 }
207
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800208 inline testing::NiceMock<MockPrefs>* mock_prefs() {
Gilad Arnold1f847232014-04-07 12:07:49 -0700209 CHECK(prefs_ == &mock_prefs_);
210 return &mock_prefs_;
211 }
212
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800213 inline testing::NiceMock<MockPrefs>* mock_powerwash_safe_prefs() {
Gilad Arnold1f847232014-04-07 12:07:49 -0700214 CHECK(powerwash_safe_prefs_ == &mock_powerwash_safe_prefs_);
215 return &mock_powerwash_safe_prefs_;
216 }
217
218 inline testing::NiceMock<MockPayloadState>* mock_payload_state() {
219 CHECK(payload_state_ == &mock_payload_state_);
220 return &mock_payload_state_;
221 }
222
Alex Deymo560ae1d2014-10-28 02:17:54 -0700223 inline testing::NiceMock<MockUpdateAttempter>* mock_update_attempter() {
Gilad Arnold1f847232014-04-07 12:07:49 -0700224 CHECK(update_attempter_ == &mock_update_attempter_);
225 return &mock_update_attempter_;
226 }
227
Alex Deymo560ae1d2014-10-28 02:17:54 -0700228 inline testing::NiceMock<MockOmahaRequestParams>* mock_request_params() {
229 CHECK(request_params_ == &mock_request_params_);
230 return &mock_request_params_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700231 }
232
233 inline testing::NiceMock<MockP2PManager>* mock_p2p_manager() {
234 CHECK(p2p_manager_ == &mock_p2p_manager_);
235 return &mock_p2p_manager_;
236 }
237
Alex Deymo63784a52014-05-28 10:46:14 -0700238 inline chromeos_update_manager::FakeUpdateManager* fake_update_manager() {
239 CHECK(update_manager_ == &fake_update_manager_);
240 return &fake_update_manager_;
David Zeuthen526cb582013-08-06 12:26:18 -0700241 }
242
Jay Srinivasan43488792012-06-19 00:25:31 -0700243 private:
Amin Hassani538bd592020-11-04 20:46:08 -0800244 // Don't allow for direct initialization of this class.
245 FakeSystemState();
246
Gilad Arnold1f847232014-04-07 12:07:49 -0700247 // Default mock/fake implementations (owned).
Alex Deymo763e7db2015-08-27 21:08:08 -0700248 FakeBootControl fake_boot_control_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700249 FakeClock fake_clock_;
250 testing::NiceMock<MockConnectionManager> mock_connection_manager_;
251 FakeHardware fake_hardware_;
Tianjie Xu1f93d092017-10-09 12:13:29 -0700252 testing::NiceMock<MockMetricsReporter> mock_metrics_reporter_;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800253 testing::NiceMock<MockPrefs> mock_prefs_;
254 testing::NiceMock<MockPrefs> mock_powerwash_safe_prefs_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800255 testing::NiceMock<MockPayloadState> mock_payload_state_;
Alex Deymo560ae1d2014-10-28 02:17:54 -0700256 testing::NiceMock<MockUpdateAttempter> mock_update_attempter_;
257 testing::NiceMock<MockOmahaRequestParams> mock_request_params_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700258 testing::NiceMock<MockP2PManager> mock_p2p_manager_;
Alex Deymo63784a52014-05-28 10:46:14 -0700259 chromeos_update_manager::FakeUpdateManager fake_update_manager_;
Sen Jiangb8c6a8f2016-06-07 17:33:17 -0700260 testing::NiceMock<MockPowerManager> mock_power_manager_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700261
Gilad Arnold1f847232014-04-07 12:07:49 -0700262 // Pointers to objects that client code can override. They are initialized to
263 // the default implementations above.
Alex Deymo763e7db2015-08-27 21:08:08 -0700264 BootControlInterface* boot_control_{&fake_boot_control_};
David Zeuthenf413fe52013-04-22 14:04:39 -0700265 ClockInterface* clock_;
Alex Deymof6ee0162015-07-31 12:35:22 -0700266 ConnectionManagerInterface* connection_manager_;
Alex Deymo42432912013-07-12 20:21:15 -0700267 HardwareInterface* hardware_;
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700268 MetricsReporterInterface* metrics_reporter_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800269 PrefsInterface* prefs_;
Chris Sosaaa18e162013-06-20 13:20:30 -0700270 PrefsInterface* powerwash_safe_prefs_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700271 PayloadStateInterface* payload_state_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700272 UpdateAttempter* update_attempter_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700273 OmahaRequestParams* request_params_;
Alex Deymo94c06162014-03-21 20:34:46 -0700274 P2PManager* p2p_manager_;
Alex Deymo63784a52014-05-28 10:46:14 -0700275 chromeos_update_manager::UpdateManager* update_manager_;
Sen Jiangb8c6a8f2016-06-07 17:33:17 -0700276 PowerManagerInterface* power_manager_{&mock_power_manager_};
Xiaochu Liu8ba486f2018-11-06 11:14:10 -0800277 DlcServiceInterface* dlcservice_;
Gilad Arnold1f847232014-04-07 12:07:49 -0700278
279 // Other object pointers (not preinitialized).
280 const policy::DevicePolicy* device_policy_;
281
282 // Other data members.
Gilad Arnold1f847232014-04-07 12:07:49 -0700283 bool fake_system_rebooted_;
Jay Srinivasan08fce042012-06-07 16:31:01 -0700284};
285
Alex Deymo94c06162014-03-21 20:34:46 -0700286} // namespace chromeos_update_engine
Jay Srinivasan08fce042012-06-07 16:31:01 -0700287
Amin Hassaniec7bc112020-10-29 16:47:58 -0700288#endif // UPDATE_ENGINE_CROS_FAKE_SYSTEM_STATE_H_