def get_default_data_dir() -> str: if is_windows_os(): return r"%AppData%\monkey_island" else: return r"$HOME/.monkey_island"
def _get_monkey_island_abs_path() -> str: if is_windows_os(): return os.path.join(os.getcwd(), "monkey_island") else: return str(Path(__file__).resolve().parent.parent.parent)
def test_remove_pba_files(): create_custom_pba_file("linux_file") create_custom_pba_file("windows_file") assert not dir_is_empty(PostBreachFilesService.get_custom_pba_directory()) PostBreachFilesService.remove_PBA_files() assert dir_is_empty(PostBreachFilesService.get_custom_pba_directory()) def dir_is_empty(dir_path): dir_contents = os.listdir(dir_path) return len(dir_contents) == 0 @pytest.mark.skipif(is_windows_os(), reason="Tests Posix (not Windows) permissions.") def test_custom_pba_dir_permissions_linux(): st = os.stat(PostBreachFilesService.get_custom_pba_directory()) assert st.st_mode == 0o40700 @pytest.mark.skipif(not is_windows_os(), reason="Tests Windows (not Posix) permissions.") def test_custom_pba_dir_permissions_windows(): pba_dir = PostBreachFilesService.get_custom_pba_directory() assert_windows_permissions(pba_dir)
return str(Path(__file__).resolve().parent.parent.parent) SERVER_CONFIG_FILENAME = "server_config.json" MONKEY_ISLAND_ABS_PATH = _get_monkey_island_abs_path() DEFAULT_DATA_DIR = expand_path(get_default_data_dir()) DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS = 60 * 5 _MONGO_BINARY_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "bin", "mongodb") _MONGO_EXECUTABLE_PATH_WIN = os.path.join(_MONGO_BINARY_DIR, "mongod.exe") _MONGO_EXECUTABLE_PATH_LINUX = os.path.join(_MONGO_BINARY_DIR, "bin", "mongod") MONGO_EXECUTABLE_PATH = (_MONGO_EXECUTABLE_PATH_WIN if is_windows_os() else _MONGO_EXECUTABLE_PATH_LINUX) MONGO_CONNECTION_TIMEOUT = 15 DEFAULT_SERVER_CONFIG_PATH = str( Path(MONKEY_ISLAND_ABS_PATH, "cc", SERVER_CONFIG_FILENAME)) DEFAULT_LOG_LEVEL = "INFO" DEFAULT_START_MONGO_DB = True DEFAULT_CRT_PATH = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", "server.crt")) DEFAULT_KEY_PATH = str(Path(MONKEY_ISLAND_ABS_PATH, "cc", "server.key")) DEFAULT_CERTIFICATE_PATHS = { "ssl_certificate_file": DEFAULT_CRT_PATH, "ssl_certificate_key_file": DEFAULT_KEY_PATH,
return path def test_create_secure_directory__already_exists(test_path): os.mkdir(test_path) assert os.path.isdir(test_path) create_secure_directory(test_path) def test_create_secure_directory__no_parent_dir(test_path_nested): with pytest.raises(Exception): create_secure_directory(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 @pytest.mark.skipif(not is_windows_os(), reason="Tests Windows (not Posix) permissions.") def test_create_secure_directory__perm_windows(test_path): create_secure_directory(test_path) assert_windows_permissions(test_path)