blob: 0eb24b5f4b14ce122f0db0da5fe4ce0d75bfc31b [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:
Tao Bao69203522018-03-08 16:09:01 -0800777 # pylint: disable=protected-access
Tao Baoae5e4c32018-03-01 19:30:00 -0800778 raw_metadata = property_files._GetPropertyFilesString(
779 zip_fp, reserve_space=False)
780 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
Tao Bao69203522018-03-08 16:09:01 -0800781 tokens = self._parse_property_files_string(streaming_metadata)
Tao Baof5110492018-03-02 09:47:43 -0800782
783 self.assertEqual(3, len(tokens))
784 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
785 # streaming metadata.
786 entries[2] = 'metadata'
787 self._verify_entries(zip_file, tokens, entries)
788
Tao Baoae5e4c32018-03-01 19:30:00 -0800789 def test_Finalize_assertReservedLength(self):
Tao Baof5110492018-03-02 09:47:43 -0800790 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800791 'required-entry1',
792 'required-entry2',
793 'optional-entry1',
794 'optional-entry2',
Tao Baof5110492018-03-02 09:47:43 -0800795 'META-INF/com/android/metadata',
796 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700797 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800798 property_files = TestPropertyFiles()
Tao Baof5110492018-03-02 09:47:43 -0800799 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
800 # First get the raw metadata string (i.e. without padding space).
Tao Bao69203522018-03-08 16:09:01 -0800801 # pylint: disable=protected-access
Tao Baoae5e4c32018-03-01 19:30:00 -0800802 raw_metadata = property_files._GetPropertyFilesString(
803 zip_fp, reserve_space=False)
Tao Baof5110492018-03-02 09:47:43 -0800804 raw_length = len(raw_metadata)
805
806 # Now pass in the exact expected length.
Tao Baoae5e4c32018-03-01 19:30:00 -0800807 streaming_metadata = property_files.Finalize(zip_fp, raw_length)
Tao Baof5110492018-03-02 09:47:43 -0800808 self.assertEqual(raw_length, len(streaming_metadata))
809
810 # Or pass in insufficient length.
811 self.assertRaises(
Tao Bao3bf8c652018-03-16 12:59:42 -0700812 PropertyFiles.InsufficientSpaceException,
Tao Baoae5e4c32018-03-01 19:30:00 -0800813 property_files.Finalize,
Tao Baof5110492018-03-02 09:47:43 -0800814 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800815 raw_length - 1)
Tao Baof5110492018-03-02 09:47:43 -0800816
817 # Or pass in a much larger size.
Tao Baoae5e4c32018-03-01 19:30:00 -0800818 streaming_metadata = property_files.Finalize(
Tao Baof5110492018-03-02 09:47:43 -0800819 zip_fp,
Tao Baoae5e4c32018-03-01 19:30:00 -0800820 raw_length + 20)
Tao Baof5110492018-03-02 09:47:43 -0800821 self.assertEqual(raw_length + 20, len(streaming_metadata))
822 self.assertEqual(' ' * 20, streaming_metadata[raw_length:])
823
Tao Baoae5e4c32018-03-01 19:30:00 -0800824 def test_Verify(self):
825 entries = (
Tao Bao69203522018-03-08 16:09:01 -0800826 'required-entry1',
827 'required-entry2',
828 'optional-entry1',
829 'optional-entry2',
830 'META-INF/com/android/metadata',
831 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700832 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800833 property_files = TestPropertyFiles()
834 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
835 # First get the raw metadata string (i.e. without padding space).
836 # pylint: disable=protected-access
837 raw_metadata = property_files._GetPropertyFilesString(
838 zip_fp, reserve_space=False)
839
840 # Should pass the test if verification passes.
841 property_files.Verify(zip_fp, raw_metadata)
842
843 # Or raise on verification failure.
844 self.assertRaises(
845 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
846
847
848class StreamingPropertyFilesTest(PropertyFilesTest):
849 """Additional sanity checks specialized for StreamingPropertyFiles."""
850
851 def test_init(self):
852 property_files = StreamingPropertyFiles()
853 self.assertEqual('ota-streaming-property-files', property_files.name)
854 self.assertEqual(
855 (
856 'payload.bin',
857 'payload_properties.txt',
858 ),
859 property_files.required)
860 self.assertEqual(
861 (
862 'care_map.txt',
863 'compatibility.zip',
864 ),
865 property_files.optional)
866
867 def test_Compute(self):
868 entries = (
Tao Baoae5e4c32018-03-01 19:30:00 -0800869 'payload.bin',
870 'payload_properties.txt',
871 'care_map.txt',
Tao Bao69203522018-03-08 16:09:01 -0800872 'compatibility.zip',
873 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700874 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800875 property_files = StreamingPropertyFiles()
876 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
877 property_files_string = property_files.Compute(zip_fp)
878
879 tokens = self._parse_property_files_string(property_files_string)
880 self.assertEqual(5, len(tokens))
881 self._verify_entries(zip_file, tokens, entries)
882
883 def test_Finalize(self):
884 entries = [
885 'payload.bin',
886 'payload_properties.txt',
887 'care_map.txt',
888 'compatibility.zip',
889 'META-INF/com/android/metadata',
890 ]
Tao Bao3bf8c652018-03-16 12:59:42 -0700891 zip_file = self.construct_zip_package(entries)
Tao Bao69203522018-03-08 16:09:01 -0800892 property_files = StreamingPropertyFiles()
893 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
894 # pylint: disable=protected-access
895 raw_metadata = property_files._GetPropertyFilesString(
896 zip_fp, reserve_space=False)
897 streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata))
898 tokens = self._parse_property_files_string(streaming_metadata)
899
900 self.assertEqual(5, len(tokens))
901 # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the
902 # streaming metadata.
903 entries[4] = 'metadata'
904 self._verify_entries(zip_file, tokens, entries)
905
906 def test_Verify(self):
907 entries = (
908 'payload.bin',
909 'payload_properties.txt',
910 'care_map.txt',
911 'compatibility.zip',
Tao Baoae5e4c32018-03-01 19:30:00 -0800912 'META-INF/com/android/metadata',
913 )
Tao Bao3bf8c652018-03-16 12:59:42 -0700914 zip_file = self.construct_zip_package(entries)
Tao Baoae5e4c32018-03-01 19:30:00 -0800915 property_files = StreamingPropertyFiles()
916 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
917 # First get the raw metadata string (i.e. without padding space).
Tao Bao69203522018-03-08 16:09:01 -0800918 # pylint: disable=protected-access
Tao Baoae5e4c32018-03-01 19:30:00 -0800919 raw_metadata = property_files._GetPropertyFilesString(
920 zip_fp, reserve_space=False)
921
922 # Should pass the test if verification passes.
923 property_files.Verify(zip_fp, raw_metadata)
924
925 # Or raise on verification failure.
926 self.assertRaises(
927 AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x')
928
Tao Baofabe0832018-01-17 15:52:28 -0800929
Tao Baob6304672018-03-08 16:28:33 -0800930class AbOtaPropertyFilesTest(PropertyFilesTest):
931 """Additional sanity checks specialized for AbOtaPropertyFiles."""
932
933 # The size for payload and metadata signature size.
934 SIGNATURE_SIZE = 256
935
936 def setUp(self):
937 self.testdata_dir = test_utils.get_testdata_dir()
938 self.assertTrue(os.path.exists(self.testdata_dir))
939
940 common.OPTIONS.wipe_user_data = False
941 common.OPTIONS.payload_signer = None
942 common.OPTIONS.payload_signer_args = None
943 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
944 common.OPTIONS.key_passwords = {
945 common.OPTIONS.package_key : None,
946 }
947
948 def test_init(self):
949 property_files = AbOtaPropertyFiles()
950 self.assertEqual('ota-property-files', property_files.name)
951 self.assertEqual(
952 (
953 'payload.bin',
954 'payload_properties.txt',
955 ),
956 property_files.required)
957 self.assertEqual(
958 (
959 'care_map.txt',
960 'compatibility.zip',
961 ),
962 property_files.optional)
963
964 def test_GetPayloadMetadataOffsetAndSize(self):
965 target_file = construct_target_files()
966 payload = Payload()
967 payload.Generate(target_file)
968
969 payload_signer = PayloadSigner()
970 payload.Sign(payload_signer)
971
972 output_file = common.MakeTempFile(suffix='.zip')
973 with zipfile.ZipFile(output_file, 'w') as output_zip:
974 payload.WriteToZip(output_zip)
975
976 # Find out the payload metadata offset and size.
977 property_files = AbOtaPropertyFiles()
978 with zipfile.ZipFile(output_file) as input_zip:
979 # pylint: disable=protected-access
980 payload_offset, metadata_total = (
981 property_files._GetPayloadMetadataOffsetAndSize(input_zip))
982
983 # Read in the metadata signature directly.
984 with open(output_file, 'rb') as verify_fp:
985 verify_fp.seek(payload_offset + metadata_total - self.SIGNATURE_SIZE)
986 metadata_signature = verify_fp.read(self.SIGNATURE_SIZE)
987
988 # Now we extract the metadata hash via brillo_update_payload script, which
989 # will serve as the oracle result.
990 payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
991 metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin")
992 cmd = ['brillo_update_payload', 'hash',
993 '--unsigned_payload', payload.payload_file,
994 '--signature_size', str(self.SIGNATURE_SIZE),
995 '--metadata_hash_file', metadata_sig_file,
996 '--payload_hash_file', payload_sig_file]
997 proc = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
998 stdoutdata, _ = proc.communicate()
999 self.assertEqual(
1000 0, proc.returncode,
1001 'Failed to run brillo_update_payload: {}'.format(stdoutdata))
1002
1003 signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file)
1004
1005 # Finally we can compare the two signatures.
1006 with open(signed_metadata_sig_file, 'rb') as verify_fp:
1007 self.assertEqual(verify_fp.read(), metadata_signature)
1008
1009 @staticmethod
Tao Bao3bf8c652018-03-16 12:59:42 -07001010 def construct_zip_package_withValidPayload(with_metadata=False):
1011 # Cannot use construct_zip_package() since we need a "valid" payload.bin.
Tao Baob6304672018-03-08 16:28:33 -08001012 target_file = construct_target_files()
1013 payload = Payload()
1014 payload.Generate(target_file)
1015
1016 payload_signer = PayloadSigner()
1017 payload.Sign(payload_signer)
1018
1019 zip_file = common.MakeTempFile(suffix='.zip')
1020 with zipfile.ZipFile(zip_file, 'w') as zip_fp:
1021 # 'payload.bin',
1022 payload.WriteToZip(zip_fp)
1023
1024 # Other entries.
1025 entries = ['care_map.txt', 'compatibility.zip']
1026
1027 # Put META-INF/com/android/metadata if needed.
1028 if with_metadata:
1029 entries.append('META-INF/com/android/metadata')
1030
1031 for entry in entries:
1032 zip_fp.writestr(
1033 entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED)
1034
1035 return zip_file
1036
1037 def test_Compute(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001038 zip_file = self.construct_zip_package_withValidPayload()
Tao Baob6304672018-03-08 16:28:33 -08001039 property_files = AbOtaPropertyFiles()
1040 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1041 property_files_string = property_files.Compute(zip_fp)
1042
1043 tokens = self._parse_property_files_string(property_files_string)
1044 # "6" indcludes the four entries above, one metadata entry, and one entry
1045 # for payload-metadata.bin.
1046 self.assertEqual(6, len(tokens))
1047 self._verify_entries(
1048 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1049
1050 def test_Finalize(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001051 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001052 property_files = AbOtaPropertyFiles()
1053 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1054 # pylint: disable=protected-access
1055 raw_metadata = property_files._GetPropertyFilesString(
1056 zip_fp, reserve_space=False)
1057 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1058
1059 tokens = self._parse_property_files_string(property_files_string)
1060 # "6" indcludes the four entries above, one metadata entry, and one entry
1061 # for payload-metadata.bin.
1062 self.assertEqual(6, len(tokens))
1063 self._verify_entries(
1064 zip_file, tokens, ('care_map.txt', 'compatibility.zip'))
1065
1066 def test_Verify(self):
Tao Bao3bf8c652018-03-16 12:59:42 -07001067 zip_file = self.construct_zip_package_withValidPayload(with_metadata=True)
Tao Baob6304672018-03-08 16:28:33 -08001068 property_files = AbOtaPropertyFiles()
1069 with zipfile.ZipFile(zip_file, 'r') as zip_fp:
1070 # pylint: disable=protected-access
1071 raw_metadata = property_files._GetPropertyFilesString(
1072 zip_fp, reserve_space=False)
1073
1074 property_files.Verify(zip_fp, raw_metadata)
1075
1076
Tao Baoc0746f42018-02-21 13:17:22 -08001077class NonAbOtaPropertyFilesTest(PropertyFilesTest):
1078 """Additional sanity checks specialized for NonAbOtaPropertyFiles."""
1079
1080 def test_init(self):
1081 property_files = NonAbOtaPropertyFiles()
1082 self.assertEqual('ota-property-files', property_files.name)
1083 self.assertEqual((), property_files.required)
1084 self.assertEqual((), property_files.optional)
1085
1086 def test_Compute(self):
1087 entries = ()
Tao Bao3bf8c652018-03-16 12:59:42 -07001088 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001089 property_files = NonAbOtaPropertyFiles()
1090 with zipfile.ZipFile(zip_file) as zip_fp:
1091 property_files_string = property_files.Compute(zip_fp)
1092
1093 tokens = self._parse_property_files_string(property_files_string)
1094 self.assertEqual(1, len(tokens))
1095 self._verify_entries(zip_file, tokens, entries)
1096
1097 def test_Finalize(self):
1098 entries = [
1099 'META-INF/com/android/metadata',
1100 ]
Tao Bao3bf8c652018-03-16 12:59:42 -07001101 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001102 property_files = NonAbOtaPropertyFiles()
1103 with zipfile.ZipFile(zip_file) as zip_fp:
1104 # pylint: disable=protected-access
1105 raw_metadata = property_files._GetPropertyFilesString(
1106 zip_fp, reserve_space=False)
1107 property_files_string = property_files.Finalize(zip_fp, len(raw_metadata))
1108 tokens = self._parse_property_files_string(property_files_string)
1109
1110 self.assertEqual(1, len(tokens))
1111 # 'META-INF/com/android/metadata' will be key'd as 'metadata'.
1112 entries[0] = 'metadata'
1113 self._verify_entries(zip_file, tokens, entries)
1114
1115 def test_Verify(self):
1116 entries = (
1117 'META-INF/com/android/metadata',
1118 )
Tao Bao3bf8c652018-03-16 12:59:42 -07001119 zip_file = self.construct_zip_package(entries)
Tao Baoc0746f42018-02-21 13:17:22 -08001120 property_files = NonAbOtaPropertyFiles()
1121 with zipfile.ZipFile(zip_file) as zip_fp:
1122 # pylint: disable=protected-access
1123 raw_metadata = property_files._GetPropertyFilesString(
1124 zip_fp, reserve_space=False)
1125
1126 property_files.Verify(zip_fp, raw_metadata)
1127
1128
Tao Baofabe0832018-01-17 15:52:28 -08001129class PayloadSignerTest(unittest.TestCase):
1130
1131 SIGFILE = 'sigfile.bin'
1132 SIGNED_SIGFILE = 'signed-sigfile.bin'
1133
1134 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001135 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baofabe0832018-01-17 15:52:28 -08001136 self.assertTrue(os.path.exists(self.testdata_dir))
1137
1138 common.OPTIONS.payload_signer = None
1139 common.OPTIONS.payload_signer_args = []
1140 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1141 common.OPTIONS.key_passwords = {
1142 common.OPTIONS.package_key : None,
1143 }
1144
1145 def tearDown(self):
1146 common.Cleanup()
1147
1148 def _assertFilesEqual(self, file1, file2):
1149 with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
1150 self.assertEqual(fp1.read(), fp2.read())
1151
1152 def test_init(self):
1153 payload_signer = PayloadSigner()
1154 self.assertEqual('openssl', payload_signer.signer)
1155
1156 def test_init_withPassword(self):
1157 common.OPTIONS.package_key = os.path.join(
1158 self.testdata_dir, 'testkey_with_passwd')
1159 common.OPTIONS.key_passwords = {
1160 common.OPTIONS.package_key : 'foo',
1161 }
1162 payload_signer = PayloadSigner()
1163 self.assertEqual('openssl', payload_signer.signer)
1164
1165 def test_init_withExternalSigner(self):
1166 common.OPTIONS.payload_signer = 'abc'
1167 common.OPTIONS.payload_signer_args = ['arg1', 'arg2']
1168 payload_signer = PayloadSigner()
1169 self.assertEqual('abc', payload_signer.signer)
1170 self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args)
1171
1172 def test_Sign(self):
1173 payload_signer = PayloadSigner()
1174 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1175 signed_file = payload_signer.Sign(input_file)
1176
1177 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1178 self._assertFilesEqual(verify_file, signed_file)
1179
1180 def test_Sign_withExternalSigner_openssl(self):
1181 """Uses openssl as the external payload signer."""
1182 common.OPTIONS.payload_signer = 'openssl'
1183 common.OPTIONS.payload_signer_args = [
1184 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey',
1185 os.path.join(self.testdata_dir, 'testkey.pk8'),
1186 '-pkeyopt', 'digest:sha256']
1187 payload_signer = PayloadSigner()
1188 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1189 signed_file = payload_signer.Sign(input_file)
1190
1191 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1192 self._assertFilesEqual(verify_file, signed_file)
1193
1194 def test_Sign_withExternalSigner_script(self):
1195 """Uses testdata/payload_signer.sh as the external payload signer."""
1196 common.OPTIONS.payload_signer = os.path.join(
1197 self.testdata_dir, 'payload_signer.sh')
1198 common.OPTIONS.payload_signer_args = [
1199 os.path.join(self.testdata_dir, 'testkey.pk8')]
1200 payload_signer = PayloadSigner()
1201 input_file = os.path.join(self.testdata_dir, self.SIGFILE)
1202 signed_file = payload_signer.Sign(input_file)
1203
1204 verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE)
1205 self._assertFilesEqual(verify_file, signed_file)
Tao Baoc7b403a2018-01-30 18:19:04 -08001206
1207
1208class PayloadTest(unittest.TestCase):
1209
1210 def setUp(self):
Tao Bao04e1f012018-02-04 12:13:35 -08001211 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoc7b403a2018-01-30 18:19:04 -08001212 self.assertTrue(os.path.exists(self.testdata_dir))
1213
1214 common.OPTIONS.wipe_user_data = False
1215 common.OPTIONS.payload_signer = None
1216 common.OPTIONS.payload_signer_args = None
1217 common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey')
1218 common.OPTIONS.key_passwords = {
1219 common.OPTIONS.package_key : None,
1220 }
1221
1222 def tearDown(self):
1223 common.Cleanup()
1224
1225 @staticmethod
Tao Baof7140c02018-01-30 17:09:24 -08001226 def _create_payload_full(secondary=False):
1227 target_file = construct_target_files(secondary)
Tao Bao667ff572018-02-10 00:02:40 -08001228 payload = Payload(secondary)
Tao Baoc7b403a2018-01-30 18:19:04 -08001229 payload.Generate(target_file)
1230 return payload
1231
Tao Baof7140c02018-01-30 17:09:24 -08001232 @staticmethod
1233 def _create_payload_incremental():
1234 target_file = construct_target_files()
1235 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001236 payload = Payload()
1237 payload.Generate(target_file, source_file)
1238 return payload
1239
1240 def test_Generate_full(self):
1241 payload = self._create_payload_full()
1242 self.assertTrue(os.path.exists(payload.payload_file))
1243
1244 def test_Generate_incremental(self):
1245 payload = self._create_payload_incremental()
1246 self.assertTrue(os.path.exists(payload.payload_file))
1247
1248 def test_Generate_additionalArgs(self):
Tao Baof7140c02018-01-30 17:09:24 -08001249 target_file = construct_target_files()
1250 source_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001251 payload = Payload()
1252 # This should work the same as calling payload.Generate(target_file,
1253 # source_file).
1254 payload.Generate(
1255 target_file, additional_args=["--source_image", source_file])
1256 self.assertTrue(os.path.exists(payload.payload_file))
1257
1258 def test_Generate_invalidInput(self):
Tao Baof7140c02018-01-30 17:09:24 -08001259 target_file = construct_target_files()
Tao Baoc7b403a2018-01-30 18:19:04 -08001260 common.ZipDelete(target_file, 'IMAGES/vendor.img')
1261 payload = Payload()
1262 self.assertRaises(AssertionError, payload.Generate, target_file)
1263
1264 def test_Sign_full(self):
1265 payload = self._create_payload_full()
1266 payload.Sign(PayloadSigner())
1267
1268 output_file = common.MakeTempFile(suffix='.zip')
1269 with zipfile.ZipFile(output_file, 'w') as output_zip:
1270 payload.WriteToZip(output_zip)
1271
1272 import check_ota_package_signature
1273 check_ota_package_signature.VerifyAbOtaPayload(
1274 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1275 output_file)
1276
1277 def test_Sign_incremental(self):
1278 payload = self._create_payload_incremental()
1279 payload.Sign(PayloadSigner())
1280
1281 output_file = common.MakeTempFile(suffix='.zip')
1282 with zipfile.ZipFile(output_file, 'w') as output_zip:
1283 payload.WriteToZip(output_zip)
1284
1285 import check_ota_package_signature
1286 check_ota_package_signature.VerifyAbOtaPayload(
1287 os.path.join(self.testdata_dir, 'testkey.x509.pem'),
1288 output_file)
1289
1290 def test_Sign_withDataWipe(self):
1291 common.OPTIONS.wipe_user_data = True
1292 payload = self._create_payload_full()
1293 payload.Sign(PayloadSigner())
1294
1295 with open(payload.payload_properties) as properties_fp:
1296 self.assertIn("POWERWASH=1", properties_fp.read())
1297
Tao Bao667ff572018-02-10 00:02:40 -08001298 def test_Sign_secondary(self):
1299 payload = self._create_payload_full(secondary=True)
1300 payload.Sign(PayloadSigner())
1301
1302 with open(payload.payload_properties) as properties_fp:
1303 self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read())
1304
Tao Baoc7b403a2018-01-30 18:19:04 -08001305 def test_Sign_badSigner(self):
1306 """Tests that signing failure can be captured."""
1307 payload = self._create_payload_full()
1308 payload_signer = PayloadSigner()
1309 payload_signer.signer_args.append('bad-option')
1310 self.assertRaises(AssertionError, payload.Sign, payload_signer)
1311
1312 def test_WriteToZip(self):
1313 payload = self._create_payload_full()
1314 payload.Sign(PayloadSigner())
1315
1316 output_file = common.MakeTempFile(suffix='.zip')
1317 with zipfile.ZipFile(output_file, 'w') as output_zip:
1318 payload.WriteToZip(output_zip)
1319
1320 with zipfile.ZipFile(output_file) as verify_zip:
1321 # First make sure we have the essential entries.
1322 namelist = verify_zip.namelist()
1323 self.assertIn(Payload.PAYLOAD_BIN, namelist)
1324 self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist)
1325
1326 # Then assert these entries are stored.
1327 for entry_info in verify_zip.infolist():
1328 if entry_info.filename not in (Payload.PAYLOAD_BIN,
1329 Payload.PAYLOAD_PROPERTIES_TXT):
1330 continue
1331 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)
1332
1333 def test_WriteToZip_unsignedPayload(self):
1334 """Unsigned payloads should not be allowed to be written to zip."""
1335 payload = self._create_payload_full()
1336
1337 output_file = common.MakeTempFile(suffix='.zip')
1338 with zipfile.ZipFile(output_file, 'w') as output_zip:
1339 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
1340
1341 # Also test with incremental payload.
1342 payload = self._create_payload_incremental()
1343
1344 output_file = common.MakeTempFile(suffix='.zip')
1345 with zipfile.ZipFile(output_file, 'w') as output_zip:
1346 self.assertRaises(AssertionError, payload.WriteToZip, output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001347
1348 def test_WriteToZip_secondary(self):
1349 payload = self._create_payload_full(secondary=True)
1350 payload.Sign(PayloadSigner())
1351
1352 output_file = common.MakeTempFile(suffix='.zip')
1353 with zipfile.ZipFile(output_file, 'w') as output_zip:
Tao Bao667ff572018-02-10 00:02:40 -08001354 payload.WriteToZip(output_zip)
Tao Baof7140c02018-01-30 17:09:24 -08001355
1356 with zipfile.ZipFile(output_file) as verify_zip:
1357 # First make sure we have the essential entries.
1358 namelist = verify_zip.namelist()
1359 self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist)
1360 self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist)
1361
1362 # Then assert these entries are stored.
1363 for entry_info in verify_zip.infolist():
1364 if entry_info.filename not in (
1365 Payload.SECONDARY_PAYLOAD_BIN,
1366 Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT):
1367 continue
1368 self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type)