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 | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 18 | import os.path |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 19 | import zipfile |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 20 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 21 | import common |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 22 | import test_utils |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 23 | from sign_target_files_apks import ( |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 24 | CheckApkAndApexKeysAvailable, EditTags, GetApkFileInfo, ReadApexKeysInfo, |
| 25 | ReplaceCerts, ReplaceVerityKeyId, RewriteProps) |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 26 | |
| 27 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 28 | class SignTargetFilesApksTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 29 | |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 30 | 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 Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 36 | # pylint: disable=line-too-long |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 37 | 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" |
| 38 | 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] | 39 | """ |
| 40 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 41 | def setUp(self): |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 42 | self.testdata_dir = test_utils.get_testdata_dir() |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 43 | |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 44 | 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 = ( |
| 56 | ('', '\n'), |
| 57 | ('ro.build.fingerprint=foo/bar/dev-keys', |
| 58 | 'ro.build.fingerprint=foo/bar/release-keys\n'), |
| 59 | ('ro.build.thumbprint=foo/bar/dev-keys', |
| 60 | 'ro.build.thumbprint=foo/bar/release-keys\n'), |
| 61 | ('ro.vendor.build.fingerprint=foo/bar/dev-keys', |
| 62 | 'ro.vendor.build.fingerprint=foo/bar/release-keys\n'), |
| 63 | ('ro.vendor.build.thumbprint=foo/bar/dev-keys', |
| 64 | 'ro.vendor.build.thumbprint=foo/bar/release-keys\n'), |
| 65 | ('# comment line 1', '# comment line 1\n'), |
| 66 | ('ro.bootimage.build.fingerprint=foo/bar/dev-keys', |
| 67 | 'ro.bootimage.build.fingerprint=foo/bar/release-keys\n'), |
| 68 | ('ro.build.description=' |
| 69 | 'sailfish-user 8.0.0 OPR6.170623.012 4283428 dev-keys', |
| 70 | 'ro.build.description=' |
| 71 | 'sailfish-user 8.0.0 OPR6.170623.012 4283428 release-keys\n'), |
| 72 | ('ro.build.tags=dev-keys', 'ro.build.tags=release-keys\n'), |
| 73 | ('# comment line 2', '# comment line 2\n'), |
| 74 | ('ro.build.display.id=OPR6.170623.012 dev-keys', |
| 75 | 'ro.build.display.id=OPR6.170623.012\n'), |
| 76 | ('# comment line 3', '# comment line 3\n'), |
| 77 | ) |
| 78 | |
| 79 | # Assert the case for each individual line. |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 80 | for prop, output in props: |
| 81 | self.assertEqual(RewriteProps(prop), output) |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 82 | |
| 83 | # Concatenate all the input lines. |
| 84 | self.assertEqual(RewriteProps('\n'.join([prop[0] for prop in props])), |
| 85 | ''.join([prop[1] for prop in props])) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 86 | |
| 87 | def test_ReplaceVerityKeyId(self): |
| 88 | BOOT_CMDLINE1 = ( |
| 89 | "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 " |
| 90 | "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 " |
| 91 | "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 " |
| 92 | "buildvariant=userdebug " |
| 93 | "veritykeyid=id:7e4333f9bba00adfe0ede979e28ed1920492b40f\n") |
| 94 | |
| 95 | BOOT_CMDLINE2 = ( |
| 96 | "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 " |
| 97 | "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 " |
| 98 | "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 " |
| 99 | "buildvariant=userdebug " |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 100 | "veritykeyid=id:d24f2590e9abab5cff5f59da4c4f0366e3f43e94\n") |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 101 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 102 | input_file = common.MakeTempFile(suffix='.zip') |
| 103 | with zipfile.ZipFile(input_file, 'w') as input_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 104 | input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE1) |
| 105 | |
| 106 | # Test with the first certificate. |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 107 | cert_file = os.path.join(self.testdata_dir, 'verity.x509.pem') |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 108 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 109 | output_file = common.MakeTempFile(suffix='.zip') |
| 110 | with zipfile.ZipFile(input_file, 'r') as input_zip, \ |
| 111 | zipfile.ZipFile(output_file, 'w') as output_zip: |
| 112 | ReplaceVerityKeyId(input_zip, output_zip, cert_file) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 113 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 114 | with zipfile.ZipFile(output_file) as output_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 115 | self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline')) |
| 116 | |
| 117 | # Test with the second certificate. |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 118 | cert_file = os.path.join(self.testdata_dir, 'testkey.x509.pem') |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 119 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 120 | with zipfile.ZipFile(input_file, 'r') as input_zip, \ |
| 121 | zipfile.ZipFile(output_file, 'w') as output_zip: |
| 122 | ReplaceVerityKeyId(input_zip, output_zip, cert_file) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 123 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 124 | with zipfile.ZipFile(output_file) as output_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 125 | self.assertEqual(BOOT_CMDLINE2, output_zip.read('BOOT/cmdline')) |
| 126 | |
| 127 | def test_ReplaceVerityKeyId_no_veritykeyid(self): |
| 128 | BOOT_CMDLINE = ( |
| 129 | "console=ttyHSL0,115200,n8 androidboot.hardware=bullhead boot_cpus=0-5 " |
| 130 | "lpm_levels.sleep_disabled=1 msm_poweroff.download_mode=0 " |
| 131 | "loop.max_part=7\n") |
| 132 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 133 | input_file = common.MakeTempFile(suffix='.zip') |
| 134 | with zipfile.ZipFile(input_file, 'w') as input_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 135 | input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE) |
| 136 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 137 | output_file = common.MakeTempFile(suffix='.zip') |
| 138 | with zipfile.ZipFile(input_file, 'r') as input_zip, \ |
| 139 | zipfile.ZipFile(output_file, 'w') as output_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 140 | ReplaceVerityKeyId(input_zip, output_zip, None) |
| 141 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame] | 142 | with zipfile.ZipFile(output_file) as output_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 143 | self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline')) |
Tao Bao | 6647263 | 2017-12-04 17:16:36 -0800 | [diff] [blame] | 144 | |
| 145 | def test_ReplaceCerts(self): |
| 146 | cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem') |
| 147 | with open(cert1_path) as cert1_fp: |
| 148 | cert1 = cert1_fp.read() |
| 149 | cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem') |
| 150 | with open(cert2_path) as cert2_fp: |
| 151 | cert2 = cert2_fp.read() |
| 152 | cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem') |
| 153 | with open(cert3_path) as cert3_fp: |
| 154 | cert3 = cert3_fp.read() |
| 155 | |
| 156 | # Replace cert1 with cert3. |
| 157 | input_xml = self.MAC_PERMISSIONS_XML.format( |
| 158 | base64.b16encode(common.ParseCertificate(cert1)).lower(), |
| 159 | base64.b16encode(common.ParseCertificate(cert2)).lower()) |
| 160 | |
| 161 | output_xml = self.MAC_PERMISSIONS_XML.format( |
| 162 | base64.b16encode(common.ParseCertificate(cert3)).lower(), |
| 163 | base64.b16encode(common.ParseCertificate(cert2)).lower()) |
| 164 | |
| 165 | common.OPTIONS.key_map = { |
| 166 | cert1_path[:-9] : cert3_path[:-9], |
| 167 | } |
| 168 | |
| 169 | self.assertEqual(output_xml, ReplaceCerts(input_xml)) |
| 170 | |
| 171 | def test_ReplaceCerts_duplicateEntries(self): |
| 172 | cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem') |
| 173 | with open(cert1_path) as cert1_fp: |
| 174 | cert1 = cert1_fp.read() |
| 175 | cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem') |
| 176 | with open(cert2_path) as cert2_fp: |
| 177 | cert2 = cert2_fp.read() |
| 178 | |
| 179 | # Replace cert1 with cert2, which leads to duplicate entries. |
| 180 | input_xml = self.MAC_PERMISSIONS_XML.format( |
| 181 | base64.b16encode(common.ParseCertificate(cert1)).lower(), |
| 182 | base64.b16encode(common.ParseCertificate(cert2)).lower()) |
| 183 | |
| 184 | common.OPTIONS.key_map = { |
| 185 | cert1_path[:-9] : cert2_path[:-9], |
| 186 | } |
| 187 | self.assertRaises(AssertionError, ReplaceCerts, input_xml) |
| 188 | |
| 189 | def test_ReplaceCerts_skipNonExistentCerts(self): |
| 190 | cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem') |
| 191 | with open(cert1_path) as cert1_fp: |
| 192 | cert1 = cert1_fp.read() |
| 193 | cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem') |
| 194 | with open(cert2_path) as cert2_fp: |
| 195 | cert2 = cert2_fp.read() |
| 196 | cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem') |
| 197 | with open(cert3_path) as cert3_fp: |
| 198 | cert3 = cert3_fp.read() |
| 199 | |
| 200 | input_xml = self.MAC_PERMISSIONS_XML.format( |
| 201 | base64.b16encode(common.ParseCertificate(cert1)).lower(), |
| 202 | base64.b16encode(common.ParseCertificate(cert2)).lower()) |
| 203 | |
| 204 | output_xml = self.MAC_PERMISSIONS_XML.format( |
| 205 | base64.b16encode(common.ParseCertificate(cert3)).lower(), |
| 206 | base64.b16encode(common.ParseCertificate(cert2)).lower()) |
| 207 | |
| 208 | common.OPTIONS.key_map = { |
| 209 | cert1_path[:-9] : cert3_path[:-9], |
| 210 | 'non-existent' : cert3_path[:-9], |
| 211 | cert2_path[:-9] : 'non-existent', |
| 212 | } |
| 213 | self.assertEqual(output_xml, ReplaceCerts(input_xml)) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 214 | |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 215 | def test_CheckApkAndApexKeysAvailable(self): |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 216 | input_file = common.MakeTempFile(suffix='.zip') |
| 217 | with zipfile.ZipFile(input_file, 'w') as input_zip: |
| 218 | input_zip.writestr('SYSTEM/app/App1.apk', "App1-content") |
| 219 | input_zip.writestr('SYSTEM/app/App2.apk.gz', "App2-content") |
| 220 | |
| 221 | apk_key_map = { |
| 222 | 'App1.apk' : 'key1', |
| 223 | 'App2.apk' : 'key2', |
| 224 | 'App3.apk' : 'key3', |
| 225 | } |
| 226 | with zipfile.ZipFile(input_file) as input_zip: |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 227 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {}) |
| 228 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.gz', {}) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 229 | |
| 230 | # 'App2.apk.gz' won't be considered as an APK. |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 231 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, {}) |
| 232 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, '.xz', {}) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 233 | |
| 234 | del apk_key_map['App2.apk'] |
| 235 | self.assertRaises( |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 236 | AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map, |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 237 | '.gz', {}) |
| 238 | |
| 239 | def test_CheckApkAndApexKeysAvailable_invalidApexKeys(self): |
| 240 | input_file = common.MakeTempFile(suffix='.zip') |
| 241 | with zipfile.ZipFile(input_file, 'w') as input_zip: |
| 242 | input_zip.writestr('SYSTEM/apex/Apex1.apex', "Apex1-content") |
| 243 | input_zip.writestr('SYSTEM/apex/Apex2.apex', "Apex2-content") |
| 244 | |
| 245 | apk_key_map = { |
| 246 | 'Apex1.apex' : 'key1', |
| 247 | 'Apex2.apex' : 'key2', |
| 248 | 'Apex3.apex' : 'key3', |
| 249 | } |
| 250 | apex_keys = { |
| 251 | 'Apex1.apex' : ('payload-key1', 'container-key1'), |
| 252 | 'Apex2.apex' : ('payload-key2', 'container-key2'), |
| 253 | } |
| 254 | with zipfile.ZipFile(input_file) as input_zip: |
| 255 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, apex_keys) |
| 256 | |
| 257 | # Fine to have both keys as PRESIGNED. |
| 258 | apex_keys['Apex2.apex'] = ('PRESIGNED', 'PRESIGNED') |
| 259 | CheckApkAndApexKeysAvailable(input_zip, apk_key_map, None, apex_keys) |
| 260 | |
| 261 | # Having only one of them as PRESIGNED is not allowed. |
| 262 | apex_keys['Apex2.apex'] = ('payload-key2', 'PRESIGNED') |
| 263 | self.assertRaises( |
| 264 | AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map, |
| 265 | None, apex_keys) |
| 266 | |
| 267 | apex_keys['Apex2.apex'] = ('PRESIGNED', 'container-key1') |
| 268 | self.assertRaises( |
| 269 | AssertionError, CheckApkAndApexKeysAvailable, input_zip, apk_key_map, |
| 270 | None, apex_keys) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 271 | |
| 272 | def test_GetApkFileInfo(self): |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 273 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 274 | "PRODUCT/apps/Chats.apk", None, []) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 275 | self.assertTrue(is_apk) |
| 276 | self.assertFalse(is_compressed) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 277 | self.assertFalse(should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 278 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 279 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 280 | "PRODUCT/apps/Chats.apk", None, []) |
| 281 | self.assertTrue(is_apk) |
| 282 | self.assertFalse(is_compressed) |
| 283 | self.assertFalse(should_be_skipped) |
| 284 | |
| 285 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 286 | "PRODUCT/apps/Chats.dat", None, []) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 287 | self.assertFalse(is_apk) |
| 288 | self.assertFalse(is_compressed) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 289 | self.assertFalse(should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 290 | |
| 291 | def test_GetApkFileInfo_withCompressedApks(self): |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 292 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 293 | "PRODUCT/apps/Chats.apk.gz", ".gz", []) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 294 | self.assertTrue(is_apk) |
| 295 | self.assertTrue(is_compressed) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 296 | self.assertFalse(should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 297 | |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 298 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 299 | "PRODUCT/apps/Chats.apk.gz", ".xz", []) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 300 | self.assertFalse(is_apk) |
| 301 | self.assertFalse(is_compressed) |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 302 | self.assertFalse(should_be_skipped) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 303 | |
| 304 | self.assertRaises( |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 305 | AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "", []) |
Tao Bao | 11f955c | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 306 | |
| 307 | self.assertRaises( |
Tao Bao | 93c2a01 | 2018-06-19 12:19:35 -0700 | [diff] [blame] | 308 | AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "apk", []) |
| 309 | |
| 310 | def test_GetApkFileInfo_withSkippedPrefixes(self): |
| 311 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 312 | "PRODUCT/preloads/apps/Chats.apk", None, set()) |
| 313 | self.assertTrue(is_apk) |
| 314 | self.assertFalse(is_compressed) |
| 315 | self.assertFalse(should_be_skipped) |
| 316 | |
| 317 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 318 | "PRODUCT/preloads/apps/Chats.apk", |
| 319 | None, |
| 320 | set(["PRODUCT/preloads/"])) |
| 321 | self.assertTrue(is_apk) |
| 322 | self.assertFalse(is_compressed) |
| 323 | self.assertTrue(should_be_skipped) |
| 324 | |
| 325 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 326 | "SYSTEM_OTHER/preloads/apps/Chats.apk", |
| 327 | None, |
| 328 | set(["SYSTEM/preloads/", "SYSTEM_OTHER/preloads/"])) |
| 329 | self.assertTrue(is_apk) |
| 330 | self.assertFalse(is_compressed) |
| 331 | self.assertTrue(should_be_skipped) |
| 332 | |
| 333 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 334 | "SYSTEM_OTHER/preloads/apps/Chats.apk.gz", |
| 335 | ".gz", |
| 336 | set(["PRODUCT/prebuilts/", "SYSTEM_OTHER/preloads/"])) |
| 337 | self.assertTrue(is_apk) |
| 338 | self.assertTrue(is_compressed) |
| 339 | self.assertTrue(should_be_skipped) |
| 340 | |
| 341 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 342 | "SYSTEM_OTHER/preloads/apps/Chats.dat", |
| 343 | None, |
| 344 | set(["SYSTEM_OTHER/preloads/"])) |
| 345 | self.assertFalse(is_apk) |
| 346 | self.assertFalse(is_compressed) |
| 347 | self.assertFalse(should_be_skipped) |
| 348 | |
| 349 | def test_GetApkFileInfo_checkSkippedPrefixesInput(self): |
| 350 | # set |
| 351 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 352 | "SYSTEM_OTHER/preloads/apps/Chats.apk", |
| 353 | None, |
| 354 | set(["SYSTEM_OTHER/preloads/"])) |
| 355 | self.assertTrue(is_apk) |
| 356 | self.assertFalse(is_compressed) |
| 357 | self.assertTrue(should_be_skipped) |
| 358 | |
| 359 | # tuple |
| 360 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 361 | "SYSTEM_OTHER/preloads/apps/Chats.apk", |
| 362 | None, |
| 363 | ("SYSTEM_OTHER/preloads/",)) |
| 364 | self.assertTrue(is_apk) |
| 365 | self.assertFalse(is_compressed) |
| 366 | self.assertTrue(should_be_skipped) |
| 367 | |
| 368 | # list |
| 369 | (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo( |
| 370 | "SYSTEM_OTHER/preloads/apps/Chats.apk", |
| 371 | None, |
| 372 | ["SYSTEM_OTHER/preloads/"]) |
| 373 | self.assertTrue(is_apk) |
| 374 | self.assertFalse(is_compressed) |
| 375 | self.assertTrue(should_be_skipped) |
| 376 | |
| 377 | # str is invalid. |
| 378 | self.assertRaises( |
| 379 | AssertionError, GetApkFileInfo, "SYSTEM_OTHER/preloads/apps/Chats.apk", |
| 380 | None, "SYSTEM_OTHER/preloads/") |
| 381 | |
| 382 | # None is invalid. |
| 383 | self.assertRaises( |
| 384 | AssertionError, GetApkFileInfo, "SYSTEM_OTHER/preloads/apps/Chats.apk", |
| 385 | None, None) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 386 | |
| 387 | def test_ReadApexKeysInfo(self): |
| 388 | target_files = common.MakeTempFile(suffix='.zip') |
| 389 | with zipfile.ZipFile(target_files, 'w') as target_files_zip: |
| 390 | target_files_zip.writestr('META/apexkeys.txt', self.APEX_KEYS_TXT) |
| 391 | |
| 392 | with zipfile.ZipFile(target_files) as target_files_zip: |
| 393 | keys_info = ReadApexKeysInfo(target_files_zip) |
| 394 | |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 395 | self.assertEqual({ |
| 396 | 'apex.apexd_test.apex': ( |
| 397 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 398 | 'build/make/target/product/security/testkey'), |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 399 | 'apex.apexd_test_different_app.apex': ( |
| 400 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 401 | 'build/make/target/product/security/testkey'), |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 402 | }, keys_info) |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 403 | |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 404 | def test_ReadApexKeysInfo_mismatchingContainerKeys(self): |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 405 | # Mismatching payload public / private keys. |
| 406 | apex_keys = self.APEX_KEYS_TXT + ( |
| 407 | 'name="apex.apexd_test_different_app2.apex" ' |
| 408 | '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] | 409 | '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] | 410 | 'container_certificate="build/make/target/product/security/testkey.x509.pem" ' |
| 411 | 'container_private_key="build/make/target/product/security/testkey2.pk8"') |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 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', apex_keys) |
| 415 | |
| 416 | with zipfile.ZipFile(target_files) as target_files_zip: |
| 417 | self.assertRaises(ValueError, ReadApexKeysInfo, target_files_zip) |
| 418 | |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 419 | def test_ReadApexKeysInfo_missingPayloadPrivateKey(self): |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 420 | # Invalid lines will be skipped. |
| 421 | apex_keys = self.APEX_KEYS_TXT + ( |
| 422 | 'name="apex.apexd_test_different_app2.apex" ' |
| 423 | '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] | 424 | 'container_certificate="build/make/target/product/security/testkey.x509.pem" ' |
| 425 | 'container_private_key="build/make/target/product/security/testkey.pk8"') |
Tao Bao | aa7e993 | 2019-03-15 09:37:01 -0700 | [diff] [blame] | 426 | target_files = common.MakeTempFile(suffix='.zip') |
| 427 | with zipfile.ZipFile(target_files, 'w') as target_files_zip: |
| 428 | target_files_zip.writestr('META/apexkeys.txt', apex_keys) |
| 429 | |
| 430 | with zipfile.ZipFile(target_files) as target_files_zip: |
| 431 | keys_info = ReadApexKeysInfo(target_files_zip) |
| 432 | |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 433 | self.assertEqual({ |
| 434 | 'apex.apexd_test.apex': ( |
| 435 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 436 | 'build/make/target/product/security/testkey'), |
Tao Bao | e134399 | 2019-03-19 12:24:03 -0700 | [diff] [blame] | 437 | 'apex.apexd_test_different_app.apex': ( |
| 438 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.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 | }, keys_info) |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 441 | |
| 442 | def test_ReadApexKeysInfo_missingPayloadPublicKey(self): |
| 443 | # Invalid lines will be skipped. |
| 444 | apex_keys = self.APEX_KEYS_TXT + ( |
| 445 | 'name="apex.apexd_test_different_app2.apex" ' |
| 446 | '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] | 447 | 'container_certificate="build/make/target/product/security/testkey.x509.pem" ' |
| 448 | 'container_private_key="build/make/target/product/security/testkey.pk8"') |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 449 | target_files = common.MakeTempFile(suffix='.zip') |
| 450 | with zipfile.ZipFile(target_files, 'w') as target_files_zip: |
| 451 | target_files_zip.writestr('META/apexkeys.txt', apex_keys) |
| 452 | |
| 453 | with zipfile.ZipFile(target_files) as target_files_zip: |
| 454 | keys_info = ReadApexKeysInfo(target_files_zip) |
| 455 | |
| 456 | self.assertEqual({ |
| 457 | 'apex.apexd_test.apex': ( |
| 458 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 459 | 'build/make/target/product/security/testkey'), |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 460 | 'apex.apexd_test_different_app.apex': ( |
| 461 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem', |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 462 | 'build/make/target/product/security/testkey'), |
Tao Bao | 6d9e3da | 2019-03-26 12:59:25 -0700 | [diff] [blame] | 463 | }, keys_info) |
Tao Bao | f454c3a | 2019-04-24 23:53:42 -0700 | [diff] [blame^] | 464 | |
| 465 | def test_ReadApexKeysInfo_presignedKeys(self): |
| 466 | apex_keys = self.APEX_KEYS_TXT + ( |
| 467 | 'name="apex.apexd_test_different_app2.apex" ' |
| 468 | 'private_key="PRESIGNED" ' |
| 469 | 'public_key="PRESIGNED" ' |
| 470 | 'container_certificate="PRESIGNED" ' |
| 471 | 'container_private_key="PRESIGNED"') |
| 472 | target_files = common.MakeTempFile(suffix='.zip') |
| 473 | with zipfile.ZipFile(target_files, 'w') as target_files_zip: |
| 474 | target_files_zip.writestr('META/apexkeys.txt', apex_keys) |
| 475 | |
| 476 | with zipfile.ZipFile(target_files) as target_files_zip: |
| 477 | keys_info = ReadApexKeysInfo(target_files_zip) |
| 478 | |
| 479 | self.assertEqual({ |
| 480 | 'apex.apexd_test.apex': ( |
| 481 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package.pem', |
| 482 | 'build/make/target/product/security/testkey'), |
| 483 | 'apex.apexd_test_different_app.apex': ( |
| 484 | 'system/apex/apexd/apexd_testdata/com.android.apex.test_package_2.pem', |
| 485 | 'build/make/target/product/security/testkey'), |
| 486 | }, keys_info) |