summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMichael Manganiello <adamantike@users.noreply.github.com>2017-01-05 09:51:12 -0300
committerSybren A. Stüvel <sybren@stuvel.eu>2017-01-05 13:51:12 +0100
commit0c906333048f1760d27e579ce5fd7a140ee2b62c (patch)
treef15f81e1c64d53368e7e85f4a281817f716f9fc4 /tests
parentdc57888fe2ead24f111aee4c38427a8f411a5eb6 (diff)
downloadrsa-git-0c906333048f1760d27e579ce5fd7a140ee2b62c.tar.gz
Remove custom PrivateKey exponents/coefficient (#71)
Thanks for the improvements!
Diffstat (limited to 'tests')
-rw-r--r--tests/test_key.py7
-rw-r--r--tests/test_load_save_keys.py41
2 files changed, 47 insertions, 1 deletions
diff --git a/tests/test_key.py b/tests/test_key.py
index afefa3a..9db30ce 100644
--- a/tests/test_key.py
+++ b/tests/test_key.py
@@ -41,6 +41,13 @@ class KeyGenTest(unittest.TestCase):
self.assertEqual(0x10001, priv.e)
self.assertEqual(0x10001, pub.e)
+ def test_exponents_coefficient_calculation(self):
+ pk = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287)
+
+ self.assertEqual(pk.exp1, 55063)
+ self.assertEqual(pk.exp2, 10095)
+ self.assertEqual(pk.coef, 50797)
+
def test_custom_getprime_func(self):
# List of primes to test with, in order [p, q, p, q, ....]
# By starting with two of the same primes, we test that this is
diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py
index ef6584b..55bd5a4 100644
--- a/tests/test_load_save_keys.py
+++ b/tests/test_load_save_keys.py
@@ -17,10 +17,13 @@
"""Unittest for saving and loading keys."""
import base64
-import unittest
+import mock
import os.path
import pickle
+import unittest
+import warnings
+from rsa._compat import range
import rsa.key
B64PRIV_DER = b'MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt'
@@ -80,6 +83,39 @@ class DerTest(unittest.TestCase):
expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287)
self.assertEqual(expected, key)
+ self.assertEqual(key.exp1, 55063)
+ self.assertEqual(key.exp2, 10095)
+ self.assertEqual(key.coef, 50797)
+
+ @mock.patch('pyasn1.codec.der.decoder.decode')
+ def test_load_malformed_private_key(self, der_decode):
+ """Test loading malformed private DER keys."""
+
+ # Decode returns an invalid exp2 value.
+ der_decode.return_value = (
+ [0, 3727264081, 65537, 3349121513, 65063, 57287, 55063, 0, 50797],
+ 0,
+ )
+
+ with warnings.catch_warnings(record=True) as w:
+ # Always print warnings
+ warnings.simplefilter('always')
+
+ # Load 3 keys
+ for _ in range(3):
+ 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))
+
+ # Check that we are creating the key with correct values
+ self.assertEqual(key.exp1, 55063)
+ self.assertEqual(key.exp2, 10095)
+ self.assertEqual(key.coef, 50797)
def test_save_private_key(self):
"""Test saving private DER keys."""
@@ -118,6 +154,9 @@ class PemTest(unittest.TestCase):
expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287)
self.assertEqual(expected, key)
+ self.assertEqual(key.exp1, 55063)
+ self.assertEqual(key.exp2, 10095)
+ self.assertEqual(key.coef, 50797)
def test_save_private_key(self):
"""Test saving private PEM files."""