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