Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2017 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 | from __future__ import print_function |
| 18 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 19 | import os.path |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 20 | import unittest |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 21 | import zipfile |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 22 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 23 | import common |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 24 | import test_utils |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 25 | from sign_target_files_apks import EditTags, ReplaceVerityKeyId, RewriteProps |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class SignTargetFilesApksTest(unittest.TestCase): |
| 29 | |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 30 | def setUp(self): |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 31 | self.testdata_dir = test_utils.get_testdata_dir() |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 32 | |
| 33 | def tearDown(self): |
| 34 | common.Cleanup() |
| 35 | |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 36 | def test_EditTags(self): |
| 37 | self.assertEqual(EditTags('dev-keys'), ('release-keys')) |
| 38 | self.assertEqual(EditTags('test-keys'), ('release-keys')) |
| 39 | |
| 40 | # Multiple tags. |
| 41 | self.assertEqual(EditTags('abc,dev-keys,xyz'), ('abc,release-keys,xyz')) |
| 42 | |
| 43 | # Tags are sorted. |
| 44 | self.assertEqual(EditTags('xyz,abc,dev-keys,xyz'), ('abc,release-keys,xyz')) |
| 45 | |
| 46 | def test_RewriteProps(self): |
| 47 | props = ( |
| 48 | ('', '\n'), |
| 49 | ('ro.build.fingerprint=foo/bar/dev-keys', |
| 50 | 'ro.build.fingerprint=foo/bar/release-keys\n'), |
| 51 | ('ro.build.thumbprint=foo/bar/dev-keys', |
| 52 | 'ro.build.thumbprint=foo/bar/release-keys\n'), |
| 53 | ('ro.vendor.build.fingerprint=foo/bar/dev-keys', |
| 54 | 'ro.vendor.build.fingerprint=foo/bar/release-keys\n'), |
| 55 | ('ro.vendor.build.thumbprint=foo/bar/dev-keys', |
| 56 | 'ro.vendor.build.thumbprint=foo/bar/release-keys\n'), |
| 57 | ('# comment line 1', '# comment line 1\n'), |
| 58 | ('ro.bootimage.build.fingerprint=foo/bar/dev-keys', |
| 59 | 'ro.bootimage.build.fingerprint=foo/bar/release-keys\n'), |
| 60 | ('ro.build.description=' |
| 61 | 'sailfish-user 8.0.0 OPR6.170623.012 4283428 dev-keys', |
| 62 | 'ro.build.description=' |
| 63 | 'sailfish-user 8.0.0 OPR6.170623.012 4283428 release-keys\n'), |
| 64 | ('ro.build.tags=dev-keys', 'ro.build.tags=release-keys\n'), |
| 65 | ('# comment line 2', '# comment line 2\n'), |
| 66 | ('ro.build.display.id=OPR6.170623.012 dev-keys', |
| 67 | 'ro.build.display.id=OPR6.170623.012\n'), |
| 68 | ('# comment line 3', '# comment line 3\n'), |
| 69 | ) |
| 70 | |
| 71 | # Assert the case for each individual line. |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 72 | for prop, output in props: |
| 73 | self.assertEqual(RewriteProps(prop), output) |
Tao Bao | a7054ee | 2017-12-08 14:42:16 -0800 | [diff] [blame] | 74 | |
| 75 | # Concatenate all the input lines. |
| 76 | self.assertEqual(RewriteProps('\n'.join([prop[0] for prop in props])), |
| 77 | ''.join([prop[1] for prop in props])) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 78 | |
| 79 | def test_ReplaceVerityKeyId(self): |
| 80 | BOOT_CMDLINE1 = ( |
| 81 | "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 " |
| 82 | "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 " |
| 83 | "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 " |
| 84 | "buildvariant=userdebug " |
| 85 | "veritykeyid=id:7e4333f9bba00adfe0ede979e28ed1920492b40f\n") |
| 86 | |
| 87 | BOOT_CMDLINE2 = ( |
| 88 | "console=ttyHSL0,115200,n8 androidboot.console=ttyHSL0 " |
| 89 | "androidboot.hardware=marlin user_debug=31 ehci-hcd.park=3 " |
| 90 | "lpm_levels.sleep_disabled=1 cma=32M@0-0xffffffff loop.max_part=7 " |
| 91 | "buildvariant=userdebug " |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 92 | "veritykeyid=id:d24f2590e9abab5cff5f59da4c4f0366e3f43e94\n") |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 93 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 94 | input_file = common.MakeTempFile(suffix='.zip') |
| 95 | with zipfile.ZipFile(input_file, 'w') as input_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 96 | input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE1) |
| 97 | |
| 98 | # Test with the first certificate. |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 99 | cert_file = os.path.join(self.testdata_dir, 'verity.x509.pem') |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 100 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 101 | output_file = common.MakeTempFile(suffix='.zip') |
| 102 | with zipfile.ZipFile(input_file, 'r') as input_zip, \ |
| 103 | zipfile.ZipFile(output_file, 'w') as output_zip: |
| 104 | ReplaceVerityKeyId(input_zip, output_zip, cert_file) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 105 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 106 | with zipfile.ZipFile(output_file) as output_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 107 | self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline')) |
| 108 | |
| 109 | # Test with the second certificate. |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 110 | cert_file = os.path.join(self.testdata_dir, 'testkey.x509.pem') |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 111 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 112 | with zipfile.ZipFile(input_file, 'r') as input_zip, \ |
| 113 | zipfile.ZipFile(output_file, 'w') as output_zip: |
| 114 | ReplaceVerityKeyId(input_zip, output_zip, cert_file) |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 115 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 116 | with zipfile.ZipFile(output_file) as output_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 117 | self.assertEqual(BOOT_CMDLINE2, output_zip.read('BOOT/cmdline')) |
| 118 | |
| 119 | def test_ReplaceVerityKeyId_no_veritykeyid(self): |
| 120 | BOOT_CMDLINE = ( |
| 121 | "console=ttyHSL0,115200,n8 androidboot.hardware=bullhead boot_cpus=0-5 " |
| 122 | "lpm_levels.sleep_disabled=1 msm_poweroff.download_mode=0 " |
| 123 | "loop.max_part=7\n") |
| 124 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 125 | input_file = common.MakeTempFile(suffix='.zip') |
| 126 | with zipfile.ZipFile(input_file, 'w') as input_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 127 | input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE) |
| 128 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 129 | output_file = common.MakeTempFile(suffix='.zip') |
| 130 | with zipfile.ZipFile(input_file, 'r') as input_zip, \ |
| 131 | zipfile.ZipFile(output_file, 'w') as output_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 132 | ReplaceVerityKeyId(input_zip, output_zip, None) |
| 133 | |
Tao Bao | de1d479 | 2018-02-20 10:05:46 -0800 | [diff] [blame^] | 134 | with zipfile.ZipFile(output_file) as output_zip: |
Tao Bao | e838d14 | 2017-12-23 23:44:48 -0800 | [diff] [blame] | 135 | self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline')) |