"""
Atheris fuzzing target for the waterfall vulnerability.

This file is automatically discovered by FuzzForge's AtherisFuzzer module.
The fuzzer looks for files named: fuzz_*.py, *_fuzz.py, or fuzz_target.py
"""

import sys
import atheris

# Enable coverage instrumentation for imported modules
# This is critical for discovering the waterfall vulnerability!
with atheris.instrument_imports():
    from main import check_secret


def TestOneInput(data):
    """
    Atheris fuzzing entry point.

    This function is called by Atheris for each fuzzing iteration.
    The fuzzer will try to find inputs that cause crashes.

    Args:
        data: Bytes to test (generated by Atheris)

    The waterfall vulnerability:
    - check_secret() validates input character-by-character
    - Each correct character creates a distinct code path
    - Coverage-guided fuzzing progressively discovers the secret "FUZZINGLABS"
    - When the complete secret is found, it crashes with SystemError

    With atheris.instrument_imports(), the main module is instrumented
    for coverage, allowing Atheris to detect when inputs reach new
    code paths (each correct character).
    """
    # Call the vulnerable function
    # It will raise SystemError when the secret is fully discovered
    check_secret(bytes(data))


if __name__ == "__main__":
    """
    Standalone fuzzing mode.

    Run directly: python fuzz_target.py
    """
    print("=" * 60)
    print("Atheris Fuzzing - Waterfall Vulnerability")
    print("=" * 60)
    print("Fuzzing will try to discover the secret string...")
    print("Watch for progress indicators: [DEBUG] Progress: X/11")
    print()
    print("Press Ctrl+C to stop fuzzing")
    print("=" * 60)
    print()

    # Setup Atheris with command-line args
    atheris.Setup(sys.argv, TestOneInput)

    # Start fuzzing
    atheris.Fuzz()
