Working multithread, with a clusterfk of useless info and no good info and report

This commit is contained in:
Maximilien Rigaut 2022-06-23 15:22:32 +02:00
parent abb45db79d
commit c90c6972d9
No known key found for this signature in database
GPG Key ID: 4F14B23709ADA5B1
5 changed files with 174 additions and 116 deletions

View File

@ -9,16 +9,5 @@ import sys
if __name__ == "__main__":
args = argument_parser.parse(sys.argv[1:])
agnostic_path = agnostic_paths.AgnosticPaths(ptb=args.ptb, force_path=args.force_path)
try:
spd = test_interface.run_check(args, agnostic_path)
except FileNotFoundError as er:
print(er)
sys.exit(1)
except OSError as er:
print(er.strerror)
sys.exit(1)
if spd == 0:
print("Discord Helper could not find anything wrong with your discord install!") # TODO: Everything should be in the run_all_tests()
sys.exit(spd)
test_interface.run_check(args, agnostic_path)

17
modules/internal_io.py Normal file
View File

@ -0,0 +1,17 @@
"""
Internal interfacting objects.
"""
from collections import namedtuple
global_status = namedtuple('global_status', 'total_tests, tests_finished, tests_failed, tests_running, tests_skipped', defaults=(0, 0, 0, 0, 0))
test_status = namedtuple('test_status', 'name, status, message', defaults=(None, None, None))
return_code_dict = {
2: "The test was not run",
3: "The test is running",
0: "The test is a success",
1: "Test has failed",
4: "Test found problems",
}

View File

@ -1,9 +1,10 @@
import asyncio
import os
from modules import test_template
class SpideyTest:
def __init__(self, args, agnpath, test_data):
class SpideyTest(test_template.TestTemplate):
def __init__(self, args, agnpath, test_data, queue):
"""
Simple test runner, for testing the detection of Spidey viruses.
:param args: List of arguments
@ -12,13 +13,10 @@ class SpideyTest:
:type agnpath: agnostic_path.AgnosticPath
:param test_data: The data to be tested
:type test_data: str
:param queue: The queue to put the status in
:type queue: queue.Queue
"""
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.alive = True
super().__init__(args, agnpath, test_data, queue)
def run_test(self):
"""
@ -52,7 +50,12 @@ class SpideyTest:
"is corrupted. We suggest uninstalling discord and deleting all its files before reinstalling.\n"
"If you've recently downloaded a .exe directly sent from someone, "
"do also consider a clean windows reinstall.")
return self.is_infected
print("Spidey test finished")
self.set_status(0)
status = self.get_status()
self.queue.append(status)
return self.get_status_code()
def get_path(self):
"""
@ -63,33 +66,3 @@ class SpideyTest:
discord_modules = self.agnostic_path("modules", "discord_modules-1", "discord_modules")
discord_desktop_core = self.agnostic_path("modules", "discord_desktop_core-1", "discord_desktop_core")
return discord_modules, discord_desktop_core
def get_status_code(self):
"""
The status code of the test
:return: 0: The test was not run, 1: The test is running, 2: The test is a success, 3: Test has failed, 4: Test found problems
:rtype: int
"""
# TODO: Add this to a global_dict
return 0
def get_status(self):
"""
Return the status of the test, as a string, as the test is ran
:return: "Test is running", "Test was not run", "Test was run and nothing was detected", "Test was run and some problems were detected (list of problems)"
:rtype: (int, str)
"""
# TODO: Make use of a global_dict
return self.get_status_code(), ""
async def main(args, agnpath, test_data):
"""
The main function of the test.
:return: The status of the test
:rtype: (int, str)
"""
spidey_test = SpideyTest(args, agnpath, test_data)
await asyncio.sleep(5)
spidey_test.run_test()
return spidey_test.get_status()

View File

@ -1,8 +1,10 @@
import threading
import os
import re
import sys
import importlib
import threading
import time
import queue
from collections import deque
from modules.internal_io import global_status, test_status, return_code_dict
"""
All modules that can be tested
@ -12,6 +14,7 @@ argparse argument : [module_name, class_name]
_TEST_MODULES = {
"spidey": ["spidey_test", "SpideyTest"],
}
POST_RUN_ALLOWED = False
class TestRunner:
@ -38,6 +41,9 @@ class TestRunner:
self.loaded_test_classes = {}
self.list_of_tests = []
self.update_module_list()
self.finished_tests = deque()
self.exec_all_tests()
def update_module_list(self):
@ -48,6 +54,7 @@ class TestRunner:
"""
modules = {}
classes = {}
module_name = None
try:
for module_name, values in _TEST_MODULES.items():
if self.args.__getattribute__(module_name):
@ -60,72 +67,136 @@ class TestRunner:
self.loaded_test_modules.update(modules)
self.loaded_test_classes.update(classes)
def test_controller(self, rate=1):
"""
Controller for the tests.
:param rate: The rate at which the controller will check the tests
:type rate: int
:return: 1 if all test ran, 0 if one or more tests failed, 2 if the user stopped the tests
"""
l_test = len(self.list_of_tests)
while True:
still_running = 0
for t in self.list_of_tests:
if t.is_alive():
still_running += 1
if still_running == 0:
return 1
def test_killer(self):
"""
Kills the tests if the user press enter
"""
while True:
if input() == "":
for t in self.list_of_tests:
t.terminate()
return
def exec_all_tests(self):
"""
Executes all the tests (later in parallel), get their result as they finishes
Executes all the tests, get their result as they finishes
:return: The number of test ran, success, failures, skipped (with ran = success+failures+skip)
:rtype: (int,int,int,int) # TODO: Change this to a nametuple
"""
self.list_of_tests = []
for module_name, test_class in self.loaded_test_classes.items():
tc = test_class(self.args, self.agnostic_path, None)
tc.daemon = True
tc = threading.Thread(target=tc.run_test)
self.list_of_tests.append(tc)
tc.start()
for i in range(10):
print(f"Starting test {module_name}")
tc = test_class(self.args, self.agnostic_path, None, self.finished_tests)
tc = threading.Thread(target=tc.run_test, args=())
tc.name = str(module_name)
tc.daemon = True
self.list_of_tests.append(tc)
tc.start()
# Launch a controller to allow the user to stop the tests
# Launch a controller to allow the user to stop all tests
# controller = threading.Thread(target=self.test_launcher)
# Allow the user to stop the tests
tk = threading.Thread(target=self.test_killer)
tk.start()
tc = threading.Thread(target=self.controller_test, args=())
tc.daemon = True
tc.start()
for t in self.list_of_tests:
# Await the end of the tests or the user interruption
tc = threading.Thread(target=self.post_test_runner, args=())
tc.start()
# Wait for the end of the tests
tc = threading.Thread(target=self.test_waiter, args=())
tc.daemon = True
tc.start()
return self.is_infected, self.detections, self.test_run
def controller_test(self):
"""
Kills the tests if the user press enter
"""
print("Press enter to stop the tests: ")
while True:
if input() == "":
if not self.is_infected:
global POST_RUN_ALLOWED
POST_RUN_ALLOWED = True
return
def post_test_runner(self):
"""
Runs the post test runner if all tests are finished
"""
global POST_RUN_ALLOWED
if self.args.launch:
while True:
if POST_RUN_ALLOWED:
print("Running post test runner")
return 0
time.sleep(0.5)
return 0
def test_waiter(self):
"""
Waits for the end of the tests
:return:
"""
for t in self.list_of_tests: # Can only proceed if all tests are finished
t.join()
if tk.is_alive():
tk.terminate()
# When all tests are finished or the user stopped the tests, we can proceed
if not self.is_infected:
global POST_RUN_ALLOWED
POST_RUN_ALLOWED = True
# await asyncio.gather(*list_of_tests)
# tc.run_test()
def get_exit_code(self):
"""
Gets the exit code of the tests
:return: The exit code
:rtype: int
"""
global POST_RUN_ALLOWED
if POST_RUN_ALLOWED:
for t in self.list_of_tests:
if t.is_alive():
return 2 # 2 means that the user stopped the tests before the end
return self.is_infected # The user didn't stop the tests
return -1 # The program is not finished yet
def get_status(self):
"""
Gets the status of the tests
:return: The status
:rtype: str
"""
nb_tests = len(self.list_of_tests)
nb_tests_success = 0
nb_tests_failure = 0
nb_tests_skipped = 0
for test in self.finished_tests:
print(f"Test {test.name} finished with status {test.status}")
if test.status == 0:
nb_tests_success += 1
elif test.status == 1:
nb_tests_failure += 1
elif test.status == 2:
nb_tests_skipped += 1
nb_tests_ran = nb_tests_success + nb_tests_failure + nb_tests_skipped
nb_test_running = nb_tests - nb_tests_ran
return global_status(
total_tests=nb_test_running,
tests_finished=nb_tests_ran,
tests_failed=nb_tests_failure,
tests_running=nb_test_running,
tests_skipped=nb_tests_skipped,
)
def run_check(args, agnpath): # Todo, skip the is_infected check and defer everything to the exec_all_tests
test_selected = True
spd = TestRunner(args, agnpath)
if not spd.is_infected:
return 0
else:
return 1
def run_check(args, agnpath):
"""
Runs all operations selected in the config
:param args: List of arguments
:param agnpath: Annostic path to the discord folder
:return: None
"""
tr = TestRunner(args, agnpath) # All operation are executed in parallel and the user can stop them
print("Waiting for the end of the tests")
while tr.get_exit_code() == -1:
print(tr.get_status())
time.sleep(0.5)
if __name__ == "__main__":
pass
sys.exit(tr.get_exit_code())

View File

@ -1,5 +1,12 @@
"""
A global template for all tests.
"""
from modules import internal_io
class TestTemplate:
def __init__(self, args, agnpath, test_data):
def __init__(self, args, agnpath, test_data, queue):
"""
Simple test template.
:param args: List of arguments
@ -17,13 +24,14 @@ class TestTemplate:
self.is_infected = False
self.status_code = 0
self.status = "Test not yet initialized."
self.return_code_dict = {
0: "The test was not run",
1: "The test is running",
2: "The test is a success",
3: "Test has failed",
4: "Test found problems",
}
self.queue = queue
def name(self):
"""
:return: Name of the test
:rtype: str
"""
return str(self.__class__.__name__)
def get_status_code(self):
"""
@ -37,15 +45,15 @@ class TestTemplate:
:return: Status at the time of call.
:rtype: str
"""
return self.status
return internal_io.test_status(self.name(), self.status_code, self.status)
@static
@staticmethod
def get_status_from_code(code):
"""
:return: Description of the status code
:rtype: str
"""
return self.return_code_dict[code]
return internal_io.return_code_dict[code]
def set_status(self, statuscode: int):
"""