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