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 | |
| 17 | import os |
| 18 | import os.path |
| 19 | |
| 20 | import apex_utils |
| 21 | import common |
| 22 | import test_utils |
| 23 | |
| 24 | |
| 25 | class ApexUtilsTest(test_utils.ReleaseToolsTestCase): |
| 26 | |
| 27 | # echo "foo" | sha256sum |
| 28 | SALT = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c' |
| 29 | |
| 30 | def setUp(self): |
| 31 | self.testdata_dir = test_utils.get_testdata_dir() |
| 32 | # The default payload signing key. |
| 33 | self.payload_key = os.path.join(self.testdata_dir, 'testkey.key') |
| 34 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame^] | 35 | common.OPTIONS.search_path = test_utils.get_search_path() |
| 36 | |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 37 | @staticmethod |
| 38 | def _GetTestPayload(): |
| 39 | payload_file = common.MakeTempFile(prefix='apex-', suffix='.img') |
| 40 | with open(payload_file, 'wb') as payload_fp: |
| 41 | payload_fp.write(os.urandom(8192)) |
| 42 | return payload_file |
| 43 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 44 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 45 | def test_ParseApexPayloadInfo(self): |
| 46 | payload_file = self._GetTestPayload() |
| 47 | apex_utils.SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 48 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 49 | self.SALT, no_hashtree=True) |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 50 | payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 51 | self.assertEqual('SHA256_RSA2048', payload_info['Algorithm']) |
| 52 | self.assertEqual(self.SALT, payload_info['Salt']) |
| 53 | self.assertEqual('testkey', payload_info['apex.key']) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 54 | self.assertEqual('0 bytes', payload_info['Tree Size']) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 55 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 56 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 57 | def test_SignApexPayload(self): |
| 58 | payload_file = self._GetTestPayload() |
| 59 | apex_utils.SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 60 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 61 | self.SALT, no_hashtree=True) |
| 62 | apex_utils.VerifyApexPayload( |
| 63 | 'avbtool', payload_file, self.payload_key, True) |
| 64 | |
| 65 | @test_utils.SkipIfExternalToolsUnavailable() |
| 66 | def test_SignApexPayload_withHashtree(self): |
| 67 | payload_file = self._GetTestPayload() |
| 68 | apex_utils.SignApexPayload( |
| 69 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
| 70 | self.SALT, no_hashtree=False) |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 71 | apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 72 | payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file) |
| 73 | self.assertEqual('4096 bytes', payload_info['Tree Size']) |
| 74 | |
| 75 | @test_utils.SkipIfExternalToolsUnavailable() |
| 76 | def test_SignApexPayload_noHashtree(self): |
| 77 | payload_file = self._GetTestPayload() |
| 78 | apex_utils.SignApexPayload( |
| 79 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
| 80 | self.SALT, no_hashtree=True) |
| 81 | apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key, |
| 82 | no_hashtree=True) |
| 83 | payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file) |
| 84 | self.assertEqual('0 bytes', payload_info['Tree Size']) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 85 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 86 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 87 | def test_SignApexPayload_withSignerHelper(self): |
| 88 | payload_file = self._GetTestPayload() |
Tao Bao | 30e3114 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 89 | signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh') |
| 90 | os.chmod(signing_helper, 0o700) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 91 | payload_signer_args = '--signing_helper_with_files {}'.format( |
Tao Bao | 30e3114 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 92 | signing_helper) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 93 | apex_utils.SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 94 | 'avbtool', |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 95 | payload_file, |
| 96 | self.payload_key, |
| 97 | 'testkey', 'SHA256_RSA2048', self.SALT, |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 98 | True, |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 99 | payload_signer_args) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 100 | apex_utils.VerifyApexPayload( |
| 101 | 'avbtool', payload_file, self.payload_key, True) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 102 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 103 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 104 | def test_SignApexPayload_invalidKey(self): |
| 105 | self.assertRaises( |
| 106 | apex_utils.ApexSigningError, |
| 107 | apex_utils.SignApexPayload, |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 108 | 'avbtool', |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 109 | self._GetTestPayload(), |
| 110 | os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 111 | 'testkey', |
| 112 | 'SHA256_RSA2048', |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 113 | self.SALT, |
| 114 | no_hashtree=True) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 115 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 116 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 117 | def test_VerifyApexPayload_wrongKey(self): |
| 118 | payload_file = self._GetTestPayload() |
| 119 | apex_utils.SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 120 | 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 121 | self.SALT, True) |
| 122 | apex_utils.VerifyApexPayload( |
| 123 | 'avbtool', payload_file, self.payload_key, True) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 124 | self.assertRaises( |
| 125 | apex_utils.ApexSigningError, |
| 126 | apex_utils.VerifyApexPayload, |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 127 | 'avbtool', |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 128 | payload_file, |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 129 | os.path.join(self.testdata_dir, 'testkey_with_passwd.key'), |
| 130 | no_hashtree=True) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame^] | 131 | |
| 132 | @test_utils.SkipIfExternalToolsUnavailable() |
| 133 | def test_ApexApkSigner_noApkPresent(self): |
| 134 | apex_path = os.path.join(self.testdata_dir, 'foo.apex') |
| 135 | signer = apex_utils.ApexApkSigner(apex_path, None, None) |
| 136 | processed_apex = signer.ProcessApexFile({}, self.payload_key, |
| 137 | None) |
| 138 | self.assertEqual(apex_path, processed_apex) |
| 139 | |
| 140 | @test_utils.SkipIfExternalToolsUnavailable() |
| 141 | def test_ApexApkSigner_apkKeyNotPresent(self): |
| 142 | apex_path = os.path.join(self.testdata_dir, 'has_apk.apex') |
| 143 | signer = apex_utils.ApexApkSigner(apex_path, None, None) |
| 144 | self.assertRaises(apex_utils.ApexSigningError, signer.ProcessApexFile, {}, |
| 145 | self.payload_key, None) |
| 146 | |
| 147 | @test_utils.SkipIfExternalToolsUnavailable() |
| 148 | def test_ApexApkSigner_signApk(self): |
| 149 | apex_path = os.path.join(self.testdata_dir, 'has_apk.apex') |
| 150 | signer = apex_utils.ApexApkSigner(apex_path, None, None) |
| 151 | apk_keys = {'wifi-service-resources.apk': os.path.join( |
| 152 | self.testdata_dir, 'testkey')} |
| 153 | |
| 154 | self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key') |
| 155 | payload_pubkey = common.ExtractAvbPublicKey('avbtool', |
| 156 | self.payload_key) |
| 157 | signer.ProcessApexFile(apk_keys, self.payload_key, payload_pubkey) |