Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2019 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 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 17 | import re |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 18 | import os |
| 19 | import os.path |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 20 | import shutil |
Tianjie Xu | 83bd55c | 2020-01-29 11:37:43 -0800 | [diff] [blame] | 21 | import zipfile |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 22 | |
| 23 | import apex_utils |
| 24 | import common |
| 25 | import test_utils |
| 26 | |
| 27 | |
| 28 | class ApexUtilsTest(test_utils.ReleaseToolsTestCase): |
| 29 | |
| 30 | # echo "foo" | sha256sum |
| 31 | SALT = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c' |
| 32 | |
| 33 | def setUp(self): |
| 34 | self.testdata_dir = test_utils.get_testdata_dir() |
| 35 | # The default payload signing key. |
| 36 | self.payload_key = os.path.join(self.testdata_dir, 'testkey.key') |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 37 | self.apex_with_apk = os.path.join(self.testdata_dir, 'has_apk.apex') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 38 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 39 | common.OPTIONS.search_path = test_utils.get_search_path() |
| 40 | |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 41 | @staticmethod |
| 42 | def _GetTestPayload(): |
| 43 | payload_file = common.MakeTempFile(prefix='apex-', suffix='.img') |
| 44 | with open(payload_file, 'wb') as payload_fp: |
| 45 | payload_fp.write(os.urandom(8192)) |
| 46 | return payload_file |
| 47 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 48 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 49 | def test_ParseApexPayloadInfo(self): |
| 50 | payload_file = self._GetTestPayload() |
| 51 | apex_utils.SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 52 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 53 | self.SALT, 'sha256', no_hashtree=True) |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 54 | payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 55 | self.assertEqual('SHA256_RSA2048', payload_info['Algorithm']) |
| 56 | self.assertEqual(self.SALT, payload_info['Salt']) |
| 57 | self.assertEqual('testkey', payload_info['apex.key']) |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 58 | self.assertEqual('sha256', payload_info['Hash Algorithm']) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 59 | self.assertEqual('0 bytes', payload_info['Tree Size']) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 60 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 61 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 62 | def test_SignApexPayload(self): |
| 63 | payload_file = self._GetTestPayload() |
| 64 | apex_utils.SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 65 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 66 | self.SALT, 'sha256', no_hashtree=True) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 67 | apex_utils.VerifyApexPayload( |
| 68 | 'avbtool', payload_file, self.payload_key, True) |
| 69 | |
| 70 | @test_utils.SkipIfExternalToolsUnavailable() |
| 71 | def test_SignApexPayload_withHashtree(self): |
| 72 | payload_file = self._GetTestPayload() |
| 73 | apex_utils.SignApexPayload( |
| 74 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 75 | self.SALT, 'sha256', no_hashtree=False) |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 76 | apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 77 | payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file) |
| 78 | self.assertEqual('4096 bytes', payload_info['Tree Size']) |
| 79 | |
| 80 | @test_utils.SkipIfExternalToolsUnavailable() |
| 81 | def test_SignApexPayload_noHashtree(self): |
| 82 | payload_file = self._GetTestPayload() |
| 83 | apex_utils.SignApexPayload( |
| 84 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 85 | self.SALT, 'sha256', no_hashtree=True) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 86 | apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key, |
| 87 | no_hashtree=True) |
| 88 | payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file) |
| 89 | self.assertEqual('0 bytes', payload_info['Tree Size']) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 90 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 91 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 92 | def test_SignApexPayload_withSignerHelper(self): |
| 93 | payload_file = self._GetTestPayload() |
Tao Bao | 30e3114 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 94 | signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh') |
| 95 | os.chmod(signing_helper, 0o700) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 96 | payload_signer_args = '--signing_helper_with_files {}'.format( |
Tao Bao | 30e3114 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 97 | signing_helper) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 98 | apex_utils.SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 99 | 'avbtool', |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 100 | payload_file, |
| 101 | self.payload_key, |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 102 | 'testkey', 'SHA256_RSA2048', self.SALT, 'sha256', |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 103 | True, |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 104 | payload_signer_args) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 105 | apex_utils.VerifyApexPayload( |
| 106 | 'avbtool', payload_file, self.payload_key, True) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 107 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 108 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 109 | def test_SignApexPayload_invalidKey(self): |
| 110 | self.assertRaises( |
| 111 | apex_utils.ApexSigningError, |
| 112 | apex_utils.SignApexPayload, |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 113 | 'avbtool', |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 114 | self._GetTestPayload(), |
| 115 | os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 116 | 'testkey', |
| 117 | 'SHA256_RSA2048', |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 118 | self.SALT, |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 119 | 'sha256', |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 120 | no_hashtree=True) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 121 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 122 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 123 | def test_VerifyApexPayload_wrongKey(self): |
| 124 | payload_file = self._GetTestPayload() |
| 125 | apex_utils.SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 126 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 127 | self.SALT, 'sha256', True) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 128 | apex_utils.VerifyApexPayload( |
| 129 | 'avbtool', payload_file, self.payload_key, True) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 130 | self.assertRaises( |
| 131 | apex_utils.ApexSigningError, |
| 132 | apex_utils.VerifyApexPayload, |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 133 | 'avbtool', |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 134 | payload_file, |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 135 | os.path.join(self.testdata_dir, 'testkey_with_passwd.key'), |
| 136 | no_hashtree=True) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 137 | |
| 138 | @test_utils.SkipIfExternalToolsUnavailable() |
| 139 | def test_ApexApkSigner_noApkPresent(self): |
| 140 | apex_path = os.path.join(self.testdata_dir, 'foo.apex') |
| 141 | signer = apex_utils.ApexApkSigner(apex_path, None, None) |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 142 | processed_apex = signer.ProcessApexFile({}, self.payload_key) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 143 | self.assertEqual(apex_path, processed_apex) |
| 144 | |
| 145 | @test_utils.SkipIfExternalToolsUnavailable() |
| 146 | def test_ApexApkSigner_apkKeyNotPresent(self): |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 147 | apex_path = common.MakeTempFile(suffix='.apex') |
| 148 | shutil.copy(self.apex_with_apk, apex_path) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 149 | signer = apex_utils.ApexApkSigner(apex_path, None, None) |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 150 | self.assertRaises(apex_utils.ApexSigningError, signer.ProcessApexFile, |
| 151 | {}, self.payload_key) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 152 | |
| 153 | @test_utils.SkipIfExternalToolsUnavailable() |
| 154 | def test_ApexApkSigner_signApk(self): |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 155 | apex_path = common.MakeTempFile(suffix='.apex') |
| 156 | shutil.copy(self.apex_with_apk, apex_path) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 157 | signer = apex_utils.ApexApkSigner(apex_path, None, None) |
| 158 | apk_keys = {'wifi-service-resources.apk': os.path.join( |
| 159 | self.testdata_dir, 'testkey')} |
| 160 | |
| 161 | self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key') |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 162 | apex_file = signer.ProcessApexFile(apk_keys, self.payload_key) |
Kelvin Zhang | f5f3eaf | 2020-08-28 17:39:11 -0400 | [diff] [blame] | 163 | package_name_extract_cmd = ['aapt2', 'dump', 'badging', apex_file] |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 164 | output = common.RunAndCheckOutput(package_name_extract_cmd) |
| 165 | for line in output.splitlines(): |
| 166 | # Sample output from aapt: "package: name='com.google.android.wifi' |
| 167 | # versionCode='1' versionName='' platformBuildVersionName='R' |
| 168 | # compileSdkVersion='29' compileSdkVersionCodename='R'" |
| 169 | match = re.search(r"^package:.* name='([\w|\.]+)'", line, re.IGNORECASE) |
| 170 | if match: |
| 171 | package_name = match.group(1) |
| 172 | self.assertEquals('com.google.android.wifi', package_name) |
Tianjie Xu | 83bd55c | 2020-01-29 11:37:43 -0800 | [diff] [blame] | 173 | |
| 174 | @test_utils.SkipIfExternalToolsUnavailable() |
| 175 | def test_ApexApkSigner_noAssetDir(self): |
Tianjie Xu | 83bd55c | 2020-01-29 11:37:43 -0800 | [diff] [blame] | 176 | no_asset = common.MakeTempFile(suffix='.apex') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 177 | with zipfile.ZipFile(no_asset, 'w', allowZip64=True) as output_zip: |
| 178 | with zipfile.ZipFile(self.apex_with_apk, 'r', allowZip64=True) as input_zip: |
Tianjie Xu | 83bd55c | 2020-01-29 11:37:43 -0800 | [diff] [blame] | 179 | name_list = input_zip.namelist() |
| 180 | for name in name_list: |
| 181 | if not name.startswith('assets'): |
| 182 | output_zip.writestr(name, input_zip.read(name)) |
| 183 | |
| 184 | signer = apex_utils.ApexApkSigner(no_asset, None, None) |
| 185 | apk_keys = {'wifi-service-resources.apk': os.path.join( |
| 186 | self.testdata_dir, 'testkey')} |
| 187 | |
| 188 | self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key') |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 189 | signer.ProcessApexFile(apk_keys, self.payload_key) |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 190 | |
| 191 | @test_utils.SkipIfExternalToolsUnavailable() |
| 192 | def test_ApexApkSigner_invokesCustomSignTool(self): |
| 193 | apex_path = common.MakeTempFile(suffix='.apex') |
| 194 | shutil.copy(self.apex_with_apk, apex_path) |
| 195 | apk_keys = {'wifi-service-resources.apk': os.path.join( |
| 196 | self.testdata_dir, 'testkey')} |
| 197 | self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key') |
| 198 | |
| 199 | # pass `false` as a sign_tool to see the invocation error |
| 200 | with self.assertRaises(common.ExternalError) as cm: |
Kelvin Zhang | 0d0ca5d | 2021-12-21 12:31:55 -0800 | [diff] [blame] | 201 | signer = apex_utils.ApexApkSigner( |
| 202 | apex_path, None, None, sign_tool='false') |
| 203 | signer.ProcessApexFile(apk_keys, self.payload_key) |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 204 | |
| 205 | the_exception = cm.exception |
Kelvin Zhang | 0d0ca5d | 2021-12-21 12:31:55 -0800 | [diff] [blame] | 206 | self.assertIn('Failed to run command \'[\'false\'', str(the_exception)) |