Skip to content

Commit a8a8de6

Browse files
committed
More cover
1 parent a9aecb6 commit a8a8de6

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/test_models.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
from cert_chain_resolver.models import Cert, CertificateChain
55
from cryptography.x509.oid import ExtensionOID, AuthorityInformationAccessOID, NameOID
66
import pytest
7+
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
8+
from cryptography.hazmat.primitives.asymmetric.ec import ECDSA, EllipticCurvePublicKey
9+
from cryptography.hazmat.primitives import hashes
10+
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
11+
from cryptography.exceptions import InvalidSignature
712

813

914
try:
@@ -22,6 +27,18 @@ def does_not_raise():
2227
unicode = str
2328

2429

30+
@pytest.fixture
31+
def mock_x509(mocker):
32+
return mocker.Mock(spec=x509.Certificate)
33+
34+
35+
@pytest.fixture
36+
def mock_cert(mocker, mock_x509):
37+
cert = mocker.Mock(spec=Cert)
38+
cert._x509 = mock_x509
39+
return cert
40+
41+
2542
@pytest.mark.parametrize("cert", CERT_FIXTURES, ids=certfixture_to_id)
2643
def test_certcontainer_x509_helper_props(cert):
2744
c = Cert(cert["cert_x509"])
@@ -190,3 +207,28 @@ class CertOverride(Cert):
190207
c = CertOverride()
191208

192209
assert repr(c) == '<Cert common_name="CN" subject="Subject" issuer="Issuer">'
210+
211+
212+
@pytest.mark.parametrize(
213+
"key_type,expected",
214+
[
215+
(RSAPublicKey, True),
216+
(RSAPublicKey, False),
217+
(EllipticCurvePublicKey, True),
218+
(EllipticCurvePublicKey, False),
219+
(object, False), # Unexpected key type FIXME: Maybe this should raise??
220+
],
221+
)
222+
def test_is_issued_by_handles_different_keys(
223+
mocker, mock_x509, mock_cert, key_type, expected
224+
):
225+
mock_public_key = mocker.Mock(spec=key_type)
226+
mock_x509.public_key.return_value = mock_public_key
227+
mock_x509.signature_hash_algorithm = mocker.Mock(spec=hashes.SHA256)
228+
229+
subject = Cert(mock_x509)
230+
231+
if not expected and hasattr(key_type, "verify"):
232+
mock_public_key.verify.side_effect = InvalidSignature()
233+
234+
assert subject.is_issued_by(mock_cert) is expected

0 commit comments

Comments
 (0)