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 weave_service()87 inline WeaveServiceInterface* weave_service() override { return nullptr; } 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_lib(MetricsLibraryInterface * metrics_lib)127 inline void set_metrics_lib(MetricsLibraryInterface* metrics_lib) { 128 metrics_lib_ = metrics_lib ? metrics_lib : &mock_metrics_lib_; 129 } 130 set_prefs(PrefsInterface * prefs)131 inline void set_prefs(PrefsInterface* prefs) { 132 prefs_ = prefs ? prefs : &mock_prefs_; 133 } 134 set_powerwash_safe_prefs(PrefsInterface * powerwash_safe_prefs)135 inline void set_powerwash_safe_prefs(PrefsInterface* powerwash_safe_prefs) { 136 powerwash_safe_prefs_ = (powerwash_safe_prefs ? powerwash_safe_prefs : 137 &mock_powerwash_safe_prefs_); 138 } 139 set_payload_state(PayloadStateInterface * payload_state)140 inline void set_payload_state(PayloadStateInterface *payload_state) { 141 payload_state_ = payload_state ? payload_state : &mock_payload_state_; 142 } 143 set_update_attempter(UpdateAttempter * update_attempter)144 inline void set_update_attempter(UpdateAttempter* update_attempter) { 145 update_attempter_ = (update_attempter ? update_attempter : 146 &mock_update_attempter_); 147 } 148 set_request_params(OmahaRequestParams * request_params)149 inline void set_request_params(OmahaRequestParams* request_params) { 150 request_params_ = (request_params ? request_params : 151 &mock_request_params_); 152 } 153 set_p2p_manager(P2PManager * p2p_manager)154 inline void set_p2p_manager(P2PManager *p2p_manager) { 155 p2p_manager_ = p2p_manager ? p2p_manager : &mock_p2p_manager_; 156 } 157 set_update_manager(chromeos_update_manager::UpdateManager * update_manager)158 inline void set_update_manager( 159 chromeos_update_manager::UpdateManager *update_manager) { 160 update_manager_ = update_manager ? update_manager : &fake_update_manager_; 161 } 162 set_system_rebooted(bool system_rebooted)163 inline void set_system_rebooted(bool system_rebooted) { 164 fake_system_rebooted_ = system_rebooted; 165 } 166 167 // Getters for the built-in default implementations. These return the actual 168 // concrete type of each implementation. For additional safety, they will fail 169 // whenever the requested default was overridden by a different 170 // implementation. 171 fake_boot_control()172 inline FakeBootControl* fake_boot_control() { 173 CHECK(boot_control_ == &fake_boot_control_); 174 return &fake_boot_control_; 175 } 176 fake_clock()177 inline FakeClock* fake_clock() { 178 CHECK(clock_ == &fake_clock_); 179 return &fake_clock_; 180 } 181 mock_connection_manager()182 inline testing::NiceMock<MockConnectionManager>* mock_connection_manager() { 183 CHECK(connection_manager_ == &mock_connection_manager_); 184 return &mock_connection_manager_; 185 } 186 fake_hardware()187 inline FakeHardware* fake_hardware() { 188 CHECK(hardware_ == &fake_hardware_); 189 return &fake_hardware_; 190 } 191 mock_metrics_lib()192 inline testing::NiceMock<MetricsLibraryMock>* mock_metrics_lib() { 193 CHECK(metrics_lib_ == &mock_metrics_lib_); 194 return &mock_metrics_lib_; 195 } 196 mock_prefs()197 inline testing::NiceMock<MockPrefs> *mock_prefs() { 198 CHECK(prefs_ == &mock_prefs_); 199 return &mock_prefs_; 200 } 201 mock_powerwash_safe_prefs()202 inline testing::NiceMock<MockPrefs> *mock_powerwash_safe_prefs() { 203 CHECK(powerwash_safe_prefs_ == &mock_powerwash_safe_prefs_); 204 return &mock_powerwash_safe_prefs_; 205 } 206 mock_payload_state()207 inline testing::NiceMock<MockPayloadState>* mock_payload_state() { 208 CHECK(payload_state_ == &mock_payload_state_); 209 return &mock_payload_state_; 210 } 211 mock_update_attempter()212 inline testing::NiceMock<MockUpdateAttempter>* mock_update_attempter() { 213 CHECK(update_attempter_ == &mock_update_attempter_); 214 return &mock_update_attempter_; 215 } 216 mock_request_params()217 inline testing::NiceMock<MockOmahaRequestParams>* mock_request_params() { 218 CHECK(request_params_ == &mock_request_params_); 219 return &mock_request_params_; 220 } 221 mock_p2p_manager()222 inline testing::NiceMock<MockP2PManager>* mock_p2p_manager() { 223 CHECK(p2p_manager_ == &mock_p2p_manager_); 224 return &mock_p2p_manager_; 225 } 226 fake_update_manager()227 inline chromeos_update_manager::FakeUpdateManager* fake_update_manager() { 228 CHECK(update_manager_ == &fake_update_manager_); 229 return &fake_update_manager_; 230 } 231 232 private: 233 // Default mock/fake implementations (owned). 234 FakeBootControl fake_boot_control_; 235 FakeClock fake_clock_; 236 testing::NiceMock<MockConnectionManager> mock_connection_manager_; 237 FakeHardware fake_hardware_; 238 testing::NiceMock<MetricsLibraryMock> mock_metrics_lib_; 239 testing::NiceMock<MockPrefs> mock_prefs_; 240 testing::NiceMock<MockPrefs> mock_powerwash_safe_prefs_; 241 testing::NiceMock<MockPayloadState> mock_payload_state_; 242 testing::NiceMock<MockUpdateAttempter> mock_update_attempter_; 243 testing::NiceMock<MockOmahaRequestParams> mock_request_params_; 244 testing::NiceMock<MockP2PManager> mock_p2p_manager_; 245 chromeos_update_manager::FakeUpdateManager fake_update_manager_; 246 testing::NiceMock<MockPowerManager> mock_power_manager_; 247 248 // Pointers to objects that client code can override. They are initialized to 249 // the default implementations above. 250 BootControlInterface* boot_control_{&fake_boot_control_}; 251 ClockInterface* clock_; 252 ConnectionManagerInterface* connection_manager_; 253 HardwareInterface* hardware_; 254 MetricsLibraryInterface* metrics_lib_; 255 PrefsInterface* prefs_; 256 PrefsInterface* powerwash_safe_prefs_; 257 PayloadStateInterface* payload_state_; 258 UpdateAttempter* update_attempter_; 259 OmahaRequestParams* request_params_; 260 P2PManager* p2p_manager_; 261 chromeos_update_manager::UpdateManager* update_manager_; 262 PowerManagerInterface* power_manager_{&mock_power_manager_}; 263 264 // Other object pointers (not preinitialized). 265 const policy::DevicePolicy* device_policy_; 266 267 // Other data members. 268 bool fake_system_rebooted_; 269 }; 270 271 } // namespace chromeos_update_engine 272 273 #endif // UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_ 274