blob: 213e830e5453629dd5773e8f0d4291665d71f74d [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 Baob6304672018-03-08 16:28:33 -080020import subprocess
Tao Bao481bab82017-12-21 11:23:09 -080021import unittest
Tao Baoc7b403a2018-01-30 18:19:04 -080022import zipfile
Tao Bao481bab82017-12-21 11:23:09 -080023
24import common
Tao Bao04e1f012018-02-04 12:13:35 -080025import test_utils
Tao Bao481bab82017-12-21 11:23:09 -080026from ota_from_target_files import (
Tao Bao3bf8c652018-03-16 12:59:42 -070027 _LoadOemDicts, AbOtaPropertyFiles, BuildInfo, FinalizeMetadata,
28 GetPackageMetadata, GetTargetFilesZipForSecondaryImages,
Tao Baoc0746f42018-02-21 13:17:22 -080029 GetTargetFilesZipWithoutPostinstallConfig, NonAbOtaPropertyFiles,
Tao Bao69203522018-03-08 16:09:01 -080030 Payload, PayloadSigner, POSTINSTALL_CONFIG, PropertyFiles,
31 StreamingPropertyFiles, WriteFingerprintAssertion)
Tao Baofabe0832018-01-17 15:52:28 -080032
33
Tao Baof7140c02018-01-30 17:09:24 -080034def construct_target_files(secondary=False):
35 """Returns a target-files.zip file for generating OTA packages."""
36 target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip')
37 with zipfile.ZipFile(target_files, 'w') as target_files_zip:
38 # META/update_engine_config.txt
39 target_files_zip.writestr(
40 'META/update_engine_config.txt',
41 "PAYLOAD_MAJOR_VERSION=2\nPAYLOAD_MINOR_VERSION=4\n")
42
Tao Bao15a146a2018-02-21 16:06:59 -080043 # META/postinstall_config.txt
44 target_files_zip.writestr(
45 POSTINSTALL_CONFIG,
46 '\n'.join([
47 "RUN_POSTINSTALL_system=true",
48 "POSTINSTALL_PATH_system=system/bin/otapreopt_script",
49 "FILESYSTEM_TYPE_system=ext4",
50 "POSTINSTALL_OPTIONAL_system=true",
51 ]))
52
Tao Bao5277d102018-04-17 23:47:21 -070053 ab_partitions = [
54 ('IMAGES', 'boot'),
55 ('IMAGES', 'system'),
56 ('IMAGES', 'vendor'),
57 ('RADIO', 'bootloader'),
58 ('RADIO', 'modem'),
59 ]
Tao Baof7140c02018-01-30 17:09:24 -080060 # META/ab_partitions.txt
Tao Baof7140c02018-01-30 17:09:24 -080061 target_files_zip.writestr(
62 'META/ab_partitions.txt',
Tao Bao5277d102018-04-17 23:47:21 -070063 '\n'.join([partition[1] for partition in ab_partitions]))
Tao Baof7140c02018-01-30 17:09:24 -080064
65 # Create dummy images for each of them.
Tao Bao5277d102018-04-17 23:47:21 -070066 for path, partition in ab_partitions:
67 target_files_zip.writestr(
68 '{}/{}.img'.format(path, partition),
69 os.urandom(len(partition)))
Tao Baof7140c02018-01-30 17:09:24 -080070
Tao Bao5277d102018-04-17 23:47:21 -070071 # system_other shouldn't appear in META/ab_partitions.txt.
Tao Baof7140c02018-01-30 17:09:24 -080072 if secondary:
73 target_files_zip.writestr('IMAGES/system_other.img',
74 os.urandom(len("system_other")))
75
76 return target_files
77
78
Tao Bao481bab82017-12-21 11:23:09 -080079class MockScriptWriter(object):
80 """A class that mocks edify_generator.EdifyGenerator.
81
82 It simply pushes the incoming arguments onto script stack, which is to assert
83 the calls to EdifyGenerator functions.
84 """
85
86 def __init__(self):
87 self.script = []
88
89 def Mount(self, *args):
90 self.script.append(('Mount',) + args)
91
92 def AssertDevice(self, *args):
93 self.script.append(('AssertDevice',) + args)
94
95 def AssertOemProperty(self, *args):
96 self.script.append(('AssertOemProperty',) + args)
97
98 def AssertFingerprintOrThumbprint(self, *args):
99 self.script.append(('AssertFingerprintOrThumbprint',) + args)
100
101 def AssertSomeFingerprint(self, *args):
102 self.script.append(('AssertSomeFingerprint',) + args)
103
104 def AssertSomeThumbprint(self, *args):
105 self.script.append(('AssertSomeThumbprint',) + args)
106
107
108class BuildInfoTest(unittest.TestCase):
109
110 TEST_INFO_DICT = {
111 'build.prop' : {
112 'ro.product.device' : 'product-device',
113 'ro.product.name' : 'product-name',
114 'ro.build.fingerprint' : 'build-fingerprint',
115 'ro.build.foo' : 'build-foo',
116 },
117 'vendor.build.prop' : {
118 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint',
119 },
120 'property1' : 'value1',
121 'property2' : 4096,
122 }
123
124 TEST_INFO_DICT_USES_OEM_PROPS = {
125 'build.prop' : {
126 'ro.product.name' : 'product-name',
127 'ro.build.thumbprint' : 'build-thumbprint',
128 'ro.build.bar' : 'build-bar',
129 },
130 'vendor.build.prop' : {
131 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint',
132 },
133 'property1' : 'value1',
134 'property2' : 4096,
135 'oem_fingerprint_properties' : 'ro.product.device ro.product.brand',
136 }
137
138 TEST_OEM_DICTS = [
139 {
140 'ro.product.brand' : 'brand1',
141 'ro.product.device' : 'device1',
142 },
143 {
144 'ro.product.brand' : 'brand2',
145 'ro.product.device' : 'device2',
146 },
147 {
148 'ro.product.brand' : 'brand3',
149 'ro.product.device' : 'device3',
150 },
151 ]
152
153 def test_init(self):
154 target_info = BuildInfo(self.TEST_INFO_DICT, None)
155 self.assertEqual('product-device', target_info.device)
156 self.assertEqual('build-fingerprint', target_info.fingerprint)
157 self.assertFalse(target_info.is_ab)
158 self.assertIsNone(target_info.oem_props)
159
160 def test_init_with_oem_props(self):
161 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
162 self.TEST_OEM_DICTS)
163 self.assertEqual('device1', target_info.device)
164 self.assertEqual('brand1/product-name/device1:build-thumbprint',
165 target_info.fingerprint)
166
167 # Swap the order in oem_dicts, which would lead to different BuildInfo.
168 oem_dicts = copy.copy(self.TEST_OEM_DICTS)
169 oem_dicts[0], oem_dicts[2] = oem_dicts[2], oem_dicts[0]
170 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, oem_dicts)
171 self.assertEqual('device3', target_info.device)
172 self.assertEqual('brand3/product-name/device3:build-thumbprint',
173 target_info.fingerprint)
174
175 # Missing oem_dict should be rejected.
176 self.assertRaises(AssertionError, BuildInfo,
177 self.TEST_INFO_DICT_USES_OEM_PROPS, None)
178
179 def test___getitem__(self):
180 target_info = BuildInfo(self.TEST_INFO_DICT, None)
181 self.assertEqual('value1', target_info['property1'])
182 self.assertEqual(4096, target_info['property2'])
183 self.assertEqual('build-foo', target_info['build.prop']['ro.build.foo'])
184
185 def test___getitem__with_oem_props(self):
186 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
187 self.TEST_OEM_DICTS)
188 self.assertEqual('value1', target_info['property1'])
189 self.assertEqual(4096, target_info['property2'])
190 self.assertRaises(KeyError,
191 lambda: target_info['build.prop']['ro.build.foo'])
192
193 def test_get(self):
194 target_info = BuildInfo(self.TEST_INFO_DICT, None)
195 self.assertEqual('value1', target_info.get('property1'))
196 self.assertEqual(4096, target_info.get('property2'))
197 self.assertEqual(4096, target_info.get('property2', 1024))
198 self.assertEqual(1024, target_info.get('property-nonexistent', 1024))
199 self.assertEqual('build-foo', target_info.get('build.prop')['ro.build.foo'])
200
201 def test_get_with_oem_props(self):
202 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
203 self.TEST_OEM_DICTS)
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.assertIsNone(target_info.get('build.prop').get('ro.build.foo'))
209 self.assertRaises(KeyError,
210 lambda: target_info.get('build.prop')['ro.build.foo'])
211
212 def test_GetBuildProp(self):
213 target_info = BuildInfo(self.TEST_INFO_DICT, None)
214 self.assertEqual('build-foo', target_info.GetBuildProp('ro.build.foo'))
215 self.assertRaises(common.ExternalError, target_info.GetBuildProp,
216 'ro.build.nonexistent')
217
218 def test_GetBuildProp_with_oem_props(self):
219 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
220 self.TEST_OEM_DICTS)
221 self.assertEqual('build-bar', target_info.GetBuildProp('ro.build.bar'))
222 self.assertRaises(common.ExternalError, target_info.GetBuildProp,
223 'ro.build.nonexistent')
224
225 def test_GetVendorBuildProp(self):
226 target_info = BuildInfo(self.TEST_INFO_DICT, None)
227 self.assertEqual('vendor-build-fingerprint',
228 target_info.GetVendorBuildProp(
229 'ro.vendor.build.fingerprint'))
230 self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp,
231 'ro.build.nonexistent')
232
233 def test_GetVendorBuildProp_with_oem_props(self):
234 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
235 self.TEST_OEM_DICTS)
236 self.assertEqual('vendor-build-fingerprint',
237 target_info.GetVendorBuildProp(
238 'ro.vendor.build.fingerprint'))
239 self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp,
240 'ro.build.nonexistent')
241
242 def test_WriteMountOemScript(self):
243 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
244 self.TEST_OEM_DICTS)
245 script_writer = MockScriptWriter()
246 target_info.WriteMountOemScript(script_writer)
247 self.assertEqual([('Mount', '/oem', None)], script_writer.script)
248
249 def test_WriteDeviceAssertions(self):
250 target_info = BuildInfo(self.TEST_INFO_DICT, None)
251 script_writer = MockScriptWriter()
252 target_info.WriteDeviceAssertions(script_writer, False)
253 self.assertEqual([('AssertDevice', 'product-device')], script_writer.script)
254
255 def test_WriteDeviceAssertions_with_oem_props(self):
256 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
257 self.TEST_OEM_DICTS)
258 script_writer = MockScriptWriter()
259 target_info.WriteDeviceAssertions(script_writer, False)
260 self.assertEqual(
261 [
262 ('AssertOemProperty', 'ro.product.device',
263 ['device1', 'device2', 'device3'], False),
264 ('AssertOemProperty', 'ro.product.brand',
265 ['brand1', 'brand2', 'brand3'], False),
266 ],
267 script_writer.script)
268
269 def test_WriteFingerprintAssertion_without_oem_props(self):
270 target_info = BuildInfo(self.TEST_INFO_DICT, None)
271 source_info_dict = copy.deepcopy(self.TEST_INFO_DICT)
272 source_info_dict['build.prop']['ro.build.fingerprint'] = (
273 'source-build-fingerprint')
274 source_info = BuildInfo(source_info_dict, None)
275
276 script_writer = MockScriptWriter()
277 WriteFingerprintAssertion(script_writer, target_info, source_info)
278 self.assertEqual(
279 [('AssertSomeFingerprint', 'source-build-fingerprint',
280 'build-fingerprint')],
281 script_writer.script)
282
283 def test_WriteFingerprintAssertion_with_source_oem_props(self):
284 target_info = BuildInfo(self.TEST_INFO_DICT, None)
285 source_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
286 self.TEST_OEM_DICTS)
287
288 script_writer = MockScriptWriter()
289 WriteFingerprintAssertion(script_writer, target_info, source_info)
290 self.assertEqual(
291 [('AssertFingerprintOrThumbprint', 'build-fingerprint',
292 'build-thumbprint')],
293 script_writer.script)
294
295 def test_WriteFingerprintAssertion_with_target_oem_props(self):
296 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
297 self.TEST_OEM_DICTS)
298 source_info = BuildInfo(self.TEST_INFO_DICT, None)
299
300 script_writer = MockScriptWriter()
301 WriteFingerprintAssertion(script_writer, target_info, source_info)
302 self.assertEqual(
303 [('AssertFingerprintOrThumbprint', 'build-fingerprint',
304 'build-thumbprint')],
305 script_writer.script)
306
307 def test_WriteFingerprintAssertion_with_both_oem_props(self):
308 target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS,
309 self.TEST_OEM_DICTS)
310 source_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS)
311 source_info_dict['build.prop']['ro.build.thumbprint'] = (
312 'source-build-thumbprint')
313 source_info = BuildInfo(source_info_dict, self.TEST_OEM_DICTS)
314
315 script_writer = MockScriptWriter()
316 WriteFingerprintAssertion(script_writer, target_info, source_info)
317 self.assertEqual(
318 [('AssertSomeThumbprint', 'build-thumbprint',
319 'source-build-thumbprint')],
320 script_writer.script)
321
322
323class LoadOemDictsTest(unittest.TestCase):
324
325 def tearDown(self):
326 common.Cleanup()
327
328 def test_NoneDict(self):
329 self.assertIsNone(_LoadOemDicts(None))
330
331 def test_SingleDict(self):
332 dict_file = common.MakeTempFile()
333 with open(dict_file, 'w') as dict_fp:
334 dict_fp.write('abc=1\ndef=2\nxyz=foo\na.b.c=bar\n')
335
336 oem_dicts = _LoadOemDicts([dict_file])
337 self.assertEqual(1, len(oem_dicts))
338 self.assertEqual('foo', oem_dicts[0]['xyz'])
339 self.assertEqual('bar', oem_dicts[0]['a.b.c'])
340
341 def test_MultipleDicts(self):
342 oem_source = []
343 for i in range(3):
344 dict_file = common.MakeTempFile()
345 with open(dict_file, 'w') as dict_fp:
346 dict_fp.write(
347 'ro.build.index={}\ndef=2\nxyz=foo\na.b.c=bar\n'.format(i))
348 oem_source.append(dict_file)
349
350 oem_dicts = _LoadOemDicts(oem_source)
351 self.assertEqual(3, len(oem_dicts))
352 for i, oem_dict in enumerate(oem_dicts):
353 self.assertEqual('2', oem_dict['def'])
354 self.assertEqual('foo', oem_dict['xyz'])
355 self.assertEqual('bar', oem_dict['a.b.c'])
356 self.assertEqual('{}'.format(i), oem_dict['ro.build.index'])
Tao Baodf3a48b2018-01-10 16:30:43 -0800357
358
359class OtaFromTargetFilesTest(unittest.TestCase):
360
361 TEST_TARGET_INFO_DICT = {
362 'build.prop' : {
363 'ro.product.device' : 'product-device',
364 'ro.build.fingerprint' : 'build-fingerprint-target',
365 'ro.build.version.incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800366 'ro.build.version.sdk' : '27',
367 'ro.build.version.security_patch' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800368 'ro.build.date.utc' : '1500000000',
369 },
370 }
371
372 TEST_SOURCE_INFO_DICT = {
373 'build.prop' : {
374 'ro.product.device' : 'product-device',
375 'ro.build.fingerprint' : 'build-fingerprint-source',
376 'ro.build.version.incremental' : 'build-version-incremental-source',
Tao Bao35dc2552018-02-01 13:18:00 -0800377 'ro.build.version.sdk' : '25',
378 'ro.build.version.security_patch' : '2016-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800379 'ro.build.date.utc' : '1400000000',
380 },
381 }
382
383 def setUp(self):
Tao Bao3bf8c652018-03-16 12:59:42 -0700384 self.testdata_dir = test_utils.get_testdata_dir()
385 self.assertTrue(os.path.exists(self.testdata_dir))
386
Tao Baodf3a48b2018-01-10 16:30:43 -0800387 # Reset the global options as in ota_from_target_files.py.
388 common.OPTIONS.incremental_source = None
389 common.OPTIONS.downgrade = False
390 common.OPTIONS.timestamp = False
391 common.OPTIONS.wipe_user_data = False
Tao Bao3bf8c652018-03-16 12:59:42 -0700392 common.OPTIONS.no_signing = False
393 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
394 common.OPTIONS.key_passwords = {
395 common.OPTIONS.package_key : None,
396 }
397
398 common.OPTIONS.search_path = test_utils.get_search_path()
399 self.assertIsNotNone(common.OPTIONS.search_path)
Tao Baodf3a48b2018-01-10 16:30:43 -0800400
Tao Baof5110492018-03-02 09:47:43 -0800401 def tearDown(self):
402 common.Cleanup()
403
Tao Baodf3a48b2018-01-10 16:30:43 -0800404 def test_GetPackageMetadata_abOta_full(self):
405 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
406 target_info_dict['ab_update'] = 'true'
407 target_info = BuildInfo(target_info_dict, None)
408 metadata = GetPackageMetadata(target_info)
409 self.assertDictEqual(
410 {
411 'ota-type' : 'AB',
412 'ota-required-cache' : '0',
413 'post-build' : 'build-fingerprint-target',
414 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800415 'post-sdk-level' : '27',
416 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800417 'post-timestamp' : '1500000000',
418 'pre-device' : 'product-device',
419 },
420 metadata)
421
422 def test_GetPackageMetadata_abOta_incremental(self):
423 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
424 target_info_dict['ab_update'] = 'true'
425 target_info = BuildInfo(target_info_dict, None)
426 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
427 common.OPTIONS.incremental_source = ''
428 metadata = GetPackageMetadata(target_info, source_info)
429 self.assertDictEqual(
430 {
431 'ota-type' : 'AB',
432 'ota-required-cache' : '0',
433 'post-build' : 'build-fingerprint-target',
434 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800435 'post-sdk-level' : '27',
436 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800437 'post-timestamp' : '1500000000',
438 'pre-device' : 'product-device',
439 'pre-build' : 'build-fingerprint-source',
440 'pre-build-incremental' : 'build-version-incremental-source',
441 },
442 metadata)
443
444 def test_GetPackageMetadata_nonAbOta_full(self):
445 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
446 metadata = GetPackageMetadata(target_info)
447 self.assertDictEqual(
448 {
449 'ota-type' : 'BLOCK',
450 'post-build' : 'build-fingerprint-target',
451 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800452 'post-sdk-level' : '27',
453 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800454 'post-timestamp' : '1500000000',
455 'pre-device' : 'product-device',
456 },
457 metadata)
458
459 def test_GetPackageMetadata_nonAbOta_incremental(self):
460 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
461 source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None)
462 common.OPTIONS.incremental_source = ''
463 metadata = GetPackageMetadata(target_info, source_info)
464 self.assertDictEqual(
465 {
466 'ota-type' : 'BLOCK',
467 'post-build' : 'build-fingerprint-target',
468 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800469 'post-sdk-level' : '27',
470 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800471 'post-timestamp' : '1500000000',
472 'pre-device' : 'product-device',
473 'pre-build' : 'build-fingerprint-source',
474 'pre-build-incremental' : 'build-version-incremental-source',
475 },
476 metadata)
477
478 def test_GetPackageMetadata_wipe(self):
479 target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None)
480 common.OPTIONS.wipe_user_data = True
481 metadata = GetPackageMetadata(target_info)
482 self.assertDictEqual(
483 {
484 'ota-type' : 'BLOCK',
485 'ota-wipe' : 'yes',
486 'post-build' : 'build-fingerprint-target',
487 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800488 'post-sdk-level' : '27',
489 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800490 'post-timestamp' : '1500000000',
491 'pre-device' : 'product-device',
492 },
493 metadata)
494
495 @staticmethod
496 def _test_GetPackageMetadata_swapBuildTimestamps(target_info, source_info):
497 (target_info['build.prop']['ro.build.date.utc'],
498 source_info['build.prop']['ro.build.date.utc']) = (
499 source_info['build.prop']['ro.build.date.utc'],
500 target_info['build.prop']['ro.build.date.utc'])
501
502 def test_GetPackageMetadata_unintentionalDowngradeDetected(self):
503 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
504 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
505 self._test_GetPackageMetadata_swapBuildTimestamps(
506 target_info_dict, source_info_dict)
507
508 target_info = BuildInfo(target_info_dict, None)
509 source_info = BuildInfo(source_info_dict, None)
510 common.OPTIONS.incremental_source = ''
511 self.assertRaises(RuntimeError, GetPackageMetadata, target_info,
512 source_info)
513
514 def test_GetPackageMetadata_downgrade(self):
515 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
516 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
517 self._test_GetPackageMetadata_swapBuildTimestamps(
518 target_info_dict, source_info_dict)
519
520 target_info = BuildInfo(target_info_dict, None)
521 source_info = BuildInfo(source_info_dict, None)
522 common.OPTIONS.incremental_source = ''
523 common.OPTIONS.downgrade = True
524 common.OPTIONS.wipe_user_data = True
525 metadata = GetPackageMetadata(target_info, source_info)
526 self.assertDictEqual(
527 {
528 'ota-downgrade' : 'yes',
529 'ota-type' : 'BLOCK',
530 'ota-wipe' : 'yes',
531 'post-build' : 'build-fingerprint-target',
532 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800533 'post-sdk-level' : '27',
534 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800535 'pre-device' : 'product-device',
536 'pre-build' : 'build-fingerprint-source',
537 'pre-build-incremental' : 'build-version-incremental-source',
538 },
539 metadata)
540
541 def test_GetPackageMetadata_overrideTimestamp(self):
542 target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT)
543 source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT)
544 self._test_GetPackageMetadata_swapBuildTimestamps(
545 target_info_dict, source_info_dict)
546
547 target_info = BuildInfo(target_info_dict, None)
548 source_info = BuildInfo(source_info_dict, None)
549 common.OPTIONS.incremental_source = ''
550 common.OPTIONS.timestamp = True
551 metadata = GetPackageMetadata(target_info, source_info)
552 self.assertDictEqual(
553 {
554 'ota-type' : 'BLOCK',
555 'post-build' : 'build-fingerprint-target',
556 'post-build-incremental' : 'build-version-incremental-target',
Tao Bao35dc2552018-02-01 13:18:00 -0800557 'post-sdk-level' : '27',
558 'post-security-patch-level' : '2017-12-01',
Tao Baodf3a48b2018-01-10 16:30:43 -0800559 'post-timestamp' : '1500000001',
560 'pre-device' : 'product-device',
561 'pre-build' : 'build-fingerprint-source',
562 'pre-build-incremental' : 'build-version-incremental-source',
563 },
564 metadata)
Tao Baofabe0832018-01-17 15:52:28 -0800565
Tao Baof7140c02018-01-30 17:09:24 -0800566 def test_GetTargetFilesZipForSecondaryImages(self):
567 input_file = construct_target_files(secondary=True)
568 target_file = GetTargetFilesZipForSecondaryImages(input_file)
569
570 with zipfile.ZipFile(target_file) as verify_zip:
571 namelist = verify_zip.namelist()
572
573 self.assertIn('META/ab_partitions.txt', namelist)
574 self.assertIn('IMAGES/boot.img', namelist)
575 self.assertIn('IMAGES/system.img', namelist)
576 self.assertIn('IMAGES/vendor.img', namelist)
Tao Bao15a146a2018-02-21 16:06:59 -0800577 self.assertIn(POSTINSTALL_CONFIG, namelist)
Tao Baof7140c02018-01-30 17:09:24 -0800578
579 self.assertNotIn('IMAGES/system_other.img', namelist)
580 self.assertNotIn('IMAGES/system.map', namelist)
581
Tao Bao15a146a2018-02-21 16:06:59 -0800582 def test_GetTargetFilesZipForSecondaryImages_skipPostinstall(self):
583 input_file = construct_target_files(secondary=True)
584 target_file = GetTargetFilesZipForSecondaryImages(
585 input_file, skip_postinstall=True)
586
587 with zipfile.ZipFile(target_file) as verify_zip:
588 namelist = verify_zip.namelist()
589
590 self.assertIn('META/ab_partitions.txt', namelist)
591 self.assertIn('IMAGES/boot.img', namelist)
592 self.assertIn('IMAGES/system.img', namelist)
593 self.assertIn('IMAGES/vendor.img', namelist)
594
595 self.assertNotIn('IMAGES/system_other.img', namelist)
596 self.assertNotIn('IMAGES/system.map', namelist)
597 self.assertNotIn(POSTINSTALL_CONFIG, namelist)
598
599 def test_GetTargetFilesZipWithoutPostinstallConfig(self):
600 input_file = construct_target_files()
601 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
602 with zipfile.ZipFile(target_file) as verify_zip:
603 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
604
605 def test_GetTargetFilesZipWithoutPostinstallConfig_missingEntry(self):
606 input_file = construct_target_files()
607 common.ZipDelete(input_file, POSTINSTALL_CONFIG)
608 target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file)
609 with zipfile.ZipFile(target_file) as verify_zip:
610 self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist())
611
Tao Bao3bf8c652018-03-16 12:59:42 -0700612 def _test_FinalizeMetadata(self, large_entry=False):
613 entries = [
614 'required-entry1',
615 'required-entry2',
616 ]
617 zip_file = PropertyFilesTest.construct_zip_package(entries)
618 # Add a large entry of 1 GiB if requested.
619 if large_entry:
620 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
621 zip_fp.writestr(
622 # Using 'zoo' so that the entry stays behind others after signing.
623 'zoo',
624 'A' * 1024 * 1024 * 1024,
625 zipfile.ZIP_STORED)
626
627 metadata = {}
628 output_file = common.MakeTempFile(suffix='.zip')
629 needed_property_files = (
630 TestPropertyFiles(),
631 )
632 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
633 self.assertIn('ota-test-property-files', metadata)
634
635 def test_FinalizeMetadata(self):
636 self._test_FinalizeMetadata()
637
638 def test_FinalizeMetadata_withNoSigning(self):
639 common.OPTIONS.no_signing = True
640 self._test_FinalizeMetadata()
641
642 def test_FinalizeMetadata_largeEntry(self):
643 self._test_FinalizeMetadata(large_entry=True)
644
645 def test_FinalizeMetadata_largeEntry_withNoSigning(self):
646 common.OPTIONS.no_signing = True
647 self._test_FinalizeMetadata(large_entry=True)
648
649 def test_FinalizeMetadata_insufficientSpace(self):
650 entries = [
651 'required-entry1',
652 'required-entry2',
653 'optional-entry1',
654 'optional-entry2',
655 ]
656 zip_file = PropertyFilesTest.construct_zip_package(entries)
657 with zipfile.ZipFile(zip_file, 'a') as zip_fp:
658 zip_fp.writestr(
659 # 'foo-entry1' will appear ahead of all other entries (in alphabetical
660 # order) after the signing, which will in turn trigger the
661 # InsufficientSpaceException and an automatic retry.
662 'foo-entry1',
663 'A' * 1024 * 1024,
664 zipfile.ZIP_STORED)
665
666 metadata = {}
667 needed_property_files = (
668 TestPropertyFiles(),
669 )
670 output_file = common.MakeTempFile(suffix='.zip')
671 FinalizeMetadata(metadata, zip_file, output_file, needed_property_files)
672 self.assertIn('ota-test-property-files', metadata)
673
Tao Baoae5e4c32018-03-01 19:30:00 -0800674
Tao Bao69203522018-03-08 16:09:01 -0800675class TestPropertyFiles(PropertyFiles):
676 """A class that extends PropertyFiles for testing purpose."""
677
678 def __init__(self):
679 super(TestPropertyFiles, self).__init__()
680 self.name = 'ota-test-property-files'
681 self.required = (
682 'required-entry1',
683 'required-entry2',
684 )
685 self.optional = (
686 'optional-entry1',
687 'optional-entry2',
688 )
689
690
691class PropertyFilesTest(unittest.TestCase):
Tao Baoae5e4c32018-03-01 19:30:00 -0800692
Tao Bao3bf8c652018-03-16 12:59:42 -0700693 def setUp(self):
694 common.OPTIONS.no_signing = False
695
Tao Baoae5e4c32018-03-01 19:30:00 -0800696 def tearDown(self):
697 common.Cleanup()
698
Tao Baof5110492018-03-02 09:47:43 -0800699 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -0700700 def construct_zip_package(entries):
Tao Baof5110492018-03-02 09:47:43 -0800701 zip_file = common.MakeTempFile(suffix='.zip')
702 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
703 for entry in entries:
704 zip_fp.writestr(
705 entry,
706 entry.replace('.', '-').upper(),
707 zipfile.ZIP_STORED)
708 return zip_file
709
710 @staticmethod
Tao Bao69203522018-03-08 16:09:01 -0800711 def _parse_property_files_string(data):
Tao Baof5110492018-03-02 09:47:43 -0800712 result = {}
713 for token in data.split(','):
714 name, info = token.split(':', 1)
715 result[name] = info
716 return result
717
718 def _verify_entries(self, input_file, tokens, entries):
719 for entry in entries:
720 offset, size = map(int, tokens[entry].split(':'))
721 with open(input_file, 'rb') as input_fp:
722 input_fp.seek(offset)
723 if entry == 'metadata':
724 expected = b'META-INF/COM/ANDROID/METADATA'
725 else:
726 expected = entry.replace('.', '-').upper().encode()
727 self.assertEqual(expected, input_fp.read(size))
728
Tao Baoae5e4c32018-03-01 19:30:00 -0800729 def test_Compute(self):
Tao Baof5110492018-03-02 09:47:43 -0800730 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800731 'required-entry1',
732 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800733 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700734 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800735 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800736 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800737 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800738
Tao Bao69203522018-03-08 16:09:01 -0800739 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800740 self.assertEqual(3, len(tokens))
741 self._verify_entries(zip_file, tokens, entries)
742
Tao Bao69203522018-03-08 16:09:01 -0800743 def test_Compute_withOptionalEntries(self):
Tao Baof5110492018-03-02 09:47:43 -0800744 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800745 'required-entry1',
746 'required-entry2',
747 'optional-entry1',
748 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800749 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700750 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800751 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800752 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Tao Bao69203522018-03-08 16:09:01 -0800753 property_files_string = property_files.Compute(zip_fp)
Tao Baof5110492018-03-02 09:47:43 -0800754
Tao Bao69203522018-03-08 16:09:01 -0800755 tokens = self._parse_property_files_string(property_files_string)
Tao Baof5110492018-03-02 09:47:43 -0800756 self.assertEqual(5, len(tokens))
757 self._verify_entries(zip_file, tokens, entries)
758
Tao Bao69203522018-03-08 16:09:01 -0800759 def test_Compute_missingRequiredEntry(self):
760 entries = (
761 'required-entry2',
762 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700763 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800764 property_files = TestPropertyFiles()
765 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
766 self.assertRaises(KeyError, property_files.Compute, zip_fp)
767
Tao Baoae5e4c32018-03-01 19:30:00 -0800768 def test_Finalize(self):
Tao Baof5110492018-03-02 09:47:43 -0800769 entries = [
Tao Bao69203522018-03-08 16:09:01 -0800770 'required-entry1',
771 'required-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800772 'META-INF/com/android/metadata',
773 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700774 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800775 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800776 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700777 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800778 zip_fp, reserve_space=False)
779 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
Tao Bao69203522018-03-08 16:09:01 -0800780 tokens = self._parse_property_files_string(streaming_metadata)
Tao Baof5110492018-03-02 09:47:43 -0800781
782 self.assertEqual(3, len(tokens))
783 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
784 # streaming metadata.
785 entries[2] = 'metadata'
786 self._verify_entries(zip_file, tokens, entries)
787
Tao Baoae5e4c32018-03-01 19:30:00 -0800788 def test_Finalize_assertReservedLength(self):
Tao Baof5110492018-03-02 09:47:43 -0800789 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800790 'required-entry1',
791 'required-entry2',
792 'optional-entry1',
793 'optional-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:
799 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700800 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800801 zip_fp, reserve_space=False)
Tao Baof5110492018-03-02 09:47:43 -0800802 raw_length = len(raw_metadata)
803
804 # Now pass in the exact expected length.
Tao Baoae5e4c32018-03-01 19:30:00 -0800805 streaming_metadata = property_files.Finalize(zip_fp, raw_length)
Tao Baof5110492018-03-02 09:47:43 -0800806 self.assertEqual(raw_length, len(streaming_metadata))
807
808 # Or pass in insufficient length.
809 self.assertRaises(
Tao Bao3bf8c652018-03-16 12:59:42 -0700810 PropertyFiles.InsufficientSpaceException,
Tao Baoae5e4c32018-03-01 19:30:00 -0800811 property_files.Finalize,
Tao Baof5110492018-03-02 09:47:43 -0800812 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800813 raw_length - 1)
Tao Baof5110492018-03-02 09:47:43 -0800814
815 # Or pass in a much larger size.
Tao Baoae5e4c32018-03-01 19:30:00 -0800816 streaming_metadata = property_files.Finalize(
Tao Baof5110492018-03-02 09:47:43 -0800817 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800818 raw_length + 20)
Tao Baof5110492018-03-02 09:47:43 -0800819 self.assertEqual(raw_length + 20, len(streaming_metadata))
820 self.assertEqual(' ' * 20, streaming_metadata[raw_length:])
821
Tao Baoae5e4c32018-03-01 19:30:00 -0800822 def test_Verify(self):
823 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800824 'required-entry1',
825 'required-entry2',
826 'optional-entry1',
827 'optional-entry2',
828 'META-INF/com/android/metadata',
829 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700830 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800831 property_files = TestPropertyFiles()
832 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
833 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700834 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800835 zip_fp, reserve_space=False)
836
837 # Should pass the test if verification passes.
838 property_files.Verify(zip_fp, raw_metadata)
839
840 # Or raise on verification failure.
841 self.assertRaises(
842 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
843
844
845class StreamingPropertyFilesTest(PropertyFilesTest):
846 """Additional sanity checks specialized for StreamingPropertyFiles."""
847
848 def test_init(self):
849 property_files = StreamingPropertyFiles()
850 self.assertEqual('ota-streaming-property-files', property_files.name)
851 self.assertEqual(
852 (
853 'payload.bin',
854 'payload_properties.txt',
855 ),
856 property_files.required)
857 self.assertEqual(
858 (
859 'care_map.txt',
860 'compatibility.zip',
861 ),
862 property_files.optional)
863
864 def test_Compute(self):
865 entries = (
Tao Baoae5e4c32018-03-01 19:30:00 -0800866 'payload.bin',
867 'payload_properties.txt',
868 'care_map.txt',
Tao Bao69203522018-03-08 16:09:01 -0800869 'compatibility.zip',
870 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700871 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800872 property_files = StreamingPropertyFiles()
873 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
874 property_files_string = property_files.Compute(zip_fp)
875
876 tokens = self._parse_property_files_string(property_files_string)
877 self.assertEqual(5, len(tokens))
878 self._verify_entries(zip_file, tokens, entries)
879
880 def test_Finalize(self):
881 entries = [
882 'payload.bin',
883 'payload_properties.txt',
884 'care_map.txt',
885 'compatibility.zip',
886 'META-INF/com/android/metadata',
887 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700888 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800889 property_files = StreamingPropertyFiles()
890 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700891 raw_metadata = property_files.GetPropertyFilesString(
Tao Bao69203522018-03-08 16:09:01 -0800892 zip_fp, reserve_space=False)
893 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
894 tokens = self._parse_property_files_string(streaming_metadata)
895
896 self.assertEqual(5, len(tokens))
897 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
898 # streaming metadata.
899 entries[4] = 'metadata'
900 self._verify_entries(zip_file, tokens, entries)
901
902 def test_Verify(self):
903 entries = (
904 'payload.bin',
905 'payload_properties.txt',
906 'care_map.txt',
907 'compatibility.zip',
Tao Baoae5e4c32018-03-01 19:30:00 -0800908 'META-INF/com/android/metadata',
909 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700910 zip_file = self.construct_zip_package(entries)
Tao Baoae5e4c32018-03-01 19:30:00 -0800911 property_files = StreamingPropertyFiles()
912 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
913 # First get the raw metadata string (i.e. without padding space).
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -0700914 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoae5e4c32018-03-01 19:30:00 -0800915 zip_fp, reserve_space=False)
916
917 # Should pass the test if verification passes.
918 property_files.Verify(zip_fp, raw_metadata)
919
920 # Or raise on verification failure.
921 self.assertRaises(
922 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
923
Tao Baofabe0832018-01-17 15:52:28 -0800924
Tao Baob6304672018-03-08 16:28:33 -0800925class AbOtaPropertyFilesTest(PropertyFilesTest):
926 """Additional sanity checks specialized for AbOtaPropertyFiles."""
927
928 # The size for payload and metadata signature size.
929 SIGNATURE_SIZE = 256
930
931 def setUp(self):
932 self.testdata_dir = test_utils.get_testdata_dir()
933 self.assertTrue(os.path.exists(self.testdata_dir))
934
935 common.OPTIONS.wipe_user_data = False
936 common.OPTIONS.payload_signer = None
937 common.OPTIONS.payload_signer_args = None
938 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
939 common.OPTIONS.key_passwords = {
940 common.OPTIONS.package_key : None,
941 }
942
943 def test_init(self):
944 property_files = AbOtaPropertyFiles()
945 self.assertEqual('ota-property-files', property_files.name)
946 self.assertEqual(
947 (
948 'payload.bin',
949 'payload_properties.txt',
950 ),
951 property_files.required)
952 self.assertEqual(
953 (
954 'care_map.txt',
955 'compatibility.zip',
956 ),
957 property_files.optional)
958
959 def test_GetPayloadMetadataOffsetAndSize(self):
960 target_file = construct_target_files()
961 payload = Payload()
962 payload.Generate(target_file)
963
964 payload_signer = PayloadSigner()
965 payload.Sign(payload_signer)
966
967 output_file = common.MakeTempFile(suffix='.zip')
968 with zipfile.ZipFile(output_file, 'w') as output_zip:
969 payload.WriteToZip(output_zip)
970
971 # Find out the payload metadata offset and size.
972 property_files = AbOtaPropertyFiles()
973 with zipfile.ZipFile(output_file) as input_zip:
974 # pylint: disable=protected-access
975 payload_offset, metadata_total = (
976 property_files._GetPayloadMetadataOffsetAndSize(input_zip))
977
978 # Read in the metadata signature directly.
979 with open(output_file, 'rb') as verify_fp:
980 verify_fp.seek(payload_offset + metadata_total - self.SIGNATURE_SIZE)
981 metadata_signature = verify_fp.read(self.SIGNATURE_SIZE)
982
983 # Now we extract the metadata hash via brillo_update_payload script, which
984 # will serve as the oracle result.
985 payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
986 metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
987 cmd = ['brillo_update_payload', 'hash',
988 '--unsigned_payload', payload.payload_file,
989 '--signature_size', str(self.SIGNATURE_SIZE),
990 '--metadata_hash_file', metadata_sig_file,
991 '--payload_hash_file', payload_sig_file]
992 proc = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
993 stdoutdata, _ = proc.communicate()
994 self.assertEqual(
995 0, proc.returncode,
996 'Failed to run brillo_update_payload: {}'.format(stdoutdata))
997
998 signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file)
999
1000 # Finally we can compare the two signatures.
1001 with open(signed_metadata_sig_file, 'rb') as verify_fp:
1002 self.assertEqual(verify_fp.read(), metadata_signature)
1003
1004 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -07001005 def construct_zip_package_withValidPayload(with_metadata=False):
1006 # Cannot use construct_zip_package() since we need a "valid" payload.bin.
Tao Baob6304672018-03-08 16:28:33 -08001007 target_file = construct_target_files()
1008 payload = Payload()
1009 payload.Generate(target_file)
1010
1011 payload_signer = PayloadSigner()
1012 payload.Sign(payload_signer)
1013
1014 zip_file = common.MakeTempFile(suffix='.zip')
1015 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
1016 # 'payload.bin',
1017 payload.WriteToZip(zip_fp)
1018
1019 # Other entries.
1020 entries = ['care_map.txt', 'compatibility.zip']
1021
1022 # Put META-INF/com/android/metadata if needed.
1023 if with_metadata:
1024 entries.append('META-INF/com/android/metadata')
1025
1026 for entry in entries:
1027 zip_fp.writestr(
1028 entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED)
1029
1030 return zip_file
1031
1032 def test_Compute(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001033 zip_file = self.construct_zip_package_withValidPayload()
Tao Baob6304672018-03-08 16:28:33 -08001034 property_files = AbOtaPropertyFiles()
1035 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1036 property_files_string = property_files.Compute(zip_fp)
1037
1038 tokens = self._parse_property_files_string(property_files_string)
1039 # "6" indcludes the four entries above, one metadata entry, and one entry
1040 # for payload-metadata.bin.
1041 self.assertEqual(6, len(tokens))
1042 self._verify_entries(
1043 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1044
1045 def test_Finalize(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001046 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001047 property_files = AbOtaPropertyFiles()
1048 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001049 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001050 zip_fp, reserve_space=False)
1051 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1052
1053 tokens = self._parse_property_files_string(property_files_string)
1054 # "6" indcludes the four entries above, one metadata entry, and one entry
1055 # for payload-metadata.bin.
1056 self.assertEqual(6, len(tokens))
1057 self._verify_entries(
1058 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1059
1060 def test_Verify(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001061 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001062 property_files = AbOtaPropertyFiles()
1063 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001064 raw_metadata = property_files.GetPropertyFilesString(
Tao Baob6304672018-03-08 16:28:33 -08001065 zip_fp, reserve_space=False)
1066
1067 property_files.Verify(zip_fp, raw_metadata)
1068
1069
Tao Baoc0746f42018-02-21 13:17:22 -08001070class NonAbOtaPropertyFilesTest(PropertyFilesTest):
1071 """Additional sanity checks specialized for NonAbOtaPropertyFiles."""
1072
1073 def test_init(self):
1074 property_files = NonAbOtaPropertyFiles()
1075 self.assertEqual('ota-property-files', property_files.name)
1076 self.assertEqual((), property_files.required)
1077 self.assertEqual((), property_files.optional)
1078
1079 def test_Compute(self):
1080 entries = ()
Tao Bao3bf8c652018-03-16 12:59:42 -07001081 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001082 property_files = NonAbOtaPropertyFiles()
1083 with zipfile.ZipFile(zip_file) as zip_fp:
1084 property_files_string = property_files.Compute(zip_fp)
1085
1086 tokens = self._parse_property_files_string(property_files_string)
1087 self.assertEqual(1, len(tokens))
1088 self._verify_entries(zip_file, tokens, entries)
1089
1090 def test_Finalize(self):
1091 entries = [
1092 'META-INF/com/android/metadata',
1093 ]
Tao Bao3bf8c652018-03-16 12:59:42 -07001094 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001095 property_files = NonAbOtaPropertyFiles()
1096 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001097 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001098 zip_fp, reserve_space=False)
1099 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1100 tokens = self._parse_property_files_string(property_files_string)
1101
1102 self.assertEqual(1, len(tokens))
1103 # 'META-INF/com/android/metadata' will be key'd as 'metadata'.
1104 entries[0] = 'metadata'
1105 self._verify_entries(zip_file, tokens, entries)
1106
1107 def test_Verify(self):
1108 entries = (
1109 'META-INF/com/android/metadata',
1110 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001111 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001112 property_files = NonAbOtaPropertyFiles()
1113 with zipfile.ZipFile(zip_file) as zip_fp:
Zhomart Mukhamejanov603655f2018-05-04 12:35:09 -07001114 raw_metadata = property_files.GetPropertyFilesString(
Tao Baoc0746f42018-02-21 13:17:22 -08001115 zip_fp, reserve_space=False)
1116
1117 property_files.Verify(zip_fp, raw_metadata)
1118
1119
Tao Baofabe0832018-01-17 15:52:28 -08001120class PayloadSignerTest(unittest.TestCase):
1121
1122 SIGFILE = 'sigfile.bin'
1123 SIGNED_SIGFILE = 'signed-sigfile.bin'
1124
1125 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001126 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baofabe0832018-01-17 15:52:28 -08001127 self.assertTrue(os.path.exists(self.testdata_dir))
1128
1129 common.OPTIONS.payload_signer = None
1130 common.OPTIONS.payload_signer_args = []
1131 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1132 common.OPTIONS.key_passwords = {
1133 common.OPTIONS.package_key : None,
1134 }
1135
1136 def tearDown(self):
1137 common.Cleanup()
1138
1139 def _assertFilesEqual(self, file1, file2):
1140 with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
1141 self.assertEqual(fp1.read(), fp2.read())
1142
1143 def test_init(self):
1144 payload_signer = PayloadSigner()
1145 self.assertEqual('openssl', payload_signer.signer)
1146
1147 def test_init_withPassword(self):
1148 common.OPTIONS.package_key = os.path.join(
1149 self.testdata_dir, 'testkey_with_passwd')
1150 common.OPTIONS.key_passwords = {
1151 common.OPTIONS.package_key : 'foo',
1152 }
1153 payload_signer = PayloadSigner()
1154 self.assertEqual('openssl', payload_signer.signer)
1155
1156 def test_init_withExternalSigner(self):
1157 common.OPTIONS.payload_signer = 'abc'
1158 common.OPTIONS.payload_signer_args = ['arg1', 'arg2']
1159 payload_signer = PayloadSigner()
1160 self.assertEqual('abc', payload_signer.signer)
1161 self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args)
1162
1163 def test_Sign(self):
1164 payload_signer = PayloadSigner()
1165 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1166 signed_file = payload_signer.Sign(input_file)
1167
1168 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1169 self._assertFilesEqual(verify_file, signed_file)
1170
1171 def test_Sign_withExternalSigner_openssl(self):
1172 """Uses openssl as the external payload signer."""
1173 common.OPTIONS.payload_signer = 'openssl'
1174 common.OPTIONS.payload_signer_args = [
1175 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey',
1176 os.path.join(self.testdata_dir, 'testkey.pk8'),
1177 '-pkeyopt', 'digest:sha256']
1178 payload_signer = PayloadSigner()
1179 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1180 signed_file = payload_signer.Sign(input_file)
1181
1182 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1183 self._assertFilesEqual(verify_file, signed_file)
1184
1185 def test_Sign_withExternalSigner_script(self):
1186 """Uses testdata/payload_signer.sh as the external payload signer."""
1187 common.OPTIONS.payload_signer = os.path.join(
1188 self.testdata_dir, 'payload_signer.sh')
1189 common.OPTIONS.payload_signer_args = [
1190 os.path.join(self.testdata_dir, 'testkey.pk8')]
1191 payload_signer = PayloadSigner()
1192 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1193 signed_file = payload_signer.Sign(input_file)
1194
1195 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1196 self._assertFilesEqual(verify_file, signed_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001197
1198
1199class PayloadTest(unittest.TestCase):
1200
1201 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001202 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoc7b403a2018-01-30 18:19:04 -08001203 self.assertTrue(os.path.exists(self.testdata_dir))
1204
1205 common.OPTIONS.wipe_user_data = False
1206 common.OPTIONS.payload_signer = None
1207 common.OPTIONS.payload_signer_args = None
1208 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1209 common.OPTIONS.key_passwords = {
1210 common.OPTIONS.package_key : None,
1211 }
1212
1213 def tearDown(self):
1214 common.Cleanup()
1215
1216 @staticmethod
Tao Baof7140c02018-01-30 17:09:24 -08001217 def _create_payload_full(secondary=False):
1218 target_file = construct_target_files(secondary)
Tao Bao667ff572018-02-10 00:02:40 -08001219 payload = Payload(secondary)
Tao Baoc7b403a2018-01-30 18:19:04 -08001220 payload.Generate(target_file)
1221 return payload
1222
Tao Baof7140c02018-01-30 17:09:24 -08001223 @staticmethod
1224 def _create_payload_incremental():
1225 target_file = construct_target_files()
1226 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001227 payload = Payload()
1228 payload.Generate(target_file, source_file)
1229 return payload
1230
1231 def test_Generate_full(self):
1232 payload = self._create_payload_full()
1233 self.assertTrue(os.path.exists(payload.payload_file))
1234
1235 def test_Generate_incremental(self):
1236 payload = self._create_payload_incremental()
1237 self.assertTrue(os.path.exists(payload.payload_file))
1238
1239 def test_Generate_additionalArgs(self):
Tao Baof7140c02018-01-30 17:09:24 -08001240 target_file = construct_target_files()
1241 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001242 payload = Payload()
1243 # This should work the same as calling payload.Generate(target_file,
1244 # source_file).
1245 payload.Generate(
1246 target_file, additional_args=["--source_image", source_file])
1247 self.assertTrue(os.path.exists(payload.payload_file))
1248
1249 def test_Generate_invalidInput(self):
Tao Baof7140c02018-01-30 17:09:24 -08001250 target_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001251 common.ZipDelete(target_file, 'IMAGES/vendor.img')
1252 payload = Payload()
1253 self.assertRaises(AssertionError, payload.Generate, target_file)
1254
1255 def test_Sign_full(self):
1256 payload = self._create_payload_full()
1257 payload.Sign(PayloadSigner())
1258
1259 output_file = common.MakeTempFile(suffix='.zip')
1260 with zipfile.ZipFile(output_file, 'w') as output_zip:
1261 payload.WriteToZip(output_zip)
1262
1263 import check_ota_package_signature
1264 check_ota_package_signature.VerifyAbOtaPayload(
1265 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1266 output_file)
1267
1268 def test_Sign_incremental(self):
1269 payload = self._create_payload_incremental()
1270 payload.Sign(PayloadSigner())
1271
1272 output_file = common.MakeTempFile(suffix='.zip')
1273 with zipfile.ZipFile(output_file, 'w') as output_zip:
1274 payload.WriteToZip(output_zip)
1275
1276 import check_ota_package_signature
1277 check_ota_package_signature.VerifyAbOtaPayload(
1278 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1279 output_file)
1280
1281 def test_Sign_withDataWipe(self):
1282 common.OPTIONS.wipe_user_data = True
1283 payload = self._create_payload_full()
1284 payload.Sign(PayloadSigner())
1285
1286 with open(payload.payload_properties) as properties_fp:
1287 self.assertIn("POWERWASH=1", properties_fp.read())
1288
Tao Bao667ff572018-02-10 00:02:40 -08001289 def test_Sign_secondary(self):
1290 payload = self._create_payload_full(secondary=True)
1291 payload.Sign(PayloadSigner())
1292
1293 with open(payload.payload_properties) as properties_fp:
1294 self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read())
1295
Tao Baoc7b403a2018-01-30 18:19:04 -08001296 def test_Sign_badSigner(self):
1297 """Tests that signing failure can be captured."""
1298 payload = self._create_payload_full()
1299 payload_signer = PayloadSigner()
1300 payload_signer.signer_args.append('bad-option')
1301 self.assertRaises(AssertionError, payload.Sign, payload_signer)
1302
1303 def test_WriteToZip(self):
1304 payload = self._create_payload_full()
1305 payload.Sign(PayloadSigner())
1306
1307 output_file = common.MakeTempFile(suffix='.zip')
1308 with zipfile.ZipFile(output_file, 'w') as output_zip:
1309 payload.WriteToZip(output_zip)
1310
1311 with zipfile.ZipFile(output_file) as verify_zip:
1312 # First make sure we have the essential entries.
1313 namelist = verify_zip.namelist()
1314 self.assertIn(Payload.PAYLOAD_BIN, namelist)
1315 self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist)
1316
1317 # Then assert these entries are stored.
1318 for entry_info in verify_zip.infolist():
1319 if entry_info.filename not in (Payload.PAYLOAD_BIN,
1320 Payload.PAYLOAD_PROPERTIES_TXT):
1321 continue
1322 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)
1323
1324 def test_WriteToZip_unsignedPayload(self):
1325 """Unsigned payloads should not be allowed to be written to zip."""
1326 payload = self._create_payload_full()
1327
1328 output_file = common.MakeTempFile(suffix='.zip')
1329 with zipfile.ZipFile(output_file, 'w') as output_zip:
1330 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
1331
1332 # Also test with incremental payload.
1333 payload = self._create_payload_incremental()
1334
1335 output_file = common.MakeTempFile(suffix='.zip')
1336 with zipfile.ZipFile(output_file, 'w') as output_zip:
1337 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001338
1339 def test_WriteToZip_secondary(self):
1340 payload = self._create_payload_full(secondary=True)
1341 payload.Sign(PayloadSigner())
1342
1343 output_file = common.MakeTempFile(suffix='.zip')
1344 with zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Bao667ff572018-02-10 00:02:40 -08001345 payload.WriteToZip(output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001346
1347 with zipfile.ZipFile(output_file) as verify_zip:
1348 # First make sure we have the essential entries.
1349 namelist = verify_zip.namelist()
1350 self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist)
1351 self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist)
1352
1353 # Then assert these entries are stored.
1354 for entry_info in verify_zip.infolist():
1355 if entry_info.filename not in (
1356 Payload.SECONDARY_PAYLOAD_BIN,
1357 Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT):
1358 continue
1359 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)