blob: e0a635a90812d74b1ab8a9781ad7b3b428c40f9c [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,
Tao Baobb733882019-07-24 23:31:19 -070026 ReplaceCerts, ReplaceVerityKeyId, 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
Tao Baoe1343992019-03-19 12:24:03 -070037 # pylint: disable=line-too-long
Dan Willemsen0ab1be62019-04-09 21:35:37 -070038 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"
39name="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 -070040"""
41
Tao Baoe838d142017-12-23 23:44:48 -080042 def setUp(self):
Tao Baode1d4792018-02-20 10:05:46 -080043 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoe838d142017-12-23 23:44:48 -080044
Tao Baoa7054ee2017-12-08 14:42:16 -080045 def test_EditTags(self):
46 self.assertEqual(EditTags('dev-keys'), ('release-keys'))
47 self.assertEqual(EditTags('test-keys'), ('release-keys'))
48
49 # Multiple tags.
50 self.assertEqual(EditTags('abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
51
52 # Tags are sorted.
53 self.assertEqual(EditTags('xyz,abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
54
55 def test_RewriteProps(self):
56 props = (
Magnus Strandh234f4b42019-05-01 23:09:30 +020057 ('', ''),
Tao Baoa7054ee2017-12-08 14:42:16 -080058 ('ro.build.fingerprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020059 'ro.build.fingerprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -080060 ('ro.build.thumbprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020061 'ro.build.thumbprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -080062 ('ro.vendor.build.fingerprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020063 'ro.vendor.build.fingerprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -080064 ('ro.vendor.build.thumbprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020065 'ro.vendor.build.thumbprint=foo/bar/release-keys'),
66 ('ro.odm.build.fingerprint=foo/bar/test-keys',
67 'ro.odm.build.fingerprint=foo/bar/release-keys'),
68 ('ro.odm.build.thumbprint=foo/bar/test-keys',
69 'ro.odm.build.thumbprint=foo/bar/release-keys'),
70 ('ro.product.build.fingerprint=foo/bar/dev-keys',
71 'ro.product.build.fingerprint=foo/bar/release-keys'),
72 ('ro.product.build.thumbprint=foo/bar/dev-keys',
73 'ro.product.build.thumbprint=foo/bar/release-keys'),
Justin Yun6151e3f2019-06-25 15:58:13 +090074 ('ro.system_ext.build.fingerprint=foo/bar/test-keys',
75 'ro.system_ext.build.fingerprint=foo/bar/release-keys'),
76 ('ro.system_ext.build.thumbprint=foo/bar/test-keys',
77 'ro.system_ext.build.thumbprint=foo/bar/release-keys'),
Magnus Strandh234f4b42019-05-01 23:09:30 +020078 ('# comment line 1', '# comment line 1'),
Tao Baoa7054ee2017-12-08 14:42:16 -080079 ('ro.bootimage.build.fingerprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020080 'ro.bootimage.build.fingerprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -080081 ('ro.build.description='
82 'sailfish-user 8.0.0 OPR6.170623.012 4283428 dev-keys',
83 'ro.build.description='
Magnus Strandh234f4b42019-05-01 23:09:30 +020084 'sailfish-user 8.0.0 OPR6.170623.012 4283428 release-keys'),
85 ('ro.build.tags=dev-keys', 'ro.build.tags=release-keys'),
86 ('ro.build.tags=test-keys', 'ro.build.tags=release-keys'),
87 ('ro.system.build.tags=dev-keys',
88 'ro.system.build.tags=release-keys'),
89 ('ro.vendor.build.tags=dev-keys',
90 'ro.vendor.build.tags=release-keys'),
91 ('ro.odm.build.tags=dev-keys',
92 'ro.odm.build.tags=release-keys'),
93 ('ro.product.build.tags=dev-keys',
94 'ro.product.build.tags=release-keys'),
Justin Yun6151e3f2019-06-25 15:58:13 +090095 ('ro.system_ext.build.tags=dev-keys',
96 'ro.system_ext.build.tags=release-keys'),
Magnus Strandh234f4b42019-05-01 23:09:30 +020097 ('# comment line 2', '# comment line 2'),
Tao Baoa7054ee2017-12-08 14:42:16 -080098 ('ro.build.display.id=OPR6.170623.012 dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020099 'ro.build.display.id=OPR6.170623.012'),
100 ('# comment line 3', '# comment line 3'),
Tao Baoa7054ee2017-12-08 14:42:16 -0800101 )
102
103 # Assert the case for each individual line.
Magnus Strandh234f4b42019-05-01 23:09:30 +0200104 for prop, expected in props:
105 self.assertEqual(expected + '\n', RewriteProps(prop))
Tao Baoa7054ee2017-12-08 14:42:16 -0800106
107 # Concatenate all the input lines.
Magnus Strandh234f4b42019-05-01 23:09:30 +0200108 self.assertEqual(
109 '\n'.join([prop[1] for prop in props]) + '\n',
110 RewriteProps('\n'.join([prop[0] for prop in props])))
Tao Baoe838d142017-12-23 23:44:48 -0800111
112 def test_ReplaceVerityKeyId(self):
113 BOOT_CMDLINE1 = (
114 "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 "
115 "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 "
116 "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 "
117 "buildvariant=userdebug "
118 "veritykeyid=id:7e4333f9bba00adfe0ede979e28ed1920492b40f\n")
119
120 BOOT_CMDLINE2 = (
121 "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 "
122 "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 "
123 "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 "
124 "buildvariant=userdebug "
Tao Baode1d4792018-02-20 10:05:46 -0800125 "veritykeyid=id:d24f2590e9abab5cff5f59da4c4f0366e3f43e94\n")
Tao Baoe838d142017-12-23 23:44:48 -0800126
Tao Baode1d4792018-02-20 10:05:46 -0800127 input_file = common.MakeTempFile(suffix='.zip')
128 with zipfile.ZipFile(input_file, 'w') as input_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800129 input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE1)
130
131 # Test with the first certificate.
Tao Baode1d4792018-02-20 10:05:46 -0800132 cert_file = os.path.join(self.testdata_dir, 'verity.x509.pem')
Tao Baoe838d142017-12-23 23:44:48 -0800133
Tao Baode1d4792018-02-20 10:05:46 -0800134 output_file = common.MakeTempFile(suffix='.zip')
135 with zipfile.ZipFile(input_file, 'r') as input_zip, \
136 zipfile.ZipFile(output_file, 'w') as output_zip:
137 ReplaceVerityKeyId(input_zip, output_zip, cert_file)
Tao Baoe838d142017-12-23 23:44:48 -0800138
Tao Baode1d4792018-02-20 10:05:46 -0800139 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoa3705452019-06-24 15:33:41 -0700140 self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline').decode())
Tao Baoe838d142017-12-23 23:44:48 -0800141
142 # Test with the second certificate.
Tao Baode1d4792018-02-20 10:05:46 -0800143 cert_file = os.path.join(self.testdata_dir, 'testkey.x509.pem')
Tao Baoe838d142017-12-23 23:44:48 -0800144
Tao Baode1d4792018-02-20 10:05:46 -0800145 with zipfile.ZipFile(input_file, 'r') as input_zip, \
146 zipfile.ZipFile(output_file, 'w') as output_zip:
147 ReplaceVerityKeyId(input_zip, output_zip, cert_file)
Tao Baoe838d142017-12-23 23:44:48 -0800148
Tao Baode1d4792018-02-20 10:05:46 -0800149 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoa3705452019-06-24 15:33:41 -0700150 self.assertEqual(BOOT_CMDLINE2, output_zip.read('BOOT/cmdline').decode())
Tao Baoe838d142017-12-23 23:44:48 -0800151
152 def test_ReplaceVerityKeyId_no_veritykeyid(self):
153 BOOT_CMDLINE = (
154 "console=ttyHSL0,115200,n8 androidboot.hardware=bullhead boot_cpus=0-5 "
155 "lpm_levels.sleep_disabled=1 msm_poweroff.download_mode=0 "
156 "loop.max_part=7\n")
157
Tao Baode1d4792018-02-20 10:05:46 -0800158 input_file = common.MakeTempFile(suffix='.zip')
159 with zipfile.ZipFile(input_file, 'w') as input_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800160 input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE)
161
Tao Baode1d4792018-02-20 10:05:46 -0800162 output_file = common.MakeTempFile(suffix='.zip')
163 with zipfile.ZipFile(input_file, 'r') as input_zip, \
164 zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800165 ReplaceVerityKeyId(input_zip, output_zip, None)
166
Tao Baode1d4792018-02-20 10:05:46 -0800167 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoa3705452019-06-24 15:33:41 -0700168 self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline').decode())
Tao Bao66472632017-12-04 17:16:36 -0800169
170 def test_ReplaceCerts(self):
171 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
172 with open(cert1_path) as cert1_fp:
173 cert1 = cert1_fp.read()
174 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
175 with open(cert2_path) as cert2_fp:
176 cert2 = cert2_fp.read()
177 cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem')
178 with open(cert3_path) as cert3_fp:
179 cert3 = cert3_fp.read()
180
181 # Replace cert1 with cert3.
182 input_xml = self.MAC_PERMISSIONS_XML.format(
183 base64.b16encode(common.ParseCertificate(cert1)).lower(),
184 base64.b16encode(common.ParseCertificate(cert2)).lower())
185
186 output_xml = self.MAC_PERMISSIONS_XML.format(
187 base64.b16encode(common.ParseCertificate(cert3)).lower(),
188 base64.b16encode(common.ParseCertificate(cert2)).lower())
189
190 common.OPTIONS.key_map = {
191 cert1_path[:-9] : cert3_path[:-9],
192 }
193
194 self.assertEqual(output_xml, ReplaceCerts(input_xml))
195
196 def test_ReplaceCerts_duplicateEntries(self):
197 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
198 with open(cert1_path) as cert1_fp:
199 cert1 = cert1_fp.read()
200 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
201 with open(cert2_path) as cert2_fp:
202 cert2 = cert2_fp.read()
203
204 # Replace cert1 with cert2, which leads to duplicate entries.
205 input_xml = self.MAC_PERMISSIONS_XML.format(
206 base64.b16encode(common.ParseCertificate(cert1)).lower(),
207 base64.b16encode(common.ParseCertificate(cert2)).lower())
208
209 common.OPTIONS.key_map = {
210 cert1_path[:-9] : cert2_path[:-9],
211 }
212 self.assertRaises(AssertionError, ReplaceCerts, input_xml)
213
214 def test_ReplaceCerts_skipNonExistentCerts(self):
215 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
216 with open(cert1_path) as cert1_fp:
217 cert1 = cert1_fp.read()
218 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
219 with open(cert2_path) as cert2_fp:
220 cert2 = cert2_fp.read()
221 cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem')
222 with open(cert3_path) as cert3_fp:
223 cert3 = cert3_fp.read()
224
225 input_xml = self.MAC_PERMISSIONS_XML.format(
226 base64.b16encode(common.ParseCertificate(cert1)).lower(),
227 base64.b16encode(common.ParseCertificate(cert2)).lower())
228
229 output_xml = self.MAC_PERMISSIONS_XML.format(
230 base64.b16encode(common.ParseCertificate(cert3)).lower(),
231 base64.b16encode(common.ParseCertificate(cert2)).lower())
232
233 common.OPTIONS.key_map = {
234 cert1_path[:-9] : cert3_path[:-9],
235 'non-existent' : cert3_path[:-9],
236 cert2_path[:-9] : 'non-existent',
237 }
238 self.assertEqual(output_xml, ReplaceCerts(input_xml))
Tao Bao11f955c2018-06-19 12:19:35 -0700239
Tao Baobb733882019-07-24 23:31:19 -0700240 def test_WriteOtacerts(self):
241 certs = [
242 os.path.join(self.testdata_dir, 'platform.x509.pem'),
243 os.path.join(self.testdata_dir, 'media.x509.pem'),
244 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
245 ]
246 entry_name = 'SYSTEM/etc/security/otacerts.zip'
247 output_file = common.MakeTempFile(suffix='.zip')
248 with zipfile.ZipFile(output_file, 'w') as output_zip:
249 WriteOtacerts(output_zip, entry_name, certs)
250 with zipfile.ZipFile(output_file) as input_zip:
251 self.assertIn(entry_name, input_zip.namelist())
252 otacerts_file = io.BytesIO(input_zip.read(entry_name))
253 with zipfile.ZipFile(otacerts_file) as otacerts_zip:
254 self.assertEqual(3, len(otacerts_zip.namelist()))
255
Tao Baoaa7e9932019-03-15 09:37:01 -0700256 def test_CheckApkAndApexKeysAvailable(self):
Tao Bao11f955c2018-06-19 12:19:35 -0700257 input_file = common.MakeTempFile(suffix='.zip')
258 with zipfile.ZipFile(input_file, 'w') as input_zip:
259 input_zip.writestr('SYSTEM/app/App1.apk', "App1-content")
260 input_zip.writestr('SYSTEM/app/App2.apk.gz', "App2-content")
261
262 apk_key_map = {
263 'App1.apk' : 'key1',
264 'App2.apk' : 'key2',
265 'App3.apk' : 'key3',
266 }
267 with zipfile.ZipFile(input_file) as input_zip:
Tao Baoe1343992019-03-19 12:24:03 -0700268 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {})
269 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.gz', {})
Tao Bao11f955c2018-06-19 12:19:35 -0700270
271 # 'App2.apk.gz' won't be considered as an APK.
Tao Baoe1343992019-03-19 12:24:03 -0700272 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {})
273 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.xz', {})
Tao Bao11f955c2018-06-19 12:19:35 -0700274
275 del apk_key_map['App2.apk']
276 self.assertRaises(
Tao Baoaa7e9932019-03-15 09:37:01 -0700277 AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map,
Tao Baoe1343992019-03-19 12:24:03 -0700278 '.gz', {})
279
280 def test_CheckApkAndApexKeysAvailable_invalidApexKeys(self):
281 input_file = common.MakeTempFile(suffix='.zip')
282 with zipfile.ZipFile(input_file, 'w') as input_zip:
283 input_zip.writestr('SYSTEM/apex/Apex1.apex', "Apex1-content")
284 input_zip.writestr('SYSTEM/apex/Apex2.apex', "Apex2-content")
285
286 apk_key_map = {
287 'Apex1.apex' : 'key1',
288 'Apex2.apex' : 'key2',
289 'Apex3.apex' : 'key3',
290 }
291 apex_keys = {
292 'Apex1.apex' : ('payload-key1', 'container-key1'),
293 'Apex2.apex' : ('payload-key2', 'container-key2'),
294 }
295 with zipfile.ZipFile(input_file) as input_zip:
296 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, apex_keys)
297
298 # Fine to have both keys as PRESIGNED.
299 apex_keys['Apex2.apex'] = ('PRESIGNED', 'PRESIGNED')
300 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, apex_keys)
301
302 # Having only one of them as PRESIGNED is not allowed.
303 apex_keys['Apex2.apex'] = ('payload-key2', 'PRESIGNED')
304 self.assertRaises(
305 AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map,
306 None, apex_keys)
307
308 apex_keys['Apex2.apex'] = ('PRESIGNED', 'container-key1')
309 self.assertRaises(
310 AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map,
311 None, apex_keys)
Tao Bao11f955c2018-06-19 12:19:35 -0700312
313 def test_GetApkFileInfo(self):
Tao Bao93c2a012018-06-19 12:19:35 -0700314 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
315 "PRODUCT/apps/Chats.apk", None, [])
Tao Bao11f955c2018-06-19 12:19:35 -0700316 self.assertTrue(is_apk)
317 self.assertFalse(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700318 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700319
Tao Bao93c2a012018-06-19 12:19:35 -0700320 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
321 "PRODUCT/apps/Chats.apk", None, [])
322 self.assertTrue(is_apk)
323 self.assertFalse(is_compressed)
324 self.assertFalse(should_be_skipped)
325
326 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
327 "PRODUCT/apps/Chats.dat", None, [])
Tao Bao11f955c2018-06-19 12:19:35 -0700328 self.assertFalse(is_apk)
329 self.assertFalse(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700330 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700331
332 def test_GetApkFileInfo_withCompressedApks(self):
Tao Bao93c2a012018-06-19 12:19:35 -0700333 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
334 "PRODUCT/apps/Chats.apk.gz", ".gz", [])
Tao Bao11f955c2018-06-19 12:19:35 -0700335 self.assertTrue(is_apk)
336 self.assertTrue(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700337 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700338
Tao Bao93c2a012018-06-19 12:19:35 -0700339 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
340 "PRODUCT/apps/Chats.apk.gz", ".xz", [])
Tao Bao11f955c2018-06-19 12:19:35 -0700341 self.assertFalse(is_apk)
342 self.assertFalse(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700343 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700344
345 self.assertRaises(
Tao Bao93c2a012018-06-19 12:19:35 -0700346 AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "", [])
Tao Bao11f955c2018-06-19 12:19:35 -0700347
348 self.assertRaises(
Tao Bao93c2a012018-06-19 12:19:35 -0700349 AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "apk", [])
350
351 def test_GetApkFileInfo_withSkippedPrefixes(self):
352 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
353 "PRODUCT/preloads/apps/Chats.apk", None, set())
354 self.assertTrue(is_apk)
355 self.assertFalse(is_compressed)
356 self.assertFalse(should_be_skipped)
357
358 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
359 "PRODUCT/preloads/apps/Chats.apk",
360 None,
361 set(["PRODUCT/preloads/"]))
362 self.assertTrue(is_apk)
363 self.assertFalse(is_compressed)
364 self.assertTrue(should_be_skipped)
365
366 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
367 "SYSTEM_OTHER/preloads/apps/Chats.apk",
368 None,
369 set(["SYSTEM/preloads/", "SYSTEM_OTHER/preloads/"]))
370 self.assertTrue(is_apk)
371 self.assertFalse(is_compressed)
372 self.assertTrue(should_be_skipped)
373
374 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
375 "SYSTEM_OTHER/preloads/apps/Chats.apk.gz",
376 ".gz",
377 set(["PRODUCT/prebuilts/", "SYSTEM_OTHER/preloads/"]))
378 self.assertTrue(is_apk)
379 self.assertTrue(is_compressed)
380 self.assertTrue(should_be_skipped)
381
382 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
383 "SYSTEM_OTHER/preloads/apps/Chats.dat",
384 None,
385 set(["SYSTEM_OTHER/preloads/"]))
386 self.assertFalse(is_apk)
387 self.assertFalse(is_compressed)
388 self.assertFalse(should_be_skipped)
389
390 def test_GetApkFileInfo_checkSkippedPrefixesInput(self):
391 # set
392 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
393 "SYSTEM_OTHER/preloads/apps/Chats.apk",
394 None,
395 set(["SYSTEM_OTHER/preloads/"]))
396 self.assertTrue(is_apk)
397 self.assertFalse(is_compressed)
398 self.assertTrue(should_be_skipped)
399
400 # tuple
401 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
402 "SYSTEM_OTHER/preloads/apps/Chats.apk",
403 None,
404 ("SYSTEM_OTHER/preloads/",))
405 self.assertTrue(is_apk)
406 self.assertFalse(is_compressed)
407 self.assertTrue(should_be_skipped)
408
409 # list
410 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
411 "SYSTEM_OTHER/preloads/apps/Chats.apk",
412 None,
413 ["SYSTEM_OTHER/preloads/"])
414 self.assertTrue(is_apk)
415 self.assertFalse(is_compressed)
416 self.assertTrue(should_be_skipped)
417
418 # str is invalid.
419 self.assertRaises(
420 AssertionError, GetApkFileInfo, "SYSTEM_OTHER/preloads/apps/Chats.apk",
421 None, "SYSTEM_OTHER/preloads/")
422
423 # None is invalid.
424 self.assertRaises(
425 AssertionError, GetApkFileInfo, "SYSTEM_OTHER/preloads/apps/Chats.apk",
426 None, None)
Tao Baoaa7e9932019-03-15 09:37:01 -0700427
428 def test_ReadApexKeysInfo(self):
429 target_files = common.MakeTempFile(suffix='.zip')
430 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
431 target_files_zip.writestr('META/apexkeys.txt', self.APEX_KEYS_TXT)
432
433 with zipfile.ZipFile(target_files) as target_files_zip:
434 keys_info = ReadApexKeysInfo(target_files_zip)
435
Tao Baoe1343992019-03-19 12:24:03 -0700436 self.assertEqual({
437 'apex.apexd_test.apex': (
438 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700439 'build/make/target/product/security/testkey'),
Tao Baoe1343992019-03-19 12:24:03 -0700440 'apex.apexd_test_different_app.apex': (
441 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700442 'build/make/target/product/security/testkey'),
Tao Baoe1343992019-03-19 12:24:03 -0700443 }, keys_info)
Tao Baoaa7e9932019-03-15 09:37:01 -0700444
Tao Bao6d9e3da2019-03-26 12:59:25 -0700445 def test_ReadApexKeysInfo_mismatchingContainerKeys(self):
Tao Baoaa7e9932019-03-15 09:37:01 -0700446 # Mismatching payload public / private keys.
447 apex_keys = self.APEX_KEYS_TXT + (
448 'name="apex.apexd_test_different_app2.apex" '
449 'public_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.avbpubkey" '
Tao Bao6d9e3da2019-03-26 12:59:25 -0700450 'private_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem" '
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700451 'container_certificate="build/make/target/product/security/testkey.x509.pem" '
452 'container_private_key="build/make/target/product/security/testkey2.pk8"')
Tao Baoaa7e9932019-03-15 09:37:01 -0700453 target_files = common.MakeTempFile(suffix='.zip')
454 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
455 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
456
457 with zipfile.ZipFile(target_files) as target_files_zip:
458 self.assertRaises(ValueError, ReadApexKeysInfo, target_files_zip)
459
Tao Bao6d9e3da2019-03-26 12:59:25 -0700460 def test_ReadApexKeysInfo_missingPayloadPrivateKey(self):
Tao Baoaa7e9932019-03-15 09:37:01 -0700461 # Invalid lines will be skipped.
462 apex_keys = self.APEX_KEYS_TXT + (
463 'name="apex.apexd_test_different_app2.apex" '
464 'public_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.avbpubkey" '
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700465 'container_certificate="build/make/target/product/security/testkey.x509.pem" '
466 'container_private_key="build/make/target/product/security/testkey.pk8"')
Tao Baoaa7e9932019-03-15 09:37:01 -0700467 target_files = common.MakeTempFile(suffix='.zip')
468 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
469 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
470
471 with zipfile.ZipFile(target_files) as target_files_zip:
472 keys_info = ReadApexKeysInfo(target_files_zip)
473
Tao Baoe1343992019-03-19 12:24:03 -0700474 self.assertEqual({
475 'apex.apexd_test.apex': (
476 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700477 'build/make/target/product/security/testkey'),
Tao Baoe1343992019-03-19 12:24:03 -0700478 'apex.apexd_test_different_app.apex': (
479 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700480 'build/make/target/product/security/testkey'),
Tao Baoe1343992019-03-19 12:24:03 -0700481 }, keys_info)
Tao Bao6d9e3da2019-03-26 12:59:25 -0700482
483 def test_ReadApexKeysInfo_missingPayloadPublicKey(self):
484 # Invalid lines will be skipped.
485 apex_keys = self.APEX_KEYS_TXT + (
486 'name="apex.apexd_test_different_app2.apex" '
487 'private_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem" '
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700488 'container_certificate="build/make/target/product/security/testkey.x509.pem" '
489 'container_private_key="build/make/target/product/security/testkey.pk8"')
Tao Bao6d9e3da2019-03-26 12:59:25 -0700490 target_files = common.MakeTempFile(suffix='.zip')
491 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
492 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
493
494 with zipfile.ZipFile(target_files) as target_files_zip:
495 keys_info = ReadApexKeysInfo(target_files_zip)
496
497 self.assertEqual({
498 'apex.apexd_test.apex': (
499 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700500 'build/make/target/product/security/testkey'),
Tao Bao6d9e3da2019-03-26 12:59:25 -0700501 'apex.apexd_test_different_app.apex': (
502 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700503 'build/make/target/product/security/testkey'),
Tao Bao6d9e3da2019-03-26 12:59:25 -0700504 }, keys_info)
Tao Baof454c3a2019-04-24 23:53:42 -0700505
506 def test_ReadApexKeysInfo_presignedKeys(self):
507 apex_keys = self.APEX_KEYS_TXT + (
508 'name="apex.apexd_test_different_app2.apex" '
509 'private_key="PRESIGNED" '
510 'public_key="PRESIGNED" '
511 'container_certificate="PRESIGNED" '
512 'container_private_key="PRESIGNED"')
513 target_files = common.MakeTempFile(suffix='.zip')
514 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
515 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
516
517 with zipfile.ZipFile(target_files) as target_files_zip:
518 keys_info = ReadApexKeysInfo(target_files_zip)
519
520 self.assertEqual({
521 'apex.apexd_test.apex': (
522 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
523 'build/make/target/product/security/testkey'),
524 'apex.apexd_test_different_app.apex': (
525 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
526 'build/make/target/product/security/testkey'),
527 }, keys_info)
Tao Bao548db7d2019-04-24 23:53:42 -0700528
529 def test_ReadApexKeysInfo_presignedKeys(self):
530 apex_keys = self.APEX_KEYS_TXT + (
531 'name="apex.apexd_test_different_app2.apex" '
532 'private_key="PRESIGNED" '
533 'public_key="PRESIGNED" '
534 'container_certificate="PRESIGNED" '
535 'container_private_key="PRESIGNED"')
536 target_files = common.MakeTempFile(suffix='.zip')
537 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
538 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
539
540 with zipfile.ZipFile(target_files) as target_files_zip:
541 keys_info = ReadApexKeysInfo(target_files_zip)
542
543 self.assertEqual({
544 'apex.apexd_test.apex': (
545 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
546 'build/make/target/product/security/testkey'),
547 'apex.apexd_test_different_app.apex': (
548 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
549 'build/make/target/product/security/testkey'),
550 }, keys_info)