1 /*
2 * Copyright (C) 2019 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 #pragma once
18
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25
26 #include <string>
27 #include <vector>
28
29 #include <android-base/unique_fd.h>
30 #include <base/files/file_path.h>
31 #include <base/strings/stringprintf.h>
32 #include <fs_avb/types.h>
33 #include <gtest/gtest.h>
34
35 // Utility macro to run the command expressed by the printf()-style string
36 // |command_format| using the system(3) utility function. Will assert unless
37 // the command exits normally with exit status |expected_exit_status|.
38 #define EXPECT_COMMAND(expected_exit_status, command_format, ...) \
39 do { \
40 int rc = system(base::StringPrintf(command_format, ##__VA_ARGS__).c_str()); \
41 EXPECT_TRUE(WIFEXITED(rc)); \
42 EXPECT_EQ(WEXITSTATUS(rc), expected_exit_status); \
43 } while (0);
44
45 using android::fs_mgr::VBMetaData;
46
47 namespace fs_avb_host_test {
48
49 struct VBMetaImage {
50 // Path to vbmeta image generated with GenerateVBMetaImage().
51 base::FilePath path;
52 // Contents of the image generated with GenerateVBMetaImage().
53 std::vector<uint8_t> content;
54 };
55
56 struct ChainPartitionConfig {
57 std::string partition_name;
58 uint32_t rollback_index_location;
59 base::FilePath key_blob_path;
60 };
61
OpenUniqueReadFd(const base::FilePath & file_path)62 inline android::base::unique_fd OpenUniqueReadFd(const base::FilePath& file_path) {
63 return android::base::unique_fd(open(file_path.value().c_str(), O_RDONLY | O_CLOEXEC));
64 }
65
66 /* Base-class used for unit test. */
67 class BaseFsAvbTest : public ::testing::Test {
68 public:
BaseFsAvbTest()69 BaseFsAvbTest() {}
70
71 protected:
~BaseFsAvbTest()72 virtual ~BaseFsAvbTest() {}
73
74 // Calculates the vbmeta digest using 'avbtool calc_vbmeta_digest' command.
75 // Note that the calculation includes chained vbmeta images.
76 std::string CalcVBMetaDigest(const std::string& file_name, const std::string& hash_algorithm);
77
78 // Generates a vbmeta image with |file_name| by avbtool.
79 // The generated vbmeta image will be written to disk, see the
80 // |vbmeta_images_| variable for its path and the content.
81 base::FilePath GenerateVBMetaImage(
82 const std::string& file_name, const std::string& avb_algorithm, uint64_t rollback_index,
83 const base::FilePath& key_path,
84 const std::vector<base::FilePath>& include_descriptor_image_paths,
85 const std::vector<ChainPartitionConfig>& chain_partitions,
86 const std::string& additional_options = "");
87 // Similar to above, but extracts a vbmeta image from the given image_path.
88 // The extracted vbmeta image will be written to disk, with |output_file_name|.
89 // See the |vbmeta_images_| variable for its path and the content.
90 base::FilePath ExtractVBMetaImage(const base::FilePath& image_path,
91 const std::string& output_file_name,
92 const size_t padding_size = 0);
93
94 // Generate a file with name |file_name| of size |image_size| with
95 // known content (0x00 0x01 0x02 .. 0xff 0x00 0x01 ..).
96 base::FilePath GenerateImage(const std::string& file_name, size_t image_size,
97 uint8_t start_byte = 0);
98 // Invokes 'avbtool add_hash_footer' or 'avbtool add_hashtree_footer' to sign
99 // the |image_path|. The |footer_type| can be either "hash" or "hashtree".
100 void AddAvbFooter(const base::FilePath& image_path, const std::string& footer_type,
101 const std::string& partition_name, const uint64_t partition_size,
102 const std::string& avb_algorithm, uint64_t rollback_index,
103 const base::FilePath& avb_signing_key, const std::string& salt = "d00df00d",
104 const std::string& additional_options = "");
105
106 VBMetaData GenerateImageAndExtractVBMetaData(
107 const std::string& partition_name, const size_t image_size, const size_t partition_size,
108 const std::string& footer_type, const base::FilePath& avb_signing_key,
109 const std::string& avb_algorithm, const uint64_t rollback_index);
110
111 VBMetaData ExtractAndLoadVBMetaData(const base::FilePath& image_path,
112 const std::string& output_file_name);
113
114 VBMetaData LoadVBMetaData(const std::string& file_name);
115
116 // Returns the output of 'avbtool info_image' for the |image_path|.
117 std::string InfoImage(const base::FilePath& image_path);
118 // Same as above, but for an internal vbmeta image with |file_name| in |vbmeta_images_|.
119 std::string InfoImage(const std::string& file_name);
120
121 // Extracts public key blob in AVB format for a .pem key, then returns the
122 // file path: a .bin file.
123 base::FilePath ExtractPublicKeyAvb(const base::FilePath& key_path);
124 // Same as above, but returns the key blob binary instead.
125 std::string ExtractPublicKeyAvbBlob(const base::FilePath& key_path);
126
127 void SetUp() override;
128 void TearDown() override;
129
130 // Temporary directory created in SetUp().
131 base::FilePath test_dir_;
132 // Maps vbmeta image name (e.g., vbmeta_a.img, system_a.img) to VBMetaImage.
133 std::map<std::string, VBMetaImage> vbmeta_images_;
134
135 static base::FilePath data_dir_;
136 };
137
138 } // namespace fs_avb_host_test
139