def test_not_executable(self): file_path = os.path.join(self.tempdir, "foo") # On Windows a file created within Certbot will always have all permissions to the # Administrators group set. Since the unit tests are typically executed under elevated # privileges, it means that current user will always have effective execute rights on the # hook script, and so the test will fail. To prevent that and represent a file created # outside Certbot as typically a hook file is, we mock the _generate_dacl function in # certbot.compat.filesystem to give rights only to the current user. This implies removing # all ACEs except the first one from the DACL created by original _generate_dacl function. from certbot.compat.filesystem import _generate_dacl def _execute_mock(user_sid, mode): dacl = _generate_dacl(user_sid, mode) for _ in range(1, dacl.GetAceCount()): dacl.DeleteAce( 1 ) # DeleteAce dynamically updates the internal index mapping. return dacl # create a non-executable file with mock.patch("certbot.compat.filesystem._generate_dacl", side_effect=_execute_mock): os.close( filesystem.open(file_path, os.O_CREAT | os.O_WRONLY, 0o666)) self.assertFalse(filesystem.is_executable(file_path))
def exe_exists(exe): """Determine whether path/name refers to an executable. :param str exe: Executable path or name :returns: If exe is a valid executable :rtype: bool """ path, _ = os.path.split(exe) if path: return filesystem.is_executable(exe) for path in os.environ["PATH"].split(os.pathsep): if filesystem.is_executable(os.path.join(path, exe)): return True return False
def list_hooks(dir_path): """List paths to all hooks found in dir_path in sorted order. :param str dir_path: directory to search :returns: `list` of `str` :rtype: sorted list of absolute paths to executables in dir_path """ allpaths = (os.path.join(dir_path, f) for f in os.listdir(dir_path)) hooks = [path for path in allpaths if filesystem.is_executable(path) and not path.endswith('~')] return sorted(hooks)
def test_not_found(self, mock_access, mock_isfile): with _fix_windows_runtime(): mock_access.return_value = True mock_isfile.return_value = False self.assertFalse(filesystem.is_executable("exe"))
def test_rel_path(self, mock_access, mock_isfile): with _fix_windows_runtime(): mock_access.return_value = True mock_isfile.return_value = True self.assertTrue(filesystem.is_executable("exe"))
def test_full_path(self, mock_access, mock_isfile): with _fix_windows_runtime(): mock_access.return_value = True mock_isfile.return_value = True self.assertIs(filesystem.is_executable("/path/to/exe"), True)