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