from unittest import TestCase

from webauthn.helpers.tpm.parse_pub_area import parse_pub_area
from webauthn.helpers.tpm.structs import TPM_ALG, TPMPubAreaParametersRSA


class TestWebAuthnTPMParsePubArea(TestCase):
    def test_properly_parses_pub_area_bytes(self) -> None:
        pub_area = b'\x00\x01\x00\x0b\x00\x06\x04r\x00 \x9d\xff\xcb\xf3l8:\xe6\x99\xfb\x98h\xdcm\xcb\x89\xd7\x158\x84\xbe(\x03\x92,\x12AX\xbf\xad"\xae\x00\x10\x00\x10\x08\x00\x00\x00\x00\x00\x01\x00\xb4\xd2\x0c\x0c\xf9-\x02\xad\x8b\xd937k\x1acE\x90\x853\x008\x08S<\xa58\x15`-{\xc0b\xf5X\xdcO\x94\x86\xf6\x18\x14\xff\xde\x01n\x15\xcf\x88\xec\xe8~\x81\xc3\xd3\xbfg\x82\x89\xe6\xbb{\xffW\xff#I!\x03\xb1\x8a\xf6\x96\x9a\x80\xa5Xj\xf8+\x9a\x01r&\xbc\\\xae\x1dU,\x86\xc3q\'Uks\xe5K/\x14Sw\x01\xfaE\x9d\x81\xfcJWb\x80c\x1b\xc4\xf3\xc3\x1fd\x9f"\xfd\xce[8z\xe0\xea\xeb\x18\x81\xd3\x8fc\xab\xfa\x84\x8d\x840\\\x8fS\x1b>#R\xb1\x91\xd0\xa6\xf6&R\xee\xc56\x18T%"\xa5\xa23i\x96\xb0\xa6{\x82\xc8@\x14\xb0T<N\xb2\xd2\xceZ2^g\x06\xd27X\\\xd8@Y\x01\x1a\xc9\x01\x8f\xafd\x98\x96\x06ntE\xc7\x89\x88\xa5\xa6\xebVU\xdf\xa9\xe3\xe0\x9e`,\xa6\xc2\x03\xc5\xc5w\x08vZ|\x08K\xd9D\xa2\xb7@\xb2^F\xb2\xa2(\x0e\xae\xe4\xa6\xa9\xd3\xe0I1R;A\xd7'

        output = parse_pub_area(pub_area)

        assert output.type == TPM_ALG.RSA
        assert output.name_alg == TPM_ALG.SHA256
        assert (
            output.auth_policy
            == b'\x9d\xff\xcb\xf3l8:\xe6\x99\xfb\x98h\xdcm\xcb\x89\xd7\x158\x84\xbe(\x03\x92,\x12AX\xbf\xad"\xae'
        )
        assert (
            output.unique.value
            == b'\xb4\xd2\x0c\x0c\xf9-\x02\xad\x8b\xd937k\x1acE\x90\x853\x008\x08S<\xa58\x15`-{\xc0b\xf5X\xdcO\x94\x86\xf6\x18\x14\xff\xde\x01n\x15\xcf\x88\xec\xe8~\x81\xc3\xd3\xbfg\x82\x89\xe6\xbb{\xffW\xff#I!\x03\xb1\x8a\xf6\x96\x9a\x80\xa5Xj\xf8+\x9a\x01r&\xbc\\\xae\x1dU,\x86\xc3q\'Uks\xe5K/\x14Sw\x01\xfaE\x9d\x81\xfcJWb\x80c\x1b\xc4\xf3\xc3\x1fd\x9f"\xfd\xce[8z\xe0\xea\xeb\x18\x81\xd3\x8fc\xab\xfa\x84\x8d\x840\\\x8fS\x1b>#R\xb1\x91\xd0\xa6\xf6&R\xee\xc56\x18T%"\xa5\xa23i\x96\xb0\xa6{\x82\xc8@\x14\xb0T<N\xb2\xd2\xceZ2^g\x06\xd27X\\\xd8@Y\x01\x1a\xc9\x01\x8f\xafd\x98\x96\x06ntE\xc7\x89\x88\xa5\xa6\xebVU\xdf\xa9\xe3\xe0\x9e`,\xa6\xc2\x03\xc5\xc5w\x08vZ|\x08K\xd9D\xa2\xb7@\xb2^F\xb2\xa2(\x0e\xae\xe4\xa6\xa9\xd3\xe0I1R;A\xd7'
        )
        # Object Attributes
        assert output.object_attributes.fixed_tpm is True
        assert output.object_attributes.st_clear is False
        assert output.object_attributes.fixed_parent is True
        assert output.object_attributes.sensitive_data_origin is True
        assert output.object_attributes.user_with_auth is True
        assert output.object_attributes.admin_with_policy is False
        assert output.object_attributes.no_da is True
        assert output.object_attributes.encrypted_duplication is False
        assert output.object_attributes.restricted is False
        assert output.object_attributes.decrypt is True
        assert output.object_attributes.sign_or_encrypt is True
        # Parameters
        assert output.parameters.symmetric == TPM_ALG.NULL
        assert output.parameters.scheme == TPM_ALG.NULL
        assert isinstance(output.parameters, TPMPubAreaParametersRSA)
        assert output.parameters.key_bits == b"\x08\x00"
        assert output.parameters.exponent == b"\x00\x00\x00\x00"
