blob: a6a8876c25f0ac7dd35463117a9ff02b42795a85 [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
Tao Baoba557702018-03-10 20:41:16 -080023import common
24import test_utils
Tao Bao28f201b2018-10-13 19:27:52 -070025import verity_utils
Tao Baoba557702018-03-10 20:41:16 -080026from validate_target_files import ValidateVerifiedBootImages
Tao Bao7549e5e2018-10-03 14:23:59 -070027from verity_utils import CreateVerityImageBuilder
Tao Baoba557702018-03-10 20:41:16 -080028
29
Tao Bao65b94e92018-10-11 21:57:26 -070030class ValidateTargetFilesTest(test_utils.ReleaseToolsTestCase):
Tao Baoba557702018-03-10 20:41:16 -080031
32 def setUp(self):
33 self.testdata_dir = test_utils.get_testdata_dir()
34
Tao Baoba557702018-03-10 20:41:16 -080035 def _generate_boot_image(self, output_file):
36 kernel = common.MakeTempFile(prefix='kernel-')
37 with open(kernel, 'wb') as kernel_fp:
38 kernel_fp.write(os.urandom(10))
39
40 cmd = ['mkbootimg', '--kernel', kernel, '-o', output_file]
Tao Bao73dd4f42018-10-04 16:25:33 -070041 proc = common.Run(cmd)
Tao Baoba557702018-03-10 20:41:16 -080042 stdoutdata, _ = proc.communicate()
43 self.assertEqual(
44 0, proc.returncode,
45 "Failed to run mkbootimg: {}".format(stdoutdata))
46
47 cmd = ['boot_signer', '/boot', output_file,
48 os.path.join(self.testdata_dir, 'testkey.pk8'),
49 os.path.join(self.testdata_dir, 'testkey.x509.pem'), output_file]
Tao Bao73dd4f42018-10-04 16:25:33 -070050 proc = common.Run(cmd)
Tao Baoba557702018-03-10 20:41:16 -080051 stdoutdata, _ = proc.communicate()
52 self.assertEqual(
53 0, proc.returncode,
54 "Failed to sign boot image with boot_signer: {}".format(stdoutdata))
55
56 def test_ValidateVerifiedBootImages_bootImage(self):
57 input_tmp = common.MakeTempDir()
58 os.mkdir(os.path.join(input_tmp, 'IMAGES'))
59 boot_image = os.path.join(input_tmp, 'IMAGES', 'boot.img')
60 self._generate_boot_image(boot_image)
61
62 info_dict = {
63 'boot_signer' : 'true',
64 }
65 options = {
66 'verity_key' : os.path.join(self.testdata_dir, 'testkey.x509.pem'),
67 }
68 ValidateVerifiedBootImages(input_tmp, info_dict, options)
69
70 def test_ValidateVerifiedBootImages_bootImage_wrongKey(self):
71 input_tmp = common.MakeTempDir()
72 os.mkdir(os.path.join(input_tmp, 'IMAGES'))
73 boot_image = os.path.join(input_tmp, 'IMAGES', 'boot.img')
74 self._generate_boot_image(boot_image)
75
76 info_dict = {
77 'boot_signer' : 'true',
78 }
79 options = {
80 'verity_key' : os.path.join(self.testdata_dir, 'verity.x509.pem'),
81 }
82 self.assertRaises(
83 AssertionError, ValidateVerifiedBootImages, input_tmp, info_dict,
84 options)
85
86 def test_ValidateVerifiedBootImages_bootImage_corrupted(self):
87 input_tmp = common.MakeTempDir()
88 os.mkdir(os.path.join(input_tmp, 'IMAGES'))
89 boot_image = os.path.join(input_tmp, 'IMAGES', 'boot.img')
90 self._generate_boot_image(boot_image)
91
92 # Corrupt the late byte of the image.
93 with open(boot_image, 'r+b') as boot_fp:
94 boot_fp.seek(-1, os.SEEK_END)
95 last_byte = boot_fp.read(1)
96 last_byte = chr(255 - ord(last_byte))
97 boot_fp.seek(-1, os.SEEK_END)
98 boot_fp.write(last_byte)
99
100 info_dict = {
101 'boot_signer' : 'true',
102 }
103 options = {
104 'verity_key' : os.path.join(self.testdata_dir, 'testkey.x509.pem'),
105 }
106 self.assertRaises(
107 AssertionError, ValidateVerifiedBootImages, input_tmp, info_dict,
108 options)
109
110 def _generate_system_image(self, output_file):
Tao Bao7549e5e2018-10-03 14:23:59 -0700111 prop_dict = {
112 'partition_size': str(1024 * 1024),
113 'verity': 'true',
114 'verity_block_device': '/dev/block/system',
115 'verity_key' : os.path.join(self.testdata_dir, 'testkey'),
116 'verity_fec': "true",
117 'verity_signer_cmd': 'verity_signer',
118 }
119 verity_image_builder = CreateVerityImageBuilder(prop_dict)
120 image_size = verity_image_builder.CalculateMaxImageSize()
Tao Baoba557702018-03-10 20:41:16 -0800121
122 # Use an empty root directory.
123 system_root = common.MakeTempDir()
Tianjie Xu57332222018-08-15 16:16:21 -0700124 cmd = ['mkuserimg_mke2fs', '-s', system_root, output_file, 'ext4',
Tao Bao35f4ebc2018-09-27 15:31:11 -0700125 '/system', str(image_size), '-j', '0']
Tao Bao73dd4f42018-10-04 16:25:33 -0700126 proc = common.Run(cmd)
Tao Baoba557702018-03-10 20:41:16 -0800127 stdoutdata, _ = proc.communicate()
128 self.assertEqual(
129 0, proc.returncode,
Tianjie Xu57332222018-08-15 16:16:21 -0700130 "Failed to create system image with mkuserimg_mke2fs: {}".format(
Tao Baoba557702018-03-10 20:41:16 -0800131 stdoutdata))
132
133 # Append the verity metadata.
Tao Bao7549e5e2018-10-03 14:23:59 -0700134 verity_image_builder.Build(output_file)
Tao Baoba557702018-03-10 20:41:16 -0800135
136 def test_ValidateVerifiedBootImages_systemImage(self):
137 input_tmp = common.MakeTempDir()
138 os.mkdir(os.path.join(input_tmp, 'IMAGES'))
139 system_image = os.path.join(input_tmp, 'IMAGES', 'system.img')
140 self._generate_system_image(system_image)
141
142 # Pack the verity key.
143 verity_key_mincrypt = os.path.join(
144 input_tmp, 'BOOT', 'RAMDISK', 'verity_key')
145 os.makedirs(os.path.dirname(verity_key_mincrypt))
146 shutil.copyfile(
147 os.path.join(self.testdata_dir, 'testkey_mincrypt'),
148 verity_key_mincrypt)
149
150 info_dict = {
151 'verity' : 'true',
152 }
153 options = {
154 'verity_key' : os.path.join(self.testdata_dir, 'testkey.x509.pem'),
155 'verity_key_mincrypt' : verity_key_mincrypt,
156 }
157 ValidateVerifiedBootImages(input_tmp, info_dict, options)