Paul Duffin | 428c651 | 2021-07-21 15:33:22 +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. |
| 16 | |
| 17 | """Unit tests for verify_overlaps_test.py.""" |
| 18 | import io |
| 19 | import unittest |
| 20 | |
| 21 | from verify_overlaps import * |
| 22 | |
| 23 | class TestDetectOverlaps(unittest.TestCase): |
| 24 | |
| 25 | def read_signature_csv_from_string_as_dict(self, csv): |
| 26 | with io.StringIO(csv) as f: |
| 27 | return read_signature_csv_from_stream_as_dict(f) |
| 28 | |
| 29 | def test_match(self): |
| 30 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 31 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 32 | Ljava/lang/Object;->toString()Ljava/lang/String;,blocked |
| 33 | ''') |
| 34 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 35 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 36 | ''') |
| 37 | mismatches = compare_signature_flags(monolithic, modular) |
| 38 | expected = [] |
| 39 | self.assertEqual(expected, mismatches) |
| 40 | |
| 41 | def test_mismatch_overlapping_flags(self): |
| 42 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 43 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api |
| 44 | ''') |
| 45 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 46 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api,system-api,test-api |
| 47 | ''') |
| 48 | mismatches = compare_signature_flags(monolithic, modular) |
| 49 | expected = [ |
| 50 | ( |
| 51 | 'Ljava/lang/Object;->toString()Ljava/lang/String;', |
| 52 | ['public-api', 'system-api', 'test-api'], |
| 53 | ['public-api'], |
| 54 | ), |
| 55 | ] |
| 56 | self.assertEqual(expected, mismatches) |
| 57 | |
| 58 | |
| 59 | def test_mismatch_monolithic_blocked(self): |
| 60 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 61 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 62 | Ljava/lang/Object;->toString()Ljava/lang/String;,blocked |
| 63 | ''') |
| 64 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 65 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api,system-api,test-api |
| 66 | ''') |
| 67 | mismatches = compare_signature_flags(monolithic, modular) |
| 68 | expected = [ |
| 69 | ( |
| 70 | 'Ljava/lang/Object;->toString()Ljava/lang/String;', |
| 71 | ['public-api', 'system-api', 'test-api'], |
| 72 | ['blocked'], |
| 73 | ), |
| 74 | ] |
| 75 | self.assertEqual(expected, mismatches) |
| 76 | |
| 77 | def test_mismatch_modular_blocked(self): |
| 78 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 79 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 80 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api,system-api,test-api |
| 81 | ''') |
| 82 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 83 | Ljava/lang/Object;->toString()Ljava/lang/String;,blocked |
| 84 | ''') |
| 85 | mismatches = compare_signature_flags(monolithic, modular) |
| 86 | expected = [ |
| 87 | ( |
| 88 | 'Ljava/lang/Object;->toString()Ljava/lang/String;', |
| 89 | ['blocked'], |
| 90 | ['public-api', 'system-api', 'test-api'], |
| 91 | ), |
| 92 | ] |
| 93 | self.assertEqual(expected, mismatches) |
| 94 | |
| 95 | def test_missing_from_monolithic(self): |
| 96 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 97 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 98 | ''') |
| 99 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 100 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api,system-api,test-api |
| 101 | ''') |
| 102 | mismatches = compare_signature_flags(monolithic, modular) |
| 103 | expected = [ |
| 104 | ( |
| 105 | 'Ljava/lang/Object;->toString()Ljava/lang/String;', |
| 106 | ['public-api', 'system-api', 'test-api'], |
| 107 | [], |
| 108 | ), |
| 109 | ] |
| 110 | self.assertEqual(expected, mismatches) |
| 111 | |
| 112 | def test_missing_from_modular(self): |
| 113 | # The modular dict defines the set of signatures to compare so an entry |
| 114 | # in the monolithic dict that does not have a corresponding entry in the |
| 115 | # modular dict is ignored. |
| 116 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 117 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 118 | ''') |
| 119 | modular = {} |
| 120 | mismatches = compare_signature_flags(monolithic, modular) |
| 121 | expected = [] |
| 122 | self.assertEqual(expected, mismatches) |
| 123 | |
| 124 | def test_blocked_missing_from_modular(self): |
| 125 | # The modular dict defines the set of signatures to compare so an entry |
| 126 | # in the monolithic dict that does not have a corresponding entry in the |
| 127 | # modular dict is ignored. |
| 128 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 129 | Ljava/lang/Object;->hashCode()I,blocked |
| 130 | ''') |
| 131 | modular = {} |
| 132 | mismatches = compare_signature_flags(monolithic, modular) |
| 133 | expected = [] |
| 134 | self.assertEqual(expected, mismatches) |
| 135 | |
| 136 | if __name__ == '__main__': |
| 137 | unittest.main(verbosity=2) |