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