1#!/usr/bin/env python
2#
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import logging
19
20from vts.runners.host import asserts
21from vts.runners.host import base_test
22from vts.runners.host import keys
23from vts.runners.host import test_runner
24from vts.testcases.vndk.golden import vndk_data
25from vts.utils.python.file import target_file_utils
26from vts.utils.python.os import path_utils
27from vts.utils.python.vndk import vndk_utils
28
29
30class VtsVndkFilesTest(base_test.BaseTestClass):
31    """A test for VNDK files and directories.
32
33    Attributes:
34        data_file_path: The path to VTS data directory.
35        _dut: The AndroidDevice under test.
36        _shell: The ShellMirrorObject that executes commands.
37        _vndk_version: The VNDK version of the device.
38    """
39    # Some LL-NDK libraries may load the implementations with the same names
40    # from /vendor/lib. Since a vendor may install an implementation of an
41    # LL-NDK library with the same name, testNoLlndkInVendor doesn't raise
42    # errors on these LL-NDK libraries.
43    _LL_NDK_COLLIDING_NAMES = ("libEGL.so", "libGLESv1_CM.so", "libGLESv2.so",
44                               "libGLESv3.so")
45    _TARGET_ODM_LIB = "/odm/{LIB}"
46    _TARGET_VENDOR_LIB = "/vendor/{LIB}"
47
48    def setUpClass(self):
49        """Initializes the data file path and shell."""
50        required_params = [keys.ConfigKeys.IKEY_DATA_FILE_PATH]
51        self.getUserParams(required_params)
52        self._dut = self.android_devices[0]
53        self._shell = self._dut.shell
54        self._vndk_version = self._dut.vndk_version
55
56    def _ListFiles(self, dir_path):
57        """Lists all files in a directory except subdirectories.
58
59        Args:
60            dir_path: A string, path to the directory on device.
61
62        Returns:
63            A list of strings, the file paths in the directory.
64        """
65        if not target_file_utils.Exists(dir_path, self._shell):
66            logging.info("%s not found", dir_path)
67            return []
68        return target_file_utils.FindFiles(self._shell, dir_path, "*",
69                                           "! -type d")
70
71    @staticmethod
72    def _Fail(unexpected_paths):
73        """Logs error and fails current test.
74
75        Args:
76            unexpected_paths: A list of strings, the paths to be shown in the
77                              log message.
78        """
79        logging.error("Unexpected files:\n%s", "\n".join(unexpected_paths))
80        assert_lines = unexpected_paths[:20]
81        if len(unexpected_paths) > 20:
82            assert_lines.append("...")
83        assert_lines.append(
84            "Total number of errors: %d" % len(unexpected_paths))
85        asserts.fail("\n".join(assert_lines))
86
87    def _TestVndkDirectory(self, vndk_dir, vndk_list_names):
88        """Verifies that the VNDK directory doesn't contain extra files.
89
90        Args:
91            vndk_dir: The path to the VNDK directory on device.
92            vndk_list_names: Strings, the categories of the VNDK libraries
93                             that can be in the directory.
94        """
95        vndk_lists = vndk_data.LoadVndkLibraryLists(
96            self.data_file_path, self._vndk_version, *vndk_list_names)
97        asserts.assertTrue(vndk_lists, "Cannot load VNDK library lists.")
98        vndk_set = set().union(*vndk_lists)
99        logging.debug("vndk set: %s", vndk_set)
100        unexpected = [x for x in self._ListFiles(vndk_dir) if
101                      path_utils.TargetBaseName(x) not in vndk_set]
102        if unexpected:
103            self._Fail(unexpected)
104
105    def _TestNotInVndkDirecotory(self, vndk_dir, vndk_list_names, except_libs):
106        """Verifies that VNDK directory doesn't contain specific files.
107
108        Args:
109            vndk_dir, The path to the VNDK directory on device.
110            vndk_list_names: A list of strings, the categories of the VNDK
111                             libraries that should not be in the directory.
112            except_libs: A set of strings, the file names of the libraries that
113                         are exceptions to this test.
114        """
115        vndk_lists = vndk_data.LoadVndkLibraryLists(
116            self.data_file_path, self._vndk_version, *vndk_list_names)
117        asserts.assertTrue(vndk_lists, "Cannot load VNDK library lists.")
118        vndk_set = set().union(*vndk_lists)
119        vndk_set.difference_update(except_libs)
120        logging.debug("vndk set: %s", vndk_set)
121        unexpected = [x for x in self._ListFiles(vndk_dir) if
122                      path_utils.TargetBaseName(x) in vndk_set]
123        if unexpected:
124            self._Fail(unexpected)
125
126    def testVndkCoreDirectory(self):
127        """Verifies that VNDK directory doesn't contain extra files."""
128        asserts.skipIf(not vndk_utils.IsVndkRuntimeEnforced(self._dut),
129                       "VNDK runtime is not enforced on the device.")
130        self._TestVndkDirectory(
131            vndk_utils.GetVndkDirectory(self.abi_bitness, self._vndk_version),
132            (vndk_data.VNDK, vndk_data.VNDK_PRIVATE, vndk_data.VNDK_SP,
133             vndk_data.VNDK_SP_PRIVATE,))
134
135    def testNoLlndkInVendor(self):
136        """Verifies that vendor partition has no LL-NDK libraries."""
137        self._TestNotInVndkDirecotory(
138            vndk_utils.FormatVndkPath(
139                self._TARGET_VENDOR_LIB, self.abi_bitness),
140            (vndk_data.LL_NDK,),
141            self._LL_NDK_COLLIDING_NAMES)
142
143    def testNoLlndkInOdm(self):
144        """Verifies that odm partition has no LL-NDK libraries."""
145        self._TestNotInVndkDirecotory(
146            vndk_utils.FormatVndkPath(self._TARGET_ODM_LIB, self.abi_bitness),
147            (vndk_data.LL_NDK,),
148            self._LL_NDK_COLLIDING_NAMES)
149
150
151if __name__ == "__main__":
152    test_runner.main()
153