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 | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 21 | def test_init(self): |
| 22 | # Check empty lists |
| 23 | flags = FlagsDict([], []) |
| 24 | self.assertEquals(flags.generate_csv(), []) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 25 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 26 | # Check valid input - two public and two private API signatures. |
| 27 | flags = FlagsDict(['A', 'B'], ['C', 'D']) |
| 28 | self.assertEquals(flags.generate_csv(), |
| 29 | [ 'A,' + FLAG_WHITELIST, 'B,' + FLAG_WHITELIST, 'C', 'D' ]) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 30 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 31 | # Check invalid input - overlapping public/private API signatures. |
| 32 | with self.assertRaises(AssertionError): |
| 33 | flags = FlagsDict(['A', 'B'], ['B', 'C', 'D']) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 34 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 35 | def test_filter_apis(self): |
| 36 | # Initialize flags so that A and B are put on the whitelist and |
| 37 | # C, D, E are left unassigned. Try filtering for the unassigned ones. |
| 38 | flags = FlagsDict(['A', 'B'], ['C', 'D', 'E']) |
| 39 | filter_set = flags.filter_apis(lambda api, flags: not flags) |
| 40 | self.assertTrue(isinstance(filter_set, set)) |
| 41 | self.assertEqual(filter_set, set([ 'C', 'D', 'E' ])) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 42 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 43 | def test_get_valid_subset_of_unassigned_keys(self): |
| 44 | # Create flags where only A is unassigned. |
| 45 | flags = FlagsDict(['A'], ['B', 'C']) |
| 46 | flags.assign_flag(FLAG_GREYLIST, set(['C'])) |
| 47 | self.assertEquals(flags.generate_csv(), |
| 48 | [ 'A,' + FLAG_WHITELIST, 'B', 'C,' + FLAG_GREYLIST ]) |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 49 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 50 | # Check three things: |
| 51 | # (1) B is selected as valid unassigned |
| 52 | # (2) A is not selected because it is assigned 'whitelist' |
| 53 | # (3) D is not selected because it is not a valid key |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 54 | self.assertEqual( |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 55 | 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] | 56 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 57 | def test_parse_and_merge_csv(self): |
| 58 | flags = FlagsDict(['A'], ['B']) |
| 59 | self.assertEquals(flags.generate_csv(), [ 'A,' + FLAG_WHITELIST, 'B' ]) |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 60 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 61 | # Test empty CSV entry. |
| 62 | flags.parse_and_merge_csv(['B']) |
| 63 | self.assertEquals(flags.generate_csv(), [ 'A,' + FLAG_WHITELIST, 'B' ]) |
David Brazdil | 4a55eeb | 2018-09-11 11:09:01 +0100 | [diff] [blame] | 64 | |
David Brazdil | 89bf0f2 | 2018-10-30 18:21:24 +0000 | [diff] [blame] | 65 | # Test assigning an already assigned flag. |
| 66 | flags.parse_and_merge_csv(['A,' + FLAG_WHITELIST]) |
| 67 | self.assertEquals(flags.generate_csv(), [ 'A,' + FLAG_WHITELIST, 'B' ]) |
| 68 | |
| 69 | # Test new additions. |
| 70 | flags.parse_and_merge_csv([ |
| 71 | 'A,' + FLAG_GREYLIST, |
| 72 | 'B,' + FLAG_BLACKLIST + ',' + FLAG_GREYLIST_MAX_O ]) |
| 73 | self.assertEqual(flags.generate_csv(), |
| 74 | [ 'A,' + FLAG_GREYLIST + "," + FLAG_WHITELIST, |
| 75 | 'B,' + FLAG_BLACKLIST + "," + FLAG_GREYLIST_MAX_O ]) |
| 76 | |
| 77 | # Test unknown API signature. |
| 78 | with self.assertRaises(AssertionError): |
| 79 | flags.parse_and_merge_csv([ 'C' ]) |
| 80 | |
| 81 | # Test unknown flag. |
| 82 | with self.assertRaises(AssertionError): |
| 83 | flags.parse_and_merge_csv([ 'A,foo' ]) |
| 84 | |
| 85 | def test_assign_flag(self): |
| 86 | flags = FlagsDict(['A'], ['B']) |
| 87 | self.assertEquals(flags.generate_csv(), [ 'A,' + FLAG_WHITELIST, 'B' ]) |
| 88 | |
| 89 | # Test assigning an already assigned flag. |
| 90 | flags.assign_flag(FLAG_WHITELIST, set([ 'A' ])) |
| 91 | self.assertEquals(flags.generate_csv(), [ 'A,' + FLAG_WHITELIST, 'B' ]) |
| 92 | |
| 93 | # Test new additions. |
| 94 | flags.assign_flag(FLAG_GREYLIST, set([ 'A', 'B' ])) |
| 95 | self.assertEquals(flags.generate_csv(), |
| 96 | [ 'A,' + FLAG_GREYLIST + "," + FLAG_WHITELIST, 'B,' + FLAG_GREYLIST ]) |
| 97 | |
| 98 | # Test invalid API signature. |
| 99 | with self.assertRaises(AssertionError): |
| 100 | flags.assign_flag(FLAG_WHITELIST, set([ 'C' ])) |
| 101 | |
| 102 | # Test invalid flag. |
| 103 | with self.assertRaises(AssertionError): |
| 104 | flags.assign_flag('foo', set([ 'A' ])) |
Paul Duffin | e9622a3 | 2018-09-20 11:03:40 +0100 | [diff] [blame] | 105 | |
David Brazdil | 8503b90 | 2018-08-30 13:35:03 +0100 | [diff] [blame] | 106 | if __name__ == '__main__': |
| 107 | unittest.main() |