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 unittest 10 11 from vndk_definition_tool import ELFLinkData, PT_SYSTEM, PT_VENDOR 12 13 14 class ELFLinkDataTest(unittest.TestCase): 15 def setUp(self): 16 self.x = ELFLinkData(PT_SYSTEM, '/system/lib/libx.so', None, 0) 17 self.y = ELFLinkData(PT_SYSTEM, '/system/lib/liby.so', None, 0) 18 self.z = ELFLinkData(PT_SYSTEM, '/system/lib/libz.so', None, 0) 19 self.w = ELFLinkData(PT_SYSTEM, '/system/lib/libw.so', None, 0) 20 self.v = ELFLinkData(PT_VENDOR, '/vendor/lib/libv.so', None, 0) 21 22 self.x.add_dep(self.y, ELFLinkData.NEEDED) 23 self.x.add_dep(self.z, ELFLinkData.DLOPEN) 24 25 self.z.add_dep(self.w, ELFLinkData.NEEDED) 26 self.z.add_dep(self.w, ELFLinkData.DLOPEN) 27 28 def test_add_dep_and_accessors(self): 29 self.assertIn(self.y, self.x.dt_deps) 30 self.assertIn(self.x, self.y.dt_users) 31 self.assertNotIn(self.y, self.x.dl_deps) 32 self.assertNotIn(self.x, self.y.dl_users) 33 34 self.assertIn(self.z, self.x.dl_deps) 35 self.assertIn(self.x, self.z.dl_users) 36 self.assertNotIn(self.z, self.x.dt_deps) 37 self.assertNotIn(self.x, self.z.dt_users) 38 39 def test_remove_dep(self): 40 self.assertIn(self.y, self.x.dt_deps) 41 self.assertIn(self.x, self.y.dt_users) 42 43 with self.assertRaises(KeyError): 44 self.x.remove_dep(self.y, ELFLinkData.DLOPEN) 45 self.assertIn(self.y, self.x.dt_deps) 46 self.assertIn(self.x, self.y.dt_users) 47 48 self.x.remove_dep(self.y, ELFLinkData.NEEDED) 49 self.assertNotIn(self.y, self.x.dt_deps) 50 self.assertNotIn(self.x, self.y.dt_users) 51 52 def test_num_deps(self): 53 self.assertEqual(2, self.x.num_deps) 54 self.assertEqual(0, self.y.num_deps) 55 self.assertEqual(0, self.w.num_deps) 56 self.assertEqual(0, self.v.num_deps) 57 58 # NEEDED and DLOPEN are counted twice. 59 self.assertEqual(2, self.z.num_deps) 60 61 def test_num_users(self): 62 self.assertEqual(0, self.x.num_users) 63 self.assertEqual(1, self.y.num_users) 64 self.assertEqual(1, self.z.num_users) 65 self.assertEqual(0, self.v.num_users) 66 67 # NEEDED and DLOPEN are counted twice. 68 self.assertEqual(2, self.w.num_users) 69 70 def test_has_dep(self): 71 self.assertTrue(self.x.has_dep(self.y)) 72 self.assertTrue(self.x.has_dep(self.z)) 73 self.assertFalse(self.x.has_dep(self.x)) 74 self.assertFalse(self.x.has_dep(self.w)) 75 76 def test_has_user(self): 77 self.assertTrue(self.y.has_user(self.x)) 78 self.assertTrue(self.z.has_user(self.x)) 79 self.assertFalse(self.x.has_user(self.x)) 80 self.assertFalse(self.w.has_user(self.x)) 81 82 def test_is_system_lib(self): 83 self.assertTrue(self.x.is_system_lib()) 84 self.assertFalse(self.v.is_system_lib()) 85 86 def test_get_dep_linked_symbols(self): 87 self.x.linked_symbols['c'] = self.y 88 self.x.linked_symbols['b'] = self.y 89 self.x.linked_symbols['a'] = self.y 90 91 self.x.linked_symbols['w'] = self.z 92 self.x.linked_symbols['z'] = self.z 93 self.x.linked_symbols['y'] = self.z 94 self.x.linked_symbols['x'] = self.z 95 96 self.assertEqual(['a', 'b', 'c'], 97 self.x.get_dep_linked_symbols(self.y)) 98 99 self.assertEqual(['w', 'x', 'y', 'z'], 100 self.x.get_dep_linked_symbols(self.z)) 101 102 103 if __name__ == '__main__': 104 unittest.main() 105