2019-05-20 22:14:18 +02:00
|
|
|
#!/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
|
|
|
|
|
|
|
|
|
2021-03-03 17:38:37 +01:00
|
|
|
def uses_library_xml(name, attr=''):
|
2019-05-20 22:14:18 +02:00
|
|
|
return '<uses-library android:name="%s"%s />' % (name, attr)
|
|
|
|
|
|
|
|
|
2021-03-03 17:38:37 +01:00
|
|
|
def required_xml(value):
|
2019-05-20 22:14:18 +02:00
|
|
|
return ' android:required="%s"' % ('true' if value else 'false')
|
|
|
|
|
|
|
|
|
2021-03-03 17:38:37 +01:00
|
|
|
def uses_library_apk(name, sfx=''):
|
|
|
|
return "uses-library%s:'%s'" % (sfx, name)
|
|
|
|
|
|
|
|
|
|
|
|
def required_apk(value):
|
|
|
|
return '' if value else '-not-required'
|
|
|
|
|
|
|
|
|
2019-05-20 22:14:18 +02:00
|
|
|
class EnforceUsesLibrariesTest(unittest.TestCase):
|
|
|
|
"""Unit tests for add_extract_native_libs function."""
|
|
|
|
|
2021-03-03 17:38:37 +01:00
|
|
|
def run_test(self, xml, apk, uses_libraries=[], optional_uses_libraries=[]):
|
|
|
|
doc = minidom.parseString(xml)
|
2019-05-20 22:14:18 +02:00
|
|
|
try:
|
Add non-fatal mode for verify_uses_libraries check.
The new mode is enabled with environment variable
RELAX_USES_LIBRARY_CHECK. If the variable is set to true, then a
verify_uses_libraries check failure does not fail the build, instead it
sets a special compiler filter "extract" for dexpreopt, which means that
the DEX file will be extracted, but it won't be compiled to native code.
Class loader context will be set to empty in this case (not &, as it is
going to be deprecated soon).
If the variable RELAX_USES_LIBRARY_CHECK is unset or set to something
other than "true", then the old behaviour of the verify_uses_libraries
check is preserved.
The intended use case for this flag is to have a smoother migration path
for the Java modules that need to add <uses-library> information in
the build files. The flag allows to quickly silence build errors. This
flag should be used with caution and only as a temporary measure, as it
masks real errors and affects performance.
verify_uses_libraries check is reworked so that it writes the error
message to a status file (which is used instead of the former timestamp
file). Currently the stored error message is not used, but it may be
used later to produce a warning. Dexpreopt command checks if the status
file exists and is nonempty; if that is the case, then compiler filter
is set to "extract".
Bug: 132357300
Test: Manually add some mismatch between the libraries in the Android.bp
and Android.mk files for dexpreopted apps, build with
RELAX_USES_LIBRARY_CHECK=true and obsserve that the build doesn't
fail and they are compiled with compiler-filter "extract".
Unset RELAX_USES_LIBRARY_CHECK and observe that the build fails.
Change-Id: Ibb5d993a25b1df1d2e70b7d5aafc6997f9d64e67
2021-02-17 17:23:28 +01:00
|
|
|
relax = False
|
|
|
|
manifest_check.enforce_uses_libraries(doc, uses_libraries,
|
2021-03-30 18:15:16 +02:00
|
|
|
optional_uses_libraries, relax, False, 'path/to/X/AndroidManifest.xml')
|
2021-03-03 17:38:37 +01:00
|
|
|
manifest_check.enforce_uses_libraries(apk, uses_libraries,
|
2021-03-30 18:15:16 +02:00
|
|
|
optional_uses_libraries, relax, True, 'path/to/X/X.apk')
|
2019-05-20 22:14:18 +02:00
|
|
|
return True
|
|
|
|
except manifest_check.ManifestMismatchError:
|
|
|
|
return False
|
|
|
|
|
2021-03-03 17:38:37 +01:00
|
|
|
xml_tmpl = (
|
2019-05-20 22:14:18 +02:00
|
|
|
'<?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')
|
|
|
|
|
2021-03-03 17:38:37 +01:00
|
|
|
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")
|
|
|
|
|
2019-05-20 22:14:18 +02:00
|
|
|
def test_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertTrue(matches)
|
|
|
|
|
|
|
|
def test_uses_library_required(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertTrue(matches)
|
|
|
|
|
|
|
|
def test_optional_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertTrue(matches)
|
|
|
|
|
|
|
|
def test_expected_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertFalse(matches)
|
|
|
|
|
|
|
|
def test_expected_optional_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertFalse(matches)
|
|
|
|
|
|
|
|
def test_missing_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
xml = self.xml_tmpl % ('')
|
|
|
|
apk = self.apk_tmpl % ('')
|
|
|
|
matches = self.run_test(xml, apk, uses_libraries=['foo'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertFalse(matches)
|
|
|
|
|
|
|
|
def test_missing_optional_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
xml = self.xml_tmpl % ('')
|
|
|
|
apk = self.apk_tmpl % ('')
|
|
|
|
matches = self.run_test(xml, apk, optional_uses_libraries=['foo'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertFalse(matches)
|
|
|
|
|
|
|
|
def test_extra_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
xml = self.xml_tmpl % (uses_library_xml('foo'))
|
|
|
|
apk = self.apk_tmpl % (uses_library_xml('foo'))
|
|
|
|
matches = self.run_test(xml, apk)
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertFalse(matches)
|
|
|
|
|
|
|
|
def test_extra_optional_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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)
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertFalse(matches)
|
|
|
|
|
|
|
|
def test_multiple_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertTrue(matches)
|
|
|
|
|
|
|
|
def test_multiple_optional_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertTrue(matches)
|
|
|
|
|
|
|
|
def test_order_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertFalse(matches)
|
|
|
|
|
|
|
|
def test_order_optional_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertFalse(matches)
|
|
|
|
|
|
|
|
def test_duplicate_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertTrue(matches)
|
|
|
|
|
|
|
|
def test_duplicate_optional_uses_library(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'])
|
2019-05-20 22:14:18 +02:00
|
|
|
self.assertTrue(matches)
|
|
|
|
|
|
|
|
def test_mixed(self):
|
2021-03-03 17:38:37 +01:00
|
|
|
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'],
|
2019-05-20 22:14:18 +02:00
|
|
|
optional_uses_libraries=['bar'])
|
|
|
|
self.assertTrue(matches)
|
|
|
|
|
2021-07-20 15:27:32 +02:00
|
|
|
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)
|
|
|
|
|
2019-05-20 22:14:18 +02:00
|
|
|
|
|
|
|
class ExtractTargetSdkVersionTest(unittest.TestCase):
|
2021-03-03 17:38:37 +01:00
|
|
|
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 = (
|
2019-05-20 22:14:18 +02:00
|
|
|
'<?xml version="1.0" encoding="utf-8"?>\n'
|
|
|
|
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
|
2021-03-03 17:38:37 +01:00
|
|
|
' <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="%s" />\n'
|
2019-05-20 22:14:18 +02:00
|
|
|
'</manifest>\n')
|
|
|
|
|
2021-03-03 17:38:37 +01:00
|
|
|
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_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")
|
2019-05-20 22:14:18 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=2)
|