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