1# Copyright (c) 2011 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 5import os 6 7from autotest_lib.client.bin import test, utils 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.cros import cryptohome 10 11class platform_CryptohomeNonDirs(test.test): 12 version = 1 13 14 def require_mount_fail(self, user): 15 try: 16 cryptohome.mount_vault(user, 'test', create=True) 17 except: 18 pass 19 else: 20 raise error.TestFail('Mount succeeded for %s' % user) 21 22 23 def replace(self, src, dest): 24 """Replaces dest with src. 25 26 Replaces the dirent at dest with the dirent at src, deleting dest first 27 if necessary. This is distinguished from os.rename() or shutil.move() by 28 the fact that it works even if dest is a non-directory dirent. 29 """ 30 if os.path.exists(dest): 31 os.remove(dest) 32 os.rename(src, dest) 33 34 def run_once(self): 35 # Leaf element of user path is non-dir. 36 user = utils.random_username() 37 path = cryptohome.user_path(user) 38 utils.open_write_close(path, '') 39 try: 40 self.require_mount_fail(user) 41 finally: 42 os.remove(path) 43 44 # Leaf element of system path is non-dir. 45 user = utils.random_username() 46 path = cryptohome.system_path(user) 47 os.symlink('/etc', path) 48 try: 49 self.require_mount_fail(user) 50 finally: 51 os.remove(path) 52 53 # Non-leaf element of user path is non-dir. 54 user = utils.random_username() 55 path = cryptohome.user_path(user) 56 parent_path = os.path.dirname(path) 57 os.rename(parent_path, parent_path + '.old') 58 try: 59 utils.open_write_close(parent_path, '') 60 self.require_mount_fail(user) 61 finally: 62 # We can't just rely on the rename() to blow away the file - 63 # rename() will refuse to rename directories to non-directory names. 64 self.replace(parent_path + '.old', parent_path) 65 66 # Non-leaf element of system path is non-dir. 67 user = utils.random_username() 68 path = cryptohome.system_path(user) 69 parent_path = os.path.dirname(path) 70 os.rename(parent_path, parent_path + '.old') 71 try: 72 utils.open_write_close(parent_path, '') 73 self.require_mount_fail(user) 74 finally: 75 self.replace(parent_path + '.old', parent_path) 76