Skip to content

Commit

Permalink
Fix for #23
Browse files Browse the repository at this point in the history
  • Loading branch information
blumu authored Jun 24, 2021
1 parent 56fdb67 commit 33fd664
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion createstubs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fi

if [ ! -d "boolean" ]; then
pyright --createstub boolean
sed -i '/class BooleanAlgebra:/a\ TRUE = ...\n FALSE = ...' typings/boolean/boolean.pyi
sed -i '/class BooleanAlgebra(object):/a\ TRUE = ...\n FALSE = ...' typings/boolean/boolean.pyi
else
echo stub 'boolean' already created
fi
Expand Down
11 changes: 4 additions & 7 deletions cyberbattle/simulation/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ class ActionResult(NamedTuple):


ALGEBRA = boolean.BooleanAlgebra()
ALGEBRA.TRUE.dual = type(ALGEBRA.FALSE)
ALGEBRA.FALSE.dual = type(ALGEBRA.TRUE)


@dataclass
Expand Down Expand Up @@ -128,12 +126,11 @@ def _check_prerequisites(self, target: model.NodeID, vulnerability: model.Vulner
node_flags = node.properties
expr = vulnerability.precondition.expression

# this line seems redundant but it is necessary to declare the symbols used in the mapping
# pylint: disable=unused-variable

mapping = {i: ALGEBRA.TRUE if str(i) in node_flags else ALGEBRA.FALSE
true_value = ALGEBRA.parse('true')
false_value = ALGEBRA.parse('false')
mapping = {i: true_value if str(i) in node_flags else false_value
for i in expr.get_symbols()}
is_true: bool = cast(boolean.Expression, expr.subs(mapping)).simplify() == ALGEBRA.TRUE
is_true: bool = cast(boolean.Expression, expr.subs(mapping)).simplify() == true_value
return is_true

def list_vulnerabilities_in_target(
Expand Down
2 changes: 2 additions & 0 deletions cyberbattle/simulation/environment_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def create_random_environment(name: str, size: int) -> model.Environment:
rand_os: str = os_types[random.randint(0, 1)]
nodes[str(i)] = create_random_node(rand_os, potential_ports)

nodes['0'].agent_installed = True

graph.add_nodes_from([(k, {'data': v}) for (k, v) in list(nodes.items())])

return model.Environment(network=graph, vulnerability_library=local_vuln_lib, identifiers=ENV_IDENTIFIERS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The unit tests for the environment_generation functions
"""
from collections import Counter
from cyberbattle.simulation import commandcontrol
from typing import List, Dict
import pytest
from . import environment_generation
Expand Down Expand Up @@ -35,6 +36,15 @@ def test_create_random_environment() -> None:
assert isinstance(result, model.Environment)


def test_random_environment_list_attacks() -> None:
"""
Unit tests for #23 caused by bug https://github.com/bastikr/boolean.py/issues/82 in boolean.py
"""
env = environment_generation.create_random_environment('test', 10)
c2 = commandcontrol.CommandControl(env)
c2.print_all_attacks()


def test_create_random_node() -> None:
"""
The unit tests for create_random_node() function
Expand Down
5 changes: 0 additions & 5 deletions cyberbattle/simulation/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@
VERSION_TAG = "0.1.0"

ALGEBRA = boolean.BooleanAlgebra()
# These two lines define True as the dual of False and vice versa
# it's necessary in order to make sure the simplify function in boolean.py
# works correctly. See https://github.com/bastikr/boolean.py/issues/82
ALGEBRA.TRUE.dual = type(ALGEBRA.FALSE)
ALGEBRA.FALSE.dual = type(ALGEBRA.TRUE)

# Type alias for identifiers
NodeID = str
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
gym~=0.17.3
numpy==1.19.4
boolean.py==3.8
boolean.py@git+https://github.com/bastikr/boolean.py/@74063a8588875c058e6dbbb85b69ed052e1f2099#egg=numpy_stubs
networkx==2.4
pyyaml~=5.4.1
setuptools~=49.2.1
Expand Down

0 comments on commit 33fd664

Please sign in to comment.