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 | |
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 | |
| 40 | care_map_pb_ = care_map_dir_.path + "/care_map.pb"s; |
| 41 | care_map_txt_ = care_map_dir_.path + "/care_map.txt"s; |
| 42 | } |
| 43 | |
| 44 | void TearDown() override { |
| 45 | unlink(care_map_pb_.c_str()); |
| 46 | unlink(care_map_txt_.c_str()); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 49 | // Returns a serialized string of the proto3 message according to the given partition info. |
| 50 | std::string ConstructProto( |
| 51 | std::vector<std::unordered_map<std::string, std::string>>& partitions) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 52 | recovery_update_verifier::CareMap result; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 53 | for (const auto& partition : partitions) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 54 | recovery_update_verifier::CareMap::PartitionInfo info; |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 55 | if (partition.find("name") != partition.end()) { |
| 56 | info.set_name(partition.at("name")); |
| 57 | } |
| 58 | if (partition.find("ranges") != partition.end()) { |
| 59 | info.set_ranges(partition.at("ranges")); |
| 60 | } |
| 61 | if (partition.find("fingerprint") != partition.end()) { |
| 62 | info.set_fingerprint(partition.at("fingerprint")); |
| 63 | } |
| 64 | |
| 65 | *result.add_partitions() = info; |
| 66 | } |
| 67 | |
| 68 | return result.SerializeAsString(); |
| 69 | } |
| 70 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 71 | bool verity_supported; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 72 | UpdateVerifier verifier_; |
| 73 | |
| 74 | TemporaryDir care_map_dir_; |
| 75 | std::string care_map_pb_; |
| 76 | std::string care_map_txt_; |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | TEST_F(UpdateVerifierTest, verify_image_no_care_map) { |
| 80 | // Non-existing care_map is allowed. |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 81 | ASSERT_FALSE(verifier_.ParseCareMap("/doesntexist")); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST_F(UpdateVerifierTest, verify_image_smoke) { |
| 85 | // This test relies on dm-verity support. |
| 86 | if (!verity_supported) { |
| 87 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 88 | return; |
| 89 | } |
| 90 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 91 | std::string content = "system\n2,0,1"; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_)); |
| 93 | ASSERT_TRUE(verifier_.ParseCareMap(care_map_txt_)); |
| 94 | ASSERT_TRUE(verifier_.VerifyPartitions()); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 95 | |
| 96 | // Leading and trailing newlines should be accepted. |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 97 | ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", care_map_txt_)); |
| 98 | ASSERT_TRUE(verifier_.ParseCareMap(care_map_txt_)); |
| 99 | ASSERT_TRUE(verifier_.VerifyPartitions()); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST_F(UpdateVerifierTest, verify_image_empty_care_map) { |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 103 | ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST_F(UpdateVerifierTest, verify_image_wrong_lines) { |
Tao Bao | ec2e8c6 | 2018-03-22 16:07:00 -0700 | [diff] [blame] | 107 | // The care map file can have only 2 / 4 / 6 lines. |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 108 | ASSERT_TRUE(android::base::WriteStringToFile("line1", care_map_txt_)); |
| 109 | ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 110 | |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 111 | ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", care_map_txt_)); |
| 112 | ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) { |
| 116 | // This test relies on dm-verity support. |
| 117 | if (!verity_supported) { |
| 118 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 119 | return; |
| 120 | } |
| 121 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 122 | std::string content = "system\n2,1,0"; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 123 | ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_)); |
| 124 | ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 125 | } |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 126 | |
| 127 | TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) { |
| 128 | // This test relies on dm-verity support. |
| 129 | if (!verity_supported) { |
| 130 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 131 | return; |
| 132 | } |
| 133 | |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 134 | std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0"; |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 135 | ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_)); |
| 136 | ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_)); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_smoke) { |
| 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 | { { "name", "system" }, { "ranges", "2,0,1" } }, |
| 148 | }; |
| 149 | |
| 150 | std::string proto = ConstructProto(partitions); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 151 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); |
| 152 | ASSERT_TRUE(verifier_.ParseCareMap(care_map_pb_)); |
| 153 | ASSERT_TRUE(verifier_.VerifyPartitions()); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_missing_name) { |
| 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 = { |
| 164 | { { "ranges", "2,0,1" } }, |
| 165 | }; |
| 166 | |
| 167 | std::string proto = ConstructProto(partitions); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 168 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); |
| 169 | ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_)); |
Tianjie Xu | 4d9e62d | 2018-05-11 10:41:44 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_bad_ranges) { |
| 173 | // This test relies on dm-verity support. |
| 174 | if (!verity_supported) { |
| 175 | GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support."; |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | std::vector<std::unordered_map<std::string, std::string>> partitions = { |
| 180 | { { "name", "system" }, { "ranges", "3,0,1" } }, |
| 181 | }; |
| 182 | |
| 183 | std::string proto = ConstructProto(partitions); |
Tianjie Xu | 446b64b | 2018-09-19 15:45:28 -0700 | [diff] [blame] | 184 | ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_)); |
| 185 | ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_)); |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 186 | } |