"""Tests for ``jose.backends._asn1``."""

import base64

import pytest

try:
    from jose.backends import _asn1
except ImportError:
    _asn1 = None

pytestmark = [
    pytest.mark.pycrypto,
    pytest.mark.pycryptodome,
    pytest.mark.skipif(_asn1 is None, reason="ASN1 backend not available"),
]

PKCS1_PRIVATE_KEY = b"""MIIJKwIBAAKCAgEAtSKfSeI0fukRIX38AHlKB1YPp
X8PUYN2JdvfM+XjNmLfU1M74N0VmdzIX95sneQGO9kC2xMIE+AIlt52Yf/KgBZggA
lS9Y0Vx8DsSL2HvOjguAdXir3vYLvAyyHin/mUisJOqccFKChHKjnk0uXy/38+1r1
7/cYTp76brKpU1I4kM20M//dbvLBWjfzyw9ehufr74aVwr+0xJfsBVr2oaQFww/XH
Gz69Q7yHK6DbxYO4w4q2sIfcC4pT8XTPHo4JZ2M733Ea8a7HxtZS563/mhhRZLU5a
ynQpwaVv2U++CL6EvGt8TlNZOkeRv8wz+Rt8B70jzoRpVK36rR+pHKlXhMGT619v8
2LneTdsqA25Wi2Ld/c0niuul24A6+aaj2u9SWbxA9LmVtFntvNbRaHXE1SLpLPoIp
8uppGF02Nz2v3ld8gCnTTWfq/BQ80Qy8e0coRRABECZrjIMzHEg6MloRDy4na0pRQ
v61VogqRKDU2r3/VezFPQDb3ciYsZjWBr3HpNOkUjTrvLmFyOE9Q5R/qQGmc6BYtf
k5rn7iIfXlkJAZHXhBy+ElBuiBM+YSkFM7dH92sSIoZ05V4MP09Xcppx7kdwsJy72
Sust9Hnd9B7V35YnVF6W791lVHnenhCJOziRmkH4xLLbPkaST2Ks3IHH7tVltM6Ns
Rk3jNdVMCAwEAAQKCAgEArx+0JXigDHtFZr4pYEPjwMgCBJ2dr8+L8PptB/4g+LoK
9MKqR7M4aTO+PoILPXPyWvZq/meeDakyZLrcdc8ad1ArKF7baDBpeGEbkRA9JfV5H
jNq/ea4gyvDMCGou8ZPSQCnkRmr8LFQbJDgnM5Za5AYrwEv2aEh67IrTHq53W83rM
ioIumCNiG+7TQ7eg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Tp76brKpU1I4kM20M//dbvLBWjfzyw9ehu
fr74aVwr+0xJfsBVr2oaQFww/XHGz69Q7yHK6DbxYO4w4q2sIfcC4pT8XTPHo4JZ2
M733Ea8a7HxtZS563/mhhRZLU5aynQpwaVv2U++CL6EvGt8TlNZOkeRv8wz+Rt8B7
0jzoRpVK36rR+pHKlXhMGT619v82LneTdsqA25Wi2Ld/c0niuul24A6+aaj2u9SWb
xA9LmVtFntvNbRaHXE1SLpLPoIp8uppGF02Nz2v3ld8gCnTTWfq/BQ80Qy8e0coRR
ABECZrjIMzHEg6MloRDy4na0pRQv61VogqRKDU2r3/VezFPQDb3ciYsZjWBr3HpNO
kUjTrvLmFyOE9Q5R/qQGmc6BYtfk5rn7iIfXlkJAZHXhBy+ElBuiBM+YSkFM7dH92
sSIoZ05V4MP09Xcppx7kdwsJy72Sust9Hnd9B7V35YnVF6W791lVHnenhCJOziRmk
H4xLLbPkaST2Ks3IHH7tVltM6NsRk3jNdVMCAwEAAQ=="""


def test_rsa_private_key_pkcs1_to_pkcs8():
    pkcs1 = base64.b64decode(PKCS1_PRIVATE_KEY)
    pkcs8 = base64.b64decode(PKCS8_PRIVATE_KEY)

    assert _asn1.rsa_private_key_pkcs1_to_pkcs8(pkcs1) == pkcs8


def test_rsa_private_key_pkcs8_to_pkcs1():
    pkcs1 = base64.b64decode(PKCS1_PRIVATE_KEY)
    pkcs8 = base64.b64decode(PKCS8_PRIVATE_KEY)

    assert _asn1.rsa_private_key_pkcs8_to_pkcs1(pkcs8) == pkcs1


def test_rsa_public_key_pkcs1_to_pkcs8():
    pkcs1 = base64.b64decode(PKCS1_PUBLIC_KEY)
    pkcs8 = base64.b64decode(PKCS8_PUBLIC_KEY)

    assert _asn1.rsa_public_key_pkcs1_to_pkcs8(pkcs1) == pkcs8


def test_rsa_public_key_pkcs8_to_pkcs1():
    pkcs1 = base64.b64decode(PKCS1_PUBLIC_KEY)
    pkcs8 = base64.b64decode(PKCS8_PUBLIC_KEY)

    assert _asn1.rsa_public_key_pkcs8_to_pkcs1(pkcs8) == pkcs1
