blob: df61ac089eef0ed042a073bd96699170877faa8c [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
Tianjie Xu88a759d2020-01-23 10:47:54 -080035 common.OPTIONS.search_path = test_utils.get_search_path()
36
Tao Bao1cd59f22019-03-15 15:13:01 -070037 @staticmethod
38 def _GetTestPayload():
39 payload_file = common.MakeTempFile(prefix='apex-', suffix='.img')
40 with open(payload_file, 'wb') as payload_fp:
41 payload_fp.write(os.urandom(8192))
42 return payload_file
43
Tao Bao82490d32019-04-09 00:12:30 -070044 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070045 def test_ParseApexPayloadInfo(self):
46 payload_file = self._GetTestPayload()
47 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070048 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -070049 self.SALT, no_hashtree=True)
Tao Bao1ac886e2019-06-26 11:58:22 -070050 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
Tao Bao1cd59f22019-03-15 15:13:01 -070051 self.assertEqual('SHA256_RSA2048', payload_info['Algorithm'])
52 self.assertEqual(self.SALT, payload_info['Salt'])
53 self.assertEqual('testkey', payload_info['apex.key'])
Tao Bao448004a2019-09-19 07:55:02 -070054 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070055
Tao Bao82490d32019-04-09 00:12:30 -070056 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070057 def test_SignApexPayload(self):
58 payload_file = self._GetTestPayload()
59 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070060 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -070061 self.SALT, no_hashtree=True)
62 apex_utils.VerifyApexPayload(
63 'avbtool', payload_file, self.payload_key, True)
64
65 @test_utils.SkipIfExternalToolsUnavailable()
66 def test_SignApexPayload_withHashtree(self):
67 payload_file = self._GetTestPayload()
68 apex_utils.SignApexPayload(
69 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
70 self.SALT, no_hashtree=False)
Tao Bao1ac886e2019-06-26 11:58:22 -070071 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key)
Tao Bao448004a2019-09-19 07:55:02 -070072 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
73 self.assertEqual('4096 bytes', payload_info['Tree Size'])
74
75 @test_utils.SkipIfExternalToolsUnavailable()
76 def test_SignApexPayload_noHashtree(self):
77 payload_file = self._GetTestPayload()
78 apex_utils.SignApexPayload(
79 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
80 self.SALT, no_hashtree=True)
81 apex_utils.VerifyApexPayload('avbtool', payload_file, self.payload_key,
82 no_hashtree=True)
83 payload_info = apex_utils.ParseApexPayloadInfo('avbtool', payload_file)
84 self.assertEqual('0 bytes', payload_info['Tree Size'])
Tao Bao1cd59f22019-03-15 15:13:01 -070085
Tao Bao82490d32019-04-09 00:12:30 -070086 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -070087 def test_SignApexPayload_withSignerHelper(self):
88 payload_file = self._GetTestPayload()
Tao Bao30e31142019-04-09 00:12:30 -070089 signing_helper = os.path.join(self.testdata_dir, 'signing_helper.sh')
90 os.chmod(signing_helper, 0o700)
Tao Bao1cd59f22019-03-15 15:13:01 -070091 payload_signer_args = '--signing_helper_with_files {}'.format(
Tao Bao30e31142019-04-09 00:12:30 -070092 signing_helper)
Tao Bao1cd59f22019-03-15 15:13:01 -070093 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -070094 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -070095 payload_file,
96 self.payload_key,
97 'testkey', 'SHA256_RSA2048', self.SALT,
Tao Bao448004a2019-09-19 07:55:02 -070098 True,
Tao Bao1cd59f22019-03-15 15:13:01 -070099 payload_signer_args)
Tao Bao448004a2019-09-19 07:55:02 -0700100 apex_utils.VerifyApexPayload(
101 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700102
Tao Bao82490d32019-04-09 00:12:30 -0700103 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700104 def test_SignApexPayload_invalidKey(self):
105 self.assertRaises(
106 apex_utils.ApexSigningError,
107 apex_utils.SignApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700108 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700109 self._GetTestPayload(),
110 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
111 'testkey',
112 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -0700113 self.SALT,
114 no_hashtree=True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700115
Tao Bao82490d32019-04-09 00:12:30 -0700116 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao1cd59f22019-03-15 15:13:01 -0700117 def test_VerifyApexPayload_wrongKey(self):
118 payload_file = self._GetTestPayload()
119 apex_utils.SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -0700120 'avbtool', payload_file, self.payload_key, 'testkey', 'SHA256_RSA2048',
Tao Bao448004a2019-09-19 07:55:02 -0700121 self.SALT, True)
122 apex_utils.VerifyApexPayload(
123 'avbtool', payload_file, self.payload_key, True)
Tao Bao1cd59f22019-03-15 15:13:01 -0700124 self.assertRaises(
125 apex_utils.ApexSigningError,
126 apex_utils.VerifyApexPayload,
Tao Bao1ac886e2019-06-26 11:58:22 -0700127 'avbtool',
Tao Bao1cd59f22019-03-15 15:13:01 -0700128 payload_file,
Tao Bao448004a2019-09-19 07:55:02 -0700129 os.path.join(self.testdata_dir, 'testkey_with_passwd.key'),
130 no_hashtree=True)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800131
132 @test_utils.SkipIfExternalToolsUnavailable()
133 def test_ApexApkSigner_noApkPresent(self):
134 apex_path = os.path.join(self.testdata_dir, 'foo.apex')
135 signer = apex_utils.ApexApkSigner(apex_path, None, None)
136 processed_apex = signer.ProcessApexFile({}, self.payload_key,
137 None)
138 self.assertEqual(apex_path, processed_apex)
139
140 @test_utils.SkipIfExternalToolsUnavailable()
141 def test_ApexApkSigner_apkKeyNotPresent(self):
142 apex_path = os.path.join(self.testdata_dir, 'has_apk.apex')
143 signer = apex_utils.ApexApkSigner(apex_path, None, None)
144 self.assertRaises(apex_utils.ApexSigningError, signer.ProcessApexFile, {},
145 self.payload_key, None)
146
147 @test_utils.SkipIfExternalToolsUnavailable()
148 def test_ApexApkSigner_signApk(self):
149 apex_path = os.path.join(self.testdata_dir, 'has_apk.apex')
150 signer = apex_utils.ApexApkSigner(apex_path, None, None)
151 apk_keys = {'wifi-service-resources.apk': os.path.join(
152 self.testdata_dir, 'testkey')}
153
154 self.payload_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
155 payload_pubkey = common.ExtractAvbPublicKey('avbtool',
156 self.payload_key)
157 signer.ProcessApexFile(apk_keys, self.payload_key, payload_pubkey)