From ea82e86df57a1fb61d944aab7233abf388c87b38 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 11 May 2021 11:07:48 -0400 Subject: [PATCH] island: Add tests for PostBreachFilesService --- monkey/test.py | 4 +++ .../cc/services/test_post_breach_files.py | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 monkey/test.py diff --git a/monkey/test.py b/monkey/test.py new file mode 100644 index 000000000..19ef00bd8 --- /dev/null +++ b/monkey/test.py @@ -0,0 +1,4 @@ +try: + raise Exception("Ex") +except Exception as ex: + print(f"F: {ex}") diff --git a/monkey/tests/monkey_island/cc/services/test_post_breach_files.py b/monkey/tests/monkey_island/cc/services/test_post_breach_files.py index 9eda4af55..7d3431cfd 100644 --- a/monkey/tests/monkey_island/cc/services/test_post_breach_files.py +++ b/monkey/tests/monkey_island/cc/services/test_post_breach_files.py @@ -5,6 +5,10 @@ import pytest from monkey_island.cc.services.post_breach_files import PostBreachFilesService +def raise_(ex): + raise ex + + @pytest.fixture(autouse=True) def custom_pba_directory(tmpdir): PostBreachFilesService.initialize(tmpdir) @@ -28,3 +32,29 @@ def test_remove_pba_files(): custom_pda_dir_contents = os.listdir(PostBreachFilesService.get_custom_pba_directory()) assert len(custom_pda_dir_contents) == 0 + + +@pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.") +def test_custom_pba_dir_permissions(): + st = os.stat(PostBreachFilesService.get_custom_pba_directory()) + + assert st.st_mode == 0o40700 + + +def test_remove_failure(monkeypatch): + monkeypatch.setattr(os, "remove", lambda x: raise_(OSError("Permission denied"))) + + try: + create_custom_pba_file("windows_file") + PostBreachFilesService.remove_PBA_files() + except Exception as ex: + pytest.fail(f"Unxepected exception: {ex}") + + +def test_remove_nonexistant_file(monkeypatch): + monkeypatch.setattr(os, "remove", lambda x: raise_(FileNotFoundError("FileNotFound"))) + + try: + PostBreachFilesService.remove_file("/nonexistant/file") + except Exception as ex: + pytest.fail(f"Unxepected exception: {ex}")