diff --git a/database/windows_base.yml b/database/windows_base.yml new file mode 100644 index 0000000..1d79ee1 --- /dev/null +++ b/database/windows_base.yml @@ -0,0 +1,19 @@ +global: + name: Windows Base + version: 1.0 + + +files: + /etc/passwd: + sha256: "6c3d65f8b8b8f5a8f62b60c0e15d5d084d3b20f470f5d8b3d26bfb3c350d37a3" + type: file + size: "1024" + permissions: "0644" + owner: "root" + group: "root" + date: "2018-01-01T00:00:00Z" + executable: "true" + symlink: "false" + md5: "6c3d65f8b8b8f5a8f62b60c0e15d5d084d3b20f470f5d8b3d26bfb3c350d37a3" + sha1: "6c3d65f8b8b8f5a8f62b60c0e15d5d084d3b20f470f5d8b3d26bfb3c350d37a3" + diff --git a/modules/agnostic_paths.py b/modules/agnostic_paths.py index ba0e728..f1f92d0 100644 --- a/modules/agnostic_paths.py +++ b/modules/agnostic_paths.py @@ -144,6 +144,17 @@ class AgnosticPaths: :rtype: str """ return self.main_path + + def walk_all_files(self, *args): + """ + Walks all files in the given path + :param args: The path + :return: A list of all files in the given path + :rtype: list[str] + """ + return [os.path.join(dirpath, filename) + for dirpath, dirnames, filenames in os.walk(self(*args)) + for filename in filenames] def __repr__(self): """ diff --git a/modules/argument_parser.py b/modules/argument_parser.py index 39fe8f0..730d167 100644 --- a/modules/argument_parser.py +++ b/modules/argument_parser.py @@ -30,5 +30,6 @@ def parse(_args): parser.add_argument("--gen-data-path", "--generate-data-path", "--gen-path", nargs=1, default="", help="Path to generate the data in") parser.add_argument("--database", "--db", nargs=1, default="", help="Alternative database for the analysis") parser.add_argument("--continue", action="store_true", help="Continue the analysis in case of failure") + parser.add_argument("--test-walk", action="store_true", help="Test walk function") return parser.parse_args(_args) diff --git a/modules/internal_io.py b/modules/internal_io.py index 8804dbb..af1784d 100644 --- a/modules/internal_io.py +++ b/modules/internal_io.py @@ -7,8 +7,8 @@ from collections import namedtuple global_status = namedtuple( 'global_status', - 'total_tests, tests_finished, test_success, tests_failed, tests_running, tests_skipped', - defaults=(0, 0, 0, 0, 0, 0) + 'total_tests, tests_finished, test_success, tests_failed, tests_running, tests_skipped, test_error', + defaults=(0, 0, 0, 0, 0, 0, 0) ) test_status = namedtuple('test_status', 'name, status, message', defaults=(None, None, None)) return_code_dict = { diff --git a/modules/test_interface.py b/modules/test_interface.py index 4b1a9f9..3c27eaf 100644 --- a/modules/test_interface.py +++ b/modules/test_interface.py @@ -13,6 +13,7 @@ argparse argument : [module_name, class_name] """ _TEST_MODULES = { "spidey": ["spidey_test", "SpideyTest"], + "test_walk": ["test_template", "TestWalkTemplate"], } POST_RUN_ALLOWED = False @@ -61,8 +62,9 @@ class TestRunner: modules[values[0]] = importlib.import_module(f"modules.{values[0]}") classes[values[0]] = getattr(modules[values[0]], values[1]) - except AttributeError: + except AttributeError as e: print(f"The module {module_name} was not loaded because it was not specified in the config.") + print(e) self.loaded_test_modules.update(modules) self.loaded_test_classes.update(classes) @@ -173,6 +175,8 @@ class TestRunner: nb_tests_failure += 1 elif test.status == "skipped": nb_tests_skipped += 1 + elif test.status == "problem": + nb_tests_failure += 1 nb_tests_ran = nb_tests_success + nb_tests_failure + nb_tests_skipped nb_test_running = nb_tests - nb_tests_ran diff --git a/modules/test_template.py b/modules/test_template.py index 9d10d92..748e43b 100644 --- a/modules/test_template.py +++ b/modules/test_template.py @@ -81,3 +81,59 @@ class TestTemplate: else: self.status = self.get_status_from_code(statuscode) return self.status_code + + +class TestWalkTemplate(TestTemplate): + def __init__(self, args, agnpath, test_data, queue): + """ + Simple test template. + :param args: List of arguments + :type args: argparse.Namespace + :param agnpath: The path to the discord folder + :type agnpath: agnostic_path.AgnosticPath + :param test_data: The data to be tested + :type test_data: str + """ + super().__init__(args, agnpath, test_data, queue) + if test_data is None: + self.set_status("problem", "No test data given.") + + def run_test(self): + if self.test_data is None: + self.set_status("problem", "No test data given.") + return self.get_status() + self.set_status("running", "The test will begin shortly, please wait...") + for path in self.walk(): + self.compare(path, "test", lambda x: True, [], {}) + + def walk(self): + """ + Walks the given path and returns a list of all files and directories. + :return: List of all files and directories + :return: + """ + return self.agnostic_path.walk_all_files(self.agnostic_path()) + + def compare(self, path, entry_name, function, args, kwargs): + """ + Use the function on the given path, with given arguments and compare it with the entry in the test data. + :param path: Path to the file or directory + :type path: agnostic_path.AgnosticPath + :param entry_name: Name of the entry in the test data + :type entry_name: str + :param function: Function to use on the path + :type function: function + :param args: Arguments to use on the function + :type args: list + :param kwargs: Keyword arguments to use on the function + :type kwargs: dict + """ + if path not in self.test_data["files"]: + raise ValueError("No entry in test data for path: " + str(path)) + if entry_name not in self.test_data["files"][path]: + raise ValueError("No entry in test data for path: " + str(path) + " and entry name: " + str(entry_name)) + data_origin = self.test_data["files"][path][entry_name] + data_result = function(path, *args, **kwargs) + if data_origin != data_result: + return False + return True