Apply pylint to scripts/manifest_check*.py

1. Run pyformat scripts/<script>.py -s 4 --force_quote_type
none -i to fix formatting
2. Annotate #pylint: disable, where straightforward fix is not available

Test: m manifest_check_test
Test: pylint --rcfile tools/repohooks/tools/pylintrc
build/soong/scripts/manifest_check.py
build/soong/scripts/manifest_check_test.py
Bug: 195738175

Change-Id: I9af498c4abd6ac9f8b9df4f93cbdd4424eacff8e
This commit is contained in:
Spandan Das 2021-08-25 20:01:17 +00:00
parent 7c16dabfa5
commit f880742582
2 changed files with 447 additions and 383 deletions

View file

@ -25,7 +25,6 @@ import subprocess
import sys import sys
from xml.dom import minidom from xml.dom import minidom
from manifest import android_ns from manifest import android_ns
from manifest import get_children_with_tag from manifest import get_children_with_tag
from manifest import parse_manifest from manifest import parse_manifest
@ -33,49 +32,61 @@ from manifest import write_xml
class ManifestMismatchError(Exception): class ManifestMismatchError(Exception):
pass pass
def parse_args(): def parse_args():
"""Parse commandline arguments.""" """Parse commandline arguments."""
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--uses-library', dest='uses_libraries', parser.add_argument(
action='append', '--uses-library',
help='specify uses-library entries known to the build system') dest='uses_libraries',
parser.add_argument('--optional-uses-library', action='append',
dest='optional_uses_libraries', help='specify uses-library entries known to the build system')
action='append', parser.add_argument(
help='specify uses-library entries known to the build system with required:false') '--optional-uses-library',
parser.add_argument('--enforce-uses-libraries', dest='optional_uses_libraries',
dest='enforce_uses_libraries', action='append',
action='store_true', help='specify uses-library entries known to the build system with '
help='check the uses-library entries known to the build system against the manifest') 'required:false'
parser.add_argument('--enforce-uses-libraries-relax', )
dest='enforce_uses_libraries_relax', parser.add_argument(
action='store_true', '--enforce-uses-libraries',
help='do not fail immediately, just save the error message to file') dest='enforce_uses_libraries',
parser.add_argument('--enforce-uses-libraries-status', action='store_true',
dest='enforce_uses_libraries_status', help='check the uses-library entries known to the build system against '
help='output file to store check status (error message)') 'the manifest'
parser.add_argument('--extract-target-sdk-version', )
dest='extract_target_sdk_version', parser.add_argument(
action='store_true', '--enforce-uses-libraries-relax',
help='print the targetSdkVersion from the manifest') dest='enforce_uses_libraries_relax',
parser.add_argument('--dexpreopt-config', action='store_true',
dest='dexpreopt_configs', help='do not fail immediately, just save the error message to file')
action='append', parser.add_argument(
help='a paths to a dexpreopt.config of some library') '--enforce-uses-libraries-status',
parser.add_argument('--aapt', dest='enforce_uses_libraries_status',
dest='aapt', help='output file to store check status (error message)')
help='path to aapt executable') parser.add_argument(
parser.add_argument('--output', '-o', dest='output', help='output AndroidManifest.xml file') '--extract-target-sdk-version',
parser.add_argument('input', help='input AndroidManifest.xml file') dest='extract_target_sdk_version',
return parser.parse_args() action='store_true',
help='print the targetSdkVersion from the manifest')
parser.add_argument(
'--dexpreopt-config',
dest='dexpreopt_configs',
action='append',
help='a paths to a dexpreopt.config of some library')
parser.add_argument('--aapt', dest='aapt', help='path to aapt executable')
parser.add_argument(
'--output', '-o', dest='output', help='output AndroidManifest.xml file')
parser.add_argument('input', help='input AndroidManifest.xml file')
return parser.parse_args()
def enforce_uses_libraries(manifest, required, optional, relax, is_apk, path): def enforce_uses_libraries(manifest, required, optional, relax, is_apk, path):
"""Verify that the <uses-library> tags in the manifest match those provided """Verify that the <uses-library> tags in the manifest match those provided
by the build system. by the build system.
Args: Args:
@ -84,274 +95,294 @@ def enforce_uses_libraries(manifest, required, optional, relax, is_apk, path):
optional: optional libs known to the build system optional: optional libs known to the build system
relax: if true, suppress error on mismatch and just write it to file relax: if true, suppress error on mismatch and just write it to file
is_apk: if the manifest comes from an APK or an XML file is_apk: if the manifest comes from an APK or an XML file
""" """
if is_apk: if is_apk:
manifest_required, manifest_optional, tags = extract_uses_libs_apk(manifest) manifest_required, manifest_optional, tags = extract_uses_libs_apk(
else: manifest)
manifest_required, manifest_optional, tags = extract_uses_libs_xml(manifest) else:
manifest_required, manifest_optional, tags = extract_uses_libs_xml(
manifest)
# Trim namespace component. Normally Soong does that automatically when it # Trim namespace component. Normally Soong does that automatically when it
# handles module names specified in Android.bp properties. However not all # handles module names specified in Android.bp properties. However not all
# <uses-library> entries in the manifest correspond to real modules: some of # <uses-library> entries in the manifest correspond to real modules: some of
# the optional libraries may be missing at build time. Therefor this script # the optional libraries may be missing at build time. Therefor this script
# accepts raw module names as spelled in Android.bp/Amdroid.mk and trims the # accepts raw module names as spelled in Android.bp/Amdroid.mk and trims the
# optional namespace part manually. # optional namespace part manually.
required = trim_namespace_parts(required) required = trim_namespace_parts(required)
optional = trim_namespace_parts(optional) optional = trim_namespace_parts(optional)
if manifest_required == required and manifest_optional == optional: if manifest_required == required and manifest_optional == optional:
return None return None
errmsg = ''.join([ #pylint: disable=line-too-long
'mismatch in the <uses-library> tags between the build system and the ' errmsg = ''.join([
'manifest:\n', 'mismatch in the <uses-library> tags between the build system and the '
'\t- required libraries in build system: [%s]\n' % ', '.join(required), 'manifest:\n',
'\t vs. in the manifest: [%s]\n' % ', '.join(manifest_required), '\t- required libraries in build system: [%s]\n' % ', '.join(required),
'\t- optional libraries in build system: [%s]\n' % ', '.join(optional), '\t vs. in the manifest: [%s]\n' %
'\t vs. in the manifest: [%s]\n' % ', '.join(manifest_optional), ', '.join(manifest_required),
'\t- tags in the manifest (%s):\n' % path, '\t- optional libraries in build system: [%s]\n' % ', '.join(optional),
'\t\t%s\n' % '\t\t'.join(tags), '\t vs. in the manifest: [%s]\n' %
'note: the following options are available:\n', ', '.join(manifest_optional),
'\t- to temporarily disable the check on command line, rebuild with ', '\t- tags in the manifest (%s):\n' % path,
'RELAX_USES_LIBRARY_CHECK=true (this will set compiler filter "verify" ', '\t\t%s\n' % '\t\t'.join(tags),
'and disable AOT-compilation in dexpreopt)\n', 'note: the following options are available:\n',
'\t- to temporarily disable the check for the whole product, set ', '\t- to temporarily disable the check on command line, rebuild with ',
'PRODUCT_BROKEN_VERIFY_USES_LIBRARIES := true in the product makefiles\n', 'RELAX_USES_LIBRARY_CHECK=true (this will set compiler filter "verify" ',
'\t- to fix the check, make build system properties coherent with the ' 'and disable AOT-compilation in dexpreopt)\n',
'manifest\n', '\t- to temporarily disable the check for the whole product, set ',
'\t- see build/make/Changes.md for details\n']) 'PRODUCT_BROKEN_VERIFY_USES_LIBRARIES := true in the product makefiles\n',
'\t- to fix the check, make build system properties coherent with the '
'manifest\n', '\t- see build/make/Changes.md for details\n'
])
#pylint: enable=line-too-long
if not relax: if not relax:
raise ManifestMismatchError(errmsg) raise ManifestMismatchError(errmsg)
return errmsg return errmsg
MODULE_NAMESPACE = re.compile("^//[^:]+:") MODULE_NAMESPACE = re.compile('^//[^:]+:')
def trim_namespace_parts(modules): def trim_namespace_parts(modules):
"""Trim the namespace part of each module, if present. Leave only the name.""" """Trim the namespace part of each module, if present.
trimmed = [] Leave only the name.
for module in modules: """
trimmed.append(MODULE_NAMESPACE.sub('', module))
return trimmed trimmed = []
for module in modules:
trimmed.append(MODULE_NAMESPACE.sub('', module))
return trimmed
def extract_uses_libs_apk(badging): def extract_uses_libs_apk(badging):
"""Extract <uses-library> tags from the manifest of an APK.""" """Extract <uses-library> tags from the manifest of an APK."""
pattern = re.compile("^uses-library(-not-required)?:'(.*)'$", re.MULTILINE) pattern = re.compile("^uses-library(-not-required)?:'(.*)'$", re.MULTILINE)
required = [] required = []
optional = [] optional = []
lines = [] lines = []
for match in re.finditer(pattern, badging): for match in re.finditer(pattern, badging):
lines.append(match.group(0)) lines.append(match.group(0))
libname = match.group(2) libname = match.group(2)
if match.group(1) == None: if match.group(1) is None:
required.append(libname) required.append(libname)
else: else:
optional.append(libname) optional.append(libname)
required = first_unique_elements(required) required = first_unique_elements(required)
optional = first_unique_elements(optional) optional = first_unique_elements(optional)
tags = first_unique_elements(lines) tags = first_unique_elements(lines)
return required, optional, tags return required, optional, tags
def extract_uses_libs_xml(xml): def extract_uses_libs_xml(xml): #pylint: disable=inconsistent-return-statements
"""Extract <uses-library> tags from the manifest.""" """Extract <uses-library> tags from the manifest."""
manifest = parse_manifest(xml) manifest = parse_manifest(xml)
elems = get_children_with_tag(manifest, 'application') elems = get_children_with_tag(manifest, 'application')
application = elems[0] if len(elems) == 1 else None application = elems[0] if len(elems) == 1 else None
if len(elems) > 1: if len(elems) > 1: #pylint: disable=no-else-raise
raise RuntimeError('found multiple <application> tags') raise RuntimeError('found multiple <application> tags')
elif not elems: elif not elems:
if uses_libraries or optional_uses_libraries: if uses_libraries or optional_uses_libraries: #pylint: disable=undefined-variable
raise ManifestMismatchError('no <application> tag found') raise ManifestMismatchError('no <application> tag found')
return return
libs = get_children_with_tag(application, 'uses-library') libs = get_children_with_tag(application, 'uses-library')
required = [uses_library_name(x) for x in libs if uses_library_required(x)] required = [uses_library_name(x) for x in libs if uses_library_required(x)]
optional = [uses_library_name(x) for x in libs if not uses_library_required(x)] optional = [
uses_library_name(x) for x in libs if not uses_library_required(x)
]
# render <uses-library> tags as XML for a pretty error message # render <uses-library> tags as XML for a pretty error message
tags = [] tags = []
for lib in libs: for lib in libs:
tags.append(lib.toprettyxml()) tags.append(lib.toprettyxml())
required = first_unique_elements(required) required = first_unique_elements(required)
optional = first_unique_elements(optional) optional = first_unique_elements(optional)
tags = first_unique_elements(tags) tags = first_unique_elements(tags)
return required, optional, tags return required, optional, tags
def first_unique_elements(l): def first_unique_elements(l):
result = [] result = []
[result.append(x) for x in l if x not in result] for x in l:
return result if x not in result:
result.append(x)
return result
def uses_library_name(lib): def uses_library_name(lib):
"""Extract the name attribute of a uses-library tag. """Extract the name attribute of a uses-library tag.
Args: Args:
lib: a <uses-library> tag. lib: a <uses-library> tag.
""" """
name = lib.getAttributeNodeNS(android_ns, 'name') name = lib.getAttributeNodeNS(android_ns, 'name')
return name.value if name is not None else "" return name.value if name is not None else ''
def uses_library_required(lib): def uses_library_required(lib):
"""Extract the required attribute of a uses-library tag. """Extract the required attribute of a uses-library tag.
Args: Args:
lib: a <uses-library> tag. lib: a <uses-library> tag.
""" """
required = lib.getAttributeNodeNS(android_ns, 'required') required = lib.getAttributeNodeNS(android_ns, 'required')
return (required.value == 'true') if required is not None else True return (required.value == 'true') if required is not None else True
def extract_target_sdk_version(manifest, is_apk = False): def extract_target_sdk_version(manifest, is_apk=False):
"""Returns the targetSdkVersion from the manifest. """Returns the targetSdkVersion from the manifest.
Args: Args:
manifest: manifest (either parsed XML or aapt dump of APK) manifest: manifest (either parsed XML or aapt dump of APK)
is_apk: if the manifest comes from an APK or an XML file is_apk: if the manifest comes from an APK or an XML file
""" """
if is_apk: if is_apk: #pylint: disable=no-else-return
return extract_target_sdk_version_apk(manifest) return extract_target_sdk_version_apk(manifest)
else: else:
return extract_target_sdk_version_xml(manifest) return extract_target_sdk_version_xml(manifest)
def extract_target_sdk_version_apk(badging): def extract_target_sdk_version_apk(badging):
"""Extract targetSdkVersion tags from the manifest of an APK.""" """Extract targetSdkVersion tags from the manifest of an APK."""
pattern = re.compile("^targetSdkVersion?:'(.*)'$", re.MULTILINE) pattern = re.compile("^targetSdkVersion?:'(.*)'$", re.MULTILINE)
for match in re.finditer(pattern, badging): for match in re.finditer(pattern, badging):
return match.group(1) return match.group(1)
raise RuntimeError('cannot find targetSdkVersion in the manifest') raise RuntimeError('cannot find targetSdkVersion in the manifest')
def extract_target_sdk_version_xml(xml): def extract_target_sdk_version_xml(xml):
"""Extract targetSdkVersion tags from the manifest.""" """Extract targetSdkVersion tags from the manifest."""
manifest = parse_manifest(xml) manifest = parse_manifest(xml)
# Get or insert the uses-sdk element # Get or insert the uses-sdk element
uses_sdk = get_children_with_tag(manifest, 'uses-sdk') uses_sdk = get_children_with_tag(manifest, 'uses-sdk')
if len(uses_sdk) > 1: if len(uses_sdk) > 1: #pylint: disable=no-else-raise
raise RuntimeError('found multiple uses-sdk elements') raise RuntimeError('found multiple uses-sdk elements')
elif len(uses_sdk) == 0: elif len(uses_sdk) == 0:
raise RuntimeError('missing uses-sdk element') raise RuntimeError('missing uses-sdk element')
uses_sdk = uses_sdk[0] uses_sdk = uses_sdk[0]
min_attr = uses_sdk.getAttributeNodeNS(android_ns, 'minSdkVersion') min_attr = uses_sdk.getAttributeNodeNS(android_ns, 'minSdkVersion')
if min_attr is None: if min_attr is None:
raise RuntimeError('minSdkVersion is not specified') raise RuntimeError('minSdkVersion is not specified')
target_attr = uses_sdk.getAttributeNodeNS(android_ns, 'targetSdkVersion') target_attr = uses_sdk.getAttributeNodeNS(android_ns, 'targetSdkVersion')
if target_attr is None: if target_attr is None:
target_attr = min_attr target_attr = min_attr
return target_attr.value return target_attr.value
def load_dexpreopt_configs(configs): def load_dexpreopt_configs(configs):
"""Load dexpreopt.config files and map module names to library names.""" """Load dexpreopt.config files and map module names to library names."""
module_to_libname = {} module_to_libname = {}
if configs is None: if configs is None:
configs = [] configs = []
for config in configs: for config in configs:
with open(config, 'r') as f: with open(config, 'r') as f:
contents = json.load(f) contents = json.load(f)
module_to_libname[contents['Name']] = contents['ProvidesUsesLibrary'] module_to_libname[contents['Name']] = contents['ProvidesUsesLibrary']
return module_to_libname return module_to_libname
def translate_libnames(modules, module_to_libname): def translate_libnames(modules, module_to_libname):
"""Translate module names into library names using the mapping.""" """Translate module names into library names using the mapping."""
if modules is None: if modules is None:
modules = [] modules = []
libnames = [] libnames = []
for name in modules: for name in modules:
if name in module_to_libname: if name in module_to_libname:
name = module_to_libname[name] name = module_to_libname[name]
libnames.append(name) libnames.append(name)
return libnames return libnames
def main(): def main():
"""Program entry point.""" """Program entry point."""
try: try:
args = parse_args() args = parse_args()
# The input can be either an XML manifest or an APK, they are parsed and # The input can be either an XML manifest or an APK, they are parsed and
# processed in different ways. # processed in different ways.
is_apk = args.input.endswith('.apk') is_apk = args.input.endswith('.apk')
if is_apk: if is_apk:
aapt = args.aapt if args.aapt != None else "aapt" aapt = args.aapt if args.aapt is not None else 'aapt'
manifest = subprocess.check_output([aapt, "dump", "badging", args.input]) manifest = subprocess.check_output(
else: [aapt, 'dump', 'badging', args.input])
manifest = minidom.parse(args.input) else:
manifest = minidom.parse(args.input)
if args.enforce_uses_libraries: if args.enforce_uses_libraries:
# Load dexpreopt.config files and build a mapping from module names to # Load dexpreopt.config files and build a mapping from module
# library names. This is necessary because build system addresses # names to library names. This is necessary because build system
# libraries by their module name (`uses_libs`, `optional_uses_libs`, # addresses libraries by their module name (`uses_libs`,
# `LOCAL_USES_LIBRARIES`, `LOCAL_OPTIONAL_LIBRARY_NAMES` all contain # `optional_uses_libs`, `LOCAL_USES_LIBRARIES`,
# module names), while the manifest addresses libraries by their name. # `LOCAL_OPTIONAL_LIBRARY_NAMES` all contain module names), while
mod_to_lib = load_dexpreopt_configs(args.dexpreopt_configs) # the manifest addresses libraries by their name.
required = translate_libnames(args.uses_libraries, mod_to_lib) mod_to_lib = load_dexpreopt_configs(args.dexpreopt_configs)
optional = translate_libnames(args.optional_uses_libraries, mod_to_lib) required = translate_libnames(args.uses_libraries, mod_to_lib)
optional = translate_libnames(args.optional_uses_libraries,
mod_to_lib)
# Check if the <uses-library> lists in the build system agree with those # Check if the <uses-library> lists in the build system agree with
# in the manifest. Raise an exception on mismatch, unless the script was # those in the manifest. Raise an exception on mismatch, unless the
# passed a special parameter to suppress exceptions. # script was passed a special parameter to suppress exceptions.
errmsg = enforce_uses_libraries(manifest, required, optional, errmsg = enforce_uses_libraries(manifest, required, optional,
args.enforce_uses_libraries_relax, is_apk, args.input) args.enforce_uses_libraries_relax,
is_apk, args.input)
# Create a status file that is empty on success, or contains an error # Create a status file that is empty on success, or contains an
# message on failure. When exceptions are suppressed, dexpreopt command # error message on failure. When exceptions are suppressed,
# command will check file size to determine if the check has failed. # dexpreopt command command will check file size to determine if
if args.enforce_uses_libraries_status: # the check has failed.
with open(args.enforce_uses_libraries_status, 'w') as f: if args.enforce_uses_libraries_status:
if not errmsg == None: with open(args.enforce_uses_libraries_status, 'w') as f:
f.write("%s\n" % errmsg) if not errmsg is not None:
f.write('%s\n' % errmsg)
if args.extract_target_sdk_version: if args.extract_target_sdk_version:
try: try:
print(extract_target_sdk_version(manifest, is_apk)) print(extract_target_sdk_version(manifest, is_apk))
except: except: #pylint: disable=bare-except
# Failed; don't crash, return "any" SDK version. This will result in # Failed; don't crash, return "any" SDK version. This will
# dexpreopt not adding any compatibility libraries. # result in dexpreopt not adding any compatibility libraries.
print(10000) print(10000)
if args.output: if args.output:
# XML output is supposed to be written only when this script is invoked # XML output is supposed to be written only when this script is
# with XML input manifest, not with an APK. # invoked with XML input manifest, not with an APK.
if is_apk: if is_apk:
raise RuntimeError('cannot save APK manifest as XML') raise RuntimeError('cannot save APK manifest as XML')
with open(args.output, 'wb') as f: with open(args.output, 'wb') as f:
write_xml(f, manifest) write_xml(f, manifest)
# pylint: disable=broad-except
except Exception as err:
print('error: ' + str(err), file=sys.stderr)
sys.exit(-1)
# pylint: disable=broad-except
except Exception as err:
print('error: ' + str(err), file=sys.stderr)
sys.exit(-1)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -26,202 +26,235 @@ sys.dont_write_bytecode = True
def uses_library_xml(name, attr=''): def uses_library_xml(name, attr=''):
return '<uses-library android:name="%s"%s />' % (name, attr) return '<uses-library android:name="%s"%s />' % (name, attr)
def required_xml(value): def required_xml(value):
return ' android:required="%s"' % ('true' if value else 'false') return ' android:required="%s"' % ('true' if value else 'false')
def uses_library_apk(name, sfx=''): def uses_library_apk(name, sfx=''):
return "uses-library%s:'%s'" % (sfx, name) return "uses-library%s:'%s'" % (sfx, name)
def required_apk(value): def required_apk(value):
return '' if value else '-not-required' return '' if value else '-not-required'
class EnforceUsesLibrariesTest(unittest.TestCase): class EnforceUsesLibrariesTest(unittest.TestCase):
"""Unit tests for add_extract_native_libs function.""" """Unit tests for add_extract_native_libs function."""
def run_test(self, xml, apk, uses_libraries=[], optional_uses_libraries=[]): def run_test(self, xml, apk, uses_libraries=[], optional_uses_libraries=[]): #pylint: disable=dangerous-default-value
doc = minidom.parseString(xml) doc = minidom.parseString(xml)
try: try:
relax = False relax = False
manifest_check.enforce_uses_libraries(doc, uses_libraries, manifest_check.enforce_uses_libraries(
optional_uses_libraries, relax, False, 'path/to/X/AndroidManifest.xml') doc, uses_libraries, optional_uses_libraries, relax, False,
manifest_check.enforce_uses_libraries(apk, uses_libraries, 'path/to/X/AndroidManifest.xml')
optional_uses_libraries, relax, True, 'path/to/X/X.apk') manifest_check.enforce_uses_libraries(apk, uses_libraries,
return True optional_uses_libraries,
except manifest_check.ManifestMismatchError: relax, True,
return False 'path/to/X/X.apk')
return True
except manifest_check.ManifestMismatchError:
return False
xml_tmpl = ( xml_tmpl = (
'<?xml version="1.0" encoding="utf-8"?>\n' '<?xml version="1.0" encoding="utf-8"?>\n<manifest '
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' 'xmlns:android="http://schemas.android.com/apk/res/android">\n '
' <application>\n' '<application>\n %s\n </application>\n</manifest>\n')
' %s\n'
' </application>\n'
'</manifest>\n')
apk_tmpl = ( apk_tmpl = (
"package: name='com.google.android.something' versionCode='100'\n" "package: name='com.google.android.something' versionCode='100'\n"
"sdkVersion:'29'\n" "sdkVersion:'29'\n"
"targetSdkVersion:'29'\n" "targetSdkVersion:'29'\n"
"uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n" "uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n"
"%s\n" '%s\n'
"densities: '160' '240' '320' '480' '640' '65534") "densities: '160' '240' '320' '480' '640' '65534")
def test_uses_library(self): def test_uses_library(self):
xml = self.xml_tmpl % (uses_library_xml('foo')) xml = self.xml_tmpl % (uses_library_xml('foo'))
apk = self.apk_tmpl % (uses_library_apk('foo')) apk = self.apk_tmpl % (uses_library_apk('foo'))
matches = self.run_test(xml, apk, uses_libraries=['foo']) matches = self.run_test(xml, apk, uses_libraries=['foo'])
self.assertTrue(matches) self.assertTrue(matches)
def test_uses_library_required(self): def test_uses_library_required(self):
xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(True))) xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(True)))
apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(True))) apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(True)))
matches = self.run_test(xml, apk, uses_libraries=['foo']) matches = self.run_test(xml, apk, uses_libraries=['foo'])
self.assertTrue(matches) self.assertTrue(matches)
def test_optional_uses_library(self): def test_optional_uses_library(self):
xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False))) xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False))) apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
matches = self.run_test(xml, apk, optional_uses_libraries=['foo']) matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
self.assertTrue(matches) self.assertTrue(matches)
def test_expected_uses_library(self): def test_expected_uses_library(self):
xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False))) xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False))) apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
matches = self.run_test(xml, apk, uses_libraries=['foo']) matches = self.run_test(xml, apk, uses_libraries=['foo'])
self.assertFalse(matches) self.assertFalse(matches)
def test_expected_optional_uses_library(self): def test_expected_optional_uses_library(self):
xml = self.xml_tmpl % (uses_library_xml('foo')) xml = self.xml_tmpl % (uses_library_xml('foo'))
apk = self.apk_tmpl % (uses_library_apk('foo')) apk = self.apk_tmpl % (uses_library_apk('foo'))
matches = self.run_test(xml, apk, optional_uses_libraries=['foo']) matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
self.assertFalse(matches) self.assertFalse(matches)
def test_missing_uses_library(self): def test_missing_uses_library(self):
xml = self.xml_tmpl % ('') xml = self.xml_tmpl % ('')
apk = self.apk_tmpl % ('') apk = self.apk_tmpl % ('')
matches = self.run_test(xml, apk, uses_libraries=['foo']) matches = self.run_test(xml, apk, uses_libraries=['foo'])
self.assertFalse(matches) self.assertFalse(matches)
def test_missing_optional_uses_library(self): def test_missing_optional_uses_library(self):
xml = self.xml_tmpl % ('') xml = self.xml_tmpl % ('')
apk = self.apk_tmpl % ('') apk = self.apk_tmpl % ('')
matches = self.run_test(xml, apk, optional_uses_libraries=['foo']) matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
self.assertFalse(matches) self.assertFalse(matches)
def test_extra_uses_library(self): def test_extra_uses_library(self):
xml = self.xml_tmpl % (uses_library_xml('foo')) xml = self.xml_tmpl % (uses_library_xml('foo'))
apk = self.apk_tmpl % (uses_library_xml('foo')) apk = self.apk_tmpl % (uses_library_xml('foo'))
matches = self.run_test(xml, apk) matches = self.run_test(xml, apk)
self.assertFalse(matches) self.assertFalse(matches)
def test_extra_optional_uses_library(self): def test_extra_optional_uses_library(self):
xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False))) xml = self.xml_tmpl % (uses_library_xml('foo', required_xml(False)))
apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False))) apk = self.apk_tmpl % (uses_library_apk('foo', required_apk(False)))
matches = self.run_test(xml, apk) matches = self.run_test(xml, apk)
self.assertFalse(matches) self.assertFalse(matches)
def test_multiple_uses_library(self): def test_multiple_uses_library(self):
xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'), xml = self.xml_tmpl % ('\n'.join(
uses_library_xml('bar')])) [uses_library_xml('foo'),
apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'), uses_library_xml('bar')]))
uses_library_apk('bar')])) apk = self.apk_tmpl % ('\n'.join(
matches = self.run_test(xml, apk, uses_libraries=['foo', 'bar']) [uses_library_apk('foo'),
self.assertTrue(matches) uses_library_apk('bar')]))
matches = self.run_test(xml, apk, uses_libraries=['foo', 'bar'])
self.assertTrue(matches)
def test_multiple_optional_uses_library(self): def test_multiple_optional_uses_library(self):
xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo', required_xml(False)), xml = self.xml_tmpl % ('\n'.join([
uses_library_xml('bar', 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_xml('bar', required_xml(False))
uses_library_apk('bar', required_apk(False))])) ]))
matches = self.run_test(xml, apk, optional_uses_libraries=['foo', 'bar']) apk = self.apk_tmpl % ('\n'.join([
self.assertTrue(matches) 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): def test_order_uses_library(self):
xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'), xml = self.xml_tmpl % ('\n'.join(
uses_library_xml('bar')])) [uses_library_xml('foo'),
apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'), uses_library_xml('bar')]))
uses_library_apk('bar')])) apk = self.apk_tmpl % ('\n'.join(
matches = self.run_test(xml, apk, uses_libraries=['bar', 'foo']) [uses_library_apk('foo'),
self.assertFalse(matches) uses_library_apk('bar')]))
matches = self.run_test(xml, apk, uses_libraries=['bar', 'foo'])
self.assertFalse(matches)
def test_order_optional_uses_library(self): def test_order_optional_uses_library(self):
xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo', required_xml(False)), xml = self.xml_tmpl % ('\n'.join([
uses_library_xml('bar', 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_xml('bar', required_xml(False))
uses_library_apk('bar', required_apk(False))])) ]))
matches = self.run_test(xml, apk, optional_uses_libraries=['bar', 'foo']) apk = self.apk_tmpl % ('\n'.join([
self.assertFalse(matches) 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): def test_duplicate_uses_library(self):
xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'), 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_xml('foo')]))
uses_library_apk('foo')])) apk = self.apk_tmpl % ('\n'.join(
matches = self.run_test(xml, apk, uses_libraries=['foo']) [uses_library_apk('foo'),
self.assertTrue(matches) uses_library_apk('foo')]))
matches = self.run_test(xml, apk, uses_libraries=['foo'])
self.assertTrue(matches)
def test_duplicate_optional_uses_library(self): def test_duplicate_optional_uses_library(self):
xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo', required_xml(False)), 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_xml('foo', required_xml(False))
uses_library_apk('foo', required_apk(False))])) ]))
matches = self.run_test(xml, apk, optional_uses_libraries=['foo']) apk = self.apk_tmpl % ('\n'.join([
self.assertTrue(matches) 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): def test_mixed(self):
xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'), xml = self.xml_tmpl % ('\n'.join([
uses_library_xml('bar', required_xml(False))])) uses_library_xml('foo'),
apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'), uses_library_xml('bar', required_xml(False))
uses_library_apk('bar', required_apk(False))])) ]))
matches = self.run_test(xml, apk, uses_libraries=['foo'], apk = self.apk_tmpl % ('\n'.join([
optional_uses_libraries=['bar']) uses_library_apk('foo'),
self.assertTrue(matches) 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): def test_mixed_with_namespace(self):
xml = self.xml_tmpl % ('\n'.join([uses_library_xml('foo'), xml = self.xml_tmpl % ('\n'.join([
uses_library_xml('bar', required_xml(False))])) uses_library_xml('foo'),
apk = self.apk_tmpl % ('\n'.join([uses_library_apk('foo'), uses_library_xml('bar', required_xml(False))
uses_library_apk('bar', required_apk(False))])) ]))
matches = self.run_test(xml, apk, uses_libraries=['//x/y/z:foo'], apk = self.apk_tmpl % ('\n'.join([
optional_uses_libraries=['//x/y/z:bar']) uses_library_apk('foo'),
self.assertTrue(matches) 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)
class ExtractTargetSdkVersionTest(unittest.TestCase): 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 = ( def run_test(self, xml, apk, version):
'<?xml version="1.0" encoding="utf-8"?>\n' doc = minidom.parseString(xml)
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n' v = manifest_check.extract_target_sdk_version(doc, is_apk=False)
' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="%s" />\n' self.assertEqual(v, version)
'</manifest>\n') v = manifest_check.extract_target_sdk_version(apk, is_apk=True)
self.assertEqual(v, version)
apk_tmpl = ( xml_tmpl = (
"package: name='com.google.android.something' versionCode='100'\n" '<?xml version="1.0" encoding="utf-8"?>\n<manifest '
"sdkVersion:'28'\n" 'xmlns:android="http://schemas.android.com/apk/res/android">\n '
"targetSdkVersion:'%s'\n" '<uses-sdk android:minSdkVersion="28" android:targetSdkVersion="%s" '
"uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n") '/>\n</manifest>\n')
def test_targert_sdk_version_28(self): apk_tmpl = (
xml = self.xml_tmpl % "28" "package: name='com.google.android.something' versionCode='100'\n"
apk = self.apk_tmpl % "28" "sdkVersion:'28'\n"
self.run_test(xml, apk, "28") "targetSdkVersion:'%s'\n"
"uses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n")
def test_targert_sdk_version_28(self):
xml = self.xml_tmpl % '28'
apk = self.apk_tmpl % '28'
self.run_test(xml, apk, '28')
def test_targert_sdk_version_29(self):
xml = self.xml_tmpl % '29'
apk = self.apk_tmpl % '29'
self.run_test(xml, apk, '29')
def test_targert_sdk_version_29(self):
xml = self.xml_tmpl % "29"
apk = self.apk_tmpl % "29"
self.run_test(xml, apk, "29")
if __name__ == '__main__': if __name__ == '__main__':
unittest.main(verbosity=2) unittest.main(verbosity=2)