1#!/usr/bin/env python3
2
3from __future__ import print_function
4
5import unittest
6
7from vndk_definition_tool import (
8    DepsInsightCommand, ModuleInfo, PT_SYSTEM, PT_VENDOR)
9
10from .compat import StringIO, patch
11from .utils import GraphBuilder
12
13
14class DepsInsightCommandTest(unittest.TestCase):
15    _PATH_FIELD = 0
16    _ELF_CLASS_FIELD = 1
17    _TAGS_FIELD = 2
18    _DEPS_FIELD = 3
19    _USERS_FIELD = 4
20    _SOURCE_DIRS_FIELD = 5
21
22
23    @classmethod
24    def _get_module(cls, strs, mods, path):
25        for mod in mods:
26            if strs[mod[cls._PATH_FIELD]] == path:
27                return mod
28        return None
29
30
31    @classmethod
32    def _get_module_deps(cls, strs, mods, path):
33        mod = cls._get_module(strs, mods, path)
34        result = set()
35        for deps in mod[cls._DEPS_FIELD]:
36            result.update(strs[mods[x][cls._PATH_FIELD]] for x in deps)
37        return result
38
39
40    @classmethod
41    def _get_module_users(cls, strs, mods, path):
42        mod = cls._get_module(strs, mods, path)
43        users = mod[cls._USERS_FIELD]
44        return set(strs[mods[x][cls._PATH_FIELD]] for x in users)
45
46
47    def test_serialize_data_with_all_deps(self):
48        # compute_degenerated_vndk() should not remove bad dependencies from
49        # the output of deps-insight.  This test checks the existance of bad
50        # dependencies.
51
52        gb = GraphBuilder()
53
54        libsystem = gb.add_lib32(PT_SYSTEM, 'libsystem')
55
56        libsystem2 = gb.add_lib32(
57            PT_SYSTEM, 'libsystem2', dt_needed=['libsystem.so'])
58
59        libvndk = gb.add_lib32(
60            PT_SYSTEM, 'libvndk', dt_needed=['libvendor_bad.so'],
61            extra_dir='vndk')
62
63        libvendor = gb.add_lib32(
64            PT_VENDOR, 'libvendor', dt_needed=['libvndk.so'])
65
66        libvendor_bad = gb.add_lib32(
67            PT_VENDOR, 'libvendor_bad', extra_dir='vndk')
68
69        gb.resolve()
70
71        with patch('sys.stderr', StringIO()):
72            vndk_sets = gb.graph.compute_degenerated_vndk(set(), None)
73
74        self.assertNotIn(libvendor_bad, libvndk.deps_good)
75
76        strs, mods = DepsInsightCommand.serialize_data(
77            list(gb.graph.all_libs()), vndk_sets, ModuleInfo())
78
79        # libsystem
80        deps = self._get_module_deps(strs, mods, libsystem.path)
81        self.assertFalse(deps)
82        users = self._get_module_users(strs, mods, libsystem.path)
83        self.assertIn(libsystem2.path, users)
84
85        # libsystem2
86        deps = self._get_module_deps(strs, mods, libsystem2.path)
87        self.assertIn(libsystem.path, deps)
88        users = self._get_module_users(strs, mods, libsystem2.path)
89        self.assertFalse(users)
90
91        # libvndk
92        deps = self._get_module_deps(strs, mods, libvndk.path)
93        self.assertIn(libvendor_bad.path, deps)
94        users = self._get_module_users(strs, mods, libvndk.path)
95        self.assertIn(libvendor.path, users)
96
97        # libvendor
98        deps = self._get_module_deps(strs, mods, libvendor.path)
99        self.assertIn(libvndk.path, deps)
100        users = self._get_module_users(strs, mods, libvendor.path)
101        self.assertFalse(users)
102
103        # libvendor_bad
104        deps = self._get_module_deps(strs, mods, libvendor_bad.path)
105        self.assertFalse(deps)
106        users = self._get_module_users(strs, mods, libvendor_bad.path)
107        self.assertIn(libvndk.path, users)
108