blob: e19bc90b32ff6e08146e7b0c78602c6a950fe87c [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',
Tao Bao448004a2019-09-19 07:55:02 -070053 self.SALT, 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'])
Tao Bao448004a2019-09-19 07:55:02 -070058 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070059
Tao Bao82490d32019-04-09 00:12:30 -070060 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070061 def test_SignApexPayload(self):
62 payload_file = self._GetTestPayload()
63 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070064 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -070065 self.SALT, no_hashtree=True)
66 apex_utils.VerifyApexPayload(
67 'avbtool', payload_file, self.payload_key, True)
68
69 @test_utils.SkipIfExternalToolsUnavailable()
70 def test_SignApexPayload_withHashtree(self):
71 payload_file = self._GetTestPayload()
72 apex_utils.SignApexPayload(
73 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
74 self.SALT, no_hashtree=False)
Tao Bao1ac886e2019-06-26 11:58:22 -070075 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key)
Tao Bao448004a2019-09-19 07:55:02 -070076 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
77 self.assertEqual('4096 bytes', payload_info['Tree Size'])
78
79 @test_utils.SkipIfExternalToolsUnavailable()
80 def test_SignApexPayload_noHashtree(self):
81 payload_file = self._GetTestPayload()
82 apex_utils.SignApexPayload(
83 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
84 self.SALT, no_hashtree=True)
85 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key,
86 no_hashtree=True)
87 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
88 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070089
Tao Bao82490d32019-04-09 00:12:30 -070090 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070091 def test_SignApexPayload_withSignerHelper(self):
92 payload_file = self._GetTestPayload()
Tao Bao30e31142019-04-09 00:12:30 -070093 signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh')
94 os.chmod(signing_helper, 0o700)
Tao Bao1cd59f22019-03-15 15:13:01 -070095 payload_signer_args = '--signing_helper_with_files {}'.format(
Tao Bao30e31142019-04-09 00:12:30 -070096 signing_helper)
Tao Bao1cd59f22019-03-15 15:13:01 -070097 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070098 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -070099 payload_file,
100 self.payload_key,
101 'testkey', 'SHA256_RSA2048', self.SALT,
Tao Bao448004a2019-09-19 07:55:02 -0700102 True,
Tao Bao1cd59f22019-03-15 15:13:01 -0700103 payload_signer_args)
Tao Bao448004a2019-09-19 07:55:02 -0700104 apex_utils.VerifyApexPayload(
105 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700106
Tao Bao82490d32019-04-09 00:12:30 -0700107 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700108 def test_SignApexPayload_invalidKey(self):
109 self.assertRaises(
110 apex_utils.ApexSigningError,
111 apex_utils.SignApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700112 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700113 self._GetTestPayload(),
114 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
115 'testkey',
116 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -0700117 self.SALT,
118 no_hashtree=True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700119
Tao Bao82490d32019-04-09 00:12:30 -0700120 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700121 def test_VerifyApexPayload_wrongKey(self):
122 payload_file = self._GetTestPayload()
123 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -0700124 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -0700125 self.SALT, True)
126 apex_utils.VerifyApexPayload(
127 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700128 self.assertRaises(
129 apex_utils.ApexSigningError,
130 apex_utils.VerifyApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700131 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700132 payload_file,
Tao Bao448004a2019-09-19 07:55:02 -0700133 os.path.join(self.testdata_dir, 'testkey_with_passwd.key'),
134 no_hashtree=True)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800135
136 @test_utils.SkipIfExternalToolsUnavailable()
137 def test_ApexApkSigner_noApkPresent(self):
138 apex_path = os.path.join(self.testdata_dir, 'foo.apex')
139 signer = apex_utils.ApexApkSigner(apex_path, None, None)
Tianjiec180a5d2020-03-23 18:14:09 -0700140 processed_apex = signer.ProcessApexFile({}, self.payload_key)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800141 self.assertEqual(apex_path, processed_apex)
142
143 @test_utils.SkipIfExternalToolsUnavailable()
144 def test_ApexApkSigner_apkKeyNotPresent(self):
Tianjiec180a5d2020-03-23 18:14:09 -0700145 apex_path = common.MakeTempFile(suffix='.apex')
146 shutil.copy(self.apex_with_apk, apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800147 signer = apex_utils.ApexApkSigner(apex_path, None, None)
Tianjiec180a5d2020-03-23 18:14:09 -0700148 self.assertRaises(apex_utils.ApexSigningError, signer.ProcessApexFile,
149 {}, self.payload_key)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800150
151 @test_utils.SkipIfExternalToolsUnavailable()
152 def test_ApexApkSigner_signApk(self):
Tianjiec180a5d2020-03-23 18:14:09 -0700153 apex_path = common.MakeTempFile(suffix='.apex')
154 shutil.copy(self.apex_with_apk, apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800155 signer = apex_utils.ApexApkSigner(apex_path, None, None)
156 apk_keys = {'wifi-service-resources.apk': os.path.join(
157 self.testdata_dir, 'testkey')}
158
159 self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
Tianjiec180a5d2020-03-23 18:14:09 -0700160 apex_file = signer.ProcessApexFile(apk_keys, self.payload_key)
161 package_name_extract_cmd = ['aapt', 'dump', 'badging', apex_file]
162 output = common.RunAndCheckOutput(package_name_extract_cmd)
163 for line in output.splitlines():
164 # Sample output from aapt: "package: name='com.google.android.wifi'
165 # versionCode='1' versionName='' platformBuildVersionName='R'
166 # compileSdkVersion='29' compileSdkVersionCodename='R'"
167 match = re.search(r"^package:.* name='([\w|\.]+)'", line, re.IGNORECASE)
168 if match:
169 package_name = match.group(1)
170 self.assertEquals('com.google.android.wifi', package_name)
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800171
172 @test_utils.SkipIfExternalToolsUnavailable()
173 def test_ApexApkSigner_noAssetDir(self):
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800174 no_asset = common.MakeTempFile(suffix='.apex')
175 with zipfile.ZipFile(no_asset, 'w') as output_zip:
Tianjiec180a5d2020-03-23 18:14:09 -0700176 with zipfile.ZipFile(self.apex_with_apk, 'r') as input_zip:
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800177 name_list = input_zip.namelist()
178 for name in name_list:
179 if not name.startswith('assets'):
180 output_zip.writestr(name, input_zip.read(name))
181
182 signer = apex_utils.ApexApkSigner(no_asset, None, None)
183 apk_keys = {'wifi-service-resources.apk': os.path.join(
184 self.testdata_dir, 'testkey')}
185
186 self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
Tianjiec180a5d2020-03-23 18:14:09 -0700187 signer.ProcessApexFile(apk_keys, self.payload_key)