From b83e1a8f7814b9253e743cbf8ae5b74a4363d781 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Sat, 25 Jun 2022 05:46:59 +0200 Subject: [PATCH] Added more readability and a base implementation for a walk test --- discordHelperWin.py | 3 ++- modules/agnostic_paths.py | 14 ++++++------- modules/argument_parser.py | 2 +- modules/main_runner.py | 28 ++++++++++++++++++-------- modules/size_test.py | 41 ++++++++++++++++++++++++++++++++++++++ modules/test_template.py | 29 +++++++++++++++++---------- 6 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 modules/size_test.py diff --git a/discordHelperWin.py b/discordHelperWin.py index 1b9a7d7..9d3e252 100644 --- a/discordHelperWin.py +++ b/discordHelperWin.py @@ -24,8 +24,9 @@ if __name__ == "__main__": db_path = "" # print(" > No user database was found, the bundled one will be used.") + # Will always try to load discordHelper/discordHelper.json # TODO : Document this feature. run_db = os.path.join(directory_path, "discordHelper_last_run.json") - argv = f"--all --autodetect --launch --continue --timeout 10 {db_path}--gen-data {run_db}.yaml".split(" ") + argv = rf"--all --autodetect --launch --continue --timeout 10 {db_path}--gen-data {run_db} --db discordHelper/discordHelper.json".split(" ") # print(" > Starting the discordHelper with the following arguments:") # print(" > " + " ".join(argv)) diff --git a/modules/agnostic_paths.py b/modules/agnostic_paths.py index 477b1c5..be2d847 100644 --- a/modules/agnostic_paths.py +++ b/modules/agnostic_paths.py @@ -40,13 +40,13 @@ class AgnosticPaths: platform = sys.platform if platform.startswith('win'): self.os = 'windows' - self.default_database = 'windows_base' + self.default_database = 'databases/windows_base.json' elif platform.startswith('linux'): self.os = 'linux' - self.default_database = 'linux_base' + self.default_database = 'databases/linux_base.json' elif platform.startswith('darwin'): self.os = 'mac' - self.default_database = 'mac_base' + self.default_database = 'databases/mac_base.json' else: raise OSError("This script can only check for Spideys on Windows, Linux and Mac.") return self.os @@ -67,16 +67,16 @@ class AgnosticPaths: if os.path.exists(ptb_path): self.ptb = "PTB" self.args.ptb = "PTB" - self.default_database = "windows_ptb" + self.default_database = "databases/windows_ptb.json" return ptb_path else: - self.default_database = "windows_base" + self.default_database = "databases/windows_base.json" return base_path if self.ptb == 'PTB': - self.default_database = "windows_base" + self.default_database = "databases/windows_bas.jsone" return base_path+"PTB" else: - self.default_database = "windows_ptb" + self.default_database = "databases/windows_ptb.json" return base_path # Try to find the default version elif self.os == 'linux': return os.path.join("/", "opt", "discord") diff --git a/modules/argument_parser.py b/modules/argument_parser.py index ab906f4..48fc944 100644 --- a/modules/argument_parser.py +++ b/modules/argument_parser.py @@ -27,7 +27,7 @@ def parse(_args): parser.add_argument("--database", "--db", type=str, help="Alternative database for the analysis") parser.add_argument("--pollrate", "--poll", default=0.1, type=float, help="Refresh rate of the status, 0.1 is the default") parser.add_argument("--printrate", "-refresh", default=5, type=int, help="Print the tests every X seconds") - parser.add_argument("--size", "--window-size", default=90, type=int, help="Size of the message window") + parser.add_argument("--size", "--window-size", default=120, type=int, help="Size of the message window") # Tests parser.add_argument('--spidey', action='store_true', help='Runs the spidey test') parser.add_argument('--line-count', action='store_true', help='Test all known files for line count') diff --git a/modules/main_runner.py b/modules/main_runner.py index 4fd2f5d..e718300 100644 --- a/modules/main_runner.py +++ b/modules/main_runner.py @@ -20,6 +20,7 @@ os.system('color') _TEST_MODULES = { "spidey": ["spidey_test", "SpideyTest"], # "test_walk": ["test_template", "TestWalkTemplate"], + "size_test": ["size_test", "SizeTest"], } PROGRAM_VERSION = "1.0.0" @@ -95,16 +96,27 @@ class TestRunner: """ Opens the base data file """ - print(self.args.database) + default_database = resource_path(self.agnostic_path.default_database) + if not self.args.database: + self.args.database = default_database if os.path.exists(self.args.database): with open(resource_path(self.args.database), "r") as f: - db_data = f.read() - # print(db_data) - else: # Try opening the default database - print(colored("\n > The database file does not exist, trying to open the default one...\n", "yellow")) - with open(resource_path('databases/windows_base.json'), "r") as f: - db_data = f.read() - # print(db_data) + self.base_data = json.load(f) + + elif os.path.exists(default_database): # Try opening the default database + # print(colored( + # "\n > The database file specified does not exist, " + # "trying to open the default one for your os...\n", "yellow" + # )) + with open(resource_path(default_database), "r") as f: + self.base_data = json.load(f) + + else: + print(colored( + "\n > The database file does not exist, and the default " + "one for your os does not exist either.\n", "red" + )) + self.base_data = {} def update_module_list(self): """ diff --git a/modules/size_test.py b/modules/size_test.py new file mode 100644 index 0000000..acf3f4c --- /dev/null +++ b/modules/size_test.py @@ -0,0 +1,41 @@ +import asyncio +import os +import time + +from modules import test_template + + +class SizeTest(test_template.TestWalkTemplate): + def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock): + """ + Simple test runner, compare file size to known good values. + """ + super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock) + + def run_test(self): + """ + Walk all files and check their size. + :return: Whether any of the files are larger than the expected size. + :rtype: bool + """ + self.set_status("running") + size = len(self.walk()) + error_msg = "" + for path in self.walk(): + if os.path.isfile(path): + if not self.compare(path, "size", os.path.getsize, (), {}): + self.add_failure(path, f"Test {self.name()} failed on {path}") + error_msg += f"{path} is " \ + f"({self.get_expected_result(path, 'size')} b) not ({os.path.getsize(path)} b).\n" + self.is_infected = True + if not self.args.continue_on_error: + return self.finish("failure", f"{path} is supposed to be {self.get_expected_result(path, 'size')} bytes, but it is {os.path.getsize(path)} bytes.") + + if self.is_infected: + return self.finish( + "failure", + error_msg + ) + self.progress = 100 + return self.finish("success", "Your discord is not infected.") + diff --git a/modules/test_template.py b/modules/test_template.py index f45696f..5068e06 100644 --- a/modules/test_template.py +++ b/modules/test_template.py @@ -51,6 +51,7 @@ class TestTemplate: self.dict_process_lock.acquire() self.dict_process[self.name()] = self self.dict_process_lock.release() + self.init_test_data() def run_test(self): """ @@ -60,6 +61,11 @@ class TestTemplate: self.set_status("running", "The test will begin shortly, please wait...") self.set_status("sucess", "The test was a success but kinda not a success.") + def init_test_data(self): + if self.test_data is None and not self.args.continue_on_error: + self.set_status("problem", "No test data given.") + return self.finish() + def name(self): """ :return: Name of the test @@ -159,8 +165,8 @@ class TestWalkTemplate(TestTemplate): :type test_data: str """ super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock) - if test_data is None: - self.set_status("problem", "No test data given.") + # if test_data is None: + # self.set_status("problem", "No test data given.") def add_test_result(self, path, testname, result): """ @@ -217,13 +223,7 @@ class TestWalkTemplate(TestTemplate): """ self.set_status("running", "The test will begin shortly, please wait...") - if self.test_data is None and not self.args.continue_on_error: - self.set_status("problem", "No test data given.") - return self.finish() - - self.set_status("running", "The test will begin shortly, please wait...") for path in self.walk(): - print(path) if not self.compare(path, "test", lambda x: True, [], {}): self.add_failure(path, f"Test {self.name()} failed on {path}") if not self.args.continue_on_error: @@ -245,7 +245,7 @@ class TestWalkTemplate(TestTemplate): """ return self.agnostic_path.walk_all_files(self.agnostic_path()) - def compare(self, path, entry_name, function, args, kwargs): + def compare(self, path, entry_name, function, args, kwargs, cmp_function=None): """ 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 @@ -258,6 +258,8 @@ class TestWalkTemplate(TestTemplate): :type args: list :param kwargs: Keyword arguments to use on the function :type kwargs: dict + :param cmp_function: Function compare both results + :type cmp_function: function """ data_origin = self.add_test_result(path, entry_name, function(path, *args, **kwargs)) @@ -266,7 +268,12 @@ class TestWalkTemplate(TestTemplate): self.set_status("problem", f"No expected data found for {path}") return False - if data_origin != expected_data: + if cmp_function is None: + if data_origin == expected_data: + return True + return False + else: + if cmp_function(data_origin, expected_data): + return True return False - return True