Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 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> |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 20 | #include <android-base/properties.h> |
| 21 | #include <android-base/strings.h> |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 22 | #include <android-base/test_utils.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include <update_verifier/update_verifier.h> |
| 25 | |
| 26 | class UpdateVerifierTest : public ::testing::Test { |
| 27 | protected: |
| 28 | void SetUp() override { |
Tao Bao | 1cc0351 | 2018-04-18 17:10:49 -0700 | [diff] [blame] | 29 | std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); |
| 30 | verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing"); |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | bool verity_supported; |
| 34 | }; |
| 35 | |
| 36 | TEST_F(UpdateVerifierTest, verify_image_no_care_map) { |
| 37 | // Non-existing care_map is allowed. |
| 38 | ASSERT_TRUE(verify_image("/doesntexist")); |
| 39 | } |
| 40 | |
| 41 | TEST_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 Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 48 | 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 | |
| 58 | TEST_F(UpdateVerifierTest, verify_image_wrong_lines) { |
Tao Bao | ec2e8c6 | 2018-03-22 16:07:00 -0700 | [diff] [blame] | 59 | // The care map file can have only 2 / 4 / 6 lines. |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 60 | 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 | |
| 70 | TEST_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 Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 82 | |
| 83 | TEST_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 | } |