#2887 - Unit test fixes ahead of raising PR.

This commit is contained in:
Charlie Crane
2025-01-23 15:28:10 +00:00
parent 65355f83e8
commit b9d2cd25f3
22 changed files with 222 additions and 142 deletions

View File

@@ -119,7 +119,14 @@ def application_class():
@pytest.fixture(scope="function")
def file_system() -> FileSystem:
computer = Computer(hostname="fs_node", ip_address="192.168.1.2", subnet_mask="255.255.255.0", start_up_duration=0)
# computer = Computer(hostname="fs_node", ip_address="192.168.1.2", subnet_mask="255.255.255.0", start_up_duration=0)
computer_cfg = {"type": "computer",
"hostname": "fs_node",
"ip_address": "192.168.1.2",
"subnet_mask": "255.255.255.0",
"start_up_duration": 0,
}
computer = Computer.from_config(config=computer_cfg)
computer.power_on()
return computer.file_system

View File

@@ -25,7 +25,8 @@ def router_with_acl_rules():
:return: A configured Router object with ACL rules.
"""
router = Router("Router")
router_cfg = {"hostname": "router_1", "type": "router"}
router = Router.from_config(config=router_cfg)
acl = router.acl
# Add rules here as needed
acl.add_rule(
@@ -62,7 +63,8 @@ def router_with_wildcard_acl():
:return: A Router object with configured ACL rules, including rules with wildcard masking.
"""
router = Router("Router")
router_cfg = {"hostname": "router_1", "type": "router"}
router = Router.from_config(config=router_cfg)
acl = router.acl
# Rule to permit traffic from a specific source IP and port to a specific destination IP and port
acl.add_rule(
@@ -243,7 +245,8 @@ def test_ip_traffic_from_specific_subnet():
- Traffic from outside the 192.168.1.0/24 subnet is denied.
"""
router = Router("Router")
router_cfg = {"hostname": "router_1", "type": "router"}
router = Router.from_config(config=router_cfg)
acl = router.acl
# Add rules here as needed
acl.add_rule(

View File

@@ -52,7 +52,7 @@ def test_wireless_router_from_config():
rt = Router.from_config(config=cfg)
assert rt.num_ports == 6
assert rt.config.num_ports == 6
assert rt.network_interface[1].ip_address == IPv4Address("192.168.1.1")
assert rt.network_interface[1].subnet_mask == IPv4Address("255.255.255.0")

View File

@@ -7,7 +7,11 @@ from primaite.simulator.network.hardware.nodes.network.switch import Switch
@pytest.fixture(scope="function")
def switch() -> Switch:
switch: Switch = Switch(hostname="switch_1", num_ports=8, start_up_duration=0)
switch_cfg = {"type": "switch",
"hostname": "switch_1",
"num_ports": 8,
"start_up_duration": 0}
switch: Switch = Switch.from_config(config=switch_cfg)
switch.power_on()
switch.show()
return switch

View File

@@ -7,7 +7,13 @@ from primaite.simulator.network.hardware.nodes.host.computer import Computer
@pytest.fixture
def node() -> Node:
return Computer(hostname="test", ip_address="192.168.1.2", subnet_mask="255.255.255.0")
computer_cfg = {"type": "computer",
"hostname": "test",
"ip_address": "192.168.1.2",
"subnet_mask": "255.255.255.0"}
computer = Computer.from_config(config=computer_cfg)
return computer
def test_nic_enabled_validator(node):

View File

@@ -12,8 +12,16 @@ from tests.conftest import DummyApplication, DummyService
@pytest.fixture
def node() -> Node:
return Computer(hostname="test", ip_address="192.168.1.2", subnet_mask="255.255.255.0")
computer_cfg = {"type": "computer",
"hostname": "test",
"ip_address": "192.168.1.2",
"subnet_mask": "255.255.255.0",
"shut_down_duration": 3,
"operating_state": "OFF",
}
computer = Computer.from_config(config=computer_cfg)
return computer
def test_node_startup(node):
assert node.operating_state == NodeOperatingState.OFF
@@ -166,7 +174,7 @@ def test_node_is_on_validator(node):
"""Test that the node is on validator."""
node.power_on()
for i in range(node.start_up_duration + 1):
for i in range(node.config.start_up_duration + 1):
node.apply_timestep(i)
validator = Node._NodeIsOnValidator(node=node)
@@ -174,7 +182,7 @@ def test_node_is_on_validator(node):
assert validator(request=[], context={})
node.power_off()
for i in range(node.shut_down_duration + 1):
for i in range(node.config.shut_down_duration + 1):
node.apply_timestep(i)
assert validator(request=[], context={}) is False
@@ -184,7 +192,7 @@ def test_node_is_off_validator(node):
"""Test that the node is on validator."""
node.power_on()
for i in range(node.start_up_duration + 1):
for i in range(node.config.start_up_duration + 1):
node.apply_timestep(i)
validator = Node._NodeIsOffValidator(node=node)
@@ -192,7 +200,7 @@ def test_node_is_off_validator(node):
assert validator(request=[], context={}) is False
node.power_off()
for i in range(node.shut_down_duration + 1):
for i in range(node.config.shut_down_duration + 1):
node.apply_timestep(i)
assert validator(request=[], context={})

View File

@@ -61,12 +61,12 @@ def test_apply_timestep_to_nodes(network):
client_1.power_off()
assert client_1.operating_state is NodeOperatingState.SHUTTING_DOWN
for i in range(client_1.shut_down_duration + 1):
for i in range(client_1.config.shut_down_duration + 1):
network.apply_timestep(timestep=i)
assert client_1.operating_state is NodeOperatingState.OFF
network.apply_timestep(client_1.shut_down_duration + 2)
network.apply_timestep(client_1.config.shut_down_duration + 2)
assert client_1.operating_state is NodeOperatingState.OFF
@@ -74,7 +74,7 @@ def test_removing_node_that_does_not_exist(network):
"""Node that does not exist on network should not affect existing nodes."""
assert len(network.nodes) is 7
network.remove_node(Computer(hostname="new_node", ip_address="192.168.1.2", subnet_mask="255.255.255.0"))
network.remove_node(Computer.from_config(config = {"type":"computer","hostname":"new_node", "ip_address":"192.168.1.2", "subnet_mask":"255.255.255.0"}))
assert len(network.nodes) is 7

View File

@@ -19,7 +19,7 @@ def _assert_valid_creation(net: Network, lan_name, subnet_base, pcs_ip_block_sta
num_routers = 1 if include_router else 0
total_nodes = num_pcs + num_switches + num_routers
assert all((n.hostname.endswith(lan_name) for n in net.nodes.values()))
assert all((n.config.hostname.endswith(lan_name) for n in net.nodes.values()))
assert len(net.computer_nodes) == num_pcs
assert len(net.switch_nodes) == num_switches
assert len(net.router_nodes) == num_routers

View File

@@ -16,19 +16,25 @@ def basic_c2_network() -> Network:
network = Network()
# Creating two generic nodes for the C2 Server and the C2 Beacon.
computer_a_cfg = {"type": "computer",
"hostname": "computer_a",
"ip_address": "192.168.0.1",
"subnet_mask": "255.255.255.252",
"start_up_duration": 0}
computer_a = Computer.from_config(config = computer_a_cfg)
computer_a = Computer(
hostname="computer_a",
ip_address="192.168.0.1",
subnet_mask="255.255.255.252",
start_up_duration=0,
)
computer_a.power_on()
computer_a.software_manager.install(software_class=C2Server)
computer_b = Computer(
hostname="computer_b", ip_address="192.168.0.2", subnet_mask="255.255.255.252", start_up_duration=0
)
computer_b_cfg = {"type": "computer",
"hostname": "computer_b",
"ip_address": "192.168.0.2",
"subnet_mask": "255.255.255.252",
"start_up_duration": 0,
}
computer_b = Computer.from_config(config=computer_b_cfg)
computer_b.power_on()
computer_b.software_manager.install(software_class=C2Beacon)

View File

@@ -12,9 +12,14 @@ from primaite.utils.validation.port import PORT_LOOKUP
@pytest.fixture(scope="function")
def dos_bot() -> DoSBot:
computer = Computer(
hostname="compromised_pc", ip_address="192.168.0.1", subnet_mask="255.255.255.0", start_up_duration=0
)
computer_cfg = {"type":"computer",
"hostname": "compromised_pc",
"ip_address": "192.168.0.1",
"subnet_mask": "255.255.255.0",
"start_up_duration": 0,
}
computer: Computer = Computer.from_config(config=computer_cfg)
computer.power_on()
computer.software_manager.install(DoSBot)

View File

@@ -17,14 +17,14 @@ from primaite.simulator.system.services.database.database_service import Databas
def database_client_on_computer() -> Tuple[DatabaseClient, Computer]:
network = Network()
db_server = Server(hostname="db_server", ip_address="192.168.0.1", subnet_mask="255.255.255.0", start_up_duration=0)
db_server: Server = Server.from_config(config={"type": "server", "hostname":"db_server", "ip_address":"192.168.0.1", "subnet_mask":"255.255.255.0", "start_up_duration":0})
db_server.power_on()
db_server.software_manager.install(DatabaseService)
db_server.software_manager.software["DatabaseService"].start()
db_client = Computer(
hostname="db_client", ip_address="192.168.0.2", subnet_mask="255.255.255.0", start_up_duration=0
)
db_client: Computer = Computer.from_config(config = {"type":"computer",
"hostname":"db_client", "ip_address":"192.168.0.2", "subnet_mask":"255.255.255.0", "start_up_duration":0
})
db_client.power_on()
db_client.software_manager.install(DatabaseClient)

View File

@@ -8,7 +8,14 @@ from primaite.simulator.system.services.database.database_service import Databas
@pytest.fixture(scope="function")
def database_server() -> Node:
node = Computer(hostname="db_node", ip_address="192.168.1.2", subnet_mask="255.255.255.0", start_up_duration=0)
node_cfg = {"type": "computer",
"hostname": "db_node",
"ip_address": "192.168.1.2",
"subnet_mask": "255.255.255.0",
"start_up_duration": 0,
}
node = Computer.from_config(config=node_cfg)
node.power_on()
node.software_manager.install(DatabaseService)
node.software_manager.software.get("DatabaseService").start()

View File

@@ -14,13 +14,21 @@ from primaite.utils.validation.port import PORT_LOOKUP
@pytest.fixture(scope="function")
def dns_client() -> Computer:
node = Computer(
hostname="dns_client",
ip_address="192.168.1.11",
subnet_mask="255.255.255.0",
default_gateway="192.168.1.1",
dns_server=IPv4Address("192.168.1.10"),
)
node_cfg = {"type": "computer",
"hostname": "dns_client",
"ip_address": "192.168.1.11",
"subnet_mask": "255.255.255.0",
"default_gateway": "192.168.1.1",
"dns_server": IPv4Address("192.168.1.10")}
node = Computer.from_config(config=node_cfg)
# node = Computer(
# hostname="dns_client",
# ip_address="192.168.1.11",
# subnet_mask="255.255.255.0",
# default_gateway="192.168.1.1",
# dns_server=IPv4Address("192.168.1.10"),
# )
return node

View File

@@ -16,13 +16,13 @@ from primaite.utils.validation.port import PORT_LOOKUP
@pytest.fixture(scope="function")
def dns_server() -> Node:
node = Server(
hostname="dns_server",
ip_address="192.168.1.10",
subnet_mask="255.255.255.0",
default_gateway="192.168.1.1",
start_up_duration=0,
)
node_cfg = {"type": "server",
"hostname": "dns_server",
"ip_address": "192.168.1.10",
"subnet_mask":"255.255.255.0",
"default_gateway": "192.168.1.1",
"start_up_duration":0}
node = Server.from_config(config=node_cfg)
node.power_on()
node.software_manager.install(software_class=DNSServer)
return node
@@ -55,7 +55,13 @@ def test_dns_server_receive(dns_server):
# register the web server in the domain controller
dns_server_service.dns_register(domain_name="real-domain.com", domain_ip_address=IPv4Address("192.168.1.12"))
client = Computer(hostname="client", ip_address="192.168.1.11", subnet_mask="255.255.255.0", start_up_duration=0)
client_cfg = {"type": "computer",
"hostname": "client",
"ip_address": "192.168.1.11",
"subnet_mask": "255.255.255.0",
"start_up_duration": 0,
}
client = Computer.from_config(config=client_cfg)
client.power_on()
client.dns_server = IPv4Address("192.168.1.10")
network = Network()

View File

@@ -16,13 +16,14 @@ from primaite.utils.validation.port import PORT_LOOKUP
@pytest.fixture(scope="function")
def ftp_client() -> Node:
node = Computer(
hostname="ftp_client",
ip_address="192.168.1.11",
subnet_mask="255.255.255.0",
default_gateway="192.168.1.1",
start_up_duration=0,
)
node_cfg = {"type": "computer",
"hostname": "ftp_client",
"ip_address":"192.168.1.11",
"subnet_mask":"255.255.255.0",
"default_gateway":"192.168.1.1",
"start_up_duration": 0,
}
node = Computer.from_config(config=node_cfg)
node.power_on()
return node
@@ -94,7 +95,7 @@ def test_offline_ftp_client_receives_request(ftp_client):
ftp_client_service: FTPClient = ftp_client.software_manager.software.get("FTPClient")
ftp_client.power_off()
for i in range(ftp_client.shut_down_duration + 1):
for i in range(ftp_client.config.shut_down_duration + 1):
ftp_client.apply_timestep(timestep=i)
assert ftp_client.operating_state is NodeOperatingState.OFF

View File

@@ -14,13 +14,13 @@ from primaite.utils.validation.port import PORT_LOOKUP
@pytest.fixture(scope="function")
def ftp_server() -> Node:
node = Server(
hostname="ftp_server",
ip_address="192.168.1.10",
subnet_mask="255.255.255.0",
default_gateway="192.168.1.1",
start_up_duration=0,
)
node_cfg = {"type": "server",
"hostname":"ftp_server",
"ip_address":"192.168.1.10",
"subnet_mask": "255.255.255.0",
"default_gateway": "192.168.1.1",
"start_up_duration":0}
node = Server.from_config(config=node_cfg)
node.power_on()
node.software_manager.install(software_class=FTPServer)
return node

View File

@@ -16,13 +16,13 @@ from primaite.utils.validation.port import PORT_LOOKUP
@pytest.fixture(scope="function")
def web_server() -> Server:
node = Server(
hostname="web_server",
ip_address="192.168.1.10",
subnet_mask="255.255.255.0",
default_gateway="192.168.1.1",
start_up_duration=0,
)
node_cfg = {"type": "server",
"hostname":"web_server",
"ip_address": "192.168.1.10",
"subnet_mask": "255.255.255.0",
"default_gateway":"192.168.1.1",
"start_up_duration":0 }
node = Server.from_config(config=node_cfg)
node.power_on()
node.software_manager.install(WebServer)
node.software_manager.software.get("WebServer").start()