1#!/usr/bin/env python
2#
3# Copyright (C) 2016 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
19import random
20
21from vts.runners.host import base_test
22from vts.runners.host import test_runner
23from vts.utils.python.controllers import android_device
24from vts.utils.python.fuzzer import GenePool
25
26
27class LightFuzzTest(base_test.BaseTestClass):
28    """A sample fuzz testcase for the legacy lights HAL."""
29
30    def setUpClass(self):
31        required_params = ["gene_pool_size", "iteartion_count"]
32        self.getUserParams(required_params)
33        self.dut = self.registerController(android_device)[0]
34        self.dut.hal.InitConventionalHal(target_type="light",
35                                         target_basepaths=["/system/lib64/hw"],
36                                         target_version=1.0,
37                                         bits=64,
38                                         target_package="hal.conventional.light")
39        self.dut.hal.light.OpenConventionalHal("backlight")
40        module_name = random.choice(
41            [self.dut.hal.light.LIGHT_ID_BACKLIGHT,
42             self.dut.hal.light.LIGHT_ID_NOTIFICATIONS,
43             self.dut.hal.light.LIGHT_ID_ATTENTION])
44
45        # TODO: broken on bullhead
46        #   self.dut.hal.light.LIGHT_ID_KEYBOARD
47        #   self.dut.hal.light.LIGHT_ID_BUTTONS
48        #   self.dut.hal.light.LIGHT_ID_BATTERY
49        #   self.dut.hal.light.LIGHT_ID_BLUETOOTH
50        #   self.dut.hal.light.LIGHT_ID_WIFI
51
52    def testTurnOnLightBlackBoxFuzzing(self):
53        """A fuzz testcase which calls a function using different values."""
54        logging.info("blackbox fuzzing")
55        genes = GenePool.CreateGenePool(
56            self.gene_pool_size,
57            self.dut.hal.light.light_state_t,
58            self.dut.hal.light.light_state_t_fuzz,
59            color=0xffffff00,
60            flashMode=self.dut.hal.light.LIGHT_FLASH_HARDWARE,
61            flashOnMs=100,
62            flashOffMs=200,
63            brightnessMode=self.dut.hal.light.BRIGHTNESS_MODE_USER)
64        for iteration in range(self.iteartion_count):
65            index = 0
66            logging.info("blackbox iteration %d", iteration)
67            for gene in genes:
68                logging.debug("Gene %d", index)
69                self.dut.hal.light.set_light(None, gene)
70                index += 1
71            evolution = GenePool.Evolution()
72            genes = evolution.Evolve(genes,
73                                     self.dut.hal.light.light_state_t_fuzz)
74
75    def testTurnOnLightWhiteBoxFuzzing(self):
76        """A fuzz testcase which calls a function using different values."""
77        logging.info("whitebox fuzzing")
78        genes = GenePool.CreateGenePool(
79            self.gene_pool_size,
80            self.dut.hal.light.light_state_t,
81            self.dut.hal.light.light_state_t_fuzz,
82            color=0xffffff00,
83            flashMode=self.dut.hal.light.LIGHT_FLASH_HARDWARE,
84            flashOnMs=100,
85            flashOffMs=200,
86            brightnessMode=self.dut.hal.light.BRIGHTNESS_MODE_USER)
87
88        for iteration in range(self.iteartion_count):
89            index = 0
90            logging.info("whitebox iteration %d", iteration)
91            coverages = []
92            for gene in genes:
93                logging.debug("Gene %d", index)
94                result = self.dut.hal.light.set_light(None, gene)
95                if len(result.processed_coverage_data) > 0:
96                    logging.info("coverage: %s", result.processed_coverage_data)
97                    gene_coverage = []
98                    for coverage_data in result.processed_coverage_data:
99                        gene_coverage.append(coverage_data)
100                    coverages.append(gene_coverage)
101                index += 1
102            evolution = GenePool.Evolution()
103            genes = evolution.Evolve(genes,
104                                     self.dut.hal.light.light_state_t_fuzz,
105                                     coverages=coverages)
106
107
108if __name__ == "__main__":
109    test_runner.main()
110