From fd69df92f2bc914493eaf97ead6fe0369cdcb1a2 Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 17 Dec 2025 04:06:01 -0600 Subject: [PATCH] add: python tester --- .gitignore | 4 +++- Makefile | 7 ++++++- src/main.c | 3 ++- test/exectution.py | 4 ++++ test/parsing.py | 24 +++++++++++++++++++++ test/print.py | 8 +++++++ test/test.py | 24 +++++++++++++++++++++ test/tests.json | 47 ++++++++++++++++++++++++++++++++++++++++++ test/thread_manager.py | 11 ++++++++++ 9 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 test/exectution.py create mode 100644 test/parsing.py create mode 100644 test/print.py create mode 100644 test/test.py create mode 100644 test/tests.json create mode 100644 test/thread_manager.py diff --git a/.gitignore b/.gitignore index 95491b2..f3b6017 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build -obj \ No newline at end of file +obj +log +*.pyc \ No newline at end of file diff --git a/Makefile b/Makefile index 30a71dc..44a788d 100644 --- a/Makefile +++ b/Makefile @@ -29,5 +29,10 @@ re: $(MAKE) fclean $(MAKE) all -.PHONY: all clean fclean re +tests: + $(MAKE) all + mkdir -p log + python3 test/test.py build/$(NAME) + +.PHONY: all clean fclean re tests -include $(DEP) diff --git a/src/main.c b/src/main.c index 23e98cd..4034e4a 100644 --- a/src/main.c +++ b/src/main.c @@ -90,6 +90,8 @@ static int check_reply(struct sockaddr_in const *me, struct sockaddr_in const *s int main(int ac, char **av) { + signal(SIGINT, signal_handler); + (void) ac; struct statistics stats; struct setting settings; @@ -104,7 +106,6 @@ int main(int ac, char **av) size_t packet_size = sizeof(struct icmphdr) + settings.payload_size; size_t recv_packet_size = packet_size + sizeof(struct iphdr); - signal(SIGINT, signal_handler); if (dns_lookup(&settings.dest)) goto error0; diff --git a/test/exectution.py b/test/exectution.py new file mode 100644 index 0000000..3736886 --- /dev/null +++ b/test/exectution.py @@ -0,0 +1,4 @@ + + +def test_excution(excutable: str, tests_category: dict[str, list[dict[str, str]]], log_path: str): + pass \ No newline at end of file diff --git a/test/parsing.py b/test/parsing.py new file mode 100644 index 0000000..511024a --- /dev/null +++ b/test/parsing.py @@ -0,0 +1,24 @@ +import subprocess +import os +import signal +import time + +import thread_manager +from print import print_test + +def _test(title: str, excutable: str, args: str, exit_code: int, log_path: str) -> None: + if not os.path.exists(log_path): + os.makedirs(log_path) + f = open(f"{log_path}/{title}.log", "w") + p = subprocess.Popen(f"{excutable} {args}".split(" "), stdout=f, stderr=f) + time.sleep(0.05) + p.send_signal(signal.SIGINT) + value = p.wait() + f.close() + print_test(exit_code, value, title, args) + +def test_parsing(excutable: str, tests_category: dict[str, list[dict[str, str]]], log_path: str): + for category_name, tests in tests_category.items(): + print(category_name, end="\n" * 2) + for test in tests: + thread_manager.add_to_queu(_test, (test["title"], excutable, test['args'], test['exit'], f"{log_path}/{category_name}")) \ No newline at end of file diff --git a/test/print.py b/test/print.py new file mode 100644 index 0000000..6e232ce --- /dev/null +++ b/test/print.py @@ -0,0 +1,8 @@ + + +def print_test(expected_value, value, title: str, desc: str) -> None: + print(title, end="\t") + if expected_value == value: + print("[OK]") + return + print(f"[ERR]: {desc} ({expected_value} != {value})") \ No newline at end of file diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..775c7dc --- /dev/null +++ b/test/test.py @@ -0,0 +1,24 @@ +from collections.abc import Callable +import json +import sys +import os +import shutil + +import thread_manager +import parsing +import exectution + +excutable: str = sys.argv[1] + +with open('test/tests.json', 'r') as file: + tests: dict[str, dict] = json.load(file) + +tester: dict[str, Callable[[str, dict], None]] = {"parsing": parsing.test_parsing, "excution": exectution.test_excution} + +shutil.rmtree("log") + +for category_name, tests in tests.items(): + print(category_name, end="\n" * 3) + tester[category_name](excutable, tests, f"log/{category_name}") + thread_manager.wait_pool() + diff --git a/test/tests.json b/test/tests.json new file mode 100644 index 0000000..55d8b1c --- /dev/null +++ b/test/tests.json @@ -0,0 +1,47 @@ +{ + "parsing": { + "working": [ + { + "title": "normal", + "args": "1.1.1.1", + "exit": 0 + }, + { + "title": "dns", + "args": "google.com", + "exit": 0 + }, + { + "title": "ttl", + "args": "1.1.1.1 --ttl 128", + "exit": 0 + }, + { + "title": "preload", + "args": "1.1.1.1 -p 1", + "exit": 0 + }, + { + "title": "payload size", + "args": "1.1.1.1 -s 1", + "exit": 0 + } + ] + }, + "excution": { + "payload_size": [ + { + "title": "normal", + "args": "1.1.1.1 -s 56" + }, + { + "title": "empty payload", + "args": "1.1.1.1 -s 0" + }, + { + "title": "too high", + "args": "1.1.1.1 -s " + } + ] + } +} \ No newline at end of file diff --git a/test/thread_manager.py b/test/thread_manager.py new file mode 100644 index 0000000..546ef3f --- /dev/null +++ b/test/thread_manager.py @@ -0,0 +1,11 @@ +import concurrent.futures + +_pool = concurrent.futures.ThreadPoolExecutor(max_workers=10) + +def add_to_queu(func: callable, args: tuple): + global _pool + _pool.submit(func, *args) + +def wait_pool(): + global _pool + _pool.shutdown(wait=True) \ No newline at end of file