blob: 07dcffb6aa929d3f49c0a745ea63895a660d2395 [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)
Tao Baof7140c02018-01-30 17:09:24 -0800594 self.assertIn('IMAGES/system.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700595 self.assertIn('RADIO/bootloader.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800596 self.assertIn(POSTINSTALL_CONFIG, namelist)
Tao Baof7140c02018-01-30 17:09:24 -0800597
Tao Bao180275b2019-09-17 22:43:11 -0700598 self.assertNotIn('IMAGES/boot.img', namelist)
Tao Baof7140c02018-01-30 17:09:24 -0800599 self.assertNotIn('IMAGES/system_other.img', namelist)
600 self.assertNotIn('IMAGES/system.map', namelist)
Tao Bao180275b2019-09-17 22:43:11 -0700601 self.assertNotIn('RADIO/modem.img', namelist)
Tao Baof7140c02018-01-30 17:09:24 -0800602
Tao Bao180275b2019-09-17 22:43:11 -0700603 expected_ab_partitions = ['system', 'bootloader']
Tianjie Xu028a1612019-09-11 00:29:26 -0700604 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)
Tao Bao15a146a2018-02-21 16:06:59 -0800616 self.assertIn('IMAGES/system.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700617 self.assertIn('RADIO/bootloader.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800618
Tao Bao180275b2019-09-17 22:43:11 -0700619 self.assertNotIn('IMAGES/boot.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800620 self.assertNotIn('IMAGES/system_other.img', namelist)
621 self.assertNotIn('IMAGES/system.map', namelist)
Tao Bao180275b2019-09-17 22:43:11 -0700622 self.assertNotIn('RADIO/modem.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800623 self.assertNotIn(POSTINSTALL_CONFIG, namelist)
624
Tianjie Xu028a1612019-09-11 00:29:26 -0700625 @test_utils.SkipIfExternalToolsUnavailable()
Tao Bao180275b2019-09-17 22:43:11 -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/system.img', namelist)
637 self.assertIn(POSTINSTALL_CONFIG, namelist)
638
639 self.assertNotIn('IMAGES/boot.img', namelist)
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
645 @test_utils.SkipIfExternalToolsUnavailable()
Tianjie Xu028a1612019-09-11 00:29:26 -0700646 def test_GetTargetFilesZipForSecondaryImages_dynamicPartitions(self):
Tao Bao12489802018-07-12 14:47:38 -0700647 input_file = construct_target_files(secondary=True)
Tianjie Xu028a1612019-09-11 00:29:26 -0700648 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
Tao Bao12489802018-07-12 14:47:38 -0700667 target_file = GetTargetFilesZipForSecondaryImages(input_file)
668
669 with zipfile.ZipFile(target_file) as verify_zip:
670 namelist = verify_zip.namelist()
Tianjie Xu028a1612019-09-11 00:29:26 -0700671 updated_misc_info = verify_zip.read('META/misc_info.txt')
672 updated_dynamic_partitions_info = verify_zip.read(
673 'META/dynamic_partitions_info.txt')
Tao Bao12489802018-07-12 14:47:38 -0700674
675 self.assertIn('META/ab_partitions.txt', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700676 self.assertIn('IMAGES/system.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700677 self.assertIn(POSTINSTALL_CONFIG, namelist)
Tianjie Xu028a1612019-09-11 00:29:26 -0700678 self.assertIn('META/misc_info.txt', namelist)
679 self.assertIn('META/dynamic_partitions_info.txt', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700680
Tao Bao180275b2019-09-17 22:43:11 -0700681 self.assertNotIn('IMAGES/boot.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700682 self.assertNotIn('IMAGES/system_other.img', namelist)
683 self.assertNotIn('IMAGES/system.map', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700684
Tianjie Xu028a1612019-09-11 00:29:26 -0700685 # 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
701 def test_GetTargetFilesZipWithoutPostinstallConfig_missingEntry(self):
702 input_file = construct_target_files()
703 common.ZipDelete(input_file, POSTINSTALL_CONFIG)
704 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
705 with zipfile.ZipFile(target_file) as verify_zip:
706 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
707
Tao Bao3bf8c652018-03-16 12:59:42 -0700708 def _test_FinalizeMetadata(self, large_entry=False):
709 entries = [
710 'required-entry1',
711 'required-entry2',
712 ]
713 zip_file = PropertyFilesTest.construct_zip_package(entries)
714 # Add a large entry of 1 GiB if requested.
715 if large_entry:
716 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
717 zip_fp.writestr(
718 # Using 'zoo' so that the entry stays behind others after signing.
719 'zoo',
720 'A' * 1024 * 1024 * 1024,
721 zipfile.ZIP_STORED)
722
723 metadata = {}
724 output_file = common.MakeTempFile(suffix='.zip')
725 needed_property_files = (
726 TestPropertyFiles(),
727 )
728 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
729 self.assertIn('ota-test-property-files', metadata)
730
731 def test_FinalizeMetadata(self):
732 self._test_FinalizeMetadata()
733
734 def test_FinalizeMetadata_withNoSigning(self):
735 common.OPTIONS.no_signing = True
736 self._test_FinalizeMetadata()
737
738 def test_FinalizeMetadata_largeEntry(self):
739 self._test_FinalizeMetadata(large_entry=True)
740
741 def test_FinalizeMetadata_largeEntry_withNoSigning(self):
742 common.OPTIONS.no_signing = True
743 self._test_FinalizeMetadata(large_entry=True)
744
745 def test_FinalizeMetadata_insufficientSpace(self):
746 entries = [
747 'required-entry1',
748 'required-entry2',
749 'optional-entry1',
750 'optional-entry2',
751 ]
752 zip_file = PropertyFilesTest.construct_zip_package(entries)
753 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
754 zip_fp.writestr(
755 # 'foo-entry1' will appear ahead of all other entries (in alphabetical
756 # order) after the signing, which will in turn trigger the
757 # InsufficientSpaceException and an automatic retry.
758 'foo-entry1',
759 'A' * 1024 * 1024,
760 zipfile.ZIP_STORED)
761
762 metadata = {}
763 needed_property_files = (
764 TestPropertyFiles(),
765 )
766 output_file = common.MakeTempFile(suffix='.zip')
767 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
768 self.assertIn('ota-test-property-files', metadata)
769
Tao Baoae5e4c32018-03-01 19:30:00 -0800770
Tao Bao69203522018-03-08 16:09:01 -0800771class TestPropertyFiles(PropertyFiles):
772 """A class that extends PropertyFiles for testing purpose."""
773
774 def __init__(self):
775 super(TestPropertyFiles, self).__init__()
776 self.name = 'ota-test-property-files'
777 self.required = (
778 'required-entry1',
779 'required-entry2',
780 )
781 self.optional = (
782 'optional-entry1',
783 'optional-entry2',
784 )
785
786
Tao Bao65b94e92018-10-11 21:57:26 -0700787class PropertyFilesTest(test_utils.ReleaseToolsTestCase):
Tao Baoae5e4c32018-03-01 19:30:00 -0800788
Tao Bao3bf8c652018-03-16 12:59:42 -0700789 def setUp(self):
790 common.OPTIONS.no_signing = False
791
Tao Baof5110492018-03-02 09:47:43 -0800792 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -0700793 def construct_zip_package(entries):
Tao Baof5110492018-03-02 09:47:43 -0800794 zip_file = common.MakeTempFile(suffix='.zip')
795 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
796 for entry in entries:
797 zip_fp.writestr(
798 entry,
799 entry.replace('.', '-').upper(),
800 zipfile.ZIP_STORED)
801 return zip_file
802
803 @staticmethod
Tao Bao69203522018-03-08 16:09:01 -0800804 def _parse_property_files_string(data):
Tao Baof5110492018-03-02 09:47:43 -0800805 result = {}
806 for token in data.split(','):
807 name, info = token.split(':', 1)
808 result[name] = info
809 return result
810
811 def _verify_entries(self, input_file, tokens, entries):
812 for entry in entries:
813 offset, size = map(int, tokens[entry].split(':'))
814 with open(input_file, 'rb') as input_fp:
815 input_fp.seek(offset)
816 if entry == 'metadata':
817 expected = b'META-INF/COM/ANDROID/METADATA'
818 else:
819 expected = entry.replace('.', '-').upper().encode()
820 self.assertEqual(expected, input_fp.read(size))
821
Tao Baoae5e4c32018-03-01 19:30:00 -0800822 def test_Compute(self):
Tao Baof5110492018-03-02 09:47:43 -0800823 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800824 'required-entry1',
825 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800826 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700827 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800828 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800829 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800830 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800831
Tao Bao69203522018-03-08 16:09:01 -0800832 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800833 self.assertEqual(3, len(tokens))
834 self._verify_entries(zip_file, tokens, entries)
835
Tao Bao69203522018-03-08 16:09:01 -0800836 def test_Compute_withOptionalEntries(self):
Tao Baof5110492018-03-02 09:47:43 -0800837 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800838 'required-entry1',
839 'required-entry2',
840 'optional-entry1',
841 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800842 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700843 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800844 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800845 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800846 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800847
Tao Bao69203522018-03-08 16:09:01 -0800848 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800849 self.assertEqual(5, len(tokens))
850 self._verify_entries(zip_file, tokens, entries)
851
Tao Bao69203522018-03-08 16:09:01 -0800852 def test_Compute_missingRequiredEntry(self):
853 entries = (
854 'required-entry2',
855 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700856 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800857 property_files = TestPropertyFiles()
858 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
859 self.assertRaises(KeyError, property_files.Compute, zip_fp)
860
Tao Baoae5e4c32018-03-01 19:30:00 -0800861 def test_Finalize(self):
Tao Baof5110492018-03-02 09:47:43 -0800862 entries = [
Tao Bao69203522018-03-08 16:09:01 -0800863 'required-entry1',
864 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800865 'META-INF/com/android/metadata',
866 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700867 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800868 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800869 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700870 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800871 zip_fp, reserve_space=False)
872 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
Tao Bao69203522018-03-08 16:09:01 -0800873 tokens = self._parse_property_files_string(streaming_metadata)
Tao Baof5110492018-03-02 09:47:43 -0800874
875 self.assertEqual(3, len(tokens))
876 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
877 # streaming metadata.
878 entries[2] = 'metadata'
879 self._verify_entries(zip_file, tokens, entries)
880
Tao Baoae5e4c32018-03-01 19:30:00 -0800881 def test_Finalize_assertReservedLength(self):
Tao Baof5110492018-03-02 09:47:43 -0800882 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800883 'required-entry1',
884 'required-entry2',
885 'optional-entry1',
886 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800887 'META-INF/com/android/metadata',
888 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700889 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800890 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800891 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
892 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700893 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800894 zip_fp, reserve_space=False)
Tao Baof5110492018-03-02 09:47:43 -0800895 raw_length = len(raw_metadata)
896
897 # Now pass in the exact expected length.
Tao Baoae5e4c32018-03-01 19:30:00 -0800898 streaming_metadata = property_files.Finalize(zip_fp, raw_length)
Tao Baof5110492018-03-02 09:47:43 -0800899 self.assertEqual(raw_length, len(streaming_metadata))
900
901 # Or pass in insufficient length.
902 self.assertRaises(
Tao Bao3bf8c652018-03-16 12:59:42 -0700903 PropertyFiles.InsufficientSpaceException,
Tao Baoae5e4c32018-03-01 19:30:00 -0800904 property_files.Finalize,
Tao Baof5110492018-03-02 09:47:43 -0800905 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800906 raw_length - 1)
Tao Baof5110492018-03-02 09:47:43 -0800907
908 # Or pass in a much larger size.
Tao Baoae5e4c32018-03-01 19:30:00 -0800909 streaming_metadata = property_files.Finalize(
Tao Baof5110492018-03-02 09:47:43 -0800910 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800911 raw_length + 20)
Tao Baof5110492018-03-02 09:47:43 -0800912 self.assertEqual(raw_length + 20, len(streaming_metadata))
913 self.assertEqual(' ' * 20, streaming_metadata[raw_length:])
914
Tao Baoae5e4c32018-03-01 19:30:00 -0800915 def test_Verify(self):
916 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800917 'required-entry1',
918 'required-entry2',
919 'optional-entry1',
920 'optional-entry2',
921 'META-INF/com/android/metadata',
922 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700923 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800924 property_files = TestPropertyFiles()
925 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
926 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700927 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800928 zip_fp, reserve_space=False)
929
930 # Should pass the test if verification passes.
931 property_files.Verify(zip_fp, raw_metadata)
932
933 # Or raise on verification failure.
934 self.assertRaises(
935 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
936
937
938class StreamingPropertyFilesTest(PropertyFilesTest):
939 """Additional sanity checks specialized for StreamingPropertyFiles."""
940
941 def test_init(self):
942 property_files = StreamingPropertyFiles()
943 self.assertEqual('ota-streaming-property-files', property_files.name)
944 self.assertEqual(
945 (
946 'payload.bin',
947 'payload_properties.txt',
948 ),
949 property_files.required)
950 self.assertEqual(
951 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -0700952 'care_map.pb',
Tao Bao69203522018-03-08 16:09:01 -0800953 'care_map.txt',
954 'compatibility.zip',
955 ),
956 property_files.optional)
957
958 def test_Compute(self):
959 entries = (
Tao Baoae5e4c32018-03-01 19:30:00 -0800960 'payload.bin',
961 'payload_properties.txt',
962 'care_map.txt',
Tao Bao69203522018-03-08 16:09:01 -0800963 'compatibility.zip',
964 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700965 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800966 property_files = StreamingPropertyFiles()
967 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
968 property_files_string = property_files.Compute(zip_fp)
969
970 tokens = self._parse_property_files_string(property_files_string)
971 self.assertEqual(5, len(tokens))
972 self._verify_entries(zip_file, tokens, entries)
973
974 def test_Finalize(self):
975 entries = [
976 'payload.bin',
977 'payload_properties.txt',
978 'care_map.txt',
979 'compatibility.zip',
980 'META-INF/com/android/metadata',
981 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700982 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800983 property_files = StreamingPropertyFiles()
984 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700985 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800986 zip_fp, reserve_space=False)
987 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
988 tokens = self._parse_property_files_string(streaming_metadata)
989
990 self.assertEqual(5, len(tokens))
991 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
992 # streaming metadata.
993 entries[4] = 'metadata'
994 self._verify_entries(zip_file, tokens, entries)
995
996 def test_Verify(self):
997 entries = (
998 'payload.bin',
999 'payload_properties.txt',
1000 'care_map.txt',
1001 'compatibility.zip',
Tao Baoae5e4c32018-03-01 19:30:00 -08001002 'META-INF/com/android/metadata',
1003 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001004 zip_file = self.construct_zip_package(entries)
Tao Baoae5e4c32018-03-01 19:30:00 -08001005 property_files = StreamingPropertyFiles()
1006 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1007 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001008 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -08001009 zip_fp, reserve_space=False)
1010
1011 # Should pass the test if verification passes.
1012 property_files.Verify(zip_fp, raw_metadata)
1013
1014 # Or raise on verification failure.
1015 self.assertRaises(
1016 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
1017
Tao Baofabe0832018-01-17 15:52:28 -08001018
Tao Baob6304672018-03-08 16:28:33 -08001019class AbOtaPropertyFilesTest(PropertyFilesTest):
1020 """Additional sanity checks specialized for AbOtaPropertyFiles."""
1021
1022 # The size for payload and metadata signature size.
1023 SIGNATURE_SIZE = 256
1024
1025 def setUp(self):
1026 self.testdata_dir = test_utils.get_testdata_dir()
1027 self.assertTrue(os.path.exists(self.testdata_dir))
1028
1029 common.OPTIONS.wipe_user_data = False
1030 common.OPTIONS.payload_signer = None
1031 common.OPTIONS.payload_signer_args = None
1032 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1033 common.OPTIONS.key_passwords = {
1034 common.OPTIONS.package_key : None,
1035 }
1036
1037 def test_init(self):
1038 property_files = AbOtaPropertyFiles()
1039 self.assertEqual('ota-property-files', property_files.name)
1040 self.assertEqual(
1041 (
1042 'payload.bin',
1043 'payload_properties.txt',
1044 ),
1045 property_files.required)
1046 self.assertEqual(
1047 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -07001048 'care_map.pb',
Tao Baob6304672018-03-08 16:28:33 -08001049 'care_map.txt',
1050 'compatibility.zip',
1051 ),
1052 property_files.optional)
1053
1054 def test_GetPayloadMetadataOffsetAndSize(self):
1055 target_file = construct_target_files()
1056 payload = Payload()
1057 payload.Generate(target_file)
1058
1059 payload_signer = PayloadSigner()
1060 payload.Sign(payload_signer)
1061
1062 output_file = common.MakeTempFile(suffix='.zip')
1063 with zipfile.ZipFile(output_file, 'w') as output_zip:
1064 payload.WriteToZip(output_zip)
1065
1066 # Find out the payload metadata offset and size.
1067 property_files = AbOtaPropertyFiles()
1068 with zipfile.ZipFile(output_file) as input_zip:
1069 # pylint: disable=protected-access
1070 payload_offset, metadata_total = (
1071 property_files._GetPayloadMetadataOffsetAndSize(input_zip))
1072
1073 # Read in the metadata signature directly.
1074 with open(output_file, 'rb') as verify_fp:
1075 verify_fp.seek(payload_offset + metadata_total - self.SIGNATURE_SIZE)
1076 metadata_signature = verify_fp.read(self.SIGNATURE_SIZE)
1077
1078 # Now we extract the metadata hash via brillo_update_payload script, which
1079 # will serve as the oracle result.
1080 payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1081 metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1082 cmd = ['brillo_update_payload', 'hash',
1083 '--unsigned_payload', payload.payload_file,
1084 '--signature_size', str(self.SIGNATURE_SIZE),
1085 '--metadata_hash_file', metadata_sig_file,
1086 '--payload_hash_file', payload_sig_file]
Tao Bao73dd4f42018-10-04 16:25:33 -07001087 proc = common.Run(cmd)
Tao Baob6304672018-03-08 16:28:33 -08001088 stdoutdata, _ = proc.communicate()
1089 self.assertEqual(
1090 0, proc.returncode,
Tao Bao73dd4f42018-10-04 16:25:33 -07001091 'Failed to run brillo_update_payload:\n{}'.format(stdoutdata))
Tao Baob6304672018-03-08 16:28:33 -08001092
1093 signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file)
1094
1095 # Finally we can compare the two signatures.
1096 with open(signed_metadata_sig_file, 'rb') as verify_fp:
1097 self.assertEqual(verify_fp.read(), metadata_signature)
1098
1099 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -07001100 def construct_zip_package_withValidPayload(with_metadata=False):
1101 # Cannot use construct_zip_package() since we need a "valid" payload.bin.
Tao Baob6304672018-03-08 16:28:33 -08001102 target_file = construct_target_files()
1103 payload = Payload()
1104 payload.Generate(target_file)
1105
1106 payload_signer = PayloadSigner()
1107 payload.Sign(payload_signer)
1108
1109 zip_file = common.MakeTempFile(suffix='.zip')
1110 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
1111 # 'payload.bin',
1112 payload.WriteToZip(zip_fp)
1113
1114 # Other entries.
1115 entries = ['care_map.txt', 'compatibility.zip']
1116
1117 # Put META-INF/com/android/metadata if needed.
1118 if with_metadata:
1119 entries.append('META-INF/com/android/metadata')
1120
1121 for entry in entries:
1122 zip_fp.writestr(
1123 entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED)
1124
1125 return zip_file
1126
1127 def test_Compute(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001128 zip_file = self.construct_zip_package_withValidPayload()
Tao Baob6304672018-03-08 16:28:33 -08001129 property_files = AbOtaPropertyFiles()
1130 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1131 property_files_string = property_files.Compute(zip_fp)
1132
1133 tokens = self._parse_property_files_string(property_files_string)
1134 # "6" indcludes the four entries above, one metadata entry, and one entry
1135 # for payload-metadata.bin.
1136 self.assertEqual(6, len(tokens))
1137 self._verify_entries(
1138 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1139
1140 def test_Finalize(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001141 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001142 property_files = AbOtaPropertyFiles()
1143 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001144 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001145 zip_fp, reserve_space=False)
1146 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1147
1148 tokens = self._parse_property_files_string(property_files_string)
1149 # "6" indcludes the four entries above, one metadata entry, and one entry
1150 # for payload-metadata.bin.
1151 self.assertEqual(6, len(tokens))
1152 self._verify_entries(
1153 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1154
1155 def test_Verify(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001156 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001157 property_files = AbOtaPropertyFiles()
1158 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001159 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001160 zip_fp, reserve_space=False)
1161
1162 property_files.Verify(zip_fp, raw_metadata)
1163
1164
Tao Baoc0746f42018-02-21 13:17:22 -08001165class NonAbOtaPropertyFilesTest(PropertyFilesTest):
1166 """Additional sanity checks specialized for NonAbOtaPropertyFiles."""
1167
1168 def test_init(self):
1169 property_files = NonAbOtaPropertyFiles()
1170 self.assertEqual('ota-property-files', property_files.name)
1171 self.assertEqual((), property_files.required)
1172 self.assertEqual((), property_files.optional)
1173
1174 def test_Compute(self):
1175 entries = ()
Tao Bao3bf8c652018-03-16 12:59:42 -07001176 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001177 property_files = NonAbOtaPropertyFiles()
1178 with zipfile.ZipFile(zip_file) as zip_fp:
1179 property_files_string = property_files.Compute(zip_fp)
1180
1181 tokens = self._parse_property_files_string(property_files_string)
1182 self.assertEqual(1, len(tokens))
1183 self._verify_entries(zip_file, tokens, entries)
1184
1185 def test_Finalize(self):
1186 entries = [
1187 'META-INF/com/android/metadata',
1188 ]
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:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001192 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001193 zip_fp, reserve_space=False)
1194 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1195 tokens = self._parse_property_files_string(property_files_string)
1196
1197 self.assertEqual(1, len(tokens))
1198 # 'META-INF/com/android/metadata' will be key'd as 'metadata'.
1199 entries[0] = 'metadata'
1200 self._verify_entries(zip_file, tokens, entries)
1201
1202 def test_Verify(self):
1203 entries = (
1204 'META-INF/com/android/metadata',
1205 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001206 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001207 property_files = NonAbOtaPropertyFiles()
1208 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001209 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001210 zip_fp, reserve_space=False)
1211
1212 property_files.Verify(zip_fp, raw_metadata)
1213
1214
Tao Bao65b94e92018-10-11 21:57:26 -07001215class PayloadSignerTest(test_utils.ReleaseToolsTestCase):
Tao Baofabe0832018-01-17 15:52:28 -08001216
1217 SIGFILE = 'sigfile.bin'
1218 SIGNED_SIGFILE = 'signed-sigfile.bin'
1219
1220 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001221 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baofabe0832018-01-17 15:52:28 -08001222 self.assertTrue(os.path.exists(self.testdata_dir))
1223
1224 common.OPTIONS.payload_signer = None
1225 common.OPTIONS.payload_signer_args = []
1226 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1227 common.OPTIONS.key_passwords = {
1228 common.OPTIONS.package_key : None,
1229 }
1230
Tao Baofabe0832018-01-17 15:52:28 -08001231 def _assertFilesEqual(self, file1, file2):
1232 with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
1233 self.assertEqual(fp1.read(), fp2.read())
1234
1235 def test_init(self):
1236 payload_signer = PayloadSigner()
1237 self.assertEqual('openssl', payload_signer.signer)
xunchang3c5de182019-04-08 23:04:58 -07001238 self.assertEqual(256, payload_signer.key_size)
Tao Baofabe0832018-01-17 15:52:28 -08001239
1240 def test_init_withPassword(self):
1241 common.OPTIONS.package_key = os.path.join(
1242 self.testdata_dir, 'testkey_with_passwd')
1243 common.OPTIONS.key_passwords = {
1244 common.OPTIONS.package_key : 'foo',
1245 }
1246 payload_signer = PayloadSigner()
1247 self.assertEqual('openssl', payload_signer.signer)
1248
1249 def test_init_withExternalSigner(self):
1250 common.OPTIONS.payload_signer = 'abc'
1251 common.OPTIONS.payload_signer_args = ['arg1', 'arg2']
xunchang3c5de182019-04-08 23:04:58 -07001252 common.OPTIONS.payload_signer_key_size = '512'
Tao Baofabe0832018-01-17 15:52:28 -08001253 payload_signer = PayloadSigner()
1254 self.assertEqual('abc', payload_signer.signer)
1255 self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args)
xunchang3c5de182019-04-08 23:04:58 -07001256 self.assertEqual(512, payload_signer.key_size)
1257
1258 def test_GetKeySizeInBytes_512Bytes(self):
1259 signing_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key')
1260 key_size = PayloadSigner._GetKeySizeInBytes(signing_key)
1261 self.assertEqual(512, key_size)
Tao Baofabe0832018-01-17 15:52:28 -08001262
1263 def test_Sign(self):
1264 payload_signer = PayloadSigner()
1265 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1266 signed_file = payload_signer.Sign(input_file)
1267
1268 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1269 self._assertFilesEqual(verify_file, signed_file)
1270
1271 def test_Sign_withExternalSigner_openssl(self):
1272 """Uses openssl as the external payload signer."""
1273 common.OPTIONS.payload_signer = 'openssl'
1274 common.OPTIONS.payload_signer_args = [
1275 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey',
1276 os.path.join(self.testdata_dir, 'testkey.pk8'),
1277 '-pkeyopt', 'digest:sha256']
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_script(self):
1286 """Uses testdata/payload_signer.sh as the external payload signer."""
1287 common.OPTIONS.payload_signer = os.path.join(
1288 self.testdata_dir, 'payload_signer.sh')
1289 common.OPTIONS.payload_signer_args = [
1290 os.path.join(self.testdata_dir, 'testkey.pk8')]
1291 payload_signer = PayloadSigner()
1292 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1293 signed_file = payload_signer.Sign(input_file)
1294
1295 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1296 self._assertFilesEqual(verify_file, signed_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001297
1298
Tao Bao65b94e92018-10-11 21:57:26 -07001299class PayloadTest(test_utils.ReleaseToolsTestCase):
Tao Baoc7b403a2018-01-30 18:19:04 -08001300
1301 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001302 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoc7b403a2018-01-30 18:19:04 -08001303 self.assertTrue(os.path.exists(self.testdata_dir))
1304
1305 common.OPTIONS.wipe_user_data = False
1306 common.OPTIONS.payload_signer = None
1307 common.OPTIONS.payload_signer_args = None
1308 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1309 common.OPTIONS.key_passwords = {
1310 common.OPTIONS.package_key : None,
1311 }
1312
Tao Baoc7b403a2018-01-30 18:19:04 -08001313 @staticmethod
Tao Baof7140c02018-01-30 17:09:24 -08001314 def _create_payload_full(secondary=False):
1315 target_file = construct_target_files(secondary)
Tao Bao667ff572018-02-10 00:02:40 -08001316 payload = Payload(secondary)
Tao Baoc7b403a2018-01-30 18:19:04 -08001317 payload.Generate(target_file)
1318 return payload
1319
Tao Baof7140c02018-01-30 17:09:24 -08001320 @staticmethod
1321 def _create_payload_incremental():
1322 target_file = construct_target_files()
1323 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001324 payload = Payload()
1325 payload.Generate(target_file, source_file)
1326 return payload
1327
1328 def test_Generate_full(self):
1329 payload = self._create_payload_full()
1330 self.assertTrue(os.path.exists(payload.payload_file))
1331
1332 def test_Generate_incremental(self):
1333 payload = self._create_payload_incremental()
1334 self.assertTrue(os.path.exists(payload.payload_file))
1335
1336 def test_Generate_additionalArgs(self):
Tao Baof7140c02018-01-30 17:09:24 -08001337 target_file = construct_target_files()
1338 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001339 payload = Payload()
1340 # This should work the same as calling payload.Generate(target_file,
1341 # source_file).
1342 payload.Generate(
1343 target_file, additional_args=["--source_image", source_file])
1344 self.assertTrue(os.path.exists(payload.payload_file))
1345
1346 def test_Generate_invalidInput(self):
Tao Baof7140c02018-01-30 17:09:24 -08001347 target_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001348 common.ZipDelete(target_file, 'IMAGES/vendor.img')
1349 payload = Payload()
Tao Baobec89c12018-10-15 11:53:28 -07001350 self.assertRaises(common.ExternalError, payload.Generate, target_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001351
1352 def test_Sign_full(self):
1353 payload = self._create_payload_full()
1354 payload.Sign(PayloadSigner())
1355
1356 output_file = common.MakeTempFile(suffix='.zip')
1357 with zipfile.ZipFile(output_file, 'w') as output_zip:
1358 payload.WriteToZip(output_zip)
1359
1360 import check_ota_package_signature
1361 check_ota_package_signature.VerifyAbOtaPayload(
1362 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1363 output_file)
1364
1365 def test_Sign_incremental(self):
1366 payload = self._create_payload_incremental()
1367 payload.Sign(PayloadSigner())
1368
1369 output_file = common.MakeTempFile(suffix='.zip')
1370 with zipfile.ZipFile(output_file, 'w') as output_zip:
1371 payload.WriteToZip(output_zip)
1372
1373 import check_ota_package_signature
1374 check_ota_package_signature.VerifyAbOtaPayload(
1375 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1376 output_file)
1377
1378 def test_Sign_withDataWipe(self):
1379 common.OPTIONS.wipe_user_data = True
1380 payload = self._create_payload_full()
1381 payload.Sign(PayloadSigner())
1382
1383 with open(payload.payload_properties) as properties_fp:
1384 self.assertIn("POWERWASH=1", properties_fp.read())
1385
Tao Bao667ff572018-02-10 00:02:40 -08001386 def test_Sign_secondary(self):
1387 payload = self._create_payload_full(secondary=True)
1388 payload.Sign(PayloadSigner())
1389
1390 with open(payload.payload_properties) as properties_fp:
1391 self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read())
1392
Tao Baoc7b403a2018-01-30 18:19:04 -08001393 def test_Sign_badSigner(self):
1394 """Tests that signing failure can be captured."""
1395 payload = self._create_payload_full()
1396 payload_signer = PayloadSigner()
1397 payload_signer.signer_args.append('bad-option')
Tao Baobec89c12018-10-15 11:53:28 -07001398 self.assertRaises(common.ExternalError, payload.Sign, payload_signer)
Tao Baoc7b403a2018-01-30 18:19:04 -08001399
1400 def test_WriteToZip(self):
1401 payload = self._create_payload_full()
1402 payload.Sign(PayloadSigner())
1403
1404 output_file = common.MakeTempFile(suffix='.zip')
1405 with zipfile.ZipFile(output_file, 'w') as output_zip:
1406 payload.WriteToZip(output_zip)
1407
1408 with zipfile.ZipFile(output_file) as verify_zip:
1409 # First make sure we have the essential entries.
1410 namelist = verify_zip.namelist()
1411 self.assertIn(Payload.PAYLOAD_BIN, namelist)
1412 self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist)
1413
1414 # Then assert these entries are stored.
1415 for entry_info in verify_zip.infolist():
1416 if entry_info.filename not in (Payload.PAYLOAD_BIN,
1417 Payload.PAYLOAD_PROPERTIES_TXT):
1418 continue
1419 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)
1420
1421 def test_WriteToZip_unsignedPayload(self):
1422 """Unsigned payloads should not be allowed to be written to zip."""
1423 payload = self._create_payload_full()
1424
1425 output_file = common.MakeTempFile(suffix='.zip')
1426 with zipfile.ZipFile(output_file, 'w') as output_zip:
1427 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
1428
1429 # Also test with incremental payload.
1430 payload = self._create_payload_incremental()
1431
1432 output_file = common.MakeTempFile(suffix='.zip')
1433 with zipfile.ZipFile(output_file, 'w') as output_zip:
1434 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001435
1436 def test_WriteToZip_secondary(self):
1437 payload = self._create_payload_full(secondary=True)
1438 payload.Sign(PayloadSigner())
1439
1440 output_file = common.MakeTempFile(suffix='.zip')
1441 with zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Bao667ff572018-02-10 00:02:40 -08001442 payload.WriteToZip(output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001443
1444 with zipfile.ZipFile(output_file) as verify_zip:
1445 # First make sure we have the essential entries.
1446 namelist = verify_zip.namelist()
1447 self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist)
1448 self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist)
1449
1450 # Then assert these entries are stored.
1451 for entry_info in verify_zip.infolist():
1452 if entry_info.filename not in (
1453 Payload.SECONDARY_PAYLOAD_BIN,
1454 Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT):
1455 continue
1456 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)