blob: 5d4cc7749488394388ca0f57c595352c75114911 [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',
Tao Bao448004a2019-09-19 07:55:02 -070047 self.SALT, no_hashtree=True)
Tao Bao1ac886e2019-06-26 11:58:22 -070048 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'])
Tao Bao448004a2019-09-19 07:55:02 -070052 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070053
Tao Bao82490d32019-04-09 00:12:30 -070054 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070055 def test_SignApexPayload(self):
56 payload_file = self._GetTestPayload()
57 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070058 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -070059 self.SALT, no_hashtree=True)
60 apex_utils.VerifyApexPayload(
61 'avbtool', payload_file, self.payload_key, True)
62
63 @test_utils.SkipIfExternalToolsUnavailable()
64 def test_SignApexPayload_withHashtree(self):
65 payload_file = self._GetTestPayload()
66 apex_utils.SignApexPayload(
67 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
68 self.SALT, no_hashtree=False)
Tao Bao1ac886e2019-06-26 11:58:22 -070069 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key)
Tao Bao448004a2019-09-19 07:55:02 -070070 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
71 self.assertEqual('4096 bytes', payload_info['Tree Size'])
72
73 @test_utils.SkipIfExternalToolsUnavailable()
74 def test_SignApexPayload_noHashtree(self):
75 payload_file = self._GetTestPayload()
76 apex_utils.SignApexPayload(
77 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
78 self.SALT, no_hashtree=True)
79 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key,
80 no_hashtree=True)
81 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
82 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070083
Tao Bao82490d32019-04-09 00:12:30 -070084 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070085 def test_SignApexPayload_withSignerHelper(self):
86 payload_file = self._GetTestPayload()
Tao Bao30e31142019-04-09 00:12:30 -070087 signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh')
88 os.chmod(signing_helper, 0o700)
Tao Bao1cd59f22019-03-15 15:13:01 -070089 payload_signer_args = '--signing_helper_with_files {}'.format(
Tao Bao30e31142019-04-09 00:12:30 -070090 signing_helper)
Tao Bao1cd59f22019-03-15 15:13:01 -070091 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070092 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -070093 payload_file,
94 self.payload_key,
95 'testkey', 'SHA256_RSA2048', self.SALT,
Tao Bao448004a2019-09-19 07:55:02 -070096 True,
Tao Bao1cd59f22019-03-15 15:13:01 -070097 payload_signer_args)
Tao Bao448004a2019-09-19 07:55:02 -070098 apex_utils.VerifyApexPayload(
99 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700100
Tao Bao82490d32019-04-09 00:12:30 -0700101 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700102 def test_SignApexPayload_invalidKey(self):
103 self.assertRaises(
104 apex_utils.ApexSigningError,
105 apex_utils.SignApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700106 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700107 self._GetTestPayload(),
108 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
109 'testkey',
110 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -0700111 self.SALT,
112 no_hashtree=True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700113
Tao Bao82490d32019-04-09 00:12:30 -0700114 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700115 def test_VerifyApexPayload_wrongKey(self):
116 payload_file = self._GetTestPayload()
117 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -0700118 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -0700119 self.SALT, True)
120 apex_utils.VerifyApexPayload(
121 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700122 self.assertRaises(
123 apex_utils.ApexSigningError,
124 apex_utils.VerifyApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700125 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700126 payload_file,
Tao Bao448004a2019-09-19 07:55:02 -0700127 os.path.join(self.testdata_dir, 'testkey_with_passwd.key'),
128 no_hashtree=True)