blob: 5c5635c2c34dfe6067888b87f90a7e3043c8b239 [file] [log] [blame]
Paul Duffin67b9d612021-07-21 17:38:47 +01001#!/usr/bin/env python
2#
3# Copyright (C) 2021 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.
Paul Duffin67b9d612021-07-21 17:38:47 +010016"""Unit tests for signature_patterns.py."""
17import io
18import unittest
19
Paul Duffin1024f1d2022-03-15 17:45:57 +000020import signature_patterns
Spandan Das2c2219b2021-08-25 17:47:43 +000021
Paul Duffin67b9d612021-07-21 17:38:47 +010022
23class TestGeneratedPatterns(unittest.TestCase):
Paul Duffin67b9d612021-07-21 17:38:47 +010024
Paul Duffin1024f1d2022-03-15 17:45:57 +000025 csv_flags = """
Paul Duffin6ffdff82021-08-09 13:47:19 +010026Ljava/lang/ProcessBuilder$Redirect$1;-><init>()V,blocked
27Ljava/lang/Character$UnicodeScript;->of(I)Ljava/lang/Character$UnicodeScript;,public-api
Paul Duffin67b9d612021-07-21 17:38:47 +010028Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api
29Ljava/lang/Object;->toString()Ljava/lang/String;,blocked
Paul Duffin1e18e982021-08-03 15:42:27 +010030"""
31
Paul Duffin1024f1d2022-03-15 17:45:57 +000032 @staticmethod
33 def produce_patterns_from_string(csv_text,
34 split_packages=None,
35 package_prefixes=None):
36 with io.StringIO(csv_text) as f:
37 return signature_patterns.produce_patterns_from_stream(
38 f, split_packages, package_prefixes)
Paul Duffin1e18e982021-08-03 15:42:27 +010039
40 def test_generate_default(self):
41 patterns = self.produce_patterns_from_string(
Paul Duffin1024f1d2022-03-15 17:45:57 +000042 TestGeneratedPatterns.csv_flags)
Paul Duffin67b9d612021-07-21 17:38:47 +010043 expected = [
Paul Duffin1e18e982021-08-03 15:42:27 +010044 'java/lang/*',
Paul Duffin67b9d612021-07-21 17:38:47 +010045 ]
46 self.assertEqual(expected, patterns)
47
Paul Duffin1e18e982021-08-03 15:42:27 +010048 def test_generate_split_package(self):
49 patterns = self.produce_patterns_from_string(
Paul Duffin1024f1d2022-03-15 17:45:57 +000050 TestGeneratedPatterns.csv_flags, split_packages={'java/lang'})
Paul Duffin1e18e982021-08-03 15:42:27 +010051 expected = [
52 'java/lang/Character',
53 'java/lang/Object',
54 'java/lang/ProcessBuilder',
55 ]
56 self.assertEqual(expected, patterns)
57
58 def test_generate_split_package_wildcard(self):
59 patterns = self.produce_patterns_from_string(
Paul Duffin1024f1d2022-03-15 17:45:57 +000060 TestGeneratedPatterns.csv_flags, split_packages={'*'})
Paul Duffin1e18e982021-08-03 15:42:27 +010061 expected = [
62 'java/lang/Character',
63 'java/lang/Object',
64 'java/lang/ProcessBuilder',
65 ]
66 self.assertEqual(expected, patterns)
67
68 def test_generate_package_prefix(self):
69 patterns = self.produce_patterns_from_string(
Paul Duffin1024f1d2022-03-15 17:45:57 +000070 TestGeneratedPatterns.csv_flags, package_prefixes={'java/lang'})
Paul Duffin1e18e982021-08-03 15:42:27 +010071 expected = [
72 'java/lang/**',
73 ]
74 self.assertEqual(expected, patterns)
75
76 def test_generate_package_prefix_top_package(self):
77 patterns = self.produce_patterns_from_string(
Paul Duffin1024f1d2022-03-15 17:45:57 +000078 TestGeneratedPatterns.csv_flags, package_prefixes={'java'})
Paul Duffin1e18e982021-08-03 15:42:27 +010079 expected = [
80 'java/**',
81 ]
82 self.assertEqual(expected, patterns)
83
84 def test_split_package_wildcard_conflicts_with_other_split_packages(self):
Paul Duffin1024f1d2022-03-15 17:45:57 +000085 errors = signature_patterns.validate_split_packages({'*', 'java'})
Paul Duffin1e18e982021-08-03 15:42:27 +010086 expected = [
87 'split packages are invalid as they contain both the wildcard (*)'
88 ' and specific packages, use the wildcard or specific packages,'
89 ' not a mixture'
90 ]
91 self.assertEqual(expected, errors)
92
93 def test_split_package_wildcard_conflicts_with_package_prefixes(self):
Paul Duffin1024f1d2022-03-15 17:45:57 +000094 errors = signature_patterns.validate_package_prefixes(
95 {'*'}, package_prefixes={'java'})
Paul Duffin1e18e982021-08-03 15:42:27 +010096 expected = [
Paul Duffin1024f1d2022-03-15 17:45:57 +000097 "split package '*' conflicts with all package prefixes java\n"
Paul Duffin1e18e982021-08-03 15:42:27 +010098 ' add split_packages:[] to fix',
99 ]
100 self.assertEqual(expected, errors)
101
102 def test_split_package_conflict(self):
Paul Duffin1024f1d2022-03-15 17:45:57 +0000103 errors = signature_patterns.validate_package_prefixes(
104 {'java/split'}, package_prefixes={'java'})
Paul Duffin1e18e982021-08-03 15:42:27 +0100105 expected = [
106 'split package java.split is matched by package prefix java',
107 ]
108 self.assertEqual(expected, errors)
109
Spandan Das2c2219b2021-08-25 17:47:43 +0000110
Paul Duffin67b9d612021-07-21 17:38:47 +0100111if __name__ == '__main__':
112 unittest.main(verbosity=2)