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 | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 30 | #include <string> |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 31 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 32 | #include <libavb_ab/libavb_ab.h> |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 33 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 34 | namespace avb { |
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 | // A delegate interface for ops callbacks. This allows tests to override default |
| 37 | // fake implementations. |
| 38 | class FakeAvbOpsDelegate { |
| 39 | public: |
| 40 | virtual ~FakeAvbOpsDelegate() {} |
| 41 | virtual AvbIOResult read_from_partition(const char* partition, |
| 42 | int64_t offset, |
| 43 | size_t num_bytes, |
| 44 | void* buffer, |
| 45 | size_t* out_num_read) = 0; |
| 46 | |
| 47 | virtual AvbIOResult write_to_partition(const char* partition, |
| 48 | int64_t offset, |
| 49 | size_t num_bytes, |
| 50 | const void* buffer) = 0; |
| 51 | |
| 52 | virtual AvbIOResult validate_vbmeta_public_key( |
| 53 | AvbOps* ops, |
| 54 | const uint8_t* public_key_data, |
| 55 | size_t public_key_length, |
| 56 | const uint8_t* public_key_metadata, |
| 57 | size_t public_key_metadata_length, |
| 58 | bool* out_key_is_trusted) = 0; |
| 59 | |
| 60 | virtual AvbIOResult read_rollback_index(AvbOps* ops, |
| 61 | size_t rollback_index_slot, |
| 62 | uint64_t* out_rollback_index) = 0; |
| 63 | |
| 64 | virtual AvbIOResult write_rollback_index(AvbOps* ops, |
| 65 | size_t rollback_index_slot, |
| 66 | uint64_t rollback_index) = 0; |
| 67 | |
| 68 | virtual AvbIOResult read_is_device_unlocked(AvbOps* ops, |
| 69 | bool* out_is_device_unlocked) = 0; |
| 70 | |
| 71 | virtual AvbIOResult get_unique_guid_for_partition(AvbOps* ops, |
| 72 | const char* partition, |
| 73 | char* guid_buf, |
| 74 | size_t guid_buf_size) = 0; |
| 75 | }; |
| 76 | |
| 77 | // Provides fake implementations of AVB ops. All instances of this class must be |
| 78 | // created on the same thread. |
| 79 | class FakeAvbOps : public FakeAvbOpsDelegate { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 80 | public: |
| 81 | FakeAvbOps(); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 82 | virtual ~FakeAvbOps(); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 83 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 84 | static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) { |
David Zeuthen | 574ed99 | 2017-01-05 15:43:11 -0500 | [diff] [blame^] | 85 | return reinterpret_cast<FakeAvbOps*>(ops->user_data); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 86 | } |
David Zeuthen | 574ed99 | 2017-01-05 15:43:11 -0500 | [diff] [blame^] | 87 | static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) { |
| 88 | return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 89 | } |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 90 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 91 | AvbOps* avb_ops() { |
| 92 | return &avb_ops_; |
| 93 | } |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 94 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 95 | AvbABOps* avb_ab_ops() { |
| 96 | return &avb_ab_ops_; |
| 97 | } |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 98 | |
| 99 | FakeAvbOpsDelegate* delegate() { |
| 100 | return delegate_; |
| 101 | } |
| 102 | |
| 103 | // Does not take ownership of |delegate|. |
| 104 | void set_delegate(FakeAvbOpsDelegate* delegate) { |
| 105 | delegate_ = delegate; |
| 106 | } |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 107 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 108 | void set_partition_dir(const base::FilePath& partition_dir) { |
| 109 | partition_dir_ = partition_dir; |
| 110 | } |
| 111 | |
| 112 | void set_expected_public_key(const std::string& expected_public_key) { |
| 113 | expected_public_key_ = expected_public_key; |
| 114 | } |
| 115 | |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 116 | void set_expected_public_key_metadata( |
| 117 | const std::string& expected_public_key_metadata) { |
| 118 | expected_public_key_metadata_ = expected_public_key_metadata; |
| 119 | } |
| 120 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 121 | void set_stored_rollback_indexes( |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 122 | const std::map<size_t, uint64_t>& stored_rollback_indexes) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 123 | stored_rollback_indexes_ = stored_rollback_indexes; |
| 124 | } |
| 125 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 126 | std::map<size_t, uint64_t> get_stored_rollback_indexes() { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 127 | return stored_rollback_indexes_; |
| 128 | } |
| 129 | |
| 130 | void set_stored_is_device_unlocked(bool stored_is_device_unlocked) { |
| 131 | stored_is_device_unlocked_ = stored_is_device_unlocked; |
| 132 | } |
| 133 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 134 | // FakeAvbOpsDelegate methods. |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 135 | AvbIOResult read_from_partition(const char* partition, |
| 136 | int64_t offset, |
| 137 | size_t num_bytes, |
| 138 | void* buffer, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 139 | size_t* out_num_read) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 140 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 141 | AvbIOResult write_to_partition(const char* partition, |
| 142 | int64_t offset, |
| 143 | size_t num_bytes, |
| 144 | const void* buffer) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 145 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 146 | AvbIOResult validate_vbmeta_public_key(AvbOps* ops, |
| 147 | const uint8_t* public_key_data, |
| 148 | size_t public_key_length, |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 149 | const uint8_t* public_key_metadata, |
| 150 | size_t public_key_metadata_length, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 151 | bool* out_key_is_trusted) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 152 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 153 | AvbIOResult read_rollback_index(AvbOps* ops, |
| 154 | size_t rollback_index_location, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 155 | uint64_t* out_rollback_index) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 156 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 157 | AvbIOResult write_rollback_index(AvbOps* ops, |
| 158 | size_t rollback_index_location, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 159 | uint64_t rollback_index) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 160 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 161 | AvbIOResult read_is_device_unlocked(AvbOps* ops, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 162 | bool* out_is_device_unlocked) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 163 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 164 | AvbIOResult get_unique_guid_for_partition(AvbOps* ops, |
| 165 | const char* partition, |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 166 | char* guid_buf, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 167 | size_t guid_buf_size) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 168 | |
| 169 | private: |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 170 | AvbOps avb_ops_; |
| 171 | AvbABOps avb_ab_ops_; |
| 172 | |
| 173 | FakeAvbOpsDelegate* delegate_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 174 | |
| 175 | base::FilePath partition_dir_; |
| 176 | |
| 177 | std::string expected_public_key_; |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 178 | std::string expected_public_key_metadata_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 179 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 180 | std::map<size_t, uint64_t> stored_rollback_indexes_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 181 | |
| 182 | bool stored_is_device_unlocked_; |
| 183 | }; |
| 184 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 185 | } // namespace avb |
| 186 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 187 | #endif /* FAKE_AVB_OPS_H_ */ |