blob: 07284ade8c2c28be65ea84fefb642d545014b64e [file] [log] [blame]
Tao Bao1cd59f22019-03-15 15:13:01 -07001#
2# Copyright (C) 2019 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
17import os
18import os.path
Tianjie Xu83bd55c2020-01-29 11:37:43 -080019import zipfile
Tao Bao1cd59f22019-03-15 15:13:01 -070020
21import apex_utils
22import common
23import test_utils
24
25
26class ApexUtilsTest(test_utils.ReleaseToolsTestCase):
27
28 # echo "foo" | sha256sum
29 SALT = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'
30
31 def setUp(self):
32 self.testdata_dir = test_utils.get_testdata_dir()
33 # The default payload signing key.
34 self.payload_key = os.path.join(self.testdata_dir, 'testkey.key')
35
Tianjie Xu88a759d2020-01-23 10:47:54 -080036 common.OPTIONS.search_path = test_utils.get_search_path()
37
Tao Bao1cd59f22019-03-15 15:13:01 -070038 @staticmethod
39 def _GetTestPayload():
40 payload_file = common.MakeTempFile(prefix='apex-', suffix='.img')
41 with open(payload_file, 'wb') as payload_fp:
42 payload_fp.write(os.urandom(8192))
43 return payload_file
44
Tao Bao82490d32019-04-09 00:12:30 -070045 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070046 def test_ParseApexPayloadInfo(self):
47 payload_file = self._GetTestPayload()
48 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070049 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -070050 self.SALT, no_hashtree=True)
Tao Bao1ac886e2019-06-26 11:58:22 -070051 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
Tao Bao1cd59f22019-03-15 15:13:01 -070052 self.assertEqual('SHA256_RSA2048', payload_info['Algorithm'])
53 self.assertEqual(self.SALT, payload_info['Salt'])
54 self.assertEqual('testkey', payload_info['apex.key'])
Tao Bao448004a2019-09-19 07:55:02 -070055 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070056
Tao Bao82490d32019-04-09 00:12:30 -070057 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070058 def test_SignApexPayload(self):
59 payload_file = self._GetTestPayload()
60 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070061 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -070062 self.SALT, no_hashtree=True)
63 apex_utils.VerifyApexPayload(
64 'avbtool', payload_file, self.payload_key, True)
65
66 @test_utils.SkipIfExternalToolsUnavailable()
67 def test_SignApexPayload_withHashtree(self):
68 payload_file = self._GetTestPayload()
69 apex_utils.SignApexPayload(
70 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
71 self.SALT, no_hashtree=False)
Tao Bao1ac886e2019-06-26 11:58:22 -070072 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key)
Tao Bao448004a2019-09-19 07:55:02 -070073 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
74 self.assertEqual('4096 bytes', payload_info['Tree Size'])
75
76 @test_utils.SkipIfExternalToolsUnavailable()
77 def test_SignApexPayload_noHashtree(self):
78 payload_file = self._GetTestPayload()
79 apex_utils.SignApexPayload(
80 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
81 self.SALT, no_hashtree=True)
82 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key,
83 no_hashtree=True)
84 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
85 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070086
Tao Bao82490d32019-04-09 00:12:30 -070087 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070088 def test_SignApexPayload_withSignerHelper(self):
89 payload_file = self._GetTestPayload()
Tao Bao30e31142019-04-09 00:12:30 -070090 signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh')
91 os.chmod(signing_helper, 0o700)
Tao Bao1cd59f22019-03-15 15:13:01 -070092 payload_signer_args = '--signing_helper_with_files {}'.format(
Tao Bao30e31142019-04-09 00:12:30 -070093 signing_helper)
Tao Bao1cd59f22019-03-15 15:13:01 -070094 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070095 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -070096 payload_file,
97 self.payload_key,
98 'testkey', 'SHA256_RSA2048', self.SALT,
Tao Bao448004a2019-09-19 07:55:02 -070099 True,
Tao Bao1cd59f22019-03-15 15:13:01 -0700100 payload_signer_args)
Tao Bao448004a2019-09-19 07:55:02 -0700101 apex_utils.VerifyApexPayload(
102 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700103
Tao Bao82490d32019-04-09 00:12:30 -0700104 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700105 def test_SignApexPayload_invalidKey(self):
106 self.assertRaises(
107 apex_utils.ApexSigningError,
108 apex_utils.SignApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700109 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700110 self._GetTestPayload(),
111 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
112 'testkey',
113 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -0700114 self.SALT,
115 no_hashtree=True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700116
Tao Bao82490d32019-04-09 00:12:30 -0700117 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700118 def test_VerifyApexPayload_wrongKey(self):
119 payload_file = self._GetTestPayload()
120 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -0700121 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -0700122 self.SALT, True)
123 apex_utils.VerifyApexPayload(
124 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700125 self.assertRaises(
126 apex_utils.ApexSigningError,
127 apex_utils.VerifyApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700128 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700129 payload_file,
Tao Bao448004a2019-09-19 07:55:02 -0700130 os.path.join(self.testdata_dir, 'testkey_with_passwd.key'),
131 no_hashtree=True)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800132
133 @test_utils.SkipIfExternalToolsUnavailable()
134 def test_ApexApkSigner_noApkPresent(self):
135 apex_path = os.path.join(self.testdata_dir, 'foo.apex')
136 signer = apex_utils.ApexApkSigner(apex_path, None, None)
137 processed_apex = signer.ProcessApexFile({}, self.payload_key,
138 None)
139 self.assertEqual(apex_path, processed_apex)
140
141 @test_utils.SkipIfExternalToolsUnavailable()
142 def test_ApexApkSigner_apkKeyNotPresent(self):
143 apex_path = os.path.join(self.testdata_dir, 'has_apk.apex')
144 signer = apex_utils.ApexApkSigner(apex_path, None, None)
145 self.assertRaises(apex_utils.ApexSigningError, signer.ProcessApexFile, {},
146 self.payload_key, None)
147
148 @test_utils.SkipIfExternalToolsUnavailable()
149 def test_ApexApkSigner_signApk(self):
150 apex_path = os.path.join(self.testdata_dir, 'has_apk.apex')
151 signer = apex_utils.ApexApkSigner(apex_path, None, None)
152 apk_keys = {'wifi-service-resources.apk': os.path.join(
153 self.testdata_dir, 'testkey')}
154
155 self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
156 payload_pubkey = common.ExtractAvbPublicKey('avbtool',
157 self.payload_key)
158 signer.ProcessApexFile(apk_keys, self.payload_key, payload_pubkey)
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800159
160 @test_utils.SkipIfExternalToolsUnavailable()
161 def test_ApexApkSigner_noAssetDir(self):
162 apex_path = os.path.join(self.testdata_dir, 'has_apk.apex')
163 no_asset = common.MakeTempFile(suffix='.apex')
164 with zipfile.ZipFile(no_asset, 'w') as output_zip:
165 with zipfile.ZipFile(apex_path, 'r') as input_zip:
166 name_list = input_zip.namelist()
167 for name in name_list:
168 if not name.startswith('assets'):
169 output_zip.writestr(name, input_zip.read(name))
170
171 signer = apex_utils.ApexApkSigner(no_asset, None, None)
172 apk_keys = {'wifi-service-resources.apk': os.path.join(
173 self.testdata_dir, 'testkey')}
174
175 self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
176 payload_pubkey = common.ExtractAvbPublicKey('avbtool',
177 self.payload_key)
178 signer.ProcessApexFile(apk_keys, self.payload_key, payload_pubkey)
Tianjie Xucea6ad12020-01-30 17:12:05 -0800179
180 @test_utils.SkipIfExternalToolsUnavailable()
181 def test_ApexApkSigner_withSignerHelper(self):
182 apex_path = os.path.join(self.testdata_dir, 'has_apk.apex')
183 signer = apex_utils.ApexApkSigner(apex_path, None, None)
184 apk_keys = {'wifi-service-resources.apk': os.path.join(
185 self.testdata_dir, 'testkey')}
186
187 self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
188 payload_pubkey = common.ExtractAvbPublicKey('avbtool', self.payload_key)
189
190 signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh')
191 os.chmod(signing_helper, 0o700)
Tianjie Xu0e38d642020-01-31 12:11:12 -0800192 payload_signer_args = '--signing_helper_with_files={}'.format(
Tianjie Xucea6ad12020-01-30 17:12:05 -0800193 signing_helper)
194 signer.ProcessApexFile(apk_keys, self.payload_key, payload_pubkey,
195 payload_signer_args)