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