Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 1 | # |
| 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 Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 17 | import base64 |
Tao Bao | bb73388 | 2019-07-24 23:31:19 -0700 | [diff] [blame] | 18 | import io |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 19 | import os.path |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 20 | import zipfile |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 21 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 22 | import common |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 23 | import test_utils |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 24 | from sign_target_files_apks import ( |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 25 | CheckApkAndApexKeysAvailable, EditTags, GetApkFileInfo, ReadApexKeysInfo, |
Tao Bao | bb73388 | 2019-07-24 23:31:19 -0700 | [diff] [blame] | 26 | ReplaceCerts, ReplaceVerityKeyId, RewriteProps, WriteOtacerts) |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 27 | |
| 28 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 29 | class SignTargetFilesApksTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 30 | |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 31 | 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 Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 37 | # pylint: disable=line-too-long |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 38 | 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" |
| 39 | name="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 Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 40 | """ |
| 41 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 42 | def setUp(self): |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 43 | self.testdata_dir = test_utils.get_testdata_dir() |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 44 | |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 45 | 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 Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 57 | ('', ''), |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 58 | ('ro.build.fingerprint=foo/bar/dev-keys', |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 59 | 'ro.build.fingerprint=foo/bar/release-keys'), |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 60 | ('ro.build.thumbprint=foo/bar/dev-keys', |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 61 | 'ro.build.thumbprint=foo/bar/release-keys'), |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 62 | ('ro.vendor.build.fingerprint=foo/bar/dev-keys', |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 63 | 'ro.vendor.build.fingerprint=foo/bar/release-keys'), |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 64 | ('ro.vendor.build.thumbprint=foo/bar/dev-keys', |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 65 | '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 Yun | 6151e3f | 2019-06-25 15:58:13 +0900 | [diff] [blame] | 74 | ('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 Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 78 | ('# comment line 1', '# comment line 1'), |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 79 | ('ro.bootimage.build.fingerprint=foo/bar/dev-keys', |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 80 | 'ro.bootimage.build.fingerprint=foo/bar/release-keys'), |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 81 | ('ro.build.description=' |
| 82 | 'sailfish-user 8.0.0 OPR6.170623.012 4283428 dev-keys', |
| 83 | 'ro.build.description=' |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 84 | '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 Yun | 6151e3f | 2019-06-25 15:58:13 +0900 | [diff] [blame] | 95 | ('ro.system_ext.build.tags=dev-keys', |
| 96 | 'ro.system_ext.build.tags=release-keys'), |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 97 | ('# comment line 2', '# comment line 2'), |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 98 | ('ro.build.display.id=OPR6.170623.012 dev-keys', |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 99 | 'ro.build.display.id=OPR6.170623.012'), |
| 100 | ('# comment line 3', '# comment line 3'), |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 101 | ) |
| 102 | |
| 103 | # Assert the case for each individual line. |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 104 | for prop, expected in props: |
| 105 | self.assertEqual(expected + '\n', RewriteProps(prop)) |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 106 | |
| 107 | # Concatenate all the input lines. |
Magnus Strandh | 234f4b4 | 2019-05-01 23:09:30 +0200 | [diff] [blame] | 108 | self.assertEqual( |
| 109 | '\n'.join([prop[1] for prop in props]) + '\n', |
| 110 | RewriteProps('\n'.join([prop[0] for prop in props]))) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 111 | |
| 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 Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 125 | "veritykeyid=id:d24f2590e9abab5cff5f59da4c4f0366e3f43e94\n") |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 126 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 127 | input_file = common.MakeTempFile(suffix='.zip') |
| 128 | with zipfile.ZipFile(input_file, 'w') as input_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 129 | input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE1) |
| 130 | |
| 131 | # Test with the first certificate. |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 132 | cert_file = os.path.join(self.testdata_dir, 'verity.x509.pem') |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 133 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 134 | 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 Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 138 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 139 | with zipfile.ZipFile(output_file) as output_zip: |
Tao Bao | a370545 | 2019-06-24 15:33:41 -0700 | [diff] [blame] | 140 | self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline').decode()) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 141 | |
| 142 | # Test with the second certificate. |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 143 | cert_file = os.path.join(self.testdata_dir, 'testkey.x509.pem') |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 144 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 145 | 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 Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 148 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 149 | with zipfile.ZipFile(output_file) as output_zip: |
Tao Bao | a370545 | 2019-06-24 15:33:41 -0700 | [diff] [blame] | 150 | self.assertEqual(BOOT_CMDLINE2, output_zip.read('BOOT/cmdline').decode()) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 151 | |
| 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 Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 158 | input_file = common.MakeTempFile(suffix='.zip') |
| 159 | with zipfile.ZipFile(input_file, 'w') as input_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 160 | input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE) |
| 161 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 162 | 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 Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 165 | ReplaceVerityKeyId(input_zip, output_zip, None) |
| 166 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 167 | with zipfile.ZipFile(output_file) as output_zip: |
Tao Bao | a370545 | 2019-06-24 15:33:41 -0700 | [diff] [blame] | 168 | self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline').decode()) |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 169 | |
| 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 Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 239 | |
Tao Bao | bb73388 | 2019-07-24 23:31:19 -0700 | [diff] [blame] | 240 | 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 Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 256 | def test_CheckApkAndApexKeysAvailable(self): |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 257 | 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 Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 268 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {}) |
| 269 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.gz', {}) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 270 | |
| 271 | # 'App2.apk.gz' won't be considered as an APK. |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 272 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {}) |
| 273 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.xz', {}) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 274 | |
| 275 | del apk_key_map['App2.apk'] |
| 276 | self.assertRaises( |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 277 | AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map, |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 278 | '.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 Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 312 | |
| 313 | def test_GetApkFileInfo(self): |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 314 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 315 | "PRODUCT/apps/Chats.apk", None, []) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 316 | self.assertTrue(is_apk) |
| 317 | self.assertFalse(is_compressed) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 318 | self.assertFalse(should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 319 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 320 | (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 Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 328 | self.assertFalse(is_apk) |
| 329 | self.assertFalse(is_compressed) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 330 | self.assertFalse(should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 331 | |
| 332 | def test_GetApkFileInfo_withCompressedApks(self): |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 333 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 334 | "PRODUCT/apps/Chats.apk.gz", ".gz", []) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 335 | self.assertTrue(is_apk) |
| 336 | self.assertTrue(is_compressed) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 337 | self.assertFalse(should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 338 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 339 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 340 | "PRODUCT/apps/Chats.apk.gz", ".xz", []) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 341 | self.assertFalse(is_apk) |
| 342 | self.assertFalse(is_compressed) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 343 | self.assertFalse(should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 344 | |
| 345 | self.assertRaises( |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 346 | AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "", []) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 347 | |
| 348 | self.assertRaises( |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 349 | 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 Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 427 | |
| 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 Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 436 | self.assertEqual({ |
| 437 | 'apex.apexd_test.apex': ( |
| 438 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 439 | 'build/make/target/product/security/testkey'), |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 440 | 'apex.apexd_test_different_app.apex': ( |
| 441 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 442 | 'build/make/target/product/security/testkey'), |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 443 | }, keys_info) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 444 | |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 445 | def test_ReadApexKeysInfo_mismatchingContainerKeys(self): |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 446 | # 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 Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 450 | 'private_key="system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem" ' |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 451 | 'container_certificate="build/make/target/product/security/testkey.x509.pem" ' |
| 452 | 'container_private_key="build/make/target/product/security/testkey2.pk8"') |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 453 | 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 Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 460 | def test_ReadApexKeysInfo_missingPayloadPrivateKey(self): |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 461 | # 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 Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 465 | 'container_certificate="build/make/target/product/security/testkey.x509.pem" ' |
| 466 | 'container_private_key="build/make/target/product/security/testkey.pk8"') |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 467 | 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 Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 474 | self.assertEqual({ |
| 475 | 'apex.apexd_test.apex': ( |
| 476 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 477 | 'build/make/target/product/security/testkey'), |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 478 | 'apex.apexd_test_different_app.apex': ( |
| 479 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 480 | 'build/make/target/product/security/testkey'), |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 481 | }, keys_info) |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 482 | |
| 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 Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 488 | 'container_certificate="build/make/target/product/security/testkey.x509.pem" ' |
| 489 | 'container_private_key="build/make/target/product/security/testkey.pk8"') |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 490 | 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 Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 500 | 'build/make/target/product/security/testkey'), |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 501 | 'apex.apexd_test_different_app.apex': ( |
| 502 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 503 | 'build/make/target/product/security/testkey'), |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 504 | }, keys_info) |
Tao Bao | f454c3a | 2019-04-24 23:53:42 -0700 | [diff] [blame] | 505 | |
| 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 Bao | 548db7d | 2019-04-24 23:53:42 -0700 | [diff] [blame] | 528 | |
| 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) |