1#!/usr/bin/env python3
2
3from __future__ import print_function
4
5import unittest
6
7from vndk_definition_tool import ELFLinkData, PT_SYSTEM, PT_VENDOR
8
9
10class ELFLinkDataTest(unittest.TestCase):
11    def setUp(self):
12        self.x = ELFLinkData(PT_SYSTEM, '/system/lib/libx.so', None, 0)
13        self.y = ELFLinkData(PT_SYSTEM, '/system/lib/liby.so', None, 0)
14        self.z = ELFLinkData(PT_SYSTEM, '/system/lib/libz.so', None, 0)
15        self.w = ELFLinkData(PT_SYSTEM, '/system/lib/libw.so', None, 0)
16        self.v = ELFLinkData(PT_VENDOR, '/vendor/lib/libv.so', None, 0)
17
18        self.x.add_needed_dep(self.y)
19        self.x.add_dlopen_dep(self.z)
20
21        self.z.add_needed_dep(self.w)
22        self.z.add_dlopen_dep(self.w)
23
24
25    def test_add_dep_and_accessors(self):
26        self.assertIn(self.y, self.x.deps_needed_all)
27        self.assertIn(self.x, self.y.users_needed_all)
28        self.assertNotIn(self.y, self.x.deps_dlopen_all)
29        self.assertNotIn(self.x, self.y.users_dlopen_all)
30
31        self.assertIn(self.z, self.x.deps_dlopen_all)
32        self.assertIn(self.x, self.z.users_dlopen_all)
33        self.assertNotIn(self.z, self.x.deps_needed_all)
34        self.assertNotIn(self.x, self.z.users_needed_all)
35
36
37    def test_remove_dep(self):
38        self.assertIn(self.y, self.x.deps_needed_all)
39        self.assertIn(self.x, self.y.users_needed_all)
40
41        with self.assertRaises(KeyError):
42            self.x.hide_dlopen_dep(self.y)
43        self.assertIn(self.y, self.x.deps_needed_all)
44        self.assertIn(self.x, self.y.users_needed_all)
45
46        self.x.hide_needed_dep(self.y)
47        self.assertIn(self.y, self.x.deps_needed_hidden)
48        self.assertIn(self.x, self.y.users_needed_hidden)
49        self.assertNotIn(self.y, self.x.deps_needed)
50        self.assertNotIn(self.x, self.y.users_needed)
51
52
53    def test_num_deps(self):
54        self.assertEqual(2, self.x.num_deps)
55        self.assertEqual(0, self.y.num_deps)
56        self.assertEqual(0, self.w.num_deps)
57        self.assertEqual(0, self.v.num_deps)
58
59        # NEEDED and DLOPEN are counted twice.
60        self.assertEqual(2, self.z.num_deps)
61
62
63    def test_num_users(self):
64        self.assertEqual(0, self.x.num_users)
65        self.assertEqual(1, self.y.num_users)
66        self.assertEqual(1, self.z.num_users)
67        self.assertEqual(0, self.v.num_users)
68
69        # NEEDED and DLOPEN are counted twice.
70        self.assertEqual(2, self.w.num_users)
71
72
73    def test_has_dep(self):
74        self.assertTrue(self.x.has_dep(self.y))
75        self.assertTrue(self.x.has_dep(self.z))
76        self.assertFalse(self.x.has_dep(self.x))
77        self.assertFalse(self.x.has_dep(self.w))
78
79
80    def test_has_user(self):
81        self.assertTrue(self.y.has_user(self.x))
82        self.assertTrue(self.z.has_user(self.x))
83        self.assertFalse(self.x.has_user(self.x))
84        self.assertFalse(self.w.has_user(self.x))
85
86
87    def test_is_system_lib(self):
88        self.assertTrue(self.x.is_system_lib())
89        self.assertFalse(self.v.is_system_lib())
90
91
92    def test_get_dep_linked_symbols(self):
93        self.x.linked_symbols['c'] = self.y
94        self.x.linked_symbols['b'] = self.y
95        self.x.linked_symbols['a'] = self.y
96
97        self.x.linked_symbols['w'] = self.z
98        self.x.linked_symbols['z'] = self.z
99        self.x.linked_symbols['y'] = self.z
100        self.x.linked_symbols['x'] = self.z
101
102        self.assertEqual(['a', 'b', 'c'],
103                         self.x.get_dep_linked_symbols(self.y))
104
105        self.assertEqual(['w', 'x', 'y', 'z'],
106                         self.x.get_dep_linked_symbols(self.z))
107