blob: e2a022a64851b56efac40924076e378af16ad258 [file] [log] [blame]
Tianjie Xu67c7cbb2018-08-30 00:32:07 -07001#
2# Copyright (C) 2018 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"""Unittests for verity_utils.py."""
18
Tao Bao4a0d5132018-10-17 22:53:54 -070019import copy
Tao Bao71197512018-10-11 14:08:45 -070020import math
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070021import os.path
Tao Bao71197512018-10-11 14:08:45 -070022import random
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070023
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070024import common
25import sparse_img
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070026from rangelib import RangeSet
Tao Bao82490d32019-04-09 00:12:30 -070027from test_utils import (
28 get_testdata_dir, ReleaseToolsTestCase, SkipIfExternalToolsUnavailable)
Tao Bao5fe287b2018-10-11 14:13:52 -070029from verity_utils import (
Tianjiebbde59f2021-05-03 21:18:56 -070030 CalculateVbmetaDigest, CreateHashtreeInfoGenerator,
31 CreateVerityImageBuilder, HashtreeInfo,
Tao Bao5fe287b2018-10-11 14:13:52 -070032 VerifiedBootVersion1HashtreeInfoGenerator)
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070033
Tao Bao7549e5e2018-10-03 14:23:59 -070034BLOCK_SIZE = common.BLOCK_SIZE
35
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070036
Tao Bao65b94e92018-10-11 21:57:26 -070037class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase):
Tao Bao5fe287b2018-10-11 14:13:52 -070038
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070039 def setUp(self):
Tao Bao65b94e92018-10-11 21:57:26 -070040 self.testdata_dir = get_testdata_dir()
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070041
42 self.partition_size = 1024 * 1024
43 self.prop_dict = {
44 'verity': 'true',
45 'verity_fec': 'true',
46 'system_verity_block_device': '/dev/block/system',
47 'system_size': self.partition_size
48 }
49
50 self.hash_algorithm = "sha256"
Tao Bao9e893c32019-06-20 16:14:55 -070051 self.fixed_salt = (
52 "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7")
53 self.expected_root_hash = (
54 "0b7c4565e87b1026e11fbab91c0bc29e185c847a5b44d40e6e86e461e8adf80d")
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070055
Tao Bao9e893c32019-06-20 16:14:55 -070056 def _CreateSimg(self, raw_data): # pylint: disable=no-self-use
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070057 output_file = common.MakeTempFile()
58 raw_image = common.MakeTempFile()
59 with open(raw_image, 'wb') as f:
60 f.write(raw_data)
61
62 cmd = ["img2simg", raw_image, output_file, '4096']
Tao Bao9e893c32019-06-20 16:14:55 -070063 common.RunAndCheckOutput(cmd)
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070064 return output_file
65
Tao Bao9e893c32019-06-20 16:14:55 -070066 def _GenerateImage(self):
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070067 partition_size = 1024 * 1024
Tao Bao7549e5e2018-10-03 14:23:59 -070068 prop_dict = {
69 'partition_size': str(partition_size),
70 'verity': 'true',
71 'verity_block_device': '/dev/block/system',
72 'verity_key': os.path.join(self.testdata_dir, 'testkey'),
73 'verity_fec': 'true',
74 'verity_signer_cmd': 'verity_signer',
75 }
76 verity_image_builder = CreateVerityImageBuilder(prop_dict)
77 self.assertIsNotNone(verity_image_builder)
78 adjusted_size = verity_image_builder.CalculateMaxImageSize()
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070079
Tao Bao9e893c32019-06-20 16:14:55 -070080 raw_image = bytearray(adjusted_size)
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070081 for i in range(adjusted_size):
Tao Bao9e893c32019-06-20 16:14:55 -070082 raw_image[i] = ord('0') + i % 10
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070083
Tao Bao9e893c32019-06-20 16:14:55 -070084 output_file = self._CreateSimg(raw_image)
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070085
86 # Append the verity metadata.
Tao Bao7549e5e2018-10-03 14:23:59 -070087 verity_image_builder.Build(output_file)
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070088
89 return output_file
90
Tao Bao82490d32019-04-09 00:12:30 -070091 @SkipIfExternalToolsUnavailable()
Tao Bao5fe287b2018-10-11 14:13:52 -070092 def test_CreateHashtreeInfoGenerator(self):
Tao Bao9e893c32019-06-20 16:14:55 -070093 image_file = sparse_img.SparseImage(self._GenerateImage())
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070094
Tao Bao5fe287b2018-10-11 14:13:52 -070095 generator = CreateHashtreeInfoGenerator(
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070096 'system', image_file, self.prop_dict)
97 self.assertEqual(
Tao Bao5fe287b2018-10-11 14:13:52 -070098 VerifiedBootVersion1HashtreeInfoGenerator, type(generator))
Tianjie Xu67c7cbb2018-08-30 00:32:07 -070099 self.assertEqual(self.partition_size, generator.partition_size)
100 self.assertTrue(generator.fec_supported)
101
Tao Bao82490d32019-04-09 00:12:30 -0700102 @SkipIfExternalToolsUnavailable()
Tao Bao5fe287b2018-10-11 14:13:52 -0700103 def test_DecomposeSparseImage(self):
Tao Bao9e893c32019-06-20 16:14:55 -0700104 image_file = sparse_img.SparseImage(self._GenerateImage())
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700105
Tao Bao5fe287b2018-10-11 14:13:52 -0700106 generator = VerifiedBootVersion1HashtreeInfoGenerator(
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700107 self.partition_size, 4096, True)
108 generator.DecomposeSparseImage(image_file)
109 self.assertEqual(991232, generator.filesystem_size)
110 self.assertEqual(12288, generator.hashtree_size)
111 self.assertEqual(32768, generator.metadata_size)
112
Tao Bao82490d32019-04-09 00:12:30 -0700113 @SkipIfExternalToolsUnavailable()
Tao Bao5fe287b2018-10-11 14:13:52 -0700114 def test_ParseHashtreeMetadata(self):
Tao Bao9e893c32019-06-20 16:14:55 -0700115 image_file = sparse_img.SparseImage(self._GenerateImage())
Tao Bao5fe287b2018-10-11 14:13:52 -0700116 generator = VerifiedBootVersion1HashtreeInfoGenerator(
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700117 self.partition_size, 4096, True)
118 generator.DecomposeSparseImage(image_file)
119
Tao Bao5fe287b2018-10-11 14:13:52 -0700120 # pylint: disable=protected-access
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700121 generator._ParseHashtreeMetadata()
122
123 self.assertEqual(
124 self.hash_algorithm, generator.hashtree_info.hash_algorithm)
125 self.assertEqual(self.fixed_salt, generator.hashtree_info.salt)
126 self.assertEqual(self.expected_root_hash, generator.hashtree_info.root_hash)
127
Tao Bao82490d32019-04-09 00:12:30 -0700128 @SkipIfExternalToolsUnavailable()
Tao Bao5fe287b2018-10-11 14:13:52 -0700129 def test_ValidateHashtree_smoke(self):
130 generator = VerifiedBootVersion1HashtreeInfoGenerator(
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700131 self.partition_size, 4096, True)
Tao Bao9e893c32019-06-20 16:14:55 -0700132 generator.image = sparse_img.SparseImage(self._GenerateImage())
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700133
Tao Bao5fe287b2018-10-11 14:13:52 -0700134 generator.hashtree_info = info = HashtreeInfo()
Tao Bao9e893c32019-06-20 16:14:55 -0700135 info.filesystem_range = RangeSet(data=[0, 991232 // 4096])
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700136 info.hashtree_range = RangeSet(
Tao Bao9e893c32019-06-20 16:14:55 -0700137 data=[991232 // 4096, (991232 + 12288) // 4096])
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700138 info.hash_algorithm = self.hash_algorithm
139 info.salt = self.fixed_salt
140 info.root_hash = self.expected_root_hash
141
142 self.assertTrue(generator.ValidateHashtree())
143
Tao Bao82490d32019-04-09 00:12:30 -0700144 @SkipIfExternalToolsUnavailable()
Tao Bao5fe287b2018-10-11 14:13:52 -0700145 def test_ValidateHashtree_failure(self):
146 generator = VerifiedBootVersion1HashtreeInfoGenerator(
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700147 self.partition_size, 4096, True)
Tao Bao9e893c32019-06-20 16:14:55 -0700148 generator.image = sparse_img.SparseImage(self._GenerateImage())
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700149
Tao Bao5fe287b2018-10-11 14:13:52 -0700150 generator.hashtree_info = info = HashtreeInfo()
Tao Bao9e893c32019-06-20 16:14:55 -0700151 info.filesystem_range = RangeSet(data=[0, 991232 // 4096])
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700152 info.hashtree_range = RangeSet(
Tao Bao9e893c32019-06-20 16:14:55 -0700153 data=[991232 // 4096, (991232 + 12288) // 4096])
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700154 info.hash_algorithm = self.hash_algorithm
155 info.salt = self.fixed_salt
156 info.root_hash = "a" + self.expected_root_hash[1:]
157
158 self.assertFalse(generator.ValidateHashtree())
159
Tao Bao82490d32019-04-09 00:12:30 -0700160 @SkipIfExternalToolsUnavailable()
Tao Bao5fe287b2018-10-11 14:13:52 -0700161 def test_Generate(self):
Tao Bao9e893c32019-06-20 16:14:55 -0700162 image_file = sparse_img.SparseImage(self._GenerateImage())
Tao Bao5fe287b2018-10-11 14:13:52 -0700163 generator = CreateHashtreeInfoGenerator('system', 4096, self.prop_dict)
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700164 info = generator.Generate(image_file)
165
Tao Bao9e893c32019-06-20 16:14:55 -0700166 self.assertEqual(RangeSet(data=[0, 991232 // 4096]), info.filesystem_range)
167 self.assertEqual(RangeSet(data=[991232 // 4096, (991232 + 12288) // 4096]),
Tianjie Xu67c7cbb2018-08-30 00:32:07 -0700168 info.hashtree_range)
169 self.assertEqual(self.hash_algorithm, info.hash_algorithm)
170 self.assertEqual(self.fixed_salt, info.salt)
171 self.assertEqual(self.expected_root_hash, info.root_hash)
Tao Bao71197512018-10-11 14:08:45 -0700172
173
Tao Bao4a0d5132018-10-17 22:53:54 -0700174class VerifiedBootVersion1VerityImageBuilderTest(ReleaseToolsTestCase):
175
176 DEFAULT_PARTITION_SIZE = 4096 * 1024
177 DEFAULT_PROP_DICT = {
178 'partition_size': str(DEFAULT_PARTITION_SIZE),
179 'verity': 'true',
180 'verity_block_device': '/dev/block/system',
181 'verity_key': os.path.join(get_testdata_dir(), 'testkey'),
182 'verity_fec': 'true',
183 'verity_signer_cmd': 'verity_signer',
184 }
185
186 def test_init(self):
187 prop_dict = copy.deepcopy(self.DEFAULT_PROP_DICT)
188 verity_image_builder = CreateVerityImageBuilder(prop_dict)
189 self.assertIsNotNone(verity_image_builder)
190 self.assertEqual(1, verity_image_builder.version)
191
192 def test_init_MissingProps(self):
193 prop_dict = copy.deepcopy(self.DEFAULT_PROP_DICT)
194 del prop_dict['verity']
195 self.assertIsNone(CreateVerityImageBuilder(prop_dict))
196
197 prop_dict = copy.deepcopy(self.DEFAULT_PROP_DICT)
198 del prop_dict['verity_block_device']
199 self.assertIsNone(CreateVerityImageBuilder(prop_dict))
200
Tao Bao82490d32019-04-09 00:12:30 -0700201 @SkipIfExternalToolsUnavailable()
Tao Bao4a0d5132018-10-17 22:53:54 -0700202 def test_CalculateMaxImageSize(self):
203 verity_image_builder = CreateVerityImageBuilder(self.DEFAULT_PROP_DICT)
204 size = verity_image_builder.CalculateMaxImageSize()
205 self.assertLess(size, self.DEFAULT_PARTITION_SIZE)
206
207 # Same result by explicitly passing the partition size.
208 self.assertEqual(
209 verity_image_builder.CalculateMaxImageSize(),
210 verity_image_builder.CalculateMaxImageSize(
211 self.DEFAULT_PARTITION_SIZE))
212
213 @staticmethod
214 def _BuildAndVerify(prop, verify_key):
215 verity_image_builder = CreateVerityImageBuilder(prop)
216 image_size = verity_image_builder.CalculateMaxImageSize()
217
218 # Build the sparse image with verity metadata.
219 input_dir = common.MakeTempDir()
220 image = common.MakeTempFile(suffix='.img')
221 cmd = ['mkuserimg_mke2fs', input_dir, image, 'ext4', '/system',
222 str(image_size), '-j', '0', '-s']
223 common.RunAndCheckOutput(cmd)
224 verity_image_builder.Build(image)
225
226 # Verify the verity metadata.
227 cmd = ['verity_verifier', image, '-mincrypt', verify_key]
228 common.RunAndCheckOutput(cmd)
229
Tao Bao82490d32019-04-09 00:12:30 -0700230 @SkipIfExternalToolsUnavailable()
Tao Bao4a0d5132018-10-17 22:53:54 -0700231 def test_Build(self):
232 self._BuildAndVerify(
233 self.DEFAULT_PROP_DICT,
234 os.path.join(get_testdata_dir(), 'testkey_mincrypt'))
235
Tao Bao82490d32019-04-09 00:12:30 -0700236 @SkipIfExternalToolsUnavailable()
Tianjiea85bdf02020-07-29 11:56:19 -0700237 def test_Build_ValidationCheck(self):
Kelvin Zhangc693d952020-07-22 19:21:22 -0400238 # A validity check for the test itself: the image shouldn't be verifiable
Tao Bao4a0d5132018-10-17 22:53:54 -0700239 # with wrong key.
240 self.assertRaises(
241 common.ExternalError,
242 self._BuildAndVerify,
243 self.DEFAULT_PROP_DICT,
244 os.path.join(get_testdata_dir(), 'verity_mincrypt'))
245
Tao Bao82490d32019-04-09 00:12:30 -0700246 @SkipIfExternalToolsUnavailable()
Tao Bao4a0d5132018-10-17 22:53:54 -0700247 def test_Build_FecDisabled(self):
248 prop_dict = copy.deepcopy(self.DEFAULT_PROP_DICT)
249 del prop_dict['verity_fec']
250 self._BuildAndVerify(
251 prop_dict,
252 os.path.join(get_testdata_dir(), 'testkey_mincrypt'))
253
Tao Bao82490d32019-04-09 00:12:30 -0700254 @SkipIfExternalToolsUnavailable()
Tao Bao4a0d5132018-10-17 22:53:54 -0700255 def test_Build_SquashFs(self):
256 verity_image_builder = CreateVerityImageBuilder(self.DEFAULT_PROP_DICT)
257 verity_image_builder.CalculateMaxImageSize()
258
259 # Build the sparse image with verity metadata.
260 input_dir = common.MakeTempDir()
261 image = common.MakeTempFile(suffix='.img')
262 cmd = ['mksquashfsimage.sh', input_dir, image, '-s']
263 common.RunAndCheckOutput(cmd)
264 verity_image_builder.PadSparseImage(image)
265 verity_image_builder.Build(image)
266
267 # Verify the verity metadata.
268 cmd = ["verity_verifier", image, '-mincrypt',
269 os.path.join(get_testdata_dir(), 'testkey_mincrypt')]
270 common.RunAndCheckOutput(cmd)
271
272
Tao Bao7549e5e2018-10-03 14:23:59 -0700273class VerifiedBootVersion2VerityImageBuilderTest(ReleaseToolsTestCase):
Tao Bao71197512018-10-11 14:08:45 -0700274
Tao Bao4a0d5132018-10-17 22:53:54 -0700275 DEFAULT_PROP_DICT = {
276 'partition_size': str(4096 * 1024),
277 'partition_name': 'system',
278 'avb_avbtool': 'avbtool',
279 'avb_hashtree_enable': 'true',
280 'avb_add_hashtree_footer_args': '',
281 }
282
283 def test_init(self):
284 prop_dict = copy.deepcopy(self.DEFAULT_PROP_DICT)
285 verity_image_builder = CreateVerityImageBuilder(prop_dict)
286 self.assertIsNotNone(verity_image_builder)
287 self.assertEqual(2, verity_image_builder.version)
288
289 def test_init_MissingProps(self):
290 prop_dict = copy.deepcopy(self.DEFAULT_PROP_DICT)
291 del prop_dict['avb_hashtree_enable']
292 verity_image_builder = CreateVerityImageBuilder(prop_dict)
293 self.assertIsNone(verity_image_builder)
294
Tao Bao82490d32019-04-09 00:12:30 -0700295 @SkipIfExternalToolsUnavailable()
Tao Bao4a0d5132018-10-17 22:53:54 -0700296 def test_Build(self):
297 prop_dict = copy.deepcopy(self.DEFAULT_PROP_DICT)
298 verity_image_builder = CreateVerityImageBuilder(prop_dict)
299 self.assertIsNotNone(verity_image_builder)
300 self.assertEqual(2, verity_image_builder.version)
301
302 input_dir = common.MakeTempDir()
303 image_dir = common.MakeTempDir()
304 system_image = os.path.join(image_dir, 'system.img')
305 system_image_size = verity_image_builder.CalculateMaxImageSize()
306 cmd = ['mkuserimg_mke2fs', input_dir, system_image, 'ext4', '/system',
307 str(system_image_size), '-j', '0', '-s']
308 common.RunAndCheckOutput(cmd)
309 verity_image_builder.Build(system_image)
310
311 # Additionally make vbmeta image so that we can verify with avbtool.
312 vbmeta_image = os.path.join(image_dir, 'vbmeta.img')
313 cmd = ['avbtool', 'make_vbmeta_image', '--include_descriptors_from_image',
314 system_image, '--output', vbmeta_image]
315 common.RunAndCheckOutput(cmd)
316
317 # Verify the verity metadata.
318 cmd = ['avbtool', 'verify_image', '--image', vbmeta_image]
319 common.RunAndCheckOutput(cmd)
320
321 def _test_CalculateMinPartitionSize_SetUp(self):
Tao Bao7549e5e2018-10-03 14:23:59 -0700322 # To test CalculateMinPartitionSize(), by using 200MB to 2GB image size.
Tao Bao71197512018-10-11 14:08:45 -0700323 # - 51200 = 200MB * 1024 * 1024 / 4096
324 # - 524288 = 2GB * 1024 * 1024 * 1024 / 4096
Tao Bao4a0d5132018-10-17 22:53:54 -0700325 image_sizes = [BLOCK_SIZE * random.randint(51200, 524288) + offset
326 for offset in range(BLOCK_SIZE)]
Tao Bao71197512018-10-11 14:08:45 -0700327
Tao Bao7549e5e2018-10-03 14:23:59 -0700328 prop_dict = {
329 'partition_size': None,
330 'partition_name': 'system',
331 'avb_avbtool': 'avbtool',
332 'avb_hashtree_enable': 'true',
333 'avb_add_hashtree_footer_args': None,
334 }
Tao Bao4a0d5132018-10-17 22:53:54 -0700335 builder = CreateVerityImageBuilder(prop_dict)
336 self.assertEqual(2, builder.version)
337 return image_sizes, builder
Tao Bao7549e5e2018-10-03 14:23:59 -0700338
339 def test_CalculateMinPartitionSize_LinearFooterSize(self):
Tao Bao71197512018-10-11 14:08:45 -0700340 """Tests with footer size which is linear to partition size."""
Tao Bao4a0d5132018-10-17 22:53:54 -0700341 image_sizes, builder = self._test_CalculateMinPartitionSize_SetUp()
342 for image_size in image_sizes:
Tao Bao71197512018-10-11 14:08:45 -0700343 for ratio in 0.95, 0.56, 0.22:
344 expected_size = common.RoundUpTo4K(int(math.ceil(image_size / ratio)))
345 self.assertEqual(
346 expected_size,
Tao Bao4a0d5132018-10-17 22:53:54 -0700347 builder.CalculateMinPartitionSize(
Tao Bao71197512018-10-11 14:08:45 -0700348 image_size, lambda x, ratio=ratio: int(x * ratio)))
349
350 def test_AVBCalcMinPartitionSize_SlowerGrowthFooterSize(self):
351 """Tests with footer size which grows slower than partition size."""
352
353 def _SizeCalculator(partition_size):
354 """Footer size is the power of 0.95 of partition size."""
355 # Minus footer size to return max image size.
356 return partition_size - int(math.pow(partition_size, 0.95))
357
Tao Bao4a0d5132018-10-17 22:53:54 -0700358 image_sizes, builder = self._test_CalculateMinPartitionSize_SetUp()
359 for image_size in image_sizes:
360 min_partition_size = builder.CalculateMinPartitionSize(
Tao Bao7549e5e2018-10-03 14:23:59 -0700361 image_size, _SizeCalculator)
Tao Bao71197512018-10-11 14:08:45 -0700362 # Checks min_partition_size can accommodate image_size.
363 self.assertGreaterEqual(
364 _SizeCalculator(min_partition_size),
365 image_size)
366 # Checks min_partition_size (round to BLOCK_SIZE) is the minimum.
367 self.assertLess(
368 _SizeCalculator(min_partition_size - BLOCK_SIZE),
369 image_size)
370
Tao Bao7549e5e2018-10-03 14:23:59 -0700371 def test_CalculateMinPartitionSize_FasterGrowthFooterSize(self):
Tao Bao71197512018-10-11 14:08:45 -0700372 """Tests with footer size which grows faster than partition size."""
373
374 def _SizeCalculator(partition_size):
375 """Max image size is the power of 0.95 of partition size."""
376 # Max image size grows less than partition size, which means
377 # footer size grows faster than partition size.
378 return int(math.pow(partition_size, 0.95))
379
Tao Bao4a0d5132018-10-17 22:53:54 -0700380 image_sizes, builder = self._test_CalculateMinPartitionSize_SetUp()
381 for image_size in image_sizes:
382 min_partition_size = builder.CalculateMinPartitionSize(
Tao Bao7549e5e2018-10-03 14:23:59 -0700383 image_size, _SizeCalculator)
Tao Bao71197512018-10-11 14:08:45 -0700384 # Checks min_partition_size can accommodate image_size.
385 self.assertGreaterEqual(
386 _SizeCalculator(min_partition_size),
387 image_size)
388 # Checks min_partition_size (round to BLOCK_SIZE) is the minimum.
389 self.assertLess(
390 _SizeCalculator(min_partition_size - BLOCK_SIZE),
391 image_size)
Tianjiebbde59f2021-05-03 21:18:56 -0700392
393 @SkipIfExternalToolsUnavailable()
394 def test_CalculateVbmetaDigest(self):
395 prop_dict = copy.deepcopy(self.DEFAULT_PROP_DICT)
396 verity_image_builder = CreateVerityImageBuilder(prop_dict)
397 self.assertEqual(2, verity_image_builder.version)
398
399 input_dir = common.MakeTempDir()
400 image_dir = common.MakeTempDir()
401 os.mkdir(os.path.join(image_dir, 'IMAGES'))
402 system_image = os.path.join(image_dir, 'IMAGES', 'system.img')
403 system_image_size = verity_image_builder.CalculateMaxImageSize()
404 cmd = ['mkuserimg_mke2fs', input_dir, system_image, 'ext4', '/system',
405 str(system_image_size), '-j', '0', '-s']
406 common.RunAndCheckOutput(cmd)
407 verity_image_builder.Build(system_image)
408
409 # Additionally make vbmeta image
410 vbmeta_image = os.path.join(image_dir, 'IMAGES', 'vbmeta.img')
411 cmd = ['avbtool', 'make_vbmeta_image', '--include_descriptors_from_image',
412 system_image, '--output', vbmeta_image]
413 common.RunAndCheckOutput(cmd)
414
415 # Verify the verity metadata.
416 cmd = ['avbtool', 'verify_image', '--image', vbmeta_image]
417 common.RunAndCheckOutput(cmd)
418 digest = CalculateVbmetaDigest(image_dir, 'avbtool')
419 self.assertIsNotNone(digest)