[GEMINI] Refactor test suite structure for new helpers and config modules
This commit is contained in:
@@ -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.
|
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()
|
runner = CliRunner()
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
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]+}"
|
assert updated_config["Competition"]["flag_format"] == "TEST_FLAG{[a-z]+}"
|
||||||
finally:
|
finally:
|
||||||
# Restore original config
|
# Restore original config
|
||||||
from ctf.utils import write_config
|
from ctf.config import write_config
|
||||||
write_config(original_config, str(config_file))
|
write_config(original_config, str(config_file))
|
||||||
|
|
||||||
def test_basic_set_flag_format_original_option_cli():
|
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.
|
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()
|
runner = CliRunner()
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
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
|
assert len(selected_pattern) > 0
|
||||||
finally:
|
finally:
|
||||||
# Restore original config
|
# Restore original config
|
||||||
from ctf.utils import write_config
|
from ctf.config import write_config
|
||||||
write_config(original_config, str(config_file))
|
write_config(original_config, str(config_file))
|
||||||
|
|
||||||
def test_basic_set_flag_format_validation_cli():
|
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.
|
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()
|
runner = CliRunner()
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
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))
|
updated_config = load_config(str(config_file))
|
||||||
assert updated_config["Competition"]["competition"] == "CyberCTF2026"
|
assert updated_config["Competition"]["competition"] == "CyberCTF2026"
|
||||||
finally:
|
finally:
|
||||||
from ctf.utils import write_config
|
from ctf.config import write_config
|
||||||
write_config(original_config, str(config_file))
|
write_config(original_config, str(config_file))
|
||||||
|
|
||||||
def test_basic_set_flag_format_warning_cli():
|
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,
|
Verifies that if competition name is set and not present in the example flag,
|
||||||
set-flag-format outputs a warning.
|
set-flag-format outputs a warning.
|
||||||
"""
|
"""
|
||||||
from ctf.utils import load_config, write_config
|
from ctf.config import load_config, write_config
|
||||||
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
config_file = Path("/home/venus/code/ctf/config.toml")
|
||||||
|
|||||||
33
tests/test_config.py
Normal file
33
tests/test_config.py
Normal file
@@ -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
|
||||||
|
# }}}
|
||||||
@@ -133,20 +133,5 @@ def test_try_decode_metadata_rot13_unique_only():
|
|||||||
assert "rot13" not in res_duplicate
|
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") == []
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import os
|
|||||||
import stat
|
import stat
|
||||||
import sys
|
import sys
|
||||||
from click.testing import CliRunner
|
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.forensics import get_metadata
|
||||||
from ctf.cli_forensics import forensics_group, 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.
|
# 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():
|
def test_active_categories_static():
|
||||||
"""Verifies active_categories using the pre-created 'comp1' folder."""
|
"""Verifies active_categories using the pre-created 'comp1' folder."""
|
||||||
|
|||||||
14
tests/test_helpers.py
Normal file
14
tests/test_helpers.py
Normal file
@@ -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") == []
|
||||||
|
# }}}
|
||||||
@@ -66,7 +66,7 @@ def test_flag_detector_stream_persistence():
|
|||||||
"""
|
"""
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from ctf.main import FlagDetectorStream
|
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
|
from pathlib import Path
|
||||||
|
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
config_file = Path("/home/venus/code/ctf/config.toml")
|
||||||
@@ -89,7 +89,7 @@ def test_flag_cmd_cli():
|
|||||||
"""
|
"""
|
||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
from ctf.main import cli
|
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
|
from pathlib import Path
|
||||||
|
|
||||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
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.
|
Verifies that running ctf flag is exempted from triggering the stdout flag warning box.
|
||||||
"""
|
"""
|
||||||
from ctf.main import main
|
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
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|||||||
Reference in New Issue
Block a user