blob: a970716353a7ce9f422e7ae4fd4483f327643269 [file] [log] [blame]
Tao Bao83b07802017-04-26 14:30:56 -07001/*
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 Xu4d9e62d2018-05-11 10:41:44 -070017#include <update_verifier/update_verifier.h>
18
Tao Bao83b07802017-04-26 14:30:56 -070019#include <string>
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070020#include <unordered_map>
21#include <vector>
Tao Bao83b07802017-04-26 14:30:56 -070022
23#include <android-base/file.h>
Tao Bao1cc03512018-04-18 17:10:49 -070024#include <android-base/properties.h>
25#include <android-base/strings.h>
Tao Bao83b07802017-04-26 14:30:56 -070026#include <android-base/test_utils.h>
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070027#include <google/protobuf/repeated_field.h>
Tao Bao83b07802017-04-26 14:30:56 -070028#include <gtest/gtest.h>
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070029
30#include "care_map.pb.h"
Tao Bao83b07802017-04-26 14:30:56 -070031
32class UpdateVerifierTest : public ::testing::Test {
33 protected:
34 void SetUp() override {
Tao Bao1cc03512018-04-18 17:10:49 -070035 std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
36 verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing");
Tao Bao83b07802017-04-26 14:30:56 -070037 }
38
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070039 // 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 Bao83b07802017-04-26 14:30:56 -070061 bool verity_supported;
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070062 TemporaryFile care_map_file;
Tao Bao83b07802017-04-26 14:30:56 -070063};
64
65TEST_F(UpdateVerifierTest, verify_image_no_care_map) {
66 // Non-existing care_map is allowed.
67 ASSERT_TRUE(verify_image("/doesntexist"));
68}
69
70TEST_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 Bao83b07802017-04-26 14:30:56 -070077 std::string content = "system\n2,0,1";
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070078 ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_file.path));
79 ASSERT_TRUE(verify_image(care_map_file.path));
Tao Bao83b07802017-04-26 14:30:56 -070080
81 // Leading and trailing newlines should be accepted.
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070082 ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", care_map_file.path));
83 ASSERT_TRUE(verify_image(care_map_file.path));
84}
85
86TEST_F(UpdateVerifierTest, verify_image_empty_care_map) {
87 ASSERT_FALSE(verify_image(care_map_file.path));
Tao Bao83b07802017-04-26 14:30:56 -070088}
89
90TEST_F(UpdateVerifierTest, verify_image_wrong_lines) {
Tao Baoec2e8c62018-03-22 16:07:00 -070091 // The care map file can have only 2 / 4 / 6 lines.
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070092 ASSERT_TRUE(android::base::WriteStringToFile("line1", care_map_file.path));
93 ASSERT_FALSE(verify_image(care_map_file.path));
Tao Bao83b07802017-04-26 14:30:56 -070094
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070095 ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", care_map_file.path));
96 ASSERT_FALSE(verify_image(care_map_file.path));
Tao Bao83b07802017-04-26 14:30:56 -070097}
98
99TEST_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 Bao83b07802017-04-26 14:30:56 -0700106 std::string content = "system\n2,1,0";
Tianjie Xu4d9e62d2018-05-11 10:41:44 -0700107 ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_file.path));
108 ASSERT_FALSE(verify_image(care_map_file.path));
Tao Bao83b07802017-04-26 14:30:56 -0700109}
Tao Baoc3196132017-07-24 09:22:39 -0700110
111TEST_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 Baoc3196132017-07-24 09:22:39 -0700118 std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0";
Tianjie Xu4d9e62d2018-05-11 10:41:44 -0700119 ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_file.path));
120 ASSERT_TRUE(verify_image(care_map_file.path));
121}
122
123TEST_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
139TEST_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
155TEST_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 Baoc3196132017-07-24 09:22:39 -0700169}