-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
687c766
commit e027941
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
monkey/tests/unit_tests/common/agent_events/test_propagation_event.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from ipaddress import IPv4Address | ||
from uuid import UUID | ||
|
||
import pytest | ||
|
||
from common.agent_events import PropagationEvent | ||
|
||
TARGET_IP_STR = "192.168.1.10" | ||
AGENT_ID = UUID("012e7238-7b81-4108-8c7f-0787bc3f3c10") | ||
TIMESTAMP = 1664371327.4067292 | ||
|
||
PROPAGATION_EVENT = PropagationEvent( | ||
source=AGENT_ID, | ||
timestamp=TIMESTAMP, | ||
target=IPv4Address(TARGET_IP_STR), | ||
success=True, | ||
exploiter_name="SSHExploiter", | ||
) | ||
|
||
PROPAGATION_OBJECT_DICT = { | ||
"source": AGENT_ID, | ||
"timestamp": TIMESTAMP, | ||
"target": IPv4Address(TARGET_IP_STR), | ||
"success": True, | ||
"exploiter_name": "SSHExploiter", | ||
"error_message": "", | ||
} | ||
|
||
PROPAGATION_SIMPLE_DICT = { | ||
"source": str(AGENT_ID), | ||
"timestamp": TIMESTAMP, | ||
"target": TARGET_IP_STR, | ||
"success": "true", | ||
"exploiter_name": "SSHExploiter", | ||
"error_message": "", | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"propagation_event_dict", [PROPAGATION_OBJECT_DICT, PROPAGATION_SIMPLE_DICT] | ||
) | ||
def test_constructor(propagation_event_dict): | ||
assert PropagationEvent(**propagation_event_dict) == PROPAGATION_EVENT | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"key, value", | ||
[ | ||
("target", None), | ||
("success", "not-a-bool"), | ||
("exploiter_name", None), | ||
("error_message", None), | ||
], | ||
) | ||
def test_construct_invalid_field__type_error(key, value): | ||
invalid_type_dict = PROPAGATION_SIMPLE_DICT.copy() | ||
invalid_type_dict[key] = value | ||
|
||
with pytest.raises(TypeError): | ||
PropagationEvent(**invalid_type_dict) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"key, value", | ||
[ | ||
("target", "not-an-ip"), | ||
], | ||
) | ||
def test_construct_invalid_field__value_error(key, value): | ||
invalid_type_dict = PROPAGATION_SIMPLE_DICT.copy() | ||
invalid_type_dict[key] = value | ||
|
||
with pytest.raises(ValueError): | ||
PropagationEvent(**invalid_type_dict) | ||
|
||
|
||
def test_construct__extra_fields_forbidden(): | ||
extra_field_dict = PROPAGATION_SIMPLE_DICT.copy() | ||
extra_field_dict["extra_field"] = 99 # red balloons | ||
|
||
with pytest.raises(ValueError): | ||
PropagationEvent(**extra_field_dict) |