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, |
Artur Satayev | 44036a7 | 2019-10-31 12:12:00 +0000 | [diff] [blame] | 56 | 'B,' + FLAG_BLACKLIST + ',' + FLAG_GREYLIST_MAX_O, |
| 57 | 'C,' + FLAG_SYSTEM_API + ',' + FLAG_WHITELIST, |
| 58 | 'D,' + FLAG_GREYLIST+ ',' + FLAG_TEST_API, |
| 59 | 'E,' + FLAG_BLACKLIST+ ',' + FLAG_TEST_API, |
| 60 | ]) |
| 61 | self.assertEqual(flags.generate_csv(), [ |
| 62 | 'A,' + FLAG_GREYLIST, |
| 63 | 'B,' + FLAG_BLACKLIST + "," + FLAG_GREYLIST_MAX_O, |
| 64 | 'C,' + FLAG_SYSTEM_API + ',' + FLAG_WHITELIST, |
| 65 | 'D,' + FLAG_GREYLIST+ ',' + FLAG_TEST_API, |
| 66 | 'E,' + FLAG_BLACKLIST+ ',' + FLAG_TEST_API, |
| 67 | ]) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 68 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 69 | # Test unknown flag. |
| 70 | with self.assertRaises(AssertionError): |
Artur Satayev | 44036a7 | 2019-10-31 12:12:00 +0000 | [diff] [blame] | 71 | flags.parse_and_merge_csv([ 'Z,foo' ]) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 72 | |
| 73 | def test_assign_flag(self): |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 74 | flags = FlagsDict() |
| 75 | flags.parse_and_merge_csv(['A,' + FLAG_WHITELIST, 'B']) |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 76 | |
| 77 | # Test new additions. |
| 78 | flags.assign_flag(FLAG_GREYLIST, set([ 'A', 'B' ])) |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 79 | self.assertEqual(flags.generate_csv(), |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 80 | [ 'A,' + FLAG_GREYLIST + "," + FLAG_WHITELIST, 'B,' + FLAG_GREYLIST ]) |
| 81 | |
| 82 | # Test invalid API signature. |
| 83 | with self.assertRaises(AssertionError): |
| 84 | flags.assign_flag(FLAG_WHITELIST, set([ 'C' ])) |
| 85 | |
| 86 | # Test invalid flag. |
| 87 | with self.assertRaises(AssertionError): |
| 88 | flags.assign_flag('foo', set([ 'A' ])) |
Paul Duffin | e9622a3 | 2018-09-20 11:03:40 +0100 | [diff] [blame] | 89 | |
Andrei Onea | a6e09b4 | 2019-03-29 15:27:55 +0000 | [diff] [blame] | 90 | def test_extract_package(self): |
| 91 | signature = 'Lcom/foo/bar/Baz;->method1()Lcom/bar/Baz;' |
| 92 | expected_package = 'com.foo.bar' |
| 93 | self.assertEqual(extract_package(signature), expected_package) |
| 94 | |
| 95 | signature = 'Lcom/foo1/bar/MyClass;->method2()V' |
| 96 | expected_package = 'com.foo1.bar' |
| 97 | self.assertEqual(extract_package(signature), expected_package) |
| 98 | |
| 99 | signature = 'Lcom/foo_bar/baz/MyClass;->method3()V' |
| 100 | expected_package = 'com.foo_bar.baz' |
| 101 | self.assertEqual(extract_package(signature), expected_package) |
| 102 | |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 103 | if __name__ == '__main__': |
| 104 | unittest.main() |