Home | History | Annotate | Download | only in hiddenapi
      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."""
     17 import unittest
     18 from generate_hiddenapi_lists import *
     19 
     20 class 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         self.assertEqual(flags.generate_csv(),
     58             [ 'A,' + FLAG_GREYLIST,
     59               'B,' + FLAG_BLACKLIST + "," + FLAG_GREYLIST_MAX_O ])
     60 
     61         # Test unknown flag.
     62         with self.assertRaises(AssertionError):
     63             flags.parse_and_merge_csv([ 'C,foo' ])
     64 
     65     def test_assign_flag(self):
     66         flags = FlagsDict()
     67         flags.parse_and_merge_csv(['A,' + FLAG_WHITELIST, 'B'])
     68 
     69         # Test new additions.
     70         flags.assign_flag(FLAG_GREYLIST, set([ 'A', 'B' ]))
     71         self.assertEqual(flags.generate_csv(),
     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' ]))
     81 
     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 
     95 if __name__ == '__main__':
     96     unittest.main()
     97