Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 1 | #!/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 | |
| 19 | import sys |
| 20 | import unittest |
| 21 | from xml.dom import minidom |
| 22 | |
| 23 | import manifest_check |
| 24 | |
| 25 | sys.dont_write_bytecode = True |
| 26 | |
| 27 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 28 | def uses_library_xml(name, attr=''): |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 29 | return '<uses-library android:name="%s"%s />' % (name, attr) |
| 30 | |
| 31 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 32 | def required_xml(value): |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 33 | return ' android:required="%s"' % ('true' if value else 'false') |
| 34 | |
| 35 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 36 | def uses_library_apk(name, sfx=''): |
| 37 | return "uses-library%s:'%s'" % (sfx, name) |
| 38 | |
| 39 | |
| 40 | def required_apk(value): |
| 41 | return '' if value else '-not-required' |
| 42 | |
| 43 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 44 | class EnforceUsesLibrariesTest(unittest.TestCase): |
| 45 | """Unit tests for add_extract_native_libs function.""" |
| 46 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 47 | def run_test(self, xml, apk, uses_libraries=[], optional_uses_libraries=[]): |
| 48 | doc = minidom.parseString(xml) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 49 | try: |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 50 | relax = False |
| 51 | manifest_check.enforce_uses_libraries(doc, uses_libraries, |
Ulya Trafimovich | bb7513d | 2021-03-30 17:15:16 +0100 | [diff] [blame] | 52 | optional_uses_libraries, relax, False, 'path/to/X/AndroidManifest.xml') |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 53 | manifest_check.enforce_uses_libraries(apk, uses_libraries, |
Ulya Trafimovich | bb7513d | 2021-03-30 17:15:16 +0100 | [diff] [blame] | 54 | optional_uses_libraries, relax, True, 'path/to/X/X.apk') |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 55 | return True |
| 56 | except manifest_check.ManifestMismatchError: |
| 57 | return False |
| 58 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 59 | xml_tmpl = ( |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 60 | '<?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 Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 67 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 75 | def test_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 76 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 79 | self.assertTrue(matches) |
| 80 | |
| 81 | def test_uses_library_required(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 82 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 85 | self.assertTrue(matches) |
| 86 | |
| 87 | def test_optional_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 88 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 91 | self.assertTrue(matches) |
| 92 | |
| 93 | def test_expected_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 94 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 97 | self.assertFalse(matches) |
| 98 | |
| 99 | def test_expected_optional_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 100 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 103 | self.assertFalse(matches) |
| 104 | |
| 105 | def test_missing_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 106 | xml = self.xml_tmpl % ('') |
| 107 | apk = self.apk_tmpl % ('') |
| 108 | matches = self.run_test(xml, apk, uses_libraries=['foo']) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 109 | self.assertFalse(matches) |
| 110 | |
| 111 | def test_missing_optional_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 112 | xml = self.xml_tmpl % ('') |
| 113 | apk = self.apk_tmpl % ('') |
| 114 | matches = self.run_test(xml, apk, optional_uses_libraries=['foo']) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 115 | self.assertFalse(matches) |
| 116 | |
| 117 | def test_extra_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 118 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 121 | self.assertFalse(matches) |
| 122 | |
| 123 | def test_extra_optional_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 124 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 127 | self.assertFalse(matches) |
| 128 | |
| 129 | def test_multiple_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 130 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 135 | self.assertTrue(matches) |
| 136 | |
| 137 | def test_multiple_optional_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 138 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 143 | self.assertTrue(matches) |
| 144 | |
| 145 | def test_order_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 146 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 151 | self.assertFalse(matches) |
| 152 | |
| 153 | def test_order_optional_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 154 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 159 | self.assertFalse(matches) |
| 160 | |
| 161 | def test_duplicate_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 162 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 167 | self.assertTrue(matches) |
| 168 | |
| 169 | def test_duplicate_optional_uses_library(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 170 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 175 | self.assertTrue(matches) |
| 176 | |
| 177 | def test_mixed(self): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 178 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 183 | optional_uses_libraries=['bar']) |
| 184 | self.assertTrue(matches) |
| 185 | |
| 186 | |
| 187 | class ExtractTargetSdkVersionTest(unittest.TestCase): |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 188 | 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 194 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 195 | xml_tmpl = ( |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 196 | '<?xml version="1.0" encoding="utf-8"?>\n' |
| 197 | '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 198 | ' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="%s" />\n' |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 199 | '</manifest>\n') |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 200 | |
| 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 Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 216 | |
| 217 | if __name__ == '__main__': |
| 218 | unittest.main(verbosity=2) |