summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSybren A. Stüvel <sybren@stuvel.eu>2016-03-29 19:05:23 +0200
committerSybren A. Stüvel <sybren@stuvel.eu>2016-03-29 19:05:23 +0200
commit9a9e08ce6559f888c345935bac7f85f747fc5f06 (patch)
tree31281edd7e70ebb76af29feba9efdf7687852ee6 /tests
parenta5487826a1f28952a560c50f14e284c4292d94ca (diff)
downloadrsa-git-9a9e08ce6559f888c345935bac7f85f747fc5f06.tar.gz
More tests with hard-coded 'random' values.
This reduces noise in the code coverage measurements.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_key.py4
-rw-r--r--tests/test_prime.py32
2 files changed, 35 insertions, 1 deletions
diff --git a/tests/test_key.py b/tests/test_key.py
index 24f6501..833a6aa 100644
--- a/tests/test_key.py
+++ b/tests/test_key.py
@@ -43,7 +43,9 @@ class KeyGenTest(unittest.TestCase):
def test_custom_getprime_func(self):
# List of primes to test with, in order [p, q, p, q, ....]
- primes = [64123, 50957, 39317, 33107]
+ # By starting with two of the same primes, we test that this is
+ # properly rejected.
+ primes = [64123, 64123, 64123, 50957, 39317, 33107]
def getprime(_):
return primes.pop(0)
diff --git a/tests/test_prime.py b/tests/test_prime.py
index a47c3f2..173c991 100644
--- a/tests/test_prime.py
+++ b/tests/test_prime.py
@@ -19,6 +19,7 @@
import unittest
import rsa.prime
+import rsa.randnum
class PrimeTest(unittest.TestCase):
@@ -42,3 +43,34 @@ class PrimeTest(unittest.TestCase):
# Test around the 50th millionth known prime.
self.assertTrue(rsa.prime.is_prime(982451653))
self.assertFalse(rsa.prime.is_prime(982451653 * 961748941))
+
+ def test_miller_rabin_primality_testing(self):
+ """Uses monkeypatching to ensure certain random numbers.
+
+ This allows us to predict/control the code path.
+ """
+
+ randints = []
+
+ def fake_randint(maxvalue):
+ return randints.pop(0)
+
+ orig_randint = rsa.randnum.randint
+ rsa.randnum.randint = fake_randint
+ try:
+ # 'n is composite'
+ randints.append(2630484831) # causes the 'n is composite' case with n=3784949785
+ self.assertEqual(False, rsa.prime.miller_rabin_primality_testing(2787998641, 7))
+ self.assertEqual([], randints)
+
+ # 'Exit inner loop and continue with next witness'
+ randints.extend([
+ 2119139097, # causes 'Exit inner loop and continue with next witness'
+ # the next witnesses for the above case:
+ 3051067715, 3603501762, 3230895846, 3687808132, 3760099986, 4026931494, 3022471881,
+ ])
+ self.assertEqual(True, rsa.prime.miller_rabin_primality_testing(2211417913,
+ len(randints)))
+ self.assertEqual([], randints)
+ finally:
+ rsa.randnum.randint = orig_randint