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