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