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 | |
| 19 | from __future__ import print_function |
| 20 | |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 21 | import os.path |
| 22 | import unittest |
| 23 | |
| 24 | import build_image |
| 25 | import common |
| 26 | import sparse_img |
| 27 | import test_utils |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 28 | from rangelib import RangeSet |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 29 | from verity_utils import ( |
| 30 | CreateHashtreeInfoGenerator, HashtreeInfo, |
| 31 | VerifiedBootVersion1HashtreeInfoGenerator) |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 32 | |
| 33 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 34 | class VerifiedBootVersion1HashtreeInfoGeneratorTest(unittest.TestCase): |
| 35 | |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 36 | def setUp(self): |
| 37 | self.testdata_dir = test_utils.get_testdata_dir() |
| 38 | |
| 39 | self.partition_size = 1024 * 1024 |
| 40 | self.prop_dict = { |
| 41 | 'verity': 'true', |
| 42 | 'verity_fec': 'true', |
| 43 | 'system_verity_block_device': '/dev/block/system', |
| 44 | 'system_size': self.partition_size |
| 45 | } |
| 46 | |
| 47 | self.hash_algorithm = "sha256" |
| 48 | self.fixed_salt = \ |
| 49 | "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7" |
| 50 | self.expected_root_hash = \ |
| 51 | "0b7c4565e87b1026e11fbab91c0bc29e185c847a5b44d40e6e86e461e8adf80d" |
| 52 | |
| 53 | def tearDown(self): |
| 54 | common.Cleanup() |
| 55 | |
| 56 | def _create_simg(self, raw_data): |
| 57 | 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'] |
| 63 | p = common.Run(cmd) |
| 64 | p.communicate() |
| 65 | self.assertEqual(0, p.returncode) |
| 66 | |
| 67 | return output_file |
| 68 | |
| 69 | def _generate_image(self): |
| 70 | partition_size = 1024 * 1024 |
| 71 | adjusted_size, verity_size = build_image.AdjustPartitionSizeForVerity( |
| 72 | partition_size, True) |
| 73 | |
| 74 | raw_image = "" |
| 75 | for i in range(adjusted_size): |
| 76 | raw_image += str(i % 10) |
| 77 | |
| 78 | output_file = self._create_simg(raw_image) |
| 79 | |
| 80 | # Append the verity metadata. |
| 81 | prop_dict = { |
| 82 | 'partition_size': str(partition_size), |
| 83 | 'image_size': str(adjusted_size), |
| 84 | 'verity_block_device': '/dev/block/system', |
| 85 | 'verity_key': os.path.join(self.testdata_dir, 'testkey'), |
| 86 | 'verity_signer_cmd': 'verity_signer', |
| 87 | 'verity_size': str(verity_size), |
| 88 | } |
| 89 | build_image.MakeVerityEnabledImage(output_file, True, prop_dict) |
| 90 | |
| 91 | return output_file |
| 92 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 93 | def test_CreateHashtreeInfoGenerator(self): |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 94 | image_file = sparse_img.SparseImage(self._generate_image()) |
| 95 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 96 | generator = CreateHashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 97 | 'system', image_file, self.prop_dict) |
| 98 | self.assertEqual( |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 99 | VerifiedBootVersion1HashtreeInfoGenerator, type(generator)) |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 100 | self.assertEqual(self.partition_size, generator.partition_size) |
| 101 | self.assertTrue(generator.fec_supported) |
| 102 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 103 | def test_DecomposeSparseImage(self): |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 104 | image_file = sparse_img.SparseImage(self._generate_image()) |
| 105 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 106 | generator = VerifiedBootVersion1HashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 107 | 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 Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 113 | def test_ParseHashtreeMetadata(self): |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 114 | image_file = sparse_img.SparseImage(self._generate_image()) |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 115 | generator = VerifiedBootVersion1HashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 116 | self.partition_size, 4096, True) |
| 117 | generator.DecomposeSparseImage(image_file) |
| 118 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 119 | # pylint: disable=protected-access |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 120 | generator._ParseHashtreeMetadata() |
| 121 | |
| 122 | self.assertEqual( |
| 123 | self.hash_algorithm, generator.hashtree_info.hash_algorithm) |
| 124 | self.assertEqual(self.fixed_salt, generator.hashtree_info.salt) |
| 125 | self.assertEqual(self.expected_root_hash, generator.hashtree_info.root_hash) |
| 126 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 127 | def test_ValidateHashtree_smoke(self): |
| 128 | generator = VerifiedBootVersion1HashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 129 | self.partition_size, 4096, True) |
| 130 | generator.image = sparse_img.SparseImage(self._generate_image()) |
| 131 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 132 | generator.hashtree_info = info = HashtreeInfo() |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 133 | info.filesystem_range = RangeSet(data=[0, 991232 / 4096]) |
| 134 | info.hashtree_range = RangeSet( |
| 135 | data=[991232 / 4096, (991232 + 12288) / 4096]) |
| 136 | info.hash_algorithm = self.hash_algorithm |
| 137 | info.salt = self.fixed_salt |
| 138 | info.root_hash = self.expected_root_hash |
| 139 | |
| 140 | self.assertTrue(generator.ValidateHashtree()) |
| 141 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 142 | def test_ValidateHashtree_failure(self): |
| 143 | generator = VerifiedBootVersion1HashtreeInfoGenerator( |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 144 | self.partition_size, 4096, True) |
| 145 | generator.image = sparse_img.SparseImage(self._generate_image()) |
| 146 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 147 | generator.hashtree_info = info = HashtreeInfo() |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 148 | info.filesystem_range = RangeSet(data=[0, 991232 / 4096]) |
| 149 | info.hashtree_range = RangeSet( |
| 150 | data=[991232 / 4096, (991232 + 12288) / 4096]) |
| 151 | info.hash_algorithm = self.hash_algorithm |
| 152 | info.salt = self.fixed_salt |
| 153 | info.root_hash = "a" + self.expected_root_hash[1:] |
| 154 | |
| 155 | self.assertFalse(generator.ValidateHashtree()) |
| 156 | |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 157 | def test_Generate(self): |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 158 | image_file = sparse_img.SparseImage(self._generate_image()) |
Tao Bao | 5fe287b | 2018-10-11 14:13:52 -0700 | [diff] [blame^] | 159 | generator = CreateHashtreeInfoGenerator('system', 4096, self.prop_dict) |
Tianjie Xu | 67c7cbb | 2018-08-30 00:32:07 -0700 | [diff] [blame] | 160 | info = generator.Generate(image_file) |
| 161 | |
| 162 | self.assertEqual(RangeSet(data=[0, 991232 / 4096]), info.filesystem_range) |
| 163 | self.assertEqual(RangeSet(data=[991232 / 4096, (991232 + 12288) / 4096]), |
| 164 | info.hashtree_range) |
| 165 | self.assertEqual(self.hash_algorithm, info.hash_algorithm) |
| 166 | self.assertEqual(self.fixed_salt, info.salt) |
| 167 | self.assertEqual(self.expected_root_hash, info.root_hash) |