Good first attempt at size test, still a lot of nightmare to cleanup

This commit is contained in:
Maximilien Rigaut 2022-06-25 06:49:19 +02:00
parent d33d146224
commit 4191b8a8af
No known key found for this signature in database
GPG Key ID: 4F14B23709ADA5B1
5 changed files with 2984 additions and 1467 deletions

File diff suppressed because it is too large Load Diff

51
modules/adler_test.py Normal file
View File

@ -0,0 +1,51 @@
"""
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.TestWalkTemplate):
def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock):
"""
Simple test runner, compare adler32 to known good values.
"""
super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock)
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"
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')} bytes, but it is {adler32(open(path, 'rb').read())} bytes.")
self.progress = 100
if self.is_infected:
return self.finish(
"failure",
error_msg
)
return self.finish("success", "Your discord is not infected.")

View File

@ -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=120, type=int, help="Size of the message window")
parser.add_argument("--size", "--window-size", default=110, type=int, help="Size of the message window")
parser.add_argument("--max-shown", default=5, type=int, help="Maximum number of errors shown")
# Tests

View File

@ -6,7 +6,8 @@ import threading
import time
from collections import deque
from modules.internal_io import global_status # , test_status, return_code_dict
from modules.internal_io import global_status
from modules import size_test, adler_test
"""
All modules that can be tested
@ -21,6 +22,7 @@ _TEST_MODULES = {
"spidey": ["spidey_test", "SpideyTest"],
# "test_walk": ["test_template", "TestWalkTemplate"],
"size_test": ["size_test", "SizeTest"],
"adler_test": ["adler_test", "AdlerTest"],
}
PROGRAM_VERSION = "1.0.0"

View File

@ -26,18 +26,19 @@ class SizeTest(test_template.TestWalkTemplate):
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"({os.path.getsize(path)} b) not ({self.get_expected_result(path, 'size')} b).\n"
f"({res} b) not ({self.get_expected_result(path, 'size')} b).\n"
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')} bytes, but it is {os.path.getsize(path)} bytes.")
self.progress = 100
if self.is_infected:
return self.finish(
"failure",
error_msg
)
self.progress = 100
return self.finish("success", "Your discord is not infected.")