def testEscapeCmdArguments(self): """Commands and their arguments that are executed through the cros interface should follow bash syntax. This test needs to run on remotely and locally on the device to check for consistency. """ options = options_for_unittests.GetCopy() with cros_interface.CrOSInterface(options.cros_remote, options.cros_remote_ssh_port, options.cros_ssh_identity) as cri: # Check arguments with no special characters stdout, _ = cri.RunCmdOnDevice( ['echo', '--arg1=value1', '--arg2=value2', '--arg3="value3"']) assert stdout.strip( ) == '--arg1=value1 --arg2=value2 --arg3=value3' # Check argument with special characters escaped stdout, _ = cri.RunCmdOnDevice(['echo', '--arg=A\\; echo \\"B\\"']) assert stdout.strip() == '--arg=A; echo "B"' # Check argument with special characters in quotes stdout, _ = cri.RunCmdOnDevice(['echo', "--arg='$HOME;;$PATH'"]) assert stdout.strip() == "--arg=$HOME;;$PATH"
def testIsCryptohomeMounted(self, mock_run_cmd): # The device's mount state is checked by the command # /bin/df --someoption `cryptohome-path user $username`. # The following mock replaces RunCmdOnDevice() to return mocked mount states # from the command execution. def mockRunCmdOnDevice(args): # pylint: disable=invalid-name if args[0] == 'cryptohome-path': return ('/home/user/%s' % args[2], '') elif args[0] == '/bin/df': if 'unmount' in args[2]: # For the user [email protected], returns the unmounted state. source, target = '/dev/sda1', '/home' elif 'mount' in args[2]: # For the user [email protected], returns the mounted state. source, target = '/dev/sda1', args[2] elif 'guest' in args[2]: # For the user $guest, returns the guest-mounted state. source, target = '/dev/loop7', args[2] return ('Filesystem Mounted on\n%s %s\n' % (source, target), '') mock_run_cmd.side_effect = mockRunCmdOnDevice cri = cros_interface.CrOSInterface( "testhostname", 22, options_for_unittests.GetCopy().cros_ssh_identity) # Returns False if the user's cryptohome is not mounted. self.assertFalse(cri.IsCryptohomeMounted('*****@*****.**', False)) # Returns True if the user's cryptohome is mounted. self.assertTrue(cri.IsCryptohomeMounted('*****@*****.**', False)) # Returns True if the guest cryptohome is mounted. self.assertTrue(cri.IsCryptohomeMounted('$guest', True)) # Sanity check. Returns False if the |is_guest| parameter does not match # with whether or not the user is really a guest. self.assertFalse(cri.IsCryptohomeMounted('*****@*****.**', True)) self.assertFalse(cri.IsCryptohomeMounted('*****@*****.**', True)) self.assertFalse(cri.IsCryptohomeMounted('$guest', False))
def __init__(self, logged_in=True, extension_paths=None, autotest_ext=False, num_tries=3, extra_browser_args=None, clear_enterprise_policy=True, expect_policy_fetch=False, dont_override_profile=False, disable_gaia_services=True, disable_default_apps=True, auto_login=True, gaia_login=False, username=None, password=None, gaia_id=None, arc_mode=None, arc_timeout=None, disable_arc_opt_in=True, disable_arc_opt_in_verification=True, disable_arc_cpu_restriction=True, disable_app_sync=False, disable_play_auto_install=False, disable_locale_sync=True, disable_play_store_auto_update=True, enable_assistant=False, enterprise_arc_test=False, init_network_controller=False, mute_audio=False, proxy_server=None, login_delay=0): """ Constructor of telemetry wrapper. @param logged_in: Regular user (True) or guest user (False). @param extension_paths: path of unpacked extension to install. @param autotest_ext: Load a component extension with privileges to invoke chrome.autotestPrivate. @param num_tries: Number of attempts to log in. @param extra_browser_args: Additional argument(s) to pass to the browser. It can be a string or a list. @param clear_enterprise_policy: Clear enterprise policy before logging in. @param expect_policy_fetch: Expect that chrome can reach the device management server and download policy. @param dont_override_profile: Don't delete cryptohome before login. Telemetry will output a warning with this option. @param disable_gaia_services: For enterprise autotests, this option may be used to enable policy fetch. @param disable_default_apps: For tests that exercise default apps. @param auto_login: Does not login automatically if this is False. Useful if you need to examine oobe. @param gaia_login: Logs in to real gaia. @param username: Log in using this username instead of the default. @param password: Log in using this password instead of the default. @param gaia_id: Log in using this gaia_id instead of the default. @param arc_mode: How ARC instance should be started. Default is to not start. @param arc_timeout: Timeout to wait for ARC to boot. @param disable_arc_opt_in: For opt in flow autotest. This option is used to disable the arc opt in flow. @param disable_arc_opt_in_verification: Adds --disable-arc-opt-in-verification to browser args. This should generally be enabled when disable_arc_opt_in is enabled. However, for data migration tests where user's home data is already set up with opted-in state before login, this option needs to be set to False with disable_arc_opt_in=True to make ARC container work. @param disable_arc_cpu_restriction: Adds --disable-arc-cpu-restriction to browser args. This is enabled by default and will make tests run faster and is generally desirable unless a test is actually trying to test performance where ARC is running in the background for some porition of the test. @param disable_app_sync: Adds --arc-disable-app-sync to browser args and this disables ARC app sync flow. By default it is enabled. @param disable_play_auto_install: Adds --arc-disable-play-auto-install to browser args and this disables ARC Play Auto Install flow. By default it is enabled. @param enable_assistant: For tests that require to enable Google Assistant service. Default is False. @param enterprise_arc_test: Skips opt_in causing enterprise tests to fail @param disable_locale_sync: Adds --arc-disable-locale-sync to browser args and this disables locale sync between Chrome and Android container. In case of disabling sync, Android container is started with language and preference language list as it was set on the moment of starting full instance. Used to prevent random app restarts caused by racy locale change, coming from profile sync. By default locale sync is disabled. @param disable_play_store_auto_update: Adds --arc-play-store-auto-update=off to browser args and this disables Play Store, GMS Core and third-party apps auto-update. By default auto-update is off to have stable autotest environment. @param mute_audio: Mute audio. @param proxy_server: To launch the chrome with --proxy-server Adds '--proxy-server="http://$HTTP_PROXY:PORT"' to browser args. By default proxy-server is disabled @param login_delay: Time for idle in login screen to simulate the time required for password typing. """ self._autotest_ext_path = None # Force autotest extension if we need enable Play Store. if (utils.is_arc_available() and (arc_util.should_start_arc(arc_mode) or not disable_arc_opt_in)): autotest_ext = True if extension_paths is None: extension_paths = [] finder_options = browser_options.BrowserFinderOptions() if proxy_server: finder_options.browser_options.AppendExtraBrowserArgs( ['--proxy-server="%s"' % proxy_server]) if utils.is_arc_available() and arc_util.should_start_arc(arc_mode): if disable_arc_opt_in and disable_arc_opt_in_verification: finder_options.browser_options.AppendExtraBrowserArgs( ['--disable-arc-opt-in-verification']) if disable_arc_cpu_restriction: finder_options.browser_options.AppendExtraBrowserArgs( ['--disable-arc-cpu-restriction']) if disable_app_sync: finder_options.browser_options.AppendExtraBrowserArgs( ['--arc-disable-app-sync']) if disable_play_auto_install: finder_options.browser_options.AppendExtraBrowserArgs( ['--arc-disable-play-auto-install']) if disable_locale_sync: finder_options.browser_options.AppendExtraBrowserArgs( ['--arc-disable-locale-sync']) if disable_play_store_auto_update: finder_options.browser_options.AppendExtraBrowserArgs( ['--arc-play-store-auto-update=off']) logged_in = True if autotest_ext: self._autotest_ext_path = os.path.join(os.path.dirname(__file__), 'autotest_private_ext') extension_paths.append(self._autotest_ext_path) finder_options.browser_options.AppendExtraBrowserArgs( ['--whitelisted-extension-id=%s' % self.AUTOTEST_EXT_ID]) self._browser_type = (self.BROWSER_TYPE_LOGIN if logged_in else self.BROWSER_TYPE_GUEST) finder_options.browser_type = self.browser_type if extra_browser_args: finder_options.browser_options.AppendExtraBrowserArgs( extra_browser_args) # finder options must be set before parse_args(), browser options must # be set before Create(). # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit # autotest debug logs finder_options.verbosity = 2 finder_options.CreateParser().parse_args(args=[]) b_options = finder_options.browser_options b_options.disable_component_extensions_with_background_pages = False b_options.create_browser_with_oobe = True b_options.clear_enterprise_policy = clear_enterprise_policy b_options.dont_override_profile = dont_override_profile b_options.disable_gaia_services = disable_gaia_services b_options.disable_default_apps = disable_default_apps b_options.disable_component_extensions_with_background_pages = disable_default_apps b_options.disable_background_networking = False b_options.expect_policy_fetch = expect_policy_fetch b_options.auto_login = auto_login b_options.gaia_login = gaia_login b_options.mute_audio = mute_audio b_options.login_delay = login_delay if utils.is_arc_available() and not disable_arc_opt_in: arc_util.set_browser_options_for_opt_in(b_options) self.username = b_options.username if username is None else username self.password = b_options.password if password is None else password self.username = NormalizeEmail(self.username) b_options.username = self.username b_options.password = self.password self.gaia_id = b_options.gaia_id if gaia_id is None else gaia_id b_options.gaia_id = self.gaia_id self.arc_mode = arc_mode if logged_in: extensions_to_load = b_options.extensions_to_load for path in extension_paths: extension = extension_to_load.ExtensionToLoad( path, self.browser_type) extensions_to_load.append(extension) self._extensions_to_load = extensions_to_load # Turn on collection of Chrome coredumps via creation of a magic file. # (Without this, Chrome coredumps are trashed.) open(constants.CHROME_CORE_MAGIC_FILE, 'w').close() self._browser_to_create = browser_finder.FindBrowser(finder_options) self._browser_to_create.SetUpEnvironment(b_options) for i in range(num_tries): try: self._browser = self._browser_to_create.Create() self._browser_pid = \ cros_interface.CrOSInterface().GetChromePid() if utils.is_arc_available(): if disable_arc_opt_in: if arc_util.should_start_arc(arc_mode): arc_util.enable_play_store(self.autotest_ext, True) else: if not enterprise_arc_test: wait_for_provisioning = \ arc_mode != arc_common.ARC_MODE_ENABLED_ASYNC arc_util.opt_in( browser=self.browser, autotest_ext=self.autotest_ext, wait_for_provisioning=wait_for_provisioning) arc_util.post_processing_after_browser(self, arc_timeout) if enable_assistant: assistant_util.enable_assistant(self.autotest_ext) break except exceptions.LoginException as e: logging.error('Timed out logging in, tries=%d, error=%s', i, repr(e)) if i == num_tries - 1: raise if init_network_controller: self._browser.platform.network_controller.Open()
from telemetry.core import cros_interface from telemetry.core import platform from telemetry.core import util from telemetry.internal.forwarders import cros_forwarder from telemetry.internal.platform import cros_device from telemetry.internal.platform import linux_based_platform_backend from telemetry.internal.platform.power_monitor import cros_power_monitor from telemetry.internal.util import ps_util class CrosPlatformBackend( linux_based_platform_backend.LinuxBasedPlatformBackend): def __init__(self, presentation.device=None): super(CrosPlatformBackend, self).__init__(presentation.device) if presentation.device and not presentation.device.is_local: self._cri = cros_interface.CrOSInterface( presentation.device.host_name, presentation.device.ssh_port, presentation.device.ssh_identity) self._cri.TryLogin() else: self._cri = cros_interface.CrOSInterface() self._powermonitor = cros_power_monitor.CrosPowerMonitor(self) @classmethod def IsPlatformBackendForHost(cls): return util.IsRunningOnCrosDevice() @classmethod def SupportsDevice(cls, presentation.device): return isinstance(presentation.device, cros_device.CrOSDevice) @classmethod def CreatePlatformForDevice(cls, presentation.device, finder_options):
def testIsServiceRunningLocal(self): with cros_interface.CrOSInterface() as cri: self.assertTrue(cri.IsServiceRunning('dbus'))
def testExistsLocal(self): with cros_interface.CrOSInterface() as cri: self.assertTrue(cri.FileExistsOnDevice('/proc/cpuinfo')) self.assertTrue(cri.FileExistsOnDevice('/etc/passwd')) self.assertFalse(cri.FileExistsOnDevice('/etc/sdlfsdjflskfjsflj'))
def _GetCRI(self): remote = options_for_unittests.GetCopy().cros_remote remote_ssh_port = options_for_unittests.GetCopy().cros_remote_ssh_port return cros_interface.CrOSInterface( remote, remote_ssh_port, options_for_unittests.GetCopy().cros_ssh_identity)
def testTryLoginSuccess(self, mock_run_cmd): mock_run_cmd.return_value = ('root\n', '') cri = cros_interface.CrOSInterface( "testhostname", 22, options_for_unittests.GetCopy().cros_ssh_identity) cri.TryLogin() mock_run_cmd.assert_called_once_with(['echo', '$USER'], quiet=True)
def __init__(self, logged_in=True, extension_paths=None, autotest_ext=False, num_tries=3, extra_browser_args=None, clear_enterprise_policy=True, expect_policy_fetch=False, dont_override_profile=False, disable_gaia_services=True, disable_default_apps = True, auto_login=True, gaia_login=False, username=None, password=None, gaia_id=None, arc_mode=None, disable_arc_opt_in=True, init_network_controller=False, login_delay=0): """ Constructor of telemetry wrapper. @param logged_in: Regular user (True) or guest user (False). @param extension_paths: path of unpacked extension to install. @param autotest_ext: Load a component extension with privileges to invoke chrome.autotestPrivate. @param num_tries: Number of attempts to log in. @param extra_browser_args: Additional argument(s) to pass to the browser. It can be a string or a list. @param clear_enterprise_policy: Clear enterprise policy before logging in. @param expect_policy_fetch: Expect that chrome can reach the device management server and download policy. @param dont_override_profile: Don't delete cryptohome before login. Telemetry will output a warning with this option. @param disable_gaia_services: For enterprise autotests, this option may be used to enable policy fetch. @param disable_default_apps: For tests that exercise default apps. @param auto_login: Does not login automatically if this is False. Useful if you need to examine oobe. @param gaia_login: Logs in to real gaia. @param username: Log in using this username instead of the default. @param password: Log in using this password instead of the default. @param gaia_id: Log in using this gaia_id instead of the default. @param arc_mode: How ARC instance should be started. Default is to not start. @param disable_arc_opt_in: For opt in flow autotest. This option is used to disable the arc opt in flow. @param login_delay: Time for idle in login screen to simulate the time required for password typing. """ self._autotest_ext_path = None # Force autotest extension if we need enable Play Store. if (utils.is_arc_available() and (arc_util.should_start_arc(arc_mode) or not disable_arc_opt_in)): autotest_ext = True if extension_paths is None: extension_paths = [] if autotest_ext: self._autotest_ext_path = os.path.join(os.path.dirname(__file__), 'autotest_private_ext') extension_paths.append(self._autotest_ext_path) finder_options = browser_options.BrowserFinderOptions() if utils.is_arc_available() and arc_util.should_start_arc(arc_mode): if disable_arc_opt_in: finder_options.browser_options.AppendExtraBrowserArgs( arc_util.get_extra_chrome_flags()) logged_in = True self._browser_type = (self.BROWSER_TYPE_LOGIN if logged_in else self.BROWSER_TYPE_GUEST) finder_options.browser_type = self.browser_type if extra_browser_args: finder_options.browser_options.AppendExtraBrowserArgs( extra_browser_args) # finder options must be set before parse_args(), browser options must # be set before Create(). # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit # autotest debug logs finder_options.verbosity = 2 finder_options.CreateParser().parse_args(args=[]) b_options = finder_options.browser_options b_options.disable_component_extensions_with_background_pages = False b_options.create_browser_with_oobe = True b_options.clear_enterprise_policy = clear_enterprise_policy b_options.dont_override_profile = dont_override_profile b_options.disable_gaia_services = disable_gaia_services b_options.disable_default_apps = disable_default_apps b_options.disable_component_extensions_with_background_pages = disable_default_apps b_options.disable_background_networking = False b_options.expect_policy_fetch = expect_policy_fetch b_options.auto_login = auto_login b_options.gaia_login = gaia_login b_options.login_delay = login_delay if utils.is_arc_available() and not disable_arc_opt_in: arc_util.set_browser_options_for_opt_in(b_options) self.username = b_options.username if username is None else username self.password = b_options.password if password is None else password self.username = NormalizeEmail(self.username) b_options.username = self.username b_options.password = self.password self.gaia_id = b_options.gaia_id if gaia_id is None else gaia_id b_options.gaia_id = self.gaia_id self.arc_mode = arc_mode if logged_in: extensions_to_load = b_options.extensions_to_load for path in extension_paths: extension = extension_to_load.ExtensionToLoad( path, self.browser_type) extensions_to_load.append(extension) self._extensions_to_load = extensions_to_load # Turn on collection of Chrome coredumps via creation of a magic file. # (Without this, Chrome coredumps are trashed.) open(constants.CHROME_CORE_MAGIC_FILE, 'w').close() self._browser_to_create = browser_finder.FindBrowser( finder_options) self._browser_to_create.SetUpEnvironment(b_options) for i in range(num_tries): try: self._browser = self._browser_to_create.Create() self._browser_pid = \ cros_interface.CrOSInterface().GetChromePid() if utils.is_arc_available(): if disable_arc_opt_in: if arc_util.should_start_arc(arc_mode): arc_util.enable_play_store(self.autotest_ext, True) else: arc_util.opt_in(self.browser, self.autotest_ext) arc_util.post_processing_after_browser(self) break except exceptions.LoginException as e: logging.error('Timed out logging in, tries=%d, error=%s', i, repr(e)) if i == num_tries-1: raise if init_network_controller: self._browser.platform.network_controller.Open()