blob: f75b3a75bea32218c68a3d4a8f84475bb584a280 [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
418 common.OPTIONS.timestamp = False
419 common.OPTIONS.wipe_user_data = False
Tao Bao3bf8c652018-03-16 12:59:42 -0700420 common.OPTIONS.no_signing = False
421 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
422 common.OPTIONS.key_passwords = {
423 common.OPTIONS.package_key : None,
424 }
425
426 common.OPTIONS.search_path = test_utils.get_search_path()
427 self.assertIsNotNone(common.OPTIONS.search_path)
Tao Baodf3a48b2018-01-10 16:30:43 -0800428
429 def test_GetPackageMetadata_abOta_full(self):
430 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
431 target_info_dict['ab_update'] = 'true'
432 target_info = BuildInfo(target_info_dict, None)
433 metadata = GetPackageMetadata(target_info)
434 self.assertDictEqual(
435 {
436 'ota-type' : 'AB',
437 'ota-required-cache' : '0',
438 'post-build' : 'build-fingerprint-target',
439 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800440 'post-sdk-level' : '27',
441 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800442 'post-timestamp' : '1500000000',
443 'pre-device' : 'product-device',
444 },
445 metadata)
446
447 def test_GetPackageMetadata_abOta_incremental(self):
448 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
449 target_info_dict['ab_update'] = 'true'
450 target_info = BuildInfo(target_info_dict, None)
451 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
452 common.OPTIONS.incremental_source = ''
453 metadata = GetPackageMetadata(target_info, source_info)
454 self.assertDictEqual(
455 {
456 'ota-type' : 'AB',
457 'ota-required-cache' : '0',
458 'post-build' : 'build-fingerprint-target',
459 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800460 'post-sdk-level' : '27',
461 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800462 'post-timestamp' : '1500000000',
463 'pre-device' : 'product-device',
464 'pre-build' : 'build-fingerprint-source',
465 'pre-build-incremental' : 'build-version-incremental-source',
466 },
467 metadata)
468
469 def test_GetPackageMetadata_nonAbOta_full(self):
470 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
471 metadata = GetPackageMetadata(target_info)
472 self.assertDictEqual(
473 {
474 'ota-type' : 'BLOCK',
475 'post-build' : 'build-fingerprint-target',
476 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800477 'post-sdk-level' : '27',
478 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800479 'post-timestamp' : '1500000000',
480 'pre-device' : 'product-device',
481 },
482 metadata)
483
484 def test_GetPackageMetadata_nonAbOta_incremental(self):
485 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
486 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
487 common.OPTIONS.incremental_source = ''
488 metadata = GetPackageMetadata(target_info, source_info)
489 self.assertDictEqual(
490 {
491 'ota-type' : 'BLOCK',
492 'post-build' : 'build-fingerprint-target',
493 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800494 'post-sdk-level' : '27',
495 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800496 'post-timestamp' : '1500000000',
497 'pre-device' : 'product-device',
498 'pre-build' : 'build-fingerprint-source',
499 'pre-build-incremental' : 'build-version-incremental-source',
500 },
501 metadata)
502
503 def test_GetPackageMetadata_wipe(self):
504 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
505 common.OPTIONS.wipe_user_data = True
506 metadata = GetPackageMetadata(target_info)
507 self.assertDictEqual(
508 {
509 'ota-type' : 'BLOCK',
510 'ota-wipe' : 'yes',
511 'post-build' : 'build-fingerprint-target',
512 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800513 'post-sdk-level' : '27',
514 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800515 'post-timestamp' : '1500000000',
516 'pre-device' : 'product-device',
517 },
518 metadata)
519
520 @staticmethod
521 def _test_GetPackageMetadata_swapBuildTimestamps(target_info, source_info):
522 (target_info['build.prop']['ro.build.date.utc'],
523 source_info['build.prop']['ro.build.date.utc']) = (
524 source_info['build.prop']['ro.build.date.utc'],
525 target_info['build.prop']['ro.build.date.utc'])
526
527 def test_GetPackageMetadata_unintentionalDowngradeDetected(self):
528 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
529 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
530 self._test_GetPackageMetadata_swapBuildTimestamps(
531 target_info_dict, source_info_dict)
532
533 target_info = BuildInfo(target_info_dict, None)
534 source_info = BuildInfo(source_info_dict, None)
535 common.OPTIONS.incremental_source = ''
536 self.assertRaises(RuntimeError, GetPackageMetadata, target_info,
537 source_info)
538
539 def test_GetPackageMetadata_downgrade(self):
540 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
541 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
542 self._test_GetPackageMetadata_swapBuildTimestamps(
543 target_info_dict, source_info_dict)
544
545 target_info = BuildInfo(target_info_dict, None)
546 source_info = BuildInfo(source_info_dict, None)
547 common.OPTIONS.incremental_source = ''
548 common.OPTIONS.downgrade = True
549 common.OPTIONS.wipe_user_data = True
550 metadata = GetPackageMetadata(target_info, source_info)
551 self.assertDictEqual(
552 {
553 'ota-downgrade' : 'yes',
554 'ota-type' : 'BLOCK',
555 'ota-wipe' : 'yes',
556 'post-build' : 'build-fingerprint-target',
557 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800558 'post-sdk-level' : '27',
559 'post-security-patch-level' : '2017-12-01',
Tao Baofaa8e0b2018-04-12 14:31:43 -0700560 'post-timestamp' : '1400000000',
Tao Baodf3a48b2018-01-10 16:30:43 -0800561 'pre-device' : 'product-device',
562 'pre-build' : 'build-fingerprint-source',
563 'pre-build-incremental' : 'build-version-incremental-source',
564 },
565 metadata)
Tao Baofabe0832018-01-17 15:52:28 -0800566
Tao Baof7140c02018-01-30 17:09:24 -0800567 def test_GetTargetFilesZipForSecondaryImages(self):
568 input_file = construct_target_files(secondary=True)
569 target_file = GetTargetFilesZipForSecondaryImages(input_file)
570
571 with zipfile.ZipFile(target_file) as verify_zip:
572 namelist = verify_zip.namelist()
573
574 self.assertIn('META/ab_partitions.txt', namelist)
575 self.assertIn('IMAGES/boot.img', namelist)
576 self.assertIn('IMAGES/system.img', namelist)
577 self.assertIn('IMAGES/vendor.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700578 self.assertIn('RADIO/bootloader.img', namelist)
579 self.assertIn('RADIO/modem.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800580 self.assertIn(POSTINSTALL_CONFIG, namelist)
Tao Baof7140c02018-01-30 17:09:24 -0800581
582 self.assertNotIn('IMAGES/system_other.img', namelist)
583 self.assertNotIn('IMAGES/system.map', namelist)
584
Tao Bao15a146a2018-02-21 16:06:59 -0800585 def test_GetTargetFilesZipForSecondaryImages_skipPostinstall(self):
586 input_file = construct_target_files(secondary=True)
587 target_file = GetTargetFilesZipForSecondaryImages(
588 input_file, skip_postinstall=True)
589
590 with zipfile.ZipFile(target_file) as verify_zip:
591 namelist = verify_zip.namelist()
592
593 self.assertIn('META/ab_partitions.txt', namelist)
594 self.assertIn('IMAGES/boot.img', namelist)
595 self.assertIn('IMAGES/system.img', namelist)
596 self.assertIn('IMAGES/vendor.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700597 self.assertIn('RADIO/bootloader.img', namelist)
598 self.assertIn('RADIO/modem.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800599
600 self.assertNotIn('IMAGES/system_other.img', namelist)
601 self.assertNotIn('IMAGES/system.map', namelist)
602 self.assertNotIn(POSTINSTALL_CONFIG, namelist)
603
Tao Bao12489802018-07-12 14:47:38 -0700604 def test_GetTargetFilesZipForSecondaryImages_withoutRadioImages(self):
605 input_file = construct_target_files(secondary=True)
606 common.ZipDelete(input_file, 'RADIO/bootloader.img')
607 common.ZipDelete(input_file, 'RADIO/modem.img')
608 target_file = GetTargetFilesZipForSecondaryImages(input_file)
609
610 with zipfile.ZipFile(target_file) as verify_zip:
611 namelist = verify_zip.namelist()
612
613 self.assertIn('META/ab_partitions.txt', namelist)
614 self.assertIn('IMAGES/boot.img', namelist)
615 self.assertIn('IMAGES/system.img', namelist)
616 self.assertIn('IMAGES/vendor.img', namelist)
617 self.assertIn(POSTINSTALL_CONFIG, namelist)
618
619 self.assertNotIn('IMAGES/system_other.img', namelist)
620 self.assertNotIn('IMAGES/system.map', namelist)
621 self.assertNotIn('RADIO/bootloader.img', namelist)
622 self.assertNotIn('RADIO/modem.img', namelist)
623
Tao Bao15a146a2018-02-21 16:06:59 -0800624 def test_GetTargetFilesZipWithoutPostinstallConfig(self):
625 input_file = construct_target_files()
626 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
627 with zipfile.ZipFile(target_file) as verify_zip:
628 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
629
630 def test_GetTargetFilesZipWithoutPostinstallConfig_missingEntry(self):
631 input_file = construct_target_files()
632 common.ZipDelete(input_file, POSTINSTALL_CONFIG)
633 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
634 with zipfile.ZipFile(target_file) as verify_zip:
635 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
636
Tao Bao3bf8c652018-03-16 12:59:42 -0700637 def _test_FinalizeMetadata(self, large_entry=False):
638 entries = [
639 'required-entry1',
640 'required-entry2',
641 ]
642 zip_file = PropertyFilesTest.construct_zip_package(entries)
643 # Add a large entry of 1 GiB if requested.
644 if large_entry:
645 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
646 zip_fp.writestr(
647 # Using 'zoo' so that the entry stays behind others after signing.
648 'zoo',
649 'A' * 1024 * 1024 * 1024,
650 zipfile.ZIP_STORED)
651
652 metadata = {}
653 output_file = common.MakeTempFile(suffix='.zip')
654 needed_property_files = (
655 TestPropertyFiles(),
656 )
657 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
658 self.assertIn('ota-test-property-files', metadata)
659
660 def test_FinalizeMetadata(self):
661 self._test_FinalizeMetadata()
662
663 def test_FinalizeMetadata_withNoSigning(self):
664 common.OPTIONS.no_signing = True
665 self._test_FinalizeMetadata()
666
667 def test_FinalizeMetadata_largeEntry(self):
668 self._test_FinalizeMetadata(large_entry=True)
669
670 def test_FinalizeMetadata_largeEntry_withNoSigning(self):
671 common.OPTIONS.no_signing = True
672 self._test_FinalizeMetadata(large_entry=True)
673
674 def test_FinalizeMetadata_insufficientSpace(self):
675 entries = [
676 'required-entry1',
677 'required-entry2',
678 'optional-entry1',
679 'optional-entry2',
680 ]
681 zip_file = PropertyFilesTest.construct_zip_package(entries)
682 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
683 zip_fp.writestr(
684 # 'foo-entry1' will appear ahead of all other entries (in alphabetical
685 # order) after the signing, which will in turn trigger the
686 # InsufficientSpaceException and an automatic retry.
687 'foo-entry1',
688 'A' * 1024 * 1024,
689 zipfile.ZIP_STORED)
690
691 metadata = {}
692 needed_property_files = (
693 TestPropertyFiles(),
694 )
695 output_file = common.MakeTempFile(suffix='.zip')
696 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
697 self.assertIn('ota-test-property-files', metadata)
698
Tao Baoae5e4c32018-03-01 19:30:00 -0800699
Tao Bao69203522018-03-08 16:09:01 -0800700class TestPropertyFiles(PropertyFiles):
701 """A class that extends PropertyFiles for testing purpose."""
702
703 def __init__(self):
704 super(TestPropertyFiles, self).__init__()
705 self.name = 'ota-test-property-files'
706 self.required = (
707 'required-entry1',
708 'required-entry2',
709 )
710 self.optional = (
711 'optional-entry1',
712 'optional-entry2',
713 )
714
715
Tao Bao65b94e92018-10-11 21:57:26 -0700716class PropertyFilesTest(test_utils.ReleaseToolsTestCase):
Tao Baoae5e4c32018-03-01 19:30:00 -0800717
Tao Bao3bf8c652018-03-16 12:59:42 -0700718 def setUp(self):
719 common.OPTIONS.no_signing = False
720
Tao Baof5110492018-03-02 09:47:43 -0800721 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -0700722 def construct_zip_package(entries):
Tao Baof5110492018-03-02 09:47:43 -0800723 zip_file = common.MakeTempFile(suffix='.zip')
724 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
725 for entry in entries:
726 zip_fp.writestr(
727 entry,
728 entry.replace('.', '-').upper(),
729 zipfile.ZIP_STORED)
730 return zip_file
731
732 @staticmethod
Tao Bao69203522018-03-08 16:09:01 -0800733 def _parse_property_files_string(data):
Tao Baof5110492018-03-02 09:47:43 -0800734 result = {}
735 for token in data.split(','):
736 name, info = token.split(':', 1)
737 result[name] = info
738 return result
739
740 def _verify_entries(self, input_file, tokens, entries):
741 for entry in entries:
742 offset, size = map(int, tokens[entry].split(':'))
743 with open(input_file, 'rb') as input_fp:
744 input_fp.seek(offset)
745 if entry == 'metadata':
746 expected = b'META-INF/COM/ANDROID/METADATA'
747 else:
748 expected = entry.replace('.', '-').upper().encode()
749 self.assertEqual(expected, input_fp.read(size))
750
Tao Baoae5e4c32018-03-01 19:30:00 -0800751 def test_Compute(self):
Tao Baof5110492018-03-02 09:47:43 -0800752 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800753 'required-entry1',
754 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800755 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700756 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800757 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800758 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800759 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800760
Tao Bao69203522018-03-08 16:09:01 -0800761 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800762 self.assertEqual(3, len(tokens))
763 self._verify_entries(zip_file, tokens, entries)
764
Tao Bao69203522018-03-08 16:09:01 -0800765 def test_Compute_withOptionalEntries(self):
Tao Baof5110492018-03-02 09:47:43 -0800766 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800767 'required-entry1',
768 'required-entry2',
769 'optional-entry1',
770 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800771 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700772 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800773 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800774 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800775 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800776
Tao Bao69203522018-03-08 16:09:01 -0800777 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800778 self.assertEqual(5, len(tokens))
779 self._verify_entries(zip_file, tokens, entries)
780
Tao Bao69203522018-03-08 16:09:01 -0800781 def test_Compute_missingRequiredEntry(self):
782 entries = (
783 'required-entry2',
784 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700785 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800786 property_files = TestPropertyFiles()
787 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
788 self.assertRaises(KeyError, property_files.Compute, zip_fp)
789
Tao Baoae5e4c32018-03-01 19:30:00 -0800790 def test_Finalize(self):
Tao Baof5110492018-03-02 09:47:43 -0800791 entries = [
Tao Bao69203522018-03-08 16:09:01 -0800792 'required-entry1',
793 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800794 'META-INF/com/android/metadata',
795 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700796 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800797 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800798 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700799 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800800 zip_fp, reserve_space=False)
801 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
Tao Bao69203522018-03-08 16:09:01 -0800802 tokens = self._parse_property_files_string(streaming_metadata)
Tao Baof5110492018-03-02 09:47:43 -0800803
804 self.assertEqual(3, len(tokens))
805 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
806 # streaming metadata.
807 entries[2] = 'metadata'
808 self._verify_entries(zip_file, tokens, entries)
809
Tao Baoae5e4c32018-03-01 19:30:00 -0800810 def test_Finalize_assertReservedLength(self):
Tao Baof5110492018-03-02 09:47:43 -0800811 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800812 'required-entry1',
813 'required-entry2',
814 'optional-entry1',
815 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800816 'META-INF/com/android/metadata',
817 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700818 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800819 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800820 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
821 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700822 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800823 zip_fp, reserve_space=False)
Tao Baof5110492018-03-02 09:47:43 -0800824 raw_length = len(raw_metadata)
825
826 # Now pass in the exact expected length.
Tao Baoae5e4c32018-03-01 19:30:00 -0800827 streaming_metadata = property_files.Finalize(zip_fp, raw_length)
Tao Baof5110492018-03-02 09:47:43 -0800828 self.assertEqual(raw_length, len(streaming_metadata))
829
830 # Or pass in insufficient length.
831 self.assertRaises(
Tao Bao3bf8c652018-03-16 12:59:42 -0700832 PropertyFiles.InsufficientSpaceException,
Tao Baoae5e4c32018-03-01 19:30:00 -0800833 property_files.Finalize,
Tao Baof5110492018-03-02 09:47:43 -0800834 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800835 raw_length - 1)
Tao Baof5110492018-03-02 09:47:43 -0800836
837 # Or pass in a much larger size.
Tao Baoae5e4c32018-03-01 19:30:00 -0800838 streaming_metadata = property_files.Finalize(
Tao Baof5110492018-03-02 09:47:43 -0800839 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800840 raw_length + 20)
Tao Baof5110492018-03-02 09:47:43 -0800841 self.assertEqual(raw_length + 20, len(streaming_metadata))
842 self.assertEqual(' ' * 20, streaming_metadata[raw_length:])
843
Tao Baoae5e4c32018-03-01 19:30:00 -0800844 def test_Verify(self):
845 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800846 'required-entry1',
847 'required-entry2',
848 'optional-entry1',
849 'optional-entry2',
850 'META-INF/com/android/metadata',
851 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700852 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800853 property_files = TestPropertyFiles()
854 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
855 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700856 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800857 zip_fp, reserve_space=False)
858
859 # Should pass the test if verification passes.
860 property_files.Verify(zip_fp, raw_metadata)
861
862 # Or raise on verification failure.
863 self.assertRaises(
864 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
865
866
867class StreamingPropertyFilesTest(PropertyFilesTest):
868 """Additional sanity checks specialized for StreamingPropertyFiles."""
869
870 def test_init(self):
871 property_files = StreamingPropertyFiles()
872 self.assertEqual('ota-streaming-property-files', property_files.name)
873 self.assertEqual(
874 (
875 'payload.bin',
876 'payload_properties.txt',
877 ),
878 property_files.required)
879 self.assertEqual(
880 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -0700881 'care_map.pb',
Tao Bao69203522018-03-08 16:09:01 -0800882 'care_map.txt',
883 'compatibility.zip',
884 ),
885 property_files.optional)
886
887 def test_Compute(self):
888 entries = (
Tao Baoae5e4c32018-03-01 19:30:00 -0800889 'payload.bin',
890 'payload_properties.txt',
891 'care_map.txt',
Tao Bao69203522018-03-08 16:09:01 -0800892 'compatibility.zip',
893 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700894 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800895 property_files = StreamingPropertyFiles()
896 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
897 property_files_string = property_files.Compute(zip_fp)
898
899 tokens = self._parse_property_files_string(property_files_string)
900 self.assertEqual(5, len(tokens))
901 self._verify_entries(zip_file, tokens, entries)
902
903 def test_Finalize(self):
904 entries = [
905 'payload.bin',
906 'payload_properties.txt',
907 'care_map.txt',
908 'compatibility.zip',
909 'META-INF/com/android/metadata',
910 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700911 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800912 property_files = StreamingPropertyFiles()
913 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700914 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800915 zip_fp, reserve_space=False)
916 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
917 tokens = self._parse_property_files_string(streaming_metadata)
918
919 self.assertEqual(5, len(tokens))
920 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
921 # streaming metadata.
922 entries[4] = 'metadata'
923 self._verify_entries(zip_file, tokens, entries)
924
925 def test_Verify(self):
926 entries = (
927 'payload.bin',
928 'payload_properties.txt',
929 'care_map.txt',
930 'compatibility.zip',
Tao Baoae5e4c32018-03-01 19:30:00 -0800931 'META-INF/com/android/metadata',
932 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700933 zip_file = self.construct_zip_package(entries)
Tao Baoae5e4c32018-03-01 19:30:00 -0800934 property_files = StreamingPropertyFiles()
935 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
936 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700937 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800938 zip_fp, reserve_space=False)
939
940 # Should pass the test if verification passes.
941 property_files.Verify(zip_fp, raw_metadata)
942
943 # Or raise on verification failure.
944 self.assertRaises(
945 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
946
Tao Baofabe0832018-01-17 15:52:28 -0800947
Tao Baob6304672018-03-08 16:28:33 -0800948class AbOtaPropertyFilesTest(PropertyFilesTest):
949 """Additional sanity checks specialized for AbOtaPropertyFiles."""
950
951 # The size for payload and metadata signature size.
952 SIGNATURE_SIZE = 256
953
954 def setUp(self):
955 self.testdata_dir = test_utils.get_testdata_dir()
956 self.assertTrue(os.path.exists(self.testdata_dir))
957
958 common.OPTIONS.wipe_user_data = False
959 common.OPTIONS.payload_signer = None
960 common.OPTIONS.payload_signer_args = None
961 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
962 common.OPTIONS.key_passwords = {
963 common.OPTIONS.package_key : None,
964 }
965
966 def test_init(self):
967 property_files = AbOtaPropertyFiles()
968 self.assertEqual('ota-property-files', property_files.name)
969 self.assertEqual(
970 (
971 'payload.bin',
972 'payload_properties.txt',
973 ),
974 property_files.required)
975 self.assertEqual(
976 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -0700977 'care_map.pb',
Tao Baob6304672018-03-08 16:28:33 -0800978 'care_map.txt',
979 'compatibility.zip',
980 ),
981 property_files.optional)
982
983 def test_GetPayloadMetadataOffsetAndSize(self):
984 target_file = construct_target_files()
985 payload = Payload()
986 payload.Generate(target_file)
987
988 payload_signer = PayloadSigner()
989 payload.Sign(payload_signer)
990
991 output_file = common.MakeTempFile(suffix='.zip')
992 with zipfile.ZipFile(output_file, 'w') as output_zip:
993 payload.WriteToZip(output_zip)
994
995 # Find out the payload metadata offset and size.
996 property_files = AbOtaPropertyFiles()
997 with zipfile.ZipFile(output_file) as input_zip:
998 # pylint: disable=protected-access
999 payload_offset, metadata_total = (
1000 property_files._GetPayloadMetadataOffsetAndSize(input_zip))
1001
1002 # Read in the metadata signature directly.
1003 with open(output_file, 'rb') as verify_fp:
1004 verify_fp.seek(payload_offset + metadata_total - self.SIGNATURE_SIZE)
1005 metadata_signature = verify_fp.read(self.SIGNATURE_SIZE)
1006
1007 # Now we extract the metadata hash via brillo_update_payload script, which
1008 # will serve as the oracle result.
1009 payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1010 metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1011 cmd = ['brillo_update_payload', 'hash',
1012 '--unsigned_payload', payload.payload_file,
1013 '--signature_size', str(self.SIGNATURE_SIZE),
1014 '--metadata_hash_file', metadata_sig_file,
1015 '--payload_hash_file', payload_sig_file]
Tao Bao73dd4f42018-10-04 16:25:33 -07001016 proc = common.Run(cmd)
Tao Baob6304672018-03-08 16:28:33 -08001017 stdoutdata, _ = proc.communicate()
1018 self.assertEqual(
1019 0, proc.returncode,
Tao Bao73dd4f42018-10-04 16:25:33 -07001020 'Failed to run brillo_update_payload:\n{}'.format(stdoutdata))
Tao Baob6304672018-03-08 16:28:33 -08001021
1022 signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file)
1023
1024 # Finally we can compare the two signatures.
1025 with open(signed_metadata_sig_file, 'rb') as verify_fp:
1026 self.assertEqual(verify_fp.read(), metadata_signature)
1027
1028 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -07001029 def construct_zip_package_withValidPayload(with_metadata=False):
1030 # Cannot use construct_zip_package() since we need a "valid" payload.bin.
Tao Baob6304672018-03-08 16:28:33 -08001031 target_file = construct_target_files()
1032 payload = Payload()
1033 payload.Generate(target_file)
1034
1035 payload_signer = PayloadSigner()
1036 payload.Sign(payload_signer)
1037
1038 zip_file = common.MakeTempFile(suffix='.zip')
1039 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
1040 # 'payload.bin',
1041 payload.WriteToZip(zip_fp)
1042
1043 # Other entries.
1044 entries = ['care_map.txt', 'compatibility.zip']
1045
1046 # Put META-INF/com/android/metadata if needed.
1047 if with_metadata:
1048 entries.append('META-INF/com/android/metadata')
1049
1050 for entry in entries:
1051 zip_fp.writestr(
1052 entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED)
1053
1054 return zip_file
1055
1056 def test_Compute(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001057 zip_file = self.construct_zip_package_withValidPayload()
Tao Baob6304672018-03-08 16:28:33 -08001058 property_files = AbOtaPropertyFiles()
1059 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1060 property_files_string = property_files.Compute(zip_fp)
1061
1062 tokens = self._parse_property_files_string(property_files_string)
1063 # "6" indcludes the four entries above, one metadata entry, and one entry
1064 # for payload-metadata.bin.
1065 self.assertEqual(6, len(tokens))
1066 self._verify_entries(
1067 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1068
1069 def test_Finalize(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001070 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001071 property_files = AbOtaPropertyFiles()
1072 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001073 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001074 zip_fp, reserve_space=False)
1075 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1076
1077 tokens = self._parse_property_files_string(property_files_string)
1078 # "6" indcludes the four entries above, one metadata entry, and one entry
1079 # for payload-metadata.bin.
1080 self.assertEqual(6, len(tokens))
1081 self._verify_entries(
1082 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1083
1084 def test_Verify(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001085 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001086 property_files = AbOtaPropertyFiles()
1087 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001088 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001089 zip_fp, reserve_space=False)
1090
1091 property_files.Verify(zip_fp, raw_metadata)
1092
1093
Tao Baoc0746f42018-02-21 13:17:22 -08001094class NonAbOtaPropertyFilesTest(PropertyFilesTest):
1095 """Additional sanity checks specialized for NonAbOtaPropertyFiles."""
1096
1097 def test_init(self):
1098 property_files = NonAbOtaPropertyFiles()
1099 self.assertEqual('ota-property-files', property_files.name)
1100 self.assertEqual((), property_files.required)
1101 self.assertEqual((), property_files.optional)
1102
1103 def test_Compute(self):
1104 entries = ()
Tao Bao3bf8c652018-03-16 12:59:42 -07001105 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001106 property_files = NonAbOtaPropertyFiles()
1107 with zipfile.ZipFile(zip_file) as zip_fp:
1108 property_files_string = property_files.Compute(zip_fp)
1109
1110 tokens = self._parse_property_files_string(property_files_string)
1111 self.assertEqual(1, len(tokens))
1112 self._verify_entries(zip_file, tokens, entries)
1113
1114 def test_Finalize(self):
1115 entries = [
1116 'META-INF/com/android/metadata',
1117 ]
Tao Bao3bf8c652018-03-16 12:59:42 -07001118 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001119 property_files = NonAbOtaPropertyFiles()
1120 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001121 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001122 zip_fp, reserve_space=False)
1123 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1124 tokens = self._parse_property_files_string(property_files_string)
1125
1126 self.assertEqual(1, len(tokens))
1127 # 'META-INF/com/android/metadata' will be key'd as 'metadata'.
1128 entries[0] = 'metadata'
1129 self._verify_entries(zip_file, tokens, entries)
1130
1131 def test_Verify(self):
1132 entries = (
1133 'META-INF/com/android/metadata',
1134 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001135 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001136 property_files = NonAbOtaPropertyFiles()
1137 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001138 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001139 zip_fp, reserve_space=False)
1140
1141 property_files.Verify(zip_fp, raw_metadata)
1142
1143
Tao Bao65b94e92018-10-11 21:57:26 -07001144class PayloadSignerTest(test_utils.ReleaseToolsTestCase):
Tao Baofabe0832018-01-17 15:52:28 -08001145
1146 SIGFILE = 'sigfile.bin'
1147 SIGNED_SIGFILE = 'signed-sigfile.bin'
1148
1149 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001150 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baofabe0832018-01-17 15:52:28 -08001151 self.assertTrue(os.path.exists(self.testdata_dir))
1152
1153 common.OPTIONS.payload_signer = None
1154 common.OPTIONS.payload_signer_args = []
1155 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1156 common.OPTIONS.key_passwords = {
1157 common.OPTIONS.package_key : None,
1158 }
1159
Tao Baofabe0832018-01-17 15:52:28 -08001160 def _assertFilesEqual(self, file1, file2):
1161 with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
1162 self.assertEqual(fp1.read(), fp2.read())
1163
1164 def test_init(self):
1165 payload_signer = PayloadSigner()
1166 self.assertEqual('openssl', payload_signer.signer)
1167
1168 def test_init_withPassword(self):
1169 common.OPTIONS.package_key = os.path.join(
1170 self.testdata_dir, 'testkey_with_passwd')
1171 common.OPTIONS.key_passwords = {
1172 common.OPTIONS.package_key : 'foo',
1173 }
1174 payload_signer = PayloadSigner()
1175 self.assertEqual('openssl', payload_signer.signer)
1176
1177 def test_init_withExternalSigner(self):
1178 common.OPTIONS.payload_signer = 'abc'
1179 common.OPTIONS.payload_signer_args = ['arg1', 'arg2']
1180 payload_signer = PayloadSigner()
1181 self.assertEqual('abc', payload_signer.signer)
1182 self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args)
1183
1184 def test_Sign(self):
1185 payload_signer = PayloadSigner()
1186 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1187 signed_file = payload_signer.Sign(input_file)
1188
1189 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1190 self._assertFilesEqual(verify_file, signed_file)
1191
1192 def test_Sign_withExternalSigner_openssl(self):
1193 """Uses openssl as the external payload signer."""
1194 common.OPTIONS.payload_signer = 'openssl'
1195 common.OPTIONS.payload_signer_args = [
1196 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey',
1197 os.path.join(self.testdata_dir, 'testkey.pk8'),
1198 '-pkeyopt', 'digest:sha256']
1199 payload_signer = PayloadSigner()
1200 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1201 signed_file = payload_signer.Sign(input_file)
1202
1203 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1204 self._assertFilesEqual(verify_file, signed_file)
1205
1206 def test_Sign_withExternalSigner_script(self):
1207 """Uses testdata/payload_signer.sh as the external payload signer."""
1208 common.OPTIONS.payload_signer = os.path.join(
1209 self.testdata_dir, 'payload_signer.sh')
1210 common.OPTIONS.payload_signer_args = [
1211 os.path.join(self.testdata_dir, 'testkey.pk8')]
1212 payload_signer = PayloadSigner()
1213 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1214 signed_file = payload_signer.Sign(input_file)
1215
1216 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1217 self._assertFilesEqual(verify_file, signed_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001218
1219
Tao Bao65b94e92018-10-11 21:57:26 -07001220class PayloadTest(test_utils.ReleaseToolsTestCase):
Tao Baoc7b403a2018-01-30 18:19:04 -08001221
1222 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001223 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoc7b403a2018-01-30 18:19:04 -08001224 self.assertTrue(os.path.exists(self.testdata_dir))
1225
1226 common.OPTIONS.wipe_user_data = False
1227 common.OPTIONS.payload_signer = None
1228 common.OPTIONS.payload_signer_args = None
1229 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1230 common.OPTIONS.key_passwords = {
1231 common.OPTIONS.package_key : None,
1232 }
1233
Tao Baoc7b403a2018-01-30 18:19:04 -08001234 @staticmethod
Tao Baof7140c02018-01-30 17:09:24 -08001235 def _create_payload_full(secondary=False):
1236 target_file = construct_target_files(secondary)
Tao Bao667ff572018-02-10 00:02:40 -08001237 payload = Payload(secondary)
Tao Baoc7b403a2018-01-30 18:19:04 -08001238 payload.Generate(target_file)
1239 return payload
1240
Tao Baof7140c02018-01-30 17:09:24 -08001241 @staticmethod
1242 def _create_payload_incremental():
1243 target_file = construct_target_files()
1244 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001245 payload = Payload()
1246 payload.Generate(target_file, source_file)
1247 return payload
1248
1249 def test_Generate_full(self):
1250 payload = self._create_payload_full()
1251 self.assertTrue(os.path.exists(payload.payload_file))
1252
1253 def test_Generate_incremental(self):
1254 payload = self._create_payload_incremental()
1255 self.assertTrue(os.path.exists(payload.payload_file))
1256
1257 def test_Generate_additionalArgs(self):
Tao Baof7140c02018-01-30 17:09:24 -08001258 target_file = construct_target_files()
1259 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001260 payload = Payload()
1261 # This should work the same as calling payload.Generate(target_file,
1262 # source_file).
1263 payload.Generate(
1264 target_file, additional_args=["--source_image", source_file])
1265 self.assertTrue(os.path.exists(payload.payload_file))
1266
1267 def test_Generate_invalidInput(self):
Tao Baof7140c02018-01-30 17:09:24 -08001268 target_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001269 common.ZipDelete(target_file, 'IMAGES/vendor.img')
1270 payload = Payload()
1271 self.assertRaises(AssertionError, payload.Generate, target_file)
1272
1273 def test_Sign_full(self):
1274 payload = self._create_payload_full()
1275 payload.Sign(PayloadSigner())
1276
1277 output_file = common.MakeTempFile(suffix='.zip')
1278 with zipfile.ZipFile(output_file, 'w') as output_zip:
1279 payload.WriteToZip(output_zip)
1280
1281 import check_ota_package_signature
1282 check_ota_package_signature.VerifyAbOtaPayload(
1283 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1284 output_file)
1285
1286 def test_Sign_incremental(self):
1287 payload = self._create_payload_incremental()
1288 payload.Sign(PayloadSigner())
1289
1290 output_file = common.MakeTempFile(suffix='.zip')
1291 with zipfile.ZipFile(output_file, 'w') as output_zip:
1292 payload.WriteToZip(output_zip)
1293
1294 import check_ota_package_signature
1295 check_ota_package_signature.VerifyAbOtaPayload(
1296 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1297 output_file)
1298
1299 def test_Sign_withDataWipe(self):
1300 common.OPTIONS.wipe_user_data = True
1301 payload = self._create_payload_full()
1302 payload.Sign(PayloadSigner())
1303
1304 with open(payload.payload_properties) as properties_fp:
1305 self.assertIn("POWERWASH=1", properties_fp.read())
1306
Tao Bao667ff572018-02-10 00:02:40 -08001307 def test_Sign_secondary(self):
1308 payload = self._create_payload_full(secondary=True)
1309 payload.Sign(PayloadSigner())
1310
1311 with open(payload.payload_properties) as properties_fp:
1312 self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read())
1313
Tao Baoc7b403a2018-01-30 18:19:04 -08001314 def test_Sign_badSigner(self):
1315 """Tests that signing failure can be captured."""
1316 payload = self._create_payload_full()
1317 payload_signer = PayloadSigner()
1318 payload_signer.signer_args.append('bad-option')
1319 self.assertRaises(AssertionError, payload.Sign, payload_signer)
1320
1321 def test_WriteToZip(self):
1322 payload = self._create_payload_full()
1323 payload.Sign(PayloadSigner())
1324
1325 output_file = common.MakeTempFile(suffix='.zip')
1326 with zipfile.ZipFile(output_file, 'w') as output_zip:
1327 payload.WriteToZip(output_zip)
1328
1329 with zipfile.ZipFile(output_file) as verify_zip:
1330 # First make sure we have the essential entries.
1331 namelist = verify_zip.namelist()
1332 self.assertIn(Payload.PAYLOAD_BIN, namelist)
1333 self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist)
1334
1335 # Then assert these entries are stored.
1336 for entry_info in verify_zip.infolist():
1337 if entry_info.filename not in (Payload.PAYLOAD_BIN,
1338 Payload.PAYLOAD_PROPERTIES_TXT):
1339 continue
1340 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)
1341
1342 def test_WriteToZip_unsignedPayload(self):
1343 """Unsigned payloads should not be allowed to be written to zip."""
1344 payload = self._create_payload_full()
1345
1346 output_file = common.MakeTempFile(suffix='.zip')
1347 with zipfile.ZipFile(output_file, 'w') as output_zip:
1348 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
1349
1350 # Also test with incremental payload.
1351 payload = self._create_payload_incremental()
1352
1353 output_file = common.MakeTempFile(suffix='.zip')
1354 with zipfile.ZipFile(output_file, 'w') as output_zip:
1355 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001356
1357 def test_WriteToZip_secondary(self):
1358 payload = self._create_payload_full(secondary=True)
1359 payload.Sign(PayloadSigner())
1360
1361 output_file = common.MakeTempFile(suffix='.zip')
1362 with zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Bao667ff572018-02-10 00:02:40 -08001363 payload.WriteToZip(output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001364
1365 with zipfile.ZipFile(output_file) as verify_zip:
1366 # First make sure we have the essential entries.
1367 namelist = verify_zip.namelist()
1368 self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist)
1369 self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist)
1370
1371 # Then assert these entries are stored.
1372 for entry_info in verify_zip.infolist():
1373 if entry_info.filename not in (
1374 Payload.SECONDARY_PAYLOAD_BIN,
1375 Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT):
1376 continue
1377 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)