• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2
3import os
4import posixpath
5import sys
6import unittest
7
8sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10from compat import StringIO
11from vndk_definition_tool import ELF, VNDKLibDir
12
13
14SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
15
16
17class VNDKLibDirTest(unittest.TestCase):
18    def test_create_vndk_dir_suffix(self):
19        self.assertEqual('', VNDKLibDir.create_vndk_dir_suffix('current'))
20        self.assertEqual('-28', VNDKLibDir.create_vndk_dir_suffix('28'))
21
22
23    def test_create_vndk_sp_dir_name(self):
24        self.assertEqual(
25                'vndk-sp', VNDKLibDir.create_vndk_sp_dir_name('current'))
26        self.assertEqual(
27                'vndk-sp-28', VNDKLibDir.create_vndk_sp_dir_name('28'))
28
29
30    def test_create_vndk_dir_name(self):
31        self.assertEqual(
32                'vndk', VNDKLibDir.create_vndk_dir_name('current'))
33        self.assertEqual(
34                'vndk-28', VNDKLibDir.create_vndk_dir_name('28'))
35
36
37    def test_extract_vndk_version_from_name(self):
38        self.assertEqual(
39                'current', VNDKLibDir.extract_version_from_name('vndk'))
40        self.assertEqual(
41                'current', VNDKLibDir.extract_version_from_name('vndk-sp'))
42        self.assertEqual(
43                '28', VNDKLibDir.extract_version_from_name('vndk-28'))
44        self.assertEqual(
45                '28', VNDKLibDir.extract_version_from_name('vndk-sp-28'))
46        self.assertEqual(
47                'p', VNDKLibDir.extract_version_from_name('vndk-p'))
48        self.assertEqual(
49                'p', VNDKLibDir.extract_version_from_name('vndk-sp-p'))
50
51
52    def test_extract_vndk_version_from_path(self):
53        ans = VNDKLibDir.extract_version_from_path(
54                '/system/lib64/vndk/libexample.so')
55        self.assertEqual('current', ans)
56
57        ans = VNDKLibDir.extract_version_from_path(
58                '/system/lib64/vndk-sp/libexample.so')
59        self.assertEqual('current', ans)
60
61        ans = VNDKLibDir.extract_version_from_path(
62                '/system/lib64/vndk-28/libexample.so')
63        self.assertEqual('28', ans)
64
65        ans = VNDKLibDir.extract_version_from_path(
66                '/system/lib64/vndk-sp-28/libexample.so')
67        self.assertEqual('28', ans)
68
69        ans = VNDKLibDir.extract_version_from_path(
70                '/system/lib64/vndk-p/libexample.so')
71        self.assertEqual('p', ans)
72
73        ans = VNDKLibDir.extract_version_from_path(
74                '/system/lib64/vndk-sp-p/libexample.so')
75        self.assertEqual('p', ans)
76
77        ans = VNDKLibDir.extract_version_from_path(
78                '/system/lib64/vndk-sp-p/hw/libexample.so')
79        self.assertEqual('p', ans)
80
81        ans = VNDKLibDir.extract_version_from_path(
82                '/system/lib64/libexample.so')
83        self.assertEqual(None, ans)
84
85
86    def test_is_in_vndk_sp_dir(self):
87        self.assertFalse(VNDKLibDir.is_in_vndk_sp_dir('/system/lib/liba.so'))
88        self.assertFalse(
89                VNDKLibDir.is_in_vndk_sp_dir('/system/lib/vndk/liba.so'))
90        self.assertFalse(
91                VNDKLibDir.is_in_vndk_sp_dir('/system/lib/vndk-28/liba.so'))
92        self.assertFalse(
93                VNDKLibDir.is_in_vndk_sp_dir('/system/lib/vndk-spec/liba.so'))
94        self.assertTrue(
95                VNDKLibDir.is_in_vndk_sp_dir('/system/lib/vndk-sp/liba.so'))
96        self.assertTrue(
97                VNDKLibDir.is_in_vndk_sp_dir('/system/lib/vndk-sp-28/liba.so'))
98
99
100    def test_is_in_vndk_dir(self):
101        self.assertFalse(VNDKLibDir.is_in_vndk_dir('/system/lib/liba.so'))
102        self.assertTrue(VNDKLibDir.is_in_vndk_dir('/system/lib/vndk/liba.so'))
103        self.assertTrue(
104                VNDKLibDir.is_in_vndk_dir('/system/lib/vndk-28/liba.so'))
105        self.assertTrue(
106                VNDKLibDir.is_in_vndk_dir('/system/lib/vndk-spec/liba.so'))
107        self.assertFalse(
108                VNDKLibDir.is_in_vndk_dir('/system/lib/vndk-sp/liba.so'))
109        self.assertFalse(
110                VNDKLibDir.is_in_vndk_dir('/system/lib/vndk-sp-28/liba.so'))
111
112
113    def test_create_vndk_search_paths(self):
114        for version in ('current', '28'):
115            for lib_dir in ('lib', 'lib64'):
116                vndk_sp_name = VNDKLibDir.create_vndk_sp_dir_name(version)
117                vndk_name = VNDKLibDir.create_vndk_dir_name(version)
118
119                expected_vndk_sp = [
120                    posixpath.join('/vendor', lib_dir, vndk_sp_name),
121                    posixpath.join('/system', lib_dir, vndk_sp_name),
122                ]
123                expected_vndk = [
124                    posixpath.join('/vendor', lib_dir, vndk_name),
125                    posixpath.join('/system', lib_dir, vndk_name),
126                ]
127
128                vndk_sp_dirs, vndk_dirs = \
129                        VNDKLibDir.create_vndk_search_paths(lib_dir, version)
130                self.assertEqual(expected_vndk_sp, vndk_sp_dirs)
131                self.assertEqual(expected_vndk, vndk_dirs)
132
133
134    def test_add_version_current(self):
135        vndk_lib_dirs = VNDKLibDir()
136        vndk_lib_dirs.append('current')
137        self.assertIn('current', vndk_lib_dirs)
138
139
140    def test_create_from_dirs_unversioned(self):
141        input_dir = os.path.join(
142                SCRIPT_DIR, 'testdata', 'test_vndk_lib_dir',
143                'vndk_unversioned')
144
145        vndk_lib_dirs = VNDKLibDir.create_from_dirs(
146                [os.path.join(input_dir, 'system')],
147                [os.path.join(input_dir, 'vendor')])
148
149        self.assertIn('current', vndk_lib_dirs)
150
151
152    def test_create_from_dirs_versioned(self):
153        input_dir = os.path.join(
154                SCRIPT_DIR, 'testdata', 'test_vndk_lib_dir', 'vndk_versioned')
155
156        vndk_lib_dirs = VNDKLibDir.create_from_dirs(
157                [os.path.join(input_dir, 'system')],
158                [os.path.join(input_dir, 'vendor')])
159
160        self.assertIn('28', vndk_lib_dirs)
161
162
163    def test_create_from_dirs_versioned_multiple(self):
164        input_dir = os.path.join(
165                SCRIPT_DIR, 'testdata', 'test_vndk_lib_dir',
166                'vndk_versioned_multiple')
167
168        vndk_lib_dirs = VNDKLibDir.create_from_dirs(
169                [os.path.join(input_dir, 'system')],
170                [os.path.join(input_dir, 'vendor')])
171
172        self.assertIn('28', vndk_lib_dirs)
173        self.assertIn('29', vndk_lib_dirs)
174
175
176    def test_create_from_dirs_versioned_32bit_only(self):
177        input_dir = os.path.join(
178                SCRIPT_DIR, 'testdata', 'test_vndk_lib_dir', 'vndk_32')
179
180        vndk_lib_dirs = VNDKLibDir.create_from_dirs(
181                [os.path.join(input_dir, 'system')],
182                [os.path.join(input_dir, 'vendor')])
183
184        self.assertIn('28', vndk_lib_dirs)
185
186
187    def test_get_property(self):
188        property_file = StringIO('ro.vndk.version=example\n')
189        ans = VNDKLibDir._get_property(property_file, 'ro.vndk.version')
190        self.assertEqual('example', ans)
191
192        property_file = StringIO('# comments\n')
193        ans = VNDKLibDir._get_property(property_file, 'ro.vndk.version')
194        self.assertEqual(None, ans)
195
196
197    def test_get_ro_vndk_version(self):
198        input_dir = os.path.join(
199                SCRIPT_DIR, 'testdata', 'test_vndk_lib_dir',
200                'vndk_versioned_multiple')
201
202        vendor_dirs = [os.path.join(input_dir, 'vendor')]
203
204        self.assertEqual('29', VNDKLibDir.get_ro_vndk_version(vendor_dirs))
205
206
207    def test_sorted_versions(self):
208        self.assertEqual(
209                ['20', '10', '2', '1'],
210                VNDKLibDir.sorted_version(['1', '2', '10', '20']))
211
212        self.assertEqual(
213                ['b', 'a', '20', '10', '2', '1'],
214                VNDKLibDir.sorted_version(['1', '2', '10', '20', 'a', 'b']))
215
216        self.assertEqual(
217                ['a', '10b', '10', '2', '1'],
218                VNDKLibDir.sorted_version(['1', '2', '10', 'a', '10b']))
219
220        self.assertEqual(
221                ['current', 'd', 'a', '10', '1'],
222                VNDKLibDir.sorted_version(['1', '10', 'a', 'd', 'current']))
223
224
225
226if __name__ == '__main__':
227    unittest.main()
228