blob: f6ef6dcfd2950458a1086a9fb2e8582e8ac8d1c2 [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>
Tao Bao1cc03512018-04-18 17:10:49 -070020#include <android-base/properties.h>
21#include <android-base/strings.h>
Tao Bao83b07802017-04-26 14:30:56 -070022#include <android-base/test_utils.h>
23#include <gtest/gtest.h>
24#include <update_verifier/update_verifier.h>
25
26class UpdateVerifierTest : public ::testing::Test {
27 protected:
28 void SetUp() override {
Tao Bao1cc03512018-04-18 17:10:49 -070029 std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
30 verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing");
Tao Bao83b07802017-04-26 14:30:56 -070031 }
32
33 bool verity_supported;
34};
35
36TEST_F(UpdateVerifierTest, verify_image_no_care_map) {
37 // Non-existing care_map is allowed.
38 ASSERT_TRUE(verify_image("/doesntexist"));
39}
40
41TEST_F(UpdateVerifierTest, verify_image_smoke) {
42 // This test relies on dm-verity support.
43 if (!verity_supported) {
44 GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support.";
45 return;
46 }
47
Tao Bao83b07802017-04-26 14:30:56 -070048 TemporaryFile temp_file;
49 std::string content = "system\n2,0,1";
50 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
51 ASSERT_TRUE(verify_image(temp_file.path));
52
53 // Leading and trailing newlines should be accepted.
54 ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", temp_file.path));
55 ASSERT_TRUE(verify_image(temp_file.path));
56}
57
58TEST_F(UpdateVerifierTest, verify_image_wrong_lines) {
Tao Baoec2e8c62018-03-22 16:07:00 -070059 // The care map file can have only 2 / 4 / 6 lines.
Tao Bao83b07802017-04-26 14:30:56 -070060 TemporaryFile temp_file;
61 ASSERT_FALSE(verify_image(temp_file.path));
62
63 ASSERT_TRUE(android::base::WriteStringToFile("line1", temp_file.path));
64 ASSERT_FALSE(verify_image(temp_file.path));
65
66 ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", temp_file.path));
67 ASSERT_FALSE(verify_image(temp_file.path));
68}
69
70TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) {
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
77 TemporaryFile temp_file;
78 std::string content = "system\n2,1,0";
79 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
80 ASSERT_FALSE(verify_image(temp_file.path));
81}
Tao Baoc3196132017-07-24 09:22:39 -070082
83TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) {
84 // This test relies on dm-verity support.
85 if (!verity_supported) {
86 GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support.";
87 return;
88 }
89
90 TemporaryFile temp_file;
91 std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0";
92 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
93 ASSERT_TRUE(verify_image(temp_file.path));
94}