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