xref: /aosp_15_r20/system/core/fs_mgr/libfs_avb/tests/basic_test.cpp (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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 #include "fs_avb_test_util.h"
18 
19 #include <stdlib.h>
20 
21 #include <android-base/file.h>
22 #include <base/files/file_util.h>
23 
24 namespace fs_avb_host_test {
25 
TEST_F(BaseFsAvbTest,GenerateImage)26 TEST_F(BaseFsAvbTest, GenerateImage) {
27     const size_t image_size = 5 * 1024 * 1024;
28     base::FilePath boot_path = GenerateImage("boot.img", image_size);
29     EXPECT_NE(0U, boot_path.value().size());
30 
31     // Checks file size is as expected.
32     int64_t file_size;
33     ASSERT_TRUE(base::GetFileSize(boot_path, &file_size));
34     EXPECT_EQ(file_size, image_size);
35 
36     // Checks file content is as expected.
37     std::vector<uint8_t> expected_content;
38     expected_content.resize(image_size);
39     for (size_t n = 0; n < image_size; n++) {
40         expected_content[n] = uint8_t(n);
41     }
42     std::vector<uint8_t> actual_content;
43     actual_content.resize(image_size);
44     EXPECT_TRUE(
45             base::ReadFile(boot_path, reinterpret_cast<char*>(actual_content.data()), image_size));
46     EXPECT_EQ(expected_content, actual_content);
47 }
48 
TEST_F(BaseFsAvbTest,GenerateVBMetaImage)49 TEST_F(BaseFsAvbTest, GenerateVBMetaImage) {
50     GenerateVBMetaImage("vbmeta.img", "SHA256_RSA2048", 0, data_dir_.Append("testkey_rsa2048.pem"),
51                         {}, /* include_descriptor_image_paths */
52                         {}, /* chain_partitions */
53                         "--internal_release_string \"unit test\"");
54     EXPECT_EQ("5eba9ad4e775645e7eac441a563c200681ae868158d06f6a6cd36d06c07bd781",
55               CalcVBMetaDigest("vbmeta.img", "sha256"));
56     EXPECT_EQ(
57             "Minimum libavb version:   1.0\n"
58             "Header Block:             256 bytes\n"
59             "Authentication Block:     320 bytes\n"
60             "Auxiliary Block:          576 bytes\n"
61             "Public key (sha1):        cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
62             "Algorithm:                SHA256_RSA2048\n"
63             "Rollback Index:           0\n"
64             "Flags:                    0\n"
65             "Rollback Index Location:  0\n"
66             "Release String:           'unit test'\n"
67             "Descriptors:\n"
68             "    (none)\n",
69             InfoImage("vbmeta.img"));
70 }
71 
TEST_F(BaseFsAvbTest,AddHashFooter)72 TEST_F(BaseFsAvbTest, AddHashFooter) {
73     // Generates a raw boot.img
74     const size_t image_size = 5 * 1024 * 1024;
75     const size_t partition_size = 10 * 1024 * 1024;
76     base::FilePath boot_path = GenerateImage("boot.img", image_size);
77     EXPECT_NE(0U, boot_path.value().size());
78     // Checks file size is as expected.
79     int64_t file_size;
80     ASSERT_TRUE(base::GetFileSize(boot_path, &file_size));
81     EXPECT_EQ(file_size, image_size);
82     // Appends AVB Hash Footer.
83     AddAvbFooter(boot_path, "hash", "boot", partition_size, "SHA256_RSA4096", 10,
84                  data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
85                  "--internal_release_string \"unit test\"");
86     // Extracts boot vbmeta from boot.img into boot-vbmeta.img.
87     ExtractVBMetaImage(boot_path, "boot-vbmeta.img");
88     EXPECT_EQ(
89             "Minimum libavb version:   1.0\n"
90             "Header Block:             256 bytes\n"
91             "Authentication Block:     576 bytes\n"
92             "Auxiliary Block:          1216 bytes\n"
93             "Public key (sha1):        2597c218aae470a130f61162feaae70afd97f011\n"
94             "Algorithm:                SHA256_RSA4096\n"
95             "Rollback Index:           10\n"
96             "Flags:                    0\n"
97             "Rollback Index Location:  0\n"
98             "Release String:           'unit test'\n"
99             "Descriptors:\n"
100             "    Hash descriptor:\n"
101             "      Image Size:            5242880 bytes\n"
102             "      Hash Algorithm:        sha256\n"
103             "      Partition Name:        boot\n"
104             "      Salt:                  d00df00d\n"
105             "      Digest:                "
106             "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
107             "      Flags:                 0\n",
108             InfoImage("boot-vbmeta.img"));
109 }
110 
TEST_F(BaseFsAvbTest,AddHashtreeFooter)111 TEST_F(BaseFsAvbTest, AddHashtreeFooter) {
112     // Generates a raw system.img
113     const size_t image_size = 50 * 1024 * 1024;
114     const size_t partition_size = 60 * 1024 * 1024;
115     base::FilePath system_path = GenerateImage("system.img", image_size);
116     EXPECT_NE(0U, system_path.value().size());
117     // Checks file size is as expected.
118     int64_t file_size;
119     ASSERT_TRUE(base::GetFileSize(system_path, &file_size));
120     EXPECT_EQ(file_size, image_size);
121     // Appends AVB Hashtree Footer.
122     AddAvbFooter(system_path, "hashtree", "system", partition_size, "SHA512_RSA8192", 20,
123                  data_dir_.Append("testkey_rsa8192.pem"), "d00df00d",
124                  "--internal_release_string \"unit test\"");
125     // Extracts system vbmeta from system.img into system-vbmeta.img.
126     ExtractVBMetaImage(system_path, "system-vbmeta.img");
127     EXPECT_EQ(
128             "Minimum libavb version:   1.0\n"
129             "Header Block:             256 bytes\n"
130             "Authentication Block:     1088 bytes\n"
131             "Auxiliary Block:          2304 bytes\n"
132             "Public key (sha1):        5227b569de003adc7f8ec3fc03e05dfbd969abad\n"
133             "Algorithm:                SHA512_RSA8192\n"
134             "Rollback Index:           20\n"
135             "Flags:                    0\n"
136             "Rollback Index Location:  0\n"
137             "Release String:           'unit test'\n"
138             "Descriptors:\n"
139             "    Hashtree descriptor:\n"
140             "      Version of dm-verity:  1\n"
141             "      Image Size:            52428800 bytes\n"
142             "      Tree Offset:           52428800\n"
143             "      Tree Size:             413696 bytes\n"
144             "      Data Block Size:       4096 bytes\n"
145             "      Hash Block Size:       4096 bytes\n"
146             "      FEC num roots:         2\n"
147             "      FEC offset:            52842496\n"
148             "      FEC size:              417792 bytes\n"
149             "      Hash Algorithm:        sha1\n"
150             "      Partition Name:        system\n"
151             "      Salt:                  d00df00d\n"
152             "      Root Digest:           d20d40c02298e385ab6d398a61a3b91dc9947d99\n"
153             "      Flags:                 0\n",
154             InfoImage("system-vbmeta.img"));
155 }
156 
TEST_F(BaseFsAvbTest,GenerateVBMetaImageWithDescriptors)157 TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithDescriptors) {
158     // Generates a raw boot.img
159     const size_t boot_image_size = 5 * 1024 * 1024;
160     const size_t boot_partition_size = 10 * 1024 * 1024;
161     base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
162     // Adds AVB Hash Footer.
163     AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA4096", 10,
164                  data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
165                  "--internal_release_string \"unit test\"");
166 
167     // Generates a raw system.img, use a smaller size to speed-up unit test.
168     const size_t system_image_size = 10 * 1024 * 1024;
169     const size_t system_partition_size = 15 * 1024 * 1024;
170     base::FilePath system_path = GenerateImage("system.img", system_image_size);
171     // Adds AVB Hashtree Footer.
172     AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA8192", 20,
173                  data_dir_.Append("testkey_rsa8192.pem"), "d00df00d",
174                  "--internal_release_string \"unit test\"");
175 
176     // Makes a vbmeta.img including both 'boot' and 'system' descriptors.
177     GenerateVBMetaImage("vbmeta.img", "SHA256_RSA2048", 0, data_dir_.Append("testkey_rsa2048.pem"),
178                         {boot_path, system_path}, /* include_descriptor_image_paths */
179                         {},                       /* chain_partitions */
180                         "--internal_release_string \"unit test\"");
181     EXPECT_EQ("a069cbfc30c816cddf3b53f1ad53b7ca5d61a3d93845eb596bbb1b40caa1c62f",
182               CalcVBMetaDigest("vbmeta.img", "sha256"));
183     EXPECT_EQ(
184             "Minimum libavb version:   1.0\n"
185             "Header Block:             256 bytes\n"
186             "Authentication Block:     320 bytes\n"
187             "Auxiliary Block:          960 bytes\n"
188             "Public key (sha1):        cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
189             "Algorithm:                SHA256_RSA2048\n"
190             "Rollback Index:           0\n"
191             "Flags:                    0\n"
192             "Rollback Index Location:  0\n"
193             "Release String:           'unit test'\n"
194             "Descriptors:\n"
195             "    Hash descriptor:\n"
196             "      Image Size:            5242880 bytes\n"
197             "      Hash Algorithm:        sha256\n"
198             "      Partition Name:        boot\n"
199             "      Salt:                  d00df00d\n"
200             "      Digest:                "
201             "222dd01e98284a1fcd7781f85d1392e43a530511a64eff96db197db90ebc4df1\n"
202             "      Flags:                 0\n"
203             "    Hashtree descriptor:\n"
204             "      Version of dm-verity:  1\n"
205             "      Image Size:            10485760 bytes\n"
206             "      Tree Offset:           10485760\n"
207             "      Tree Size:             86016 bytes\n"
208             "      Data Block Size:       4096 bytes\n"
209             "      Hash Block Size:       4096 bytes\n"
210             "      FEC num roots:         2\n"
211             "      FEC offset:            10571776\n"
212             "      FEC size:              90112 bytes\n"
213             "      Hash Algorithm:        sha1\n"
214             "      Partition Name:        system\n"
215             "      Salt:                  d00df00d\n"
216             "      Root Digest:           a3d5dd307341393d85de356c384ff543ec1ed81b\n"
217             "      Flags:                 0\n",
218             InfoImage("vbmeta.img"));
219 }
220 
TEST_F(BaseFsAvbTest,GenerateVBMetaImageWithChainDescriptors)221 TEST_F(BaseFsAvbTest, GenerateVBMetaImageWithChainDescriptors) {
222     // Generates a raw boot.img
223     const size_t boot_image_size = 5 * 1024 * 1024;
224     const size_t boot_partition_size = 10 * 1024 * 1024;
225     base::FilePath boot_path = GenerateImage("boot.img", boot_image_size);
226     // Adds AVB Hash Footer.
227     AddAvbFooter(boot_path, "hash", "boot", boot_partition_size, "SHA256_RSA2048", 10,
228                  data_dir_.Append("testkey_rsa2048.pem"), "d00df00d",
229                  "--internal_release_string \"unit test\"");
230 
231     // Generates a raw system.img, use a smaller size to speed-up unit test.
232     const size_t system_image_size = 10 * 1024 * 1024;
233     const size_t system_partition_size = 15 * 1024 * 1024;
234     base::FilePath system_path = GenerateImage("system.img", system_image_size);
235     // Adds AVB Hashtree Footer.
236     AddAvbFooter(system_path, "hashtree", "system", system_partition_size, "SHA512_RSA4096", 20,
237                  data_dir_.Append("testkey_rsa4096.pem"), "d00df00d",
238                  "--internal_release_string \"unit test\"");
239 
240     // Make a vbmeta image with chain partitions.
241     base::FilePath rsa2048_public_key =
242             ExtractPublicKeyAvb(data_dir_.Append("testkey_rsa2048.pem"));
243     base::FilePath rsa4096_public_key =
244             ExtractPublicKeyAvb(data_dir_.Append("testkey_rsa4096.pem"));
245     GenerateVBMetaImage("vbmeta.img", "SHA256_RSA8192", 0, data_dir_.Append("testkey_rsa8192.pem"),
246                         {},                               /* include_descriptor_image_paths */
247                         {{"boot", 1, rsa2048_public_key}, /* chain_partitions */
248                          {"system", 2, rsa4096_public_key}},
249                         "--internal_release_string \"unit test\"");
250 
251     // vbmeta digest calculation includes the chained vbmeta from boot.img and system.img.
252     EXPECT_EQ("abbe11b316901f3336e26630f64c4732dadbe14532186ac8640e4141a403721f",
253               CalcVBMetaDigest("vbmeta.img", "sha256"));
254     EXPECT_EQ(
255             "Minimum libavb version:   1.0\n"
256             "Header Block:             256 bytes\n"
257             "Authentication Block:     1088 bytes\n"
258             "Auxiliary Block:          3840 bytes\n"
259             "Public key (sha1):        5227b569de003adc7f8ec3fc03e05dfbd969abad\n"
260             "Algorithm:                SHA256_RSA8192\n"
261             "Rollback Index:           0\n"
262             "Flags:                    0\n"
263             "Rollback Index Location:  0\n"
264             "Release String:           'unit test'\n"
265             "Descriptors:\n"
266             "    Chain Partition descriptor:\n"
267             "      Partition Name:          boot\n"
268             "      Rollback Index Location: 1\n"
269             "      Public key (sha1):       cdbb77177f731920bbe0a0f94f84d9038ae0617d\n"
270             "      Flags:                   0\n"
271             "    Chain Partition descriptor:\n"
272             "      Partition Name:          system\n"
273             "      Rollback Index Location: 2\n"
274             "      Public key (sha1):       2597c218aae470a130f61162feaae70afd97f011\n"
275             "      Flags:                   0\n",
276             InfoImage("vbmeta.img"));
277 }
278 
279 }  // namespace fs_avb_host_test
280 
main(int argc,char ** argv)281 int main(int argc, char** argv) {
282     ::testing::InitGoogleTest(&argc, argv);
283     return RUN_ALL_TESTS();
284 }
285