[pycrypto] Test code - Random

Dwayne C. Litzenberger dlitz at dlitz.net
Fri Nov 7 15:06:26 CST 2008


On Fri, Nov 07, 2008 at 08:55:10PM +0000, Sam Phippen wrote:
>After reviewing the random number test code I thought some extra functions
>might be useful, attached is said code

Thanks for the patch.  I've reposted it below in unified diff form.

========= BEGIN PATCH =========
diff --git a/SelfTest/Random/test_random.py b/SelfTest/Random/test_random.py
index 3b47e9f..0afb963 100644
--- a/SelfTest/Random/test_random.py
+++ b/SelfTest/Random/test_random.py
@@ -41,9 +41,37 @@ class SimpleTest(unittest.TestCase):
          x = randobj.read(16)
          y = randobj.read(16)
          self.assertNotEqual(x, y)
+class TestNotAlwaysEqual(unittest.TestCase):
+    def runTest(self):
+        from Crypto import Random
+        randobj = Random.new()
+        k = 10
+        DataArray = []
+        x = randobj.read(16)
+        count = 0
+        for i in range(0,k):
+            y = randobj.read(16)
+            if x == y:
+                count += 1
+        self.assertNotEqual(count,k)
+class TestAverage(unittest.TestCase):
+    def runTest(self):
+        from Crypto import Random
+        randobj = Random.new()
+        x = randobj.read(2**15)
+        values = []
+        for i in x:
+            values.append(ord(i))
+        total = 0
+        for i in values:
+            total += i
+        average = total/len(values)
+        print average
+        self.assertEqual(average in range((256/2)-28,(256/2)+28),True)
+
  
  def get_tests():
-    return [SimpleTest()]
+    return [SimpleTest(),TestNotAlwaysEqual(),TestAverage()]
  
  if __name__ == '__main__':
      suite = lambda: unittest.TestSuite(get_tests())
========= END PATCH =========

Also, I'll quote our discussion on IRC, for the record:

> <samphippen> dwon, mind if I write some more tests for the rng?
> <dwon> sure, if you can meet these requirements first: 
> http://www.dlitz.net/software/pycrypto/submission-requirements/
> <samphippen> dwon, I meet all those requirements
> <samphippen> dwon, i'm a uk citizen

This is referring to "PyCrypto Code Submission Requirements - Rev. B", dated 
2008-09-14.  Thanks for that.

-- 
Dwayne C. Litzenberger <dlitz at dlitz.net>
  Key-signing key   - 19E1 1FE8 B3CF F273 ED17  4A24 928C EC13 39C2 5CF7
  Annual key (2008) - 4B2A FD82 FC7D 9E38 38D9  179F 1C11 B877 E780 4B45
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
Url : http://lists.dlitz.net/pipermail/pycrypto/attachments/20081107/15e8860b/attachment.pgp 


More information about the pycrypto mailing list