From 4bddd4a49b7ec3fadf27a2e14d81bdfa89ac62ad Mon Sep 17 00:00:00 2001 From: venus Date: Sun, 19 Jul 2026 00:53:00 -0500 Subject: [PATCH] [GEMINI] Refactor test suite structure for new helpers and config modules --- tests/test_commands.py | 14 +++++++------- tests/test_config.py | 33 +++++++++++++++++++++++++++++++++ tests/test_decoding.py | 15 --------------- tests/test_forensics.py | 16 ++-------------- tests/test_helpers.py | 14 ++++++++++++++ tests/test_main.py | 6 +++--- 6 files changed, 59 insertions(+), 39 deletions(-) create mode 100644 tests/test_config.py create mode 100644 tests/test_helpers.py diff --git a/tests/test_commands.py b/tests/test_commands.py index 3a6be40..3f45ba0 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -27,7 +27,7 @@ def test_basic_set_flag_format_cli(): """ Verifies that 'basic set-flag-format' CLI command writes the pattern to config.toml. """ - from ctf.utils import load_config + from ctf.config import load_config runner = CliRunner() config_file = Path("/home/venus/code/ctf/config.toml") @@ -45,14 +45,14 @@ def test_basic_set_flag_format_cli(): assert updated_config["Competition"]["flag_format"] == "TEST_FLAG{[a-z]+}" finally: # Restore original config - from ctf.utils import write_config + from ctf.config import write_config write_config(original_config, str(config_file)) def test_basic_set_flag_format_original_option_cli(): """ Verifies that 'basic set-flag-format' with --original/-o option prompts for selection and writes it. """ - from ctf.utils import load_config + from ctf.config import load_config runner = CliRunner() config_file = Path("/home/venus/code/ctf/config.toml") @@ -78,7 +78,7 @@ def test_basic_set_flag_format_original_option_cli(): assert len(selected_pattern) > 0 finally: # Restore original config - from ctf.utils import write_config + from ctf.config import write_config write_config(original_config, str(config_file)) def test_basic_set_flag_format_validation_cli(): @@ -109,7 +109,7 @@ def test_basic_set_competition_cli(): """ Verifies that 'basic set-competition' updates the competition name in config.toml. """ - from ctf.utils import load_config + from ctf.config import load_config runner = CliRunner() config_file = Path("/home/venus/code/ctf/config.toml") @@ -123,7 +123,7 @@ def test_basic_set_competition_cli(): updated_config = load_config(str(config_file)) assert updated_config["Competition"]["competition"] == "CyberCTF2026" finally: - from ctf.utils import write_config + from ctf.config import write_config write_config(original_config, str(config_file)) def test_basic_set_flag_format_warning_cli(): @@ -131,7 +131,7 @@ def test_basic_set_flag_format_warning_cli(): Verifies that if competition name is set and not present in the example flag, set-flag-format outputs a warning. """ - from ctf.utils import load_config, write_config + from ctf.config import load_config, write_config runner = CliRunner() config_file = Path("/home/venus/code/ctf/config.toml") diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..ee76691 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,33 @@ +# tests/test_config.py +import toml +from pathlib import Path +from ctf.config import load_config, write_config + +TEST_ENV = Path("tests/env") + +# {{{ test_load_config_static +def test_load_config_static(): + """Verifies load_config using the persistent test file.""" + config_file = TEST_ENV / "config.toml" + test_data = { + "Competition": {"name": "PersistentComp"}, + "Enviroment": {"data_dir": str(TEST_ENV.absolute())} + } + config_file.parent.mkdir(parents=True, exist_ok=True) + with open(config_file, "w") as f: + toml.dump(test_data, f) + + loaded_data = load_config(str(config_file)) + assert loaded_data["Competition"]["name"] == "PersistentComp" +# }}} + +# {{{ test_write_config_static +def test_write_config_static(): + """Verifies write_config by writing and reloading.""" + config_file = TEST_ENV / "test_write_config.toml" + test_data = {"TestKey": "TestVal"} + + write_config(test_data, str(config_file)) + loaded = load_config(str(config_file)) + assert loaded == test_data +# }}} diff --git a/tests/test_decoding.py b/tests/test_decoding.py index 2931f91..edc5a54 100644 --- a/tests/test_decoding.py +++ b/tests/test_decoding.py @@ -133,20 +133,5 @@ def test_try_decode_metadata_rot13_unique_only(): assert "rot13" not in res_duplicate # }}} -# {{{ test_check_for_flag -def test_check_for_flag(): - """ - Verifies that check_for_flag extracts multiple matching flags or returns empty list. - """ - from ctf.decoding import check_for_flag - # Default fallback - matches multiple - res = check_for_flag("flag{one} and flag{two}") - assert set(res) == {"flag{one}", "flag{two}"} - # Custom format - res_custom = check_for_flag("custom{first} custom{second}", r"custom\{[a-z]+\}") - assert set(res_custom) == {"custom{first}", "custom{second}"} - # No matches - assert check_for_flag("no flag matches here") == [] -# }}} diff --git a/tests/test_forensics.py b/tests/test_forensics.py index b105088..27065de 100644 --- a/tests/test_forensics.py +++ b/tests/test_forensics.py @@ -4,7 +4,8 @@ import os import stat import sys from click.testing import CliRunner -from ctf.utils import load_config, active_categories +from ctf.config import load_config +from ctf.utils import active_categories from ctf.forensics import get_metadata from ctf.cli_forensics import forensics_group, metadata @@ -46,19 +47,6 @@ TEST_ENV = Path("tests/env") # where POSIX ownership resolution or xattr is not native. # ===================================================================== -def test_load_config_static(): - """Verifies load_config using the persistent test file.""" - config_file = TEST_ENV / "config.toml" - test_data = { - "Competition": {"name": "PersistentComp"}, - "Enviroment": {"data_dir": str(TEST_ENV.absolute())} - } - config_file.parent.mkdir(parents=True, exist_ok=True) - with open(config_file, "w") as f: - toml.dump(test_data, f) - - loaded_data = load_config(str(config_file)) - assert loaded_data["Competition"]["name"] == "PersistentComp" def test_active_categories_static(): """Verifies active_categories using the pre-created 'comp1' folder.""" diff --git a/tests/test_helpers.py b/tests/test_helpers.py new file mode 100644 index 0000000..961227b --- /dev/null +++ b/tests/test_helpers.py @@ -0,0 +1,14 @@ +# tests/test_helpers.py +from ctf.helpers import check_for_flag + +# {{{ test_check_for_flag +def test_check_for_flag(): + """ + Verifies that check_for_flag extracts multiple matching flags or returns empty list. + """ + res = check_for_flag("flag{one} and flag{two}") + assert set(res) == {"flag{one}", "flag{two}"} + res_custom = check_for_flag("custom{first} custom{second}", r"custom\{[a-z]+\}") + assert set(res_custom) == {"custom{first}", "custom{second}"} + assert check_for_flag("no flag matches here") == [] +# }}} diff --git a/tests/test_main.py b/tests/test_main.py index 4c3e8e9..365a0af 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -66,7 +66,7 @@ def test_flag_detector_stream_persistence(): """ from io import StringIO from ctf.main import FlagDetectorStream - from ctf.utils import load_config, write_config + from ctf.config import load_config, write_config from pathlib import Path config_file = Path("/home/venus/code/ctf/config.toml") @@ -89,7 +89,7 @@ def test_flag_cmd_cli(): """ from click.testing import CliRunner from ctf.main import cli - from ctf.utils import load_config, write_config + from ctf.config import load_config, write_config from pathlib import Path config_file = Path("/home/venus/code/ctf/config.toml") @@ -123,7 +123,7 @@ def test_flag_cmd_exemption(capsys): Verifies that running ctf flag is exempted from triggering the stdout flag warning box. """ from ctf.main import main - from ctf.utils import load_config, write_config + from ctf.config import load_config, write_config from pathlib import Path import sys from unittest.mock import patch