Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 1 | # |
| 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 Bao | 7119751 | 2018-10-11 14:08:45 -0700 | [diff] [blame] | 19 | import math |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 20 | import os.path |
Tao Bao | 7119751 | 2018-10-11 14:08:45 -0700 | [diff] [blame] | 21 | import random |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 22 | |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 23 | import common |
| 24 | import sparse_img |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 25 | from rangelib import RangeSet |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 26 | from test_utils import get_testdata_dir, ReleaseToolsTestCase |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 27 | from verity_utils import ( |
Tao Bao | 7119751 | 2018-10-11 14:08:45 -0700 | [diff] [blame] | 28 | AdjustPartitionSizeForVerity, AVBCalcMinPartitionSize, BLOCK_SIZE, |
| 29 | CreateHashtreeInfoGenerator, HashtreeInfo, MakeVerityEnabledImage, |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 30 | VerifiedBootVersion1HashtreeInfoGenerator) |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 31 | |
| 32 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 33 | class VerifiedBootVersion1HashtreeInfoGeneratorTest(ReleaseToolsTestCase): |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 34 | |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 35 | def setUp(self): |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 36 | self.testdata_dir = get_testdata_dir() |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 37 | |
| 38 | self.partition_size = 1024 * 1024 |
| 39 | self.prop_dict = { |
| 40 | 'verity': 'true', |
| 41 | 'verity_fec': 'true', |
| 42 | 'system_verity_block_device': '/dev/block/system', |
| 43 | 'system_size': self.partition_size |
| 44 | } |
| 45 | |
| 46 | self.hash_algorithm = "sha256" |
| 47 | self.fixed_salt = \ |
| 48 | "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7" |
| 49 | self.expected_root_hash = \ |
| 50 | "0b7c4565e87b1026e11fbab91c0bc29e185c847a5b44d40e6e86e461e8adf80d" |
| 51 | |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 52 | def _create_simg(self, raw_data): |
| 53 | output_file = common.MakeTempFile() |
| 54 | raw_image = common.MakeTempFile() |
| 55 | with open(raw_image, 'wb') as f: |
| 56 | f.write(raw_data) |
| 57 | |
| 58 | cmd = ["img2simg", raw_image, output_file, '4096'] |
| 59 | p = common.Run(cmd) |
| 60 | p.communicate() |
| 61 | self.assertEqual(0, p.returncode) |
| 62 | |
| 63 | return output_file |
| 64 | |
| 65 | def _generate_image(self): |
| 66 | partition_size = 1024 * 1024 |
Tao Bao | 7119751 | 2018-10-11 14:08:45 -0700 | [diff] [blame] | 67 | adjusted_size, verity_size = AdjustPartitionSizeForVerity( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 68 | partition_size, True) |
| 69 | |
| 70 | raw_image = "" |
| 71 | for i in range(adjusted_size): |
| 72 | raw_image += str(i % 10) |
| 73 | |
| 74 | output_file = self._create_simg(raw_image) |
| 75 | |
| 76 | # Append the verity metadata. |
| 77 | prop_dict = { |
| 78 | 'partition_size': str(partition_size), |
| 79 | 'image_size': str(adjusted_size), |
| 80 | 'verity_block_device': '/dev/block/system', |
| 81 | 'verity_key': os.path.join(self.testdata_dir, 'testkey'), |
| 82 | 'verity_signer_cmd': 'verity_signer', |
| 83 | 'verity_size': str(verity_size), |
| 84 | } |
Tao Bao | 7119751 | 2018-10-11 14:08:45 -0700 | [diff] [blame] | 85 | MakeVerityEnabledImage(output_file, True, prop_dict) |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 86 | |
| 87 | return output_file |
| 88 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 89 | def test_CreateHashtreeInfoGenerator(self): |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 90 | image_file = sparse_img.SparseImage(self._generate_image()) |
| 91 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 92 | generator = CreateHashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 93 | 'system', image_file, self.prop_dict) |
| 94 | self.assertEqual( |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 95 | VerifiedBootVersion1HashtreeInfoGenerator, type(generator)) |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 96 | self.assertEqual(self.partition_size, generator.partition_size) |
| 97 | self.assertTrue(generator.fec_supported) |
| 98 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 99 | def test_DecomposeSparseImage(self): |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 100 | image_file = sparse_img.SparseImage(self._generate_image()) |
| 101 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 102 | generator = VerifiedBootVersion1HashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 103 | self.partition_size, 4096, True) |
| 104 | generator.DecomposeSparseImage(image_file) |
| 105 | self.assertEqual(991232, generator.filesystem_size) |
| 106 | self.assertEqual(12288, generator.hashtree_size) |
| 107 | self.assertEqual(32768, generator.metadata_size) |
| 108 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 109 | def test_ParseHashtreeMetadata(self): |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 110 | image_file = sparse_img.SparseImage(self._generate_image()) |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 111 | generator = VerifiedBootVersion1HashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 112 | self.partition_size, 4096, True) |
| 113 | generator.DecomposeSparseImage(image_file) |
| 114 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 115 | # pylint: disable=protected-access |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 116 | generator._ParseHashtreeMetadata() |
| 117 | |
| 118 | self.assertEqual( |
| 119 | self.hash_algorithm, generator.hashtree_info.hash_algorithm) |
| 120 | self.assertEqual(self.fixed_salt, generator.hashtree_info.salt) |
| 121 | self.assertEqual(self.expected_root_hash, generator.hashtree_info.root_hash) |
| 122 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 123 | def test_ValidateHashtree_smoke(self): |
| 124 | generator = VerifiedBootVersion1HashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 125 | self.partition_size, 4096, True) |
| 126 | generator.image = sparse_img.SparseImage(self._generate_image()) |
| 127 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 128 | generator.hashtree_info = info = HashtreeInfo() |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 129 | info.filesystem_range = RangeSet(data=[0, 991232 / 4096]) |
| 130 | info.hashtree_range = RangeSet( |
| 131 | data=[991232 / 4096, (991232 + 12288) / 4096]) |
| 132 | info.hash_algorithm = self.hash_algorithm |
| 133 | info.salt = self.fixed_salt |
| 134 | info.root_hash = self.expected_root_hash |
| 135 | |
| 136 | self.assertTrue(generator.ValidateHashtree()) |
| 137 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 138 | def test_ValidateHashtree_failure(self): |
| 139 | generator = VerifiedBootVersion1HashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 140 | self.partition_size, 4096, True) |
| 141 | generator.image = sparse_img.SparseImage(self._generate_image()) |
| 142 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 143 | generator.hashtree_info = info = HashtreeInfo() |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 144 | info.filesystem_range = RangeSet(data=[0, 991232 / 4096]) |
| 145 | info.hashtree_range = RangeSet( |
| 146 | data=[991232 / 4096, (991232 + 12288) / 4096]) |
| 147 | info.hash_algorithm = self.hash_algorithm |
| 148 | info.salt = self.fixed_salt |
| 149 | info.root_hash = "a" + self.expected_root_hash[1:] |
| 150 | |
| 151 | self.assertFalse(generator.ValidateHashtree()) |
| 152 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 153 | def test_Generate(self): |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 154 | image_file = sparse_img.SparseImage(self._generate_image()) |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame] | 155 | generator = CreateHashtreeInfoGenerator('system', 4096, self.prop_dict) |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 156 | info = generator.Generate(image_file) |
| 157 | |
| 158 | self.assertEqual(RangeSet(data=[0, 991232 / 4096]), info.filesystem_range) |
| 159 | self.assertEqual(RangeSet(data=[991232 / 4096, (991232 + 12288) / 4096]), |
| 160 | info.hashtree_range) |
| 161 | self.assertEqual(self.hash_algorithm, info.hash_algorithm) |
| 162 | self.assertEqual(self.fixed_salt, info.salt) |
| 163 | self.assertEqual(self.expected_root_hash, info.root_hash) |
Tao Bao | 7119751 | 2018-10-11 14:08:45 -0700 | [diff] [blame] | 164 | |
| 165 | |
| 166 | class VerityUtilsTest(ReleaseToolsTestCase): |
| 167 | |
| 168 | def setUp(self): |
| 169 | # To test AVBCalcMinPartitionSize(), by using 200MB to 2GB image size. |
| 170 | # - 51200 = 200MB * 1024 * 1024 / 4096 |
| 171 | # - 524288 = 2GB * 1024 * 1024 * 1024 / 4096 |
| 172 | self._image_sizes = [BLOCK_SIZE * random.randint(51200, 524288) + offset |
| 173 | for offset in range(BLOCK_SIZE)] |
| 174 | |
| 175 | def test_AVBCalcMinPartitionSize_LinearFooterSize(self): |
| 176 | """Tests with footer size which is linear to partition size.""" |
| 177 | for image_size in self._image_sizes: |
| 178 | for ratio in 0.95, 0.56, 0.22: |
| 179 | expected_size = common.RoundUpTo4K(int(math.ceil(image_size / ratio))) |
| 180 | self.assertEqual( |
| 181 | expected_size, |
| 182 | AVBCalcMinPartitionSize( |
| 183 | image_size, lambda x, ratio=ratio: int(x * ratio))) |
| 184 | |
| 185 | def test_AVBCalcMinPartitionSize_SlowerGrowthFooterSize(self): |
| 186 | """Tests with footer size which grows slower than partition size.""" |
| 187 | |
| 188 | def _SizeCalculator(partition_size): |
| 189 | """Footer size is the power of 0.95 of partition size.""" |
| 190 | # Minus footer size to return max image size. |
| 191 | return partition_size - int(math.pow(partition_size, 0.95)) |
| 192 | |
| 193 | for image_size in self._image_sizes: |
| 194 | min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator) |
| 195 | # Checks min_partition_size can accommodate image_size. |
| 196 | self.assertGreaterEqual( |
| 197 | _SizeCalculator(min_partition_size), |
| 198 | image_size) |
| 199 | # Checks min_partition_size (round to BLOCK_SIZE) is the minimum. |
| 200 | self.assertLess( |
| 201 | _SizeCalculator(min_partition_size - BLOCK_SIZE), |
| 202 | image_size) |
| 203 | |
| 204 | def test_AVBCalcMinPartitionSize_FasterGrowthFooterSize(self): |
| 205 | """Tests with footer size which grows faster than partition size.""" |
| 206 | |
| 207 | def _SizeCalculator(partition_size): |
| 208 | """Max image size is the power of 0.95 of partition size.""" |
| 209 | # Max image size grows less than partition size, which means |
| 210 | # footer size grows faster than partition size. |
| 211 | return int(math.pow(partition_size, 0.95)) |
| 212 | |
| 213 | for image_size in self._image_sizes: |
| 214 | min_partition_size = AVBCalcMinPartitionSize(image_size, _SizeCalculator) |
| 215 | # Checks min_partition_size can accommodate image_size. |
| 216 | self.assertGreaterEqual( |
| 217 | _SizeCalculator(min_partition_size), |
| 218 | image_size) |
| 219 | # Checks min_partition_size (round to BLOCK_SIZE) is the minimum. |
| 220 | self.assertLess( |
| 221 | _SizeCalculator(min_partition_size - BLOCK_SIZE), |
| 222 | image_size) |