Merge remote-tracking branch 'origin/dev' into feature/2533-improve-dev-tools-in-primaite-cli
This commit is contained in:
77
tests/assets/configs/wireless_wan_network_config.yaml
Normal file
77
tests/assets/configs/wireless_wan_network_config.yaml
Normal file
@@ -0,0 +1,77 @@
|
||||
game:
|
||||
max_episode_length: 256
|
||||
ports:
|
||||
- ARP
|
||||
protocols:
|
||||
- ICMP
|
||||
- TCP
|
||||
- UDP
|
||||
|
||||
simulation:
|
||||
network:
|
||||
nodes:
|
||||
- type: computer
|
||||
hostname: pc_a
|
||||
ip_address: 192.168.0.2
|
||||
subnet_mask: 255.255.255.0
|
||||
default_gateway: 192.168.0.1
|
||||
start_up_duration: 0
|
||||
|
||||
- type: computer
|
||||
hostname: pc_b
|
||||
ip_address: 192.168.2.2
|
||||
subnet_mask: 255.255.255.0
|
||||
default_gateway: 192.168.2.1
|
||||
start_up_duration: 0
|
||||
|
||||
- type: wireless_router
|
||||
hostname: router_1
|
||||
start_up_duration: 0
|
||||
|
||||
router_interface:
|
||||
ip_address: 192.168.0.1
|
||||
subnet_mask: 255.255.255.0
|
||||
|
||||
wireless_access_point:
|
||||
ip_address: 192.168.1.1
|
||||
subnet_mask: 255.255.255.0
|
||||
frequency: WIFI_2_4
|
||||
acl:
|
||||
1:
|
||||
action: PERMIT
|
||||
routes:
|
||||
- address: 192.168.2.0 # PC B subnet
|
||||
subnet_mask: 255.255.255.0
|
||||
next_hop_ip_address: 192.168.1.2
|
||||
metric: 0
|
||||
|
||||
- type: wireless_router
|
||||
hostname: router_2
|
||||
start_up_duration: 0
|
||||
|
||||
router_interface:
|
||||
ip_address: 192.168.2.1
|
||||
subnet_mask: 255.255.255.0
|
||||
|
||||
wireless_access_point:
|
||||
ip_address: 192.168.1.2
|
||||
subnet_mask: 255.255.255.0
|
||||
frequency: WIFI_2_4
|
||||
acl:
|
||||
1:
|
||||
action: PERMIT
|
||||
routes:
|
||||
- address: 192.168.0.0 # PC A subnet
|
||||
subnet_mask: 255.255.255.0
|
||||
next_hop_ip_address: 192.168.1.1
|
||||
metric: 0
|
||||
links:
|
||||
- endpoint_a_hostname: pc_a
|
||||
endpoint_a_port: 1
|
||||
endpoint_b_hostname: router_1
|
||||
endpoint_b_port: 2
|
||||
|
||||
- endpoint_a_hostname: pc_b
|
||||
endpoint_a_port: 1
|
||||
endpoint_b_hostname: router_2
|
||||
endpoint_b_port: 2
|
||||
@@ -1,16 +1,18 @@
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from primaite.simulator.network.airspace import AIR_SPACE, AirSpaceFrequency
|
||||
from primaite.game.game import PrimaiteGame
|
||||
from primaite.simulator.network.container import Network
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.network.hardware.nodes.network.router import ACLAction
|
||||
from primaite.simulator.network.hardware.nodes.network.wireless_router import WirelessRouter
|
||||
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
from tests import TEST_ASSETS_ROOT
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def setup_network():
|
||||
def wireless_wan_network():
|
||||
network = Network()
|
||||
|
||||
# Configure PC A
|
||||
@@ -25,7 +27,7 @@ def setup_network():
|
||||
network.add_node(pc_a)
|
||||
|
||||
# Configure Router 1
|
||||
router_1 = WirelessRouter(hostname="router_1", start_up_duration=0)
|
||||
router_1 = WirelessRouter(hostname="router_1", start_up_duration=0, airspace=network.airspace)
|
||||
router_1.power_on()
|
||||
network.add_node(router_1)
|
||||
|
||||
@@ -49,7 +51,7 @@ def setup_network():
|
||||
network.add_node(pc_b)
|
||||
|
||||
# Configure Router 2
|
||||
router_2 = WirelessRouter(hostname="router_2", start_up_duration=0)
|
||||
router_2 = WirelessRouter(hostname="router_2", start_up_duration=0, airspace=network.airspace)
|
||||
router_2.power_on()
|
||||
network.add_node(router_2)
|
||||
|
||||
@@ -63,7 +65,7 @@ def setup_network():
|
||||
router_1.configure_wireless_access_point("192.168.1.1", "255.255.255.0")
|
||||
router_2.configure_wireless_access_point("192.168.1.2", "255.255.255.0")
|
||||
|
||||
AIR_SPACE.show()
|
||||
network.airspace.show()
|
||||
|
||||
router_1.route_table.add_route(
|
||||
address="192.168.2.0", subnet_mask="255.255.255.0", next_hop_ip_address="192.168.1.2"
|
||||
@@ -77,11 +79,35 @@ def setup_network():
|
||||
return pc_a, pc_b, router_1, router_2
|
||||
|
||||
|
||||
def test_cross_router_connectivity(setup_network):
|
||||
pc_a, pc_b, router_1, router_2 = setup_network
|
||||
@pytest.fixture(scope="function")
|
||||
def wireless_wan_network_from_config_yaml():
|
||||
config_path = TEST_ASSETS_ROOT / "configs" / "wireless_wan_network_config.yaml"
|
||||
|
||||
with open(config_path, "r") as f:
|
||||
config_dict = yaml.safe_load(f)
|
||||
network = PrimaiteGame.from_config(cfg=config_dict).simulation.network
|
||||
|
||||
network.airspace.show()
|
||||
|
||||
return network
|
||||
|
||||
|
||||
def test_cross_wireless_wan_connectivity(wireless_wan_network):
|
||||
pc_a, pc_b, router_1, router_2 = wireless_wan_network
|
||||
# Ensure that PCs can ping across routers before any frequency change
|
||||
assert pc_a.ping(pc_a.default_gateway), "PC A should ping its default gateway successfully."
|
||||
assert pc_b.ping(pc_b.default_gateway), "PC B should ping its default gateway successfully."
|
||||
|
||||
assert pc_a.ping(pc_b.network_interface[1].ip_address), "PC A should ping PC B across routers successfully."
|
||||
assert pc_b.ping(pc_a.network_interface[1].ip_address), "PC B should ping PC A across routers successfully."
|
||||
|
||||
|
||||
def test_cross_wireless_wan_connectivity_from_yaml(wireless_wan_network_from_config_yaml):
|
||||
pc_a = wireless_wan_network_from_config_yaml.get_node_by_hostname("pc_a")
|
||||
pc_b = wireless_wan_network_from_config_yaml.get_node_by_hostname("pc_b")
|
||||
|
||||
assert pc_a.ping(pc_a.default_gateway), "PC A should ping its default gateway successfully."
|
||||
assert pc_b.ping(pc_b.default_gateway), "PC B should ping its default gateway successfully."
|
||||
|
||||
assert pc_a.ping(pc_b.network_interface[1].ip_address), "PC A should ping PC B across routers successfully."
|
||||
assert pc_b.ping(pc_a.network_interface[1].ip_address), "PC B should ping PC A across routers successfully."
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
from primaite.simulator.network.hardware.nodes.network.router import ACLAction
|
||||
from primaite.simulator.network.hardware.nodes.network.wireless_router import WirelessRouter
|
||||
from primaite.simulator.network.transmission.network_layer import IPProtocol
|
||||
from primaite.simulator.network.transmission.transport_layer import Port
|
||||
|
||||
|
||||
def test_wireless_router_from_config():
|
||||
cfg = {
|
||||
"ref": "router_2",
|
||||
"type": "wireless_router",
|
||||
"hostname": "router_2",
|
||||
"router_interface": {
|
||||
"ip_address": "192.168.1.1",
|
||||
"subnet_mask": "255.255.255.0",
|
||||
},
|
||||
"wireless_access_point": {
|
||||
"ip_address": "192.170.1.1",
|
||||
"subnet_mask": "255.255.255.0",
|
||||
"frequency": "WIFI_2_4",
|
||||
},
|
||||
"acl": {
|
||||
0: {
|
||||
"action": "PERMIT",
|
||||
"src_port": "POSTGRES_SERVER",
|
||||
"dst_port": "POSTGRES_SERVER",
|
||||
},
|
||||
1: {
|
||||
"action": "PERMIT",
|
||||
"protocol": "ICMP",
|
||||
},
|
||||
2: {
|
||||
"action": "PERMIT",
|
||||
"src_ip": "100.100.100.1",
|
||||
"dst_ip": "100.100.101.1",
|
||||
},
|
||||
3: {
|
||||
"action": "PERMIT",
|
||||
"src_ip": "100.100.102.0",
|
||||
"dst_ip": "100.100.103.0",
|
||||
"src_wildcard_mask": "0.0.0.255",
|
||||
"dst_wildcard_mask": "0.0.0.255",
|
||||
},
|
||||
20: {
|
||||
"action": "DENY",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
rt = WirelessRouter.from_config(cfg=cfg)
|
||||
|
||||
r0 = rt.acl.acl[0]
|
||||
assert r0.action == ACLAction.PERMIT
|
||||
assert r0.src_port == r0.dst_port == Port.POSTGRES_SERVER
|
||||
assert r0.src_ip_address == r0.dst_ip_address == r0.dst_wildcard_mask == r0.src_wildcard_mask == r0.protocol == None
|
||||
|
||||
r1 = rt.acl.acl[1]
|
||||
assert r1.action == ACLAction.PERMIT
|
||||
assert r1.protocol == IPProtocol.ICMP
|
||||
assert (
|
||||
r1.src_ip_address
|
||||
== r1.dst_ip_address
|
||||
== r1.dst_wildcard_mask
|
||||
== r1.src_wildcard_mask
|
||||
== r1.src_port
|
||||
== r1.dst_port
|
||||
== None
|
||||
)
|
||||
|
||||
r2 = rt.acl.acl[2]
|
||||
assert r2.action == ACLAction.PERMIT
|
||||
assert r2.src_ip_address == IPv4Address("100.100.100.1")
|
||||
assert r2.dst_ip_address == IPv4Address("100.100.101.1")
|
||||
assert r2.src_wildcard_mask == r2.dst_wildcard_mask == None
|
||||
assert r2.src_port == r2.dst_port == r2.protocol == None
|
||||
|
||||
r3 = rt.acl.acl[3]
|
||||
assert r3.action == ACLAction.PERMIT
|
||||
assert r3.src_ip_address == IPv4Address("100.100.102.0")
|
||||
assert r3.dst_ip_address == IPv4Address("100.100.103.0")
|
||||
assert r3.src_wildcard_mask == IPv4Address("0.0.0.255")
|
||||
assert r3.dst_wildcard_mask == IPv4Address("0.0.0.255")
|
||||
assert r3.src_port == r3.dst_port == r3.protocol == None
|
||||
|
||||
r20 = rt.acl.acl[20]
|
||||
assert r20.action == ACLAction.DENY
|
||||
assert (
|
||||
r20.src_ip_address
|
||||
== r20.dst_ip_address
|
||||
== r20.src_wildcard_mask
|
||||
== r20.dst_wildcard_mask
|
||||
== r20.src_port
|
||||
== r20.dst_port
|
||||
== r20.protocol
|
||||
== None
|
||||
)
|
||||
Reference in New Issue
Block a user