Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 1 | #!/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 Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 16 | """Unit tests for signature_patterns.py.""" |
| 17 | import io |
| 18 | import unittest |
| 19 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 20 | import signature_patterns |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 21 | |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 22 | |
| 23 | class TestGeneratedPatterns(unittest.TestCase): |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 24 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 25 | csv_flags = """ |
Paul Duffin | 6ffdff8 | 2021-08-09 13:47:19 +0100 | [diff] [blame] | 26 | Ljava/lang/ProcessBuilder$Redirect$1;-><init>()V,blocked |
| 27 | Ljava/lang/Character$UnicodeScript;->of(I)Ljava/lang/Character$UnicodeScript;,public-api |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 28 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 29 | Ljava/lang/Object;->toString()Ljava/lang/String;,blocked |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 30 | """ |
| 31 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 32 | @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 Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 39 | |
| 40 | def test_generate_default(self): |
| 41 | patterns = self.produce_patterns_from_string( |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 42 | TestGeneratedPatterns.csv_flags) |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 43 | expected = [ |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 44 | 'java/lang/*', |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 45 | ] |
| 46 | self.assertEqual(expected, patterns) |
| 47 | |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 48 | def test_generate_split_package(self): |
| 49 | patterns = self.produce_patterns_from_string( |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 50 | TestGeneratedPatterns.csv_flags, split_packages={'java/lang'}) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 51 | 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 Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 60 | TestGeneratedPatterns.csv_flags, split_packages={'*'}) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 61 | 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 Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 70 | TestGeneratedPatterns.csv_flags, package_prefixes={'java/lang'}) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 71 | 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 Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 78 | TestGeneratedPatterns.csv_flags, package_prefixes={'java'}) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 79 | expected = [ |
| 80 | 'java/**', |
| 81 | ] |
| 82 | self.assertEqual(expected, patterns) |
| 83 | |
| 84 | def test_split_package_wildcard_conflicts_with_other_split_packages(self): |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 85 | errors = signature_patterns.validate_split_packages({'*', 'java'}) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 86 | 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 Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 94 | errors = signature_patterns.validate_package_prefixes( |
| 95 | {'*'}, package_prefixes={'java'}) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 96 | expected = [ |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 97 | "split package '*' conflicts with all package prefixes java\n" |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 98 | ' add split_packages:[] to fix', |
| 99 | ] |
| 100 | self.assertEqual(expected, errors) |
| 101 | |
| 102 | def test_split_package_conflict(self): |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 103 | errors = signature_patterns.validate_package_prefixes( |
| 104 | {'java/split'}, package_prefixes={'java'}) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 105 | expected = [ |
| 106 | 'split package java.split is matched by package prefix java', |
| 107 | ] |
| 108 | self.assertEqual(expected, errors) |
| 109 | |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 110 | |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 111 | if __name__ == '__main__': |
| 112 | unittest.main(verbosity=2) |