Esempio n. 1
0
    def execute(self, state, args):
        self.validate_args(args)
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        if args.output:
            outdir = os.path.basename(args.output)
        else:
            outdir = os.getcwd()

        self.tm = TargetManager(device, device_config, outdir)
        self.tm.initialize()
        self.target = self.tm.target
        self.revent_recorder = ReventRecorder(self.target)
        self.revent_recorder.deploy()

        if args.workload:
            self.workload_record(args)
        elif args.package:
            self.package_record(args)
        else:
            self.manual_record(args)

        self.revent_recorder.remove()
Esempio n. 2
0
    def execute(self, state, args):
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        target_manager = TargetManager(device, device_config, None)
        target_manager.initialize()
        self.target = target_manager.target
        revent_file = self.target.path.join(self.target.working_directory,
                                            os.path.split(args.recording)[1])

        self.logger.info("Pushing file to target")
        self.target.push(args.recording, self.target.working_directory)

        revent_recorder = ReventRecorder(target_manager.target)
        revent_recorder.deploy()

        if args.clear:
            self.target.execute('pm clear {}'.format(args.package))

        if args.package:
            self.logger.info('Starting {}'.format(args.package))
            cmd = 'monkey -p {} -c android.intent.category.LAUNCHER 1'
            self.target.execute(cmd.format(args.package))

        self.logger.info("Starting replay")
        revent_recorder.replay(revent_file)
        self.logger.info("Finished replay")
        revent_recorder.remove()
Esempio n. 3
0
 def __init__(self, workload, target, setup_timeout, run_timeout,
              extract_results_timeout, teardown_timeout):
     self.workload = workload
     self.target = target
     self.setup_timeout = setup_timeout
     self.run_timeout = run_timeout
     self.extract_results_timeout = extract_results_timeout
     self.teardown_timeout = teardown_timeout
     self.revent_recorder = ReventRecorder(self.target)
     self.on_target_revent_binary = self.target.get_workpath('revent')
     self.on_target_setup_revent = self.target.get_workpath('{}.setup.revent'.format(self.target.model))
     self.on_target_run_revent = self.target.get_workpath('{}.run.revent'.format(self.target.model))
     self.on_target_extract_results_revent = self.target.get_workpath('{}.extract_results.revent'.format(self.target.model))
     self.on_target_teardown_revent = self.target.get_workpath('{}.teardown.revent'.format(self.target.model))
     self.logger = logging.getLogger('revent')
     self.revent_setup_file = None
     self.revent_run_file = None
     self.revent_extract_results_file = None
     self.revent_teardown_file = None
Esempio n. 4
0
class RecordCommand(Command):

    name = 'record'
    description = '''
    Performs a revent recording

    This command helps making revent recordings. It will automatically
    deploy revent and has options to automatically open apps and record
    specified stages of a workload.

    Revent allows you to record raw inputs such as screen swipes or button presses.
    This can be useful for recording inputs for workloads such as games that don't
    have XML UI layouts that can be used with UIAutomator. As a drawback from this,
    revent recordings are specific to the device type they were recorded on.

    WA uses two parts to the names of revent recordings in the format,
    {device_name}.{suffix}.revent.

     - device_name can either be specified manually with the ``-d`` argument or
       it can be automatically determined. On Android device it will be obtained
       from ``build.prop``, on Linux devices it is obtained from ``/proc/device-tree/model``.
     - suffix is used by WA to determine which part of the app execution the
       recording is for, currently these are either ``setup``, ``run``, ``extract_results``
       or ``teardown``. All stages are optional for recording and these should
       be specified with the ``-s``, ``-r``, ``-e`` or ``-t`` arguments respectively,
       or optionally ``-a`` to indicate all stages should be recorded.
    '''

    def __init__(self, **kwargs):
        super(RecordCommand, self).__init__(**kwargs)
        self.tm = None
        self.target = None
        self.revent_recorder = None

    def initialize(self, context):
        self.parser.add_argument('-d', '--device', metavar='DEVICE',
                                 help='''
                                 Specify the device on which to run. This will
                                 take precedence over the device (if any)
                                 specified in configuration.
                                 ''')
        self.parser.add_argument('-o', '--output', help='Specify the output file', metavar='FILE')
        self.parser.add_argument('-s', '--setup', help='Record a recording for setup stage',
                                 action='store_true')
        self.parser.add_argument('-r', '--run', help='Record a recording for run stage',
                                 action='store_true')
        self.parser.add_argument('-e', '--extract_results', help='Record a recording for extract_results stage',
                                 action='store_true')
        self.parser.add_argument('-t', '--teardown', help='Record a recording for teardown stage',
                                 action='store_true')
        self.parser.add_argument('-a', '--all', help='Record recordings for available stages',
                                 action='store_true')

        # Need validation
        self.parser.add_argument('-C', '--clear', help='Clear app cache before launching it',
                                 action='store_true')
        group = self.parser.add_mutually_exclusive_group(required=False)
        group.add_argument('-p', '--package', help='Android package to launch before recording')
        group.add_argument('-w', '--workload', help='Name of a revent workload (mostly games)')

    def validate_args(self, args):
        if args.clear and not (args.package or args.workload):
            self.logger.error("Package/Workload must be specified if you want to clear cache")
            sys.exit()
        if args.workload and args.output:
            self.logger.error("Output file cannot be specified with Workload")
            sys.exit()
        if not args.workload and (args.setup or args.extract_results or
                                  args.teardown or args.all):
            self.logger.error("Cannot specify a recording stage without a Workload")
            sys.exit()
        if args.workload and not any([args.all, args.teardown, args.extract_results, args.run, args.setup]):
            self.logger.error("Please specify which workload stages you wish to record")
            sys.exit()

    def execute(self, state, args):
        self.validate_args(args)
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        if args.output:
            outdir = os.path.basename(args.output)
        else:
            outdir = os.getcwd()

        self.tm = TargetManager(device, device_config, outdir)
        self.tm.initialize()
        self.target = self.tm.target
        self.revent_recorder = ReventRecorder(self.target)
        self.revent_recorder.deploy()

        if args.workload:
            self.workload_record(args)
        elif args.package:
            self.package_record(args)
        else:
            self.manual_record(args)

        self.revent_recorder.remove()

    def record(self, revent_file, name, output_path):
        msg = 'Press Enter when you are ready to record {}...'
        self.logger.info(msg.format(name))
        raw_input('')
        self.revent_recorder.start_record(revent_file)
        msg = 'Press Enter when you have finished recording {}...'
        self.logger.info(msg.format(name))
        raw_input('')
        self.revent_recorder.stop_record()

        if not os.path.isdir(output_path):
            os.makedirs(output_path)

        revent_file_name = self.target.path.basename(revent_file)
        host_path = os.path.join(output_path, revent_file_name)
        if os.path.exists(host_path):
            msg = 'Revent file \'{}\' already exists, overwrite? [y/n]'
            self.logger.info(msg.format(revent_file_name))
            if input('') == 'y':
                os.remove(host_path)
            else:
                msg = 'Did not pull and overwrite \'{}\''
                self.logger.warning(msg.format(revent_file_name))
                return
        msg = 'Pulling \'{}\' from device'
        self.logger.info(msg.format(self.target.path.basename(revent_file)))
        self.target.pull(revent_file, output_path, as_root=self.target.is_rooted)

    def manual_record(self, args):
        output_path, file_name = self._split_revent_location(args.output)
        revent_file = self.target.get_workpath(file_name)
        self.record(revent_file, '', output_path)
        msg = 'Recording is available at: \'{}\''
        self.logger.info(msg.format(os.path.join(output_path, file_name)))

    def package_record(self, args):
        if self.target.os != 'android' and self.target.os != 'chromeos':
            raise ConfigError('Target does not appear to be running Android')
        if self.target.os == 'chromeos' and not self.target.supports_android:
            raise ConfigError('Target does not appear to support Android')
        if args.clear:
            self.target.execute('pm clear {}'.format(args.package))
        self.logger.info('Starting {}'.format(args.package))
        cmd = 'monkey -p {} -c android.intent.category.LAUNCHER 1'
        self.target.execute(cmd.format(args.package))

        output_path, file_name = self._split_revent_location(args.output)
        revent_file = self.target.get_workpath(file_name)
        self.record(revent_file, '', output_path)
        msg = 'Recording is available at: \'{}\''
        self.logger.info(msg.format(os.path.join(output_path, file_name)))

    def workload_record(self, args):
        context = LightContext(self.tm)
        setup_revent = '{}.setup.revent'.format(self.target.model)
        run_revent = '{}.run.revent'.format(self.target.model)
        extract_results_revent = '{}.extract_results.revent'.format(self.target.model)
        teardown_file_revent = '{}.teardown.revent'.format(self.target.model)
        setup_file = self.target.get_workpath(setup_revent)
        run_file = self.target.get_workpath(run_revent)
        extract_results_file = self.target.get_workpath(extract_results_revent)
        teardown_file = self.target.get_workpath(teardown_file_revent)

        self.logger.info('Deploying {}'.format(args.workload))
        workload = pluginloader.get_workload(args.workload, self.target)
        # Setup apk if android workload
        if hasattr(workload, 'apk'):
            workload.apk.initialize(context)
            workload.apk.setup(context)
            sleep(workload.loading_time)

        output_path = os.path.join(workload.dependencies_directory,
                                   'revent_files')
        if args.setup or args.all:
            self.record(setup_file, 'SETUP', output_path)
        if args.run or args.all:
            self.record(run_file, 'RUN', output_path)
        if args.extract_results or args.all:
            self.record(extract_results_file, 'EXTRACT_RESULTS', output_path)
        if args.teardown or args.all:
            self.record(teardown_file, 'TEARDOWN', output_path)
        self.logger.info('Tearing down {}'.format(args.workload))
        workload.teardown(context)
        self.logger.info('Recording(s) are available at: \'{}\''.format(output_path))

    def _split_revent_location(self, output):
        output_path = None
        file_name = None
        if output:
            output_path, file_name, = os.path.split(output)

        if not file_name:
            file_name = '{}.revent'.format(self.target.model)
        if not output_path:
            output_path = os.getcwd()

        return output_path, file_name
Esempio n. 5
0
class ReventGUI(object):

    def __init__(self, workload, target, setup_timeout, run_timeout,
                 extract_results_timeout, teardown_timeout):
        self.workload = workload
        self.target = target
        self.setup_timeout = setup_timeout
        self.run_timeout = run_timeout
        self.extract_results_timeout = extract_results_timeout
        self.teardown_timeout = teardown_timeout
        self.revent_recorder = ReventRecorder(self.target)
        self.on_target_revent_binary = self.target.get_workpath('revent')
        self.on_target_setup_revent = self.target.get_workpath('{}.setup.revent'.format(self.target.model))
        self.on_target_run_revent = self.target.get_workpath('{}.run.revent'.format(self.target.model))
        self.on_target_extract_results_revent = self.target.get_workpath('{}.extract_results.revent'.format(self.target.model))
        self.on_target_teardown_revent = self.target.get_workpath('{}.teardown.revent'.format(self.target.model))
        self.logger = logging.getLogger('revent')
        self.revent_setup_file = None
        self.revent_run_file = None
        self.revent_extract_results_file = None
        self.revent_teardown_file = None

    def init_resources(self, resolver):
        self.revent_setup_file = resolver.get(ReventFile(owner=self.workload,
                                                         stage='setup',
                                                         target=self.target.model),
                                              strict=False)
        self.revent_run_file = resolver.get(ReventFile(owner=self.workload,
                                                       stage='run',
                                                       target=self.target.model))
        self.revent_extract_results_file = resolver.get(ReventFile(owner=self.workload,
                                                                   stage='extract_results',
                                                                   target=self.target.model),
                                                        strict=False)
        self.revent_teardown_file = resolver.get(resource=ReventFile(owner=self.workload,
                                                                     stage='teardown',
                                                                     target=self.target.model),
                                                 strict=False)

    def deploy(self):
        self.revent_recorder.deploy()

    def setup(self):
        self._check_revent_files()
        if self.revent_setup_file:
            self.revent_recorder.replay(self.on_target_setup_revent,
                                        timeout=self.setup_timeout)

    def run(self):
        msg = 'Replaying {}'
        self.logger.debug(msg.format(os.path.basename(self.on_target_run_revent)))
        self.revent_recorder.replay(self.on_target_run_revent,
                                    timeout=self.run_timeout)
        self.logger.debug('Replay completed.')

    def extract_results(self):
        if self.revent_extract_results_file:
            self.revent_recorder.replay(self.on_target_extract_results_revent,
                                        timeout=self.extract_results_timeout)

    def teardown(self):
        if self.revent_teardown_file:
            self.revent_recorder.replay(self.on_target_teardown_revent,
                                        timeout=self.teardown_timeout)
        self.target.remove(self.on_target_setup_revent)
        self.target.remove(self.on_target_run_revent)
        self.target.remove(self.on_target_extract_results_revent)
        self.target.remove(self.on_target_teardown_revent)

    def remove(self):
        self.revent_recorder.remove()

    def _check_revent_files(self):
        if not self.revent_run_file:
            # pylint: disable=too-few-format-args
            message = '{0}.run.revent file does not exist, ' \
                      'Please provide one for your target, {0}'
            raise WorkloadError(message.format(self.target.model))

        self.target.push(self.revent_run_file, self.on_target_run_revent)
        if self.revent_setup_file:
            self.target.push(self.revent_setup_file, self.on_target_setup_revent)
        if self.revent_extract_results_file:
            self.target.push(self.revent_extract_results_file, self.on_target_extract_results_revent)
        if self.revent_teardown_file:
            self.target.push(self.revent_teardown_file, self.on_target_teardown_revent)