blob: fe10ab997c1c485208b4938f995e278620779745 [file] [log] [blame]
Rick Dean433dc642009-07-07 13:11:55 -05001# Copyright (C) Frederick Dean 2009, All rights reserved
2
3"""
4Unit tests for L{OpenSSL.rand}.
5"""
6
7from unittest import main
8import os
9import stat
10
11from OpenSSL.test.util import TestCase
12from OpenSSL import rand
13
14class RandTests(TestCase):
15 def test_bytes(self):
16 """
17 Verify that we can obtain bytes from rand_bytes() and
18 that they are different each time. Test the parameter
19 of rand_bytes() for bad values.
20 """
21 b1 = rand.bytes(50)
22 self.assertEqual(len(b1), 50)
23 b2 = rand.bytes(num_bytes=50) # parameter by name
24 self.assertNotEqual(b1, b2) # Hip, Hip, Horay! FIPS complaince
25 b3 = rand.bytes(num_bytes=0)
26 self.assertEqual(len(b3), 0)
27 try:
28 b4 = rand.bytes(-1)
29 self.assertTrue(False) # We shouldn't get here
30 except ValueError, v:
31 self.assertTrue(v.message == "num_bytes must not be negative")
32
33
34 def test_add(self):
35 """
36 Test adding of entropy to the PRNG.
37 """
38 rand.add('hamburger', 3)
39 rand.seed('milk shake')
40 self.assertTrue(rand.status())
41
42
43 def test_files(self):
44 """
45 Test reading and writing of files via rand functions.
46 """
47 # Write random bytes to a file
48 tmpfile = self.mktemp()
49 rand.write_file(tmpfile)
50 # Verify length of written file
51 size = os.stat(tmpfile)[stat.ST_SIZE]
52 self.assertEquals(size, 1024)
53 # Read random bytes from file
54 rand.load_file(tmpfile)
55 rand.load_file(tmpfile, 4) # specify a length
56 # Cleanup
57 os.unlink(tmpfile)
58
59
60if __name__ == '__main__':
61 main()
62
63