blob: bf94cc2e35bd27b227cea51b433507c612d2c29e [file] [log] [blame]
Tao Bao481bab82017-12-21 11:23:09 -08001#
2# Copyright (C) 2018 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 copy
Tao Baoc7b403a2018-01-30 18:19:04 -080018import os
Tao Baofabe0832018-01-17 15:52:28 -080019import os.path
Tao Baoc7b403a2018-01-30 18:19:04 -080020import zipfile
Tao Bao481bab82017-12-21 11:23:09 -080021
22import common
Tao Bao04e1f012018-02-04 12:13:35 -080023import test_utils
Tao Bao481bab82017-12-21 11:23:09 -080024from ota_from_target_files import (
Tao Bao3bf8c652018-03-16 12:59:42 -070025 _LoadOemDicts, AbOtaPropertyFiles, BuildInfo, FinalizeMetadata,
26 GetPackageMetadata, GetTargetFilesZipForSecondaryImages,
Tao Baoc0746f42018-02-21 13:17:22 -080027 GetTargetFilesZipWithoutPostinstallConfig, NonAbOtaPropertyFiles,
Tao Bao69203522018-03-08 16:09:01 -080028 Payload, PayloadSigner, POSTINSTALL_CONFIG, PropertyFiles,
29 StreamingPropertyFiles, WriteFingerprintAssertion)
Tao Baofabe0832018-01-17 15:52:28 -080030
31
Tao Baof7140c02018-01-30 17:09:24 -080032def construct_target_files(secondary=False):
33 """Returns a target-files.zip file for generating OTA packages."""
34 target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip')
35 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
36 # META/update_engine_config.txt
37 target_files_zip.writestr(
38 'META/update_engine_config.txt',
39 "PAYLOAD_MAJOR_VERSION=2\nPAYLOAD_MINOR_VERSION=4\n")
40
Tao Bao15a146a2018-02-21 16:06:59 -080041 # META/postinstall_config.txt
42 target_files_zip.writestr(
43 POSTINSTALL_CONFIG,
44 '\n'.join([
45 "RUN_POSTINSTALL_system=true",
46 "POSTINSTALL_PATH_system=system/bin/otapreopt_script",
47 "FILESYSTEM_TYPE_system=ext4",
48 "POSTINSTALL_OPTIONAL_system=true",
49 ]))
50
Tao Bao5277d102018-04-17 23:47:21 -070051 ab_partitions = [
52 ('IMAGES', 'boot'),
53 ('IMAGES', 'system'),
54 ('IMAGES', 'vendor'),
55 ('RADIO', 'bootloader'),
56 ('RADIO', 'modem'),
57 ]
Tao Baof7140c02018-01-30 17:09:24 -080058 # META/ab_partitions.txt
Tao Baof7140c02018-01-30 17:09:24 -080059 target_files_zip.writestr(
60 'META/ab_partitions.txt',
Tao Bao5277d102018-04-17 23:47:21 -070061 '\n'.join([partition[1] for partition in ab_partitions]))
Tao Baof7140c02018-01-30 17:09:24 -080062
63 # Create dummy images for each of them.
Tao Bao5277d102018-04-17 23:47:21 -070064 for path, partition in ab_partitions:
65 target_files_zip.writestr(
66 '{}/{}.img'.format(path, partition),
67 os.urandom(len(partition)))
Tao Baof7140c02018-01-30 17:09:24 -080068
Tao Bao5277d102018-04-17 23:47:21 -070069 # system_other shouldn't appear in META/ab_partitions.txt.
Tao Baof7140c02018-01-30 17:09:24 -080070 if secondary:
71 target_files_zip.writestr('IMAGES/system_other.img',
72 os.urandom(len("system_other")))
73
74 return target_files
75
76
Tao Bao481bab82017-12-21 11:23:09 -080077class MockScriptWriter(object):
78 """A class that mocks edify_generator.EdifyGenerator.
79
80 It simply pushes the incoming arguments onto script stack, which is to assert
81 the calls to EdifyGenerator functions.
82 """
83
84 def __init__(self):
85 self.script = []
86
87 def Mount(self, *args):
88 self.script.append(('Mount',) + args)
89
90 def AssertDevice(self, *args):
91 self.script.append(('AssertDevice',) + args)
92
93 def AssertOemProperty(self, *args):
94 self.script.append(('AssertOemProperty',) + args)
95
96 def AssertFingerprintOrThumbprint(self, *args):
97 self.script.append(('AssertFingerprintOrThumbprint',) + args)
98
99 def AssertSomeFingerprint(self, *args):
100 self.script.append(('AssertSomeFingerprint',) + args)
101
102 def AssertSomeThumbprint(self, *args):
103 self.script.append(('AssertSomeThumbprint',) + args)
104
105
Tao Bao65b94e92018-10-11 21:57:26 -0700106class BuildInfoTest(test_utils.ReleaseToolsTestCase):
Tao Bao481bab82017-12-21 11:23:09 -0800107
108 TEST_INFO_DICT = {
109 'build.prop' : {
110 'ro.product.device' : 'product-device',
111 'ro.product.name' : 'product-name',
112 'ro.build.fingerprint' : 'build-fingerprint',
113 'ro.build.foo' : 'build-foo',
114 },
115 'vendor.build.prop' : {
116 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint',
117 },
118 'property1' : 'value1',
119 'property2' : 4096,
120 }
121
122 TEST_INFO_DICT_USES_OEM_PROPS = {
123 'build.prop' : {
124 'ro.product.name' : 'product-name',
125 'ro.build.thumbprint' : 'build-thumbprint',
126 'ro.build.bar' : 'build-bar',
127 },
128 'vendor.build.prop' : {
129 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint',
130 },
131 'property1' : 'value1',
132 'property2' : 4096,
133 'oem_fingerprint_properties' : 'ro.product.device ro.product.brand',
134 }
135
136 TEST_OEM_DICTS = [
137 {
138 'ro.product.brand' : 'brand1',
139 'ro.product.device' : 'device1',
140 },
141 {
142 'ro.product.brand' : 'brand2',
143 'ro.product.device' : 'device2',
144 },
145 {
146 'ro.product.brand' : 'brand3',
147 'ro.product.device' : 'device3',
148 },
149 ]
150
151 def test_init(self):
152 target_info = BuildInfo(self.TEST_INFO_DICT, None)
153 self.assertEqual('product-device', target_info.device)
154 self.assertEqual('build-fingerprint', target_info.fingerprint)
155 self.assertFalse(target_info.is_ab)
156 self.assertIsNone(target_info.oem_props)
157
158 def test_init_with_oem_props(self):
159 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
160 self.TEST_OEM_DICTS)
161 self.assertEqual('device1', target_info.device)
162 self.assertEqual('brand1/product-name/device1:build-thumbprint',
163 target_info.fingerprint)
164
165 # Swap the order in oem_dicts, which would lead to different BuildInfo.
166 oem_dicts = copy.copy(self.TEST_OEM_DICTS)
167 oem_dicts[0], oem_dicts[2] = oem_dicts[2], oem_dicts[0]
168 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, oem_dicts)
169 self.assertEqual('device3', target_info.device)
170 self.assertEqual('brand3/product-name/device3:build-thumbprint',
171 target_info.fingerprint)
172
173 # Missing oem_dict should be rejected.
174 self.assertRaises(AssertionError, BuildInfo,
175 self.TEST_INFO_DICT_USES_OEM_PROPS, None)
176
177 def test___getitem__(self):
178 target_info = BuildInfo(self.TEST_INFO_DICT, None)
179 self.assertEqual('value1', target_info['property1'])
180 self.assertEqual(4096, target_info['property2'])
181 self.assertEqual('build-foo', target_info['build.prop']['ro.build.foo'])
182
183 def test___getitem__with_oem_props(self):
184 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
185 self.TEST_OEM_DICTS)
186 self.assertEqual('value1', target_info['property1'])
187 self.assertEqual(4096, target_info['property2'])
188 self.assertRaises(KeyError,
189 lambda: target_info['build.prop']['ro.build.foo'])
190
Tao Bao667c7532018-07-06 10:13:59 -0700191 def test___setitem__(self):
192 target_info = BuildInfo(copy.deepcopy(self.TEST_INFO_DICT), None)
193 self.assertEqual('value1', target_info['property1'])
194 target_info['property1'] = 'value2'
195 self.assertEqual('value2', target_info['property1'])
196
197 self.assertEqual('build-foo', target_info['build.prop']['ro.build.foo'])
198 target_info['build.prop']['ro.build.foo'] = 'build-bar'
199 self.assertEqual('build-bar', target_info['build.prop']['ro.build.foo'])
200
Tao Bao481bab82017-12-21 11:23:09 -0800201 def test_get(self):
202 target_info = BuildInfo(self.TEST_INFO_DICT, None)
203 self.assertEqual('value1', target_info.get('property1'))
204 self.assertEqual(4096, target_info.get('property2'))
205 self.assertEqual(4096, target_info.get('property2', 1024))
206 self.assertEqual(1024, target_info.get('property-nonexistent', 1024))
207 self.assertEqual('build-foo', target_info.get('build.prop')['ro.build.foo'])
208
209 def test_get_with_oem_props(self):
210 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
211 self.TEST_OEM_DICTS)
212 self.assertEqual('value1', target_info.get('property1'))
213 self.assertEqual(4096, target_info.get('property2'))
214 self.assertEqual(4096, target_info.get('property2', 1024))
215 self.assertEqual(1024, target_info.get('property-nonexistent', 1024))
216 self.assertIsNone(target_info.get('build.prop').get('ro.build.foo'))
217 self.assertRaises(KeyError,
218 lambda: target_info.get('build.prop')['ro.build.foo'])
219
Tao Bao667c7532018-07-06 10:13:59 -0700220 def test_items(self):
221 target_info = BuildInfo(self.TEST_INFO_DICT, None)
222 items = target_info.items()
223 self.assertIn(('property1', 'value1'), items)
224 self.assertIn(('property2', 4096), items)
225
Tao Bao481bab82017-12-21 11:23:09 -0800226 def test_GetBuildProp(self):
227 target_info = BuildInfo(self.TEST_INFO_DICT, None)
228 self.assertEqual('build-foo', target_info.GetBuildProp('ro.build.foo'))
229 self.assertRaises(common.ExternalError, target_info.GetBuildProp,
230 'ro.build.nonexistent')
231
232 def test_GetBuildProp_with_oem_props(self):
233 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
234 self.TEST_OEM_DICTS)
235 self.assertEqual('build-bar', target_info.GetBuildProp('ro.build.bar'))
236 self.assertRaises(common.ExternalError, target_info.GetBuildProp,
237 'ro.build.nonexistent')
238
239 def test_GetVendorBuildProp(self):
240 target_info = BuildInfo(self.TEST_INFO_DICT, None)
241 self.assertEqual('vendor-build-fingerprint',
242 target_info.GetVendorBuildProp(
243 'ro.vendor.build.fingerprint'))
244 self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp,
245 'ro.build.nonexistent')
246
247 def test_GetVendorBuildProp_with_oem_props(self):
248 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
249 self.TEST_OEM_DICTS)
250 self.assertEqual('vendor-build-fingerprint',
251 target_info.GetVendorBuildProp(
252 'ro.vendor.build.fingerprint'))
253 self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp,
254 'ro.build.nonexistent')
255
Tao Baoea6cbd02018-09-05 13:06:37 -0700256 def test_vendor_fingerprint(self):
257 target_info = BuildInfo(self.TEST_INFO_DICT, None)
258 self.assertEqual('vendor-build-fingerprint',
259 target_info.vendor_fingerprint)
260
261 def test_vendor_fingerprint_blacklisted(self):
262 target_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS)
263 del target_info_dict['vendor.build.prop']['ro.vendor.build.fingerprint']
264 target_info = BuildInfo(target_info_dict, self.TEST_OEM_DICTS)
265 self.assertIsNone(target_info.vendor_fingerprint)
266
267 def test_vendor_fingerprint_without_vendor_build_prop(self):
268 target_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS)
269 del target_info_dict['vendor.build.prop']
270 target_info = BuildInfo(target_info_dict, self.TEST_OEM_DICTS)
271 self.assertIsNone(target_info.vendor_fingerprint)
272
Tao Bao481bab82017-12-21 11:23:09 -0800273 def test_WriteMountOemScript(self):
274 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
275 self.TEST_OEM_DICTS)
276 script_writer = MockScriptWriter()
277 target_info.WriteMountOemScript(script_writer)
278 self.assertEqual([('Mount', '/oem', None)], script_writer.script)
279
280 def test_WriteDeviceAssertions(self):
281 target_info = BuildInfo(self.TEST_INFO_DICT, None)
282 script_writer = MockScriptWriter()
283 target_info.WriteDeviceAssertions(script_writer, False)
284 self.assertEqual([('AssertDevice', 'product-device')], script_writer.script)
285
286 def test_WriteDeviceAssertions_with_oem_props(self):
287 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
288 self.TEST_OEM_DICTS)
289 script_writer = MockScriptWriter()
290 target_info.WriteDeviceAssertions(script_writer, False)
291 self.assertEqual(
292 [
293 ('AssertOemProperty', 'ro.product.device',
294 ['device1', 'device2', 'device3'], False),
295 ('AssertOemProperty', 'ro.product.brand',
296 ['brand1', 'brand2', 'brand3'], False),
297 ],
298 script_writer.script)
299
300 def test_WriteFingerprintAssertion_without_oem_props(self):
301 target_info = BuildInfo(self.TEST_INFO_DICT, None)
302 source_info_dict = copy.deepcopy(self.TEST_INFO_DICT)
303 source_info_dict['build.prop']['ro.build.fingerprint'] = (
304 'source-build-fingerprint')
305 source_info = BuildInfo(source_info_dict, None)
306
307 script_writer = MockScriptWriter()
308 WriteFingerprintAssertion(script_writer, target_info, source_info)
309 self.assertEqual(
310 [('AssertSomeFingerprint', 'source-build-fingerprint',
311 'build-fingerprint')],
312 script_writer.script)
313
314 def test_WriteFingerprintAssertion_with_source_oem_props(self):
315 target_info = BuildInfo(self.TEST_INFO_DICT, None)
316 source_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
317 self.TEST_OEM_DICTS)
318
319 script_writer = MockScriptWriter()
320 WriteFingerprintAssertion(script_writer, target_info, source_info)
321 self.assertEqual(
322 [('AssertFingerprintOrThumbprint', 'build-fingerprint',
323 'build-thumbprint')],
324 script_writer.script)
325
326 def test_WriteFingerprintAssertion_with_target_oem_props(self):
327 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
328 self.TEST_OEM_DICTS)
329 source_info = BuildInfo(self.TEST_INFO_DICT, None)
330
331 script_writer = MockScriptWriter()
332 WriteFingerprintAssertion(script_writer, target_info, source_info)
333 self.assertEqual(
334 [('AssertFingerprintOrThumbprint', 'build-fingerprint',
335 'build-thumbprint')],
336 script_writer.script)
337
338 def test_WriteFingerprintAssertion_with_both_oem_props(self):
339 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
340 self.TEST_OEM_DICTS)
341 source_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS)
342 source_info_dict['build.prop']['ro.build.thumbprint'] = (
343 'source-build-thumbprint')
344 source_info = BuildInfo(source_info_dict, self.TEST_OEM_DICTS)
345
346 script_writer = MockScriptWriter()
347 WriteFingerprintAssertion(script_writer, target_info, source_info)
348 self.assertEqual(
349 [('AssertSomeThumbprint', 'build-thumbprint',
350 'source-build-thumbprint')],
351 script_writer.script)
352
353
Tao Bao65b94e92018-10-11 21:57:26 -0700354class LoadOemDictsTest(test_utils.ReleaseToolsTestCase):
Tao Bao481bab82017-12-21 11:23:09 -0800355
356 def test_NoneDict(self):
357 self.assertIsNone(_LoadOemDicts(None))
358
359 def test_SingleDict(self):
360 dict_file = common.MakeTempFile()
361 with open(dict_file, 'w') as dict_fp:
362 dict_fp.write('abc=1\ndef=2\nxyz=foo\na.b.c=bar\n')
363
364 oem_dicts = _LoadOemDicts([dict_file])
365 self.assertEqual(1, len(oem_dicts))
366 self.assertEqual('foo', oem_dicts[0]['xyz'])
367 self.assertEqual('bar', oem_dicts[0]['a.b.c'])
368
369 def test_MultipleDicts(self):
370 oem_source = []
371 for i in range(3):
372 dict_file = common.MakeTempFile()
373 with open(dict_file, 'w') as dict_fp:
374 dict_fp.write(
375 'ro.build.index={}\ndef=2\nxyz=foo\na.b.c=bar\n'.format(i))
376 oem_source.append(dict_file)
377
378 oem_dicts = _LoadOemDicts(oem_source)
379 self.assertEqual(3, len(oem_dicts))
380 for i, oem_dict in enumerate(oem_dicts):
381 self.assertEqual('2', oem_dict['def'])
382 self.assertEqual('foo', oem_dict['xyz'])
383 self.assertEqual('bar', oem_dict['a.b.c'])
384 self.assertEqual('{}'.format(i), oem_dict['ro.build.index'])
Tao Baodf3a48b2018-01-10 16:30:43 -0800385
386
Tao Bao65b94e92018-10-11 21:57:26 -0700387class OtaFromTargetFilesTest(test_utils.ReleaseToolsTestCase):
Tao Baodf3a48b2018-01-10 16:30:43 -0800388
389 TEST_TARGET_INFO_DICT = {
390 'build.prop' : {
391 'ro.product.device' : 'product-device',
392 'ro.build.fingerprint' : 'build-fingerprint-target',
393 'ro.build.version.incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800394 'ro.build.version.sdk' : '27',
395 'ro.build.version.security_patch' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800396 'ro.build.date.utc' : '1500000000',
397 },
398 }
399
400 TEST_SOURCE_INFO_DICT = {
401 'build.prop' : {
402 'ro.product.device' : 'product-device',
403 'ro.build.fingerprint' : 'build-fingerprint-source',
404 'ro.build.version.incremental' : 'build-version-incremental-source',
Tao Bao35dc2552018-02-01 13:18:00 -0800405 'ro.build.version.sdk' : '25',
406 'ro.build.version.security_patch' : '2016-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800407 'ro.build.date.utc' : '1400000000',
408 },
409 }
410
411 def setUp(self):
Tao Bao3bf8c652018-03-16 12:59:42 -0700412 self.testdata_dir = test_utils.get_testdata_dir()
413 self.assertTrue(os.path.exists(self.testdata_dir))
414
Tao Baodf3a48b2018-01-10 16:30:43 -0800415 # Reset the global options as in ota_from_target_files.py.
416 common.OPTIONS.incremental_source = None
417 common.OPTIONS.downgrade = False
Tao Bao393eeb42019-03-06 16:00:38 -0800418 common.OPTIONS.retrofit_dynamic_partitions = False
Tao Baodf3a48b2018-01-10 16:30:43 -0800419 common.OPTIONS.timestamp = False
420 common.OPTIONS.wipe_user_data = False
Tao Bao3bf8c652018-03-16 12:59:42 -0700421 common.OPTIONS.no_signing = False
422 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
423 common.OPTIONS.key_passwords = {
424 common.OPTIONS.package_key : None,
425 }
426
427 common.OPTIONS.search_path = test_utils.get_search_path()
428 self.assertIsNotNone(common.OPTIONS.search_path)
Tao Baodf3a48b2018-01-10 16:30:43 -0800429
Tao Bao82490d32019-04-09 00:12:30 -0700430 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baodf3a48b2018-01-10 16:30:43 -0800431 def test_GetPackageMetadata_abOta_full(self):
432 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
433 target_info_dict['ab_update'] = 'true'
434 target_info = BuildInfo(target_info_dict, None)
435 metadata = GetPackageMetadata(target_info)
436 self.assertDictEqual(
437 {
438 'ota-type' : 'AB',
439 'ota-required-cache' : '0',
440 'post-build' : 'build-fingerprint-target',
441 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800442 'post-sdk-level' : '27',
443 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800444 'post-timestamp' : '1500000000',
445 'pre-device' : 'product-device',
446 },
447 metadata)
448
Tao Bao82490d32019-04-09 00:12:30 -0700449 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baodf3a48b2018-01-10 16:30:43 -0800450 def test_GetPackageMetadata_abOta_incremental(self):
451 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
452 target_info_dict['ab_update'] = 'true'
453 target_info = BuildInfo(target_info_dict, None)
454 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
455 common.OPTIONS.incremental_source = ''
456 metadata = GetPackageMetadata(target_info, source_info)
457 self.assertDictEqual(
458 {
459 'ota-type' : 'AB',
460 'ota-required-cache' : '0',
461 'post-build' : 'build-fingerprint-target',
462 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800463 'post-sdk-level' : '27',
464 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800465 'post-timestamp' : '1500000000',
466 'pre-device' : 'product-device',
467 'pre-build' : 'build-fingerprint-source',
468 'pre-build-incremental' : 'build-version-incremental-source',
469 },
470 metadata)
471
Tao Bao82490d32019-04-09 00:12:30 -0700472 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baodf3a48b2018-01-10 16:30:43 -0800473 def test_GetPackageMetadata_nonAbOta_full(self):
474 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
475 metadata = GetPackageMetadata(target_info)
476 self.assertDictEqual(
477 {
478 'ota-type' : 'BLOCK',
479 'post-build' : 'build-fingerprint-target',
480 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800481 'post-sdk-level' : '27',
482 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800483 'post-timestamp' : '1500000000',
484 'pre-device' : 'product-device',
485 },
486 metadata)
487
Tao Bao82490d32019-04-09 00:12:30 -0700488 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baodf3a48b2018-01-10 16:30:43 -0800489 def test_GetPackageMetadata_nonAbOta_incremental(self):
490 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
491 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
492 common.OPTIONS.incremental_source = ''
493 metadata = GetPackageMetadata(target_info, source_info)
494 self.assertDictEqual(
495 {
496 'ota-type' : 'BLOCK',
497 'post-build' : 'build-fingerprint-target',
498 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800499 'post-sdk-level' : '27',
500 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800501 'post-timestamp' : '1500000000',
502 'pre-device' : 'product-device',
503 'pre-build' : 'build-fingerprint-source',
504 'pre-build-incremental' : 'build-version-incremental-source',
505 },
506 metadata)
507
Tao Bao82490d32019-04-09 00:12:30 -0700508 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baodf3a48b2018-01-10 16:30:43 -0800509 def test_GetPackageMetadata_wipe(self):
510 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
511 common.OPTIONS.wipe_user_data = True
512 metadata = GetPackageMetadata(target_info)
513 self.assertDictEqual(
514 {
515 'ota-type' : 'BLOCK',
516 'ota-wipe' : 'yes',
517 'post-build' : 'build-fingerprint-target',
518 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800519 'post-sdk-level' : '27',
520 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800521 'post-timestamp' : '1500000000',
522 'pre-device' : 'product-device',
523 },
524 metadata)
525
Tao Bao82490d32019-04-09 00:12:30 -0700526 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao393eeb42019-03-06 16:00:38 -0800527 def test_GetPackageMetadata_retrofitDynamicPartitions(self):
528 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
529 common.OPTIONS.retrofit_dynamic_partitions = True
530 metadata = GetPackageMetadata(target_info)
531 self.assertDictEqual(
532 {
533 'ota-retrofit-dynamic-partitions' : 'yes',
534 'ota-type' : 'BLOCK',
535 'post-build' : 'build-fingerprint-target',
536 'post-build-incremental' : 'build-version-incremental-target',
537 'post-sdk-level' : '27',
538 'post-security-patch-level' : '2017-12-01',
539 'post-timestamp' : '1500000000',
540 'pre-device' : 'product-device',
541 },
542 metadata)
543
Tao Baodf3a48b2018-01-10 16:30:43 -0800544 @staticmethod
545 def _test_GetPackageMetadata_swapBuildTimestamps(target_info, source_info):
546 (target_info['build.prop']['ro.build.date.utc'],
547 source_info['build.prop']['ro.build.date.utc']) = (
548 source_info['build.prop']['ro.build.date.utc'],
549 target_info['build.prop']['ro.build.date.utc'])
550
Tao Bao82490d32019-04-09 00:12:30 -0700551 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baodf3a48b2018-01-10 16:30:43 -0800552 def test_GetPackageMetadata_unintentionalDowngradeDetected(self):
553 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
554 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
555 self._test_GetPackageMetadata_swapBuildTimestamps(
556 target_info_dict, source_info_dict)
557
558 target_info = BuildInfo(target_info_dict, None)
559 source_info = BuildInfo(source_info_dict, None)
560 common.OPTIONS.incremental_source = ''
561 self.assertRaises(RuntimeError, GetPackageMetadata, target_info,
562 source_info)
563
Tao Bao82490d32019-04-09 00:12:30 -0700564 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baodf3a48b2018-01-10 16:30:43 -0800565 def test_GetPackageMetadata_downgrade(self):
566 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
567 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
568 self._test_GetPackageMetadata_swapBuildTimestamps(
569 target_info_dict, source_info_dict)
570
571 target_info = BuildInfo(target_info_dict, None)
572 source_info = BuildInfo(source_info_dict, None)
573 common.OPTIONS.incremental_source = ''
574 common.OPTIONS.downgrade = True
575 common.OPTIONS.wipe_user_data = True
576 metadata = GetPackageMetadata(target_info, source_info)
577 self.assertDictEqual(
578 {
579 'ota-downgrade' : 'yes',
580 'ota-type' : 'BLOCK',
581 'ota-wipe' : 'yes',
582 'post-build' : 'build-fingerprint-target',
583 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800584 'post-sdk-level' : '27',
585 'post-security-patch-level' : '2017-12-01',
Tao Baofaa8e0b2018-04-12 14:31:43 -0700586 'post-timestamp' : '1400000000',
Tao Baodf3a48b2018-01-10 16:30:43 -0800587 'pre-device' : 'product-device',
588 'pre-build' : 'build-fingerprint-source',
589 'pre-build-incremental' : 'build-version-incremental-source',
590 },
591 metadata)
Tao Baofabe0832018-01-17 15:52:28 -0800592
Tao Bao82490d32019-04-09 00:12:30 -0700593 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baof7140c02018-01-30 17:09:24 -0800594 def test_GetTargetFilesZipForSecondaryImages(self):
595 input_file = construct_target_files(secondary=True)
596 target_file = GetTargetFilesZipForSecondaryImages(input_file)
597
598 with zipfile.ZipFile(target_file) as verify_zip:
599 namelist = verify_zip.namelist()
600
601 self.assertIn('META/ab_partitions.txt', namelist)
602 self.assertIn('IMAGES/boot.img', namelist)
603 self.assertIn('IMAGES/system.img', namelist)
604 self.assertIn('IMAGES/vendor.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700605 self.assertIn('RADIO/bootloader.img', namelist)
606 self.assertIn('RADIO/modem.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800607 self.assertIn(POSTINSTALL_CONFIG, namelist)
Tao Baof7140c02018-01-30 17:09:24 -0800608
609 self.assertNotIn('IMAGES/system_other.img', namelist)
610 self.assertNotIn('IMAGES/system.map', namelist)
611
Tao Bao82490d32019-04-09 00:12:30 -0700612 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao15a146a2018-02-21 16:06:59 -0800613 def test_GetTargetFilesZipForSecondaryImages_skipPostinstall(self):
614 input_file = construct_target_files(secondary=True)
615 target_file = GetTargetFilesZipForSecondaryImages(
616 input_file, skip_postinstall=True)
617
618 with zipfile.ZipFile(target_file) as verify_zip:
619 namelist = verify_zip.namelist()
620
621 self.assertIn('META/ab_partitions.txt', namelist)
622 self.assertIn('IMAGES/boot.img', namelist)
623 self.assertIn('IMAGES/system.img', namelist)
624 self.assertIn('IMAGES/vendor.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700625 self.assertIn('RADIO/bootloader.img', namelist)
626 self.assertIn('RADIO/modem.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800627
628 self.assertNotIn('IMAGES/system_other.img', namelist)
629 self.assertNotIn('IMAGES/system.map', namelist)
630 self.assertNotIn(POSTINSTALL_CONFIG, namelist)
631
Tao Bao82490d32019-04-09 00:12:30 -0700632 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao12489802018-07-12 14:47:38 -0700633 def test_GetTargetFilesZipForSecondaryImages_withoutRadioImages(self):
634 input_file = construct_target_files(secondary=True)
635 common.ZipDelete(input_file, 'RADIO/bootloader.img')
636 common.ZipDelete(input_file, 'RADIO/modem.img')
637 target_file = GetTargetFilesZipForSecondaryImages(input_file)
638
639 with zipfile.ZipFile(target_file) as verify_zip:
640 namelist = verify_zip.namelist()
641
642 self.assertIn('META/ab_partitions.txt', namelist)
643 self.assertIn('IMAGES/boot.img', namelist)
644 self.assertIn('IMAGES/system.img', namelist)
645 self.assertIn('IMAGES/vendor.img', namelist)
646 self.assertIn(POSTINSTALL_CONFIG, namelist)
647
648 self.assertNotIn('IMAGES/system_other.img', namelist)
649 self.assertNotIn('IMAGES/system.map', namelist)
650 self.assertNotIn('RADIO/bootloader.img', namelist)
651 self.assertNotIn('RADIO/modem.img', namelist)
652
Tao Bao82490d32019-04-09 00:12:30 -0700653 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao15a146a2018-02-21 16:06:59 -0800654 def test_GetTargetFilesZipWithoutPostinstallConfig(self):
655 input_file = construct_target_files()
656 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
657 with zipfile.ZipFile(target_file) as verify_zip:
658 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
659
Tao Bao82490d32019-04-09 00:12:30 -0700660 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao15a146a2018-02-21 16:06:59 -0800661 def test_GetTargetFilesZipWithoutPostinstallConfig_missingEntry(self):
662 input_file = construct_target_files()
663 common.ZipDelete(input_file, POSTINSTALL_CONFIG)
664 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
665 with zipfile.ZipFile(target_file) as verify_zip:
666 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
667
Tao Bao3bf8c652018-03-16 12:59:42 -0700668 def _test_FinalizeMetadata(self, large_entry=False):
669 entries = [
670 'required-entry1',
671 'required-entry2',
672 ]
673 zip_file = PropertyFilesTest.construct_zip_package(entries)
674 # Add a large entry of 1 GiB if requested.
675 if large_entry:
676 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
677 zip_fp.writestr(
678 # Using 'zoo' so that the entry stays behind others after signing.
679 'zoo',
680 'A' * 1024 * 1024 * 1024,
681 zipfile.ZIP_STORED)
682
683 metadata = {}
684 output_file = common.MakeTempFile(suffix='.zip')
685 needed_property_files = (
686 TestPropertyFiles(),
687 )
688 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
689 self.assertIn('ota-test-property-files', metadata)
690
Tao Bao82490d32019-04-09 00:12:30 -0700691 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700692 def test_FinalizeMetadata(self):
693 self._test_FinalizeMetadata()
694
Tao Bao82490d32019-04-09 00:12:30 -0700695 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700696 def test_FinalizeMetadata_withNoSigning(self):
697 common.OPTIONS.no_signing = True
698 self._test_FinalizeMetadata()
699
Tao Bao82490d32019-04-09 00:12:30 -0700700 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700701 def test_FinalizeMetadata_largeEntry(self):
702 self._test_FinalizeMetadata(large_entry=True)
703
Tao Bao82490d32019-04-09 00:12:30 -0700704 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700705 def test_FinalizeMetadata_largeEntry_withNoSigning(self):
706 common.OPTIONS.no_signing = True
707 self._test_FinalizeMetadata(large_entry=True)
708
Tao Bao82490d32019-04-09 00:12:30 -0700709 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700710 def test_FinalizeMetadata_insufficientSpace(self):
711 entries = [
712 'required-entry1',
713 'required-entry2',
714 'optional-entry1',
715 'optional-entry2',
716 ]
717 zip_file = PropertyFilesTest.construct_zip_package(entries)
718 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
719 zip_fp.writestr(
720 # 'foo-entry1' will appear ahead of all other entries (in alphabetical
721 # order) after the signing, which will in turn trigger the
722 # InsufficientSpaceException and an automatic retry.
723 'foo-entry1',
724 'A' * 1024 * 1024,
725 zipfile.ZIP_STORED)
726
727 metadata = {}
728 needed_property_files = (
729 TestPropertyFiles(),
730 )
731 output_file = common.MakeTempFile(suffix='.zip')
732 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
733 self.assertIn('ota-test-property-files', metadata)
734
Tao Baoae5e4c32018-03-01 19:30:00 -0800735
Tao Bao69203522018-03-08 16:09:01 -0800736class TestPropertyFiles(PropertyFiles):
737 """A class that extends PropertyFiles for testing purpose."""
738
739 def __init__(self):
740 super(TestPropertyFiles, self).__init__()
741 self.name = 'ota-test-property-files'
742 self.required = (
743 'required-entry1',
744 'required-entry2',
745 )
746 self.optional = (
747 'optional-entry1',
748 'optional-entry2',
749 )
750
751
Tao Bao65b94e92018-10-11 21:57:26 -0700752class PropertyFilesTest(test_utils.ReleaseToolsTestCase):
Tao Baoae5e4c32018-03-01 19:30:00 -0800753
Tao Bao3bf8c652018-03-16 12:59:42 -0700754 def setUp(self):
755 common.OPTIONS.no_signing = False
756
Tao Baof5110492018-03-02 09:47:43 -0800757 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -0700758 def construct_zip_package(entries):
Tao Baof5110492018-03-02 09:47:43 -0800759 zip_file = common.MakeTempFile(suffix='.zip')
760 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
761 for entry in entries:
762 zip_fp.writestr(
763 entry,
764 entry.replace('.', '-').upper(),
765 zipfile.ZIP_STORED)
766 return zip_file
767
768 @staticmethod
Tao Bao69203522018-03-08 16:09:01 -0800769 def _parse_property_files_string(data):
Tao Baof5110492018-03-02 09:47:43 -0800770 result = {}
771 for token in data.split(','):
772 name, info = token.split(':', 1)
773 result[name] = info
774 return result
775
776 def _verify_entries(self, input_file, tokens, entries):
777 for entry in entries:
778 offset, size = map(int, tokens[entry].split(':'))
779 with open(input_file, 'rb') as input_fp:
780 input_fp.seek(offset)
781 if entry == 'metadata':
782 expected = b'META-INF/COM/ANDROID/METADATA'
783 else:
784 expected = entry.replace('.', '-').upper().encode()
785 self.assertEqual(expected, input_fp.read(size))
786
Tao Bao82490d32019-04-09 00:12:30 -0700787 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoae5e4c32018-03-01 19:30:00 -0800788 def test_Compute(self):
Tao Baof5110492018-03-02 09:47:43 -0800789 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800790 'required-entry1',
791 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800792 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700793 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800794 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800795 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800796 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800797
Tao Bao69203522018-03-08 16:09:01 -0800798 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800799 self.assertEqual(3, len(tokens))
800 self._verify_entries(zip_file, tokens, entries)
801
Tao Bao69203522018-03-08 16:09:01 -0800802 def test_Compute_withOptionalEntries(self):
Tao Baof5110492018-03-02 09:47:43 -0800803 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800804 'required-entry1',
805 'required-entry2',
806 'optional-entry1',
807 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800808 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700809 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800810 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800811 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800812 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800813
Tao Bao69203522018-03-08 16:09:01 -0800814 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800815 self.assertEqual(5, len(tokens))
816 self._verify_entries(zip_file, tokens, entries)
817
Tao Bao69203522018-03-08 16:09:01 -0800818 def test_Compute_missingRequiredEntry(self):
819 entries = (
820 'required-entry2',
821 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700822 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800823 property_files = TestPropertyFiles()
824 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
825 self.assertRaises(KeyError, property_files.Compute, zip_fp)
826
Tao Bao82490d32019-04-09 00:12:30 -0700827 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoae5e4c32018-03-01 19:30:00 -0800828 def test_Finalize(self):
Tao Baof5110492018-03-02 09:47:43 -0800829 entries = [
Tao Bao69203522018-03-08 16:09:01 -0800830 'required-entry1',
831 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800832 'META-INF/com/android/metadata',
833 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700834 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800835 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800836 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700837 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800838 zip_fp, reserve_space=False)
839 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
Tao Bao69203522018-03-08 16:09:01 -0800840 tokens = self._parse_property_files_string(streaming_metadata)
Tao Baof5110492018-03-02 09:47:43 -0800841
842 self.assertEqual(3, len(tokens))
843 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
844 # streaming metadata.
845 entries[2] = 'metadata'
846 self._verify_entries(zip_file, tokens, entries)
847
Tao Bao82490d32019-04-09 00:12:30 -0700848 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoae5e4c32018-03-01 19:30:00 -0800849 def test_Finalize_assertReservedLength(self):
Tao Baof5110492018-03-02 09:47:43 -0800850 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800851 'required-entry1',
852 'required-entry2',
853 'optional-entry1',
854 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800855 'META-INF/com/android/metadata',
856 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700857 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800858 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800859 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
860 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700861 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800862 zip_fp, reserve_space=False)
Tao Baof5110492018-03-02 09:47:43 -0800863 raw_length = len(raw_metadata)
864
865 # Now pass in the exact expected length.
Tao Baoae5e4c32018-03-01 19:30:00 -0800866 streaming_metadata = property_files.Finalize(zip_fp, raw_length)
Tao Baof5110492018-03-02 09:47:43 -0800867 self.assertEqual(raw_length, len(streaming_metadata))
868
869 # Or pass in insufficient length.
870 self.assertRaises(
Tao Bao3bf8c652018-03-16 12:59:42 -0700871 PropertyFiles.InsufficientSpaceException,
Tao Baoae5e4c32018-03-01 19:30:00 -0800872 property_files.Finalize,
Tao Baof5110492018-03-02 09:47:43 -0800873 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800874 raw_length - 1)
Tao Baof5110492018-03-02 09:47:43 -0800875
876 # Or pass in a much larger size.
Tao Baoae5e4c32018-03-01 19:30:00 -0800877 streaming_metadata = property_files.Finalize(
Tao Baof5110492018-03-02 09:47:43 -0800878 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800879 raw_length + 20)
Tao Baof5110492018-03-02 09:47:43 -0800880 self.assertEqual(raw_length + 20, len(streaming_metadata))
881 self.assertEqual(' ' * 20, streaming_metadata[raw_length:])
882
Tao Baoae5e4c32018-03-01 19:30:00 -0800883 def test_Verify(self):
884 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800885 'required-entry1',
886 'required-entry2',
887 'optional-entry1',
888 'optional-entry2',
889 'META-INF/com/android/metadata',
890 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700891 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800892 property_files = TestPropertyFiles()
893 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
894 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700895 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800896 zip_fp, reserve_space=False)
897
898 # Should pass the test if verification passes.
899 property_files.Verify(zip_fp, raw_metadata)
900
901 # Or raise on verification failure.
902 self.assertRaises(
903 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
904
905
906class StreamingPropertyFilesTest(PropertyFilesTest):
907 """Additional sanity checks specialized for StreamingPropertyFiles."""
908
909 def test_init(self):
910 property_files = StreamingPropertyFiles()
911 self.assertEqual('ota-streaming-property-files', property_files.name)
912 self.assertEqual(
913 (
914 'payload.bin',
915 'payload_properties.txt',
916 ),
917 property_files.required)
918 self.assertEqual(
919 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -0700920 'care_map.pb',
Tao Bao69203522018-03-08 16:09:01 -0800921 'care_map.txt',
922 'compatibility.zip',
923 ),
924 property_files.optional)
925
926 def test_Compute(self):
927 entries = (
Tao Baoae5e4c32018-03-01 19:30:00 -0800928 'payload.bin',
929 'payload_properties.txt',
930 'care_map.txt',
Tao Bao69203522018-03-08 16:09:01 -0800931 'compatibility.zip',
932 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700933 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800934 property_files = StreamingPropertyFiles()
935 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
936 property_files_string = property_files.Compute(zip_fp)
937
938 tokens = self._parse_property_files_string(property_files_string)
939 self.assertEqual(5, len(tokens))
940 self._verify_entries(zip_file, tokens, entries)
941
942 def test_Finalize(self):
943 entries = [
944 'payload.bin',
945 'payload_properties.txt',
946 'care_map.txt',
947 'compatibility.zip',
948 'META-INF/com/android/metadata',
949 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700950 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800951 property_files = StreamingPropertyFiles()
952 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700953 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800954 zip_fp, reserve_space=False)
955 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
956 tokens = self._parse_property_files_string(streaming_metadata)
957
958 self.assertEqual(5, len(tokens))
959 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
960 # streaming metadata.
961 entries[4] = 'metadata'
962 self._verify_entries(zip_file, tokens, entries)
963
964 def test_Verify(self):
965 entries = (
966 'payload.bin',
967 'payload_properties.txt',
968 'care_map.txt',
969 'compatibility.zip',
Tao Baoae5e4c32018-03-01 19:30:00 -0800970 'META-INF/com/android/metadata',
971 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700972 zip_file = self.construct_zip_package(entries)
Tao Baoae5e4c32018-03-01 19:30:00 -0800973 property_files = StreamingPropertyFiles()
974 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
975 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700976 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800977 zip_fp, reserve_space=False)
978
979 # Should pass the test if verification passes.
980 property_files.Verify(zip_fp, raw_metadata)
981
982 # Or raise on verification failure.
983 self.assertRaises(
984 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
985
Tao Baofabe0832018-01-17 15:52:28 -0800986
Tao Baob6304672018-03-08 16:28:33 -0800987class AbOtaPropertyFilesTest(PropertyFilesTest):
988 """Additional sanity checks specialized for AbOtaPropertyFiles."""
989
990 # The size for payload and metadata signature size.
991 SIGNATURE_SIZE = 256
992
993 def setUp(self):
994 self.testdata_dir = test_utils.get_testdata_dir()
995 self.assertTrue(os.path.exists(self.testdata_dir))
996
997 common.OPTIONS.wipe_user_data = False
998 common.OPTIONS.payload_signer = None
999 common.OPTIONS.payload_signer_args = None
1000 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1001 common.OPTIONS.key_passwords = {
1002 common.OPTIONS.package_key : None,
1003 }
1004
1005 def test_init(self):
1006 property_files = AbOtaPropertyFiles()
1007 self.assertEqual('ota-property-files', property_files.name)
1008 self.assertEqual(
1009 (
1010 'payload.bin',
1011 'payload_properties.txt',
1012 ),
1013 property_files.required)
1014 self.assertEqual(
1015 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -07001016 'care_map.pb',
Tao Baob6304672018-03-08 16:28:33 -08001017 'care_map.txt',
1018 'compatibility.zip',
1019 ),
1020 property_files.optional)
1021
Tao Bao82490d32019-04-09 00:12:30 -07001022 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baob6304672018-03-08 16:28:33 -08001023 def test_GetPayloadMetadataOffsetAndSize(self):
1024 target_file = construct_target_files()
1025 payload = Payload()
1026 payload.Generate(target_file)
1027
1028 payload_signer = PayloadSigner()
1029 payload.Sign(payload_signer)
1030
1031 output_file = common.MakeTempFile(suffix='.zip')
1032 with zipfile.ZipFile(output_file, 'w') as output_zip:
1033 payload.WriteToZip(output_zip)
1034
1035 # Find out the payload metadata offset and size.
1036 property_files = AbOtaPropertyFiles()
1037 with zipfile.ZipFile(output_file) as input_zip:
1038 # pylint: disable=protected-access
1039 payload_offset, metadata_total = (
1040 property_files._GetPayloadMetadataOffsetAndSize(input_zip))
1041
1042 # Read in the metadata signature directly.
1043 with open(output_file, 'rb') as verify_fp:
1044 verify_fp.seek(payload_offset + metadata_total - self.SIGNATURE_SIZE)
1045 metadata_signature = verify_fp.read(self.SIGNATURE_SIZE)
1046
1047 # Now we extract the metadata hash via brillo_update_payload script, which
1048 # will serve as the oracle result.
1049 payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1050 metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1051 cmd = ['brillo_update_payload', 'hash',
1052 '--unsigned_payload', payload.payload_file,
1053 '--signature_size', str(self.SIGNATURE_SIZE),
1054 '--metadata_hash_file', metadata_sig_file,
1055 '--payload_hash_file', payload_sig_file]
Tao Bao73dd4f42018-10-04 16:25:33 -07001056 proc = common.Run(cmd)
Tao Baob6304672018-03-08 16:28:33 -08001057 stdoutdata, _ = proc.communicate()
1058 self.assertEqual(
1059 0, proc.returncode,
Tao Bao73dd4f42018-10-04 16:25:33 -07001060 'Failed to run brillo_update_payload:\n{}'.format(stdoutdata))
Tao Baob6304672018-03-08 16:28:33 -08001061
1062 signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file)
1063
1064 # Finally we can compare the two signatures.
1065 with open(signed_metadata_sig_file, 'rb') as verify_fp:
1066 self.assertEqual(verify_fp.read(), metadata_signature)
1067
1068 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -07001069 def construct_zip_package_withValidPayload(with_metadata=False):
1070 # Cannot use construct_zip_package() since we need a "valid" payload.bin.
Tao Baob6304672018-03-08 16:28:33 -08001071 target_file = construct_target_files()
1072 payload = Payload()
1073 payload.Generate(target_file)
1074
1075 payload_signer = PayloadSigner()
1076 payload.Sign(payload_signer)
1077
1078 zip_file = common.MakeTempFile(suffix='.zip')
1079 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
1080 # 'payload.bin',
1081 payload.WriteToZip(zip_fp)
1082
1083 # Other entries.
1084 entries = ['care_map.txt', 'compatibility.zip']
1085
1086 # Put META-INF/com/android/metadata if needed.
1087 if with_metadata:
1088 entries.append('META-INF/com/android/metadata')
1089
1090 for entry in entries:
1091 zip_fp.writestr(
1092 entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED)
1093
1094 return zip_file
1095
Tao Bao82490d32019-04-09 00:12:30 -07001096 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baob6304672018-03-08 16:28:33 -08001097 def test_Compute(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001098 zip_file = self.construct_zip_package_withValidPayload()
Tao Baob6304672018-03-08 16:28:33 -08001099 property_files = AbOtaPropertyFiles()
1100 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1101 property_files_string = property_files.Compute(zip_fp)
1102
1103 tokens = self._parse_property_files_string(property_files_string)
1104 # "6" indcludes the four entries above, one metadata entry, and one entry
1105 # for payload-metadata.bin.
1106 self.assertEqual(6, len(tokens))
1107 self._verify_entries(
1108 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1109
Tao Bao82490d32019-04-09 00:12:30 -07001110 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baob6304672018-03-08 16:28:33 -08001111 def test_Finalize(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001112 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001113 property_files = AbOtaPropertyFiles()
1114 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001115 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001116 zip_fp, reserve_space=False)
1117 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1118
1119 tokens = self._parse_property_files_string(property_files_string)
1120 # "6" indcludes the four entries above, one metadata entry, and one entry
1121 # for payload-metadata.bin.
1122 self.assertEqual(6, len(tokens))
1123 self._verify_entries(
1124 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1125
Tao Bao82490d32019-04-09 00:12:30 -07001126 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baob6304672018-03-08 16:28:33 -08001127 def test_Verify(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001128 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001129 property_files = AbOtaPropertyFiles()
1130 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001131 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001132 zip_fp, reserve_space=False)
1133
1134 property_files.Verify(zip_fp, raw_metadata)
1135
1136
Tao Baoc0746f42018-02-21 13:17:22 -08001137class NonAbOtaPropertyFilesTest(PropertyFilesTest):
1138 """Additional sanity checks specialized for NonAbOtaPropertyFiles."""
1139
1140 def test_init(self):
1141 property_files = NonAbOtaPropertyFiles()
1142 self.assertEqual('ota-property-files', property_files.name)
1143 self.assertEqual((), property_files.required)
1144 self.assertEqual((), property_files.optional)
1145
1146 def test_Compute(self):
1147 entries = ()
Tao Bao3bf8c652018-03-16 12:59:42 -07001148 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001149 property_files = NonAbOtaPropertyFiles()
1150 with zipfile.ZipFile(zip_file) as zip_fp:
1151 property_files_string = property_files.Compute(zip_fp)
1152
1153 tokens = self._parse_property_files_string(property_files_string)
1154 self.assertEqual(1, len(tokens))
1155 self._verify_entries(zip_file, tokens, entries)
1156
1157 def test_Finalize(self):
1158 entries = [
1159 'META-INF/com/android/metadata',
1160 ]
Tao Bao3bf8c652018-03-16 12:59:42 -07001161 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001162 property_files = NonAbOtaPropertyFiles()
1163 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001164 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001165 zip_fp, reserve_space=False)
1166 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1167 tokens = self._parse_property_files_string(property_files_string)
1168
1169 self.assertEqual(1, len(tokens))
1170 # 'META-INF/com/android/metadata' will be key'd as 'metadata'.
1171 entries[0] = 'metadata'
1172 self._verify_entries(zip_file, tokens, entries)
1173
1174 def test_Verify(self):
1175 entries = (
1176 'META-INF/com/android/metadata',
1177 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001178 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001179 property_files = NonAbOtaPropertyFiles()
1180 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001181 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001182 zip_fp, reserve_space=False)
1183
1184 property_files.Verify(zip_fp, raw_metadata)
1185
1186
Tao Bao65b94e92018-10-11 21:57:26 -07001187class PayloadSignerTest(test_utils.ReleaseToolsTestCase):
Tao Baofabe0832018-01-17 15:52:28 -08001188
1189 SIGFILE = 'sigfile.bin'
1190 SIGNED_SIGFILE = 'signed-sigfile.bin'
1191
1192 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001193 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baofabe0832018-01-17 15:52:28 -08001194 self.assertTrue(os.path.exists(self.testdata_dir))
1195
1196 common.OPTIONS.payload_signer = None
1197 common.OPTIONS.payload_signer_args = []
1198 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1199 common.OPTIONS.key_passwords = {
1200 common.OPTIONS.package_key : None,
1201 }
1202
Tao Baofabe0832018-01-17 15:52:28 -08001203 def _assertFilesEqual(self, file1, file2):
1204 with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
1205 self.assertEqual(fp1.read(), fp2.read())
1206
1207 def test_init(self):
1208 payload_signer = PayloadSigner()
1209 self.assertEqual('openssl', payload_signer.signer)
xunchang376cc7c2019-04-08 23:04:58 -07001210 self.assertEqual(256, payload_signer.key_size)
Tao Baofabe0832018-01-17 15:52:28 -08001211
1212 def test_init_withPassword(self):
1213 common.OPTIONS.package_key = os.path.join(
1214 self.testdata_dir, 'testkey_with_passwd')
1215 common.OPTIONS.key_passwords = {
1216 common.OPTIONS.package_key : 'foo',
1217 }
1218 payload_signer = PayloadSigner()
1219 self.assertEqual('openssl', payload_signer.signer)
1220
1221 def test_init_withExternalSigner(self):
1222 common.OPTIONS.payload_signer = 'abc'
1223 common.OPTIONS.payload_signer_args = ['arg1', 'arg2']
xunchang376cc7c2019-04-08 23:04:58 -07001224 common.OPTIONS.payload_signer_key_size = '512'
Tao Baofabe0832018-01-17 15:52:28 -08001225 payload_signer = PayloadSigner()
1226 self.assertEqual('abc', payload_signer.signer)
1227 self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args)
xunchang376cc7c2019-04-08 23:04:58 -07001228 self.assertEqual(512, payload_signer.key_size)
1229
1230 def test_GetKeySizeInBytes_512Bytes(self):
1231 signing_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
Tao Bao82490d32019-04-09 00:12:30 -07001232 # pylint: disable=protected-access
xunchang376cc7c2019-04-08 23:04:58 -07001233 key_size = PayloadSigner._GetKeySizeInBytes(signing_key)
1234 self.assertEqual(512, key_size)
Tao Baofabe0832018-01-17 15:52:28 -08001235
1236 def test_Sign(self):
1237 payload_signer = PayloadSigner()
1238 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1239 signed_file = payload_signer.Sign(input_file)
1240
1241 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1242 self._assertFilesEqual(verify_file, signed_file)
1243
1244 def test_Sign_withExternalSigner_openssl(self):
1245 """Uses openssl as the external payload signer."""
1246 common.OPTIONS.payload_signer = 'openssl'
1247 common.OPTIONS.payload_signer_args = [
1248 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey',
1249 os.path.join(self.testdata_dir, 'testkey.pk8'),
1250 '-pkeyopt', 'digest:sha256']
1251 payload_signer = PayloadSigner()
1252 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1253 signed_file = payload_signer.Sign(input_file)
1254
1255 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1256 self._assertFilesEqual(verify_file, signed_file)
1257
1258 def test_Sign_withExternalSigner_script(self):
1259 """Uses testdata/payload_signer.sh as the external payload signer."""
1260 common.OPTIONS.payload_signer = os.path.join(
1261 self.testdata_dir, 'payload_signer.sh')
Tao Bao30e31142019-04-09 00:12:30 -07001262 os.chmod(common.OPTIONS.payload_signer, 0o700)
Tao Baofabe0832018-01-17 15:52:28 -08001263 common.OPTIONS.payload_signer_args = [
1264 os.path.join(self.testdata_dir, 'testkey.pk8')]
1265 payload_signer = PayloadSigner()
1266 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1267 signed_file = payload_signer.Sign(input_file)
1268
1269 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1270 self._assertFilesEqual(verify_file, signed_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001271
1272
Tao Bao65b94e92018-10-11 21:57:26 -07001273class PayloadTest(test_utils.ReleaseToolsTestCase):
Tao Baoc7b403a2018-01-30 18:19:04 -08001274
1275 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001276 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoc7b403a2018-01-30 18:19:04 -08001277 self.assertTrue(os.path.exists(self.testdata_dir))
1278
1279 common.OPTIONS.wipe_user_data = False
1280 common.OPTIONS.payload_signer = None
1281 common.OPTIONS.payload_signer_args = None
1282 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1283 common.OPTIONS.key_passwords = {
1284 common.OPTIONS.package_key : None,
1285 }
1286
Tao Baoc7b403a2018-01-30 18:19:04 -08001287 @staticmethod
Tao Baof7140c02018-01-30 17:09:24 -08001288 def _create_payload_full(secondary=False):
1289 target_file = construct_target_files(secondary)
Tao Bao667ff572018-02-10 00:02:40 -08001290 payload = Payload(secondary)
Tao Baoc7b403a2018-01-30 18:19:04 -08001291 payload.Generate(target_file)
1292 return payload
1293
Tao Baof7140c02018-01-30 17:09:24 -08001294 @staticmethod
1295 def _create_payload_incremental():
1296 target_file = construct_target_files()
1297 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001298 payload = Payload()
1299 payload.Generate(target_file, source_file)
1300 return payload
1301
Tao Bao82490d32019-04-09 00:12:30 -07001302 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001303 def test_Generate_full(self):
1304 payload = self._create_payload_full()
1305 self.assertTrue(os.path.exists(payload.payload_file))
1306
Tao Bao82490d32019-04-09 00:12:30 -07001307 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001308 def test_Generate_incremental(self):
1309 payload = self._create_payload_incremental()
1310 self.assertTrue(os.path.exists(payload.payload_file))
1311
Tao Bao82490d32019-04-09 00:12:30 -07001312 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001313 def test_Generate_additionalArgs(self):
Tao Baof7140c02018-01-30 17:09:24 -08001314 target_file = construct_target_files()
1315 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001316 payload = Payload()
1317 # This should work the same as calling payload.Generate(target_file,
1318 # source_file).
1319 payload.Generate(
1320 target_file, additional_args=["--source_image", source_file])
1321 self.assertTrue(os.path.exists(payload.payload_file))
1322
Tao Bao82490d32019-04-09 00:12:30 -07001323 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001324 def test_Generate_invalidInput(self):
Tao Baof7140c02018-01-30 17:09:24 -08001325 target_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001326 common.ZipDelete(target_file, 'IMAGES/vendor.img')
1327 payload = Payload()
Tao Baobec89c12018-10-15 11:53:28 -07001328 self.assertRaises(common.ExternalError, payload.Generate, target_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001329
Tao Bao82490d32019-04-09 00:12:30 -07001330 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001331 def test_Sign_full(self):
1332 payload = self._create_payload_full()
1333 payload.Sign(PayloadSigner())
1334
1335 output_file = common.MakeTempFile(suffix='.zip')
1336 with zipfile.ZipFile(output_file, 'w') as output_zip:
1337 payload.WriteToZip(output_zip)
1338
1339 import check_ota_package_signature
1340 check_ota_package_signature.VerifyAbOtaPayload(
1341 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1342 output_file)
1343
Tao Bao82490d32019-04-09 00:12:30 -07001344 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001345 def test_Sign_incremental(self):
1346 payload = self._create_payload_incremental()
1347 payload.Sign(PayloadSigner())
1348
1349 output_file = common.MakeTempFile(suffix='.zip')
1350 with zipfile.ZipFile(output_file, 'w') as output_zip:
1351 payload.WriteToZip(output_zip)
1352
1353 import check_ota_package_signature
1354 check_ota_package_signature.VerifyAbOtaPayload(
1355 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1356 output_file)
1357
Tao Bao82490d32019-04-09 00:12:30 -07001358 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001359 def test_Sign_withDataWipe(self):
1360 common.OPTIONS.wipe_user_data = True
1361 payload = self._create_payload_full()
1362 payload.Sign(PayloadSigner())
1363
1364 with open(payload.payload_properties) as properties_fp:
1365 self.assertIn("POWERWASH=1", properties_fp.read())
1366
Tao Bao82490d32019-04-09 00:12:30 -07001367 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao667ff572018-02-10 00:02:40 -08001368 def test_Sign_secondary(self):
1369 payload = self._create_payload_full(secondary=True)
1370 payload.Sign(PayloadSigner())
1371
1372 with open(payload.payload_properties) as properties_fp:
1373 self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read())
1374
Tao Bao82490d32019-04-09 00:12:30 -07001375 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001376 def test_Sign_badSigner(self):
1377 """Tests that signing failure can be captured."""
1378 payload = self._create_payload_full()
1379 payload_signer = PayloadSigner()
1380 payload_signer.signer_args.append('bad-option')
Tao Baobec89c12018-10-15 11:53:28 -07001381 self.assertRaises(common.ExternalError, payload.Sign, payload_signer)
Tao Baoc7b403a2018-01-30 18:19:04 -08001382
Tao Bao82490d32019-04-09 00:12:30 -07001383 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001384 def test_WriteToZip(self):
1385 payload = self._create_payload_full()
1386 payload.Sign(PayloadSigner())
1387
1388 output_file = common.MakeTempFile(suffix='.zip')
1389 with zipfile.ZipFile(output_file, 'w') as output_zip:
1390 payload.WriteToZip(output_zip)
1391
1392 with zipfile.ZipFile(output_file) as verify_zip:
1393 # First make sure we have the essential entries.
1394 namelist = verify_zip.namelist()
1395 self.assertIn(Payload.PAYLOAD_BIN, namelist)
1396 self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist)
1397
1398 # Then assert these entries are stored.
1399 for entry_info in verify_zip.infolist():
1400 if entry_info.filename not in (Payload.PAYLOAD_BIN,
1401 Payload.PAYLOAD_PROPERTIES_TXT):
1402 continue
1403 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)
1404
Tao Bao82490d32019-04-09 00:12:30 -07001405 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001406 def test_WriteToZip_unsignedPayload(self):
1407 """Unsigned payloads should not be allowed to be written to zip."""
1408 payload = self._create_payload_full()
1409
1410 output_file = common.MakeTempFile(suffix='.zip')
1411 with zipfile.ZipFile(output_file, 'w') as output_zip:
1412 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
1413
1414 # Also test with incremental payload.
1415 payload = self._create_payload_incremental()
1416
1417 output_file = common.MakeTempFile(suffix='.zip')
1418 with zipfile.ZipFile(output_file, 'w') as output_zip:
1419 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001420
Tao Bao82490d32019-04-09 00:12:30 -07001421 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baof7140c02018-01-30 17:09:24 -08001422 def test_WriteToZip_secondary(self):
1423 payload = self._create_payload_full(secondary=True)
1424 payload.Sign(PayloadSigner())
1425
1426 output_file = common.MakeTempFile(suffix='.zip')
1427 with zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Bao667ff572018-02-10 00:02:40 -08001428 payload.WriteToZip(output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001429
1430 with zipfile.ZipFile(output_file) as verify_zip:
1431 # First make sure we have the essential entries.
1432 namelist = verify_zip.namelist()
1433 self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist)
1434 self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist)
1435
1436 # Then assert these entries are stored.
1437 for entry_info in verify_zip.infolist():
1438 if entry_info.filename not in (
1439 Payload.SECONDARY_PAYLOAD_BIN,
1440 Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT):
1441 continue
1442 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)