blob: 56c2d9ed14ca3f29907a5b226a22a227da30b65d [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
28def uses_library(name, attr=''):
29 return '<uses-library android:name="%s"%s />' % (name, attr)
30
31
32def required(value):
33 return ' android:required="%s"' % ('true' if value else 'false')
34
35
36class EnforceUsesLibrariesTest(unittest.TestCase):
37 """Unit tests for add_extract_native_libs function."""
38
39 def run_test(self, input_manifest, uses_libraries=None, optional_uses_libraries=None):
40 doc = minidom.parseString(input_manifest)
41 try:
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000042 relax = False
43 manifest_check.enforce_uses_libraries(doc, uses_libraries,
44 optional_uses_libraries, relax)
Colin Cross72119102019-05-20 13:14:18 -070045 return True
46 except manifest_check.ManifestMismatchError:
47 return False
48
49 manifest_tmpl = (
50 '<?xml version="1.0" encoding="utf-8"?>\n'
51 '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
52 ' <application>\n'
53 ' %s\n'
54 ' </application>\n'
55 '</manifest>\n')
56
57 def test_uses_library(self):
58 manifest_input = self.manifest_tmpl % (uses_library('foo'))
59 matches = self.run_test(manifest_input, uses_libraries=['foo'])
60 self.assertTrue(matches)
61
62 def test_uses_library_required(self):
63 manifest_input = self.manifest_tmpl % (uses_library('foo', required(True)))
64 matches = self.run_test(manifest_input, uses_libraries=['foo'])
65 self.assertTrue(matches)
66
67 def test_optional_uses_library(self):
68 manifest_input = self.manifest_tmpl % (uses_library('foo', required(False)))
69 matches = self.run_test(manifest_input, optional_uses_libraries=['foo'])
70 self.assertTrue(matches)
71
72 def test_expected_uses_library(self):
73 manifest_input = self.manifest_tmpl % (uses_library('foo', required(False)))
74 matches = self.run_test(manifest_input, uses_libraries=['foo'])
75 self.assertFalse(matches)
76
77 def test_expected_optional_uses_library(self):
78 manifest_input = self.manifest_tmpl % (uses_library('foo'))
79 matches = self.run_test(manifest_input, optional_uses_libraries=['foo'])
80 self.assertFalse(matches)
81
82 def test_missing_uses_library(self):
83 manifest_input = self.manifest_tmpl % ('')
84 matches = self.run_test(manifest_input, uses_libraries=['foo'])
85 self.assertFalse(matches)
86
87 def test_missing_optional_uses_library(self):
88 manifest_input = self.manifest_tmpl % ('')
89 matches = self.run_test(manifest_input, optional_uses_libraries=['foo'])
90 self.assertFalse(matches)
91
92 def test_extra_uses_library(self):
93 manifest_input = self.manifest_tmpl % (uses_library('foo'))
94 matches = self.run_test(manifest_input)
95 self.assertFalse(matches)
96
97 def test_extra_optional_uses_library(self):
98 manifest_input = self.manifest_tmpl % (uses_library('foo', required(False)))
99 matches = self.run_test(manifest_input)
100 self.assertFalse(matches)
101
102 def test_multiple_uses_library(self):
103 manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo'),
104 uses_library('bar')]))
105 matches = self.run_test(manifest_input, uses_libraries=['foo', 'bar'])
106 self.assertTrue(matches)
107
108 def test_multiple_optional_uses_library(self):
109 manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo', required(False)),
110 uses_library('bar', required(False))]))
111 matches = self.run_test(manifest_input, optional_uses_libraries=['foo', 'bar'])
112 self.assertTrue(matches)
113
114 def test_order_uses_library(self):
115 manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo'),
116 uses_library('bar')]))
117 matches = self.run_test(manifest_input, uses_libraries=['bar', 'foo'])
118 self.assertFalse(matches)
119
120 def test_order_optional_uses_library(self):
121 manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo', required(False)),
122 uses_library('bar', required(False))]))
123 matches = self.run_test(manifest_input, optional_uses_libraries=['bar', 'foo'])
124 self.assertFalse(matches)
125
126 def test_duplicate_uses_library(self):
127 manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo'),
128 uses_library('foo')]))
129 matches = self.run_test(manifest_input, uses_libraries=['foo'])
130 self.assertTrue(matches)
131
132 def test_duplicate_optional_uses_library(self):
133 manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo', required(False)),
134 uses_library('foo', required(False))]))
135 matches = self.run_test(manifest_input, optional_uses_libraries=['foo'])
136 self.assertTrue(matches)
137
138 def test_mixed(self):
139 manifest_input = self.manifest_tmpl % ('\n'.join([uses_library('foo'),
140 uses_library('bar', required(False))]))
141 matches = self.run_test(manifest_input, uses_libraries=['foo'],
142 optional_uses_libraries=['bar'])
143 self.assertTrue(matches)
144
145
146class ExtractTargetSdkVersionTest(unittest.TestCase):
147 def test_target_sdk_version(self):
148 manifest = (
149 '<?xml version="1.0" encoding="utf-8"?>\n'
150 '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
151 ' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="29" />\n'
152 '</manifest>\n')
153 doc = minidom.parseString(manifest)
154 target_sdk_version = manifest_check.extract_target_sdk_version(doc)
155 self.assertEqual(target_sdk_version, '29')
156
157 def test_min_sdk_version(self):
158 manifest = (
159 '<?xml version="1.0" encoding="utf-8"?>\n'
160 '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
161 ' <uses-sdk android:minSdkVersion="28" />\n'
162 '</manifest>\n')
163 doc = minidom.parseString(manifest)
164 target_sdk_version = manifest_check.extract_target_sdk_version(doc)
165 self.assertEqual(target_sdk_version, '28')
166
167if __name__ == '__main__':
168 unittest.main(verbosity=2)