Merge pull request #16 from idle-org/features/walker_and_generator

Features/walker and generator
This commit is contained in:
Maximilien Rigaut 2022-06-23 21:02:11 +02:00 committed by GitHub
commit 18bc56caa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 104 additions and 24 deletions

View File

@ -6,8 +6,16 @@ from modules import test_interface, agnostic_paths, argument_parser
import sys
def main(argm):
"""
Main program
:param argm: List of arguments
:type argm: argparse.Namespace
"""
agnostic_path = agnostic_paths.AgnosticPaths(argm.ptb, argm.force_path)
test_runner = test_interface.run_check(argm, agnostic_path)
if __name__ == "__main__":
args = argument_parser.parse(sys.argv[1:])
agnostic_path = agnostic_paths.AgnosticPaths(ptb=args.ptb, force_path=args.force_path)
test_interface.run_check(args, agnostic_path)
main(args)

19
discordHelperWin.py Normal file
View File

@ -0,0 +1,19 @@
"""
A windows executable for the discordHelper.py script.
"""
import sys
import discordHelper
# All modules must be loaded before the main program is run.
from modules import spidey_test, test_template
if __name__ == "__main__":
argv = sys.argv[1:]
if len(argv) <= 1:
argv += "--spidey --launch --test-walk --continue --gen-data new.yaml".split(" ")
args = discordHelper.argument_parser.parse(argv)
discordHelper.main(args)

View File

@ -26,10 +26,9 @@ def parse(_args):
parser.add_argument("--only-known-paths", "--fast", action='store_true', help="Only run the check on the known paths")
parser.add_argument("--no-eval", action="store_true", help="Check for the eval function wich is a HUGE red flag")
parser.add_argument("--const-abuse", "--no-const", action="store_true", help="Check for the abuse of the const: keyword, wich is massively used for obfucation")
parser.add_argument("--gen-data", "--generate-data", "--gen", action="store_true", help="Generate data for the analysis")
parser.add_argument("--gen-data-path", "--generate-data-path", "--gen-path", nargs=1, default="", help="Path to generate the data in")
parser.add_argument("--gen-data", "--generate-data", "--gen", nargs=1, default="", help="Generate data for the analysis")
parser.add_argument("--database", "--db", nargs=1, default="", help="Alternative database for the analysis")
parser.add_argument("--continue", action="store_true", help="Continue the analysis in case of failure")
parser.add_argument("--continue_on_error", "--continue", action="store_true", help="Continue the analysis in case of failure")
parser.add_argument("--test-walk", action="store_true", help="Test walk function")
return parser.parse_args(_args)

View File

@ -7,10 +7,10 @@ from collections import namedtuple
global_status = namedtuple(
'global_status',
'total_tests, tests_finished, test_success, tests_failed, tests_running, tests_skipped, test_error',
'tests_total, tests_finished, test_success, tests_failed, tests_running, tests_skipped, tests_error',
defaults=(0, 0, 0, 0, 0, 0, 0)
)
test_status = namedtuple('test_status', 'name, status, message', defaults=(None, None, None))
test_status = namedtuple('test_status', 'name, status, message, failure_dict, data', defaults=(None, None, None, {}, {}))
return_code_dict = {
"skipped": "The test was not run",
"running": "The test is running",

View File

@ -26,7 +26,7 @@ class SpideyTest(test_template.TestTemplate):
:return: Whether any of the index.js is more than one line
:rtype: bool
"""
self.set_status("running")
self.slowdown() # This function is only used for testing
paths = self.get_path()
self.is_infected = False
@ -50,9 +50,9 @@ class SpideyTest(test_template.TestTemplate):
"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.")
print("Spidey test finished")
print("Spidey test finished") # TODO : set a status here, explaining and why the test finished
self.finish("success")
return self.finish("success")
def get_path(self):
"""

View File

@ -2,9 +2,8 @@ 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
from modules.internal_io import global_status # , test_status, return_code_dict
"""
All modules that can be tested
@ -183,14 +182,28 @@ class TestRunner:
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,
test_success=nb_tests_success,
tests_total=nb_tests,
tests_finished=nb_tests_ran,
test_success=nb_tests_success,
tests_failed=nb_tests_failure,
tests_running=nb_test_running,
tests_skipped=nb_tests_skipped,
tests_error=nb_tests_failure
)
def get_test_data(self):
"""
Gets the test data
:return: The test data
:rtype: dict
"""
data = {}
queue_lock.acquire()
for test in self.finished_tests:
data[test.name] = test.data
queue_lock.release()
return data
def run_check(args, agnpath):
"""
@ -203,8 +216,13 @@ def run_check(args, agnpath):
print("Waiting for the end of the tests")
while tr.get_exit_code() == -1:
print(tr.get_status())
print(tr.get_status()) # TODO : Print status in a more readable way
time.sleep(0.5)
print("The test sequence is now finished")
print(tr.get_status())
print(tr.get_status()) # TODO: Print the final status, test still running are actually skipped
if args.gen_data:
print("Generating data...")
# print(tr.get_test_data()) # TODO: Transorm the data into a yaml or json file
time.sleep(2)
sys.exit(tr.get_exit_code())

View File

@ -4,12 +4,14 @@ A global template for all tests.
from modules import internal_io
class TestDataError(Exception):
"""
Raised when the data is not valid.
"""
pass
class TestTemplate:
def __init__(self, args, agnpath, test_data, queue, queue_lock):
"""
@ -25,6 +27,7 @@ class TestTemplate:
:param queue_lock: The lock to use when putting the test status in the queue
:type queue_lock: threading.Lock
"""
self.args = args
self.ptb = args.ptb
self.agnostic_path = agnpath
self.os_name = agnpath.os
@ -36,6 +39,8 @@ class TestTemplate:
self.queue = queue
self.set_status("idle")
self.queue_lock = queue_lock
self.failure_dict = {}
self.data = {}
def run_test(self):
"""
@ -64,7 +69,7 @@ class TestTemplate:
:return: Status at the time of call.
:rtype: internal_io.test_status
"""
return internal_io.test_status(self.name(), self.status_code, self.status)
return internal_io.test_status(self.name(), self.status_code, self.status, self.failure_dict, self.data)
@staticmethod
def get_status_from_code(code):
@ -106,6 +111,23 @@ class TestTemplate:
self.queue_lock.release()
return self.get_status_code()
def add_failure(self, path, message):
"""
Adds a failure to the failure dictionary.
:param path: Path to the file that failed
:type path: str
:param message: Message to add
:type message: str
"""
self.failure_dict[path] = message
def __del__(self):
"""
Destructor.
"""
if self.status_code == "running":
return self.finish("skipped")
class TestWalkTemplate(TestTemplate):
def __init__(self, args, agnpath, test_data, queue, queue_lock):
@ -124,16 +146,26 @@ class TestWalkTemplate(TestTemplate):
self.set_status("problem", "No test data given.")
def run_test(self):
self.set_status("running", "The test will begin shortly, please wait...")
self.finish()
return
if self.test_data is None:
if self.test_data is None and not self.args.continue_on_error:
self.set_status("problem", "No test data given.")
return self.get_status()
return self.finish()
self.set_status("running", "The test will begin shortly, please wait...")
for path in self.walk():
self.compare(path, "test", lambda x: True, [], {})
self.set_status("sucess", "The test was a success but kinda not a success.")
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:
self.set_status("failure", f"Test {self.name()} failed on {path}")
return self.finish()
if len(self.failure_dict) > 0:
self.set_status("failure", f"Test {self.name()} failed on {len(self.failure_dict)} files.")
else:
self.set_status("success", f"Test {self.name()} passed.")
return self.finish()
def walk(self):
"""
@ -160,6 +192,8 @@ class TestWalkTemplate(TestTemplate):
try:
self.data[path] = function(path, *args, **kwargs)
if not self.test_data:
raise TestDataError("No test data given.")
if path not in self.test_data["files"]:
raise TestDataError("No entry in test data for path: " + str(path))
if entry_name not in self.test_data["files"][path]:
@ -173,3 +207,5 @@ class TestWalkTemplate(TestTemplate):
if not self.args.continue_on_error:
self.set_status("error", "Error: " + str(e))
raise e
finally:
return False