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