Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Permission is hereby granted, free of charge, to any person
      5  * obtaining a copy of this software and associated documentation
      6  * files (the "Software"), to deal in the Software without
      7  * restriction, including without limitation the rights to use, copy,
      8  * modify, merge, publish, distribute, sublicense, and/or sell copies
      9  * of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be
     13  * included in all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
     19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
     20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22  * SOFTWARE.
     23  */
     24 
     25 #ifndef FAKE_AVB_OPS_H_
     26 #define FAKE_AVB_OPS_H_
     27 
     28 #include <base/files/file_util.h>
     29 #include <map>
     30 #include <string>
     31 
     32 #include <libavb_ab/libavb_ab.h>
     33 #include <libavb_atx/libavb_atx.h>
     34 
     35 namespace avb {
     36 
     37 // A delegate interface for ops callbacks. This allows tests to override default
     38 // fake implementations.
     39 class FakeAvbOpsDelegate {
     40  public:
     41   virtual ~FakeAvbOpsDelegate() {}
     42   virtual AvbIOResult read_from_partition(const char* partition,
     43                                           int64_t offset,
     44                                           size_t num_bytes,
     45                                           void* buffer,
     46                                           size_t* out_num_read) = 0;
     47 
     48   virtual AvbIOResult write_to_partition(const char* partition,
     49                                          int64_t offset,
     50                                          size_t num_bytes,
     51                                          const void* buffer) = 0;
     52 
     53   virtual AvbIOResult validate_vbmeta_public_key(
     54       AvbOps* ops,
     55       const uint8_t* public_key_data,
     56       size_t public_key_length,
     57       const uint8_t* public_key_metadata,
     58       size_t public_key_metadata_length,
     59       bool* out_key_is_trusted) = 0;
     60 
     61   virtual AvbIOResult read_rollback_index(AvbOps* ops,
     62                                           size_t rollback_index_slot,
     63                                           uint64_t* out_rollback_index) = 0;
     64 
     65   virtual AvbIOResult write_rollback_index(AvbOps* ops,
     66                                            size_t rollback_index_slot,
     67                                            uint64_t rollback_index) = 0;
     68 
     69   virtual AvbIOResult read_is_device_unlocked(AvbOps* ops,
     70                                               bool* out_is_device_unlocked) = 0;
     71 
     72   virtual AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
     73                                                     const char* partition,
     74                                                     char* guid_buf,
     75                                                     size_t guid_buf_size) = 0;
     76 
     77   virtual AvbIOResult read_permanent_attributes(
     78       AvbAtxPermanentAttributes* attributes) = 0;
     79 
     80   virtual AvbIOResult read_permanent_attributes_hash(
     81       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) = 0;
     82 };
     83 
     84 // Provides fake implementations of AVB ops. All instances of this class must be
     85 // created on the same thread.
     86 class FakeAvbOps : public FakeAvbOpsDelegate {
     87  public:
     88   FakeAvbOps();
     89   virtual ~FakeAvbOps();
     90 
     91   static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) {
     92     return reinterpret_cast<FakeAvbOps*>(ops->user_data);
     93   }
     94   static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) {
     95     return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data);
     96   }
     97 
     98   AvbOps* avb_ops() {
     99     return &avb_ops_;
    100   }
    101 
    102   AvbABOps* avb_ab_ops() {
    103     return &avb_ab_ops_;
    104   }
    105 
    106   AvbAtxOps* avb_atx_ops() {
    107     return &avb_atx_ops_;
    108   }
    109 
    110   FakeAvbOpsDelegate* delegate() {
    111     return delegate_;
    112   }
    113 
    114   // Does not take ownership of |delegate|.
    115   void set_delegate(FakeAvbOpsDelegate* delegate) {
    116     delegate_ = delegate;
    117   }
    118 
    119   void set_partition_dir(const base::FilePath& partition_dir) {
    120     partition_dir_ = partition_dir;
    121   }
    122 
    123   void set_expected_public_key(const std::string& expected_public_key) {
    124     expected_public_key_ = expected_public_key;
    125   }
    126 
    127   void set_expected_public_key_metadata(
    128       const std::string& expected_public_key_metadata) {
    129     expected_public_key_metadata_ = expected_public_key_metadata;
    130   }
    131 
    132   void set_stored_rollback_indexes(
    133       const std::map<size_t, uint64_t>& stored_rollback_indexes) {
    134     stored_rollback_indexes_ = stored_rollback_indexes;
    135   }
    136 
    137   std::map<size_t, uint64_t> get_stored_rollback_indexes() {
    138     return stored_rollback_indexes_;
    139   }
    140 
    141   void set_stored_is_device_unlocked(bool stored_is_device_unlocked) {
    142     stored_is_device_unlocked_ = stored_is_device_unlocked;
    143   }
    144 
    145   void set_permanent_attributes(const AvbAtxPermanentAttributes& attributes) {
    146     permanent_attributes_ = attributes;
    147   }
    148 
    149   void set_permanent_attributes_hash(const std::string& hash) {
    150     permanent_attributes_hash_ = hash;
    151   }
    152 
    153   // FakeAvbOpsDelegate methods.
    154   AvbIOResult read_from_partition(const char* partition,
    155                                   int64_t offset,
    156                                   size_t num_bytes,
    157                                   void* buffer,
    158                                   size_t* out_num_read) override;
    159 
    160   AvbIOResult write_to_partition(const char* partition,
    161                                  int64_t offset,
    162                                  size_t num_bytes,
    163                                  const void* buffer) override;
    164 
    165   AvbIOResult validate_vbmeta_public_key(AvbOps* ops,
    166                                          const uint8_t* public_key_data,
    167                                          size_t public_key_length,
    168                                          const uint8_t* public_key_metadata,
    169                                          size_t public_key_metadata_length,
    170                                          bool* out_key_is_trusted) override;
    171 
    172   AvbIOResult read_rollback_index(AvbOps* ops,
    173                                   size_t rollback_index_location,
    174                                   uint64_t* out_rollback_index) override;
    175 
    176   AvbIOResult write_rollback_index(AvbOps* ops,
    177                                    size_t rollback_index_location,
    178                                    uint64_t rollback_index) override;
    179 
    180   AvbIOResult read_is_device_unlocked(AvbOps* ops,
    181                                       bool* out_is_device_unlocked) override;
    182 
    183   AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
    184                                             const char* partition,
    185                                             char* guid_buf,
    186                                             size_t guid_buf_size) override;
    187 
    188   AvbIOResult read_permanent_attributes(
    189       AvbAtxPermanentAttributes* attributes) override;
    190 
    191   AvbIOResult read_permanent_attributes_hash(
    192       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override;
    193 
    194  private:
    195   AvbOps avb_ops_;
    196   AvbABOps avb_ab_ops_;
    197   AvbAtxOps avb_atx_ops_;
    198 
    199   FakeAvbOpsDelegate* delegate_;
    200 
    201   base::FilePath partition_dir_;
    202 
    203   std::string expected_public_key_;
    204   std::string expected_public_key_metadata_;
    205 
    206   std::map<size_t, uint64_t> stored_rollback_indexes_;
    207 
    208   bool stored_is_device_unlocked_;
    209 
    210   AvbAtxPermanentAttributes permanent_attributes_;
    211   std::string permanent_attributes_hash_;
    212 };
    213 
    214 }  // namespace avb
    215 
    216 #endif /* FAKE_AVB_OPS_H_ */
    217