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