From 8afd69634c0e01d186e94c0700c993fd79e9ef80 Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 5 Jul 2021 19:10:21 +0530 Subject: [PATCH] tests: Add unit test for ransomware target dir path with env variables --- .../infection_monkey/ransomware/conftest.py | 7 +++++++ .../ransomware/test_ransomware_payload.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/monkey/tests/unit_tests/infection_monkey/ransomware/conftest.py b/monkey/tests/unit_tests/infection_monkey/ransomware/conftest.py index 60b14a322..a23751633 100644 --- a/monkey/tests/unit_tests/infection_monkey/ransomware/conftest.py +++ b/monkey/tests/unit_tests/infection_monkey/ransomware/conftest.py @@ -4,6 +4,13 @@ from pathlib import Path import pytest +@pytest.fixture +def patched_home_env(monkeypatch, tmp_path): + monkeypatch.setenv("HOME", str(tmp_path)) + + return tmp_path + + @pytest.fixture def ransomware_target(tmp_path, data_for_tests_dir): ransomware_test_data = Path(data_for_tests_dir) / "ransomware_targets" diff --git a/monkey/tests/unit_tests/infection_monkey/ransomware/test_ransomware_payload.py b/monkey/tests/unit_tests/infection_monkey/ransomware/test_ransomware_payload.py index 36dc6615e..2014c7486 100644 --- a/monkey/tests/unit_tests/infection_monkey/ransomware/test_ransomware_payload.py +++ b/monkey/tests/unit_tests/infection_monkey/ransomware/test_ransomware_payload.py @@ -48,6 +48,24 @@ def ransomware_payload(ransomware_payload_config, telemetry_messenger_spy): return RansomwarePayload(ransomware_payload_config, telemetry_messenger_spy) +def test_env_variables_in_target_dir_resolved_linux( + ransomware_payload_config, ransomware_target, telemetry_messenger_spy, patched_home_env +): + path_with_env_variable = "$HOME/ransomware_target" + + ransomware_payload_config["encryption"]["directories"][ + "linux_target_dir" + ] = ransomware_payload_config["encryption"]["directories"][ + "windows_target_dir" + ] = path_with_env_variable + RansomwarePayload(ransomware_payload_config, telemetry_messenger_spy).run_payload() + + assert ( + hash_file(ransomware_target / with_extension(ALL_ZEROS_PDF)) + == ALL_ZEROS_PDF_ENCRYPTED_SHA256 + ) + + def test_file_with_excluded_extension_not_encrypted(ransomware_target, ransomware_payload): ransomware_payload.run_payload()