blob: 2942d611dfddbad3812cb060d133fb7fb2737a5c [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
430 def test_GetPackageMetadata_abOta_full(self):
431 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
432 target_info_dict['ab_update'] = 'true'
433 target_info = BuildInfo(target_info_dict, None)
434 metadata = GetPackageMetadata(target_info)
435 self.assertDictEqual(
436 {
437 'ota-type' : 'AB',
438 'ota-required-cache' : '0',
439 'post-build' : 'build-fingerprint-target',
440 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800441 'post-sdk-level' : '27',
442 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800443 'post-timestamp' : '1500000000',
444 'pre-device' : 'product-device',
445 },
446 metadata)
447
448 def test_GetPackageMetadata_abOta_incremental(self):
449 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
450 target_info_dict['ab_update'] = 'true'
451 target_info = BuildInfo(target_info_dict, None)
452 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
453 common.OPTIONS.incremental_source = ''
454 metadata = GetPackageMetadata(target_info, source_info)
455 self.assertDictEqual(
456 {
457 'ota-type' : 'AB',
458 'ota-required-cache' : '0',
459 'post-build' : 'build-fingerprint-target',
460 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800461 'post-sdk-level' : '27',
462 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800463 'post-timestamp' : '1500000000',
464 'pre-device' : 'product-device',
465 'pre-build' : 'build-fingerprint-source',
466 'pre-build-incremental' : 'build-version-incremental-source',
467 },
468 metadata)
469
470 def test_GetPackageMetadata_nonAbOta_full(self):
471 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
472 metadata = GetPackageMetadata(target_info)
473 self.assertDictEqual(
474 {
475 'ota-type' : 'BLOCK',
476 'post-build' : 'build-fingerprint-target',
477 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800478 'post-sdk-level' : '27',
479 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800480 'post-timestamp' : '1500000000',
481 'pre-device' : 'product-device',
482 },
483 metadata)
484
485 def test_GetPackageMetadata_nonAbOta_incremental(self):
486 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
487 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
488 common.OPTIONS.incremental_source = ''
489 metadata = GetPackageMetadata(target_info, source_info)
490 self.assertDictEqual(
491 {
492 'ota-type' : 'BLOCK',
493 'post-build' : 'build-fingerprint-target',
494 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800495 'post-sdk-level' : '27',
496 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800497 'post-timestamp' : '1500000000',
498 'pre-device' : 'product-device',
499 'pre-build' : 'build-fingerprint-source',
500 'pre-build-incremental' : 'build-version-incremental-source',
501 },
502 metadata)
503
504 def test_GetPackageMetadata_wipe(self):
505 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
506 common.OPTIONS.wipe_user_data = True
507 metadata = GetPackageMetadata(target_info)
508 self.assertDictEqual(
509 {
510 'ota-type' : 'BLOCK',
511 'ota-wipe' : 'yes',
512 'post-build' : 'build-fingerprint-target',
513 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800514 'post-sdk-level' : '27',
515 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800516 'post-timestamp' : '1500000000',
517 'pre-device' : 'product-device',
518 },
519 metadata)
520
Tao Bao393eeb42019-03-06 16:00:38 -0800521 def test_GetPackageMetadata_retrofitDynamicPartitions(self):
522 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
523 common.OPTIONS.retrofit_dynamic_partitions = True
524 metadata = GetPackageMetadata(target_info)
525 self.assertDictEqual(
526 {
527 'ota-retrofit-dynamic-partitions' : 'yes',
528 'ota-type' : 'BLOCK',
529 'post-build' : 'build-fingerprint-target',
530 'post-build-incremental' : 'build-version-incremental-target',
531 'post-sdk-level' : '27',
532 'post-security-patch-level' : '2017-12-01',
533 'post-timestamp' : '1500000000',
534 'pre-device' : 'product-device',
535 },
536 metadata)
537
Tao Baodf3a48b2018-01-10 16:30:43 -0800538 @staticmethod
539 def _test_GetPackageMetadata_swapBuildTimestamps(target_info, source_info):
540 (target_info['build.prop']['ro.build.date.utc'],
541 source_info['build.prop']['ro.build.date.utc']) = (
542 source_info['build.prop']['ro.build.date.utc'],
543 target_info['build.prop']['ro.build.date.utc'])
544
545 def test_GetPackageMetadata_unintentionalDowngradeDetected(self):
546 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
547 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
548 self._test_GetPackageMetadata_swapBuildTimestamps(
549 target_info_dict, source_info_dict)
550
551 target_info = BuildInfo(target_info_dict, None)
552 source_info = BuildInfo(source_info_dict, None)
553 common.OPTIONS.incremental_source = ''
554 self.assertRaises(RuntimeError, GetPackageMetadata, target_info,
555 source_info)
556
557 def test_GetPackageMetadata_downgrade(self):
558 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
559 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
560 self._test_GetPackageMetadata_swapBuildTimestamps(
561 target_info_dict, source_info_dict)
562
563 target_info = BuildInfo(target_info_dict, None)
564 source_info = BuildInfo(source_info_dict, None)
565 common.OPTIONS.incremental_source = ''
566 common.OPTIONS.downgrade = True
567 common.OPTIONS.wipe_user_data = True
568 metadata = GetPackageMetadata(target_info, source_info)
569 self.assertDictEqual(
570 {
571 'ota-downgrade' : 'yes',
572 'ota-type' : 'BLOCK',
573 'ota-wipe' : 'yes',
574 'post-build' : 'build-fingerprint-target',
575 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800576 'post-sdk-level' : '27',
577 'post-security-patch-level' : '2017-12-01',
Tao Baofaa8e0b2018-04-12 14:31:43 -0700578 'post-timestamp' : '1400000000',
Tao Baodf3a48b2018-01-10 16:30:43 -0800579 'pre-device' : 'product-device',
580 'pre-build' : 'build-fingerprint-source',
581 'pre-build-incremental' : 'build-version-incremental-source',
582 },
583 metadata)
Tao Baofabe0832018-01-17 15:52:28 -0800584
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 Xu028a1612019-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 Xu028a1612019-09-11 00:29:26 -0700603 expected_ab_partitions = ['boot', 'system', 'bootloader', 'modem']
604 self.assertEqual('\n'.join(expected_ab_partitions), ab_partitions)
605
606 @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
Tianjie Xu028a1612019-09-11 00:29:26 -0700625 @test_utils.SkipIfExternalToolsUnavailable()
626 def test_GetTargetFilesZipForSecondaryImages_dynamicPartitions(self):
Tao Bao12489802018-07-12 14:47:38 -0700627 input_file = construct_target_files(secondary=True)
Tianjie Xu028a1612019-09-11 00:29:26 -0700628 misc_info = '\n'.join([
629 'use_dynamic_partition_size=true',
630 'use_dynamic_partitions=true',
631 'dynamic_partition_list=system vendor product',
632 'super_partition_groups=google_dynamic_partitions',
633 'super_google_dynamic_partitions_group_size=4873781248',
634 'super_google_dynamic_partitions_partition_list=system vendor product',
635 ])
636 dynamic_partitions_info = '\n'.join([
637 'super_partition_groups=google_dynamic_partitions',
638 'super_google_dynamic_partitions_group_size=4873781248',
639 'super_google_dynamic_partitions_partition_list=system vendor product',
640 ])
641
642 with zipfile.ZipFile(input_file, 'a') as append_zip:
643 common.ZipWriteStr(append_zip, 'META/misc_info.txt', misc_info)
644 common.ZipWriteStr(append_zip, 'META/dynamic_partitions_info.txt',
645 dynamic_partitions_info)
646
Tao Bao12489802018-07-12 14:47:38 -0700647 target_file = GetTargetFilesZipForSecondaryImages(input_file)
648
649 with zipfile.ZipFile(target_file) as verify_zip:
650 namelist = verify_zip.namelist()
Tianjie Xu028a1612019-09-11 00:29:26 -0700651 updated_misc_info = verify_zip.read('META/misc_info.txt')
652 updated_dynamic_partitions_info = verify_zip.read(
653 'META/dynamic_partitions_info.txt')
Tao Bao12489802018-07-12 14:47:38 -0700654
655 self.assertIn('META/ab_partitions.txt', namelist)
656 self.assertIn('IMAGES/boot.img', namelist)
657 self.assertIn('IMAGES/system.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700658 self.assertIn(POSTINSTALL_CONFIG, namelist)
Tianjie Xu028a1612019-09-11 00:29:26 -0700659 self.assertIn('META/misc_info.txt', namelist)
660 self.assertIn('META/dynamic_partitions_info.txt', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700661
662 self.assertNotIn('IMAGES/system_other.img', namelist)
663 self.assertNotIn('IMAGES/system.map', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700664
Tianjie Xu028a1612019-09-11 00:29:26 -0700665 # Check the vendor & product are removed from the partitions list.
666 expected_misc_info = misc_info.replace('system vendor product',
667 'system')
668 expected_dynamic_partitions_info = dynamic_partitions_info.replace(
669 'system vendor product', 'system')
670 self.assertEqual(expected_misc_info, updated_misc_info)
671 self.assertEqual(expected_dynamic_partitions_info,
672 updated_dynamic_partitions_info)
673
674 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao15a146a2018-02-21 16:06:59 -0800675 def test_GetTargetFilesZipWithoutPostinstallConfig(self):
676 input_file = construct_target_files()
677 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
678 with zipfile.ZipFile(target_file) as verify_zip:
679 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
680
681 def test_GetTargetFilesZipWithoutPostinstallConfig_missingEntry(self):
682 input_file = construct_target_files()
683 common.ZipDelete(input_file, POSTINSTALL_CONFIG)
684 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
685 with zipfile.ZipFile(target_file) as verify_zip:
686 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
687
Tao Bao3bf8c652018-03-16 12:59:42 -0700688 def _test_FinalizeMetadata(self, large_entry=False):
689 entries = [
690 'required-entry1',
691 'required-entry2',
692 ]
693 zip_file = PropertyFilesTest.construct_zip_package(entries)
694 # Add a large entry of 1 GiB if requested.
695 if large_entry:
696 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
697 zip_fp.writestr(
698 # Using 'zoo' so that the entry stays behind others after signing.
699 'zoo',
700 'A' * 1024 * 1024 * 1024,
701 zipfile.ZIP_STORED)
702
703 metadata = {}
704 output_file = common.MakeTempFile(suffix='.zip')
705 needed_property_files = (
706 TestPropertyFiles(),
707 )
708 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
709 self.assertIn('ota-test-property-files', metadata)
710
711 def test_FinalizeMetadata(self):
712 self._test_FinalizeMetadata()
713
714 def test_FinalizeMetadata_withNoSigning(self):
715 common.OPTIONS.no_signing = True
716 self._test_FinalizeMetadata()
717
718 def test_FinalizeMetadata_largeEntry(self):
719 self._test_FinalizeMetadata(large_entry=True)
720
721 def test_FinalizeMetadata_largeEntry_withNoSigning(self):
722 common.OPTIONS.no_signing = True
723 self._test_FinalizeMetadata(large_entry=True)
724
725 def test_FinalizeMetadata_insufficientSpace(self):
726 entries = [
727 'required-entry1',
728 'required-entry2',
729 'optional-entry1',
730 'optional-entry2',
731 ]
732 zip_file = PropertyFilesTest.construct_zip_package(entries)
733 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
734 zip_fp.writestr(
735 # 'foo-entry1' will appear ahead of all other entries (in alphabetical
736 # order) after the signing, which will in turn trigger the
737 # InsufficientSpaceException and an automatic retry.
738 'foo-entry1',
739 'A' * 1024 * 1024,
740 zipfile.ZIP_STORED)
741
742 metadata = {}
743 needed_property_files = (
744 TestPropertyFiles(),
745 )
746 output_file = common.MakeTempFile(suffix='.zip')
747 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
748 self.assertIn('ota-test-property-files', metadata)
749
Tao Baoae5e4c32018-03-01 19:30:00 -0800750
Tao Bao69203522018-03-08 16:09:01 -0800751class TestPropertyFiles(PropertyFiles):
752 """A class that extends PropertyFiles for testing purpose."""
753
754 def __init__(self):
755 super(TestPropertyFiles, self).__init__()
756 self.name = 'ota-test-property-files'
757 self.required = (
758 'required-entry1',
759 'required-entry2',
760 )
761 self.optional = (
762 'optional-entry1',
763 'optional-entry2',
764 )
765
766
Tao Bao65b94e92018-10-11 21:57:26 -0700767class PropertyFilesTest(test_utils.ReleaseToolsTestCase):
Tao Baoae5e4c32018-03-01 19:30:00 -0800768
Tao Bao3bf8c652018-03-16 12:59:42 -0700769 def setUp(self):
770 common.OPTIONS.no_signing = False
771
Tao Baof5110492018-03-02 09:47:43 -0800772 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -0700773 def construct_zip_package(entries):
Tao Baof5110492018-03-02 09:47:43 -0800774 zip_file = common.MakeTempFile(suffix='.zip')
775 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
776 for entry in entries:
777 zip_fp.writestr(
778 entry,
779 entry.replace('.', '-').upper(),
780 zipfile.ZIP_STORED)
781 return zip_file
782
783 @staticmethod
Tao Bao69203522018-03-08 16:09:01 -0800784 def _parse_property_files_string(data):
Tao Baof5110492018-03-02 09:47:43 -0800785 result = {}
786 for token in data.split(','):
787 name, info = token.split(':', 1)
788 result[name] = info
789 return result
790
791 def _verify_entries(self, input_file, tokens, entries):
792 for entry in entries:
793 offset, size = map(int, tokens[entry].split(':'))
794 with open(input_file, 'rb') as input_fp:
795 input_fp.seek(offset)
796 if entry == 'metadata':
797 expected = b'META-INF/COM/ANDROID/METADATA'
798 else:
799 expected = entry.replace('.', '-').upper().encode()
800 self.assertEqual(expected, input_fp.read(size))
801
Tao Baoae5e4c32018-03-01 19:30:00 -0800802 def test_Compute(self):
Tao Baof5110492018-03-02 09:47:43 -0800803 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800804 'required-entry1',
805 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800806 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700807 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800808 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800809 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800810 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800811
Tao Bao69203522018-03-08 16:09:01 -0800812 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800813 self.assertEqual(3, len(tokens))
814 self._verify_entries(zip_file, tokens, entries)
815
Tao Bao69203522018-03-08 16:09:01 -0800816 def test_Compute_withOptionalEntries(self):
Tao Baof5110492018-03-02 09:47:43 -0800817 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800818 'required-entry1',
819 'required-entry2',
820 'optional-entry1',
821 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800822 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700823 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800824 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800825 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800826 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800827
Tao Bao69203522018-03-08 16:09:01 -0800828 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800829 self.assertEqual(5, len(tokens))
830 self._verify_entries(zip_file, tokens, entries)
831
Tao Bao69203522018-03-08 16:09:01 -0800832 def test_Compute_missingRequiredEntry(self):
833 entries = (
834 'required-entry2',
835 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700836 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800837 property_files = TestPropertyFiles()
838 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
839 self.assertRaises(KeyError, property_files.Compute, zip_fp)
840
Tao Baoae5e4c32018-03-01 19:30:00 -0800841 def test_Finalize(self):
Tao Baof5110492018-03-02 09:47:43 -0800842 entries = [
Tao Bao69203522018-03-08 16:09:01 -0800843 'required-entry1',
844 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800845 'META-INF/com/android/metadata',
846 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700847 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800848 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800849 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700850 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800851 zip_fp, reserve_space=False)
852 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
Tao Bao69203522018-03-08 16:09:01 -0800853 tokens = self._parse_property_files_string(streaming_metadata)
Tao Baof5110492018-03-02 09:47:43 -0800854
855 self.assertEqual(3, len(tokens))
856 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
857 # streaming metadata.
858 entries[2] = 'metadata'
859 self._verify_entries(zip_file, tokens, entries)
860
Tao Baoae5e4c32018-03-01 19:30:00 -0800861 def test_Finalize_assertReservedLength(self):
Tao Baof5110492018-03-02 09:47:43 -0800862 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800863 'required-entry1',
864 'required-entry2',
865 'optional-entry1',
866 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800867 'META-INF/com/android/metadata',
868 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700869 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800870 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800871 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
872 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700873 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800874 zip_fp, reserve_space=False)
Tao Baof5110492018-03-02 09:47:43 -0800875 raw_length = len(raw_metadata)
876
877 # Now pass in the exact expected length.
Tao Baoae5e4c32018-03-01 19:30:00 -0800878 streaming_metadata = property_files.Finalize(zip_fp, raw_length)
Tao Baof5110492018-03-02 09:47:43 -0800879 self.assertEqual(raw_length, len(streaming_metadata))
880
881 # Or pass in insufficient length.
882 self.assertRaises(
Tao Bao3bf8c652018-03-16 12:59:42 -0700883 PropertyFiles.InsufficientSpaceException,
Tao Baoae5e4c32018-03-01 19:30:00 -0800884 property_files.Finalize,
Tao Baof5110492018-03-02 09:47:43 -0800885 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800886 raw_length - 1)
Tao Baof5110492018-03-02 09:47:43 -0800887
888 # Or pass in a much larger size.
Tao Baoae5e4c32018-03-01 19:30:00 -0800889 streaming_metadata = property_files.Finalize(
Tao Baof5110492018-03-02 09:47:43 -0800890 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800891 raw_length + 20)
Tao Baof5110492018-03-02 09:47:43 -0800892 self.assertEqual(raw_length + 20, len(streaming_metadata))
893 self.assertEqual(' ' * 20, streaming_metadata[raw_length:])
894
Tao Baoae5e4c32018-03-01 19:30:00 -0800895 def test_Verify(self):
896 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800897 'required-entry1',
898 'required-entry2',
899 'optional-entry1',
900 'optional-entry2',
901 'META-INF/com/android/metadata',
902 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700903 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800904 property_files = TestPropertyFiles()
905 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
906 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700907 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800908 zip_fp, reserve_space=False)
909
910 # Should pass the test if verification passes.
911 property_files.Verify(zip_fp, raw_metadata)
912
913 # Or raise on verification failure.
914 self.assertRaises(
915 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
916
917
918class StreamingPropertyFilesTest(PropertyFilesTest):
919 """Additional sanity checks specialized for StreamingPropertyFiles."""
920
921 def test_init(self):
922 property_files = StreamingPropertyFiles()
923 self.assertEqual('ota-streaming-property-files', property_files.name)
924 self.assertEqual(
925 (
926 'payload.bin',
927 'payload_properties.txt',
928 ),
929 property_files.required)
930 self.assertEqual(
931 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -0700932 'care_map.pb',
Tao Bao69203522018-03-08 16:09:01 -0800933 'care_map.txt',
934 'compatibility.zip',
935 ),
936 property_files.optional)
937
938 def test_Compute(self):
939 entries = (
Tao Baoae5e4c32018-03-01 19:30:00 -0800940 'payload.bin',
941 'payload_properties.txt',
942 'care_map.txt',
Tao Bao69203522018-03-08 16:09:01 -0800943 'compatibility.zip',
944 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700945 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800946 property_files = StreamingPropertyFiles()
947 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
948 property_files_string = property_files.Compute(zip_fp)
949
950 tokens = self._parse_property_files_string(property_files_string)
951 self.assertEqual(5, len(tokens))
952 self._verify_entries(zip_file, tokens, entries)
953
954 def test_Finalize(self):
955 entries = [
956 'payload.bin',
957 'payload_properties.txt',
958 'care_map.txt',
959 'compatibility.zip',
960 'META-INF/com/android/metadata',
961 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700962 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800963 property_files = StreamingPropertyFiles()
964 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700965 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800966 zip_fp, reserve_space=False)
967 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
968 tokens = self._parse_property_files_string(streaming_metadata)
969
970 self.assertEqual(5, len(tokens))
971 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
972 # streaming metadata.
973 entries[4] = 'metadata'
974 self._verify_entries(zip_file, tokens, entries)
975
976 def test_Verify(self):
977 entries = (
978 'payload.bin',
979 'payload_properties.txt',
980 'care_map.txt',
981 'compatibility.zip',
Tao Baoae5e4c32018-03-01 19:30:00 -0800982 'META-INF/com/android/metadata',
983 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700984 zip_file = self.construct_zip_package(entries)
Tao Baoae5e4c32018-03-01 19:30:00 -0800985 property_files = StreamingPropertyFiles()
986 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
987 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700988 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800989 zip_fp, reserve_space=False)
990
991 # Should pass the test if verification passes.
992 property_files.Verify(zip_fp, raw_metadata)
993
994 # Or raise on verification failure.
995 self.assertRaises(
996 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
997
Tao Baofabe0832018-01-17 15:52:28 -0800998
Tao Baob6304672018-03-08 16:28:33 -0800999class AbOtaPropertyFilesTest(PropertyFilesTest):
1000 """Additional sanity checks specialized for AbOtaPropertyFiles."""
1001
1002 # The size for payload and metadata signature size.
1003 SIGNATURE_SIZE = 256
1004
1005 def setUp(self):
1006 self.testdata_dir = test_utils.get_testdata_dir()
1007 self.assertTrue(os.path.exists(self.testdata_dir))
1008
1009 common.OPTIONS.wipe_user_data = False
1010 common.OPTIONS.payload_signer = None
1011 common.OPTIONS.payload_signer_args = None
1012 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1013 common.OPTIONS.key_passwords = {
1014 common.OPTIONS.package_key : None,
1015 }
1016
1017 def test_init(self):
1018 property_files = AbOtaPropertyFiles()
1019 self.assertEqual('ota-property-files', property_files.name)
1020 self.assertEqual(
1021 (
1022 'payload.bin',
1023 'payload_properties.txt',
1024 ),
1025 property_files.required)
1026 self.assertEqual(
1027 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -07001028 'care_map.pb',
Tao Baob6304672018-03-08 16:28:33 -08001029 'care_map.txt',
1030 'compatibility.zip',
1031 ),
1032 property_files.optional)
1033
1034 def test_GetPayloadMetadataOffsetAndSize(self):
1035 target_file = construct_target_files()
1036 payload = Payload()
1037 payload.Generate(target_file)
1038
1039 payload_signer = PayloadSigner()
1040 payload.Sign(payload_signer)
1041
1042 output_file = common.MakeTempFile(suffix='.zip')
1043 with zipfile.ZipFile(output_file, 'w') as output_zip:
1044 payload.WriteToZip(output_zip)
1045
1046 # Find out the payload metadata offset and size.
1047 property_files = AbOtaPropertyFiles()
1048 with zipfile.ZipFile(output_file) as input_zip:
1049 # pylint: disable=protected-access
1050 payload_offset, metadata_total = (
1051 property_files._GetPayloadMetadataOffsetAndSize(input_zip))
1052
1053 # Read in the metadata signature directly.
1054 with open(output_file, 'rb') as verify_fp:
1055 verify_fp.seek(payload_offset + metadata_total - self.SIGNATURE_SIZE)
1056 metadata_signature = verify_fp.read(self.SIGNATURE_SIZE)
1057
1058 # Now we extract the metadata hash via brillo_update_payload script, which
1059 # will serve as the oracle result.
1060 payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1061 metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1062 cmd = ['brillo_update_payload', 'hash',
1063 '--unsigned_payload', payload.payload_file,
1064 '--signature_size', str(self.SIGNATURE_SIZE),
1065 '--metadata_hash_file', metadata_sig_file,
1066 '--payload_hash_file', payload_sig_file]
Tao Bao73dd4f42018-10-04 16:25:33 -07001067 proc = common.Run(cmd)
Tao Baob6304672018-03-08 16:28:33 -08001068 stdoutdata, _ = proc.communicate()
1069 self.assertEqual(
1070 0, proc.returncode,
Tao Bao73dd4f42018-10-04 16:25:33 -07001071 'Failed to run brillo_update_payload:\n{}'.format(stdoutdata))
Tao Baob6304672018-03-08 16:28:33 -08001072
1073 signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file)
1074
1075 # Finally we can compare the two signatures.
1076 with open(signed_metadata_sig_file, 'rb') as verify_fp:
1077 self.assertEqual(verify_fp.read(), metadata_signature)
1078
1079 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -07001080 def construct_zip_package_withValidPayload(with_metadata=False):
1081 # Cannot use construct_zip_package() since we need a "valid" payload.bin.
Tao Baob6304672018-03-08 16:28:33 -08001082 target_file = construct_target_files()
1083 payload = Payload()
1084 payload.Generate(target_file)
1085
1086 payload_signer = PayloadSigner()
1087 payload.Sign(payload_signer)
1088
1089 zip_file = common.MakeTempFile(suffix='.zip')
1090 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
1091 # 'payload.bin',
1092 payload.WriteToZip(zip_fp)
1093
1094 # Other entries.
1095 entries = ['care_map.txt', 'compatibility.zip']
1096
1097 # Put META-INF/com/android/metadata if needed.
1098 if with_metadata:
1099 entries.append('META-INF/com/android/metadata')
1100
1101 for entry in entries:
1102 zip_fp.writestr(
1103 entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED)
1104
1105 return zip_file
1106
1107 def test_Compute(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001108 zip_file = self.construct_zip_package_withValidPayload()
Tao Baob6304672018-03-08 16:28:33 -08001109 property_files = AbOtaPropertyFiles()
1110 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1111 property_files_string = property_files.Compute(zip_fp)
1112
1113 tokens = self._parse_property_files_string(property_files_string)
1114 # "6" indcludes the four entries above, one metadata entry, and one entry
1115 # for payload-metadata.bin.
1116 self.assertEqual(6, len(tokens))
1117 self._verify_entries(
1118 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1119
1120 def test_Finalize(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001121 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001122 property_files = AbOtaPropertyFiles()
1123 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001124 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001125 zip_fp, reserve_space=False)
1126 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1127
1128 tokens = self._parse_property_files_string(property_files_string)
1129 # "6" indcludes the four entries above, one metadata entry, and one entry
1130 # for payload-metadata.bin.
1131 self.assertEqual(6, len(tokens))
1132 self._verify_entries(
1133 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1134
1135 def test_Verify(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001136 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001137 property_files = AbOtaPropertyFiles()
1138 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001139 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001140 zip_fp, reserve_space=False)
1141
1142 property_files.Verify(zip_fp, raw_metadata)
1143
1144
Tao Baoc0746f42018-02-21 13:17:22 -08001145class NonAbOtaPropertyFilesTest(PropertyFilesTest):
1146 """Additional sanity checks specialized for NonAbOtaPropertyFiles."""
1147
1148 def test_init(self):
1149 property_files = NonAbOtaPropertyFiles()
1150 self.assertEqual('ota-property-files', property_files.name)
1151 self.assertEqual((), property_files.required)
1152 self.assertEqual((), property_files.optional)
1153
1154 def test_Compute(self):
1155 entries = ()
Tao Bao3bf8c652018-03-16 12:59:42 -07001156 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001157 property_files = NonAbOtaPropertyFiles()
1158 with zipfile.ZipFile(zip_file) as zip_fp:
1159 property_files_string = property_files.Compute(zip_fp)
1160
1161 tokens = self._parse_property_files_string(property_files_string)
1162 self.assertEqual(1, len(tokens))
1163 self._verify_entries(zip_file, tokens, entries)
1164
1165 def test_Finalize(self):
1166 entries = [
1167 'META-INF/com/android/metadata',
1168 ]
Tao Bao3bf8c652018-03-16 12:59:42 -07001169 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001170 property_files = NonAbOtaPropertyFiles()
1171 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001172 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001173 zip_fp, reserve_space=False)
1174 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1175 tokens = self._parse_property_files_string(property_files_string)
1176
1177 self.assertEqual(1, len(tokens))
1178 # 'META-INF/com/android/metadata' will be key'd as 'metadata'.
1179 entries[0] = 'metadata'
1180 self._verify_entries(zip_file, tokens, entries)
1181
1182 def test_Verify(self):
1183 entries = (
1184 'META-INF/com/android/metadata',
1185 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001186 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001187 property_files = NonAbOtaPropertyFiles()
1188 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001189 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001190 zip_fp, reserve_space=False)
1191
1192 property_files.Verify(zip_fp, raw_metadata)
1193
1194
Tao Bao65b94e92018-10-11 21:57:26 -07001195class PayloadSignerTest(test_utils.ReleaseToolsTestCase):
Tao Baofabe0832018-01-17 15:52:28 -08001196
1197 SIGFILE = 'sigfile.bin'
1198 SIGNED_SIGFILE = 'signed-sigfile.bin'
1199
1200 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001201 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baofabe0832018-01-17 15:52:28 -08001202 self.assertTrue(os.path.exists(self.testdata_dir))
1203
1204 common.OPTIONS.payload_signer = None
1205 common.OPTIONS.payload_signer_args = []
1206 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1207 common.OPTIONS.key_passwords = {
1208 common.OPTIONS.package_key : None,
1209 }
1210
Tao Baofabe0832018-01-17 15:52:28 -08001211 def _assertFilesEqual(self, file1, file2):
1212 with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
1213 self.assertEqual(fp1.read(), fp2.read())
1214
1215 def test_init(self):
1216 payload_signer = PayloadSigner()
1217 self.assertEqual('openssl', payload_signer.signer)
xunchang3c5de182019-04-08 23:04:58 -07001218 self.assertEqual(256, payload_signer.key_size)
Tao Baofabe0832018-01-17 15:52:28 -08001219
1220 def test_init_withPassword(self):
1221 common.OPTIONS.package_key = os.path.join(
1222 self.testdata_dir, 'testkey_with_passwd')
1223 common.OPTIONS.key_passwords = {
1224 common.OPTIONS.package_key : 'foo',
1225 }
1226 payload_signer = PayloadSigner()
1227 self.assertEqual('openssl', payload_signer.signer)
1228
1229 def test_init_withExternalSigner(self):
1230 common.OPTIONS.payload_signer = 'abc'
1231 common.OPTIONS.payload_signer_args = ['arg1', 'arg2']
xunchang3c5de182019-04-08 23:04:58 -07001232 common.OPTIONS.payload_signer_key_size = '512'
Tao Baofabe0832018-01-17 15:52:28 -08001233 payload_signer = PayloadSigner()
1234 self.assertEqual('abc', payload_signer.signer)
1235 self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args)
xunchang3c5de182019-04-08 23:04:58 -07001236 self.assertEqual(512, payload_signer.key_size)
1237
1238 def test_GetKeySizeInBytes_512Bytes(self):
1239 signing_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
1240 key_size = PayloadSigner._GetKeySizeInBytes(signing_key)
1241 self.assertEqual(512, key_size)
Tao Baofabe0832018-01-17 15:52:28 -08001242
1243 def test_Sign(self):
1244 payload_signer = PayloadSigner()
1245 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1246 signed_file = payload_signer.Sign(input_file)
1247
1248 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1249 self._assertFilesEqual(verify_file, signed_file)
1250
1251 def test_Sign_withExternalSigner_openssl(self):
1252 """Uses openssl as the external payload signer."""
1253 common.OPTIONS.payload_signer = 'openssl'
1254 common.OPTIONS.payload_signer_args = [
1255 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey',
1256 os.path.join(self.testdata_dir, 'testkey.pk8'),
1257 '-pkeyopt', 'digest:sha256']
1258 payload_signer = PayloadSigner()
1259 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1260 signed_file = payload_signer.Sign(input_file)
1261
1262 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1263 self._assertFilesEqual(verify_file, signed_file)
1264
1265 def test_Sign_withExternalSigner_script(self):
1266 """Uses testdata/payload_signer.sh as the external payload signer."""
1267 common.OPTIONS.payload_signer = os.path.join(
1268 self.testdata_dir, 'payload_signer.sh')
1269 common.OPTIONS.payload_signer_args = [
1270 os.path.join(self.testdata_dir, 'testkey.pk8')]
1271 payload_signer = PayloadSigner()
1272 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1273 signed_file = payload_signer.Sign(input_file)
1274
1275 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1276 self._assertFilesEqual(verify_file, signed_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001277
1278
Tao Bao65b94e92018-10-11 21:57:26 -07001279class PayloadTest(test_utils.ReleaseToolsTestCase):
Tao Baoc7b403a2018-01-30 18:19:04 -08001280
1281 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001282 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoc7b403a2018-01-30 18:19:04 -08001283 self.assertTrue(os.path.exists(self.testdata_dir))
1284
1285 common.OPTIONS.wipe_user_data = False
1286 common.OPTIONS.payload_signer = None
1287 common.OPTIONS.payload_signer_args = None
1288 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1289 common.OPTIONS.key_passwords = {
1290 common.OPTIONS.package_key : None,
1291 }
1292
Tao Baoc7b403a2018-01-30 18:19:04 -08001293 @staticmethod
Tao Baof7140c02018-01-30 17:09:24 -08001294 def _create_payload_full(secondary=False):
1295 target_file = construct_target_files(secondary)
Tao Bao667ff572018-02-10 00:02:40 -08001296 payload = Payload(secondary)
Tao Baoc7b403a2018-01-30 18:19:04 -08001297 payload.Generate(target_file)
1298 return payload
1299
Tao Baof7140c02018-01-30 17:09:24 -08001300 @staticmethod
1301 def _create_payload_incremental():
1302 target_file = construct_target_files()
1303 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001304 payload = Payload()
1305 payload.Generate(target_file, source_file)
1306 return payload
1307
1308 def test_Generate_full(self):
1309 payload = self._create_payload_full()
1310 self.assertTrue(os.path.exists(payload.payload_file))
1311
1312 def test_Generate_incremental(self):
1313 payload = self._create_payload_incremental()
1314 self.assertTrue(os.path.exists(payload.payload_file))
1315
1316 def test_Generate_additionalArgs(self):
Tao Baof7140c02018-01-30 17:09:24 -08001317 target_file = construct_target_files()
1318 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001319 payload = Payload()
1320 # This should work the same as calling payload.Generate(target_file,
1321 # source_file).
1322 payload.Generate(
1323 target_file, additional_args=["--source_image", source_file])
1324 self.assertTrue(os.path.exists(payload.payload_file))
1325
1326 def test_Generate_invalidInput(self):
Tao Baof7140c02018-01-30 17:09:24 -08001327 target_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001328 common.ZipDelete(target_file, 'IMAGES/vendor.img')
1329 payload = Payload()
Tao Baobec89c12018-10-15 11:53:28 -07001330 self.assertRaises(common.ExternalError, payload.Generate, target_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001331
1332 def test_Sign_full(self):
1333 payload = self._create_payload_full()
1334 payload.Sign(PayloadSigner())
1335
1336 output_file = common.MakeTempFile(suffix='.zip')
1337 with zipfile.ZipFile(output_file, 'w') as output_zip:
1338 payload.WriteToZip(output_zip)
1339
1340 import check_ota_package_signature
1341 check_ota_package_signature.VerifyAbOtaPayload(
1342 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1343 output_file)
1344
1345 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
1358 def test_Sign_withDataWipe(self):
1359 common.OPTIONS.wipe_user_data = True
1360 payload = self._create_payload_full()
1361 payload.Sign(PayloadSigner())
1362
1363 with open(payload.payload_properties) as properties_fp:
1364 self.assertIn("POWERWASH=1", properties_fp.read())
1365
Tao Bao667ff572018-02-10 00:02:40 -08001366 def test_Sign_secondary(self):
1367 payload = self._create_payload_full(secondary=True)
1368 payload.Sign(PayloadSigner())
1369
1370 with open(payload.payload_properties) as properties_fp:
1371 self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read())
1372
Tao Baoc7b403a2018-01-30 18:19:04 -08001373 def test_Sign_badSigner(self):
1374 """Tests that signing failure can be captured."""
1375 payload = self._create_payload_full()
1376 payload_signer = PayloadSigner()
1377 payload_signer.signer_args.append('bad-option')
Tao Baobec89c12018-10-15 11:53:28 -07001378 self.assertRaises(common.ExternalError, payload.Sign, payload_signer)
Tao Baoc7b403a2018-01-30 18:19:04 -08001379
1380 def test_WriteToZip(self):
1381 payload = self._create_payload_full()
1382 payload.Sign(PayloadSigner())
1383
1384 output_file = common.MakeTempFile(suffix='.zip')
1385 with zipfile.ZipFile(output_file, 'w') as output_zip:
1386 payload.WriteToZip(output_zip)
1387
1388 with zipfile.ZipFile(output_file) as verify_zip:
1389 # First make sure we have the essential entries.
1390 namelist = verify_zip.namelist()
1391 self.assertIn(Payload.PAYLOAD_BIN, namelist)
1392 self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist)
1393
1394 # Then assert these entries are stored.
1395 for entry_info in verify_zip.infolist():
1396 if entry_info.filename not in (Payload.PAYLOAD_BIN,
1397 Payload.PAYLOAD_PROPERTIES_TXT):
1398 continue
1399 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)
1400
1401 def test_WriteToZip_unsignedPayload(self):
1402 """Unsigned payloads should not be allowed to be written to zip."""
1403 payload = self._create_payload_full()
1404
1405 output_file = common.MakeTempFile(suffix='.zip')
1406 with zipfile.ZipFile(output_file, 'w') as output_zip:
1407 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
1408
1409 # Also test with incremental payload.
1410 payload = self._create_payload_incremental()
1411
1412 output_file = common.MakeTempFile(suffix='.zip')
1413 with zipfile.ZipFile(output_file, 'w') as output_zip:
1414 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001415
1416 def test_WriteToZip_secondary(self):
1417 payload = self._create_payload_full(secondary=True)
1418 payload.Sign(PayloadSigner())
1419
1420 output_file = common.MakeTempFile(suffix='.zip')
1421 with zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Bao667ff572018-02-10 00:02:40 -08001422 payload.WriteToZip(output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001423
1424 with zipfile.ZipFile(output_file) as verify_zip:
1425 # First make sure we have the essential entries.
1426 namelist = verify_zip.namelist()
1427 self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist)
1428 self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist)
1429
1430 # Then assert these entries are stored.
1431 for entry_info in verify_zip.infolist():
1432 if entry_info.filename not in (
1433 Payload.SECONDARY_PAYLOAD_BIN,
1434 Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT):
1435 continue
1436 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)