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 | |
| 35 | @staticmethod |
| 36 | def _GetTestPayload(): |
| 37 | payload_file = common.MakeTempFile(prefix='apex-', suffix='.img') |
| 38 | with open(payload_file, 'wb') as payload_fp: |
| 39 | payload_fp.write(os.urandom(8192)) |
| 40 | return payload_file |
| 41 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 42 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 43 | def test_ParseApexPayloadInfo(self): |
| 44 | payload_file = self._GetTestPayload() |
| 45 | apex_utils.SignApexPayload( |
| 46 | payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', self.SALT) |
| 47 | payload_info = apex_utils.ParseApexPayloadInfo(payload_file) |
| 48 | self.assertEqual('SHA256_RSA2048', payload_info['Algorithm']) |
| 49 | self.assertEqual(self.SALT, payload_info['Salt']) |
| 50 | self.assertEqual('testkey', payload_info['apex.key']) |
| 51 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 52 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 53 | def test_SignApexPayload(self): |
| 54 | payload_file = self._GetTestPayload() |
| 55 | apex_utils.SignApexPayload( |
| 56 | payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', self.SALT) |
| 57 | apex_utils.VerifyApexPayload(payload_file, self.payload_key) |
| 58 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 59 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 60 | def test_SignApexPayload_withSignerHelper(self): |
| 61 | payload_file = self._GetTestPayload() |
Tao Bao | 30e3114 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 62 | signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh') |
| 63 | os.chmod(signing_helper, 0o700) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 64 | payload_signer_args = '--signing_helper_with_files {}'.format( |
Tao Bao | 30e3114 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 65 | signing_helper) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 66 | apex_utils.SignApexPayload( |
| 67 | payload_file, |
| 68 | self.payload_key, |
| 69 | 'testkey', 'SHA256_RSA2048', self.SALT, |
| 70 | payload_signer_args) |
| 71 | apex_utils.VerifyApexPayload(payload_file, self.payload_key) |
| 72 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 73 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 74 | def test_SignApexPayload_invalidKey(self): |
| 75 | self.assertRaises( |
| 76 | apex_utils.ApexSigningError, |
| 77 | apex_utils.SignApexPayload, |
| 78 | self._GetTestPayload(), |
| 79 | os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 80 | 'testkey', |
| 81 | 'SHA256_RSA2048', |
| 82 | self.SALT) |
| 83 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 84 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 85 | def test_VerifyApexPayload_wrongKey(self): |
| 86 | payload_file = self._GetTestPayload() |
| 87 | apex_utils.SignApexPayload( |
| 88 | payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', self.SALT) |
| 89 | apex_utils.VerifyApexPayload(payload_file, self.payload_key) |
| 90 | self.assertRaises( |
| 91 | apex_utils.ApexSigningError, |
| 92 | apex_utils.VerifyApexPayload, |
| 93 | payload_file, |
| 94 | os.path.join(self.testdata_dir, 'testkey_with_passwd.key')) |