Adopted machine unit tests

pull/44/head
Thorsten Sick 3 years ago
parent 4721bc986c
commit cae79c5393

@ -6,6 +6,7 @@ from app.exceptions import ConfigurationError
from app.config import MachineConfig from app.config import MachineConfig
from unittest.mock import patch from unittest.mock import patch
from app.attack_log import AttackLog from app.attack_log import AttackLog
from app.config_verifier import Attacker, Target
# https://docs.python.org/3/library/unittest.html # https://docs.python.org/3/library/unittest.html
@ -16,13 +17,23 @@ class TestMachineControl(unittest.TestCase):
self.attack_logger = AttackLog(0) self.attack_logger = AttackLog(0)
def test_get_os_linux_machine(self): def test_get_os_linux_machine(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = { # "root": "systems/attacker1",
"os": "linux", "os": "linux",
"vm_controller": { "vm_name": "foo_bar",
"vm_type": "vagrant", "vm_controller": {
"vagrantfilepath": "systems", "vm_type": "vagrant",
}, "vagrantfilepath": "systems",
"vm_name": "target3"}), self.attack_logger) },
"vm_name": "target3",
"machinepath": "target3",
"nicknames": [],
"sensors": [],
"paw": "ignoreme",
"name": "Foobar",
"group": "some_group",
}
m = Machine(Target(**conf), self.attack_logger)
self.assertEqual(m.get_os(), "linux") self.assertEqual(m.get_os(), "linux")
def test_get_os_linux_machine_with_config_class(self): def test_get_os_linux_machine_with_config_class(self):
@ -37,48 +48,78 @@ class TestMachineControl(unittest.TestCase):
self.assertEqual(m.get_os(), "linux") self.assertEqual(m.get_os(), "linux")
def test_get_paw_good(self): def test_get_paw_good(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = { # "root": "systems/attacker1",
"os": "linux", "os": "linux",
"paw": "testme", "vm_name": "foo_bar",
"vm_controller": { "vm_controller": {
"vm_type": "vagrant", "vm_type": "vagrant",
"vagrantfilepath": "systems", "vagrantfilepath": "systems",
}, },
"vm_name": "target3"}), self.attack_logger) "vm_name": "target3",
"machinepath": "target3",
"nicknames": [],
"sensors": [],
"paw": "testme",
"name": "Foobar",
"group": "some_group",
}
m = Machine(Target(**conf), self.attack_logger)
self.assertEqual(m.get_paw(), "testme") self.assertEqual(m.get_paw(), "testme")
def test_get_paw_missing(self): def test_get_paw_missing(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = {# "root": "systems/attacker1",
"os": "linux", "os": "linux",
"vm_controller": { "vm_name": "foo_bar",
"vm_type": "vagrant", "vm_controller": {
"vagrantfilepath": "systems", "vm_type": "vagrant",
}, "vagrantfilepath": "systems",
"vm_name": "target3" },
}), self.attack_logger) "vm_name": "target3",
self.assertEqual(m.get_paw(), None) "machinepath": "target3",
"nicknames": [],
"sensors": [],
"name": "Foobar",
"group": "some_group",
}
with self.assertRaisesRegex(TypeError, 'paw'):
m = Machine(Target(**conf), self.attack_logger)
def test_get_group_good(self): def test_get_group_good(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = {# "root": "systems/attacker1",
"os": "linux", "os": "linux",
"group": "testme", "vm_name": "foo_bar",
"vm_controller": { "vm_controller": {
"vm_type": "vagrant", "vm_type": "vagrant",
"vagrantfilepath": "systems", "vagrantfilepath": "systems",
}, },
"vm_name": "target3"}), self.attack_logger) "vm_name": "target3",
"machinepath": "target3",
"nicknames": [],
"sensors": [],
"name": "Foobar",
"paw": "some_paw",
"group": "testme"
}
m = Machine(Target(**conf), self.attack_logger)
self.assertEqual(m.get_group(), "testme") self.assertEqual(m.get_group(), "testme")
def test_get_group_missing(self): def test_get_group_missing(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = {# "root": "systems/attacker1",
"os": "linux", "os": "linux",
"vm_controller": { "vm_name": "foo_bar",
"vm_type": "vagrant", "vm_controller": {
"vagrantfilepath": "systems", "vm_type": "vagrant",
}, "vagrantfilepath": "systems",
"vm_name": "target3" },
}), self.attack_logger) "vm_name": "target3",
self.assertEqual(m.get_group(), None) "machinepath": "target3",
"nicknames": [],
"sensors": [],
"name": "Foobar",
"paw": "some_paw",
}
with self.assertRaisesRegex(TypeError, 'group'):
m = Machine(Target(**conf), self.attack_logger)
def test_vagrantfilepath_missing(self): def test_vagrantfilepath_missing(self):
with self.assertRaises(ConfigurationError): with self.assertRaises(ConfigurationError):
@ -102,14 +143,18 @@ class TestMachineControl(unittest.TestCase):
}), self.attack_logger) }), self.attack_logger)
def test_vagrantfile_existing(self): def test_vagrantfile_existing(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = {# "root": "systems/attacker1",
"os": "linux", "os": "linux",
"vm_controller": { "vm_controller": {
"vm_type": "vagrant", "vm_type": "vagrant",
"vagrantfilepath": "systems", "vagrantfilepath": "systems",
}, },
"vm_name": "target3" "vm_name": "target3",
}), self.attack_logger) "name": "test_attacker",
"nicknames": ["a","b"],
"machinepath": "attacker1"
}
m = Machine(Attacker(**conf), self.attack_logger)
self.assertIsNotNone(m) self.assertIsNotNone(m)
# test: auto generated, dir missing # test: auto generated, dir missing
@ -138,33 +183,45 @@ class TestMachineControl(unittest.TestCase):
}), self.attack_logger) }), self.attack_logger)
# test auto generated, dir there (external/internal dirs must work !) # test auto generated, dir there (external/internal dirs must work !)
def test_auto_generated_machinepath_with_good_config(self): def test_missing_machinepath_with_good_config_eeception(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = {# "root": "systems/attacker1",
"os": "linux", "os": "linux",
"vm_controller": { "vm_name": "foo_bar",
"vm_type": "vagrant", "vm_controller": {
"vagrantfilepath": "systems", "vm_type": "vagrant",
}, "vagrantfilepath": "systems",
"vm_name": "target3" },
}), self.attack_logger) "vm_name": "target3",
vagrantfilepath = os.path.abspath("systems") "nicknames": [],
ext = os.path.join(vagrantfilepath, "target3") "sensors": [],
internal = os.path.join("/vagrant/", "target3") "name": "Foobar",
"paw": "some_paw",
"group": "some_group",
}
self.assertEqual(m.abs_machinepath_external, ext) with self.assertRaisesRegex(TypeError, "machinepath"):
self.assertEqual(m.abs_machinepath_internal, internal) m = Machine(Target(**conf), self.attack_logger)
# test: manual config, dir there (external/internal dirs must work !) # test: manual config, dir there (external/internal dirs must work !)
def test_configured_machinepath_with_good_config(self): def test_configured_machinepath_with_good_config(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = {# "root": "systems/attacker1",
"os": "linux", "os": "linux",
"vm_controller": { "vm_name": "foo_bar",
"vm_type": "vagrant", "vm_controller": {
"vagrantfilepath": "systems", "vm_type": "vagrant",
}, "vagrantfilepath": "systems",
"vm_name": "missing", },
"machinepath": "target3" "vm_name": "target3",
}), self.attack_logger) "machinepath": "target3",
"nicknames": [],
"sensors": [],
"name": "Foobar",
"paw": "some_paw",
"group": "some_group",
}
m = Machine(Target(**conf), self.attack_logger)
vagrantfilepath = os.path.abspath("systems") vagrantfilepath = os.path.abspath("systems")
ext = os.path.join(vagrantfilepath, "target3") ext = os.path.join(vagrantfilepath, "target3")
internal = os.path.join("/vagrant/", "target3") internal = os.path.join("/vagrant/", "target3")
@ -183,15 +240,21 @@ class TestMachineControl(unittest.TestCase):
# Create caldera start command and verify it # Create caldera start command and verify it
def test_get_linux_caldera_start_cmd(self): def test_get_linux_caldera_start_cmd(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = {# "root": "systems/attacker1",
"os": "linux", "os": "linux",
"vm_controller": { "vm_controller": {
"vm_type": "vagrant", "vm_type": "vagrant",
"vagrantfilepath": "systems", "vagrantfilepath": "systems",
}, },
"vm_name": "target3", "vm_name": "target3",
"group": "testgroup", "group": "testgroup",
"paw": "testpaw"}), self.attack_logger) "paw": "testpaw",
"name": "test_attacker",
"nicknames": ["a","b"],
"machinepath": "target3",
"sensors": []
}
m = Machine(Target(**conf), self.attack_logger)
m.set_caldera_server("http://www.test.test") m.set_caldera_server("http://www.test.test")
with patch.object(m.vm_manager, "get_playground", return_value="/vagrant/target3"): with patch.object(m.vm_manager, "get_playground", return_value="/vagrant/target3"):
cmd = m.create_start_caldera_client_cmd() cmd = m.create_start_caldera_client_cmd()
@ -199,16 +262,21 @@ class TestMachineControl(unittest.TestCase):
# Create caldera start command and verify it (windows) # Create caldera start command and verify it (windows)
def test_get_windows_caldera_start_cmd(self): def test_get_windows_caldera_start_cmd(self):
m = Machine(DotMap({"root": "systems/attacker1", conf = {# "root": "systems/attacker1",
"os": "windows", "os": "windows",
"vm_controller": { "vm_controller": {
"vm_type": "vagrant", "vm_type": "vagrant",
"vagrantfilepath": "systems", "vagrantfilepath": "systems",
}, },
"vm_name": "target3", "vm_name": "target3",
"group": "testgroup", "group": "testgroup",
"paw": "testpaw", "paw": "testpaw",
"machinepath": "target3"}), self.attack_logger) "name": "test_attacker",
"nicknames": ["a","b"],
"machinepath": "target3",
"sensors": []
}
m = Machine(Target(**conf), self.attack_logger)
m.set_caldera_server("www.test.test") m.set_caldera_server("www.test.test")
cmd = m.create_start_caldera_client_cmd() cmd = m.create_start_caldera_client_cmd()
self.maxDiff = None self.maxDiff = None

Loading…
Cancel
Save