Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/CyberBattleSim
Path: blob/main/cyberbattle/simulation/environment_generation_test.py
597 views
1
# Copyright (c) Microsoft Corporation.
2
# Licensed under the MIT License.
3
4
"""
5
The unit tests for the environment_generation functions
6
"""
7
8
from collections import Counter
9
from cyberbattle.simulation import commandcontrol
10
from typing import List, Dict
11
import pytest
12
from . import environment_generation
13
from . import model
14
15
windows_vulns: Dict[str, model.VulnerabilityInfo] = environment_generation.potential_windows_vulns
16
linux_vulns: Dict[str, model.VulnerabilityInfo] = environment_generation.potential_linux_vulns
17
18
windows_node_states: List[model.PropertyName] = environment_generation.potential_linux_node_states
19
linux_node_states: List[model.PropertyName] = environment_generation.potential_linux_node_states
20
21
potential_ports: List[model.PortName] = environment_generation.potential_ports
22
23
24
def test_create_random_environment() -> None:
25
"""
26
The unit tests for create_random_environment function
27
"""
28
with pytest.raises(ValueError, match=r"Please supply a non empty string for the name"):
29
environment_generation.create_random_environment("", 2)
30
31
with pytest.raises(ValueError, match=r"Please supply a positive non zero positive" r"integer for the size of the environment"):
32
environment_generation.create_random_environment("Test_environment", -5)
33
34
result: model.Environment = environment_generation.create_random_environment("Test_environment 2", 4)
35
assert isinstance(result, model.Environment)
36
37
38
def test_random_environment_list_attacks() -> None:
39
"""
40
Unit tests for #23 caused by bug https://github.com/bastikr/boolean.py/issues/82 in boolean.py
41
"""
42
env = environment_generation.create_random_environment("test", 10)
43
c2 = commandcontrol.CommandControl(env)
44
c2.print_all_attacks()
45
46
47
def test_create_random_node() -> None:
48
"""
49
The unit tests for create_random_node() function
50
"""
51
52
# check that the correct exceptions are generated
53
with pytest.raises(ValueError, match=r"No endpoints supplied"):
54
environment_generation.create_random_node("Linux", [])
55
56
with pytest.raises(ValueError, match=r"Unsupported OS Type please enter Linux or Windows"):
57
environment_generation.create_random_node("Solaris", potential_ports)
58
59
test_node: model.NodeInfo = environment_generation.create_random_node("Linux", potential_ports)
60
61
assert isinstance(test_node, model.NodeInfo)
62
63
64
def test_get_properties_from_vulnerabilities() -> None:
65
"""
66
This function tests the get_properties_from_vulnerabilities function
67
It takes nothing and returns nothing.
68
"""
69
# testing on linux vulns
70
props: List[model.PropertyName] = environment_generation.get_properties_from_vulnerabilities("Linux", linux_vulns)
71
assert "Linux" in props
72
assert "PortSSHOpen" in props
73
assert "PortSMBOpen" in props
74
75
# testing on Windows vulns
76
windows_props: List[model.PropertyName] = environment_generation.get_properties_from_vulnerabilities("Windows", windows_vulns)
77
assert "Windows" in windows_props
78
assert "PortRDPOpen" in windows_props
79
assert "PortSMBOpen" in windows_props
80
assert "DomainJoined" in windows_props
81
assert "Win10" in windows_props
82
assert "Win7" in windows_props
83
84
85
def test_create_firewall_rules() -> None:
86
"""
87
This function tests the create_firewall_rules function.
88
It takes nothing and returns nothing.
89
"""
90
empty_ports: List[model.PortName] = []
91
potential_port_list: List[model.PortName] = ["RDP", "SSH", "HTTP", "HTTPs", "SMB", "SQL", "FTP", "WMI"]
92
half_ports: List[model.PortName] = ["SSH", "HTTPs", "SQL", "FTP", "WMI"]
93
all_blocked: List[model.FirewallRule] = [model.FirewallRule(port, model.RulePermission.BLOCK) for port in potential_port_list]
94
all_allowed: List[model.FirewallRule] = [model.FirewallRule(port, model.RulePermission.ALLOW) for port in potential_port_list]
95
half_allowed: List[model.FirewallRule] = [
96
model.FirewallRule(port, model.RulePermission.ALLOW) if port in half_ports else model.FirewallRule(port, model.RulePermission.BLOCK) for port in potential_port_list
97
]
98
99
# testing on an empty list should lead to
100
results: model.FirewallConfiguration = environment_generation.create_firewall_rules(empty_ports)
101
assert Counter(results.incoming) == Counter(all_blocked)
102
assert Counter(results.outgoing) == Counter(all_blocked)
103
# testing on a the list supported ports
104
results = environment_generation.create_firewall_rules(potential_ports)
105
assert Counter(results.incoming) == Counter(all_allowed)
106
assert Counter(results.outgoing) == Counter(all_allowed)
107
108
results = environment_generation.create_firewall_rules(half_ports)
109
assert Counter(results.incoming) == Counter(half_allowed)
110
assert Counter(results.outgoing) == Counter(half_allowed)
111
112