blob: 9cc6df428c4d067c36e330952456c5ffde16cbf6 [file] [log] [blame]
Tao Baoa7054ee2017-12-08 14:42:16 -08001#
2# Copyright (C) 2017 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 Bao66472632017-12-04 17:16:36 -080017import base64
Tao Baobb733882019-07-24 23:31:19 -070018import io
Tao Baode1d4792018-02-20 10:05:46 -080019import os.path
Tao Baoe838d142017-12-23 23:44:48 -080020import zipfile
Tao Baoa7054ee2017-12-08 14:42:16 -080021
Tao Baoe838d142017-12-23 23:44:48 -080022import common
Tao Baode1d4792018-02-20 10:05:46 -080023import test_utils
Tao Bao66472632017-12-04 17:16:36 -080024from sign_target_files_apks import (
Tao Baoaa7e9932019-03-15 09:37:01 -070025 CheckApkAndApexKeysAvailable, EditTags, GetApkFileInfo, ReadApexKeysInfo,
Yi-Yo Chianga4d5f432024-01-24 14:10:17 +080026 ReplaceCerts, RewriteAvbProps, RewriteProps, WriteOtacerts)
Tao Baoa7054ee2017-12-08 14:42:16 -080027
28
Tao Bao65b94e92018-10-11 21:57:26 -070029class SignTargetFilesApksTest(test_utils.ReleaseToolsTestCase):
Tao Baoa7054ee2017-12-08 14:42:16 -080030
Tao Bao66472632017-12-04 17:16:36 -080031 MAC_PERMISSIONS_XML = """<?xml version="1.0" encoding="iso-8859-1"?>
32<policy>
33 <signer signature="{}"><seinfo value="platform"/></signer>
34 <signer signature="{}"><seinfo value="media"/></signer>
35</policy>"""
36
Bill Peckham5c7b0342020-04-03 15:36:23 -070037 # Note that we test one apex with the partition tag, and another without to
38 # make sure that new OTA tools can process an older target files package that
39 # does not include the partition tag.
40
Tao Baoe1343992019-03-19 12:24:03 -070041 # pylint: disable=line-too-long
Bill Peckham19c3feb2020-03-20 18:31:43 -070042 APEX_KEYS_TXT = """name="apex.apexd_test.apex" public_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package.avbpubkey" private_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem" container_certificate="build/make/target/product/security/testkey.x509.pem" container_private_key="build/make/target/product/security/testkey.pk8" partition="system"
Bill Peckham5c7b0342020-04-03 15:36:23 -070043name="apex.apexd_test_different_app.apex" public_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.avbpubkey" private_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem" container_certificate="build/make/target/product/security/testkey.x509.pem" container_private_key="build/make/target/product/security/testkey.pk8"
Tao Baoaa7e9932019-03-15 09:37:01 -070044"""
45
Tao Baoe838d142017-12-23 23:44:48 -080046 def setUp(self):
Tao Baode1d4792018-02-20 10:05:46 -080047 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoe838d142017-12-23 23:44:48 -080048
Tao Baoa7054ee2017-12-08 14:42:16 -080049 def test_EditTags(self):
50 self.assertEqual(EditTags('dev-keys'), ('release-keys'))
51 self.assertEqual(EditTags('test-keys'), ('release-keys'))
52
53 # Multiple tags.
54 self.assertEqual(EditTags('abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
55
56 # Tags are sorted.
57 self.assertEqual(EditTags('xyz,abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
58
Tao Bao19b02fe2019-10-09 00:04:28 -070059 def test_RewriteAvbProps(self):
60 misc_info = {
61 'avb_boot_add_hash_footer_args':
62 ('--prop com.android.build.boot.os_version:R '
63 '--prop com.android.build.boot.security_patch:2019-09-05'),
Devin Mooreafdd7c72021-12-13 22:04:08 +000064 'avb_init_boot_add_hash_footer_args':
65 ('--prop com.android.build.boot.os_version:R '
66 '--prop com.android.build.boot.security_patch:2019-09-05'),
Tao Bao19b02fe2019-10-09 00:04:28 -070067 'avb_system_add_hashtree_footer_args':
68 ('--prop com.android.build.system.os_version:R '
69 '--prop com.android.build.system.security_patch:2019-09-05 '
70 '--prop com.android.build.system.fingerprint:'
71 'Android/aosp_taimen/taimen:R/QT/foo:userdebug/test-keys'),
72 'avb_vendor_add_hashtree_footer_args':
73 ('--prop com.android.build.vendor.os_version:R '
74 '--prop com.android.build.vendor.security_patch:2019-09-05 '
75 '--prop com.android.build.vendor.fingerprint:'
76 'Android/aosp_taimen/taimen:R/QT/foo:userdebug/dev-keys'),
77 }
78 expected_dict = {
79 'avb_boot_add_hash_footer_args':
80 ('--prop com.android.build.boot.os_version:R '
81 '--prop com.android.build.boot.security_patch:2019-09-05'),
Devin Mooreafdd7c72021-12-13 22:04:08 +000082 'avb_init_boot_add_hash_footer_args':
83 ('--prop com.android.build.boot.os_version:R '
84 '--prop com.android.build.boot.security_patch:2019-09-05'),
Tao Bao19b02fe2019-10-09 00:04:28 -070085 'avb_system_add_hashtree_footer_args':
86 ('--prop com.android.build.system.os_version:R '
87 '--prop com.android.build.system.security_patch:2019-09-05 '
88 '--prop com.android.build.system.fingerprint:'
89 'Android/aosp_taimen/taimen:R/QT/foo:userdebug/release-keys'),
90 'avb_vendor_add_hashtree_footer_args':
91 ('--prop com.android.build.vendor.os_version:R '
92 '--prop com.android.build.vendor.security_patch:2019-09-05 '
93 '--prop com.android.build.vendor.fingerprint:'
94 'Android/aosp_taimen/taimen:R/QT/foo:userdebug/release-keys'),
95 }
96 RewriteAvbProps(misc_info)
97 self.assertDictEqual(expected_dict, misc_info)
98
Tao Baoa7054ee2017-12-08 14:42:16 -080099 def test_RewriteProps(self):
100 props = (
Magnus Strandh234f4b42019-05-01 23:09:30 +0200101 ('', ''),
Tao Baoa7054ee2017-12-08 14:42:16 -0800102 ('ro.build.fingerprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +0200103 'ro.build.fingerprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -0800104 ('ro.build.thumbprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +0200105 'ro.build.thumbprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -0800106 ('ro.vendor.build.fingerprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +0200107 'ro.vendor.build.fingerprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -0800108 ('ro.vendor.build.thumbprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +0200109 'ro.vendor.build.thumbprint=foo/bar/release-keys'),
110 ('ro.odm.build.fingerprint=foo/bar/test-keys',
111 'ro.odm.build.fingerprint=foo/bar/release-keys'),
112 ('ro.odm.build.thumbprint=foo/bar/test-keys',
113 'ro.odm.build.thumbprint=foo/bar/release-keys'),
114 ('ro.product.build.fingerprint=foo/bar/dev-keys',
115 'ro.product.build.fingerprint=foo/bar/release-keys'),
116 ('ro.product.build.thumbprint=foo/bar/dev-keys',
117 'ro.product.build.thumbprint=foo/bar/release-keys'),
Justin Yun6151e3f2019-06-25 15:58:13 +0900118 ('ro.system_ext.build.fingerprint=foo/bar/test-keys',
119 'ro.system_ext.build.fingerprint=foo/bar/release-keys'),
120 ('ro.system_ext.build.thumbprint=foo/bar/test-keys',
121 'ro.system_ext.build.thumbprint=foo/bar/release-keys'),
Magnus Strandh234f4b42019-05-01 23:09:30 +0200122 ('# comment line 1', '# comment line 1'),
Tao Baoa7054ee2017-12-08 14:42:16 -0800123 ('ro.bootimage.build.fingerprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +0200124 'ro.bootimage.build.fingerprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -0800125 ('ro.build.description='
126 'sailfish-user 8.0.0 OPR6.170623.012 4283428 dev-keys',
127 'ro.build.description='
Magnus Strandh234f4b42019-05-01 23:09:30 +0200128 'sailfish-user 8.0.0 OPR6.170623.012 4283428 release-keys'),
129 ('ro.build.tags=dev-keys', 'ro.build.tags=release-keys'),
130 ('ro.build.tags=test-keys', 'ro.build.tags=release-keys'),
131 ('ro.system.build.tags=dev-keys',
132 'ro.system.build.tags=release-keys'),
133 ('ro.vendor.build.tags=dev-keys',
134 'ro.vendor.build.tags=release-keys'),
135 ('ro.odm.build.tags=dev-keys',
136 'ro.odm.build.tags=release-keys'),
137 ('ro.product.build.tags=dev-keys',
138 'ro.product.build.tags=release-keys'),
Justin Yun6151e3f2019-06-25 15:58:13 +0900139 ('ro.system_ext.build.tags=dev-keys',
140 'ro.system_ext.build.tags=release-keys'),
Magnus Strandh234f4b42019-05-01 23:09:30 +0200141 ('# comment line 2', '# comment line 2'),
Tao Baoa7054ee2017-12-08 14:42:16 -0800142 ('ro.build.display.id=OPR6.170623.012 dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +0200143 'ro.build.display.id=OPR6.170623.012'),
144 ('# comment line 3', '# comment line 3'),
Tao Baoa7054ee2017-12-08 14:42:16 -0800145 )
146
147 # Assert the case for each individual line.
Magnus Strandh234f4b42019-05-01 23:09:30 +0200148 for prop, expected in props:
149 self.assertEqual(expected + '\n', RewriteProps(prop))
Tao Baoa7054ee2017-12-08 14:42:16 -0800150
151 # Concatenate all the input lines.
Magnus Strandh234f4b42019-05-01 23:09:30 +0200152 self.assertEqual(
153 '\n'.join([prop[1] for prop in props]) + '\n',
154 RewriteProps('\n'.join([prop[0] for prop in props])))
Tao Baoe838d142017-12-23 23:44:48 -0800155
Tao Bao66472632017-12-04 17:16:36 -0800156 def test_ReplaceCerts(self):
157 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
158 with open(cert1_path) as cert1_fp:
159 cert1 = cert1_fp.read()
160 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
161 with open(cert2_path) as cert2_fp:
162 cert2 = cert2_fp.read()
163 cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem')
164 with open(cert3_path) as cert3_fp:
165 cert3 = cert3_fp.read()
166
167 # Replace cert1 with cert3.
168 input_xml = self.MAC_PERMISSIONS_XML.format(
169 base64.b16encode(common.ParseCertificate(cert1)).lower(),
170 base64.b16encode(common.ParseCertificate(cert2)).lower())
171
172 output_xml = self.MAC_PERMISSIONS_XML.format(
173 base64.b16encode(common.ParseCertificate(cert3)).lower(),
174 base64.b16encode(common.ParseCertificate(cert2)).lower())
175
176 common.OPTIONS.key_map = {
177 cert1_path[:-9] : cert3_path[:-9],
178 }
179
180 self.assertEqual(output_xml, ReplaceCerts(input_xml))
181
182 def test_ReplaceCerts_duplicateEntries(self):
183 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
184 with open(cert1_path) as cert1_fp:
185 cert1 = cert1_fp.read()
186 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
187 with open(cert2_path) as cert2_fp:
188 cert2 = cert2_fp.read()
189
190 # Replace cert1 with cert2, which leads to duplicate entries.
191 input_xml = self.MAC_PERMISSIONS_XML.format(
192 base64.b16encode(common.ParseCertificate(cert1)).lower(),
193 base64.b16encode(common.ParseCertificate(cert2)).lower())
194
195 common.OPTIONS.key_map = {
196 cert1_path[:-9] : cert2_path[:-9],
197 }
198 self.assertRaises(AssertionError, ReplaceCerts, input_xml)
199
200 def test_ReplaceCerts_skipNonExistentCerts(self):
201 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
202 with open(cert1_path) as cert1_fp:
203 cert1 = cert1_fp.read()
204 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
205 with open(cert2_path) as cert2_fp:
206 cert2 = cert2_fp.read()
207 cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem')
208 with open(cert3_path) as cert3_fp:
209 cert3 = cert3_fp.read()
210
211 input_xml = self.MAC_PERMISSIONS_XML.format(
212 base64.b16encode(common.ParseCertificate(cert1)).lower(),
213 base64.b16encode(common.ParseCertificate(cert2)).lower())
214
215 output_xml = self.MAC_PERMISSIONS_XML.format(
216 base64.b16encode(common.ParseCertificate(cert3)).lower(),
217 base64.b16encode(common.ParseCertificate(cert2)).lower())
218
219 common.OPTIONS.key_map = {
220 cert1_path[:-9] : cert3_path[:-9],
221 'non-existent' : cert3_path[:-9],
222 cert2_path[:-9] : 'non-existent',
223 }
224 self.assertEqual(output_xml, ReplaceCerts(input_xml))
Tao Bao11f955c2018-06-19 12:19:35 -0700225
Tao Baobb733882019-07-24 23:31:19 -0700226 def test_WriteOtacerts(self):
227 certs = [
228 os.path.join(self.testdata_dir, 'platform.x509.pem'),
229 os.path.join(self.testdata_dir, 'media.x509.pem'),
230 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
231 ]
232 entry_name = 'SYSTEM/etc/security/otacerts.zip'
233 output_file = common.MakeTempFile(suffix='.zip')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400234 with zipfile.ZipFile(output_file, 'w', allowZip64=True) as output_zip:
Tao Baobb733882019-07-24 23:31:19 -0700235 WriteOtacerts(output_zip, entry_name, certs)
236 with zipfile.ZipFile(output_file) as input_zip:
237 self.assertIn(entry_name, input_zip.namelist())
238 otacerts_file = io.BytesIO(input_zip.read(entry_name))
239 with zipfile.ZipFile(otacerts_file) as otacerts_zip:
240 self.assertEqual(3, len(otacerts_zip.namelist()))
241
Tao Baoaa7e9932019-03-15 09:37:01 -0700242 def test_CheckApkAndApexKeysAvailable(self):
Tao Bao11f955c2018-06-19 12:19:35 -0700243 input_file = common.MakeTempFile(suffix='.zip')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400244 with zipfile.ZipFile(input_file, 'w', allowZip64=True) as input_zip:
Tao Bao11f955c2018-06-19 12:19:35 -0700245 input_zip.writestr('SYSTEM/app/App1.apk', "App1-content")
246 input_zip.writestr('SYSTEM/app/App2.apk.gz', "App2-content")
247
248 apk_key_map = {
249 'App1.apk' : 'key1',
250 'App2.apk' : 'key2',
251 'App3.apk' : 'key3',
252 }
253 with zipfile.ZipFile(input_file) as input_zip:
Tao Baoe1343992019-03-19 12:24:03 -0700254 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {})
255 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.gz', {})
Tao Bao11f955c2018-06-19 12:19:35 -0700256
257 # 'App2.apk.gz' won't be considered as an APK.
Tao Baoe1343992019-03-19 12:24:03 -0700258 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {})
259 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.xz', {})
Tao Bao11f955c2018-06-19 12:19:35 -0700260
261 del apk_key_map['App2.apk']
262 self.assertRaises(
Tao Baoaa7e9932019-03-15 09:37:01 -0700263 AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map,
Tao Baoe1343992019-03-19 12:24:03 -0700264 '.gz', {})
265
266 def test_CheckApkAndApexKeysAvailable_invalidApexKeys(self):
267 input_file = common.MakeTempFile(suffix='.zip')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400268 with zipfile.ZipFile(input_file, 'w', allowZip64=True) as input_zip:
Tao Baoe1343992019-03-19 12:24:03 -0700269 input_zip.writestr('SYSTEM/apex/Apex1.apex', "Apex1-content")
270 input_zip.writestr('SYSTEM/apex/Apex2.apex', "Apex2-content")
271
272 apk_key_map = {
273 'Apex1.apex' : 'key1',
274 'Apex2.apex' : 'key2',
275 'Apex3.apex' : 'key3',
276 }
277 apex_keys = {
Jooyung Han8caba5e2021-10-27 03:58:09 +0900278 'Apex1.apex' : ('payload-key1', 'container-key1', None),
279 'Apex2.apex' : ('payload-key2', 'container-key2', None),
Tao Baoe1343992019-03-19 12:24:03 -0700280 }
281 with zipfile.ZipFile(input_file) as input_zip:
282 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, apex_keys)
283
284 # Fine to have both keys as PRESIGNED.
Jooyung Han8caba5e2021-10-27 03:58:09 +0900285 apex_keys['Apex2.apex'] = ('PRESIGNED', 'PRESIGNED', None)
Tao Baoe1343992019-03-19 12:24:03 -0700286 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, apex_keys)
287
288 # Having only one of them as PRESIGNED is not allowed.
Jooyung Han8caba5e2021-10-27 03:58:09 +0900289 apex_keys['Apex2.apex'] = ('payload-key2', 'PRESIGNED', None)
Tao Baoe1343992019-03-19 12:24:03 -0700290 self.assertRaises(
291 AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map,
292 None, apex_keys)
293
Jooyung Han8caba5e2021-10-27 03:58:09 +0900294 apex_keys['Apex2.apex'] = ('PRESIGNED', 'container-key1', None)
Tao Baoe1343992019-03-19 12:24:03 -0700295 self.assertRaises(
296 AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map,
297 None, apex_keys)
Tao Bao11f955c2018-06-19 12:19:35 -0700298
299 def test_GetApkFileInfo(self):
Tao Bao93c2a012018-06-19 12:19:35 -0700300 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
301 "PRODUCT/apps/Chats.apk", None, [])
Tao Bao11f955c2018-06-19 12:19:35 -0700302 self.assertTrue(is_apk)
303 self.assertFalse(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700304 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700305
Tao Bao93c2a012018-06-19 12:19:35 -0700306 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
307 "PRODUCT/apps/Chats.apk", None, [])
308 self.assertTrue(is_apk)
309 self.assertFalse(is_compressed)
310 self.assertFalse(should_be_skipped)
311
312 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
313 "PRODUCT/apps/Chats.dat", None, [])
Tao Bao11f955c2018-06-19 12:19:35 -0700314 self.assertFalse(is_apk)
315 self.assertFalse(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700316 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700317
318 def test_GetApkFileInfo_withCompressedApks(self):
Tao Bao93c2a012018-06-19 12:19:35 -0700319 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
320 "PRODUCT/apps/Chats.apk.gz", ".gz", [])
Tao Bao11f955c2018-06-19 12:19:35 -0700321 self.assertTrue(is_apk)
322 self.assertTrue(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700323 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700324
Tao Bao93c2a012018-06-19 12:19:35 -0700325 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
326 "PRODUCT/apps/Chats.apk.gz", ".xz", [])
Tao Bao11f955c2018-06-19 12:19:35 -0700327 self.assertFalse(is_apk)
328 self.assertFalse(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700329 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700330
331 self.assertRaises(
Tao Bao93c2a012018-06-19 12:19:35 -0700332 AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "", [])
Tao Bao11f955c2018-06-19 12:19:35 -0700333
334 self.assertRaises(
Tao Bao93c2a012018-06-19 12:19:35 -0700335 AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "apk", [])
336
337 def test_GetApkFileInfo_withSkippedPrefixes(self):
338 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
339 "PRODUCT/preloads/apps/Chats.apk", None, set())
340 self.assertTrue(is_apk)
341 self.assertFalse(is_compressed)
342 self.assertFalse(should_be_skipped)
343
344 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
345 "PRODUCT/preloads/apps/Chats.apk",
346 None,
347 set(["PRODUCT/preloads/"]))
348 self.assertTrue(is_apk)
349 self.assertFalse(is_compressed)
350 self.assertTrue(should_be_skipped)
351
352 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
353 "SYSTEM_OTHER/preloads/apps/Chats.apk",
354 None,
355 set(["SYSTEM/preloads/", "SYSTEM_OTHER/preloads/"]))
356 self.assertTrue(is_apk)
357 self.assertFalse(is_compressed)
358 self.assertTrue(should_be_skipped)
359
360 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
361 "SYSTEM_OTHER/preloads/apps/Chats.apk.gz",
362 ".gz",
363 set(["PRODUCT/prebuilts/", "SYSTEM_OTHER/preloads/"]))
364 self.assertTrue(is_apk)
365 self.assertTrue(is_compressed)
366 self.assertTrue(should_be_skipped)
367
368 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
369 "SYSTEM_OTHER/preloads/apps/Chats.dat",
370 None,
371 set(["SYSTEM_OTHER/preloads/"]))
372 self.assertFalse(is_apk)
373 self.assertFalse(is_compressed)
374 self.assertFalse(should_be_skipped)
375
376 def test_GetApkFileInfo_checkSkippedPrefixesInput(self):
377 # set
378 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
379 "SYSTEM_OTHER/preloads/apps/Chats.apk",
380 None,
381 set(["SYSTEM_OTHER/preloads/"]))
382 self.assertTrue(is_apk)
383 self.assertFalse(is_compressed)
384 self.assertTrue(should_be_skipped)
385
386 # tuple
387 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
388 "SYSTEM_OTHER/preloads/apps/Chats.apk",
389 None,
390 ("SYSTEM_OTHER/preloads/",))
391 self.assertTrue(is_apk)
392 self.assertFalse(is_compressed)
393 self.assertTrue(should_be_skipped)
394
395 # list
396 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
397 "SYSTEM_OTHER/preloads/apps/Chats.apk",
398 None,
399 ["SYSTEM_OTHER/preloads/"])
400 self.assertTrue(is_apk)
401 self.assertFalse(is_compressed)
402 self.assertTrue(should_be_skipped)
403
404 # str is invalid.
405 self.assertRaises(
406 AssertionError, GetApkFileInfo, "SYSTEM_OTHER/preloads/apps/Chats.apk",
407 None, "SYSTEM_OTHER/preloads/")
408
409 # None is invalid.
410 self.assertRaises(
411 AssertionError, GetApkFileInfo, "SYSTEM_OTHER/preloads/apps/Chats.apk",
412 None, None)
Tao Baoaa7e9932019-03-15 09:37:01 -0700413
414 def test_ReadApexKeysInfo(self):
415 target_files = common.MakeTempFile(suffix='.zip')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400416 with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip:
Tao Baoaa7e9932019-03-15 09:37:01 -0700417 target_files_zip.writestr('META/apexkeys.txt', self.APEX_KEYS_TXT)
418
Kelvin Zhang928c2342020-09-22 16:15:57 -0400419 with zipfile.ZipFile(target_files, allowZip64=True) as target_files_zip:
Melisa Carranza Zúñigada308bf2022-04-12 23:22:11 +0000420 keys_info = ReadApexKeysInfo(target_files_zip)
Tao Baoaa7e9932019-03-15 09:37:01 -0700421
Tao Baoe1343992019-03-19 12:24:03 -0700422 self.assertEqual({
423 'apex.apexd_test.apex': (
424 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900425 'build/make/target/product/security/testkey', None),
Tao Baoe1343992019-03-19 12:24:03 -0700426 'apex.apexd_test_different_app.apex': (
427 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900428 'build/make/target/product/security/testkey', None),
Tao Baoe1343992019-03-19 12:24:03 -0700429 }, keys_info)
Tao Baoaa7e9932019-03-15 09:37:01 -0700430
Tao Bao6d9e3da2019-03-26 12:59:25 -0700431 def test_ReadApexKeysInfo_mismatchingContainerKeys(self):
Tao Baoaa7e9932019-03-15 09:37:01 -0700432 # Mismatching payload public / private keys.
433 apex_keys = self.APEX_KEYS_TXT + (
434 'name="apex.apexd_test_different_app2.apex" '
435 'public_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.avbpubkey" '
Tao Bao6d9e3da2019-03-26 12:59:25 -0700436 'private_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem" '
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700437 'container_certificate="build/make/target/product/security/testkey.x509.pem" '
Bill Peckham19c3feb2020-03-20 18:31:43 -0700438 'container_private_key="build/make/target/product/security/testkey2.pk8" '
439 'partition="system"')
Tao Baoaa7e9932019-03-15 09:37:01 -0700440 target_files = common.MakeTempFile(suffix='.zip')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400441 with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip:
Tao Baoaa7e9932019-03-15 09:37:01 -0700442 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
443
Kelvin Zhang928c2342020-09-22 16:15:57 -0400444 with zipfile.ZipFile(target_files, allowZip64=True) as target_files_zip:
Tao Baoaa7e9932019-03-15 09:37:01 -0700445 self.assertRaises(ValueError, ReadApexKeysInfo, target_files_zip)
446
Tao Bao6d9e3da2019-03-26 12:59:25 -0700447 def test_ReadApexKeysInfo_missingPayloadPrivateKey(self):
Tao Baoaa7e9932019-03-15 09:37:01 -0700448 # Invalid lines will be skipped.
449 apex_keys = self.APEX_KEYS_TXT + (
450 'name="apex.apexd_test_different_app2.apex" '
451 'public_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.avbpubkey" '
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700452 'container_certificate="build/make/target/product/security/testkey.x509.pem" '
453 'container_private_key="build/make/target/product/security/testkey.pk8"')
Tao Baoaa7e9932019-03-15 09:37:01 -0700454 target_files = common.MakeTempFile(suffix='.zip')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400455 with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip:
Tao Baoaa7e9932019-03-15 09:37:01 -0700456 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
457
Kelvin Zhang928c2342020-09-22 16:15:57 -0400458 with zipfile.ZipFile(target_files, allowZip64=True) as target_files_zip:
Melisa Carranza Zúñigada308bf2022-04-12 23:22:11 +0000459 keys_info = ReadApexKeysInfo(target_files_zip)
Tao Baoaa7e9932019-03-15 09:37:01 -0700460
Tao Baoe1343992019-03-19 12:24:03 -0700461 self.assertEqual({
462 'apex.apexd_test.apex': (
463 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900464 'build/make/target/product/security/testkey', None),
Tao Baoe1343992019-03-19 12:24:03 -0700465 'apex.apexd_test_different_app.apex': (
466 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900467 'build/make/target/product/security/testkey', None),
Tao Baoe1343992019-03-19 12:24:03 -0700468 }, keys_info)
Tao Bao6d9e3da2019-03-26 12:59:25 -0700469
470 def test_ReadApexKeysInfo_missingPayloadPublicKey(self):
471 # Invalid lines will be skipped.
472 apex_keys = self.APEX_KEYS_TXT + (
473 'name="apex.apexd_test_different_app2.apex" '
474 'private_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem" '
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700475 'container_certificate="build/make/target/product/security/testkey.x509.pem" '
476 'container_private_key="build/make/target/product/security/testkey.pk8"')
Tao Bao6d9e3da2019-03-26 12:59:25 -0700477 target_files = common.MakeTempFile(suffix='.zip')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400478 with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip:
Tao Bao6d9e3da2019-03-26 12:59:25 -0700479 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
480
Kelvin Zhang928c2342020-09-22 16:15:57 -0400481 with zipfile.ZipFile(target_files, allowZip64=True) as target_files_zip:
Melisa Carranza Zúñigada308bf2022-04-12 23:22:11 +0000482 keys_info = ReadApexKeysInfo(target_files_zip)
Tao Bao6d9e3da2019-03-26 12:59:25 -0700483
484 self.assertEqual({
485 'apex.apexd_test.apex': (
486 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900487 'build/make/target/product/security/testkey', None),
Tao Bao6d9e3da2019-03-26 12:59:25 -0700488 'apex.apexd_test_different_app.apex': (
489 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900490 'build/make/target/product/security/testkey', None),
Tao Bao6d9e3da2019-03-26 12:59:25 -0700491 }, keys_info)
Tao Baof454c3a2019-04-24 23:53:42 -0700492
493 def test_ReadApexKeysInfo_presignedKeys(self):
494 apex_keys = self.APEX_KEYS_TXT + (
495 'name="apex.apexd_test_different_app2.apex" '
496 'private_key="PRESIGNED" '
497 'public_key="PRESIGNED" '
498 'container_certificate="PRESIGNED" '
499 'container_private_key="PRESIGNED"')
500 target_files = common.MakeTempFile(suffix='.zip')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400501 with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip:
Tao Baof454c3a2019-04-24 23:53:42 -0700502 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
503
Kelvin Zhang928c2342020-09-22 16:15:57 -0400504 with zipfile.ZipFile(target_files, allowZip64=True) as target_files_zip:
Melisa Carranza Zúñigada308bf2022-04-12 23:22:11 +0000505 keys_info = ReadApexKeysInfo(target_files_zip)
Tao Baof454c3a2019-04-24 23:53:42 -0700506
507 self.assertEqual({
508 'apex.apexd_test.apex': (
509 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900510 'build/make/target/product/security/testkey', None),
Tao Baof454c3a2019-04-24 23:53:42 -0700511 'apex.apexd_test_different_app.apex': (
512 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900513 'build/make/target/product/security/testkey', None),
Tao Baof454c3a2019-04-24 23:53:42 -0700514 }, keys_info)
Tao Bao548db7d2019-04-24 23:53:42 -0700515
516 def test_ReadApexKeysInfo_presignedKeys(self):
517 apex_keys = self.APEX_KEYS_TXT + (
518 'name="apex.apexd_test_different_app2.apex" '
519 'private_key="PRESIGNED" '
520 'public_key="PRESIGNED" '
521 'container_certificate="PRESIGNED" '
522 'container_private_key="PRESIGNED"')
523 target_files = common.MakeTempFile(suffix='.zip')
Kelvin Zhang928c2342020-09-22 16:15:57 -0400524 with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip:
Tao Bao548db7d2019-04-24 23:53:42 -0700525 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
526
Kelvin Zhang928c2342020-09-22 16:15:57 -0400527 with zipfile.ZipFile(target_files, allowZip64=True) as target_files_zip:
Melisa Carranza Zúñigada308bf2022-04-12 23:22:11 +0000528 keys_info = ReadApexKeysInfo(target_files_zip)
Tao Bao548db7d2019-04-24 23:53:42 -0700529
530 self.assertEqual({
531 'apex.apexd_test.apex': (
532 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900533 'build/make/target/product/security/testkey', None),
Tao Bao548db7d2019-04-24 23:53:42 -0700534 'apex.apexd_test_different_app.apex': (
535 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Jooyung Han8caba5e2021-10-27 03:58:09 +0900536 'build/make/target/product/security/testkey', None),
Tao Bao548db7d2019-04-24 23:53:42 -0700537 }, keys_info)