Skip to content

Commit 57f9d69

Browse files
committed
added rsa module tests
1 parent ca66169 commit 57f9d69

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/crypt/test__cryptography_rsa.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import json
1616
import os
1717
import pickle
18+
import warnings
1819

1920
from cryptography.hazmat.primitives.asymmetric import rsa
2021
import pytest # type: ignore
@@ -174,3 +175,37 @@ def test_pickle(self):
174175

175176
assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
176177
assert isinstance(signer._key, rsa.RSAPrivateKey)
178+
179+
180+
@pytest.mark.skipif(rsa is None, reason="rsa library is not installed")
181+
class TestRSATransparency(object):
182+
@classmethod
183+
def setup_class(cls):
184+
import rsa
185+
(cls.pub_key_rsa, cls.priv_key_rsa) = rsa.newkeys(512)
186+
cls.message = b"test message"
187+
188+
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
189+
def test_rsa_key_signing_and_verification(self):
190+
# Test RSASigner with rsa.key.PrivateKey
191+
signer = _cryptography_rsa.RSASigner(self.priv_key_rsa)
192+
signature = signer.sign(self.message)
193+
assert isinstance(signer._key, rsa.RSAPrivateKey)
194+
195+
# Test RSAVerifier with rsa.key.PublicKey
196+
verifier = _cryptography_rsa.RSAVerifier(self.pub_key_rsa)
197+
assert verifier.verify(self.message, signature) is True
198+
assert isinstance(verifier._pubkey, rsa.RSAPublicKey)
199+
200+
def test_rsa_key_warning(self):
201+
# Reset the global flag to ensure the warning is issued
202+
_cryptography_rsa._RSA_DEPRECATION_WARNED = False
203+
with pytest.warns(DeprecationWarning) as record:
204+
_cryptography_rsa.RSASigner(self.priv_key_rsa)
205+
# duplicates should be ignored
206+
_cryptography_rsa.RSAVerifier(self.pub_key_rsa)
207+
_cryptography_rsa.RSASigner(self.priv_key_rsa)
208+
_cryptography_rsa.RSAVerifier(self.pub_key_rsa)
209+
210+
assert len(record) == 1
211+
assert "The 'rsa' library is deprecated" in str(record[0].message)

0 commit comments

Comments
 (0)