blob: 2f8ee4982364b37e70831dd4f58c7240c2913a6b [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
42 def test_ParseApexPayloadInfo(self):
43 payload_file = self._GetTestPayload()
44 apex_utils.SignApexPayload(
45 payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', self.SALT)
46 payload_info = apex_utils.ParseApexPayloadInfo(payload_file)
47 self.assertEqual('SHA256_RSA2048', payload_info['Algorithm'])
48 self.assertEqual(self.SALT, payload_info['Salt'])
49 self.assertEqual('testkey', payload_info['apex.key'])
50
51 def test_SignApexPayload(self):
52 payload_file = self._GetTestPayload()
53 apex_utils.SignApexPayload(
54 payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', self.SALT)
55 apex_utils.VerifyApexPayload(payload_file, self.payload_key)
56
57 def test_SignApexPayload_withSignerHelper(self):
58 payload_file = self._GetTestPayload()
59 payload_signer_args = '--signing_helper_with_files {}'.format(
60 os.path.join(self.testdata_dir, 'signing_helper.sh'))
61 apex_utils.SignApexPayload(
62 payload_file,
63 self.payload_key,
64 'testkey', 'SHA256_RSA2048', self.SALT,
65 payload_signer_args)
66 apex_utils.VerifyApexPayload(payload_file, self.payload_key)
67
68 def test_SignApexPayload_invalidKey(self):
69 self.assertRaises(
70 apex_utils.ApexSigningError,
71 apex_utils.SignApexPayload,
72 self._GetTestPayload(),
73 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
74 'testkey',
75 'SHA256_RSA2048',
76 self.SALT)
77
78 def test_VerifyApexPayload_wrongKey(self):
79 payload_file = self._GetTestPayload()
80 apex_utils.SignApexPayload(
81 payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048', self.SALT)
82 apex_utils.VerifyApexPayload(payload_file, self.payload_key)
83 self.assertRaises(
84 apex_utils.ApexSigningError,
85 apex_utils.VerifyApexPayload,
86 payload_file,
87 os.path.join(self.testdata_dir, 'testkey_with_passwd.key'))