Merge pull request #15 from idle-org/features/walker_and_generator
Features/walker and generator
This commit is contained in:
commit
59695373d6
|
|
@ -0,0 +1,19 @@
|
|||
global:
|
||||
name: Windows Base
|
||||
version: 1.0
|
||||
|
||||
|
||||
files:
|
||||
/etc/passwd:
|
||||
sha256: "6c3d65f8b8b8f5a8f62b60c0e15d5d084d3b20f470f5d8b3d26bfb3c350d37a3"
|
||||
type: file
|
||||
size: "1024"
|
||||
permissions: "0644"
|
||||
owner: "root"
|
||||
group: "root"
|
||||
date: "2018-01-01T00:00:00Z"
|
||||
executable: "true"
|
||||
symlink: "false"
|
||||
md5: "6c3d65f8b8b8f5a8f62b60c0e15d5d084d3b20f470f5d8b3d26bfb3c350d37a3"
|
||||
sha1: "6c3d65f8b8b8f5a8f62b60c0e15d5d084d3b20f470f5d8b3d26bfb3c350d37a3"
|
||||
|
||||
|
|
@ -144,6 +144,17 @@ class AgnosticPaths:
|
|||
:rtype: str
|
||||
"""
|
||||
return self.main_path
|
||||
|
||||
def walk_all_files(self, *args):
|
||||
"""
|
||||
Walks all files in the given path
|
||||
:param args: The path
|
||||
:return: A list of all files in the given path
|
||||
:rtype: list[str]
|
||||
"""
|
||||
return [os.path.join(dirpath, filename)
|
||||
for dirpath, dirnames, filenames in os.walk(self(*args))
|
||||
for filename in filenames]
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -30,5 +30,6 @@ def parse(_args):
|
|||
parser.add_argument("--gen-data-path", "--generate-data-path", "--gen-path", nargs=1, default="", help="Path to generate the data in")
|
||||
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("--test-walk", action="store_true", help="Test walk function")
|
||||
|
||||
return parser.parse_args(_args)
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ from collections import namedtuple
|
|||
|
||||
global_status = namedtuple(
|
||||
'global_status',
|
||||
'total_tests, tests_finished, test_success, tests_failed, tests_running, tests_skipped',
|
||||
defaults=(0, 0, 0, 0, 0, 0)
|
||||
'total_tests, tests_finished, test_success, tests_failed, tests_running, tests_skipped, test_error',
|
||||
defaults=(0, 0, 0, 0, 0, 0, 0)
|
||||
)
|
||||
test_status = namedtuple('test_status', 'name, status, message', defaults=(None, None, None))
|
||||
return_code_dict = {
|
||||
|
|
|
|||
|
|
@ -4,19 +4,11 @@ from modules import test_template
|
|||
|
||||
|
||||
class SpideyTest(test_template.TestTemplate):
|
||||
def __init__(self, args, agnpath, test_data, queue):
|
||||
def __init__(self, args, agnpath, test_data, queue, queue_lock):
|
||||
"""
|
||||
Simple test runner, for testing the detection of Spidey viruses.
|
||||
:param args: List of arguments
|
||||
:type args: argparse.Namespace
|
||||
:param agnpath: The path to the discord folder
|
||||
: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
|
||||
"""
|
||||
super().__init__(args, agnpath, test_data, queue)
|
||||
super().__init__(args, agnpath, test_data, queue, queue_lock)
|
||||
|
||||
def slowdown(self):
|
||||
"""
|
||||
|
|
@ -60,10 +52,7 @@ class SpideyTest(test_template.TestTemplate):
|
|||
"do also consider a clean windows reinstall.")
|
||||
print("Spidey test finished")
|
||||
|
||||
self.set_status("success")
|
||||
status = self.get_status()
|
||||
self.queue.append(status)
|
||||
return self.get_status_code()
|
||||
self.finish("success")
|
||||
|
||||
def get_path(self):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -13,8 +13,10 @@ argparse argument : [module_name, class_name]
|
|||
"""
|
||||
_TEST_MODULES = {
|
||||
"spidey": ["spidey_test", "SpideyTest"],
|
||||
"test_walk": ["test_template", "TestWalkTemplate"],
|
||||
}
|
||||
POST_RUN_ALLOWED = False
|
||||
queue_lock = threading.Lock()
|
||||
|
||||
|
||||
class TestRunner:
|
||||
|
|
@ -61,8 +63,9 @@ class TestRunner:
|
|||
modules[values[0]] = importlib.import_module(f"modules.{values[0]}")
|
||||
classes[values[0]] = getattr(modules[values[0]], values[1])
|
||||
|
||||
except AttributeError:
|
||||
except AttributeError as e:
|
||||
print(f"The module {module_name} was not loaded because it was not specified in the config.")
|
||||
print(e)
|
||||
|
||||
self.loaded_test_modules.update(modules)
|
||||
self.loaded_test_classes.update(classes)
|
||||
|
|
@ -77,7 +80,7 @@ class TestRunner:
|
|||
for module_name, test_class in self.loaded_test_classes.items():
|
||||
for i in range(10):
|
||||
print(f"Starting test {module_name}")
|
||||
tc = test_class(self.args, self.agnostic_path, None, self.finished_tests)
|
||||
tc = test_class(self.args, self.agnostic_path, None, self.finished_tests, queue_lock)
|
||||
tc = threading.Thread(target=tc.run_test, args=())
|
||||
tc.name = str(module_name)
|
||||
tc.daemon = True
|
||||
|
|
@ -161,6 +164,7 @@ class TestRunner:
|
|||
:return: The status
|
||||
:rtype: str
|
||||
"""
|
||||
queue_lock.acquire()
|
||||
nb_tests = len(self.list_of_tests)
|
||||
nb_tests_success = 0
|
||||
nb_tests_failure = 0
|
||||
|
|
@ -173,7 +177,9 @@ class TestRunner:
|
|||
nb_tests_failure += 1
|
||||
elif test.status == "skipped":
|
||||
nb_tests_skipped += 1
|
||||
|
||||
elif test.status == "problem":
|
||||
nb_tests_failure += 1
|
||||
queue_lock.release()
|
||||
nb_tests_ran = nb_tests_success + nb_tests_failure + nb_tests_skipped
|
||||
nb_test_running = nb_tests - nb_tests_ran
|
||||
return global_status(
|
||||
|
|
|
|||
|
|
@ -4,9 +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):
|
||||
def __init__(self, args, agnpath, test_data, queue, queue_lock):
|
||||
"""
|
||||
Simple test template.
|
||||
:param args: List of arguments
|
||||
|
|
@ -15,6 +20,10 @@ class TestTemplate:
|
|||
:type agnpath: agnostic_path.AgnosticPath
|
||||
:param test_data: The data to be tested
|
||||
:type test_data: str
|
||||
:param queue: The queue to put the test status in
|
||||
:type queue: queue.deque
|
||||
:param queue_lock: The lock to use when putting the test status in the queue
|
||||
:type queue_lock: threading.Lock
|
||||
"""
|
||||
self.ptb = args.ptb
|
||||
self.agnostic_path = agnpath
|
||||
|
|
@ -26,6 +35,7 @@ class TestTemplate:
|
|||
self.status = "Test not yet initialized."
|
||||
self.queue = queue
|
||||
self.set_status("idle")
|
||||
self.queue_lock = queue_lock
|
||||
|
||||
def run_test(self):
|
||||
"""
|
||||
|
|
@ -81,3 +91,85 @@ class TestTemplate:
|
|||
else:
|
||||
self.status = self.get_status_from_code(statuscode)
|
||||
return self.status_code
|
||||
|
||||
def finish(self, statuscode: str = None, message: str = None):
|
||||
"""
|
||||
Finishes the test.
|
||||
"""
|
||||
if message is not None:
|
||||
self.status = message
|
||||
if statuscode is not None:
|
||||
self.status_code = statuscode
|
||||
|
||||
self.queue_lock.acquire()
|
||||
self.queue.append(self.get_status())
|
||||
self.queue_lock.release()
|
||||
return self.get_status_code()
|
||||
|
||||
|
||||
class TestWalkTemplate(TestTemplate):
|
||||
def __init__(self, args, agnpath, test_data, queue, queue_lock):
|
||||
"""
|
||||
Simple test template.
|
||||
:param args: List of arguments
|
||||
:type args: argparse.Namespace
|
||||
:param agnpath: The path to the discord folder
|
||||
:type agnpath: agnostic_path.AgnosticPath
|
||||
:param test_data: The data to be tested
|
||||
:type test_data: str
|
||||
"""
|
||||
super().__init__(args, agnpath, test_data, queue, queue_lock)
|
||||
self.data = {}
|
||||
if test_data is None:
|
||||
self.set_status("problem", "No test data given.")
|
||||
|
||||
def run_test(self):
|
||||
|
||||
self.finish()
|
||||
return
|
||||
if self.test_data is None:
|
||||
self.set_status("problem", "No test data given.")
|
||||
return self.get_status()
|
||||
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.")
|
||||
|
||||
def walk(self):
|
||||
"""
|
||||
Walks the given path and returns a list of all files and directories.
|
||||
:return: List of all files and directories
|
||||
:return:
|
||||
"""
|
||||
return self.agnostic_path.walk_all_files(self.agnostic_path())
|
||||
|
||||
def compare(self, path, entry_name, function, args, kwargs):
|
||||
"""
|
||||
Use the function on the given path, with given arguments and compare it with the entry in the test data.
|
||||
:param path: Path to the file or directory
|
||||
:type path: agnostic_path.AgnosticPath
|
||||
:param entry_name: Name of the entry in the test data
|
||||
:type entry_name: str
|
||||
:param function: Function to use on the path
|
||||
:type function: function
|
||||
:param args: Arguments to use on the function
|
||||
:type args: list
|
||||
:param kwargs: Keyword arguments to use on the function
|
||||
:type kwargs: dict
|
||||
"""
|
||||
|
||||
try:
|
||||
self.data[path] = function(path, *args, **kwargs)
|
||||
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]:
|
||||
raise TestDataError("No entry in test data for path: " + str(path) + " and entry name: " + str(entry_name))
|
||||
data_origin = self.test_data["files"][path][entry_name]
|
||||
self.data[path] = function(path, *args, **kwargs)
|
||||
if data_origin != self.data[path]:
|
||||
return False
|
||||
return True
|
||||
except TestDataError as e:
|
||||
if not self.args.continue_on_error:
|
||||
self.set_status("error", "Error: " + str(e))
|
||||
raise e
|
||||
|
|
|
|||
Reference in New Issue