blob: 6604534c27b9284989b4e5fd03cf09662c075fb1 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2013 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//
Alex Deymo42432912013-07-12 20:21:15 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_
18#define UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_
Alex Deymo42432912013-07-12 20:21:15 -070019
Don Garrett83692e42013-11-08 10:11:30 -080020#include <map>
Alex Deymodf632d12014-04-29 20:04:36 -070021#include <string>
Don Garrett83692e42013-11-08 10:11:30 -080022
Alex Deymobccbc382014-04-03 13:38:55 -070023#include <base/time/time.h>
24
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/hardware_interface.h"
Alex Deymo42432912013-07-12 20:21:15 -070026
Alex Deymo42432912013-07-12 20:21:15 -070027namespace chromeos_update_engine {
28
29// Implements a fake hardware interface used for testing.
30class FakeHardware : public HardwareInterface {
31 public:
Alex Deymoebbe7ef2014-10-30 13:02:49 -070032 // Value used to signal that the powerwash_count file is not present. When
33 // this value is used in SetPowerwashCount(), GetPowerwashCount() will return
34 // false.
35 static const int kPowerwashCountNotSet = -1;
36
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080037 // Default value for crossystem tpm_kernver.
38 static const int kMinKernelKeyVersion = 3;
39
Marton Hunyady99ced782018-05-08 12:59:50 +020040 // Default value for crossystem tpm_fwver.
41 static const int kMinFirmwareKeyVersion = 13;
42
Zentaro Kavanagh5d956152018-05-15 09:40:33 -070043 // Default value for crossystem kernel_max_rollforward. This value is the
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080044 // default for consumer devices and effectively means "unlimited rollforward
45 // is allowed", which is the same as the behavior prior to implementing
46 // roll forward prevention.
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -070047 static const int kKernelMaxRollforward = 0xfffffffe;
48
49 // Default value for crossystem firmware_max_rollforward. This value is the
50 // default for consumer devices and effectively means "unlimited rollforward
51 // is allowed", which is the same as the behavior prior to implementing
52 // roll forward prevention.
53 static const int kFirmwareMaxRollforward = 0xfffffffe;
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080054
Alex Deymo46a9aae2016-05-04 20:20:11 -070055 FakeHardware() = default;
Alex Deymo42432912013-07-12 20:21:15 -070056
57 // HardwareInterface methods.
Alex Vakulenko157fe302014-08-11 15:59:58 -070058 bool IsOfficialBuild() const override { return is_official_build_; }
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080059
Alex Vakulenko157fe302014-08-11 15:59:58 -070060 bool IsNormalBootMode() const override { return is_normal_boot_mode_; }
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080061
Sen Jiange67bb5b2016-06-20 15:53:56 -070062 bool AreDevFeaturesEnabled() const override {
63 return are_dev_features_enabled_;
64 }
65
Alex Deymo46a9aae2016-05-04 20:20:11 -070066 bool IsOOBEEnabled() const override { return is_oobe_enabled_; }
67
Alex Vakulenko157fe302014-08-11 15:59:58 -070068 bool IsOOBEComplete(base::Time* out_time_of_oobe) const override {
Alex Deymobccbc382014-04-03 13:38:55 -070069 if (out_time_of_oobe != nullptr)
70 *out_time_of_oobe = oobe_timestamp_;
71 return is_oobe_complete_;
72 }
73
Alex Vakulenko157fe302014-08-11 15:59:58 -070074 std::string GetHardwareClass() const override { return hardware_class_; }
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080075
Alex Vakulenko157fe302014-08-11 15:59:58 -070076 std::string GetFirmwareVersion() const override { return firmware_version_; }
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080077
Alex Vakulenko157fe302014-08-11 15:59:58 -070078 std::string GetECVersion() const override { return ec_version_; }
Alex Deymo42432912013-07-12 20:21:15 -070079
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -040080 std::string GetDeviceRequisition() const override {
81 return device_requisition_;
82 }
83
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080084 int GetMinKernelKeyVersion() const override {
85 return min_kernel_key_version_;
86 }
87
Marton Hunyady99ced782018-05-08 12:59:50 +020088 int GetMinFirmwareKeyVersion() const override {
89 return min_firmware_key_version_;
90 }
91
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -070092 int GetMaxFirmwareKeyRollforward() const override {
93 return firmware_max_rollforward_;
94 }
95
96 bool SetMaxFirmwareKeyRollforward(int firmware_max_rollforward) override {
97 if (GetMaxFirmwareKeyRollforward() == -1)
98 return false;
99
100 firmware_max_rollforward_ = firmware_max_rollforward;
101 return true;
102 }
103
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700104 bool SetMaxKernelKeyRollforward(int kernel_max_rollforward) override {
105 kernel_max_rollforward_ = kernel_max_rollforward;
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800106 return true;
107 }
108
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700109 int GetPowerwashCount() const override { return powerwash_count_; }
110
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800111 bool SchedulePowerwash(bool save_rollback_data) override {
Alex Deymofb905d92016-06-03 19:26:58 -0700112 powerwash_scheduled_ = true;
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800113 save_rollback_data_ = save_rollback_data;
Alex Deymofb905d92016-06-03 19:26:58 -0700114 return true;
115 }
116
117 bool CancelPowerwash() override {
118 powerwash_scheduled_ = false;
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800119 save_rollback_data_ = false;
Alex Deymofb905d92016-06-03 19:26:58 -0700120 return true;
121 }
122
123 bool IsPowerwashScheduled() { return powerwash_scheduled_; }
124
Alex Deymodd132f32015-09-14 19:12:07 -0700125 bool GetNonVolatileDirectory(base::FilePath* path) const override {
126 return false;
127 }
128
129 bool GetPowerwashSafeDirectory(base::FilePath* path) const override {
130 return false;
131 }
132
Sen Jiang5011df62017-06-28 17:13:19 -0700133 int64_t GetBuildTimestamp() const override { return build_timestamp_; }
134
Amin Hassani1677e812017-06-21 13:36:36 -0700135 bool GetFirstActiveOmahaPingSent() const override {
136 return first_active_omaha_ping_sent_;
137 }
138
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700139 bool SetFirstActiveOmahaPingSent() override {
Amin Hassani1677e812017-06-21 13:36:36 -0700140 first_active_omaha_ping_sent_ = true;
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700141 return true;
Amin Hassani1677e812017-06-21 13:36:36 -0700142 }
143
Alex Deymo42432912013-07-12 20:21:15 -0700144 // Setters
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700145 void SetIsOfficialBuild(bool is_official_build) {
146 is_official_build_ = is_official_build;
147 }
148
149 void SetIsNormalBootMode(bool is_normal_boot_mode) {
150 is_normal_boot_mode_ = is_normal_boot_mode;
151 }
152
Sen Jiange67bb5b2016-06-20 15:53:56 -0700153 void SetAreDevFeaturesEnabled(bool are_dev_features_enabled) {
154 are_dev_features_enabled_ = are_dev_features_enabled;
155 }
156
Alex Deymo46a9aae2016-05-04 20:20:11 -0700157 // Sets the SetIsOOBEEnabled to |is_oobe_enabled|.
158 void SetIsOOBEEnabled(bool is_oobe_enabled) {
159 is_oobe_enabled_ = is_oobe_enabled;
160 }
161
Alex Deymobccbc382014-04-03 13:38:55 -0700162 // Sets the IsOOBEComplete to True with the given timestamp.
163 void SetIsOOBEComplete(base::Time oobe_timestamp) {
164 is_oobe_complete_ = true;
165 oobe_timestamp_ = oobe_timestamp;
166 }
167
Amin Hassanib2689592019-01-13 17:04:28 -0800168 void UnsetIsOOBEComplete() { is_oobe_complete_ = false; }
Alex Deymobccbc382014-04-03 13:38:55 -0700169
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -0700170 void SetHardwareClass(const std::string& hardware_class) {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700171 hardware_class_ = hardware_class;
172 }
173
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -0700174 void SetFirmwareVersion(const std::string& firmware_version) {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700175 firmware_version_ = firmware_version;
176 }
177
Amin Hassanib2689592019-01-13 17:04:28 -0800178 void SetECVersion(const std::string& ec_version) { ec_version_ = ec_version; }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700179
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -0400180 void SetDeviceRequisition(const std::string& requisition) {
181 device_requisition_ = requisition;
182 }
183
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800184 void SetMinKernelKeyVersion(int min_kernel_key_version) {
185 min_kernel_key_version_ = min_kernel_key_version;
186 }
187
Marton Hunyady99ced782018-05-08 12:59:50 +0200188 void SetMinFirmwareKeyVersion(int min_firmware_key_version) {
189 min_firmware_key_version_ = min_firmware_key_version;
190 }
191
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700192 void SetPowerwashCount(int powerwash_count) {
193 powerwash_count_ = powerwash_count;
194 }
195
Sen Jiang5011df62017-06-28 17:13:19 -0700196 void SetBuildTimestamp(int64_t build_timestamp) {
197 build_timestamp_ = build_timestamp;
198 }
199
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800200 // Getters to verify state.
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700201 int GetMaxKernelKeyRollforward() const { return kernel_max_rollforward_; }
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800202
Zentaro Kavanagh0418de32019-01-15 10:29:35 -0800203 bool GetIsRollbackPowerwashScheduled() const {
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800204 return powerwash_scheduled_ && save_rollback_data_;
Zentaro Kavanagh0418de32019-01-15 10:29:35 -0800205 }
206
Alex Deymo42432912013-07-12 20:21:15 -0700207 private:
Alex Deymo46a9aae2016-05-04 20:20:11 -0700208 bool is_official_build_{true};
209 bool is_normal_boot_mode_{true};
Sen Jiange67bb5b2016-06-20 15:53:56 -0700210 bool are_dev_features_enabled_{false};
Alex Deymo46a9aae2016-05-04 20:20:11 -0700211 bool is_oobe_enabled_{true};
212 bool is_oobe_complete_{true};
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800213 // Jan 20, 2007
214 base::Time oobe_timestamp_{base::Time::FromTimeT(1169280000)};
Alex Deymo46a9aae2016-05-04 20:20:11 -0700215 std::string hardware_class_{"Fake HWID BLAH-1234"};
216 std::string firmware_version_{"Fake Firmware v1.0.1"};
217 std::string ec_version_{"Fake EC v1.0a"};
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -0400218 std::string device_requisition_{"fake_requisition"};
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800219 int min_kernel_key_version_{kMinKernelKeyVersion};
Marton Hunyady99ced782018-05-08 12:59:50 +0200220 int min_firmware_key_version_{kMinFirmwareKeyVersion};
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -0700221 int kernel_max_rollforward_{kKernelMaxRollforward};
222 int firmware_max_rollforward_{kFirmwareMaxRollforward};
Alex Deymo46a9aae2016-05-04 20:20:11 -0700223 int powerwash_count_{kPowerwashCountNotSet};
Alex Deymofb905d92016-06-03 19:26:58 -0700224 bool powerwash_scheduled_{false};
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800225 bool save_rollback_data_{false};
Sen Jiang5011df62017-06-28 17:13:19 -0700226 int64_t build_timestamp_{0};
Amin Hassani1677e812017-06-21 13:36:36 -0700227 bool first_active_omaha_ping_sent_{false};
Alex Deymo42432912013-07-12 20:21:15 -0700228
229 DISALLOW_COPY_AND_ASSIGN(FakeHardware);
230};
231
232} // namespace chromeos_update_engine
233
Alex Deymo39910dc2015-11-09 17:04:30 -0800234#endif // UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_