blob: 60b0e7cfd5bf0dfe06f9b32819f2c4c172463b3a [file] [log] [blame]
Chris Masone5e06f182010-03-23 08:29:52 -07001# 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
3# found in the LICENSE file.
4
Sean Oe5d8fd02010-09-30 10:44:44 +02005import logging, os, re, utils
Chris Masonee7dd0162010-03-23 13:50:58 -07006from autotest_lib.client.bin import chromeos_constants, test
Chris Masone5e06f182010-03-23 08:29:52 -07007from autotest_lib.client.common_lib import error
8
Sean Oe5d8fd02010-09-30 10:44:44 +02009
10CRYPTOHOME_CMD = '/usr/sbin/cryptohome'
11
12class ChromiumOSError(error.InstallError):
13 """Generic error for ChromiumOS-specific exceptions."""
14 pass
15
16
17def __run_cmd(cmd):
18 return utils.system_output(cmd + ' 2>&1', retain_output=True,
19 ignore_status=True).strip()
20
21
22def 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
28def 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
40def 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
55def 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
61def 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
70def __get_mount_parts(expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT,
Sourav Poddar574bd622010-05-26 14:22:26 +053071 allow_fail = False):
Will Drewry81ad6162010-04-01 10:26:07 -050072 mount_line = utils.system_output(
73 'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt,
Sourav Poddar574bd622010-05-26 14:22:26 +053074 ignore_status = allow_fail)
75 return mount_line.split()
76
77
Frank Swiderski52653c32010-05-26 17:40:47 -070078def is_mounted(device=chromeos_constants.CRYPTOHOME_DEVICE_REGEX,
79 expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT,
80 allow_fail=False):
81 mount_line = utils.system_output(
82 'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt,
83 ignore_status=allow_fail)
84 mount_parts = mount_line.split()
85 return len(mount_parts) > 0 and re.match(device, mount_parts[0])
Sourav Poddar574bd622010-05-26 14:22:26 +053086
87
Frank Swiderski52653c32010-05-26 17:40:47 -070088def is_mounted_on_tmpfs(device = chromeos_constants.CRYPTOHOME_INCOGNITO,
Sourav Poddar574bd622010-05-26 14:22:26 +053089 expected_mountpt =
90 chromeos_constants.CRYPTOHOME_MOUNT_PT,
91 allow_fail = False):
92 mount_parts = __get_mount_parts(device, allow_fail)
93 return (len(mount_parts) > 2 and device == mount_parts[0] and
94 'tmpfs' == mount_parts[2])