Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [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 | |
Tao Bao | b4ec6d7 | 2018-03-15 23:21:28 -0700 | [diff] [blame] | 17 | """Unittests for validate_target_files.py.""" |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 18 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 19 | import os |
| 20 | import os.path |
| 21 | import shutil |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 22 | |
| 23 | import build_image |
| 24 | import common |
| 25 | import test_utils |
| 26 | from validate_target_files import ValidateVerifiedBootImages |
| 27 | |
| 28 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame^] | 29 | class ValidateTargetFilesTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 30 | |
| 31 | def setUp(self): |
| 32 | self.testdata_dir = test_utils.get_testdata_dir() |
| 33 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 34 | def _generate_boot_image(self, output_file): |
| 35 | kernel = common.MakeTempFile(prefix='kernel-') |
| 36 | with open(kernel, 'wb') as kernel_fp: |
| 37 | kernel_fp.write(os.urandom(10)) |
| 38 | |
| 39 | cmd = ['mkbootimg', '--kernel', kernel, '-o', output_file] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame] | 40 | proc = common.Run(cmd) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 41 | stdoutdata, _ = proc.communicate() |
| 42 | self.assertEqual( |
| 43 | 0, proc.returncode, |
| 44 | "Failed to run mkbootimg: {}".format(stdoutdata)) |
| 45 | |
| 46 | cmd = ['boot_signer', '/boot', output_file, |
| 47 | os.path.join(self.testdata_dir, 'testkey.pk8'), |
| 48 | os.path.join(self.testdata_dir, 'testkey.x509.pem'), output_file] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame] | 49 | proc = common.Run(cmd) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 50 | stdoutdata, _ = proc.communicate() |
| 51 | self.assertEqual( |
| 52 | 0, proc.returncode, |
| 53 | "Failed to sign boot image with boot_signer: {}".format(stdoutdata)) |
| 54 | |
| 55 | def test_ValidateVerifiedBootImages_bootImage(self): |
| 56 | input_tmp = common.MakeTempDir() |
| 57 | os.mkdir(os.path.join(input_tmp, 'IMAGES')) |
| 58 | boot_image = os.path.join(input_tmp, 'IMAGES', 'boot.img') |
| 59 | self._generate_boot_image(boot_image) |
| 60 | |
| 61 | info_dict = { |
| 62 | 'boot_signer' : 'true', |
| 63 | } |
| 64 | options = { |
| 65 | 'verity_key' : os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 66 | } |
| 67 | ValidateVerifiedBootImages(input_tmp, info_dict, options) |
| 68 | |
| 69 | def test_ValidateVerifiedBootImages_bootImage_wrongKey(self): |
| 70 | input_tmp = common.MakeTempDir() |
| 71 | os.mkdir(os.path.join(input_tmp, 'IMAGES')) |
| 72 | boot_image = os.path.join(input_tmp, 'IMAGES', 'boot.img') |
| 73 | self._generate_boot_image(boot_image) |
| 74 | |
| 75 | info_dict = { |
| 76 | 'boot_signer' : 'true', |
| 77 | } |
| 78 | options = { |
| 79 | 'verity_key' : os.path.join(self.testdata_dir, 'verity.x509.pem'), |
| 80 | } |
| 81 | self.assertRaises( |
| 82 | AssertionError, ValidateVerifiedBootImages, input_tmp, info_dict, |
| 83 | options) |
| 84 | |
| 85 | def test_ValidateVerifiedBootImages_bootImage_corrupted(self): |
| 86 | input_tmp = common.MakeTempDir() |
| 87 | os.mkdir(os.path.join(input_tmp, 'IMAGES')) |
| 88 | boot_image = os.path.join(input_tmp, 'IMAGES', 'boot.img') |
| 89 | self._generate_boot_image(boot_image) |
| 90 | |
| 91 | # Corrupt the late byte of the image. |
| 92 | with open(boot_image, 'r+b') as boot_fp: |
| 93 | boot_fp.seek(-1, os.SEEK_END) |
| 94 | last_byte = boot_fp.read(1) |
| 95 | last_byte = chr(255 - ord(last_byte)) |
| 96 | boot_fp.seek(-1, os.SEEK_END) |
| 97 | boot_fp.write(last_byte) |
| 98 | |
| 99 | info_dict = { |
| 100 | 'boot_signer' : 'true', |
| 101 | } |
| 102 | options = { |
| 103 | 'verity_key' : os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 104 | } |
| 105 | self.assertRaises( |
| 106 | AssertionError, ValidateVerifiedBootImages, input_tmp, info_dict, |
| 107 | options) |
| 108 | |
| 109 | def _generate_system_image(self, output_file): |
| 110 | verity_fec = True |
| 111 | partition_size = 1024 * 1024 |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 112 | image_size, verity_size = build_image.AdjustPartitionSizeForVerity( |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 113 | partition_size, verity_fec) |
| 114 | |
| 115 | # Use an empty root directory. |
| 116 | system_root = common.MakeTempDir() |
Tianjie Xu | 5733222 | 2018-08-15 16:16:21 -0700 | [diff] [blame] | 117 | cmd = ['mkuserimg_mke2fs', '-s', system_root, output_file, 'ext4', |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 118 | '/system', str(image_size), '-j', '0'] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame] | 119 | proc = common.Run(cmd) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 120 | stdoutdata, _ = proc.communicate() |
| 121 | self.assertEqual( |
| 122 | 0, proc.returncode, |
Tianjie Xu | 5733222 | 2018-08-15 16:16:21 -0700 | [diff] [blame] | 123 | "Failed to create system image with mkuserimg_mke2fs: {}".format( |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 124 | stdoutdata)) |
| 125 | |
| 126 | # Append the verity metadata. |
| 127 | prop_dict = { |
Tao Bao | 35f4ebc | 2018-09-27 15:31:11 -0700 | [diff] [blame] | 128 | 'partition_size' : str(partition_size), |
| 129 | 'image_size' : str(image_size), |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 130 | 'verity_block_device' : '/dev/block/system', |
| 131 | 'verity_key' : os.path.join(self.testdata_dir, 'testkey'), |
| 132 | 'verity_signer_cmd' : 'verity_signer', |
| 133 | 'verity_size' : str(verity_size), |
| 134 | } |
Tao Bao | c6bd70a | 2018-09-27 16:58:00 -0700 | [diff] [blame] | 135 | build_image.MakeVerityEnabledImage(output_file, verity_fec, prop_dict) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 136 | |
| 137 | def test_ValidateVerifiedBootImages_systemImage(self): |
| 138 | input_tmp = common.MakeTempDir() |
| 139 | os.mkdir(os.path.join(input_tmp, 'IMAGES')) |
| 140 | system_image = os.path.join(input_tmp, 'IMAGES', 'system.img') |
| 141 | self._generate_system_image(system_image) |
| 142 | |
| 143 | # Pack the verity key. |
| 144 | verity_key_mincrypt = os.path.join( |
| 145 | input_tmp, 'BOOT', 'RAMDISK', 'verity_key') |
| 146 | os.makedirs(os.path.dirname(verity_key_mincrypt)) |
| 147 | shutil.copyfile( |
| 148 | os.path.join(self.testdata_dir, 'testkey_mincrypt'), |
| 149 | verity_key_mincrypt) |
| 150 | |
| 151 | info_dict = { |
| 152 | 'verity' : 'true', |
| 153 | } |
| 154 | options = { |
| 155 | 'verity_key' : os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 156 | 'verity_key_mincrypt' : verity_key_mincrypt, |
| 157 | } |
| 158 | ValidateVerifiedBootImages(input_tmp, info_dict, options) |