Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2018 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | import copy |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 18 | import os |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 19 | import os.path |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 20 | import zipfile |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 21 | |
| 22 | import common |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 23 | import test_utils |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 24 | from ota_from_target_files import ( |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 25 | _LoadOemDicts, AbOtaPropertyFiles, BuildInfo, FinalizeMetadata, |
| 26 | GetPackageMetadata, GetTargetFilesZipForSecondaryImages, |
Tao Bao | c0746f4 | 2018-02-21 13:17:22 -0800 | [diff] [blame] | 27 | GetTargetFilesZipWithoutPostinstallConfig, NonAbOtaPropertyFiles, |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 28 | Payload, PayloadSigner, POSTINSTALL_CONFIG, PropertyFiles, |
| 29 | StreamingPropertyFiles, WriteFingerprintAssertion) |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 30 | |
| 31 | |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 32 | def construct_target_files(secondary=False): |
| 33 | """Returns a target-files.zip file for generating OTA packages.""" |
| 34 | target_files = common.MakeTempFile(prefix='target_files-', suffix='.zip') |
| 35 | with zipfile.ZipFile(target_files, 'w') as target_files_zip: |
| 36 | # META/update_engine_config.txt |
| 37 | target_files_zip.writestr( |
| 38 | 'META/update_engine_config.txt', |
| 39 | "PAYLOAD_MAJOR_VERSION=2\nPAYLOAD_MINOR_VERSION=4\n") |
| 40 | |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 41 | # META/postinstall_config.txt |
| 42 | target_files_zip.writestr( |
| 43 | POSTINSTALL_CONFIG, |
| 44 | '\n'.join([ |
| 45 | "RUN_POSTINSTALL_system=true", |
| 46 | "POSTINSTALL_PATH_system=system/bin/otapreopt_script", |
| 47 | "FILESYSTEM_TYPE_system=ext4", |
| 48 | "POSTINSTALL_OPTIONAL_system=true", |
| 49 | ])) |
| 50 | |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 51 | ab_partitions = [ |
| 52 | ('IMAGES', 'boot'), |
| 53 | ('IMAGES', 'system'), |
| 54 | ('IMAGES', 'vendor'), |
| 55 | ('RADIO', 'bootloader'), |
| 56 | ('RADIO', 'modem'), |
| 57 | ] |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 58 | # META/ab_partitions.txt |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 59 | target_files_zip.writestr( |
| 60 | 'META/ab_partitions.txt', |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 61 | '\n'.join([partition[1] for partition in ab_partitions])) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 62 | |
| 63 | # Create dummy images for each of them. |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 64 | for path, partition in ab_partitions: |
| 65 | target_files_zip.writestr( |
| 66 | '{}/{}.img'.format(path, partition), |
| 67 | os.urandom(len(partition))) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 68 | |
Tao Bao | 5277d10 | 2018-04-17 23:47:21 -0700 | [diff] [blame] | 69 | # system_other shouldn't appear in META/ab_partitions.txt. |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 70 | if secondary: |
| 71 | target_files_zip.writestr('IMAGES/system_other.img', |
| 72 | os.urandom(len("system_other"))) |
| 73 | |
| 74 | return target_files |
| 75 | |
| 76 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 77 | class MockScriptWriter(object): |
| 78 | """A class that mocks edify_generator.EdifyGenerator. |
| 79 | |
| 80 | It simply pushes the incoming arguments onto script stack, which is to assert |
| 81 | the calls to EdifyGenerator functions. |
| 82 | """ |
| 83 | |
| 84 | def __init__(self): |
| 85 | self.script = [] |
| 86 | |
| 87 | def Mount(self, *args): |
| 88 | self.script.append(('Mount',) + args) |
| 89 | |
| 90 | def AssertDevice(self, *args): |
| 91 | self.script.append(('AssertDevice',) + args) |
| 92 | |
| 93 | def AssertOemProperty(self, *args): |
| 94 | self.script.append(('AssertOemProperty',) + args) |
| 95 | |
| 96 | def AssertFingerprintOrThumbprint(self, *args): |
| 97 | self.script.append(('AssertFingerprintOrThumbprint',) + args) |
| 98 | |
| 99 | def AssertSomeFingerprint(self, *args): |
| 100 | self.script.append(('AssertSomeFingerprint',) + args) |
| 101 | |
| 102 | def AssertSomeThumbprint(self, *args): |
| 103 | self.script.append(('AssertSomeThumbprint',) + args) |
| 104 | |
| 105 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame^] | 106 | class BuildInfoTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 107 | |
| 108 | TEST_INFO_DICT = { |
| 109 | 'build.prop' : { |
| 110 | 'ro.product.device' : 'product-device', |
| 111 | 'ro.product.name' : 'product-name', |
| 112 | 'ro.build.fingerprint' : 'build-fingerprint', |
| 113 | 'ro.build.foo' : 'build-foo', |
| 114 | }, |
| 115 | 'vendor.build.prop' : { |
| 116 | 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint', |
| 117 | }, |
| 118 | 'property1' : 'value1', |
| 119 | 'property2' : 4096, |
| 120 | } |
| 121 | |
| 122 | TEST_INFO_DICT_USES_OEM_PROPS = { |
| 123 | 'build.prop' : { |
| 124 | 'ro.product.name' : 'product-name', |
| 125 | 'ro.build.thumbprint' : 'build-thumbprint', |
| 126 | 'ro.build.bar' : 'build-bar', |
| 127 | }, |
| 128 | 'vendor.build.prop' : { |
| 129 | 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint', |
| 130 | }, |
| 131 | 'property1' : 'value1', |
| 132 | 'property2' : 4096, |
| 133 | 'oem_fingerprint_properties' : 'ro.product.device ro.product.brand', |
| 134 | } |
| 135 | |
| 136 | TEST_OEM_DICTS = [ |
| 137 | { |
| 138 | 'ro.product.brand' : 'brand1', |
| 139 | 'ro.product.device' : 'device1', |
| 140 | }, |
| 141 | { |
| 142 | 'ro.product.brand' : 'brand2', |
| 143 | 'ro.product.device' : 'device2', |
| 144 | }, |
| 145 | { |
| 146 | 'ro.product.brand' : 'brand3', |
| 147 | 'ro.product.device' : 'device3', |
| 148 | }, |
| 149 | ] |
| 150 | |
| 151 | def test_init(self): |
| 152 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 153 | self.assertEqual('product-device', target_info.device) |
| 154 | self.assertEqual('build-fingerprint', target_info.fingerprint) |
| 155 | self.assertFalse(target_info.is_ab) |
| 156 | self.assertIsNone(target_info.oem_props) |
| 157 | |
| 158 | def test_init_with_oem_props(self): |
| 159 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 160 | self.TEST_OEM_DICTS) |
| 161 | self.assertEqual('device1', target_info.device) |
| 162 | self.assertEqual('brand1/product-name/device1:build-thumbprint', |
| 163 | target_info.fingerprint) |
| 164 | |
| 165 | # Swap the order in oem_dicts, which would lead to different BuildInfo. |
| 166 | oem_dicts = copy.copy(self.TEST_OEM_DICTS) |
| 167 | oem_dicts[0], oem_dicts[2] = oem_dicts[2], oem_dicts[0] |
| 168 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, oem_dicts) |
| 169 | self.assertEqual('device3', target_info.device) |
| 170 | self.assertEqual('brand3/product-name/device3:build-thumbprint', |
| 171 | target_info.fingerprint) |
| 172 | |
| 173 | # Missing oem_dict should be rejected. |
| 174 | self.assertRaises(AssertionError, BuildInfo, |
| 175 | self.TEST_INFO_DICT_USES_OEM_PROPS, None) |
| 176 | |
| 177 | def test___getitem__(self): |
| 178 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 179 | self.assertEqual('value1', target_info['property1']) |
| 180 | self.assertEqual(4096, target_info['property2']) |
| 181 | self.assertEqual('build-foo', target_info['build.prop']['ro.build.foo']) |
| 182 | |
| 183 | def test___getitem__with_oem_props(self): |
| 184 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 185 | self.TEST_OEM_DICTS) |
| 186 | self.assertEqual('value1', target_info['property1']) |
| 187 | self.assertEqual(4096, target_info['property2']) |
| 188 | self.assertRaises(KeyError, |
| 189 | lambda: target_info['build.prop']['ro.build.foo']) |
| 190 | |
Tao Bao | 667c753 | 2018-07-06 10:13:59 -0700 | [diff] [blame] | 191 | def test___setitem__(self): |
| 192 | target_info = BuildInfo(copy.deepcopy(self.TEST_INFO_DICT), None) |
| 193 | self.assertEqual('value1', target_info['property1']) |
| 194 | target_info['property1'] = 'value2' |
| 195 | self.assertEqual('value2', target_info['property1']) |
| 196 | |
| 197 | self.assertEqual('build-foo', target_info['build.prop']['ro.build.foo']) |
| 198 | target_info['build.prop']['ro.build.foo'] = 'build-bar' |
| 199 | self.assertEqual('build-bar', target_info['build.prop']['ro.build.foo']) |
| 200 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 201 | def test_get(self): |
| 202 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 203 | self.assertEqual('value1', target_info.get('property1')) |
| 204 | self.assertEqual(4096, target_info.get('property2')) |
| 205 | self.assertEqual(4096, target_info.get('property2', 1024)) |
| 206 | self.assertEqual(1024, target_info.get('property-nonexistent', 1024)) |
| 207 | self.assertEqual('build-foo', target_info.get('build.prop')['ro.build.foo']) |
| 208 | |
| 209 | def test_get_with_oem_props(self): |
| 210 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 211 | self.TEST_OEM_DICTS) |
| 212 | self.assertEqual('value1', target_info.get('property1')) |
| 213 | self.assertEqual(4096, target_info.get('property2')) |
| 214 | self.assertEqual(4096, target_info.get('property2', 1024)) |
| 215 | self.assertEqual(1024, target_info.get('property-nonexistent', 1024)) |
| 216 | self.assertIsNone(target_info.get('build.prop').get('ro.build.foo')) |
| 217 | self.assertRaises(KeyError, |
| 218 | lambda: target_info.get('build.prop')['ro.build.foo']) |
| 219 | |
Tao Bao | 667c753 | 2018-07-06 10:13:59 -0700 | [diff] [blame] | 220 | def test_items(self): |
| 221 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 222 | items = target_info.items() |
| 223 | self.assertIn(('property1', 'value1'), items) |
| 224 | self.assertIn(('property2', 4096), items) |
| 225 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 226 | def test_GetBuildProp(self): |
| 227 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 228 | self.assertEqual('build-foo', target_info.GetBuildProp('ro.build.foo')) |
| 229 | self.assertRaises(common.ExternalError, target_info.GetBuildProp, |
| 230 | 'ro.build.nonexistent') |
| 231 | |
| 232 | def test_GetBuildProp_with_oem_props(self): |
| 233 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 234 | self.TEST_OEM_DICTS) |
| 235 | self.assertEqual('build-bar', target_info.GetBuildProp('ro.build.bar')) |
| 236 | self.assertRaises(common.ExternalError, target_info.GetBuildProp, |
| 237 | 'ro.build.nonexistent') |
| 238 | |
| 239 | def test_GetVendorBuildProp(self): |
| 240 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 241 | self.assertEqual('vendor-build-fingerprint', |
| 242 | target_info.GetVendorBuildProp( |
| 243 | 'ro.vendor.build.fingerprint')) |
| 244 | self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp, |
| 245 | 'ro.build.nonexistent') |
| 246 | |
| 247 | def test_GetVendorBuildProp_with_oem_props(self): |
| 248 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 249 | self.TEST_OEM_DICTS) |
| 250 | self.assertEqual('vendor-build-fingerprint', |
| 251 | target_info.GetVendorBuildProp( |
| 252 | 'ro.vendor.build.fingerprint')) |
| 253 | self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp, |
| 254 | 'ro.build.nonexistent') |
| 255 | |
Tao Bao | ea6cbd0 | 2018-09-05 13:06:37 -0700 | [diff] [blame] | 256 | def test_vendor_fingerprint(self): |
| 257 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 258 | self.assertEqual('vendor-build-fingerprint', |
| 259 | target_info.vendor_fingerprint) |
| 260 | |
| 261 | def test_vendor_fingerprint_blacklisted(self): |
| 262 | target_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS) |
| 263 | del target_info_dict['vendor.build.prop']['ro.vendor.build.fingerprint'] |
| 264 | target_info = BuildInfo(target_info_dict, self.TEST_OEM_DICTS) |
| 265 | self.assertIsNone(target_info.vendor_fingerprint) |
| 266 | |
| 267 | def test_vendor_fingerprint_without_vendor_build_prop(self): |
| 268 | target_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS) |
| 269 | del target_info_dict['vendor.build.prop'] |
| 270 | target_info = BuildInfo(target_info_dict, self.TEST_OEM_DICTS) |
| 271 | self.assertIsNone(target_info.vendor_fingerprint) |
| 272 | |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 273 | def test_WriteMountOemScript(self): |
| 274 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 275 | self.TEST_OEM_DICTS) |
| 276 | script_writer = MockScriptWriter() |
| 277 | target_info.WriteMountOemScript(script_writer) |
| 278 | self.assertEqual([('Mount', '/oem', None)], script_writer.script) |
| 279 | |
| 280 | def test_WriteDeviceAssertions(self): |
| 281 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 282 | script_writer = MockScriptWriter() |
| 283 | target_info.WriteDeviceAssertions(script_writer, False) |
| 284 | self.assertEqual([('AssertDevice', 'product-device')], script_writer.script) |
| 285 | |
| 286 | def test_WriteDeviceAssertions_with_oem_props(self): |
| 287 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 288 | self.TEST_OEM_DICTS) |
| 289 | script_writer = MockScriptWriter() |
| 290 | target_info.WriteDeviceAssertions(script_writer, False) |
| 291 | self.assertEqual( |
| 292 | [ |
| 293 | ('AssertOemProperty', 'ro.product.device', |
| 294 | ['device1', 'device2', 'device3'], False), |
| 295 | ('AssertOemProperty', 'ro.product.brand', |
| 296 | ['brand1', 'brand2', 'brand3'], False), |
| 297 | ], |
| 298 | script_writer.script) |
| 299 | |
| 300 | def test_WriteFingerprintAssertion_without_oem_props(self): |
| 301 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 302 | source_info_dict = copy.deepcopy(self.TEST_INFO_DICT) |
| 303 | source_info_dict['build.prop']['ro.build.fingerprint'] = ( |
| 304 | 'source-build-fingerprint') |
| 305 | source_info = BuildInfo(source_info_dict, None) |
| 306 | |
| 307 | script_writer = MockScriptWriter() |
| 308 | WriteFingerprintAssertion(script_writer, target_info, source_info) |
| 309 | self.assertEqual( |
| 310 | [('AssertSomeFingerprint', 'source-build-fingerprint', |
| 311 | 'build-fingerprint')], |
| 312 | script_writer.script) |
| 313 | |
| 314 | def test_WriteFingerprintAssertion_with_source_oem_props(self): |
| 315 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 316 | source_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 317 | self.TEST_OEM_DICTS) |
| 318 | |
| 319 | script_writer = MockScriptWriter() |
| 320 | WriteFingerprintAssertion(script_writer, target_info, source_info) |
| 321 | self.assertEqual( |
| 322 | [('AssertFingerprintOrThumbprint', 'build-fingerprint', |
| 323 | 'build-thumbprint')], |
| 324 | script_writer.script) |
| 325 | |
| 326 | def test_WriteFingerprintAssertion_with_target_oem_props(self): |
| 327 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 328 | self.TEST_OEM_DICTS) |
| 329 | source_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 330 | |
| 331 | script_writer = MockScriptWriter() |
| 332 | WriteFingerprintAssertion(script_writer, target_info, source_info) |
| 333 | self.assertEqual( |
| 334 | [('AssertFingerprintOrThumbprint', 'build-fingerprint', |
| 335 | 'build-thumbprint')], |
| 336 | script_writer.script) |
| 337 | |
| 338 | def test_WriteFingerprintAssertion_with_both_oem_props(self): |
| 339 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 340 | self.TEST_OEM_DICTS) |
| 341 | source_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS) |
| 342 | source_info_dict['build.prop']['ro.build.thumbprint'] = ( |
| 343 | 'source-build-thumbprint') |
| 344 | source_info = BuildInfo(source_info_dict, self.TEST_OEM_DICTS) |
| 345 | |
| 346 | script_writer = MockScriptWriter() |
| 347 | WriteFingerprintAssertion(script_writer, target_info, source_info) |
| 348 | self.assertEqual( |
| 349 | [('AssertSomeThumbprint', 'build-thumbprint', |
| 350 | 'source-build-thumbprint')], |
| 351 | script_writer.script) |
| 352 | |
| 353 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame^] | 354 | class LoadOemDictsTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 355 | |
| 356 | def test_NoneDict(self): |
| 357 | self.assertIsNone(_LoadOemDicts(None)) |
| 358 | |
| 359 | def test_SingleDict(self): |
| 360 | dict_file = common.MakeTempFile() |
| 361 | with open(dict_file, 'w') as dict_fp: |
| 362 | dict_fp.write('abc=1\ndef=2\nxyz=foo\na.b.c=bar\n') |
| 363 | |
| 364 | oem_dicts = _LoadOemDicts([dict_file]) |
| 365 | self.assertEqual(1, len(oem_dicts)) |
| 366 | self.assertEqual('foo', oem_dicts[0]['xyz']) |
| 367 | self.assertEqual('bar', oem_dicts[0]['a.b.c']) |
| 368 | |
| 369 | def test_MultipleDicts(self): |
| 370 | oem_source = [] |
| 371 | for i in range(3): |
| 372 | dict_file = common.MakeTempFile() |
| 373 | with open(dict_file, 'w') as dict_fp: |
| 374 | dict_fp.write( |
| 375 | 'ro.build.index={}\ndef=2\nxyz=foo\na.b.c=bar\n'.format(i)) |
| 376 | oem_source.append(dict_file) |
| 377 | |
| 378 | oem_dicts = _LoadOemDicts(oem_source) |
| 379 | self.assertEqual(3, len(oem_dicts)) |
| 380 | for i, oem_dict in enumerate(oem_dicts): |
| 381 | self.assertEqual('2', oem_dict['def']) |
| 382 | self.assertEqual('foo', oem_dict['xyz']) |
| 383 | self.assertEqual('bar', oem_dict['a.b.c']) |
| 384 | self.assertEqual('{}'.format(i), oem_dict['ro.build.index']) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 385 | |
| 386 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame^] | 387 | class OtaFromTargetFilesTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 388 | |
| 389 | TEST_TARGET_INFO_DICT = { |
| 390 | 'build.prop' : { |
| 391 | 'ro.product.device' : 'product-device', |
| 392 | 'ro.build.fingerprint' : 'build-fingerprint-target', |
| 393 | 'ro.build.version.incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 394 | 'ro.build.version.sdk' : '27', |
| 395 | 'ro.build.version.security_patch' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 396 | 'ro.build.date.utc' : '1500000000', |
| 397 | }, |
| 398 | } |
| 399 | |
| 400 | TEST_SOURCE_INFO_DICT = { |
| 401 | 'build.prop' : { |
| 402 | 'ro.product.device' : 'product-device', |
| 403 | 'ro.build.fingerprint' : 'build-fingerprint-source', |
| 404 | 'ro.build.version.incremental' : 'build-version-incremental-source', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 405 | 'ro.build.version.sdk' : '25', |
| 406 | 'ro.build.version.security_patch' : '2016-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 407 | 'ro.build.date.utc' : '1400000000', |
| 408 | }, |
| 409 | } |
| 410 | |
| 411 | def setUp(self): |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 412 | self.testdata_dir = test_utils.get_testdata_dir() |
| 413 | self.assertTrue(os.path.exists(self.testdata_dir)) |
| 414 | |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 415 | # Reset the global options as in ota_from_target_files.py. |
| 416 | common.OPTIONS.incremental_source = None |
| 417 | common.OPTIONS.downgrade = False |
| 418 | common.OPTIONS.timestamp = False |
| 419 | common.OPTIONS.wipe_user_data = False |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 420 | common.OPTIONS.no_signing = False |
| 421 | common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey') |
| 422 | common.OPTIONS.key_passwords = { |
| 423 | common.OPTIONS.package_key : None, |
| 424 | } |
| 425 | |
| 426 | common.OPTIONS.search_path = test_utils.get_search_path() |
| 427 | self.assertIsNotNone(common.OPTIONS.search_path) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 428 | |
| 429 | def test_GetPackageMetadata_abOta_full(self): |
| 430 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 431 | target_info_dict['ab_update'] = 'true' |
| 432 | target_info = BuildInfo(target_info_dict, None) |
| 433 | metadata = GetPackageMetadata(target_info) |
| 434 | self.assertDictEqual( |
| 435 | { |
| 436 | 'ota-type' : 'AB', |
| 437 | 'ota-required-cache' : '0', |
| 438 | 'post-build' : 'build-fingerprint-target', |
| 439 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 440 | 'post-sdk-level' : '27', |
| 441 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 442 | 'post-timestamp' : '1500000000', |
| 443 | 'pre-device' : 'product-device', |
| 444 | }, |
| 445 | metadata) |
| 446 | |
| 447 | def test_GetPackageMetadata_abOta_incremental(self): |
| 448 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 449 | target_info_dict['ab_update'] = 'true' |
| 450 | target_info = BuildInfo(target_info_dict, None) |
| 451 | source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None) |
| 452 | common.OPTIONS.incremental_source = '' |
| 453 | metadata = GetPackageMetadata(target_info, source_info) |
| 454 | self.assertDictEqual( |
| 455 | { |
| 456 | 'ota-type' : 'AB', |
| 457 | 'ota-required-cache' : '0', |
| 458 | 'post-build' : 'build-fingerprint-target', |
| 459 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 460 | 'post-sdk-level' : '27', |
| 461 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 462 | 'post-timestamp' : '1500000000', |
| 463 | 'pre-device' : 'product-device', |
| 464 | 'pre-build' : 'build-fingerprint-source', |
| 465 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 466 | }, |
| 467 | metadata) |
| 468 | |
| 469 | def test_GetPackageMetadata_nonAbOta_full(self): |
| 470 | target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
| 471 | metadata = GetPackageMetadata(target_info) |
| 472 | self.assertDictEqual( |
| 473 | { |
| 474 | 'ota-type' : 'BLOCK', |
| 475 | 'post-build' : 'build-fingerprint-target', |
| 476 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 477 | 'post-sdk-level' : '27', |
| 478 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 479 | 'post-timestamp' : '1500000000', |
| 480 | 'pre-device' : 'product-device', |
| 481 | }, |
| 482 | metadata) |
| 483 | |
| 484 | def test_GetPackageMetadata_nonAbOta_incremental(self): |
| 485 | target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
| 486 | source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None) |
| 487 | common.OPTIONS.incremental_source = '' |
| 488 | metadata = GetPackageMetadata(target_info, source_info) |
| 489 | self.assertDictEqual( |
| 490 | { |
| 491 | 'ota-type' : 'BLOCK', |
| 492 | 'post-build' : 'build-fingerprint-target', |
| 493 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 494 | 'post-sdk-level' : '27', |
| 495 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 496 | 'post-timestamp' : '1500000000', |
| 497 | 'pre-device' : 'product-device', |
| 498 | 'pre-build' : 'build-fingerprint-source', |
| 499 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 500 | }, |
| 501 | metadata) |
| 502 | |
| 503 | def test_GetPackageMetadata_wipe(self): |
| 504 | target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
| 505 | common.OPTIONS.wipe_user_data = True |
| 506 | metadata = GetPackageMetadata(target_info) |
| 507 | self.assertDictEqual( |
| 508 | { |
| 509 | 'ota-type' : 'BLOCK', |
| 510 | 'ota-wipe' : 'yes', |
| 511 | 'post-build' : 'build-fingerprint-target', |
| 512 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 513 | 'post-sdk-level' : '27', |
| 514 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 515 | 'post-timestamp' : '1500000000', |
| 516 | 'pre-device' : 'product-device', |
| 517 | }, |
| 518 | metadata) |
| 519 | |
| 520 | @staticmethod |
| 521 | def _test_GetPackageMetadata_swapBuildTimestamps(target_info, source_info): |
| 522 | (target_info['build.prop']['ro.build.date.utc'], |
| 523 | source_info['build.prop']['ro.build.date.utc']) = ( |
| 524 | source_info['build.prop']['ro.build.date.utc'], |
| 525 | target_info['build.prop']['ro.build.date.utc']) |
| 526 | |
| 527 | def test_GetPackageMetadata_unintentionalDowngradeDetected(self): |
| 528 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 529 | source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT) |
| 530 | self._test_GetPackageMetadata_swapBuildTimestamps( |
| 531 | target_info_dict, source_info_dict) |
| 532 | |
| 533 | target_info = BuildInfo(target_info_dict, None) |
| 534 | source_info = BuildInfo(source_info_dict, None) |
| 535 | common.OPTIONS.incremental_source = '' |
| 536 | self.assertRaises(RuntimeError, GetPackageMetadata, target_info, |
| 537 | source_info) |
| 538 | |
| 539 | def test_GetPackageMetadata_downgrade(self): |
| 540 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 541 | source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT) |
| 542 | self._test_GetPackageMetadata_swapBuildTimestamps( |
| 543 | target_info_dict, source_info_dict) |
| 544 | |
| 545 | target_info = BuildInfo(target_info_dict, None) |
| 546 | source_info = BuildInfo(source_info_dict, None) |
| 547 | common.OPTIONS.incremental_source = '' |
| 548 | common.OPTIONS.downgrade = True |
| 549 | common.OPTIONS.wipe_user_data = True |
| 550 | metadata = GetPackageMetadata(target_info, source_info) |
| 551 | self.assertDictEqual( |
| 552 | { |
| 553 | 'ota-downgrade' : 'yes', |
| 554 | 'ota-type' : 'BLOCK', |
| 555 | 'ota-wipe' : 'yes', |
| 556 | 'post-build' : 'build-fingerprint-target', |
| 557 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame] | 558 | 'post-sdk-level' : '27', |
| 559 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | faa8e0b | 2018-04-12 14:31:43 -0700 | [diff] [blame] | 560 | 'post-timestamp' : '1400000000', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 561 | 'pre-device' : 'product-device', |
| 562 | 'pre-build' : 'build-fingerprint-source', |
| 563 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 564 | }, |
| 565 | metadata) |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 566 | |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 567 | def test_GetTargetFilesZipForSecondaryImages(self): |
| 568 | input_file = construct_target_files(secondary=True) |
| 569 | target_file = GetTargetFilesZipForSecondaryImages(input_file) |
| 570 | |
| 571 | with zipfile.ZipFile(target_file) as verify_zip: |
| 572 | namelist = verify_zip.namelist() |
| 573 | |
| 574 | self.assertIn('META/ab_partitions.txt', namelist) |
| 575 | self.assertIn('IMAGES/boot.img', namelist) |
| 576 | self.assertIn('IMAGES/system.img', namelist) |
| 577 | self.assertIn('IMAGES/vendor.img', namelist) |
Tao Bao | 1248980 | 2018-07-12 14:47:38 -0700 | [diff] [blame] | 578 | self.assertIn('RADIO/bootloader.img', namelist) |
| 579 | self.assertIn('RADIO/modem.img', namelist) |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 580 | self.assertIn(POSTINSTALL_CONFIG, namelist) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 581 | |
| 582 | self.assertNotIn('IMAGES/system_other.img', namelist) |
| 583 | self.assertNotIn('IMAGES/system.map', namelist) |
| 584 | |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 585 | def test_GetTargetFilesZipForSecondaryImages_skipPostinstall(self): |
| 586 | input_file = construct_target_files(secondary=True) |
| 587 | target_file = GetTargetFilesZipForSecondaryImages( |
| 588 | input_file, skip_postinstall=True) |
| 589 | |
| 590 | with zipfile.ZipFile(target_file) as verify_zip: |
| 591 | namelist = verify_zip.namelist() |
| 592 | |
| 593 | self.assertIn('META/ab_partitions.txt', namelist) |
| 594 | self.assertIn('IMAGES/boot.img', namelist) |
| 595 | self.assertIn('IMAGES/system.img', namelist) |
| 596 | self.assertIn('IMAGES/vendor.img', namelist) |
Tao Bao | 1248980 | 2018-07-12 14:47:38 -0700 | [diff] [blame] | 597 | self.assertIn('RADIO/bootloader.img', namelist) |
| 598 | self.assertIn('RADIO/modem.img', namelist) |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 599 | |
| 600 | self.assertNotIn('IMAGES/system_other.img', namelist) |
| 601 | self.assertNotIn('IMAGES/system.map', namelist) |
| 602 | self.assertNotIn(POSTINSTALL_CONFIG, namelist) |
| 603 | |
Tao Bao | 1248980 | 2018-07-12 14:47:38 -0700 | [diff] [blame] | 604 | def test_GetTargetFilesZipForSecondaryImages_withoutRadioImages(self): |
| 605 | input_file = construct_target_files(secondary=True) |
| 606 | common.ZipDelete(input_file, 'RADIO/bootloader.img') |
| 607 | common.ZipDelete(input_file, 'RADIO/modem.img') |
| 608 | target_file = GetTargetFilesZipForSecondaryImages(input_file) |
| 609 | |
| 610 | with zipfile.ZipFile(target_file) as verify_zip: |
| 611 | namelist = verify_zip.namelist() |
| 612 | |
| 613 | self.assertIn('META/ab_partitions.txt', namelist) |
| 614 | self.assertIn('IMAGES/boot.img', namelist) |
| 615 | self.assertIn('IMAGES/system.img', namelist) |
| 616 | self.assertIn('IMAGES/vendor.img', namelist) |
| 617 | self.assertIn(POSTINSTALL_CONFIG, namelist) |
| 618 | |
| 619 | self.assertNotIn('IMAGES/system_other.img', namelist) |
| 620 | self.assertNotIn('IMAGES/system.map', namelist) |
| 621 | self.assertNotIn('RADIO/bootloader.img', namelist) |
| 622 | self.assertNotIn('RADIO/modem.img', namelist) |
| 623 | |
Tao Bao | 15a146a | 2018-02-21 16:06:59 -0800 | [diff] [blame] | 624 | def test_GetTargetFilesZipWithoutPostinstallConfig(self): |
| 625 | input_file = construct_target_files() |
| 626 | target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file) |
| 627 | with zipfile.ZipFile(target_file) as verify_zip: |
| 628 | self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist()) |
| 629 | |
| 630 | def test_GetTargetFilesZipWithoutPostinstallConfig_missingEntry(self): |
| 631 | input_file = construct_target_files() |
| 632 | common.ZipDelete(input_file, POSTINSTALL_CONFIG) |
| 633 | target_file = GetTargetFilesZipWithoutPostinstallConfig(input_file) |
| 634 | with zipfile.ZipFile(target_file) as verify_zip: |
| 635 | self.assertNotIn(POSTINSTALL_CONFIG, verify_zip.namelist()) |
| 636 | |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 637 | def _test_FinalizeMetadata(self, large_entry=False): |
| 638 | entries = [ |
| 639 | 'required-entry1', |
| 640 | 'required-entry2', |
| 641 | ] |
| 642 | zip_file = PropertyFilesTest.construct_zip_package(entries) |
| 643 | # Add a large entry of 1 GiB if requested. |
| 644 | if large_entry: |
| 645 | with zipfile.ZipFile(zip_file, 'a') as zip_fp: |
| 646 | zip_fp.writestr( |
| 647 | # Using 'zoo' so that the entry stays behind others after signing. |
| 648 | 'zoo', |
| 649 | 'A' * 1024 * 1024 * 1024, |
| 650 | zipfile.ZIP_STORED) |
| 651 | |
| 652 | metadata = {} |
| 653 | output_file = common.MakeTempFile(suffix='.zip') |
| 654 | needed_property_files = ( |
| 655 | TestPropertyFiles(), |
| 656 | ) |
| 657 | FinalizeMetadata(metadata, zip_file, output_file, needed_property_files) |
| 658 | self.assertIn('ota-test-property-files', metadata) |
| 659 | |
| 660 | def test_FinalizeMetadata(self): |
| 661 | self._test_FinalizeMetadata() |
| 662 | |
| 663 | def test_FinalizeMetadata_withNoSigning(self): |
| 664 | common.OPTIONS.no_signing = True |
| 665 | self._test_FinalizeMetadata() |
| 666 | |
| 667 | def test_FinalizeMetadata_largeEntry(self): |
| 668 | self._test_FinalizeMetadata(large_entry=True) |
| 669 | |
| 670 | def test_FinalizeMetadata_largeEntry_withNoSigning(self): |
| 671 | common.OPTIONS.no_signing = True |
| 672 | self._test_FinalizeMetadata(large_entry=True) |
| 673 | |
| 674 | def test_FinalizeMetadata_insufficientSpace(self): |
| 675 | entries = [ |
| 676 | 'required-entry1', |
| 677 | 'required-entry2', |
| 678 | 'optional-entry1', |
| 679 | 'optional-entry2', |
| 680 | ] |
| 681 | zip_file = PropertyFilesTest.construct_zip_package(entries) |
| 682 | with zipfile.ZipFile(zip_file, 'a') as zip_fp: |
| 683 | zip_fp.writestr( |
| 684 | # 'foo-entry1' will appear ahead of all other entries (in alphabetical |
| 685 | # order) after the signing, which will in turn trigger the |
| 686 | # InsufficientSpaceException and an automatic retry. |
| 687 | 'foo-entry1', |
| 688 | 'A' * 1024 * 1024, |
| 689 | zipfile.ZIP_STORED) |
| 690 | |
| 691 | metadata = {} |
| 692 | needed_property_files = ( |
| 693 | TestPropertyFiles(), |
| 694 | ) |
| 695 | output_file = common.MakeTempFile(suffix='.zip') |
| 696 | FinalizeMetadata(metadata, zip_file, output_file, needed_property_files) |
| 697 | self.assertIn('ota-test-property-files', metadata) |
| 698 | |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 699 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 700 | class TestPropertyFiles(PropertyFiles): |
| 701 | """A class that extends PropertyFiles for testing purpose.""" |
| 702 | |
| 703 | def __init__(self): |
| 704 | super(TestPropertyFiles, self).__init__() |
| 705 | self.name = 'ota-test-property-files' |
| 706 | self.required = ( |
| 707 | 'required-entry1', |
| 708 | 'required-entry2', |
| 709 | ) |
| 710 | self.optional = ( |
| 711 | 'optional-entry1', |
| 712 | 'optional-entry2', |
| 713 | ) |
| 714 | |
| 715 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame^] | 716 | class PropertyFilesTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 717 | |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 718 | def setUp(self): |
| 719 | common.OPTIONS.no_signing = False |
| 720 | |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 721 | @staticmethod |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 722 | def construct_zip_package(entries): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 723 | zip_file = common.MakeTempFile(suffix='.zip') |
| 724 | with zipfile.ZipFile(zip_file, 'w') as zip_fp: |
| 725 | for entry in entries: |
| 726 | zip_fp.writestr( |
| 727 | entry, |
| 728 | entry.replace('.', '-').upper(), |
| 729 | zipfile.ZIP_STORED) |
| 730 | return zip_file |
| 731 | |
| 732 | @staticmethod |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 733 | def _parse_property_files_string(data): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 734 | result = {} |
| 735 | for token in data.split(','): |
| 736 | name, info = token.split(':', 1) |
| 737 | result[name] = info |
| 738 | return result |
| 739 | |
| 740 | def _verify_entries(self, input_file, tokens, entries): |
| 741 | for entry in entries: |
| 742 | offset, size = map(int, tokens[entry].split(':')) |
| 743 | with open(input_file, 'rb') as input_fp: |
| 744 | input_fp.seek(offset) |
| 745 | if entry == 'metadata': |
| 746 | expected = b'META-INF/COM/ANDROID/METADATA' |
| 747 | else: |
| 748 | expected = entry.replace('.', '-').upper().encode() |
| 749 | self.assertEqual(expected, input_fp.read(size)) |
| 750 | |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 751 | def test_Compute(self): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 752 | entries = ( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 753 | 'required-entry1', |
| 754 | 'required-entry2', |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 755 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 756 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 757 | property_files = TestPropertyFiles() |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 758 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 759 | property_files_string = property_files.Compute(zip_fp) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 760 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 761 | tokens = self._parse_property_files_string(property_files_string) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 762 | self.assertEqual(3, len(tokens)) |
| 763 | self._verify_entries(zip_file, tokens, entries) |
| 764 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 765 | def test_Compute_withOptionalEntries(self): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 766 | entries = ( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 767 | 'required-entry1', |
| 768 | 'required-entry2', |
| 769 | 'optional-entry1', |
| 770 | 'optional-entry2', |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 771 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 772 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 773 | property_files = TestPropertyFiles() |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 774 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 775 | property_files_string = property_files.Compute(zip_fp) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 776 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 777 | tokens = self._parse_property_files_string(property_files_string) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 778 | self.assertEqual(5, len(tokens)) |
| 779 | self._verify_entries(zip_file, tokens, entries) |
| 780 | |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 781 | def test_Compute_missingRequiredEntry(self): |
| 782 | entries = ( |
| 783 | 'required-entry2', |
| 784 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 785 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 786 | property_files = TestPropertyFiles() |
| 787 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 788 | self.assertRaises(KeyError, property_files.Compute, zip_fp) |
| 789 | |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 790 | def test_Finalize(self): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 791 | entries = [ |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 792 | 'required-entry1', |
| 793 | 'required-entry2', |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 794 | 'META-INF/com/android/metadata', |
| 795 | ] |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 796 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 797 | property_files = TestPropertyFiles() |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 798 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 799 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 800 | zip_fp, reserve_space=False) |
| 801 | streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata)) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 802 | tokens = self._parse_property_files_string(streaming_metadata) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 803 | |
| 804 | self.assertEqual(3, len(tokens)) |
| 805 | # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the |
| 806 | # streaming metadata. |
| 807 | entries[2] = 'metadata' |
| 808 | self._verify_entries(zip_file, tokens, entries) |
| 809 | |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 810 | def test_Finalize_assertReservedLength(self): |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 811 | entries = ( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 812 | 'required-entry1', |
| 813 | 'required-entry2', |
| 814 | 'optional-entry1', |
| 815 | 'optional-entry2', |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 816 | 'META-INF/com/android/metadata', |
| 817 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 818 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 819 | property_files = TestPropertyFiles() |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 820 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 821 | # First get the raw metadata string (i.e. without padding space). |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 822 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 823 | zip_fp, reserve_space=False) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 824 | raw_length = len(raw_metadata) |
| 825 | |
| 826 | # Now pass in the exact expected length. |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 827 | streaming_metadata = property_files.Finalize(zip_fp, raw_length) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 828 | self.assertEqual(raw_length, len(streaming_metadata)) |
| 829 | |
| 830 | # Or pass in insufficient length. |
| 831 | self.assertRaises( |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 832 | PropertyFiles.InsufficientSpaceException, |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 833 | property_files.Finalize, |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 834 | zip_fp, |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 835 | raw_length - 1) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 836 | |
| 837 | # Or pass in a much larger size. |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 838 | streaming_metadata = property_files.Finalize( |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 839 | zip_fp, |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 840 | raw_length + 20) |
Tao Bao | f511049 | 2018-03-02 09:47:43 -0800 | [diff] [blame] | 841 | self.assertEqual(raw_length + 20, len(streaming_metadata)) |
| 842 | self.assertEqual(' ' * 20, streaming_metadata[raw_length:]) |
| 843 | |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 844 | def test_Verify(self): |
| 845 | entries = ( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 846 | 'required-entry1', |
| 847 | 'required-entry2', |
| 848 | 'optional-entry1', |
| 849 | 'optional-entry2', |
| 850 | 'META-INF/com/android/metadata', |
| 851 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 852 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 853 | property_files = TestPropertyFiles() |
| 854 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 855 | # First get the raw metadata string (i.e. without padding space). |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 856 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 857 | zip_fp, reserve_space=False) |
| 858 | |
| 859 | # Should pass the test if verification passes. |
| 860 | property_files.Verify(zip_fp, raw_metadata) |
| 861 | |
| 862 | # Or raise on verification failure. |
| 863 | self.assertRaises( |
| 864 | AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x') |
| 865 | |
| 866 | |
| 867 | class StreamingPropertyFilesTest(PropertyFilesTest): |
| 868 | """Additional sanity checks specialized for StreamingPropertyFiles.""" |
| 869 | |
| 870 | def test_init(self): |
| 871 | property_files = StreamingPropertyFiles() |
| 872 | self.assertEqual('ota-streaming-property-files', property_files.name) |
| 873 | self.assertEqual( |
| 874 | ( |
| 875 | 'payload.bin', |
| 876 | 'payload_properties.txt', |
| 877 | ), |
| 878 | property_files.required) |
| 879 | self.assertEqual( |
| 880 | ( |
Tianjie Xu | 4c05f4a | 2018-09-14 16:24:41 -0700 | [diff] [blame] | 881 | 'care_map.pb', |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 882 | 'care_map.txt', |
| 883 | 'compatibility.zip', |
| 884 | ), |
| 885 | property_files.optional) |
| 886 | |
| 887 | def test_Compute(self): |
| 888 | entries = ( |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 889 | 'payload.bin', |
| 890 | 'payload_properties.txt', |
| 891 | 'care_map.txt', |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 892 | 'compatibility.zip', |
| 893 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 894 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 895 | property_files = StreamingPropertyFiles() |
| 896 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 897 | property_files_string = property_files.Compute(zip_fp) |
| 898 | |
| 899 | tokens = self._parse_property_files_string(property_files_string) |
| 900 | self.assertEqual(5, len(tokens)) |
| 901 | self._verify_entries(zip_file, tokens, entries) |
| 902 | |
| 903 | def test_Finalize(self): |
| 904 | entries = [ |
| 905 | 'payload.bin', |
| 906 | 'payload_properties.txt', |
| 907 | 'care_map.txt', |
| 908 | 'compatibility.zip', |
| 909 | 'META-INF/com/android/metadata', |
| 910 | ] |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 911 | zip_file = self.construct_zip_package(entries) |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 912 | property_files = StreamingPropertyFiles() |
| 913 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 914 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | 6920352 | 2018-03-08 16:09:01 -0800 | [diff] [blame] | 915 | zip_fp, reserve_space=False) |
| 916 | streaming_metadata = property_files.Finalize(zip_fp, len(raw_metadata)) |
| 917 | tokens = self._parse_property_files_string(streaming_metadata) |
| 918 | |
| 919 | self.assertEqual(5, len(tokens)) |
| 920 | # 'META-INF/com/android/metadata' will be key'd as 'metadata' in the |
| 921 | # streaming metadata. |
| 922 | entries[4] = 'metadata' |
| 923 | self._verify_entries(zip_file, tokens, entries) |
| 924 | |
| 925 | def test_Verify(self): |
| 926 | entries = ( |
| 927 | 'payload.bin', |
| 928 | 'payload_properties.txt', |
| 929 | 'care_map.txt', |
| 930 | 'compatibility.zip', |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 931 | 'META-INF/com/android/metadata', |
| 932 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 933 | zip_file = self.construct_zip_package(entries) |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 934 | property_files = StreamingPropertyFiles() |
| 935 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 936 | # First get the raw metadata string (i.e. without padding space). |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 937 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | ae5e4c3 | 2018-03-01 19:30:00 -0800 | [diff] [blame] | 938 | zip_fp, reserve_space=False) |
| 939 | |
| 940 | # Should pass the test if verification passes. |
| 941 | property_files.Verify(zip_fp, raw_metadata) |
| 942 | |
| 943 | # Or raise on verification failure. |
| 944 | self.assertRaises( |
| 945 | AssertionError, property_files.Verify, zip_fp, raw_metadata + 'x') |
| 946 | |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 947 | |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 948 | class AbOtaPropertyFilesTest(PropertyFilesTest): |
| 949 | """Additional sanity checks specialized for AbOtaPropertyFiles.""" |
| 950 | |
| 951 | # The size for payload and metadata signature size. |
| 952 | SIGNATURE_SIZE = 256 |
| 953 | |
| 954 | def setUp(self): |
| 955 | self.testdata_dir = test_utils.get_testdata_dir() |
| 956 | self.assertTrue(os.path.exists(self.testdata_dir)) |
| 957 | |
| 958 | common.OPTIONS.wipe_user_data = False |
| 959 | common.OPTIONS.payload_signer = None |
| 960 | common.OPTIONS.payload_signer_args = None |
| 961 | common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey') |
| 962 | common.OPTIONS.key_passwords = { |
| 963 | common.OPTIONS.package_key : None, |
| 964 | } |
| 965 | |
| 966 | def test_init(self): |
| 967 | property_files = AbOtaPropertyFiles() |
| 968 | self.assertEqual('ota-property-files', property_files.name) |
| 969 | self.assertEqual( |
| 970 | ( |
| 971 | 'payload.bin', |
| 972 | 'payload_properties.txt', |
| 973 | ), |
| 974 | property_files.required) |
| 975 | self.assertEqual( |
| 976 | ( |
Tianjie Xu | 4c05f4a | 2018-09-14 16:24:41 -0700 | [diff] [blame] | 977 | 'care_map.pb', |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 978 | 'care_map.txt', |
| 979 | 'compatibility.zip', |
| 980 | ), |
| 981 | property_files.optional) |
| 982 | |
| 983 | def test_GetPayloadMetadataOffsetAndSize(self): |
| 984 | target_file = construct_target_files() |
| 985 | payload = Payload() |
| 986 | payload.Generate(target_file) |
| 987 | |
| 988 | payload_signer = PayloadSigner() |
| 989 | payload.Sign(payload_signer) |
| 990 | |
| 991 | output_file = common.MakeTempFile(suffix='.zip') |
| 992 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 993 | payload.WriteToZip(output_zip) |
| 994 | |
| 995 | # Find out the payload metadata offset and size. |
| 996 | property_files = AbOtaPropertyFiles() |
| 997 | with zipfile.ZipFile(output_file) as input_zip: |
| 998 | # pylint: disable=protected-access |
| 999 | payload_offset, metadata_total = ( |
| 1000 | property_files._GetPayloadMetadataOffsetAndSize(input_zip)) |
| 1001 | |
| 1002 | # Read in the metadata signature directly. |
| 1003 | with open(output_file, 'rb') as verify_fp: |
| 1004 | verify_fp.seek(payload_offset + metadata_total - self.SIGNATURE_SIZE) |
| 1005 | metadata_signature = verify_fp.read(self.SIGNATURE_SIZE) |
| 1006 | |
| 1007 | # Now we extract the metadata hash via brillo_update_payload script, which |
| 1008 | # will serve as the oracle result. |
| 1009 | payload_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin") |
| 1010 | metadata_sig_file = common.MakeTempFile(prefix="sig-", suffix=".bin") |
| 1011 | cmd = ['brillo_update_payload', 'hash', |
| 1012 | '--unsigned_payload', payload.payload_file, |
| 1013 | '--signature_size', str(self.SIGNATURE_SIZE), |
| 1014 | '--metadata_hash_file', metadata_sig_file, |
| 1015 | '--payload_hash_file', payload_sig_file] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame] | 1016 | proc = common.Run(cmd) |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 1017 | stdoutdata, _ = proc.communicate() |
| 1018 | self.assertEqual( |
| 1019 | 0, proc.returncode, |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame] | 1020 | 'Failed to run brillo_update_payload:\n{}'.format(stdoutdata)) |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 1021 | |
| 1022 | signed_metadata_sig_file = payload_signer.Sign(metadata_sig_file) |
| 1023 | |
| 1024 | # Finally we can compare the two signatures. |
| 1025 | with open(signed_metadata_sig_file, 'rb') as verify_fp: |
| 1026 | self.assertEqual(verify_fp.read(), metadata_signature) |
| 1027 | |
| 1028 | @staticmethod |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 1029 | def construct_zip_package_withValidPayload(with_metadata=False): |
| 1030 | # Cannot use construct_zip_package() since we need a "valid" payload.bin. |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 1031 | target_file = construct_target_files() |
| 1032 | payload = Payload() |
| 1033 | payload.Generate(target_file) |
| 1034 | |
| 1035 | payload_signer = PayloadSigner() |
| 1036 | payload.Sign(payload_signer) |
| 1037 | |
| 1038 | zip_file = common.MakeTempFile(suffix='.zip') |
| 1039 | with zipfile.ZipFile(zip_file, 'w') as zip_fp: |
| 1040 | # 'payload.bin', |
| 1041 | payload.WriteToZip(zip_fp) |
| 1042 | |
| 1043 | # Other entries. |
| 1044 | entries = ['care_map.txt', 'compatibility.zip'] |
| 1045 | |
| 1046 | # Put META-INF/com/android/metadata if needed. |
| 1047 | if with_metadata: |
| 1048 | entries.append('META-INF/com/android/metadata') |
| 1049 | |
| 1050 | for entry in entries: |
| 1051 | zip_fp.writestr( |
| 1052 | entry, entry.replace('.', '-').upper(), zipfile.ZIP_STORED) |
| 1053 | |
| 1054 | return zip_file |
| 1055 | |
| 1056 | def test_Compute(self): |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 1057 | zip_file = self.construct_zip_package_withValidPayload() |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 1058 | property_files = AbOtaPropertyFiles() |
| 1059 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
| 1060 | property_files_string = property_files.Compute(zip_fp) |
| 1061 | |
| 1062 | tokens = self._parse_property_files_string(property_files_string) |
| 1063 | # "6" indcludes the four entries above, one metadata entry, and one entry |
| 1064 | # for payload-metadata.bin. |
| 1065 | self.assertEqual(6, len(tokens)) |
| 1066 | self._verify_entries( |
| 1067 | zip_file, tokens, ('care_map.txt', 'compatibility.zip')) |
| 1068 | |
| 1069 | def test_Finalize(self): |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 1070 | zip_file = self.construct_zip_package_withValidPayload(with_metadata=True) |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 1071 | property_files = AbOtaPropertyFiles() |
| 1072 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 1073 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 1074 | zip_fp, reserve_space=False) |
| 1075 | property_files_string = property_files.Finalize(zip_fp, len(raw_metadata)) |
| 1076 | |
| 1077 | tokens = self._parse_property_files_string(property_files_string) |
| 1078 | # "6" indcludes the four entries above, one metadata entry, and one entry |
| 1079 | # for payload-metadata.bin. |
| 1080 | self.assertEqual(6, len(tokens)) |
| 1081 | self._verify_entries( |
| 1082 | zip_file, tokens, ('care_map.txt', 'compatibility.zip')) |
| 1083 | |
| 1084 | def test_Verify(self): |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 1085 | zip_file = self.construct_zip_package_withValidPayload(with_metadata=True) |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 1086 | property_files = AbOtaPropertyFiles() |
| 1087 | with zipfile.ZipFile(zip_file, 'r') as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 1088 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | b630467 | 2018-03-08 16:28:33 -0800 | [diff] [blame] | 1089 | zip_fp, reserve_space=False) |
| 1090 | |
| 1091 | property_files.Verify(zip_fp, raw_metadata) |
| 1092 | |
| 1093 | |
Tao Bao | c0746f4 | 2018-02-21 13:17:22 -0800 | [diff] [blame] | 1094 | class NonAbOtaPropertyFilesTest(PropertyFilesTest): |
| 1095 | """Additional sanity checks specialized for NonAbOtaPropertyFiles.""" |
| 1096 | |
| 1097 | def test_init(self): |
| 1098 | property_files = NonAbOtaPropertyFiles() |
| 1099 | self.assertEqual('ota-property-files', property_files.name) |
| 1100 | self.assertEqual((), property_files.required) |
| 1101 | self.assertEqual((), property_files.optional) |
| 1102 | |
| 1103 | def test_Compute(self): |
| 1104 | entries = () |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 1105 | zip_file = self.construct_zip_package(entries) |
Tao Bao | c0746f4 | 2018-02-21 13:17:22 -0800 | [diff] [blame] | 1106 | property_files = NonAbOtaPropertyFiles() |
| 1107 | with zipfile.ZipFile(zip_file) as zip_fp: |
| 1108 | property_files_string = property_files.Compute(zip_fp) |
| 1109 | |
| 1110 | tokens = self._parse_property_files_string(property_files_string) |
| 1111 | self.assertEqual(1, len(tokens)) |
| 1112 | self._verify_entries(zip_file, tokens, entries) |
| 1113 | |
| 1114 | def test_Finalize(self): |
| 1115 | entries = [ |
| 1116 | 'META-INF/com/android/metadata', |
| 1117 | ] |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 1118 | zip_file = self.construct_zip_package(entries) |
Tao Bao | c0746f4 | 2018-02-21 13:17:22 -0800 | [diff] [blame] | 1119 | property_files = NonAbOtaPropertyFiles() |
| 1120 | with zipfile.ZipFile(zip_file) as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 1121 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | c0746f4 | 2018-02-21 13:17:22 -0800 | [diff] [blame] | 1122 | zip_fp, reserve_space=False) |
| 1123 | property_files_string = property_files.Finalize(zip_fp, len(raw_metadata)) |
| 1124 | tokens = self._parse_property_files_string(property_files_string) |
| 1125 | |
| 1126 | self.assertEqual(1, len(tokens)) |
| 1127 | # 'META-INF/com/android/metadata' will be key'd as 'metadata'. |
| 1128 | entries[0] = 'metadata' |
| 1129 | self._verify_entries(zip_file, tokens, entries) |
| 1130 | |
| 1131 | def test_Verify(self): |
| 1132 | entries = ( |
| 1133 | 'META-INF/com/android/metadata', |
| 1134 | ) |
Tao Bao | 3bf8c65 | 2018-03-16 12:59:42 -0700 | [diff] [blame] | 1135 | zip_file = self.construct_zip_package(entries) |
Tao Bao | c0746f4 | 2018-02-21 13:17:22 -0800 | [diff] [blame] | 1136 | property_files = NonAbOtaPropertyFiles() |
| 1137 | with zipfile.ZipFile(zip_file) as zip_fp: |
Zhomart Mukhamejanov | 603655f | 2018-05-04 12:35:09 -0700 | [diff] [blame] | 1138 | raw_metadata = property_files.GetPropertyFilesString( |
Tao Bao | c0746f4 | 2018-02-21 13:17:22 -0800 | [diff] [blame] | 1139 | zip_fp, reserve_space=False) |
| 1140 | |
| 1141 | property_files.Verify(zip_fp, raw_metadata) |
| 1142 | |
| 1143 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame^] | 1144 | class PayloadSignerTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 1145 | |
| 1146 | SIGFILE = 'sigfile.bin' |
| 1147 | SIGNED_SIGFILE = 'signed-sigfile.bin' |
| 1148 | |
| 1149 | def setUp(self): |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 1150 | self.testdata_dir = test_utils.get_testdata_dir() |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 1151 | self.assertTrue(os.path.exists(self.testdata_dir)) |
| 1152 | |
| 1153 | common.OPTIONS.payload_signer = None |
| 1154 | common.OPTIONS.payload_signer_args = [] |
| 1155 | common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey') |
| 1156 | common.OPTIONS.key_passwords = { |
| 1157 | common.OPTIONS.package_key : None, |
| 1158 | } |
| 1159 | |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 1160 | def _assertFilesEqual(self, file1, file2): |
| 1161 | with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2: |
| 1162 | self.assertEqual(fp1.read(), fp2.read()) |
| 1163 | |
| 1164 | def test_init(self): |
| 1165 | payload_signer = PayloadSigner() |
| 1166 | self.assertEqual('openssl', payload_signer.signer) |
| 1167 | |
| 1168 | def test_init_withPassword(self): |
| 1169 | common.OPTIONS.package_key = os.path.join( |
| 1170 | self.testdata_dir, 'testkey_with_passwd') |
| 1171 | common.OPTIONS.key_passwords = { |
| 1172 | common.OPTIONS.package_key : 'foo', |
| 1173 | } |
| 1174 | payload_signer = PayloadSigner() |
| 1175 | self.assertEqual('openssl', payload_signer.signer) |
| 1176 | |
| 1177 | def test_init_withExternalSigner(self): |
| 1178 | common.OPTIONS.payload_signer = 'abc' |
| 1179 | common.OPTIONS.payload_signer_args = ['arg1', 'arg2'] |
| 1180 | payload_signer = PayloadSigner() |
| 1181 | self.assertEqual('abc', payload_signer.signer) |
| 1182 | self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args) |
| 1183 | |
| 1184 | def test_Sign(self): |
| 1185 | payload_signer = PayloadSigner() |
| 1186 | input_file = os.path.join(self.testdata_dir, self.SIGFILE) |
| 1187 | signed_file = payload_signer.Sign(input_file) |
| 1188 | |
| 1189 | verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE) |
| 1190 | self._assertFilesEqual(verify_file, signed_file) |
| 1191 | |
| 1192 | def test_Sign_withExternalSigner_openssl(self): |
| 1193 | """Uses openssl as the external payload signer.""" |
| 1194 | common.OPTIONS.payload_signer = 'openssl' |
| 1195 | common.OPTIONS.payload_signer_args = [ |
| 1196 | 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey', |
| 1197 | os.path.join(self.testdata_dir, 'testkey.pk8'), |
| 1198 | '-pkeyopt', 'digest:sha256'] |
| 1199 | payload_signer = PayloadSigner() |
| 1200 | input_file = os.path.join(self.testdata_dir, self.SIGFILE) |
| 1201 | signed_file = payload_signer.Sign(input_file) |
| 1202 | |
| 1203 | verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE) |
| 1204 | self._assertFilesEqual(verify_file, signed_file) |
| 1205 | |
| 1206 | def test_Sign_withExternalSigner_script(self): |
| 1207 | """Uses testdata/payload_signer.sh as the external payload signer.""" |
| 1208 | common.OPTIONS.payload_signer = os.path.join( |
| 1209 | self.testdata_dir, 'payload_signer.sh') |
| 1210 | common.OPTIONS.payload_signer_args = [ |
| 1211 | os.path.join(self.testdata_dir, 'testkey.pk8')] |
| 1212 | payload_signer = PayloadSigner() |
| 1213 | input_file = os.path.join(self.testdata_dir, self.SIGFILE) |
| 1214 | signed_file = payload_signer.Sign(input_file) |
| 1215 | |
| 1216 | verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE) |
| 1217 | self._assertFilesEqual(verify_file, signed_file) |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1218 | |
| 1219 | |
Tao Bao | 65b94e9 | 2018-10-11 21:57:26 -0700 | [diff] [blame^] | 1220 | class PayloadTest(test_utils.ReleaseToolsTestCase): |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1221 | |
| 1222 | def setUp(self): |
Tao Bao | 04e1f01 | 2018-02-04 12:13:35 -0800 | [diff] [blame] | 1223 | self.testdata_dir = test_utils.get_testdata_dir() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1224 | self.assertTrue(os.path.exists(self.testdata_dir)) |
| 1225 | |
| 1226 | common.OPTIONS.wipe_user_data = False |
| 1227 | common.OPTIONS.payload_signer = None |
| 1228 | common.OPTIONS.payload_signer_args = None |
| 1229 | common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey') |
| 1230 | common.OPTIONS.key_passwords = { |
| 1231 | common.OPTIONS.package_key : None, |
| 1232 | } |
| 1233 | |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1234 | @staticmethod |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1235 | def _create_payload_full(secondary=False): |
| 1236 | target_file = construct_target_files(secondary) |
Tao Bao | 667ff57 | 2018-02-10 00:02:40 -0800 | [diff] [blame] | 1237 | payload = Payload(secondary) |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1238 | payload.Generate(target_file) |
| 1239 | return payload |
| 1240 | |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1241 | @staticmethod |
| 1242 | def _create_payload_incremental(): |
| 1243 | target_file = construct_target_files() |
| 1244 | source_file = construct_target_files() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1245 | payload = Payload() |
| 1246 | payload.Generate(target_file, source_file) |
| 1247 | return payload |
| 1248 | |
| 1249 | def test_Generate_full(self): |
| 1250 | payload = self._create_payload_full() |
| 1251 | self.assertTrue(os.path.exists(payload.payload_file)) |
| 1252 | |
| 1253 | def test_Generate_incremental(self): |
| 1254 | payload = self._create_payload_incremental() |
| 1255 | self.assertTrue(os.path.exists(payload.payload_file)) |
| 1256 | |
| 1257 | def test_Generate_additionalArgs(self): |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1258 | target_file = construct_target_files() |
| 1259 | source_file = construct_target_files() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1260 | payload = Payload() |
| 1261 | # This should work the same as calling payload.Generate(target_file, |
| 1262 | # source_file). |
| 1263 | payload.Generate( |
| 1264 | target_file, additional_args=["--source_image", source_file]) |
| 1265 | self.assertTrue(os.path.exists(payload.payload_file)) |
| 1266 | |
| 1267 | def test_Generate_invalidInput(self): |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1268 | target_file = construct_target_files() |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1269 | common.ZipDelete(target_file, 'IMAGES/vendor.img') |
| 1270 | payload = Payload() |
| 1271 | self.assertRaises(AssertionError, payload.Generate, target_file) |
| 1272 | |
| 1273 | def test_Sign_full(self): |
| 1274 | payload = self._create_payload_full() |
| 1275 | payload.Sign(PayloadSigner()) |
| 1276 | |
| 1277 | output_file = common.MakeTempFile(suffix='.zip') |
| 1278 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1279 | payload.WriteToZip(output_zip) |
| 1280 | |
| 1281 | import check_ota_package_signature |
| 1282 | check_ota_package_signature.VerifyAbOtaPayload( |
| 1283 | os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 1284 | output_file) |
| 1285 | |
| 1286 | def test_Sign_incremental(self): |
| 1287 | payload = self._create_payload_incremental() |
| 1288 | payload.Sign(PayloadSigner()) |
| 1289 | |
| 1290 | output_file = common.MakeTempFile(suffix='.zip') |
| 1291 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1292 | payload.WriteToZip(output_zip) |
| 1293 | |
| 1294 | import check_ota_package_signature |
| 1295 | check_ota_package_signature.VerifyAbOtaPayload( |
| 1296 | os.path.join(self.testdata_dir, 'testkey.x509.pem'), |
| 1297 | output_file) |
| 1298 | |
| 1299 | def test_Sign_withDataWipe(self): |
| 1300 | common.OPTIONS.wipe_user_data = True |
| 1301 | payload = self._create_payload_full() |
| 1302 | payload.Sign(PayloadSigner()) |
| 1303 | |
| 1304 | with open(payload.payload_properties) as properties_fp: |
| 1305 | self.assertIn("POWERWASH=1", properties_fp.read()) |
| 1306 | |
Tao Bao | 667ff57 | 2018-02-10 00:02:40 -0800 | [diff] [blame] | 1307 | def test_Sign_secondary(self): |
| 1308 | payload = self._create_payload_full(secondary=True) |
| 1309 | payload.Sign(PayloadSigner()) |
| 1310 | |
| 1311 | with open(payload.payload_properties) as properties_fp: |
| 1312 | self.assertIn("SWITCH_SLOT_ON_REBOOT=0", properties_fp.read()) |
| 1313 | |
Tao Bao | c7b403a | 2018-01-30 18:19:04 -0800 | [diff] [blame] | 1314 | def test_Sign_badSigner(self): |
| 1315 | """Tests that signing failure can be captured.""" |
| 1316 | payload = self._create_payload_full() |
| 1317 | payload_signer = PayloadSigner() |
| 1318 | payload_signer.signer_args.append('bad-option') |
| 1319 | self.assertRaises(AssertionError, payload.Sign, payload_signer) |
| 1320 | |
| 1321 | def test_WriteToZip(self): |
| 1322 | payload = self._create_payload_full() |
| 1323 | payload.Sign(PayloadSigner()) |
| 1324 | |
| 1325 | output_file = common.MakeTempFile(suffix='.zip') |
| 1326 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1327 | payload.WriteToZip(output_zip) |
| 1328 | |
| 1329 | with zipfile.ZipFile(output_file) as verify_zip: |
| 1330 | # First make sure we have the essential entries. |
| 1331 | namelist = verify_zip.namelist() |
| 1332 | self.assertIn(Payload.PAYLOAD_BIN, namelist) |
| 1333 | self.assertIn(Payload.PAYLOAD_PROPERTIES_TXT, namelist) |
| 1334 | |
| 1335 | # Then assert these entries are stored. |
| 1336 | for entry_info in verify_zip.infolist(): |
| 1337 | if entry_info.filename not in (Payload.PAYLOAD_BIN, |
| 1338 | Payload.PAYLOAD_PROPERTIES_TXT): |
| 1339 | continue |
| 1340 | self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type) |
| 1341 | |
| 1342 | def test_WriteToZip_unsignedPayload(self): |
| 1343 | """Unsigned payloads should not be allowed to be written to zip.""" |
| 1344 | payload = self._create_payload_full() |
| 1345 | |
| 1346 | output_file = common.MakeTempFile(suffix='.zip') |
| 1347 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1348 | self.assertRaises(AssertionError, payload.WriteToZip, output_zip) |
| 1349 | |
| 1350 | # Also test with incremental payload. |
| 1351 | payload = self._create_payload_incremental() |
| 1352 | |
| 1353 | output_file = common.MakeTempFile(suffix='.zip') |
| 1354 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
| 1355 | self.assertRaises(AssertionError, payload.WriteToZip, output_zip) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1356 | |
| 1357 | def test_WriteToZip_secondary(self): |
| 1358 | payload = self._create_payload_full(secondary=True) |
| 1359 | payload.Sign(PayloadSigner()) |
| 1360 | |
| 1361 | output_file = common.MakeTempFile(suffix='.zip') |
| 1362 | with zipfile.ZipFile(output_file, 'w') as output_zip: |
Tao Bao | 667ff57 | 2018-02-10 00:02:40 -0800 | [diff] [blame] | 1363 | payload.WriteToZip(output_zip) |
Tao Bao | f7140c0 | 2018-01-30 17:09:24 -0800 | [diff] [blame] | 1364 | |
| 1365 | with zipfile.ZipFile(output_file) as verify_zip: |
| 1366 | # First make sure we have the essential entries. |
| 1367 | namelist = verify_zip.namelist() |
| 1368 | self.assertIn(Payload.SECONDARY_PAYLOAD_BIN, namelist) |
| 1369 | self.assertIn(Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT, namelist) |
| 1370 | |
| 1371 | # Then assert these entries are stored. |
| 1372 | for entry_info in verify_zip.infolist(): |
| 1373 | if entry_info.filename not in ( |
| 1374 | Payload.SECONDARY_PAYLOAD_BIN, |
| 1375 | Payload.SECONDARY_PAYLOAD_PROPERTIES_TXT): |
| 1376 | continue |
| 1377 | self.assertEqual(zipfile.ZIP_STORED, entry_info.compress_type) |