• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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 //
16 
17 #ifndef UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_
18 #define UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_
19 
20 #include <map>
21 #include <string>
22 
23 #include <base/time/time.h>
24 
25 #include "update_engine/common/hardware_interface.h"
26 
27 namespace chromeos_update_engine {
28 
29 // Implements a fake hardware interface used for testing.
30 class FakeHardware : public HardwareInterface {
31  public:
32   // 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 
37   // Default value for crossystem tpm_kernver.
38   static const int kMinKernelKeyVersion = 3;
39 
40   // Default value for crossystem tpm_fwver.
41   static const int kMinFirmwareKeyVersion = 13;
42 
43   // Default value for crossystem kernel_max_rollforward. This value is the
44   // 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.
47   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;
54 
55   FakeHardware() = default;
56 
57   // HardwareInterface methods.
IsOfficialBuild()58   bool IsOfficialBuild() const override { return is_official_build_; }
59 
IsNormalBootMode()60   bool IsNormalBootMode() const override { return is_normal_boot_mode_; }
61 
AreDevFeaturesEnabled()62   bool AreDevFeaturesEnabled() const override {
63     return are_dev_features_enabled_;
64   }
65 
IsOOBEEnabled()66   bool IsOOBEEnabled() const override { return is_oobe_enabled_; }
67 
IsOOBEComplete(base::Time * out_time_of_oobe)68   bool IsOOBEComplete(base::Time* out_time_of_oobe) const override {
69     if (out_time_of_oobe != nullptr)
70       *out_time_of_oobe = oobe_timestamp_;
71     return is_oobe_complete_;
72   }
73 
GetHardwareClass()74   std::string GetHardwareClass() const override { return hardware_class_; }
75 
GetFirmwareVersion()76   std::string GetFirmwareVersion() const override { return firmware_version_; }
77 
GetECVersion()78   std::string GetECVersion() const override { return ec_version_; }
79 
GetMinKernelKeyVersion()80   int GetMinKernelKeyVersion() const override {
81     return min_kernel_key_version_;
82   }
83 
GetMinFirmwareKeyVersion()84   int GetMinFirmwareKeyVersion() const override {
85     return min_firmware_key_version_;
86   }
87 
GetMaxFirmwareKeyRollforward()88   int GetMaxFirmwareKeyRollforward() const override {
89     return firmware_max_rollforward_;
90   }
91 
SetMaxFirmwareKeyRollforward(int firmware_max_rollforward)92   bool SetMaxFirmwareKeyRollforward(int firmware_max_rollforward) override {
93     if (GetMaxFirmwareKeyRollforward() == -1)
94       return false;
95 
96     firmware_max_rollforward_ = firmware_max_rollforward;
97     return true;
98   }
99 
SetMaxKernelKeyRollforward(int kernel_max_rollforward)100   bool SetMaxKernelKeyRollforward(int kernel_max_rollforward) override {
101     kernel_max_rollforward_ = kernel_max_rollforward;
102     return true;
103   }
104 
GetPowerwashCount()105   int GetPowerwashCount() const override { return powerwash_count_; }
106 
SchedulePowerwash(bool is_rollback)107   bool SchedulePowerwash(bool is_rollback) override {
108     powerwash_scheduled_ = true;
109     is_rollback_powerwash_ = is_rollback;
110     return true;
111   }
112 
CancelPowerwash()113   bool CancelPowerwash() override {
114     powerwash_scheduled_ = false;
115     is_rollback_powerwash_ = false;
116     return true;
117   }
118 
IsPowerwashScheduled()119   bool IsPowerwashScheduled() { return powerwash_scheduled_; }
120 
GetNonVolatileDirectory(base::FilePath * path)121   bool GetNonVolatileDirectory(base::FilePath* path) const override {
122     return false;
123   }
124 
GetPowerwashSafeDirectory(base::FilePath * path)125   bool GetPowerwashSafeDirectory(base::FilePath* path) const override {
126     return false;
127   }
128 
GetBuildTimestamp()129   int64_t GetBuildTimestamp() const override { return build_timestamp_; }
130 
AllowDowngrade()131   bool AllowDowngrade() const override { return false; }
132 
GetFirstActiveOmahaPingSent()133   bool GetFirstActiveOmahaPingSent() const override {
134     return first_active_omaha_ping_sent_;
135   }
136 
SetFirstActiveOmahaPingSent()137   bool SetFirstActiveOmahaPingSent() override {
138     first_active_omaha_ping_sent_ = true;
139     return true;
140   }
141 
142   // Setters
SetIsOfficialBuild(bool is_official_build)143   void SetIsOfficialBuild(bool is_official_build) {
144     is_official_build_ = is_official_build;
145   }
146 
SetIsNormalBootMode(bool is_normal_boot_mode)147   void SetIsNormalBootMode(bool is_normal_boot_mode) {
148     is_normal_boot_mode_ = is_normal_boot_mode;
149   }
150 
SetAreDevFeaturesEnabled(bool are_dev_features_enabled)151   void SetAreDevFeaturesEnabled(bool are_dev_features_enabled) {
152     are_dev_features_enabled_ = are_dev_features_enabled;
153   }
154 
155   // Sets the SetIsOOBEEnabled to |is_oobe_enabled|.
SetIsOOBEEnabled(bool is_oobe_enabled)156   void SetIsOOBEEnabled(bool is_oobe_enabled) {
157     is_oobe_enabled_ = is_oobe_enabled;
158   }
159 
160   // Sets the IsOOBEComplete to True with the given timestamp.
SetIsOOBEComplete(base::Time oobe_timestamp)161   void SetIsOOBEComplete(base::Time oobe_timestamp) {
162     is_oobe_complete_ = true;
163     oobe_timestamp_ = oobe_timestamp;
164   }
165 
UnsetIsOOBEComplete()166   void UnsetIsOOBEComplete() { is_oobe_complete_ = false; }
167 
SetHardwareClass(const std::string & hardware_class)168   void SetHardwareClass(const std::string& hardware_class) {
169     hardware_class_ = hardware_class;
170   }
171 
SetFirmwareVersion(const std::string & firmware_version)172   void SetFirmwareVersion(const std::string& firmware_version) {
173     firmware_version_ = firmware_version;
174   }
175 
SetECVersion(const std::string & ec_version)176   void SetECVersion(const std::string& ec_version) { ec_version_ = ec_version; }
177 
SetMinKernelKeyVersion(int min_kernel_key_version)178   void SetMinKernelKeyVersion(int min_kernel_key_version) {
179     min_kernel_key_version_ = min_kernel_key_version;
180   }
181 
SetMinFirmwareKeyVersion(int min_firmware_key_version)182   void SetMinFirmwareKeyVersion(int min_firmware_key_version) {
183     min_firmware_key_version_ = min_firmware_key_version;
184   }
185 
SetPowerwashCount(int powerwash_count)186   void SetPowerwashCount(int powerwash_count) {
187     powerwash_count_ = powerwash_count;
188   }
189 
SetBuildTimestamp(int64_t build_timestamp)190   void SetBuildTimestamp(int64_t build_timestamp) {
191     build_timestamp_ = build_timestamp;
192   }
193 
SetWarmReset(bool warm_reset)194   void SetWarmReset(bool warm_reset) { warm_reset_ = warm_reset; }
195 
196   // Getters to verify state.
GetMaxKernelKeyRollforward()197   int GetMaxKernelKeyRollforward() const { return kernel_max_rollforward_; }
198 
GetIsRollbackPowerwashScheduled()199   bool GetIsRollbackPowerwashScheduled() const {
200     return powerwash_scheduled_ && is_rollback_powerwash_;
201   }
202 
203  private:
204   bool is_official_build_{true};
205   bool is_normal_boot_mode_{true};
206   bool are_dev_features_enabled_{false};
207   bool is_oobe_enabled_{true};
208   bool is_oobe_complete_{true};
209   // Jan 20, 2007
210   base::Time oobe_timestamp_{base::Time::FromTimeT(1169280000)};
211   std::string hardware_class_{"Fake HWID BLAH-1234"};
212   std::string firmware_version_{"Fake Firmware v1.0.1"};
213   std::string ec_version_{"Fake EC v1.0a"};
214   int min_kernel_key_version_{kMinKernelKeyVersion};
215   int min_firmware_key_version_{kMinFirmwareKeyVersion};
216   int kernel_max_rollforward_{kKernelMaxRollforward};
217   int firmware_max_rollforward_{kFirmwareMaxRollforward};
218   int powerwash_count_{kPowerwashCountNotSet};
219   bool powerwash_scheduled_{false};
220   bool is_rollback_powerwash_{false};
221   int64_t build_timestamp_{0};
222   bool first_active_omaha_ping_sent_{false};
223   bool warm_reset_{false};
224 
225   DISALLOW_COPY_AND_ASSIGN(FakeHardware);
226 };
227 
228 }  // namespace chromeos_update_engine
229 
230 #endif  // UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_
231