From 34aca29359f1f08ea59f04ee17e81a5d0afdd462 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Sun, 26 Jun 2022 06:44:48 +0200 Subject: [PATCH] Moar linting, cleared roadblocks for the middleground error --- discordHelperWin.py | 3 +- modules/adler_test.py | 4 +- modules/agnostic_paths.py | 5 ++- modules/internal_io.py | 6 +++ modules/main_runner.py | 19 ++++++--- modules/size_test.py | 4 +- modules/spidey_test.py | 4 +- modules/test_template.py | 88 ++++++++++++++++++++------------------- 8 files changed, 77 insertions(+), 56 deletions(-) diff --git a/discordHelperWin.py b/discordHelperWin.py index e692095..d89c84a 100644 --- a/discordHelperWin.py +++ b/discordHelperWin.py @@ -26,8 +26,7 @@ if __name__ == "__main__": # Will always try to load discordHelper/discordHelper.json # TODO : Document this feature. run_db = os.path.join(directory_path, "test_results_found.json") - argv = rf"--all --autodetect --launch --continue --timeout 10 {db_path}--gen-data {run_db} \ - --db discordHelper/test_results.json".split(" ") + argv = rf"--all --autodetect --launch --continue --timeout 10 {db_path}--gen-data {run_db} --db discordHelper/test_results.json".split(" ") # print(" > Starting the discordHelper with the following arguments:") # print(" > " + " ".join(argv)) diff --git a/modules/adler_test.py b/modules/adler_test.py index 511998e..ea4a6db 100644 --- a/modules/adler_test.py +++ b/modules/adler_test.py @@ -20,11 +20,11 @@ def apply_adler(path): class AdlerTest(test_template.TestWalkTemplateNoLogs): - def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock): + def __init__(self, thread_parameters): """ Simple test runner, compare adler32 to known good values. """ - super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock) + super().__init__(thread_parameters) def run_test(self): self.set_status("running") diff --git a/modules/agnostic_paths.py b/modules/agnostic_paths.py index e8ee2c6..ee8f598 100644 --- a/modules/agnostic_paths.py +++ b/modules/agnostic_paths.py @@ -4,7 +4,7 @@ The agnostic path handler, gives all methods to access all paths of the discord import os import re import sys - +import random class AgnosticPaths: """ @@ -27,6 +27,9 @@ class AgnosticPaths: self.main_path = self._get_path() self.version = self.get_version() self._update_path() + self.all_files = self.walk_all_files() + random.shuffle(self.all_files) + self.size = len(self.all_files) def get_os(self): """ diff --git a/modules/internal_io.py b/modules/internal_io.py index 7fc0fd1..8e94cfa 100644 --- a/modules/internal_io.py +++ b/modules/internal_io.py @@ -22,3 +22,9 @@ return_code_dict = { "problems": "Test found problems", "idle": "Test is idle", } + +thread_parameters = namedtuple( + 'thread_parameters', + 'args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock, bad_database', + defaults=(None, None, None, None, None, None, None, None) +) \ No newline at end of file diff --git a/modules/main_runner.py b/modules/main_runner.py index c4329d2..38fa0c2 100644 --- a/modules/main_runner.py +++ b/modules/main_runner.py @@ -7,7 +7,7 @@ import threading import time from collections import deque -from modules.internal_io import global_status +from modules.internal_io import global_status, thread_parameters from modules import size_test, adler_test # noqa # Used to force import of modules by pyinstaller """ @@ -107,6 +107,14 @@ class TestRunner: self.open_base_data() self.exec_all_tests() + def update_threat(self, threat_level): + """ + Updates the threat level + :param threat_level: The new threat level + :type threat_level: int + """ + threat_level = max(threat_level, self.threat_level) + def open_base_data(self): """ Opens the base data file @@ -215,16 +223,17 @@ class TestRunner: self.list_of_tests = [] for module_name, test_class in self.loaded_test_classes.items(): print(colored(f" > Starting test {module_name}...", "blue")) - tc = test_class( + tc = test_class(thread_parameters( self.args, self.agnostic_path, self.base_data, self.finished_tests, queue_lock, self.dict_of_processes, - processes_lock - ) - tc = threading.Thread(target=tc.run_test, args=()) + processes_lock, + self.bad_database, + )) + tc = threading.Thread(target=tc.run, args=()) tc.name = str(module_name) tc.daemon = True self.list_of_tests.append(tc) diff --git a/modules/size_test.py b/modules/size_test.py index ccdddb5..07b6596 100644 --- a/modules/size_test.py +++ b/modules/size_test.py @@ -4,11 +4,11 @@ from modules import test_template class SizeTest(test_template.TestWalkTemplateNoLogs): - def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock): + def __init__(self, thread_parameters): """ Simple test runner, compare file size to known good values. """ - super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock) + super().__init__(thread_parameters) def run_test(self): """ diff --git a/modules/spidey_test.py b/modules/spidey_test.py index 6782d8b..9fb32a7 100644 --- a/modules/spidey_test.py +++ b/modules/spidey_test.py @@ -4,11 +4,11 @@ from modules import test_template class SpideyTest(test_template.TestTemplate): - def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock): + def __init__(self, thread_parameters): """ Simple test runner, for testing the detection of Spidey viruses. """ - super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock) + super().__init__(thread_parameters) def run_test(self): """ diff --git a/modules/test_template.py b/modules/test_template.py index d94e03d..f113d2c 100644 --- a/modules/test_template.py +++ b/modules/test_template.py @@ -13,46 +13,59 @@ class TestDataError(Exception): class TestTemplate: - def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock): + def __init__(self, thread_parameters: internal_io.thread_parameters): """ 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: dict - :param queue: The queue to put the test status in - :type queue: queue.deque - :param queue_lock: The lock to use when putting the test status in the queue - :type queue_lock: threading.Lock - :param dict_process: The dictionary of processes - :type dict_process: dict - :param dict_process_lock: The lock to use when putting the process in the dictionary - :type dict_process_lock: threading.Lock + :param thread_parameters.args: List of arguments + :type thread_parameters.args: argparse.Namespace + :param thread_parameters.agnpath: The path to the discord folder + :type thread_parameters.agnpath: agnostic_path.AgnosticPath + :param thread_parameters.test_data: The data to be tested + :type thread_parameters.test_data: dict + :param thread_parameters.queue: The queue to put the test status in + :type thread_parameters.queue: queue.deque + :param thread_parameters.queue_lock: The lock to use when putting the test status in the queue + :type thread_parameters.queue_lock: threading.Lock + :param thread_parameters.dict_process: The dictionary of processes + :type thread_parameters.dict_process: dict + :param thread_parameters.dict_process_lock: The lock to use when putting the process in the dictionary + :type thread_parameters.dict_process_lock: threading.Lock + :param thread_parameters.bad_database: Whether the database was sucessfully loaded and contains valid data + :type thread_parameters.bad_database: bool """ - self.args = args - self.ptb = args.ptb - self.agnostic_path = agnpath - self.os_name = agnpath.os - self.main_path = agnpath.main_path - self.test_data = test_data + self.args = thread_parameters.args + self.ptb = thread_parameters.args.ptb + self.agnostic_path = thread_parameters.agnpath + self.os_name = thread_parameters.agnpath.os + self.main_path = thread_parameters.agnpath.main_path + self.test_data = thread_parameters.test_data self.is_infected = False self.status_code = "idle" self.status = "Test not yet initialized." - self.queue = queue + self.queue = thread_parameters.queue self.set_status("idle") - self.queue_lock = queue_lock + self.queue_lock = thread_parameters.queue_lock self.failure_dict = {} self.new_data = {} self.progress = 0 - self.dict_process = dict_process - self.dict_process_lock = dict_process_lock + self.dict_process = thread_parameters.dict_process + self.dict_process_lock = thread_parameters.dict_process_lock + self.bad_database = thread_parameters.bad_database self.dict_process_lock.acquire() self.dict_process[self.name()] = self self.dict_process_lock.release() self.init_test_data() + def run(self): + """ + Runs the test. + """ + try: + self.run_test() + except Exception as e: + self.set_status("problem", str(e)) + return self.finish() + def run_test(self): """ The main runner function, must be implemented by the child class. @@ -154,17 +167,11 @@ class TestTemplate: class TestWalkTemplate(TestTemplate): - def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock): + def __init__(self, thread_parameters): """ 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, queue_lock, dict_process, dict_process_lock) + super().__init__(thread_parameters) self.to_skip = [] def add_test_result(self, path, testname, result): @@ -234,7 +241,10 @@ class TestWalkTemplate(TestTemplate): return self.finish() if len(self.failure_dict) > 0: - self.set_status("failure", f"Test {self.name()} failed on {len(self.failure_dict)} files.") + if self.bad_database: + self.set_status("unknown", f"Test {self.name()} failed on {len(self.failure_dict)} files") + else: + self.set_status("failure", f"Test {self.name()} failed on {len(self.failure_dict)} files.") else: self.set_status("success", f"Test {self.name()} passed.") @@ -286,15 +296,9 @@ class TestWalkTemplate(TestTemplate): class TestWalkTemplateNoLogs(TestWalkTemplate): - def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock): + def __init__(self, thread_parameters): """ 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, queue_lock, dict_process, dict_process_lock) + super().__init__(thread_parameters) self.to_skip = [r"modules\discord_dispatch-1\discord_dispatch\dispatch.log"]