Use constants/literals for tests

This commit is contained in:
Shreya 2021-02-18 22:40:30 +05:30
parent a4603853a9
commit 15107eeea3
6 changed files with 56 additions and 17 deletions

View File

@ -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}

View File

@ -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}

View File

@ -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

View File

@ -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"

View File

@ -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,
}

View File

@ -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"