Skip to content

Commit

Permalink
XSAdd unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ohemorange committed Jan 15, 2025
1 parent 17a3e5b commit 4d6d1e9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
57 changes: 46 additions & 11 deletions tests/json_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import itertools
import sys
import unittest
import warnings
from typing import Any, Dict, Mapping
from unittest import mock

import pytest
import test_util
from cryptography import x509

from josepy import errors, interfaces, util

CERT = test_util.load_comparable_cert("cert.pem")
CSR = test_util.load_comparable_csr("csr.pem")
CERT_CRYPTOGRAPHY = test_util.load_cert_cryptography("cert.pem")
CSR_CRYPTOGRAPHY = test_util.load_csr_cryptography("csr.pem")


class FieldTest(unittest.TestCase):
Expand Down Expand Up @@ -321,30 +325,61 @@ def test_decode_hex16_odd_length(self) -> None:
def test_encode_cert(self) -> None:
from josepy.json_util import encode_cert

assert self.b64_cert == encode_cert(CERT)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
assert self.b64_cert == encode_cert(CERT)

assert self.b64_cert == encode_cert(CERT_CRYPTOGRAPHY)

def test_decode_cert(self) -> None:
from josepy.json_util import decode_cert
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
from josepy.json_util import decode_cert

cert = decode_cert(self.b64_cert)
assert isinstance(cert, util.ComparableX509)
assert cert == CERT
with pytest.raises(errors.DeserializationError):
decode_cert("")

cert = decode_cert(self.b64_cert)
assert isinstance(cert, util.ComparableX509)
assert cert == CERT
def test_decode_cert_cryptography(self) -> None:
from josepy.json_util import decode_cert_cryptography

cert = decode_cert_cryptography(self.b64_cert)
assert isinstance(cert, x509.Certificate)
assert cert == CERT_CRYPTOGRAPHY
with pytest.raises(errors.DeserializationError):
decode_cert("")
decode_cert_cryptography("")

def test_encode_csr(self) -> None:
from josepy.json_util import encode_csr

assert self.b64_csr == encode_csr(CSR)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
assert self.b64_csr == encode_csr(CSR)

assert self.b64_csr == encode_csr(CSR_CRYPTOGRAPHY)

def test_decode_csr(self) -> None:
from josepy.json_util import decode_csr

csr = decode_csr(self.b64_csr)
assert isinstance(csr, util.ComparableX509)
assert csr == CSR
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)

csr = decode_csr(self.b64_csr)
assert isinstance(csr, util.ComparableX509)
assert csr == CSR
with pytest.raises(errors.DeserializationError):
decode_csr("")

def test_decode_csr_cryptography(self) -> None:
from josepy.json_util import decode_csr_cryptography

csr = decode_csr_cryptography(self.b64_csr)
assert isinstance(csr, x509.CertificateSigningRequest)
assert csr == CSR_CRYPTOGRAPHY
with pytest.raises(errors.DeserializationError):
decode_csr("")
decode_csr_cryptography("")


class TypedJSONObjectWithFieldsTest(unittest.TestCase):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
from typing import Any

from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from OpenSSL import crypto
Expand Down Expand Up @@ -57,6 +58,14 @@ def load_cert(*names: str) -> crypto.X509:
return crypto.load_certificate(loader, load_vector(*names))


def load_cert_cryptography(*names: str) -> x509.Certificate:
"""Load certificate using cryptography API."""
loader = _guess_loader(
names[-1], x509.load_pem_x509_certificate, x509.load_der_x509_certificate
)
return loader(load_vector(*names))


def load_comparable_cert(*names: str) -> josepy.util.ComparableX509:
"""Load ComparableX509 cert."""
return ComparableX509(load_cert(*names))
Expand All @@ -68,6 +77,12 @@ def load_csr(*names: str) -> crypto.X509Req:
return crypto.load_certificate_request(loader, load_vector(*names))


def load_csr_cryptography(*names: str) -> x509.CertificateSigningRequest:
"""Load certificate request."""
loader = _guess_loader(names[-1], x509.load_pem_x509_csr, x509.load_der_x509_csr)
return loader(load_vector(*names))


def load_comparable_csr(*names: str) -> josepy.util.ComparableX509:
"""Load ComparableX509 certificate request."""
return ComparableX509(load_csr(*names))
Expand Down

0 comments on commit 4d6d1e9

Please sign in to comment.