blob: c7d58073023ac992ef1c475b244e1c2782ae7b28 [file] [log] [blame]
Tao Bao1cd59f22019-03-15 15:13:01 -07001#
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
17import os
18import os.path
19
20import apex_utils
21import common
22import test_utils
23
24
25class 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 Bao82490d32019-04-09 00:12:30 -070042 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070043 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 Bao82490d32019-04-09 00:12:30 -070052 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070053 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 Bao82490d32019-04-09 00:12:30 -070059 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070060 def test_SignApexPayload_withSignerHelper(self):
61 payload_file = self._GetTestPayload()
Tao Bao30e31142019-04-09 00:12:30 -070062 signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh')
63 os.chmod(signing_helper, 0o700)
Tao Bao1cd59f22019-03-15 15:13:01 -070064 payload_signer_args = '--signing_helper_with_files {}'.format(
Tao Bao30e31142019-04-09 00:12:30 -070065 signing_helper)
Tao Bao1cd59f22019-03-15 15:13:01 -070066 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 Bao82490d32019-04-09 00:12:30 -070073 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070074 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 Bao82490d32019-04-09 00:12:30 -070084 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070085 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'))