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_rng.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) typedef int GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. import os
  27. import sys
  28. import unittest
  29. import uuid
  30. import random
  31. from nacl import utils, public
  32. from autobahn import util
  33. @unittest.skipIf(not ('AUTOBAHN_CI_ENABLE_RNG_DEPLETION_TESTS' in os.environ and os.environ['AUTOBAHN_CI_ENABLE_RNG_DEPLETION_TESTS']), 'entropy depletion tests not enabled (env var AUTOBAHN_CI_ENABLE_RNG_DEPLETION_TESTS not set)')
  34. @unittest.skipIf(not sys.platform.startswith('linux'), 'entropy depletion tests only available on Linux')
  35. class TestEntropy(unittest.TestCase):
  36. def test_non_depleting(self):
  37. res = {}
  38. with open('/dev/urandom', 'rb') as rng:
  39. for i in range(1000):
  40. for j in range(100):
  41. # "reseed" (seems pointless, but ..)
  42. random.seed()
  43. # random UUIDs
  44. v1 = uuid.uuid4() # noqa
  45. # stdlib random
  46. v2 = random.random() # noqa
  47. v3 = random.getrandbits(32) # noqa
  48. v4 = random.randint(0, 9007199254740992) # noqa
  49. v5 = random.normalvariate(10, 100) # noqa
  50. v6 = random.choice(range(100)) # noqa
  51. # PyNaCl
  52. v7 = utils.random(public.Box.NONCE_SIZE) # noqa
  53. # Autobahn utils
  54. v8 = util.generate_token(4, 4) # noqa
  55. v9 = util.id() # noqa
  56. v10 = util.rid() # noqa
  57. v11 = util.newid() # noqa
  58. # direct procfs access to PRNG
  59. d = rng.read(1000) # noqa
  60. # check available entropy
  61. with open('/proc/sys/kernel/random/entropy_avail', 'r') as ent:
  62. ea = int(ent.read()) // 100
  63. if ea not in res:
  64. res[ea] = 0
  65. res[ea] += 1
  66. skeys = sorted(res.keys())
  67. print('\nsystem entropy depletion stats:')
  68. for k in skeys:
  69. print('{}: {}'.format(k, res[k]))
  70. self.assertTrue(skeys[0] > 0)
  71. def test_depleting(self):
  72. res = {}
  73. with open('/dev/random', 'rb') as rng:
  74. for i in range(10000):
  75. # direct procfs access to "real" RNG
  76. d = rng.read(1000) # noqa
  77. # check available entropy
  78. with open('/proc/sys/kernel/random/entropy_avail', 'r') as ent:
  79. ea = int(ent.read()) // 100
  80. if ea not in res:
  81. res[ea] = 0
  82. res[ea] += 1
  83. skeys = sorted(res.keys())
  84. print('\nsystem entropy depletion stats:')
  85. for k in skeys:
  86. print('{}: {}'.format(k, res[k]))
  87. self.assertTrue(skeys[0] == 0)