import sys import pytest from unittest.mock import patch from ctf.main import main def test_main_entry_point_help(): """ Verifies that calling main() executes the Click CLI app and responds to '--help' by listing registration groups. """ # Mock sys.argv to simulate running 'ctf --help' from the shell with patch.object(sys, "argv", ["ctf", "--help"]): # Click calls sys.exit() after displaying help, raising SystemExit with pytest.raises(SystemExit) as exc_info: main() # Verify it exits with a successful exit code (0) assert exc_info.value.code == 0 def test_flag_detector_stream_detection(): """ Verifies that FlagDetectorStream successfully intercepts writes to output potential flags. """ from io import StringIO from ctf.main 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 def test_flag_detector_stream_anchored_stripping(): """ Verifies that FlagDetectorStream strips standard anchors ^ and $ to support substring searches. """ from io import StringIO from ctf.main 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 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.main import FlagDetectorStream out = StringIO() stream = FlagDetectorStream(out, "") stream.write("normal output flag{hello}") assert out.getvalue() == "normal output flag{hello}"