summaryrefslogtreecommitdiff
path: root/files/vts_vndk_files_test.py
blob: faa3e6ada2cc509b5a2ac6cdea5f9a2ece9c54b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
#
# Copyright (C) 2020 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import logging
import os
import posixpath as target_path_module
import sys
import unittest

from vts.testcases.vndk import utils
from vts.testcases.vndk.golden import vndk_data
from vts.utils.python.vndk import vndk_utils


class VtsVndkFilesTest(unittest.TestCase):
    """A test for VNDK files and directories.

    Attributes:
        _dut: The AndroidDevice under test.
        _vndk_version: The VNDK version of the device.
    """
    # Some LL-NDK libraries may load the implementations with the same names
    # from /vendor/lib. Since a vendor may install an implementation of an
    # LL-NDK library with the same name, testNoLlndkInVendor doesn't raise
    # errors on these LL-NDK libraries.
    _LL_NDK_COLLIDING_NAMES = ("libEGL.so", "libGLESv1_CM.so", "libGLESv2.so",
                               "libGLESv3.so")
    _TARGET_ODM_LIB = "/odm/{LIB}"
    _TARGET_VENDOR_LIB = "/vendor/{LIB}"

    def setUp(self):
        """Initializes attributes."""
        serial_number = os.environ.get("ANDROID_SERIAL")
        self.assertTrue(serial_number, "$ANDROID_SERIAL is empty.")
        self._dut = utils.AndroidDevice(serial_number)
        self.assertTrue(self._dut.IsRoot(), "This test requires adb root.")
        self._vndk_version = self._dut.GetVndkVersion()

    def _ListFiles(self, dir_path):
        """Lists all files in a directory except subdirectories.

        Args:
            dir_path: A string, path to the directory on device.

        Returns:
            A list of strings, the file paths in the directory.
        """
        if not self._dut.Exists(dir_path):
            logging.info("%s not found", dir_path)
            return []
        return self._dut.FindFiles(dir_path, "*", "!", "-type", "d")

    def _Fail(self, unexpected_paths, message):
        """Logs error and fails current test.

        Args:
            unexpected_paths: A list of strings, the paths to be shown in the
                              log message.
            message: A string, the error message.
        """
        logging.error("Unexpected files:\n%s", "\n".join(unexpected_paths))
        assert_lines = unexpected_paths[:20]
        if len(unexpected_paths) > 20:
            assert_lines.extend([
                "...",
                "Total number of errors: %d" % len(unexpected_paths)])
        assert_lines.append(message)
        self.fail("\n".join(assert_lines))

    def _TestVndkDirectory(self, vndk_dir, vndk_list_names):
        """Verifies that the VNDK directory doesn't contain extra files.

        Args:
            vndk_dir: The path to the VNDK directory on device.
            vndk_list_names: Strings, the categories of the VNDK libraries
                             that can be in the directory.
        """
        vndk_lists = vndk_data.LoadVndkLibraryListsFromResources(
            self._vndk_version, *vndk_list_names)
        self.assertTrue(vndk_lists, "Cannot load VNDK library lists.")
        vndk_set = set().union(*vndk_lists)
        logging.debug("vndk set: %s", vndk_set)
        unexpected = [x for x in self._ListFiles(vndk_dir) if
                      target_path_module.basename(x) not in vndk_set]
        if unexpected:
            self._Fail(unexpected,
                       "The above libraries are not %s." %
                       ", ".join(vndk_list_names))

    def _TestNotInVndkDirecotory(self, vndk_dir, vndk_list_names, except_libs):
        """Verifies that VNDK directory doesn't contain specific files.

        Args:
            vndk_dir, The path to the VNDK directory on device.
            vndk_list_names: A list of strings, the categories of the VNDK
                             libraries that should not be in the directory.
            except_libs: A set of strings, the file names of the libraries that
                         are exceptions to this test.
        """
        vndk_lists = vndk_data.LoadVndkLibraryListsFromResources(
            self._vndk_version, *vndk_list_names)
        self.assertTrue(vndk_lists, "Cannot load VNDK library lists.")
        vndk_set = set().union(*vndk_lists)
        vndk_set.difference_update(except_libs)
        logging.debug("vndk set: %s", vndk_set)
        unexpected = [x for x in self._ListFiles(vndk_dir) if
                      target_path_module.basename(x) in vndk_set]
        if unexpected:
            self._Fail(unexpected,
                       "%s must not contain %s libraries." %
                       (vndk_dir, ", ",join(vndk_list_names)))

    def _TestVndkCoreDirectory(self, bitness):
        """Verifies that VNDK directory doesn't contain extra files."""
        if not vndk_utils.IsVndkRuntimeEnforced(self._dut):
            logging.info("Skip the test as VNDK runtime is not enforced on "
                         "the device.")
            return
        self._TestVndkDirectory(
            vndk_utils.GetVndkDirectory(bitness, self._vndk_version),
            (vndk_data.VNDK, vndk_data.VNDK_PRIVATE, vndk_data.VNDK_SP,
             vndk_data.VNDK_SP_PRIVATE,))

    def testVndkCoreDirectory32(self):
        """Runs _TestVndkCoreDirectory for 32-bit libraries."""
        self._TestVndkCoreDirectory(32)

    def testVndkCoreDirectory64(self):
        """Runs _TestVndkCoreDirectory for 64-bit libraries."""
        if self._dut.GetCpuAbiList(64):
            self._TestVndkCoreDirectory(64)
        else:
            logging.info("Skip the test as the device doesn't support 64-bit "
                         "ABI.")

    def _TestNoLlndkInVendor(self, bitness):
        """Verifies that vendor partition has no LL-NDK libraries."""
        self._TestNotInVndkDirecotory(
            vndk_utils.FormatVndkPath(self._TARGET_VENDOR_LIB, bitness),
            (vndk_data.LL_NDK,),
            self._LL_NDK_COLLIDING_NAMES)

    def testNoLlndkInVendor32(self):
        """Runs _TestNoLlndkInVendor for 32-bit libraries."""
        self._TestNoLlndkInVendor(32)

    def testNoLlndkInVendor64(self):
        """Runs _TestNoLlndkInVendor for 64-bit libraries."""
        if self._dut.GetCpuAbiList(64):
            self._TestNoLlndkInVendor(64)
        else:
            logging.info("Skip the test as the device doesn't support 64-bit "
                         "ABI.")

    def _TestNoLlndkInOdm(self, bitness):
        """Verifies that odm partition has no LL-NDK libraries."""
        self._TestNotInVndkDirecotory(
            vndk_utils.FormatVndkPath(self._TARGET_ODM_LIB, bitness),
            (vndk_data.LL_NDK,),
            self._LL_NDK_COLLIDING_NAMES)

    def testNoLlndkInOdm32(self):
        """Runs _TestNoLlndkInOdm for 32-bit libraries."""
        self._TestNoLlndkInOdm(32)

    def testNoLlndkInOdm64(self):
        """Runs _TestNoLlndkInOdm for 64-bit libraries."""
        if self._dut.GetCpuAbiList(64):
            self._TestNoLlndkInOdm(64)
        else:
            logging.info("Skip the test as the device doesn't support 64-bit "
                         "ABI.")


if __name__ == "__main__":
    # The logs are written to stdout so that TradeFed test runner can parse the
    # results from stderr.
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
    # Setting verbosity is required to generate output that the TradeFed test
    # runner can parse.
    unittest.main(verbosity=3)