blob: 29e0d83dfc64b9f18a08be82703cbc31b11ba322 [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 Bao481bab82017-12-21 11:23:09 -080020import unittest
Tao Baoc7b403a2018-01-30 18:19:04 -080021import zipfile
Tao Bao481bab82017-12-21 11:23:09 -080022
23import common
Tao Bao04e1f012018-02-04 12:13:35 -080024import test_utils
Tao Bao481bab82017-12-21 11:23:09 -080025from ota_from_target_files import (
Tao Bao3bf8c652018-03-16 12:59:42 -070026 _LoadOemDicts, AbOtaPropertyFiles, BuildInfo, FinalizeMetadata,
27 GetPackageMetadata, GetTargetFilesZipForSecondaryImages,
Tao Baoc0746f42018-02-21 13:17:22 -080028 GetTargetFilesZipWithoutPostinstallConfig, NonAbOtaPropertyFiles,
Tao Bao69203522018-03-08 16:09:01 -080029 Payload, PayloadSigner, POSTINSTALL_CONFIG, PropertyFiles,
30 StreamingPropertyFiles, WriteFingerprintAssertion)
Tao Baofabe0832018-01-17 15:52:28 -080031
32
Tao Baof7140c02018-01-30 17:09:24 -080033def construct_target_files(secondary=False):
34 """Returns a target-files.zip file for generating OTA packages."""
35 target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip')
36 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
37 # META/update_engine_config.txt
38 target_files_zip.writestr(
39 'META/update_engine_config.txt',
40 "PAYLOAD_MAJOR_VERSION=2\nPAYLOAD_MINOR_VERSION=4\n")
41
Tao Bao15a146a2018-02-21 16:06:59 -080042 # META/postinstall_config.txt
43 target_files_zip.writestr(
44 POSTINSTALL_CONFIG,
45 '\n'.join([
46 "RUN_POSTINSTALL_system=true",
47 "POSTINSTALL_PATH_system=system/bin/otapreopt_script",
48 "FILESYSTEM_TYPE_system=ext4",
49 "POSTINSTALL_OPTIONAL_system=true",
50 ]))
51
Tao Bao5277d102018-04-17 23:47:21 -070052 ab_partitions = [
53 ('IMAGES', 'boot'),
54 ('IMAGES', 'system'),
55 ('IMAGES', 'vendor'),
56 ('RADIO', 'bootloader'),
57 ('RADIO', 'modem'),
58 ]
Tao Baof7140c02018-01-30 17:09:24 -080059 # META/ab_partitions.txt
Tao Baof7140c02018-01-30 17:09:24 -080060 target_files_zip.writestr(
61 'META/ab_partitions.txt',
Tao Bao5277d102018-04-17 23:47:21 -070062 '\n'.join([partition[1] for partition in ab_partitions]))
Tao Baof7140c02018-01-30 17:09:24 -080063
64 # Create dummy images for each of them.
Tao Bao5277d102018-04-17 23:47:21 -070065 for path, partition in ab_partitions:
66 target_files_zip.writestr(
67 '{}/{}.img'.format(path, partition),
68 os.urandom(len(partition)))
Tao Baof7140c02018-01-30 17:09:24 -080069
Tao Bao5277d102018-04-17 23:47:21 -070070 # system_other shouldn't appear in META/ab_partitions.txt.
Tao Baof7140c02018-01-30 17:09:24 -080071 if secondary:
72 target_files_zip.writestr('IMAGES/system_other.img',
73 os.urandom(len("system_other")))
74
75 return target_files
76
77
Tao Bao481bab82017-12-21 11:23:09 -080078class MockScriptWriter(object):
79 """A class that mocks edify_generator.EdifyGenerator.
80
81 It simply pushes the incoming arguments onto script stack, which is to assert
82 the calls to EdifyGenerator functions.
83 """
84
85 def __init__(self):
86 self.script = []
87
88 def Mount(self, *args):
89 self.script.append(('Mount',) + args)
90
91 def AssertDevice(self, *args):
92 self.script.append(('AssertDevice',) + args)
93
94 def AssertOemProperty(self, *args):
95 self.script.append(('AssertOemProperty',) + args)
96
97 def AssertFingerprintOrThumbprint(self, *args):
98 self.script.append(('AssertFingerprintOrThumbprint',) + args)
99
100 def AssertSomeFingerprint(self, *args):
101 self.script.append(('AssertSomeFingerprint',) + args)
102
103 def AssertSomeThumbprint(self, *args):
104 self.script.append(('AssertSomeThumbprint',) + args)
105
106
107class BuildInfoTest(unittest.TestCase):
108
109 TEST_INFO_DICT = {
110 'build.prop' : {
111 'ro.product.device' : 'product-device',
112 'ro.product.name' : 'product-name',
113 'ro.build.fingerprint' : 'build-fingerprint',
114 'ro.build.foo' : 'build-foo',
115 },
116 'vendor.build.prop' : {
117 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint',
118 },
119 'property1' : 'value1',
120 'property2' : 4096,
121 }
122
123 TEST_INFO_DICT_USES_OEM_PROPS = {
124 'build.prop' : {
125 'ro.product.name' : 'product-name',
126 'ro.build.thumbprint' : 'build-thumbprint',
127 'ro.build.bar' : 'build-bar',
128 },
129 'vendor.build.prop' : {
130 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint',
131 },
132 'property1' : 'value1',
133 'property2' : 4096,
134 'oem_fingerprint_properties' : 'ro.product.device ro.product.brand',
135 }
136
137 TEST_OEM_DICTS = [
138 {
139 'ro.product.brand' : 'brand1',
140 'ro.product.device' : 'device1',
141 },
142 {
143 'ro.product.brand' : 'brand2',
144 'ro.product.device' : 'device2',
145 },
146 {
147 'ro.product.brand' : 'brand3',
148 'ro.product.device' : 'device3',
149 },
150 ]
151
152 def test_init(self):
153 target_info = BuildInfo(self.TEST_INFO_DICT, None)
154 self.assertEqual('product-device', target_info.device)
155 self.assertEqual('build-fingerprint', target_info.fingerprint)
156 self.assertFalse(target_info.is_ab)
157 self.assertIsNone(target_info.oem_props)
158
159 def test_init_with_oem_props(self):
160 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
161 self.TEST_OEM_DICTS)
162 self.assertEqual('device1', target_info.device)
163 self.assertEqual('brand1/product-name/device1:build-thumbprint',
164 target_info.fingerprint)
165
166 # Swap the order in oem_dicts, which would lead to different BuildInfo.
167 oem_dicts = copy.copy(self.TEST_OEM_DICTS)
168 oem_dicts[0], oem_dicts[2] = oem_dicts[2], oem_dicts[0]
169 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, oem_dicts)
170 self.assertEqual('device3', target_info.device)
171 self.assertEqual('brand3/product-name/device3:build-thumbprint',
172 target_info.fingerprint)
173
174 # Missing oem_dict should be rejected.
175 self.assertRaises(AssertionError, BuildInfo,
176 self.TEST_INFO_DICT_USES_OEM_PROPS, None)
177
178 def test___getitem__(self):
179 target_info = BuildInfo(self.TEST_INFO_DICT, None)
180 self.assertEqual('value1', target_info['property1'])
181 self.assertEqual(4096, target_info['property2'])
182 self.assertEqual('build-foo', target_info['build.prop']['ro.build.foo'])
183
184 def test___getitem__with_oem_props(self):
185 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
186 self.TEST_OEM_DICTS)
187 self.assertEqual('value1', target_info['property1'])
188 self.assertEqual(4096, target_info['property2'])
189 self.assertRaises(KeyError,
190 lambda: target_info['build.prop']['ro.build.foo'])
191
Tao Bao667c7532018-07-06 10:13:59 -0700192 def test___setitem__(self):
193 target_info = BuildInfo(copy.deepcopy(self.TEST_INFO_DICT), None)
194 self.assertEqual('value1', target_info['property1'])
195 target_info['property1'] = 'value2'
196 self.assertEqual('value2', target_info['property1'])
197
198 self.assertEqual('build-foo', target_info['build.prop']['ro.build.foo'])
199 target_info['build.prop']['ro.build.foo'] = 'build-bar'
200 self.assertEqual('build-bar', target_info['build.prop']['ro.build.foo'])
201
Tao Bao481bab82017-12-21 11:23:09 -0800202 def test_get(self):
203 target_info = BuildInfo(self.TEST_INFO_DICT, None)
204 self.assertEqual('value1', target_info.get('property1'))
205 self.assertEqual(4096, target_info.get('property2'))
206 self.assertEqual(4096, target_info.get('property2', 1024))
207 self.assertEqual(1024, target_info.get('property-nonexistent', 1024))
208 self.assertEqual('build-foo', target_info.get('build.prop')['ro.build.foo'])
209
210 def test_get_with_oem_props(self):
211 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
212 self.TEST_OEM_DICTS)
213 self.assertEqual('value1', target_info.get('property1'))
214 self.assertEqual(4096, target_info.get('property2'))
215 self.assertEqual(4096, target_info.get('property2', 1024))
216 self.assertEqual(1024, target_info.get('property-nonexistent', 1024))
217 self.assertIsNone(target_info.get('build.prop').get('ro.build.foo'))
218 self.assertRaises(KeyError,
219 lambda: target_info.get('build.prop')['ro.build.foo'])
220
Tao Bao667c7532018-07-06 10:13:59 -0700221 def test_items(self):
222 target_info = BuildInfo(self.TEST_INFO_DICT, None)
223 items = target_info.items()
224 self.assertIn(('property1', 'value1'), items)
225 self.assertIn(('property2', 4096), items)
226
Tao Bao481bab82017-12-21 11:23:09 -0800227 def test_GetBuildProp(self):
228 target_info = BuildInfo(self.TEST_INFO_DICT, None)
229 self.assertEqual('build-foo', target_info.GetBuildProp('ro.build.foo'))
230 self.assertRaises(common.ExternalError, target_info.GetBuildProp,
231 'ro.build.nonexistent')
232
233 def test_GetBuildProp_with_oem_props(self):
234 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
235 self.TEST_OEM_DICTS)
236 self.assertEqual('build-bar', target_info.GetBuildProp('ro.build.bar'))
237 self.assertRaises(common.ExternalError, target_info.GetBuildProp,
238 'ro.build.nonexistent')
239
240 def test_GetVendorBuildProp(self):
241 target_info = BuildInfo(self.TEST_INFO_DICT, None)
242 self.assertEqual('vendor-build-fingerprint',
243 target_info.GetVendorBuildProp(
244 'ro.vendor.build.fingerprint'))
245 self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp,
246 'ro.build.nonexistent')
247
248 def test_GetVendorBuildProp_with_oem_props(self):
249 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
250 self.TEST_OEM_DICTS)
251 self.assertEqual('vendor-build-fingerprint',
252 target_info.GetVendorBuildProp(
253 'ro.vendor.build.fingerprint'))
254 self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp,
255 'ro.build.nonexistent')
256
Tao Baoea6cbd02018-09-05 13:06:37 -0700257 def test_vendor_fingerprint(self):
258 target_info = BuildInfo(self.TEST_INFO_DICT, None)
259 self.assertEqual('vendor-build-fingerprint',
260 target_info.vendor_fingerprint)
261
262 def test_vendor_fingerprint_blacklisted(self):
263 target_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS)
264 del target_info_dict['vendor.build.prop']['ro.vendor.build.fingerprint']
265 target_info = BuildInfo(target_info_dict, self.TEST_OEM_DICTS)
266 self.assertIsNone(target_info.vendor_fingerprint)
267
268 def test_vendor_fingerprint_without_vendor_build_prop(self):
269 target_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS)
270 del target_info_dict['vendor.build.prop']
271 target_info = BuildInfo(target_info_dict, self.TEST_OEM_DICTS)
272 self.assertIsNone(target_info.vendor_fingerprint)
273
Tao Bao481bab82017-12-21 11:23:09 -0800274 def test_WriteMountOemScript(self):
275 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
276 self.TEST_OEM_DICTS)
277 script_writer = MockScriptWriter()
278 target_info.WriteMountOemScript(script_writer)
279 self.assertEqual([('Mount', '/oem', None)], script_writer.script)
280
281 def test_WriteDeviceAssertions(self):
282 target_info = BuildInfo(self.TEST_INFO_DICT, None)
283 script_writer = MockScriptWriter()
284 target_info.WriteDeviceAssertions(script_writer, False)
285 self.assertEqual([('AssertDevice', 'product-device')], script_writer.script)
286
287 def test_WriteDeviceAssertions_with_oem_props(self):
288 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
289 self.TEST_OEM_DICTS)
290 script_writer = MockScriptWriter()
291 target_info.WriteDeviceAssertions(script_writer, False)
292 self.assertEqual(
293 [
294 ('AssertOemProperty', 'ro.product.device',
295 ['device1', 'device2', 'device3'], False),
296 ('AssertOemProperty', 'ro.product.brand',
297 ['brand1', 'brand2', 'brand3'], False),
298 ],
299 script_writer.script)
300
301 def test_WriteFingerprintAssertion_without_oem_props(self):
302 target_info = BuildInfo(self.TEST_INFO_DICT, None)
303 source_info_dict = copy.deepcopy(self.TEST_INFO_DICT)
304 source_info_dict['build.prop']['ro.build.fingerprint'] = (
305 'source-build-fingerprint')
306 source_info = BuildInfo(source_info_dict, None)
307
308 script_writer = MockScriptWriter()
309 WriteFingerprintAssertion(script_writer, target_info, source_info)
310 self.assertEqual(
311 [('AssertSomeFingerprint', 'source-build-fingerprint',
312 'build-fingerprint')],
313 script_writer.script)
314
315 def test_WriteFingerprintAssertion_with_source_oem_props(self):
316 target_info = BuildInfo(self.TEST_INFO_DICT, None)
317 source_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
318 self.TEST_OEM_DICTS)
319
320 script_writer = MockScriptWriter()
321 WriteFingerprintAssertion(script_writer, target_info, source_info)
322 self.assertEqual(
323 [('AssertFingerprintOrThumbprint', 'build-fingerprint',
324 'build-thumbprint')],
325 script_writer.script)
326
327 def test_WriteFingerprintAssertion_with_target_oem_props(self):
328 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
329 self.TEST_OEM_DICTS)
330 source_info = BuildInfo(self.TEST_INFO_DICT, None)
331
332 script_writer = MockScriptWriter()
333 WriteFingerprintAssertion(script_writer, target_info, source_info)
334 self.assertEqual(
335 [('AssertFingerprintOrThumbprint', 'build-fingerprint',
336 'build-thumbprint')],
337 script_writer.script)
338
339 def test_WriteFingerprintAssertion_with_both_oem_props(self):
340 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
341 self.TEST_OEM_DICTS)
342 source_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS)
343 source_info_dict['build.prop']['ro.build.thumbprint'] = (
344 'source-build-thumbprint')
345 source_info = BuildInfo(source_info_dict, self.TEST_OEM_DICTS)
346
347 script_writer = MockScriptWriter()
348 WriteFingerprintAssertion(script_writer, target_info, source_info)
349 self.assertEqual(
350 [('AssertSomeThumbprint', 'build-thumbprint',
351 'source-build-thumbprint')],
352 script_writer.script)
353
354
355class LoadOemDictsTest(unittest.TestCase):
356
357 def tearDown(self):
358 common.Cleanup()
359
360 def test_NoneDict(self):
361 self.assertIsNone(_LoadOemDicts(None))
362
363 def test_SingleDict(self):
364 dict_file = common.MakeTempFile()
365 with open(dict_file, 'w') as dict_fp:
366 dict_fp.write('abc=1\ndef=2\nxyz=foo\na.b.c=bar\n')
367
368 oem_dicts = _LoadOemDicts([dict_file])
369 self.assertEqual(1, len(oem_dicts))
370 self.assertEqual('foo', oem_dicts[0]['xyz'])
371 self.assertEqual('bar', oem_dicts[0]['a.b.c'])
372
373 def test_MultipleDicts(self):
374 oem_source = []
375 for i in range(3):
376 dict_file = common.MakeTempFile()
377 with open(dict_file, 'w') as dict_fp:
378 dict_fp.write(
379 'ro.build.index={}\ndef=2\nxyz=foo\na.b.c=bar\n'.format(i))
380 oem_source.append(dict_file)
381
382 oem_dicts = _LoadOemDicts(oem_source)
383 self.assertEqual(3, len(oem_dicts))
384 for i, oem_dict in enumerate(oem_dicts):
385 self.assertEqual('2', oem_dict['def'])
386 self.assertEqual('foo', oem_dict['xyz'])
387 self.assertEqual('bar', oem_dict['a.b.c'])
388 self.assertEqual('{}'.format(i), oem_dict['ro.build.index'])
Tao Baodf3a48b2018-01-10 16:30:43 -0800389
390
391class OtaFromTargetFilesTest(unittest.TestCase):
392
393 TEST_TARGET_INFO_DICT = {
394 'build.prop' : {
395 'ro.product.device' : 'product-device',
396 'ro.build.fingerprint' : 'build-fingerprint-target',
397 'ro.build.version.incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800398 'ro.build.version.sdk' : '27',
399 'ro.build.version.security_patch' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800400 'ro.build.date.utc' : '1500000000',
401 },
402 }
403
404 TEST_SOURCE_INFO_DICT = {
405 'build.prop' : {
406 'ro.product.device' : 'product-device',
407 'ro.build.fingerprint' : 'build-fingerprint-source',
408 'ro.build.version.incremental' : 'build-version-incremental-source',
Tao Bao35dc2552018-02-01 13:18:00 -0800409 'ro.build.version.sdk' : '25',
410 'ro.build.version.security_patch' : '2016-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800411 'ro.build.date.utc' : '1400000000',
412 },
413 }
414
415 def setUp(self):
Tao Bao3bf8c652018-03-16 12:59:42 -0700416 self.testdata_dir = test_utils.get_testdata_dir()
417 self.assertTrue(os.path.exists(self.testdata_dir))
418
Tao Baodf3a48b2018-01-10 16:30:43 -0800419 # Reset the global options as in ota_from_target_files.py.
420 common.OPTIONS.incremental_source = None
421 common.OPTIONS.downgrade = False
422 common.OPTIONS.timestamp = False
423 common.OPTIONS.wipe_user_data = False
Tao Bao3bf8c652018-03-16 12:59:42 -0700424 common.OPTIONS.no_signing = False
425 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
426 common.OPTIONS.key_passwords = {
427 common.OPTIONS.package_key : None,
428 }
429
430 common.OPTIONS.search_path = test_utils.get_search_path()
431 self.assertIsNotNone(common.OPTIONS.search_path)
Tao Baodf3a48b2018-01-10 16:30:43 -0800432
Tao Baof5110492018-03-02 09:47:43 -0800433 def tearDown(self):
434 common.Cleanup()
435
Tao Baodf3a48b2018-01-10 16:30:43 -0800436 def test_GetPackageMetadata_abOta_full(self):
437 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
438 target_info_dict['ab_update'] = 'true'
439 target_info = BuildInfo(target_info_dict, None)
440 metadata = GetPackageMetadata(target_info)
441 self.assertDictEqual(
442 {
443 'ota-type' : 'AB',
444 'ota-required-cache' : '0',
445 'post-build' : 'build-fingerprint-target',
446 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800447 'post-sdk-level' : '27',
448 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800449 'post-timestamp' : '1500000000',
450 'pre-device' : 'product-device',
451 },
452 metadata)
453
454 def test_GetPackageMetadata_abOta_incremental(self):
455 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
456 target_info_dict['ab_update'] = 'true'
457 target_info = BuildInfo(target_info_dict, None)
458 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
459 common.OPTIONS.incremental_source = ''
460 metadata = GetPackageMetadata(target_info, source_info)
461 self.assertDictEqual(
462 {
463 'ota-type' : 'AB',
464 'ota-required-cache' : '0',
465 'post-build' : 'build-fingerprint-target',
466 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800467 'post-sdk-level' : '27',
468 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800469 'post-timestamp' : '1500000000',
470 'pre-device' : 'product-device',
471 'pre-build' : 'build-fingerprint-source',
472 'pre-build-incremental' : 'build-version-incremental-source',
473 },
474 metadata)
475
476 def test_GetPackageMetadata_nonAbOta_full(self):
477 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
478 metadata = GetPackageMetadata(target_info)
479 self.assertDictEqual(
480 {
481 'ota-type' : 'BLOCK',
482 'post-build' : 'build-fingerprint-target',
483 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800484 'post-sdk-level' : '27',
485 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800486 'post-timestamp' : '1500000000',
487 'pre-device' : 'product-device',
488 },
489 metadata)
490
491 def test_GetPackageMetadata_nonAbOta_incremental(self):
492 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
493 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
494 common.OPTIONS.incremental_source = ''
495 metadata = GetPackageMetadata(target_info, source_info)
496 self.assertDictEqual(
497 {
498 'ota-type' : 'BLOCK',
499 'post-build' : 'build-fingerprint-target',
500 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800501 'post-sdk-level' : '27',
502 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800503 'post-timestamp' : '1500000000',
504 'pre-device' : 'product-device',
505 'pre-build' : 'build-fingerprint-source',
506 'pre-build-incremental' : 'build-version-incremental-source',
507 },
508 metadata)
509
510 def test_GetPackageMetadata_wipe(self):
511 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
512 common.OPTIONS.wipe_user_data = True
513 metadata = GetPackageMetadata(target_info)
514 self.assertDictEqual(
515 {
516 'ota-type' : 'BLOCK',
517 'ota-wipe' : 'yes',
518 'post-build' : 'build-fingerprint-target',
519 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800520 'post-sdk-level' : '27',
521 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800522 'post-timestamp' : '1500000000',
523 'pre-device' : 'product-device',
524 },
525 metadata)
526
527 @staticmethod
528 def _test_GetPackageMetadata_swapBuildTimestamps(target_info, source_info):
529 (target_info['build.prop']['ro.build.date.utc'],
530 source_info['build.prop']['ro.build.date.utc']) = (
531 source_info['build.prop']['ro.build.date.utc'],
532 target_info['build.prop']['ro.build.date.utc'])
533
534 def test_GetPackageMetadata_unintentionalDowngradeDetected(self):
535 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
536 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
537 self._test_GetPackageMetadata_swapBuildTimestamps(
538 target_info_dict, source_info_dict)
539
540 target_info = BuildInfo(target_info_dict, None)
541 source_info = BuildInfo(source_info_dict, None)
542 common.OPTIONS.incremental_source = ''
543 self.assertRaises(RuntimeError, GetPackageMetadata, target_info,
544 source_info)
545
546 def test_GetPackageMetadata_downgrade(self):
547 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
548 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
549 self._test_GetPackageMetadata_swapBuildTimestamps(
550 target_info_dict, source_info_dict)
551
552 target_info = BuildInfo(target_info_dict, None)
553 source_info = BuildInfo(source_info_dict, None)
554 common.OPTIONS.incremental_source = ''
555 common.OPTIONS.downgrade = True
556 common.OPTIONS.wipe_user_data = True
557 metadata = GetPackageMetadata(target_info, source_info)
558 self.assertDictEqual(
559 {
560 'ota-downgrade' : 'yes',
561 'ota-type' : 'BLOCK',
562 'ota-wipe' : 'yes',
563 'post-build' : 'build-fingerprint-target',
564 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800565 'post-sdk-level' : '27',
566 'post-security-patch-level' : '2017-12-01',
Tao Baofaa8e0b2018-04-12 14:31:43 -0700567 'post-timestamp' : '1400000000',
Tao Baodf3a48b2018-01-10 16:30:43 -0800568 'pre-device' : 'product-device',
569 'pre-build' : 'build-fingerprint-source',
570 'pre-build-incremental' : 'build-version-incremental-source',
571 },
572 metadata)
Tao Baofabe0832018-01-17 15:52:28 -0800573
Tao Baof7140c02018-01-30 17:09:24 -0800574 def test_GetTargetFilesZipForSecondaryImages(self):
575 input_file = construct_target_files(secondary=True)
576 target_file = GetTargetFilesZipForSecondaryImages(input_file)
577
578 with zipfile.ZipFile(target_file) as verify_zip:
579 namelist = verify_zip.namelist()
580
581 self.assertIn('META/ab_partitions.txt', namelist)
582 self.assertIn('IMAGES/boot.img', namelist)
583 self.assertIn('IMAGES/system.img', namelist)
584 self.assertIn('IMAGES/vendor.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700585 self.assertIn('RADIO/bootloader.img', namelist)
586 self.assertIn('RADIO/modem.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800587 self.assertIn(POSTINSTALL_CONFIG, namelist)
Tao Baof7140c02018-01-30 17:09:24 -0800588
589 self.assertNotIn('IMAGES/system_other.img', namelist)
590 self.assertNotIn('IMAGES/system.map', namelist)
591
Tao Bao15a146a2018-02-21 16:06:59 -0800592 def test_GetTargetFilesZipForSecondaryImages_skipPostinstall(self):
593 input_file = construct_target_files(secondary=True)
594 target_file = GetTargetFilesZipForSecondaryImages(
595 input_file, skip_postinstall=True)
596
597 with zipfile.ZipFile(target_file) as verify_zip:
598 namelist = verify_zip.namelist()
599
600 self.assertIn('META/ab_partitions.txt', namelist)
601 self.assertIn('IMAGES/boot.img', namelist)
602 self.assertIn('IMAGES/system.img', namelist)
603 self.assertIn('IMAGES/vendor.img', namelist)
Tao Bao12489802018-07-12 14:47:38 -0700604 self.assertIn('RADIO/bootloader.img', namelist)
605 self.assertIn('RADIO/modem.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800606
607 self.assertNotIn('IMAGES/system_other.img', namelist)
608 self.assertNotIn('IMAGES/system.map', namelist)
609 self.assertNotIn(POSTINSTALL_CONFIG, namelist)
610
Tao Bao12489802018-07-12 14:47:38 -0700611 def test_GetTargetFilesZipForSecondaryImages_withoutRadioImages(self):
612 input_file = construct_target_files(secondary=True)
613 common.ZipDelete(input_file, 'RADIO/bootloader.img')
614 common.ZipDelete(input_file, 'RADIO/modem.img')
615 target_file = GetTargetFilesZipForSecondaryImages(input_file)
616
617 with zipfile.ZipFile(target_file) as verify_zip:
618 namelist = verify_zip.namelist()
619
620 self.assertIn('META/ab_partitions.txt', namelist)
621 self.assertIn('IMAGES/boot.img', namelist)
622 self.assertIn('IMAGES/system.img', namelist)
623 self.assertIn('IMAGES/vendor.img', namelist)
624 self.assertIn(POSTINSTALL_CONFIG, namelist)
625
626 self.assertNotIn('IMAGES/system_other.img', namelist)
627 self.assertNotIn('IMAGES/system.map', namelist)
628 self.assertNotIn('RADIO/bootloader.img', namelist)
629 self.assertNotIn('RADIO/modem.img', namelist)
630
Tao Bao15a146a2018-02-21 16:06:59 -0800631 def test_GetTargetFilesZipWithoutPostinstallConfig(self):
632 input_file = construct_target_files()
633 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
634 with zipfile.ZipFile(target_file) as verify_zip:
635 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
636
637 def test_GetTargetFilesZipWithoutPostinstallConfig_missingEntry(self):
638 input_file = construct_target_files()
639 common.ZipDelete(input_file, POSTINSTALL_CONFIG)
640 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
641 with zipfile.ZipFile(target_file) as verify_zip:
642 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
643
Tao Bao3bf8c652018-03-16 12:59:42 -0700644 def _test_FinalizeMetadata(self, large_entry=False):
645 entries = [
646 'required-entry1',
647 'required-entry2',
648 ]
649 zip_file = PropertyFilesTest.construct_zip_package(entries)
650 # Add a large entry of 1 GiB if requested.
651 if large_entry:
652 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
653 zip_fp.writestr(
654 # Using 'zoo' so that the entry stays behind others after signing.
655 'zoo',
656 'A' * 1024 * 1024 * 1024,
657 zipfile.ZIP_STORED)
658
659 metadata = {}
660 output_file = common.MakeTempFile(suffix='.zip')
661 needed_property_files = (
662 TestPropertyFiles(),
663 )
664 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
665 self.assertIn('ota-test-property-files', metadata)
666
667 def test_FinalizeMetadata(self):
668 self._test_FinalizeMetadata()
669
670 def test_FinalizeMetadata_withNoSigning(self):
671 common.OPTIONS.no_signing = True
672 self._test_FinalizeMetadata()
673
674 def test_FinalizeMetadata_largeEntry(self):
675 self._test_FinalizeMetadata(large_entry=True)
676
677 def test_FinalizeMetadata_largeEntry_withNoSigning(self):
678 common.OPTIONS.no_signing = True
679 self._test_FinalizeMetadata(large_entry=True)
680
681 def test_FinalizeMetadata_insufficientSpace(self):
682 entries = [
683 'required-entry1',
684 'required-entry2',
685 'optional-entry1',
686 'optional-entry2',
687 ]
688 zip_file = PropertyFilesTest.construct_zip_package(entries)
689 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
690 zip_fp.writestr(
691 # 'foo-entry1' will appear ahead of all other entries (in alphabetical
692 # order) after the signing, which will in turn trigger the
693 # InsufficientSpaceException and an automatic retry.
694 'foo-entry1',
695 'A' * 1024 * 1024,
696 zipfile.ZIP_STORED)
697
698 metadata = {}
699 needed_property_files = (
700 TestPropertyFiles(),
701 )
702 output_file = common.MakeTempFile(suffix='.zip')
703 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
704 self.assertIn('ota-test-property-files', metadata)
705
Tao Baoae5e4c32018-03-01 19:30:00 -0800706
Tao Bao69203522018-03-08 16:09:01 -0800707class TestPropertyFiles(PropertyFiles):
708 """A class that extends PropertyFiles for testing purpose."""
709
710 def __init__(self):
711 super(TestPropertyFiles, self).__init__()
712 self.name = 'ota-test-property-files'
713 self.required = (
714 'required-entry1',
715 'required-entry2',
716 )
717 self.optional = (
718 'optional-entry1',
719 'optional-entry2',
720 )
721
722
723class PropertyFilesTest(unittest.TestCase):
Tao Baoae5e4c32018-03-01 19:30:00 -0800724
Tao Bao3bf8c652018-03-16 12:59:42 -0700725 def setUp(self):
726 common.OPTIONS.no_signing = False
727
Tao Baoae5e4c32018-03-01 19:30:00 -0800728 def tearDown(self):
729 common.Cleanup()
730
Tao Baof5110492018-03-02 09:47:43 -0800731 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -0700732 def construct_zip_package(entries):
Tao Baof5110492018-03-02 09:47:43 -0800733 zip_file = common.MakeTempFile(suffix='.zip')
734 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
735 for entry in entries:
736 zip_fp.writestr(
737 entry,
738 entry.replace('.', '-').upper(),
739 zipfile.ZIP_STORED)
740 return zip_file
741
742 @staticmethod
Tao Bao69203522018-03-08 16:09:01 -0800743 def _parse_property_files_string(data):
Tao Baof5110492018-03-02 09:47:43 -0800744 result = {}
745 for token in data.split(','):
746 name, info = token.split(':', 1)
747 result[name] = info
748 return result
749
750 def _verify_entries(self, input_file, tokens, entries):
751 for entry in entries:
752 offset, size = map(int, tokens[entry].split(':'))
753 with open(input_file, 'rb') as input_fp:
754 input_fp.seek(offset)
755 if entry == 'metadata':
756 expected = b'META-INF/COM/ANDROID/METADATA'
757 else:
758 expected = entry.replace('.', '-').upper().encode()
759 self.assertEqual(expected, input_fp.read(size))
760
Tao Baoae5e4c32018-03-01 19:30:00 -0800761 def test_Compute(self):
Tao Baof5110492018-03-02 09:47:43 -0800762 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800763 'required-entry1',
764 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800765 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700766 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800767 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800768 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800769 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800770
Tao Bao69203522018-03-08 16:09:01 -0800771 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800772 self.assertEqual(3, len(tokens))
773 self._verify_entries(zip_file, tokens, entries)
774
Tao Bao69203522018-03-08 16:09:01 -0800775 def test_Compute_withOptionalEntries(self):
Tao Baof5110492018-03-02 09:47:43 -0800776 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800777 'required-entry1',
778 'required-entry2',
779 'optional-entry1',
780 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800781 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700782 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800783 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800784 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800785 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800786
Tao Bao69203522018-03-08 16:09:01 -0800787 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800788 self.assertEqual(5, len(tokens))
789 self._verify_entries(zip_file, tokens, entries)
790
Tao Bao69203522018-03-08 16:09:01 -0800791 def test_Compute_missingRequiredEntry(self):
792 entries = (
793 'required-entry2',
794 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700795 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800796 property_files = TestPropertyFiles()
797 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
798 self.assertRaises(KeyError, property_files.Compute, zip_fp)
799
Tao Baoae5e4c32018-03-01 19:30:00 -0800800 def test_Finalize(self):
Tao Baof5110492018-03-02 09:47:43 -0800801 entries = [
Tao Bao69203522018-03-08 16:09:01 -0800802 'required-entry1',
803 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800804 'META-INF/com/android/metadata',
805 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700806 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800807 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800808 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700809 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800810 zip_fp, reserve_space=False)
811 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
Tao Bao69203522018-03-08 16:09:01 -0800812 tokens = self._parse_property_files_string(streaming_metadata)
Tao Baof5110492018-03-02 09:47:43 -0800813
814 self.assertEqual(3, len(tokens))
815 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
816 # streaming metadata.
817 entries[2] = 'metadata'
818 self._verify_entries(zip_file, tokens, entries)
819
Tao Baoae5e4c32018-03-01 19:30:00 -0800820 def test_Finalize_assertReservedLength(self):
Tao Baof5110492018-03-02 09:47:43 -0800821 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800822 'required-entry1',
823 'required-entry2',
824 'optional-entry1',
825 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800826 'META-INF/com/android/metadata',
827 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700828 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800829 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800830 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
831 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700832 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800833 zip_fp, reserve_space=False)
Tao Baof5110492018-03-02 09:47:43 -0800834 raw_length = len(raw_metadata)
835
836 # Now pass in the exact expected length.
Tao Baoae5e4c32018-03-01 19:30:00 -0800837 streaming_metadata = property_files.Finalize(zip_fp, raw_length)
Tao Baof5110492018-03-02 09:47:43 -0800838 self.assertEqual(raw_length, len(streaming_metadata))
839
840 # Or pass in insufficient length.
841 self.assertRaises(
Tao Bao3bf8c652018-03-16 12:59:42 -0700842 PropertyFiles.InsufficientSpaceException,
Tao Baoae5e4c32018-03-01 19:30:00 -0800843 property_files.Finalize,
Tao Baof5110492018-03-02 09:47:43 -0800844 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800845 raw_length - 1)
Tao Baof5110492018-03-02 09:47:43 -0800846
847 # Or pass in a much larger size.
Tao Baoae5e4c32018-03-01 19:30:00 -0800848 streaming_metadata = property_files.Finalize(
Tao Baof5110492018-03-02 09:47:43 -0800849 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800850 raw_length + 20)
Tao Baof5110492018-03-02 09:47:43 -0800851 self.assertEqual(raw_length + 20, len(streaming_metadata))
852 self.assertEqual(' ' * 20, streaming_metadata[raw_length:])
853
Tao Baoae5e4c32018-03-01 19:30:00 -0800854 def test_Verify(self):
855 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800856 'required-entry1',
857 'required-entry2',
858 'optional-entry1',
859 'optional-entry2',
860 'META-INF/com/android/metadata',
861 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700862 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800863 property_files = TestPropertyFiles()
864 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
865 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700866 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800867 zip_fp, reserve_space=False)
868
869 # Should pass the test if verification passes.
870 property_files.Verify(zip_fp, raw_metadata)
871
872 # Or raise on verification failure.
873 self.assertRaises(
874 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
875
876
877class StreamingPropertyFilesTest(PropertyFilesTest):
878 """Additional sanity checks specialized for StreamingPropertyFiles."""
879
880 def test_init(self):
881 property_files = StreamingPropertyFiles()
882 self.assertEqual('ota-streaming-property-files', property_files.name)
883 self.assertEqual(
884 (
885 'payload.bin',
886 'payload_properties.txt',
887 ),
888 property_files.required)
889 self.assertEqual(
890 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -0700891 'care_map.pb',
Tao Bao69203522018-03-08 16:09:01 -0800892 'care_map.txt',
893 'compatibility.zip',
894 ),
895 property_files.optional)
896
897 def test_Compute(self):
898 entries = (
Tao Baoae5e4c32018-03-01 19:30:00 -0800899 'payload.bin',
900 'payload_properties.txt',
901 'care_map.txt',
Tao Bao69203522018-03-08 16:09:01 -0800902 'compatibility.zip',
903 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700904 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800905 property_files = StreamingPropertyFiles()
906 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
907 property_files_string = property_files.Compute(zip_fp)
908
909 tokens = self._parse_property_files_string(property_files_string)
910 self.assertEqual(5, len(tokens))
911 self._verify_entries(zip_file, tokens, entries)
912
913 def test_Finalize(self):
914 entries = [
915 'payload.bin',
916 'payload_properties.txt',
917 'care_map.txt',
918 'compatibility.zip',
919 'META-INF/com/android/metadata',
920 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700921 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800922 property_files = StreamingPropertyFiles()
923 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700924 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800925 zip_fp, reserve_space=False)
926 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
927 tokens = self._parse_property_files_string(streaming_metadata)
928
929 self.assertEqual(5, len(tokens))
930 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
931 # streaming metadata.
932 entries[4] = 'metadata'
933 self._verify_entries(zip_file, tokens, entries)
934
935 def test_Verify(self):
936 entries = (
937 'payload.bin',
938 'payload_properties.txt',
939 'care_map.txt',
940 'compatibility.zip',
Tao Baoae5e4c32018-03-01 19:30:00 -0800941 'META-INF/com/android/metadata',
942 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700943 zip_file = self.construct_zip_package(entries)
Tao Baoae5e4c32018-03-01 19:30:00 -0800944 property_files = StreamingPropertyFiles()
945 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
946 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700947 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800948 zip_fp, reserve_space=False)
949
950 # Should pass the test if verification passes.
951 property_files.Verify(zip_fp, raw_metadata)
952
953 # Or raise on verification failure.
954 self.assertRaises(
955 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
956
Tao Baofabe0832018-01-17 15:52:28 -0800957
Tao Baob6304672018-03-08 16:28:33 -0800958class AbOtaPropertyFilesTest(PropertyFilesTest):
959 """Additional sanity checks specialized for AbOtaPropertyFiles."""
960
961 # The size for payload and metadata signature size.
962 SIGNATURE_SIZE = 256
963
964 def setUp(self):
965 self.testdata_dir = test_utils.get_testdata_dir()
966 self.assertTrue(os.path.exists(self.testdata_dir))
967
968 common.OPTIONS.wipe_user_data = False
969 common.OPTIONS.payload_signer = None
970 common.OPTIONS.payload_signer_args = None
971 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
972 common.OPTIONS.key_passwords = {
973 common.OPTIONS.package_key : None,
974 }
975
976 def test_init(self):
977 property_files = AbOtaPropertyFiles()
978 self.assertEqual('ota-property-files', property_files.name)
979 self.assertEqual(
980 (
981 'payload.bin',
982 'payload_properties.txt',
983 ),
984 property_files.required)
985 self.assertEqual(
986 (
Tianjie Xu4c05f4a2018-09-14 16:24:41 -0700987 'care_map.pb',
Tao Baob6304672018-03-08 16:28:33 -0800988 'care_map.txt',
989 'compatibility.zip',
990 ),
991 property_files.optional)
992
993 def test_GetPayloadMetadataOffsetAndSize(self):
994 target_file = construct_target_files()
995 payload = Payload()
996 payload.Generate(target_file)
997
998 payload_signer = PayloadSigner()
999 payload.Sign(payload_signer)
1000
1001 output_file = common.MakeTempFile(suffix='.zip')
1002 with zipfile.ZipFile(output_file, 'w') as output_zip:
1003 payload.WriteToZip(output_zip)
1004
1005 # Find out the payload metadata offset and size.
1006 property_files = AbOtaPropertyFiles()
1007 with zipfile.ZipFile(output_file) as input_zip:
1008 # pylint: disable=protected-access
1009 payload_offset, metadata_total = (
1010 property_files._GetPayloadMetadataOffsetAndSize(input_zip))
1011
1012 # Read in the metadata signature directly.
1013 with open(output_file, 'rb') as verify_fp:
1014 verify_fp.seek(payload_offset + metadata_total - self.SIGNATURE_SIZE)
1015 metadata_signature = verify_fp.read(self.SIGNATURE_SIZE)
1016
1017 # Now we extract the metadata hash via brillo_update_payload script, which
1018 # will serve as the oracle result.
1019 payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1020 metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
1021 cmd = ['brillo_update_payload', 'hash',
1022 '--unsigned_payload', payload.payload_file,
1023 '--signature_size', str(self.SIGNATURE_SIZE),
1024 '--metadata_hash_file', metadata_sig_file,
1025 '--payload_hash_file', payload_sig_file]
Tao Bao73dd4f42018-10-04 16:25:33 -07001026 proc = common.Run(cmd)
Tao Baob6304672018-03-08 16:28:33 -08001027 stdoutdata, _ = proc.communicate()
1028 self.assertEqual(
1029 0, proc.returncode,
Tao Bao73dd4f42018-10-04 16:25:33 -07001030 'Failed to run brillo_update_payload:\n{}'.format(stdoutdata))
Tao Baob6304672018-03-08 16:28:33 -08001031
1032 signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file)
1033
1034 # Finally we can compare the two signatures.
1035 with open(signed_metadata_sig_file, 'rb') as verify_fp:
1036 self.assertEqual(verify_fp.read(), metadata_signature)
1037
1038 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -07001039 def construct_zip_package_withValidPayload(with_metadata=False):
1040 # Cannot use construct_zip_package() since we need a "valid" payload.bin.
Tao Baob6304672018-03-08 16:28:33 -08001041 target_file = construct_target_files()
1042 payload = Payload()
1043 payload.Generate(target_file)
1044
1045 payload_signer = PayloadSigner()
1046 payload.Sign(payload_signer)
1047
1048 zip_file = common.MakeTempFile(suffix='.zip')
1049 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
1050 # 'payload.bin',
1051 payload.WriteToZip(zip_fp)
1052
1053 # Other entries.
1054 entries = ['care_map.txt', 'compatibility.zip']
1055
1056 # Put META-INF/com/android/metadata if needed.
1057 if with_metadata:
1058 entries.append('META-INF/com/android/metadata')
1059
1060 for entry in entries:
1061 zip_fp.writestr(
1062 entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED)
1063
1064 return zip_file
1065
1066 def test_Compute(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001067 zip_file = self.construct_zip_package_withValidPayload()
Tao Baob6304672018-03-08 16:28:33 -08001068 property_files = AbOtaPropertyFiles()
1069 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1070 property_files_string = property_files.Compute(zip_fp)
1071
1072 tokens = self._parse_property_files_string(property_files_string)
1073 # "6" indcludes the four entries above, one metadata entry, and one entry
1074 # for payload-metadata.bin.
1075 self.assertEqual(6, len(tokens))
1076 self._verify_entries(
1077 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1078
1079 def test_Finalize(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001080 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001081 property_files = AbOtaPropertyFiles()
1082 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001083 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001084 zip_fp, reserve_space=False)
1085 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1086
1087 tokens = self._parse_property_files_string(property_files_string)
1088 # "6" indcludes the four entries above, one metadata entry, and one entry
1089 # for payload-metadata.bin.
1090 self.assertEqual(6, len(tokens))
1091 self._verify_entries(
1092 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1093
1094 def test_Verify(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001095 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001096 property_files = AbOtaPropertyFiles()
1097 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001098 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001099 zip_fp, reserve_space=False)
1100
1101 property_files.Verify(zip_fp, raw_metadata)
1102
1103
Tao Baoc0746f42018-02-21 13:17:22 -08001104class NonAbOtaPropertyFilesTest(PropertyFilesTest):
1105 """Additional sanity checks specialized for NonAbOtaPropertyFiles."""
1106
1107 def test_init(self):
1108 property_files = NonAbOtaPropertyFiles()
1109 self.assertEqual('ota-property-files', property_files.name)
1110 self.assertEqual((), property_files.required)
1111 self.assertEqual((), property_files.optional)
1112
1113 def test_Compute(self):
1114 entries = ()
Tao Bao3bf8c652018-03-16 12:59:42 -07001115 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001116 property_files = NonAbOtaPropertyFiles()
1117 with zipfile.ZipFile(zip_file) as zip_fp:
1118 property_files_string = property_files.Compute(zip_fp)
1119
1120 tokens = self._parse_property_files_string(property_files_string)
1121 self.assertEqual(1, len(tokens))
1122 self._verify_entries(zip_file, tokens, entries)
1123
1124 def test_Finalize(self):
1125 entries = [
1126 'META-INF/com/android/metadata',
1127 ]
Tao Bao3bf8c652018-03-16 12:59:42 -07001128 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001129 property_files = NonAbOtaPropertyFiles()
1130 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001131 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001132 zip_fp, reserve_space=False)
1133 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1134 tokens = self._parse_property_files_string(property_files_string)
1135
1136 self.assertEqual(1, len(tokens))
1137 # 'META-INF/com/android/metadata' will be key'd as 'metadata'.
1138 entries[0] = 'metadata'
1139 self._verify_entries(zip_file, tokens, entries)
1140
1141 def test_Verify(self):
1142 entries = (
1143 'META-INF/com/android/metadata',
1144 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001145 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001146 property_files = NonAbOtaPropertyFiles()
1147 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001148 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001149 zip_fp, reserve_space=False)
1150
1151 property_files.Verify(zip_fp, raw_metadata)
1152
1153
Tao Baofabe0832018-01-17 15:52:28 -08001154class PayloadSignerTest(unittest.TestCase):
1155
1156 SIGFILE = 'sigfile.bin'
1157 SIGNED_SIGFILE = 'signed-sigfile.bin'
1158
1159 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001160 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baofabe0832018-01-17 15:52:28 -08001161 self.assertTrue(os.path.exists(self.testdata_dir))
1162
1163 common.OPTIONS.payload_signer = None
1164 common.OPTIONS.payload_signer_args = []
1165 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1166 common.OPTIONS.key_passwords = {
1167 common.OPTIONS.package_key : None,
1168 }
1169
1170 def tearDown(self):
1171 common.Cleanup()
1172
1173 def _assertFilesEqual(self, file1, file2):
1174 with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
1175 self.assertEqual(fp1.read(), fp2.read())
1176
1177 def test_init(self):
1178 payload_signer = PayloadSigner()
1179 self.assertEqual('openssl', payload_signer.signer)
1180
1181 def test_init_withPassword(self):
1182 common.OPTIONS.package_key = os.path.join(
1183 self.testdata_dir, 'testkey_with_passwd')
1184 common.OPTIONS.key_passwords = {
1185 common.OPTIONS.package_key : 'foo',
1186 }
1187 payload_signer = PayloadSigner()
1188 self.assertEqual('openssl', payload_signer.signer)
1189
1190 def test_init_withExternalSigner(self):
1191 common.OPTIONS.payload_signer = 'abc'
1192 common.OPTIONS.payload_signer_args = ['arg1', 'arg2']
1193 payload_signer = PayloadSigner()
1194 self.assertEqual('abc', payload_signer.signer)
1195 self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args)
1196
1197 def test_Sign(self):
1198 payload_signer = PayloadSigner()
1199 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1200 signed_file = payload_signer.Sign(input_file)
1201
1202 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1203 self._assertFilesEqual(verify_file, signed_file)
1204
1205 def test_Sign_withExternalSigner_openssl(self):
1206 """Uses openssl as the external payload signer."""
1207 common.OPTIONS.payload_signer = 'openssl'
1208 common.OPTIONS.payload_signer_args = [
1209 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey',
1210 os.path.join(self.testdata_dir, 'testkey.pk8'),
1211 '-pkeyopt', 'digest:sha256']
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)
1218
1219 def test_Sign_withExternalSigner_script(self):
1220 """Uses testdata/payload_signer.sh as the external payload signer."""
1221 common.OPTIONS.payload_signer = os.path.join(
1222 self.testdata_dir, 'payload_signer.sh')
1223 common.OPTIONS.payload_signer_args = [
1224 os.path.join(self.testdata_dir, 'testkey.pk8')]
1225 payload_signer = PayloadSigner()
1226 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1227 signed_file = payload_signer.Sign(input_file)
1228
1229 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1230 self._assertFilesEqual(verify_file, signed_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001231
1232
1233class PayloadTest(unittest.TestCase):
1234
1235 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001236 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoc7b403a2018-01-30 18:19:04 -08001237 self.assertTrue(os.path.exists(self.testdata_dir))
1238
1239 common.OPTIONS.wipe_user_data = False
1240 common.OPTIONS.payload_signer = None
1241 common.OPTIONS.payload_signer_args = None
1242 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1243 common.OPTIONS.key_passwords = {
1244 common.OPTIONS.package_key : None,
1245 }
1246
1247 def tearDown(self):
1248 common.Cleanup()
1249
1250 @staticmethod
Tao Baof7140c02018-01-30 17:09:24 -08001251 def _create_payload_full(secondary=False):
1252 target_file = construct_target_files(secondary)
Tao Bao667ff572018-02-10 00:02:40 -08001253 payload = Payload(secondary)
Tao Baoc7b403a2018-01-30 18:19:04 -08001254 payload.Generate(target_file)
1255 return payload
1256
Tao Baof7140c02018-01-30 17:09:24 -08001257 @staticmethod
1258 def _create_payload_incremental():
1259 target_file = construct_target_files()
1260 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001261 payload = Payload()
1262 payload.Generate(target_file, source_file)
1263 return payload
1264
1265 def test_Generate_full(self):
1266 payload = self._create_payload_full()
1267 self.assertTrue(os.path.exists(payload.payload_file))
1268
1269 def test_Generate_incremental(self):
1270 payload = self._create_payload_incremental()
1271 self.assertTrue(os.path.exists(payload.payload_file))
1272
1273 def test_Generate_additionalArgs(self):
Tao Baof7140c02018-01-30 17:09:24 -08001274 target_file = construct_target_files()
1275 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001276 payload = Payload()
1277 # This should work the same as calling payload.Generate(target_file,
1278 # source_file).
1279 payload.Generate(
1280 target_file, additional_args=["--source_image", source_file])
1281 self.assertTrue(os.path.exists(payload.payload_file))
1282
1283 def test_Generate_invalidInput(self):
Tao Baof7140c02018-01-30 17:09:24 -08001284 target_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001285 common.ZipDelete(target_file, 'IMAGES/vendor.img')
1286 payload = Payload()
1287 self.assertRaises(AssertionError, payload.Generate, target_file)
1288
1289 def test_Sign_full(self):
1290 payload = self._create_payload_full()
1291 payload.Sign(PayloadSigner())
1292
1293 output_file = common.MakeTempFile(suffix='.zip')
1294 with zipfile.ZipFile(output_file, 'w') as output_zip:
1295 payload.WriteToZip(output_zip)
1296
1297 import check_ota_package_signature
1298 check_ota_package_signature.VerifyAbOtaPayload(
1299 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1300 output_file)
1301
1302 def test_Sign_incremental(self):
1303 payload = self._create_payload_incremental()
1304 payload.Sign(PayloadSigner())
1305
1306 output_file = common.MakeTempFile(suffix='.zip')
1307 with zipfile.ZipFile(output_file, 'w') as output_zip:
1308 payload.WriteToZip(output_zip)
1309
1310 import check_ota_package_signature
1311 check_ota_package_signature.VerifyAbOtaPayload(
1312 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1313 output_file)
1314
1315 def test_Sign_withDataWipe(self):
1316 common.OPTIONS.wipe_user_data = True
1317 payload = self._create_payload_full()
1318 payload.Sign(PayloadSigner())
1319
1320 with open(payload.payload_properties) as properties_fp:
1321 self.assertIn("POWERWASH=1", properties_fp.read())
1322
Tao Bao667ff572018-02-10 00:02:40 -08001323 def test_Sign_secondary(self):
1324 payload = self._create_payload_full(secondary=True)
1325 payload.Sign(PayloadSigner())
1326
1327 with open(payload.payload_properties) as properties_fp:
1328 self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read())
1329
Tao Baoc7b403a2018-01-30 18:19:04 -08001330 def test_Sign_badSigner(self):
1331 """Tests that signing failure can be captured."""
1332 payload = self._create_payload_full()
1333 payload_signer = PayloadSigner()
1334 payload_signer.signer_args.append('bad-option')
1335 self.assertRaises(AssertionError, payload.Sign, payload_signer)
1336
1337 def test_WriteToZip(self):
1338 payload = self._create_payload_full()
1339 payload.Sign(PayloadSigner())
1340
1341 output_file = common.MakeTempFile(suffix='.zip')
1342 with zipfile.ZipFile(output_file, 'w') as output_zip:
1343 payload.WriteToZip(output_zip)
1344
1345 with zipfile.ZipFile(output_file) as verify_zip:
1346 # First make sure we have the essential entries.
1347 namelist = verify_zip.namelist()
1348 self.assertIn(Payload.PAYLOAD_BIN, namelist)
1349 self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist)
1350
1351 # Then assert these entries are stored.
1352 for entry_info in verify_zip.infolist():
1353 if entry_info.filename not in (Payload.PAYLOAD_BIN,
1354 Payload.PAYLOAD_PROPERTIES_TXT):
1355 continue
1356 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)
1357
1358 def test_WriteToZip_unsignedPayload(self):
1359 """Unsigned payloads should not be allowed to be written to zip."""
1360 payload = self._create_payload_full()
1361
1362 output_file = common.MakeTempFile(suffix='.zip')
1363 with zipfile.ZipFile(output_file, 'w') as output_zip:
1364 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
1365
1366 # Also test with incremental payload.
1367 payload = self._create_payload_incremental()
1368
1369 output_file = common.MakeTempFile(suffix='.zip')
1370 with zipfile.ZipFile(output_file, 'w') as output_zip:
1371 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001372
1373 def test_WriteToZip_secondary(self):
1374 payload = self._create_payload_full(secondary=True)
1375 payload.Sign(PayloadSigner())
1376
1377 output_file = common.MakeTempFile(suffix='.zip')
1378 with zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Bao667ff572018-02-10 00:02:40 -08001379 payload.WriteToZip(output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001380
1381 with zipfile.ZipFile(output_file) as verify_zip:
1382 # First make sure we have the essential entries.
1383 namelist = verify_zip.namelist()
1384 self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist)
1385 self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist)
1386
1387 # Then assert these entries are stored.
1388 for entry_info in verify_zip.infolist():
1389 if entry_info.filename not in (
1390 Payload.SECONDARY_PAYLOAD_BIN,
1391 Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT):
1392 continue
1393 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)