def install_application_if_needed(apk_path, force_update): """Install application package if it does not exist on device or if force_update is set.""" # Make sure that apk exists and has non-zero size. Otherwise, it means we # are using a system package that we just want to fuzz, but not care about # installation. if (not apk_path or not os.path.exists(apk_path) or not os.path.getsize(apk_path)): return # If we don't have a package name, we can't uninstall the app. This is needed # for installation workflow. package_name = adb.get_package_name() if not package_name: return # Add |REINSTALL_APP_BEFORE_EACH_TASK| to force update decision. reinstall_app_before_each_task = environment.get_value( 'REINSTALL_APP_BEFORE_EACH_TASK', False) force_update = force_update or reinstall_app_before_each_task # Install application if it is not found in the device's # package list or force_update flag has been set. if force_update or not adb.is_package_installed(package_name): # Update system webview when fuzzing webview shell apk. if package_name == 'org.chromium.webview_shell': update_system_web_view() adb.uninstall_package(package_name) adb.install_package(apk_path) if not adb.is_package_installed(package_name): logs.log_error('Package %s was not installed successfully.' % package_name) return logs.log('Package %s is successfully installed using apk %s.' % (package_name, apk_path)) adb.reset_application_state()
def configure_wifi_and_airplane_mode(wifi_enabled=False): """Configure airplane mode and wifi on device.""" # Airplane mode should be disabled in all cases. This can get inadvertently # turned on via gestures. adb.disable_airplane_mode() # Need to disable wifi before changing configuration. adb.disable_wifi() # Check if wifi needs to be enabled. If not, then no need to modify the # supplicant file. wifi_enabled = wifi_enabled or environment.get_value('WIFI', True) if not wifi_enabled: # No more work to do, we already disabled it at start. return if adb.is_gce(): wifi_ssid = 'VirtWifi' wifi_password = '' else: config = db_config.get() if not config.wifi_ssid: logs.log('No wifi ssid is set, skipping wifi config.') return wifi_ssid = config.wifi_ssid wifi_password = config.wifi_password or '' adb.enable_wifi() # Wait 2 seconds to allow the wifi to be enabled. time.sleep(2) wifi_util_apk_path = os.path.join( environment.get_platform_resources_directory(), 'wifi_util.apk') if not adb.is_package_installed(WIFI_UTIL_PACKAGE_NAME): adb.install_package(wifi_util_apk_path) connect_wifi_command = ( 'am instrument -e method connectToNetwork -e ssid {ssid} ') if wifi_password: connect_wifi_command += '-e psk {password} ' connect_wifi_command += '-w {call_path}' output = adb.run_adb_shell_command( connect_wifi_command.format( ssid=quote(wifi_ssid), password=quote(wifi_password), call_path=WIFI_UTIL_CALL_PATH)) if 'result=true' not in output: logs.log_error('Failed to connect to wifi.', output=output)
def add_test_accounts_if_needed(): """Add test account to work with GmsCore, etc.""" last_test_account_check_time = persistent_cache.get_value( LAST_TEST_ACCOUNT_CHECK_KEY, constructor=datetime.datetime.utcfromtimestamp) needs_test_account_update = (last_test_account_check_time is None or dates.time_has_expired( last_test_account_check_time, seconds=ADD_TEST_ACCOUNT_CHECK_INTERVAL)) if not needs_test_account_update: return config = db_config.get() test_account_email = config.test_account_email test_account_password = config.test_account_password if not test_account_email or not test_account_password: return adb.run_as_root() configure_wifi_and_airplane_mode(wifi_enabled=True) if not adb.is_package_installed(ADD_TEST_ACCOUNT_PKG_NAME): logs.log('Installing helper apk for adding test account.') android_directory = environment.get_platform_resources_directory() add_test_account_apk_path = os.path.join(android_directory, ADD_TEST_ACCOUNT_APK_NAME) adb.install_package(add_test_account_apk_path) logs.log('Trying to add test account.') output = adb.run_adb_shell_command( 'am instrument -e account %s -e password %s -w %s' % (test_account_email, test_account_password, ADD_TEST_ACCOUNT_CALL_PATH), timeout=ADD_TEST_ACCOUNT_TIMEOUT) if not output or test_account_email not in output: logs.log('Failed to add test account, probably due to wifi issues.') return logs.log('Test account added successfully.') persistent_cache.set_value(LAST_TEST_ACCOUNT_CHECK_KEY, time.time())
def update_system_web_view(): """Updates the system webview on the device.""" app_directory = environment.get_value('APP_DIR') system_webview_apk = os.path.join(app_directory, SYSTEM_WEBVIEW_APK_NAME) if not os.path.exists(system_webview_apk): logs.log_error('System Webview apk not found.') return adb.set_property('persist.sys.webview.vmsize', SYSTEM_WEBVIEW_VMSIZE_BYTES) adb.run_as_root() if any([adb.directory_exists(d) for d in SYSTEM_WEBVIEW_DIRS]): adb.remount() adb.stop_shell() adb.run_adb_shell_command(['rm', '-rf', ' '.join(SYSTEM_WEBVIEW_DIRS)]) reboot() adb.uninstall_package(SYSTEM_WEBVIEW_PACKAGE) adb.install_package(system_webview_apk) if not adb.is_package_installed(SYSTEM_WEBVIEW_PACKAGE): logs.log_error('Package %s was not installed successfully.' % SYSTEM_WEBVIEW_PACKAGE)
def test_package_installed(self): """Ensure that gms (which should always be available) is installed.""" self.assertTrue(adb.is_package_installed('com.google.android.gms'))
def test_partial_package_name_not_installed(self): """Test that com.google is not recognized as an installed package.""" self.assertFalse(adb.is_package_installed('com.google'))
def test_nonexistent_package_not_installed(self): """Ensure that a non-existent package is not installed.""" self.assertFalse(adb.is_package_installed('non.existent.package'))
def test_is_package_installed(self): """Tests is_package_installed.""" self.assertFalse(adb.is_package_installed('non.existent.package')) self.assertFalse(adb.is_package_installed('com.google')) self.assertTrue(adb.is_package_installed('com.google.android.gms'))