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 | |
Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame^] | 29 | extractInput = ''' |
| 30 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 31 | Ljava/lang/Object;->toString()Ljava/lang/String;,blocked |
| 32 | ''' |
| 33 | |
| 34 | def test_extract_subset(self): |
| 35 | monolithic = self.read_signature_csv_from_string_as_dict(TestDetectOverlaps.extractInput) |
| 36 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 37 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 38 | ''') |
| 39 | subset = extract_subset_from_monolithic_flags_as_dict(monolithic, modular.keys()) |
| 40 | expected = { |
| 41 | 'Ljava/lang/Object;->hashCode()I': { |
| 42 | None: ['public-api', 'system-api', 'test-api'], |
| 43 | 'signature': 'Ljava/lang/Object;->hashCode()I', |
| 44 | }, |
| 45 | } |
| 46 | self.assertEqual(expected, subset) |
| 47 | |
Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 48 | def test_match(self): |
| 49 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 50 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 51 | ''') |
| 52 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 53 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 54 | ''') |
| 55 | mismatches = compare_signature_flags(monolithic, modular) |
| 56 | expected = [] |
| 57 | self.assertEqual(expected, mismatches) |
| 58 | |
| 59 | def test_mismatch_overlapping_flags(self): |
| 60 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 61 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api |
| 62 | ''') |
| 63 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 64 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api,system-api,test-api |
| 65 | ''') |
| 66 | mismatches = compare_signature_flags(monolithic, modular) |
| 67 | expected = [ |
| 68 | ( |
| 69 | 'Ljava/lang/Object;->toString()Ljava/lang/String;', |
| 70 | ['public-api', 'system-api', 'test-api'], |
| 71 | ['public-api'], |
| 72 | ), |
| 73 | ] |
| 74 | self.assertEqual(expected, mismatches) |
| 75 | |
| 76 | |
| 77 | def test_mismatch_monolithic_blocked(self): |
| 78 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 79 | Ljava/lang/Object;->toString()Ljava/lang/String;,blocked |
| 80 | ''') |
| 81 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 82 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api,system-api,test-api |
| 83 | ''') |
| 84 | mismatches = compare_signature_flags(monolithic, modular) |
| 85 | expected = [ |
| 86 | ( |
| 87 | 'Ljava/lang/Object;->toString()Ljava/lang/String;', |
| 88 | ['public-api', 'system-api', 'test-api'], |
| 89 | ['blocked'], |
| 90 | ), |
| 91 | ] |
| 92 | self.assertEqual(expected, mismatches) |
| 93 | |
| 94 | def test_mismatch_modular_blocked(self): |
| 95 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 96 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api,system-api,test-api |
| 97 | ''') |
| 98 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 99 | Ljava/lang/Object;->toString()Ljava/lang/String;,blocked |
| 100 | ''') |
| 101 | mismatches = compare_signature_flags(monolithic, modular) |
| 102 | expected = [ |
| 103 | ( |
| 104 | 'Ljava/lang/Object;->toString()Ljava/lang/String;', |
| 105 | ['blocked'], |
| 106 | ['public-api', 'system-api', 'test-api'], |
| 107 | ), |
| 108 | ] |
| 109 | self.assertEqual(expected, mismatches) |
| 110 | |
| 111 | def test_missing_from_monolithic(self): |
Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame^] | 112 | monolithic = self.read_signature_csv_from_string_as_dict('') |
Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 113 | modular = self.read_signature_csv_from_string_as_dict(''' |
| 114 | Ljava/lang/Object;->toString()Ljava/lang/String;,public-api,system-api,test-api |
| 115 | ''') |
| 116 | mismatches = compare_signature_flags(monolithic, modular) |
| 117 | expected = [ |
| 118 | ( |
| 119 | 'Ljava/lang/Object;->toString()Ljava/lang/String;', |
| 120 | ['public-api', 'system-api', 'test-api'], |
| 121 | [], |
| 122 | ), |
| 123 | ] |
| 124 | self.assertEqual(expected, mismatches) |
| 125 | |
| 126 | def test_missing_from_modular(self): |
Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 127 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 128 | Ljava/lang/Object;->hashCode()I,public-api,system-api,test-api |
| 129 | ''') |
| 130 | modular = {} |
| 131 | mismatches = compare_signature_flags(monolithic, modular) |
Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame^] | 132 | expected = [ |
| 133 | ( |
| 134 | 'Ljava/lang/Object;->hashCode()I', |
| 135 | [], |
| 136 | ['public-api', 'system-api', 'test-api'], |
| 137 | ), |
| 138 | ] |
Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 139 | self.assertEqual(expected, mismatches) |
| 140 | |
| 141 | def test_blocked_missing_from_modular(self): |
Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 142 | monolithic = self.read_signature_csv_from_string_as_dict(''' |
| 143 | Ljava/lang/Object;->hashCode()I,blocked |
| 144 | ''') |
| 145 | modular = {} |
| 146 | mismatches = compare_signature_flags(monolithic, modular) |
Paul Duffin | 53a7607 | 2021-07-21 17:27:09 +0100 | [diff] [blame^] | 147 | expected = [ |
| 148 | ( |
| 149 | 'Ljava/lang/Object;->hashCode()I', |
| 150 | [], |
| 151 | ['blocked'], |
| 152 | ), |
| 153 | ] |
Paul Duffin | 428c651 | 2021-07-21 15:33:22 +0100 | [diff] [blame] | 154 | self.assertEqual(expected, mismatches) |
| 155 | |
| 156 | if __name__ == '__main__': |
| 157 | unittest.main(verbosity=2) |