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