1 // 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 // 16 17 #ifndef UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_ 18 #define UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_ 19 20 #include <base/logging.h> 21 #include <gmock/gmock.h> 22 #include <policy/mock_device_policy.h> 23 24 #include "metrics/metrics_library_mock.h" 25 #include "update_engine/common/fake_boot_control.h" 26 #include "update_engine/common/fake_clock.h" 27 #include "update_engine/common/fake_hardware.h" 28 #include "update_engine/common/mock_prefs.h" 29 #include "update_engine/mock_connection_manager.h" 30 #include "update_engine/mock_metrics_reporter.h" 31 #include "update_engine/mock_omaha_request_params.h" 32 #include "update_engine/mock_p2p_manager.h" 33 #include "update_engine/mock_payload_state.h" 34 #include "update_engine/mock_power_manager.h" 35 #include "update_engine/mock_update_attempter.h" 36 #include "update_engine/system_state.h" 37 #include "update_engine/update_manager/fake_update_manager.h" 38 39 namespace 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. 43 class FakeSystemState : public SystemState { 44 public: 45 FakeSystemState(); 46 47 // Base class overrides. All getters return the current implementation of 48 // various members, either the default (fake/mock) or the one set to override 49 // it by client code. 50 boot_control()51 BootControlInterface* boot_control() override { return boot_control_; } 52 clock()53 inline ClockInterface* clock() override { return clock_; } 54 set_device_policy(const policy::DevicePolicy * device_policy)55 inline void set_device_policy( 56 const policy::DevicePolicy* device_policy) override { 57 device_policy_ = device_policy; 58 } 59 device_policy()60 inline const policy::DevicePolicy* device_policy() override { 61 return device_policy_; 62 } 63 connection_manager()64 inline ConnectionManagerInterface* connection_manager() override { 65 return connection_manager_; 66 } 67 hardware()68 inline HardwareInterface* hardware() override { return hardware_; } 69 metrics_reporter()70 inline MetricsReporterInterface* metrics_reporter() override { 71 CHECK(metrics_reporter_ != nullptr); 72 return metrics_reporter_; 73 } 74 prefs()75 inline PrefsInterface* prefs() override { return prefs_; } 76 powerwash_safe_prefs()77 inline PrefsInterface* powerwash_safe_prefs() override { 78 return powerwash_safe_prefs_; 79 } 80 payload_state()81 inline PayloadStateInterface* payload_state() override { 82 return payload_state_; 83 } 84 update_attempter()85 inline UpdateAttempter* update_attempter() override { 86 return update_attempter_; 87 } 88 request_params()89 inline OmahaRequestParams* request_params() override { 90 return request_params_; 91 } 92 p2p_manager()93 inline P2PManager* p2p_manager() override { return p2p_manager_; } 94 update_manager()95 inline chromeos_update_manager::UpdateManager* update_manager() override { 96 return update_manager_; 97 } 98 power_manager()99 inline PowerManagerInterface* power_manager() override { 100 return power_manager_; 101 } 102 system_rebooted()103 inline bool system_rebooted() override { return fake_system_rebooted_; } 104 105 // Setters for the various members, can be used for overriding the default 106 // implementations. For convenience, setting to a null pointer will restore 107 // the default implementation. 108 set_boot_control(BootControlInterface * boot_control)109 void set_boot_control(BootControlInterface* boot_control) { 110 boot_control_ = boot_control ? boot_control : &fake_boot_control_; 111 } 112 set_clock(ClockInterface * clock)113 inline void set_clock(ClockInterface* clock) { 114 clock_ = clock ? clock : &fake_clock_; 115 } 116 set_connection_manager(ConnectionManagerInterface * connection_manager)117 inline void set_connection_manager( 118 ConnectionManagerInterface* connection_manager) { 119 connection_manager_ = (connection_manager ? connection_manager : 120 &mock_connection_manager_); 121 } 122 set_hardware(HardwareInterface * hardware)123 inline void set_hardware(HardwareInterface* hardware) { 124 hardware_ = hardware ? hardware : &fake_hardware_; 125 } 126 set_metrics_reporter(MetricsReporterInterface * metrics_reporter)127 inline void set_metrics_reporter(MetricsReporterInterface* metrics_reporter) { 128 metrics_reporter_ = 129 metrics_reporter ? metrics_reporter : &mock_metrics_reporter_; 130 } 131 set_prefs(PrefsInterface * prefs)132 inline void set_prefs(PrefsInterface* prefs) { 133 prefs_ = prefs ? prefs : &mock_prefs_; 134 } 135 set_powerwash_safe_prefs(PrefsInterface * powerwash_safe_prefs)136 inline void set_powerwash_safe_prefs(PrefsInterface* powerwash_safe_prefs) { 137 powerwash_safe_prefs_ = (powerwash_safe_prefs ? powerwash_safe_prefs : 138 &mock_powerwash_safe_prefs_); 139 } 140 set_payload_state(PayloadStateInterface * payload_state)141 inline void set_payload_state(PayloadStateInterface *payload_state) { 142 payload_state_ = payload_state ? payload_state : &mock_payload_state_; 143 } 144 set_update_attempter(UpdateAttempter * update_attempter)145 inline void set_update_attempter(UpdateAttempter* update_attempter) { 146 update_attempter_ = (update_attempter ? update_attempter : 147 &mock_update_attempter_); 148 } 149 set_request_params(OmahaRequestParams * request_params)150 inline void set_request_params(OmahaRequestParams* request_params) { 151 request_params_ = (request_params ? request_params : 152 &mock_request_params_); 153 } 154 set_p2p_manager(P2PManager * p2p_manager)155 inline void set_p2p_manager(P2PManager *p2p_manager) { 156 p2p_manager_ = p2p_manager ? p2p_manager : &mock_p2p_manager_; 157 } 158 set_update_manager(chromeos_update_manager::UpdateManager * update_manager)159 inline void set_update_manager( 160 chromeos_update_manager::UpdateManager *update_manager) { 161 update_manager_ = update_manager ? update_manager : &fake_update_manager_; 162 } 163 set_system_rebooted(bool system_rebooted)164 inline void set_system_rebooted(bool system_rebooted) { 165 fake_system_rebooted_ = system_rebooted; 166 } 167 168 // Getters for the built-in default implementations. These return the actual 169 // concrete type of each implementation. For additional safety, they will fail 170 // whenever the requested default was overridden by a different 171 // implementation. 172 fake_boot_control()173 inline FakeBootControl* fake_boot_control() { 174 CHECK(boot_control_ == &fake_boot_control_); 175 return &fake_boot_control_; 176 } 177 fake_clock()178 inline FakeClock* fake_clock() { 179 CHECK(clock_ == &fake_clock_); 180 return &fake_clock_; 181 } 182 mock_connection_manager()183 inline testing::NiceMock<MockConnectionManager>* mock_connection_manager() { 184 CHECK(connection_manager_ == &mock_connection_manager_); 185 return &mock_connection_manager_; 186 } 187 fake_hardware()188 inline FakeHardware* fake_hardware() { 189 CHECK(hardware_ == &fake_hardware_); 190 return &fake_hardware_; 191 } 192 mock_metrics_reporter()193 inline testing::NiceMock<MockMetricsReporter>* mock_metrics_reporter() { 194 CHECK(metrics_reporter_ == &mock_metrics_reporter_); 195 return &mock_metrics_reporter_; 196 } 197 mock_prefs()198 inline testing::NiceMock<MockPrefs> *mock_prefs() { 199 CHECK(prefs_ == &mock_prefs_); 200 return &mock_prefs_; 201 } 202 mock_powerwash_safe_prefs()203 inline testing::NiceMock<MockPrefs> *mock_powerwash_safe_prefs() { 204 CHECK(powerwash_safe_prefs_ == &mock_powerwash_safe_prefs_); 205 return &mock_powerwash_safe_prefs_; 206 } 207 mock_payload_state()208 inline testing::NiceMock<MockPayloadState>* mock_payload_state() { 209 CHECK(payload_state_ == &mock_payload_state_); 210 return &mock_payload_state_; 211 } 212 mock_update_attempter()213 inline testing::NiceMock<MockUpdateAttempter>* mock_update_attempter() { 214 CHECK(update_attempter_ == &mock_update_attempter_); 215 return &mock_update_attempter_; 216 } 217 mock_request_params()218 inline testing::NiceMock<MockOmahaRequestParams>* mock_request_params() { 219 CHECK(request_params_ == &mock_request_params_); 220 return &mock_request_params_; 221 } 222 mock_p2p_manager()223 inline testing::NiceMock<MockP2PManager>* mock_p2p_manager() { 224 CHECK(p2p_manager_ == &mock_p2p_manager_); 225 return &mock_p2p_manager_; 226 } 227 fake_update_manager()228 inline chromeos_update_manager::FakeUpdateManager* fake_update_manager() { 229 CHECK(update_manager_ == &fake_update_manager_); 230 return &fake_update_manager_; 231 } 232 233 private: 234 // Default mock/fake implementations (owned). 235 FakeBootControl fake_boot_control_; 236 FakeClock fake_clock_; 237 testing::NiceMock<MockConnectionManager> mock_connection_manager_; 238 FakeHardware fake_hardware_; 239 testing::NiceMock<MockMetricsReporter> mock_metrics_reporter_; 240 testing::NiceMock<MockPrefs> mock_prefs_; 241 testing::NiceMock<MockPrefs> mock_powerwash_safe_prefs_; 242 testing::NiceMock<MockPayloadState> mock_payload_state_; 243 testing::NiceMock<MockUpdateAttempter> mock_update_attempter_; 244 testing::NiceMock<MockOmahaRequestParams> mock_request_params_; 245 testing::NiceMock<MockP2PManager> mock_p2p_manager_; 246 chromeos_update_manager::FakeUpdateManager fake_update_manager_; 247 testing::NiceMock<MockPowerManager> mock_power_manager_; 248 249 // Pointers to objects that client code can override. They are initialized to 250 // the default implementations above. 251 BootControlInterface* boot_control_{&fake_boot_control_}; 252 ClockInterface* clock_; 253 ConnectionManagerInterface* connection_manager_; 254 HardwareInterface* hardware_; 255 MetricsReporterInterface* metrics_reporter_; 256 PrefsInterface* prefs_; 257 PrefsInterface* powerwash_safe_prefs_; 258 PayloadStateInterface* payload_state_; 259 UpdateAttempter* update_attempter_; 260 OmahaRequestParams* request_params_; 261 P2PManager* p2p_manager_; 262 chromeos_update_manager::UpdateManager* update_manager_; 263 PowerManagerInterface* power_manager_{&mock_power_manager_}; 264 265 // Other object pointers (not preinitialized). 266 const policy::DevicePolicy* device_policy_; 267 268 // Other data members. 269 bool fake_system_rebooted_; 270 }; 271 272 } // namespace chromeos_update_engine 273 274 #endif // UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_ 275