Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python3
      2 
      3 from __future__ import print_function
      4 
      5 import os
      6 import sys
      7 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
      8 
      9 import argparse
     10 import unittest
     11 
     12 from compat import TemporaryDirectory, makedirs
     13 from vndk_definition_tool import GenericRefs
     14 
     15 
     16 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
     17 
     18 
     19 class MockELF(object):
     20     def __init__(self, exported_symbols):
     21         self.exported_symbols = exported_symbols
     22 
     23 
     24 class MockLib(object):
     25     def __init__(self, path, exported_symbols):
     26         self.path = path
     27         self.elf = MockELF(exported_symbols)
     28 
     29 
     30 class GenericRefsTest(unittest.TestCase):
     31     def setUp(self):
     32         self.ref = GenericRefs()
     33         self.ref.add('/system/lib/libc.so',
     34                      MockELF({'fclose', 'fopen', 'fread', 'fwrite'}))
     35         self.ref.add('/system/lib/libm.so',
     36                      MockELF({'cos', 'sin', 'tan'}))
     37         self.ref.add('/system/lib64/libc.so',
     38                      MockELF({'fclose', 'fopen', 'fread', 'fwrite'}))
     39         self.ref.add('/system/lib64/libm.so',
     40                      MockELF({'cos', 'sin', 'tan'}))
     41 
     42     def test_create_from_sym_dir(self):
     43         input_dir = os.path.join(SCRIPT_DIR, 'testdata', 'test_generic_refs')
     44 
     45         g = GenericRefs.create_from_sym_dir(input_dir)
     46         self.assertEqual(4, len(g.refs))
     47 
     48         self.assertIn('/system/lib/libc.so', g.refs)
     49         self.assertIn('/system/lib/libm.so', g.refs)
     50         self.assertIn('/system/lib64/libc.so', g.refs)
     51         self.assertIn('/system/lib64/libm.so', g.refs)
     52 
     53         self.assertEqual({'fclose', 'fopen', 'fread', 'fwrite'},
     54                          g.refs['/system/lib/libc.so'].exported_symbols)
     55         self.assertEqual({'fclose', 'fopen', 'fread', 'fwrite'},
     56                          g.refs['/system/lib64/libc.so'].exported_symbols)
     57 
     58         self.assertEqual({'cos', 'sin', 'tan'},
     59                          g.refs['/system/lib/libm.so'].exported_symbols)
     60         self.assertEqual({'cos', 'sin', 'tan'},
     61                          g.refs['/system/lib64/libm.so'].exported_symbols)
     62 
     63     def test_classify_lib(self):
     64         libc_sub = MockLib('/system/lib/libc.so', {'fclose', 'fopen', 'fread'})
     65         libc_sup = MockLib('/system/lib/libc.so',
     66                            {'fclose', 'fopen', 'fread', 'fwrite', 'open'})
     67         libc_eq = MockLib('/system/lib/libc.so',
     68                           {'fclose', 'fopen', 'fread', 'fwrite'})
     69         libfoo = MockLib('/system/lib/libfoo.so', {})
     70 
     71         self.assertEqual(GenericRefs.MODIFIED, self.ref.classify_lib(libc_sub))
     72         self.assertEqual(GenericRefs.EXPORT_SUPER_SET,
     73                          self.ref.classify_lib(libc_sup))
     74         self.assertEqual(GenericRefs.EXPORT_EQUAL,
     75                          self.ref.classify_lib(libc_eq))
     76         self.assertEqual(GenericRefs.NEW_LIB, self.ref.classify_lib(libfoo))
     77 
     78     def test_is_equivalent_lib(self):
     79         libc_sub = MockLib('/system/lib/libc.so', {'fclose', 'fopen', 'fread'})
     80         libc_sup = MockLib('/system/lib/libc.so',
     81                            {'fclose', 'fopen', 'fread', 'fwrite', 'open'})
     82         libc_eq = MockLib('/system/lib/libc.so',
     83                           {'fclose', 'fopen', 'fread', 'fwrite'})
     84 
     85         self.assertFalse(self.ref.is_equivalent_lib(libc_sub))
     86         self.assertFalse(self.ref.is_equivalent_lib(libc_sup))
     87 
     88         self.assertTrue(self.ref.is_equivalent_lib(libc_eq))
     89 
     90     def test_has_same_name_lib(self):
     91         self.assertTrue(self.ref.has_same_name_lib(
     92             MockLib('/vendor/lib/libc.so', {})))
     93         self.assertFalse(self.ref.has_same_name_lib(
     94             MockLib('/vendor/lib/lib_does_not_exist.so', {})))
     95 
     96 
     97 if __name__ == '__main__':
     98     unittest.main()
     99