From aa249aad51c9b19df8222b9b43a119201d2cb972 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Mon, 27 Jun 2022 02:59:16 +0200 Subject: [PATCH] Renamed tests and added a fast class for function interaction --- modules/adler_test.py | 70 ---------------------- modules/size_test.py | 60 ------------------- modules/test_adler.py | 26 ++++++++ modules/test_line_count.py | 13 ++++ modules/test_sha.py | 25 ++++++++ modules/test_size.py | 11 ++++ modules/{spidey_test.py => test_spidey.py} | 0 modules/test_template.py | 68 +++++++++++++++++++++ 8 files changed, 143 insertions(+), 130 deletions(-) delete mode 100644 modules/adler_test.py delete mode 100644 modules/size_test.py create mode 100644 modules/test_adler.py create mode 100644 modules/test_line_count.py create mode 100644 modules/test_sha.py create mode 100644 modules/test_size.py rename modules/{spidey_test.py => test_spidey.py} (100%) diff --git a/modules/adler_test.py b/modules/adler_test.py deleted file mode 100644 index 6493895..0000000 --- a/modules/adler_test.py +++ /dev/null @@ -1,70 +0,0 @@ -""" -Fast adler32 checksum implementation. -""" -import os - -from modules import test_template -from zlib import adler32 - - -def apply_adler(path): - """ - Apply the adler32 checksum to a file. - :param path: The path to the file. - :type path: str - :return: The adler32 checksum. - :rtype: int - """ - with open(path, "rb") as f: - return hex(adler32(f.read())) - - -class AdlerTest(test_template.TestWalkTemplateNoLogs): - def __init__(self, thread_parameters): - """ - Simple test runner, compare adler32 to known good values. - """ - super().__init__(thread_parameters) - - def run_test(self): - self.set_status("running") - size = len(self.walk()) - error_msg = "" - for i, path in enumerate(self.walk()): - self.progress = int(100*i/size) - short_path = self.agnostic_path.get_short_path(path) - if os.path.isfile(path): - if not self.compare(path, "adler32", apply_adler, (), {}): - res = self.get_test_data(path, "adler32") - self.add_failure(path, f"Test {self.name()} failed on {short_path}") - error_msg += f"{short_path} is " \ - f"({res}) not ({self.get_expected_result(path, 'adler32')} b).\n" - if not self.bad_database: - self.is_infected = True - if not self.args.continue_on_error: - return self.finish( - "failure", - f"{short_path} is supposed to be {self.get_expected_result(path, 'adler32')} " - f"bytes, but it is {adler32(open(path, 'rb').read())} bytes." - ) - else: - self.is_unknown = True - if not self.args.continue_on_error: - return self.finish( - "problem", - f"{short_path} is supposed to be {self.get_expected_result(path, 'adler32')} " - f"bytes, but it is {adler32(open(path, 'rb').read())} bytes." - ) - - self.progress = 100 - if self.is_infected: - return self.finish( - "failure", - error_msg - ) - elif self.is_unknown: - return self.finish( - "problem", - error_msg - ) - return self.finish("success", "Your discord is not infected.") diff --git a/modules/size_test.py b/modules/size_test.py deleted file mode 100644 index e1912ed..0000000 --- a/modules/size_test.py +++ /dev/null @@ -1,60 +0,0 @@ -import os - -from modules import test_template - - -class SizeTest(test_template.TestWalkTemplateNoLogs): - def __init__(self, thread_parameters): - """ - Simple test runner, compare file size to known good values. - """ - super().__init__(thread_parameters) - self.is_unknown = False - - 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 = self.agnostic_path.size - - error_msg = "" - for i, path in enumerate(self.walk()): - self.progress = int(100*i/size) - short_path = self.agnostic_path.get_short_path(path) - if os.path.isfile(path): - if not self.compare(path, "size", os.path.getsize, (), {}): - res = self.get_test_data(path, "adler32") - self.add_failure(path, f"Test {self.name()} failed on {short_path}") - error_msg += f"{short_path} is " \ - f"({res} b) not ({self.get_expected_result(path, 'size')} b).\n" - # print(self.bad_database) - if not self.bad_database: - self.is_infected = True - if not self.args.continue_on_error: - return self.finish( - "failure", - f"{short_path} is supposed to be {self.get_expected_result(path, 'size')}" - f" bytes, but it is {os.path.getsize(path)} bytes.") - else: - self.is_unknown = True - if not self.args.continue_on_error: - return self.finish( - "problem", - f"{short_path} is supposed to be {self.get_expected_result(path, 'size')}" - f" bytes, but it is {os.path.getsize(path)} bytes.") - - self.progress = 100 - if self.is_infected: - return self.finish( - "failure", - error_msg - ) - elif self.is_unknown: - return self.finish( - "problem", - error_msg - ) - return self.finish("success", "Your discord is not infected.") diff --git a/modules/test_adler.py b/modules/test_adler.py new file mode 100644 index 0000000..2ef4431 --- /dev/null +++ b/modules/test_adler.py @@ -0,0 +1,26 @@ +""" +Fast adler32 checksum implementation. +""" + +from modules import test_template +from zlib import adler32 + + +def apply_adler(path): + """ + Apply the adler32 checksum to a file. + :param path: The path to the file. + :type path: str + :return: The adler32 checksum. + :rtype: int + """ + with open(path, "rb") as f: + return hex(adler32(f.read())) + + +class Adler32(test_template.TestWalkTemplateSimpleFunction): + def __init__(self, thread_parameters): + """ + Simple test runner, compare adler32 to known good values. + """ + super().__init__(thread_parameters, apply_adler, "") diff --git a/modules/test_line_count.py b/modules/test_line_count.py new file mode 100644 index 0000000..e79808e --- /dev/null +++ b/modules/test_line_count.py @@ -0,0 +1,13 @@ +""" +Test against the line count of the files in the discord folder. +""" + +from modules import test_template + + +class LineCount(test_template.TestWalkTemplateSimpleFunction): + def __init__(self, thread_parameters): + """ + Simple test runner, compare line count to known good values. + """ + super().__init__(thread_parameters, lambda path: len(open(path, "rb").readlines()), " lines") diff --git a/modules/test_sha.py b/modules/test_sha.py new file mode 100644 index 0000000..a265a54 --- /dev/null +++ b/modules/test_sha.py @@ -0,0 +1,25 @@ +""" +Sha256 hash function. +""" + +from hashlib import sha256 +from modules import test_template + + +def sha256_hash(path): + """ + Returns the sha256 hash of the data. + """ + with open(path, "rb") as f: + return sha256(f.read()).hexdigest() + + +class Sha256(test_template.TestWalkTemplateSimpleFunction): + """ + Sha256 hash function. + """ + def __init__(self, thread_parameters): + """ + Sha256 hash function. + """ + super().__init__(thread_parameters, sha256_hash, "") diff --git a/modules/test_size.py b/modules/test_size.py new file mode 100644 index 0000000..4b70887 --- /dev/null +++ b/modules/test_size.py @@ -0,0 +1,11 @@ +import os + +from modules import test_template + + +class SizeCheck(test_template.TestWalkTemplateSimpleFunction): + def __init__(self, thread_parameters): + """ + Simple test runner, compare file size to known good values. + """ + super().__init__(thread_parameters, os.path.getsize, " bytes") diff --git a/modules/spidey_test.py b/modules/test_spidey.py similarity index 100% rename from modules/spidey_test.py rename to modules/test_spidey.py diff --git a/modules/test_template.py b/modules/test_template.py index ca5e989..24e3392 100644 --- a/modules/test_template.py +++ b/modules/test_template.py @@ -1,6 +1,7 @@ """ A global template for all tests. """ +import os from modules import internal_io @@ -303,3 +304,70 @@ class TestWalkTemplateNoLogs(TestWalkTemplate): """ super().__init__(thread_parameters) self.to_skip = [r"modules\discord_dispatch-1\discord_dispatch\dispatch.log"] + + +class TestWalkTemplateSimpleFunction(TestWalkTemplateNoLogs): + def __init__(self, thread_parameters, function, unit_test): + """ + A test template that runs a simple function on every file, and compares the result with the expected data. + :param thread_parameters: Thread parameters + :type thread_parameters: dict + :param function: Function to use on every file + :type function: function + :param unit_test: A descriptor of what the test returns + :type unit_test: str + """ + super().__init__(thread_parameters) + self.function = function + self.test_name = self.name() + self.unit_test = unit_test + + 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 = self.agnostic_path.size + + error_msg = "" + for i, path in enumerate(self.walk()): + self.progress = int(100*i/size) + short_path = self.agnostic_path.get_short_path(path) + if os.path.isfile(path): + if not self.compare(path, self.test_name, self.function, (), {}): + res = self.get_test_data(path, self.test_name) + self.add_failure(path, f"Test {self.name()} failed on {short_path}") + error_msg += f"{short_path} is " \ + f"({res}{self.unit_test}) not ({self.get_expected_result(path, self.test_name)}" \ + f"{self.unit_test}).\n" + # print(self.bad_database) + if not self.bad_database: + self.is_infected = True + if not self.args.continue_on_error: + return self.finish( + "failure", + f"{short_path} is supposed to be {self.get_expected_result(path, self.test_name)}" + f" {self.unit_test}, but it is {self.get_expected_result(path, self.test_name)}" + f"{self.unit_test}.") + else: + self.is_unknown = True + if not self.args.continue_on_error: + return self.finish( + "problem", + f"{short_path} is supposed to be {self.get_expected_result(path, self.test_name)}" + f"{self.unit_test}, but it is {os.path.getsize(path)}{self.unit_test}") + + self.progress = 100 + if self.is_infected: + return self.finish( + "failure", + error_msg + ) + elif self.is_unknown: + return self.finish( + "problem", + error_msg + ) + return self.finish("success", "Your discord is not infected.")