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 #include <utility>
23 
24 #include <base/time/time.h>
25 
26 #include "update_engine/common/error_code.h"
27 #include "update_engine/common/hardware_interface.h"
28 #include "update_engine/common/utils.h"
29 
30 namespace chromeos_update_engine {
31 
32 // Implements a fake hardware interface used for testing.
33 class FakeHardware : public HardwareInterface {
34  public:
35   // Value used to signal that the powerwash_count file is not present. When
36   // this value is used in SetPowerwashCount(), GetPowerwashCount() will return
37   // false.
38   static const int kPowerwashCountNotSet = -1;
39 
40   // Default value for crossystem tpm_kernver.
41   static const int kMinKernelKeyVersion = 3;
42 
43   // Default value for crossystem tpm_fwver.
44   static const int kMinFirmwareKeyVersion = 13;
45 
46   // Default value for crossystem kernel_max_rollforward. This value is the
47   // default for consumer devices and effectively means "unlimited rollforward
48   // is allowed", which is the same as the behavior prior to implementing
49   // roll forward prevention.
50   static const int kKernelMaxRollforward = 0xfffffffe;
51 
52   // Default value for crossystem firmware_max_rollforward. This value is the
53   // default for consumer devices and effectively means "unlimited rollforward
54   // is allowed", which is the same as the behavior prior to implementing
55   // roll forward prevention.
56   static const int kFirmwareMaxRollforward = 0xfffffffe;
57 
58   FakeHardware() = default;
59 
60   // HardwareInterface methods.
IsOfficialBuild()61   bool IsOfficialBuild() const override { return is_official_build_; }
62 
IsNormalBootMode()63   bool IsNormalBootMode() const override { return is_normal_boot_mode_; }
64 
AreDevFeaturesEnabled()65   bool AreDevFeaturesEnabled() const override {
66     return are_dev_features_enabled_;
67   }
68 
IsOOBEEnabled()69   bool IsOOBEEnabled() const override { return is_oobe_enabled_; }
70 
IsOOBEComplete(base::Time * out_time_of_oobe)71   bool IsOOBEComplete(base::Time* out_time_of_oobe) const override {
72     if (out_time_of_oobe != nullptr)
73       *out_time_of_oobe = oobe_timestamp_;
74     return is_oobe_complete_;
75   }
76 
GetHardwareClass()77   std::string GetHardwareClass() const override { return hardware_class_; }
78 
GetDeviceRequisition()79   std::string GetDeviceRequisition() const override {
80     return device_requisition_;
81   }
82 
GetMinKernelKeyVersion()83   int GetMinKernelKeyVersion() const override {
84     return min_kernel_key_version_;
85   }
86 
GetMinFirmwareKeyVersion()87   int GetMinFirmwareKeyVersion() const override {
88     return min_firmware_key_version_;
89   }
90 
GetMaxFirmwareKeyRollforward()91   int GetMaxFirmwareKeyRollforward() const override {
92     return firmware_max_rollforward_;
93   }
94 
SetMaxFirmwareKeyRollforward(int firmware_max_rollforward)95   bool SetMaxFirmwareKeyRollforward(int firmware_max_rollforward) override {
96     if (GetMaxFirmwareKeyRollforward() == -1)
97       return false;
98 
99     firmware_max_rollforward_ = firmware_max_rollforward;
100     return true;
101   }
102 
SetMaxKernelKeyRollforward(int kernel_max_rollforward)103   bool SetMaxKernelKeyRollforward(int kernel_max_rollforward) override {
104     kernel_max_rollforward_ = kernel_max_rollforward;
105     return true;
106   }
107 
GetPowerwashCount()108   int GetPowerwashCount() const override { return powerwash_count_; }
109 
SchedulePowerwash(bool save_rollback_data)110   bool SchedulePowerwash(bool save_rollback_data) override {
111     powerwash_scheduled_ = true;
112     save_rollback_data_ = save_rollback_data;
113     return true;
114   }
115 
CancelPowerwash()116   bool CancelPowerwash() override {
117     powerwash_scheduled_ = false;
118     save_rollback_data_ = false;
119     return true;
120   }
121 
IsPowerwashScheduled()122   bool IsPowerwashScheduled() { return powerwash_scheduled_; }
123 
GetNonVolatileDirectory(base::FilePath * path)124   bool GetNonVolatileDirectory(base::FilePath* path) const override {
125     return false;
126   }
127 
GetPowerwashSafeDirectory(base::FilePath * path)128   bool GetPowerwashSafeDirectory(base::FilePath* path) const override {
129     return false;
130   }
131 
GetBuildTimestamp()132   int64_t GetBuildTimestamp() const override { return build_timestamp_; }
133 
AllowDowngrade()134   bool AllowDowngrade() const override { return false; }
135 
GetFirstActiveOmahaPingSent()136   bool GetFirstActiveOmahaPingSent() const override {
137     return first_active_omaha_ping_sent_;
138   }
139 
SetFirstActiveOmahaPingSent()140   bool SetFirstActiveOmahaPingSent() override {
141     first_active_omaha_ping_sent_ = true;
142     return true;
143   }
144 
145   // Setters
SetIsOfficialBuild(bool is_official_build)146   void SetIsOfficialBuild(bool is_official_build) {
147     is_official_build_ = is_official_build;
148   }
149 
SetIsNormalBootMode(bool is_normal_boot_mode)150   void SetIsNormalBootMode(bool is_normal_boot_mode) {
151     is_normal_boot_mode_ = is_normal_boot_mode;
152   }
153 
SetAreDevFeaturesEnabled(bool are_dev_features_enabled)154   void SetAreDevFeaturesEnabled(bool are_dev_features_enabled) {
155     are_dev_features_enabled_ = are_dev_features_enabled;
156   }
157 
158   // Sets the SetIsOOBEEnabled to |is_oobe_enabled|.
SetIsOOBEEnabled(bool is_oobe_enabled)159   void SetIsOOBEEnabled(bool is_oobe_enabled) {
160     is_oobe_enabled_ = is_oobe_enabled;
161   }
162 
163   // Sets the IsOOBEComplete to True with the given timestamp.
SetIsOOBEComplete(base::Time oobe_timestamp)164   void SetIsOOBEComplete(base::Time oobe_timestamp) {
165     is_oobe_complete_ = true;
166     oobe_timestamp_ = oobe_timestamp;
167   }
168 
UnsetIsOOBEComplete()169   void UnsetIsOOBEComplete() { is_oobe_complete_ = false; }
170 
SetHardwareClass(const std::string & hardware_class)171   void SetHardwareClass(const std::string& hardware_class) {
172     hardware_class_ = hardware_class;
173   }
174 
SetDeviceRequisition(const std::string & requisition)175   void SetDeviceRequisition(const std::string& requisition) {
176     device_requisition_ = requisition;
177   }
178 
SetMinKernelKeyVersion(int min_kernel_key_version)179   void SetMinKernelKeyVersion(int min_kernel_key_version) {
180     min_kernel_key_version_ = min_kernel_key_version;
181   }
182 
SetMinFirmwareKeyVersion(int min_firmware_key_version)183   void SetMinFirmwareKeyVersion(int min_firmware_key_version) {
184     min_firmware_key_version_ = min_firmware_key_version;
185   }
186 
SetPowerwashCount(int powerwash_count)187   void SetPowerwashCount(int powerwash_count) {
188     powerwash_count_ = powerwash_count;
189   }
190 
SetBuildTimestamp(int64_t build_timestamp)191   void SetBuildTimestamp(int64_t build_timestamp) {
192     build_timestamp_ = build_timestamp;
193   }
194 
SetWarmReset(bool warm_reset)195   void SetWarmReset(bool warm_reset) override { warm_reset_ = warm_reset; }
196 
SetVbmetaDigestForInactiveSlot(bool reset)197   void SetVbmetaDigestForInactiveSlot(bool reset) override {}
198 
199   // Getters to verify state.
GetMaxKernelKeyRollforward()200   int GetMaxKernelKeyRollforward() const { return kernel_max_rollforward_; }
201 
GetIsRollbackPowerwashScheduled()202   bool GetIsRollbackPowerwashScheduled() const {
203     return powerwash_scheduled_ && save_rollback_data_;
204   }
GetVersionForLogging(const std::string & partition_name)205   std::string GetVersionForLogging(
206       const std::string& partition_name) const override {
207     return partition_timestamps_[partition_name];
208   }
SetVersion(const std::string & partition_name,std::string timestamp)209   void SetVersion(const std::string& partition_name, std::string timestamp) {
210     partition_timestamps_[partition_name] = std::move(timestamp);
211   }
IsPartitionUpdateValid(const std::string & partition_name,const std::string & new_version)212   ErrorCode IsPartitionUpdateValid(
213       const std::string& partition_name,
214       const std::string& new_version) const override {
215     const auto old_version = GetVersionForLogging(partition_name);
216     return utils::IsTimestampNewer(old_version, new_version);
217   }
218 
GetPartitionMountOptions(const std::string & partition_name)219   const char* GetPartitionMountOptions(
220       const std::string& partition_name) const override {
221 #ifdef __ANDROID__
222     // TODO(allight): This matches the declaration in hardware_android.cc but
223     // ideally shouldn't be duplicated.
224     return "defcontext=u:object_r:postinstall_file:s0";
225 #else
226     return "";
227 #endif
228   }
229 
230  private:
231   bool is_official_build_{true};
232   bool is_normal_boot_mode_{true};
233   bool are_dev_features_enabled_{false};
234   bool is_oobe_enabled_{true};
235   bool is_oobe_complete_{true};
236   // Jan 20, 2007
237   base::Time oobe_timestamp_{base::Time::FromTimeT(1169280000)};
238   std::string hardware_class_{"Fake HWID BLAH-1234"};
239   std::string device_requisition_{"fake_requisition"};
240   int min_kernel_key_version_{kMinKernelKeyVersion};
241   int min_firmware_key_version_{kMinFirmwareKeyVersion};
242   int kernel_max_rollforward_{kKernelMaxRollforward};
243   int firmware_max_rollforward_{kFirmwareMaxRollforward};
244   int powerwash_count_{kPowerwashCountNotSet};
245   bool powerwash_scheduled_{false};
246   bool save_rollback_data_{false};
247   int64_t build_timestamp_{0};
248   bool first_active_omaha_ping_sent_{false};
249   bool warm_reset_{false};
250   mutable std::map<std::string, std::string> partition_timestamps_;
251 
252   DISALLOW_COPY_AND_ASSIGN(FakeHardware);
253 };
254 
255 }  // namespace chromeos_update_engine
256 
257 #endif  // UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_
258