<commit_before>
from jose.constants import ALGORITHMS
from jose.exceptions import JOSEError, JWKError

from jose.backends import ECKey
try:
    from jose.backends.ecdsa_backend import ECDSAECKey
    import ecdsa
except ImportError:
    ECDSAECKey = ecdsa = None

try:
    from jose.backends.cryptography_backend import CryptographyECKey
except ImportError:
    CryptographyECKey = None

import pytest

private_key = """-----BEGIN EC PRIVATE KEY-----
<KEY> KEY-----"""

# Private key generated using NIST256p curve
TOO_SHORT_PRIVATE_KEY = b"""\
-----BEGIN EC PRIVATE KEY-----
<KEY>
"""


def _backend_exception_types():
    """Build the backend exception types based on available backends."""
    if None not in (ECDSAECKey, ecdsa):
        yield ECDSAECKey, ecdsa.BadDigestError

    if CryptographyECKey is not None:
        yield CryptographyECKey, TypeError


@pytest.mark.ecdsa
@pytest.mark.skipif(
    None in (ECDSAECKey, ecdsa),
    reason="python-ecdsa backend not available"
)
def test_key_from_ecdsa():
    key = ecdsa.SigningKey.from_pem(private_key)
    assert not ECKey(key, ALGORITHMS.ES256).is_public()


class TestECAlgorithm:

    def test_key_from_pem(self):
        assert not ECKey(private_key, ALGORITHMS.ES256).is_public()

    def test_to_pem(self):
        key = ECKey(private_key, ALGORITHMS.ES256)
        assert not key.is_public()
        assert key.to_pem().strip() == private_key.strip().encode('utf-8')

        public_pem = key.public_key().to_pem()
        assert ECKey(public_pem, ALGORITHMS.ES256).is_public()
<commit_msg>add tests to verify der/asn1 conversions<commit_after>
from jose.constants import ALGORITHMS
from jose.exceptions import JOSEError, JWKError

from jose.backends import ECKey
try:
    from jose.backends.ecdsa_backend import ECDSAECKey
    import ecdsa
except ImportError:
    ECDSAECKey = ecdsa = None

try:
    from jose.backends.cryptography_backend import CryptographyECKey
except ImportError:
    CryptographyECKey = None

import pytest

private_key = """-----BEGIN EC PRIVATE KEY-----
<KEY>
-----END EC PRIVATE KEY-----"""

# Private key generated using NIST256p curve
TOO_SHORT_PRIVATE_KEY = b"""\
-----BEGIN EC PRIVATE KEY-----
<KEY>
-----END EC PRIVATE KEY-----
"""

# ES256 signatures generated to test conversion logic
DER_SIGNATURE = (
    b"0F\x02!\x00\x89yG\x81W\x01\x11\x9b0\x08\xa4\xd0\xe3g([\x07\xb5\x01\xb3"
    b"\x9d\xdf \xd1\xbc\xedK\x01\x87:}\xf2\x02!\x00\xb2shTA\x00\x1a\x13~\xba"
    b"J\xdb\xeem\x12\x1e\xfeMO\x04\xb2[\x86A\xbd\xc6hu\x953X\x1e"
)
ASN1_SIGNATURE = (
    b"\x89yG\x81W\x01\x11\x9b0\x08\xa4\xd0\xe3g([\x07\xb5\x01\xb3\x9d\xdf "
    b"\xd1\xbc\xedK\x01\x87:}\xf2\xb2shTA\x00\x1a\x13~\xbaJ\xdb\xeem\x12\x1e"
    b"\xfeMO\x04\xb2[\x86A\xbd\xc6hu\x953X\x1e"
)


def _backend_exception_types():
    """Build the backend exception types based on available backends."""
    if None not in (ECDSAECKey, ecdsa):
        yield ECDSAECKey, ecdsa.BadDigestError

    if CryptographyECKey is not None:
        yield CryptographyECKey, TypeError


@pytest.mark.ecdsa
@pytest.mark.skipif(
    None in (ECDSAECKey, ecdsa),
    reason="python-ecdsa backend not available"
)
def test_key_from_ecdsa():
    key = ecdsa.SigningKey.from_pem(private_key)
    assert not ECKey(key, ALGORITHMS.ES256).is_public()


@pytest.mark.cryptography
@pytest.mark.skipif(CryptographyECKey is None, reason="pyca/cryptography backend not available")
def test_cryptograhy_der_to_asn1():
    key = CryptographyECKey(private_key, ALGORITHMS.ES256)
    assert key._der_to_asn1(DER_SIGNATURE) == ASN1_SIGNATURE


@pytest.mark.cryptography
@pytest.mark.skipif(CryptographyECKey is None, reason="pyca/cryptography backend not available")
def test_cryptograhy_asn1_to_der():
    key = CryptographyECKey(private_key, ALGORITHMS.ES256)
    assert key._asn1_to_der(ASN1_SIGNATURE) == DER_SIGNATURE


class TestECAlgorithm:

    def test_key_from_pem(self):
        assert not ECKey(private_key, ALGORITHMS.ES256).is_public()

    def test_to_pem(self):
        key = ECKey(private_key, ALGORITHMS.ES256)
        assert not key.is_public()
        assert key.to_pem().strip() == private_key.strip().encode('utf-8')

        public_pem = key.public_key().to_pem()
        assert ECKey(public_pem, ALGORITHMS.ES256).is_public()

{'fraction_non_alphanumeric': 0.11961610486891386, 'fraction_numerical': 0.035580524344569285, 'mean_word_length': 3.801123595505618, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2946048', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1645, 'n_words': 314}