1#!/usr/bin/env python3
2
3from __future__ import print_function
4
5import unittest
6
7from vndk_definition_tool import ELFResolver
8
9
10class ELFResolverTest(unittest.TestCase):
11    def setUp(self):
12        lib_set = {
13            '/system/lib/liba.so': 'a',
14            '/system/lib/libb.so': 'b',
15            '/vendor/lib/liba.so': 'a2',
16            '/vendor/lib/libc.so': 'c',
17            '/vendor/lib/libd.so': 'd',
18            '/system/lib/hw/libe.so': 'e',
19            '/vendor/lib/hw/libf.so': 'f',
20        }
21
22        self.resolver = ELFResolver(lib_set, ['/system/lib', '/vendor/lib'])
23
24
25    def test_get_candidates(self):
26        r = self.resolver
27
28        self.assertEqual(
29            ['/system/lib/libx.so', '/vendor/lib/libx.so'],
30            list(r.get_candidates('/system/lib/libreq.so', 'libx.so')))
31
32        self.assertEqual(
33            ['/C/libx.so', '/system/lib/libx.so', '/vendor/lib/libx.so'],
34            list(r.get_candidates('/system/lib/libreq.so', 'libx.so', ['/C'])))
35
36        self.assertEqual(
37            ['/C/libx.so', '/D/libx.so', '/system/lib/libx.so',
38             '/vendor/lib/libx.so'],
39            list(r.get_candidates('/system/lib/libreq.so', 'libx.so',
40                                  ['/C', '/D'])))
41
42        self.assertEqual(
43            ['/E/libx.so', '/system/lib/libx.so', '/vendor/lib/libx.so'],
44            list(r.get_candidates('/system/lib/libreq.so', 'libx.so', None,
45                                  ['/E'])))
46
47        self.assertEqual(
48            ['/E/libx.so', '/F/libx.so', '/system/lib/libx.so',
49             '/vendor/lib/libx.so'],
50            list(r.get_candidates('/system/lib/libreq.so', 'libx.so', None,
51                                  ['/E', '/F'])))
52
53        self.assertEqual(
54            ['/C/libx.so', '/D/libx.so', '/E/libx.so', '/F/libx.so',
55             '/system/lib/libx.so', '/vendor/lib/libx.so'],
56            list(r.get_candidates('/system/lib/libreq.so', 'libx.so',
57                                  ['/C', '/D'], ['/E', '/F'])))
58
59        # Test app-specific search paths.
60        self.assertEqual(
61            ['/system/app/example/lib/armeabi-v7a/libx.so',
62             '/C/libx.so', '/D/libx.so', '/E/libx.so', '/F/libx.so',
63             '/system/lib/libx.so', '/vendor/lib/libx.so'],
64            list(r.get_candidates(
65                '/system/app/example/lib/armeabi-v7a/libreq.so',
66                'libx.so',
67                ['/C', '/D'], ['/E', '/F'])))
68
69
70    def test_resolve(self):
71        r = self.resolver
72        self.assertEqual('a', r.resolve('/system/lib/libreq.so', 'liba.so'))
73        self.assertEqual('c', r.resolve('/system/lib/libreq.so', 'libc.so'))
74
75        self.assertEqual(None, r.resolve('/system/lib/libreq.so', 'libe.so'))
76        self.assertEqual(
77            'e',
78            r.resolve('/system/lib/libreq.so', 'libe.so',
79                      dt_rpath=['/system/lib/hw']))
80        self.assertEqual(
81            'e',
82            r.resolve('/system/lib/libreq.so', 'libe.so',
83                      dt_runpath=['/system/lib/hw']))
84
85        self.assertEqual(
86            'a2',
87            r.resolve('/system/lib/libreq.so', 'liba.so',
88                      dt_rpath=['/vendor/lib']))
89        self.assertEqual(
90            'a2',
91            r.resolve('/system/lib/libreq.so', 'liba.so',
92                      dt_runpath=['/vendor/lib']))
93