summaryrefslogtreecommitdiff
path: root/tests/test_load_save_keys.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_load_save_keys.py')
-rw-r--r--tests/test_load_save_keys.py72
1 files changed, 44 insertions, 28 deletions
diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py
index d881d20..3e90999 100644
--- a/tests/test_load_save_keys.py
+++ b/tests/test_load_save_keys.py
@@ -23,51 +23,67 @@ from unittest import mock
import rsa.key
-B64PRIV_DER = b'MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt'
+B64PRIV_DER = b"MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt"
PRIVATE_DER = base64.standard_b64decode(B64PRIV_DER)
-B64PUB_DER = b'MAwCBQDeKYlRAgMBAAE='
+B64PUB_DER = b"MAwCBQDeKYlRAgMBAAE="
PUBLIC_DER = base64.standard_b64decode(B64PUB_DER)
-PRIVATE_PEM = b'''\
+PRIVATE_PEM = (
+ b"""\
-----BEGIN CONFUSING STUFF-----
Cruft before the key
-----BEGIN RSA PRIVATE KEY-----
Comment: something blah
-''' + B64PRIV_DER + b'''
+"""
+ + B64PRIV_DER
+ + b"""
-----END RSA PRIVATE KEY-----
Stuff after the key
-----END CONFUSING STUFF-----
-'''
+"""
+)
-CLEAN_PRIVATE_PEM = b'''\
+CLEAN_PRIVATE_PEM = (
+ b"""\
-----BEGIN RSA PRIVATE KEY-----
-''' + B64PRIV_DER + b'''
+"""
+ + B64PRIV_DER
+ + b"""
-----END RSA PRIVATE KEY-----
-'''
+"""
+)
-PUBLIC_PEM = b'''\
+PUBLIC_PEM = (
+ b"""\
-----BEGIN CONFUSING STUFF-----
Cruft before the key
-----BEGIN RSA PUBLIC KEY-----
Comment: something blah
-''' + B64PUB_DER + b'''
+"""
+ + B64PUB_DER
+ + b"""
-----END RSA PUBLIC KEY-----
Stuff after the key
-----END CONFUSING STUFF-----
-'''
+"""
+)
-CLEAN_PUBLIC_PEM = b'''\
+CLEAN_PUBLIC_PEM = (
+ b"""\
-----BEGIN RSA PUBLIC KEY-----
-''' + B64PUB_DER + b'''
+"""
+ + B64PUB_DER
+ + b"""
-----END RSA PUBLIC KEY-----
-'''
+"""
+)
class DerTest(unittest.TestCase):
@@ -76,7 +92,7 @@ class DerTest(unittest.TestCase):
def test_load_private_key(self):
"""Test loading private DER keys."""
- key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, 'DER')
+ key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, "DER")
expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287)
self.assertEqual(expected, key)
@@ -84,7 +100,7 @@ class DerTest(unittest.TestCase):
self.assertEqual(key.exp2, 10095)
self.assertEqual(key.coef, 50797)
- @mock.patch('pyasn1.codec.der.decoder.decode')
+ @mock.patch("pyasn1.codec.der.decoder.decode")
def test_load_malformed_private_key(self, der_decode):
"""Test loading malformed private DER keys."""
@@ -96,18 +112,18 @@ class DerTest(unittest.TestCase):
with warnings.catch_warnings(record=True) as w:
# Always print warnings
- warnings.simplefilter('always')
+ warnings.simplefilter("always")
# Load 3 keys
for _ in range(3):
- key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, 'DER')
+ key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, "DER")
# Check that 3 warnings were generated.
self.assertEqual(3, len(w))
for warning in w:
self.assertTrue(issubclass(warning.category, UserWarning))
- self.assertIn('malformed', str(warning.message))
+ self.assertIn("malformed", str(warning.message))
# Check that we are creating the key with correct values
self.assertEqual(key.exp1, 55063)
@@ -118,7 +134,7 @@ class DerTest(unittest.TestCase):
"""Test saving private DER keys."""
key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287)
- der = key.save_pkcs1('DER')
+ der = key.save_pkcs1("DER")
self.assertIsInstance(der, bytes)
self.assertEqual(PRIVATE_DER, der)
@@ -126,7 +142,7 @@ class DerTest(unittest.TestCase):
def test_load_public_key(self):
"""Test loading public DER keys."""
- key = rsa.key.PublicKey.load_pkcs1(PUBLIC_DER, 'DER')
+ key = rsa.key.PublicKey.load_pkcs1(PUBLIC_DER, "DER")
expected = rsa.key.PublicKey(3727264081, 65537)
self.assertEqual(expected, key)
@@ -135,7 +151,7 @@ class DerTest(unittest.TestCase):
"""Test saving public DER keys."""
key = rsa.key.PublicKey(3727264081, 65537)
- der = key.save_pkcs1('DER')
+ der = key.save_pkcs1("DER")
self.assertIsInstance(der, bytes)
self.assertEqual(PUBLIC_DER, der)
@@ -147,7 +163,7 @@ class PemTest(unittest.TestCase):
def test_load_private_key(self):
"""Test loading private PEM files."""
- key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_PEM, 'PEM')
+ key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_PEM, "PEM")
expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287)
self.assertEqual(expected, key)
@@ -159,7 +175,7 @@ class PemTest(unittest.TestCase):
"""Test saving private PEM files."""
key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287)
- pem = key.save_pkcs1('PEM')
+ pem = key.save_pkcs1("PEM")
self.assertIsInstance(pem, bytes)
self.assertEqual(CLEAN_PRIVATE_PEM, pem)
@@ -167,7 +183,7 @@ class PemTest(unittest.TestCase):
def test_load_public_key(self):
"""Test loading public PEM files."""
- key = rsa.key.PublicKey.load_pkcs1(PUBLIC_PEM, 'PEM')
+ key = rsa.key.PublicKey.load_pkcs1(PUBLIC_PEM, "PEM")
expected = rsa.key.PublicKey(3727264081, 65537)
self.assertEqual(expected, key)
@@ -176,7 +192,7 @@ class PemTest(unittest.TestCase):
"""Test saving public PEM files."""
key = rsa.key.PublicKey(3727264081, 65537)
- pem = key.save_pkcs1('PEM')
+ pem = key.save_pkcs1("PEM")
self.assertIsInstance(pem, bytes)
self.assertEqual(CLEAN_PUBLIC_PEM, pem)
@@ -184,8 +200,8 @@ class PemTest(unittest.TestCase):
def test_load_from_disk(self):
"""Test loading a PEM file from disk."""
- fname = os.path.join(os.path.dirname(__file__), 'private.pem')
- with open(fname, mode='rb') as privatefile:
+ fname = os.path.join(os.path.dirname(__file__), "private.pem")
+ with open(fname, mode="rb") as privatefile:
keydata = privatefile.read()
privkey = rsa.key.PrivateKey.load_pkcs1(keydata)