summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Crypto/Random/random.py6
-rw-r--r--lib/Crypto/SelfTest/Cipher/test_DES.py6
-rw-r--r--lib/Crypto/SelfTest/Random/test_random.py114
-rw-r--r--python-3-changes.txt2
4 files changed, 122 insertions, 6 deletions
diff --git a/lib/Crypto/Random/random.py b/lib/Crypto/Random/random.py
index 28a99ea..bc08889 100644
--- a/lib/Crypto/Random/random.py
+++ b/lib/Crypto/Random/random.py
@@ -109,9 +109,9 @@ class StrongRandom(object):
# Choose a random item (without replacement) until all the items have been
# chosen.
for i in xrange(len(x)):
- p = self.randint(len(items))
- x[i] = items[p]
- del items[p]
+ p = self.randint(1,len(items))
+ x[i] = items[p-1]
+ del items[p-1]
def sample(self, population, k):
"""Return a k-length list of unique elements chosen from the population sequence."""
diff --git a/lib/Crypto/SelfTest/Cipher/test_DES.py b/lib/Crypto/SelfTest/Cipher/test_DES.py
index 416af43..406a540 100644
--- a/lib/Crypto/SelfTest/Cipher/test_DES.py
+++ b/lib/Crypto/SelfTest/Cipher/test_DES.py
@@ -287,8 +287,8 @@ test_data = [
'NIST SP800-17 B.2 #55'),
]
-class RonRivetTest(unittest.TestCase):
- """ Ronald L. Rivet's DES test, see
+class RonRivestTest(unittest.TestCase):
+ """ Ronald L. Rivest's DES test, see
http://people.csail.mit.edu/rivest/Destest.txt
ABSTRACT
--------
@@ -329,7 +329,7 @@ class RonRivetTest(unittest.TestCase):
def get_tests(config={}):
from Crypto.Cipher import DES
from common import make_block_tests
- return make_block_tests(DES, "DES", test_data) + [RonRivetTest()]
+ return make_block_tests(DES, "DES", test_data) + [RonRivestTest()]
if __name__ == '__main__':
import unittest
diff --git a/lib/Crypto/SelfTest/Random/test_random.py b/lib/Crypto/SelfTest/Random/test_random.py
index 39aaf42..3ae2142 100644
--- a/lib/Crypto/SelfTest/Random/test_random.py
+++ b/lib/Crypto/SelfTest/Random/test_random.py
@@ -27,6 +27,10 @@
__revision__ = "$Id$"
import unittest
+import sys
+if sys.version_info[0] == 2 and sys.version_info[1] == 1:
+ from Crypto.Util.py21compat import *
+from Crypto.Util.py3compat import *
class SimpleTest(unittest.TestCase):
def runTest(self):
@@ -40,6 +44,116 @@ class SimpleTest(unittest.TestCase):
z = Random.get_random_bytes(16)
self.assertNotEqual(x, z)
self.assertNotEqual(y, z)
+ # Test the Random.random module, which
+ # implements a subset of Python's random API
+ # Not implemented:
+ # seed(), getstate(), setstate(), jumpahead()
+ # random(), uniform(), triangular(), betavariate()
+ # expovariate(), gammavariate(), gauss(),
+ # longnormvariate(), normalvariate(),
+ # vonmisesvariate(), paretovariate()
+ # weibullvariate()
+ # WichmannHill(), whseed(), SystemRandom()
+ from Crypto.Random import random
+ x = random.getrandbits(16*8)
+ y = random.getrandbits(16*8)
+ self.assertNotEqual(x, y)
+ # Test randrange
+ if x>y:
+ start = y
+ stop = x
+ else:
+ start = x
+ stop = y
+ for step in range(1,10):
+ x = random.randrange(start,stop,step)
+ y = random.randrange(start,stop,step)
+ self.assertNotEqual(x, y)
+ self.assertEqual(start <= x <= stop, True)
+ self.assertEqual(start <= y <= stop, True)
+ for i in range(10):
+ self.assertEqual(random.randrange(1,2), 1)
+ self.assertRaises(ValueError, random.randrange, stop, start, step)
+ self.assertRaises(TypeError, random.randrange, start, stop, step, step)
+ self.assertRaises(TypeError, random.randrange, start, stop, "1")
+ self.assertRaises(TypeError, random.randrange, "1", stop, step)
+ self.assertRaises(TypeError, random.randrange, 1, "2", step)
+ self.assertRaises(ValueError, random.randrange, start, stop, 0)
+ # Test randint
+ x = random.randint(start,stop)
+ y = random.randint(start,stop)
+ self.assertNotEqual(x, y)
+ self.assertEqual(start <= x <= stop, True)
+ self.assertEqual(start <= y <= stop, True)
+ for i in range(10):
+ self.assertEqual(random.randint(1,1), 1)
+ self.assertRaises(ValueError, random.randint, stop, start)
+ self.assertRaises(TypeError, random.randint, start, stop, step)
+ self.assertRaises(TypeError, random.randint, "1", stop)
+ self.assertRaises(TypeError, random.randint, 1, "2")
+ # Test choice
+ seq = []
+ for i in range(500): # seed the sequence
+ seq[i:] = [random.getrandbits(32)]
+ x = random.choice(seq)
+ y = random.choice(seq)
+ self.assertNotEqual(x, y)
+ self.assertEqual(x in seq, True)
+ self.assertEqual(y in seq, True)
+ for i in range(10):
+ self.assertEqual(random.choice((1,2,3)) in (1,2,3), True)
+ self.assertEqual(random.choice(range(3)) in range(3), True)
+ if sys.version_info[0] is 3:
+ self.assertEqual(random.choice(bytearray(b('123'))) in bytearray(b('123')), True)
+ self.assertEqual(1, random.choice([1]))
+ self.assertRaises(IndexError, random.choice, [])
+ self.assertRaises(TypeError, random.choice, 1)
+ # Test shuffle. Lacks random parameter to specify function.
+ # Make copies of seq
+ x = list(seq)
+ y = list(seq)
+ random.shuffle(x)
+ random.shuffle(y)
+ self.assertNotEqual(x, y)
+ for i in range(len(seq)):
+ self.assertEqual(x[i] in seq, True)
+ self.assertEqual(y[i] in seq, True)
+ z = [1]
+ random.shuffle(z)
+ self.assertEqual(z, [1])
+ if sys.version_info[0] == 3:
+ z = bytearray(b('12'))
+ random.shuffle(z)
+ self.assertEqual(b('1') in z, True)
+ self.assertRaises(TypeError, random.shuffle, b('12'))
+ self.assertRaises(TypeError, random.shuffle, 1)
+ self.assertRaises(TypeError, random.shuffle, "1")
+ self.assertRaises(TypeError, random.shuffle, (1,2))
+ # 2to3 wraps a list() around it, alas - but I want to shoot
+ # myself in the foot here! :D
+ # if sys.version_info[0] == 3:
+ # self.assertRaises(TypeError, random.shuffle, range(3))
+ # Test sample
+ x = random.sample(seq, 20)
+ y = random.sample(seq, 20)
+ self.assertNotEqual(x, y)
+ for i in range(20):
+ self.assertEqual(x[i] in seq, True)
+ self.assertEqual(y[i] in seq, True)
+ z = random.sample([1], 1)
+ self.assertEqual(z, [1])
+ z = random.sample((1,2,3), 1)
+ self.assertEqual(z[0] in (1,2,3), True)
+ z = random.sample("123", 1)
+ self.assertEqual(z[0] in "123", True)
+ z = random.sample(range(3), 1)
+ self.assertEqual(z[0] in range(3), True)
+ if sys.version_info[0] == 3:
+ z = random.sample(b("123"), 1)
+ self.assertEqual(z[0] in b("123"), True)
+ z = random.sample(bytearray(b("123")), 1)
+ self.assertEqual(z[0] in bytearray(b("123")), True)
+ self.assertRaises(TypeError, random.sample, 1)
def get_tests(config={}):
return [SimpleTest()]
diff --git a/python-3-changes.txt b/python-3-changes.txt
index bd524aa..687396f 100644
--- a/python-3-changes.txt
+++ b/python-3-changes.txt
@@ -57,6 +57,8 @@ Do not use assert_/failUnless or failIf. These are deprecated and are scheduled
Use instead assertEqual(expr,True) for assert_ and assertEqual(expr,False) for
failIf
+Added unit tests for Crypto.Random.random. Fixed random.shuffle().
+
C code:
Extended "pycrypto_compat.h". It handles #define's for Python 3.x forward