#1800 - Added ACL and routing classes.
- Added .show() methods to new router classes to enable inspection of the components as you would a real router. - Removed gateway from the NIC and added default_gateway to Node so that Node has a single default gateway. - Added some routing tests to check that ping can be performed when router between subnets.
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
from primaite.simulator.network.hardware.nodes.router import AccessControlList, ACLAction, ACLRule
|
||||
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
|
||||
|
||||
def test_add_rule():
|
||||
acl = AccessControlList()
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip=IPv4Address("192.168.1.1"),
|
||||
src_port=Port(8080),
|
||||
dst_ip=IPv4Address("192.168.1.2"),
|
||||
dst_port=Port(80),
|
||||
position=1,
|
||||
)
|
||||
assert acl.acl[1].action == ACLAction.PERMIT
|
||||
assert acl.acl[1].protocol == IPProtocol.TCP
|
||||
assert acl.acl[1].src_ip == IPv4Address("192.168.1.1")
|
||||
assert acl.acl[1].src_port == Port(8080)
|
||||
assert acl.acl[1].dst_ip == IPv4Address("192.168.1.2")
|
||||
assert acl.acl[1].dst_port == Port(80)
|
||||
|
||||
|
||||
def test_remove_rule():
|
||||
acl = AccessControlList()
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip=IPv4Address("192.168.1.1"),
|
||||
src_port=Port(8080),
|
||||
dst_ip=IPv4Address("192.168.1.2"),
|
||||
dst_port=Port(80),
|
||||
position=1,
|
||||
)
|
||||
acl.remove_rule(1)
|
||||
assert not acl.acl[1]
|
||||
|
||||
|
||||
def test_rules():
|
||||
acl = AccessControlList()
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip=IPv4Address("192.168.1.1"),
|
||||
src_port=Port(8080),
|
||||
dst_ip=IPv4Address("192.168.1.2"),
|
||||
dst_port=Port(80),
|
||||
position=1,
|
||||
)
|
||||
acl.add_rule(
|
||||
action=ACLAction.DENY,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip=IPv4Address("192.168.1.3"),
|
||||
src_port=Port(8080),
|
||||
dst_ip=IPv4Address("192.168.1.4"),
|
||||
dst_port=Port(80),
|
||||
position=2,
|
||||
)
|
||||
assert acl.is_permitted(
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip=IPv4Address("192.168.1.1"),
|
||||
src_port=Port(8080),
|
||||
dst_ip=IPv4Address("192.168.1.2"),
|
||||
dst_port=Port(80),
|
||||
)
|
||||
assert not acl.is_permitted(
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip=IPv4Address("192.168.1.3"),
|
||||
src_port=Port(8080),
|
||||
dst_ip=IPv4Address("192.168.1.4"),
|
||||
dst_port=Port(80),
|
||||
)
|
||||
|
||||
|
||||
def test_default_rule():
|
||||
acl = AccessControlList()
|
||||
acl.add_rule(
|
||||
action=ACLAction.PERMIT,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip=IPv4Address("192.168.1.1"),
|
||||
src_port=Port(8080),
|
||||
dst_ip=IPv4Address("192.168.1.2"),
|
||||
dst_port=Port(80),
|
||||
position=1,
|
||||
)
|
||||
acl.add_rule(
|
||||
action=ACLAction.DENY,
|
||||
protocol=IPProtocol.TCP,
|
||||
src_ip=IPv4Address("192.168.1.3"),
|
||||
src_port=Port(8080),
|
||||
dst_ip=IPv4Address("192.168.1.4"),
|
||||
dst_port=Port(80),
|
||||
position=2,
|
||||
)
|
||||
assert not acl.is_permitted(
|
||||
protocol=IPProtocol.UDP,
|
||||
src_ip=IPv4Address("192.168.1.5"),
|
||||
src_port=Port(8080),
|
||||
dst_ip=IPv4Address("192.168.1.12"),
|
||||
dst_port=Port(80),
|
||||
)
|
||||
@@ -32,10 +32,8 @@ def test_nic_ip_address_type_conversion():
|
||||
nic = NIC(
|
||||
ip_address="192.168.1.2",
|
||||
subnet_mask="255.255.255.0",
|
||||
gateway="192.168.0.1",
|
||||
)
|
||||
assert isinstance(nic.ip_address, IPv4Address)
|
||||
assert isinstance(nic.gateway, IPv4Address)
|
||||
|
||||
|
||||
def test_nic_deserialize():
|
||||
@@ -43,7 +41,6 @@ def test_nic_deserialize():
|
||||
nic = NIC(
|
||||
ip_address="192.168.1.2",
|
||||
subnet_mask="255.255.255.0",
|
||||
gateway="192.168.0.1",
|
||||
)
|
||||
|
||||
nic_json = nic.model_dump_json()
|
||||
@@ -51,21 +48,10 @@ def test_nic_deserialize():
|
||||
assert nic == deserialized_nic
|
||||
|
||||
|
||||
def test_nic_ip_address_as_gateway_fails():
|
||||
"""Tests NIC creation fails if ip address is the same as the gateway."""
|
||||
with pytest.raises(ValueError):
|
||||
NIC(
|
||||
ip_address="192.168.0.1",
|
||||
subnet_mask="255.255.255.0",
|
||||
gateway="192.168.0.1",
|
||||
)
|
||||
|
||||
|
||||
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",
|
||||
gateway="192.168.0.1",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user