platform_build_soong/scripts/hiddenapi/signature_patterns_test.py
Paul Duffin 1e18e98b14 Allow implementation details to be excluded from signature patterns file
Previously, the signature-patterns.csv file (which was output in an sdk
snapshot) included implementation details, e.g. the names of classes
that are not part of any API, including the hidden API. This change
allows a bootclasspath_fragment module owner to customize the generated
signature patterns file to remove as many implementation details as
possible from the sdk snapshot. That means that implementation only
changes would not require that sdk snapshots be updated in sync with
the corresponding APEX.

Module owners can remove implementation in the following ways:
1) Limit the number of packages that are treated as split packages by
   explicitly specifying the split packages in the split_packages
   property. A split package is one whose classes are provided by
   separate bootclasspath_fragment modules and so the signature
   patterns has to include every class in that package provided by a
   specific bootclasspath_fragment module, including implementation
   classes, instead of just listing the package.

   The default is to treat all packages as being split, so if no
   split_packages is specified then it defaults to ["*"] which matches
   all packages. Assuming that no package was split unless specifically
   stated would require that all the modules that had split packages
   explicitly list them before this change could be submitted as
   without them this change would break the build.

   Once all existing modules with split packages have been updated to
   include them then the default may be changed.

2) Adding package prefixes for any hierarchy of packages that are
   owned solely by that bootclasspath_fragment. This removes the need
   to list the packages in that hierarchy, including implementation
   specific packages.

Bug: 194063708
Test: atest --host verify_overlaps_test signature_patterns_test
      m out/soong/hiddenapi/hiddenapi-flags.csv
      - manually change files to cause difference in flags to check
        that it detects the differences.
Change-Id: If2f90cfc41b6fff9fa4ac8b9d4973110938b9392
2021-10-05 15:47:05 +01:00

111 lines
4 KiB
Python
Executable file

#!/usr/bin/env python
#
# Copyright (C) 2021 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 signature_patterns.py."""
import io
import unittest
from signature_patterns import * #pylint: disable=unused-wildcard-import,wildcard-import
class TestGeneratedPatterns(unittest.TestCase):
csvFlags = """
Ljava/lang/ProcessBuilder$Redirect$1;-><init>()V,blocked
Ljava/lang/Character$UnicodeScript;->of(I)Ljava/lang/Character$UnicodeScript;,public-api
Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api
Ljava/lang/Object;->toString()Ljava/lang/String;,blocked
"""
def produce_patterns_from_string(self,
csv,
splitPackages=None,
packagePrefixes=None):
with io.StringIO(csv) as f:
return produce_patterns_from_stream(f, splitPackages,
packagePrefixes)
def test_generate_default(self):
patterns = self.produce_patterns_from_string(
TestGeneratedPatterns.csvFlags)
expected = [
'java/lang/*',
]
self.assertEqual(expected, patterns)
def test_generate_split_package(self):
patterns = self.produce_patterns_from_string(
TestGeneratedPatterns.csvFlags, splitPackages={'java/lang'})
expected = [
'java/lang/Character',
'java/lang/Object',
'java/lang/ProcessBuilder',
]
self.assertEqual(expected, patterns)
def test_generate_split_package_wildcard(self):
patterns = self.produce_patterns_from_string(
TestGeneratedPatterns.csvFlags, splitPackages={'*'})
expected = [
'java/lang/Character',
'java/lang/Object',
'java/lang/ProcessBuilder',
]
self.assertEqual(expected, patterns)
def test_generate_package_prefix(self):
patterns = self.produce_patterns_from_string(
TestGeneratedPatterns.csvFlags, packagePrefixes={'java/lang'})
expected = [
'java/lang/**',
]
self.assertEqual(expected, patterns)
def test_generate_package_prefix_top_package(self):
patterns = self.produce_patterns_from_string(
TestGeneratedPatterns.csvFlags, packagePrefixes={'java'})
expected = [
'java/**',
]
self.assertEqual(expected, patterns)
def test_split_package_wildcard_conflicts_with_other_split_packages(self):
errors = validate_split_packages({'*', 'java'})
expected = [
'split packages are invalid as they contain both the wildcard (*)'
' and specific packages, use the wildcard or specific packages,'
' not a mixture'
]
self.assertEqual(expected, errors)
def test_split_package_wildcard_conflicts_with_package_prefixes(self):
errors = validate_package_prefixes({'*'}, packagePrefixes={'java'})
expected = [
'split package "*" conflicts with all package prefixes java\n'
' add split_packages:[] to fix',
]
self.assertEqual(expected, errors)
def test_split_package_conflict(self):
errors = validate_package_prefixes({'java/split'},
packagePrefixes={'java'})
expected = [
'split package java.split is matched by package prefix java',
]
self.assertEqual(expected, errors)
if __name__ == '__main__':
unittest.main(verbosity=2)