From 15107eeea3d7be46b292a6af92bef2bf578a9f96 Mon Sep 17 00:00:00 2001 From: Shreya Date: Thu, 18 Feb 2021 22:40:30 +0530 Subject: [PATCH] Use constants/literals for tests --- .../tests/attack/test_t1197_telem.py | 8 +++--- .../tests/attack/test_t1222_telem.py | 8 +++--- .../tests/attack/test_victim_host_telem.py | 8 +++--- .../telemetry/tests/test_exploit_telem.py | 25 +++++++++++++++---- .../telemetry/tests/test_post_breach_telem.py | 13 ++++++++-- .../telemetry/tests/test_scan_telem.py | 11 +++++++- 6 files changed, 56 insertions(+), 17 deletions(-) diff --git a/monkey/infection_monkey/telemetry/tests/attack/test_t1197_telem.py b/monkey/infection_monkey/telemetry/tests/attack/test_t1197_telem.py index 3c620e854..89d174090 100644 --- a/monkey/infection_monkey/telemetry/tests/attack/test_t1197_telem.py +++ b/monkey/infection_monkey/telemetry/tests/attack/test_t1197_telem.py @@ -5,7 +5,9 @@ from infection_monkey.model import VictimHost from infection_monkey.telemetry.attack.t1197_telem import T1197Telem -MACHINE = VictimHost('127.0.0.1') +DOMAIN_NAME = 'domain-name' +IP = '127.0.0.1' +MACHINE = VictimHost(IP, DOMAIN_NAME) STATUS = ScanStatus.USED USAGE_STR = '[Usage info]' @@ -17,8 +19,8 @@ def T1197_telem_test_instance(): def test_T1197_send(T1197_telem_test_instance, spy_send_telemetry): T1197_telem_test_instance.send() - expected_data = {'machine': {'domain_name': MACHINE.domain_name, - 'ip_addr': MACHINE.ip_addr}, + expected_data = {'machine': {'domain_name': DOMAIN_NAME, + 'ip_addr': IP}, 'status': STATUS.value, 'technique': 'T1197', 'usage': USAGE_STR} diff --git a/monkey/infection_monkey/telemetry/tests/attack/test_t1222_telem.py b/monkey/infection_monkey/telemetry/tests/attack/test_t1222_telem.py index da87dfe7d..7a8f88a75 100644 --- a/monkey/infection_monkey/telemetry/tests/attack/test_t1222_telem.py +++ b/monkey/infection_monkey/telemetry/tests/attack/test_t1222_telem.py @@ -6,7 +6,9 @@ from infection_monkey.telemetry.attack.t1222_telem import T1222Telem COMMAND = 'echo hi' -MACHINE = VictimHost('127.0.0.1') +DOMAIN_NAME = 'domain-name' +IP = '127.0.0.1' +MACHINE = VictimHost(IP, DOMAIN_NAME) STATUS = ScanStatus.USED @@ -17,8 +19,8 @@ def T1222_telem_test_instance(): def test_T1222_send(T1222_telem_test_instance, spy_send_telemetry): T1222_telem_test_instance.send() - expected_data = {'machine': {'domain_name': MACHINE.domain_name, - 'ip_addr': MACHINE.ip_addr}, + expected_data = {'machine': {'domain_name': DOMAIN_NAME, + 'ip_addr': IP}, 'status': STATUS.value, 'technique': 'T1222', 'command': COMMAND} diff --git a/monkey/infection_monkey/telemetry/tests/attack/test_victim_host_telem.py b/monkey/infection_monkey/telemetry/tests/attack/test_victim_host_telem.py index 3743b7d76..6a102983b 100644 --- a/monkey/infection_monkey/telemetry/tests/attack/test_victim_host_telem.py +++ b/monkey/infection_monkey/telemetry/tests/attack/test_victim_host_telem.py @@ -5,7 +5,9 @@ from infection_monkey.model import VictimHost from infection_monkey.telemetry.attack.victim_host_telem import VictimHostTelem -MACHINE = VictimHost('127.0.0.1') +DOMAIN_NAME = 'domain-name' +IP = '127.0.0.1' +MACHINE = VictimHost(IP, DOMAIN_NAME) STATUS = ScanStatus.USED TECHNIQUE = 'T9999' @@ -17,8 +19,8 @@ def victim_host_telem_test_instance(): def test_victim_host_telem_send(victim_host_telem_test_instance, spy_send_telemetry): victim_host_telem_test_instance.send() - expected_data = {'machine': {'domain_name': MACHINE.domain_name, - 'ip_addr': MACHINE.ip_addr}, + expected_data = {'machine': {'domain_name': DOMAIN_NAME, + 'ip_addr': IP}, 'status': STATUS.value, 'technique': TECHNIQUE} assert spy_send_telemetry.data == expected_data diff --git a/monkey/infection_monkey/telemetry/tests/test_exploit_telem.py b/monkey/infection_monkey/telemetry/tests/test_exploit_telem.py index 1002a3cb3..92ca51ac3 100644 --- a/monkey/infection_monkey/telemetry/tests/test_exploit_telem.py +++ b/monkey/infection_monkey/telemetry/tests/test_exploit_telem.py @@ -5,11 +5,26 @@ from infection_monkey.model.host import VictimHost from infection_monkey.telemetry.exploit_telem import ExploitTelem -HOSTNAME = "hostname" DOMAIN_NAME = "domain-name" IP = "0.0.0.0" HOST = VictimHost(IP, DOMAIN_NAME) +HOST_AS_DICT = {'ip_addr': IP, + 'domain_name': DOMAIN_NAME, + 'os': {}, + 'services': {}, + 'icmp': False, + 'monkey_exe': None, + 'default_tunnel': None, + 'default_server': None} EXPLOITER = WmiExploiter(HOST) +EXPLOITER_NAME = 'WmiExploiter' +EXPLOITER_INFO = {'display_name': WmiExploiter._EXPLOITED_SERVICE, + 'started': '', + 'finished': '', + 'vulnerable_urls': [], + 'vulnerable_ports': [], + 'executed_cmds': []} +EXPLOITER_ATTEMPTS = [] RESULT = False @@ -22,10 +37,10 @@ def test_exploit_telem_send(exploit_telem_test_instance, spy_send_telemetry): exploit_telem_test_instance.send() expected_data = { "result": RESULT, - "machine": HOST.as_dict(), - "exploiter": EXPLOITER.__class__.__name__, - "info": EXPLOITER.exploit_info, - "attempts": EXPLOITER.exploit_attempts, + "machine": HOST_AS_DICT, + "exploiter": EXPLOITER_NAME, + "info": EXPLOITER_INFO, + "attempts": EXPLOITER_ATTEMPTS, } assert spy_send_telemetry.data == expected_data assert spy_send_telemetry.telem_category == "exploit" diff --git a/monkey/infection_monkey/telemetry/tests/test_post_breach_telem.py b/monkey/infection_monkey/telemetry/tests/test_post_breach_telem.py index d38cfdbde..5dd1123ab 100644 --- a/monkey/infection_monkey/telemetry/tests/test_post_breach_telem.py +++ b/monkey/infection_monkey/telemetry/tests/test_post_breach_telem.py @@ -1,12 +1,21 @@ import pytest +from common.data.post_breach_consts import POST_BREACH_JOB_SCHEDULING from infection_monkey.post_breach.actions.schedule_jobs import ScheduleJobs +from infection_monkey.post_breach.job_scheduling.linux_job_scheduling import \ + get_linux_commands_to_schedule_jobs +from infection_monkey.post_breach.job_scheduling.windows_job_scheduling import \ + get_windows_commands_to_schedule_jobs from infection_monkey.telemetry.post_breach_telem import PostBreachTelem +from infection_monkey.utils.environment import is_windows_os HOSTNAME = "hostname" IP = "0.0.0.0" PBA = ScheduleJobs() +PBA_COMMAND = get_windows_commands_to_schedule_jobs() if is_windows_os() else\ + ' '.join(get_linux_commands_to_schedule_jobs()) +PBA_NAME = POST_BREACH_JOB_SCHEDULING RESULT = False @@ -19,9 +28,9 @@ def post_breach_telem_test_instance(monkeypatch): def test_post_breach_telem_send(post_breach_telem_test_instance, spy_send_telemetry): post_breach_telem_test_instance.send() expected_data = { - "command": PBA.command, + "command": PBA_COMMAND, "result": RESULT, - "name": PBA.name, + "name": PBA_NAME, "hostname": HOSTNAME, "ip": IP, } diff --git a/monkey/infection_monkey/telemetry/tests/test_scan_telem.py b/monkey/infection_monkey/telemetry/tests/test_scan_telem.py index d75aecdb1..f35d84289 100644 --- a/monkey/infection_monkey/telemetry/tests/test_scan_telem.py +++ b/monkey/infection_monkey/telemetry/tests/test_scan_telem.py @@ -6,6 +6,15 @@ from infection_monkey.model.host import VictimHost DOMAIN_NAME = "domain-name" IP = "0.0.0.0" HOST = VictimHost(IP, DOMAIN_NAME) +HOST_AS_DICT = {'ip_addr': IP, + 'domain_name': DOMAIN_NAME, + 'os': {}, + 'services': {}, + 'icmp': False, + 'monkey_exe': None, + 'default_tunnel': None, + 'default_server': None} +HOST_SERVICES = {} @pytest.fixture @@ -15,6 +24,6 @@ def scan_telem_test_instance(): def test_scan_telem_send(scan_telem_test_instance, spy_send_telemetry): scan_telem_test_instance.send() - expected_data = {"machine": HOST.as_dict(), "service_count": len(HOST.services)} + expected_data = {"machine": HOST_AS_DICT, "service_count": len(HOST_SERVICES)} assert spy_send_telemetry.data == expected_data assert spy_send_telemetry.telem_category == "scan"