Add port and protocol custom validators

This commit is contained in:
Marek Wolan
2024-09-20 11:21:28 +01:00
parent 08f1cf1fbd
commit 695891f55c
77 changed files with 616 additions and 613 deletions

View File

@@ -1,7 +1,7 @@
# © Crown-owned copyright 2024, Defence Science and Technology Laboratory UK
from primaite.simulator.network.transmission.network_layer import IPProtocol
from primaite.simulator.network.transmission.transport_layer import Port
from primaite.simulator.network.transmission.transport_layer import PORT_LOOKUP
from primaite.utils.converters import convert_dict_enum_keys_to_enum_values
from primaite.utils.validators import PROTOCOL_LOOKUP
def test_simple_conversion():
@@ -11,7 +11,7 @@ def test_simple_conversion():
The original dictionary contains one level of nested dictionary with enums as keys.
The expected output should have string values of enums as keys.
"""
original_dict = {IPProtocol["UDP"]: {Port["ARP"]: {"inbound": 0, "outbound": 1016.0}}}
original_dict = {PROTOCOL_LOOKUP["UDP"]: {PORT_LOOKUP["ARP"]: {"inbound": 0, "outbound": 1016.0}}}
expected_dict = {"udp": {219: {"inbound": 0, "outbound": 1016.0}}}
assert convert_dict_enum_keys_to_enum_values(original_dict) == expected_dict
@@ -36,8 +36,8 @@ def test_mixed_keys():
The expected output should have string values of enums and original string keys.
"""
original_dict = {
IPProtocol["TCP"]: {"port": {"inbound": 0, "outbound": 1016.0}},
"protocol": {Port["HTTP"]: {"inbound": 10, "outbound": 2020.0}},
PROTOCOL_LOOKUP["TCP"]: {"port": {"inbound": 0, "outbound": 1016.0}},
"protocol": {PORT_LOOKUP["HTTP"]: {"inbound": 10, "outbound": 2020.0}},
}
expected_dict = {
"tcp": {"port": {"inbound": 0, "outbound": 1016.0}},
@@ -66,8 +66,12 @@ def test_nested_dicts():
The expected output should have string values of enums as keys at all levels.
"""
original_dict = {
IPProtocol["UDP"]: {
Port["ARP"]: {"inbound": 0, "outbound": 1016.0, "details": {IPProtocol["TCP"]: {"latency": "low"}}}
PROTOCOL_LOOKUP["UDP"]: {
PORT_LOOKUP["ARP"]: {
"inbound": 0,
"outbound": 1016.0,
"details": {PROTOCOL_LOOKUP["TCP"]: {"latency": "low"}},
}
}
}
expected_dict = {"udp": {219: {"inbound": 0, "outbound": 1016.0, "details": {"tcp": {"latency": "low"}}}}}
@@ -82,8 +86,11 @@ def test_non_dict_values():
The expected output should preserve these non-dictionary values while converting enum keys to string values.
"""
original_dict = {
IPProtocol["UDP"]: [Port["ARP"], Port["HTTP"]],
"protocols": (IPProtocol["TCP"], IPProtocol["UDP"]),
PROTOCOL_LOOKUP["UDP"]: [PORT_LOOKUP["ARP"], PORT_LOOKUP["HTTP"]],
"protocols": (PROTOCOL_LOOKUP["TCP"], PROTOCOL_LOOKUP["UDP"]),
}
expected_dict = {
"udp": [PORT_LOOKUP["ARP"], PORT_LOOKUP["HTTP"]],
"protocols": (PROTOCOL_LOOKUP["TCP"], PROTOCOL_LOOKUP["UDP"]),
}
expected_dict = {"udp": [Port["ARP"], Port["HTTP"]], "protocols": (IPProtocol["TCP"], IPProtocol["UDP"])}
assert convert_dict_enum_keys_to_enum_values(original_dict) == expected_dict