platform_CryptohomeNonDirs: fixify.

1) Use CryptohomeProxy, not Cryptohome;
2) Move the existing path out of the way, since it's created at startup time.

BUG=chromium-os:20778
TEST=Yes

Change-Id: I40a6f2d50954b55758b96c9886050387d3001cd1
Signed-off-by: Elly Jones <ellyjones@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/10976
Reviewed-by: Will Drewry <wad@chromium.org>
Reviewed-by: Gaurav Shah <gauravsh@chromium.org>
diff --git a/client/site_tests/platform_CryptohomeNonDirs/platform_CryptohomeNonDirs.py b/client/site_tests/platform_CryptohomeNonDirs/platform_CryptohomeNonDirs.py
index 926073f..57bb07a 100644
--- a/client/site_tests/platform_CryptohomeNonDirs/platform_CryptohomeNonDirs.py
+++ b/client/site_tests/platform_CryptohomeNonDirs/platform_CryptohomeNonDirs.py
@@ -16,8 +16,19 @@
         if self.cryptohome_proxy.mount(user, 'test', create=True):
             raise error.TestFail('Mount failed for %s' % user)
 
+    def replace(self, src, dest):
+        """Replaces dest with src.
+
+        Replaces the dirent at dest with the dirent at src, deleting dest first
+        if necessary. This is distinguished from os.rename() or shutil.move() by
+        the fact that it works even if dest is a non-directory dirent.
+        """
+        if os.path.exists(dest):
+            os.remove(dest)
+        os.rename(src, dest)
+
     def run_once(self):
-        self.cryptohome_proxy = cryptohome.Cryptohome()
+        self.cryptohome_proxy = cryptohome.CryptohomeProxy()
 
         # Leaf element of user path is non-dir.
         user = utils.random_username()
@@ -41,18 +52,22 @@
         user = utils.random_username()
         path = cryptohome.user_path(user)
         parent_path = os.path.dirname(path)
-        utils.open_write_close(path, '')
+        os.rename(parent_path, parent_path + '.old')
         try:
+            utils.open_write_close(parent_path, '')
             self.require_mount_fail(user)
         finally:
-            os.remove(parent_path)
+            # We can't just rely on the rename() to blow away the file -
+            # rename() will refuse to rename directories to non-directory names.
+            self.replace(parent_path + '.old', parent_path)
 
         # Non-leaf element of system path is non-dir.
         user = utils.random_username()
         path = cryptohome.system_path(user)
         parent_path = os.path.dirname(path)
-        utils.open_write_close(parent_path, 'w')
+        os.rename(parent_path, parent_path + '.old')
         try:
+            utils.open_write_close(parent_path, '')
             self.require_mount_fail(user)
         finally:
-            os.remove(parent_path)
+            self.replace(parent_path + '.old', parent_path)