OLD | NEW |
(Empty) | |
| 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 |
| 5 import logging |
| 6 import os |
| 7 import sys |
| 8 |
| 9 import pyauto_functional # Must come before pyauto (and thus, policy_base). |
| 10 import policy_base |
| 11 |
| 12 sys.path.append('/usr/local') # Required to import autotest libs. |
| 13 from autotest.cros import constants |
| 14 from autotest.cros import cryptohome |
| 15 |
| 16 |
| 17 class ChromeosEphemeral(policy_base.PolicyTestBase): |
| 18 """Tests a policy that makes all users except the owner ephemeral. |
| 19 |
| 20 When this policy is enabled, no persistent information in the form of |
| 21 cryptohome shadow directories or local state prefs should be created for |
| 22 users. Additionally, any persistent information previously accumulated should |
| 23 be cleared when a user first logs in after enabling the policy.""" |
| 24 |
| 25 def _SetDevicePolicyAndOwner(self, ephemeral_users_enabled, owner_index): |
| 26 """Sets device policy and owner. |
| 27 |
| 28 TODO(bartfab): Ensure Login still works after crosbug.com/20709 is fixed. |
| 29 The show_user_names policy is set to False to ensure that even if the local |
| 30 state is not being automatically cleared, the login screen never shows user |
| 31 pods. This is required by the Login browser automation call. |
| 32 """ |
| 33 self.SetDevicePolicy( |
| 34 device_policy={'ephemeral_users_enabled': ephemeral_users_enabled, |
| 35 'show_user_names': False}, |
| 36 owner=self._usernames[owner_index]) |
| 37 |
| 38 def _DoesVaultDirectoryExist(self, user_index): |
| 39 user_hash = cryptohome.get_user_hash(self._usernames[user_index]) |
| 40 return os.path.exists(os.path.join(constants.SHADOW_ROOT, user_hash)) |
| 41 |
| 42 def _AssertLocalStatePrefsSet(self, user_indexes): |
| 43 expected = sorted([self._usernames[index] for index in user_indexes]) |
| 44 # The OAuthTokenStatus pref is populated asynchronously. Checking whether it |
| 45 # is set would lead to an ugly race. |
| 46 for pref in ['LoggedInUsers', 'UserImages', 'UserDisplayEmail', ]: |
| 47 actual = sorted(self.GetLocalStatePrefsInfo().Prefs(pref)) |
| 48 self.assertEqual(actual, expected, |
| 49 msg='Expected to find prefs in local state for users.') |
| 50 |
| 51 def _AssertLocalStatePrefsEmpty(self): |
| 52 for pref in ['LoggedInUsers', |
| 53 'UserImages', |
| 54 'UserDisplayEmail', |
| 55 'OAuthTokenStatus']: |
| 56 self.assertFalse(self.GetLocalStatePrefsInfo().Prefs(pref), |
| 57 msg='Expected to not find prefs in local state for any user.') |
| 58 |
| 59 def _AssertVaultDirectoryExists(self, user_index): |
| 60 self.assertTrue(self._DoesVaultDirectoryExist(user_index=user_index), |
| 61 msg='Expected vault shadow directory to exist.') |
| 62 |
| 63 def _AssertVaultDirectoryDoesNotExist(self, user_index): |
| 64 self.assertFalse(self._DoesVaultDirectoryExist(user_index=user_index), |
| 65 msg='Expected vault shadow directory to not exist.') |
| 66 |
| 67 def _AssertVaultMounted(self, user_index, ephemeral): |
| 68 if ephemeral: |
| 69 device_regex = constants.CRYPTOHOME_DEV_REGEX_REGULAR_USER_EPHEMERAL |
| 70 fs_regex = constants.CRYPTOHOME_FS_REGEX_TMPFS |
| 71 else: |
| 72 device_regex = constants.CRYPTOHOME_DEV_REGEX_REGULAR_USER_SHADOW |
| 73 fs_regex = constants.CRYPTOHOME_FS_REGEX_ANY |
| 74 self.assertTrue( |
| 75 cryptohome.is_vault_mounted(device_regex=device_regex, |
| 76 fs_regex=fs_regex, |
| 77 user=self._usernames[user_index], |
| 78 allow_fail=True), |
| 79 msg='Expected vault backed by %s to be mounted.' % |
| 80 'tmpfs' if ephemeral else 'shadow directory') |
| 81 |
| 82 def _AssertNoVaultMounted(self): |
| 83 self.assertFalse(cryptohome.is_vault_mounted(allow_fail=True), |
| 84 msg='Did not expect any vault to be mounted.') |
| 85 |
| 86 def Login(self, user_index): |
| 87 self.assertFalse(self.GetLoginInfo()['is_logged_in'], |
| 88 msg='Expected to be logged out.') |
| 89 policy_base.PolicyTestBase.Login(self, |
| 90 self._usernames[user_index], |
| 91 self._passwords[user_index]) |
| 92 self.assertTrue(self.GetLoginInfo()['is_logged_in'], |
| 93 msg='Expected to be logged in.') |
| 94 |
| 95 def ExtraChromeFlags(self): |
| 96 """Sets up Chrome to skip OOBE. |
| 97 |
| 98 TODO(bartfab): Ensure OOBE is still skipped when crosbug.com/20709 is fixed. |
| 99 Disabling automatic clearing of the local state has the curious side effect |
| 100 of removing a flag that disables OOBE. This method adds back the flag. |
| 101 """ |
| 102 flags = policy_base.PolicyTestBase.ExtraChromeFlags(self) |
| 103 flags.append('--login-screen=login') |
| 104 return flags |
| 105 |
| 106 def setUp(self): |
| 107 policy_base.PolicyTestBase.setUp(self) |
| 108 # TODO(bartfab): Remove this after crosbug.com/20709 is fixed. |
| 109 # Try to disable automatic clearing of the local state. |
| 110 self.TryToDisableLocalStateAutoClearingOnChromeOS() |
| 111 self._local_state_auto_clearing = \ |
| 112 self.IsLocalStateAutoClearingEnabledOnChromeOS() |
| 113 if not self._local_state_auto_clearing: |
| 114 # Prevent the inherited Logout() method from cleaning up /home/chronos |
| 115 # as this also clears the local state. |
| 116 self.set_clear_profile(False) |
| 117 |
| 118 credentials = (self.GetPrivateInfo()['prod_enterprise_test_user'], |
| 119 self.GetPrivateInfo()['prod_enterprise_executive_user'], |
| 120 self.GetPrivateInfo()['prod_enterprise_sales_user']) |
| 121 self._usernames = [credential['username'] for credential in credentials] |
| 122 self._passwords = [credential['password'] for credential in credentials] |
| 123 |
| 124 def tearDown(self): |
| 125 # TODO(bartfab): Remove this after crosbug.com/20709 is fixed. |
| 126 # Try to re-enable automatic clearing of the local state and /home/chronos. |
| 127 if not self._local_state_auto_clearing: |
| 128 self.TryToEnableLocalStateAutoClearingOnChromeOS() |
| 129 self.set_clear_profile(True) |
| 130 policy_base.PolicyTestBase.tearDown(self) |
| 131 |
| 132 def testLoginAsOwnerIsNotEphemeral(self): |
| 133 """Checks that the owner does not become ephemeral.""" |
| 134 self._SetDevicePolicyAndOwner(ephemeral_users_enabled=True, owner_index=0) |
| 135 |
| 136 self.Login(user_index=0) |
| 137 # TODO(bartfab): Remove this when crosbug.com/20709 is fixed. |
| 138 if self._local_state_auto_clearing: |
| 139 self._AssertLocalStatePrefsSet(user_indexes=[0]) |
| 140 self._AssertVaultDirectoryExists(user_index=0) |
| 141 self._AssertVaultMounted(user_index=0, ephemeral=False) |
| 142 self.Logout() |
| 143 # TODO(bartfab): Make this unconditional when crosbug.com/20709 is fixed. |
| 144 if not self._local_state_auto_clearing: |
| 145 self._AssertLocalStatePrefsSet(user_indexes=[0]) |
| 146 self._AssertVaultDirectoryExists(user_index=0) |
| 147 self._AssertNoVaultMounted() |
| 148 |
| 149 def testLoginAsNonOwnerIsEphemeral(self): |
| 150 """Checks that a non-owner user does become ephemeral.""" |
| 151 self._SetDevicePolicyAndOwner(ephemeral_users_enabled=True, owner_index=0) |
| 152 |
| 153 self.Login(user_index=1) |
| 154 # TODO(bartfab): Remove this when crosbug.com/20709 is fixed. |
| 155 if self._local_state_auto_clearing: |
| 156 self._AssertLocalStatePrefsEmpty() |
| 157 self._AssertVaultDirectoryDoesNotExist(user_index=1) |
| 158 self._AssertVaultMounted(user_index=1, ephemeral=True) |
| 159 self.Logout() |
| 160 # TODO(bartfab): Make this unconditional when crosbug.com/20709 is fixed. |
| 161 if not self._local_state_auto_clearing: |
| 162 self._AssertLocalStatePrefsEmpty() |
| 163 |
| 164 self._AssertVaultDirectoryDoesNotExist(user_index=1) |
| 165 self._AssertNoVaultMounted() |
| 166 |
| 167 def testEnablingEphemeralUsersCleansUp(self): |
| 168 """Checks that persistent information is cleared.""" |
| 169 self._SetDevicePolicyAndOwner(ephemeral_users_enabled=False, owner_index=0) |
| 170 |
| 171 self.Login(user_index=0) |
| 172 # TODO(bartfab): Remove this when crosbug.com/20709 is fixed. |
| 173 if self._local_state_auto_clearing: |
| 174 self._AssertLocalStatePrefsSet(user_indexes=[0]) |
| 175 self.Logout() |
| 176 # TODO(bartfab): Make this unconditional when crosbug.com/20709 is fixed. |
| 177 if not self._local_state_auto_clearing: |
| 178 self._AssertLocalStatePrefsSet(user_indexes=[0]) |
| 179 |
| 180 self.Login(user_index=1) |
| 181 # TODO(bartfab): Remove this when crosbug.com/20709 is fixed. |
| 182 if self._local_state_auto_clearing: |
| 183 self._AssertLocalStatePrefsSet(user_indexes=[1]) |
| 184 self.Logout() |
| 185 # TODO(bartfab): Make this unconditional when crosbug.com/20709 is fixed. |
| 186 if not self._local_state_auto_clearing: |
| 187 self._AssertLocalStatePrefsSet(user_indexes=[0, 1]) |
| 188 |
| 189 self.Login(user_index=2) |
| 190 # TODO(bartfab): Remove this when crosbug.com/20709 is fixed. |
| 191 if self._local_state_auto_clearing: |
| 192 self._AssertLocalStatePrefsSet(user_indexes=[2]) |
| 193 self.Logout() |
| 194 # TODO(bartfab): Make this unconditional when crosbug.com/20709 is fixed. |
| 195 if not self._local_state_auto_clearing: |
| 196 self._AssertLocalStatePrefsSet(user_indexes=[0, 1, 2]) |
| 197 |
| 198 self._AssertVaultDirectoryExists(user_index=0) |
| 199 self._AssertVaultDirectoryExists(user_index=1) |
| 200 self._AssertVaultDirectoryExists(user_index=2) |
| 201 |
| 202 self._SetDevicePolicyAndOwner(ephemeral_users_enabled=True, owner_index=0) |
| 203 |
| 204 self.Login(user_index=1) |
| 205 # TODO(bartfab): Remove this when crosbug.com/20709 is fixed. |
| 206 if self._local_state_auto_clearing: |
| 207 self._AssertLocalStatePrefsEmpty() |
| 208 self._AssertVaultMounted(user_index=1, ephemeral=True) |
| 209 self.Logout() |
| 210 |
| 211 # TODO(bartfab): Make this unconditional when crosbug.com/20709 is fixed. |
| 212 if not self._local_state_auto_clearing: |
| 213 self._AssertLocalStatePrefsSet(user_indexes=[0]) |
| 214 |
| 215 self._AssertVaultDirectoryExists(user_index=0) |
| 216 self._AssertVaultDirectoryDoesNotExist(user_index=1) |
| 217 self._AssertVaultDirectoryDoesNotExist(user_index=2) |
| 218 |
| 219 |
| 220 if __name__ == '__main__': |
| 221 pyauto_functional.Main() |
OLD | NEW |