PageRenderTime 393ms CodeModel.GetById 33ms RepoModel.GetById 2ms app.codeStats 2ms

/Windows/Python3.8/WPy64-3830/WPy64-3830/python-3.8.3.amd64/Lib/site-packages/Crypto/SelfTest/Random/test_random.py

https://gitlab.com/abhi1tb/build
Python | 167 lines | 114 code | 6 blank | 47 comment | 12 complexity | 7ba38792c13a6eb07f640237e3751c76 MD5 | raw file
  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. import sys
  26. import unittest
  27. from Crypto.Util.py3compat import b
  28. class SimpleTest(unittest.TestCase):
  29. def runTest(self):
  30. """Crypto.Random.new()"""
  31. # Import the Random module and try to use it
  32. from Crypto import Random
  33. randobj = Random.new()
  34. x = randobj.read(16)
  35. y = randobj.read(16)
  36. self.assertNotEqual(x, y)
  37. z = Random.get_random_bytes(16)
  38. self.assertNotEqual(x, z)
  39. self.assertNotEqual(y, z)
  40. # Test the Random.random module, which
  41. # implements a subset of Python's random API
  42. # Not implemented:
  43. # seed(), getstate(), setstate(), jumpahead()
  44. # random(), uniform(), triangular(), betavariate()
  45. # expovariate(), gammavariate(), gauss(),
  46. # longnormvariate(), normalvariate(),
  47. # vonmisesvariate(), paretovariate()
  48. # weibullvariate()
  49. # WichmannHill(), whseed(), SystemRandom()
  50. from Crypto.Random import random
  51. x = random.getrandbits(16*8)
  52. y = random.getrandbits(16*8)
  53. self.assertNotEqual(x, y)
  54. # Test randrange
  55. if x>y:
  56. start = y
  57. stop = x
  58. else:
  59. start = x
  60. stop = y
  61. for step in range(1,10):
  62. x = random.randrange(start,stop,step)
  63. y = random.randrange(start,stop,step)
  64. self.assertNotEqual(x, y)
  65. self.assertEqual(start <= x < stop, True)
  66. self.assertEqual(start <= y < stop, True)
  67. self.assertEqual((x - start) % step, 0)
  68. self.assertEqual((y - start) % step, 0)
  69. for i in range(10):
  70. self.assertEqual(random.randrange(1,2), 1)
  71. self.assertRaises(ValueError, random.randrange, start, start)
  72. self.assertRaises(ValueError, random.randrange, stop, start, step)
  73. self.assertRaises(TypeError, random.randrange, start, stop, step, step)
  74. self.assertRaises(TypeError, random.randrange, start, stop, "1")
  75. self.assertRaises(TypeError, random.randrange, "1", stop, step)
  76. self.assertRaises(TypeError, random.randrange, 1, "2", step)
  77. self.assertRaises(ValueError, random.randrange, start, stop, 0)
  78. # Test randint
  79. x = random.randint(start,stop)
  80. y = random.randint(start,stop)
  81. self.assertNotEqual(x, y)
  82. self.assertEqual(start <= x <= stop, True)
  83. self.assertEqual(start <= y <= stop, True)
  84. for i in range(10):
  85. self.assertEqual(random.randint(1,1), 1)
  86. self.assertRaises(ValueError, random.randint, stop, start)
  87. self.assertRaises(TypeError, random.randint, start, stop, step)
  88. self.assertRaises(TypeError, random.randint, "1", stop)
  89. self.assertRaises(TypeError, random.randint, 1, "2")
  90. # Test choice
  91. seq = range(10000)
  92. x = random.choice(seq)
  93. y = random.choice(seq)
  94. self.assertNotEqual(x, y)
  95. self.assertEqual(x in seq, True)
  96. self.assertEqual(y in seq, True)
  97. for i in range(10):
  98. self.assertEqual(random.choice((1,2,3)) in (1,2,3), True)
  99. self.assertEqual(random.choice([1,2,3]) in [1,2,3], True)
  100. if sys.version_info[0] == 3:
  101. self.assertEqual(random.choice(bytearray(b('123'))) in bytearray(b('123')), True)
  102. self.assertEqual(1, random.choice([1]))
  103. self.assertRaises(IndexError, random.choice, [])
  104. self.assertRaises(TypeError, random.choice, 1)
  105. # Test shuffle. Lacks random parameter to specify function.
  106. # Make copies of seq
  107. seq = range(500)
  108. x = list(seq)
  109. y = list(seq)
  110. random.shuffle(x)
  111. random.shuffle(y)
  112. self.assertNotEqual(x, y)
  113. self.assertEqual(len(seq), len(x))
  114. self.assertEqual(len(seq), len(y))
  115. for i in range(len(seq)):
  116. self.assertEqual(x[i] in seq, True)
  117. self.assertEqual(y[i] in seq, True)
  118. self.assertEqual(seq[i] in x, True)
  119. self.assertEqual(seq[i] in y, True)
  120. z = [1]
  121. random.shuffle(z)
  122. self.assertEqual(z, [1])
  123. if sys.version_info[0] == 3:
  124. z = bytearray(b('12'))
  125. random.shuffle(z)
  126. self.assertEqual(b('1') in z, True)
  127. self.assertRaises(TypeError, random.shuffle, b('12'))
  128. self.assertRaises(TypeError, random.shuffle, 1)
  129. self.assertRaises(TypeError, random.shuffle, "11")
  130. self.assertRaises(TypeError, random.shuffle, (1,2))
  131. # 2to3 wraps a list() around it, alas - but I want to shoot
  132. # myself in the foot here! :D
  133. # if sys.version_info[0] == 3:
  134. # self.assertRaises(TypeError, random.shuffle, range(3))
  135. # Test sample
  136. x = random.sample(seq, 20)
  137. y = random.sample(seq, 20)
  138. self.assertNotEqual(x, y)
  139. for i in range(20):
  140. self.assertEqual(x[i] in seq, True)
  141. self.assertEqual(y[i] in seq, True)
  142. z = random.sample([1], 1)
  143. self.assertEqual(z, [1])
  144. z = random.sample((1,2,3), 1)
  145. self.assertEqual(z[0] in (1,2,3), True)
  146. z = random.sample("123", 1)
  147. self.assertEqual(z[0] in "123", True)
  148. z = random.sample(range(3), 1)
  149. self.assertEqual(z[0] in range(3), True)
  150. if sys.version_info[0] == 3:
  151. z = random.sample(b("123"), 1)
  152. self.assertEqual(z[0] in b("123"), True)
  153. z = random.sample(bytearray(b("123")), 1)
  154. self.assertEqual(z[0] in bytearray(b("123")), True)
  155. self.assertRaises(TypeError, random.sample, 1)
  156. def get_tests(config={}):
  157. return [SimpleTest()]
  158. if __name__ == '__main__':
  159. suite = lambda: unittest.TestSuite(get_tests())
  160. unittest.main(defaultTest='suite')
  161. # vim:set ts=4 sw=4 sts=4 expandtab: