1# Copyright 2014 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 5import logging 6import urllib2 7import os 8 9from telemetry.core import exceptions 10from telemetry.core import util 11from telemetry import decorators 12from telemetry.internal.backends.chrome import cros_test_case 13 14 15class CrOSCryptohomeTest(cros_test_case.CrOSTestCase): 16 @decorators.Enabled('chromeos') 17 def testCryptohome(self): 18 """Verifies cryptohome mount status for regular and guest user and when 19 logged out""" 20 with self._CreateBrowser() as b: 21 self.assertEquals(1, len(b.tabs)) 22 self.assertTrue(b.tabs[0].url) 23 self.assertTrue(self._IsCryptohomeMounted()) 24 25 # TODO(achuith): Remove dependency on /home/chronos/user. 26 chronos_fs = self._cri.FilesystemMountedAt('/home/chronos/user') 27 self.assertTrue(chronos_fs) 28 if self._is_guest: 29 self.assertEquals(chronos_fs, 'guestfs') 30 else: 31 crypto_fs = self._cri.FilesystemMountedAt( 32 self._cri.CryptohomePath(self._username)) 33 self.assertEquals(crypto_fs, chronos_fs) 34 35 self.assertFalse(self._IsCryptohomeMounted()) 36 self.assertEquals(self._cri.FilesystemMountedAt('/home/chronos/user'), 37 '/dev/mapper/encstateful') 38 39 40class CrOSLoginTest(cros_test_case.CrOSTestCase): 41 def _GetCredentials(self): 42 """Read username and password from credentials.txt. The file is a single 43 line of the format username:password""" 44 username = None 45 password = None 46 credentials_file = os.path.join(os.path.dirname(__file__), 47 'credentials.txt') 48 if os.path.exists(credentials_file): 49 with open(credentials_file) as f: 50 username, password = f.read().strip().split(':') 51 # Remove dots. 52 username = username.replace('.', '') 53 # Canonicalize. 54 if username.find('@') == -1: 55 username += '@gmail.com' 56 return (username, password) 57 58 @decorators.Enabled('chromeos') 59 def testLoginStatus(self): 60 """Tests autotestPrivate.loginStatus""" 61 if self._is_guest: 62 return 63 with self._CreateBrowser(autotest_ext=True) as b: 64 login_status = self._GetLoginStatus(b) 65 self.assertEquals(type(login_status), dict) 66 67 self.assertEquals(not self._is_guest, login_status['isRegularUser']) 68 self.assertEquals(self._is_guest, login_status['isGuest']) 69 self.assertEquals(login_status['email'], self._username) 70 self.assertFalse(login_status['isScreenLocked']) 71 72 @decorators.Enabled('chromeos') 73 def testLogout(self): 74 """Tests autotestPrivate.logout""" 75 if self._is_guest: 76 return 77 with self._CreateBrowser(autotest_ext=True) as b: 78 extension = self._GetAutotestExtension(b) 79 try: 80 extension.ExecuteJavaScript('chrome.autotestPrivate.logout();') 81 except exceptions.Error: 82 pass 83 util.WaitFor(lambda: not self._IsCryptohomeMounted(), 20) 84 85 @decorators.Disabled('all') 86 def testGaiaLogin(self): 87 """Tests gaia login. Use credentials in credentials.txt if it exists, 88 otherwise use powerloadtest.""" 89 if self._is_guest: 90 return 91 username, password = self._GetCredentials() 92 if not username or not password: 93 username = 'powerloadtest@gmail.com' 94 password = urllib2.urlopen( 95 'https://sites.google.com/a/chromium.org/dev/chromium-os/testing/' 96 'power-testing/pltp/pltp').read().rstrip() 97 with self._CreateBrowser(gaia_login=True, 98 username=username, 99 password=password): 100 self.assertTrue(util.WaitFor(self._IsCryptohomeMounted, 10)) 101 102 @decorators.Enabled('chromeos') 103 def testEnterpriseEnroll(self): 104 """Tests enterprise enrollment. Credentials are expected to be found in a 105 credentials.txt file. The account must be from an enterprise domain and 106 have device enrollment permission. The device must be unowned.""" 107 if self._is_guest: 108 return 109 110 username, password = self._GetCredentials() 111 if not username or not password: 112 return 113 # Enroll the device. 114 with self._CreateBrowser(auto_login=False) as browser: 115 browser.oobe.NavigateGaiaLogin(username, password, 116 enterprise_enroll=True, 117 for_user_triggered_enrollment=True) 118 119 # Check for the existence of the device policy file. 120 self.assertTrue(util.WaitFor(lambda: self._cri.FileExistsOnDevice( 121 '/home/.shadow/install_attributes.pb'), 15)) 122 123 124class CrOSScreenLockerTest(cros_test_case.CrOSTestCase): 125 def _IsScreenLocked(self, browser): 126 return self._GetLoginStatus(browser)['isScreenLocked'] 127 128 def _LockScreen(self, browser): 129 self.assertFalse(self._IsScreenLocked(browser)) 130 131 extension = self._GetAutotestExtension(browser) 132 self.assertTrue(extension.EvaluateJavaScript( 133 "typeof chrome.autotestPrivate.lockScreen == 'function'")) 134 logging.info('Locking screen') 135 extension.ExecuteJavaScript('chrome.autotestPrivate.lockScreen();') 136 137 logging.info('Waiting for the lock screen') 138 def ScreenLocked(): 139 return (browser.oobe_exists and 140 browser.oobe.EvaluateJavaScript("typeof Oobe == 'function'") and 141 browser.oobe.EvaluateJavaScript( 142 "typeof Oobe.authenticateForTesting == 'function'")) 143 util.WaitFor(ScreenLocked, 10) 144 self.assertTrue(self._IsScreenLocked(browser)) 145 146 def _AttemptUnlockBadPassword(self, browser): 147 logging.info('Trying a bad password') 148 def ErrorBubbleVisible(): 149 return not browser.oobe.EvaluateJavaScript(''' 150 document.getElementById('bubble').hidden 151 ''') 152 self.assertFalse(ErrorBubbleVisible()) 153 browser.oobe.ExecuteJavaScript(''' 154 Oobe.authenticateForTesting('%s', 'bad'); 155 ''' % self._username) 156 util.WaitFor(ErrorBubbleVisible, 10) 157 self.assertTrue(self._IsScreenLocked(browser)) 158 159 def _UnlockScreen(self, browser): 160 logging.info('Unlocking') 161 browser.oobe.ExecuteJavaScript(''' 162 Oobe.authenticateForTesting('%s', '%s'); 163 ''' % (self._username, self._password)) 164 util.WaitFor(lambda: not browser.oobe_exists, 10) 165 self.assertFalse(self._IsScreenLocked(browser)) 166 167 @decorators.Disabled('all') 168 def testScreenLock(self): 169 """Tests autotestPrivate.screenLock""" 170 if self._is_guest: 171 return 172 with self._CreateBrowser(autotest_ext=True) as browser: 173 self._LockScreen(browser) 174 self._AttemptUnlockBadPassword(browser) 175 self._UnlockScreen(browser) 176