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
17 #include <string>
18
19 #include <android-base/file.h>
20 #include <android-base/test_utils.h>
21 #include <gtest/gtest.h>
22 #include <update_verifier/update_verifier.h>
23
24 class UpdateVerifierTest : public ::testing::Test {
25 protected:
SetUp()26 void SetUp() override {
27 #if defined(PRODUCT_SUPPORTS_VERITY) || defined(BOARD_AVB_ENABLE)
28 verity_supported = true;
29 #else
30 verity_supported = false;
31 #endif
32 }
33
34 bool verity_supported;
35 };
36
TEST_F(UpdateVerifierTest,verify_image_no_care_map)37 TEST_F(UpdateVerifierTest, verify_image_no_care_map) {
38 // Non-existing care_map is allowed.
39 ASSERT_TRUE(verify_image("/doesntexist"));
40 }
41
TEST_F(UpdateVerifierTest,verify_image_smoke)42 TEST_F(UpdateVerifierTest, verify_image_smoke) {
43 // This test relies on dm-verity support.
44 if (!verity_supported) {
45 GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support.";
46 return;
47 }
48
49 TemporaryFile temp_file;
50 std::string content = "system\n2,0,1";
51 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
52 ASSERT_TRUE(verify_image(temp_file.path));
53
54 // Leading and trailing newlines should be accepted.
55 ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", temp_file.path));
56 ASSERT_TRUE(verify_image(temp_file.path));
57 }
58
TEST_F(UpdateVerifierTest,verify_image_wrong_lines)59 TEST_F(UpdateVerifierTest, verify_image_wrong_lines) {
60 // The care map file can have only 2 / 4 / 6 lines.
61 TemporaryFile temp_file;
62 ASSERT_FALSE(verify_image(temp_file.path));
63
64 ASSERT_TRUE(android::base::WriteStringToFile("line1", temp_file.path));
65 ASSERT_FALSE(verify_image(temp_file.path));
66
67 ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", temp_file.path));
68 ASSERT_FALSE(verify_image(temp_file.path));
69 }
70
TEST_F(UpdateVerifierTest,verify_image_malformed_care_map)71 TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) {
72 // This test relies on dm-verity support.
73 if (!verity_supported) {
74 GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support.";
75 return;
76 }
77
78 TemporaryFile temp_file;
79 std::string content = "system\n2,1,0";
80 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
81 ASSERT_FALSE(verify_image(temp_file.path));
82 }
83
TEST_F(UpdateVerifierTest,verify_image_legacy_care_map)84 TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) {
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
91 TemporaryFile temp_file;
92 std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0";
93 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
94 ASSERT_TRUE(verify_image(temp_file.path));
95 }
96