blob: e9c26f0d9ea698450636504f4ff3dc4dca3a5b6f [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(
Tao Bao1ac886e2019-06-26 11:58:22 -070046 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
47 self.SALT)
48 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
Tao Bao1cd59f22019-03-15 15:13:01 -070049 self.assertEqual('SHA256_RSA2048', payload_info['Algorithm'])
50 self.assertEqual(self.SALT, payload_info['Salt'])
51 self.assertEqual('testkey', payload_info['apex.key'])
52
Tao Bao82490d32019-04-09 00:12:30 -070053 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070054 def test_SignApexPayload(self):
55 payload_file = self._GetTestPayload()
56 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070057 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
58 self.SALT)
59 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key)
Tao Bao1cd59f22019-03-15 15:13:01 -070060
Tao Bao82490d32019-04-09 00:12:30 -070061 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070062 def test_SignApexPayload_withSignerHelper(self):
63 payload_file = self._GetTestPayload()
Tao Bao30e31142019-04-09 00:12:30 -070064 signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh')
65 os.chmod(signing_helper, 0o700)
Tao Bao1cd59f22019-03-15 15:13:01 -070066 payload_signer_args = '--signing_helper_with_files {}'.format(
Tao Bao30e31142019-04-09 00:12:30 -070067 signing_helper)
Tao Bao1cd59f22019-03-15 15:13:01 -070068 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070069 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -070070 payload_file,
71 self.payload_key,
72 'testkey', 'SHA256_RSA2048', self.SALT,
73 payload_signer_args)
Tao Bao1ac886e2019-06-26 11:58:22 -070074 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key)
Tao Bao1cd59f22019-03-15 15:13:01 -070075
Tao Bao82490d32019-04-09 00:12:30 -070076 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070077 def test_SignApexPayload_invalidKey(self):
78 self.assertRaises(
79 apex_utils.ApexSigningError,
80 apex_utils.SignApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -070081 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -070082 self._GetTestPayload(),
83 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
84 'testkey',
85 'SHA256_RSA2048',
86 self.SALT)
87
Tao Bao82490d32019-04-09 00:12:30 -070088 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070089 def test_VerifyApexPayload_wrongKey(self):
90 payload_file = self._GetTestPayload()
91 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070092 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
93 self.SALT)
94 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key)
Tao Bao1cd59f22019-03-15 15:13:01 -070095 self.assertRaises(
96 apex_utils.ApexSigningError,
97 apex_utils.VerifyApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -070098 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -070099 payload_file,
100 os.path.join(self.testdata_dir, 'testkey_with_passwd.key'))