Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2022 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. |
| 16 | """Unit tests for verify_overlaps_test.py.""" |
| 17 | import io |
| 18 | import unittest |
| 19 | |
| 20 | from signature_trie import InteriorNode |
| 21 | from signature_trie import signature_trie |
| 22 | |
| 23 | |
| 24 | class TestSignatureToElements(unittest.TestCase): |
| 25 | |
| 26 | @staticmethod |
| 27 | def signature_to_elements(signature): |
| 28 | return InteriorNode.signature_to_elements(signature) |
| 29 | |
| 30 | def test_nested_inner_classes(self): |
| 31 | elements = [ |
| 32 | "package:java", |
| 33 | "package:lang", |
| 34 | "class:ProcessBuilder", |
| 35 | "class:Redirect", |
| 36 | "class:1", |
| 37 | "member:<init>()V", |
| 38 | ] |
| 39 | signature = "Ljava/lang/ProcessBuilder$Redirect$1;-><init>()V" |
| 40 | self.assertEqual(elements, self.signature_to_elements(signature)) |
| 41 | |
| 42 | def test_basic_member(self): |
| 43 | elements = [ |
| 44 | "package:java", |
| 45 | "package:lang", |
| 46 | "class:Object", |
| 47 | "member:hashCode()I", |
| 48 | ] |
| 49 | signature = "Ljava/lang/Object;->hashCode()I" |
| 50 | self.assertEqual(elements, self.signature_to_elements(signature)) |
| 51 | |
| 52 | def test_double_dollar_class(self): |
| 53 | elements = [ |
| 54 | "package:java", |
| 55 | "package:lang", |
| 56 | "class:CharSequence", |
| 57 | "class:", |
| 58 | "class:ExternalSyntheticLambda0", |
| 59 | "member:<init>(Ljava/lang/CharSequence;)V", |
| 60 | ] |
| 61 | signature = "Ljava/lang/CharSequence$$ExternalSyntheticLambda0;" \ |
| 62 | "-><init>(Ljava/lang/CharSequence;)V" |
| 63 | self.assertEqual(elements, self.signature_to_elements(signature)) |
| 64 | |
| 65 | def test_no_member(self): |
| 66 | elements = [ |
| 67 | "package:java", |
| 68 | "package:lang", |
| 69 | "class:CharSequence", |
| 70 | "class:", |
| 71 | "class:ExternalSyntheticLambda0", |
| 72 | ] |
| 73 | signature = "Ljava/lang/CharSequence$$ExternalSyntheticLambda0" |
| 74 | self.assertEqual(elements, self.signature_to_elements(signature)) |
| 75 | |
| 76 | def test_wildcard(self): |
| 77 | elements = [ |
| 78 | "package:java", |
| 79 | "package:lang", |
Paul Duffin | 19255f1 | 2022-03-08 16:35:52 +0000 | [diff] [blame^] | 80 | "wildcard:*", |
Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 81 | ] |
| 82 | signature = "java/lang/*" |
| 83 | self.assertEqual(elements, self.signature_to_elements(signature)) |
| 84 | |
| 85 | def test_recursive_wildcard(self): |
| 86 | elements = [ |
| 87 | "package:java", |
| 88 | "package:lang", |
Paul Duffin | 19255f1 | 2022-03-08 16:35:52 +0000 | [diff] [blame^] | 89 | "wildcard:**", |
Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 90 | ] |
| 91 | signature = "java/lang/**" |
| 92 | self.assertEqual(elements, self.signature_to_elements(signature)) |
| 93 | |
| 94 | def test_no_packages_wildcard(self): |
| 95 | elements = [ |
Paul Duffin | 19255f1 | 2022-03-08 16:35:52 +0000 | [diff] [blame^] | 96 | "wildcard:*", |
Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 97 | ] |
| 98 | signature = "*" |
| 99 | self.assertEqual(elements, self.signature_to_elements(signature)) |
| 100 | |
| 101 | def test_no_packages_recursive_wildcard(self): |
| 102 | elements = [ |
Paul Duffin | 19255f1 | 2022-03-08 16:35:52 +0000 | [diff] [blame^] | 103 | "wildcard:**", |
Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 104 | ] |
| 105 | signature = "**" |
| 106 | self.assertEqual(elements, self.signature_to_elements(signature)) |
| 107 | |
Paul Duffin | 19255f1 | 2022-03-08 16:35:52 +0000 | [diff] [blame^] | 108 | def test_invalid_no_class_or_wildcard(self): |
| 109 | signature = "java/lang" |
| 110 | with self.assertRaises(Exception) as context: |
| 111 | self.signature_to_elements(signature) |
| 112 | self.assertIn( |
| 113 | "last element 'lang' is lower case but should be an " |
| 114 | "upper case class name or wildcard", str(context.exception)) |
| 115 | |
Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 116 | def test_non_standard_class_name(self): |
| 117 | elements = [ |
| 118 | "package:javax", |
| 119 | "package:crypto", |
| 120 | "class:extObjectInputStream", |
| 121 | ] |
| 122 | signature = "Ljavax/crypto/extObjectInputStream" |
| 123 | self.assertEqual(elements, self.signature_to_elements(signature)) |
| 124 | |
Paul Duffin | 19255f1 | 2022-03-08 16:35:52 +0000 | [diff] [blame^] | 125 | def test_invalid_pattern_wildcard(self): |
| 126 | pattern = "Ljava/lang/Class*" |
| 127 | with self.assertRaises(Exception) as context: |
| 128 | self.signature_to_elements(pattern) |
| 129 | self.assertIn("invalid wildcard 'Class*'", str(context.exception)) |
| 130 | |
Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 131 | def test_invalid_pattern_wildcard_and_member(self): |
| 132 | pattern = "Ljava/lang/*;->hashCode()I" |
| 133 | with self.assertRaises(Exception) as context: |
| 134 | self.signature_to_elements(pattern) |
Paul Duffin | 19255f1 | 2022-03-08 16:35:52 +0000 | [diff] [blame^] | 135 | self.assertIn( |
| 136 | "contains wildcard '*' and member signature 'hashCode()I'", |
| 137 | str(context.exception)) |
Paul Duffin | b5cd522 | 2022-02-28 19:06:49 +0000 | [diff] [blame] | 138 | |
| 139 | |
| 140 | class TestGetMatchingRows(unittest.TestCase): |
| 141 | extractInput = """ |
| 142 | Ljava/lang/Character$UnicodeScript;->of(I)Ljava/lang/Character$UnicodeScript; |
| 143 | Ljava/lang/Character;->serialVersionUID:J |
| 144 | Ljava/lang/Object;->hashCode()I |
| 145 | Ljava/lang/Object;->toString()Ljava/lang/String; |
| 146 | Ljava/lang/ProcessBuilder$Redirect$1;-><init>()V |
| 147 | Ljava/util/zip/ZipFile;-><clinit>()V |
| 148 | """ |
| 149 | |
| 150 | def read_trie(self): |
| 151 | trie = signature_trie() |
| 152 | with io.StringIO(self.extractInput.strip()) as f: |
| 153 | for line in iter(f.readline, ""): |
| 154 | line = line.rstrip() |
| 155 | trie.add(line, line) |
| 156 | return trie |
| 157 | |
| 158 | def check_patterns(self, pattern, expected): |
| 159 | trie = self.read_trie() |
| 160 | self.check_node_patterns(trie, pattern, expected) |
| 161 | |
| 162 | def check_node_patterns(self, node, pattern, expected): |
| 163 | actual = list(node.get_matching_rows(pattern)) |
| 164 | actual.sort() |
| 165 | self.assertEqual(expected, actual) |
| 166 | |
| 167 | def test_member_pattern(self): |
| 168 | self.check_patterns("java/util/zip/ZipFile;-><clinit>()V", |
| 169 | ["Ljava/util/zip/ZipFile;-><clinit>()V"]) |
| 170 | |
| 171 | def test_class_pattern(self): |
| 172 | self.check_patterns("java/lang/Object", [ |
| 173 | "Ljava/lang/Object;->hashCode()I", |
| 174 | "Ljava/lang/Object;->toString()Ljava/lang/String;", |
| 175 | ]) |
| 176 | |
| 177 | # pylint: disable=line-too-long |
| 178 | def test_nested_class_pattern(self): |
| 179 | self.check_patterns("java/lang/Character", [ |
| 180 | "Ljava/lang/Character$UnicodeScript;->of(I)Ljava/lang/Character$UnicodeScript;", |
| 181 | "Ljava/lang/Character;->serialVersionUID:J", |
| 182 | ]) |
| 183 | |
| 184 | def test_wildcard(self): |
| 185 | self.check_patterns("java/lang/*", [ |
| 186 | "Ljava/lang/Character$UnicodeScript;->of(I)Ljava/lang/Character$UnicodeScript;", |
| 187 | "Ljava/lang/Character;->serialVersionUID:J", |
| 188 | "Ljava/lang/Object;->hashCode()I", |
| 189 | "Ljava/lang/Object;->toString()Ljava/lang/String;", |
| 190 | "Ljava/lang/ProcessBuilder$Redirect$1;-><init>()V", |
| 191 | ]) |
| 192 | |
| 193 | def test_recursive_wildcard(self): |
| 194 | self.check_patterns("java/**", [ |
| 195 | "Ljava/lang/Character$UnicodeScript;->of(I)Ljava/lang/Character$UnicodeScript;", |
| 196 | "Ljava/lang/Character;->serialVersionUID:J", |
| 197 | "Ljava/lang/Object;->hashCode()I", |
| 198 | "Ljava/lang/Object;->toString()Ljava/lang/String;", |
| 199 | "Ljava/lang/ProcessBuilder$Redirect$1;-><init>()V", |
| 200 | "Ljava/util/zip/ZipFile;-><clinit>()V", |
| 201 | ]) |
| 202 | |
| 203 | # pylint: enable=line-too-long |
| 204 | |
| 205 | |
| 206 | if __name__ == "__main__": |
| 207 | unittest.main(verbosity=2) |