blob: 5fc7ef63fd655c43fc7162b48de1f84c29506d6a [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 Zeuthen336cbce2017-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
49 // The care map file can have only two or four lines.
50 TemporaryFile temp_file;
51 std::string content = "system\n2,0,1";
52 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
53 ASSERT_TRUE(verify_image(temp_file.path));
54
55 // Leading and trailing newlines should be accepted.
56 ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", temp_file.path));
57 ASSERT_TRUE(verify_image(temp_file.path));
58}
59
60TEST_F(UpdateVerifierTest, verify_image_wrong_lines) {
61 // The care map file can have only two or four lines.
62 TemporaryFile temp_file;
63 ASSERT_FALSE(verify_image(temp_file.path));
64
65 ASSERT_TRUE(android::base::WriteStringToFile("line1", temp_file.path));
66 ASSERT_FALSE(verify_image(temp_file.path));
67
68 ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", temp_file.path));
69 ASSERT_FALSE(verify_image(temp_file.path));
70}
71
72TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) {
73 // This test relies on dm-verity support.
74 if (!verity_supported) {
75 GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support.";
76 return;
77 }
78
79 TemporaryFile temp_file;
80 std::string content = "system\n2,1,0";
81 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
82 ASSERT_FALSE(verify_image(temp_file.path));
83}