1# 2# Copyright (C) 2016 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17from vts.runners.host import const 18from vts.testcases.kernel.ltp.shell_environment.definitions.base_definitions import check_setup_cleanup 19 20 21class PathPermission(check_setup_cleanup.CheckSetupCleanup): 22 """Class for check and set path permissions. 23 24 Attributes: 25 to_check: bool, whether or not to check the defined environment 26 requirement. Default: True 27 to_setup: bool, whether or not to setup the defined environment 28 requirement. Default: False 29 to_cleanup: bool, whether or not to cleanup the defined environment 30 requirement if it is set up by this class. Default: False 31 _paths: list string, target paths 32 _failed_paths: list of string, paths that don't have desired permissions 33 _permissions: list of int, desired permissions for each path 34 """ 35 36 def __init__(self, 37 paths=None, 38 permissions=None, 39 to_check=True, 40 to_setup=False, 41 to_cleanup=False): 42 self._paths = paths 43 self._permissions = permissions 44 self.to_check = to_check 45 self.to_setup = to_setup 46 self.to_cleanup = to_cleanup 47 48 def ValidateInputs(self): 49 """Validate inputs. 50 51 Check whether input lists is not null or empty list 52 or list containing empty string, and two lists containing same number 53 of items. If inputs are two single item, they will 54 be converted to single item lists. 55 """ 56 normalized = self.NormalizeInputLists(self._paths, self._permissions) 57 if not normalized: 58 return False 59 self._paths, self._permissions = normalized 60 self._failed_paths = zip(self._paths, self._permissions, 61 self._permissions) 62 63 return True 64 65 def Check(self): 66 commands = ["stat -c {}a {}".format('%', path) for path in self._paths] 67 results = self.ExecuteShellCommand(commands)[const.STDOUT] 68 69 self._failed_paths = [ 70 (path, permission, result) 71 for path, permission, result in zip(self._paths, self._permissions, 72 results) 73 if str(permission) != result 74 ] 75 76 if not self._failed_paths: 77 return True 78 79 self.note = ("Some paths do not have desired " 80 "permission: %s") % self._failed_paths 81 return False 82 83 def Setup(self): 84 commands = ["chmod {} {}".format(permission, path) 85 for (path, permission, result) in self._failed_paths] 86 # TODO: perhaps store or print failed setup paths 87 return not any(self.ExecuteShellCommand(commands)[const.EXIT_CODE]) 88 89 def Cleanup(self): 90 commands = ["chmod {} {}".format(original, path) 91 for (path, permission, original) in self._failed_paths] 92 return not any(self.ExecuteShellCommand(commands)[const.EXIT_CODE]) 93