• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <set>
31 #include <string>
32 
33 #include <libavb_ab/libavb_ab.h>
34 #include <libavb_atx/libavb_atx.h>
35 
36 namespace avb {
37 
38 // A delegate interface for ops callbacks. This allows tests to override default
39 // fake implementations.
40 class FakeAvbOpsDelegate {
41  public:
~FakeAvbOpsDelegate()42   virtual ~FakeAvbOpsDelegate() {}
43   virtual AvbIOResult read_from_partition(const char* partition,
44                                           int64_t offset,
45                                           size_t num_bytes,
46                                           void* buffer,
47                                           size_t* out_num_read) = 0;
48 
49   virtual AvbIOResult write_to_partition(const char* partition,
50                                          int64_t offset,
51                                          size_t num_bytes,
52                                          const void* buffer) = 0;
53 
54   virtual AvbIOResult validate_vbmeta_public_key(
55       AvbOps* ops,
56       const uint8_t* public_key_data,
57       size_t public_key_length,
58       const uint8_t* public_key_metadata,
59       size_t public_key_metadata_length,
60       bool* out_key_is_trusted) = 0;
61 
62   virtual AvbIOResult read_rollback_index(AvbOps* ops,
63                                           size_t rollback_index_slot,
64                                           uint64_t* out_rollback_index) = 0;
65 
66   virtual AvbIOResult write_rollback_index(AvbOps* ops,
67                                            size_t rollback_index_slot,
68                                            uint64_t rollback_index) = 0;
69 
70   virtual AvbIOResult read_is_device_unlocked(AvbOps* ops,
71                                               bool* out_is_device_unlocked) = 0;
72 
73   virtual AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
74                                                     const char* partition,
75                                                     char* guid_buf,
76                                                     size_t guid_buf_size) = 0;
77 
78   virtual AvbIOResult get_size_of_partition(AvbOps* ops,
79                                             const char* partition,
80                                             uint64_t* out_size) = 0;
81 
82   virtual AvbIOResult read_permanent_attributes(
83       AvbAtxPermanentAttributes* attributes) = 0;
84 
85   virtual AvbIOResult read_permanent_attributes_hash(
86       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) = 0;
87 };
88 
89 // Provides fake implementations of AVB ops. All instances of this class must be
90 // created on the same thread.
91 class FakeAvbOps : public FakeAvbOpsDelegate {
92  public:
93   FakeAvbOps();
94   virtual ~FakeAvbOps();
95 
GetInstanceFromAvbOps(AvbOps * ops)96   static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) {
97     return reinterpret_cast<FakeAvbOps*>(ops->user_data);
98   }
GetInstanceFromAvbABOps(AvbABOps * ab_ops)99   static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) {
100     return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data);
101   }
102 
avb_ops()103   AvbOps* avb_ops() {
104     return &avb_ops_;
105   }
106 
avb_ab_ops()107   AvbABOps* avb_ab_ops() {
108     return &avb_ab_ops_;
109   }
110 
avb_atx_ops()111   AvbAtxOps* avb_atx_ops() {
112     return &avb_atx_ops_;
113   }
114 
delegate()115   FakeAvbOpsDelegate* delegate() {
116     return delegate_;
117   }
118 
119   // Does not take ownership of |delegate|.
set_delegate(FakeAvbOpsDelegate * delegate)120   void set_delegate(FakeAvbOpsDelegate* delegate) {
121     delegate_ = delegate;
122   }
123 
set_partition_dir(const base::FilePath & partition_dir)124   void set_partition_dir(const base::FilePath& partition_dir) {
125     partition_dir_ = partition_dir;
126   }
127 
set_expected_public_key(const std::string & expected_public_key)128   void set_expected_public_key(const std::string& expected_public_key) {
129     expected_public_key_ = expected_public_key;
130   }
131 
set_expected_public_key_metadata(const std::string & expected_public_key_metadata)132   void set_expected_public_key_metadata(
133       const std::string& expected_public_key_metadata) {
134     expected_public_key_metadata_ = expected_public_key_metadata;
135   }
136 
set_stored_rollback_indexes(const std::map<size_t,uint64_t> & stored_rollback_indexes)137   void set_stored_rollback_indexes(
138       const std::map<size_t, uint64_t>& stored_rollback_indexes) {
139     stored_rollback_indexes_ = stored_rollback_indexes;
140   }
141 
get_stored_rollback_indexes()142   std::map<size_t, uint64_t> get_stored_rollback_indexes() {
143     return stored_rollback_indexes_;
144   }
145 
set_stored_is_device_unlocked(bool stored_is_device_unlocked)146   void set_stored_is_device_unlocked(bool stored_is_device_unlocked) {
147     stored_is_device_unlocked_ = stored_is_device_unlocked;
148   }
149 
set_permanent_attributes(const AvbAtxPermanentAttributes & attributes)150   void set_permanent_attributes(const AvbAtxPermanentAttributes& attributes) {
151     permanent_attributes_ = attributes;
152   }
153 
set_permanent_attributes_hash(const std::string & hash)154   void set_permanent_attributes_hash(const std::string& hash) {
155     permanent_attributes_hash_ = hash;
156   }
157 
158   // Gets the partition names that were passed to the
159   // read_from_partition() operation.
160   std::set<std::string> get_partition_names_read_from();
161 
162   // FakeAvbOpsDelegate methods.
163   AvbIOResult read_from_partition(const char* partition,
164                                   int64_t offset,
165                                   size_t num_bytes,
166                                   void* buffer,
167                                   size_t* out_num_read) override;
168 
169   AvbIOResult write_to_partition(const char* partition,
170                                  int64_t offset,
171                                  size_t num_bytes,
172                                  const void* buffer) override;
173 
174   AvbIOResult validate_vbmeta_public_key(AvbOps* ops,
175                                          const uint8_t* public_key_data,
176                                          size_t public_key_length,
177                                          const uint8_t* public_key_metadata,
178                                          size_t public_key_metadata_length,
179                                          bool* out_key_is_trusted) override;
180 
181   AvbIOResult read_rollback_index(AvbOps* ops,
182                                   size_t rollback_index_location,
183                                   uint64_t* out_rollback_index) override;
184 
185   AvbIOResult write_rollback_index(AvbOps* ops,
186                                    size_t rollback_index_location,
187                                    uint64_t rollback_index) override;
188 
189   AvbIOResult read_is_device_unlocked(AvbOps* ops,
190                                       bool* out_is_device_unlocked) override;
191 
192   AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
193                                             const char* partition,
194                                             char* guid_buf,
195                                             size_t guid_buf_size) override;
196 
197   AvbIOResult get_size_of_partition(AvbOps* ops,
198                                     const char* partition,
199                                     uint64_t* out_size) override;
200 
201   AvbIOResult read_permanent_attributes(
202       AvbAtxPermanentAttributes* attributes) override;
203 
204   AvbIOResult read_permanent_attributes_hash(
205       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override;
206 
207  private:
208   AvbOps avb_ops_;
209   AvbABOps avb_ab_ops_;
210   AvbAtxOps avb_atx_ops_;
211 
212   FakeAvbOpsDelegate* delegate_;
213 
214   base::FilePath partition_dir_;
215 
216   std::string expected_public_key_;
217   std::string expected_public_key_metadata_;
218 
219   std::map<size_t, uint64_t> stored_rollback_indexes_;
220 
221   bool stored_is_device_unlocked_;
222 
223   AvbAtxPermanentAttributes permanent_attributes_;
224   std::string permanent_attributes_hash_;
225 
226   std::set<std::string> partition_names_read_from_;
227 };
228 
229 }  // namespace avb
230 
231 #endif /* FAKE_AVB_OPS_H_ */
232