David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 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> |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 29 | #include <map> |
David Zeuthen | 01ca996 | 2017-05-23 15:28:17 -0400 | [diff] [blame] | 30 | #include <set> |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 31 | #include <string> |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 32 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 33 | #include <libavb_ab/libavb_ab.h> |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame] | 34 | #include <libavb_atx/libavb_atx.h> |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 35 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 36 | namespace avb { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 37 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 38 | // A delegate interface for ops callbacks. This allows tests to override default |
| 39 | // fake implementations. |
| 40 | class FakeAvbOpsDelegate { |
| 41 | public: |
| 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; |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame] | 77 | |
David Zeuthen | 27a291f | 2017-04-27 18:18:33 -0400 | [diff] [blame] | 78 | virtual AvbIOResult get_size_of_partition(AvbOps* ops, |
| 79 | const char* partition, |
| 80 | uint64_t* out_size) = 0; |
| 81 | |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame] | 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; |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 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 { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 92 | public: |
| 93 | FakeAvbOps(); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 94 | virtual ~FakeAvbOps(); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 95 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 96 | static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) { |
David Zeuthen | 574ed99 | 2017-01-05 15:43:11 -0500 | [diff] [blame] | 97 | return reinterpret_cast<FakeAvbOps*>(ops->user_data); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 98 | } |
David Zeuthen | 574ed99 | 2017-01-05 15:43:11 -0500 | [diff] [blame] | 99 | static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) { |
| 100 | return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 101 | } |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 102 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 103 | AvbOps* avb_ops() { |
| 104 | return &avb_ops_; |
| 105 | } |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 106 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 107 | AvbABOps* avb_ab_ops() { |
| 108 | return &avb_ab_ops_; |
| 109 | } |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 110 | |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame] | 111 | AvbAtxOps* avb_atx_ops() { |
| 112 | return &avb_atx_ops_; |
| 113 | } |
| 114 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 115 | FakeAvbOpsDelegate* delegate() { |
| 116 | return delegate_; |
| 117 | } |
| 118 | |
| 119 | // Does not take ownership of |delegate|. |
| 120 | void set_delegate(FakeAvbOpsDelegate* delegate) { |
| 121 | delegate_ = delegate; |
| 122 | } |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 123 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 124 | void set_partition_dir(const base::FilePath& partition_dir) { |
| 125 | partition_dir_ = partition_dir; |
| 126 | } |
| 127 | |
| 128 | void set_expected_public_key(const std::string& expected_public_key) { |
| 129 | expected_public_key_ = expected_public_key; |
| 130 | } |
| 131 | |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 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 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 137 | void set_stored_rollback_indexes( |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 138 | const std::map<size_t, uint64_t>& stored_rollback_indexes) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 139 | stored_rollback_indexes_ = stored_rollback_indexes; |
| 140 | } |
| 141 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 142 | std::map<size_t, uint64_t> get_stored_rollback_indexes() { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 143 | return stored_rollback_indexes_; |
| 144 | } |
| 145 | |
| 146 | void set_stored_is_device_unlocked(bool stored_is_device_unlocked) { |
| 147 | stored_is_device_unlocked_ = stored_is_device_unlocked; |
| 148 | } |
| 149 | |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame] | 150 | void set_permanent_attributes(const AvbAtxPermanentAttributes& attributes) { |
| 151 | permanent_attributes_ = attributes; |
| 152 | } |
| 153 | |
| 154 | void set_permanent_attributes_hash(const std::string& hash) { |
| 155 | permanent_attributes_hash_ = hash; |
| 156 | } |
| 157 | |
David Zeuthen | 01ca996 | 2017-05-23 15:28:17 -0400 | [diff] [blame] | 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 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 162 | // FakeAvbOpsDelegate methods. |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 163 | AvbIOResult read_from_partition(const char* partition, |
| 164 | int64_t offset, |
| 165 | size_t num_bytes, |
| 166 | void* buffer, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 167 | size_t* out_num_read) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 168 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 169 | AvbIOResult write_to_partition(const char* partition, |
| 170 | int64_t offset, |
| 171 | size_t num_bytes, |
| 172 | const void* buffer) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 173 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 174 | AvbIOResult validate_vbmeta_public_key(AvbOps* ops, |
| 175 | const uint8_t* public_key_data, |
| 176 | size_t public_key_length, |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 177 | const uint8_t* public_key_metadata, |
| 178 | size_t public_key_metadata_length, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 179 | bool* out_key_is_trusted) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 180 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 181 | AvbIOResult read_rollback_index(AvbOps* ops, |
| 182 | size_t rollback_index_location, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 183 | uint64_t* out_rollback_index) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 184 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 185 | AvbIOResult write_rollback_index(AvbOps* ops, |
| 186 | size_t rollback_index_location, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 187 | uint64_t rollback_index) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 188 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 189 | AvbIOResult read_is_device_unlocked(AvbOps* ops, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 190 | bool* out_is_device_unlocked) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 191 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 192 | AvbIOResult get_unique_guid_for_partition(AvbOps* ops, |
| 193 | const char* partition, |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 194 | char* guid_buf, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 195 | size_t guid_buf_size) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 196 | |
David Zeuthen | 27a291f | 2017-04-27 18:18:33 -0400 | [diff] [blame] | 197 | AvbIOResult get_size_of_partition(AvbOps* ops, |
| 198 | const char* partition, |
| 199 | uint64_t* out_size) override; |
| 200 | |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame] | 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 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 207 | private: |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 208 | AvbOps avb_ops_; |
| 209 | AvbABOps avb_ab_ops_; |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame] | 210 | AvbAtxOps avb_atx_ops_; |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 211 | |
| 212 | FakeAvbOpsDelegate* delegate_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 213 | |
| 214 | base::FilePath partition_dir_; |
| 215 | |
| 216 | std::string expected_public_key_; |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 217 | std::string expected_public_key_metadata_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 218 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 219 | std::map<size_t, uint64_t> stored_rollback_indexes_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 220 | |
| 221 | bool stored_is_device_unlocked_; |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame] | 222 | |
| 223 | AvbAtxPermanentAttributes permanent_attributes_; |
| 224 | std::string permanent_attributes_hash_; |
David Zeuthen | 01ca996 | 2017-05-23 15:28:17 -0400 | [diff] [blame] | 225 | |
| 226 | std::set<std::string> partition_names_read_from_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 227 | }; |
| 228 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 229 | } // namespace avb |
| 230 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 231 | #endif /* FAKE_AVB_OPS_H_ */ |