blob: 71bd2594056ee6827a06e996ed744cb87d7e562e [file] [log] [blame]
Tao Baoa7054ee2017-12-08 14:42:16 -08001#
2# Copyright (C) 2017 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
17from __future__ import print_function
18
Tao Bao66472632017-12-04 17:16:36 -080019import base64
Tao Baode1d4792018-02-20 10:05:46 -080020import os.path
Tao Baoa7054ee2017-12-08 14:42:16 -080021import unittest
Tao Baoe838d142017-12-23 23:44:48 -080022import zipfile
Tao Baoa7054ee2017-12-08 14:42:16 -080023
Tao Baoe838d142017-12-23 23:44:48 -080024import common
Tao Baode1d4792018-02-20 10:05:46 -080025import test_utils
Tao Bao66472632017-12-04 17:16:36 -080026from sign_target_files_apks import (
Tao Bao11f955c2018-06-19 12:19:35 -070027 CheckAllApksSigned, EditTags, GetApkFileInfo, ReplaceCerts,
28 ReplaceVerityKeyId, RewriteProps)
Tao Baoa7054ee2017-12-08 14:42:16 -080029
30
31class SignTargetFilesApksTest(unittest.TestCase):
32
Tao Bao66472632017-12-04 17:16:36 -080033 MAC_PERMISSIONS_XML = """<?xml version="1.0" encoding="iso-8859-1"?>
34<policy>
35 <signer signature="{}"><seinfo value="platform"/></signer>
36 <signer signature="{}"><seinfo value="media"/></signer>
37</policy>"""
38
Tao Baoe838d142017-12-23 23:44:48 -080039 def setUp(self):
Tao Baode1d4792018-02-20 10:05:46 -080040 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoe838d142017-12-23 23:44:48 -080041
42 def tearDown(self):
43 common.Cleanup()
44
Tao Baoa7054ee2017-12-08 14:42:16 -080045 def test_EditTags(self):
46 self.assertEqual(EditTags('dev-keys'), ('release-keys'))
47 self.assertEqual(EditTags('test-keys'), ('release-keys'))
48
49 # Multiple tags.
50 self.assertEqual(EditTags('abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
51
52 # Tags are sorted.
53 self.assertEqual(EditTags('xyz,abc,dev-keys,xyz'), ('abc,release-keys,xyz'))
54
55 def test_RewriteProps(self):
56 props = (
57 ('', '\n'),
58 ('ro.build.fingerprint=foo/bar/dev-keys',
59 'ro.build.fingerprint=foo/bar/release-keys\n'),
60 ('ro.build.thumbprint=foo/bar/dev-keys',
61 'ro.build.thumbprint=foo/bar/release-keys\n'),
62 ('ro.vendor.build.fingerprint=foo/bar/dev-keys',
63 'ro.vendor.build.fingerprint=foo/bar/release-keys\n'),
64 ('ro.vendor.build.thumbprint=foo/bar/dev-keys',
65 'ro.vendor.build.thumbprint=foo/bar/release-keys\n'),
66 ('# comment line 1', '# comment line 1\n'),
67 ('ro.bootimage.build.fingerprint=foo/bar/dev-keys',
68 'ro.bootimage.build.fingerprint=foo/bar/release-keys\n'),
69 ('ro.build.description='
70 'sailfish-user 8.0.0 OPR6.170623.012 4283428 dev-keys',
71 'ro.build.description='
72 'sailfish-user 8.0.0 OPR6.170623.012 4283428 release-keys\n'),
73 ('ro.build.tags=dev-keys', 'ro.build.tags=release-keys\n'),
74 ('# comment line 2', '# comment line 2\n'),
75 ('ro.build.display.id=OPR6.170623.012 dev-keys',
76 'ro.build.display.id=OPR6.170623.012\n'),
77 ('# comment line 3', '# comment line 3\n'),
78 )
79
80 # Assert the case for each individual line.
Tao Baoe838d142017-12-23 23:44:48 -080081 for prop, output in props:
82 self.assertEqual(RewriteProps(prop), output)
Tao Baoa7054ee2017-12-08 14:42:16 -080083
84 # Concatenate all the input lines.
85 self.assertEqual(RewriteProps('\n'.join([prop[0] for prop in props])),
86 ''.join([prop[1] for prop in props]))
Tao Baoe838d142017-12-23 23:44:48 -080087
88 def test_ReplaceVerityKeyId(self):
89 BOOT_CMDLINE1 = (
90 "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 "
91 "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 "
92 "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 "
93 "buildvariant=userdebug "
94 "veritykeyid=id:7e4333f9bba00adfe0ede979e28ed1920492b40f\n")
95
96 BOOT_CMDLINE2 = (
97 "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 "
98 "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 "
99 "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 "
100 "buildvariant=userdebug "
Tao Baode1d4792018-02-20 10:05:46 -0800101 "veritykeyid=id:d24f2590e9abab5cff5f59da4c4f0366e3f43e94\n")
Tao Baoe838d142017-12-23 23:44:48 -0800102
Tao Baode1d4792018-02-20 10:05:46 -0800103 input_file = common.MakeTempFile(suffix='.zip')
104 with zipfile.ZipFile(input_file, 'w') as input_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800105 input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE1)
106
107 # Test with the first certificate.
Tao Baode1d4792018-02-20 10:05:46 -0800108 cert_file = os.path.join(self.testdata_dir, 'verity.x509.pem')
Tao Baoe838d142017-12-23 23:44:48 -0800109
Tao Baode1d4792018-02-20 10:05:46 -0800110 output_file = common.MakeTempFile(suffix='.zip')
111 with zipfile.ZipFile(input_file, 'r') as input_zip, \
112 zipfile.ZipFile(output_file, 'w') as output_zip:
113 ReplaceVerityKeyId(input_zip, output_zip, cert_file)
Tao Baoe838d142017-12-23 23:44:48 -0800114
Tao Baode1d4792018-02-20 10:05:46 -0800115 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800116 self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline'))
117
118 # Test with the second certificate.
Tao Baode1d4792018-02-20 10:05:46 -0800119 cert_file = os.path.join(self.testdata_dir, 'testkey.x509.pem')
Tao Baoe838d142017-12-23 23:44:48 -0800120
Tao Baode1d4792018-02-20 10:05:46 -0800121 with zipfile.ZipFile(input_file, 'r') as input_zip, \
122 zipfile.ZipFile(output_file, 'w') as output_zip:
123 ReplaceVerityKeyId(input_zip, output_zip, cert_file)
Tao Baoe838d142017-12-23 23:44:48 -0800124
Tao Baode1d4792018-02-20 10:05:46 -0800125 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800126 self.assertEqual(BOOT_CMDLINE2, output_zip.read('BOOT/cmdline'))
127
128 def test_ReplaceVerityKeyId_no_veritykeyid(self):
129 BOOT_CMDLINE = (
130 "console=ttyHSL0,115200,n8 androidboot.hardware=bullhead boot_cpus=0-5 "
131 "lpm_levels.sleep_disabled=1 msm_poweroff.download_mode=0 "
132 "loop.max_part=7\n")
133
Tao Baode1d4792018-02-20 10:05:46 -0800134 input_file = common.MakeTempFile(suffix='.zip')
135 with zipfile.ZipFile(input_file, 'w') as input_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800136 input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE)
137
Tao Baode1d4792018-02-20 10:05:46 -0800138 output_file = common.MakeTempFile(suffix='.zip')
139 with zipfile.ZipFile(input_file, 'r') as input_zip, \
140 zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800141 ReplaceVerityKeyId(input_zip, output_zip, None)
142
Tao Baode1d4792018-02-20 10:05:46 -0800143 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800144 self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline'))
Tao Bao66472632017-12-04 17:16:36 -0800145
146 def test_ReplaceCerts(self):
147 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
148 with open(cert1_path) as cert1_fp:
149 cert1 = cert1_fp.read()
150 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
151 with open(cert2_path) as cert2_fp:
152 cert2 = cert2_fp.read()
153 cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem')
154 with open(cert3_path) as cert3_fp:
155 cert3 = cert3_fp.read()
156
157 # Replace cert1 with cert3.
158 input_xml = self.MAC_PERMISSIONS_XML.format(
159 base64.b16encode(common.ParseCertificate(cert1)).lower(),
160 base64.b16encode(common.ParseCertificate(cert2)).lower())
161
162 output_xml = self.MAC_PERMISSIONS_XML.format(
163 base64.b16encode(common.ParseCertificate(cert3)).lower(),
164 base64.b16encode(common.ParseCertificate(cert2)).lower())
165
166 common.OPTIONS.key_map = {
167 cert1_path[:-9] : cert3_path[:-9],
168 }
169
170 self.assertEqual(output_xml, ReplaceCerts(input_xml))
171
172 def test_ReplaceCerts_duplicateEntries(self):
173 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
174 with open(cert1_path) as cert1_fp:
175 cert1 = cert1_fp.read()
176 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
177 with open(cert2_path) as cert2_fp:
178 cert2 = cert2_fp.read()
179
180 # Replace cert1 with cert2, which leads to duplicate entries.
181 input_xml = self.MAC_PERMISSIONS_XML.format(
182 base64.b16encode(common.ParseCertificate(cert1)).lower(),
183 base64.b16encode(common.ParseCertificate(cert2)).lower())
184
185 common.OPTIONS.key_map = {
186 cert1_path[:-9] : cert2_path[:-9],
187 }
188 self.assertRaises(AssertionError, ReplaceCerts, input_xml)
189
190 def test_ReplaceCerts_skipNonExistentCerts(self):
191 cert1_path = os.path.join(self.testdata_dir, 'platform.x509.pem')
192 with open(cert1_path) as cert1_fp:
193 cert1 = cert1_fp.read()
194 cert2_path = os.path.join(self.testdata_dir, 'media.x509.pem')
195 with open(cert2_path) as cert2_fp:
196 cert2 = cert2_fp.read()
197 cert3_path = os.path.join(self.testdata_dir, 'testkey.x509.pem')
198 with open(cert3_path) as cert3_fp:
199 cert3 = cert3_fp.read()
200
201 input_xml = self.MAC_PERMISSIONS_XML.format(
202 base64.b16encode(common.ParseCertificate(cert1)).lower(),
203 base64.b16encode(common.ParseCertificate(cert2)).lower())
204
205 output_xml = self.MAC_PERMISSIONS_XML.format(
206 base64.b16encode(common.ParseCertificate(cert3)).lower(),
207 base64.b16encode(common.ParseCertificate(cert2)).lower())
208
209 common.OPTIONS.key_map = {
210 cert1_path[:-9] : cert3_path[:-9],
211 'non-existent' : cert3_path[:-9],
212 cert2_path[:-9] : 'non-existent',
213 }
214 self.assertEqual(output_xml, ReplaceCerts(input_xml))
Tao Bao11f955c2018-06-19 12:19:35 -0700215
216 def test_CheckAllApksSigned(self):
217 input_file = common.MakeTempFile(suffix='.zip')
218 with zipfile.ZipFile(input_file, 'w') as input_zip:
219 input_zip.writestr('SYSTEM/app/App1.apk', "App1-content")
220 input_zip.writestr('SYSTEM/app/App2.apk.gz', "App2-content")
221
222 apk_key_map = {
223 'App1.apk' : 'key1',
224 'App2.apk' : 'key2',
225 'App3.apk' : 'key3',
226 }
227 with zipfile.ZipFile(input_file) as input_zip:
228 CheckAllApksSigned(input_zip, apk_key_map, None)
229 CheckAllApksSigned(input_zip, apk_key_map, '.gz')
230
231 # 'App2.apk.gz' won't be considered as an APK.
232 CheckAllApksSigned(input_zip, apk_key_map, None)
233 CheckAllApksSigned(input_zip, apk_key_map, '.xz')
234
235 del apk_key_map['App2.apk']
236 self.assertRaises(
237 AssertionError, CheckAllApksSigned, input_zip, apk_key_map, '.gz')
238
239 def test_GetApkFileInfo(self):
240 (is_apk, is_compressed) = GetApkFileInfo("PRODUCT/apps/Chats.apk", None)
241 self.assertTrue(is_apk)
242 self.assertFalse(is_compressed)
243
244 (is_apk, is_compressed) = GetApkFileInfo("PRODUCT/apps/Chats.dat", None)
245 self.assertFalse(is_apk)
246 self.assertFalse(is_compressed)
247
248 def test_GetApkFileInfo_withCompressedApks(self):
249 (is_apk, is_compressed) = GetApkFileInfo("PRODUCT/apps/Chats.apk.gz", ".gz")
250 self.assertTrue(is_apk)
251 self.assertTrue(is_compressed)
252
253 (is_apk, is_compressed) = GetApkFileInfo("PRODUCT/apps/Chats.apk.gz", ".xz")
254 self.assertFalse(is_apk)
255 self.assertFalse(is_compressed)
256
257 self.assertRaises(
258 AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "")
259
260 self.assertRaises(
261 AssertionError, GetApkFileInfo, "PRODUCT/apps/Chats.apk", "apk")