• 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. For convenience, test fixtures can inherit
40 // FakeAvbOpsDelegateWithDefaults and only override as needed.
41 class FakeAvbOpsDelegate {
42  public:
~FakeAvbOpsDelegate()43   virtual ~FakeAvbOpsDelegate() {}
44   virtual AvbIOResult read_from_partition(const char* partition,
45                                           int64_t offset,
46                                           size_t num_bytes,
47                                           void* buffer,
48                                           size_t* out_num_read) = 0;
49 
50   virtual AvbIOResult get_preloaded_partition(
51       const char* partition,
52       size_t num_bytes,
53       uint8_t** out_pointer,
54       size_t* out_num_bytes_preloaded) = 0;
55 
56   virtual AvbIOResult write_to_partition(const char* partition,
57                                          int64_t offset,
58                                          size_t num_bytes,
59                                          const void* buffer) = 0;
60 
61   virtual AvbIOResult validate_vbmeta_public_key(
62       AvbOps* ops,
63       const uint8_t* public_key_data,
64       size_t public_key_length,
65       const uint8_t* public_key_metadata,
66       size_t public_key_metadata_length,
67       bool* out_key_is_trusted) = 0;
68 
69   virtual AvbIOResult read_rollback_index(AvbOps* ops,
70                                           size_t rollback_index_slot,
71                                           uint64_t* out_rollback_index) = 0;
72 
73   virtual AvbIOResult write_rollback_index(AvbOps* ops,
74                                            size_t rollback_index_slot,
75                                            uint64_t rollback_index) = 0;
76 
77   virtual AvbIOResult read_is_device_unlocked(AvbOps* ops,
78                                               bool* out_is_device_unlocked) = 0;
79 
80   virtual AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
81                                                     const char* partition,
82                                                     char* guid_buf,
83                                                     size_t guid_buf_size) = 0;
84 
85   virtual AvbIOResult get_size_of_partition(AvbOps* ops,
86                                             const char* partition,
87                                             uint64_t* out_size) = 0;
88 
89   virtual AvbIOResult read_persistent_value(const char* name,
90                                             size_t buffer_size,
91                                             uint8_t* out_buffer,
92                                             size_t* out_num_bytes_read) = 0;
93 
94   virtual AvbIOResult write_persistent_value(const char* name,
95                                              size_t value_size,
96                                              const uint8_t* value) = 0;
97 
98   virtual AvbIOResult read_permanent_attributes(
99       AvbAtxPermanentAttributes* attributes) = 0;
100 
101   virtual AvbIOResult read_permanent_attributes_hash(
102       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) = 0;
103 
104   virtual void set_key_version(size_t rollback_index_location,
105                                uint64_t key_version) = 0;
106 };
107 
108 // Provides fake implementations of AVB ops. All instances of this class must be
109 // created on the same thread.
110 class FakeAvbOps : public FakeAvbOpsDelegate {
111  public:
112   FakeAvbOps();
113   virtual ~FakeAvbOps();
114 
GetInstanceFromAvbOps(AvbOps * ops)115   static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) {
116     return reinterpret_cast<FakeAvbOps*>(ops->user_data);
117   }
GetInstanceFromAvbABOps(AvbABOps * ab_ops)118   static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) {
119     return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data);
120   }
121 
avb_ops()122   AvbOps* avb_ops() {
123     return &avb_ops_;
124   }
125 
avb_ab_ops()126   AvbABOps* avb_ab_ops() {
127     return &avb_ab_ops_;
128   }
129 
avb_atx_ops()130   AvbAtxOps* avb_atx_ops() {
131     return &avb_atx_ops_;
132   }
133 
delegate()134   FakeAvbOpsDelegate* delegate() {
135     return delegate_;
136   }
137 
138   // Does not take ownership of |delegate|.
set_delegate(FakeAvbOpsDelegate * delegate)139   void set_delegate(FakeAvbOpsDelegate* delegate) {
140     delegate_ = delegate;
141   }
142 
set_partition_dir(const base::FilePath & partition_dir)143   void set_partition_dir(const base::FilePath& partition_dir) {
144     partition_dir_ = partition_dir;
145   }
146 
set_expected_public_key(const std::string & expected_public_key)147   void set_expected_public_key(const std::string& expected_public_key) {
148     expected_public_key_ = expected_public_key;
149   }
150 
set_expected_public_key_metadata(const std::string & expected_public_key_metadata)151   void set_expected_public_key_metadata(
152       const std::string& expected_public_key_metadata) {
153     expected_public_key_metadata_ = expected_public_key_metadata;
154   }
155 
set_stored_rollback_indexes(const std::map<size_t,uint64_t> & stored_rollback_indexes)156   void set_stored_rollback_indexes(
157       const std::map<size_t, uint64_t>& stored_rollback_indexes) {
158     stored_rollback_indexes_ = stored_rollback_indexes;
159   }
160 
get_stored_rollback_indexes()161   std::map<size_t, uint64_t> get_stored_rollback_indexes() {
162     return stored_rollback_indexes_;
163   }
164 
get_verified_rollback_indexes()165   std::map<size_t, uint64_t> get_verified_rollback_indexes() {
166     return verified_rollback_indexes_;
167   }
168 
set_stored_is_device_unlocked(bool stored_is_device_unlocked)169   void set_stored_is_device_unlocked(bool stored_is_device_unlocked) {
170     stored_is_device_unlocked_ = stored_is_device_unlocked;
171   }
172 
set_permanent_attributes(const AvbAtxPermanentAttributes & attributes)173   void set_permanent_attributes(const AvbAtxPermanentAttributes& attributes) {
174     permanent_attributes_ = attributes;
175   }
176 
set_permanent_attributes_hash(const std::string & hash)177   void set_permanent_attributes_hash(const std::string& hash) {
178     permanent_attributes_hash_ = hash;
179   }
180 
181   void enable_get_preloaded_partition();
182 
183   bool preload_partition(const std::string& partition,
184                          const base::FilePath& path);
185 
186   // Gets the partition names that were passed to the
187   // read_from_partition() operation.
188   std::set<std::string> get_partition_names_read_from();
189 
190   // FakeAvbOpsDelegate methods.
191   AvbIOResult read_from_partition(const char* partition,
192                                   int64_t offset,
193                                   size_t num_bytes,
194                                   void* buffer,
195                                   size_t* out_num_read) override;
196 
197   AvbIOResult get_preloaded_partition(const char* partition,
198                                       size_t num_bytes,
199                                       uint8_t** out_pointer,
200                                       size_t* out_num_bytes_preloaded) override;
201 
202   AvbIOResult write_to_partition(const char* partition,
203                                  int64_t offset,
204                                  size_t num_bytes,
205                                  const void* buffer) override;
206 
207   AvbIOResult validate_vbmeta_public_key(AvbOps* ops,
208                                          const uint8_t* public_key_data,
209                                          size_t public_key_length,
210                                          const uint8_t* public_key_metadata,
211                                          size_t public_key_metadata_length,
212                                          bool* out_key_is_trusted) override;
213 
214   AvbIOResult read_rollback_index(AvbOps* ops,
215                                   size_t rollback_index_location,
216                                   uint64_t* out_rollback_index) override;
217 
218   AvbIOResult write_rollback_index(AvbOps* ops,
219                                    size_t rollback_index_location,
220                                    uint64_t rollback_index) override;
221 
222   AvbIOResult read_is_device_unlocked(AvbOps* ops,
223                                       bool* out_is_device_unlocked) override;
224 
225   AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
226                                             const char* partition,
227                                             char* guid_buf,
228                                             size_t guid_buf_size) override;
229 
230   AvbIOResult get_size_of_partition(AvbOps* ops,
231                                     const char* partition,
232                                     uint64_t* out_size) override;
233 
234   AvbIOResult read_persistent_value(const char* name,
235                                     size_t buffer_size,
236                                     uint8_t* out_buffer,
237                                     size_t* out_num_bytes_read) override;
238 
239   AvbIOResult write_persistent_value(const char* name,
240                                      size_t value_size,
241                                      const uint8_t* value) override;
242 
243   AvbIOResult read_permanent_attributes(
244       AvbAtxPermanentAttributes* attributes) override;
245 
246   AvbIOResult read_permanent_attributes_hash(
247       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override;
248 
249   void set_key_version(size_t rollback_index_location,
250                        uint64_t key_version) override;
251 
252  private:
253   AvbOps avb_ops_;
254   AvbABOps avb_ab_ops_;
255   AvbAtxOps avb_atx_ops_;
256 
257   FakeAvbOpsDelegate* delegate_;
258 
259   base::FilePath partition_dir_;
260 
261   std::string expected_public_key_;
262   std::string expected_public_key_metadata_;
263 
264   std::map<size_t, uint64_t> stored_rollback_indexes_;
265   std::map<size_t, uint64_t> verified_rollback_indexes_;
266 
267   bool stored_is_device_unlocked_;
268 
269   AvbAtxPermanentAttributes permanent_attributes_;
270   std::string permanent_attributes_hash_;
271 
272   std::set<std::string> partition_names_read_from_;
273   std::map<std::string, uint8_t*> preloaded_partitions_;
274 
275   std::map<std::string, std::string> stored_values_;
276 };
277 
278 // A delegate implementation that calls FakeAvbOps by default.
279 class FakeAvbOpsDelegateWithDefaults : public FakeAvbOpsDelegate {
280  public:
read_from_partition(const char * partition,int64_t offset,size_t num_bytes,void * buffer,size_t * out_num_read)281   AvbIOResult read_from_partition(const char* partition,
282                                   int64_t offset,
283                                   size_t num_bytes,
284                                   void* buffer,
285                                   size_t* out_num_read) override {
286     return ops_.read_from_partition(
287         partition, offset, num_bytes, buffer, out_num_read);
288   }
289 
get_preloaded_partition(const char * partition,size_t num_bytes,uint8_t ** out_pointer,size_t * out_num_bytes_preloaded)290   AvbIOResult get_preloaded_partition(
291       const char* partition,
292       size_t num_bytes,
293       uint8_t** out_pointer,
294       size_t* out_num_bytes_preloaded) override {
295     return ops_.get_preloaded_partition(
296         partition, num_bytes, out_pointer, out_num_bytes_preloaded);
297   }
298 
write_to_partition(const char * partition,int64_t offset,size_t num_bytes,const void * buffer)299   AvbIOResult write_to_partition(const char* partition,
300                                  int64_t offset,
301                                  size_t num_bytes,
302                                  const void* buffer) override {
303     return ops_.write_to_partition(partition, offset, num_bytes, buffer);
304   }
305 
validate_vbmeta_public_key(AvbOps * ops,const uint8_t * public_key_data,size_t public_key_length,const uint8_t * public_key_metadata,size_t public_key_metadata_length,bool * out_key_is_trusted)306   AvbIOResult validate_vbmeta_public_key(AvbOps* ops,
307                                          const uint8_t* public_key_data,
308                                          size_t public_key_length,
309                                          const uint8_t* public_key_metadata,
310                                          size_t public_key_metadata_length,
311                                          bool* out_key_is_trusted) override {
312     return ops_.validate_vbmeta_public_key(ops,
313                                            public_key_data,
314                                            public_key_length,
315                                            public_key_metadata,
316                                            public_key_metadata_length,
317                                            out_key_is_trusted);
318   }
319 
read_rollback_index(AvbOps * ops,size_t rollback_index_slot,uint64_t * out_rollback_index)320   AvbIOResult read_rollback_index(AvbOps* ops,
321                                   size_t rollback_index_slot,
322                                   uint64_t* out_rollback_index) override {
323     return ops_.read_rollback_index(
324         ops, rollback_index_slot, out_rollback_index);
325   }
326 
write_rollback_index(AvbOps * ops,size_t rollback_index_slot,uint64_t rollback_index)327   AvbIOResult write_rollback_index(AvbOps* ops,
328                                    size_t rollback_index_slot,
329                                    uint64_t rollback_index) override {
330     return ops_.write_rollback_index(ops, rollback_index_slot, rollback_index);
331   }
332 
read_is_device_unlocked(AvbOps * ops,bool * out_is_device_unlocked)333   AvbIOResult read_is_device_unlocked(AvbOps* ops,
334                                       bool* out_is_device_unlocked) override {
335     return ops_.read_is_device_unlocked(ops, out_is_device_unlocked);
336   }
337 
get_unique_guid_for_partition(AvbOps * ops,const char * partition,char * guid_buf,size_t guid_buf_size)338   AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
339                                             const char* partition,
340                                             char* guid_buf,
341                                             size_t guid_buf_size) override {
342     return ops_.get_unique_guid_for_partition(
343         ops, partition, guid_buf, guid_buf_size);
344   }
345 
get_size_of_partition(AvbOps * ops,const char * partition,uint64_t * out_size)346   AvbIOResult get_size_of_partition(AvbOps* ops,
347                                     const char* partition,
348                                     uint64_t* out_size) override {
349     return ops_.get_size_of_partition(ops, partition, out_size);
350   }
351 
read_persistent_value(const char * name,size_t buffer_size,uint8_t * out_buffer,size_t * out_num_bytes_read)352   AvbIOResult read_persistent_value(const char* name,
353                                     size_t buffer_size,
354                                     uint8_t* out_buffer,
355                                     size_t* out_num_bytes_read) override {
356     return ops_.read_persistent_value(
357         name, buffer_size, out_buffer, out_num_bytes_read);
358   }
359 
write_persistent_value(const char * name,size_t value_size,const uint8_t * value)360   AvbIOResult write_persistent_value(const char* name,
361                                      size_t value_size,
362                                      const uint8_t* value) override {
363     return ops_.write_persistent_value(name, value_size, value);
364   }
365 
read_permanent_attributes(AvbAtxPermanentAttributes * attributes)366   AvbIOResult read_permanent_attributes(
367       AvbAtxPermanentAttributes* attributes) override {
368     return ops_.read_permanent_attributes(attributes);
369   }
370 
read_permanent_attributes_hash(uint8_t hash[AVB_SHA256_DIGEST_SIZE])371   AvbIOResult read_permanent_attributes_hash(
372       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override {
373     return ops_.read_permanent_attributes_hash(hash);
374   }
375 
set_key_version(size_t rollback_index_location,uint64_t key_version)376   void set_key_version(size_t rollback_index_location,
377                        uint64_t key_version) override {
378     ops_.set_key_version(rollback_index_location, key_version);
379   }
380 
381  protected:
382   FakeAvbOps ops_;
383 };
384 
385 }  // namespace avb
386 
387 #endif /* FAKE_AVB_OPS_H_ */
388