blob: 01007290ae8aaffb07ad43a798c084b12ea81b7d [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 Baode1d4792018-02-20 10:05:46 -080018import os.path
Tao Baoe838d142017-12-23 23:44:48 -080019import zipfile
Tao Baoa7054ee2017-12-08 14:42:16 -080020
Tao Baoe838d142017-12-23 23:44:48 -080021import common
Tao Baode1d4792018-02-20 10:05:46 -080022import test_utils
Tao Bao66472632017-12-04 17:16:36 -080023from sign_target_files_apks import (
Tao Baoaa7e9932019-03-15 09:37:01 -070024 CheckApkAndApexKeysAvailable, EditTags, GetApkFileInfo, ReadApexKeysInfo,
25 ReplaceCerts, ReplaceVerityKeyId, RewriteProps)
Tao Baoa7054ee2017-12-08 14:42:16 -080026
27
Tao Bao65b94e92018-10-11 21:57:26 -070028class SignTargetFilesApksTest(test_utils.ReleaseToolsTestCase):
Tao Baoa7054ee2017-12-08 14:42:16 -080029
Tao Bao66472632017-12-04 17:16:36 -080030 MAC_PERMISSIONS_XML = """<?xml version="1.0" encoding="iso-8859-1"?>
31<policy>
32 <signer signature="{}"><seinfo value="platform"/></signer>
33 <signer signature="{}"><seinfo value="media"/></signer>
34</policy>"""
35
Tao Baoe1343992019-03-19 12:24:03 -070036 # pylint: disable=line-too-long
Dan Willemsen0ab1be62019-04-09 21:35:37 -070037 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"
38name="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 -070039"""
40
Tao Baoe838d142017-12-23 23:44:48 -080041 def setUp(self):
Tao Baode1d4792018-02-20 10:05:46 -080042 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoe838d142017-12-23 23:44:48 -080043
Tao Baoa7054ee2017-12-08 14:42:16 -080044 def test_EditTags(self):
45 self.assertEqual(EditTags('dev-keys'), ('release-keys'))
46 self.assertEqual(EditTags('test-keys'), ('release-keys'))
47
48 # Multiple tags.
49 self.assertEqual(EditTags('abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
50
51 # Tags are sorted.
52 self.assertEqual(EditTags('xyz,abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
53
54 def test_RewriteProps(self):
55 props = (
Magnus Strandh234f4b42019-05-01 23:09:30 +020056 ('', ''),
Tao Baoa7054ee2017-12-08 14:42:16 -080057 ('ro.build.fingerprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020058 'ro.build.fingerprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -080059 ('ro.build.thumbprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020060 'ro.build.thumbprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -080061 ('ro.vendor.build.fingerprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020062 'ro.vendor.build.fingerprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -080063 ('ro.vendor.build.thumbprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020064 'ro.vendor.build.thumbprint=foo/bar/release-keys'),
65 ('ro.odm.build.fingerprint=foo/bar/test-keys',
66 'ro.odm.build.fingerprint=foo/bar/release-keys'),
67 ('ro.odm.build.thumbprint=foo/bar/test-keys',
68 'ro.odm.build.thumbprint=foo/bar/release-keys'),
69 ('ro.product.build.fingerprint=foo/bar/dev-keys',
70 'ro.product.build.fingerprint=foo/bar/release-keys'),
71 ('ro.product.build.thumbprint=foo/bar/dev-keys',
72 'ro.product.build.thumbprint=foo/bar/release-keys'),
Justin Yun6151e3f2019-06-25 15:58:13 +090073 ('ro.system_ext.build.fingerprint=foo/bar/test-keys',
74 'ro.system_ext.build.fingerprint=foo/bar/release-keys'),
75 ('ro.system_ext.build.thumbprint=foo/bar/test-keys',
76 'ro.system_ext.build.thumbprint=foo/bar/release-keys'),
Magnus Strandh234f4b42019-05-01 23:09:30 +020077 ('# comment line 1', '# comment line 1'),
Tao Baoa7054ee2017-12-08 14:42:16 -080078 ('ro.bootimage.build.fingerprint=foo/bar/dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020079 'ro.bootimage.build.fingerprint=foo/bar/release-keys'),
Tao Baoa7054ee2017-12-08 14:42:16 -080080 ('ro.build.description='
81 'sailfish-user 8.0.0 OPR6.170623.012 4283428 dev-keys',
82 'ro.build.description='
Magnus Strandh234f4b42019-05-01 23:09:30 +020083 'sailfish-user 8.0.0 OPR6.170623.012 4283428 release-keys'),
84 ('ro.build.tags=dev-keys', 'ro.build.tags=release-keys'),
85 ('ro.build.tags=test-keys', 'ro.build.tags=release-keys'),
86 ('ro.system.build.tags=dev-keys',
87 'ro.system.build.tags=release-keys'),
88 ('ro.vendor.build.tags=dev-keys',
89 'ro.vendor.build.tags=release-keys'),
90 ('ro.odm.build.tags=dev-keys',
91 'ro.odm.build.tags=release-keys'),
92 ('ro.product.build.tags=dev-keys',
93 'ro.product.build.tags=release-keys'),
Justin Yun6151e3f2019-06-25 15:58:13 +090094 ('ro.system_ext.build.tags=dev-keys',
95 'ro.system_ext.build.tags=release-keys'),
Magnus Strandh234f4b42019-05-01 23:09:30 +020096 ('# comment line 2', '# comment line 2'),
Tao Baoa7054ee2017-12-08 14:42:16 -080097 ('ro.build.display.id=OPR6.170623.012 dev-keys',
Magnus Strandh234f4b42019-05-01 23:09:30 +020098 'ro.build.display.id=OPR6.170623.012'),
99 ('# comment line 3', '# comment line 3'),
Tao Baoa7054ee2017-12-08 14:42:16 -0800100 )
101
102 # Assert the case for each individual line.
Magnus Strandh234f4b42019-05-01 23:09:30 +0200103 for prop, expected in props:
104 self.assertEqual(expected + '\n', RewriteProps(prop))
Tao Baoa7054ee2017-12-08 14:42:16 -0800105
106 # Concatenate all the input lines.
Magnus Strandh234f4b42019-05-01 23:09:30 +0200107 self.assertEqual(
108 '\n'.join([prop[1] for prop in props]) + '\n',
109 RewriteProps('\n'.join([prop[0] for prop in props])))
Tao Baoe838d142017-12-23 23:44:48 -0800110
111 def test_ReplaceVerityKeyId(self):
112 BOOT_CMDLINE1 = (
113 "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 "
114 "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 "
115 "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 "
116 "buildvariant=userdebug "
117 "veritykeyid=id:7e4333f9bba00adfe0ede979e28ed1920492b40f\n")
118
119 BOOT_CMDLINE2 = (
120 "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 "
121 "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 "
122 "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 "
123 "buildvariant=userdebug "
Tao Baode1d4792018-02-20 10:05:46 -0800124 "veritykeyid=id:d24f2590e9abab5cff5f59da4c4f0366e3f43e94\n")
Tao Baoe838d142017-12-23 23:44:48 -0800125
Tao Baode1d4792018-02-20 10:05:46 -0800126 input_file = common.MakeTempFile(suffix='.zip')
127 with zipfile.ZipFile(input_file, 'w') as input_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800128 input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE1)
129
130 # Test with the first certificate.
Tao Baode1d4792018-02-20 10:05:46 -0800131 cert_file = os.path.join(self.testdata_dir, 'verity.x509.pem')
Tao Baoe838d142017-12-23 23:44:48 -0800132
Tao Baode1d4792018-02-20 10:05:46 -0800133 output_file = common.MakeTempFile(suffix='.zip')
134 with zipfile.ZipFile(input_file, 'r') as input_zip, \
135 zipfile.ZipFile(output_file, 'w') as output_zip:
136 ReplaceVerityKeyId(input_zip, output_zip, cert_file)
Tao Baoe838d142017-12-23 23:44:48 -0800137
Tao Baode1d4792018-02-20 10:05:46 -0800138 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoa3705452019-06-24 15:33:41 -0700139 self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline').decode())
Tao Baoe838d142017-12-23 23:44:48 -0800140
141 # Test with the second certificate.
Tao Baode1d4792018-02-20 10:05:46 -0800142 cert_file = os.path.join(self.testdata_dir, 'testkey.x509.pem')
Tao Baoe838d142017-12-23 23:44:48 -0800143
Tao Baode1d4792018-02-20 10:05:46 -0800144 with zipfile.ZipFile(input_file, 'r') as input_zip, \
145 zipfile.ZipFile(output_file, 'w') as output_zip:
146 ReplaceVerityKeyId(input_zip, output_zip, cert_file)
Tao Baoe838d142017-12-23 23:44:48 -0800147
Tao Baode1d4792018-02-20 10:05:46 -0800148 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoa3705452019-06-24 15:33:41 -0700149 self.assertEqual(BOOT_CMDLINE2, output_zip.read('BOOT/cmdline').decode())
Tao Baoe838d142017-12-23 23:44:48 -0800150
151 def test_ReplaceVerityKeyId_no_veritykeyid(self):
152 BOOT_CMDLINE = (
153 "console=ttyHSL0,115200,n8 androidboot.hardware=bullhead boot_cpus=0-5 "
154 "lpm_levels.sleep_disabled=1 msm_poweroff.download_mode=0 "
155 "loop.max_part=7\n")
156
Tao Baode1d4792018-02-20 10:05:46 -0800157 input_file = common.MakeTempFile(suffix='.zip')
158 with zipfile.ZipFile(input_file, 'w') as input_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800159 input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE)
160
Tao Baode1d4792018-02-20 10:05:46 -0800161 output_file = common.MakeTempFile(suffix='.zip')
162 with zipfile.ZipFile(input_file, 'r') as input_zip, \
163 zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800164 ReplaceVerityKeyId(input_zip, output_zip, None)
165
Tao Baode1d4792018-02-20 10:05:46 -0800166 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoa3705452019-06-24 15:33:41 -0700167 self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline').decode())
Tao Bao66472632017-12-04 17:16:36 -0800168
169 def test_ReplaceCerts(self):
170 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
171 with open(cert1_path) as cert1_fp:
172 cert1 = cert1_fp.read()
173 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
174 with open(cert2_path) as cert2_fp:
175 cert2 = cert2_fp.read()
176 cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem')
177 with open(cert3_path) as cert3_fp:
178 cert3 = cert3_fp.read()
179
180 # Replace cert1 with cert3.
181 input_xml = self.MAC_PERMISSIONS_XML.format(
182 base64.b16encode(common.ParseCertificate(cert1)).lower(),
183 base64.b16encode(common.ParseCertificate(cert2)).lower())
184
185 output_xml = self.MAC_PERMISSIONS_XML.format(
186 base64.b16encode(common.ParseCertificate(cert3)).lower(),
187 base64.b16encode(common.ParseCertificate(cert2)).lower())
188
189 common.OPTIONS.key_map = {
190 cert1_path[:-9] : cert3_path[:-9],
191 }
192
193 self.assertEqual(output_xml, ReplaceCerts(input_xml))
194
195 def test_ReplaceCerts_duplicateEntries(self):
196 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
197 with open(cert1_path) as cert1_fp:
198 cert1 = cert1_fp.read()
199 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
200 with open(cert2_path) as cert2_fp:
201 cert2 = cert2_fp.read()
202
203 # Replace cert1 with cert2, which leads to duplicate entries.
204 input_xml = self.MAC_PERMISSIONS_XML.format(
205 base64.b16encode(common.ParseCertificate(cert1)).lower(),
206 base64.b16encode(common.ParseCertificate(cert2)).lower())
207
208 common.OPTIONS.key_map = {
209 cert1_path[:-9] : cert2_path[:-9],
210 }
211 self.assertRaises(AssertionError, ReplaceCerts, input_xml)
212
213 def test_ReplaceCerts_skipNonExistentCerts(self):
214 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
215 with open(cert1_path) as cert1_fp:
216 cert1 = cert1_fp.read()
217 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
218 with open(cert2_path) as cert2_fp:
219 cert2 = cert2_fp.read()
220 cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem')
221 with open(cert3_path) as cert3_fp:
222 cert3 = cert3_fp.read()
223
224 input_xml = self.MAC_PERMISSIONS_XML.format(
225 base64.b16encode(common.ParseCertificate(cert1)).lower(),
226 base64.b16encode(common.ParseCertificate(cert2)).lower())
227
228 output_xml = self.MAC_PERMISSIONS_XML.format(
229 base64.b16encode(common.ParseCertificate(cert3)).lower(),
230 base64.b16encode(common.ParseCertificate(cert2)).lower())
231
232 common.OPTIONS.key_map = {
233 cert1_path[:-9] : cert3_path[:-9],
234 'non-existent' : cert3_path[:-9],
235 cert2_path[:-9] : 'non-existent',
236 }
237 self.assertEqual(output_xml, ReplaceCerts(input_xml))
Tao Bao11f955c2018-06-19 12:19:35 -0700238
Tao Baoaa7e9932019-03-15 09:37:01 -0700239 def test_CheckApkAndApexKeysAvailable(self):
Tao Bao11f955c2018-06-19 12:19:35 -0700240 input_file = common.MakeTempFile(suffix='.zip')
241 with zipfile.ZipFile(input_file, 'w') as input_zip:
242 input_zip.writestr('SYSTEM/app/App1.apk', "App1-content")
243 input_zip.writestr('SYSTEM/app/App2.apk.gz', "App2-content")
244
245 apk_key_map = {
246 'App1.apk' : 'key1',
247 'App2.apk' : 'key2',
248 'App3.apk' : 'key3',
249 }
250 with zipfile.ZipFile(input_file) as input_zip:
Tao Baoe1343992019-03-19 12:24:03 -0700251 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {})
252 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.gz', {})
Tao Bao11f955c2018-06-19 12:19:35 -0700253
254 # 'App2.apk.gz' won't be considered as an APK.
Tao Baoe1343992019-03-19 12:24:03 -0700255 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {})
256 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.xz', {})
Tao Bao11f955c2018-06-19 12:19:35 -0700257
258 del apk_key_map['App2.apk']
259 self.assertRaises(
Tao Baoaa7e9932019-03-15 09:37:01 -0700260 AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map,
Tao Baoe1343992019-03-19 12:24:03 -0700261 '.gz', {})
262
263 def test_CheckApkAndApexKeysAvailable_invalidApexKeys(self):
264 input_file = common.MakeTempFile(suffix='.zip')
265 with zipfile.ZipFile(input_file, 'w') as input_zip:
266 input_zip.writestr('SYSTEM/apex/Apex1.apex', "Apex1-content")
267 input_zip.writestr('SYSTEM/apex/Apex2.apex', "Apex2-content")
268
269 apk_key_map = {
270 'Apex1.apex' : 'key1',
271 'Apex2.apex' : 'key2',
272 'Apex3.apex' : 'key3',
273 }
274 apex_keys = {
275 'Apex1.apex' : ('payload-key1', 'container-key1'),
276 'Apex2.apex' : ('payload-key2', 'container-key2'),
277 }
278 with zipfile.ZipFile(input_file) as input_zip:
279 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, apex_keys)
280
281 # Fine to have both keys as PRESIGNED.
282 apex_keys['Apex2.apex'] = ('PRESIGNED', 'PRESIGNED')
283 CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, apex_keys)
284
285 # Having only one of them as PRESIGNED is not allowed.
286 apex_keys['Apex2.apex'] = ('payload-key2', 'PRESIGNED')
287 self.assertRaises(
288 AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map,
289 None, apex_keys)
290
291 apex_keys['Apex2.apex'] = ('PRESIGNED', 'container-key1')
292 self.assertRaises(
293 AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map,
294 None, apex_keys)
Tao Bao11f955c2018-06-19 12:19:35 -0700295
296 def test_GetApkFileInfo(self):
Tao Bao93c2a012018-06-19 12:19:35 -0700297 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
298 "PRODUCT/apps/Chats.apk", None, [])
Tao Bao11f955c2018-06-19 12:19:35 -0700299 self.assertTrue(is_apk)
300 self.assertFalse(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700301 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700302
Tao Bao93c2a012018-06-19 12:19:35 -0700303 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
304 "PRODUCT/apps/Chats.apk", None, [])
305 self.assertTrue(is_apk)
306 self.assertFalse(is_compressed)
307 self.assertFalse(should_be_skipped)
308
309 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
310 "PRODUCT/apps/Chats.dat", None, [])
Tao Bao11f955c2018-06-19 12:19:35 -0700311 self.assertFalse(is_apk)
312 self.assertFalse(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700313 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700314
315 def test_GetApkFileInfo_withCompressedApks(self):
Tao Bao93c2a012018-06-19 12:19:35 -0700316 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
317 "PRODUCT/apps/Chats.apk.gz", ".gz", [])
Tao Bao11f955c2018-06-19 12:19:35 -0700318 self.assertTrue(is_apk)
319 self.assertTrue(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700320 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700321
Tao Bao93c2a012018-06-19 12:19:35 -0700322 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
323 "PRODUCT/apps/Chats.apk.gz", ".xz", [])
Tao Bao11f955c2018-06-19 12:19:35 -0700324 self.assertFalse(is_apk)
325 self.assertFalse(is_compressed)
Tao Bao93c2a012018-06-19 12:19:35 -0700326 self.assertFalse(should_be_skipped)
Tao Bao11f955c2018-06-19 12:19:35 -0700327
328 self.assertRaises(
Tao Bao93c2a012018-06-19 12:19:35 -0700329 AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "", [])
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", "apk", [])
333
334 def test_GetApkFileInfo_withSkippedPrefixes(self):
335 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
336 "PRODUCT/preloads/apps/Chats.apk", None, set())
337 self.assertTrue(is_apk)
338 self.assertFalse(is_compressed)
339 self.assertFalse(should_be_skipped)
340
341 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
342 "PRODUCT/preloads/apps/Chats.apk",
343 None,
344 set(["PRODUCT/preloads/"]))
345 self.assertTrue(is_apk)
346 self.assertFalse(is_compressed)
347 self.assertTrue(should_be_skipped)
348
349 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
350 "SYSTEM_OTHER/preloads/apps/Chats.apk",
351 None,
352 set(["SYSTEM/preloads/", "SYSTEM_OTHER/preloads/"]))
353 self.assertTrue(is_apk)
354 self.assertFalse(is_compressed)
355 self.assertTrue(should_be_skipped)
356
357 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
358 "SYSTEM_OTHER/preloads/apps/Chats.apk.gz",
359 ".gz",
360 set(["PRODUCT/prebuilts/", "SYSTEM_OTHER/preloads/"]))
361 self.assertTrue(is_apk)
362 self.assertTrue(is_compressed)
363 self.assertTrue(should_be_skipped)
364
365 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
366 "SYSTEM_OTHER/preloads/apps/Chats.dat",
367 None,
368 set(["SYSTEM_OTHER/preloads/"]))
369 self.assertFalse(is_apk)
370 self.assertFalse(is_compressed)
371 self.assertFalse(should_be_skipped)
372
373 def test_GetApkFileInfo_checkSkippedPrefixesInput(self):
374 # set
375 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
376 "SYSTEM_OTHER/preloads/apps/Chats.apk",
377 None,
378 set(["SYSTEM_OTHER/preloads/"]))
379 self.assertTrue(is_apk)
380 self.assertFalse(is_compressed)
381 self.assertTrue(should_be_skipped)
382
383 # tuple
384 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
385 "SYSTEM_OTHER/preloads/apps/Chats.apk",
386 None,
387 ("SYSTEM_OTHER/preloads/",))
388 self.assertTrue(is_apk)
389 self.assertFalse(is_compressed)
390 self.assertTrue(should_be_skipped)
391
392 # list
393 (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(
394 "SYSTEM_OTHER/preloads/apps/Chats.apk",
395 None,
396 ["SYSTEM_OTHER/preloads/"])
397 self.assertTrue(is_apk)
398 self.assertFalse(is_compressed)
399 self.assertTrue(should_be_skipped)
400
401 # str is invalid.
402 self.assertRaises(
403 AssertionError, GetApkFileInfo, "SYSTEM_OTHER/preloads/apps/Chats.apk",
404 None, "SYSTEM_OTHER/preloads/")
405
406 # None is invalid.
407 self.assertRaises(
408 AssertionError, GetApkFileInfo, "SYSTEM_OTHER/preloads/apps/Chats.apk",
409 None, None)
Tao Baoaa7e9932019-03-15 09:37:01 -0700410
411 def test_ReadApexKeysInfo(self):
412 target_files = common.MakeTempFile(suffix='.zip')
413 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
414 target_files_zip.writestr('META/apexkeys.txt', self.APEX_KEYS_TXT)
415
416 with zipfile.ZipFile(target_files) as target_files_zip:
417 keys_info = ReadApexKeysInfo(target_files_zip)
418
Tao Baoe1343992019-03-19 12:24:03 -0700419 self.assertEqual({
420 'apex.apexd_test.apex': (
421 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700422 'build/make/target/product/security/testkey'),
Tao Baoe1343992019-03-19 12:24:03 -0700423 'apex.apexd_test_different_app.apex': (
424 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700425 'build/make/target/product/security/testkey'),
Tao Baoe1343992019-03-19 12:24:03 -0700426 }, keys_info)
Tao Baoaa7e9932019-03-15 09:37:01 -0700427
Tao Bao6d9e3da2019-03-26 12:59:25 -0700428 def test_ReadApexKeysInfo_mismatchingContainerKeys(self):
Tao Baoaa7e9932019-03-15 09:37:01 -0700429 # Mismatching payload public / private keys.
430 apex_keys = self.APEX_KEYS_TXT + (
431 'name="apex.apexd_test_different_app2.apex" '
432 'public_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.avbpubkey" '
Tao Bao6d9e3da2019-03-26 12:59:25 -0700433 'private_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem" '
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700434 'container_certificate="build/make/target/product/security/testkey.x509.pem" '
435 'container_private_key="build/make/target/product/security/testkey2.pk8"')
Tao Baoaa7e9932019-03-15 09:37:01 -0700436 target_files = common.MakeTempFile(suffix='.zip')
437 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
438 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
439
440 with zipfile.ZipFile(target_files) as target_files_zip:
441 self.assertRaises(ValueError, ReadApexKeysInfo, target_files_zip)
442
Tao Bao6d9e3da2019-03-26 12:59:25 -0700443 def test_ReadApexKeysInfo_missingPayloadPrivateKey(self):
Tao Baoaa7e9932019-03-15 09:37:01 -0700444 # Invalid lines will be skipped.
445 apex_keys = self.APEX_KEYS_TXT + (
446 'name="apex.apexd_test_different_app2.apex" '
447 'public_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.avbpubkey" '
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700448 'container_certificate="build/make/target/product/security/testkey.x509.pem" '
449 'container_private_key="build/make/target/product/security/testkey.pk8"')
Tao Baoaa7e9932019-03-15 09:37:01 -0700450 target_files = common.MakeTempFile(suffix='.zip')
451 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
452 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
453
454 with zipfile.ZipFile(target_files) as target_files_zip:
455 keys_info = ReadApexKeysInfo(target_files_zip)
456
Tao Baoe1343992019-03-19 12:24:03 -0700457 self.assertEqual({
458 'apex.apexd_test.apex': (
459 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700460 'build/make/target/product/security/testkey'),
Tao Baoe1343992019-03-19 12:24:03 -0700461 'apex.apexd_test_different_app.apex': (
462 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700463 'build/make/target/product/security/testkey'),
Tao Baoe1343992019-03-19 12:24:03 -0700464 }, keys_info)
Tao Bao6d9e3da2019-03-26 12:59:25 -0700465
466 def test_ReadApexKeysInfo_missingPayloadPublicKey(self):
467 # Invalid lines will be skipped.
468 apex_keys = self.APEX_KEYS_TXT + (
469 'name="apex.apexd_test_different_app2.apex" '
470 'private_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem" '
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700471 'container_certificate="build/make/target/product/security/testkey.x509.pem" '
472 'container_private_key="build/make/target/product/security/testkey.pk8"')
Tao Bao6d9e3da2019-03-26 12:59:25 -0700473 target_files = common.MakeTempFile(suffix='.zip')
474 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
475 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
476
477 with zipfile.ZipFile(target_files) as target_files_zip:
478 keys_info = ReadApexKeysInfo(target_files_zip)
479
480 self.assertEqual({
481 'apex.apexd_test.apex': (
482 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700483 'build/make/target/product/security/testkey'),
Tao Bao6d9e3da2019-03-26 12:59:25 -0700484 'apex.apexd_test_different_app.apex': (
485 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
Dan Willemsen0ab1be62019-04-09 21:35:37 -0700486 'build/make/target/product/security/testkey'),
Tao Bao6d9e3da2019-03-26 12:59:25 -0700487 }, keys_info)
Tao Baof454c3a2019-04-24 23:53:42 -0700488
489 def test_ReadApexKeysInfo_presignedKeys(self):
490 apex_keys = self.APEX_KEYS_TXT + (
491 'name="apex.apexd_test_different_app2.apex" '
492 'private_key="PRESIGNED" '
493 'public_key="PRESIGNED" '
494 'container_certificate="PRESIGNED" '
495 'container_private_key="PRESIGNED"')
496 target_files = common.MakeTempFile(suffix='.zip')
497 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
498 target_files_zip.writestr('META/apexkeys.txt', apex_keys)
499
500 with zipfile.ZipFile(target_files) as target_files_zip:
501 keys_info = ReadApexKeysInfo(target_files_zip)
502
503 self.assertEqual({
504 'apex.apexd_test.apex': (
505 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem',
506 'build/make/target/product/security/testkey'),
507 'apex.apexd_test_different_app.apex': (
508 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem',
509 'build/make/target/product/security/testkey'),
510 }, keys_info)