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 | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 18 | import os.path |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 19 | import unittest |
| 20 | |
| 21 | import common |
| 22 | from ota_from_target_files import ( |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 23 | _LoadOemDicts, BuildInfo, GetPackageMetadata, PayloadSigner, |
| 24 | WriteFingerprintAssertion) |
| 25 | |
| 26 | |
| 27 | def get_testdata_dir(): |
| 28 | """Returns the testdata dir, in relative to the script dir.""" |
| 29 | # The script dir is the one we want, which could be different from pwd. |
| 30 | current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 31 | return os.path.join(current_dir, 'testdata') |
Tao Bao | 481bab8 | 2017-12-21 11:23:09 -0800 | [diff] [blame] | 32 | |
| 33 | |
| 34 | class MockScriptWriter(object): |
| 35 | """A class that mocks edify_generator.EdifyGenerator. |
| 36 | |
| 37 | It simply pushes the incoming arguments onto script stack, which is to assert |
| 38 | the calls to EdifyGenerator functions. |
| 39 | """ |
| 40 | |
| 41 | def __init__(self): |
| 42 | self.script = [] |
| 43 | |
| 44 | def Mount(self, *args): |
| 45 | self.script.append(('Mount',) + args) |
| 46 | |
| 47 | def AssertDevice(self, *args): |
| 48 | self.script.append(('AssertDevice',) + args) |
| 49 | |
| 50 | def AssertOemProperty(self, *args): |
| 51 | self.script.append(('AssertOemProperty',) + args) |
| 52 | |
| 53 | def AssertFingerprintOrThumbprint(self, *args): |
| 54 | self.script.append(('AssertFingerprintOrThumbprint',) + args) |
| 55 | |
| 56 | def AssertSomeFingerprint(self, *args): |
| 57 | self.script.append(('AssertSomeFingerprint',) + args) |
| 58 | |
| 59 | def AssertSomeThumbprint(self, *args): |
| 60 | self.script.append(('AssertSomeThumbprint',) + args) |
| 61 | |
| 62 | |
| 63 | class BuildInfoTest(unittest.TestCase): |
| 64 | |
| 65 | TEST_INFO_DICT = { |
| 66 | 'build.prop' : { |
| 67 | 'ro.product.device' : 'product-device', |
| 68 | 'ro.product.name' : 'product-name', |
| 69 | 'ro.build.fingerprint' : 'build-fingerprint', |
| 70 | 'ro.build.foo' : 'build-foo', |
| 71 | }, |
| 72 | 'vendor.build.prop' : { |
| 73 | 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint', |
| 74 | }, |
| 75 | 'property1' : 'value1', |
| 76 | 'property2' : 4096, |
| 77 | } |
| 78 | |
| 79 | TEST_INFO_DICT_USES_OEM_PROPS = { |
| 80 | 'build.prop' : { |
| 81 | 'ro.product.name' : 'product-name', |
| 82 | 'ro.build.thumbprint' : 'build-thumbprint', |
| 83 | 'ro.build.bar' : 'build-bar', |
| 84 | }, |
| 85 | 'vendor.build.prop' : { |
| 86 | 'ro.vendor.build.fingerprint' : 'vendor-build-fingerprint', |
| 87 | }, |
| 88 | 'property1' : 'value1', |
| 89 | 'property2' : 4096, |
| 90 | 'oem_fingerprint_properties' : 'ro.product.device ro.product.brand', |
| 91 | } |
| 92 | |
| 93 | TEST_OEM_DICTS = [ |
| 94 | { |
| 95 | 'ro.product.brand' : 'brand1', |
| 96 | 'ro.product.device' : 'device1', |
| 97 | }, |
| 98 | { |
| 99 | 'ro.product.brand' : 'brand2', |
| 100 | 'ro.product.device' : 'device2', |
| 101 | }, |
| 102 | { |
| 103 | 'ro.product.brand' : 'brand3', |
| 104 | 'ro.product.device' : 'device3', |
| 105 | }, |
| 106 | ] |
| 107 | |
| 108 | def test_init(self): |
| 109 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 110 | self.assertEqual('product-device', target_info.device) |
| 111 | self.assertEqual('build-fingerprint', target_info.fingerprint) |
| 112 | self.assertFalse(target_info.is_ab) |
| 113 | self.assertIsNone(target_info.oem_props) |
| 114 | |
| 115 | def test_init_with_oem_props(self): |
| 116 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 117 | self.TEST_OEM_DICTS) |
| 118 | self.assertEqual('device1', target_info.device) |
| 119 | self.assertEqual('brand1/product-name/device1:build-thumbprint', |
| 120 | target_info.fingerprint) |
| 121 | |
| 122 | # Swap the order in oem_dicts, which would lead to different BuildInfo. |
| 123 | oem_dicts = copy.copy(self.TEST_OEM_DICTS) |
| 124 | oem_dicts[0], oem_dicts[2] = oem_dicts[2], oem_dicts[0] |
| 125 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, oem_dicts) |
| 126 | self.assertEqual('device3', target_info.device) |
| 127 | self.assertEqual('brand3/product-name/device3:build-thumbprint', |
| 128 | target_info.fingerprint) |
| 129 | |
| 130 | # Missing oem_dict should be rejected. |
| 131 | self.assertRaises(AssertionError, BuildInfo, |
| 132 | self.TEST_INFO_DICT_USES_OEM_PROPS, None) |
| 133 | |
| 134 | def test___getitem__(self): |
| 135 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 136 | self.assertEqual('value1', target_info['property1']) |
| 137 | self.assertEqual(4096, target_info['property2']) |
| 138 | self.assertEqual('build-foo', target_info['build.prop']['ro.build.foo']) |
| 139 | |
| 140 | def test___getitem__with_oem_props(self): |
| 141 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 142 | self.TEST_OEM_DICTS) |
| 143 | self.assertEqual('value1', target_info['property1']) |
| 144 | self.assertEqual(4096, target_info['property2']) |
| 145 | self.assertRaises(KeyError, |
| 146 | lambda: target_info['build.prop']['ro.build.foo']) |
| 147 | |
| 148 | def test_get(self): |
| 149 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 150 | self.assertEqual('value1', target_info.get('property1')) |
| 151 | self.assertEqual(4096, target_info.get('property2')) |
| 152 | self.assertEqual(4096, target_info.get('property2', 1024)) |
| 153 | self.assertEqual(1024, target_info.get('property-nonexistent', 1024)) |
| 154 | self.assertEqual('build-foo', target_info.get('build.prop')['ro.build.foo']) |
| 155 | |
| 156 | def test_get_with_oem_props(self): |
| 157 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 158 | self.TEST_OEM_DICTS) |
| 159 | self.assertEqual('value1', target_info.get('property1')) |
| 160 | self.assertEqual(4096, target_info.get('property2')) |
| 161 | self.assertEqual(4096, target_info.get('property2', 1024)) |
| 162 | self.assertEqual(1024, target_info.get('property-nonexistent', 1024)) |
| 163 | self.assertIsNone(target_info.get('build.prop').get('ro.build.foo')) |
| 164 | self.assertRaises(KeyError, |
| 165 | lambda: target_info.get('build.prop')['ro.build.foo']) |
| 166 | |
| 167 | def test_GetBuildProp(self): |
| 168 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 169 | self.assertEqual('build-foo', target_info.GetBuildProp('ro.build.foo')) |
| 170 | self.assertRaises(common.ExternalError, target_info.GetBuildProp, |
| 171 | 'ro.build.nonexistent') |
| 172 | |
| 173 | def test_GetBuildProp_with_oem_props(self): |
| 174 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 175 | self.TEST_OEM_DICTS) |
| 176 | self.assertEqual('build-bar', target_info.GetBuildProp('ro.build.bar')) |
| 177 | self.assertRaises(common.ExternalError, target_info.GetBuildProp, |
| 178 | 'ro.build.nonexistent') |
| 179 | |
| 180 | def test_GetVendorBuildProp(self): |
| 181 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 182 | self.assertEqual('vendor-build-fingerprint', |
| 183 | target_info.GetVendorBuildProp( |
| 184 | 'ro.vendor.build.fingerprint')) |
| 185 | self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp, |
| 186 | 'ro.build.nonexistent') |
| 187 | |
| 188 | def test_GetVendorBuildProp_with_oem_props(self): |
| 189 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 190 | self.TEST_OEM_DICTS) |
| 191 | self.assertEqual('vendor-build-fingerprint', |
| 192 | target_info.GetVendorBuildProp( |
| 193 | 'ro.vendor.build.fingerprint')) |
| 194 | self.assertRaises(common.ExternalError, target_info.GetVendorBuildProp, |
| 195 | 'ro.build.nonexistent') |
| 196 | |
| 197 | def test_WriteMountOemScript(self): |
| 198 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 199 | self.TEST_OEM_DICTS) |
| 200 | script_writer = MockScriptWriter() |
| 201 | target_info.WriteMountOemScript(script_writer) |
| 202 | self.assertEqual([('Mount', '/oem', None)], script_writer.script) |
| 203 | |
| 204 | def test_WriteDeviceAssertions(self): |
| 205 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 206 | script_writer = MockScriptWriter() |
| 207 | target_info.WriteDeviceAssertions(script_writer, False) |
| 208 | self.assertEqual([('AssertDevice', 'product-device')], script_writer.script) |
| 209 | |
| 210 | def test_WriteDeviceAssertions_with_oem_props(self): |
| 211 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 212 | self.TEST_OEM_DICTS) |
| 213 | script_writer = MockScriptWriter() |
| 214 | target_info.WriteDeviceAssertions(script_writer, False) |
| 215 | self.assertEqual( |
| 216 | [ |
| 217 | ('AssertOemProperty', 'ro.product.device', |
| 218 | ['device1', 'device2', 'device3'], False), |
| 219 | ('AssertOemProperty', 'ro.product.brand', |
| 220 | ['brand1', 'brand2', 'brand3'], False), |
| 221 | ], |
| 222 | script_writer.script) |
| 223 | |
| 224 | def test_WriteFingerprintAssertion_without_oem_props(self): |
| 225 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 226 | source_info_dict = copy.deepcopy(self.TEST_INFO_DICT) |
| 227 | source_info_dict['build.prop']['ro.build.fingerprint'] = ( |
| 228 | 'source-build-fingerprint') |
| 229 | source_info = BuildInfo(source_info_dict, None) |
| 230 | |
| 231 | script_writer = MockScriptWriter() |
| 232 | WriteFingerprintAssertion(script_writer, target_info, source_info) |
| 233 | self.assertEqual( |
| 234 | [('AssertSomeFingerprint', 'source-build-fingerprint', |
| 235 | 'build-fingerprint')], |
| 236 | script_writer.script) |
| 237 | |
| 238 | def test_WriteFingerprintAssertion_with_source_oem_props(self): |
| 239 | target_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 240 | source_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 241 | self.TEST_OEM_DICTS) |
| 242 | |
| 243 | script_writer = MockScriptWriter() |
| 244 | WriteFingerprintAssertion(script_writer, target_info, source_info) |
| 245 | self.assertEqual( |
| 246 | [('AssertFingerprintOrThumbprint', 'build-fingerprint', |
| 247 | 'build-thumbprint')], |
| 248 | script_writer.script) |
| 249 | |
| 250 | def test_WriteFingerprintAssertion_with_target_oem_props(self): |
| 251 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 252 | self.TEST_OEM_DICTS) |
| 253 | source_info = BuildInfo(self.TEST_INFO_DICT, None) |
| 254 | |
| 255 | script_writer = MockScriptWriter() |
| 256 | WriteFingerprintAssertion(script_writer, target_info, source_info) |
| 257 | self.assertEqual( |
| 258 | [('AssertFingerprintOrThumbprint', 'build-fingerprint', |
| 259 | 'build-thumbprint')], |
| 260 | script_writer.script) |
| 261 | |
| 262 | def test_WriteFingerprintAssertion_with_both_oem_props(self): |
| 263 | target_info = BuildInfo(self.TEST_INFO_DICT_USES_OEM_PROPS, |
| 264 | self.TEST_OEM_DICTS) |
| 265 | source_info_dict = copy.deepcopy(self.TEST_INFO_DICT_USES_OEM_PROPS) |
| 266 | source_info_dict['build.prop']['ro.build.thumbprint'] = ( |
| 267 | 'source-build-thumbprint') |
| 268 | source_info = BuildInfo(source_info_dict, self.TEST_OEM_DICTS) |
| 269 | |
| 270 | script_writer = MockScriptWriter() |
| 271 | WriteFingerprintAssertion(script_writer, target_info, source_info) |
| 272 | self.assertEqual( |
| 273 | [('AssertSomeThumbprint', 'build-thumbprint', |
| 274 | 'source-build-thumbprint')], |
| 275 | script_writer.script) |
| 276 | |
| 277 | |
| 278 | class LoadOemDictsTest(unittest.TestCase): |
| 279 | |
| 280 | def tearDown(self): |
| 281 | common.Cleanup() |
| 282 | |
| 283 | def test_NoneDict(self): |
| 284 | self.assertIsNone(_LoadOemDicts(None)) |
| 285 | |
| 286 | def test_SingleDict(self): |
| 287 | dict_file = common.MakeTempFile() |
| 288 | with open(dict_file, 'w') as dict_fp: |
| 289 | dict_fp.write('abc=1\ndef=2\nxyz=foo\na.b.c=bar\n') |
| 290 | |
| 291 | oem_dicts = _LoadOemDicts([dict_file]) |
| 292 | self.assertEqual(1, len(oem_dicts)) |
| 293 | self.assertEqual('foo', oem_dicts[0]['xyz']) |
| 294 | self.assertEqual('bar', oem_dicts[0]['a.b.c']) |
| 295 | |
| 296 | def test_MultipleDicts(self): |
| 297 | oem_source = [] |
| 298 | for i in range(3): |
| 299 | dict_file = common.MakeTempFile() |
| 300 | with open(dict_file, 'w') as dict_fp: |
| 301 | dict_fp.write( |
| 302 | 'ro.build.index={}\ndef=2\nxyz=foo\na.b.c=bar\n'.format(i)) |
| 303 | oem_source.append(dict_file) |
| 304 | |
| 305 | oem_dicts = _LoadOemDicts(oem_source) |
| 306 | self.assertEqual(3, len(oem_dicts)) |
| 307 | for i, oem_dict in enumerate(oem_dicts): |
| 308 | self.assertEqual('2', oem_dict['def']) |
| 309 | self.assertEqual('foo', oem_dict['xyz']) |
| 310 | self.assertEqual('bar', oem_dict['a.b.c']) |
| 311 | self.assertEqual('{}'.format(i), oem_dict['ro.build.index']) |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 312 | |
| 313 | |
| 314 | class OtaFromTargetFilesTest(unittest.TestCase): |
| 315 | |
| 316 | TEST_TARGET_INFO_DICT = { |
| 317 | 'build.prop' : { |
| 318 | 'ro.product.device' : 'product-device', |
| 319 | 'ro.build.fingerprint' : 'build-fingerprint-target', |
| 320 | 'ro.build.version.incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame^] | 321 | 'ro.build.version.sdk' : '27', |
| 322 | 'ro.build.version.security_patch' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 323 | 'ro.build.date.utc' : '1500000000', |
| 324 | }, |
| 325 | } |
| 326 | |
| 327 | TEST_SOURCE_INFO_DICT = { |
| 328 | 'build.prop' : { |
| 329 | 'ro.product.device' : 'product-device', |
| 330 | 'ro.build.fingerprint' : 'build-fingerprint-source', |
| 331 | 'ro.build.version.incremental' : 'build-version-incremental-source', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame^] | 332 | 'ro.build.version.sdk' : '25', |
| 333 | 'ro.build.version.security_patch' : '2016-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 334 | 'ro.build.date.utc' : '1400000000', |
| 335 | }, |
| 336 | } |
| 337 | |
| 338 | def setUp(self): |
| 339 | # Reset the global options as in ota_from_target_files.py. |
| 340 | common.OPTIONS.incremental_source = None |
| 341 | common.OPTIONS.downgrade = False |
| 342 | common.OPTIONS.timestamp = False |
| 343 | common.OPTIONS.wipe_user_data = False |
| 344 | |
| 345 | def test_GetPackageMetadata_abOta_full(self): |
| 346 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 347 | target_info_dict['ab_update'] = 'true' |
| 348 | target_info = BuildInfo(target_info_dict, None) |
| 349 | metadata = GetPackageMetadata(target_info) |
| 350 | self.assertDictEqual( |
| 351 | { |
| 352 | 'ota-type' : 'AB', |
| 353 | 'ota-required-cache' : '0', |
| 354 | 'post-build' : 'build-fingerprint-target', |
| 355 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame^] | 356 | 'post-sdk-level' : '27', |
| 357 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 358 | 'post-timestamp' : '1500000000', |
| 359 | 'pre-device' : 'product-device', |
| 360 | }, |
| 361 | metadata) |
| 362 | |
| 363 | def test_GetPackageMetadata_abOta_incremental(self): |
| 364 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 365 | target_info_dict['ab_update'] = 'true' |
| 366 | target_info = BuildInfo(target_info_dict, None) |
| 367 | source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None) |
| 368 | common.OPTIONS.incremental_source = '' |
| 369 | metadata = GetPackageMetadata(target_info, source_info) |
| 370 | self.assertDictEqual( |
| 371 | { |
| 372 | 'ota-type' : 'AB', |
| 373 | 'ota-required-cache' : '0', |
| 374 | 'post-build' : 'build-fingerprint-target', |
| 375 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame^] | 376 | 'post-sdk-level' : '27', |
| 377 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 378 | 'post-timestamp' : '1500000000', |
| 379 | 'pre-device' : 'product-device', |
| 380 | 'pre-build' : 'build-fingerprint-source', |
| 381 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 382 | }, |
| 383 | metadata) |
| 384 | |
| 385 | def test_GetPackageMetadata_nonAbOta_full(self): |
| 386 | target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
| 387 | metadata = GetPackageMetadata(target_info) |
| 388 | self.assertDictEqual( |
| 389 | { |
| 390 | 'ota-type' : 'BLOCK', |
| 391 | 'post-build' : 'build-fingerprint-target', |
| 392 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame^] | 393 | 'post-sdk-level' : '27', |
| 394 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 395 | 'post-timestamp' : '1500000000', |
| 396 | 'pre-device' : 'product-device', |
| 397 | }, |
| 398 | metadata) |
| 399 | |
| 400 | def test_GetPackageMetadata_nonAbOta_incremental(self): |
| 401 | target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
| 402 | source_info = BuildInfo(self.TEST_SOURCE_INFO_DICT, None) |
| 403 | common.OPTIONS.incremental_source = '' |
| 404 | metadata = GetPackageMetadata(target_info, source_info) |
| 405 | self.assertDictEqual( |
| 406 | { |
| 407 | 'ota-type' : 'BLOCK', |
| 408 | 'post-build' : 'build-fingerprint-target', |
| 409 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame^] | 410 | 'post-sdk-level' : '27', |
| 411 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 412 | 'post-timestamp' : '1500000000', |
| 413 | 'pre-device' : 'product-device', |
| 414 | 'pre-build' : 'build-fingerprint-source', |
| 415 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 416 | }, |
| 417 | metadata) |
| 418 | |
| 419 | def test_GetPackageMetadata_wipe(self): |
| 420 | target_info = BuildInfo(self.TEST_TARGET_INFO_DICT, None) |
| 421 | common.OPTIONS.wipe_user_data = True |
| 422 | metadata = GetPackageMetadata(target_info) |
| 423 | self.assertDictEqual( |
| 424 | { |
| 425 | 'ota-type' : 'BLOCK', |
| 426 | 'ota-wipe' : 'yes', |
| 427 | 'post-build' : 'build-fingerprint-target', |
| 428 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame^] | 429 | 'post-sdk-level' : '27', |
| 430 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 431 | 'post-timestamp' : '1500000000', |
| 432 | 'pre-device' : 'product-device', |
| 433 | }, |
| 434 | metadata) |
| 435 | |
| 436 | @staticmethod |
| 437 | def _test_GetPackageMetadata_swapBuildTimestamps(target_info, source_info): |
| 438 | (target_info['build.prop']['ro.build.date.utc'], |
| 439 | source_info['build.prop']['ro.build.date.utc']) = ( |
| 440 | source_info['build.prop']['ro.build.date.utc'], |
| 441 | target_info['build.prop']['ro.build.date.utc']) |
| 442 | |
| 443 | def test_GetPackageMetadata_unintentionalDowngradeDetected(self): |
| 444 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 445 | source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT) |
| 446 | self._test_GetPackageMetadata_swapBuildTimestamps( |
| 447 | target_info_dict, source_info_dict) |
| 448 | |
| 449 | target_info = BuildInfo(target_info_dict, None) |
| 450 | source_info = BuildInfo(source_info_dict, None) |
| 451 | common.OPTIONS.incremental_source = '' |
| 452 | self.assertRaises(RuntimeError, GetPackageMetadata, target_info, |
| 453 | source_info) |
| 454 | |
| 455 | def test_GetPackageMetadata_downgrade(self): |
| 456 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 457 | source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT) |
| 458 | self._test_GetPackageMetadata_swapBuildTimestamps( |
| 459 | target_info_dict, source_info_dict) |
| 460 | |
| 461 | target_info = BuildInfo(target_info_dict, None) |
| 462 | source_info = BuildInfo(source_info_dict, None) |
| 463 | common.OPTIONS.incremental_source = '' |
| 464 | common.OPTIONS.downgrade = True |
| 465 | common.OPTIONS.wipe_user_data = True |
| 466 | metadata = GetPackageMetadata(target_info, source_info) |
| 467 | self.assertDictEqual( |
| 468 | { |
| 469 | 'ota-downgrade' : 'yes', |
| 470 | 'ota-type' : 'BLOCK', |
| 471 | 'ota-wipe' : 'yes', |
| 472 | 'post-build' : 'build-fingerprint-target', |
| 473 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame^] | 474 | 'post-sdk-level' : '27', |
| 475 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 476 | 'pre-device' : 'product-device', |
| 477 | 'pre-build' : 'build-fingerprint-source', |
| 478 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 479 | }, |
| 480 | metadata) |
| 481 | |
| 482 | def test_GetPackageMetadata_overrideTimestamp(self): |
| 483 | target_info_dict = copy.deepcopy(self.TEST_TARGET_INFO_DICT) |
| 484 | source_info_dict = copy.deepcopy(self.TEST_SOURCE_INFO_DICT) |
| 485 | self._test_GetPackageMetadata_swapBuildTimestamps( |
| 486 | target_info_dict, source_info_dict) |
| 487 | |
| 488 | target_info = BuildInfo(target_info_dict, None) |
| 489 | source_info = BuildInfo(source_info_dict, None) |
| 490 | common.OPTIONS.incremental_source = '' |
| 491 | common.OPTIONS.timestamp = True |
| 492 | metadata = GetPackageMetadata(target_info, source_info) |
| 493 | self.assertDictEqual( |
| 494 | { |
| 495 | 'ota-type' : 'BLOCK', |
| 496 | 'post-build' : 'build-fingerprint-target', |
| 497 | 'post-build-incremental' : 'build-version-incremental-target', |
Tao Bao | 35dc255 | 2018-02-01 13:18:00 -0800 | [diff] [blame^] | 498 | 'post-sdk-level' : '27', |
| 499 | 'post-security-patch-level' : '2017-12-01', |
Tao Bao | df3a48b | 2018-01-10 16:30:43 -0800 | [diff] [blame] | 500 | 'post-timestamp' : '1500000001', |
| 501 | 'pre-device' : 'product-device', |
| 502 | 'pre-build' : 'build-fingerprint-source', |
| 503 | 'pre-build-incremental' : 'build-version-incremental-source', |
| 504 | }, |
| 505 | metadata) |
Tao Bao | fabe083 | 2018-01-17 15:52:28 -0800 | [diff] [blame] | 506 | |
| 507 | |
| 508 | class PayloadSignerTest(unittest.TestCase): |
| 509 | |
| 510 | SIGFILE = 'sigfile.bin' |
| 511 | SIGNED_SIGFILE = 'signed-sigfile.bin' |
| 512 | |
| 513 | def setUp(self): |
| 514 | self.testdata_dir = get_testdata_dir() |
| 515 | self.assertTrue(os.path.exists(self.testdata_dir)) |
| 516 | |
| 517 | common.OPTIONS.payload_signer = None |
| 518 | common.OPTIONS.payload_signer_args = [] |
| 519 | common.OPTIONS.package_key = os.path.join(self.testdata_dir, 'testkey') |
| 520 | common.OPTIONS.key_passwords = { |
| 521 | common.OPTIONS.package_key : None, |
| 522 | } |
| 523 | |
| 524 | def tearDown(self): |
| 525 | common.Cleanup() |
| 526 | |
| 527 | def _assertFilesEqual(self, file1, file2): |
| 528 | with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2: |
| 529 | self.assertEqual(fp1.read(), fp2.read()) |
| 530 | |
| 531 | def test_init(self): |
| 532 | payload_signer = PayloadSigner() |
| 533 | self.assertEqual('openssl', payload_signer.signer) |
| 534 | |
| 535 | def test_init_withPassword(self): |
| 536 | common.OPTIONS.package_key = os.path.join( |
| 537 | self.testdata_dir, 'testkey_with_passwd') |
| 538 | common.OPTIONS.key_passwords = { |
| 539 | common.OPTIONS.package_key : 'foo', |
| 540 | } |
| 541 | payload_signer = PayloadSigner() |
| 542 | self.assertEqual('openssl', payload_signer.signer) |
| 543 | |
| 544 | def test_init_withExternalSigner(self): |
| 545 | common.OPTIONS.payload_signer = 'abc' |
| 546 | common.OPTIONS.payload_signer_args = ['arg1', 'arg2'] |
| 547 | payload_signer = PayloadSigner() |
| 548 | self.assertEqual('abc', payload_signer.signer) |
| 549 | self.assertEqual(['arg1', 'arg2'], payload_signer.signer_args) |
| 550 | |
| 551 | def test_Sign(self): |
| 552 | payload_signer = PayloadSigner() |
| 553 | input_file = os.path.join(self.testdata_dir, self.SIGFILE) |
| 554 | signed_file = payload_signer.Sign(input_file) |
| 555 | |
| 556 | verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE) |
| 557 | self._assertFilesEqual(verify_file, signed_file) |
| 558 | |
| 559 | def test_Sign_withExternalSigner_openssl(self): |
| 560 | """Uses openssl as the external payload signer.""" |
| 561 | common.OPTIONS.payload_signer = 'openssl' |
| 562 | common.OPTIONS.payload_signer_args = [ |
| 563 | 'pkeyutl', '-sign', '-keyform', 'DER', '-inkey', |
| 564 | os.path.join(self.testdata_dir, 'testkey.pk8'), |
| 565 | '-pkeyopt', 'digest:sha256'] |
| 566 | payload_signer = PayloadSigner() |
| 567 | input_file = os.path.join(self.testdata_dir, self.SIGFILE) |
| 568 | signed_file = payload_signer.Sign(input_file) |
| 569 | |
| 570 | verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE) |
| 571 | self._assertFilesEqual(verify_file, signed_file) |
| 572 | |
| 573 | def test_Sign_withExternalSigner_script(self): |
| 574 | """Uses testdata/payload_signer.sh as the external payload signer.""" |
| 575 | common.OPTIONS.payload_signer = os.path.join( |
| 576 | self.testdata_dir, 'payload_signer.sh') |
| 577 | common.OPTIONS.payload_signer_args = [ |
| 578 | os.path.join(self.testdata_dir, 'testkey.pk8')] |
| 579 | payload_signer = PayloadSigner() |
| 580 | input_file = os.path.join(self.testdata_dir, self.SIGFILE) |
| 581 | signed_file = payload_signer.Sign(input_file) |
| 582 | |
| 583 | verify_file = os.path.join(self.testdata_dir, self.SIGNED_SIGFILE) |
| 584 | self._assertFilesEqual(verify_file, signed_file) |