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