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