Home | History | Annotate | Download | only in common
      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   FakeHardware() = default;
     38 
     39   // HardwareInterface methods.
     40   bool IsOfficialBuild() const override { return is_official_build_; }
     41 
     42   bool IsNormalBootMode() const override { return is_normal_boot_mode_; }
     43 
     44   bool AreDevFeaturesEnabled() const override {
     45     return are_dev_features_enabled_;
     46   }
     47 
     48   bool IsOOBEEnabled() const override { return is_oobe_enabled_; }
     49 
     50   bool IsOOBEComplete(base::Time* out_time_of_oobe) const override {
     51     if (out_time_of_oobe != nullptr)
     52       *out_time_of_oobe = oobe_timestamp_;
     53     return is_oobe_complete_;
     54   }
     55 
     56   std::string GetHardwareClass() const override { return hardware_class_; }
     57 
     58   std::string GetFirmwareVersion() const override { return firmware_version_; }
     59 
     60   std::string GetECVersion() const override { return ec_version_; }
     61 
     62   int GetPowerwashCount() const override { return powerwash_count_; }
     63 
     64   bool SchedulePowerwash() override {
     65     powerwash_scheduled_ = true;
     66     return true;
     67   }
     68 
     69   bool CancelPowerwash() override {
     70     powerwash_scheduled_ = false;
     71     return true;
     72   }
     73 
     74   bool IsPowerwashScheduled() { return powerwash_scheduled_; }
     75 
     76   bool GetNonVolatileDirectory(base::FilePath* path) const override {
     77     return false;
     78   }
     79 
     80   bool GetPowerwashSafeDirectory(base::FilePath* path) const override {
     81     return false;
     82   }
     83 
     84   int64_t GetBuildTimestamp() const override { return build_timestamp_; }
     85 
     86   bool GetFirstActiveOmahaPingSent() const override {
     87     return first_active_omaha_ping_sent_;
     88   }
     89 
     90   void SetFirstActiveOmahaPingSent() override {
     91     first_active_omaha_ping_sent_ = true;
     92   }
     93 
     94   // Setters
     95   void SetIsOfficialBuild(bool is_official_build) {
     96     is_official_build_ = is_official_build;
     97   }
     98 
     99   void SetIsNormalBootMode(bool is_normal_boot_mode) {
    100     is_normal_boot_mode_ = is_normal_boot_mode;
    101   }
    102 
    103   void SetAreDevFeaturesEnabled(bool are_dev_features_enabled) {
    104     are_dev_features_enabled_ = are_dev_features_enabled;
    105   }
    106 
    107   // Sets the SetIsOOBEEnabled to |is_oobe_enabled|.
    108   void SetIsOOBEEnabled(bool is_oobe_enabled) {
    109     is_oobe_enabled_ = is_oobe_enabled;
    110   }
    111 
    112   // Sets the IsOOBEComplete to True with the given timestamp.
    113   void SetIsOOBEComplete(base::Time oobe_timestamp) {
    114     is_oobe_complete_ = true;
    115     oobe_timestamp_ = oobe_timestamp;
    116   }
    117 
    118   void UnsetIsOOBEComplete() {
    119     is_oobe_complete_ = false;
    120   }
    121 
    122   void SetHardwareClass(const std::string& hardware_class) {
    123     hardware_class_ = hardware_class;
    124   }
    125 
    126   void SetFirmwareVersion(const std::string& firmware_version) {
    127     firmware_version_ = firmware_version;
    128   }
    129 
    130   void SetECVersion(const std::string& ec_version) {
    131     ec_version_ = ec_version;
    132   }
    133 
    134   void SetPowerwashCount(int powerwash_count) {
    135     powerwash_count_ = powerwash_count;
    136   }
    137 
    138   void SetBuildTimestamp(int64_t build_timestamp) {
    139     build_timestamp_ = build_timestamp;
    140   }
    141 
    142  private:
    143   bool is_official_build_{true};
    144   bool is_normal_boot_mode_{true};
    145   bool are_dev_features_enabled_{false};
    146   bool is_oobe_enabled_{true};
    147   bool is_oobe_complete_{true};
    148   base::Time oobe_timestamp_{base::Time::FromTimeT(1169280000)}; // Jan 20, 2007
    149   std::string hardware_class_{"Fake HWID BLAH-1234"};
    150   std::string firmware_version_{"Fake Firmware v1.0.1"};
    151   std::string ec_version_{"Fake EC v1.0a"};
    152   int powerwash_count_{kPowerwashCountNotSet};
    153   bool powerwash_scheduled_{false};
    154   int64_t build_timestamp_{0};
    155   bool first_active_omaha_ping_sent_{false};
    156 
    157   DISALLOW_COPY_AND_ASSIGN(FakeHardware);
    158 };
    159 
    160 }  // namespace chromeos_update_engine
    161 
    162 #endif  // UPDATE_ENGINE_COMMON_FAKE_HARDWARE_H_
    163