Merge remote-tracking branch 'origin/dev' into feature/2317-refactor-reset
This commit is contained in:
@@ -1,111 +1,293 @@
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
from primaite.simulator.network.hardware.nodes.router import ACLAction, Router
|
||||
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.hardware.base import generate_mac_address
|
||||
from primaite.simulator.network.hardware.nodes.network.router import ACLAction, Router
|
||||
from primaite.simulator.network.protocols.icmp import ICMPPacket
|
||||
from primaite.simulator.network.transmission.data_link_layer import EthernetHeader, Frame
|
||||
from primaite.simulator.network.transmission.network_layer import IPPacket, IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port, TCPHeader, UDPHeader
|
||||
|
||||
|
||||
def test_add_rule():
|
||||
@pytest.fixture(scope="function")
|
||||
def router_with_acl_rules():
|
||||
"""
|
||||
Provides a router instance with predefined ACL rules for testing.
|
||||
|
||||
:Setup:
|
||||
1. Creates a Router object named "Router".
|
||||
2. Adds a PERMIT rule for TCP traffic from 192.168.1.1:HTTPS to 192.168.1.2:HTTP.
|
||||
3. Adds a DENY rule for TCP traffic from 192.168.1.3:8080 to 192.168.1.4:80.
|
||||
|
||||
:return: A configured Router object with ACL rules.
|
||||
"""
|
||||
router = Router("Router")
|
||||
acl = router.acl
|
||||
# Add rules here as needed
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address=IPv4Address("192.168.1.1"),
|
||||
src_ip_address="192.168.1.1",
|
||||
src_port=Port.HTTPS,
|
||||
dst_ip_address="192.168.1.2",
|
||||
dst_port=Port.HTTP,
|
||||
position=1,
|
||||
)
|
||||
acl.add_rule(
|
||||
action=ACLAction.DENY,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address="192.168.1.3",
|
||||
src_port=Port(8080),
|
||||
dst_ip_address=IPv4Address("192.168.1.2"),
|
||||
dst_ip_address="192.168.1.4",
|
||||
dst_port=Port(80),
|
||||
position=2,
|
||||
)
|
||||
return router
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def router_with_wildcard_acl():
|
||||
"""
|
||||
Provides a router instance with ACL rules that include wildcard masking for testing.
|
||||
|
||||
:Setup:
|
||||
1. Creates a Router object named "Router".
|
||||
2. Adds a PERMIT rule for TCP traffic from 192.168.1.1:8080 to 10.1.1.2:80.
|
||||
3. Adds a DENY rule with a wildcard mask for TCP traffic from the 192.168.1.0/24 network to 10.1.1.3:443.
|
||||
4. Adds a PERMIT rule for any traffic to the 10.2.0.0/16 network.
|
||||
|
||||
:return: A Router object with configured ACL rules, including rules with wildcard masking.
|
||||
"""
|
||||
router = Router("Router")
|
||||
acl = router.acl
|
||||
# Rule to permit traffic from a specific source IP and port to a specific destination IP and port
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address="192.168.1.1",
|
||||
src_port=Port(8080),
|
||||
dst_ip_address="10.1.1.2",
|
||||
dst_port=Port(80),
|
||||
position=1,
|
||||
)
|
||||
# Rule to deny traffic from an IP range to a specific destination IP and port
|
||||
acl.add_rule(
|
||||
action=ACLAction.DENY,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address="192.168.1.0",
|
||||
src_wildcard_mask="0.0.0.255",
|
||||
dst_ip_address="10.1.1.3",
|
||||
dst_port=Port(443),
|
||||
position=2,
|
||||
)
|
||||
# Rule to permit any traffic to a range of destination IPs
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=None,
|
||||
src_ip_address=None,
|
||||
dst_ip_address="10.2.0.0",
|
||||
dst_wildcard_mask="0.0.255.255",
|
||||
position=3,
|
||||
)
|
||||
return router
|
||||
|
||||
|
||||
def test_add_rule(router_with_acl_rules):
|
||||
"""
|
||||
Tests that an ACL rule is added correctly to the router's ACL.
|
||||
|
||||
Asserts:
|
||||
- The action of the added rule is PERMIT.
|
||||
- The protocol of the added rule is TCP.
|
||||
- The source IP address matches "192.168.1.1".
|
||||
- The source port is HTTPS.
|
||||
- The destination IP address matches "192.168.1.2".
|
||||
- The destination port is HTTP.
|
||||
"""
|
||||
acl = router_with_acl_rules.acl
|
||||
|
||||
assert acl.acl[1].action == ACLAction.PERMIT
|
||||
assert acl.acl[1].protocol == IPProtocol.TCP
|
||||
assert acl.acl[1].src_ip_address == IPv4Address("192.168.1.1")
|
||||
assert acl.acl[1].src_port == Port(8080)
|
||||
assert acl.acl[1].src_port == Port.HTTPS
|
||||
assert acl.acl[1].dst_ip_address == IPv4Address("192.168.1.2")
|
||||
assert acl.acl[1].dst_port == Port(80)
|
||||
assert acl.acl[1].dst_port == Port.HTTP
|
||||
|
||||
|
||||
def test_remove_rule():
|
||||
router = Router("Router")
|
||||
acl = router.acl
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address=IPv4Address("192.168.1.1"),
|
||||
src_port=Port(8080),
|
||||
dst_ip_address=IPv4Address("192.168.1.2"),
|
||||
dst_port=Port(80),
|
||||
position=1,
|
||||
)
|
||||
def test_remove_rule(router_with_acl_rules):
|
||||
"""
|
||||
Tests the removal of an ACL rule from the router's ACL.
|
||||
|
||||
Asserts that accessing the removed rule index in the ACL returns None.
|
||||
"""
|
||||
acl = router_with_acl_rules.acl
|
||||
acl.remove_rule(1)
|
||||
assert not acl.acl[1]
|
||||
assert acl.acl[1] is None
|
||||
|
||||
|
||||
def test_rules():
|
||||
router = Router("Router")
|
||||
acl = router.acl
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address=IPv4Address("192.168.1.1"),
|
||||
src_port=Port(8080),
|
||||
dst_ip_address=IPv4Address("192.168.1.2"),
|
||||
dst_port=Port(80),
|
||||
position=1,
|
||||
)
|
||||
acl.add_rule(
|
||||
action=ACLAction.DENY,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address=IPv4Address("192.168.1.3"),
|
||||
src_port=Port(8080),
|
||||
dst_ip_address=IPv4Address("192.168.1.4"),
|
||||
dst_port=Port(80),
|
||||
position=2,
|
||||
)
|
||||
is_permitted, rule = acl.is_permitted(
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address=IPv4Address("192.168.1.1"),
|
||||
src_port=Port(8080),
|
||||
dst_ip_address=IPv4Address("192.168.1.2"),
|
||||
dst_port=Port(80),
|
||||
def test_traffic_permitted_by_specific_rule(router_with_acl_rules):
|
||||
"""
|
||||
Verifies that traffic matching a specific ACL rule is correctly permitted.
|
||||
|
||||
Asserts traffic that matches a permit rule is allowed through the ACL.
|
||||
"""
|
||||
acl = router_with_acl_rules.acl
|
||||
permitted_frame = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.1.1", dst_ip_address="192.168.1.2", protocol=IPProtocol.TCP),
|
||||
tcp=TCPHeader(src_port=Port.HTTPS, dst_port=Port.HTTP),
|
||||
)
|
||||
is_permitted, _ = acl.is_permitted(permitted_frame)
|
||||
assert is_permitted
|
||||
is_permitted, rule = acl.is_permitted(
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address=IPv4Address("192.168.1.3"),
|
||||
src_port=Port(8080),
|
||||
dst_ip_address=IPv4Address("192.168.1.4"),
|
||||
dst_port=Port(80),
|
||||
|
||||
|
||||
def test_traffic_denied_by_specific_rule(router_with_acl_rules):
|
||||
"""
|
||||
Verifies that traffic matching a specific ACL rule is correctly denied.
|
||||
|
||||
Asserts traffic that matches a deny rule is blocked by the ACL.
|
||||
"""
|
||||
|
||||
acl = router_with_acl_rules.acl
|
||||
not_permitted_frame = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.1.3", dst_ip_address="192.168.1.4", protocol=IPProtocol.TCP),
|
||||
tcp=TCPHeader(src_port=Port(8080), dst_port=Port(80)),
|
||||
)
|
||||
is_permitted, _ = acl.is_permitted(not_permitted_frame)
|
||||
assert not is_permitted
|
||||
|
||||
|
||||
def test_default_rule():
|
||||
def test_default_rule(router_with_acl_rules):
|
||||
"""
|
||||
Tests the default deny rule of the ACL.
|
||||
|
||||
This test verifies that traffic which does not match any explicit permit rule in the ACL
|
||||
is correctly denied, as per the common "default deny" security stance that ACLs implement.
|
||||
|
||||
Asserts the frame does not match any of the predefined ACL rules and is therefore not permitted by the ACL,
|
||||
illustrating the default deny behavior when no explicit permit rule is matched.
|
||||
"""
|
||||
acl = router_with_acl_rules.acl
|
||||
not_permitted_frame = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.1.5", dst_ip_address="192.168.1.12", protocol=IPProtocol.UDP),
|
||||
udp=UDPHeader(src_port=Port.HTTPS, dst_port=Port.HTTP),
|
||||
)
|
||||
is_permitted, rule = acl.is_permitted(not_permitted_frame)
|
||||
assert not is_permitted
|
||||
|
||||
|
||||
def test_direct_ip_match_with_acl(router_with_wildcard_acl):
|
||||
"""
|
||||
Tests ACL functionality for a direct IP address match.
|
||||
|
||||
Asserts direct IP address match traffic is permitted by the ACL rule.
|
||||
"""
|
||||
acl = router_with_wildcard_acl.acl
|
||||
frame = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.1.1", dst_ip_address="10.1.1.2", protocol=IPProtocol.TCP),
|
||||
tcp=TCPHeader(src_port=Port(8080), dst_port=Port(80)),
|
||||
)
|
||||
assert acl.is_permitted(frame)[0], "Direct IP match should be permitted."
|
||||
|
||||
|
||||
def test_ip_range_match_denied_with_acl(router_with_wildcard_acl):
|
||||
"""
|
||||
Tests ACL functionality for denying traffic from an IP range using wildcard masking.
|
||||
|
||||
Asserts traffic from the specified IP range is correctly denied by the ACL rule.
|
||||
"""
|
||||
acl = router_with_wildcard_acl.acl
|
||||
frame = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.1.100", dst_ip_address="10.1.1.3", protocol=IPProtocol.TCP),
|
||||
tcp=TCPHeader(src_port=Port(8080), dst_port=Port(443)),
|
||||
)
|
||||
assert not acl.is_permitted(frame)[0], "IP range match with wildcard mask should be denied."
|
||||
|
||||
|
||||
def test_traffic_permitted_to_destination_range_with_acl(router_with_wildcard_acl):
|
||||
"""
|
||||
Tests ACL functionality for permitting traffic to a destination IP range using wildcard masking.
|
||||
|
||||
Asserts traffic to the specified destination IP range is correctly permitted by the ACL rule.
|
||||
"""
|
||||
acl = router_with_wildcard_acl.acl
|
||||
frame = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.1.50", dst_ip_address="10.2.200.200", protocol=IPProtocol.UDP),
|
||||
udp=UDPHeader(src_port=Port(1433), dst_port=Port(1433)),
|
||||
)
|
||||
assert acl.is_permitted(frame)[0], "Traffic to destination IP range should be permitted."
|
||||
|
||||
|
||||
def test_ip_traffic_from_specific_subnet():
|
||||
"""
|
||||
Tests that the ACL permits or denies IP traffic from specific subnets, mimicking a Cisco ACL rule for IP traffic.
|
||||
|
||||
This test verifies the ACL's ability to permit all IP traffic from a specific subnet (192.168.1.0/24) while denying
|
||||
traffic from other subnets. The test mimics a Cisco ACL rule that allows IP traffic from a specified range using
|
||||
wildcard masking.
|
||||
|
||||
The test frames are constructed with varying protocols (TCP, UDP, ICMP) and source IP addresses, to demonstrate the
|
||||
rule's general applicability to all IP protocols and its enforcement based on source IP address range.
|
||||
|
||||
Asserts
|
||||
- Traffic from within the 192.168.1.0/24 subnet is permitted.
|
||||
- Traffic from outside the 192.168.1.0/24 subnet is denied.
|
||||
"""
|
||||
|
||||
router = Router("Router")
|
||||
acl = router.acl
|
||||
# Add rules here as needed
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address=IPv4Address("192.168.1.1"),
|
||||
src_port=Port(8080),
|
||||
dst_ip_address=IPv4Address("192.168.1.2"),
|
||||
dst_port=Port(80),
|
||||
src_ip_address="192.168.1.0",
|
||||
src_wildcard_mask="0.0.0.255",
|
||||
position=1,
|
||||
)
|
||||
acl.add_rule(
|
||||
action=ACLAction.DENY,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip_address=IPv4Address("192.168.1.3"),
|
||||
src_port=Port(8080),
|
||||
dst_ip_address=IPv4Address("192.168.1.4"),
|
||||
dst_port=Port(80),
|
||||
position=2,
|
||||
|
||||
permitted_frame_1 = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.1.50", dst_ip_address="10.2.200.200", protocol=IPProtocol.TCP),
|
||||
tcp=TCPHeader(src_port=Port.POSTGRES_SERVER, dst_port=Port.POSTGRES_SERVER),
|
||||
)
|
||||
is_permitted, rule = acl.is_permitted(
|
||||
protocol=IPProtocol.UDP,
|
||||
src_ip_address=IPv4Address("192.168.1.5"),
|
||||
src_port=Port(8080),
|
||||
dst_ip_address=IPv4Address("192.168.1.12"),
|
||||
dst_port=Port(80),
|
||||
|
||||
assert acl.is_permitted(permitted_frame_1)[0]
|
||||
|
||||
permitted_frame_2 = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.1.10", dst_ip_address="85.199.214.101", protocol=IPProtocol.UDP),
|
||||
udp=UDPHeader(src_port=Port.NTP, dst_port=Port.NTP),
|
||||
)
|
||||
assert not is_permitted
|
||||
|
||||
assert acl.is_permitted(permitted_frame_2)[0]
|
||||
|
||||
permitted_frame_3 = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.1.200", dst_ip_address="192.168.1.1", protocol=IPProtocol.ICMP),
|
||||
icmp=ICMPPacket(identifier=1),
|
||||
)
|
||||
|
||||
assert acl.is_permitted(permitted_frame_3)[0]
|
||||
|
||||
not_permitted_frame_1 = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.0.50", dst_ip_address="10.2.200.200", protocol=IPProtocol.TCP),
|
||||
tcp=TCPHeader(src_port=Port.POSTGRES_SERVER, dst_port=Port.POSTGRES_SERVER),
|
||||
)
|
||||
|
||||
assert not acl.is_permitted(not_permitted_frame_1)[0]
|
||||
|
||||
not_permitted_frame_2 = Frame(
|
||||
ethernet=EthernetHeader(src_mac_addr=generate_mac_address(), dst_mac_addr=generate_mac_address()),
|
||||
ip=IPPacket(src_ip_address="192.168.2.10", dst_ip_address="85.199.214.101", protocol=IPProtocol.UDP),
|
||||
udp=UDPHeader(src_port=Port.NTP, dst_port=Port.NTP),
|
||||
)
|
||||
|
||||
assert not acl.is_permitted(not_permitted_frame_2)[0]
|
||||
|
||||
acl.show()
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.switch import Switch
|
||||
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, operating_state=NodeOperatingState.ON)
|
||||
switch: Switch = Switch(hostname="switch_1", num_ports=8, start_up_duration=0)
|
||||
switch.power_on()
|
||||
switch.show()
|
||||
return switch
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@ import re
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from primaite.simulator.network.hardware.base import generate_mac_address, NIC
|
||||
from primaite.simulator.network.hardware.base import generate_mac_address
|
||||
from primaite.simulator.network.hardware.nodes.host.host_node import NIC
|
||||
|
||||
|
||||
def test_mac_address_generation():
|
||||
@@ -29,29 +31,26 @@ def test_invalid_oui_mac_address():
|
||||
|
||||
def test_nic_ip_address_type_conversion():
|
||||
"""Tests NIC IP and gateway address is converted to IPv4Address is originally a string."""
|
||||
nic = NIC(
|
||||
network_interface = NIC(
|
||||
ip_address="192.168.1.2",
|
||||
subnet_mask="255.255.255.0",
|
||||
)
|
||||
assert isinstance(nic.ip_address, IPv4Address)
|
||||
assert isinstance(network_interface.ip_address, IPv4Address)
|
||||
|
||||
|
||||
def test_nic_deserialize():
|
||||
"""Tests NIC serialization and deserialization."""
|
||||
nic = NIC(
|
||||
network_interface = NIC(
|
||||
ip_address="192.168.1.2",
|
||||
subnet_mask="255.255.255.0",
|
||||
)
|
||||
|
||||
nic_json = nic.model_dump_json()
|
||||
nic_json = network_interface.model_dump_json()
|
||||
deserialized_nic = NIC.model_validate_json(nic_json)
|
||||
assert nic_json == deserialized_nic.model_dump_json()
|
||||
|
||||
|
||||
def test_nic_ip_address_as_network_address_fails():
|
||||
"""Tests NIC creation fails if ip address and subnet mask are a network address."""
|
||||
with pytest.raises(ValueError):
|
||||
NIC(
|
||||
ip_address="192.168.0.0",
|
||||
subnet_mask="255.255.255.0",
|
||||
)
|
||||
with pytest.raises(ValidationError):
|
||||
NIC(ip_address="192.168.0.0", subnet_mask="255.255.255.0")
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import re
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.hardware.base import Node
|
||||
|
||||
|
||||
def test_node_creation():
|
||||
node = Node(hostname="host_1")
|
||||
@@ -4,12 +4,13 @@ from primaite.simulator.file_system.file import File
|
||||
from primaite.simulator.file_system.file_system_item_abc import FileSystemItemHealthStatus
|
||||
from primaite.simulator.file_system.folder import Folder
|
||||
from primaite.simulator.network.hardware.base import Node, NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.system.software import SoftwareHealthState
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def node() -> Node:
|
||||
return Node(hostname="test")
|
||||
return Computer(hostname="test", ip_address="192.168.1.2", subnet_mask="255.255.255.0")
|
||||
|
||||
|
||||
def test_node_startup(node):
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.protocols.icmp import ICMPPacket
|
||||
from primaite.simulator.network.transmission.data_link_layer import EthernetHeader, Frame
|
||||
from primaite.simulator.network.transmission.network_layer import ICMPPacket, IPPacket, IPProtocol, Precedence
|
||||
from primaite.simulator.network.transmission.network_layer import IPPacket, IPProtocol, Precedence
|
||||
from primaite.simulator.network.transmission.primaite_layer import AgentSource, DataStatus
|
||||
from primaite.simulator.network.transmission.transport_layer import Port, TCPFlags, TCPHeader, UDPHeader
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.transmission.network_layer import ICMPPacket, ICMPType
|
||||
from primaite.simulator.network.protocols.icmp import ICMPPacket, ICMPType
|
||||
|
||||
|
||||
def test_icmp_minimal_header_creation():
|
||||
|
||||
@@ -5,9 +5,7 @@ import pytest
|
||||
from primaite.simulator.network.container import Network
|
||||
from primaite.simulator.network.hardware.base import Link, Node
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.computer import Computer
|
||||
from primaite.simulator.system.applications.database_client import DatabaseClient
|
||||
from primaite.simulator.system.services.database.database_service import DatabaseService
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
|
||||
|
||||
def filter_keys_nested_item(data, keys):
|
||||
@@ -109,7 +107,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(Node(hostname="new_node"))
|
||||
network.remove_node(Computer(hostname="new_node", ip_address="192.168.1.2", subnet_mask="255.255.255.0"))
|
||||
assert len(network.nodes) is 7
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ from ipaddress import IPv4Address
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.computer import Computer
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
from primaite.simulator.system.applications.application import ApplicationOperatingState
|
||||
from primaite.simulator.system.applications.red_applications.dos_bot import DoSAttackStage, DoSBot
|
||||
@@ -12,12 +12,9 @@ from primaite.simulator.system.applications.red_applications.dos_bot import DoSA
|
||||
@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",
|
||||
operating_state=NodeOperatingState.ON,
|
||||
hostname="compromised_pc", ip_address="192.168.0.1", subnet_mask="255.255.255.0", start_up_duration=0
|
||||
)
|
||||
|
||||
computer.power_on()
|
||||
computer.software_manager.install(DoSBot)
|
||||
|
||||
dos_bot: DoSBot = computer.software_manager.software.get("DoSBot")
|
||||
|
||||
@@ -1,26 +1,39 @@
|
||||
from ipaddress import IPv4Address
|
||||
from typing import Tuple, Union
|
||||
from typing import Tuple
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.computer import Computer
|
||||
from primaite.simulator.network.container import Network
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.network.hardware.nodes.host.server import Server
|
||||
from primaite.simulator.system.applications.application import ApplicationOperatingState
|
||||
from primaite.simulator.system.applications.database_client import DatabaseClient
|
||||
from primaite.simulator.system.services.database.database_service import DatabaseService
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def database_client_on_computer() -> Tuple[DatabaseClient, Computer]:
|
||||
computer = Computer(
|
||||
hostname="db_node", ip_address="192.168.0.1", subnet_mask="255.255.255.0", operating_state=NodeOperatingState.ON
|
||||
)
|
||||
computer.software_manager.install(DatabaseClient)
|
||||
network = Network()
|
||||
|
||||
database_client: DatabaseClient = computer.software_manager.software.get("DatabaseClient")
|
||||
db_server = 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.power_on()
|
||||
db_client.software_manager.install(DatabaseClient)
|
||||
|
||||
database_client: DatabaseClient = db_client.software_manager.software.get("DatabaseClient")
|
||||
database_client.configure(server_ip_address=IPv4Address("192.168.0.1"))
|
||||
database_client.run()
|
||||
return database_client, computer
|
||||
|
||||
network.connect(db_server.network_interface[1], db_client.network_interface[1])
|
||||
|
||||
return database_client, db_client
|
||||
|
||||
|
||||
def test_creation(database_client_on_computer):
|
||||
@@ -50,7 +63,7 @@ def test_disconnect_when_client_is_closed(database_client_on_computer):
|
||||
"""Database client disconnect should not do anything when it is not running."""
|
||||
database_client, computer = database_client_on_computer
|
||||
|
||||
database_client.connected = True
|
||||
database_client.connect()
|
||||
assert database_client.server_ip_address is not None
|
||||
|
||||
database_client.close()
|
||||
@@ -66,23 +79,15 @@ def test_disconnect(database_client_on_computer):
|
||||
"""Database client should remove the connection."""
|
||||
database_client, computer = database_client_on_computer
|
||||
|
||||
database_client._connections[str(uuid4())] = {"item": True}
|
||||
assert len(database_client.connections) == 1
|
||||
assert not database_client.connected
|
||||
|
||||
assert database_client.operating_state is ApplicationOperatingState.RUNNING
|
||||
assert database_client.server_ip_address is not None
|
||||
database_client.connect()
|
||||
|
||||
assert database_client.connected
|
||||
|
||||
database_client.disconnect()
|
||||
|
||||
assert len(database_client.connections) == 0
|
||||
|
||||
uuid = str(uuid4())
|
||||
database_client._connections[uuid] = {"item": True}
|
||||
assert len(database_client.connections) == 1
|
||||
|
||||
database_client.disconnect(connection_id=uuid)
|
||||
|
||||
assert len(database_client.connections) == 0
|
||||
assert not database_client.connected
|
||||
|
||||
|
||||
def test_query_when_client_is_closed(database_client_on_computer):
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
from typing import Tuple
|
||||
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.computer import Computer
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.network.protocols.http import HttpResponsePacket, HttpStatusCode
|
||||
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
@@ -18,8 +16,9 @@ def web_browser() -> WebBrowser:
|
||||
ip_address="192.168.1.11",
|
||||
subnet_mask="255.255.255.0",
|
||||
default_gateway="192.168.1.1",
|
||||
operating_state=NodeOperatingState.ON,
|
||||
start_up_duration=0,
|
||||
)
|
||||
computer.power_on()
|
||||
# Web Browser should be pre-installed in computer
|
||||
web_browser: WebBrowser = computer.software_manager.software.get("WebBrowser")
|
||||
web_browser.run()
|
||||
@@ -33,8 +32,9 @@ def test_create_web_client():
|
||||
ip_address="192.168.1.11",
|
||||
subnet_mask="255.255.255.0",
|
||||
default_gateway="192.168.1.1",
|
||||
operating_state=NodeOperatingState.ON,
|
||||
start_up_duration=0,
|
||||
)
|
||||
computer.power_on()
|
||||
# Web Browser should be pre-installed in computer
|
||||
web_browser: WebBrowser = computer.software_manager.software.get("WebBrowser")
|
||||
assert web_browser.name is "WebBrowser"
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.hardware.base import Node
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.system.services.database.database_service import DatabaseService
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def database_server() -> Node:
|
||||
node = Node(hostname="db_node")
|
||||
node = Computer(hostname="db_node", ip_address="192.168.1.2", subnet_mask="255.255.255.0", start_up_duration=0)
|
||||
node.power_on()
|
||||
node.software_manager.install(DatabaseService)
|
||||
node.software_manager.software.get("DatabaseService").start()
|
||||
return node
|
||||
|
||||
@@ -2,9 +2,8 @@ from ipaddress import IPv4Address
|
||||
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.hardware.base import Node
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.computer import Computer
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.network.protocols.dns import DNSPacket, DNSReply, DNSRequest
|
||||
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
@@ -13,7 +12,7 @@ from primaite.simulator.system.services.service import ServiceOperatingState
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def dns_client() -> Node:
|
||||
def dns_client() -> Computer:
|
||||
node = Computer(
|
||||
hostname="dns_client",
|
||||
ip_address="192.168.1.11",
|
||||
|
||||
@@ -2,12 +2,14 @@ from ipaddress import IPv4Address
|
||||
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.container import Network
|
||||
from primaite.simulator.network.hardware.base import Node
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.server import Server
|
||||
from primaite.simulator.network.protocols.dns import DNSPacket, DNSRequest
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.network.hardware.nodes.host.server import Server
|
||||
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
from primaite.simulator.system.services.dns.dns_client import DNSClient
|
||||
from primaite.simulator.system.services.dns.dns_server import DNSServer
|
||||
|
||||
|
||||
@@ -18,8 +20,9 @@ def dns_server() -> Node:
|
||||
ip_address="192.168.1.10",
|
||||
subnet_mask="255.255.255.0",
|
||||
default_gateway="192.168.1.1",
|
||||
operating_state=NodeOperatingState.ON,
|
||||
start_up_duration=0,
|
||||
)
|
||||
node.power_on()
|
||||
node.software_manager.install(software_class=DNSServer)
|
||||
return node
|
||||
|
||||
@@ -51,14 +54,16 @@ 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"))
|
||||
|
||||
assert (
|
||||
dns_server_service.receive(payload=DNSPacket(dns_request=DNSRequest(domain_name_request="fake-domain.com")))
|
||||
is False
|
||||
)
|
||||
client = Computer(hostname="client", ip_address="192.168.1.11", subnet_mask="255.255.255.0", start_up_duration=0)
|
||||
client.power_on()
|
||||
client.dns_server = IPv4Address("192.168.1.10")
|
||||
network = Network()
|
||||
network.connect(dns_server.network_interface[1], client.network_interface[1])
|
||||
dns_client: DNSClient = client.software_manager.software["DNSClient"] # noqa
|
||||
dns_client.check_domain_exists("fake-domain.com")
|
||||
|
||||
assert (
|
||||
dns_server_service.receive(payload=DNSPacket(dns_request=DNSRequest(domain_name_request="real-domain.com")))
|
||||
is True
|
||||
)
|
||||
assert dns_client.check_domain_exists("fake-domain.com") is False
|
||||
|
||||
assert dns_client.check_domain_exists("real-domain.com") is False
|
||||
|
||||
dns_server_service.show()
|
||||
|
||||
@@ -5,7 +5,7 @@ import pytest
|
||||
from primaite.simulator.file_system.file_system_item_abc import FileSystemItemHealthStatus
|
||||
from primaite.simulator.network.hardware.base import Node
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.computer import Computer
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.network.protocols.ftp import FTPCommand, FTPPacket, FTPStatusCode
|
||||
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
@@ -20,8 +20,9 @@ def ftp_client() -> Node:
|
||||
ip_address="192.168.1.11",
|
||||
subnet_mask="255.255.255.0",
|
||||
default_gateway="192.168.1.1",
|
||||
operating_state=NodeOperatingState.ON,
|
||||
start_up_duration=0,
|
||||
)
|
||||
node.power_on()
|
||||
return node
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import pytest
|
||||
from primaite.simulator.file_system.file_system_item_abc import FileSystemItemHealthStatus
|
||||
from primaite.simulator.network.hardware.base import Node
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.server import Server
|
||||
from primaite.simulator.network.hardware.nodes.host.server import Server
|
||||
from primaite.simulator.network.protocols.ftp import FTPCommand, FTPPacket, FTPStatusCode
|
||||
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
@@ -18,8 +18,9 @@ def ftp_server() -> Node:
|
||||
ip_address="192.168.1.10",
|
||||
subnet_mask="255.255.255.0",
|
||||
default_gateway="192.168.1.1",
|
||||
operating_state=NodeOperatingState.ON,
|
||||
start_up_duration=0,
|
||||
)
|
||||
node.power_on()
|
||||
node.software_manager.install(software_class=FTPServer)
|
||||
return node
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import pytest
|
||||
|
||||
from primaite.simulator.network.hardware.node_operating_state import NodeOperatingState
|
||||
from primaite.simulator.network.hardware.nodes.server import Server
|
||||
from primaite.simulator.network.hardware.nodes.host.server import Server
|
||||
from primaite.simulator.network.protocols.http import (
|
||||
HttpRequestMethod,
|
||||
HttpRequestPacket,
|
||||
@@ -20,9 +20,10 @@ def web_server() -> Server:
|
||||
ip_address="192.168.1.10",
|
||||
subnet_mask="255.255.255.0",
|
||||
default_gateway="192.168.1.1",
|
||||
operating_state=NodeOperatingState.ON,
|
||||
start_up_duration=0,
|
||||
)
|
||||
node.software_manager.install(software_class=WebServer)
|
||||
node.power_on()
|
||||
node.software_manager.install(WebServer)
|
||||
node.software_manager.software.get("WebServer").start()
|
||||
return node
|
||||
|
||||
@@ -51,19 +52,3 @@ def test_handling_get_request_home_page(web_server):
|
||||
|
||||
response: HttpResponsePacket = web_server_service._handle_get_request(payload=payload)
|
||||
assert response.status_code == HttpStatusCode.OK
|
||||
|
||||
|
||||
def test_process_http_request_get(web_server):
|
||||
payload = HttpRequestPacket(request_method=HttpRequestMethod.GET, request_url="http://domain.com/")
|
||||
|
||||
web_server_service: WebServer = web_server.software_manager.software.get("WebServer")
|
||||
|
||||
assert web_server_service._process_http_request(payload=payload) is True
|
||||
|
||||
|
||||
def test_process_http_request_method_not_allowed(web_server):
|
||||
payload = HttpRequestPacket(request_method=HttpRequestMethod.DELETE, request_url="http://domain.com/")
|
||||
|
||||
web_server_service: WebServer = web_server.software_manager.software.get("WebServer")
|
||||
|
||||
assert web_server_service._process_http_request(payload=payload) is False
|
||||
|
||||
Reference in New Issue
Block a user