David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 5 | # Licensed under the Apache License, Version 2.0 (the 'License'); |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 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 |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 12 | # distributed under the License is distributed on an 'AS IS' BASIS, |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 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 Hidden API list generation.""" |
| 17 | import unittest |
| 18 | from generate_hiddenapi_lists import * |
| 19 | |
| 20 | class TestHiddenapiListGeneration(unittest.TestCase): |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 21 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 22 | def test_filter_apis(self): |
| 23 | # Initialize flags so that A and B are put on the whitelist and |
| 24 | # C, D, E are left unassigned. Try filtering for the unassigned ones. |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 25 | flags = FlagsDict() |
| 26 | flags.parse_and_merge_csv(['A,' + FLAG_WHITELIST, 'B,' + FLAG_WHITELIST, |
| 27 | 'C', 'D', 'E']) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 28 | filter_set = flags.filter_apis(lambda api, flags: not flags) |
| 29 | self.assertTrue(isinstance(filter_set, set)) |
| 30 | self.assertEqual(filter_set, set([ 'C', 'D', 'E' ])) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 31 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 32 | def test_get_valid_subset_of_unassigned_keys(self): |
| 33 | # Create flags where only A is unassigned. |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 34 | flags = FlagsDict() |
| 35 | flags.parse_and_merge_csv(['A,' + FLAG_WHITELIST, 'B', 'C']) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 36 | flags.assign_flag(FLAG_GREYLIST, set(['C'])) |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 37 | self.assertEqual(flags.generate_csv(), |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 38 | [ 'A,' + FLAG_WHITELIST, 'B', 'C,' + FLAG_GREYLIST ]) |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 39 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 40 | # Check three things: |
| 41 | # (1) B is selected as valid unassigned |
| 42 | # (2) A is not selected because it is assigned 'whitelist' |
| 43 | # (3) D is not selected because it is not a valid key |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 44 | self.assertEqual( |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 45 | flags.get_valid_subset_of_unassigned_apis(set(['A', 'B', 'D'])), set([ 'B' ])) |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 46 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 47 | def test_parse_and_merge_csv(self): |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 48 | flags = FlagsDict() |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 49 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 50 | # Test empty CSV entry. |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 51 | self.assertEqual(flags.generate_csv(), []) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 52 | |
| 53 | # Test new additions. |
| 54 | flags.parse_and_merge_csv([ |
| 55 | 'A,' + FLAG_GREYLIST, |
| 56 | 'B,' + FLAG_BLACKLIST + ',' + FLAG_GREYLIST_MAX_O ]) |
| 57 | self.assertEqual(flags.generate_csv(), |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 58 | [ 'A,' + FLAG_GREYLIST, |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 59 | 'B,' + FLAG_BLACKLIST + "," + FLAG_GREYLIST_MAX_O ]) |
| 60 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 61 | # Test unknown flag. |
| 62 | with self.assertRaises(AssertionError): |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 63 | flags.parse_and_merge_csv([ 'C,foo' ]) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 64 | |
| 65 | def test_assign_flag(self): |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 66 | flags = FlagsDict() |
| 67 | flags.parse_and_merge_csv(['A,' + FLAG_WHITELIST, 'B']) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 68 | |
| 69 | # Test new additions. |
| 70 | flags.assign_flag(FLAG_GREYLIST, set([ 'A', 'B' ])) |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 71 | self.assertEqual(flags.generate_csv(), |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 72 | [ 'A,' + FLAG_GREYLIST + "," + FLAG_WHITELIST, 'B,' + FLAG_GREYLIST ]) |
| 73 | |
| 74 | # Test invalid API signature. |
| 75 | with self.assertRaises(AssertionError): |
| 76 | flags.assign_flag(FLAG_WHITELIST, set([ 'C' ])) |
| 77 | |
| 78 | # Test invalid flag. |
| 79 | with self.assertRaises(AssertionError): |
| 80 | flags.assign_flag('foo', set([ 'A' ])) |
Paul Duffin | e9622a3 | 2018-09-20 11:03:40 +0100 | [diff] [blame] | 81 | |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 82 | def test_extract_package(self): |
| 83 | signature = 'Lcom/foo/bar/Baz;->method1()Lcom/bar/Baz;' |
| 84 | expected_package = 'com.foo.bar' |
| 85 | self.assertEqual(extract_package(signature), expected_package) |
| 86 | |
| 87 | signature = 'Lcom/foo1/bar/MyClass;->method2()V' |
| 88 | expected_package = 'com.foo1.bar' |
| 89 | self.assertEqual(extract_package(signature), expected_package) |
| 90 | |
| 91 | signature = 'Lcom/foo_bar/baz/MyClass;->method3()V' |
| 92 | expected_package = 'com.foo_bar.baz' |
| 93 | self.assertEqual(extract_package(signature), expected_package) |
| 94 | |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 95 | if __name__ == '__main__': |
| 96 | unittest.main() |