Refactor platform_CryptohomeTestAuth + add testcase.

Also add functionality and cleanups to site_cryptohome lib.

Change-Id: I250e998ab8bcc6e35da314311a929c2b1c24c40d

Fix test.

Change-Id: Ie200a5bfcc1114c7528ba075ec53e5ea5e6ecf3d

Initial refactoring of platform_CryptohomeTestAuth

Change-Id: I819a62844b4b1d63c30f5b2fd4f497e808ea3cc9

BUG=
TEST=ran manually on my dev box.

Review URL: http://codereview.chromium.org/3525003
diff --git a/client/bin/site_cryptohome.py b/client/bin/site_cryptohome.py
index 303b4d6..60b0e7c 100644
--- a/client/bin/site_cryptohome.py
+++ b/client/bin/site_cryptohome.py
@@ -2,11 +2,72 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import logging, os, re, time, utils
+import logging, os, re, utils
 from autotest_lib.client.bin import chromeos_constants, test
 from autotest_lib.client.common_lib import error
 
-def __get_mount_parts(expected_mountpt = chromeos_constants.CRYPTOHOME_MOUNT_PT,
+
+CRYPTOHOME_CMD = '/usr/sbin/cryptohome'
+
+class ChromiumOSError(error.InstallError):
+    """Generic error for ChromiumOS-specific exceptions."""
+    pass
+
+
+def __run_cmd(cmd):
+    return utils.system_output(cmd + ' 2>&1', retain_output=True,
+                               ignore_status=True).strip()
+
+
+def get_user_hash(user):
+    """Get the hash for the test user account."""
+    hash_cmd = CRYPTOHOME_CMD + ' --action=obfuscate_user --user=%s' % user
+    return __run_cmd(hash_cmd)
+
+
+def remove_vault(user):
+    """Remove the test user account."""
+    logging.debug('user is %s', user)
+    user_hash = get_user_hash(user)
+    logging.debug('Removing vault for user %s - %s' % (user, user_hash))
+    cmd = CRYPTOHOME_CMD + ' --action=remove --force --user=%s' % user
+    __run_cmd(cmd)
+    # Ensure that the user directory does not exist
+    if os.path.exists(os.path.join('/home/.shadow/', user_hash)):
+        raise ChromiumOSError('Cryptohome could not remove the test user.')
+
+
+def mount_vault(user, password, create=False):
+    cmd = (CRYPTOHOME_CMD + ' --action=mount --user=%s --password=%s' %
+           (user, password))
+    if create:
+        cmd += ' --create'
+    __run_cmd(cmd)
+    # Ensure that the user directory exists
+    user_hash = get_user_hash(user)
+    if not os.path.exists(os.path.join('/home/.shadow/', user_hash)):
+        raise ChromiumOSError('Cryptohome vault not found after mount.')
+    # Ensure that the user directory is mounted
+    if not is_mounted(allow_fail=True):
+        raise ChromiumOSError('Cryptohome created the user but did not mount.')
+
+
+def test_auth(user, password):
+    cmd = (CRYPTOHOME_CMD + ' --action=test_auth --user=%s --password=%s' %
+           (user, password))
+    return 'Authentication succeeded' in __run_cmd(cmd)
+
+
+def unmount_vault():
+    """Unmount the directory."""
+    cmd = (CRYPTOHOME_CMD + ' --action=unmount')
+    __run_cmd(cmd)
+    # Ensure that the user directory is not mounted
+    if is_mounted(allow_fail=True):
+        raise ChromiumOSError('Cryptohome did not unmount the user.')
+
+
+def __get_mount_parts(expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT,
                       allow_fail = False):
     mount_line = utils.system_output(
         'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt,
@@ -14,7 +75,6 @@
     return mount_line.split()
 
 
-
 def is_mounted(device=chromeos_constants.CRYPTOHOME_DEVICE_REGEX,
                expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT,
                allow_fail=False):