普通文本  |  100行  |  3.75 KB

#!/usr/bin/env python
#
# Copyright (C) 2017 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

from vts.runners.host import keys
from vts.runners.host import test_runner
from vts.utils.python.controllers import adb
from vts.utils.python.controllers import android_device
from vts.utils.python.common import vts_spec_utils

from vts.testcases.fuzz.template.libfuzzer_test import libfuzzer_test_config as config
from vts.testcases.fuzz.template.libfuzzer_test import libfuzzer_test_case
from vts.testcases.fuzz.template.func_fuzzer_test import func_fuzzer_test


class IfaceFuzzerTest(func_fuzzer_test.FuncFuzzerTest):
    """Runs iface_fuzzer tests on target.

    Attributes:
        See func_fuzzer_test.
    """
    _VTS_SPEC_DIR_TARGET = os.path.join(config.FUZZER_TEST_DIR, 'spec')

    def _PushVtsResources(self, hal_name, hal_version):
        """Pushes resources needed for test to target device.

        Args:
            hal_name: string, name of the hal, e.g. 'vibrator'.
            hal_version: string, version of the hal, e.g '7.4'
        """
        # Push .vts spec files.
        hal_name_dir = hal_name.replace('.', '/')
        src_dir = os.path.join(self.data_file_path, 'spec', 'hardware',
                               'interfaces', hal_name_dir, hal_version, 'vts')

        # Push corresponding VTS drivers.
        driver_name = 'android.hardware.%s@%s-vts.driver.so' % (hal_name,
                                                                hal_version)
        driver32 = os.path.join(self.data_file_path, 'DATA', 'lib', driver_name)
        driver64 = os.path.join(self.data_file_path, 'DATA', 'lib64',
                                driver_name)
        try:
            self._dut.adb.push(src_dir, self._VTS_SPEC_DIR_TARGET)
            self._dut.adb.push(driver32, 'data/local/tmp/32')
            self._dut.adb.push(driver64, 'data/local/tmp/64')
        except adb.AdbError as e:
            logging.exception(e)

    # Override
    def CreateTestCases(self):
        """See base class."""
        hal_package = self.hal_hidl_package_name
        hal_name, hal_version = vts_spec_utils.HalPackageToNameAndVersion(
            hal_package)
        self._PushVtsResources(hal_name, hal_version)
        vts_spec_names = self._vts_spec_parser.VtsSpecNames(hal_name,
                                                            hal_version)
        registered_interfaces = self._RegisteredInterfaces(
            self.hal_hidl_package_name)

        test_cases = []
        for iface in registered_interfaces:
            additional_params = {
                'vts_spec_dir': self._VTS_SPEC_DIR_TARGET,
                'vts_exec_size': 128,
                'vts_target_iface': iface,
            }
            libfuzzer_params = {
                'max_len': 65536,
                'max_total_time': 1,
            }
            bin_host_path = os.path.join(self.data_file_path, 'DATA', 'bin',
                                           'vts_proto_fuzzer')
            test_case = libfuzzer_test_case.LibFuzzerTestCase(
                bin_host_path, libfuzzer_params, additional_params)
            test_case.test_name = iface
            test_cases.append(test_case)

        return test_cases


if __name__ == '__main__':
    test_runner.main()