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_CROS_FAKE_SYSTEM_STATE_H_ 18 #define UPDATE_ENGINE_CROS_FAKE_SYSTEM_STATE_H_ 19 20 #include <memory> 21 22 #include <base/logging.h> 23 #include <gmock/gmock.h> 24 #include <policy/mock_device_policy.h> 25 26 #include "metrics/metrics_library_mock.h" 27 #include "update_engine/common/fake_boot_control.h" 28 #include "update_engine/common/fake_clock.h" 29 #include "update_engine/common/fake_hardware.h" 30 #include "update_engine/common/fake_prefs.h" 31 #include "update_engine/common/mock_metrics_reporter.h" 32 #include "update_engine/common/mock_prefs.h" 33 #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" 40 #include "update_engine/update_manager/fake_update_manager.h" 41 42 namespace 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. 46 class FakeSystemState : public SystemState { 47 public: CreateInstance()48 static void CreateInstance() { 49 static std::unique_ptr<FakeSystemState> system_state; 50 system_state.reset(new FakeSystemState()); 51 g_pointer_ = system_state.get(); 52 } 53 Get()54 static FakeSystemState* Get() { 55 return reinterpret_cast<FakeSystemState*>(g_pointer_); 56 } 57 58 // 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. 61 boot_control()62 BootControlInterface* boot_control() override { return boot_control_; } 63 clock()64 inline ClockInterface* clock() override { return clock_; } 65 set_device_policy(const policy::DevicePolicy * device_policy)66 inline void set_device_policy( 67 const policy::DevicePolicy* device_policy) override { 68 device_policy_ = device_policy; 69 } 70 device_policy()71 inline const policy::DevicePolicy* device_policy() override { 72 return device_policy_; 73 } 74 connection_manager()75 inline ConnectionManagerInterface* connection_manager() override { 76 return connection_manager_; 77 } 78 hardware()79 inline HardwareInterface* hardware() override { return hardware_; } 80 metrics_reporter()81 inline MetricsReporterInterface* metrics_reporter() override { 82 CHECK(metrics_reporter_ != nullptr); 83 return metrics_reporter_; 84 } 85 prefs()86 inline PrefsInterface* prefs() override { return prefs_; } 87 powerwash_safe_prefs()88 inline PrefsInterface* powerwash_safe_prefs() override { 89 return powerwash_safe_prefs_; 90 } 91 payload_state()92 inline PayloadStateInterface* payload_state() override { 93 return payload_state_; 94 } 95 update_attempter()96 inline UpdateAttempter* update_attempter() override { 97 return update_attempter_; 98 } 99 request_params()100 inline OmahaRequestParams* request_params() override { 101 return request_params_; 102 } 103 p2p_manager()104 inline P2PManager* p2p_manager() override { return p2p_manager_; } 105 update_manager()106 inline chromeos_update_manager::UpdateManager* update_manager() override { 107 return update_manager_; 108 } 109 power_manager()110 inline PowerManagerInterface* power_manager() override { 111 return power_manager_; 112 } 113 dlcservice()114 inline DlcServiceInterface* dlcservice() override { return dlcservice_; } 115 system_rebooted()116 inline bool system_rebooted() override { return fake_system_rebooted_; } 117 118 // 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. 121 set_boot_control(BootControlInterface * boot_control)122 void set_boot_control(BootControlInterface* boot_control) { 123 boot_control_ = boot_control ? boot_control : &fake_boot_control_; 124 } 125 set_clock(ClockInterface * clock)126 inline void set_clock(ClockInterface* clock) { 127 clock_ = clock ? clock : &fake_clock_; 128 } 129 set_connection_manager(ConnectionManagerInterface * connection_manager)130 inline void set_connection_manager( 131 ConnectionManagerInterface* connection_manager) { 132 connection_manager_ = 133 (connection_manager ? connection_manager : &mock_connection_manager_); 134 } 135 set_hardware(HardwareInterface * hardware)136 inline void set_hardware(HardwareInterface* hardware) { 137 hardware_ = hardware ? hardware : &fake_hardware_; 138 } 139 set_metrics_reporter(MetricsReporterInterface * metrics_reporter)140 inline void set_metrics_reporter(MetricsReporterInterface* metrics_reporter) { 141 metrics_reporter_ = 142 metrics_reporter ? metrics_reporter : &mock_metrics_reporter_; 143 } 144 set_prefs(PrefsInterface * prefs)145 inline void set_prefs(PrefsInterface* prefs) { 146 prefs_ = prefs ? prefs : &mock_prefs_; 147 } 148 set_powerwash_safe_prefs(PrefsInterface * powerwash_safe_prefs)149 inline void set_powerwash_safe_prefs(PrefsInterface* powerwash_safe_prefs) { 150 powerwash_safe_prefs_ = 151 (powerwash_safe_prefs ? powerwash_safe_prefs 152 : &mock_powerwash_safe_prefs_); 153 } 154 set_payload_state(PayloadStateInterface * payload_state)155 inline void set_payload_state(PayloadStateInterface* payload_state) { 156 payload_state_ = payload_state ? payload_state : &mock_payload_state_; 157 } 158 set_update_attempter(UpdateAttempter * update_attempter)159 inline void set_update_attempter(UpdateAttempter* update_attempter) { 160 update_attempter_ = 161 (update_attempter ? update_attempter : &mock_update_attempter_); 162 } 163 set_request_params(OmahaRequestParams * request_params)164 inline void set_request_params(OmahaRequestParams* request_params) { 165 request_params_ = (request_params ? request_params : &mock_request_params_); 166 } 167 set_p2p_manager(P2PManager * p2p_manager)168 inline void set_p2p_manager(P2PManager* p2p_manager) { 169 p2p_manager_ = p2p_manager ? p2p_manager : &mock_p2p_manager_; 170 } 171 set_update_manager(chromeos_update_manager::UpdateManager * update_manager)172 inline void set_update_manager( 173 chromeos_update_manager::UpdateManager* update_manager) { 174 update_manager_ = update_manager ? update_manager : &fake_update_manager_; 175 } 176 set_system_rebooted(bool system_rebooted)177 inline void set_system_rebooted(bool system_rebooted) { 178 fake_system_rebooted_ = system_rebooted; 179 } 180 set_dlcservice(DlcServiceInterface * dlcservice)181 inline void set_dlcservice(DlcServiceInterface* dlcservice) { 182 dlcservice_ = dlcservice; 183 } 184 185 // 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 fake_boot_control()190 inline FakeBootControl* fake_boot_control() { 191 CHECK(boot_control_ == &fake_boot_control_); 192 return &fake_boot_control_; 193 } 194 fake_clock()195 inline FakeClock* fake_clock() { 196 CHECK(clock_ == &fake_clock_); 197 return &fake_clock_; 198 } 199 mock_connection_manager()200 inline testing::NiceMock<MockConnectionManager>* mock_connection_manager() { 201 CHECK(connection_manager_ == &mock_connection_manager_); 202 return &mock_connection_manager_; 203 } 204 fake_hardware()205 inline FakeHardware* fake_hardware() { 206 CHECK(hardware_ == &fake_hardware_); 207 return &fake_hardware_; 208 } 209 fake_prefs()210 inline FakePrefs* fake_prefs() { 211 CHECK(prefs_ == &fake_prefs_); 212 return &fake_prefs_; 213 } 214 fake_powerwash_safe_prefs()215 inline FakePrefs* fake_powerwash_safe_prefs() { 216 CHECK(powerwash_safe_prefs_ == &fake_powerwash_safe_prefs_); 217 return &fake_powerwash_safe_prefs_; 218 } 219 mock_metrics_reporter()220 inline testing::NiceMock<MockMetricsReporter>* mock_metrics_reporter() { 221 CHECK(metrics_reporter_ == &mock_metrics_reporter_); 222 return &mock_metrics_reporter_; 223 } 224 mock_prefs()225 inline testing::NiceMock<MockPrefs>* mock_prefs() { 226 CHECK(prefs_ == &mock_prefs_); 227 return &mock_prefs_; 228 } 229 mock_powerwash_safe_prefs()230 inline testing::NiceMock<MockPrefs>* mock_powerwash_safe_prefs() { 231 CHECK(powerwash_safe_prefs_ == &mock_powerwash_safe_prefs_); 232 return &mock_powerwash_safe_prefs_; 233 } 234 mock_payload_state()235 inline testing::NiceMock<MockPayloadState>* mock_payload_state() { 236 CHECK(payload_state_ == &mock_payload_state_); 237 return &mock_payload_state_; 238 } 239 mock_update_attempter()240 inline testing::NiceMock<MockUpdateAttempter>* mock_update_attempter() { 241 CHECK(update_attempter_ == &mock_update_attempter_); 242 return &mock_update_attempter_; 243 } 244 mock_request_params()245 inline testing::NiceMock<MockOmahaRequestParams>* mock_request_params() { 246 CHECK(request_params_ == &mock_request_params_); 247 return &mock_request_params_; 248 } 249 mock_p2p_manager()250 inline testing::NiceMock<MockP2PManager>* mock_p2p_manager() { 251 CHECK(p2p_manager_ == &mock_p2p_manager_); 252 return &mock_p2p_manager_; 253 } 254 fake_update_manager()255 inline chromeos_update_manager::FakeUpdateManager* fake_update_manager() { 256 CHECK(update_manager_ == &fake_update_manager_); 257 return &fake_update_manager_; 258 } 259 260 private: 261 // Don't allow for direct initialization of this class. 262 FakeSystemState(); 263 264 // Default mock/fake implementations (owned). 265 chromeos_update_manager::FakeUpdateManager fake_update_manager_; 266 FakeBootControl fake_boot_control_; 267 FakeClock fake_clock_; 268 FakeHardware fake_hardware_; 269 FakePrefs fake_prefs_; 270 FakePrefs fake_powerwash_safe_prefs_; 271 272 testing::NiceMock<MockConnectionManager> mock_connection_manager_; 273 testing::NiceMock<MockMetricsReporter> mock_metrics_reporter_; 274 testing::NiceMock<MockPrefs> mock_prefs_; 275 testing::NiceMock<MockPrefs> mock_powerwash_safe_prefs_; 276 testing::NiceMock<MockPayloadState> mock_payload_state_; 277 testing::NiceMock<MockUpdateAttempter> mock_update_attempter_; 278 testing::NiceMock<MockOmahaRequestParams> mock_request_params_; 279 testing::NiceMock<MockP2PManager> mock_p2p_manager_; 280 testing::NiceMock<MockPowerManager> mock_power_manager_; 281 282 // Pointers to objects that client code can override. They are initialized to 283 // the default implementations above. 284 BootControlInterface* boot_control_{&fake_boot_control_}; 285 ClockInterface* clock_; 286 ConnectionManagerInterface* connection_manager_; 287 HardwareInterface* hardware_; 288 MetricsReporterInterface* metrics_reporter_; 289 PrefsInterface* prefs_; 290 PrefsInterface* powerwash_safe_prefs_; 291 PayloadStateInterface* payload_state_; 292 UpdateAttempter* update_attempter_; 293 OmahaRequestParams* request_params_; 294 P2PManager* p2p_manager_; 295 chromeos_update_manager::UpdateManager* update_manager_; 296 PowerManagerInterface* power_manager_{&mock_power_manager_}; 297 DlcServiceInterface* dlcservice_; 298 299 // Other object pointers (not preinitialized). 300 const policy::DevicePolicy* device_policy_; 301 302 // Other data members. 303 bool fake_system_rebooted_; 304 }; 305 306 } // namespace chromeos_update_engine 307 308 #endif // UPDATE_ENGINE_CROS_FAKE_SYSTEM_STATE_H_ 309