3
"""Unit tests for M2Crypto.Rand.
5
Copyright (C) 2006 Open Source Applications Foundation (OSAF). All Rights Reserved.
10
from M2Crypto import Rand
12
class RandTestCase(unittest.TestCase):
14
self.assertRaises(MemoryError, Rand.rand_bytes, -1)
15
assert Rand.rand_bytes(0) == ''
16
assert len(Rand.rand_bytes(1)) == 1
18
def test_pseudo_bytes(self):
19
self.assertRaises(MemoryError, Rand.rand_pseudo_bytes, -1)
20
assert Rand.rand_pseudo_bytes(0) == ('', 1)
21
a, b = Rand.rand_pseudo_bytes(1)
25
def test_load_save(self):
27
os.remove('tests/randpool.dat')
30
assert Rand.load_file('tests/randpool.dat', -1) == 0
31
assert Rand.save_file('tests/randpool.dat') == 1024
32
assert Rand.load_file('tests/randpool.dat', -1) == 1024
34
def test_seed_add(self):
35
if sys.version_info >= (2, 4):
36
assert Rand.rand_seed(os.urandom(1024)) is None
38
# XXX Should there be limits on the entropy parameter?
39
assert Rand.rand_add(os.urandom(2), 0.5) is None
40
Rand.rand_add(os.urandom(2), -0.5)
41
Rand.rand_add(os.urandom(2), 5000.0)
45
suite = unittest.TestSuite()
46
suite.addTest(unittest.makeSuite(RandTestCase))
50
if __name__ == '__main__':
51
unittest.TextTestRunner().run(suite())