Skip to content

Commit dd43565

Browse files
author
Gasper Zejn
committed
Add Python RSAKey to parametrized tests and remote redundant tests.
1 parent 77ed279 commit dd43565

File tree

1 file changed

+13
-104
lines changed

1 file changed

+13
-104
lines changed

tests/algorithms/test_RSA.py

Lines changed: 13 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
class TestRSAAlgorithm:
7373

74-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
74+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
7575
def test_RSA_key(self, Backend):
7676
assert not Backend(private_key, ALGORITHMS.RS256).is_public()
7777

@@ -94,33 +94,33 @@ def test_cryptography_RSA_key_instance(self):
9494
pem = pubkey.to_pem()
9595
assert pem.startswith(b'-----BEGIN PUBLIC KEY-----')
9696

97-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
97+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
9898
def test_string_secret(self, Backend):
9999
key = 'secret'
100100
with pytest.raises(JOSEError):
101101
Backend(key, ALGORITHMS.RS256)
102102

103-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
103+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
104104
def test_object(self, Backend):
105105
key = object()
106106
with pytest.raises(JOSEError):
107107
Backend(key, ALGORITHMS.RS256)
108108

109-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
109+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
110110
def test_bad_cert(self, Backend):
111111
key = '-----BEGIN CERTIFICATE-----'
112112
with pytest.raises(JOSEError):
113113
Backend(key, ALGORITHMS.RS256)
114114

115-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
115+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
116116
def test_invalid_algorithm(self, Backend):
117117
with pytest.raises(JWKError):
118118
Backend(private_key, ALGORITHMS.ES256)
119119

120120
with pytest.raises(JWKError):
121121
Backend({'kty': 'bla'}, ALGORITHMS.RS256)
122122

123-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
123+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
124124
def test_RSA_jwk(self, Backend):
125125
key = {
126126
"kty": "RSA",
@@ -158,13 +158,13 @@ def test_RSA_jwk(self, Backend):
158158
# None of the extra parameters are present, but 'key' is still private.
159159
assert not Backend(key, ALGORITHMS.RS256).is_public()
160160

161-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
161+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
162162
def test_string_secret(self, Backend):
163163
key = 'secret'
164164
with pytest.raises(JOSEError):
165165
Backend(key, ALGORITHMS.RS256)
166166

167-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
167+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
168168
def test_get_public_key(self, Backend):
169169
key = Backend(private_key, ALGORITHMS.RS256)
170170
public_key = key.public_key()
@@ -173,7 +173,7 @@ def test_get_public_key(self, Backend):
173173
assert public_key2.is_public()
174174
assert public_key == public_key2
175175

176-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
176+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
177177
def test_to_pem(self, Backend):
178178
key = Backend(private_key, ALGORITHMS.RS256)
179179
assert key.to_pem().strip() == private_key.strip()
@@ -208,16 +208,16 @@ def assert_roundtrip(self, key, Backend):
208208
ALGORITHMS.RS256
209209
).to_dict() == key.to_dict()
210210

211-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
211+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
212212
def test_to_dict(self, Backend):
213213
key = Backend(private_key, ALGORITHMS.RS256)
214214
self.assert_parameters(key.to_dict(), private=True)
215215
self.assert_parameters(key.public_key().to_dict(), private=False)
216216
self.assert_roundtrip(key, Backend)
217217
self.assert_roundtrip(key.public_key(), Backend)
218218

219-
@pytest.mark.parametrize("BackendSign", [RSAKey, CryptographyRSAKey])
220-
@pytest.mark.parametrize("BackendVerify", [RSAKey, CryptographyRSAKey])
219+
@pytest.mark.parametrize("BackendSign", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
220+
@pytest.mark.parametrize("BackendVerify", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
221221
def test_signing_parity(self, BackendSign, BackendVerify):
222222
key_sign = BackendSign(private_key, ALGORITHMS.RS256)
223223
key_verify = BackendVerify(private_key, ALGORITHMS.RS256).public_key()
@@ -231,7 +231,7 @@ def test_signing_parity(self, BackendSign, BackendVerify):
231231
# invalid signature
232232
assert not key_verify.verify(msg, b'n' * 64)
233233

234-
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey])
234+
@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
235235
def test_pycrypto_unencoded_cleartext(self, Backend):
236236
key = Backend(private_key, ALGORITHMS.RS256)
237237

@@ -243,94 +243,3 @@ def test_pycrypto_unencoded_cleartext(self, Backend):
243243
assert public_key.verify(msg, signature) == True
244244
assert public_key.verify(msg, 1) == False
245245

246-
247-
class TestPythonRSA:
248-
def test_RSA_key(self):
249-
PurePythonRSAKey(private_key, ALGORITHMS.RS256)
250-
251-
def test_RSA_key_instance(self):
252-
import rsa
253-
key = rsa.PublicKey(
254-
e=65537,
255-
n=26057131595212989515105618545799160306093557851986992545257129318694524535510983041068168825614868056510242030438003863929818932202262132630250203397069801217463517914103389095129323580576852108653940669240896817348477800490303630912852266209307160550655497615975529276169196271699168537716821419779900117025818140018436554173242441334827711966499484119233207097432165756707507563413323850255548329534279691658369466534587631102538061857114141268972476680597988266772849780811214198186940677291891818952682545840788356616771009013059992237747149380197028452160324144544057074406611859615973035412993832273216732343819,
256-
)
257-
258-
pubkey = PurePythonRSAKey(key, ALGORITHMS.RS256)
259-
pem = pubkey.to_pem()
260-
assert pem.startswith(b'-----BEGIN PUBLIC KEY-----')
261-
262-
def test_invalid_algorithm(self):
263-
with pytest.raises(JWKError):
264-
PurePythonRSAKey(private_key, ALGORITHMS.ES256)
265-
266-
with pytest.raises(JWKError):
267-
PurePythonRSAKey({'kty': 'bla'}, ALGORITHMS.RS256)
268-
269-
def test_RSA_jwk(self):
270-
d = {
271-
"kty": "RSA",
272-
"n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
273-
"e": "AQAB",
274-
}
275-
PurePythonRSAKey(d, ALGORITHMS.RS256)
276-
277-
def test_string_secret(self):
278-
key = 'secret'
279-
with pytest.raises(JOSEError):
280-
PurePythonRSAKey(key, ALGORITHMS.RS256)
281-
282-
def test_object(self):
283-
key = object()
284-
with pytest.raises(JOSEError):
285-
PurePythonRSAKey(key, ALGORITHMS.RS256)
286-
287-
def test_bad_cert(self):
288-
key = '-----BEGIN CERTIFICATE-----'
289-
with pytest.raises(JOSEError):
290-
PurePythonRSAKey(key, ALGORITHMS.RS256)
291-
292-
def test_get_public_key(self):
293-
key = PurePythonRSAKey(private_key, ALGORITHMS.RS256)
294-
public_key = key.public_key()
295-
public_key2 = public_key.public_key()
296-
assert public_key == public_key2
297-
298-
key = RSAKey(private_key, ALGORITHMS.RS256)
299-
public_key = key.public_key()
300-
public_key2 = public_key.public_key()
301-
assert public_key == public_key2
302-
303-
def test_to_pem(self):
304-
key = PurePythonRSAKey(private_key, ALGORITHMS.RS256)
305-
assert key.to_pem().strip() == private_key.strip()
306-
307-
key = RSAKey(private_key, ALGORITHMS.RS256)
308-
assert key.to_pem().strip() == private_key.strip()
309-
310-
def test_signing_parity(self):
311-
key1 = RSAKey(private_key, ALGORITHMS.RS256)
312-
vkey1 = key1.public_key()
313-
key2 = PurePythonRSAKey(private_key, ALGORITHMS.RS256)
314-
vkey2 = key2.public_key()
315-
316-
msg = b'test'
317-
sig1 = key1.sign(msg)
318-
sig2 = key2.sign(msg)
319-
320-
assert vkey1.verify(msg, sig1)
321-
assert vkey1.verify(msg, sig2)
322-
assert vkey2.verify(msg, sig1)
323-
assert vkey2.verify(msg, sig2)
324-
325-
# invalid signature
326-
assert not vkey2.verify(msg, b'n' * 64)
327-
328-
def test_pycrypto_invalid_signature(self):
329-
330-
key = RSAKey(private_key, ALGORITHMS.RS256)
331-
msg = b'test'
332-
signature = key.sign(msg)
333-
public_key = key.public_key()
334-
335-
assert public_key.verify(msg, signature) == True
336-
assert public_key.verify(msg, 1) == False

0 commit comments

Comments
 (0)