1#!/usr/bin/env python
2#
3# Copyright (C) 2018 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"""Unit tests for Hidden API list generation."""
17import unittest
18from generate_hiddenapi_lists import *
19
20class TestHiddenapiListGeneration(unittest.TestCase):
21
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.
25        flags = FlagsDict()
26        flags.parse_and_merge_csv(['A,' + FLAG_WHITELIST, 'B,' + FLAG_WHITELIST,
27                        'C', 'D', 'E'])
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' ]))
31
32    def test_get_valid_subset_of_unassigned_keys(self):
33        # Create flags where only A is unassigned.
34        flags = FlagsDict()
35        flags.parse_and_merge_csv(['A,' + FLAG_WHITELIST, 'B', 'C'])
36        flags.assign_flag(FLAG_GREYLIST, set(['C']))
37        self.assertEqual(flags.generate_csv(),
38            [ 'A,' + FLAG_WHITELIST, 'B', 'C,' + FLAG_GREYLIST ])
39
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
44        self.assertEqual(
45            flags.get_valid_subset_of_unassigned_apis(set(['A', 'B', 'D'])), set([ 'B' ]))
46
47    def test_parse_and_merge_csv(self):
48        flags = FlagsDict()
49
50        # Test empty CSV entry.
51        self.assertEqual(flags.generate_csv(), [])
52
53        # Test new additions.
54        flags.parse_and_merge_csv([
55            'A,' + FLAG_GREYLIST,
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        ])
68
69        # Test unknown flag.
70        with self.assertRaises(AssertionError):
71            flags.parse_and_merge_csv([ 'Z,foo' ])
72
73    def test_assign_flag(self):
74        flags = FlagsDict()
75        flags.parse_and_merge_csv(['A,' + FLAG_WHITELIST, 'B'])
76
77        # Test new additions.
78        flags.assign_flag(FLAG_GREYLIST, set([ 'A', 'B' ]))
79        self.assertEqual(flags.generate_csv(),
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' ]))
89
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
103if __name__ == '__main__':
104    unittest.main()
105