test_random.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Util/test_generic.py: Self-test for the Crypto.Random.new() function
  4. #
  5. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. """Self-test suite for Crypto.Random.new()"""
  25. __revision__ = "$Id$"
  26. import unittest
  27. import sys
  28. if sys.version_info[0] == 2 and sys.version_info[1] == 1:
  29. from Crypto.Util.py21compat import *
  30. from Crypto.Util.py3compat import *
  31. class SimpleTest(unittest.TestCase):
  32. def runTest(self):
  33. """Crypto.Random.new()"""
  34. # Import the Random module and try to use it
  35. from Crypto import Random
  36. randobj = Random.new()
  37. x = randobj.read(16)
  38. y = randobj.read(16)
  39. self.assertNotEqual(x, y)
  40. z = Random.get_random_bytes(16)
  41. self.assertNotEqual(x, z)
  42. self.assertNotEqual(y, z)
  43. # Test the Random.random module, which
  44. # implements a subset of Python's random API
  45. # Not implemented:
  46. # seed(), getstate(), setstate(), jumpahead()
  47. # random(), uniform(), triangular(), betavariate()
  48. # expovariate(), gammavariate(), gauss(),
  49. # longnormvariate(), normalvariate(),
  50. # vonmisesvariate(), paretovariate()
  51. # weibullvariate()
  52. # WichmannHill(), whseed(), SystemRandom()
  53. from Crypto.Random import random
  54. x = random.getrandbits(16*8)
  55. y = random.getrandbits(16*8)
  56. self.assertNotEqual(x, y)
  57. # Test randrange
  58. if x>y:
  59. start = y
  60. stop = x
  61. else:
  62. start = x
  63. stop = y
  64. for step in range(1,10):
  65. x = random.randrange(start,stop,step)
  66. y = random.randrange(start,stop,step)
  67. self.assertNotEqual(x, y)
  68. self.assertEqual(start <= x < stop, True)
  69. self.assertEqual(start <= y < stop, True)
  70. self.assertEqual((x - start) % step, 0)
  71. self.assertEqual((y - start) % step, 0)
  72. for i in range(10):
  73. self.assertEqual(random.randrange(1,2), 1)
  74. self.assertRaises(ValueError, random.randrange, start, start)
  75. self.assertRaises(ValueError, random.randrange, stop, start, step)
  76. self.assertRaises(TypeError, random.randrange, start, stop, step, step)
  77. self.assertRaises(TypeError, random.randrange, start, stop, "1")
  78. self.assertRaises(TypeError, random.randrange, "1", stop, step)
  79. self.assertRaises(TypeError, random.randrange, 1, "2", step)
  80. self.assertRaises(ValueError, random.randrange, start, stop, 0)
  81. # Test randint
  82. x = random.randint(start,stop)
  83. y = random.randint(start,stop)
  84. self.assertNotEqual(x, y)
  85. self.assertEqual(start <= x <= stop, True)
  86. self.assertEqual(start <= y <= stop, True)
  87. for i in range(10):
  88. self.assertEqual(random.randint(1,1), 1)
  89. self.assertRaises(ValueError, random.randint, stop, start)
  90. self.assertRaises(TypeError, random.randint, start, stop, step)
  91. self.assertRaises(TypeError, random.randint, "1", stop)
  92. self.assertRaises(TypeError, random.randint, 1, "2")
  93. # Test choice
  94. seq = range(10000)
  95. x = random.choice(seq)
  96. y = random.choice(seq)
  97. self.assertNotEqual(x, y)
  98. self.assertEqual(x in seq, True)
  99. self.assertEqual(y in seq, True)
  100. for i in range(10):
  101. self.assertEqual(random.choice((1,2,3)) in (1,2,3), True)
  102. self.assertEqual(random.choice([1,2,3]) in [1,2,3], True)
  103. if sys.version_info[0] is 3:
  104. self.assertEqual(random.choice(bytearray(b('123'))) in bytearray(b('123')), True)
  105. self.assertEqual(1, random.choice([1]))
  106. self.assertRaises(IndexError, random.choice, [])
  107. self.assertRaises(TypeError, random.choice, 1)
  108. # Test shuffle. Lacks random parameter to specify function.
  109. # Make copies of seq
  110. seq = range(500)
  111. x = list(seq)
  112. y = list(seq)
  113. random.shuffle(x)
  114. random.shuffle(y)
  115. self.assertNotEqual(x, y)
  116. self.assertEqual(len(seq), len(x))
  117. self.assertEqual(len(seq), len(y))
  118. for i in range(len(seq)):
  119. self.assertEqual(x[i] in seq, True)
  120. self.assertEqual(y[i] in seq, True)
  121. self.assertEqual(seq[i] in x, True)
  122. self.assertEqual(seq[i] in y, True)
  123. z = [1]
  124. random.shuffle(z)
  125. self.assertEqual(z, [1])
  126. if sys.version_info[0] == 3:
  127. z = bytearray(b('12'))
  128. random.shuffle(z)
  129. self.assertEqual(b('1') in z, True)
  130. self.assertRaises(TypeError, random.shuffle, b('12'))
  131. self.assertRaises(TypeError, random.shuffle, 1)
  132. self.assertRaises(TypeError, random.shuffle, "1")
  133. self.assertRaises(TypeError, random.shuffle, (1,2))
  134. # 2to3 wraps a list() around it, alas - but I want to shoot
  135. # myself in the foot here! :D
  136. # if sys.version_info[0] == 3:
  137. # self.assertRaises(TypeError, random.shuffle, range(3))
  138. # Test sample
  139. x = random.sample(seq, 20)
  140. y = random.sample(seq, 20)
  141. self.assertNotEqual(x, y)
  142. for i in range(20):
  143. self.assertEqual(x[i] in seq, True)
  144. self.assertEqual(y[i] in seq, True)
  145. z = random.sample([1], 1)
  146. self.assertEqual(z, [1])
  147. z = random.sample((1,2,3), 1)
  148. self.assertEqual(z[0] in (1,2,3), True)
  149. z = random.sample("123", 1)
  150. self.assertEqual(z[0] in "123", True)
  151. z = random.sample(range(3), 1)
  152. self.assertEqual(z[0] in range(3), True)
  153. if sys.version_info[0] == 3:
  154. z = random.sample(b("123"), 1)
  155. self.assertEqual(z[0] in b("123"), True)
  156. z = random.sample(bytearray(b("123")), 1)
  157. self.assertEqual(z[0] in bytearray(b("123")), True)
  158. self.assertRaises(TypeError, random.sample, 1)
  159. def get_tests(config={}):
  160. return [SimpleTest()]
  161. if __name__ == '__main__':
  162. suite = lambda: unittest.TestSuite(get_tests())
  163. unittest.main(defaultTest='suite')
  164. # vim:set ts=4 sw=4 sts=4 expandtab: