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