Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1 | # |
| 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 | |
| 17 | import copy |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 18 | import os |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 19 | import os.path |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 20 | import zipfile |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 21 | |
| 22 | import common |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 23 | import test_utils |
Kelvin Zhang | cff4d76 | 2020-07-29 16:37:51 -0400 | [diff] [blame^] | 24 | from ota_utils import CalculateRuntimeDevicesAndFingerprints |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 25 | from ota_from_target_files import ( |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 26 | _LoadOemDicts, AbOtaPropertyFiles, FinalizeMetadata, |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 27 | GetPackageMetadata, GetTargetFilesZipForSecondaryImages, |
Kelvin Zhang | cff4d76 | 2020-07-29 16:37:51 -0400 | [diff] [blame^] | 28 | GetTargetFilesZipWithoutPostinstallConfig, |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 29 | Payload, PayloadSigner, POSTINSTALL_CONFIG, PropertyFiles, |
Kelvin Zhang | cff4d76 | 2020-07-29 16:37:51 -0400 | [diff] [blame^] | 30 | StreamingPropertyFiles) |
| 31 | from non_ab_ota import NonAbOtaPropertyFiles |
| 32 | from test_utils import PropertyFilesTestCase |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 33 | |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 34 | def 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 Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 43 | # 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 Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 53 | ab_partitions = [ |
| 54 | ('IMAGES', 'boot'), |
| 55 | ('IMAGES', 'system'), |
| 56 | ('IMAGES', 'vendor'), |
| 57 | ('RADIO', 'bootloader'), |
| 58 | ('RADIO', 'modem'), |
| 59 | ] |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 60 | # META/ab_partitions.txt |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 61 | target_files_zip.writestr( |
| 62 | 'META/ab_partitions.txt', |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 63 | '\n'.join([partition[1] for partition in ab_partitions])) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 64 | |
Kelvin Zhang | c693d95 | 2020-07-22 19:21:22 -0400 | [diff] [blame] | 65 | # Create fake images for each of them. |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 66 | for path, partition in ab_partitions: |
| 67 | target_files_zip.writestr( |
| 68 | '{}/{}.img'.format(path, partition), |
| 69 | os.urandom(len(partition))) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 70 | |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 71 | # system_other shouldn't appear in META/ab_partitions.txt. |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 72 | 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 Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 79 | class LoadOemDictsTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 80 | |
| 81 | def test_NoneDict(self): |
| 82 | self.assertIsNone(_LoadOemDicts(None)) |
| 83 | |
| 84 | def test_SingleDict(self): |
| 85 | dict_file = common.MakeTempFile() |
| 86 | with open(dict_file, 'w') as dict_fp: |
| 87 | dict_fp.write('abc=1\ndef=2\nxyz=foo\na.b.c=bar\n') |
| 88 | |
| 89 | oem_dicts = _LoadOemDicts([dict_file]) |
| 90 | self.assertEqual(1, len(oem_dicts)) |
| 91 | self.assertEqual('foo', oem_dicts[0]['xyz']) |
| 92 | self.assertEqual('bar', oem_dicts[0]['a.b.c']) |
| 93 | |
| 94 | def test_MultipleDicts(self): |
| 95 | oem_source = [] |
| 96 | for i in range(3): |
| 97 | dict_file = common.MakeTempFile() |
| 98 | with open(dict_file, 'w') as dict_fp: |
| 99 | dict_fp.write( |
| 100 | 'ro.build.index={}\ndef=2\nxyz=foo\na.b.c=bar\n'.format(i)) |
| 101 | oem_source.append(dict_file) |
| 102 | |
| 103 | oem_dicts = _LoadOemDicts(oem_source) |
| 104 | self.assertEqual(3, len(oem_dicts)) |
| 105 | for i, oem_dict in enumerate(oem_dicts): |
| 106 | self.assertEqual('2', oem_dict['def']) |
| 107 | self.assertEqual('foo', oem_dict['xyz']) |
| 108 | self.assertEqual('bar', oem_dict['a.b.c']) |
| 109 | self.assertEqual('{}'.format(i), oem_dict['ro.build.index']) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 110 | |
| 111 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 112 | class OtaFromTargetFilesTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 113 | TEST_TARGET_INFO_DICT = { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 114 | 'build.prop': common.PartitionBuildProps.FromDictionary( |
| 115 | 'system', { |
| 116 | 'ro.product.device': 'product-device', |
| 117 | 'ro.build.fingerprint': 'build-fingerprint-target', |
| 118 | 'ro.build.version.incremental': 'build-version-incremental-target', |
| 119 | 'ro.build.version.sdk': '27', |
| 120 | 'ro.build.version.security_patch': '2017-12-01', |
| 121 | 'ro.build.date.utc': '1500000000'} |
| 122 | ) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST_SOURCE_INFO_DICT = { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 126 | 'build.prop': common.PartitionBuildProps.FromDictionary( |
| 127 | 'system', { |
| 128 | 'ro.product.device': 'product-device', |
| 129 | 'ro.build.fingerprint': 'build-fingerprint-source', |
| 130 | 'ro.build.version.incremental': 'build-version-incremental-source', |
| 131 | 'ro.build.version.sdk': '25', |
| 132 | 'ro.build.version.security_patch': '2016-12-01', |
| 133 | 'ro.build.date.utc': '1400000000'} |
| 134 | ) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 137 | TEST_INFO_DICT_USES_OEM_PROPS = { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 138 | 'build.prop': common.PartitionBuildProps.FromDictionary( |
| 139 | 'system', { |
| 140 | 'ro.product.name': 'product-name', |
| 141 | 'ro.build.thumbprint': 'build-thumbprint', |
| 142 | 'ro.build.bar': 'build-bar'} |
| 143 | ), |
| 144 | 'vendor.build.prop': common.PartitionBuildProps.FromDictionary( |
| 145 | 'vendor', { |
| 146 | 'ro.vendor.build.fingerprint': 'vendor-build-fingerprint'} |
| 147 | ), |
| 148 | 'property1': 'value1', |
| 149 | 'property2': 4096, |
| 150 | 'oem_fingerprint_properties': 'ro.product.device ro.product.brand', |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 153 | |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 154 | def setUp(self): |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 155 | self.testdata_dir = test_utils.get_testdata_dir() |
| 156 | self.assertTrue(os.path.exists(self.testdata_dir)) |
| 157 | |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 158 | # Reset the global options as in ota_from_target_files.py. |
| 159 | common.OPTIONS.incremental_source = None |
| 160 | common.OPTIONS.downgrade = False |
Tao Bao | 393eeb4 | 2019-03-06 16:00:38 -0800 | [diff] [blame] | 161 | common.OPTIONS.retrofit_dynamic_partitions = False |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 162 | common.OPTIONS.timestamp = False |
| 163 | common.OPTIONS.wipe_user_data = False |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 164 | common.OPTIONS.no_signing = False |
| 165 | common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey') |
| 166 | common.OPTIONS.key_passwords = { |
| 167 | common.OPTIONS.package_key : None, |
| 168 | } |
| 169 | |
| 170 | common.OPTIONS.search_path = test_utils.get_search_path() |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 171 | |
| 172 | def test_GetPackageMetadata_abOta_full(self): |
| 173 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 174 | target_info_dict['ab_update'] = 'true' |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 175 | target_info = common.BuildInfo(target_info_dict, None) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 176 | metadata = GetPackageMetadata(target_info) |
| 177 | self.assertDictEqual( |
| 178 | { |
| 179 | 'ota-type' : 'AB', |
| 180 | 'ota-required-cache' : '0', |
| 181 | 'post-build' : 'build-fingerprint-target', |
| 182 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 183 | 'post-sdk-level' : '27', |
| 184 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 185 | 'post-timestamp' : '1500000000', |
| 186 | 'pre-device' : 'product-device', |
| 187 | }, |
| 188 | metadata) |
| 189 | |
| 190 | def test_GetPackageMetadata_abOta_incremental(self): |
| 191 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 192 | target_info_dict['ab_update'] = 'true' |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 193 | target_info = common.BuildInfo(target_info_dict, None) |
| 194 | source_info = common.BuildInfo(self.TEST_SOURCE_INFO_DICT, None) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 195 | common.OPTIONS.incremental_source = '' |
| 196 | metadata = GetPackageMetadata(target_info, source_info) |
| 197 | self.assertDictEqual( |
| 198 | { |
| 199 | 'ota-type' : 'AB', |
| 200 | 'ota-required-cache' : '0', |
| 201 | 'post-build' : 'build-fingerprint-target', |
| 202 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 203 | 'post-sdk-level' : '27', |
| 204 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 205 | 'post-timestamp' : '1500000000', |
| 206 | 'pre-device' : 'product-device', |
| 207 | 'pre-build' : 'build-fingerprint-source', |
| 208 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 209 | }, |
| 210 | metadata) |
| 211 | |
| 212 | def test_GetPackageMetadata_nonAbOta_full(self): |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 213 | target_info = common.BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 214 | metadata = GetPackageMetadata(target_info) |
| 215 | self.assertDictEqual( |
| 216 | { |
| 217 | 'ota-type' : 'BLOCK', |
| 218 | 'post-build' : 'build-fingerprint-target', |
| 219 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 220 | 'post-sdk-level' : '27', |
| 221 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 222 | 'post-timestamp' : '1500000000', |
| 223 | 'pre-device' : 'product-device', |
| 224 | }, |
| 225 | metadata) |
| 226 | |
| 227 | def test_GetPackageMetadata_nonAbOta_incremental(self): |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 228 | target_info = common.BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
| 229 | source_info = common.BuildInfo(self.TEST_SOURCE_INFO_DICT, None) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 230 | common.OPTIONS.incremental_source = '' |
| 231 | metadata = GetPackageMetadata(target_info, source_info) |
| 232 | self.assertDictEqual( |
| 233 | { |
| 234 | 'ota-type' : 'BLOCK', |
| 235 | 'post-build' : 'build-fingerprint-target', |
| 236 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 237 | 'post-sdk-level' : '27', |
| 238 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 239 | 'post-timestamp' : '1500000000', |
| 240 | 'pre-device' : 'product-device', |
| 241 | 'pre-build' : 'build-fingerprint-source', |
| 242 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 243 | }, |
| 244 | metadata) |
| 245 | |
| 246 | def test_GetPackageMetadata_wipe(self): |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 247 | target_info = common.BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 248 | common.OPTIONS.wipe_user_data = True |
| 249 | metadata = GetPackageMetadata(target_info) |
| 250 | self.assertDictEqual( |
| 251 | { |
| 252 | 'ota-type' : 'BLOCK', |
| 253 | 'ota-wipe' : 'yes', |
| 254 | 'post-build' : 'build-fingerprint-target', |
| 255 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 256 | 'post-sdk-level' : '27', |
| 257 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 258 | 'post-timestamp' : '1500000000', |
| 259 | 'pre-device' : 'product-device', |
| 260 | }, |
| 261 | metadata) |
| 262 | |
Tao Bao | 393eeb4 | 2019-03-06 16:00:38 -0800 | [diff] [blame] | 263 | def test_GetPackageMetadata_retrofitDynamicPartitions(self): |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 264 | target_info = common.BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
Tao Bao | 393eeb4 | 2019-03-06 16:00:38 -0800 | [diff] [blame] | 265 | common.OPTIONS.retrofit_dynamic_partitions = True |
| 266 | metadata = GetPackageMetadata(target_info) |
| 267 | self.assertDictEqual( |
| 268 | { |
| 269 | 'ota-retrofit-dynamic-partitions' : 'yes', |
| 270 | 'ota-type' : 'BLOCK', |
| 271 | 'post-build' : 'build-fingerprint-target', |
| 272 | 'post-build-incremental' : 'build-version-incremental-target', |
| 273 | 'post-sdk-level' : '27', |
| 274 | 'post-security-patch-level' : '2017-12-01', |
| 275 | 'post-timestamp' : '1500000000', |
| 276 | 'pre-device' : 'product-device', |
| 277 | }, |
| 278 | metadata) |
| 279 | |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 280 | @staticmethod |
| 281 | def _test_GetPackageMetadata_swapBuildTimestamps(target_info, source_info): |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 282 | (target_info['build.prop'].build_props['ro.build.date.utc'], |
| 283 | source_info['build.prop'].build_props['ro.build.date.utc']) = ( |
| 284 | source_info['build.prop'].build_props['ro.build.date.utc'], |
| 285 | target_info['build.prop'].build_props['ro.build.date.utc']) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 286 | |
| 287 | def test_GetPackageMetadata_unintentionalDowngradeDetected(self): |
| 288 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 289 | source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT) |
| 290 | self._test_GetPackageMetadata_swapBuildTimestamps( |
| 291 | target_info_dict, source_info_dict) |
| 292 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 293 | target_info = common.BuildInfo(target_info_dict, None) |
| 294 | source_info = common.BuildInfo(source_info_dict, None) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 295 | common.OPTIONS.incremental_source = '' |
| 296 | self.assertRaises(RuntimeError, GetPackageMetadata, target_info, |
| 297 | source_info) |
| 298 | |
| 299 | def test_GetPackageMetadata_downgrade(self): |
| 300 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 301 | source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT) |
| 302 | self._test_GetPackageMetadata_swapBuildTimestamps( |
| 303 | target_info_dict, source_info_dict) |
| 304 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 305 | target_info = common.BuildInfo(target_info_dict, None) |
| 306 | source_info = common.BuildInfo(source_info_dict, None) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 307 | common.OPTIONS.incremental_source = '' |
| 308 | common.OPTIONS.downgrade = True |
| 309 | common.OPTIONS.wipe_user_data = True |
| 310 | metadata = GetPackageMetadata(target_info, source_info) |
| 311 | self.assertDictEqual( |
| 312 | { |
| 313 | 'ota-downgrade' : 'yes', |
| 314 | 'ota-type' : 'BLOCK', |
| 315 | 'ota-wipe' : 'yes', |
| 316 | 'post-build' : 'build-fingerprint-target', |
| 317 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 318 | 'post-sdk-level' : '27', |
| 319 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | faa8e0b | 2018-04-12 14:31:43 -0700 | [diff] [blame] | 320 | 'post-timestamp' : '1400000000', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 321 | 'pre-device' : 'product-device', |
| 322 | 'pre-build' : 'build-fingerprint-source', |
| 323 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 324 | }, |
| 325 | metadata) |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 326 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 327 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 328 | def test_GetTargetFilesZipForSecondaryImages(self): |
| 329 | input_file = construct_target_files(secondary=True) |
| 330 | target_file = GetTargetFilesZipForSecondaryImages(input_file) |
| 331 | |
| 332 | with zipfile.ZipFile(target_file) as verify_zip: |
| 333 | namelist = verify_zip.namelist() |
Tao Bao | 615b65d | 2019-10-06 22:59:45 -0700 | [diff] [blame] | 334 | ab_partitions = verify_zip.read('META/ab_partitions.txt').decode() |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 335 | |
| 336 | self.assertIn('META/ab_partitions.txt', namelist) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 337 | self.assertIn('IMAGES/system.img', namelist) |
Tao Bao | 1248980 | 2018-07-12 14:47:38 -0700 | [diff] [blame] | 338 | self.assertIn('RADIO/bootloader.img', namelist) |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 339 | self.assertIn(POSTINSTALL_CONFIG, namelist) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 340 | |
Tao Bao | 3e75946 | 2019-09-17 22:43:11 -0700 | [diff] [blame] | 341 | self.assertNotIn('IMAGES/boot.img', namelist) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 342 | self.assertNotIn('IMAGES/system_other.img', namelist) |
| 343 | self.assertNotIn('IMAGES/system.map', namelist) |
Tao Bao | 3e75946 | 2019-09-17 22:43:11 -0700 | [diff] [blame] | 344 | self.assertNotIn('RADIO/modem.img', namelist) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 345 | |
Tao Bao | 3e75946 | 2019-09-17 22:43:11 -0700 | [diff] [blame] | 346 | expected_ab_partitions = ['system', 'bootloader'] |
Tianjie Xu | 1c80800 | 2019-09-11 00:29:26 -0700 | [diff] [blame] | 347 | self.assertEqual('\n'.join(expected_ab_partitions), ab_partitions) |
| 348 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 349 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 350 | def test_GetTargetFilesZipForSecondaryImages_skipPostinstall(self): |
| 351 | input_file = construct_target_files(secondary=True) |
| 352 | target_file = GetTargetFilesZipForSecondaryImages( |
| 353 | input_file, skip_postinstall=True) |
| 354 | |
| 355 | with zipfile.ZipFile(target_file) as verify_zip: |
| 356 | namelist = verify_zip.namelist() |
| 357 | |
| 358 | self.assertIn('META/ab_partitions.txt', namelist) |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 359 | self.assertIn('IMAGES/system.img', namelist) |
Tao Bao | 1248980 | 2018-07-12 14:47:38 -0700 | [diff] [blame] | 360 | self.assertIn('RADIO/bootloader.img', namelist) |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 361 | |
Tao Bao | 3e75946 | 2019-09-17 22:43:11 -0700 | [diff] [blame] | 362 | self.assertNotIn('IMAGES/boot.img', namelist) |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 363 | self.assertNotIn('IMAGES/system_other.img', namelist) |
| 364 | self.assertNotIn('IMAGES/system.map', namelist) |
Tao Bao | 3e75946 | 2019-09-17 22:43:11 -0700 | [diff] [blame] | 365 | self.assertNotIn('RADIO/modem.img', namelist) |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 366 | self.assertNotIn(POSTINSTALL_CONFIG, namelist) |
| 367 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 368 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 1248980 | 2018-07-12 14:47:38 -0700 | [diff] [blame] | 369 | def test_GetTargetFilesZipForSecondaryImages_withoutRadioImages(self): |
| 370 | input_file = construct_target_files(secondary=True) |
| 371 | common.ZipDelete(input_file, 'RADIO/bootloader.img') |
| 372 | common.ZipDelete(input_file, 'RADIO/modem.img') |
| 373 | target_file = GetTargetFilesZipForSecondaryImages(input_file) |
| 374 | |
| 375 | with zipfile.ZipFile(target_file) as verify_zip: |
| 376 | namelist = verify_zip.namelist() |
| 377 | |
| 378 | self.assertIn('META/ab_partitions.txt', namelist) |
Tao Bao | 1248980 | 2018-07-12 14:47:38 -0700 | [diff] [blame] | 379 | self.assertIn('IMAGES/system.img', namelist) |
Tao Bao | 1248980 | 2018-07-12 14:47:38 -0700 | [diff] [blame] | 380 | self.assertIn(POSTINSTALL_CONFIG, namelist) |
| 381 | |
Tao Bao | 3e75946 | 2019-09-17 22:43:11 -0700 | [diff] [blame] | 382 | self.assertNotIn('IMAGES/boot.img', namelist) |
Tao Bao | 1248980 | 2018-07-12 14:47:38 -0700 | [diff] [blame] | 383 | self.assertNotIn('IMAGES/system_other.img', namelist) |
| 384 | self.assertNotIn('IMAGES/system.map', namelist) |
| 385 | self.assertNotIn('RADIO/bootloader.img', namelist) |
| 386 | self.assertNotIn('RADIO/modem.img', namelist) |
| 387 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 388 | @test_utils.SkipIfExternalToolsUnavailable() |
Tianjie Xu | 1c80800 | 2019-09-11 00:29:26 -0700 | [diff] [blame] | 389 | def test_GetTargetFilesZipForSecondaryImages_dynamicPartitions(self): |
| 390 | input_file = construct_target_files(secondary=True) |
| 391 | misc_info = '\n'.join([ |
| 392 | 'use_dynamic_partition_size=true', |
| 393 | 'use_dynamic_partitions=true', |
| 394 | 'dynamic_partition_list=system vendor product', |
| 395 | 'super_partition_groups=google_dynamic_partitions', |
| 396 | 'super_google_dynamic_partitions_group_size=4873781248', |
| 397 | 'super_google_dynamic_partitions_partition_list=system vendor product', |
| 398 | ]) |
| 399 | dynamic_partitions_info = '\n'.join([ |
| 400 | 'super_partition_groups=google_dynamic_partitions', |
| 401 | 'super_google_dynamic_partitions_group_size=4873781248', |
| 402 | 'super_google_dynamic_partitions_partition_list=system vendor product', |
| 403 | ]) |
| 404 | |
| 405 | with zipfile.ZipFile(input_file, 'a') as append_zip: |
| 406 | common.ZipWriteStr(append_zip, 'META/misc_info.txt', misc_info) |
| 407 | common.ZipWriteStr(append_zip, 'META/dynamic_partitions_info.txt', |
| 408 | dynamic_partitions_info) |
| 409 | |
| 410 | target_file = GetTargetFilesZipForSecondaryImages(input_file) |
| 411 | |
| 412 | with zipfile.ZipFile(target_file) as verify_zip: |
| 413 | namelist = verify_zip.namelist() |
Tao Bao | 615b65d | 2019-10-06 22:59:45 -0700 | [diff] [blame] | 414 | updated_misc_info = verify_zip.read('META/misc_info.txt').decode() |
Tianjie Xu | 1c80800 | 2019-09-11 00:29:26 -0700 | [diff] [blame] | 415 | updated_dynamic_partitions_info = verify_zip.read( |
Tao Bao | 615b65d | 2019-10-06 22:59:45 -0700 | [diff] [blame] | 416 | 'META/dynamic_partitions_info.txt').decode() |
Tianjie Xu | 1c80800 | 2019-09-11 00:29:26 -0700 | [diff] [blame] | 417 | |
| 418 | self.assertIn('META/ab_partitions.txt', namelist) |
Tianjie Xu | 1c80800 | 2019-09-11 00:29:26 -0700 | [diff] [blame] | 419 | self.assertIn('IMAGES/system.img', namelist) |
| 420 | self.assertIn(POSTINSTALL_CONFIG, namelist) |
| 421 | self.assertIn('META/misc_info.txt', namelist) |
| 422 | self.assertIn('META/dynamic_partitions_info.txt', namelist) |
| 423 | |
Tao Bao | 3e75946 | 2019-09-17 22:43:11 -0700 | [diff] [blame] | 424 | self.assertNotIn('IMAGES/boot.img', namelist) |
Tianjie Xu | 1c80800 | 2019-09-11 00:29:26 -0700 | [diff] [blame] | 425 | self.assertNotIn('IMAGES/system_other.img', namelist) |
| 426 | self.assertNotIn('IMAGES/system.map', namelist) |
| 427 | |
| 428 | # Check the vendor & product are removed from the partitions list. |
| 429 | expected_misc_info = misc_info.replace('system vendor product', |
| 430 | 'system') |
| 431 | expected_dynamic_partitions_info = dynamic_partitions_info.replace( |
| 432 | 'system vendor product', 'system') |
| 433 | self.assertEqual(expected_misc_info, updated_misc_info) |
| 434 | self.assertEqual(expected_dynamic_partitions_info, |
| 435 | updated_dynamic_partitions_info) |
| 436 | |
| 437 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 438 | def test_GetTargetFilesZipWithoutPostinstallConfig(self): |
| 439 | input_file = construct_target_files() |
| 440 | target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file) |
| 441 | with zipfile.ZipFile(target_file) as verify_zip: |
| 442 | self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist()) |
| 443 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 444 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 445 | def test_GetTargetFilesZipWithoutPostinstallConfig_missingEntry(self): |
| 446 | input_file = construct_target_files() |
| 447 | common.ZipDelete(input_file, POSTINSTALL_CONFIG) |
| 448 | target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file) |
| 449 | with zipfile.ZipFile(target_file) as verify_zip: |
| 450 | self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist()) |
| 451 | |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 452 | def _test_FinalizeMetadata(self, large_entry=False): |
| 453 | entries = [ |
| 454 | 'required-entry1', |
| 455 | 'required-entry2', |
| 456 | ] |
| 457 | zip_file = PropertyFilesTest.construct_zip_package(entries) |
| 458 | # Add a large entry of 1 GiB if requested. |
| 459 | if large_entry: |
| 460 | with zipfile.ZipFile(zip_file, 'a') as zip_fp: |
| 461 | zip_fp.writestr( |
| 462 | # Using 'zoo' so that the entry stays behind others after signing. |
| 463 | 'zoo', |
| 464 | 'A' * 1024 * 1024 * 1024, |
| 465 | zipfile.ZIP_STORED) |
| 466 | |
| 467 | metadata = {} |
| 468 | output_file = common.MakeTempFile(suffix='.zip') |
| 469 | needed_property_files = ( |
| 470 | TestPropertyFiles(), |
| 471 | ) |
| 472 | FinalizeMetadata(metadata, zip_file, output_file, needed_property_files) |
| 473 | self.assertIn('ota-test-property-files', metadata) |
| 474 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 475 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 476 | def test_FinalizeMetadata(self): |
| 477 | self._test_FinalizeMetadata() |
| 478 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 479 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 480 | def test_FinalizeMetadata_withNoSigning(self): |
| 481 | common.OPTIONS.no_signing = True |
| 482 | self._test_FinalizeMetadata() |
| 483 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 484 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 485 | def test_FinalizeMetadata_largeEntry(self): |
| 486 | self._test_FinalizeMetadata(large_entry=True) |
| 487 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 488 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 489 | def test_FinalizeMetadata_largeEntry_withNoSigning(self): |
| 490 | common.OPTIONS.no_signing = True |
| 491 | self._test_FinalizeMetadata(large_entry=True) |
| 492 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 493 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 494 | def test_FinalizeMetadata_insufficientSpace(self): |
| 495 | entries = [ |
| 496 | 'required-entry1', |
| 497 | 'required-entry2', |
| 498 | 'optional-entry1', |
| 499 | 'optional-entry2', |
| 500 | ] |
| 501 | zip_file = PropertyFilesTest.construct_zip_package(entries) |
| 502 | with zipfile.ZipFile(zip_file, 'a') as zip_fp: |
| 503 | zip_fp.writestr( |
| 504 | # 'foo-entry1' will appear ahead of all other entries (in alphabetical |
| 505 | # order) after the signing, which will in turn trigger the |
| 506 | # InsufficientSpaceException and an automatic retry. |
| 507 | 'foo-entry1', |
| 508 | 'A' * 1024 * 1024, |
| 509 | zipfile.ZIP_STORED) |
| 510 | |
| 511 | metadata = {} |
| 512 | needed_property_files = ( |
| 513 | TestPropertyFiles(), |
| 514 | ) |
| 515 | output_file = common.MakeTempFile(suffix='.zip') |
| 516 | FinalizeMetadata(metadata, zip_file, output_file, needed_property_files) |
| 517 | self.assertIn('ota-test-property-files', metadata) |
| 518 | |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 519 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 520 | class TestPropertyFiles(PropertyFiles): |
| 521 | """A class that extends PropertyFiles for testing purpose.""" |
| 522 | |
| 523 | def __init__(self): |
| 524 | super(TestPropertyFiles, self).__init__() |
| 525 | self.name = 'ota-test-property-files' |
| 526 | self.required = ( |
| 527 | 'required-entry1', |
| 528 | 'required-entry2', |
| 529 | ) |
| 530 | self.optional = ( |
| 531 | 'optional-entry1', |
| 532 | 'optional-entry2', |
| 533 | ) |
| 534 | |
Kelvin Zhang | cff4d76 | 2020-07-29 16:37:51 -0400 | [diff] [blame^] | 535 | class PropertyFilesTest(PropertyFilesTestCase): |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 536 | |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 537 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 538 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 539 | def test_Compute(self): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 540 | entries = ( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 541 | 'required-entry1', |
| 542 | 'required-entry2', |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 543 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 544 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 545 | property_files = TestPropertyFiles() |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 546 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 547 | property_files_string = property_files.Compute(zip_fp) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 548 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 549 | tokens = self._parse_property_files_string(property_files_string) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 550 | self.assertEqual(3, len(tokens)) |
| 551 | self._verify_entries(zip_file, tokens, entries) |
| 552 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 553 | def test_Compute_withOptionalEntries(self): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 554 | entries = ( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 555 | 'required-entry1', |
| 556 | 'required-entry2', |
| 557 | 'optional-entry1', |
| 558 | 'optional-entry2', |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 559 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 560 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 561 | property_files = TestPropertyFiles() |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 562 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 563 | property_files_string = property_files.Compute(zip_fp) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 564 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 565 | tokens = self._parse_property_files_string(property_files_string) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 566 | self.assertEqual(5, len(tokens)) |
| 567 | self._verify_entries(zip_file, tokens, entries) |
| 568 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 569 | def test_Compute_missingRequiredEntry(self): |
| 570 | entries = ( |
| 571 | 'required-entry2', |
| 572 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 573 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 574 | property_files = TestPropertyFiles() |
| 575 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 576 | self.assertRaises(KeyError, property_files.Compute, zip_fp) |
| 577 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 578 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 579 | def test_Finalize(self): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 580 | entries = [ |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 581 | 'required-entry1', |
| 582 | 'required-entry2', |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 583 | 'META-INF/com/android/metadata', |
| 584 | ] |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 585 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 586 | property_files = TestPropertyFiles() |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 587 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 588 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 589 | zip_fp, reserve_space=False) |
| 590 | streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata)) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 591 | tokens = self._parse_property_files_string(streaming_metadata) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 592 | |
| 593 | self.assertEqual(3, len(tokens)) |
| 594 | # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the |
| 595 | # streaming metadata. |
| 596 | entries[2] = 'metadata' |
| 597 | self._verify_entries(zip_file, tokens, entries) |
| 598 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 599 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 600 | def test_Finalize_assertReservedLength(self): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 601 | entries = ( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 602 | 'required-entry1', |
| 603 | 'required-entry2', |
| 604 | 'optional-entry1', |
| 605 | 'optional-entry2', |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 606 | 'META-INF/com/android/metadata', |
| 607 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 608 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 609 | property_files = TestPropertyFiles() |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 610 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 611 | # First get the raw metadata string (i.e. without padding space). |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 612 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 613 | zip_fp, reserve_space=False) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 614 | raw_length = len(raw_metadata) |
| 615 | |
| 616 | # Now pass in the exact expected length. |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 617 | streaming_metadata = property_files.Finalize(zip_fp, raw_length) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 618 | self.assertEqual(raw_length, len(streaming_metadata)) |
| 619 | |
| 620 | # Or pass in insufficient length. |
| 621 | self.assertRaises( |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 622 | PropertyFiles.InsufficientSpaceException, |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 623 | property_files.Finalize, |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 624 | zip_fp, |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 625 | raw_length - 1) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 626 | |
| 627 | # Or pass in a much larger size. |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 628 | streaming_metadata = property_files.Finalize( |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 629 | zip_fp, |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 630 | raw_length + 20) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 631 | self.assertEqual(raw_length + 20, len(streaming_metadata)) |
| 632 | self.assertEqual(' ' * 20, streaming_metadata[raw_length:]) |
| 633 | |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 634 | def test_Verify(self): |
| 635 | entries = ( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 636 | 'required-entry1', |
| 637 | 'required-entry2', |
| 638 | 'optional-entry1', |
| 639 | 'optional-entry2', |
| 640 | 'META-INF/com/android/metadata', |
| 641 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 642 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 643 | property_files = TestPropertyFiles() |
| 644 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 645 | # First get the raw metadata string (i.e. without padding space). |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 646 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 647 | zip_fp, reserve_space=False) |
| 648 | |
| 649 | # Should pass the test if verification passes. |
| 650 | property_files.Verify(zip_fp, raw_metadata) |
| 651 | |
| 652 | # Or raise on verification failure. |
| 653 | self.assertRaises( |
| 654 | AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x') |
| 655 | |
| 656 | |
Kelvin Zhang | cff4d76 | 2020-07-29 16:37:51 -0400 | [diff] [blame^] | 657 | class StreamingPropertyFilesTest(PropertyFilesTestCase): |
Kelvin Zhang | c693d95 | 2020-07-22 19:21:22 -0400 | [diff] [blame] | 658 | """Additional validity checks specialized for StreamingPropertyFiles.""" |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 659 | |
| 660 | def test_init(self): |
| 661 | property_files = StreamingPropertyFiles() |
| 662 | self.assertEqual('ota-streaming-property-files', property_files.name) |
| 663 | self.assertEqual( |
| 664 | ( |
| 665 | 'payload.bin', |
| 666 | 'payload_properties.txt', |
| 667 | ), |
| 668 | property_files.required) |
| 669 | self.assertEqual( |
| 670 | ( |
Tianjie Xu | 4c05f4a | 2018-09-14 16:24:41 -0700 | [diff] [blame] | 671 | 'care_map.pb', |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 672 | 'care_map.txt', |
| 673 | 'compatibility.zip', |
| 674 | ), |
| 675 | property_files.optional) |
| 676 | |
| 677 | def test_Compute(self): |
| 678 | entries = ( |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 679 | 'payload.bin', |
| 680 | 'payload_properties.txt', |
| 681 | 'care_map.txt', |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 682 | 'compatibility.zip', |
| 683 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 684 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 685 | property_files = StreamingPropertyFiles() |
| 686 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 687 | property_files_string = property_files.Compute(zip_fp) |
| 688 | |
| 689 | tokens = self._parse_property_files_string(property_files_string) |
| 690 | self.assertEqual(5, len(tokens)) |
| 691 | self._verify_entries(zip_file, tokens, entries) |
| 692 | |
| 693 | def test_Finalize(self): |
| 694 | entries = [ |
| 695 | 'payload.bin', |
| 696 | 'payload_properties.txt', |
| 697 | 'care_map.txt', |
| 698 | 'compatibility.zip', |
| 699 | 'META-INF/com/android/metadata', |
| 700 | ] |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 701 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 702 | property_files = StreamingPropertyFiles() |
| 703 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 704 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 705 | zip_fp, reserve_space=False) |
| 706 | streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata)) |
| 707 | tokens = self._parse_property_files_string(streaming_metadata) |
| 708 | |
| 709 | self.assertEqual(5, len(tokens)) |
| 710 | # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the |
| 711 | # streaming metadata. |
| 712 | entries[4] = 'metadata' |
| 713 | self._verify_entries(zip_file, tokens, entries) |
| 714 | |
| 715 | def test_Verify(self): |
| 716 | entries = ( |
| 717 | 'payload.bin', |
| 718 | 'payload_properties.txt', |
| 719 | 'care_map.txt', |
| 720 | 'compatibility.zip', |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 721 | 'META-INF/com/android/metadata', |
| 722 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 723 | zip_file = self.construct_zip_package(entries) |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 724 | property_files = StreamingPropertyFiles() |
| 725 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 726 | # First get the raw metadata string (i.e. without padding space). |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 727 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 728 | zip_fp, reserve_space=False) |
| 729 | |
| 730 | # Should pass the test if verification passes. |
| 731 | property_files.Verify(zip_fp, raw_metadata) |
| 732 | |
| 733 | # Or raise on verification failure. |
| 734 | self.assertRaises( |
| 735 | AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x') |
| 736 | |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 737 | |
Kelvin Zhang | cff4d76 | 2020-07-29 16:37:51 -0400 | [diff] [blame^] | 738 | class AbOtaPropertyFilesTest(PropertyFilesTestCase): |
Kelvin Zhang | c693d95 | 2020-07-22 19:21:22 -0400 | [diff] [blame] | 739 | """Additional validity checks specialized for AbOtaPropertyFiles.""" |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 740 | |
| 741 | # The size for payload and metadata signature size. |
| 742 | SIGNATURE_SIZE = 256 |
| 743 | |
| 744 | def setUp(self): |
| 745 | self.testdata_dir = test_utils.get_testdata_dir() |
| 746 | self.assertTrue(os.path.exists(self.testdata_dir)) |
| 747 | |
| 748 | common.OPTIONS.wipe_user_data = False |
| 749 | common.OPTIONS.payload_signer = None |
| 750 | common.OPTIONS.payload_signer_args = None |
| 751 | common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey') |
| 752 | common.OPTIONS.key_passwords = { |
| 753 | common.OPTIONS.package_key : None, |
| 754 | } |
| 755 | |
| 756 | def test_init(self): |
| 757 | property_files = AbOtaPropertyFiles() |
| 758 | self.assertEqual('ota-property-files', property_files.name) |
| 759 | self.assertEqual( |
| 760 | ( |
| 761 | 'payload.bin', |
| 762 | 'payload_properties.txt', |
| 763 | ), |
| 764 | property_files.required) |
| 765 | self.assertEqual( |
| 766 | ( |
Tianjie Xu | 4c05f4a | 2018-09-14 16:24:41 -0700 | [diff] [blame] | 767 | 'care_map.pb', |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 768 | 'care_map.txt', |
| 769 | 'compatibility.zip', |
| 770 | ), |
| 771 | property_files.optional) |
| 772 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 773 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 774 | def test_GetPayloadMetadataOffsetAndSize(self): |
| 775 | target_file = construct_target_files() |
| 776 | payload = Payload() |
| 777 | payload.Generate(target_file) |
| 778 | |
| 779 | payload_signer = PayloadSigner() |
| 780 | payload.Sign(payload_signer) |
| 781 | |
| 782 | output_file = common.MakeTempFile(suffix='.zip') |
| 783 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 784 | payload.WriteToZip(output_zip) |
| 785 | |
| 786 | # Find out the payload metadata offset and size. |
| 787 | property_files = AbOtaPropertyFiles() |
| 788 | with zipfile.ZipFile(output_file) as input_zip: |
| 789 | # pylint: disable=protected-access |
| 790 | payload_offset, metadata_total = ( |
| 791 | property_files._GetPayloadMetadataOffsetAndSize(input_zip)) |
| 792 | |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 793 | # The signature proto has the following format (details in |
| 794 | # /platform/system/update_engine/update_metadata.proto): |
| 795 | # message Signature { |
| 796 | # optional uint32 version = 1; |
| 797 | # optional bytes data = 2; |
| 798 | # optional fixed32 unpadded_signature_size = 3; |
| 799 | # } |
| 800 | # |
| 801 | # According to the protobuf encoding, the tail of the signature message will |
| 802 | # be [signature string(256 bytes) + encoding of the fixed32 number 256]. And |
| 803 | # 256 is encoded as 'x1d\x00\x01\x00\x00': |
| 804 | # [3 (field number) << 3 | 5 (type) + byte reverse of 0x100 (256)]. |
| 805 | # Details in (https://developers.google.com/protocol-buffers/docs/encoding) |
| 806 | signature_tail_length = self.SIGNATURE_SIZE + 5 |
| 807 | self.assertGreater(metadata_total, signature_tail_length) |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 808 | with open(output_file, 'rb') as verify_fp: |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 809 | verify_fp.seek(payload_offset + metadata_total - signature_tail_length) |
| 810 | metadata_signature_proto_tail = verify_fp.read(signature_tail_length) |
| 811 | |
| 812 | self.assertEqual(b'\x1d\x00\x01\x00\x00', |
| 813 | metadata_signature_proto_tail[-5:]) |
| 814 | metadata_signature = metadata_signature_proto_tail[:-5] |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 815 | |
| 816 | # Now we extract the metadata hash via brillo_update_payload script, which |
| 817 | # will serve as the oracle result. |
| 818 | payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin") |
| 819 | metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin") |
| 820 | cmd = ['brillo_update_payload', 'hash', |
| 821 | '--unsigned_payload', payload.payload_file, |
| 822 | '--signature_size', str(self.SIGNATURE_SIZE), |
| 823 | '--metadata_hash_file', metadata_sig_file, |
| 824 | '--payload_hash_file', payload_sig_file] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame] | 825 | proc = common.Run(cmd) |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 826 | stdoutdata, _ = proc.communicate() |
| 827 | self.assertEqual( |
| 828 | 0, proc.returncode, |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame] | 829 | 'Failed to run brillo_update_payload:\n{}'.format(stdoutdata)) |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 830 | |
| 831 | signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file) |
| 832 | |
| 833 | # Finally we can compare the two signatures. |
| 834 | with open(signed_metadata_sig_file, 'rb') as verify_fp: |
| 835 | self.assertEqual(verify_fp.read(), metadata_signature) |
| 836 | |
| 837 | @staticmethod |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 838 | def construct_zip_package_withValidPayload(with_metadata=False): |
| 839 | # Cannot use construct_zip_package() since we need a "valid" payload.bin. |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 840 | target_file = construct_target_files() |
| 841 | payload = Payload() |
| 842 | payload.Generate(target_file) |
| 843 | |
| 844 | payload_signer = PayloadSigner() |
| 845 | payload.Sign(payload_signer) |
| 846 | |
| 847 | zip_file = common.MakeTempFile(suffix='.zip') |
| 848 | with zipfile.ZipFile(zip_file, 'w') as zip_fp: |
| 849 | # 'payload.bin', |
| 850 | payload.WriteToZip(zip_fp) |
| 851 | |
| 852 | # Other entries. |
| 853 | entries = ['care_map.txt', 'compatibility.zip'] |
| 854 | |
| 855 | # Put META-INF/com/android/metadata if needed. |
| 856 | if with_metadata: |
| 857 | entries.append('META-INF/com/android/metadata') |
| 858 | |
| 859 | for entry in entries: |
| 860 | zip_fp.writestr( |
| 861 | entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED) |
| 862 | |
| 863 | return zip_file |
| 864 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 865 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 866 | def test_Compute(self): |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 867 | zip_file = self.construct_zip_package_withValidPayload() |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 868 | property_files = AbOtaPropertyFiles() |
| 869 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 870 | property_files_string = property_files.Compute(zip_fp) |
| 871 | |
| 872 | tokens = self._parse_property_files_string(property_files_string) |
| 873 | # "6" indcludes the four entries above, one metadata entry, and one entry |
| 874 | # for payload-metadata.bin. |
| 875 | self.assertEqual(6, len(tokens)) |
| 876 | self._verify_entries( |
| 877 | zip_file, tokens, ('care_map.txt', 'compatibility.zip')) |
| 878 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 879 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 880 | def test_Finalize(self): |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 881 | zip_file = self.construct_zip_package_withValidPayload(with_metadata=True) |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 882 | property_files = AbOtaPropertyFiles() |
| 883 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 884 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 885 | zip_fp, reserve_space=False) |
| 886 | property_files_string = property_files.Finalize(zip_fp, len(raw_metadata)) |
| 887 | |
| 888 | tokens = self._parse_property_files_string(property_files_string) |
| 889 | # "6" indcludes the four entries above, one metadata entry, and one entry |
| 890 | # for payload-metadata.bin. |
| 891 | self.assertEqual(6, len(tokens)) |
| 892 | self._verify_entries( |
| 893 | zip_file, tokens, ('care_map.txt', 'compatibility.zip')) |
| 894 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 895 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 896 | def test_Verify(self): |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 897 | zip_file = self.construct_zip_package_withValidPayload(with_metadata=True) |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 898 | property_files = AbOtaPropertyFiles() |
| 899 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 900 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 901 | zip_fp, reserve_space=False) |
| 902 | |
| 903 | property_files.Verify(zip_fp, raw_metadata) |
| 904 | |
| 905 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 906 | class PayloadSignerTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 907 | |
| 908 | SIGFILE = 'sigfile.bin' |
| 909 | SIGNED_SIGFILE = 'signed-sigfile.bin' |
| 910 | |
| 911 | def setUp(self): |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 912 | self.testdata_dir = test_utils.get_testdata_dir() |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 913 | self.assertTrue(os.path.exists(self.testdata_dir)) |
| 914 | |
| 915 | common.OPTIONS.payload_signer = None |
| 916 | common.OPTIONS.payload_signer_args = [] |
| 917 | common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey') |
| 918 | common.OPTIONS.key_passwords = { |
| 919 | common.OPTIONS.package_key : None, |
| 920 | } |
| 921 | |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 922 | def _assertFilesEqual(self, file1, file2): |
| 923 | with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2: |
| 924 | self.assertEqual(fp1.read(), fp2.read()) |
| 925 | |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 926 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 927 | def test_init(self): |
| 928 | payload_signer = PayloadSigner() |
| 929 | self.assertEqual('openssl', payload_signer.signer) |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 930 | self.assertEqual(256, payload_signer.maximum_signature_size) |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 931 | |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 932 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 933 | def test_init_withPassword(self): |
| 934 | common.OPTIONS.package_key = os.path.join( |
| 935 | self.testdata_dir, 'testkey_with_passwd') |
| 936 | common.OPTIONS.key_passwords = { |
| 937 | common.OPTIONS.package_key : 'foo', |
| 938 | } |
| 939 | payload_signer = PayloadSigner() |
| 940 | self.assertEqual('openssl', payload_signer.signer) |
| 941 | |
| 942 | def test_init_withExternalSigner(self): |
| 943 | common.OPTIONS.payload_signer = 'abc' |
| 944 | common.OPTIONS.payload_signer_args = ['arg1', 'arg2'] |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 945 | common.OPTIONS.payload_signer_maximum_signature_size = '512' |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 946 | payload_signer = PayloadSigner() |
| 947 | self.assertEqual('abc', payload_signer.signer) |
| 948 | self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args) |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 949 | self.assertEqual(512, payload_signer.maximum_signature_size) |
xunchang | 376cc7c | 2019-04-08 23:04:58 -0700 | [diff] [blame] | 950 | |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 951 | @test_utils.SkipIfExternalToolsUnavailable() |
| 952 | def test_GetMaximumSignatureSizeInBytes_512Bytes(self): |
xunchang | 376cc7c | 2019-04-08 23:04:58 -0700 | [diff] [blame] | 953 | signing_key = os.path.join(self.testdata_dir, 'testkey_RSA4096.key') |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 954 | # pylint: disable=protected-access |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 955 | signature_size = PayloadSigner._GetMaximumSignatureSizeInBytes(signing_key) |
| 956 | self.assertEqual(512, signature_size) |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 957 | |
Tianjie Xu | 21e6deb | 2019-10-07 18:01:00 -0700 | [diff] [blame] | 958 | @test_utils.SkipIfExternalToolsUnavailable() |
| 959 | def test_GetMaximumSignatureSizeInBytes_ECKey(self): |
| 960 | signing_key = os.path.join(self.testdata_dir, 'testkey_EC.key') |
| 961 | # pylint: disable=protected-access |
| 962 | signature_size = PayloadSigner._GetMaximumSignatureSizeInBytes(signing_key) |
| 963 | self.assertEqual(72, signature_size) |
| 964 | |
| 965 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 966 | def test_Sign(self): |
| 967 | payload_signer = PayloadSigner() |
| 968 | input_file = os.path.join(self.testdata_dir, self.SIGFILE) |
| 969 | signed_file = payload_signer.Sign(input_file) |
| 970 | |
| 971 | verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE) |
| 972 | self._assertFilesEqual(verify_file, signed_file) |
| 973 | |
| 974 | def test_Sign_withExternalSigner_openssl(self): |
| 975 | """Uses openssl as the external payload signer.""" |
| 976 | common.OPTIONS.payload_signer = 'openssl' |
| 977 | common.OPTIONS.payload_signer_args = [ |
| 978 | 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey', |
| 979 | os.path.join(self.testdata_dir, 'testkey.pk8'), |
| 980 | '-pkeyopt', 'digest:sha256'] |
| 981 | payload_signer = PayloadSigner() |
| 982 | input_file = os.path.join(self.testdata_dir, self.SIGFILE) |
| 983 | signed_file = payload_signer.Sign(input_file) |
| 984 | |
| 985 | verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE) |
| 986 | self._assertFilesEqual(verify_file, signed_file) |
| 987 | |
| 988 | def test_Sign_withExternalSigner_script(self): |
| 989 | """Uses testdata/payload_signer.sh as the external payload signer.""" |
| 990 | common.OPTIONS.payload_signer = os.path.join( |
| 991 | self.testdata_dir, 'payload_signer.sh') |
Tao Bao | 30e3114 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 992 | os.chmod(common.OPTIONS.payload_signer, 0o700) |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 993 | common.OPTIONS.payload_signer_args = [ |
| 994 | os.path.join(self.testdata_dir, 'testkey.pk8')] |
| 995 | payload_signer = PayloadSigner() |
| 996 | input_file = os.path.join(self.testdata_dir, self.SIGFILE) |
| 997 | signed_file = payload_signer.Sign(input_file) |
| 998 | |
| 999 | verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE) |
| 1000 | self._assertFilesEqual(verify_file, signed_file) |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1001 | |
| 1002 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 1003 | class PayloadTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1004 | |
| 1005 | def setUp(self): |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 1006 | self.testdata_dir = test_utils.get_testdata_dir() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1007 | self.assertTrue(os.path.exists(self.testdata_dir)) |
| 1008 | |
| 1009 | common.OPTIONS.wipe_user_data = False |
| 1010 | common.OPTIONS.payload_signer = None |
| 1011 | common.OPTIONS.payload_signer_args = None |
| 1012 | common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey') |
| 1013 | common.OPTIONS.key_passwords = { |
| 1014 | common.OPTIONS.package_key : None, |
| 1015 | } |
| 1016 | |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1017 | @staticmethod |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1018 | def _create_payload_full(secondary=False): |
| 1019 | target_file = construct_target_files(secondary) |
Tao Bao | 667ff57 | 2018-02-10 00:02:40 -0800 | [diff] [blame] | 1020 | payload = Payload(secondary) |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1021 | payload.Generate(target_file) |
| 1022 | return payload |
| 1023 | |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1024 | @staticmethod |
| 1025 | def _create_payload_incremental(): |
| 1026 | target_file = construct_target_files() |
| 1027 | source_file = construct_target_files() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1028 | payload = Payload() |
| 1029 | payload.Generate(target_file, source_file) |
| 1030 | return payload |
| 1031 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1032 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1033 | def test_Generate_full(self): |
| 1034 | payload = self._create_payload_full() |
| 1035 | self.assertTrue(os.path.exists(payload.payload_file)) |
| 1036 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1037 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1038 | def test_Generate_incremental(self): |
| 1039 | payload = self._create_payload_incremental() |
| 1040 | self.assertTrue(os.path.exists(payload.payload_file)) |
| 1041 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1042 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1043 | def test_Generate_additionalArgs(self): |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1044 | target_file = construct_target_files() |
| 1045 | source_file = construct_target_files() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1046 | payload = Payload() |
| 1047 | # This should work the same as calling payload.Generate(target_file, |
| 1048 | # source_file). |
| 1049 | payload.Generate( |
| 1050 | target_file, additional_args=["--source_image", source_file]) |
| 1051 | self.assertTrue(os.path.exists(payload.payload_file)) |
| 1052 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1053 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1054 | def test_Generate_invalidInput(self): |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1055 | target_file = construct_target_files() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1056 | common.ZipDelete(target_file, 'IMAGES/vendor.img') |
| 1057 | payload = Payload() |
Tao Bao | bec89c1 | 2018-10-15 11:53:28 -0700 | [diff] [blame] | 1058 | self.assertRaises(common.ExternalError, payload.Generate, target_file) |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1059 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1060 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1061 | def test_Sign_full(self): |
| 1062 | payload = self._create_payload_full() |
| 1063 | payload.Sign(PayloadSigner()) |
| 1064 | |
| 1065 | output_file = common.MakeTempFile(suffix='.zip') |
| 1066 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1067 | payload.WriteToZip(output_zip) |
| 1068 | |
| 1069 | import check_ota_package_signature |
| 1070 | check_ota_package_signature.VerifyAbOtaPayload( |
| 1071 | os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 1072 | output_file) |
| 1073 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1074 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1075 | def test_Sign_incremental(self): |
| 1076 | payload = self._create_payload_incremental() |
| 1077 | payload.Sign(PayloadSigner()) |
| 1078 | |
| 1079 | output_file = common.MakeTempFile(suffix='.zip') |
| 1080 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1081 | payload.WriteToZip(output_zip) |
| 1082 | |
| 1083 | import check_ota_package_signature |
| 1084 | check_ota_package_signature.VerifyAbOtaPayload( |
| 1085 | os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 1086 | output_file) |
| 1087 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1088 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1089 | def test_Sign_withDataWipe(self): |
| 1090 | common.OPTIONS.wipe_user_data = True |
| 1091 | payload = self._create_payload_full() |
| 1092 | payload.Sign(PayloadSigner()) |
| 1093 | |
| 1094 | with open(payload.payload_properties) as properties_fp: |
| 1095 | self.assertIn("POWERWASH=1", properties_fp.read()) |
| 1096 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1097 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 667ff57 | 2018-02-10 00:02:40 -0800 | [diff] [blame] | 1098 | def test_Sign_secondary(self): |
| 1099 | payload = self._create_payload_full(secondary=True) |
| 1100 | payload.Sign(PayloadSigner()) |
| 1101 | |
| 1102 | with open(payload.payload_properties) as properties_fp: |
| 1103 | self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read()) |
| 1104 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1105 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1106 | def test_Sign_badSigner(self): |
| 1107 | """Tests that signing failure can be captured.""" |
| 1108 | payload = self._create_payload_full() |
| 1109 | payload_signer = PayloadSigner() |
| 1110 | payload_signer.signer_args.append('bad-option') |
Tao Bao | bec89c1 | 2018-10-15 11:53:28 -0700 | [diff] [blame] | 1111 | self.assertRaises(common.ExternalError, payload.Sign, payload_signer) |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1112 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1113 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1114 | def test_WriteToZip(self): |
| 1115 | payload = self._create_payload_full() |
| 1116 | payload.Sign(PayloadSigner()) |
| 1117 | |
| 1118 | output_file = common.MakeTempFile(suffix='.zip') |
| 1119 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1120 | payload.WriteToZip(output_zip) |
| 1121 | |
| 1122 | with zipfile.ZipFile(output_file) as verify_zip: |
| 1123 | # First make sure we have the essential entries. |
| 1124 | namelist = verify_zip.namelist() |
| 1125 | self.assertIn(Payload.PAYLOAD_BIN, namelist) |
| 1126 | self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist) |
| 1127 | |
| 1128 | # Then assert these entries are stored. |
| 1129 | for entry_info in verify_zip.infolist(): |
| 1130 | if entry_info.filename not in (Payload.PAYLOAD_BIN, |
| 1131 | Payload.PAYLOAD_PROPERTIES_TXT): |
| 1132 | continue |
| 1133 | self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type) |
| 1134 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1135 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1136 | def test_WriteToZip_unsignedPayload(self): |
| 1137 | """Unsigned payloads should not be allowed to be written to zip.""" |
| 1138 | payload = self._create_payload_full() |
| 1139 | |
| 1140 | output_file = common.MakeTempFile(suffix='.zip') |
| 1141 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1142 | self.assertRaises(AssertionError, payload.WriteToZip, output_zip) |
| 1143 | |
| 1144 | # Also test with incremental payload. |
| 1145 | payload = self._create_payload_incremental() |
| 1146 | |
| 1147 | output_file = common.MakeTempFile(suffix='.zip') |
| 1148 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1149 | self.assertRaises(AssertionError, payload.WriteToZip, output_zip) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1150 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1151 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1152 | def test_WriteToZip_secondary(self): |
| 1153 | payload = self._create_payload_full(secondary=True) |
| 1154 | payload.Sign(PayloadSigner()) |
| 1155 | |
| 1156 | output_file = common.MakeTempFile(suffix='.zip') |
| 1157 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
Tao Bao | 667ff57 | 2018-02-10 00:02:40 -0800 | [diff] [blame] | 1158 | payload.WriteToZip(output_zip) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1159 | |
| 1160 | with zipfile.ZipFile(output_file) as verify_zip: |
| 1161 | # First make sure we have the essential entries. |
| 1162 | namelist = verify_zip.namelist() |
| 1163 | self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist) |
| 1164 | self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist) |
| 1165 | |
| 1166 | # Then assert these entries are stored. |
| 1167 | for entry_info in verify_zip.infolist(): |
| 1168 | if entry_info.filename not in ( |
| 1169 | Payload.SECONDARY_PAYLOAD_BIN, |
| 1170 | Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT): |
| 1171 | continue |
| 1172 | self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type) |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1173 | |
| 1174 | |
| 1175 | class RuntimeFingerprintTest(test_utils.ReleaseToolsTestCase): |
| 1176 | MISC_INFO = [ |
| 1177 | 'recovery_api_version=3', |
| 1178 | 'fstab_version=2', |
| 1179 | 'recovery_as_boot=true', |
| 1180 | ] |
| 1181 | |
| 1182 | BUILD_PROP = [ |
| 1183 | 'ro.build.version.release=version-release', |
| 1184 | 'ro.build.id=build-id', |
| 1185 | 'ro.build.version.incremental=version-incremental', |
| 1186 | 'ro.build.type=build-type', |
| 1187 | 'ro.build.tags=build-tags', |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1188 | 'ro.build.version.sdk=30', |
| 1189 | 'ro.build.version.security_patch=2020', |
| 1190 | 'ro.build.date.utc=12345678' |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1191 | ] |
| 1192 | |
| 1193 | VENDOR_BUILD_PROP = [ |
| 1194 | 'ro.product.vendor.brand=vendor-product-brand', |
| 1195 | 'ro.product.vendor.name=vendor-product-name', |
| 1196 | 'ro.product.vendor.device=vendor-product-device' |
| 1197 | ] |
| 1198 | |
| 1199 | def setUp(self): |
| 1200 | common.OPTIONS.oem_dicts = None |
| 1201 | self.test_dir = common.MakeTempDir() |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1202 | self.writeFiles({'META/misc_info.txt': '\n'.join(self.MISC_INFO)}, |
| 1203 | self.test_dir) |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1204 | |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1205 | def writeFiles(self, contents_dict, out_dir): |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1206 | for path, content in contents_dict.items(): |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1207 | abs_path = os.path.join(out_dir, path) |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1208 | dir_name = os.path.dirname(abs_path) |
| 1209 | if not os.path.exists(dir_name): |
| 1210 | os.makedirs(dir_name) |
| 1211 | with open(abs_path, 'w') as f: |
| 1212 | f.write(content) |
| 1213 | |
| 1214 | @staticmethod |
| 1215 | def constructFingerprint(prefix): |
| 1216 | return '{}:version-release/build-id/version-incremental:' \ |
| 1217 | 'build-type/build-tags'.format(prefix) |
| 1218 | |
| 1219 | def test_CalculatePossibleFingerprints_no_dynamic_fingerprint(self): |
| 1220 | build_prop = copy.deepcopy(self.BUILD_PROP) |
| 1221 | build_prop.extend([ |
| 1222 | 'ro.product.brand=product-brand', |
| 1223 | 'ro.product.name=product-name', |
| 1224 | 'ro.product.device=product-device', |
| 1225 | ]) |
| 1226 | self.writeFiles({ |
| 1227 | 'SYSTEM/build.prop': '\n'.join(build_prop), |
| 1228 | 'VENDOR/build.prop': '\n'.join(self.VENDOR_BUILD_PROP), |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1229 | }, self.test_dir) |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1230 | |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1231 | build_info = common.BuildInfo(common.LoadInfoDict(self.test_dir)) |
| 1232 | expected = ({'product-device'}, |
| 1233 | {self.constructFingerprint( |
| 1234 | 'product-brand/product-name/product-device')}) |
| 1235 | self.assertEqual(expected, |
| 1236 | CalculateRuntimeDevicesAndFingerprints(build_info, {})) |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1237 | |
| 1238 | def test_CalculatePossibleFingerprints_single_override(self): |
| 1239 | vendor_build_prop = copy.deepcopy(self.VENDOR_BUILD_PROP) |
| 1240 | vendor_build_prop.extend([ |
| 1241 | 'import /vendor/etc/build_${ro.boot.sku_name}.prop', |
| 1242 | ]) |
| 1243 | self.writeFiles({ |
| 1244 | 'SYSTEM/build.prop': '\n'.join(self.BUILD_PROP), |
| 1245 | 'VENDOR/build.prop': '\n'.join(vendor_build_prop), |
| 1246 | 'VENDOR/etc/build_std.prop': |
| 1247 | 'ro.product.vendor.name=vendor-product-std', |
| 1248 | 'VENDOR/etc/build_pro.prop': |
| 1249 | 'ro.product.vendor.name=vendor-product-pro', |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1250 | }, self.test_dir) |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1251 | |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1252 | build_info = common.BuildInfo(common.LoadInfoDict(self.test_dir)) |
| 1253 | boot_variable_values = {'ro.boot.sku_name': ['std', 'pro']} |
| 1254 | |
| 1255 | expected = ({'vendor-product-device'}, { |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1256 | self.constructFingerprint( |
| 1257 | 'vendor-product-brand/vendor-product-name/vendor-product-device'), |
| 1258 | self.constructFingerprint( |
| 1259 | 'vendor-product-brand/vendor-product-std/vendor-product-device'), |
| 1260 | self.constructFingerprint( |
| 1261 | 'vendor-product-brand/vendor-product-pro/vendor-product-device'), |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1262 | }) |
| 1263 | self.assertEqual( |
| 1264 | expected, CalculateRuntimeDevicesAndFingerprints( |
| 1265 | build_info, boot_variable_values)) |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1266 | |
| 1267 | def test_CalculatePossibleFingerprints_multiple_overrides(self): |
| 1268 | vendor_build_prop = copy.deepcopy(self.VENDOR_BUILD_PROP) |
| 1269 | vendor_build_prop.extend([ |
| 1270 | 'import /vendor/etc/build_${ro.boot.sku_name}.prop', |
| 1271 | 'import /vendor/etc/build_${ro.boot.device_name}.prop', |
| 1272 | ]) |
| 1273 | self.writeFiles({ |
| 1274 | 'SYSTEM/build.prop': '\n'.join(self.BUILD_PROP), |
| 1275 | 'VENDOR/build.prop': '\n'.join(vendor_build_prop), |
| 1276 | 'VENDOR/etc/build_std.prop': |
| 1277 | 'ro.product.vendor.name=vendor-product-std', |
| 1278 | 'VENDOR/etc/build_product1.prop': |
| 1279 | 'ro.product.vendor.device=vendor-device-product1', |
| 1280 | 'VENDOR/etc/build_pro.prop': |
| 1281 | 'ro.product.vendor.name=vendor-product-pro', |
| 1282 | 'VENDOR/etc/build_product2.prop': |
| 1283 | 'ro.product.vendor.device=vendor-device-product2', |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1284 | }, self.test_dir) |
| 1285 | |
| 1286 | build_info = common.BuildInfo(common.LoadInfoDict(self.test_dir)) |
| 1287 | boot_variable_values = { |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1288 | 'ro.boot.sku_name': ['std', 'pro'], |
| 1289 | 'ro.boot.device_name': ['product1', 'product2'], |
| 1290 | } |
| 1291 | |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1292 | expected_devices = {'vendor-product-device', 'vendor-device-product1', |
| 1293 | 'vendor-device-product2'} |
| 1294 | expected_fingerprints = { |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1295 | self.constructFingerprint( |
| 1296 | 'vendor-product-brand/vendor-product-name/vendor-product-device'), |
| 1297 | self.constructFingerprint( |
| 1298 | 'vendor-product-brand/vendor-product-std/vendor-device-product1'), |
| 1299 | self.constructFingerprint( |
| 1300 | 'vendor-product-brand/vendor-product-pro/vendor-device-product1'), |
| 1301 | self.constructFingerprint( |
| 1302 | 'vendor-product-brand/vendor-product-std/vendor-device-product2'), |
| 1303 | self.constructFingerprint( |
Tianjie | d686716 | 2020-05-10 14:30:13 -0700 | [diff] [blame] | 1304 | 'vendor-product-brand/vendor-product-pro/vendor-device-product2') |
| 1305 | } |
| 1306 | self.assertEqual((expected_devices, expected_fingerprints), |
| 1307 | CalculateRuntimeDevicesAndFingerprints( |
| 1308 | build_info, boot_variable_values)) |
| 1309 | |
| 1310 | def test_GetPackageMetadata_full_package(self): |
| 1311 | vendor_build_prop = copy.deepcopy(self.VENDOR_BUILD_PROP) |
| 1312 | vendor_build_prop.extend([ |
| 1313 | 'import /vendor/etc/build_${ro.boot.sku_name}.prop', |
| 1314 | ]) |
| 1315 | self.writeFiles({ |
| 1316 | 'SYSTEM/build.prop': '\n'.join(self.BUILD_PROP), |
| 1317 | 'VENDOR/build.prop': '\n'.join(vendor_build_prop), |
| 1318 | 'VENDOR/etc/build_std.prop': |
| 1319 | 'ro.product.vendor.name=vendor-product-std', |
| 1320 | 'VENDOR/etc/build_pro.prop': |
| 1321 | 'ro.product.vendor.name=vendor-product-pro', |
| 1322 | }, self.test_dir) |
| 1323 | |
| 1324 | common.OPTIONS.boot_variable_file = common.MakeTempFile() |
| 1325 | with open(common.OPTIONS.boot_variable_file, 'w') as f: |
| 1326 | f.write('ro.boot.sku_name=std,pro') |
| 1327 | |
| 1328 | build_info = common.BuildInfo(common.LoadInfoDict(self.test_dir)) |
| 1329 | metadata = GetPackageMetadata(build_info) |
| 1330 | self.assertEqual('vendor-product-device', metadata['pre-device']) |
| 1331 | fingerprints = [ |
| 1332 | self.constructFingerprint( |
| 1333 | 'vendor-product-brand/vendor-product-name/vendor-product-device'), |
| 1334 | self.constructFingerprint( |
| 1335 | 'vendor-product-brand/vendor-product-pro/vendor-product-device'), |
| 1336 | self.constructFingerprint( |
| 1337 | 'vendor-product-brand/vendor-product-std/vendor-product-device'), |
| 1338 | ] |
| 1339 | self.assertEqual('|'.join(fingerprints), metadata['post-build']) |
| 1340 | |
| 1341 | def test_GetPackageMetadata_incremental_package(self): |
| 1342 | vendor_build_prop = copy.deepcopy(self.VENDOR_BUILD_PROP) |
| 1343 | vendor_build_prop.extend([ |
| 1344 | 'import /vendor/etc/build_${ro.boot.sku_name}.prop', |
| 1345 | ]) |
| 1346 | self.writeFiles({ |
| 1347 | 'SYSTEM/build.prop': '\n'.join(self.BUILD_PROP), |
| 1348 | 'VENDOR/build.prop': '\n'.join(vendor_build_prop), |
| 1349 | 'VENDOR/etc/build_std.prop': |
| 1350 | 'ro.product.vendor.device=vendor-device-std', |
| 1351 | 'VENDOR/etc/build_pro.prop': |
| 1352 | 'ro.product.vendor.device=vendor-device-pro', |
| 1353 | }, self.test_dir) |
| 1354 | |
| 1355 | common.OPTIONS.boot_variable_file = common.MakeTempFile() |
| 1356 | with open(common.OPTIONS.boot_variable_file, 'w') as f: |
| 1357 | f.write('ro.boot.sku_name=std,pro') |
| 1358 | |
| 1359 | source_dir = common.MakeTempDir() |
| 1360 | source_build_prop = [ |
| 1361 | 'ro.build.version.release=source-version-release', |
| 1362 | 'ro.build.id=source-build-id', |
| 1363 | 'ro.build.version.incremental=source-version-incremental', |
| 1364 | 'ro.build.type=build-type', |
| 1365 | 'ro.build.tags=build-tags', |
| 1366 | 'ro.build.version.sdk=29', |
| 1367 | 'ro.build.version.security_patch=2020', |
| 1368 | 'ro.build.date.utc=12340000' |
| 1369 | ] |
| 1370 | self.writeFiles({ |
| 1371 | 'META/misc_info.txt': '\n'.join(self.MISC_INFO), |
| 1372 | 'SYSTEM/build.prop': '\n'.join(source_build_prop), |
| 1373 | 'VENDOR/build.prop': '\n'.join(vendor_build_prop), |
| 1374 | 'VENDOR/etc/build_std.prop': |
| 1375 | 'ro.product.vendor.device=vendor-device-std', |
| 1376 | 'VENDOR/etc/build_pro.prop': |
| 1377 | 'ro.product.vendor.device=vendor-device-pro', |
| 1378 | }, source_dir) |
| 1379 | common.OPTIONS.incremental_source = source_dir |
| 1380 | |
| 1381 | target_info = common.BuildInfo(common.LoadInfoDict(self.test_dir)) |
| 1382 | source_info = common.BuildInfo(common.LoadInfoDict(source_dir)) |
| 1383 | |
| 1384 | metadata = GetPackageMetadata(target_info, source_info) |
| 1385 | self.assertEqual( |
| 1386 | 'vendor-device-pro|vendor-device-std|vendor-product-device', |
| 1387 | metadata['pre-device']) |
| 1388 | suffix = ':source-version-release/source-build-id/' \ |
| 1389 | 'source-version-incremental:build-type/build-tags' |
| 1390 | pre_fingerprints = [ |
| 1391 | 'vendor-product-brand/vendor-product-name/vendor-device-pro' |
| 1392 | '{}'.format(suffix), |
| 1393 | 'vendor-product-brand/vendor-product-name/vendor-device-std' |
| 1394 | '{}'.format(suffix), |
| 1395 | 'vendor-product-brand/vendor-product-name/vendor-product-device' |
| 1396 | '{}'.format(suffix), |
| 1397 | ] |
| 1398 | self.assertEqual('|'.join(pre_fingerprints), metadata['pre-build']) |
| 1399 | |
| 1400 | post_fingerprints = [ |
| 1401 | self.constructFingerprint( |
| 1402 | 'vendor-product-brand/vendor-product-name/vendor-device-pro'), |
| 1403 | self.constructFingerprint( |
| 1404 | 'vendor-product-brand/vendor-product-name/vendor-device-std'), |
| 1405 | self.constructFingerprint( |
| 1406 | 'vendor-product-brand/vendor-product-name/vendor-product-device'), |
| 1407 | ] |
| 1408 | self.assertEqual('|'.join(post_fingerprints), metadata['post-build']) |