1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 import json 5 import logging 6 import os 7 import unittest 8 9 from telemetry.core import browser_finder 10 from telemetry.core import exceptions 11 from telemetry.core import extension_to_load 12 from telemetry.core import util 13 from telemetry.core.backends.chrome import cros_interface 14 from telemetry.unittest import options_for_unittests 15 16 class CrOSAutoTest(unittest.TestCase): 17 def setUp(self): 18 options = options_for_unittests.GetCopy() 19 self._cri = cros_interface.CrOSInterface(options.cros_remote, 20 options.cros_ssh_identity) 21 self._is_guest = options.browser_type == 'cros-chrome-guest' 22 self._email = '' if self._is_guest else 'test (at] test.test' 23 24 def _IsCryptohomeMounted(self): 25 """Returns True if cryptohome is mounted""" 26 cryptohomeJSON, _ = self._cri.RunCmdOnDevice(['/usr/sbin/cryptohome', 27 '--action=status']) 28 cryptohomeStatus = json.loads(cryptohomeJSON) 29 return (cryptohomeStatus['mounts'] and 30 cryptohomeStatus['mounts'][0]['mounted']) 31 32 def _CreateBrowser(self, autotest_ext=False, auto_login=True): 33 """Finds and creates a browser for tests. if autotest_ext is True, 34 also loads the autotest extension""" 35 options = options_for_unittests.GetCopy() 36 37 if autotest_ext: 38 extension_path = os.path.join(os.path.dirname(__file__), 'autotest_ext') 39 self._load_extension = extension_to_load.ExtensionToLoad( 40 path=extension_path, 41 browser_type=options.browser_type, 42 is_component=True) 43 options.extensions_to_load = [self._load_extension] 44 45 browser_to_create = browser_finder.FindBrowser(options) 46 self.assertTrue(browser_to_create) 47 options.browser_options.create_browser_with_oobe = True 48 options.browser_options.auto_login = auto_login 49 b = browser_to_create.Create() 50 b.Start() 51 return b 52 53 def _GetAutotestExtension(self, browser): 54 """Returns the autotest extension instance""" 55 extension = browser.extensions[self._load_extension] 56 self.assertTrue(extension) 57 return extension 58 59 def _GetLoginStatus(self, browser): 60 extension = self._GetAutotestExtension(browser) 61 self.assertTrue(extension.EvaluateJavaScript( 62 "typeof('chrome.autotestPrivate') != 'undefined'")) 63 extension.ExecuteJavaScript(''' 64 window.__login_status = null; 65 chrome.autotestPrivate.loginStatus(function(s) { 66 window.__login_status = s; 67 }); 68 ''') 69 return util.WaitFor( 70 lambda: extension.EvaluateJavaScript('window.__login_status'), 10) 71 72 def testCryptohomeMounted(self): 73 """Verifies cryptohome mount status for regular and guest user and when 74 logged out""" 75 with self._CreateBrowser() as b: 76 self.assertEquals(1, len(b.tabs)) 77 self.assertTrue(b.tabs[0].url) 78 self.assertTrue(self._IsCryptohomeMounted()) 79 80 chronos_fs = self._cri.FilesystemMountedAt('/home/chronos/user') 81 self.assertTrue(chronos_fs) 82 if self._is_guest: 83 self.assertEquals(chronos_fs, 'guestfs') 84 else: 85 home, _ = self._cri.RunCmdOnDevice(['/usr/sbin/cryptohome-path', 86 'user', self._email]) 87 self.assertEquals(self._cri.FilesystemMountedAt(home.rstrip()), 88 chronos_fs) 89 90 self.assertFalse(self._IsCryptohomeMounted()) 91 self.assertEquals(self._cri.FilesystemMountedAt('/home/chronos/user'), 92 '/dev/mapper/encstateful') 93 94 def testLoginStatus(self): 95 """Tests autotestPrivate.loginStatus""" 96 with self._CreateBrowser(autotest_ext=True) as b: 97 login_status = self._GetLoginStatus(b) 98 self.assertEquals(type(login_status), dict) 99 100 self.assertEquals(not self._is_guest, login_status['isRegularUser']) 101 self.assertEquals(self._is_guest, login_status['isGuest']) 102 self.assertEquals(login_status['email'], self._email) 103 self.assertFalse(login_status['isScreenLocked']) 104 105 def _IsScreenLocked(self, browser): 106 return self._GetLoginStatus(browser)['isScreenLocked'] 107 108 def _LockScreen(self, browser): 109 self.assertFalse(self._IsScreenLocked(browser)) 110 111 extension = self._GetAutotestExtension(browser) 112 self.assertTrue(extension.EvaluateJavaScript( 113 "typeof chrome.autotestPrivate.lockScreen == 'function'")) 114 logging.info('Locking screen') 115 extension.ExecuteJavaScript('chrome.autotestPrivate.lockScreen();') 116 117 logging.info('Waiting for the lock screen') 118 def ScreenLocked(): 119 return (browser.oobe and 120 browser.oobe.EvaluateJavaScript("typeof Oobe == 'function'") and 121 browser.oobe.EvaluateJavaScript( 122 "typeof Oobe.authenticateForTesting == 'function'")) 123 util.WaitFor(ScreenLocked, 10) 124 self.assertTrue(self._IsScreenLocked(browser)) 125 126 def _AttemptUnlockBadPassword(self, browser): 127 logging.info('Trying a bad password') 128 def ErrorBubbleVisible(): 129 return not browser.oobe.EvaluateJavaScript(''' 130 document.getElementById('bubble').hidden 131 ''') 132 self.assertFalse(ErrorBubbleVisible()) 133 browser.oobe.ExecuteJavaScript(''' 134 Oobe.authenticateForTesting('test@test.test', 'bad'); 135 ''') 136 util.WaitFor(ErrorBubbleVisible, 10) 137 self.assertTrue(self._IsScreenLocked(browser)) 138 139 def _UnlockScreen(self, browser): 140 logging.info('Unlocking') 141 browser.oobe.ExecuteJavaScript(''' 142 Oobe.authenticateForTesting('test@test.test', ''); 143 ''') 144 util.WaitFor(lambda: not browser.oobe, 10) 145 self.assertFalse(self._IsScreenLocked(browser)) 146 147 def testScreenLock(self): 148 """Tests autotestPrivate.screenLock""" 149 with self._CreateBrowser(autotest_ext=True) as browser: 150 self._LockScreen(browser) 151 self._AttemptUnlockBadPassword(browser) 152 self._UnlockScreen(browser) 153 154 155 def testLogout(self): 156 """Tests autotestPrivate.logout""" 157 with self._CreateBrowser(autotest_ext=True) as b: 158 extension = self._GetAutotestExtension(b) 159 try: 160 extension.ExecuteJavaScript('chrome.autotestPrivate.logout();') 161 except (exceptions.BrowserConnectionGoneException, 162 exceptions.BrowserGoneException): 163 pass 164 util.WaitFor(lambda: not self._IsCryptohomeMounted(), 20) 165