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