[GEMINI] Reorganize tests for FlagDetectorStream and cli_helpers commands
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
# tests/test_helpers.py
|
||||
from pathlib import Path
|
||||
from ctf.helpers import check_for_flag
|
||||
|
||||
# {{{ test_check_for_flag
|
||||
@@ -29,3 +30,77 @@ def test_is_valid_flag():
|
||||
assert not is_valid_flag("flag{same}", original="flag{same}")
|
||||
# }}}
|
||||
|
||||
# {{{ test_flag_detector_stream_detection
|
||||
def test_flag_detector_stream_detection():
|
||||
"""
|
||||
Verifies that FlagDetectorStream successfully intercepts writes to output potential flags.
|
||||
"""
|
||||
from io import StringIO
|
||||
from ctf.helpers import FlagDetectorStream
|
||||
|
||||
out = StringIO()
|
||||
stream = FlagDetectorStream(out, r"flag\{[a-z_]+\}")
|
||||
|
||||
stream.write("Some text before flag{my_flag_here} and text after.")
|
||||
output = out.getvalue()
|
||||
assert "Potential flag(s) detected in command output" in output
|
||||
assert "flag{my_flag_here}" in output
|
||||
# }}}
|
||||
|
||||
# {{{ test_flag_detector_stream_anchored_stripping
|
||||
def test_flag_detector_stream_anchored_stripping():
|
||||
"""
|
||||
Verifies that FlagDetectorStream strips standard anchors ^ and $ to support substring searches.
|
||||
"""
|
||||
from io import StringIO
|
||||
from ctf.helpers import FlagDetectorStream
|
||||
|
||||
out = StringIO()
|
||||
stream = FlagDetectorStream(out, r"^flag\{[a-z_]+\}$")
|
||||
|
||||
stream.write("random flag{my_flag_here} data")
|
||||
output = out.getvalue()
|
||||
assert "Potential flag(s) detected in command output" in output
|
||||
assert "flag{my_flag_here}" in output
|
||||
# }}}
|
||||
|
||||
# {{{ test_flag_detector_stream_no_pattern
|
||||
def test_flag_detector_stream_no_pattern():
|
||||
"""
|
||||
Verifies that FlagDetectorStream passes through text unmodified if no flag pattern is configured.
|
||||
"""
|
||||
from io import StringIO
|
||||
from ctf.helpers import FlagDetectorStream
|
||||
|
||||
out = StringIO()
|
||||
stream = FlagDetectorStream(out, "")
|
||||
|
||||
stream.write("normal output flag{hello}")
|
||||
assert out.getvalue() == "normal output flag{hello}"
|
||||
# }}}
|
||||
|
||||
# {{{ test_flag_detector_stream_persistence
|
||||
def test_flag_detector_stream_persistence():
|
||||
"""
|
||||
Verifies that FlagDetectorStream writes the detected flag to config.toml.
|
||||
"""
|
||||
from io import StringIO
|
||||
from ctf.helpers import FlagDetectorStream
|
||||
from ctf.config import load_config, write_config
|
||||
|
||||
config_file = Path("/home/venus/code/ctf/config.toml")
|
||||
original_config = load_config(str(config_file))
|
||||
|
||||
try:
|
||||
out = StringIO()
|
||||
stream = FlagDetectorStream(out, r"flag\{[a-z_]+\}")
|
||||
stream.write("found flag{persisted_flag} in stdout")
|
||||
|
||||
# Reload config and check last_flag
|
||||
updated_config = load_config(str(config_file))
|
||||
assert updated_config.get("Competition", {}).get("last_flag") == "flag{persisted_flag}"
|
||||
finally:
|
||||
write_config(original_config, str(config_file))
|
||||
# }}}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user