blob: c8645a5d7465eca0036b03b6374278a722d9e55b [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
Jean-Paul Calderonee379d732010-07-30 17:06:48 -04008import os
Rick Dean433dc642009-07-07 13:11:55 -05009import stat
10
11from OpenSSL.test.util import TestCase
12from OpenSSL import rand
13
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040014
Rick Dean433dc642009-07-07 13:11:55 -050015class RandTests(TestCase):
Jean-Paul Calderonee379d732010-07-30 17:06:48 -040016 def test_bytes_wrong_args(self):
17 """
18 L{OpenSSL.rand.bytes} raises L{TypeError} if called with the wrong
19 number of arguments or with a non-C{int} argument.
20 """
21 self.assertRaises(TypeError, rand.bytes)
22 self.assertRaises(TypeError, rand.bytes, None)
23 self.assertRaises(TypeError, rand.bytes, 3, None)
24
Jean-Paul Calderoneb1753662010-07-30 17:40:42 -040025 # XXX Test failure of the malloc() in rand_bytes.
Jean-Paul Calderonee379d732010-07-30 17:06:48 -040026
Rick Dean433dc642009-07-07 13:11:55 -050027 def test_bytes(self):
28 """
29 Verify that we can obtain bytes from rand_bytes() and
30 that they are different each time. Test the parameter
31 of rand_bytes() for bad values.
32 """
33 b1 = rand.bytes(50)
34 self.assertEqual(len(b1), 50)
35 b2 = rand.bytes(num_bytes=50) # parameter by name
36 self.assertNotEqual(b1, b2) # Hip, Hip, Horay! FIPS complaince
Jean-Paul Calderonee379d732010-07-30 17:06:48 -040037 b3 = rand.bytes(num_bytes=0)
Rick Dean433dc642009-07-07 13:11:55 -050038 self.assertEqual(len(b3), 0)
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040039 exc = self.assertRaises(ValueError, rand.bytes, -1)
Jean-Paul Calderoned2ead822009-07-16 19:03:30 -040040 self.assertEqual(str(exc), "num_bytes must not be negative")
Rick Dean433dc642009-07-07 13:11:55 -050041
42
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040043 def test_add_wrong_args(self):
44 """
45 When called with the wrong number of arguments, or with arguments not of
46 type C{str} and C{int}, L{OpenSSL.rand.add} raises L{TypeError}.
47 """
48 self.assertRaises(TypeError, rand.add)
49 self.assertRaises(TypeError, rand.add, "foo", None)
50 self.assertRaises(TypeError, rand.add, None, 3)
51 self.assertRaises(TypeError, rand.add, "foo", 3, None)
52
53
Rick Dean433dc642009-07-07 13:11:55 -050054 def test_add(self):
55 """
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040056 L{OpenSSL.rand.add} adds entropy to the PRNG.
Rick Dean433dc642009-07-07 13:11:55 -050057 """
58 rand.add('hamburger', 3)
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040059
60
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040061 def test_seed_wrong_args(self):
62 """
63 When called with the wrong number of arguments, or with a non-C{str}
64 argument, L{OpenSSL.rand.seed} raises L{TypeError}.
65 """
66 self.assertRaises(TypeError, rand.seed)
67 self.assertRaises(TypeError, rand.seed, None)
68 self.assertRaises(TypeError, rand.seed, "foo", None)
69
70
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040071 def test_seed(self):
72 """
73 L{OpenSSL.rand.seed} adds entropy to the PRNG.
74 """
Rick Dean433dc642009-07-07 13:11:55 -050075 rand.seed('milk shake')
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040076
77
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040078 def test_status_wrong_args(self):
79 """
80 L{OpenSSL.rand.status} raises L{TypeError} when called with any
81 arguments.
82 """
83 self.assertRaises(TypeError, rand.status, None)
84
85
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040086 def test_status(self):
87 """
88 L{OpenSSL.rand.status} returns C{True} if the PRNG has sufficient
89 entropy, C{False} otherwise.
90 """
91 # It's hard to know what it is actually going to return. Different
92 # OpenSSL random engines decide differently whether they have enough
93 # entropy or not.
94 self.assertTrue(rand.status() in (1, 2))
Rick Dean433dc642009-07-07 13:11:55 -050095
96
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040097 def test_egd_wrong_args(self):
98 """
99 L{OpenSSL.rand.egd} raises L{TypeError} when called with the wrong
100 number of arguments or with arguments not of type C{str} and C{int}.
101 """
102 self.assertRaises(TypeError, rand.egd)
103 self.assertRaises(TypeError, rand.egd, None)
104 self.assertRaises(TypeError, rand.egd, "foo", None)
105 self.assertRaises(TypeError, rand.egd, None, 3)
106 self.assertRaises(TypeError, rand.egd, "foo", 3, None)
107
108
109 def test_egd_missing(self):
110 """
111 L{OpenSSL.rand.egd} returns C{0} if the EGD socket passed to it does not
112 exist.
113 """
114 self.assertEquals(rand.egd(self.mktemp()), 0)
115
116
117 def test_cleanup_wrong_args(self):
118 """
119 L{OpenSSL.rand.cleanup} raises L{TypeError} when called with any
120 arguments.
121 """
122 self.assertRaises(TypeError, rand.cleanup, None)
123
124
125 def test_cleanup(self):
126 """
127 L{OpenSSL.rand.cleanup} releases the memory used by the PRNG and returns
128 C{None}.
129 """
130 self.assertIdentical(rand.cleanup(), None)
131
132
133 def test_load_file_wrong_args(self):
134 """
135 L{OpenSSL.rand.load_file} raises L{TypeError} when called the wrong
136 number of arguments or arguments not of type C{str} and C{int}.
137 """
138 self.assertRaises(TypeError, rand.load_file)
139 self.assertRaises(TypeError, rand.load_file, "foo", None)
140 self.assertRaises(TypeError, rand.load_file, None, 1)
141 self.assertRaises(TypeError, rand.load_file, "foo", 1, None)
142
143
144 def test_write_file_wrong_args(self):
145 """
146 L{OpenSSL.rand.write_file} raises L{TypeError} when called with the
147 wrong number of arguments or a non-C{str} argument.
148 """
149 self.assertRaises(TypeError, rand.write_file)
150 self.assertRaises(TypeError, rand.write_file, None)
151 self.assertRaises(TypeError, rand.write_file, "foo", None)
152
153
Rick Dean433dc642009-07-07 13:11:55 -0500154 def test_files(self):
155 """
156 Test reading and writing of files via rand functions.
157 """
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400158 # Write random bytes to a file
Rick Dean433dc642009-07-07 13:11:55 -0500159 tmpfile = self.mktemp()
Jean-Paul Calderoneb534ff42009-07-16 13:44:56 -0400160 # Make sure it exists (so cleanup definitely succeeds)
161 fObj = file(tmpfile, 'w')
162 fObj.close()
163 try:
164 rand.write_file(tmpfile)
165 # Verify length of written file
166 size = os.stat(tmpfile)[stat.ST_SIZE]
167 self.assertEquals(size, 1024)
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400168 # Read random bytes from file
Jean-Paul Calderoneb534ff42009-07-16 13:44:56 -0400169 rand.load_file(tmpfile)
170 rand.load_file(tmpfile, 4) # specify a length
171 finally:
172 # Cleanup
173 os.unlink(tmpfile)
Rick Dean433dc642009-07-07 13:11:55 -0500174
175
176if __name__ == '__main__':
177 main()