blob: 543e4278e07aa9c53b1294fdc4c51790a9752973 [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
Tianjie Xu446b64b2018-09-19 15:45:28 -070032using namespace std::string_literals;
33
Tao Bao83b07802017-04-26 14:30:56 -070034class UpdateVerifierTest : public ::testing::Test {
35 protected:
36 void SetUp() override {
Tao Bao1cc03512018-04-18 17:10:49 -070037 std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
38 verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing");
Tianjie Xu446b64b2018-09-19 15:45:28 -070039
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 Bao83b07802017-04-26 14:30:56 -070047 }
48
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070049 // 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 Xu446b64b2018-09-19 15:45:28 -070052 recovery_update_verifier::CareMap result;
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070053 for (const auto& partition : partitions) {
Tianjie Xu446b64b2018-09-19 15:45:28 -070054 recovery_update_verifier::CareMap::PartitionInfo info;
Tianjie Xu4d9e62d2018-05-11 10:41:44 -070055 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 Bao83b07802017-04-26 14:30:56 -070071 bool verity_supported;
Tianjie Xu446b64b2018-09-19 15:45:28 -070072 UpdateVerifier verifier_;
73
74 TemporaryDir care_map_dir_;
75 std::string care_map_pb_;
76 std::string care_map_txt_;
Tao Bao83b07802017-04-26 14:30:56 -070077};
78
79TEST_F(UpdateVerifierTest, verify_image_no_care_map) {
80 // Non-existing care_map is allowed.
Tianjie Xu446b64b2018-09-19 15:45:28 -070081 ASSERT_FALSE(verifier_.ParseCareMap("/doesntexist"));
Tao Bao83b07802017-04-26 14:30:56 -070082}
83
84TEST_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 Bao83b07802017-04-26 14:30:56 -070091 std::string content = "system\n2,0,1";
Tianjie Xu446b64b2018-09-19 15:45:28 -070092 ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_));
93 ASSERT_TRUE(verifier_.ParseCareMap(care_map_txt_));
94 ASSERT_TRUE(verifier_.VerifyPartitions());
Tao Bao83b07802017-04-26 14:30:56 -070095
96 // Leading and trailing newlines should be accepted.
Tianjie Xu446b64b2018-09-19 15:45:28 -070097 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 Xu4d9e62d2018-05-11 10:41:44 -0700100}
101
102TEST_F(UpdateVerifierTest, verify_image_empty_care_map) {
Tianjie Xu446b64b2018-09-19 15:45:28 -0700103 ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
Tao Bao83b07802017-04-26 14:30:56 -0700104}
105
106TEST_F(UpdateVerifierTest, verify_image_wrong_lines) {
Tao Baoec2e8c62018-03-22 16:07:00 -0700107 // The care map file can have only 2 / 4 / 6 lines.
Tianjie Xu446b64b2018-09-19 15:45:28 -0700108 ASSERT_TRUE(android::base::WriteStringToFile("line1", care_map_txt_));
109 ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
Tao Bao83b07802017-04-26 14:30:56 -0700110
Tianjie Xu446b64b2018-09-19 15:45:28 -0700111 ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", care_map_txt_));
112 ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
Tao Bao83b07802017-04-26 14:30:56 -0700113}
114
115TEST_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 Bao83b07802017-04-26 14:30:56 -0700122 std::string content = "system\n2,1,0";
Tianjie Xu446b64b2018-09-19 15:45:28 -0700123 ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_));
124 ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
Tao Bao83b07802017-04-26 14:30:56 -0700125}
Tao Baoc3196132017-07-24 09:22:39 -0700126
127TEST_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 Baoc3196132017-07-24 09:22:39 -0700134 std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0";
Tianjie Xu446b64b2018-09-19 15:45:28 -0700135 ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_));
136 ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
Tianjie Xu4d9e62d2018-05-11 10:41:44 -0700137}
138
139TEST_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 Xu446b64b2018-09-19 15:45:28 -0700151 ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
152 ASSERT_TRUE(verifier_.ParseCareMap(care_map_pb_));
153 ASSERT_TRUE(verifier_.VerifyPartitions());
Tianjie Xu4d9e62d2018-05-11 10:41:44 -0700154}
155
156TEST_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 Xu446b64b2018-09-19 15:45:28 -0700168 ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
169 ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_));
Tianjie Xu4d9e62d2018-05-11 10:41:44 -0700170}
171
172TEST_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 Xu446b64b2018-09-19 15:45:28 -0700184 ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
185 ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_));
Tao Baoc3196132017-07-24 09:22:39 -0700186}