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 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 19 | #include <string> |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 20 | #include <unordered_map> |
| 21 | #include <vector> |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 22 | |
| 23 | #include <android-base/file.h> |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 24 | #include <android-base/properties.h> |
| 25 | #include <android-base/strings.h> |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 26 | #include <android-base/test_utils.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 | |
| 32 | class UpdateVerifierTest : public ::testing::Test { |
| 33 | protected: |
| 34 | void SetUp() override { |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 35 | std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); |
| 36 | verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing"); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 39 | // Returns a serialized string of the proto3 message according to the given partition info. |
| 40 | std::string ConstructProto( |
| 41 | std::vector<std::unordered_map<std::string, std::string>>& partitions) { |
| 42 | UpdateVerifier::CareMap result; |
| 43 | for (const auto& partition : partitions) { |
| 44 | UpdateVerifier::CareMap::PartitionInfo info; |
| 45 | if (partition.find("name") != partition.end()) { |
| 46 | info.set_name(partition.at("name")); |
| 47 | } |
| 48 | if (partition.find("ranges") != partition.end()) { |
| 49 | info.set_ranges(partition.at("ranges")); |
| 50 | } |
| 51 | if (partition.find("fingerprint") != partition.end()) { |
| 52 | info.set_fingerprint(partition.at("fingerprint")); |
| 53 | } |
| 54 | |
| 55 | *result.add_partitions() = info; |
| 56 | } |
| 57 | |
| 58 | return result.SerializeAsString(); |
| 59 | } |
| 60 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 61 | bool verity_supported; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 62 | TemporaryFile care_map_file; |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | TEST_F(UpdateVerifierTest, verify_image_no_care_map) { |
| 66 | // Non-existing care_map is allowed. |
| 67 | ASSERT_TRUE(verify_image("/doesntexist")); |
| 68 | } |
| 69 | |
| 70 | TEST_F(UpdateVerifierTest, verify_image_smoke) { |
| 71 | // This test relies on dm-verity support. |
| 72 | if (!verity_supported) { |
| 73 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 74 | return; |
| 75 | } |
| 76 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 77 | std::string content = "system\n2,0,1"; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_file.path)); |
| 79 | ASSERT_TRUE(verify_image(care_map_file.path)); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 80 | |
| 81 | // Leading and trailing newlines should be accepted. |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 82 | ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", care_map_file.path)); |
| 83 | ASSERT_TRUE(verify_image(care_map_file.path)); |
| 84 | } |
| 85 | |
| 86 | TEST_F(UpdateVerifierTest, verify_image_empty_care_map) { |
| 87 | ASSERT_FALSE(verify_image(care_map_file.path)); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST_F(UpdateVerifierTest, verify_image_wrong_lines) { |
Tao Bao | ec2e8c6 | 2018-03-22 16:07:00 -0700 | [diff] [blame] | 91 | // The care map file can have only 2 / 4 / 6 lines. |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(android::base::WriteStringToFile("line1", care_map_file.path)); |
| 93 | ASSERT_FALSE(verify_image(care_map_file.path)); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 94 | |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 95 | ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", care_map_file.path)); |
| 96 | ASSERT_FALSE(verify_image(care_map_file.path)); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) { |
| 100 | // This test relies on dm-verity support. |
| 101 | if (!verity_supported) { |
| 102 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 103 | return; |
| 104 | } |
| 105 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 106 | std::string content = "system\n2,1,0"; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 107 | ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_file.path)); |
| 108 | ASSERT_FALSE(verify_image(care_map_file.path)); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 109 | } |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 110 | |
| 111 | TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) { |
| 112 | // This test relies on dm-verity support. |
| 113 | if (!verity_supported) { |
| 114 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 115 | return; |
| 116 | } |
| 117 | |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 118 | std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0"; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 119 | ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_file.path)); |
| 120 | ASSERT_TRUE(verify_image(care_map_file.path)); |
| 121 | } |
| 122 | |
| 123 | TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_smoke) { |
| 124 | // This test relies on dm-verity support. |
| 125 | if (!verity_supported) { |
| 126 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | std::vector<std::unordered_map<std::string, std::string>> partitions = { |
| 131 | { { "name", "system" }, { "ranges", "2,0,1" } }, |
| 132 | }; |
| 133 | |
| 134 | std::string proto = ConstructProto(partitions); |
| 135 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_file.path)); |
| 136 | ASSERT_TRUE(verify_image(care_map_file.path)); |
| 137 | } |
| 138 | |
| 139 | TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_missing_name) { |
| 140 | // This test relies on dm-verity support. |
| 141 | if (!verity_supported) { |
| 142 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | std::vector<std::unordered_map<std::string, std::string>> partitions = { |
| 147 | { { "ranges", "2,0,1" } }, |
| 148 | }; |
| 149 | |
| 150 | std::string proto = ConstructProto(partitions); |
| 151 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_file.path)); |
| 152 | ASSERT_FALSE(verify_image(care_map_file.path)); |
| 153 | } |
| 154 | |
| 155 | TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_bad_ranges) { |
| 156 | // This test relies on dm-verity support. |
| 157 | if (!verity_supported) { |
| 158 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | std::vector<std::unordered_map<std::string, std::string>> partitions = { |
| 163 | { { "name", "system" }, { "ranges", "3,0,1" } }, |
| 164 | }; |
| 165 | |
| 166 | std::string proto = ConstructProto(partitions); |
| 167 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_file.path)); |
| 168 | ASSERT_FALSE(verify_image(care_map_file.path)); |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 169 | } |