diff --git a/tests/utils.py b/tests/__utils__.py similarity index 100% rename from tests/utils.py rename to tests/__utils__.py diff --git a/tests/test_base.py b/tests/test_base.py index 42ef177..4393fd6 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -1,55 +1,55 @@ -#!/usr/bin/env python -# -*- coding: UTF-8 -*- -"""Core entity assets' tests. - -""" -from utils import * - - -class TestBase(TestCase): - def test_general_commands(self): - self.assertRaises(SystemExit, Entity.get_subclass(Command, "Back")().run) - self.assertRaises(SystemExit, Entity.get_subclass(Command, "Exit")().run) - for Help in Entity.get_subclass(Command, "Help"): - if Help.level == "general": - self.assertIsNone(Help().run()) - search = Entity.get_subclass(Command, "Search")() - self.assertIsNone(search.run("does_not_exist")) - self.assertIsNone(search.run("first")) - for Show in Entity.get_subclass(Command, "Show"): - if Show.level == "root": - Show.keys += ["issues"] - Show().set_keys() - for k in Show.keys + ["issues"]: - self.assertIsNotNone(Show().complete_values(k)) - self.assertIsNone(Show().run(k)) - if k == "options": - self.assertIsNone(Show().run(k, "DEBUG")) - break - Set = Entity.get_subclass(Command, "Set") - keys = list(Set().complete_keys()) - self.assertTrue(len(keys) > 0) - self.assertIsNotNone(Set().complete_values(keys[0])) - self.assertIsNotNone(Set().complete_values("WORKSPACE")) - self.assertRaises(ValueError, Set().validate, "BAD", "whatever") - self.assertRaises(ValueError, Set().validate, "WORKSPACE", None) - self.assertRaises(ValueError, Set().validate, "DEBUG", "whatever") - self.assertIsNone(Set().run("DEBUG", "false")) - Unset = Entity.get_subclass(Command, "Unset") - self.assertTrue(len(list(Unset().complete_values())) > 0) - self.assertRaises(ValueError, Unset().validate, "BAD") - self.assertRaises(ValueError, Unset().validate, "WORKSPACE") - self.assertRaises(ValueError, Unset().run, "DEBUG") - self.assertIsNone(Set().run("DEBUG", "false")) - - def test_root_commands(self): - for Help in Entity.get_subclass(Command, "Help"): - if Help.level == "root": - self.assertIsNone(Help().validate()) - self.assertRaises(ValueError, Help().validate, "BAD") - self.assertIsNone(Help().run()) - for k in Help().keys: - for v in Help().complete_values(k): - self.assertIsNone(Help().run(k, v)) - break - +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +"""Core entity assets' tests. + +""" +from __utils__ import * + + +class TestBase(TestCase): + def test_general_commands(self): + self.assertRaises(SystemExit, Entity.get_subclass(Command, "Back")().run) + self.assertRaises(SystemExit, Entity.get_subclass(Command, "Exit")().run) + for Help in Entity.get_subclass(Command, "Help"): + if Help.level == "general": + self.assertIsNone(Help().run()) + search = Entity.get_subclass(Command, "Search")() + self.assertIsNone(search.run("does_not_exist")) + self.assertIsNone(search.run("first")) + for Show in Entity.get_subclass(Command, "Show"): + if Show.level == "root": + Show.keys += ["issues"] + Show().set_keys() + for k in Show.keys + ["issues"]: + self.assertIsNotNone(Show().complete_values(k)) + self.assertIsNone(Show().run(k)) + if k == "options": + self.assertIsNone(Show().run(k, "DEBUG")) + break + Set = Entity.get_subclass(Command, "Set") + keys = list(Set().complete_keys()) + self.assertTrue(len(keys) > 0) + self.assertIsNotNone(Set().complete_values(keys[0])) + self.assertIsNotNone(Set().complete_values("WORKSPACE")) + self.assertRaises(ValueError, Set().validate, "BAD", "whatever") + self.assertRaises(ValueError, Set().validate, "WORKSPACE", None) + self.assertRaises(ValueError, Set().validate, "DEBUG", "whatever") + self.assertIsNone(Set().run("DEBUG", "false")) + Unset = Entity.get_subclass(Command, "Unset") + self.assertTrue(len(list(Unset().complete_values())) > 0) + self.assertRaises(ValueError, Unset().validate, "BAD") + self.assertRaises(ValueError, Unset().validate, "WORKSPACE") + self.assertRaises(ValueError, Unset().run, "DEBUG") + self.assertIsNone(Set().run("DEBUG", "false")) + + def test_root_commands(self): + for Help in Entity.get_subclass(Command, "Help"): + if Help.level == "root": + self.assertIsNone(Help().validate()) + self.assertRaises(ValueError, Help().validate, "BAD") + self.assertIsNone(Help().run()) + for k in Help().keys: + for v in Help().complete_values(k): + self.assertIsNone(Help().run(k, v)) + break + diff --git a/tests/test_components.py b/tests/test_components.py index 0dc1544..a9150c5 100644 --- a/tests/test_components.py +++ b/tests/test_components.py @@ -1,29 +1,29 @@ -#!/usr/bin/env python -# -*- coding: UTF-8 -*- -"""Core entity assets' tests. - -""" -from sploitkit.core.components import * - -from utils import * - - -class TestComponents(TestCase): - def test_store(self): - s = CONSOLE._storage - self.assertTrue(isinstance(s.extensions, list)) - s = CONSOLE.store - self.assertTrue(isinstance(s.basemodels, list)) - self.assertTrue(isinstance(s.models, list)) - self.assertFalse(s.volatile) - try: - s.get_user(username="test") - except DoesNotExist: - s.set_user(username="test") - self.assertEqual(s._last_snapshot, 0) - self.assertIsNone(s.snapshot()) - self.assertEqual(s._last_snapshot, 1) - self.assertIsNone(s.snapshot(False)) - self.assertEqual(s._last_snapshot, 0) - self.assertIsNone(s.snapshot(False)) - +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +"""Core entity assets' tests. + +""" +from sploitkit.core.components import * + +from __utils__ import * + + +class TestComponents(TestCase): + def test_store(self): + s = CONSOLE._storage + self.assertTrue(isinstance(s.extensions, list)) + s = CONSOLE.store + self.assertTrue(isinstance(s.basemodels, list)) + self.assertTrue(isinstance(s.models, list)) + self.assertFalse(s.volatile) + try: + s.get_user(username="test") + except DoesNotExist: + s.set_user(username="test") + self.assertEqual(s._last_snapshot, 0) + self.assertIsNone(s.snapshot()) + self.assertEqual(s._last_snapshot, 1) + self.assertIsNone(s.snapshot(False)) + self.assertEqual(s._last_snapshot, 0) + self.assertIsNone(s.snapshot(False)) + diff --git a/tests/test_console.py b/tests/test_console.py index b05aaf5..d537781 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -1,22 +1,22 @@ -#!/usr/bin/env python -# -*- coding: UTF-8 -*- -"""Core entity assets' tests. - -""" -from sploitkit.core.components import * - -from utils import * - - -class TestConsole(TestCase): - def test_console(self): - self.assertIsNotNone(CONSOLE._get_tokens("help")) - self.assertIsNone(CONSOLE.play("help")) - r = CONSOLE.play("help", "show modules", capture=True) - # check the presence of some commands from the base - for cmd in ["?", "exit", "quit", "unset", "use", "record", "replay", "help", "show", "select"]: - self.assertIn(" " + cmd + " ", r[0][1]) - # check that some particular commands are missing - for cmd in ["pydbg", "memory", "dict"]: - self.assertNotIn(" " + cmd + " ", r[0][1]) - +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +"""Core entity assets' tests. + +""" +from sploitkit.core.components import * + +from __utils__ import * + + +class TestConsole(TestCase): + def test_console(self): + self.assertIsNotNone(CONSOLE._get_tokens("help")) + self.assertIsNone(CONSOLE.play("help")) + r = CONSOLE.play("help", "show modules", capture=True) + # check the presence of some commands from the base + for cmd in ["?", "exit", "quit", "unset", "use", "record", "replay", "help", "show", "select"]: + self.assertIn(" " + cmd + " ", r[0][1]) + # check that some particular commands are missing + for cmd in ["pydbg", "memory", "dict"]: + self.assertNotIn(" " + cmd + " ", r[0][1]) + diff --git a/tests/test_entity.py b/tests/test_entity.py index 134e246..d0dedf2 100644 --- a/tests/test_entity.py +++ b/tests/test_entity.py @@ -1,48 +1,48 @@ -#!/usr/bin/env python -# -*- coding: UTF-8 -*- -"""Core entity assets' tests. - -""" -from sploitkit.core.entity import load_entities, set_metadata -from tinyscript.helpers import parse_docstring - -from utils import * - - -class TestEntity(TestCase): - def test_load_entities(self): - self.assertIn(Command, list(Entity._subclasses.keys())) - self.assertIn(Model, list(Entity._subclasses.keys())) - self.assertIn(Module, list(Entity._subclasses.keys())) - self.assertTrue(len(Command.subclasses) > 0) - self.assertTrue(len(Model.subclasses) > 0) - l = len(Module.subclasses) - self.assertTrue(l > 0) - M = Module.subclasses[0] - del Entity._subclasses[Module] - load_entities([Module], CONSOLE._root.dirname.joinpath("modules"), exclude={'module': [M]}) - self.assertTrue(len(Module.subclasses) > 0) - self.assertRaises(ValueError, Entity._subclasses.__getitem__, (Module, M.__name__)) - del Entity._subclasses[Module] - load_entities([Module], CONSOLE._root.dirname.joinpath("modules")) - self.assertTrue(len(Module.subclasses) > 0) - self.assertIn(Entity._subclasses[Module, M.__name__], Module.subclasses) - load_entities([Console], CONSOLE._root, backref={'command': ["console"]}) - - def test_set_metadata(self): - # check that every subclass has its own description, and not the one of its entity class - for cls in Entity._subclasses.keys(): - for subcls in cls.subclasses: - self.assertNotEqual(subcls.__doc__, cls.__doc__) - # now, alter a Command subclass to test for set_metadata - C = Command.subclasses[0] - C.meta = {'options': ["BAD_OPTION"]} - self.assertRaises(ValueError, set_metadata, C, parse_docstring) - C.meta = {'options': [("test", "default", False, "description")]} - set_metadata(C, parse_docstring) - M = Module.subclasses[0] - B = M.__base__ - B.meta = {'test': "test"} - M._inherit_metadata = True - set_metadata(M, parse_docstring) - +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +"""Core entity assets' tests. + +""" +from sploitkit.core.entity import load_entities, set_metadata +from tinyscript.helpers import parse_docstring + +from __utils__ import * + + +class TestEntity(TestCase): + def test_load_entities(self): + self.assertIn(Command, list(Entity._subclasses.keys())) + self.assertIn(Model, list(Entity._subclasses.keys())) + self.assertIn(Module, list(Entity._subclasses.keys())) + self.assertTrue(len(Command.subclasses) > 0) + self.assertTrue(len(Model.subclasses) > 0) + l = len(Module.subclasses) + self.assertTrue(l > 0) + M = Module.subclasses[0] + del Entity._subclasses[Module] + load_entities([Module], CONSOLE._root.dirname.joinpath("modules"), exclude={'module': [M]}) + self.assertTrue(len(Module.subclasses) > 0) + self.assertRaises(ValueError, Entity._subclasses.__getitem__, (Module, M.__name__)) + del Entity._subclasses[Module] + load_entities([Module], CONSOLE._root.dirname.joinpath("modules")) + self.assertTrue(len(Module.subclasses) > 0) + self.assertIn(Entity._subclasses[Module, M.__name__], Module.subclasses) + load_entities([Console], CONSOLE._root, backref={'command': ["console"]}) + + def test_set_metadata(self): + # check that every subclass has its own description, and not the one of its entity class + for cls in Entity._subclasses.keys(): + for subcls in cls.subclasses: + self.assertNotEqual(subcls.__doc__, cls.__doc__) + # now, alter a Command subclass to test for set_metadata + C = Command.subclasses[0] + C.meta = {'options': ["BAD_OPTION"]} + self.assertRaises(ValueError, set_metadata, C, parse_docstring) + C.meta = {'options': [("test", "default", False, "description")]} + set_metadata(C, parse_docstring) + M = Module.subclasses[0] + B = M.__base__ + B.meta = {'test': "test"} + M._inherit_metadata = True + set_metadata(M, parse_docstring) + diff --git a/tests/test_model.py b/tests/test_model.py index 8bb45c8..a0075b0 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -1,14 +1,14 @@ -#!/usr/bin/env python -# -*- coding: UTF-8 -*- -"""Model entity tests. - -""" -from utils import * - - -class TestModule(TestCase): - def test_model(self): - self.assertTrue(len(Model.subclasses) > 0) - self.assertTrue(len(StoreExtension.subclasses) == 0) # sploitkit's base has no StoreExtension at this moment - self.assertIsNotNone(repr(Model.subclasses[0])) - +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +"""Model entity tests. + +""" +from __utils__ import * + + +class TestModule(TestCase): + def test_model(self): + self.assertTrue(len(Model.subclasses) > 0) + self.assertTrue(len(StoreExtension.subclasses) == 0) # sploitkit's base has no StoreExtension at this moment + self.assertIsNotNone(repr(Model.subclasses[0])) + diff --git a/tests/test_module.py b/tests/test_module.py index 7468c22..c77dc4d 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -1,37 +1,37 @@ -#!/usr/bin/env python -# -*- coding: UTF-8 -*- -"""Module entity tests. - -""" -from utils import * - - -class TestModule(TestCase): - def test_module_attributes(self): - for m in Module.subclasses: - m.console = CONSOLE # use the main console ; should normally be a ModuleConsole - self.assertIsNotNone(m.base) - for a in ["files", "logger", "store", "workspace"]: - self.assertIsNotNone(getattr(m(), a)) - - def test_module_help(self): - for c in [None, "uncategorized", "does_not_exist"]: - self.assertIsNotNone(Module.get_help(c)) - M = Module.subclasses[0] - self.assertIsNone(M()._feedback(None, "")) - self.assertIsNone(M()._feedback(True, "test")) - self.assertIsNone(M()._feedback(False, "test")) - - def test_module_registry(self): - self.assertIsNotNone(Module.get_list()) - self.assertIsNotNone(Module.get_modules()) - class FakeModule(Module): - path = "fake_module" - name = "fake_module" - self.assertIsNone(Module.unregister_module(FakeModule)) - class OrphanModule(Module): - path = None - name = "orphan_module" - self.assertIsNone(Module.register_module(OrphanModule)) - self.assertNotIn(OrphanModule, Module.subclasses) - +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +"""Module entity tests. + +""" +from __utils__ import * + + +class TestModule(TestCase): + def test_module_attributes(self): + for m in Module.subclasses: + m.console = CONSOLE # use the main console ; should normally be a ModuleConsole + self.assertIsNotNone(m.base) + for a in ["files", "logger", "store", "workspace"]: + self.assertIsNotNone(getattr(m(), a)) + + def test_module_help(self): + for c in [None, "uncategorized", "does_not_exist"]: + self.assertIsNotNone(Module.get_help(c)) + M = Module.subclasses[0] + self.assertIsNone(M()._feedback(None, "")) + self.assertIsNone(M()._feedback(True, "test")) + self.assertIsNone(M()._feedback(False, "test")) + + def test_module_registry(self): + self.assertIsNotNone(Module.get_list()) + self.assertIsNotNone(Module.get_modules()) + class FakeModule(Module): + path = "fake_module" + name = "fake_module" + self.assertIsNone(Module.unregister_module(FakeModule)) + class OrphanModule(Module): + path = None + name = "orphan_module" + self.assertIsNone(Module.register_module(OrphanModule)) + self.assertNotIn(OrphanModule, Module.subclasses) + diff --git a/tests/test_scenarios.py b/tests/test_scenarios.py index ae855de..505b745 100644 --- a/tests/test_scenarios.py +++ b/tests/test_scenarios.py @@ -1,55 +1,55 @@ -#!/usr/bin/env python -# -*- coding: UTF-8 -*- -"""Scenario-based tests of a Sploitkit application. - -""" -from utils import * - - -class TestScenarios(TestCase): - def test_bad_command(self): - # single bad command - out, err = execute("bad") - self.assertNotEqual(err, "") - self.assertEqual(out[0][1], "") - # successful command before the failing one - out, err = execute("help", "bad") - self.assertNotEqual(err, "") - self.assertNotEqual(out[0][1], "") - self.assertEqual(out[1][1], "") - # failing command before the successful one - out, err = execute("bad", "help") - self.assertNotEqual(err, "") - self.assertEqual(out[0][1], "") - self.assertIsNone(out[1][1]) - - def test_help(self): - out, err = execute("help") - self.assertEqual(err, "") - self.assertNotEqual(out, "") - out, err = execute("?") - self.assertEqual(err, "") - self.assertNotEqual(out, "") - - def test_set_debug(self): - out, err = execute("set DEBUG true", "help") - - def test_show_modules(self): - out, err = execute("show modules") - self.assertEqual(err, "") - self.assertIn("modules", out[0][1]) - self.assertIn("my_first_module", out[0][1]) - - def test_show_options(self): - out, err = execute("show options") - self.assertEqual(err, "") - self.assertIn("Console options", out[0][1]) - self.assertIn("APP_FOLDER", out[0][1]) - self.assertIn("DEBUG", out[0][1]) - self.assertIn("WORKSPACE", out[0][1]) - - def test_show_projects(self): - out, err = execute("show projects") - self.assertEqual(err, "") - self.assertIn("Existing projects", out[0][1]) - +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +"""Scenario-based tests of a Sploitkit application. + +""" +from __utils__ import * + + +class TestScenarios(TestCase): + def test_bad_command(self): + # single bad command + out, err = execute("bad") + self.assertNotEqual(err, "") + self.assertEqual(out[0][1], "") + # successful command before the failing one + out, err = execute("help", "bad") + self.assertNotEqual(err, "") + self.assertNotEqual(out[0][1], "") + self.assertEqual(out[1][1], "") + # failing command before the successful one + out, err = execute("bad", "help") + self.assertNotEqual(err, "") + self.assertEqual(out[0][1], "") + self.assertIsNone(out[1][1]) + + def test_help(self): + out, err = execute("help") + self.assertEqual(err, "") + self.assertNotEqual(out, "") + out, err = execute("?") + self.assertEqual(err, "") + self.assertNotEqual(out, "") + + def test_set_debug(self): + out, err = execute("set DEBUG true", "help") + + def test_show_modules(self): + out, err = execute("show modules") + self.assertEqual(err, "") + self.assertIn("modules", out[0][1]) + self.assertIn("my_first_module", out[0][1]) + + def test_show_options(self): + out, err = execute("show options") + self.assertEqual(err, "") + self.assertIn("Console options", out[0][1]) + self.assertIn("APP_FOLDER", out[0][1]) + self.assertIn("DEBUG", out[0][1]) + self.assertIn("WORKSPACE", out[0][1]) + + def test_show_projects(self): + out, err = execute("show projects") + self.assertEqual(err, "") + self.assertIn("Existing projects", out[0][1]) +