blob: 0a7246d328dd7561c86aa58657b26208b72c1953 [file] [log] [blame]
David Zeuthen8b6973b2016-09-20 12:39:49 -04001/*
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>
Darren Krahn72d57902016-12-12 18:34:08 -080029#include <map>
David Zeuthen8b6973b2016-09-20 12:39:49 -040030#include <string>
David Zeuthen8b6973b2016-09-20 12:39:49 -040031
David Zeuthenbaf59e22016-11-14 15:39:43 -050032#include <libavb_ab/libavb_ab.h>
Darren Krahn147b08d2016-12-20 16:38:29 -080033#include <libavb_atx/libavb_atx.h>
David Zeuthen8b6973b2016-09-20 12:39:49 -040034
Darren Krahn72d57902016-12-12 18:34:08 -080035namespace avb {
David Zeuthen8b6973b2016-09-20 12:39:49 -040036
Darren Krahn72d57902016-12-12 18:34:08 -080037// A delegate interface for ops callbacks. This allows tests to override default
38// fake implementations.
39class 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;
Darren Krahn147b08d2016-12-20 16:38:29 -080076
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;
Darren Krahn72d57902016-12-12 18:34:08 -080082};
83
84// Provides fake implementations of AVB ops. All instances of this class must be
85// created on the same thread.
86class FakeAvbOps : public FakeAvbOpsDelegate {
David Zeuthen8b6973b2016-09-20 12:39:49 -040087 public:
88 FakeAvbOps();
Darren Krahn72d57902016-12-12 18:34:08 -080089 virtual ~FakeAvbOps();
David Zeuthen8b6973b2016-09-20 12:39:49 -040090
Darren Krahn72d57902016-12-12 18:34:08 -080091 static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) {
David Zeuthen574ed992017-01-05 15:43:11 -050092 return reinterpret_cast<FakeAvbOps*>(ops->user_data);
Darren Krahn72d57902016-12-12 18:34:08 -080093 }
David Zeuthen574ed992017-01-05 15:43:11 -050094 static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) {
95 return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data);
Darren Krahn72d57902016-12-12 18:34:08 -080096 }
David Zeuthen8b6973b2016-09-20 12:39:49 -040097
David Zeuthen4b6a6342017-01-03 15:19:56 -050098 AvbOps* avb_ops() {
99 return &avb_ops_;
100 }
Darren Krahn72d57902016-12-12 18:34:08 -0800101
David Zeuthen4b6a6342017-01-03 15:19:56 -0500102 AvbABOps* avb_ab_ops() {
103 return &avb_ab_ops_;
104 }
Darren Krahn72d57902016-12-12 18:34:08 -0800105
Darren Krahn147b08d2016-12-20 16:38:29 -0800106 AvbAtxOps* avb_atx_ops() {
107 return &avb_atx_ops_;
108 }
109
Darren Krahn72d57902016-12-12 18:34:08 -0800110 FakeAvbOpsDelegate* delegate() {
111 return delegate_;
112 }
113
114 // Does not take ownership of |delegate|.
115 void set_delegate(FakeAvbOpsDelegate* delegate) {
116 delegate_ = delegate;
117 }
David Zeuthenbaf59e22016-11-14 15:39:43 -0500118
David Zeuthen8b6973b2016-09-20 12:39:49 -0400119 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
David Zeuthen18666ab2016-11-15 11:18:05 -0500127 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
David Zeuthen8b6973b2016-09-20 12:39:49 -0400132 void set_stored_rollback_indexes(
Darren Krahn72d57902016-12-12 18:34:08 -0800133 const std::map<size_t, uint64_t>& stored_rollback_indexes) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400134 stored_rollback_indexes_ = stored_rollback_indexes;
135 }
136
Darren Krahn72d57902016-12-12 18:34:08 -0800137 std::map<size_t, uint64_t> get_stored_rollback_indexes() {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400138 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
Darren Krahn147b08d2016-12-20 16:38:29 -0800145 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
Darren Krahn72d57902016-12-12 18:34:08 -0800153 // FakeAvbOpsDelegate methods.
David Zeuthen4b6a6342017-01-03 15:19:56 -0500154 AvbIOResult read_from_partition(const char* partition,
155 int64_t offset,
156 size_t num_bytes,
157 void* buffer,
Darren Krahn72d57902016-12-12 18:34:08 -0800158 size_t* out_num_read) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400159
David Zeuthen4b6a6342017-01-03 15:19:56 -0500160 AvbIOResult write_to_partition(const char* partition,
161 int64_t offset,
162 size_t num_bytes,
163 const void* buffer) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400164
David Zeuthen507752b2016-09-29 16:31:18 -0400165 AvbIOResult validate_vbmeta_public_key(AvbOps* ops,
166 const uint8_t* public_key_data,
167 size_t public_key_length,
David Zeuthen18666ab2016-11-15 11:18:05 -0500168 const uint8_t* public_key_metadata,
169 size_t public_key_metadata_length,
Darren Krahn72d57902016-12-12 18:34:08 -0800170 bool* out_key_is_trusted) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400171
David Zeuthen4b6a6342017-01-03 15:19:56 -0500172 AvbIOResult read_rollback_index(AvbOps* ops,
173 size_t rollback_index_location,
Darren Krahn72d57902016-12-12 18:34:08 -0800174 uint64_t* out_rollback_index) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400175
David Zeuthen4b6a6342017-01-03 15:19:56 -0500176 AvbIOResult write_rollback_index(AvbOps* ops,
177 size_t rollback_index_location,
Darren Krahn72d57902016-12-12 18:34:08 -0800178 uint64_t rollback_index) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400179
David Zeuthen507752b2016-09-29 16:31:18 -0400180 AvbIOResult read_is_device_unlocked(AvbOps* ops,
Darren Krahn72d57902016-12-12 18:34:08 -0800181 bool* out_is_device_unlocked) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400182
David Zeuthen4b6a6342017-01-03 15:19:56 -0500183 AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
184 const char* partition,
David Zeuthen507752b2016-09-29 16:31:18 -0400185 char* guid_buf,
Darren Krahn72d57902016-12-12 18:34:08 -0800186 size_t guid_buf_size) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400187
Darren Krahn147b08d2016-12-20 16:38:29 -0800188 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
David Zeuthen8b6973b2016-09-20 12:39:49 -0400194 private:
Darren Krahn72d57902016-12-12 18:34:08 -0800195 AvbOps avb_ops_;
196 AvbABOps avb_ab_ops_;
Darren Krahn147b08d2016-12-20 16:38:29 -0800197 AvbAtxOps avb_atx_ops_;
Darren Krahn72d57902016-12-12 18:34:08 -0800198
199 FakeAvbOpsDelegate* delegate_;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400200
201 base::FilePath partition_dir_;
202
203 std::string expected_public_key_;
David Zeuthen18666ab2016-11-15 11:18:05 -0500204 std::string expected_public_key_metadata_;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400205
Darren Krahn72d57902016-12-12 18:34:08 -0800206 std::map<size_t, uint64_t> stored_rollback_indexes_;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400207
208 bool stored_is_device_unlocked_;
Darren Krahn147b08d2016-12-20 16:38:29 -0800209
210 AvbAtxPermanentAttributes permanent_attributes_;
211 std::string permanent_attributes_hash_;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400212};
213
Darren Krahn72d57902016-12-12 18:34:08 -0800214} // namespace avb
215
David Zeuthen8b6973b2016-09-20 12:39:49 -0400216#endif /* FAKE_AVB_OPS_H_ */