blob: 0aaf069fc5ad3f7927c5383c9133f883563b8a63 [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
24import subprocess
25import unittest
26
27import build_image
28import common
29import test_utils
30from validate_target_files import ValidateVerifiedBootImages
31
32
33class 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 Bao35f4ebc2018-09-27 15:31:11 -0700119 image_size, verity_size = build_image.AdjustPartitionSizeForVerity(
Tao Baoba557702018-03-10 20:41:16 -0800120 partition_size, verity_fec)
121
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 Baoba557702018-03-10 20:41:16 -0800126 proc = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
127 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.
134 prop_dict = {
Tao Bao35f4ebc2018-09-27 15:31:11 -0700135 'partition_size' : str(partition_size),
136 'image_size' : str(image_size),
Tao Baoba557702018-03-10 20:41:16 -0800137 '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 Baoc6bd70a2018-09-27 16:58:00 -0700142 build_image.MakeVerityEnabledImage(output_file, verity_fec, prop_dict)
Tao Baoba557702018-03-10 20:41:16 -0800143
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)