blob: 21910d72057d318f9c916fdf5f995a2c21ffc130 [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
25
Rick Dean433dc642009-07-07 13:11:55 -050026 def test_bytes(self):
27 """
28 Verify that we can obtain bytes from rand_bytes() and
29 that they are different each time. Test the parameter
30 of rand_bytes() for bad values.
31 """
32 b1 = rand.bytes(50)
33 self.assertEqual(len(b1), 50)
34 b2 = rand.bytes(num_bytes=50) # parameter by name
35 self.assertNotEqual(b1, b2) # Hip, Hip, Horay! FIPS complaince
Jean-Paul Calderonee379d732010-07-30 17:06:48 -040036 b3 = rand.bytes(num_bytes=0)
Rick Dean433dc642009-07-07 13:11:55 -050037 self.assertEqual(len(b3), 0)
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040038 exc = self.assertRaises(ValueError, rand.bytes, -1)
Jean-Paul Calderoned2ead822009-07-16 19:03:30 -040039 self.assertEqual(str(exc), "num_bytes must not be negative")
Rick Dean433dc642009-07-07 13:11:55 -050040
41
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040042 def test_add_wrong_args(self):
43 """
44 When called with the wrong number of arguments, or with arguments not of
45 type C{str} and C{int}, L{OpenSSL.rand.add} raises L{TypeError}.
46 """
47 self.assertRaises(TypeError, rand.add)
48 self.assertRaises(TypeError, rand.add, "foo", None)
49 self.assertRaises(TypeError, rand.add, None, 3)
50 self.assertRaises(TypeError, rand.add, "foo", 3, None)
51
52
Rick Dean433dc642009-07-07 13:11:55 -050053 def test_add(self):
54 """
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040055 L{OpenSSL.rand.add} adds entropy to the PRNG.
Rick Dean433dc642009-07-07 13:11:55 -050056 """
57 rand.add('hamburger', 3)
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040058
59
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040060 def test_seed_wrong_args(self):
61 """
62 When called with the wrong number of arguments, or with a non-C{str}
63 argument, L{OpenSSL.rand.seed} raises L{TypeError}.
64 """
65 self.assertRaises(TypeError, rand.seed)
66 self.assertRaises(TypeError, rand.seed, None)
67 self.assertRaises(TypeError, rand.seed, "foo", None)
68
69
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040070 def test_seed(self):
71 """
72 L{OpenSSL.rand.seed} adds entropy to the PRNG.
73 """
Rick Dean433dc642009-07-07 13:11:55 -050074 rand.seed('milk shake')
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040075
76
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040077 def test_status_wrong_args(self):
78 """
79 L{OpenSSL.rand.status} raises L{TypeError} when called with any
80 arguments.
81 """
82 self.assertRaises(TypeError, rand.status, None)
83
84
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040085 def test_status(self):
86 """
87 L{OpenSSL.rand.status} returns C{True} if the PRNG has sufficient
88 entropy, C{False} otherwise.
89 """
90 # It's hard to know what it is actually going to return. Different
91 # OpenSSL random engines decide differently whether they have enough
92 # entropy or not.
93 self.assertTrue(rand.status() in (1, 2))
Rick Dean433dc642009-07-07 13:11:55 -050094
95
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040096 def test_egd_wrong_args(self):
97 """
98 L{OpenSSL.rand.egd} raises L{TypeError} when called with the wrong
99 number of arguments or with arguments not of type C{str} and C{int}.
100 """
101 self.assertRaises(TypeError, rand.egd)
102 self.assertRaises(TypeError, rand.egd, None)
103 self.assertRaises(TypeError, rand.egd, "foo", None)
104 self.assertRaises(TypeError, rand.egd, None, 3)
105 self.assertRaises(TypeError, rand.egd, "foo", 3, None)
106
107
108 def test_egd_missing(self):
109 """
110 L{OpenSSL.rand.egd} returns C{0} if the EGD socket passed to it does not
111 exist.
112 """
113 self.assertEquals(rand.egd(self.mktemp()), 0)
114
115
116 def test_cleanup_wrong_args(self):
117 """
118 L{OpenSSL.rand.cleanup} raises L{TypeError} when called with any
119 arguments.
120 """
121 self.assertRaises(TypeError, rand.cleanup, None)
122
123
124 def test_cleanup(self):
125 """
126 L{OpenSSL.rand.cleanup} releases the memory used by the PRNG and returns
127 C{None}.
128 """
129 self.assertIdentical(rand.cleanup(), None)
130
131
132 def test_load_file_wrong_args(self):
133 """
134 L{OpenSSL.rand.load_file} raises L{TypeError} when called the wrong
135 number of arguments or arguments not of type C{str} and C{int}.
136 """
137 self.assertRaises(TypeError, rand.load_file)
138 self.assertRaises(TypeError, rand.load_file, "foo", None)
139 self.assertRaises(TypeError, rand.load_file, None, 1)
140 self.assertRaises(TypeError, rand.load_file, "foo", 1, None)
141
142
143 def test_write_file_wrong_args(self):
144 """
145 L{OpenSSL.rand.write_file} raises L{TypeError} when called with the
146 wrong number of arguments or a non-C{str} argument.
147 """
148 self.assertRaises(TypeError, rand.write_file)
149 self.assertRaises(TypeError, rand.write_file, None)
150 self.assertRaises(TypeError, rand.write_file, "foo", None)
151
152
Rick Dean433dc642009-07-07 13:11:55 -0500153 def test_files(self):
154 """
155 Test reading and writing of files via rand functions.
156 """
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400157 # Write random bytes to a file
Rick Dean433dc642009-07-07 13:11:55 -0500158 tmpfile = self.mktemp()
Jean-Paul Calderoneb534ff42009-07-16 13:44:56 -0400159 # Make sure it exists (so cleanup definitely succeeds)
160 fObj = file(tmpfile, 'w')
161 fObj.close()
162 try:
163 rand.write_file(tmpfile)
164 # Verify length of written file
165 size = os.stat(tmpfile)[stat.ST_SIZE]
166 self.assertEquals(size, 1024)
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400167 # Read random bytes from file
Jean-Paul Calderoneb534ff42009-07-16 13:44:56 -0400168 rand.load_file(tmpfile)
169 rand.load_file(tmpfile, 4) # specify a length
170 finally:
171 # Cleanup
172 os.unlink(tmpfile)
Rick Dean433dc642009-07-07 13:11:55 -0500173
174
175if __name__ == '__main__':
176 main()