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