# tests/test_decoding.py
from ctf.decoding import try_decode_metadata

# {{{ test_try_decode_metadata_hex
def test_try_decode_metadata_hex():
    """
    Verifies that try_decode_metadata successfully decodes standard hex string payloads.
    """
    # Hex encoding of "picoCTF{hex_flag_hint}"
    hex_str = "7069636f4354467b6865785f666c61675f68696e747d"
    res = try_decode_metadata(hex_str)
    assert res.get("hex") == "picoCTF{hex_flag_hint}"
# }}}

# {{{ test_try_decode_metadata_base64
def test_try_decode_metadata_base64():
    """
    Verifies that try_decode_metadata successfully decodes standard base64 string payloads.
    """
    # Base64 encoding of "flag{b64_metadata}"
    b64_str = "ZmxhZ3tiNjRfbWV0YWRhdGF9"
    res = try_decode_metadata(b64_str)
    assert res.get("base64") == "flag{b64_metadata}"
# }}}

# {{{ test_try_decode_metadata_non_decodable
def test_try_decode_metadata_non_decodable():
    """
    Verifies that try_decode_metadata returns empty results for invalid or non-printable payloads.
    """
    # Invalid characters
    res_invalid = try_decode_metadata("invalid!characters")
    assert not res_invalid

    # Not even length for hex, not modulo 4 for base64
    res_odd = try_decode_metadata("1")
    assert not res_odd

    # Non-printable decoded content (binary raw data)
    # Hex: \x01\x02\x03\x04
    res_binary = try_decode_metadata("01020304")
    assert not res_binary
# {{{ test_try_decode_metadata_base32
def test_try_decode_metadata_base32():
    """
    Verifies that try_decode_metadata successfully decodes standard base32 string payloads.
    """
    # Base32 encoding of "flag{b32_metadata}"
    b32_str = "MZWGCZ33MIZTEX3NMV2GCZDBORQX2==="
    res = try_decode_metadata(b32_str)
    assert res.get("base32") == "flag{b32_metadata}"
# }}}

# {{{ test_try_decode_metadata_binary
def test_try_decode_metadata_binary():
    """
    Verifies that try_decode_metadata successfully decodes binary strings.
    """
    # Binary encoding of "flag{bin}"
    bin_str = "011001100110110001100001011001110111101101100010011010010110111001111101"
    res = try_decode_metadata(bin_str)
    assert res.get("binary") == "flag{bin}"
# }}}

# {{{ test_try_decode_metadata_url
def test_try_decode_metadata_url():
    """
    Verifies that try_decode_metadata successfully decodes percent/URL encoded payloads.
    """
    url_str = "%66%6c%61%67%7b%75%72%6c%7d"
    res = try_decode_metadata(url_str)
    assert res.get("url") == "flag{url}"
# }}}

# {{{ test_try_decode_metadata_rot13_only_flags
def test_try_decode_metadata_rot13_only_flags():
    """
    Verifies that rot13 decodes flags but ignores standard text noise.
    """
    # ROT13 of "flag{rot13}" -> "synt{ebg13}"
    rot_str = "synt{ebg13}"
    res = try_decode_metadata(rot_str)
    assert res.get("rot13") == "flag{rot13}"

    # ROT13 of "normal text" -> "abezny grkg"
    res_noise = try_decode_metadata("abezny grkg")
    assert "rot13" not in res_noise
# }}}

# {{{ test_try_decode_metadata_reversed_only_flags
def test_try_decode_metadata_reversed_only_flags():
    """
    Verifies that reversed decodes flags but ignores standard text noise.
    """
    # Reversed of "flag{rev}" -> "}ver{galf"
    rev_str = "}ver{galf"
    res = try_decode_metadata(rev_str)
    assert res.get("reversed") == "flag{rev}"

    # Reversed of "normal text" -> "txet lamron"
    res_noise = try_decode_metadata("txet lamron")
    assert "reversed" not in res_noise
# {{{ test_try_decode_metadata_nested
def test_try_decode_metadata_nested():
    """
    Verifies that try_decode_metadata recursively decodes nested encodings and reports the path.
    """
    # Payload: base64(hex("flag{nested_flag}"))
    nested_str = "NjY2YzYxNjc3YjZlNjU3Mzc0NjU2NDVmNjY2YzYxNjc3ZA=="
    res = try_decode_metadata(nested_str)
    assert res.get("base64") == "666c61677b6e65737465645f666c61677d"
    assert res.get("base64->hex") == "flag{nested_flag}"
# {{{ test_try_decode_metadata_unanchored
def test_try_decode_metadata_unanchored():
    """
    Verifies that try_decode_metadata successfully decodes unanchored substrings (e.g. "steghide:cEF6endvcmQ=").
    """
    res = try_decode_metadata("steghide:cEF6endvcmQ=")
    assert res.get("base64") == "pAzzword"
# }}}

# {{{ test_try_decode_metadata_rot13_unique_only
def test_try_decode_metadata_rot13_unique_only():
    """
    Verifies that rot13 only decodes unique flags and ignores flags already visible.
    """
    # Case 1: Flag is encoded and not present in plaintext in the original string.
    res = try_decode_metadata("synt{ebg13_havdhr}")
    assert res.get("rot13") == "flag{rot13_unique}"

    # Case 2: Flag is already present in plaintext in the original string.
    res_duplicate = try_decode_metadata("flag{rot13_unique} synt{ebg13_havdhr}")
    assert "rot13" not in res_duplicate
# }}}



