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