# tests/test_analysis.py from pathlib import Path import pytest from click.testing import CliRunner from ctf.analysis import run_analysis, RouteResult TEST_ENV = Path("tests/env") def test_run_analysis_no_file(): """ Verifies that run_analysis returns non-executed metadata route when file_path is None. """ result = run_analysis(None) assert not result.success assert len(result.routes) == 1 assert not result.routes[0].executed assert "No file attached" in result.routes[0].message def test_run_analysis_file_not_found(): """ Verifies that run_analysis returns a failed metadata route when path does not exist. """ result = run_analysis(Path("non_existent_file_xyz.jpg")) assert not result.success assert len(result.routes) == 1 assert result.routes[0].executed assert not result.routes[0].success assert "File not found" in result.routes[0].message def test_run_analysis_success_with_flag(): """ Verifies that run_analysis successfully extracts flag from file comment. """ import struct comment_text = b"flag{metadata_analyse_success}" comment_len = len(comment_text) + 2 mock_jpeg = b"\xff\xd8\xff\xfe" + struct.pack(">H", comment_len) + comment_text + b"\xff\xd9" test_file = TEST_ENV / "mock_analyse_flag.jpg" with open(test_file, "wb") as f: f.write(mock_jpeg) result = run_analysis(test_file) assert result.success assert len(result.routes) == 1 assert result.routes[0].success assert "flag{metadata_analyse_success}" in result.routes[0].extracted_flags def test_cli_analyse_success(): """ Verifies that 'ctf analyse' CLI successfully runs and outputs the extracted flags. """ from ctf.main import cli runner = CliRunner() import struct comment_text = b"flag{cli_analyse_success}" comment_len = len(comment_text) + 2 mock_jpeg = b"\xff\xd8\xff\xfe" + struct.pack(">H", comment_len) + comment_text + b"\xff\xd9" test_file = TEST_ENV / "mock_cli_analyse_flag.jpg" with open(test_file, "wb") as f: f.write(mock_jpeg) result = runner.invoke(cli, ["analyse", "-f", str(test_file)]) assert result.exit_code == 0 assert "Starting dynamic challenge analysis" in result.output assert "Route: File Metadata Extraction" in result.output assert "flag{cli_analyse_success}" in result.output assert "Challenge solved successfully!" in result.output def test_cli_analyse_no_file(): """ Verifies that 'ctf analyse' CLI displays warnings when run without -f option. """ from ctf.main import cli runner = CliRunner() result = runner.invoke(cli, ["analyse"]) assert result.exit_code == 0 assert "No file attached to analyze" in result.output assert "No clear flag found yet" in result.output