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> |
| 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: |
| 26 | void SetUp() override { |
David Zeuthen | 8ed9738 | 2017-05-08 13:41:28 -0400 | [diff] [blame] | 27 | #if defined(PRODUCT_SUPPORTS_VERITY) || defined(BOARD_AVB_ENABLE) |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 28 | verity_supported = true; |
| 29 | #else |
| 30 | verity_supported = false; |
| 31 | #endif |
| 32 | } |
| 33 | |
| 34 | bool verity_supported; |
| 35 | }; |
| 36 | |
| 37 | TEST_F(UpdateVerifierTest, verify_image_no_care_map) { |
| 38 | // Non-existing care_map is allowed. |
| 39 | ASSERT_TRUE(verify_image("/doesntexist")); |
| 40 | } |
| 41 | |
| 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 | |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 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 | |
| 59 | TEST_F(UpdateVerifierTest, verify_image_wrong_lines) { |
Tao Bao | 454d8cb | 2018-03-22 16:07:00 -0700 | [diff] [blame] | 60 | // The care map file can have only 2 / 4 / 6 lines. |
Tao Bao | 83b0780 | 2017-04-26 14:30:56 -0700 | [diff] [blame] | 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 | |
| 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 | } |
Tao Bao | c319613 | 2017-07-24 09:22:39 -0700 | [diff] [blame] | 83 | |
| 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 | } |