blob: 2aa6f6c58c6130768ca4f4f63059d2c13ba26f23 [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
Tianjiec180a5d2020-03-23 18:14:09 -070017import re
Tao Bao1cd59f22019-03-15 15:13:01 -070018import os
19import os.path
Tianjiec180a5d2020-03-23 18:14:09 -070020import shutil
Tianjie Xu83bd55c2020-01-29 11:37:43 -080021import zipfile
Tao Bao1cd59f22019-03-15 15:13:01 -070022
23import apex_utils
24import common
25import test_utils
26
27
28class ApexUtilsTest(test_utils.ReleaseToolsTestCase):
29
30 # echo "foo" | sha256sum
31 SALT = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'
32
33 def setUp(self):
34 self.testdata_dir = test_utils.get_testdata_dir()
35 # The default payload signing key.
36 self.payload_key = os.path.join(self.testdata_dir, 'testkey.key')
Tianjiec180a5d2020-03-23 18:14:09 -070037 self.apex_with_apk = os.path.join(self.testdata_dir, 'has_apk.apex')
Tao Bao1cd59f22019-03-15 15:13:01 -070038
Tianjie Xu88a759d2020-01-23 10:47:54 -080039 common.OPTIONS.search_path = test_utils.get_search_path()
40
Tao Bao1cd59f22019-03-15 15:13:01 -070041 @staticmethod
42 def _GetTestPayload():
43 payload_file = common.MakeTempFile(prefix='apex-', suffix='.img')
44 with open(payload_file, 'wb') as payload_fp:
45 payload_fp.write(os.urandom(8192))
46 return payload_file
47
Tao Bao82490d32019-04-09 00:12:30 -070048 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070049 def test_ParseApexPayloadInfo(self):
50 payload_file = self._GetTestPayload()
51 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070052 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Jiyong Parka1887f32020-05-19 23:18:03 +090053 self.SALT, 'sha256', no_hashtree=True)
Tao Bao1ac886e2019-06-26 11:58:22 -070054 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
Tao Bao1cd59f22019-03-15 15:13:01 -070055 self.assertEqual('SHA256_RSA2048', payload_info['Algorithm'])
56 self.assertEqual(self.SALT, payload_info['Salt'])
57 self.assertEqual('testkey', payload_info['apex.key'])
Jiyong Parka1887f32020-05-19 23:18:03 +090058 self.assertEqual('sha256', payload_info['Hash Algorithm'])
Tao Bao448004a2019-09-19 07:55:02 -070059 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070060
Tao Bao82490d32019-04-09 00:12:30 -070061 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070062 def test_SignApexPayload(self):
63 payload_file = self._GetTestPayload()
64 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070065 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Jiyong Parka1887f32020-05-19 23:18:03 +090066 self.SALT, 'sha256', no_hashtree=True)
Tao Bao448004a2019-09-19 07:55:02 -070067 apex_utils.VerifyApexPayload(
68 'avbtool', payload_file, self.payload_key, True)
69
70 @test_utils.SkipIfExternalToolsUnavailable()
71 def test_SignApexPayload_withHashtree(self):
72 payload_file = self._GetTestPayload()
73 apex_utils.SignApexPayload(
74 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Jiyong Parka1887f32020-05-19 23:18:03 +090075 self.SALT, 'sha256', no_hashtree=False)
Tao Bao1ac886e2019-06-26 11:58:22 -070076 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key)
Tao Bao448004a2019-09-19 07:55:02 -070077 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
78 self.assertEqual('4096 bytes', payload_info['Tree Size'])
79
80 @test_utils.SkipIfExternalToolsUnavailable()
81 def test_SignApexPayload_noHashtree(self):
82 payload_file = self._GetTestPayload()
83 apex_utils.SignApexPayload(
84 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Jiyong Parka1887f32020-05-19 23:18:03 +090085 self.SALT, 'sha256', no_hashtree=True)
Tao Bao448004a2019-09-19 07:55:02 -070086 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key,
87 no_hashtree=True)
88 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
89 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070090
Tao Bao82490d32019-04-09 00:12:30 -070091 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070092 def test_SignApexPayload_withSignerHelper(self):
93 payload_file = self._GetTestPayload()
Tao Bao30e31142019-04-09 00:12:30 -070094 signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh')
95 os.chmod(signing_helper, 0o700)
Tao Bao1cd59f22019-03-15 15:13:01 -070096 payload_signer_args = '--signing_helper_with_files {}'.format(
Tao Bao30e31142019-04-09 00:12:30 -070097 signing_helper)
Tao Bao1cd59f22019-03-15 15:13:01 -070098 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070099 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700100 payload_file,
101 self.payload_key,
Jiyong Parka1887f32020-05-19 23:18:03 +0900102 'testkey', 'SHA256_RSA2048', self.SALT, 'sha256',
Tao Bao448004a2019-09-19 07:55:02 -0700103 True,
Tao Bao1cd59f22019-03-15 15:13:01 -0700104 payload_signer_args)
Tao Bao448004a2019-09-19 07:55:02 -0700105 apex_utils.VerifyApexPayload(
106 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700107
Tao Bao82490d32019-04-09 00:12:30 -0700108 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700109 def test_SignApexPayload_invalidKey(self):
110 self.assertRaises(
111 apex_utils.ApexSigningError,
112 apex_utils.SignApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700113 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700114 self._GetTestPayload(),
115 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
116 'testkey',
117 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -0700118 self.SALT,
Jiyong Parka1887f32020-05-19 23:18:03 +0900119 'sha256',
Tao Bao448004a2019-09-19 07:55:02 -0700120 no_hashtree=True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700121
Tao Bao82490d32019-04-09 00:12:30 -0700122 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700123 def test_VerifyApexPayload_wrongKey(self):
124 payload_file = self._GetTestPayload()
125 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -0700126 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Jiyong Parka1887f32020-05-19 23:18:03 +0900127 self.SALT, 'sha256', True)
Tao Bao448004a2019-09-19 07:55:02 -0700128 apex_utils.VerifyApexPayload(
129 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700130 self.assertRaises(
131 apex_utils.ApexSigningError,
132 apex_utils.VerifyApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700133 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700134 payload_file,
Tao Bao448004a2019-09-19 07:55:02 -0700135 os.path.join(self.testdata_dir, 'testkey_with_passwd.key'),
136 no_hashtree=True)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800137
138 @test_utils.SkipIfExternalToolsUnavailable()
139 def test_ApexApkSigner_noApkPresent(self):
140 apex_path = os.path.join(self.testdata_dir, 'foo.apex')
141 signer = apex_utils.ApexApkSigner(apex_path, None, None)
Tianjiec180a5d2020-03-23 18:14:09 -0700142 processed_apex = signer.ProcessApexFile({}, self.payload_key)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800143 self.assertEqual(apex_path, processed_apex)
144
145 @test_utils.SkipIfExternalToolsUnavailable()
146 def test_ApexApkSigner_apkKeyNotPresent(self):
Tianjiec180a5d2020-03-23 18:14:09 -0700147 apex_path = common.MakeTempFile(suffix='.apex')
148 shutil.copy(self.apex_with_apk, apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800149 signer = apex_utils.ApexApkSigner(apex_path, None, None)
Tianjiec180a5d2020-03-23 18:14:09 -0700150 self.assertRaises(apex_utils.ApexSigningError, signer.ProcessApexFile,
151 {}, self.payload_key)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800152
153 @test_utils.SkipIfExternalToolsUnavailable()
154 def test_ApexApkSigner_signApk(self):
Tianjiec180a5d2020-03-23 18:14:09 -0700155 apex_path = common.MakeTempFile(suffix='.apex')
156 shutil.copy(self.apex_with_apk, apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800157 signer = apex_utils.ApexApkSigner(apex_path, None, None)
158 apk_keys = {'wifi-service-resources.apk': os.path.join(
159 self.testdata_dir, 'testkey')}
160
161 self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
Tianjiec180a5d2020-03-23 18:14:09 -0700162 apex_file = signer.ProcessApexFile(apk_keys, self.payload_key)
Kelvin Zhangf5f3eaf2020-08-28 17:39:11 -0400163 package_name_extract_cmd = ['aapt2', 'dump', 'badging', apex_file]
Tianjiec180a5d2020-03-23 18:14:09 -0700164 output = common.RunAndCheckOutput(package_name_extract_cmd)
165 for line in output.splitlines():
166 # Sample output from aapt: "package: name='com.google.android.wifi'
167 # versionCode='1' versionName='' platformBuildVersionName='R'
168 # compileSdkVersion='29' compileSdkVersionCodename='R'"
169 match = re.search(r"^package:.* name='([\w|\.]+)'", line, re.IGNORECASE)
170 if match:
171 package_name = match.group(1)
172 self.assertEquals('com.google.android.wifi', package_name)
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800173
174 @test_utils.SkipIfExternalToolsUnavailable()
175 def test_ApexApkSigner_noAssetDir(self):
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800176 no_asset = common.MakeTempFile(suffix='.apex')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400177 with zipfile.ZipFile(no_asset, 'w', allowZip64=True) as output_zip:
178 with zipfile.ZipFile(self.apex_with_apk, 'r', allowZip64=True) as input_zip:
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800179 name_list = input_zip.namelist()
180 for name in name_list:
181 if not name.startswith('assets'):
182 output_zip.writestr(name, input_zip.read(name))
183
184 signer = apex_utils.ApexApkSigner(no_asset, None, None)
185 apk_keys = {'wifi-service-resources.apk': os.path.join(
186 self.testdata_dir, 'testkey')}
187
188 self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
Tianjiec180a5d2020-03-23 18:14:09 -0700189 signer.ProcessApexFile(apk_keys, self.payload_key)
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900190
191 @test_utils.SkipIfExternalToolsUnavailable()
192 def test_ApexApkSigner_invokesCustomSignTool(self):
193 apex_path = common.MakeTempFile(suffix='.apex')
194 shutil.copy(self.apex_with_apk, apex_path)
195 apk_keys = {'wifi-service-resources.apk': os.path.join(
196 self.testdata_dir, 'testkey')}
197 self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
198
199 # pass `false` as a sign_tool to see the invocation error
200 with self.assertRaises(common.ExternalError) as cm:
Kelvin Zhang0d0ca5d2021-12-21 12:31:55 -0800201 signer = apex_utils.ApexApkSigner(
202 apex_path, None, None, sign_tool='false')
203 signer.ProcessApexFile(apk_keys, self.payload_key)
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900204
205 the_exception = cm.exception
Kelvin Zhang0d0ca5d2021-12-21 12:31:55 -0800206 self.assertIn('Failed to run command \'[\'false\'', str(the_exception))