"""Tests for pybyd._crypto._pkcs7."""

from __future__ import annotations

from pybyd._crypto._pkcs7 import add_pkcs7, strip_pkcs7


class TestAddPkcs7:
    def test_exact_block(self) -> None:
        result = add_pkcs7(b"a" * 16)
        assert len(result) == 32
        assert result[16:] == bytes([16] * 16)

    def test_partial_block(self) -> None:
        result = add_pkcs7(b"hello")  # 5 bytes -> needs 11 padding
        assert len(result) == 16
        assert result[5:] == bytes([11] * 11)

    def test_one_byte_short(self) -> None:
        result = add_pkcs7(b"a" * 15)
        assert len(result) == 16
        assert result[-1] == 1

    def test_empty(self) -> None:
        result = add_pkcs7(b"")
        assert len(result) == 16
        assert result == bytes([16] * 16)


class TestStripPkcs7:
    def test_valid_padding(self) -> None:
        padded = b"hello" + bytes([11] * 11)
        assert strip_pkcs7(padded) == b"hello"

    def test_full_block_padding(self) -> None:
        padded = b"a" * 16 + bytes([16] * 16)
        assert strip_pkcs7(padded) == b"a" * 16

    def test_single_byte_padding(self) -> None:
        padded = b"a" * 15 + bytes([1])
        assert strip_pkcs7(padded) == b"a" * 15

    def test_invalid_padding_returns_original(self) -> None:
        bad = b"hello" + bytes([11] * 10) + bytes([12])
        assert strip_pkcs7(bad) == bad

    def test_zero_padding_returns_original(self) -> None:
        data = b"test" + bytes([0])
        assert strip_pkcs7(data) == data

    def test_empty_returns_empty(self) -> None:
        assert strip_pkcs7(b"") == b""

    def test_roundtrip(self) -> None:
        original = b"test data"
        assert strip_pkcs7(add_pkcs7(original)) == original
