blob: 0cab801953598a005636b5c7f6d8e7420e6e9fd0 [file] [log] [blame]
Tao Baoa7054ee2017-12-08 14:42:16 -08001#
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
17from __future__ import print_function
18
Tao Baode1d4792018-02-20 10:05:46 -080019import os.path
Tao Baoa7054ee2017-12-08 14:42:16 -080020import unittest
Tao Baoe838d142017-12-23 23:44:48 -080021import zipfile
Tao Baoa7054ee2017-12-08 14:42:16 -080022
Tao Baoe838d142017-12-23 23:44:48 -080023import common
Tao Baode1d4792018-02-20 10:05:46 -080024import test_utils
Tao Baoe838d142017-12-23 23:44:48 -080025from sign_target_files_apks import EditTags, ReplaceVerityKeyId, RewriteProps
Tao Baoa7054ee2017-12-08 14:42:16 -080026
27
28class SignTargetFilesApksTest(unittest.TestCase):
29
Tao Baoe838d142017-12-23 23:44:48 -080030 def setUp(self):
Tao Baode1d4792018-02-20 10:05:46 -080031 self.testdata_dir = test_utils.get_testdata_dir()
Tao Baoe838d142017-12-23 23:44:48 -080032
33 def tearDown(self):
34 common.Cleanup()
35
Tao Baoa7054ee2017-12-08 14:42:16 -080036 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 Baoe838d142017-12-23 23:44:48 -080072 for prop, output in props:
73 self.assertEqual(RewriteProps(prop), output)
Tao Baoa7054ee2017-12-08 14:42:16 -080074
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 Baoe838d142017-12-23 23:44:48 -080078
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 Baode1d4792018-02-20 10:05:46 -080092 "veritykeyid=id:d24f2590e9abab5cff5f59da4c4f0366e3f43e94\n")
Tao Baoe838d142017-12-23 23:44:48 -080093
Tao Baode1d4792018-02-20 10:05:46 -080094 input_file = common.MakeTempFile(suffix='.zip')
95 with zipfile.ZipFile(input_file, 'w') as input_zip:
Tao Baoe838d142017-12-23 23:44:48 -080096 input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE1)
97
98 # Test with the first certificate.
Tao Baode1d4792018-02-20 10:05:46 -080099 cert_file = os.path.join(self.testdata_dir, 'verity.x509.pem')
Tao Baoe838d142017-12-23 23:44:48 -0800100
Tao Baode1d4792018-02-20 10:05:46 -0800101 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 Baoe838d142017-12-23 23:44:48 -0800105
Tao Baode1d4792018-02-20 10:05:46 -0800106 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800107 self.assertEqual(BOOT_CMDLINE1, output_zip.read('BOOT/cmdline'))
108
109 # Test with the second certificate.
Tao Baode1d4792018-02-20 10:05:46 -0800110 cert_file = os.path.join(self.testdata_dir, 'testkey.x509.pem')
Tao Baoe838d142017-12-23 23:44:48 -0800111
Tao Baode1d4792018-02-20 10:05:46 -0800112 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 Baoe838d142017-12-23 23:44:48 -0800115
Tao Baode1d4792018-02-20 10:05:46 -0800116 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800117 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 Baode1d4792018-02-20 10:05:46 -0800125 input_file = common.MakeTempFile(suffix='.zip')
126 with zipfile.ZipFile(input_file, 'w') as input_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800127 input_zip.writestr('BOOT/cmdline', BOOT_CMDLINE)
128
Tao Baode1d4792018-02-20 10:05:46 -0800129 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 Baoe838d142017-12-23 23:44:48 -0800132 ReplaceVerityKeyId(input_zip, output_zip, None)
133
Tao Baode1d4792018-02-20 10:05:46 -0800134 with zipfile.ZipFile(output_file) as output_zip:
Tao Baoe838d142017-12-23 23:44:48 -0800135 self.assertEqual(BOOT_CMDLINE, output_zip.read('BOOT/cmdline'))