blob: 7b29ef8e829fcde0148651264cf8d142ead0b16e [file] [log] [blame]
Tao Baoba557702018-03-10 20:41:16 -08001#
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 Baob4ec6d72018-03-15 23:21:28 -070017"""Unittests for validate_target_files.py."""
Tao Baoba557702018-03-10 20:41:16 -080018
Tao Baoba557702018-03-10 20:41:16 -080019import os
20import os.path
21import shutil
Tao Baoba557702018-03-10 20:41:16 -080022
23import build_image
24import common
25import test_utils
26from validate_target_files import ValidateVerifiedBootImages
27
28
Tao Bao65b94e92018-10-11 21:57:26 -070029class ValidateTargetFilesTest(test_utils.ReleaseToolsTestCase):
Tao Baoba557702018-03-10 20:41:16 -080030
31 def setUp(self):
32 self.testdata_dir = test_utils.get_testdata_dir()
33
Tao Baoba557702018-03-10 20:41:16 -080034 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 Bao73dd4f42018-10-04 16:25:33 -070040 proc = common.Run(cmd)
Tao Baoba557702018-03-10 20:41:16 -080041 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 Bao73dd4f42018-10-04 16:25:33 -070049 proc = common.Run(cmd)
Tao Baoba557702018-03-10 20:41:16 -080050 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 Bao35f4ebc2018-09-27 15:31:11 -0700112 image_size, verity_size = build_image.AdjustPartitionSizeForVerity(
Tao Baoba557702018-03-10 20:41:16 -0800113 partition_size, verity_fec)
114
115 # Use an empty root directory.
116 system_root = common.MakeTempDir()
Tianjie Xu57332222018-08-15 16:16:21 -0700117 cmd = ['mkuserimg_mke2fs', '-s', system_root, output_file, 'ext4',
Tao Bao35f4ebc2018-09-27 15:31:11 -0700118 '/system', str(image_size), '-j', '0']
Tao Bao73dd4f42018-10-04 16:25:33 -0700119 proc = common.Run(cmd)
Tao Baoba557702018-03-10 20:41:16 -0800120 stdoutdata, _ = proc.communicate()
121 self.assertEqual(
122 0, proc.returncode,
Tianjie Xu57332222018-08-15 16:16:21 -0700123 "Failed to create system image with mkuserimg_mke2fs: {}".format(
Tao Baoba557702018-03-10 20:41:16 -0800124 stdoutdata))
125
126 # Append the verity metadata.
127 prop_dict = {
Tao Bao35f4ebc2018-09-27 15:31:11 -0700128 'partition_size' : str(partition_size),
129 'image_size' : str(image_size),
Tao Baoba557702018-03-10 20:41:16 -0800130 '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 Baoc6bd70a2018-09-27 16:58:00 -0700135 build_image.MakeVerityEnabledImage(output_file, verity_fec, prop_dict)
Tao Baoba557702018-03-10 20:41:16 -0800136
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)