Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_randbytes.py 3.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases for L{twisted.python.randbytes}.
  5. """
  6. from twisted.python import randbytes
  7. from twisted.trial import unittest
  8. class SecureRandomTestCaseBase:
  9. """
  10. Base class for secureRandom test cases.
  11. """
  12. def _check(self, source):
  13. """
  14. The given random bytes source should return the number of bytes
  15. requested each time it is called and should probably not return the
  16. same bytes on two consecutive calls (although this is a perfectly
  17. legitimate occurrence and rejecting it may generate a spurious failure
  18. -- maybe we'll get lucky and the heat death with come first).
  19. """
  20. for nbytes in range(17, 25):
  21. s = source(nbytes)
  22. self.assertEqual(len(s), nbytes)
  23. s2 = source(nbytes)
  24. self.assertEqual(len(s2), nbytes)
  25. # This is crude but hey
  26. self.assertNotEqual(s2, s)
  27. class SecureRandomTests(SecureRandomTestCaseBase, unittest.TestCase):
  28. """
  29. Test secureRandom under normal conditions.
  30. """
  31. def test_normal(self):
  32. """
  33. L{randbytes.secureRandom} should return a string of the requested
  34. length and make some effort to make its result otherwise unpredictable.
  35. """
  36. self._check(randbytes.secureRandom)
  37. class ConditionalSecureRandomTests(
  38. SecureRandomTestCaseBase, unittest.SynchronousTestCase
  39. ):
  40. """
  41. Test random sources one by one, then remove it to.
  42. """
  43. def setUp(self):
  44. """
  45. Create a L{randbytes.RandomFactory} to use in the tests.
  46. """
  47. self.factory = randbytes.RandomFactory()
  48. def errorFactory(self, nbytes):
  49. """
  50. A factory raising an error when a source is not available.
  51. """
  52. raise randbytes.SourceNotAvailable()
  53. def test_osUrandom(self):
  54. """
  55. L{RandomFactory._osUrandom} should work as a random source whenever
  56. L{os.urandom} is available.
  57. """
  58. self._check(self.factory._osUrandom)
  59. def test_withoutAnything(self):
  60. """
  61. Remove all secure sources and assert it raises a failure. Then try the
  62. fallback parameter.
  63. """
  64. self.factory._osUrandom = self.errorFactory
  65. self.assertRaises(
  66. randbytes.SecureRandomNotAvailable, self.factory.secureRandom, 18
  67. )
  68. def wrapper():
  69. return self.factory.secureRandom(18, fallback=True)
  70. s = self.assertWarns(
  71. RuntimeWarning,
  72. "urandom unavailable - "
  73. "proceeding with non-cryptographically secure random source",
  74. __file__,
  75. wrapper,
  76. )
  77. self.assertEqual(len(s), 18)
  78. class RandomBaseTests(SecureRandomTestCaseBase, unittest.SynchronousTestCase):
  79. """
  80. 'Normal' random test cases.
  81. """
  82. def test_normal(self):
  83. """
  84. Test basic case.
  85. """
  86. self._check(randbytes.insecureRandom)
  87. def test_withoutGetrandbits(self):
  88. """
  89. Test C{insecureRandom} without C{random.getrandbits}.
  90. """
  91. factory = randbytes.RandomFactory()
  92. factory.getrandbits = None
  93. self._check(factory.insecureRandom)