Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2015 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 | # |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 16 | |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 17 | import copy |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 18 | import os |
Tao Bao | 17e4e61 | 2018-02-16 17:12:54 -0800 | [diff] [blame] | 19 | import subprocess |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 20 | import tempfile |
Tianjie | 20dd8f2 | 2020-04-19 15:51:16 -0700 | [diff] [blame] | 21 | import unittest |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 22 | import zipfile |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 23 | from hashlib import sha1 |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 24 | from typing import BinaryIO |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 25 | |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 26 | import common |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 27 | import test_utils |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 28 | import validate_target_files |
Abhishek Nigam | 1dfca46 | 2023-11-08 02:21:39 +0000 | [diff] [blame^] | 29 | from images import EmptyImage, DataImage |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 30 | from rangelib import RangeSet |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 31 | |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 32 | |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 33 | KiB = 1024 |
| 34 | MiB = 1024 * KiB |
| 35 | GiB = 1024 * MiB |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 36 | |
Tao Bao | 1c830bf | 2017-12-25 10:43:47 -0800 | [diff] [blame] | 37 | |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 38 | def get_2gb_file(): |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 39 | size = int(2 * GiB + 1) |
| 40 | block_size = 4 * KiB |
| 41 | step_size = 4 * MiB |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 42 | tmpfile = tempfile.NamedTemporaryFile() |
| 43 | tmpfile.truncate(size) |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 44 | for _ in range(0, size, step_size): |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 45 | tmpfile.write(os.urandom(block_size)) |
| 46 | tmpfile.seek(step_size - block_size, os.SEEK_CUR) |
| 47 | return tmpfile |
| 48 | |
| 49 | |
| 50 | def hash_file(filename): |
| 51 | sha1_hash = sha1() |
| 52 | with open(filename, "rb") as fp: |
| 53 | for data in iter(lambda: fp.read(4*MiB), b''): |
| 54 | sha1_hash.update(data) |
| 55 | return sha1_hash |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 56 | |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 57 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 58 | class BuildInfoTest(test_utils.ReleaseToolsTestCase): |
| 59 | |
Tianjie | fdda51d | 2021-05-05 14:46:35 -0700 | [diff] [blame] | 60 | TEST_INFO_FINGERPRINT_DICT = { |
| 61 | 'build.prop': common.PartitionBuildProps.FromDictionary( |
| 62 | 'system', { |
| 63 | 'ro.product.brand': 'product-brand', |
| 64 | 'ro.product.name': 'product-name', |
| 65 | 'ro.product.device': 'product-device', |
| 66 | 'ro.build.version.release': 'version-release', |
| 67 | 'ro.build.id': 'build-id', |
| 68 | 'ro.build.version.incremental': 'version-incremental', |
| 69 | 'ro.build.type': 'build-type', |
| 70 | 'ro.build.tags': 'build-tags', |
| 71 | 'ro.build.version.sdk': 30, |
| 72 | } |
| 73 | ), |
| 74 | } |
| 75 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 76 | TEST_INFO_DICT = { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 77 | 'build.prop': common.PartitionBuildProps.FromDictionary( |
| 78 | 'system', { |
| 79 | 'ro.product.device': 'product-device', |
| 80 | 'ro.product.name': 'product-name', |
| 81 | 'ro.build.fingerprint': 'build-fingerprint', |
| 82 | 'ro.build.foo': 'build-foo'} |
| 83 | ), |
| 84 | 'system.build.prop': common.PartitionBuildProps.FromDictionary( |
| 85 | 'system', { |
| 86 | 'ro.product.system.brand': 'product-brand', |
| 87 | 'ro.product.system.name': 'product-name', |
| 88 | 'ro.product.system.device': 'product-device', |
| 89 | 'ro.system.build.version.release': 'version-release', |
| 90 | 'ro.system.build.id': 'build-id', |
| 91 | 'ro.system.build.version.incremental': 'version-incremental', |
| 92 | 'ro.system.build.type': 'build-type', |
| 93 | 'ro.system.build.tags': 'build-tags', |
| 94 | 'ro.system.build.foo': 'build-foo'} |
| 95 | ), |
| 96 | 'vendor.build.prop': common.PartitionBuildProps.FromDictionary( |
| 97 | 'vendor', { |
| 98 | 'ro.product.vendor.brand': 'vendor-product-brand', |
| 99 | 'ro.product.vendor.name': 'vendor-product-name', |
| 100 | 'ro.product.vendor.device': 'vendor-product-device', |
| 101 | 'ro.vendor.build.version.release': 'vendor-version-release', |
| 102 | 'ro.vendor.build.id': 'vendor-build-id', |
| 103 | 'ro.vendor.build.version.incremental': |
| 104 | 'vendor-version-incremental', |
| 105 | 'ro.vendor.build.type': 'vendor-build-type', |
| 106 | 'ro.vendor.build.tags': 'vendor-build-tags'} |
| 107 | ), |
| 108 | 'property1': 'value1', |
| 109 | 'property2': 4096, |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST_INFO_DICT_USES_OEM_PROPS = { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 113 | 'build.prop': common.PartitionBuildProps.FromDictionary( |
| 114 | 'system', { |
| 115 | 'ro.product.name': 'product-name', |
| 116 | 'ro.build.thumbprint': 'build-thumbprint', |
| 117 | 'ro.build.bar': 'build-bar'} |
| 118 | ), |
| 119 | 'vendor.build.prop': common.PartitionBuildProps.FromDictionary( |
| 120 | 'vendor', { |
| 121 | 'ro.vendor.build.fingerprint': 'vendor-build-fingerprint'} |
| 122 | ), |
| 123 | 'property1': 'value1', |
| 124 | 'property2': 4096, |
| 125 | 'oem_fingerprint_properties': 'ro.product.device ro.product.brand', |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_OEM_DICTS = [ |
| 129 | { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 130 | 'ro.product.brand': 'brand1', |
| 131 | 'ro.product.device': 'device1', |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 132 | }, |
| 133 | { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 134 | 'ro.product.brand': 'brand2', |
| 135 | 'ro.product.device': 'device2', |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 136 | }, |
| 137 | { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 138 | 'ro.product.brand': 'brand3', |
| 139 | 'ro.product.device': 'device3', |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 140 | }, |
| 141 | ] |
| 142 | |
Steven Laver | 8e2086e | 2020-04-27 16:26:31 -0700 | [diff] [blame] | 143 | TEST_INFO_DICT_PROPERTY_SOURCE_ORDER = { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 144 | 'build.prop': common.PartitionBuildProps.FromDictionary( |
| 145 | 'system', { |
| 146 | 'ro.build.fingerprint': 'build-fingerprint', |
| 147 | 'ro.product.property_source_order': |
| 148 | 'product,odm,vendor,system_ext,system'} |
| 149 | ), |
| 150 | 'system.build.prop': common.PartitionBuildProps.FromDictionary( |
| 151 | 'system', { |
| 152 | 'ro.product.system.device': 'system-product-device'} |
| 153 | ), |
| 154 | 'vendor.build.prop': common.PartitionBuildProps.FromDictionary( |
| 155 | 'vendor', { |
| 156 | 'ro.product.vendor.device': 'vendor-product-device'} |
| 157 | ), |
Steven Laver | 8e2086e | 2020-04-27 16:26:31 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | TEST_INFO_DICT_PROPERTY_SOURCE_ORDER_ANDROID_10 = { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 161 | 'build.prop': common.PartitionBuildProps.FromDictionary( |
| 162 | 'system', { |
| 163 | 'ro.build.fingerprint': 'build-fingerprint', |
| 164 | 'ro.product.property_source_order': |
| 165 | 'product,product_services,odm,vendor,system', |
| 166 | 'ro.build.version.release': '10', |
| 167 | 'ro.build.version.codename': 'REL'} |
| 168 | ), |
| 169 | 'system.build.prop': common.PartitionBuildProps.FromDictionary( |
| 170 | 'system', { |
| 171 | 'ro.product.system.device': 'system-product-device'} |
| 172 | ), |
| 173 | 'vendor.build.prop': common.PartitionBuildProps.FromDictionary( |
| 174 | 'vendor', { |
| 175 | 'ro.product.vendor.device': 'vendor-product-device'} |
| 176 | ), |
Steven Laver | 8e2086e | 2020-04-27 16:26:31 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | TEST_INFO_DICT_PROPERTY_SOURCE_ORDER_ANDROID_9 = { |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 180 | 'build.prop': common.PartitionBuildProps.FromDictionary( |
| 181 | 'system', { |
| 182 | 'ro.product.device': 'product-device', |
| 183 | 'ro.build.fingerprint': 'build-fingerprint', |
| 184 | 'ro.build.version.release': '9', |
| 185 | 'ro.build.version.codename': 'REL'} |
| 186 | ), |
| 187 | 'system.build.prop': common.PartitionBuildProps.FromDictionary( |
| 188 | 'system', { |
| 189 | 'ro.product.system.device': 'system-product-device'} |
| 190 | ), |
| 191 | 'vendor.build.prop': common.PartitionBuildProps.FromDictionary( |
| 192 | 'vendor', { |
| 193 | 'ro.product.vendor.device': 'vendor-product-device'} |
| 194 | ), |
Steven Laver | 8e2086e | 2020-04-27 16:26:31 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 197 | def test_init(self): |
| 198 | target_info = common.BuildInfo(self.TEST_INFO_DICT, None) |
| 199 | self.assertEqual('product-device', target_info.device) |
| 200 | self.assertEqual('build-fingerprint', target_info.fingerprint) |
| 201 | self.assertFalse(target_info.is_ab) |
| 202 | self.assertIsNone(target_info.oem_props) |
| 203 | |
| 204 | def test_init_with_oem_props(self): |
| 205 | target_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 206 | self.TEST_OEM_DICTS) |
| 207 | self.assertEqual('device1', target_info.device) |
| 208 | self.assertEqual('brand1/product-name/device1:build-thumbprint', |
| 209 | target_info.fingerprint) |
| 210 | |
| 211 | # Swap the order in oem_dicts, which would lead to different BuildInfo. |
| 212 | oem_dicts = copy.copy(self.TEST_OEM_DICTS) |
| 213 | oem_dicts[0], oem_dicts[2] = oem_dicts[2], oem_dicts[0] |
| 214 | target_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 215 | oem_dicts) |
| 216 | self.assertEqual('device3', target_info.device) |
| 217 | self.assertEqual('brand3/product-name/device3:build-thumbprint', |
| 218 | target_info.fingerprint) |
| 219 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 220 | def test_init_badFingerprint(self): |
| 221 | info_dict = copy.deepcopy(self.TEST_INFO_DICT) |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 222 | info_dict['build.prop'].build_props[ |
| 223 | 'ro.build.fingerprint'] = 'bad fingerprint' |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 224 | self.assertRaises(ValueError, common.BuildInfo, info_dict, None) |
| 225 | |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 226 | info_dict['build.prop'].build_props[ |
| 227 | 'ro.build.fingerprint'] = 'bad\x80fingerprint' |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 228 | self.assertRaises(ValueError, common.BuildInfo, info_dict, None) |
| 229 | |
Tianjie | fdda51d | 2021-05-05 14:46:35 -0700 | [diff] [blame] | 230 | def test_init_goodFingerprint(self): |
| 231 | info_dict = copy.deepcopy(self.TEST_INFO_FINGERPRINT_DICT) |
| 232 | build_info = common.BuildInfo(info_dict) |
| 233 | self.assertEqual( |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 234 | 'product-brand/product-name/product-device:version-release/build-id/' |
| 235 | 'version-incremental:build-type/build-tags', build_info.fingerprint) |
Tianjie | fdda51d | 2021-05-05 14:46:35 -0700 | [diff] [blame] | 236 | |
| 237 | build_props = info_dict['build.prop'].build_props |
| 238 | del build_props['ro.build.id'] |
| 239 | build_props['ro.build.legacy.id'] = 'legacy-build-id' |
| 240 | build_info = common.BuildInfo(info_dict, use_legacy_id=True) |
| 241 | self.assertEqual( |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 242 | 'product-brand/product-name/product-device:version-release/' |
| 243 | 'legacy-build-id/version-incremental:build-type/build-tags', |
| 244 | build_info.fingerprint) |
Tianjie | fdda51d | 2021-05-05 14:46:35 -0700 | [diff] [blame] | 245 | |
| 246 | self.assertRaises(common.ExternalError, common.BuildInfo, info_dict, None, |
| 247 | False) |
| 248 | |
| 249 | info_dict['avb_enable'] = 'true' |
| 250 | info_dict['vbmeta_digest'] = 'abcde12345' |
| 251 | build_info = common.BuildInfo(info_dict, use_legacy_id=False) |
| 252 | self.assertEqual( |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 253 | 'product-brand/product-name/product-device:version-release/' |
| 254 | 'legacy-build-id.abcde123/version-incremental:build-type/build-tags', |
| 255 | build_info.fingerprint) |
Tianjie | fdda51d | 2021-05-05 14:46:35 -0700 | [diff] [blame] | 256 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 257 | def test___getitem__(self): |
| 258 | target_info = common.BuildInfo(self.TEST_INFO_DICT, None) |
| 259 | self.assertEqual('value1', target_info['property1']) |
| 260 | self.assertEqual(4096, target_info['property2']) |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 261 | self.assertEqual('build-foo', |
| 262 | target_info['build.prop'].GetProp('ro.build.foo')) |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 263 | |
| 264 | def test___getitem__with_oem_props(self): |
| 265 | target_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 266 | self.TEST_OEM_DICTS) |
| 267 | self.assertEqual('value1', target_info['property1']) |
| 268 | self.assertEqual(4096, target_info['property2']) |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 269 | self.assertIsNone(target_info['build.prop'].GetProp('ro.build.foo')) |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 270 | |
| 271 | def test___setitem__(self): |
| 272 | target_info = common.BuildInfo(copy.deepcopy(self.TEST_INFO_DICT), None) |
| 273 | self.assertEqual('value1', target_info['property1']) |
| 274 | target_info['property1'] = 'value2' |
| 275 | self.assertEqual('value2', target_info['property1']) |
| 276 | |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 277 | self.assertEqual('build-foo', |
| 278 | target_info['build.prop'].GetProp('ro.build.foo')) |
| 279 | target_info['build.prop'].build_props['ro.build.foo'] = 'build-bar' |
| 280 | self.assertEqual('build-bar', |
| 281 | target_info['build.prop'].GetProp('ro.build.foo')) |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 282 | |
| 283 | def test_get(self): |
| 284 | target_info = common.BuildInfo(self.TEST_INFO_DICT, None) |
| 285 | self.assertEqual('value1', target_info.get('property1')) |
| 286 | self.assertEqual(4096, target_info.get('property2')) |
| 287 | self.assertEqual(4096, target_info.get('property2', 1024)) |
| 288 | self.assertEqual(1024, target_info.get('property-nonexistent', 1024)) |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 289 | self.assertEqual('build-foo', |
| 290 | target_info.get('build.prop').GetProp('ro.build.foo')) |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 291 | |
| 292 | def test_get_with_oem_props(self): |
| 293 | target_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 294 | self.TEST_OEM_DICTS) |
| 295 | self.assertEqual('value1', target_info.get('property1')) |
| 296 | self.assertEqual(4096, target_info.get('property2')) |
| 297 | self.assertEqual(4096, target_info.get('property2', 1024)) |
| 298 | self.assertEqual(1024, target_info.get('property-nonexistent', 1024)) |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 299 | self.assertIsNone(target_info.get('build.prop').GetProp('ro.build.foo')) |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 300 | |
| 301 | def test_items(self): |
| 302 | target_info = common.BuildInfo(self.TEST_INFO_DICT, None) |
| 303 | items = target_info.items() |
| 304 | self.assertIn(('property1', 'value1'), items) |
| 305 | self.assertIn(('property2', 4096), items) |
| 306 | |
| 307 | def test_GetBuildProp(self): |
| 308 | target_info = common.BuildInfo(self.TEST_INFO_DICT, None) |
| 309 | self.assertEqual('build-foo', target_info.GetBuildProp('ro.build.foo')) |
| 310 | self.assertRaises(common.ExternalError, target_info.GetBuildProp, |
| 311 | 'ro.build.nonexistent') |
| 312 | |
| 313 | def test_GetBuildProp_with_oem_props(self): |
| 314 | target_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 315 | self.TEST_OEM_DICTS) |
| 316 | self.assertEqual('build-bar', target_info.GetBuildProp('ro.build.bar')) |
| 317 | self.assertRaises(common.ExternalError, target_info.GetBuildProp, |
| 318 | 'ro.build.nonexistent') |
| 319 | |
Daniel Norman | d5fe862 | 2020-01-08 17:01:11 -0800 | [diff] [blame] | 320 | def test_GetPartitionFingerprint(self): |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 321 | target_info = common.BuildInfo(self.TEST_INFO_DICT, None) |
Daniel Norman | d5fe862 | 2020-01-08 17:01:11 -0800 | [diff] [blame] | 322 | self.assertEqual( |
| 323 | target_info.GetPartitionFingerprint('vendor'), |
| 324 | 'vendor-product-brand/vendor-product-name/vendor-product-device' |
| 325 | ':vendor-version-release/vendor-build-id/vendor-version-incremental' |
| 326 | ':vendor-build-type/vendor-build-tags') |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 327 | |
Daniel Norman | d5fe862 | 2020-01-08 17:01:11 -0800 | [diff] [blame] | 328 | def test_GetPartitionFingerprint_system_other_uses_system(self): |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 329 | target_info = common.BuildInfo(self.TEST_INFO_DICT, None) |
Daniel Norman | d5fe862 | 2020-01-08 17:01:11 -0800 | [diff] [blame] | 330 | self.assertEqual( |
| 331 | target_info.GetPartitionFingerprint('system_other'), |
| 332 | target_info.GetPartitionFingerprint('system')) |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 333 | |
Daniel Norman | d5fe862 | 2020-01-08 17:01:11 -0800 | [diff] [blame] | 334 | def test_GetPartitionFingerprint_uses_fingerprint_prop_if_available(self): |
| 335 | info_dict = copy.deepcopy(self.TEST_INFO_DICT) |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 336 | info_dict['vendor.build.prop'].build_props[ |
| 337 | 'ro.vendor.build.fingerprint'] = 'vendor:fingerprint' |
Daniel Norman | d5fe862 | 2020-01-08 17:01:11 -0800 | [diff] [blame] | 338 | target_info = common.BuildInfo(info_dict, None) |
| 339 | self.assertEqual( |
| 340 | target_info.GetPartitionFingerprint('vendor'), |
| 341 | 'vendor:fingerprint') |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 342 | |
| 343 | def test_WriteMountOemScript(self): |
| 344 | target_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 345 | self.TEST_OEM_DICTS) |
| 346 | script_writer = test_utils.MockScriptWriter() |
| 347 | target_info.WriteMountOemScript(script_writer) |
| 348 | self.assertEqual([('Mount', '/oem', None)], script_writer.lines) |
| 349 | |
| 350 | def test_WriteDeviceAssertions(self): |
| 351 | target_info = common.BuildInfo(self.TEST_INFO_DICT, None) |
| 352 | script_writer = test_utils.MockScriptWriter() |
| 353 | target_info.WriteDeviceAssertions(script_writer, False) |
| 354 | self.assertEqual([('AssertDevice', 'product-device')], script_writer.lines) |
| 355 | |
| 356 | def test_WriteDeviceAssertions_with_oem_props(self): |
| 357 | target_info = common.BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 358 | self.TEST_OEM_DICTS) |
| 359 | script_writer = test_utils.MockScriptWriter() |
| 360 | target_info.WriteDeviceAssertions(script_writer, False) |
| 361 | self.assertEqual( |
| 362 | [ |
| 363 | ('AssertOemProperty', 'ro.product.device', |
| 364 | ['device1', 'device2', 'device3'], False), |
| 365 | ('AssertOemProperty', 'ro.product.brand', |
| 366 | ['brand1', 'brand2', 'brand3'], False), |
| 367 | ], |
| 368 | script_writer.lines) |
| 369 | |
Steven Laver | 8e2086e | 2020-04-27 16:26:31 -0700 | [diff] [blame] | 370 | def test_ResolveRoProductProperty_FromVendor(self): |
| 371 | info_dict = copy.deepcopy(self.TEST_INFO_DICT_PROPERTY_SOURCE_ORDER) |
| 372 | info = common.BuildInfo(info_dict, None) |
| 373 | self.assertEqual('vendor-product-device', |
| 374 | info.GetBuildProp('ro.product.device')) |
| 375 | |
| 376 | def test_ResolveRoProductProperty_FromSystem(self): |
| 377 | info_dict = copy.deepcopy(self.TEST_INFO_DICT_PROPERTY_SOURCE_ORDER) |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 378 | del info_dict['vendor.build.prop'].build_props['ro.product.vendor.device'] |
Steven Laver | 8e2086e | 2020-04-27 16:26:31 -0700 | [diff] [blame] | 379 | info = common.BuildInfo(info_dict, None) |
| 380 | self.assertEqual('system-product-device', |
| 381 | info.GetBuildProp('ro.product.device')) |
| 382 | |
| 383 | def test_ResolveRoProductProperty_InvalidPropertySearchOrder(self): |
| 384 | info_dict = copy.deepcopy(self.TEST_INFO_DICT_PROPERTY_SOURCE_ORDER) |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 385 | info_dict['build.prop'].build_props[ |
| 386 | 'ro.product.property_source_order'] = 'bad-source' |
Steven Laver | 8e2086e | 2020-04-27 16:26:31 -0700 | [diff] [blame] | 387 | with self.assertRaisesRegexp(common.ExternalError, |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 388 | 'Invalid ro.product.property_source_order'): |
Steven Laver | 8e2086e | 2020-04-27 16:26:31 -0700 | [diff] [blame] | 389 | info = common.BuildInfo(info_dict, None) |
| 390 | info.GetBuildProp('ro.product.device') |
| 391 | |
| 392 | def test_ResolveRoProductProperty_Android10PropertySearchOrder(self): |
| 393 | info_dict = copy.deepcopy( |
| 394 | self.TEST_INFO_DICT_PROPERTY_SOURCE_ORDER_ANDROID_10) |
| 395 | info = common.BuildInfo(info_dict, None) |
| 396 | self.assertEqual('vendor-product-device', |
| 397 | info.GetBuildProp('ro.product.device')) |
| 398 | |
| 399 | def test_ResolveRoProductProperty_Android9PropertySearchOrder(self): |
| 400 | info_dict = copy.deepcopy( |
| 401 | self.TEST_INFO_DICT_PROPERTY_SOURCE_ORDER_ANDROID_9) |
| 402 | info = common.BuildInfo(info_dict, None) |
| 403 | self.assertEqual('product-device', |
| 404 | info.GetBuildProp('ro.product.device')) |
| 405 | |
Tao Bao | 1c320f8 | 2019-10-04 23:25:12 -0700 | [diff] [blame] | 406 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 407 | class CommonZipTest(test_utils.ReleaseToolsTestCase): |
| 408 | |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 409 | def _verify(self, zip_file, zip_file_name, arcname, expected_hash, |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 410 | test_file_name=None, expected_stat=None, expected_mode=0o644, |
| 411 | expected_compress_type=zipfile.ZIP_STORED): |
| 412 | # Verify the stat if present. |
| 413 | if test_file_name is not None: |
| 414 | new_stat = os.stat(test_file_name) |
| 415 | self.assertEqual(int(expected_stat.st_mode), int(new_stat.st_mode)) |
| 416 | self.assertEqual(int(expected_stat.st_mtime), int(new_stat.st_mtime)) |
| 417 | |
| 418 | # Reopen the zip file to verify. |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 419 | zip_file = zipfile.ZipFile(zip_file_name, "r", allowZip64=True) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 420 | |
| 421 | # Verify the timestamp. |
| 422 | info = zip_file.getinfo(arcname) |
| 423 | self.assertEqual(info.date_time, (2009, 1, 1, 0, 0, 0)) |
| 424 | |
| 425 | # Verify the file mode. |
| 426 | mode = (info.external_attr >> 16) & 0o777 |
| 427 | self.assertEqual(mode, expected_mode) |
| 428 | |
| 429 | # Verify the compress type. |
| 430 | self.assertEqual(info.compress_type, expected_compress_type) |
| 431 | |
| 432 | # Verify the zip contents. |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 433 | entry = zip_file.open(arcname) |
| 434 | sha1_hash = sha1() |
Tao Bao | c1a1ec3 | 2019-06-18 16:29:37 -0700 | [diff] [blame] | 435 | for chunk in iter(lambda: entry.read(4 * MiB), b''): |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 436 | sha1_hash.update(chunk) |
| 437 | self.assertEqual(expected_hash, sha1_hash.hexdigest()) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 438 | self.assertIsNone(zip_file.testzip()) |
| 439 | |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 440 | def _test_ZipWrite(self, contents, extra_zipwrite_args=None): |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 441 | with tempfile.NamedTemporaryFile() as test_file: |
| 442 | test_file_name = test_file.name |
| 443 | for data in contents: |
| 444 | test_file.write(bytes(data)) |
| 445 | return self._test_ZipWriteFile(test_file_name, extra_zipwrite_args) |
| 446 | |
| 447 | def _test_ZipWriteFile(self, test_file_name, extra_zipwrite_args=None): |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 448 | extra_zipwrite_args = dict(extra_zipwrite_args or {}) |
| 449 | |
| 450 | test_file = tempfile.NamedTemporaryFile(delete=False) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 451 | test_file_name = test_file.name |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 452 | |
| 453 | zip_file = tempfile.NamedTemporaryFile(delete=False) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 454 | zip_file_name = zip_file.name |
| 455 | |
| 456 | # File names within an archive strip the leading slash. |
| 457 | arcname = extra_zipwrite_args.get("arcname", test_file_name) |
| 458 | if arcname[0] == "/": |
| 459 | arcname = arcname[1:] |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 460 | sha1_hash = hash_file(test_file_name) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 461 | |
| 462 | zip_file.close() |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 463 | zip_file = zipfile.ZipFile(zip_file_name, "w", allowZip64=True) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 464 | |
| 465 | try: |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 466 | expected_mode = extra_zipwrite_args.get("perms", 0o644) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 467 | expected_compress_type = extra_zipwrite_args.get("compress_type", |
| 468 | zipfile.ZIP_STORED) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 469 | |
Kelvin Zhang | ea84d42 | 2023-04-11 12:50:34 -0700 | [diff] [blame] | 470 | # Arbitrary timestamp, just to make sure common.ZipWrite() restores |
| 471 | # the timestamp after writing. |
| 472 | os.utime(test_file_name, (1234567, 1234567)) |
| 473 | expected_stat = os.stat(test_file_name) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 474 | common.ZipWrite(zip_file, test_file_name, **extra_zipwrite_args) |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 475 | common.ZipClose(zip_file) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 476 | |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 477 | self._verify(zip_file, zip_file_name, arcname, sha1_hash.hexdigest(), |
| 478 | test_file_name, expected_stat, expected_mode, |
| 479 | expected_compress_type) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 480 | finally: |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 481 | os.remove(zip_file_name) |
| 482 | |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 483 | def _test_ZipWriteStr(self, zinfo_or_arcname, contents, extra_args=None): |
| 484 | extra_args = dict(extra_args or {}) |
| 485 | |
| 486 | zip_file = tempfile.NamedTemporaryFile(delete=False) |
| 487 | zip_file_name = zip_file.name |
| 488 | zip_file.close() |
| 489 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 490 | zip_file = zipfile.ZipFile(zip_file_name, "w", allowZip64=True) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 491 | |
| 492 | try: |
| 493 | expected_compress_type = extra_args.get("compress_type", |
| 494 | zipfile.ZIP_STORED) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 495 | if not isinstance(zinfo_or_arcname, zipfile.ZipInfo): |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 496 | arcname = zinfo_or_arcname |
| 497 | expected_mode = extra_args.get("perms", 0o644) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 498 | else: |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 499 | arcname = zinfo_or_arcname.filename |
Tao Bao | c1a1ec3 | 2019-06-18 16:29:37 -0700 | [diff] [blame] | 500 | if zinfo_or_arcname.external_attr: |
| 501 | zinfo_perms = zinfo_or_arcname.external_attr >> 16 |
| 502 | else: |
| 503 | zinfo_perms = 0o600 |
| 504 | expected_mode = extra_args.get("perms", zinfo_perms) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 505 | |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 506 | common.ZipWriteStr(zip_file, zinfo_or_arcname, contents, **extra_args) |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 507 | common.ZipClose(zip_file) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 508 | |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 509 | self._verify(zip_file, zip_file_name, arcname, sha1(contents).hexdigest(), |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 510 | expected_mode=expected_mode, |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 511 | expected_compress_type=expected_compress_type) |
| 512 | finally: |
| 513 | os.remove(zip_file_name) |
| 514 | |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 515 | def _test_ZipWriteStr_large_file(self, large_file: BinaryIO, small, extra_args=None): |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 516 | extra_args = dict(extra_args or {}) |
| 517 | |
| 518 | zip_file = tempfile.NamedTemporaryFile(delete=False) |
| 519 | zip_file_name = zip_file.name |
| 520 | |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 521 | test_file_name = large_file.name |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 522 | |
| 523 | arcname_large = test_file_name |
| 524 | arcname_small = "bar" |
| 525 | |
| 526 | # File names within an archive strip the leading slash. |
| 527 | if arcname_large[0] == "/": |
| 528 | arcname_large = arcname_large[1:] |
| 529 | |
| 530 | zip_file.close() |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 531 | zip_file = zipfile.ZipFile(zip_file_name, "w", allowZip64=True) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 532 | |
| 533 | try: |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 534 | sha1_hash = hash_file(test_file_name) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 535 | |
Kelvin Zhang | ea84d42 | 2023-04-11 12:50:34 -0700 | [diff] [blame] | 536 | # Arbitrary timestamp, just to make sure common.ZipWrite() restores |
| 537 | # the timestamp after writing. |
| 538 | os.utime(test_file_name, (1234567, 1234567)) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 539 | expected_stat = os.stat(test_file_name) |
| 540 | expected_mode = 0o644 |
| 541 | expected_compress_type = extra_args.get("compress_type", |
| 542 | zipfile.ZIP_STORED) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 543 | |
| 544 | common.ZipWrite(zip_file, test_file_name, **extra_args) |
| 545 | common.ZipWriteStr(zip_file, arcname_small, small, **extra_args) |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 546 | common.ZipClose(zip_file) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 547 | |
| 548 | # Verify the contents written by ZipWrite(). |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 549 | self._verify(zip_file, zip_file_name, arcname_large, |
| 550 | sha1_hash.hexdigest(), test_file_name, expected_stat, |
| 551 | expected_mode, expected_compress_type) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 552 | |
| 553 | # Verify the contents written by ZipWriteStr(). |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 554 | self._verify(zip_file, zip_file_name, arcname_small, |
| 555 | sha1(small).hexdigest(), |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 556 | expected_compress_type=expected_compress_type) |
| 557 | finally: |
| 558 | os.remove(zip_file_name) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 559 | |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 560 | def _test_reset_ZIP64_LIMIT(self, func, *args): |
| 561 | default_limit = (1 << 31) - 1 |
| 562 | self.assertEqual(default_limit, zipfile.ZIP64_LIMIT) |
| 563 | func(*args) |
| 564 | self.assertEqual(default_limit, zipfile.ZIP64_LIMIT) |
| 565 | |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 566 | def test_ZipWrite(self): |
| 567 | file_contents = os.urandom(1024) |
| 568 | self._test_ZipWrite(file_contents) |
| 569 | |
| 570 | def test_ZipWrite_with_opts(self): |
| 571 | file_contents = os.urandom(1024) |
| 572 | self._test_ZipWrite(file_contents, { |
| 573 | "arcname": "foobar", |
| 574 | "perms": 0o777, |
| 575 | "compress_type": zipfile.ZIP_DEFLATED, |
| 576 | }) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 577 | self._test_ZipWrite(file_contents, { |
| 578 | "arcname": "foobar", |
| 579 | "perms": 0o700, |
| 580 | "compress_type": zipfile.ZIP_STORED, |
| 581 | }) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 582 | |
| 583 | def test_ZipWrite_large_file(self): |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 584 | with get_2gb_file() as tmpfile: |
| 585 | self._test_ZipWriteFile(tmpfile.name, { |
| 586 | "compress_type": zipfile.ZIP_DEFLATED, |
| 587 | }) |
Dan Albert | 8e0178d | 2015-01-27 15:53:15 -0800 | [diff] [blame] | 588 | |
| 589 | def test_ZipWrite_resets_ZIP64_LIMIT(self): |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 590 | self._test_reset_ZIP64_LIMIT(self._test_ZipWrite, "") |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 591 | |
| 592 | def test_ZipWriteStr(self): |
| 593 | random_string = os.urandom(1024) |
| 594 | # Passing arcname |
| 595 | self._test_ZipWriteStr("foo", random_string) |
| 596 | |
| 597 | # Passing zinfo |
| 598 | zinfo = zipfile.ZipInfo(filename="foo") |
| 599 | self._test_ZipWriteStr(zinfo, random_string) |
| 600 | |
| 601 | # Timestamp in the zinfo should be overwritten. |
| 602 | zinfo.date_time = (2015, 3, 1, 15, 30, 0) |
| 603 | self._test_ZipWriteStr(zinfo, random_string) |
| 604 | |
| 605 | def test_ZipWriteStr_with_opts(self): |
| 606 | random_string = os.urandom(1024) |
| 607 | # Passing arcname |
| 608 | self._test_ZipWriteStr("foo", random_string, { |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 609 | "perms": 0o700, |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 610 | "compress_type": zipfile.ZIP_DEFLATED, |
| 611 | }) |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 612 | self._test_ZipWriteStr("bar", random_string, { |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 613 | "compress_type": zipfile.ZIP_STORED, |
| 614 | }) |
| 615 | |
| 616 | # Passing zinfo |
| 617 | zinfo = zipfile.ZipInfo(filename="foo") |
| 618 | self._test_ZipWriteStr(zinfo, random_string, { |
| 619 | "compress_type": zipfile.ZIP_DEFLATED, |
| 620 | }) |
| 621 | self._test_ZipWriteStr(zinfo, random_string, { |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 622 | "perms": 0o600, |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 623 | "compress_type": zipfile.ZIP_STORED, |
| 624 | }) |
Tao Bao | c1a1ec3 | 2019-06-18 16:29:37 -0700 | [diff] [blame] | 625 | self._test_ZipWriteStr(zinfo, random_string, { |
| 626 | "perms": 0o000, |
| 627 | "compress_type": zipfile.ZIP_STORED, |
| 628 | }) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 629 | |
| 630 | def test_ZipWriteStr_large_file(self): |
| 631 | # zipfile.writestr() doesn't work when the str size is over 2GiB even with |
| 632 | # the workaround. We will only test the case of writing a string into a |
| 633 | # large archive. |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 634 | short_string = os.urandom(1024) |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 635 | with get_2gb_file() as large_file: |
| 636 | self._test_ZipWriteStr_large_file(large_file, short_string, { |
| 637 | "compress_type": zipfile.ZIP_DEFLATED, |
| 638 | }) |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 639 | |
| 640 | def test_ZipWriteStr_resets_ZIP64_LIMIT(self): |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 641 | self._test_reset_ZIP64_LIMIT(self._test_ZipWriteStr, 'foo', b'') |
Tao Bao | f3282b4 | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 642 | zinfo = zipfile.ZipInfo(filename="foo") |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 643 | self._test_reset_ZIP64_LIMIT(self._test_ZipWriteStr, zinfo, b'') |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 644 | |
| 645 | def test_bug21309935(self): |
| 646 | zip_file = tempfile.NamedTemporaryFile(delete=False) |
| 647 | zip_file_name = zip_file.name |
| 648 | zip_file.close() |
| 649 | |
| 650 | try: |
| 651 | random_string = os.urandom(1024) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 652 | zip_file = zipfile.ZipFile(zip_file_name, "w", allowZip64=True) |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 653 | # Default perms should be 0o644 when passing the filename. |
| 654 | common.ZipWriteStr(zip_file, "foo", random_string) |
| 655 | # Honor the specified perms. |
| 656 | common.ZipWriteStr(zip_file, "bar", random_string, perms=0o755) |
| 657 | # The perms in zinfo should be untouched. |
| 658 | zinfo = zipfile.ZipInfo(filename="baz") |
| 659 | zinfo.external_attr = 0o740 << 16 |
| 660 | common.ZipWriteStr(zip_file, zinfo, random_string) |
| 661 | # Explicitly specified perms has the priority. |
| 662 | zinfo = zipfile.ZipInfo(filename="qux") |
| 663 | zinfo.external_attr = 0o700 << 16 |
| 664 | common.ZipWriteStr(zip_file, zinfo, random_string, perms=0o400) |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 665 | common.ZipClose(zip_file) |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 666 | |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 667 | self._verify(zip_file, zip_file_name, "foo", |
| 668 | sha1(random_string).hexdigest(), |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 669 | expected_mode=0o644) |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 670 | self._verify(zip_file, zip_file_name, "bar", |
| 671 | sha1(random_string).hexdigest(), |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 672 | expected_mode=0o755) |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 673 | self._verify(zip_file, zip_file_name, "baz", |
| 674 | sha1(random_string).hexdigest(), |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 675 | expected_mode=0o740) |
Tao Bao | 31b0807 | 2017-11-08 15:50:59 -0800 | [diff] [blame] | 676 | self._verify(zip_file, zip_file_name, "qux", |
| 677 | sha1(random_string).hexdigest(), |
Tao Bao | 58c1b96 | 2015-05-20 09:32:18 -0700 | [diff] [blame] | 678 | expected_mode=0o400) |
| 679 | finally: |
| 680 | os.remove(zip_file_name) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 681 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 682 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 683 | def test_ZipDelete(self): |
| 684 | zip_file = tempfile.NamedTemporaryFile(delete=False, suffix='.zip') |
| 685 | output_zip = zipfile.ZipFile(zip_file.name, 'w', |
| 686 | compression=zipfile.ZIP_DEFLATED) |
| 687 | with tempfile.NamedTemporaryFile() as entry_file: |
| 688 | entry_file.write(os.urandom(1024)) |
| 689 | common.ZipWrite(output_zip, entry_file.name, arcname='Test1') |
| 690 | common.ZipWrite(output_zip, entry_file.name, arcname='Test2') |
| 691 | common.ZipWrite(output_zip, entry_file.name, arcname='Test3') |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 692 | common.ZipClose(output_zip) |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 693 | zip_file.close() |
| 694 | |
| 695 | try: |
| 696 | common.ZipDelete(zip_file.name, 'Test2') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 697 | with zipfile.ZipFile(zip_file.name, 'r', allowZip64=True) as check_zip: |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 698 | entries = check_zip.namelist() |
| 699 | self.assertTrue('Test1' in entries) |
| 700 | self.assertFalse('Test2' in entries) |
| 701 | self.assertTrue('Test3' in entries) |
| 702 | |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 703 | self.assertRaises( |
| 704 | common.ExternalError, common.ZipDelete, zip_file.name, 'Test2') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 705 | with zipfile.ZipFile(zip_file.name, 'r', allowZip64=True) as check_zip: |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 706 | entries = check_zip.namelist() |
| 707 | self.assertTrue('Test1' in entries) |
| 708 | self.assertFalse('Test2' in entries) |
| 709 | self.assertTrue('Test3' in entries) |
| 710 | |
| 711 | common.ZipDelete(zip_file.name, ['Test3']) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 712 | with zipfile.ZipFile(zip_file.name, 'r', allowZip64=True) as check_zip: |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 713 | entries = check_zip.namelist() |
| 714 | self.assertTrue('Test1' in entries) |
| 715 | self.assertFalse('Test2' in entries) |
| 716 | self.assertFalse('Test3' in entries) |
| 717 | |
| 718 | common.ZipDelete(zip_file.name, ['Test1', 'Test2']) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 719 | with zipfile.ZipFile(zip_file.name, 'r', allowZip64=True) as check_zip: |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 720 | entries = check_zip.namelist() |
| 721 | self.assertFalse('Test1' in entries) |
| 722 | self.assertFalse('Test2' in entries) |
| 723 | self.assertFalse('Test3' in entries) |
| 724 | finally: |
| 725 | os.remove(zip_file.name) |
| 726 | |
Tao Bao | 0ff15de | 2019-03-20 11:26:06 -0700 | [diff] [blame] | 727 | @staticmethod |
| 728 | def _test_UnzipTemp_createZipFile(): |
| 729 | zip_file = common.MakeTempFile(suffix='.zip') |
| 730 | output_zip = zipfile.ZipFile( |
| 731 | zip_file, 'w', compression=zipfile.ZIP_DEFLATED) |
| 732 | contents = os.urandom(1024) |
| 733 | with tempfile.NamedTemporaryFile() as entry_file: |
| 734 | entry_file.write(contents) |
| 735 | common.ZipWrite(output_zip, entry_file.name, arcname='Test1') |
| 736 | common.ZipWrite(output_zip, entry_file.name, arcname='Test2') |
| 737 | common.ZipWrite(output_zip, entry_file.name, arcname='Foo3') |
| 738 | common.ZipWrite(output_zip, entry_file.name, arcname='Bar4') |
| 739 | common.ZipWrite(output_zip, entry_file.name, arcname='Dir5/Baz5') |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 740 | common.ZipClose(output_zip) |
| 741 | common.ZipClose(output_zip) |
Tao Bao | 0ff15de | 2019-03-20 11:26:06 -0700 | [diff] [blame] | 742 | return zip_file |
| 743 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 744 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 0ff15de | 2019-03-20 11:26:06 -0700 | [diff] [blame] | 745 | def test_UnzipTemp(self): |
| 746 | zip_file = self._test_UnzipTemp_createZipFile() |
| 747 | unzipped_dir = common.UnzipTemp(zip_file) |
| 748 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Test1'))) |
| 749 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Test2'))) |
| 750 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Foo3'))) |
| 751 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Bar4'))) |
| 752 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Dir5/Baz5'))) |
| 753 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 754 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 0ff15de | 2019-03-20 11:26:06 -0700 | [diff] [blame] | 755 | def test_UnzipTemp_withPatterns(self): |
| 756 | zip_file = self._test_UnzipTemp_createZipFile() |
| 757 | |
| 758 | unzipped_dir = common.UnzipTemp(zip_file, ['Test1']) |
| 759 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Test1'))) |
| 760 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Test2'))) |
| 761 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Foo3'))) |
| 762 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Bar4'))) |
| 763 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Dir5/Baz5'))) |
| 764 | |
| 765 | unzipped_dir = common.UnzipTemp(zip_file, ['Test1', 'Foo3']) |
| 766 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Test1'))) |
| 767 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Test2'))) |
| 768 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Foo3'))) |
| 769 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Bar4'))) |
| 770 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Dir5/Baz5'))) |
| 771 | |
| 772 | unzipped_dir = common.UnzipTemp(zip_file, ['Test*', 'Foo3*']) |
| 773 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Test1'))) |
| 774 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Test2'))) |
| 775 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Foo3'))) |
| 776 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Bar4'))) |
| 777 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Dir5/Baz5'))) |
| 778 | |
| 779 | unzipped_dir = common.UnzipTemp(zip_file, ['*Test1', '*Baz*']) |
| 780 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Test1'))) |
| 781 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Test2'))) |
| 782 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Foo3'))) |
| 783 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Bar4'))) |
| 784 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Dir5/Baz5'))) |
| 785 | |
| 786 | def test_UnzipTemp_withEmptyPatterns(self): |
| 787 | zip_file = self._test_UnzipTemp_createZipFile() |
| 788 | unzipped_dir = common.UnzipTemp(zip_file, []) |
| 789 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Test1'))) |
| 790 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Test2'))) |
| 791 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Foo3'))) |
| 792 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Bar4'))) |
| 793 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Dir5/Baz5'))) |
| 794 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 795 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 0ff15de | 2019-03-20 11:26:06 -0700 | [diff] [blame] | 796 | def test_UnzipTemp_withPartiallyMatchingPatterns(self): |
| 797 | zip_file = self._test_UnzipTemp_createZipFile() |
| 798 | unzipped_dir = common.UnzipTemp(zip_file, ['Test*', 'Nonexistent*']) |
| 799 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Test1'))) |
| 800 | self.assertTrue(os.path.exists(os.path.join(unzipped_dir, 'Test2'))) |
| 801 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Foo3'))) |
| 802 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Bar4'))) |
| 803 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Dir5/Baz5'))) |
| 804 | |
| 805 | def test_UnzipTemp_withNoMatchingPatterns(self): |
| 806 | zip_file = self._test_UnzipTemp_createZipFile() |
| 807 | unzipped_dir = common.UnzipTemp(zip_file, ['Foo4', 'Nonexistent*']) |
| 808 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Test1'))) |
| 809 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Test2'))) |
| 810 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Foo3'))) |
| 811 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Bar4'))) |
| 812 | self.assertFalse(os.path.exists(os.path.join(unzipped_dir, 'Dir5/Baz5'))) |
| 813 | |
Tao Bao | 89d7ab2 | 2017-12-14 17:05:33 -0800 | [diff] [blame] | 814 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 815 | class CommonApkUtilsTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 816 | """Tests the APK utils related functions.""" |
| 817 | |
| 818 | APKCERTS_TXT1 = ( |
| 819 | 'name="RecoveryLocalizer.apk" certificate="certs/devkey.x509.pem"' |
| 820 | ' private_key="certs/devkey.pk8"\n' |
| 821 | 'name="Settings.apk"' |
Dan Willemsen | 0ab1be6 | 2019-04-09 21:35:37 -0700 | [diff] [blame] | 822 | ' certificate="build/make/target/product/security/platform.x509.pem"' |
| 823 | ' private_key="build/make/target/product/security/platform.pk8"\n' |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 824 | 'name="TV.apk" certificate="PRESIGNED" private_key=""\n' |
| 825 | ) |
| 826 | |
| 827 | APKCERTS_CERTMAP1 = { |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 828 | 'RecoveryLocalizer.apk': 'certs/devkey', |
| 829 | 'Settings.apk': 'build/make/target/product/security/platform', |
| 830 | 'TV.apk': 'PRESIGNED', |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | APKCERTS_TXT2 = ( |
| 834 | 'name="Compressed1.apk" certificate="certs/compressed1.x509.pem"' |
| 835 | ' private_key="certs/compressed1.pk8" compressed="gz"\n' |
| 836 | 'name="Compressed2a.apk" certificate="certs/compressed2.x509.pem"' |
| 837 | ' private_key="certs/compressed2.pk8" compressed="gz"\n' |
| 838 | 'name="Compressed2b.apk" certificate="certs/compressed2.x509.pem"' |
| 839 | ' private_key="certs/compressed2.pk8" compressed="gz"\n' |
| 840 | 'name="Compressed3.apk" certificate="certs/compressed3.x509.pem"' |
| 841 | ' private_key="certs/compressed3.pk8" compressed="gz"\n' |
| 842 | ) |
| 843 | |
| 844 | APKCERTS_CERTMAP2 = { |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 845 | 'Compressed1.apk': 'certs/compressed1', |
| 846 | 'Compressed2a.apk': 'certs/compressed2', |
| 847 | 'Compressed2b.apk': 'certs/compressed2', |
| 848 | 'Compressed3.apk': 'certs/compressed3', |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | APKCERTS_TXT3 = ( |
| 852 | 'name="Compressed4.apk" certificate="certs/compressed4.x509.pem"' |
| 853 | ' private_key="certs/compressed4.pk8" compressed="xz"\n' |
| 854 | ) |
| 855 | |
| 856 | APKCERTS_CERTMAP3 = { |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 857 | 'Compressed4.apk': 'certs/compressed4', |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Bill Peckham | 5c7b034 | 2020-04-03 15:36:23 -0700 | [diff] [blame] | 860 | # Test parsing with no optional fields, both optional fields, and only the |
| 861 | # partition optional field. |
| 862 | APKCERTS_TXT4 = ( |
| 863 | 'name="RecoveryLocalizer.apk" certificate="certs/devkey.x509.pem"' |
| 864 | ' private_key="certs/devkey.pk8"\n' |
| 865 | 'name="Settings.apk"' |
| 866 | ' certificate="build/make/target/product/security/platform.x509.pem"' |
| 867 | ' private_key="build/make/target/product/security/platform.pk8"' |
| 868 | ' compressed="gz" partition="system"\n' |
| 869 | 'name="TV.apk" certificate="PRESIGNED" private_key=""' |
| 870 | ' partition="product"\n' |
| 871 | ) |
| 872 | |
| 873 | APKCERTS_CERTMAP4 = { |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 874 | 'RecoveryLocalizer.apk': 'certs/devkey', |
| 875 | 'Settings.apk': 'build/make/target/product/security/platform', |
| 876 | 'TV.apk': 'PRESIGNED', |
Bill Peckham | 5c7b034 | 2020-04-03 15:36:23 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Tao Bao | 17e4e61 | 2018-02-16 17:12:54 -0800 | [diff] [blame] | 879 | def setUp(self): |
| 880 | self.testdata_dir = test_utils.get_testdata_dir() |
| 881 | |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 882 | @staticmethod |
| 883 | def _write_apkcerts_txt(apkcerts_txt, additional=None): |
| 884 | if additional is None: |
| 885 | additional = [] |
| 886 | target_files = common.MakeTempFile(suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 887 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 888 | target_files_zip.writestr('META/apkcerts.txt', apkcerts_txt) |
| 889 | for entry in additional: |
| 890 | target_files_zip.writestr(entry, '') |
| 891 | return target_files |
| 892 | |
| 893 | def test_ReadApkCerts_NoncompressedApks(self): |
| 894 | target_files = self._write_apkcerts_txt(self.APKCERTS_TXT1) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 895 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 896 | certmap, ext = common.ReadApkCerts(input_zip) |
| 897 | |
| 898 | self.assertDictEqual(self.APKCERTS_CERTMAP1, certmap) |
| 899 | self.assertIsNone(ext) |
| 900 | |
| 901 | def test_ReadApkCerts_CompressedApks(self): |
| 902 | # We have "installed" Compressed1.apk.gz only. Note that Compressed3.apk is |
| 903 | # not stored in '.gz' format, so it shouldn't be considered as installed. |
| 904 | target_files = self._write_apkcerts_txt( |
| 905 | self.APKCERTS_TXT2, |
| 906 | ['Compressed1.apk.gz', 'Compressed3.apk']) |
| 907 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 908 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 909 | certmap, ext = common.ReadApkCerts(input_zip) |
| 910 | |
| 911 | self.assertDictEqual(self.APKCERTS_CERTMAP2, certmap) |
| 912 | self.assertEqual('.gz', ext) |
| 913 | |
| 914 | # Alternative case with '.xz'. |
| 915 | target_files = self._write_apkcerts_txt( |
| 916 | self.APKCERTS_TXT3, ['Compressed4.apk.xz']) |
| 917 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 918 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 919 | certmap, ext = common.ReadApkCerts(input_zip) |
| 920 | |
| 921 | self.assertDictEqual(self.APKCERTS_CERTMAP3, certmap) |
| 922 | self.assertEqual('.xz', ext) |
| 923 | |
| 924 | def test_ReadApkCerts_CompressedAndNoncompressedApks(self): |
| 925 | target_files = self._write_apkcerts_txt( |
| 926 | self.APKCERTS_TXT1 + self.APKCERTS_TXT2, |
| 927 | ['Compressed1.apk.gz', 'Compressed3.apk']) |
| 928 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 929 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 930 | certmap, ext = common.ReadApkCerts(input_zip) |
| 931 | |
| 932 | certmap_merged = self.APKCERTS_CERTMAP1.copy() |
| 933 | certmap_merged.update(self.APKCERTS_CERTMAP2) |
| 934 | self.assertDictEqual(certmap_merged, certmap) |
| 935 | self.assertEqual('.gz', ext) |
| 936 | |
| 937 | def test_ReadApkCerts_MultipleCompressionMethods(self): |
| 938 | target_files = self._write_apkcerts_txt( |
| 939 | self.APKCERTS_TXT2 + self.APKCERTS_TXT3, |
| 940 | ['Compressed1.apk.gz', 'Compressed4.apk.xz']) |
| 941 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 942 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 943 | self.assertRaises(ValueError, common.ReadApkCerts, input_zip) |
| 944 | |
| 945 | def test_ReadApkCerts_MismatchingKeys(self): |
| 946 | malformed_apkcerts_txt = ( |
| 947 | 'name="App1.apk" certificate="certs/cert1.x509.pem"' |
| 948 | ' private_key="certs/cert2.pk8"\n' |
| 949 | ) |
| 950 | target_files = self._write_apkcerts_txt(malformed_apkcerts_txt) |
| 951 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 952 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 953 | self.assertRaises(ValueError, common.ReadApkCerts, input_zip) |
| 954 | |
Bill Peckham | 5c7b034 | 2020-04-03 15:36:23 -0700 | [diff] [blame] | 955 | def test_ReadApkCerts_WithWithoutOptionalFields(self): |
| 956 | target_files = self._write_apkcerts_txt(self.APKCERTS_TXT4) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 957 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Bill Peckham | 5c7b034 | 2020-04-03 15:36:23 -0700 | [diff] [blame] | 958 | certmap, ext = common.ReadApkCerts(input_zip) |
| 959 | |
| 960 | self.assertDictEqual(self.APKCERTS_CERTMAP4, certmap) |
| 961 | self.assertIsNone(ext) |
| 962 | |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 963 | def test_ExtractPublicKey(self): |
Tao Bao | 17e4e61 | 2018-02-16 17:12:54 -0800 | [diff] [blame] | 964 | cert = os.path.join(self.testdata_dir, 'testkey.x509.pem') |
| 965 | pubkey = os.path.join(self.testdata_dir, 'testkey.pubkey.pem') |
Tao Bao | da30cfa | 2017-12-01 16:19:46 -0800 | [diff] [blame] | 966 | with open(pubkey) as pubkey_fp: |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 967 | self.assertEqual(pubkey_fp.read(), common.ExtractPublicKey(cert)) |
| 968 | |
| 969 | def test_ExtractPublicKey_invalidInput(self): |
Tao Bao | 17e4e61 | 2018-02-16 17:12:54 -0800 | [diff] [blame] | 970 | wrong_input = os.path.join(self.testdata_dir, 'testkey.pk8') |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 971 | self.assertRaises(AssertionError, common.ExtractPublicKey, wrong_input) |
| 972 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 973 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 2cc0ca1 | 2019-03-15 10:44:43 -0700 | [diff] [blame] | 974 | def test_ExtractAvbPublicKey(self): |
| 975 | privkey = os.path.join(self.testdata_dir, 'testkey.key') |
| 976 | pubkey = os.path.join(self.testdata_dir, 'testkey.pubkey.pem') |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 977 | extracted_from_privkey = common.ExtractAvbPublicKey('avbtool', privkey) |
| 978 | extracted_from_pubkey = common.ExtractAvbPublicKey('avbtool', pubkey) |
| 979 | with open(extracted_from_privkey, 'rb') as privkey_fp, \ |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 980 | open(extracted_from_pubkey, 'rb') as pubkey_fp: |
Tao Bao | 2cc0ca1 | 2019-03-15 10:44:43 -0700 | [diff] [blame] | 981 | self.assertEqual(privkey_fp.read(), pubkey_fp.read()) |
| 982 | |
Tao Bao | 17e4e61 | 2018-02-16 17:12:54 -0800 | [diff] [blame] | 983 | def test_ParseCertificate(self): |
| 984 | cert = os.path.join(self.testdata_dir, 'testkey.x509.pem') |
| 985 | |
| 986 | cmd = ['openssl', 'x509', '-in', cert, '-outform', 'DER'] |
Tao Bao | da30cfa | 2017-12-01 16:19:46 -0800 | [diff] [blame] | 987 | proc = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 988 | universal_newlines=False) |
Tao Bao | 17e4e61 | 2018-02-16 17:12:54 -0800 | [diff] [blame] | 989 | expected, _ = proc.communicate() |
| 990 | self.assertEqual(0, proc.returncode) |
| 991 | |
| 992 | with open(cert) as cert_fp: |
| 993 | actual = common.ParseCertificate(cert_fp.read()) |
| 994 | self.assertEqual(expected, actual) |
| 995 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 996 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | f47bf0f | 2018-03-21 23:28:51 -0700 | [diff] [blame] | 997 | def test_GetMinSdkVersion(self): |
| 998 | test_app = os.path.join(self.testdata_dir, 'TestApp.apk') |
| 999 | self.assertEqual('24', common.GetMinSdkVersion(test_app)) |
| 1000 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1001 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | f47bf0f | 2018-03-21 23:28:51 -0700 | [diff] [blame] | 1002 | def test_GetMinSdkVersion_invalidInput(self): |
| 1003 | self.assertRaises( |
| 1004 | common.ExternalError, common.GetMinSdkVersion, 'does-not-exist.apk') |
| 1005 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1006 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | f47bf0f | 2018-03-21 23:28:51 -0700 | [diff] [blame] | 1007 | def test_GetMinSdkVersionInt(self): |
| 1008 | test_app = os.path.join(self.testdata_dir, 'TestApp.apk') |
| 1009 | self.assertEqual(24, common.GetMinSdkVersionInt(test_app, {})) |
| 1010 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1011 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | f47bf0f | 2018-03-21 23:28:51 -0700 | [diff] [blame] | 1012 | def test_GetMinSdkVersionInt_invalidInput(self): |
| 1013 | self.assertRaises( |
| 1014 | common.ExternalError, common.GetMinSdkVersionInt, 'does-not-exist.apk', |
| 1015 | {}) |
| 1016 | |
Tao Bao | 818ddf5 | 2018-01-05 11:17:34 -0800 | [diff] [blame] | 1017 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame] | 1018 | class CommonUtilsTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1019 | |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 1020 | def setUp(self): |
| 1021 | self.testdata_dir = test_utils.get_testdata_dir() |
| 1022 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1023 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1024 | def test_GetSparseImage_emptyBlockMapFile(self): |
| 1025 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1026 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1027 | target_files_zip.write( |
| 1028 | test_utils.construct_sparse_image([ |
| 1029 | (0xCAC1, 6), |
| 1030 | (0xCAC3, 3), |
| 1031 | (0xCAC1, 4)]), |
| 1032 | arcname='IMAGES/system.img') |
| 1033 | target_files_zip.writestr('IMAGES/system.map', '') |
| 1034 | target_files_zip.writestr('SYSTEM/file1', os.urandom(4096 * 8)) |
| 1035 | target_files_zip.writestr('SYSTEM/file2', os.urandom(4096 * 3)) |
| 1036 | |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1037 | tempdir = common.UnzipTemp(target_files) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1038 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1039 | sparse_image = common.GetSparseImage('system', tempdir, input_zip, False) |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1040 | |
| 1041 | self.assertDictEqual( |
| 1042 | { |
| 1043 | '__COPY': RangeSet("0"), |
| 1044 | '__NONZERO-0': RangeSet("1-5 9-12"), |
| 1045 | }, |
| 1046 | sparse_image.file_map) |
| 1047 | |
Daniel Norman | 21c34f7 | 2020-11-11 17:25:50 -0800 | [diff] [blame] | 1048 | def test_PartitionMapFromTargetFiles(self): |
| 1049 | target_files_dir = common.MakeTempDir() |
| 1050 | os.makedirs(os.path.join(target_files_dir, 'SYSTEM')) |
| 1051 | os.makedirs(os.path.join(target_files_dir, 'SYSTEM', 'vendor')) |
| 1052 | os.makedirs(os.path.join(target_files_dir, 'PRODUCT')) |
| 1053 | os.makedirs(os.path.join(target_files_dir, 'SYSTEM', 'product')) |
| 1054 | os.makedirs(os.path.join(target_files_dir, 'SYSTEM', 'vendor', 'odm')) |
| 1055 | os.makedirs(os.path.join(target_files_dir, 'VENDOR_DLKM')) |
| 1056 | partition_map = common.PartitionMapFromTargetFiles(target_files_dir) |
| 1057 | self.assertDictEqual( |
| 1058 | partition_map, |
| 1059 | { |
| 1060 | 'system': 'SYSTEM', |
| 1061 | 'vendor': 'SYSTEM/vendor', |
| 1062 | # Prefer PRODUCT over SYSTEM/product |
| 1063 | 'product': 'PRODUCT', |
| 1064 | 'odm': 'SYSTEM/vendor/odm', |
| 1065 | 'vendor_dlkm': 'VENDOR_DLKM', |
| 1066 | # No system_ext or odm_dlkm |
| 1067 | }) |
| 1068 | |
Daniel Norman | d335156 | 2020-10-29 12:33:11 -0700 | [diff] [blame] | 1069 | def test_SharedUidPartitionViolations(self): |
| 1070 | uid_dict = { |
| 1071 | 'android.uid.phone': { |
| 1072 | 'system': ['system_phone.apk'], |
| 1073 | 'system_ext': ['system_ext_phone.apk'], |
| 1074 | }, |
| 1075 | 'android.uid.wifi': { |
| 1076 | 'vendor': ['vendor_wifi.apk'], |
| 1077 | 'odm': ['odm_wifi.apk'], |
| 1078 | }, |
| 1079 | } |
| 1080 | errors = common.SharedUidPartitionViolations( |
| 1081 | uid_dict, [('system', 'system_ext'), ('vendor', 'odm')]) |
| 1082 | self.assertEqual(errors, []) |
| 1083 | |
| 1084 | def test_SharedUidPartitionViolations_Violation(self): |
| 1085 | uid_dict = { |
| 1086 | 'android.uid.phone': { |
| 1087 | 'system': ['system_phone.apk'], |
| 1088 | 'vendor': ['vendor_phone.apk'], |
| 1089 | }, |
| 1090 | } |
| 1091 | errors = common.SharedUidPartitionViolations( |
| 1092 | uid_dict, [('system', 'system_ext'), ('vendor', 'odm')]) |
| 1093 | self.assertIn( |
| 1094 | ('APK sharedUserId "android.uid.phone" found across partition groups ' |
| 1095 | 'in partitions "system,vendor"'), errors) |
| 1096 | |
Tao Bao | b2de7d9 | 2019-04-10 10:01:47 -0700 | [diff] [blame] | 1097 | def test_GetSparseImage_missingImageFile(self): |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1098 | self.assertRaises( |
Tao Bao | b2de7d9 | 2019-04-10 10:01:47 -0700 | [diff] [blame] | 1099 | AssertionError, common.GetSparseImage, 'system2', self.testdata_dir, |
| 1100 | None, False) |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1101 | self.assertRaises( |
Tao Bao | b2de7d9 | 2019-04-10 10:01:47 -0700 | [diff] [blame] | 1102 | AssertionError, common.GetSparseImage, 'unknown', self.testdata_dir, |
| 1103 | None, False) |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1104 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1105 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1106 | def test_GetSparseImage_missingBlockMapFile(self): |
| 1107 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1108 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1109 | target_files_zip.write( |
| 1110 | test_utils.construct_sparse_image([ |
| 1111 | (0xCAC1, 6), |
| 1112 | (0xCAC3, 3), |
| 1113 | (0xCAC1, 4)]), |
| 1114 | arcname='IMAGES/system.img') |
| 1115 | target_files_zip.writestr('SYSTEM/file1', os.urandom(4096 * 8)) |
| 1116 | target_files_zip.writestr('SYSTEM/file2', os.urandom(4096 * 3)) |
| 1117 | |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1118 | tempdir = common.UnzipTemp(target_files) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1119 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1120 | self.assertRaises( |
| 1121 | AssertionError, common.GetSparseImage, 'system', tempdir, input_zip, |
| 1122 | False) |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1123 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1124 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1125 | def test_GetSparseImage_sharedBlocks_notAllowed(self): |
| 1126 | """Tests the case of having overlapping blocks but disallowed.""" |
| 1127 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1128 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1129 | target_files_zip.write( |
| 1130 | test_utils.construct_sparse_image([(0xCAC2, 16)]), |
| 1131 | arcname='IMAGES/system.img') |
| 1132 | # Block 10 is shared between two files. |
| 1133 | target_files_zip.writestr( |
| 1134 | 'IMAGES/system.map', |
| 1135 | '\n'.join([ |
| 1136 | '/system/file1 1-5 9-10', |
| 1137 | '/system/file2 10-12'])) |
| 1138 | target_files_zip.writestr('SYSTEM/file1', os.urandom(4096 * 7)) |
| 1139 | target_files_zip.writestr('SYSTEM/file2', os.urandom(4096 * 3)) |
| 1140 | |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1141 | tempdir = common.UnzipTemp(target_files) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1142 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1143 | self.assertRaises( |
| 1144 | AssertionError, common.GetSparseImage, 'system', tempdir, input_zip, |
| 1145 | False) |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1146 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1147 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1148 | def test_GetSparseImage_sharedBlocks_allowed(self): |
| 1149 | """Tests the case for target using BOARD_EXT4_SHARE_DUP_BLOCKS := true.""" |
| 1150 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1151 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1152 | # Construct an image with a care_map of "0-5 9-12". |
| 1153 | target_files_zip.write( |
| 1154 | test_utils.construct_sparse_image([(0xCAC2, 16)]), |
| 1155 | arcname='IMAGES/system.img') |
| 1156 | # Block 10 is shared between two files. |
| 1157 | target_files_zip.writestr( |
| 1158 | 'IMAGES/system.map', |
| 1159 | '\n'.join([ |
| 1160 | '/system/file1 1-5 9-10', |
| 1161 | '/system/file2 10-12'])) |
| 1162 | target_files_zip.writestr('SYSTEM/file1', os.urandom(4096 * 7)) |
| 1163 | target_files_zip.writestr('SYSTEM/file2', os.urandom(4096 * 3)) |
| 1164 | |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1165 | tempdir = common.UnzipTemp(target_files) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1166 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1167 | sparse_image = common.GetSparseImage('system', tempdir, input_zip, True) |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1168 | |
| 1169 | self.assertDictEqual( |
| 1170 | { |
| 1171 | '__COPY': RangeSet("0"), |
| 1172 | '__NONZERO-0': RangeSet("6-8 13-15"), |
| 1173 | '/system/file1': RangeSet("1-5 9-10"), |
| 1174 | '/system/file2': RangeSet("11-12"), |
| 1175 | }, |
| 1176 | sparse_image.file_map) |
| 1177 | |
| 1178 | # '/system/file2' should be marked with 'uses_shared_blocks', but not with |
| 1179 | # 'incomplete'. |
| 1180 | self.assertTrue( |
| 1181 | sparse_image.file_map['/system/file2'].extra['uses_shared_blocks']) |
| 1182 | self.assertNotIn( |
| 1183 | 'incomplete', sparse_image.file_map['/system/file2'].extra) |
| 1184 | |
Tao Bao | a264fef | 2019-10-06 21:55:20 -0700 | [diff] [blame] | 1185 | # '/system/file1' will only contain one field -- a copy of the input text. |
| 1186 | self.assertEqual(1, len(sparse_image.file_map['/system/file1'].extra)) |
| 1187 | |
| 1188 | # Meta entries should not have any extra tag. |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1189 | self.assertFalse(sparse_image.file_map['__COPY'].extra) |
| 1190 | self.assertFalse(sparse_image.file_map['__NONZERO-0'].extra) |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1191 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1192 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1193 | def test_GetSparseImage_incompleteRanges(self): |
| 1194 | """Tests the case of ext4 images with holes.""" |
| 1195 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1196 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1197 | target_files_zip.write( |
| 1198 | test_utils.construct_sparse_image([(0xCAC2, 16)]), |
| 1199 | arcname='IMAGES/system.img') |
| 1200 | target_files_zip.writestr( |
| 1201 | 'IMAGES/system.map', |
| 1202 | '\n'.join([ |
| 1203 | '/system/file1 1-5 9-10', |
| 1204 | '/system/file2 11-12'])) |
| 1205 | target_files_zip.writestr('SYSTEM/file1', os.urandom(4096 * 7)) |
| 1206 | # '/system/file2' has less blocks listed (2) than actual (3). |
| 1207 | target_files_zip.writestr('SYSTEM/file2', os.urandom(4096 * 3)) |
| 1208 | |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1209 | tempdir = common.UnzipTemp(target_files) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1210 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | dba59ee | 2018-01-09 13:21:02 -0800 | [diff] [blame] | 1211 | sparse_image = common.GetSparseImage('system', tempdir, input_zip, False) |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1212 | |
Tao Bao | a264fef | 2019-10-06 21:55:20 -0700 | [diff] [blame] | 1213 | self.assertEqual( |
| 1214 | '1-5 9-10', |
| 1215 | sparse_image.file_map['/system/file1'].extra['text_str']) |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1216 | self.assertTrue(sparse_image.file_map['/system/file2'].extra['incomplete']) |
| 1217 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1218 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1219 | def test_GetSparseImage_systemRootImage_filenameWithExtraLeadingSlash(self): |
| 1220 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1221 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1222 | target_files_zip.write( |
| 1223 | test_utils.construct_sparse_image([(0xCAC2, 16)]), |
| 1224 | arcname='IMAGES/system.img') |
| 1225 | target_files_zip.writestr( |
| 1226 | 'IMAGES/system.map', |
| 1227 | '\n'.join([ |
| 1228 | '//system/file1 1-5 9-10', |
| 1229 | '//system/file2 11-12', |
| 1230 | '/system/app/file3 13-15'])) |
| 1231 | target_files_zip.writestr('SYSTEM/file1', os.urandom(4096 * 7)) |
| 1232 | # '/system/file2' has less blocks listed (2) than actual (3). |
| 1233 | target_files_zip.writestr('SYSTEM/file2', os.urandom(4096 * 3)) |
| 1234 | # '/system/app/file3' has less blocks listed (3) than actual (4). |
| 1235 | target_files_zip.writestr('SYSTEM/app/file3', os.urandom(4096 * 4)) |
| 1236 | |
| 1237 | tempdir = common.UnzipTemp(target_files) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1238 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1239 | sparse_image = common.GetSparseImage('system', tempdir, input_zip, False) |
| 1240 | |
Tao Bao | a264fef | 2019-10-06 21:55:20 -0700 | [diff] [blame] | 1241 | self.assertEqual( |
| 1242 | '1-5 9-10', |
| 1243 | sparse_image.file_map['//system/file1'].extra['text_str']) |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 1244 | self.assertTrue( |
| 1245 | sparse_image.file_map['//system/file2'].extra['incomplete']) |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1246 | self.assertTrue( |
| 1247 | sparse_image.file_map['/system/app/file3'].extra['incomplete']) |
| 1248 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1249 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1250 | def test_GetSparseImage_systemRootImage_nonSystemFiles(self): |
| 1251 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1252 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1253 | target_files_zip.write( |
| 1254 | test_utils.construct_sparse_image([(0xCAC2, 16)]), |
| 1255 | arcname='IMAGES/system.img') |
| 1256 | target_files_zip.writestr( |
| 1257 | 'IMAGES/system.map', |
| 1258 | '\n'.join([ |
| 1259 | '//system/file1 1-5 9-10', |
| 1260 | '//init.rc 13-15'])) |
| 1261 | target_files_zip.writestr('SYSTEM/file1', os.urandom(4096 * 7)) |
| 1262 | # '/init.rc' has less blocks listed (3) than actual (4). |
| 1263 | target_files_zip.writestr('ROOT/init.rc', os.urandom(4096 * 4)) |
| 1264 | |
| 1265 | tempdir = common.UnzipTemp(target_files) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1266 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1267 | sparse_image = common.GetSparseImage('system', tempdir, input_zip, False) |
| 1268 | |
Tao Bao | a264fef | 2019-10-06 21:55:20 -0700 | [diff] [blame] | 1269 | self.assertEqual( |
| 1270 | '1-5 9-10', |
| 1271 | sparse_image.file_map['//system/file1'].extra['text_str']) |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1272 | self.assertTrue(sparse_image.file_map['//init.rc'].extra['incomplete']) |
| 1273 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1274 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1275 | def test_GetSparseImage_fileNotFound(self): |
| 1276 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1277 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1278 | target_files_zip.write( |
| 1279 | test_utils.construct_sparse_image([(0xCAC2, 16)]), |
| 1280 | arcname='IMAGES/system.img') |
| 1281 | target_files_zip.writestr( |
| 1282 | 'IMAGES/system.map', |
| 1283 | '\n'.join([ |
| 1284 | '//system/file1 1-5 9-10', |
| 1285 | '//system/file2 11-12'])) |
| 1286 | target_files_zip.writestr('SYSTEM/file1', os.urandom(4096 * 7)) |
| 1287 | |
| 1288 | tempdir = common.UnzipTemp(target_files) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1289 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as input_zip: |
Tao Bao | d3554e6 | 2018-07-10 15:31:22 -0700 | [diff] [blame] | 1290 | self.assertRaises( |
| 1291 | AssertionError, common.GetSparseImage, 'system', tempdir, input_zip, |
| 1292 | False) |
| 1293 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1294 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 1295 | def test_GetAvbChainedPartitionArg(self): |
| 1296 | pubkey = os.path.join(self.testdata_dir, 'testkey.pubkey.pem') |
| 1297 | info_dict = { |
| 1298 | 'avb_avbtool': 'avbtool', |
| 1299 | 'avb_system_key_path': pubkey, |
| 1300 | 'avb_system_rollback_index_location': 2, |
| 1301 | } |
Dennis Song | 4aae62e | 2023-10-02 04:31:34 +0000 | [diff] [blame] | 1302 | chained_partition_args = common.GetAvbChainedPartitionArg( |
| 1303 | 'system', info_dict) |
| 1304 | self.assertEqual('system', chained_partition_args.partition) |
| 1305 | self.assertEqual(2, chained_partition_args.rollback_index_location) |
| 1306 | self.assertTrue(os.path.exists(chained_partition_args.pubkey_path)) |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 1307 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1308 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 1309 | def test_GetAvbChainedPartitionArg_withPrivateKey(self): |
| 1310 | key = os.path.join(self.testdata_dir, 'testkey.key') |
| 1311 | info_dict = { |
| 1312 | 'avb_avbtool': 'avbtool', |
| 1313 | 'avb_product_key_path': key, |
| 1314 | 'avb_product_rollback_index_location': 2, |
| 1315 | } |
Dennis Song | 4aae62e | 2023-10-02 04:31:34 +0000 | [diff] [blame] | 1316 | chained_partition_args = common.GetAvbChainedPartitionArg( |
| 1317 | 'product', info_dict) |
| 1318 | self.assertEqual('product', chained_partition_args.partition) |
| 1319 | self.assertEqual(2, chained_partition_args.rollback_index_location) |
| 1320 | self.assertTrue(os.path.exists(chained_partition_args.pubkey_path)) |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 1321 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1322 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 1323 | def test_GetAvbChainedPartitionArg_withSpecifiedKey(self): |
| 1324 | info_dict = { |
| 1325 | 'avb_avbtool': 'avbtool', |
| 1326 | 'avb_system_key_path': 'does-not-exist', |
| 1327 | 'avb_system_rollback_index_location': 2, |
| 1328 | } |
| 1329 | pubkey = os.path.join(self.testdata_dir, 'testkey.pubkey.pem') |
Dennis Song | 4aae62e | 2023-10-02 04:31:34 +0000 | [diff] [blame] | 1330 | chained_partition_args = common.GetAvbChainedPartitionArg( |
| 1331 | 'system', info_dict, pubkey) |
| 1332 | self.assertEqual('system', chained_partition_args.partition) |
| 1333 | self.assertEqual(2, chained_partition_args.rollback_index_location) |
| 1334 | self.assertTrue(os.path.exists(chained_partition_args.pubkey_path)) |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 1335 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1336 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 1337 | def test_GetAvbChainedPartitionArg_invalidKey(self): |
| 1338 | pubkey = os.path.join(self.testdata_dir, 'testkey_with_passwd.x509.pem') |
| 1339 | info_dict = { |
| 1340 | 'avb_avbtool': 'avbtool', |
| 1341 | 'avb_system_key_path': pubkey, |
| 1342 | 'avb_system_rollback_index_location': 2, |
| 1343 | } |
| 1344 | self.assertRaises( |
Tao Bao | 986ee86 | 2018-10-04 15:46:16 -0700 | [diff] [blame] | 1345 | common.ExternalError, common.GetAvbChainedPartitionArg, 'system', |
| 1346 | info_dict) |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 1347 | |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1348 | INFO_DICT_DEFAULT = { |
| 1349 | 'recovery_api_version': 3, |
| 1350 | 'fstab_version': 2, |
| 1351 | 'system_root_image': 'true', |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 1352 | 'no_recovery': 'true', |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1353 | 'recovery_as_boot': 'true', |
| 1354 | } |
| 1355 | |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1356 | def test_LoadListFromFile(self): |
| 1357 | file_path = os.path.join(self.testdata_dir, |
| 1358 | 'merge_config_framework_item_list') |
| 1359 | contents = common.LoadListFromFile(file_path) |
| 1360 | expected_contents = [ |
| 1361 | 'META/apkcerts.txt', |
| 1362 | 'META/filesystem_config.txt', |
| 1363 | 'META/root_filesystem_config.txt', |
| 1364 | 'META/system_manifest.xml', |
| 1365 | 'META/system_matrix.xml', |
| 1366 | 'META/update_engine_config.txt', |
| 1367 | 'PRODUCT/*', |
| 1368 | 'ROOT/*', |
| 1369 | 'SYSTEM/*', |
| 1370 | ] |
| 1371 | self.assertEqual(sorted(contents), sorted(expected_contents)) |
| 1372 | |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1373 | @staticmethod |
| 1374 | def _test_LoadInfoDict_createTargetFiles(info_dict, fstab_path): |
| 1375 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1376 | with zipfile.ZipFile(target_files, 'w', allowZip64=True) as target_files_zip: |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1377 | info_values = ''.join( |
Tao Bao | da30cfa | 2017-12-01 16:19:46 -0800 | [diff] [blame] | 1378 | ['{}={}\n'.format(k, v) for k, v in sorted(info_dict.items())]) |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1379 | common.ZipWriteStr(target_files_zip, 'META/misc_info.txt', info_values) |
| 1380 | |
| 1381 | FSTAB_TEMPLATE = "/dev/block/system {} ext4 ro,barrier=1 defaults" |
| 1382 | if info_dict.get('system_root_image') == 'true': |
| 1383 | fstab_values = FSTAB_TEMPLATE.format('/') |
| 1384 | else: |
| 1385 | fstab_values = FSTAB_TEMPLATE.format('/system') |
| 1386 | common.ZipWriteStr(target_files_zip, fstab_path, fstab_values) |
Tao Bao | 410ad8b | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1387 | |
| 1388 | common.ZipWriteStr( |
| 1389 | target_files_zip, 'META/file_contexts', 'file-contexts') |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1390 | return target_files |
| 1391 | |
| 1392 | def test_LoadInfoDict(self): |
| 1393 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1394 | self.INFO_DICT_DEFAULT, |
| 1395 | 'BOOT/RAMDISK/system/etc/recovery.fstab') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1396 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as target_files_zip: |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1397 | loaded_dict = common.LoadInfoDict(target_files_zip) |
| 1398 | self.assertEqual(3, loaded_dict['recovery_api_version']) |
| 1399 | self.assertEqual(2, loaded_dict['fstab_version']) |
| 1400 | self.assertIn('/', loaded_dict['fstab']) |
| 1401 | self.assertIn('/system', loaded_dict['fstab']) |
| 1402 | |
| 1403 | def test_LoadInfoDict_legacyRecoveryFstabPath(self): |
| 1404 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1405 | self.INFO_DICT_DEFAULT, |
| 1406 | 'BOOT/RAMDISK/etc/recovery.fstab') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1407 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as target_files_zip: |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1408 | loaded_dict = common.LoadInfoDict(target_files_zip) |
| 1409 | self.assertEqual(3, loaded_dict['recovery_api_version']) |
| 1410 | self.assertEqual(2, loaded_dict['fstab_version']) |
| 1411 | self.assertIn('/', loaded_dict['fstab']) |
| 1412 | self.assertIn('/system', loaded_dict['fstab']) |
| 1413 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1414 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1415 | def test_LoadInfoDict_dirInput(self): |
| 1416 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1417 | self.INFO_DICT_DEFAULT, |
| 1418 | 'BOOT/RAMDISK/system/etc/recovery.fstab') |
| 1419 | unzipped = common.UnzipTemp(target_files) |
| 1420 | loaded_dict = common.LoadInfoDict(unzipped) |
| 1421 | self.assertEqual(3, loaded_dict['recovery_api_version']) |
| 1422 | self.assertEqual(2, loaded_dict['fstab_version']) |
| 1423 | self.assertIn('/', loaded_dict['fstab']) |
| 1424 | self.assertIn('/system', loaded_dict['fstab']) |
| 1425 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1426 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1427 | def test_LoadInfoDict_dirInput_legacyRecoveryFstabPath(self): |
| 1428 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1429 | self.INFO_DICT_DEFAULT, |
| 1430 | 'BOOT/RAMDISK/system/etc/recovery.fstab') |
| 1431 | unzipped = common.UnzipTemp(target_files) |
| 1432 | loaded_dict = common.LoadInfoDict(unzipped) |
| 1433 | self.assertEqual(3, loaded_dict['recovery_api_version']) |
| 1434 | self.assertEqual(2, loaded_dict['fstab_version']) |
| 1435 | self.assertIn('/', loaded_dict['fstab']) |
| 1436 | self.assertIn('/system', loaded_dict['fstab']) |
| 1437 | |
| 1438 | def test_LoadInfoDict_systemRootImageFalse(self): |
| 1439 | # Devices not using system-as-root nor recovery-as-boot. Non-A/B devices |
| 1440 | # launched prior to P will likely have this config. |
| 1441 | info_dict = copy.copy(self.INFO_DICT_DEFAULT) |
| 1442 | del info_dict['no_recovery'] |
| 1443 | del info_dict['system_root_image'] |
| 1444 | del info_dict['recovery_as_boot'] |
| 1445 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1446 | info_dict, |
| 1447 | 'RECOVERY/RAMDISK/system/etc/recovery.fstab') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1448 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as target_files_zip: |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1449 | loaded_dict = common.LoadInfoDict(target_files_zip) |
| 1450 | self.assertEqual(3, loaded_dict['recovery_api_version']) |
| 1451 | self.assertEqual(2, loaded_dict['fstab_version']) |
| 1452 | self.assertNotIn('/', loaded_dict['fstab']) |
| 1453 | self.assertIn('/system', loaded_dict['fstab']) |
| 1454 | |
| 1455 | def test_LoadInfoDict_recoveryAsBootFalse(self): |
| 1456 | # Devices using system-as-root, but with standalone recovery image. Non-A/B |
| 1457 | # devices launched since P will likely have this config. |
| 1458 | info_dict = copy.copy(self.INFO_DICT_DEFAULT) |
| 1459 | del info_dict['no_recovery'] |
| 1460 | del info_dict['recovery_as_boot'] |
| 1461 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1462 | info_dict, |
| 1463 | 'RECOVERY/RAMDISK/system/etc/recovery.fstab') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1464 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as target_files_zip: |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1465 | loaded_dict = common.LoadInfoDict(target_files_zip) |
| 1466 | self.assertEqual(3, loaded_dict['recovery_api_version']) |
| 1467 | self.assertEqual(2, loaded_dict['fstab_version']) |
| 1468 | self.assertIn('/', loaded_dict['fstab']) |
| 1469 | self.assertIn('/system', loaded_dict['fstab']) |
| 1470 | |
| 1471 | def test_LoadInfoDict_noRecoveryTrue(self): |
| 1472 | # Device doesn't have a recovery partition at all. |
| 1473 | info_dict = copy.copy(self.INFO_DICT_DEFAULT) |
| 1474 | del info_dict['recovery_as_boot'] |
| 1475 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1476 | info_dict, |
| 1477 | 'RECOVERY/RAMDISK/system/etc/recovery.fstab') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1478 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as target_files_zip: |
Tao Bao | a57ab9f | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1479 | loaded_dict = common.LoadInfoDict(target_files_zip) |
| 1480 | self.assertEqual(3, loaded_dict['recovery_api_version']) |
| 1481 | self.assertEqual(2, loaded_dict['fstab_version']) |
| 1482 | self.assertIsNone(loaded_dict['fstab']) |
| 1483 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1484 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 410ad8b | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1485 | def test_LoadInfoDict_missingMetaMiscInfoTxt(self): |
| 1486 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1487 | self.INFO_DICT_DEFAULT, |
| 1488 | 'BOOT/RAMDISK/system/etc/recovery.fstab') |
| 1489 | common.ZipDelete(target_files, 'META/misc_info.txt') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1490 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as target_files_zip: |
Tao Bao | 410ad8b | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1491 | self.assertRaises(ValueError, common.LoadInfoDict, target_files_zip) |
| 1492 | |
Tao Bao | 82490d3 | 2019-04-09 00:12:30 -0700 | [diff] [blame] | 1493 | @test_utils.SkipIfExternalToolsUnavailable() |
Tao Bao | 410ad8b | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1494 | def test_LoadInfoDict_repacking(self): |
| 1495 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1496 | self.INFO_DICT_DEFAULT, |
| 1497 | 'BOOT/RAMDISK/system/etc/recovery.fstab') |
| 1498 | unzipped = common.UnzipTemp(target_files) |
| 1499 | loaded_dict = common.LoadInfoDict(unzipped, True) |
| 1500 | self.assertEqual(3, loaded_dict['recovery_api_version']) |
| 1501 | self.assertEqual(2, loaded_dict['fstab_version']) |
| 1502 | self.assertIn('/', loaded_dict['fstab']) |
| 1503 | self.assertIn('/system', loaded_dict['fstab']) |
| 1504 | self.assertEqual( |
| 1505 | os.path.join(unzipped, 'ROOT'), loaded_dict['root_dir']) |
| 1506 | self.assertEqual( |
| 1507 | os.path.join(unzipped, 'META', 'root_filesystem_config.txt'), |
| 1508 | loaded_dict['root_fs_config']) |
| 1509 | |
| 1510 | def test_LoadInfoDict_repackingWithZipFileInput(self): |
| 1511 | target_files = self._test_LoadInfoDict_createTargetFiles( |
| 1512 | self.INFO_DICT_DEFAULT, |
| 1513 | 'BOOT/RAMDISK/system/etc/recovery.fstab') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1514 | with zipfile.ZipFile(target_files, 'r', allowZip64=True) as target_files_zip: |
Tao Bao | 410ad8b | 2018-08-24 12:08:38 -0700 | [diff] [blame] | 1515 | self.assertRaises( |
| 1516 | AssertionError, common.LoadInfoDict, target_files_zip, True) |
| 1517 | |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1518 | def test_MergeDynamicPartitionInfoDicts_ReturnsMergedDict(self): |
| 1519 | framework_dict = { |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1520 | 'use_dynamic_partitions': 'true', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1521 | 'super_partition_groups': 'group_a', |
| 1522 | 'dynamic_partition_list': 'system', |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 1523 | 'super_group_a_partition_list': 'system', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1524 | } |
| 1525 | vendor_dict = { |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1526 | 'use_dynamic_partitions': 'true', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1527 | 'super_partition_groups': 'group_a group_b', |
| 1528 | 'dynamic_partition_list': 'vendor product', |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1529 | 'super_block_devices': 'super', |
| 1530 | 'super_super_device_size': '3000', |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 1531 | 'super_group_a_partition_list': 'vendor', |
| 1532 | 'super_group_a_group_size': '1000', |
| 1533 | 'super_group_b_partition_list': 'product', |
| 1534 | 'super_group_b_group_size': '2000', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1535 | } |
| 1536 | merged_dict = common.MergeDynamicPartitionInfoDicts( |
| 1537 | framework_dict=framework_dict, |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 1538 | vendor_dict=vendor_dict) |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1539 | expected_merged_dict = { |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1540 | 'use_dynamic_partitions': 'true', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1541 | 'super_partition_groups': 'group_a group_b', |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1542 | 'dynamic_partition_list': 'product system vendor', |
| 1543 | 'super_block_devices': 'super', |
| 1544 | 'super_super_device_size': '3000', |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 1545 | 'super_group_a_partition_list': 'system vendor', |
| 1546 | 'super_group_a_group_size': '1000', |
| 1547 | 'super_group_b_partition_list': 'product', |
| 1548 | 'super_group_b_group_size': '2000', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1549 | } |
| 1550 | self.assertEqual(merged_dict, expected_merged_dict) |
| 1551 | |
| 1552 | def test_MergeDynamicPartitionInfoDicts_IgnoringFrameworkGroupSize(self): |
| 1553 | framework_dict = { |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1554 | 'use_dynamic_partitions': 'true', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1555 | 'super_partition_groups': 'group_a', |
| 1556 | 'dynamic_partition_list': 'system', |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 1557 | 'super_group_a_partition_list': 'system', |
| 1558 | 'super_group_a_group_size': '5000', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1559 | } |
| 1560 | vendor_dict = { |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1561 | 'use_dynamic_partitions': 'true', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1562 | 'super_partition_groups': 'group_a group_b', |
| 1563 | 'dynamic_partition_list': 'vendor product', |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 1564 | 'super_group_a_partition_list': 'vendor', |
| 1565 | 'super_group_a_group_size': '1000', |
| 1566 | 'super_group_b_partition_list': 'product', |
| 1567 | 'super_group_b_group_size': '2000', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1568 | } |
| 1569 | merged_dict = common.MergeDynamicPartitionInfoDicts( |
| 1570 | framework_dict=framework_dict, |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 1571 | vendor_dict=vendor_dict) |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1572 | expected_merged_dict = { |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1573 | 'use_dynamic_partitions': 'true', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1574 | 'super_partition_groups': 'group_a group_b', |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1575 | 'dynamic_partition_list': 'product system vendor', |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 1576 | 'super_group_a_partition_list': 'system vendor', |
| 1577 | 'super_group_a_group_size': '1000', |
| 1578 | 'super_group_b_partition_list': 'product', |
| 1579 | 'super_group_b_group_size': '2000', |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 1580 | } |
| 1581 | self.assertEqual(merged_dict, expected_merged_dict) |
| 1582 | |
Daniel Norman | 276f062 | 2019-07-26 14:13:51 -0700 | [diff] [blame] | 1583 | def test_GetAvbPartitionArg(self): |
| 1584 | info_dict = {} |
| 1585 | cmd = common.GetAvbPartitionArg('system', '/path/to/system.img', info_dict) |
| 1586 | self.assertEqual( |
Dennis Song | 6e5e44d | 2023-10-03 02:18:06 +0000 | [diff] [blame] | 1587 | [common.AVB_ARG_NAME_INCLUDE_DESC_FROM_IMG, '/path/to/system.img'], |
| 1588 | cmd) |
Daniel Norman | 276f062 | 2019-07-26 14:13:51 -0700 | [diff] [blame] | 1589 | |
| 1590 | @test_utils.SkipIfExternalToolsUnavailable() |
| 1591 | def test_AppendVBMetaArgsForPartition_vendorAsChainedPartition(self): |
| 1592 | testdata_dir = test_utils.get_testdata_dir() |
| 1593 | pubkey = os.path.join(testdata_dir, 'testkey.pubkey.pem') |
| 1594 | info_dict = { |
| 1595 | 'avb_avbtool': 'avbtool', |
| 1596 | 'avb_vendor_key_path': pubkey, |
| 1597 | 'avb_vendor_rollback_index_location': 5, |
| 1598 | } |
| 1599 | cmd = common.GetAvbPartitionArg('vendor', '/path/to/vendor.img', info_dict) |
| 1600 | self.assertEqual(2, len(cmd)) |
Dennis Song | 6e5e44d | 2023-10-03 02:18:06 +0000 | [diff] [blame] | 1601 | self.assertEqual(common.AVB_ARG_NAME_CHAIN_PARTITION, cmd[0]) |
Dennis Song | 4aae62e | 2023-10-02 04:31:34 +0000 | [diff] [blame] | 1602 | chained_partition_args = cmd[1] |
| 1603 | self.assertEqual('vendor', chained_partition_args.partition) |
| 1604 | self.assertEqual(5, chained_partition_args.rollback_index_location) |
| 1605 | self.assertTrue(os.path.exists(chained_partition_args.pubkey_path)) |
Daniel Norman | 276f062 | 2019-07-26 14:13:51 -0700 | [diff] [blame] | 1606 | |
Tao Bao | 3612c88 | 2019-10-14 17:49:31 -0700 | [diff] [blame] | 1607 | @test_utils.SkipIfExternalToolsUnavailable() |
| 1608 | def test_AppendVBMetaArgsForPartition_recoveryAsChainedPartition_nonAb(self): |
| 1609 | testdata_dir = test_utils.get_testdata_dir() |
| 1610 | pubkey = os.path.join(testdata_dir, 'testkey.pubkey.pem') |
| 1611 | info_dict = { |
| 1612 | 'avb_avbtool': 'avbtool', |
| 1613 | 'avb_recovery_key_path': pubkey, |
| 1614 | 'avb_recovery_rollback_index_location': 3, |
| 1615 | } |
| 1616 | cmd = common.GetAvbPartitionArg( |
| 1617 | 'recovery', '/path/to/recovery.img', info_dict) |
| 1618 | self.assertFalse(cmd) |
| 1619 | |
| 1620 | @test_utils.SkipIfExternalToolsUnavailable() |
| 1621 | def test_AppendVBMetaArgsForPartition_recoveryAsChainedPartition_ab(self): |
| 1622 | testdata_dir = test_utils.get_testdata_dir() |
| 1623 | pubkey = os.path.join(testdata_dir, 'testkey.pubkey.pem') |
| 1624 | info_dict = { |
| 1625 | 'ab_update': 'true', |
| 1626 | 'avb_avbtool': 'avbtool', |
| 1627 | 'avb_recovery_key_path': pubkey, |
| 1628 | 'avb_recovery_rollback_index_location': 3, |
| 1629 | } |
| 1630 | cmd = common.GetAvbPartitionArg( |
| 1631 | 'recovery', '/path/to/recovery.img', info_dict) |
| 1632 | self.assertEqual(2, len(cmd)) |
Dennis Song | 6e5e44d | 2023-10-03 02:18:06 +0000 | [diff] [blame] | 1633 | self.assertEqual(common.AVB_ARG_NAME_CHAIN_PARTITION, cmd[0]) |
Dennis Song | 4aae62e | 2023-10-02 04:31:34 +0000 | [diff] [blame] | 1634 | chained_partition_args = cmd[1] |
| 1635 | self.assertEqual('recovery', chained_partition_args.partition) |
| 1636 | self.assertEqual(3, chained_partition_args.rollback_index_location) |
| 1637 | self.assertTrue(os.path.exists(chained_partition_args.pubkey_path)) |
Tao Bao | 3612c88 | 2019-10-14 17:49:31 -0700 | [diff] [blame] | 1638 | |
Yi-Yo Chiang | 36054e2 | 2022-01-08 22:29:30 +0800 | [diff] [blame] | 1639 | def test_GenerateGkiCertificate_KeyPathNotFound(self): |
Bowgo Tsai | 27c39b0 | 2021-03-12 21:40:32 +0800 | [diff] [blame] | 1640 | pubkey = os.path.join(self.testdata_dir, 'no_testkey_gki.pem') |
| 1641 | self.assertFalse(os.path.exists(pubkey)) |
| 1642 | |
| 1643 | common.OPTIONS.info_dict = { |
| 1644 | 'gki_signing_key_path': pubkey, |
| 1645 | 'gki_signing_algorithm': 'SHA256_RSA4096', |
| 1646 | 'gki_signing_signature_args': '--prop foo:bar', |
| 1647 | } |
Kelvin Zhang | 03dc6ee | 2023-06-28 10:10:39 -0700 | [diff] [blame] | 1648 | common.OPTIONS.search_path = None |
Yi-Yo Chiang | 36054e2 | 2022-01-08 22:29:30 +0800 | [diff] [blame] | 1649 | test_file = tempfile.NamedTemporaryFile() |
| 1650 | self.assertRaises(common.ExternalError, common._GenerateGkiCertificate, |
Yi-Yo Chiang | 24da1a4 | 2022-02-22 19:51:15 +0800 | [diff] [blame] | 1651 | test_file.name, 'generic_kernel') |
Bowgo Tsai | 27c39b0 | 2021-03-12 21:40:32 +0800 | [diff] [blame] | 1652 | |
Yi-Yo Chiang | 36054e2 | 2022-01-08 22:29:30 +0800 | [diff] [blame] | 1653 | def test_GenerateGkiCertificate_SearchKeyPathNotFound(self): |
Bowgo Tsai | 27c39b0 | 2021-03-12 21:40:32 +0800 | [diff] [blame] | 1654 | pubkey = 'no_testkey_gki.pem' |
| 1655 | self.assertFalse(os.path.exists(pubkey)) |
| 1656 | |
| 1657 | # Tests it should raise ExternalError if no key found under |
| 1658 | # OPTIONS.search_path. |
| 1659 | search_path_dir = common.MakeTempDir() |
| 1660 | search_pubkey = os.path.join(search_path_dir, pubkey) |
| 1661 | self.assertFalse(os.path.exists(search_pubkey)) |
| 1662 | |
| 1663 | common.OPTIONS.search_path = search_path_dir |
| 1664 | common.OPTIONS.info_dict = { |
| 1665 | 'gki_signing_key_path': pubkey, |
| 1666 | 'gki_signing_algorithm': 'SHA256_RSA4096', |
| 1667 | 'gki_signing_signature_args': '--prop foo:bar', |
| 1668 | } |
Yi-Yo Chiang | 36054e2 | 2022-01-08 22:29:30 +0800 | [diff] [blame] | 1669 | test_file = tempfile.NamedTemporaryFile() |
| 1670 | self.assertRaises(common.ExternalError, common._GenerateGkiCertificate, |
Yi-Yo Chiang | 24da1a4 | 2022-02-22 19:51:15 +0800 | [diff] [blame] | 1671 | test_file.name, 'generic_kernel') |
Tao Bao | fc7e0e0 | 2018-02-13 13:54:02 -0800 | [diff] [blame] | 1672 | |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 1673 | |
Abhishek Nigam | 1dfca46 | 2023-11-08 02:21:39 +0000 | [diff] [blame^] | 1674 | class InstallRecoveryScriptFormatTest(test_utils.ReleaseToolsTestCase): |
| 1675 | """Checks the format of install-recovery.sh. |
| 1676 | |
| 1677 | Its format should match between common.py and validate_target_files.py. |
| 1678 | """ |
| 1679 | |
| 1680 | def setUp(self): |
| 1681 | self._tempdir = common.MakeTempDir() |
| 1682 | # Create a fake dict that contains the fstab info for boot&recovery. |
| 1683 | self._info = {"fstab": {}} |
| 1684 | fake_fstab = [ |
| 1685 | "/dev/soc.0/by-name/boot /boot emmc defaults defaults", |
| 1686 | "/dev/soc.0/by-name/recovery /recovery emmc defaults defaults"] |
| 1687 | self._info["fstab"] = common.LoadRecoveryFSTab("\n".join, 2, fake_fstab) |
| 1688 | # Construct the gzipped recovery.img and boot.img |
| 1689 | self.recovery_data = bytearray([ |
| 1690 | 0x1f, 0x8b, 0x08, 0x00, 0x81, 0x11, 0x02, 0x5a, 0x00, 0x03, 0x2b, 0x4a, |
| 1691 | 0x4d, 0xce, 0x2f, 0x4b, 0x2d, 0xaa, 0x04, 0x00, 0xc9, 0x93, 0x43, 0xf3, |
| 1692 | 0x08, 0x00, 0x00, 0x00 |
| 1693 | ]) |
| 1694 | # echo -n "boot" | gzip -f | hd |
| 1695 | self.boot_data = bytearray([ |
| 1696 | 0x1f, 0x8b, 0x08, 0x00, 0x8c, 0x12, 0x02, 0x5a, 0x00, 0x03, 0x4b, 0xca, |
| 1697 | 0xcf, 0x2f, 0x01, 0x00, 0xc4, 0xae, 0xed, 0x46, 0x04, 0x00, 0x00, 0x00 |
| 1698 | ]) |
| 1699 | |
| 1700 | def _out_tmp_sink(self, name, data, prefix="SYSTEM"): |
| 1701 | loc = os.path.join(self._tempdir, prefix, name) |
| 1702 | if not os.path.exists(os.path.dirname(loc)): |
| 1703 | os.makedirs(os.path.dirname(loc)) |
| 1704 | with open(loc, "wb") as f: |
| 1705 | f.write(data) |
| 1706 | |
| 1707 | def test_full_recovery(self): |
| 1708 | recovery_image = common.File("recovery.img", self.recovery_data) |
| 1709 | boot_image = common.File("boot.img", self.boot_data) |
| 1710 | self._info["full_recovery_image"] = "true" |
| 1711 | |
| 1712 | common.MakeRecoveryPatch(self._tempdir, self._out_tmp_sink, |
| 1713 | recovery_image, boot_image, self._info) |
| 1714 | validate_target_files.ValidateInstallRecoveryScript(self._tempdir, |
| 1715 | self._info) |
| 1716 | |
| 1717 | @test_utils.SkipIfExternalToolsUnavailable() |
| 1718 | def test_recovery_from_boot(self): |
| 1719 | recovery_image = common.File("recovery.img", self.recovery_data) |
| 1720 | self._out_tmp_sink("recovery.img", recovery_image.data, "IMAGES") |
| 1721 | boot_image = common.File("boot.img", self.boot_data) |
| 1722 | self._out_tmp_sink("boot.img", boot_image.data, "IMAGES") |
| 1723 | |
| 1724 | common.MakeRecoveryPatch(self._tempdir, self._out_tmp_sink, |
| 1725 | recovery_image, boot_image, self._info) |
| 1726 | validate_target_files.ValidateInstallRecoveryScript(self._tempdir, |
| 1727 | self._info) |
| 1728 | # Validate 'recovery-from-boot' with bonus argument. |
| 1729 | self._out_tmp_sink("etc/recovery-resource.dat", b"bonus", "SYSTEM") |
| 1730 | common.MakeRecoveryPatch(self._tempdir, self._out_tmp_sink, |
| 1731 | recovery_image, boot_image, self._info) |
| 1732 | validate_target_files.ValidateInstallRecoveryScript(self._tempdir, |
| 1733 | self._info) |
| 1734 | |
| 1735 | |
| 1736 | class MockBlockDifference(object): |
| 1737 | |
| 1738 | def __init__(self, partition, tgt, src=None): |
| 1739 | self.partition = partition |
| 1740 | self.tgt = tgt |
| 1741 | self.src = src |
| 1742 | |
| 1743 | def WriteScript(self, script, _, progress=None, |
| 1744 | write_verify_script=False): |
| 1745 | if progress: |
| 1746 | script.AppendExtra("progress({})".format(progress)) |
| 1747 | script.AppendExtra("patch({});".format(self.partition)) |
| 1748 | if write_verify_script: |
| 1749 | self.WritePostInstallVerifyScript(script) |
| 1750 | |
| 1751 | def WritePostInstallVerifyScript(self, script): |
| 1752 | script.AppendExtra("verify({});".format(self.partition)) |
| 1753 | |
| 1754 | |
| 1755 | class FakeSparseImage(object): |
| 1756 | |
| 1757 | def __init__(self, size): |
| 1758 | self.blocksize = 4096 |
| 1759 | self.total_blocks = size // 4096 |
| 1760 | assert size % 4096 == 0, "{} is not a multiple of 4096".format(size) |
| 1761 | |
| 1762 | |
| 1763 | class DynamicPartitionsDifferenceTest(test_utils.ReleaseToolsTestCase): |
| 1764 | |
| 1765 | @staticmethod |
| 1766 | def get_op_list(output_path): |
| 1767 | with zipfile.ZipFile(output_path, allowZip64=True) as output_zip: |
| 1768 | with output_zip.open('dynamic_partitions_op_list') as op_list: |
| 1769 | return [line.decode().strip() for line in op_list.readlines() |
| 1770 | if not line.startswith(b'#')] |
| 1771 | |
| 1772 | def setUp(self): |
| 1773 | self.script = test_utils.MockScriptWriter() |
| 1774 | self.output_path = common.MakeTempFile(suffix='.zip') |
| 1775 | |
| 1776 | def test_full(self): |
| 1777 | target_info = common.LoadDictionaryFromLines(""" |
| 1778 | dynamic_partition_list=system vendor |
| 1779 | super_partition_groups=group_foo |
| 1780 | super_group_foo_group_size={group_size} |
| 1781 | super_group_foo_partition_list=system vendor |
| 1782 | """.format(group_size=4 * GiB).split("\n")) |
| 1783 | block_diffs = [MockBlockDifference("system", FakeSparseImage(3 * GiB)), |
| 1784 | MockBlockDifference("vendor", FakeSparseImage(1 * GiB))] |
| 1785 | |
| 1786 | dp_diff = common.DynamicPartitionsDifference(target_info, block_diffs) |
| 1787 | with zipfile.ZipFile(self.output_path, 'w', allowZip64=True) as output_zip: |
| 1788 | dp_diff.WriteScript(self.script, output_zip, write_verify_script=True) |
| 1789 | |
| 1790 | self.assertEqual(str(self.script).strip(), """ |
| 1791 | assert(update_dynamic_partitions(package_extract_file("dynamic_partitions_op_list"))); |
| 1792 | patch(system); |
| 1793 | verify(system); |
| 1794 | unmap_partition("system"); |
| 1795 | patch(vendor); |
| 1796 | verify(vendor); |
| 1797 | unmap_partition("vendor"); |
| 1798 | """.strip()) |
| 1799 | |
| 1800 | lines = self.get_op_list(self.output_path) |
| 1801 | |
| 1802 | remove_all_groups = lines.index("remove_all_groups") |
| 1803 | add_group = lines.index("add_group group_foo 4294967296") |
| 1804 | add_vendor = lines.index("add vendor group_foo") |
| 1805 | add_system = lines.index("add system group_foo") |
| 1806 | resize_vendor = lines.index("resize vendor 1073741824") |
| 1807 | resize_system = lines.index("resize system 3221225472") |
| 1808 | |
| 1809 | self.assertLess(remove_all_groups, add_group, |
| 1810 | "Should add groups after removing all groups") |
| 1811 | self.assertLess(add_group, min(add_vendor, add_system), |
| 1812 | "Should add partitions after adding group") |
| 1813 | self.assertLess(add_system, resize_system, |
| 1814 | "Should resize system after adding it") |
| 1815 | self.assertLess(add_vendor, resize_vendor, |
| 1816 | "Should resize vendor after adding it") |
| 1817 | |
| 1818 | def test_inc_groups(self): |
| 1819 | source_info = common.LoadDictionaryFromLines(""" |
| 1820 | super_partition_groups=group_foo group_bar group_baz |
| 1821 | super_group_foo_group_size={group_foo_size} |
| 1822 | super_group_bar_group_size={group_bar_size} |
| 1823 | """.format(group_foo_size=4 * GiB, group_bar_size=3 * GiB).split("\n")) |
| 1824 | target_info = common.LoadDictionaryFromLines(""" |
| 1825 | super_partition_groups=group_foo group_baz group_qux |
| 1826 | super_group_foo_group_size={group_foo_size} |
| 1827 | super_group_baz_group_size={group_baz_size} |
| 1828 | super_group_qux_group_size={group_qux_size} |
| 1829 | """.format(group_foo_size=3 * GiB, group_baz_size=4 * GiB, |
| 1830 | group_qux_size=1 * GiB).split("\n")) |
| 1831 | |
| 1832 | dp_diff = common.DynamicPartitionsDifference(target_info, |
| 1833 | block_diffs=[], |
| 1834 | source_info_dict=source_info) |
| 1835 | with zipfile.ZipFile(self.output_path, 'w', allowZip64=True) as output_zip: |
| 1836 | dp_diff.WriteScript(self.script, output_zip, write_verify_script=True) |
| 1837 | |
| 1838 | lines = self.get_op_list(self.output_path) |
| 1839 | |
| 1840 | removed = lines.index("remove_group group_bar") |
| 1841 | shrunk = lines.index("resize_group group_foo 3221225472") |
| 1842 | grown = lines.index("resize_group group_baz 4294967296") |
| 1843 | added = lines.index("add_group group_qux 1073741824") |
| 1844 | |
| 1845 | self.assertLess(max(removed, shrunk), |
| 1846 | min(grown, added), |
| 1847 | "ops that remove / shrink partitions must precede ops that " |
| 1848 | "grow / add partitions") |
| 1849 | |
| 1850 | def test_incremental(self): |
| 1851 | source_info = common.LoadDictionaryFromLines(""" |
| 1852 | dynamic_partition_list=system vendor product system_ext |
| 1853 | super_partition_groups=group_foo |
| 1854 | super_group_foo_group_size={group_foo_size} |
| 1855 | super_group_foo_partition_list=system vendor product system_ext |
| 1856 | """.format(group_foo_size=4 * GiB).split("\n")) |
| 1857 | target_info = common.LoadDictionaryFromLines(""" |
| 1858 | dynamic_partition_list=system vendor product odm |
| 1859 | super_partition_groups=group_foo group_bar |
| 1860 | super_group_foo_group_size={group_foo_size} |
| 1861 | super_group_foo_partition_list=system vendor odm |
| 1862 | super_group_bar_group_size={group_bar_size} |
| 1863 | super_group_bar_partition_list=product |
| 1864 | """.format(group_foo_size=3 * GiB, group_bar_size=1 * GiB).split("\n")) |
| 1865 | |
| 1866 | block_diffs = [MockBlockDifference("system", FakeSparseImage(1536 * MiB), |
| 1867 | src=FakeSparseImage(1024 * MiB)), |
| 1868 | MockBlockDifference("vendor", FakeSparseImage(512 * MiB), |
| 1869 | src=FakeSparseImage(1024 * MiB)), |
| 1870 | MockBlockDifference("product", FakeSparseImage(1024 * MiB), |
| 1871 | src=FakeSparseImage(1024 * MiB)), |
| 1872 | MockBlockDifference("system_ext", None, |
| 1873 | src=FakeSparseImage(1024 * MiB)), |
| 1874 | MockBlockDifference("odm", FakeSparseImage(1024 * MiB), |
| 1875 | src=None)] |
| 1876 | |
| 1877 | dp_diff = common.DynamicPartitionsDifference(target_info, block_diffs, |
| 1878 | source_info_dict=source_info) |
| 1879 | with zipfile.ZipFile(self.output_path, 'w', allowZip64=True) as output_zip: |
| 1880 | dp_diff.WriteScript(self.script, output_zip, write_verify_script=True) |
| 1881 | |
| 1882 | metadata_idx = self.script.lines.index( |
| 1883 | 'assert(update_dynamic_partitions(package_extract_file(' |
| 1884 | '"dynamic_partitions_op_list")));') |
| 1885 | self.assertLess(self.script.lines.index('patch(vendor);'), metadata_idx) |
| 1886 | self.assertLess(metadata_idx, self.script.lines.index('verify(vendor);')) |
| 1887 | for p in ("product", "system", "odm"): |
| 1888 | patch_idx = self.script.lines.index("patch({});".format(p)) |
| 1889 | verify_idx = self.script.lines.index("verify({});".format(p)) |
| 1890 | self.assertLess(metadata_idx, patch_idx, |
| 1891 | "Should patch {} after updating metadata".format(p)) |
| 1892 | self.assertLess(patch_idx, verify_idx, |
| 1893 | "Should verify {} after patching".format(p)) |
| 1894 | |
| 1895 | self.assertNotIn("patch(system_ext);", self.script.lines) |
| 1896 | |
| 1897 | lines = self.get_op_list(self.output_path) |
| 1898 | |
| 1899 | remove = lines.index("remove system_ext") |
| 1900 | move_product_out = lines.index("move product default") |
| 1901 | shrink = lines.index("resize vendor 536870912") |
| 1902 | shrink_group = lines.index("resize_group group_foo 3221225472") |
| 1903 | add_group_bar = lines.index("add_group group_bar 1073741824") |
| 1904 | add_odm = lines.index("add odm group_foo") |
| 1905 | grow_existing = lines.index("resize system 1610612736") |
| 1906 | grow_added = lines.index("resize odm 1073741824") |
| 1907 | move_product_in = lines.index("move product group_bar") |
| 1908 | |
| 1909 | max_idx_move_partition_out_foo = max(remove, move_product_out, shrink) |
| 1910 | min_idx_move_partition_in_foo = min(add_odm, grow_existing, grow_added) |
| 1911 | |
| 1912 | self.assertLess(max_idx_move_partition_out_foo, shrink_group, |
| 1913 | "Must shrink group after partitions inside group are shrunk" |
| 1914 | " / removed") |
| 1915 | |
| 1916 | self.assertLess(add_group_bar, move_product_in, |
| 1917 | "Must add partitions to group after group is added") |
| 1918 | |
| 1919 | self.assertLess(max_idx_move_partition_out_foo, |
| 1920 | min_idx_move_partition_in_foo, |
| 1921 | "Must shrink partitions / remove partitions from group" |
| 1922 | "before adding / moving partitions into group") |
| 1923 | |
| 1924 | def test_remove_partition(self): |
| 1925 | source_info = common.LoadDictionaryFromLines(""" |
| 1926 | blockimgdiff_versions=3,4 |
| 1927 | use_dynamic_partitions=true |
| 1928 | dynamic_partition_list=foo |
| 1929 | super_partition_groups=group_foo |
| 1930 | super_group_foo_group_size={group_foo_size} |
| 1931 | super_group_foo_partition_list=foo |
| 1932 | """.format(group_foo_size=4 * GiB).split("\n")) |
| 1933 | target_info = common.LoadDictionaryFromLines(""" |
| 1934 | blockimgdiff_versions=3,4 |
| 1935 | use_dynamic_partitions=true |
| 1936 | super_partition_groups=group_foo |
| 1937 | super_group_foo_group_size={group_foo_size} |
| 1938 | """.format(group_foo_size=4 * GiB).split("\n")) |
| 1939 | |
| 1940 | common.OPTIONS.info_dict = target_info |
| 1941 | common.OPTIONS.target_info_dict = target_info |
| 1942 | common.OPTIONS.source_info_dict = source_info |
| 1943 | common.OPTIONS.cache_size = 4 * 4096 |
| 1944 | |
| 1945 | block_diffs = [common.BlockDifference("foo", EmptyImage(), |
| 1946 | src=DataImage("source", pad=True))] |
| 1947 | |
| 1948 | dp_diff = common.DynamicPartitionsDifference(target_info, block_diffs, |
| 1949 | source_info_dict=source_info) |
| 1950 | with zipfile.ZipFile(self.output_path, 'w', allowZip64=True) as output_zip: |
| 1951 | dp_diff.WriteScript(self.script, output_zip, write_verify_script=True) |
| 1952 | |
| 1953 | self.assertNotIn("block_image_update", str(self.script), |
| 1954 | "Removed partition should not be patched.") |
| 1955 | |
| 1956 | lines = self.get_op_list(self.output_path) |
| 1957 | self.assertEqual(lines, ["remove foo"]) |
| 1958 | |
| 1959 | |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 1960 | class PartitionBuildPropsTest(test_utils.ReleaseToolsTestCase): |
| 1961 | def setUp(self): |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1962 | self.odm_build_prop = [ |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 1963 | 'ro.odm.build.date.utc=1578430045', |
| 1964 | 'ro.odm.build.fingerprint=' |
| 1965 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 1966 | 'ro.product.odm.device=coral', |
| 1967 | 'import /odm/etc/build_${ro.boot.product.device_name}.prop', |
| 1968 | ] |
| 1969 | |
| 1970 | @staticmethod |
| 1971 | def _BuildZipFile(entries): |
| 1972 | input_file = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1973 | with zipfile.ZipFile(input_file, 'w', allowZip64=True) as input_zip: |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 1974 | for name, content in entries.items(): |
| 1975 | input_zip.writestr(name, content) |
| 1976 | |
| 1977 | return input_file |
| 1978 | |
| 1979 | def test_parseBuildProps_noImportStatement(self): |
| 1980 | build_prop = [ |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1981 | 'ro.odm.build.date.utc=1578430045', |
| 1982 | 'ro.odm.build.fingerprint=' |
| 1983 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 1984 | 'ro.product.odm.device=coral', |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 1985 | ] |
| 1986 | input_file = self._BuildZipFile({ |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1987 | 'ODM/etc/build.prop': '\n'.join(build_prop), |
| 1988 | }) |
| 1989 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 1990 | with zipfile.ZipFile(input_file, 'r', allowZip64=True) as input_zip: |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 1991 | placeholder_values = { |
| 1992 | 'ro.boot.product.device_name': ['std', 'pro'] |
| 1993 | } |
| 1994 | partition_props = common.PartitionBuildProps.FromInputFile( |
| 1995 | input_zip, 'odm', placeholder_values) |
| 1996 | |
| 1997 | self.assertEqual({ |
| 1998 | 'ro.odm.build.date.utc': '1578430045', |
| 1999 | 'ro.odm.build.fingerprint': |
| 2000 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 2001 | 'ro.product.odm.device': 'coral', |
| 2002 | }, partition_props.build_props) |
| 2003 | |
| 2004 | self.assertEqual(set(), partition_props.prop_overrides) |
| 2005 | |
| 2006 | def test_parseBuildProps_singleImportStatement(self): |
| 2007 | build_std_prop = [ |
| 2008 | 'ro.product.odm.device=coral', |
| 2009 | 'ro.product.odm.name=product1', |
| 2010 | ] |
| 2011 | build_pro_prop = [ |
| 2012 | 'ro.product.odm.device=coralpro', |
| 2013 | 'ro.product.odm.name=product2', |
| 2014 | ] |
| 2015 | |
| 2016 | input_file = self._BuildZipFile({ |
| 2017 | 'ODM/etc/build.prop': '\n'.join(self.odm_build_prop), |
| 2018 | 'ODM/etc/build_std.prop': '\n'.join(build_std_prop), |
| 2019 | 'ODM/etc/build_pro.prop': '\n'.join(build_pro_prop), |
| 2020 | }) |
| 2021 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 2022 | with zipfile.ZipFile(input_file, 'r', allowZip64=True) as input_zip: |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 2023 | placeholder_values = { |
| 2024 | 'ro.boot.product.device_name': 'std' |
| 2025 | } |
| 2026 | partition_props = common.PartitionBuildProps.FromInputFile( |
| 2027 | input_zip, 'odm', placeholder_values) |
| 2028 | |
| 2029 | self.assertEqual({ |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 2030 | 'ro.odm.build.date.utc': '1578430045', |
| 2031 | 'ro.odm.build.fingerprint': |
| 2032 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 2033 | 'ro.product.odm.device': 'coral', |
| 2034 | 'ro.product.odm.name': 'product1', |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 2035 | }, partition_props.build_props) |
| 2036 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 2037 | with zipfile.ZipFile(input_file, 'r', allowZip64=True) as input_zip: |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 2038 | placeholder_values = { |
| 2039 | 'ro.boot.product.device_name': 'pro' |
| 2040 | } |
| 2041 | partition_props = common.PartitionBuildProps.FromInputFile( |
| 2042 | input_zip, 'odm', placeholder_values) |
| 2043 | |
| 2044 | self.assertEqual({ |
| 2045 | 'ro.odm.build.date.utc': '1578430045', |
| 2046 | 'ro.odm.build.fingerprint': |
| 2047 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 2048 | 'ro.product.odm.device': 'coralpro', |
| 2049 | 'ro.product.odm.name': 'product2', |
| 2050 | }, partition_props.build_props) |
| 2051 | |
| 2052 | def test_parseBuildProps_noPlaceHolders(self): |
| 2053 | build_prop = copy.copy(self.odm_build_prop) |
| 2054 | input_file = self._BuildZipFile({ |
| 2055 | 'ODM/etc/build.prop': '\n'.join(build_prop), |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 2056 | }) |
| 2057 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 2058 | with zipfile.ZipFile(input_file, 'r', allowZip64=True) as input_zip: |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 2059 | partition_props = common.PartitionBuildProps.FromInputFile( |
| 2060 | input_zip, 'odm') |
| 2061 | |
| 2062 | self.assertEqual({ |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 2063 | 'ro.odm.build.date.utc': '1578430045', |
| 2064 | 'ro.odm.build.fingerprint': |
| 2065 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 2066 | 'ro.product.odm.device': 'coral', |
Tianjie Xu | 0fde41e | 2020-05-09 05:24:18 +0000 | [diff] [blame] | 2067 | }, partition_props.build_props) |
| 2068 | |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 2069 | self.assertEqual(set(), partition_props.prop_overrides) |
| 2070 | |
| 2071 | def test_parseBuildProps_multipleImportStatements(self): |
| 2072 | build_prop = copy.deepcopy(self.odm_build_prop) |
| 2073 | build_prop.append( |
| 2074 | 'import /odm/etc/build_${ro.boot.product.product_name}.prop') |
| 2075 | |
| 2076 | build_std_prop = [ |
| 2077 | 'ro.product.odm.device=coral', |
| 2078 | ] |
| 2079 | build_pro_prop = [ |
| 2080 | 'ro.product.odm.device=coralpro', |
| 2081 | ] |
| 2082 | |
| 2083 | product1_prop = [ |
| 2084 | 'ro.product.odm.name=product1', |
| 2085 | 'ro.product.not_care=not_care', |
| 2086 | ] |
| 2087 | |
| 2088 | product2_prop = [ |
| 2089 | 'ro.product.odm.name=product2', |
| 2090 | 'ro.product.not_care=not_care', |
| 2091 | ] |
| 2092 | |
| 2093 | input_file = self._BuildZipFile({ |
| 2094 | 'ODM/etc/build.prop': '\n'.join(build_prop), |
| 2095 | 'ODM/etc/build_std.prop': '\n'.join(build_std_prop), |
| 2096 | 'ODM/etc/build_pro.prop': '\n'.join(build_pro_prop), |
| 2097 | 'ODM/etc/build_product1.prop': '\n'.join(product1_prop), |
| 2098 | 'ODM/etc/build_product2.prop': '\n'.join(product2_prop), |
| 2099 | }) |
| 2100 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 2101 | with zipfile.ZipFile(input_file, 'r', allowZip64=True) as input_zip: |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 2102 | placeholder_values = { |
| 2103 | 'ro.boot.product.device_name': 'std', |
| 2104 | 'ro.boot.product.product_name': 'product1', |
| 2105 | 'ro.boot.product.not_care': 'not_care', |
| 2106 | } |
| 2107 | partition_props = common.PartitionBuildProps.FromInputFile( |
| 2108 | input_zip, 'odm', placeholder_values) |
| 2109 | |
| 2110 | self.assertEqual({ |
| 2111 | 'ro.odm.build.date.utc': '1578430045', |
| 2112 | 'ro.odm.build.fingerprint': |
| 2113 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 2114 | 'ro.product.odm.device': 'coral', |
| 2115 | 'ro.product.odm.name': 'product1' |
| 2116 | }, partition_props.build_props) |
| 2117 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 2118 | with zipfile.ZipFile(input_file, 'r', allowZip64=True) as input_zip: |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 2119 | placeholder_values = { |
| 2120 | 'ro.boot.product.device_name': 'pro', |
| 2121 | 'ro.boot.product.product_name': 'product2', |
| 2122 | 'ro.boot.product.not_care': 'not_care', |
| 2123 | } |
| 2124 | partition_props = common.PartitionBuildProps.FromInputFile( |
| 2125 | input_zip, 'odm', placeholder_values) |
| 2126 | |
| 2127 | self.assertEqual({ |
| 2128 | 'ro.odm.build.date.utc': '1578430045', |
| 2129 | 'ro.odm.build.fingerprint': |
| 2130 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 2131 | 'ro.product.odm.device': 'coralpro', |
| 2132 | 'ro.product.odm.name': 'product2' |
| 2133 | }, partition_props.build_props) |
| 2134 | |
| 2135 | def test_parseBuildProps_defineAfterOverride(self): |
| 2136 | build_prop = copy.deepcopy(self.odm_build_prop) |
| 2137 | build_prop.append('ro.product.odm.device=coral') |
| 2138 | |
| 2139 | build_std_prop = [ |
| 2140 | 'ro.product.odm.device=coral', |
| 2141 | ] |
| 2142 | build_pro_prop = [ |
| 2143 | 'ro.product.odm.device=coralpro', |
| 2144 | ] |
| 2145 | |
| 2146 | input_file = self._BuildZipFile({ |
| 2147 | 'ODM/etc/build.prop': '\n'.join(build_prop), |
| 2148 | 'ODM/etc/build_std.prop': '\n'.join(build_std_prop), |
| 2149 | 'ODM/etc/build_pro.prop': '\n'.join(build_pro_prop), |
| 2150 | }) |
| 2151 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 2152 | with zipfile.ZipFile(input_file, 'r', allowZip64=True) as input_zip: |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 2153 | placeholder_values = { |
| 2154 | 'ro.boot.product.device_name': 'std', |
| 2155 | } |
| 2156 | |
| 2157 | self.assertRaises(ValueError, common.PartitionBuildProps.FromInputFile, |
| 2158 | input_zip, 'odm', placeholder_values) |
| 2159 | |
| 2160 | def test_parseBuildProps_duplicateOverride(self): |
| 2161 | build_prop = copy.deepcopy(self.odm_build_prop) |
| 2162 | build_prop.append( |
| 2163 | 'import /odm/etc/build_${ro.boot.product.product_name}.prop') |
| 2164 | |
| 2165 | build_std_prop = [ |
| 2166 | 'ro.product.odm.device=coral', |
| 2167 | 'ro.product.odm.name=product1', |
| 2168 | ] |
| 2169 | build_pro_prop = [ |
| 2170 | 'ro.product.odm.device=coralpro', |
| 2171 | ] |
| 2172 | |
| 2173 | product1_prop = [ |
| 2174 | 'ro.product.odm.name=product1', |
| 2175 | ] |
| 2176 | |
| 2177 | product2_prop = [ |
| 2178 | 'ro.product.odm.name=product2', |
| 2179 | ] |
| 2180 | |
| 2181 | input_file = self._BuildZipFile({ |
| 2182 | 'ODM/etc/build.prop': '\n'.join(build_prop), |
| 2183 | 'ODM/etc/build_std.prop': '\n'.join(build_std_prop), |
| 2184 | 'ODM/etc/build_pro.prop': '\n'.join(build_pro_prop), |
| 2185 | 'ODM/etc/build_product1.prop': '\n'.join(product1_prop), |
| 2186 | 'ODM/etc/build_product2.prop': '\n'.join(product2_prop), |
| 2187 | }) |
| 2188 | |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 2189 | with zipfile.ZipFile(input_file, 'r', allowZip64=True) as input_zip: |
Tianjie Xu | 9afb221 | 2020-05-10 21:48:15 +0000 | [diff] [blame] | 2190 | placeholder_values = { |
| 2191 | 'ro.boot.product.device_name': 'std', |
| 2192 | 'ro.boot.product.product_name': 'product1', |
| 2193 | } |
| 2194 | self.assertRaises(ValueError, common.PartitionBuildProps.FromInputFile, |
| 2195 | input_zip, 'odm', placeholder_values) |
GeQi | 3fa9e32 | 2022-10-18 11:50:16 +0800 | [diff] [blame] | 2196 | |
| 2197 | def test_partitionBuildProps_fromInputFile_deepcopy(self): |
| 2198 | build_prop = [ |
| 2199 | 'ro.odm.build.date.utc=1578430045', |
| 2200 | 'ro.odm.build.fingerprint=' |
| 2201 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 2202 | 'ro.product.odm.device=coral', |
| 2203 | ] |
| 2204 | input_file = self._BuildZipFile({ |
| 2205 | 'ODM/etc/build.prop': '\n'.join(build_prop), |
| 2206 | }) |
| 2207 | |
| 2208 | with zipfile.ZipFile(input_file, 'r', allowZip64=True) as input_zip: |
| 2209 | placeholder_values = { |
| 2210 | 'ro.boot.product.device_name': ['std', 'pro'] |
| 2211 | } |
| 2212 | partition_props = common.PartitionBuildProps.FromInputFile( |
| 2213 | input_zip, 'odm', placeholder_values) |
| 2214 | |
| 2215 | copied_props = copy.deepcopy(partition_props) |
| 2216 | self.assertEqual({ |
Kelvin Zhang | 8aa6525 | 2023-08-30 18:29:59 -0700 | [diff] [blame] | 2217 | 'ro.odm.build.date.utc': '1578430045', |
| 2218 | 'ro.odm.build.fingerprint': |
| 2219 | 'google/coral/coral:10/RP1A.200325.001/6337676:user/dev-keys', |
| 2220 | 'ro.product.odm.device': 'coral', |
GeQi | 3fa9e32 | 2022-10-18 11:50:16 +0800 | [diff] [blame] | 2221 | }, copied_props.build_props) |