Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 17 | #include <update_verifier/update_verifier.h> |
| 18 | |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 19 | #include <functional> |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 20 | #include <string> |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 21 | #include <unordered_map> |
| 22 | #include <vector> |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 23 | |
| 24 | #include <android-base/file.h> |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 25 | #include <android-base/properties.h> |
| 26 | #include <android-base/strings.h> |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 27 | #include <google/protobuf/repeated_field.h> |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 28 | #include <gtest/gtest.h> |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 29 | |
| 30 | #include "care_map.pb.h" |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 31 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 32 | using namespace std::string_literals; |
| 33 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 34 | class UpdateVerifierTest : public ::testing::Test { |
| 35 | protected: |
| 36 | void SetUp() override { |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 37 | std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); |
| 38 | verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing"); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 39 | |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 40 | care_map_prefix_ = care_map_dir_.path + "/care_map"s; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 41 | care_map_pb_ = care_map_dir_.path + "/care_map.pb"s; |
| 42 | care_map_txt_ = care_map_dir_.path + "/care_map.txt"s; |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 43 | // Overrides the the care_map_prefix. |
| 44 | verifier_.set_care_map_prefix(care_map_prefix_); |
| 45 | |
| 46 | property_id_ = "ro.build.fingerprint"; |
| 47 | fingerprint_ = android::base::GetProperty(property_id_, ""); |
| 48 | // Overrides the property_reader if we cannot read the given property on the device. |
| 49 | if (fingerprint_.empty()) { |
| 50 | fingerprint_ = "mock_fingerprint"; |
| 51 | verifier_.set_property_reader([](const std::string& /* id */) { return "mock_fingerprint"; }); |
| 52 | } |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void TearDown() override { |
| 56 | unlink(care_map_pb_.c_str()); |
| 57 | unlink(care_map_txt_.c_str()); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 60 | // Returns a serialized string of the proto3 message according to the given partition info. |
| 61 | std::string ConstructProto( |
| 62 | std::vector<std::unordered_map<std::string, std::string>>& partitions) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 63 | recovery_update_verifier::CareMap result; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 64 | for (const auto& partition : partitions) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 65 | recovery_update_verifier::CareMap::PartitionInfo info; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 66 | if (partition.find("name") != partition.end()) { |
| 67 | info.set_name(partition.at("name")); |
| 68 | } |
| 69 | if (partition.find("ranges") != partition.end()) { |
| 70 | info.set_ranges(partition.at("ranges")); |
| 71 | } |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 72 | if (partition.find("id") != partition.end()) { |
| 73 | info.set_id(partition.at("id")); |
| 74 | } |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 75 | if (partition.find("fingerprint") != partition.end()) { |
| 76 | info.set_fingerprint(partition.at("fingerprint")); |
| 77 | } |
| 78 | |
| 79 | *result.add_partitions() = info; |
| 80 | } |
| 81 | |
| 82 | return result.SerializeAsString(); |
| 83 | } |
| 84 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 85 | bool verity_supported; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 86 | UpdateVerifier verifier_; |
| 87 | |
| 88 | TemporaryDir care_map_dir_; |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 89 | std::string care_map_prefix_; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 90 | std::string care_map_pb_; |
| 91 | std::string care_map_txt_; |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 92 | |
| 93 | std::string property_id_; |
| 94 | std::string fingerprint_; |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | TEST_F(UpdateVerifierTest, verify_image_no_care_map) { |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 98 | ASSERT_FALSE(verifier_.ParseCareMap()); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | TEST_F(UpdateVerifierTest, verify_image_smoke) { |
| 102 | // This test relies on dm-verity support. |
| 103 | if (!verity_supported) { |
| 104 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 105 | return; |
| 106 | } |
| 107 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 108 | std::string content = "system\n2,0,1"; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_)); |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 110 | ASSERT_TRUE(verifier_.ParseCareMap()); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 111 | ASSERT_TRUE(verifier_.VerifyPartitions()); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 112 | |
| 113 | // Leading and trailing newlines should be accepted. |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 114 | ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", care_map_txt_)); |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 115 | ASSERT_TRUE(verifier_.ParseCareMap()); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 116 | ASSERT_TRUE(verifier_.VerifyPartitions()); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST_F(UpdateVerifierTest, verify_image_empty_care_map) { |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 120 | ASSERT_FALSE(verifier_.ParseCareMap()); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(UpdateVerifierTest, verify_image_wrong_lines) { |
Tao Bao | ec2e8c6 | 2018-03-22 16:07:00 -0700 | [diff] [blame] | 124 | // The care map file can have only 2 / 4 / 6 lines. |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 125 | ASSERT_TRUE(android::base::WriteStringToFile("line1", care_map_txt_)); |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 126 | ASSERT_FALSE(verifier_.ParseCareMap()); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 127 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 128 | ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", care_map_txt_)); |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 129 | ASSERT_FALSE(verifier_.ParseCareMap()); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) { |
| 133 | // This test relies on dm-verity support. |
| 134 | if (!verity_supported) { |
| 135 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 136 | return; |
| 137 | } |
| 138 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 139 | std::string content = "system\n2,1,0"; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 140 | ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_)); |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 141 | ASSERT_FALSE(verifier_.ParseCareMap()); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 142 | } |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 143 | |
| 144 | TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) { |
| 145 | // This test relies on dm-verity support. |
| 146 | if (!verity_supported) { |
| 147 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 148 | return; |
| 149 | } |
| 150 | |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 151 | std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0"; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 152 | ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_)); |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 153 | ASSERT_FALSE(verifier_.ParseCareMap()); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_smoke) { |
| 157 | // This test relies on dm-verity support. |
| 158 | if (!verity_supported) { |
| 159 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | std::vector<std::unordered_map<std::string, std::string>> partitions = { |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 164 | { |
| 165 | { "name", "system" }, |
| 166 | { "ranges", "2,0,1" }, |
| 167 | { "id", property_id_ }, |
| 168 | { "fingerprint", fingerprint_ }, |
| 169 | }, |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | std::string proto = ConstructProto(partitions); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 173 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 174 | ASSERT_TRUE(verifier_.ParseCareMap()); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 175 | ASSERT_TRUE(verifier_.VerifyPartitions()); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_missing_name) { |
| 179 | // This test relies on dm-verity support. |
| 180 | if (!verity_supported) { |
| 181 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | std::vector<std::unordered_map<std::string, std::string>> partitions = { |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 186 | { |
| 187 | { "ranges", "2,0,1" }, |
| 188 | { "id", property_id_ }, |
| 189 | { "fingerprint", fingerprint_ }, |
| 190 | }, |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | std::string proto = ConstructProto(partitions); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 194 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 195 | ASSERT_FALSE(verifier_.ParseCareMap()); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_bad_ranges) { |
| 199 | // This test relies on dm-verity support. |
| 200 | if (!verity_supported) { |
| 201 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | std::vector<std::unordered_map<std::string, std::string>> partitions = { |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 206 | { |
| 207 | { "name", "system" }, |
| 208 | { "ranges", "3,0,1" }, |
| 209 | { "id", property_id_ }, |
| 210 | { "fingerprint", fingerprint_ }, |
| 211 | }, |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | std::string proto = ConstructProto(partitions); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 215 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); |
Tianjie Xu | 9eed65e | 2018-09-14 12:52:02 -0700 | [diff] [blame] | 216 | ASSERT_FALSE(verifier_.ParseCareMap()); |
| 217 | } |
| 218 | |
| 219 | TEST_F(UpdateVerifierTest, verify_image_protobuf_empty_fingerprint) { |
| 220 | // This test relies on dm-verity support. |
| 221 | if (!verity_supported) { |
| 222 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | std::vector<std::unordered_map<std::string, std::string>> partitions = { |
| 227 | { |
| 228 | { "name", "system" }, |
| 229 | { "ranges", "2,0,1" }, |
| 230 | }, |
| 231 | }; |
| 232 | |
| 233 | std::string proto = ConstructProto(partitions); |
| 234 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); |
| 235 | ASSERT_FALSE(verifier_.ParseCareMap()); |
| 236 | } |
| 237 | |
| 238 | TEST_F(UpdateVerifierTest, verify_image_protobuf_fingerprint_mismatch) { |
| 239 | // This test relies on dm-verity support. |
| 240 | if (!verity_supported) { |
| 241 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | std::vector<std::unordered_map<std::string, std::string>> partitions = { |
| 246 | { |
| 247 | { "name", "system" }, |
| 248 | { "ranges", "2,0,1" }, |
| 249 | { "id", property_id_ }, |
| 250 | { "fingerprint", "unsupported_fingerprint" }, |
| 251 | }, |
| 252 | }; |
| 253 | |
| 254 | std::string proto = ConstructProto(partitions); |
| 255 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); |
| 256 | ASSERT_FALSE(verifier_.ParseCareMap()); |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 257 | } |