Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_ |
| 6 | #define UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_ |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 7 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 8 | #include <base/logging.h> |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 10 | #include <policy/mock_device_policy.h> |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 11 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 12 | #include "metrics/metrics_library_mock.h" |
| 13 | #include "update_engine/fake_clock.h" |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 14 | #include "update_engine/fake_hardware.h" |
David Zeuthen | 1cac5aa | 2014-04-02 10:18:00 -0700 | [diff] [blame] | 15 | #include "update_engine/mock_connection_manager.h" |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 16 | #include "update_engine/mock_dbus_wrapper.h" |
Alex Deymo | 560ae1d | 2014-10-28 02:17:54 -0700 | [diff] [blame] | 17 | #include "update_engine/mock_omaha_request_params.h" |
David Zeuthen | 526cb58 | 2013-08-06 12:26:18 -0700 | [diff] [blame] | 18 | #include "update_engine/mock_p2p_manager.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 19 | #include "update_engine/mock_payload_state.h" |
Alex Deymo | 560ae1d | 2014-10-28 02:17:54 -0700 | [diff] [blame] | 20 | #include "update_engine/mock_update_attempter.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 21 | #include "update_engine/prefs_mock.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 22 | #include "update_engine/system_state.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 23 | #include "update_engine/update_manager/fake_update_manager.h" |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 24 | |
| 25 | namespace chromeos_update_engine { |
| 26 | |
| 27 | // Mock the SystemStateInterface so that we could lie that |
| 28 | // OOBE is completed even when there's no such marker file, etc. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 29 | class FakeSystemState : public SystemState { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 30 | public: |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 31 | FakeSystemState(); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 32 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 33 | // Base class overrides. All getters return the current implementation of |
| 34 | // various members, either the default (fake/mock) or the one set to override |
| 35 | // it by client code. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 36 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 37 | inline ClockInterface* clock() override { return clock_; } |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 38 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 39 | inline void set_device_policy( |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 40 | const policy::DevicePolicy* device_policy) override { |
| 41 | device_policy_ = device_policy; |
| 42 | } |
| 43 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 44 | inline const policy::DevicePolicy* device_policy() override { |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 45 | return device_policy_; |
| 46 | } |
| 47 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 48 | inline ConnectionManager* connection_manager() override { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 49 | return connection_manager_; |
| 50 | } |
| 51 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 52 | inline HardwareInterface* hardware() override { return hardware_; } |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 53 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 54 | inline MetricsLibraryInterface* metrics_lib() override { |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 55 | return metrics_lib_; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 58 | inline PrefsInterface* prefs() override { return prefs_; } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 59 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 60 | inline PrefsInterface* powerwash_safe_prefs() override { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 61 | return powerwash_safe_prefs_; |
| 62 | } |
| 63 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 64 | inline PayloadStateInterface* payload_state() override { |
David Zeuthen | 526cb58 | 2013-08-06 12:26:18 -0700 | [diff] [blame] | 65 | return payload_state_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 68 | inline UpdateAttempter* update_attempter() override { |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 69 | return update_attempter_; |
Gilad Arnold | 76b2b48 | 2014-04-01 13:32:43 -0700 | [diff] [blame] | 70 | } |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 71 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 72 | inline OmahaRequestParams* request_params() override { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 73 | return request_params_; |
| 74 | } |
| 75 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 76 | inline P2PManager* p2p_manager() override { return p2p_manager_; } |
David Zeuthen | 526cb58 | 2013-08-06 12:26:18 -0700 | [diff] [blame] | 77 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 78 | inline chromeos_update_manager::UpdateManager* update_manager() override { |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 79 | return update_manager_; |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 82 | inline bool system_rebooted() override { return fake_system_rebooted_; } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 83 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 84 | // Setters for the various members, can be used for overriding the default |
| 85 | // implementations. For convenience, setting to a null pointer will restore |
| 86 | // the default implementation. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 87 | |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 88 | inline void set_clock(ClockInterface* clock) { |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 89 | clock_ = clock ? clock : &fake_clock_; |
| 90 | } |
| 91 | |
| 92 | inline void set_connection_manager(ConnectionManager* connection_manager) { |
| 93 | connection_manager_ = (connection_manager ? connection_manager : |
| 94 | &mock_connection_manager_); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 97 | inline void set_hardware(HardwareInterface* hardware) { |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 98 | hardware_ = hardware ? hardware : &fake_hardware_; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 101 | inline void set_metrics_lib(MetricsLibraryInterface* metrics_lib) { |
| 102 | metrics_lib_ = metrics_lib ? metrics_lib : &mock_metrics_lib_; |
J. Richard Barnette | 4da2cc1 | 2013-10-28 16:11:10 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Gilad Arnold | bf7919b | 2013-01-08 13:07:37 -0800 | [diff] [blame] | 105 | inline void set_prefs(PrefsInterface* prefs) { |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 106 | prefs_ = prefs ? prefs : &mock_prefs_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 109 | inline void set_powerwash_safe_prefs(PrefsInterface* powerwash_safe_prefs) { |
| 110 | powerwash_safe_prefs_ = (powerwash_safe_prefs ? powerwash_safe_prefs : |
| 111 | &mock_powerwash_safe_prefs_); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 114 | inline void set_payload_state(PayloadStateInterface *payload_state) { |
| 115 | payload_state_ = payload_state ? payload_state : &mock_payload_state_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 118 | inline void set_update_attempter(UpdateAttempter* update_attempter) { |
| 119 | update_attempter_ = (update_attempter ? update_attempter : |
| 120 | &mock_update_attempter_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 121 | } |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 122 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 123 | inline void set_request_params(OmahaRequestParams* request_params) { |
| 124 | request_params_ = (request_params ? request_params : |
Alex Deymo | 560ae1d | 2014-10-28 02:17:54 -0700 | [diff] [blame] | 125 | &mock_request_params_); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 126 | } |
| 127 | |
David Zeuthen | 526cb58 | 2013-08-06 12:26:18 -0700 | [diff] [blame] | 128 | inline void set_p2p_manager(P2PManager *p2p_manager) { |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 129 | p2p_manager_ = p2p_manager ? p2p_manager : &mock_p2p_manager_; |
David Zeuthen | 526cb58 | 2013-08-06 12:26:18 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 132 | inline void set_update_manager( |
| 133 | chromeos_update_manager::UpdateManager *update_manager) { |
| 134 | update_manager_ = update_manager ? update_manager : &fake_update_manager_; |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 137 | inline void set_system_rebooted(bool system_rebooted) { |
| 138 | fake_system_rebooted_ = system_rebooted; |
| 139 | } |
| 140 | |
| 141 | // Getters for the built-in default implementations. These return the actual |
| 142 | // concrete type of each implementation. For additional safety, they will fail |
| 143 | // whenever the requested default was overridden by a different |
| 144 | // implementation. |
| 145 | |
| 146 | inline FakeClock* fake_clock() { |
| 147 | CHECK(clock_ == &fake_clock_); |
| 148 | return &fake_clock_; |
| 149 | } |
| 150 | |
| 151 | inline testing::NiceMock<MockConnectionManager>* mock_connection_manager() { |
| 152 | CHECK(connection_manager_ == &mock_connection_manager_); |
| 153 | return &mock_connection_manager_; |
| 154 | } |
| 155 | |
| 156 | inline FakeHardware* fake_hardware() { |
| 157 | CHECK(hardware_ == &fake_hardware_); |
| 158 | return &fake_hardware_; |
| 159 | } |
| 160 | |
| 161 | inline testing::NiceMock<MetricsLibraryMock>* mock_metrics_lib() { |
| 162 | CHECK(metrics_lib_ == &mock_metrics_lib_); |
| 163 | return &mock_metrics_lib_; |
| 164 | } |
| 165 | |
| 166 | inline testing::NiceMock<PrefsMock> *mock_prefs() { |
| 167 | CHECK(prefs_ == &mock_prefs_); |
| 168 | return &mock_prefs_; |
| 169 | } |
| 170 | |
| 171 | inline testing::NiceMock<PrefsMock> *mock_powerwash_safe_prefs() { |
| 172 | CHECK(powerwash_safe_prefs_ == &mock_powerwash_safe_prefs_); |
| 173 | return &mock_powerwash_safe_prefs_; |
| 174 | } |
| 175 | |
| 176 | inline testing::NiceMock<MockPayloadState>* mock_payload_state() { |
| 177 | CHECK(payload_state_ == &mock_payload_state_); |
| 178 | return &mock_payload_state_; |
| 179 | } |
| 180 | |
Alex Deymo | 560ae1d | 2014-10-28 02:17:54 -0700 | [diff] [blame] | 181 | inline testing::NiceMock<MockUpdateAttempter>* mock_update_attempter() { |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 182 | CHECK(update_attempter_ == &mock_update_attempter_); |
| 183 | return &mock_update_attempter_; |
| 184 | } |
| 185 | |
Alex Deymo | 560ae1d | 2014-10-28 02:17:54 -0700 | [diff] [blame] | 186 | inline testing::NiceMock<MockOmahaRequestParams>* mock_request_params() { |
| 187 | CHECK(request_params_ == &mock_request_params_); |
| 188 | return &mock_request_params_; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | inline testing::NiceMock<MockP2PManager>* mock_p2p_manager() { |
| 192 | CHECK(p2p_manager_ == &mock_p2p_manager_); |
| 193 | return &mock_p2p_manager_; |
| 194 | } |
| 195 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 196 | inline chromeos_update_manager::FakeUpdateManager* fake_update_manager() { |
| 197 | CHECK(update_manager_ == &fake_update_manager_); |
| 198 | return &fake_update_manager_; |
David Zeuthen | 526cb58 | 2013-08-06 12:26:18 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 201 | private: |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 202 | // Default mock/fake implementations (owned). |
| 203 | FakeClock fake_clock_; |
| 204 | testing::NiceMock<MockConnectionManager> mock_connection_manager_; |
| 205 | FakeHardware fake_hardware_; |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 206 | testing::NiceMock<MetricsLibraryMock> mock_metrics_lib_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 207 | testing::NiceMock<PrefsMock> mock_prefs_; |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 208 | testing::NiceMock<PrefsMock> mock_powerwash_safe_prefs_; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 209 | testing::NiceMock<MockPayloadState> mock_payload_state_; |
Alex Deymo | 560ae1d | 2014-10-28 02:17:54 -0700 | [diff] [blame] | 210 | testing::NiceMock<MockUpdateAttempter> mock_update_attempter_; |
| 211 | testing::NiceMock<MockOmahaRequestParams> mock_request_params_; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 212 | testing::NiceMock<MockP2PManager> mock_p2p_manager_; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 213 | chromeos_update_manager::FakeUpdateManager fake_update_manager_; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 214 | |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 215 | // Pointers to objects that client code can override. They are initialized to |
| 216 | // the default implementations above. |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 217 | ClockInterface* clock_; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 218 | ConnectionManager* connection_manager_; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 219 | HardwareInterface* hardware_; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 220 | MetricsLibraryInterface* metrics_lib_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 221 | PrefsInterface* prefs_; |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 222 | PrefsInterface* powerwash_safe_prefs_; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 223 | PayloadStateInterface* payload_state_; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 224 | UpdateAttempter* update_attempter_; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 225 | OmahaRequestParams* request_params_; |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 226 | P2PManager* p2p_manager_; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 227 | chromeos_update_manager::UpdateManager* update_manager_; |
Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 228 | |
| 229 | // Other object pointers (not preinitialized). |
| 230 | const policy::DevicePolicy* device_policy_; |
| 231 | |
| 232 | // Other data members. |
| 233 | MockDBusWrapper dbus_; |
| 234 | bool fake_system_rebooted_; |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 237 | } // namespace chromeos_update_engine |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 238 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 239 | #endif // UPDATE_ENGINE_FAKE_SYSTEM_STATE_H_ |