blob: 7159bdd7462e1bb18954a914da23ce7ae9a3d639 [file] [log] [blame]
Colin Cross72119102019-05-20 13:14:18 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""Unit tests for manifest_fixer.py."""
18
19import sys
20import unittest
21from xml.dom import minidom
22
23import manifest_check
24
25sys.dont_write_bytecode = True
26
27
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000028def uses_library_xml(name, attr=''):
Colin Cross72119102019-05-20 13:14:18 -070029 return '<uses-library android:name="%s"%s />' % (name, attr)
30
31
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000032def required_xml(value):
Colin Cross72119102019-05-20 13:14:18 -070033 return ' android:required="%s"' % ('true' if value else 'false')
34
35
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000036def uses_library_apk(name, sfx=''):
37 return "uses-library%s:'%s'" % (sfx, name)
38
39
40def required_apk(value):
41 return '' if value else '-not-required'
42
43
Colin Cross72119102019-05-20 13:14:18 -070044class EnforceUsesLibrariesTest(unittest.TestCase):
45 """Unit tests for add_extract_native_libs function."""
46
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000047 def run_test(self, xml, apk, uses_libraries=[], optional_uses_libraries=[]):
48 doc = minidom.parseString(xml)
Colin Cross72119102019-05-20 13:14:18 -070049 try:
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000050 relax = False
51 manifest_check.enforce_uses_libraries(doc, uses_libraries,
Ulya Trafimovichbb7513d2021-03-30 17:15:16 +010052 optional_uses_libraries, relax, False, 'path/to/X/AndroidManifest.xml')
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000053 manifest_check.enforce_uses_libraries(apk, uses_libraries,
Ulya Trafimovichbb7513d2021-03-30 17:15:16 +010054 optional_uses_libraries, relax, True, 'path/to/X/X.apk')
Colin Cross72119102019-05-20 13:14:18 -070055 return True
56 except manifest_check.ManifestMismatchError:
57 return False
58
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000059 xml_tmpl = (
Colin Cross72119102019-05-20 13:14:18 -070060 '<?xml version="1.0" encoding="utf-8"?>\n'
61 '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
62 ' <application>\n'
63 ' %s\n'
64 ' </application>\n'
65 '</manifest>\n')
66
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000067 apk_tmpl = (
68 "package: name='com.google.android.something' versionCode='100'\n"
69 "sdkVersion:'29'\n"
70 "targetSdkVersion:'29'\n"
71 "uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n"
72 "%s\n"
73 "densities: '160' '240' '320' '480' '640' '65534")
74
Colin Cross72119102019-05-20 13:14:18 -070075 def test_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000076 xml = self.xml_tmpl % (uses_library_xml('foo'))
77 apk = self.apk_tmpl % (uses_library_apk('foo'))
78 matches = self.run_test(xml, apk, uses_libraries=['foo'])
Colin Cross72119102019-05-20 13:14:18 -070079 self.assertTrue(matches)
80
81 def test_uses_library_required(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000082 xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(True)))
83 apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(True)))
84 matches = self.run_test(xml, apk, uses_libraries=['foo'])
Colin Cross72119102019-05-20 13:14:18 -070085 self.assertTrue(matches)
86
87 def test_optional_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000088 xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
89 apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
90 matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
Colin Cross72119102019-05-20 13:14:18 -070091 self.assertTrue(matches)
92
93 def test_expected_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +000094 xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
95 apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
96 matches = self.run_test(xml, apk, uses_libraries=['foo'])
Colin Cross72119102019-05-20 13:14:18 -070097 self.assertFalse(matches)
98
99 def test_expected_optional_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000100 xml = self.xml_tmpl % (uses_library_xml('foo'))
101 apk = self.apk_tmpl % (uses_library_apk('foo'))
102 matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
Colin Cross72119102019-05-20 13:14:18 -0700103 self.assertFalse(matches)
104
105 def test_missing_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000106 xml = self.xml_tmpl % ('')
107 apk = self.apk_tmpl % ('')
108 matches = self.run_test(xml, apk, uses_libraries=['foo'])
Colin Cross72119102019-05-20 13:14:18 -0700109 self.assertFalse(matches)
110
111 def test_missing_optional_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000112 xml = self.xml_tmpl % ('')
113 apk = self.apk_tmpl % ('')
114 matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
Colin Cross72119102019-05-20 13:14:18 -0700115 self.assertFalse(matches)
116
117 def test_extra_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000118 xml = self.xml_tmpl % (uses_library_xml('foo'))
119 apk = self.apk_tmpl % (uses_library_xml('foo'))
120 matches = self.run_test(xml, apk)
Colin Cross72119102019-05-20 13:14:18 -0700121 self.assertFalse(matches)
122
123 def test_extra_optional_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000124 xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
125 apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
126 matches = self.run_test(xml, apk)
Colin Cross72119102019-05-20 13:14:18 -0700127 self.assertFalse(matches)
128
129 def test_multiple_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000130 xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'),
131 uses_library_xml('bar')]))
132 apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'),
133 uses_library_apk('bar')]))
134 matches = self.run_test(xml, apk, uses_libraries=['foo', 'bar'])
Colin Cross72119102019-05-20 13:14:18 -0700135 self.assertTrue(matches)
136
137 def test_multiple_optional_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000138 xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo', required_xml(False)),
139 uses_library_xml('bar', required_xml(False))]))
140 apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo', required_apk(False)),
141 uses_library_apk('bar', required_apk(False))]))
142 matches = self.run_test(xml, apk, optional_uses_libraries=['foo', 'bar'])
Colin Cross72119102019-05-20 13:14:18 -0700143 self.assertTrue(matches)
144
145 def test_order_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000146 xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'),
147 uses_library_xml('bar')]))
148 apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'),
149 uses_library_apk('bar')]))
150 matches = self.run_test(xml, apk, uses_libraries=['bar', 'foo'])
Colin Cross72119102019-05-20 13:14:18 -0700151 self.assertFalse(matches)
152
153 def test_order_optional_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000154 xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo', required_xml(False)),
155 uses_library_xml('bar', required_xml(False))]))
156 apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo', required_apk(False)),
157 uses_library_apk('bar', required_apk(False))]))
158 matches = self.run_test(xml, apk, optional_uses_libraries=['bar', 'foo'])
Colin Cross72119102019-05-20 13:14:18 -0700159 self.assertFalse(matches)
160
161 def test_duplicate_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000162 xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'),
163 uses_library_xml('foo')]))
164 apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'),
165 uses_library_apk('foo')]))
166 matches = self.run_test(xml, apk, uses_libraries=['foo'])
Colin Cross72119102019-05-20 13:14:18 -0700167 self.assertTrue(matches)
168
169 def test_duplicate_optional_uses_library(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000170 xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo', required_xml(False)),
171 uses_library_xml('foo', required_xml(False))]))
172 apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo', required_apk(False)),
173 uses_library_apk('foo', required_apk(False))]))
174 matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
Colin Cross72119102019-05-20 13:14:18 -0700175 self.assertTrue(matches)
176
177 def test_mixed(self):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000178 xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'),
179 uses_library_xml('bar', required_xml(False))]))
180 apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'),
181 uses_library_apk('bar', required_apk(False))]))
182 matches = self.run_test(xml, apk, uses_libraries=['foo'],
Colin Cross72119102019-05-20 13:14:18 -0700183 optional_uses_libraries=['bar'])
184 self.assertTrue(matches)
185
186
187class ExtractTargetSdkVersionTest(unittest.TestCase):
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000188 def run_test(self, xml, apk, version):
189 doc = minidom.parseString(xml)
190 v = manifest_check.extract_target_sdk_version(doc, is_apk=False)
191 self.assertEqual(v, version)
192 v = manifest_check.extract_target_sdk_version(apk, is_apk=True)
193 self.assertEqual(v, version)
Colin Cross72119102019-05-20 13:14:18 -0700194
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000195 xml_tmpl = (
Colin Cross72119102019-05-20 13:14:18 -0700196 '<?xml version="1.0" encoding="utf-8"?>\n'
197 '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000198 ' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="%s" />\n'
Colin Cross72119102019-05-20 13:14:18 -0700199 '</manifest>\n')
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000200
201 apk_tmpl = (
202 "package: name='com.google.android.something' versionCode='100'\n"
203 "sdkVersion:'28'\n"
204 "targetSdkVersion:'%s'\n"
205 "uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n")
206
207 def test_targert_sdk_version_28(self):
208 xml = self.xml_tmpl % "28"
209 apk = self.apk_tmpl % "28"
210 self.run_test(xml, apk, "28")
211
212 def test_targert_sdk_version_29(self):
213 xml = self.xml_tmpl % "29"
214 apk = self.apk_tmpl % "29"
215 self.run_test(xml, apk, "29")
Colin Cross72119102019-05-20 13:14:18 -0700216
217if __name__ == '__main__':
218 unittest.main(verbosity=2)