blob: 1544bb2a46bceb72b5bd56202b2b3a434af0227c [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
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
24class UpdateVerifierTest : public ::testing::Test {
25 protected:
26 void SetUp() override {
David Zeuthen8ed97382017-05-08 13:41:28 -040027#if defined(PRODUCT_SUPPORTS_VERITY) || defined(BOARD_AVB_ENABLE)
Tao Bao83b07802017-04-26 14:30:56 -070028 verity_supported = true;
29#else
30 verity_supported = false;
31#endif
32 }
33
34 bool verity_supported;
35};
36
37TEST_F(UpdateVerifierTest, verify_image_no_care_map) {
38 // Non-existing care_map is allowed.
39 ASSERT_TRUE(verify_image("/doesntexist"));
40}
41
42TEST_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
Tao Bao83b07802017-04-26 14:30:56 -070049 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
59TEST_F(UpdateVerifierTest, verify_image_wrong_lines) {
Tao Bao454d8cb2018-03-22 16:07:00 -070060 // The care map file can have only 2 / 4 / 6 lines.
Tao Bao83b07802017-04-26 14:30:56 -070061 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
71TEST_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}
Tao Baoc3196132017-07-24 09:22:39 -070083
84TEST_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}