Skip to content

Commit 939950d

Browse files
committed
expand all tests that use an RSA private key to test with both sizes
1 parent 130c383 commit 939950d

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

tests/algorithms/test_RSA.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
if sys.version_info > (3,):
2323
long = int
2424

25-
private_key = b"""-----BEGIN RSA PRIVATE KEY-----
25+
private_key_4096 = b"""-----BEGIN RSA PRIVATE KEY-----
2626
MIIJKwIBAAKCAgEAtSKfSeI0fukRIX38AHlKB1YPpX8PUYN2JdvfM+XjNmLfU1M7
2727
4N0VmdzIX95sneQGO9kC2xMIE+AIlt52Yf/KgBZggAlS9Y0Vx8DsSL2HvOjguAdX
2828
ir3vYLvAyyHin/mUisJOqccFKChHKjnk0uXy/38+1r17/cYTp76brKpU1I4kM20M
@@ -100,8 +100,10 @@
100100
dXI4NN9leDeIpNaGU6ozr+At3f50GtCWxdUppy9FDh5qDamBV8K4/+uNqFPiKFQ9
101101
uwNcJ8daMgVZ0QBrD3CBcSZQrfC484BlV6spJ3C16qDVSQPt7sAI
102102
-----END RSA PRIVATE KEY-----"""
103-
private_key_4096 = private_key
104-
PRIVATE_KEYS = (private_key_2048, private_key_4096)
103+
PRIVATE_KEYS = (
104+
pytest.param(private_key_2048, id="RSA-2048"),
105+
pytest.param(private_key_4096, id="RSA-4096")
106+
)
105107

106108

107109
@pytest.mark.pycrypto
@@ -113,10 +115,12 @@ def test_pycrypto_RSA_key_instance():
113115
long(65537)))
114116
RSAKey(key, ALGORITHMS.RS256)
115117

118+
116119
# TODO: Unclear why this test was marked as only for pycrypto
117120
@pytest.mark.pycrypto
118121
@pytest.mark.pycryptodome
119-
def test_pycrypto_unencoded_cleartext():
122+
@pytest.mark.parametrize("private_key", PRIVATE_KEYS)
123+
def test_pycrypto_unencoded_cleartext(private_key):
120124
key = RSAKey(private_key, ALGORITHMS.RS256)
121125
msg = b'test'
122126
signature = key.sign(msg)
@@ -147,7 +151,7 @@ def test_cryptography_RSA_key_instance():
147151

148152
class TestRSAAlgorithm:
149153
def test_RSA_key(self):
150-
assert not RSAKey(private_key, ALGORITHMS.RS256).is_public()
154+
assert not RSAKey(private_key_4096, ALGORITHMS.RS256).is_public()
151155

152156
def test_string_secret(self):
153157
key = 'secret'
@@ -166,7 +170,7 @@ def test_bad_cert(self,):
166170

167171
def test_invalid_algorithm(self):
168172
with pytest.raises(JWKError):
169-
RSAKey(private_key, ALGORITHMS.ES256)
173+
RSAKey(private_key_4096, ALGORITHMS.ES256)
170174

171175
with pytest.raises(JWKError):
172176
RSAKey({'kty': 'bla'}, ALGORITHMS.RS256)
@@ -208,23 +212,25 @@ def test_RSA_jwk(self):
208212
# None of the extra parameters are present, but 'key' is still private.
209213
assert not RSAKey(key, ALGORITHMS.RS256).is_public()
210214

211-
def test_get_public_key(self):
215+
@pytest.mark.parametrize("private_key", PRIVATE_KEYS)
216+
def test_get_public_key(self, private_key):
212217
key = RSAKey(private_key, ALGORITHMS.RS256)
213218
public_key = key.public_key()
214219
public_key2 = public_key.public_key()
215220
assert public_key.is_public()
216221
assert public_key2.is_public()
217222
assert public_key == public_key2
218223

219-
def test_to_pem(self):
220-
key = RSAKey(private_key, ALGORITHMS.RS256)
221-
assert key.to_pem(pem_format='PKCS1').strip() == private_key.strip()
224+
@pytest.mark.parametrize("pkey", PRIVATE_KEYS)
225+
def test_to_pem(self, pkey):
226+
key = RSAKey(pkey, ALGORITHMS.RS256)
227+
assert key.to_pem(pem_format='PKCS1').strip() == pkey.strip()
222228

223229
pkcs8 = key.to_pem(pem_format='PKCS8').strip()
224-
assert pkcs8 != private_key.strip()
230+
assert pkcs8 != pkey.strip()
225231

226232
newkey = RSAKey(pkcs8, ALGORITHMS.RS256)
227-
assert newkey.to_pem(pem_format='PKCS1').strip() == private_key.strip()
233+
assert newkey.to_pem(pem_format='PKCS1').strip() == pkey.strip()
228234

229235
def assert_parameters(self, as_dict, private):
230236
assert isinstance(as_dict, dict)
@@ -256,7 +262,8 @@ def assert_roundtrip(self, key):
256262
ALGORITHMS.RS256
257263
).to_dict() == key.to_dict()
258264

259-
def test_to_dict(self):
265+
@pytest.mark.parametrize("private_key", PRIVATE_KEYS)
266+
def test_to_dict(self, private_key):
260267
key = RSAKey(private_key, ALGORITHMS.RS256)
261268
self.assert_parameters(key.to_dict(), private=True)
262269
self.assert_parameters(key.public_key().to_dict(), private=False)

tests/algorithms/test_RSA_compat.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
PurePythonRSAKey = CryptographyRSAKey = PyCryptoRSAKey = None
99
from jose.constants import ALGORITHMS
1010

11-
from .test_RSA import private_key
11+
from .test_RSA import PRIVATE_KEYS
1212

1313
CRYPTO_BACKENDS = (
1414
pytest.param(PurePythonRSAKey, id="python_rsa"),
@@ -27,7 +27,8 @@ class TestBackendRsaCompatibility(object):
2727

2828
@pytest.mark.parametrize("BackendSign", CRYPTO_BACKENDS)
2929
@pytest.mark.parametrize("BackendVerify", CRYPTO_BACKENDS)
30-
def test_signing_parity(self, BackendSign, BackendVerify):
30+
@pytest.mark.parametrize("private_key", PRIVATE_KEYS)
31+
def test_signing_parity(self, BackendSign, BackendVerify, private_key):
3132
key_sign = BackendSign(private_key, ALGORITHMS.RS256)
3233
key_verify = BackendVerify(private_key, ALGORITHMS.RS256).public_key()
3334

@@ -43,7 +44,8 @@ def test_signing_parity(self, BackendSign, BackendVerify):
4344
@pytest.mark.parametrize("encoding", ENCODINGS)
4445
@pytest.mark.parametrize("BackendFrom", CRYPTO_BACKENDS)
4546
@pytest.mark.parametrize("BackendTo", CRYPTO_BACKENDS)
46-
def test_public_key_to_pem(self, BackendFrom, BackendTo, encoding):
47+
@pytest.mark.parametrize("private_key", PRIVATE_KEYS)
48+
def test_public_key_to_pem(self, BackendFrom, BackendTo, encoding, private_key):
4749
key = BackendFrom(private_key, ALGORITHMS.RS256)
4850
key2 = BackendTo(private_key, ALGORITHMS.RS256)
4951

@@ -54,7 +56,8 @@ def test_public_key_to_pem(self, BackendFrom, BackendTo, encoding):
5456
@pytest.mark.parametrize("encoding", ENCODINGS)
5557
@pytest.mark.parametrize("BackendFrom", CRYPTO_BACKENDS)
5658
@pytest.mark.parametrize("BackendTo", CRYPTO_BACKENDS)
57-
def test_private_key_to_pem(self, BackendFrom, BackendTo, encoding):
59+
@pytest.mark.parametrize("private_key", PRIVATE_KEYS)
60+
def test_private_key_to_pem(self, BackendFrom, BackendTo, encoding, private_key):
5861
key = BackendFrom(private_key, ALGORITHMS.RS256)
5962
key2 = BackendTo(private_key, ALGORITHMS.RS256)
6063

@@ -72,7 +75,8 @@ def test_private_key_to_pem(self, BackendFrom, BackendTo, encoding):
7275
@pytest.mark.parametrize("encoding_load", ENCODINGS)
7376
@pytest.mark.parametrize("BackendFrom", CRYPTO_BACKENDS)
7477
@pytest.mark.parametrize("BackendTo", CRYPTO_BACKENDS)
75-
def test_public_key_load_cycle(self, BackendFrom, BackendTo, encoding_save, encoding_load):
78+
@pytest.mark.parametrize("private_key", PRIVATE_KEYS)
79+
def test_public_key_load_cycle(self, BackendFrom, BackendTo, encoding_save, encoding_load, private_key):
7680
key = BackendFrom(private_key, ALGORITHMS.RS256)
7781

7882
pem_pub_reference = key.public_key().to_pem(pem_format=encoding_save).strip()
@@ -86,7 +90,8 @@ def test_public_key_load_cycle(self, BackendFrom, BackendTo, encoding_save, enco
8690
@pytest.mark.parametrize("encoding_load", ENCODINGS)
8791
@pytest.mark.parametrize("BackendFrom", CRYPTO_BACKENDS)
8892
@pytest.mark.parametrize("BackendTo", CRYPTO_BACKENDS)
89-
def test_private_key_load_cycle(self, BackendFrom, BackendTo, encoding_save, encoding_load):
93+
@pytest.mark.parametrize("private_key", PRIVATE_KEYS)
94+
def test_private_key_load_cycle(self, BackendFrom, BackendTo, encoding_save, encoding_load, private_key):
9095
key = BackendFrom(private_key, ALGORITHMS.RS256)
9196

9297
pem_reference = key.to_pem(pem_format=encoding_save).strip()

0 commit comments

Comments
 (0)