summaryrefslogtreecommitdiff
path: root/scripts/manifest_check_test.py
blob: abe0d8b0e00dc9817b13faad6ccf147e8ce2ab90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
#
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Unit tests for manifest_fixer.py."""

import sys
import unittest
from xml.dom import minidom

import manifest_check

sys.dont_write_bytecode = True


def uses_library_xml(name, attr=''):
    return '<uses-library android:name="%s"%s />' % (name, attr)


def required_xml(value):
    return ' android:required="%s"' % ('true' if value else 'false')


def uses_library_apk(name, sfx=''):
    return "uses-library%s:'%s'" % (sfx, name)


def required_apk(value):
    return '' if value else '-not-required'


class EnforceUsesLibrariesTest(unittest.TestCase):
    """Unit tests for add_extract_native_libs function."""

    def run_test(self, xml, apk, uses_libraries=(), optional_uses_libraries=(),
                 missing_optional_uses_libraries=()):  #pylint: disable=dangerous-default-value
        doc = minidom.parseString(xml)
        try:
            relax = False
            manifest_check.enforce_uses_libraries(
                doc, uses_libraries, optional_uses_libraries, missing_optional_uses_libraries,
                relax, False, 'path/to/X/AndroidManifest.xml')
            manifest_check.enforce_uses_libraries(apk, uses_libraries,
                                                  optional_uses_libraries,
                                                  missing_optional_uses_libraries,
                                                  relax, True,
                                                  'path/to/X/X.apk')
            return True
        except manifest_check.ManifestMismatchError:
            return False

    xml_tmpl = (
        '<?xml version="1.0" encoding="utf-8"?>\n<manifest '
        'xmlns:android="http://schemas.android.com/apk/res/android">\n    '
        '<application>\n    %s\n    </application>\n</manifest>\n')

    apk_tmpl = (
        "package: name='com.google.android.something' versionCode='100'\n"
        "sdkVersion:'29'\n"
        "targetSdkVersion:'29'\n"
        "uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n"
        '%s\n'
        "densities: '160' '240' '320' '480' '640' '65534")

    def test_uses_library(self):
        xml = self.xml_tmpl % (uses_library_xml('foo'))
        apk = self.apk_tmpl % (uses_library_apk('foo'))
        matches = self.run_test(xml, apk, uses_libraries=['foo'])
        self.assertTrue(matches)

    def test_uses_library_required(self):
        xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(True)))
        apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(True)))
        matches = self.run_test(xml, apk, uses_libraries=['foo'])
        self.assertTrue(matches)

    def test_optional_uses_library(self):
        xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
        apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
        matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
        self.assertTrue(matches)

    def test_expected_uses_library(self):
        xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
        apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
        matches = self.run_test(xml, apk, uses_libraries=['foo'])
        self.assertFalse(matches)

    def test_expected_optional_uses_library(self):
        xml = self.xml_tmpl % (uses_library_xml('foo'))
        apk = self.apk_tmpl % (uses_library_apk('foo'))
        matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
        self.assertFalse(matches)

    def test_expected_missing_optional_uses_library(self):
        xml = self.xml_tmpl % (
            uses_library_xml('foo') + uses_library_xml('missing') + uses_library_xml('bar'))
        apk = self.apk_tmpl % (
            uses_library_apk('foo') + uses_library_apk('missing') + uses_library_apk('bar'))
        matches = self.run_test(xml, apk, optional_uses_libraries=['foo', 'bar'],
                                missing_optional_uses_libraries=['missing'])
        self.assertFalse(matches)

    def test_missing_uses_library(self):
        xml = self.xml_tmpl % ''
        apk = self.apk_tmpl % ''
        matches = self.run_test(xml, apk, uses_libraries=['foo'])
        self.assertFalse(matches)

    def test_missing_optional_uses_library(self):
        xml = self.xml_tmpl % ''
        apk = self.apk_tmpl % ''
        matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
        self.assertFalse(matches)

    def test_extra_uses_library(self):
        xml = self.xml_tmpl % (uses_library_xml('foo'))
        apk = self.apk_tmpl % (uses_library_xml('foo'))
        matches = self.run_test(xml, apk)
        self.assertFalse(matches)

    def test_extra_optional_uses_library(self):
        xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
        apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
        matches = self.run_test(xml, apk)
        self.assertFalse(matches)

    def test_multiple_uses_library(self):
        xml = self.xml_tmpl % ('\n'.join(
            [uses_library_xml('foo'),
             uses_library_xml('bar')]))
        apk = self.apk_tmpl % ('\n'.join(
            [uses_library_apk('foo'),
             uses_library_apk('bar')]))
        matches = self.run_test(xml, apk, uses_libraries=['foo', 'bar'])
        self.assertTrue(matches)

    def test_multiple_optional_uses_library(self):
        xml = self.xml_tmpl % ('\n'.join([
            uses_library_xml('foo', required_xml(False)),
            uses_library_xml('bar', required_xml(False))
        ]))
        apk = self.apk_tmpl % ('\n'.join([
            uses_library_apk('foo', required_apk(False)),
            uses_library_apk('bar', required_apk(False))
        ]))
        matches = self.run_test(
            xml, apk, optional_uses_libraries=['foo', 'bar'])
        self.assertTrue(matches)

    def test_order_uses_library(self):
        xml = self.xml_tmpl % ('\n'.join(
            [uses_library_xml('foo'),
             uses_library_xml('bar')]))
        apk = self.apk_tmpl % ('\n'.join(
            [uses_library_apk('foo'),
             uses_library_apk('bar')]))
        matches = self.run_test(xml, apk, uses_libraries=['bar', 'foo'])
        self.assertFalse(matches)

    def test_order_optional_uses_library(self):
        xml = self.xml_tmpl % ('\n'.join([
            uses_library_xml('foo', required_xml(False)),
            uses_library_xml('bar', required_xml(False))
        ]))
        apk = self.apk_tmpl % ('\n'.join([
            uses_library_apk('foo', required_apk(False)),
            uses_library_apk('bar', required_apk(False))
        ]))
        matches = self.run_test(
            xml, apk, optional_uses_libraries=['bar', 'foo'])
        self.assertFalse(matches)

    def test_duplicate_uses_library(self):
        xml = self.xml_tmpl % ('\n'.join(
            [uses_library_xml('foo'),
             uses_library_xml('foo')]))
        apk = self.apk_tmpl % ('\n'.join(
            [uses_library_apk('foo'),
             uses_library_apk('foo')]))
        matches = self.run_test(xml, apk, uses_libraries=['foo'])
        self.assertTrue(matches)

    def test_duplicate_optional_uses_library(self):
        xml = self.xml_tmpl % ('\n'.join([
            uses_library_xml('foo', required_xml(False)),
            uses_library_xml('foo', required_xml(False))
        ]))
        apk = self.apk_tmpl % ('\n'.join([
            uses_library_apk('foo', required_apk(False)),
            uses_library_apk('foo', required_apk(False))
        ]))
        matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
        self.assertTrue(matches)

    def test_mixed(self):
        xml = self.xml_tmpl % ('\n'.join([
            uses_library_xml('foo'),
            uses_library_xml('bar', required_xml(False))
        ]))
        apk = self.apk_tmpl % ('\n'.join([
            uses_library_apk('foo'),
            uses_library_apk('bar', required_apk(False))
        ]))
        matches = self.run_test(
            xml, apk, uses_libraries=['foo'], optional_uses_libraries=['bar'])
        self.assertTrue(matches)

    def test_mixed_with_namespace(self):
        xml = self.xml_tmpl % ('\n'.join([
            uses_library_xml('foo'),
            uses_library_xml('bar', required_xml(False))
        ]))
        apk = self.apk_tmpl % ('\n'.join([
            uses_library_apk('foo'),
            uses_library_apk('bar', required_apk(False))
        ]))
        matches = self.run_test(
            xml,
            apk,
            uses_libraries=['//x/y/z:foo'],
            optional_uses_libraries=['//x/y/z:bar'])
        self.assertTrue(matches)

    def test_multiple_applications(self):
        xml = """<?xml version="1.0" encoding="utf-8"?>
            <manifest xmlns:android="http://schemas.android.com/apk/res/android">
                <application android:featureFlag="foo">
                    <uses-library android:name="foo" />
                    <uses-library android:name="bar" android:required="false" />
                </application>
                <application android:featureFlag="!foo">
                    <uses-library android:name="foo" />
                    <uses-library android:name="qux" android:required="false" />
                </application>
            </manifest>
        """
        apk = self.apk_tmpl % ('\n'.join([
            uses_library_apk('foo'),
            uses_library_apk('bar', required_apk(False)),
            uses_library_apk('foo'),
            uses_library_apk('qux', required_apk(False))
        ]))
        matches = self.run_test(
            xml,
            apk,
            uses_libraries=['//x/y/z:foo'],
            optional_uses_libraries=['//x/y/z:bar', '//x/y/z:qux'])
        self.assertTrue(matches)


class ExtractTargetSdkVersionTest(unittest.TestCase):

    def run_test(self, xml, apk, version):
        doc = minidom.parseString(xml)
        v = manifest_check.extract_target_sdk_version(doc, is_apk=False)
        self.assertEqual(v, version)
        v = manifest_check.extract_target_sdk_version(apk, is_apk=True)
        self.assertEqual(v, version)

    xml_tmpl = (
        '<?xml version="1.0" encoding="utf-8"?>\n<manifest '
        'xmlns:android="http://schemas.android.com/apk/res/android">\n    '
        '<uses-sdk android:minSdkVersion="28" android:targetSdkVersion="%s" '
        '/>\n</manifest>\n')

    apk_tmpl = (
        "package: name='com.google.android.something' versionCode='100'\n"
        "sdkVersion:'28'\n"
        "targetSdkVersion:'%s'\n"
        "uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n")

    def test_target_sdk_version_28(self):
        xml = self.xml_tmpl % '28'
        apk = self.apk_tmpl % '28'
        self.run_test(xml, apk, '28')

    def test_target_sdk_version_29(self):
        xml = self.xml_tmpl % '29'
        apk = self.apk_tmpl % '29'
        self.run_test(xml, apk, '29')


if __name__ == '__main__':
    unittest.main(verbosity=2)