From 3c1e25b88cc96c5b8dc2f5349ed1f2bb41f3cb2b Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 22 Apr 2022 09:35:41 -0400 Subject: [PATCH] UT: Move Linux directory permissions check to a utility function --- monkey/tests/monkey_island/utils.py | 12 ++++++++++++ .../monkey_island/cc/server_utils/test_file_utils.py | 8 ++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/monkey/tests/monkey_island/utils.py b/monkey/tests/monkey_island/utils.py index dd781f85d..8ab767774 100644 --- a/monkey/tests/monkey_island/utils.py +++ b/monkey/tests/monkey_island/utils.py @@ -7,6 +7,9 @@ if is_windows_os(): FULL_CONTROL = 2032127 ACE_ACCESS_MODE_GRANT_ACCESS = win32security.GRANT_ACCESS ACE_INHERIT_OBJECT_AND_CONTAINER = 3 +else: + import os + import stat def _get_acl_and_sid_from_path(path: str): @@ -33,3 +36,12 @@ def assert_windows_permissions(path: str): assert ace_sid == user_sid assert ace_permissions == FULL_CONTROL and ace_access_mode == ACE_ACCESS_MODE_GRANT_ACCESS assert ace_inheritance == ACE_INHERIT_OBJECT_AND_CONTAINER + + +def assert_linux_permissions(path: str): + st = os.stat(path) + + expected_mode = stat.S_IRWXU + actual_mode = st.st_mode & (stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) + + assert expected_mode == actual_mode diff --git a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py index 6605673d0..7d05e29c2 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py +++ b/monkey/tests/unit_tests/monkey_island/cc/server_utils/test_file_utils.py @@ -2,7 +2,7 @@ import os import stat import pytest -from tests.monkey_island.utils import assert_windows_permissions +from tests.monkey_island.utils import assert_linux_permissions, assert_windows_permissions from monkey_island.cc.server_utils.file_utils import ( create_secure_directory, @@ -39,12 +39,8 @@ def test_create_secure_directory__no_parent_dir(test_path_nested): @pytest.mark.skipif(is_windows_os(), reason="Tests Posix (not Windows) permissions.") def test_create_secure_directory__perm_linux(test_path): create_secure_directory(test_path) - st = os.stat(test_path) - expected_mode = stat.S_IRWXU - actual_mode = st.st_mode & (stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) - - assert expected_mode == actual_mode + assert_linux_permissions(test_path) @pytest.mark.skipif(not is_windows_os(), reason="Tests Windows (not Posix) permissions.")