blob: 0846d87156fe9f5eb9e4b5b5ebd00603aee2134d [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()
Tao Baodf3a48b2018-01-10 16:30:43 -0800428
429 def test_GetPackageMetadata_abOta_full(self):
430 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
431 target_info_dict['ab_update'] = 'true'
432 target_info = BuildInfo(target_info_dict, None)
433 metadata = GetPackageMetadata(target_info)
434 self.assertDictEqual(
435 {
436 'ota-type' : 'AB',
437 'ota-required-cache' : '0',
438 'post-build' : 'build-fingerprint-target',
439 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800440 'post-sdk-level' : '27',
441 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800442 'post-timestamp' : '1500000000',
443 'pre-device' : 'product-device',
444 },
445 metadata)
446
447 def test_GetPackageMetadata_abOta_incremental(self):
448 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
449 target_info_dict['ab_update'] = 'true'
450 target_info = BuildInfo(target_info_dict, None)
451 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
452 common.OPTIONS.incremental_source = ''
453 metadata = GetPackageMetadata(target_info, source_info)
454 self.assertDictEqual(
455 {
456 'ota-type' : 'AB',
457 'ota-required-cache' : '0',
458 'post-build' : 'build-fingerprint-target',
459 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800460 'post-sdk-level' : '27',
461 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800462 'post-timestamp' : '1500000000',
463 'pre-device' : 'product-device',
464 'pre-build' : 'build-fingerprint-source',
465 'pre-build-incremental' : 'build-version-incremental-source',
466 },
467 metadata)
468
469 def test_GetPackageMetadata_nonAbOta_full(self):
470 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
471 metadata = GetPackageMetadata(target_info)
472 self.assertDictEqual(
473 {
474 'ota-type' : 'BLOCK',
475 'post-build' : 'build-fingerprint-target',
476 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800477 'post-sdk-level' : '27',
478 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800479 'post-timestamp' : '1500000000',
480 'pre-device' : 'product-device',
481 },
482 metadata)
483
484 def test_GetPackageMetadata_nonAbOta_incremental(self):
485 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
486 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
487 common.OPTIONS.incremental_source = ''
488 metadata = GetPackageMetadata(target_info, source_info)
489 self.assertDictEqual(
490 {
491 'ota-type' : 'BLOCK',
492 'post-build' : 'build-fingerprint-target',
493 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800494 'post-sdk-level' : '27',
495 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800496 'post-timestamp' : '1500000000',
497 'pre-device' : 'product-device',
498 'pre-build' : 'build-fingerprint-source',
499 'pre-build-incremental' : 'build-version-incremental-source',
500 },
501 metadata)
502
503 def test_GetPackageMetadata_wipe(self):
504 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
505 common.OPTIONS.wipe_user_data = True
506 metadata = GetPackageMetadata(target_info)
507 self.assertDictEqual(
508 {
509 'ota-type' : 'BLOCK',
510 'ota-wipe' : 'yes',
511 'post-build' : 'build-fingerprint-target',
512 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800513 'post-sdk-level' : '27',
514 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800515 'post-timestamp' : '1500000000',
516 'pre-device' : 'product-device',
517 },
518 metadata)
519
Tao Bao393eeb42019-03-06 16:00:38 -0800520 def test_GetPackageMetadata_retrofitDynamicPartitions(self):
521 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
522 common.OPTIONS.retrofit_dynamic_partitions = True
523 metadata = GetPackageMetadata(target_info)
524 self.assertDictEqual(
525 {
526 'ota-retrofit-dynamic-partitions' : 'yes',
527 'ota-type' : 'BLOCK',
528 'post-build' : 'build-fingerprint-target',
529 'post-build-incremental' : 'build-version-incremental-target',
530 'post-sdk-level' : '27',
531 'post-security-patch-level' : '2017-12-01',
532 'post-timestamp' : '1500000000',
533 'pre-device' : 'product-device',
534 },
535 metadata)
536
Tao Baodf3a48b2018-01-10 16:30:43 -0800537 @staticmethod
538 def _test_GetPackageMetadata_swapBuildTimestamps(target_info, source_info):
539 (target_info['build.prop']['ro.build.date.utc'],
540 source_info['build.prop']['ro.build.date.utc']) = (
541 source_info['build.prop']['ro.build.date.utc'],
542 target_info['build.prop']['ro.build.date.utc'])
543
544 def test_GetPackageMetadata_unintentionalDowngradeDetected(self):
545 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
546 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
547 self._test_GetPackageMetadata_swapBuildTimestamps(
548 target_info_dict, source_info_dict)
549
550 target_info = BuildInfo(target_info_dict, None)
551 source_info = BuildInfo(source_info_dict, None)
552 common.OPTIONS.incremental_source = ''
553 self.assertRaises(RuntimeError, GetPackageMetadata, target_info,
554 source_info)
555
556 def test_GetPackageMetadata_downgrade(self):
557 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
558 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
559 self._test_GetPackageMetadata_swapBuildTimestamps(
560 target_info_dict, source_info_dict)
561
562 target_info = BuildInfo(target_info_dict, None)
563 source_info = BuildInfo(source_info_dict, None)
564 common.OPTIONS.incremental_source = ''
565 common.OPTIONS.downgrade = True
566 common.OPTIONS.wipe_user_data = True
567 metadata = GetPackageMetadata(target_info, source_info)
568 self.assertDictEqual(
569 {
570 'ota-downgrade' : 'yes',
571 'ota-type' : 'BLOCK',
572 'ota-wipe' : 'yes',
573 'post-build' : 'build-fingerprint-target',
574 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800575 'post-sdk-level' : '27',
576 'post-security-patch-level' : '2017-12-01',
Tao Baofaa8e0b2018-04-12 14:31:43 -0700577 'post-timestamp' : '1400000000',
Tao Baodf3a48b2018-01-10 16:30:43 -0800578 'pre-device' : 'product-device',
579 'pre-build' : 'build-fingerprint-source',
580 'pre-build-incremental' : 'build-version-incremental-source',
581 },
582 metadata)
Tao Baofabe0832018-01-17 15:52:28 -0800583
Tao Bao82490d32019-04-09 00:12:30 -0700584 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baof7140c02018-01-30 17:09:24 -0800585 def test_GetTargetFilesZipForSecondaryImages(self):
586 input_file = construct_target_files(secondary=True)
587 target_file = GetTargetFilesZipForSecondaryImages(input_file)
588
589 with zipfile.ZipFile(target_file) as verify_zip:
590 namelist = verify_zip.namelist()
Tianjie Xu1c808002019-09-11 00:29:26 -0700591 ab_partitions = verify_zip.read('META/ab_partitions.txt')
Tao Baof7140c02018-01-30 17:09:24 -0800592
593 self.assertIn('META/ab_partitions.txt', namelist)
594 self.assertIn('IMAGES/boot.img', namelist)
595 self.assertIn('IMAGES/system.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700596 self.assertIn('RADIO/bootloader.img', namelist)
597 self.assertIn('RADIO/modem.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800598 self.assertIn(POSTINSTALL_CONFIG, namelist)
Tao Baof7140c02018-01-30 17:09:24 -0800599
600 self.assertNotIn('IMAGES/system_other.img', namelist)
601 self.assertNotIn('IMAGES/system.map', namelist)
602
Tianjie Xu1c808002019-09-11 00:29:26 -0700603 expected_ab_partitions = ['boot', 'system', 'bootloader', 'modem']
604 self.assertEqual('\n'.join(expected_ab_partitions), ab_partitions)
605
Tao Bao82490d32019-04-09 00:12:30 -0700606 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao15a146a2018-02-21 16:06:59 -0800607 def test_GetTargetFilesZipForSecondaryImages_skipPostinstall(self):
608 input_file = construct_target_files(secondary=True)
609 target_file = GetTargetFilesZipForSecondaryImages(
610 input_file, skip_postinstall=True)
611
612 with zipfile.ZipFile(target_file) as verify_zip:
613 namelist = verify_zip.namelist()
614
615 self.assertIn('META/ab_partitions.txt', namelist)
616 self.assertIn('IMAGES/boot.img', namelist)
617 self.assertIn('IMAGES/system.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700618 self.assertIn('RADIO/bootloader.img', namelist)
619 self.assertIn('RADIO/modem.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800620
621 self.assertNotIn('IMAGES/system_other.img', namelist)
622 self.assertNotIn('IMAGES/system.map', namelist)
623 self.assertNotIn(POSTINSTALL_CONFIG, namelist)
624
Tao Bao82490d32019-04-09 00:12:30 -0700625 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao12489802018-07-12 14:47:38 -0700626 def test_GetTargetFilesZipForSecondaryImages_withoutRadioImages(self):
627 input_file = construct_target_files(secondary=True)
628 common.ZipDelete(input_file, 'RADIO/bootloader.img')
629 common.ZipDelete(input_file, 'RADIO/modem.img')
630 target_file = GetTargetFilesZipForSecondaryImages(input_file)
631
632 with zipfile.ZipFile(target_file) as verify_zip:
633 namelist = verify_zip.namelist()
634
635 self.assertIn('META/ab_partitions.txt', namelist)
636 self.assertIn('IMAGES/boot.img', namelist)
637 self.assertIn('IMAGES/system.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700638 self.assertIn(POSTINSTALL_CONFIG, namelist)
639
640 self.assertNotIn('IMAGES/system_other.img', namelist)
641 self.assertNotIn('IMAGES/system.map', namelist)
642 self.assertNotIn('RADIO/bootloader.img', namelist)
643 self.assertNotIn('RADIO/modem.img', namelist)
644
Tao Bao82490d32019-04-09 00:12:30 -0700645 @test_utils.SkipIfExternalToolsUnavailable()
Tianjie Xu1c808002019-09-11 00:29:26 -0700646 def test_GetTargetFilesZipForSecondaryImages_dynamicPartitions(self):
647 input_file = construct_target_files(secondary=True)
648 misc_info = '\n'.join([
649 'use_dynamic_partition_size=true',
650 'use_dynamic_partitions=true',
651 'dynamic_partition_list=system vendor product',
652 'super_partition_groups=google_dynamic_partitions',
653 'super_google_dynamic_partitions_group_size=4873781248',
654 'super_google_dynamic_partitions_partition_list=system vendor product',
655 ])
656 dynamic_partitions_info = '\n'.join([
657 'super_partition_groups=google_dynamic_partitions',
658 'super_google_dynamic_partitions_group_size=4873781248',
659 'super_google_dynamic_partitions_partition_list=system vendor product',
660 ])
661
662 with zipfile.ZipFile(input_file, 'a') as append_zip:
663 common.ZipWriteStr(append_zip, 'META/misc_info.txt', misc_info)
664 common.ZipWriteStr(append_zip, 'META/dynamic_partitions_info.txt',
665 dynamic_partitions_info)
666
667 target_file = GetTargetFilesZipForSecondaryImages(input_file)
668
669 with zipfile.ZipFile(target_file) as verify_zip:
670 namelist = verify_zip.namelist()
671 updated_misc_info = verify_zip.read('META/misc_info.txt')
672 updated_dynamic_partitions_info = verify_zip.read(
673 'META/dynamic_partitions_info.txt')
674
675 self.assertIn('META/ab_partitions.txt', namelist)
676 self.assertIn('IMAGES/boot.img', namelist)
677 self.assertIn('IMAGES/system.img', namelist)
678 self.assertIn(POSTINSTALL_CONFIG, namelist)
679 self.assertIn('META/misc_info.txt', namelist)
680 self.assertIn('META/dynamic_partitions_info.txt', namelist)
681
682 self.assertNotIn('IMAGES/system_other.img', namelist)
683 self.assertNotIn('IMAGES/system.map', namelist)
684
685 # Check the vendor & product are removed from the partitions list.
686 expected_misc_info = misc_info.replace('system vendor product',
687 'system')
688 expected_dynamic_partitions_info = dynamic_partitions_info.replace(
689 'system vendor product', 'system')
690 self.assertEqual(expected_misc_info, updated_misc_info)
691 self.assertEqual(expected_dynamic_partitions_info,
692 updated_dynamic_partitions_info)
693
694 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao15a146a2018-02-21 16:06:59 -0800695 def test_GetTargetFilesZipWithoutPostinstallConfig(self):
696 input_file = construct_target_files()
697 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
698 with zipfile.ZipFile(target_file) as verify_zip:
699 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
700
Tao Bao82490d32019-04-09 00:12:30 -0700701 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao15a146a2018-02-21 16:06:59 -0800702 def test_GetTargetFilesZipWithoutPostinstallConfig_missingEntry(self):
703 input_file = construct_target_files()
704 common.ZipDelete(input_file, POSTINSTALL_CONFIG)
705 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
706 with zipfile.ZipFile(target_file) as verify_zip:
707 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
708
Tao Bao3bf8c652018-03-16 12:59:42 -0700709 def _test_FinalizeMetadata(self, large_entry=False):
710 entries = [
711 'required-entry1',
712 'required-entry2',
713 ]
714 zip_file = PropertyFilesTest.construct_zip_package(entries)
715 # Add a large entry of 1 GiB if requested.
716 if large_entry:
717 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
718 zip_fp.writestr(
719 # Using 'zoo' so that the entry stays behind others after signing.
720 'zoo',
721 'A' * 1024 * 1024 * 1024,
722 zipfile.ZIP_STORED)
723
724 metadata = {}
725 output_file = common.MakeTempFile(suffix='.zip')
726 needed_property_files = (
727 TestPropertyFiles(),
728 )
729 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
730 self.assertIn('ota-test-property-files', metadata)
731
Tao Bao82490d32019-04-09 00:12:30 -0700732 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700733 def test_FinalizeMetadata(self):
734 self._test_FinalizeMetadata()
735
Tao Bao82490d32019-04-09 00:12:30 -0700736 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700737 def test_FinalizeMetadata_withNoSigning(self):
738 common.OPTIONS.no_signing = True
739 self._test_FinalizeMetadata()
740
Tao Bao82490d32019-04-09 00:12:30 -0700741 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700742 def test_FinalizeMetadata_largeEntry(self):
743 self._test_FinalizeMetadata(large_entry=True)
744
Tao Bao82490d32019-04-09 00:12:30 -0700745 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700746 def test_FinalizeMetadata_largeEntry_withNoSigning(self):
747 common.OPTIONS.no_signing = True
748 self._test_FinalizeMetadata(large_entry=True)
749
Tao Bao82490d32019-04-09 00:12:30 -0700750 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao3bf8c652018-03-16 12:59:42 -0700751 def test_FinalizeMetadata_insufficientSpace(self):
752 entries = [
753 'required-entry1',
754 'required-entry2',
755 'optional-entry1',
756 'optional-entry2',
757 ]
758 zip_file = PropertyFilesTest.construct_zip_package(entries)
759 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
760 zip_fp.writestr(
761 # 'foo-entry1' will appear ahead of all other entries (in alphabetical
762 # order) after the signing, which will in turn trigger the
763 # InsufficientSpaceException and an automatic retry.
764 'foo-entry1',
765 'A' * 1024 * 1024,
766 zipfile.ZIP_STORED)
767
768 metadata = {}
769 needed_property_files = (
770 TestPropertyFiles(),
771 )
772 output_file = common.MakeTempFile(suffix='.zip')
773 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
774 self.assertIn('ota-test-property-files', metadata)
775
Tao Baoae5e4c32018-03-01 19:30:00 -0800776
Tao Bao69203522018-03-08 16:09:01 -0800777class TestPropertyFiles(PropertyFiles):
778 """A class that extends PropertyFiles for testing purpose."""
779
780 def __init__(self):
781 super(TestPropertyFiles, self).__init__()
782 self.name = 'ota-test-property-files'
783 self.required = (
784 'required-entry1',
785 'required-entry2',
786 )
787 self.optional = (
788 'optional-entry1',
789 'optional-entry2',
790 )
791
792
Tao Bao65b94e92018-10-11 21:57:26 -0700793class PropertyFilesTest(test_utils.ReleaseToolsTestCase):
Tao Baoae5e4c32018-03-01 19:30:00 -0800794
Tao Bao3bf8c652018-03-16 12:59:42 -0700795 def setUp(self):
796 common.OPTIONS.no_signing = False
797
Tao Baof5110492018-03-02 09:47:43 -0800798 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -0700799 def construct_zip_package(entries):
Tao Baof5110492018-03-02 09:47:43 -0800800 zip_file = common.MakeTempFile(suffix='.zip')
801 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
802 for entry in entries:
803 zip_fp.writestr(
804 entry,
805 entry.replace('.', '-').upper(),
806 zipfile.ZIP_STORED)
807 return zip_file
808
809 @staticmethod
Tao Bao69203522018-03-08 16:09:01 -0800810 def _parse_property_files_string(data):
Tao Baof5110492018-03-02 09:47:43 -0800811 result = {}
812 for token in data.split(','):
813 name, info = token.split(':', 1)
814 result[name] = info
815 return result
816
817 def _verify_entries(self, input_file, tokens, entries):
818 for entry in entries:
819 offset, size = map(int, tokens[entry].split(':'))
820 with open(input_file, 'rb') as input_fp:
821 input_fp.seek(offset)
822 if entry == 'metadata':
823 expected = b'META-INF/COM/ANDROID/METADATA'
824 else:
825 expected = entry.replace('.', '-').upper().encode()
826 self.assertEqual(expected, input_fp.read(size))
827
Tao Bao82490d32019-04-09 00:12:30 -0700828 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoae5e4c32018-03-01 19:30:00 -0800829 def test_Compute(self):
Tao Baof5110492018-03-02 09:47:43 -0800830 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800831 'required-entry1',
832 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800833 )
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:
Tao Bao69203522018-03-08 16:09:01 -0800837 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800838
Tao Bao69203522018-03-08 16:09:01 -0800839 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800840 self.assertEqual(3, len(tokens))
841 self._verify_entries(zip_file, tokens, entries)
842
Tao Bao69203522018-03-08 16:09:01 -0800843 def test_Compute_withOptionalEntries(self):
Tao Baof5110492018-03-02 09:47:43 -0800844 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800845 'required-entry1',
846 'required-entry2',
847 'optional-entry1',
848 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800849 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700850 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800851 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800852 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800853 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800854
Tao Bao69203522018-03-08 16:09:01 -0800855 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800856 self.assertEqual(5, len(tokens))
857 self._verify_entries(zip_file, tokens, entries)
858
Tao Bao69203522018-03-08 16:09:01 -0800859 def test_Compute_missingRequiredEntry(self):
860 entries = (
861 'required-entry2',
862 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700863 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800864 property_files = TestPropertyFiles()
865 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
866 self.assertRaises(KeyError, property_files.Compute, zip_fp)
867
Tao Bao82490d32019-04-09 00:12:30 -0700868 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoae5e4c32018-03-01 19:30:00 -0800869 def test_Finalize(self):
Tao Baof5110492018-03-02 09:47:43 -0800870 entries = [
Tao Bao69203522018-03-08 16:09:01 -0800871 'required-entry1',
872 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800873 'META-INF/com/android/metadata',
874 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700875 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800876 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800877 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700878 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800879 zip_fp, reserve_space=False)
880 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
Tao Bao69203522018-03-08 16:09:01 -0800881 tokens = self._parse_property_files_string(streaming_metadata)
Tao Baof5110492018-03-02 09:47:43 -0800882
883 self.assertEqual(3, len(tokens))
884 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
885 # streaming metadata.
886 entries[2] = 'metadata'
887 self._verify_entries(zip_file, tokens, entries)
888
Tao Bao82490d32019-04-09 00:12:30 -0700889 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoae5e4c32018-03-01 19:30:00 -0800890 def test_Finalize_assertReservedLength(self):
Tao Baof5110492018-03-02 09:47:43 -0800891 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800892 'required-entry1',
893 'required-entry2',
894 'optional-entry1',
895 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800896 'META-INF/com/android/metadata',
897 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700898 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800899 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800900 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
901 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700902 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800903 zip_fp, reserve_space=False)
Tao Baof5110492018-03-02 09:47:43 -0800904 raw_length = len(raw_metadata)
905
906 # Now pass in the exact expected length.
Tao Baoae5e4c32018-03-01 19:30:00 -0800907 streaming_metadata = property_files.Finalize(zip_fp, raw_length)
Tao Baof5110492018-03-02 09:47:43 -0800908 self.assertEqual(raw_length, len(streaming_metadata))
909
910 # Or pass in insufficient length.
911 self.assertRaises(
Tao Bao3bf8c652018-03-16 12:59:42 -0700912 PropertyFiles.InsufficientSpaceException,
Tao Baoae5e4c32018-03-01 19:30:00 -0800913 property_files.Finalize,
Tao Baof5110492018-03-02 09:47:43 -0800914 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800915 raw_length - 1)
Tao Baof5110492018-03-02 09:47:43 -0800916
917 # Or pass in a much larger size.
Tao Baoae5e4c32018-03-01 19:30:00 -0800918 streaming_metadata = property_files.Finalize(
Tao Baof5110492018-03-02 09:47:43 -0800919 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800920 raw_length + 20)
Tao Baof5110492018-03-02 09:47:43 -0800921 self.assertEqual(raw_length + 20, len(streaming_metadata))
922 self.assertEqual(' ' * 20, streaming_metadata[raw_length:])
923
Tao Baoae5e4c32018-03-01 19:30:00 -0800924 def test_Verify(self):
925 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800926 'required-entry1',
927 'required-entry2',
928 'optional-entry1',
929 'optional-entry2',
930 'META-INF/com/android/metadata',
931 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700932 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800933 property_files = TestPropertyFiles()
934 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
935 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700936 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800937 zip_fp, reserve_space=False)
938
939 # Should pass the test if verification passes.
940 property_files.Verify(zip_fp, raw_metadata)
941
942 # Or raise on verification failure.
943 self.assertRaises(
944 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
945
946
947class StreamingPropertyFilesTest(PropertyFilesTest):
948 """Additional sanity checks specialized for StreamingPropertyFiles."""
949
950 def test_init(self):
951 property_files = StreamingPropertyFiles()
952 self.assertEqual('ota-streaming-property-files', property_files.name)
953 self.assertEqual(
954 (
955 'payload.bin',
956 'payload_properties.txt',
957 ),
958 property_files.required)
959 self.assertEqual(
960 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -0700961 'care_map.pb',
Tao Bao69203522018-03-08 16:09:01 -0800962 'care_map.txt',
963 'compatibility.zip',
964 ),
965 property_files.optional)
966
967 def test_Compute(self):
968 entries = (
Tao Baoae5e4c32018-03-01 19:30:00 -0800969 'payload.bin',
970 'payload_properties.txt',
971 'care_map.txt',
Tao Bao69203522018-03-08 16:09:01 -0800972 'compatibility.zip',
973 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700974 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800975 property_files = StreamingPropertyFiles()
976 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
977 property_files_string = property_files.Compute(zip_fp)
978
979 tokens = self._parse_property_files_string(property_files_string)
980 self.assertEqual(5, len(tokens))
981 self._verify_entries(zip_file, tokens, entries)
982
983 def test_Finalize(self):
984 entries = [
985 'payload.bin',
986 'payload_properties.txt',
987 'care_map.txt',
988 'compatibility.zip',
989 'META-INF/com/android/metadata',
990 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700991 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800992 property_files = StreamingPropertyFiles()
993 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700994 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800995 zip_fp, reserve_space=False)
996 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
997 tokens = self._parse_property_files_string(streaming_metadata)
998
999 self.assertEqual(5, len(tokens))
1000 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
1001 # streaming metadata.
1002 entries[4] = 'metadata'
1003 self._verify_entries(zip_file, tokens, entries)
1004
1005 def test_Verify(self):
1006 entries = (
1007 'payload.bin',
1008 'payload_properties.txt',
1009 'care_map.txt',
1010 'compatibility.zip',
Tao Baoae5e4c32018-03-01 19:30:00 -08001011 'META-INF/com/android/metadata',
1012 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001013 zip_file = self.construct_zip_package(entries)
Tao Baoae5e4c32018-03-01 19:30:00 -08001014 property_files = StreamingPropertyFiles()
1015 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1016 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001017 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -08001018 zip_fp, reserve_space=False)
1019
1020 # Should pass the test if verification passes.
1021 property_files.Verify(zip_fp, raw_metadata)
1022
1023 # Or raise on verification failure.
1024 self.assertRaises(
1025 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
1026
Tao Baofabe0832018-01-17 15:52:28 -08001027
Tao Baob6304672018-03-08 16:28:33 -08001028class AbOtaPropertyFilesTest(PropertyFilesTest):
1029 """Additional sanity checks specialized for AbOtaPropertyFiles."""
1030
1031 # The size for payload and metadata signature size.
1032 SIGNATURE_SIZE = 256
1033
1034 def setUp(self):
1035 self.testdata_dir = test_utils.get_testdata_dir()
1036 self.assertTrue(os.path.exists(self.testdata_dir))
1037
1038 common.OPTIONS.wipe_user_data = False
1039 common.OPTIONS.payload_signer = None
1040 common.OPTIONS.payload_signer_args = None
1041 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1042 common.OPTIONS.key_passwords = {
1043 common.OPTIONS.package_key : None,
1044 }
1045
1046 def test_init(self):
1047 property_files = AbOtaPropertyFiles()
1048 self.assertEqual('ota-property-files', property_files.name)
1049 self.assertEqual(
1050 (
1051 'payload.bin',
1052 'payload_properties.txt',
1053 ),
1054 property_files.required)
1055 self.assertEqual(
1056 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -07001057 'care_map.pb',
Tao Baob6304672018-03-08 16:28:33 -08001058 'care_map.txt',
1059 'compatibility.zip',
1060 ),
1061 property_files.optional)
1062
Tao Bao82490d32019-04-09 00:12:30 -07001063 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baob6304672018-03-08 16:28:33 -08001064 def test_GetPayloadMetadataOffsetAndSize(self):
1065 target_file = construct_target_files()
1066 payload = Payload()
1067 payload.Generate(target_file)
1068
1069 payload_signer = PayloadSigner()
1070 payload.Sign(payload_signer)
1071
1072 output_file = common.MakeTempFile(suffix='.zip')
1073 with zipfile.ZipFile(output_file, 'w') as output_zip:
1074 payload.WriteToZip(output_zip)
1075
1076 # Find out the payload metadata offset and size.
1077 property_files = AbOtaPropertyFiles()
1078 with zipfile.ZipFile(output_file) as input_zip:
1079 # pylint: disable=protected-access
1080 payload_offset, metadata_total = (
1081 property_files._GetPayloadMetadataOffsetAndSize(input_zip))
1082
1083 # Read in the metadata signature directly.
1084 with open(output_file, 'rb') as verify_fp:
1085 verify_fp.seek(payload_offset + metadata_total - self.SIGNATURE_SIZE)
1086 metadata_signature = verify_fp.read(self.SIGNATURE_SIZE)
1087
1088 # Now we extract the metadata hash via brillo_update_payload script, which
1089 # will serve as the oracle result.
1090 payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1091 metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1092 cmd = ['brillo_update_payload', 'hash',
1093 '--unsigned_payload', payload.payload_file,
1094 '--signature_size', str(self.SIGNATURE_SIZE),
1095 '--metadata_hash_file', metadata_sig_file,
1096 '--payload_hash_file', payload_sig_file]
Tao Bao73dd4f42018-10-04 16:25:33 -07001097 proc = common.Run(cmd)
Tao Baob6304672018-03-08 16:28:33 -08001098 stdoutdata, _ = proc.communicate()
1099 self.assertEqual(
1100 0, proc.returncode,
Tao Bao73dd4f42018-10-04 16:25:33 -07001101 'Failed to run brillo_update_payload:\n{}'.format(stdoutdata))
Tao Baob6304672018-03-08 16:28:33 -08001102
1103 signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file)
1104
1105 # Finally we can compare the two signatures.
1106 with open(signed_metadata_sig_file, 'rb') as verify_fp:
1107 self.assertEqual(verify_fp.read(), metadata_signature)
1108
1109 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -07001110 def construct_zip_package_withValidPayload(with_metadata=False):
1111 # Cannot use construct_zip_package() since we need a "valid" payload.bin.
Tao Baob6304672018-03-08 16:28:33 -08001112 target_file = construct_target_files()
1113 payload = Payload()
1114 payload.Generate(target_file)
1115
1116 payload_signer = PayloadSigner()
1117 payload.Sign(payload_signer)
1118
1119 zip_file = common.MakeTempFile(suffix='.zip')
1120 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
1121 # 'payload.bin',
1122 payload.WriteToZip(zip_fp)
1123
1124 # Other entries.
1125 entries = ['care_map.txt', 'compatibility.zip']
1126
1127 # Put META-INF/com/android/metadata if needed.
1128 if with_metadata:
1129 entries.append('META-INF/com/android/metadata')
1130
1131 for entry in entries:
1132 zip_fp.writestr(
1133 entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED)
1134
1135 return zip_file
1136
Tao Bao82490d32019-04-09 00:12:30 -07001137 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baob6304672018-03-08 16:28:33 -08001138 def test_Compute(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001139 zip_file = self.construct_zip_package_withValidPayload()
Tao Baob6304672018-03-08 16:28:33 -08001140 property_files = AbOtaPropertyFiles()
1141 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1142 property_files_string = property_files.Compute(zip_fp)
1143
1144 tokens = self._parse_property_files_string(property_files_string)
1145 # "6" indcludes the four entries above, one metadata entry, and one entry
1146 # for payload-metadata.bin.
1147 self.assertEqual(6, len(tokens))
1148 self._verify_entries(
1149 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1150
Tao Bao82490d32019-04-09 00:12:30 -07001151 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baob6304672018-03-08 16:28:33 -08001152 def test_Finalize(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001153 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001154 property_files = AbOtaPropertyFiles()
1155 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001156 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001157 zip_fp, reserve_space=False)
1158 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1159
1160 tokens = self._parse_property_files_string(property_files_string)
1161 # "6" indcludes the four entries above, one metadata entry, and one entry
1162 # for payload-metadata.bin.
1163 self.assertEqual(6, len(tokens))
1164 self._verify_entries(
1165 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1166
Tao Bao82490d32019-04-09 00:12:30 -07001167 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baob6304672018-03-08 16:28:33 -08001168 def test_Verify(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001169 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001170 property_files = AbOtaPropertyFiles()
1171 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001172 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001173 zip_fp, reserve_space=False)
1174
1175 property_files.Verify(zip_fp, raw_metadata)
1176
1177
Tao Baoc0746f42018-02-21 13:17:22 -08001178class NonAbOtaPropertyFilesTest(PropertyFilesTest):
1179 """Additional sanity checks specialized for NonAbOtaPropertyFiles."""
1180
1181 def test_init(self):
1182 property_files = NonAbOtaPropertyFiles()
1183 self.assertEqual('ota-property-files', property_files.name)
1184 self.assertEqual((), property_files.required)
1185 self.assertEqual((), property_files.optional)
1186
1187 def test_Compute(self):
1188 entries = ()
Tao Bao3bf8c652018-03-16 12:59:42 -07001189 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001190 property_files = NonAbOtaPropertyFiles()
1191 with zipfile.ZipFile(zip_file) as zip_fp:
1192 property_files_string = property_files.Compute(zip_fp)
1193
1194 tokens = self._parse_property_files_string(property_files_string)
1195 self.assertEqual(1, len(tokens))
1196 self._verify_entries(zip_file, tokens, entries)
1197
1198 def test_Finalize(self):
1199 entries = [
1200 'META-INF/com/android/metadata',
1201 ]
Tao Bao3bf8c652018-03-16 12:59:42 -07001202 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001203 property_files = NonAbOtaPropertyFiles()
1204 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001205 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001206 zip_fp, reserve_space=False)
1207 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1208 tokens = self._parse_property_files_string(property_files_string)
1209
1210 self.assertEqual(1, len(tokens))
1211 # 'META-INF/com/android/metadata' will be key'd as 'metadata'.
1212 entries[0] = 'metadata'
1213 self._verify_entries(zip_file, tokens, entries)
1214
1215 def test_Verify(self):
1216 entries = (
1217 'META-INF/com/android/metadata',
1218 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001219 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001220 property_files = NonAbOtaPropertyFiles()
1221 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001222 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001223 zip_fp, reserve_space=False)
1224
1225 property_files.Verify(zip_fp, raw_metadata)
1226
1227
Tao Bao65b94e92018-10-11 21:57:26 -07001228class PayloadSignerTest(test_utils.ReleaseToolsTestCase):
Tao Baofabe0832018-01-17 15:52:28 -08001229
1230 SIGFILE = 'sigfile.bin'
1231 SIGNED_SIGFILE = 'signed-sigfile.bin'
1232
1233 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001234 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baofabe0832018-01-17 15:52:28 -08001235 self.assertTrue(os.path.exists(self.testdata_dir))
1236
1237 common.OPTIONS.payload_signer = None
1238 common.OPTIONS.payload_signer_args = []
1239 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1240 common.OPTIONS.key_passwords = {
1241 common.OPTIONS.package_key : None,
1242 }
1243
Tao Baofabe0832018-01-17 15:52:28 -08001244 def _assertFilesEqual(self, file1, file2):
1245 with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
1246 self.assertEqual(fp1.read(), fp2.read())
1247
1248 def test_init(self):
1249 payload_signer = PayloadSigner()
1250 self.assertEqual('openssl', payload_signer.signer)
xunchang376cc7c2019-04-08 23:04:58 -07001251 self.assertEqual(256, payload_signer.key_size)
Tao Baofabe0832018-01-17 15:52:28 -08001252
1253 def test_init_withPassword(self):
1254 common.OPTIONS.package_key = os.path.join(
1255 self.testdata_dir, 'testkey_with_passwd')
1256 common.OPTIONS.key_passwords = {
1257 common.OPTIONS.package_key : 'foo',
1258 }
1259 payload_signer = PayloadSigner()
1260 self.assertEqual('openssl', payload_signer.signer)
1261
1262 def test_init_withExternalSigner(self):
1263 common.OPTIONS.payload_signer = 'abc'
1264 common.OPTIONS.payload_signer_args = ['arg1', 'arg2']
xunchang376cc7c2019-04-08 23:04:58 -07001265 common.OPTIONS.payload_signer_key_size = '512'
Tao Baofabe0832018-01-17 15:52:28 -08001266 payload_signer = PayloadSigner()
1267 self.assertEqual('abc', payload_signer.signer)
1268 self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args)
xunchang376cc7c2019-04-08 23:04:58 -07001269 self.assertEqual(512, payload_signer.key_size)
1270
1271 def test_GetKeySizeInBytes_512Bytes(self):
1272 signing_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
Tao Bao82490d32019-04-09 00:12:30 -07001273 # pylint: disable=protected-access
xunchang376cc7c2019-04-08 23:04:58 -07001274 key_size = PayloadSigner._GetKeySizeInBytes(signing_key)
1275 self.assertEqual(512, key_size)
Tao Baofabe0832018-01-17 15:52:28 -08001276
1277 def test_Sign(self):
1278 payload_signer = PayloadSigner()
1279 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1280 signed_file = payload_signer.Sign(input_file)
1281
1282 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1283 self._assertFilesEqual(verify_file, signed_file)
1284
1285 def test_Sign_withExternalSigner_openssl(self):
1286 """Uses openssl as the external payload signer."""
1287 common.OPTIONS.payload_signer = 'openssl'
1288 common.OPTIONS.payload_signer_args = [
1289 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey',
1290 os.path.join(self.testdata_dir, 'testkey.pk8'),
1291 '-pkeyopt', 'digest:sha256']
1292 payload_signer = PayloadSigner()
1293 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1294 signed_file = payload_signer.Sign(input_file)
1295
1296 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1297 self._assertFilesEqual(verify_file, signed_file)
1298
1299 def test_Sign_withExternalSigner_script(self):
1300 """Uses testdata/payload_signer.sh as the external payload signer."""
1301 common.OPTIONS.payload_signer = os.path.join(
1302 self.testdata_dir, 'payload_signer.sh')
Tao Bao30e31142019-04-09 00:12:30 -07001303 os.chmod(common.OPTIONS.payload_signer, 0o700)
Tao Baofabe0832018-01-17 15:52:28 -08001304 common.OPTIONS.payload_signer_args = [
1305 os.path.join(self.testdata_dir, 'testkey.pk8')]
1306 payload_signer = PayloadSigner()
1307 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1308 signed_file = payload_signer.Sign(input_file)
1309
1310 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1311 self._assertFilesEqual(verify_file, signed_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001312
1313
Tao Bao65b94e92018-10-11 21:57:26 -07001314class PayloadTest(test_utils.ReleaseToolsTestCase):
Tao Baoc7b403a2018-01-30 18:19:04 -08001315
1316 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001317 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoc7b403a2018-01-30 18:19:04 -08001318 self.assertTrue(os.path.exists(self.testdata_dir))
1319
1320 common.OPTIONS.wipe_user_data = False
1321 common.OPTIONS.payload_signer = None
1322 common.OPTIONS.payload_signer_args = None
1323 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1324 common.OPTIONS.key_passwords = {
1325 common.OPTIONS.package_key : None,
1326 }
1327
Tao Baoc7b403a2018-01-30 18:19:04 -08001328 @staticmethod
Tao Baof7140c02018-01-30 17:09:24 -08001329 def _create_payload_full(secondary=False):
1330 target_file = construct_target_files(secondary)
Tao Bao667ff572018-02-10 00:02:40 -08001331 payload = Payload(secondary)
Tao Baoc7b403a2018-01-30 18:19:04 -08001332 payload.Generate(target_file)
1333 return payload
1334
Tao Baof7140c02018-01-30 17:09:24 -08001335 @staticmethod
1336 def _create_payload_incremental():
1337 target_file = construct_target_files()
1338 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001339 payload = Payload()
1340 payload.Generate(target_file, source_file)
1341 return payload
1342
Tao Bao82490d32019-04-09 00:12:30 -07001343 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001344 def test_Generate_full(self):
1345 payload = self._create_payload_full()
1346 self.assertTrue(os.path.exists(payload.payload_file))
1347
Tao Bao82490d32019-04-09 00:12:30 -07001348 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001349 def test_Generate_incremental(self):
1350 payload = self._create_payload_incremental()
1351 self.assertTrue(os.path.exists(payload.payload_file))
1352
Tao Bao82490d32019-04-09 00:12:30 -07001353 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001354 def test_Generate_additionalArgs(self):
Tao Baof7140c02018-01-30 17:09:24 -08001355 target_file = construct_target_files()
1356 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001357 payload = Payload()
1358 # This should work the same as calling payload.Generate(target_file,
1359 # source_file).
1360 payload.Generate(
1361 target_file, additional_args=["--source_image", source_file])
1362 self.assertTrue(os.path.exists(payload.payload_file))
1363
Tao Bao82490d32019-04-09 00:12:30 -07001364 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001365 def test_Generate_invalidInput(self):
Tao Baof7140c02018-01-30 17:09:24 -08001366 target_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001367 common.ZipDelete(target_file, 'IMAGES/vendor.img')
1368 payload = Payload()
Tao Baobec89c12018-10-15 11:53:28 -07001369 self.assertRaises(common.ExternalError, payload.Generate, target_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001370
Tao Bao82490d32019-04-09 00:12:30 -07001371 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001372 def test_Sign_full(self):
1373 payload = self._create_payload_full()
1374 payload.Sign(PayloadSigner())
1375
1376 output_file = common.MakeTempFile(suffix='.zip')
1377 with zipfile.ZipFile(output_file, 'w') as output_zip:
1378 payload.WriteToZip(output_zip)
1379
1380 import check_ota_package_signature
1381 check_ota_package_signature.VerifyAbOtaPayload(
1382 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1383 output_file)
1384
Tao Bao82490d32019-04-09 00:12:30 -07001385 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001386 def test_Sign_incremental(self):
1387 payload = self._create_payload_incremental()
1388 payload.Sign(PayloadSigner())
1389
1390 output_file = common.MakeTempFile(suffix='.zip')
1391 with zipfile.ZipFile(output_file, 'w') as output_zip:
1392 payload.WriteToZip(output_zip)
1393
1394 import check_ota_package_signature
1395 check_ota_package_signature.VerifyAbOtaPayload(
1396 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1397 output_file)
1398
Tao Bao82490d32019-04-09 00:12:30 -07001399 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001400 def test_Sign_withDataWipe(self):
1401 common.OPTIONS.wipe_user_data = True
1402 payload = self._create_payload_full()
1403 payload.Sign(PayloadSigner())
1404
1405 with open(payload.payload_properties) as properties_fp:
1406 self.assertIn("POWERWASH=1", properties_fp.read())
1407
Tao Bao82490d32019-04-09 00:12:30 -07001408 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao667ff572018-02-10 00:02:40 -08001409 def test_Sign_secondary(self):
1410 payload = self._create_payload_full(secondary=True)
1411 payload.Sign(PayloadSigner())
1412
1413 with open(payload.payload_properties) as properties_fp:
1414 self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read())
1415
Tao Bao82490d32019-04-09 00:12:30 -07001416 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001417 def test_Sign_badSigner(self):
1418 """Tests that signing failure can be captured."""
1419 payload = self._create_payload_full()
1420 payload_signer = PayloadSigner()
1421 payload_signer.signer_args.append('bad-option')
Tao Baobec89c12018-10-15 11:53:28 -07001422 self.assertRaises(common.ExternalError, payload.Sign, payload_signer)
Tao Baoc7b403a2018-01-30 18:19:04 -08001423
Tao Bao82490d32019-04-09 00:12:30 -07001424 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001425 def test_WriteToZip(self):
1426 payload = self._create_payload_full()
1427 payload.Sign(PayloadSigner())
1428
1429 output_file = common.MakeTempFile(suffix='.zip')
1430 with zipfile.ZipFile(output_file, 'w') as output_zip:
1431 payload.WriteToZip(output_zip)
1432
1433 with zipfile.ZipFile(output_file) as verify_zip:
1434 # First make sure we have the essential entries.
1435 namelist = verify_zip.namelist()
1436 self.assertIn(Payload.PAYLOAD_BIN, namelist)
1437 self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist)
1438
1439 # Then assert these entries are stored.
1440 for entry_info in verify_zip.infolist():
1441 if entry_info.filename not in (Payload.PAYLOAD_BIN,
1442 Payload.PAYLOAD_PROPERTIES_TXT):
1443 continue
1444 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)
1445
Tao Bao82490d32019-04-09 00:12:30 -07001446 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baoc7b403a2018-01-30 18:19:04 -08001447 def test_WriteToZip_unsignedPayload(self):
1448 """Unsigned payloads should not be allowed to be written to zip."""
1449 payload = self._create_payload_full()
1450
1451 output_file = common.MakeTempFile(suffix='.zip')
1452 with zipfile.ZipFile(output_file, 'w') as output_zip:
1453 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
1454
1455 # Also test with incremental payload.
1456 payload = self._create_payload_incremental()
1457
1458 output_file = common.MakeTempFile(suffix='.zip')
1459 with zipfile.ZipFile(output_file, 'w') as output_zip:
1460 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001461
Tao Bao82490d32019-04-09 00:12:30 -07001462 @test_utils.SkipIfExternalToolsUnavailable()
Tao Baof7140c02018-01-30 17:09:24 -08001463 def test_WriteToZip_secondary(self):
1464 payload = self._create_payload_full(secondary=True)
1465 payload.Sign(PayloadSigner())
1466
1467 output_file = common.MakeTempFile(suffix='.zip')
1468 with zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Bao667ff572018-02-10 00:02:40 -08001469 payload.WriteToZip(output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001470
1471 with zipfile.ZipFile(output_file) as verify_zip:
1472 # First make sure we have the essential entries.
1473 namelist = verify_zip.namelist()
1474 self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist)
1475 self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist)
1476
1477 # Then assert these entries are stored.
1478 for entry_info in verify_zip.infolist():
1479 if entry_info.filename not in (
1480 Payload.SECONDARY_PAYLOAD_BIN,
1481 Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT):
1482 continue
1483 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)