def run(self, device: Device, package_name: str, script_name: str) -> None: verbose_level = RequiredFeature('verbose_level').request() start_time = time.time() self.prepare_device_for_run(device) string_seeding_flag = "" if self.use_motifgene: string_seeding_flag = f"--string-seeding /mnt/sdcard/{package_name}_strings.xml" motifcore_cmd = f"motifcore -p {package_name} --ignore-crashes --ignore-security-exceptions --ignore-timeouts" \ f" --bugreport {string_seeding_flag} -f /mnt/sdcard/{script_name} 1" output, errors, result_code = adb.shell_command( device, motifcore_cmd, timeout=settings.TEST_CASE_EVAL_TIMEOUT) if verbose_level > 1: print(f"Test case running finished with output:\n{output}") if "Exception" in errors: device_stacktrace = errors.split("** Error: ")[1] raise Exception( f"An error occurred when running test case: {device_stacktrace}" ) # need to manually kill motifcore when timeout adb.pkill(device, "motifcore") self.clean_device_after_run(device) if verbose_level > 0: logger.log_progress( f'\nMotifcore test run took: {time.time() - start_time:.2f} seconds' )
def run(self, device: Device, package_name: str, script_name: str) -> None: assert device.api_level() >= self.minimum_api verbose_level = RequiredFeature('verbose_level').request() start_time = time.time() self.prepare_device_for_run(device) evolutiz_cmd = f"evolutiz -p {package_name} -v -v -v --throttle 200 --ignore-crashes " \ f"--ignore-security-exceptions --ignore-timeouts --bugreport -f /mnt/sdcard/{script_name} 1" output, errors, result_code = adb.shell_command( device, evolutiz_cmd, timeout=settings.TEST_CASE_EVAL_TIMEOUT) if verbose_level > 1: print(f"Test case running finished with output:\n{output}") if "Exception" in errors: device_stacktrace = errors.split("** Error: ")[1] raise Exception( f"An error occurred when running test case: {device_stacktrace}" ) # need to manually kill evolutiz when timeout adb.pkill(device, "evolutiz") self.clean_device_after_run(device) if verbose_level > 0: logger.log_progress( f'\nEvolutiz test run took: {time.time() - start_time:.2f} seconds' )
def generate(self, device: Device, package_name: str, destination_file_name: str) -> TestCase: verbose_level = RequiredFeature('verbose_level').request() start_time = time.time() self.prepare_device_for_run(device) evolutiz_events = random.randint(settings.SEQUENCE_LENGTH_MIN, settings.SEQUENCE_LENGTH_MAX) string_seeding_flag = "" # String seeding is NOT supported yet # if self.use_motifgene: # string_seeding_flag = f"--string-seeding /mnt/sdcard/{package_name}_strings.xml" evolutiz_cmd = f"evolutiz -p {package_name} --ignore-crashes --ignore-security-exceptions --ignore-timeouts" \ f" --bugreport {string_seeding_flag} -o {self.evolutiz_script_path_in_devices}" \ f" --throttle {self.throttle} -v {str(evolutiz_events)}" output, errors, result_code = adb.shell_command( device, evolutiz_cmd, timeout=settings.TEST_CASE_EVAL_TIMEOUT) if verbose_level > 1: print(f"Test case generation finished with output:\n{output}") if "Exception" in errors: if "** Error: " in errors: device_stacktrace = errors.split("** Error: ")[1] raise Exception( f"An error occurred when generating test case: {device_stacktrace}" ) else: raise Exception( f"An error occurred when generating test case: {errors}") # need to manually kill evolutiz when timeout adb.pkill(device, "evolutiz") time.sleep(5) test_case: TestCase = self.retrieve_generated_test( device, destination_file_name) self.clean_device_after_run(device) if verbose_level > 0: logger.log_progress( f'\nEvolutiz test generation took: {time.time() - start_time:.2f} seconds ' f'for {evolutiz_events:d} events') return test_case
def send_command(self, device: Device, package_name: str, command: str) -> str: """ :param command: to send through the socket and be run by the runner. :return: the response of the runner as a string. """ # ensure there is only one return character at the end of the command command = command.rstrip("\n") + "\n" # set up evolutiz runner in emulator adb.adb_command(device, f"forward tcp:{self.port} tcp:{self.port}") adb.shell_command( device, f"evolutiz -p {package_name} -c android.intent.category.LAUNCHER --port {self.port} &", discard_output=True) # wait for evolutiz runner to be ready output = "" tries = 0 while "Using EvolutizSourceNetwork" not in output: if tries >= 6: raise Exception( f"Unable to connect to Evolutiz test runner for command: {command}." ) tries += 1 output, errors, result_code = adb.adb_command( device, f"logcat -s Evolutiz -d | tail -n 1") time.sleep(0.5) # send command and collect result with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((self.host, self.port)) s.sendall(command.encode('utf-8')) data = self.receive_data(s) s.close() # need to manually kill evolutiz after working adb.pkill(device, "evolutiz") if data is None: raise Exception(f"Unable to parse result of command {command}.") else: return data.rstrip("\n")
def run_monkey_one_app(app_path, device): folder_name = os.path.basename(app_path) try: result_dir = "../../results/" + folder_name os.chdir(app_path) os.system("rm " + result_dir + "/*" + logger.redirect_string()) apk_path, package_name = instrument_apk(folder_name, app_path, result_dir) os.system(adb.adb_cmd_prefix + " -s " + device + " install " + apk_path + " 2>&1 >" + result_dir + "/install.log") logger.log_progress("\nPreparing device: " + device + " sdcard") adb.sudo_shell_command(device, "mount -o rw,remount rootfs /", timeout=settings.ADB_REGULAR_COMMAND_TIMEOUT) adb.sudo_shell_command(device, "chmod 777 /mnt/sdcard", timeout=settings.ADB_REGULAR_COMMAND_TIMEOUT) adb.sudo_shell_command(device, "mount -o rw,remount /system", timeout=settings.ADB_REGULAR_COMMAND_TIMEOUT) for repetition in range(0, REPETITIONS): logger.log_progress("\nStarting repetition: " + str(repetition) + " for app: " + folder_name) files_repetition_suffix = "." + str(repetition) # clear package data from previous runs adb.shell_command(device, "pm clear " + package_name, timeout=settings.ADB_REGULAR_COMMAND_TIMEOUT) # clear logcat os.system(adb.adb_cmd_prefix + " -s " + device + " logcat -c") # run logcat logcat_file = open( result_dir + "/monkey.logcat" + files_repetition_suffix, 'w') sub.Popen(adb.adb_cmd_prefix + " -s " + device + " logcat", stdout=logcat_file, stderr=logcat_file, shell=True) # start dumping intermediate coverage # monkey_finished_event = multiprocessing.Event() # p = multiprocessing.Process(target=startIntermediateCoverage, args=(device, result_dir, monkey_finished_event)) # p.start() # start running monkey with timeout EXPERIMENT_TIME logger.log_progress("\nStarting monkey for app: " + folder_name + " in device: " + device + " at: " + datetime.today().strftime("%H:%M:%S")) monkey_cmd = timeout_cmd + adb.adb_cmd_prefix + " -s " + device + " shell monkey -p " + package_name + " -v --throttle 200 --ignore-crashes --ignore-native-crashes --ignore-timeouts --ignore-security-exceptions 1000000 2>&1 >" + result_dir + "/monkey.log" + files_repetition_suffix os.system(monkey_cmd) adb.pkill(device, "monkey") logger.log_progress("\nMonkey finished for app: " + folder_name) # monkey_finished_event.set() # p.join() # collect final coverage collectCoverage(device, package_name, result_dir, suffix=files_repetition_suffix) return (True, device) except Exception as e: logger.log_progress("\nThere was an error running monkey on app: " + folder_name) traceback.print_exc() return (False, device)
def kill_test_runner_in_device(self, device: Device) -> None: adb.pkill(device, self.test_runner_name)