OLD | NEW |
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging, os, re, time, utils | 5 import logging, os, re, utils |
6 from autotest_lib.client.bin import chromeos_constants, test | 6 from autotest_lib.client.bin import chromeos_constants, test |
7 from autotest_lib.client.common_lib import error | 7 from autotest_lib.client.common_lib import error |
8 | 8 |
9 def __get_mount_parts(expected_mountpt = chromeos_constants.CRYPTOHOME_MOUNT_PT, | 9 |
| 10 CRYPTOHOME_CMD = '/usr/sbin/cryptohome' |
| 11 |
| 12 class ChromiumOSError(error.InstallError): |
| 13 """Generic error for ChromiumOS-specific exceptions.""" |
| 14 pass |
| 15 |
| 16 |
| 17 def __run_cmd(cmd): |
| 18 return utils.system_output(cmd + ' 2>&1', retain_output=True, |
| 19 ignore_status=True).strip() |
| 20 |
| 21 |
| 22 def get_user_hash(user): |
| 23 """Get the hash for the test user account.""" |
| 24 hash_cmd = CRYPTOHOME_CMD + ' --action=obfuscate_user --user=%s' % user |
| 25 return __run_cmd(hash_cmd) |
| 26 |
| 27 |
| 28 def remove_vault(user): |
| 29 """Remove the test user account.""" |
| 30 logging.debug('user is %s', user) |
| 31 user_hash = get_user_hash(user) |
| 32 logging.debug('Removing vault for user %s - %s' % (user, user_hash)) |
| 33 cmd = CRYPTOHOME_CMD + ' --action=remove --force --user=%s' % user |
| 34 __run_cmd(cmd) |
| 35 # Ensure that the user directory does not exist |
| 36 if os.path.exists(os.path.join('/home/.shadow/', user_hash)): |
| 37 raise ChromiumOSError('Cryptohome could not remove the test user.') |
| 38 |
| 39 |
| 40 def mount_vault(user, password, create=False): |
| 41 cmd = (CRYPTOHOME_CMD + ' --action=mount --user=%s --password=%s' % |
| 42 (user, password)) |
| 43 if create: |
| 44 cmd += ' --create' |
| 45 __run_cmd(cmd) |
| 46 # Ensure that the user directory exists |
| 47 user_hash = get_user_hash(user) |
| 48 if not os.path.exists(os.path.join('/home/.shadow/', user_hash)): |
| 49 raise ChromiumOSError('Cryptohome vault not found after mount.') |
| 50 # Ensure that the user directory is mounted |
| 51 if not is_mounted(allow_fail=True): |
| 52 raise ChromiumOSError('Cryptohome created the user but did not mount.') |
| 53 |
| 54 |
| 55 def test_auth(user, password): |
| 56 cmd = (CRYPTOHOME_CMD + ' --action=test_auth --user=%s --password=%s' % |
| 57 (user, password)) |
| 58 return 'Authentication succeeded' in __run_cmd(cmd) |
| 59 |
| 60 |
| 61 def unmount_vault(): |
| 62 """Unmount the directory.""" |
| 63 cmd = (CRYPTOHOME_CMD + ' --action=unmount') |
| 64 __run_cmd(cmd) |
| 65 # Ensure that the user directory is not mounted |
| 66 if is_mounted(allow_fail=True): |
| 67 raise ChromiumOSError('Cryptohome did not unmount the user.') |
| 68 |
| 69 |
| 70 def __get_mount_parts(expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT, |
10 allow_fail = False): | 71 allow_fail = False): |
11 mount_line = utils.system_output( | 72 mount_line = utils.system_output( |
12 'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt, | 73 'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt, |
13 ignore_status = allow_fail) | 74 ignore_status = allow_fail) |
14 return mount_line.split() | 75 return mount_line.split() |
15 | 76 |
16 | 77 |
17 | |
18 def is_mounted(device=chromeos_constants.CRYPTOHOME_DEVICE_REGEX, | 78 def is_mounted(device=chromeos_constants.CRYPTOHOME_DEVICE_REGEX, |
19 expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT, | 79 expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT, |
20 allow_fail=False): | 80 allow_fail=False): |
21 mount_line = utils.system_output( | 81 mount_line = utils.system_output( |
22 'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt, | 82 'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt, |
23 ignore_status=allow_fail) | 83 ignore_status=allow_fail) |
24 mount_parts = mount_line.split() | 84 mount_parts = mount_line.split() |
25 return len(mount_parts) > 0 and re.match(device, mount_parts[0]) | 85 return len(mount_parts) > 0 and re.match(device, mount_parts[0]) |
26 | 86 |
27 | 87 |
28 def is_mounted_on_tmpfs(device = chromeos_constants.CRYPTOHOME_INCOGNITO, | 88 def is_mounted_on_tmpfs(device = chromeos_constants.CRYPTOHOME_INCOGNITO, |
29 expected_mountpt = | 89 expected_mountpt = |
30 chromeos_constants.CRYPTOHOME_MOUNT_PT, | 90 chromeos_constants.CRYPTOHOME_MOUNT_PT, |
31 allow_fail = False): | 91 allow_fail = False): |
32 mount_parts = __get_mount_parts(device, allow_fail) | 92 mount_parts = __get_mount_parts(device, allow_fail) |
33 return (len(mount_parts) > 2 and device == mount_parts[0] and | 93 return (len(mount_parts) > 2 and device == mount_parts[0] and |
34 'tmpfs' == mount_parts[2]) | 94 'tmpfs' == mount_parts[2]) |
OLD | NEW |