Renamed tests and added a fast class for function interaction

This commit is contained in:
Maximilien Rigaut 2022-06-27 02:59:16 +02:00
parent 724a054f14
commit aa249aad51
No known key found for this signature in database
GPG Key ID: 4F14B23709ADA5B1
8 changed files with 143 additions and 130 deletions

View File

@ -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.")

View File

@ -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.")

26
modules/test_adler.py Normal file
View File

@ -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, "")

View File

@ -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")

25
modules/test_sha.py Normal file
View File

@ -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, "")

11
modules/test_size.py Normal file
View File

@ -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")

View File

@ -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.")