1 # Copyright (c) Frederick Dean
2 # See LICENSE for details.
5 Unit tests for L{OpenSSL.rand}.
8 from unittest import main
12 from OpenSSL.test.util import TestCase, b
13 from OpenSSL import rand
16 class RandTests(TestCase):
17 def test_bytes_wrong_args(self):
19 L{OpenSSL.rand.bytes} raises L{TypeError} if called with the wrong
20 number of arguments or with a non-C{int} argument.
22 self.assertRaises(TypeError, rand.bytes)
23 self.assertRaises(TypeError, rand.bytes, None)
24 self.assertRaises(TypeError, rand.bytes, 3, None)
26 # XXX Test failure of the malloc() in rand_bytes.
30 Verify that we can obtain bytes from rand_bytes() and
31 that they are different each time. Test the parameter
32 of rand_bytes() for bad values.
35 self.assertEqual(len(b1), 50)
36 b2 = rand.bytes(num_bytes=50) # parameter by name
37 self.assertNotEqual(b1, b2) # Hip, Hip, Horay! FIPS complaince
38 b3 = rand.bytes(num_bytes=0)
39 self.assertEqual(len(b3), 0)
40 exc = self.assertRaises(ValueError, rand.bytes, -1)
41 self.assertEqual(str(exc), "num_bytes must not be negative")
44 def test_add_wrong_args(self):
46 When called with the wrong number of arguments, or with arguments not of
47 type C{str} and C{int}, L{OpenSSL.rand.add} raises L{TypeError}.
49 self.assertRaises(TypeError, rand.add)
50 self.assertRaises(TypeError, rand.add, b("foo"), None)
51 self.assertRaises(TypeError, rand.add, None, 3)
52 self.assertRaises(TypeError, rand.add, b("foo"), 3, None)
57 L{OpenSSL.rand.add} adds entropy to the PRNG.
59 rand.add(b('hamburger'), 3)
62 def test_seed_wrong_args(self):
64 When called with the wrong number of arguments, or with a non-C{str}
65 argument, L{OpenSSL.rand.seed} raises L{TypeError}.
67 self.assertRaises(TypeError, rand.seed)
68 self.assertRaises(TypeError, rand.seed, None)
69 self.assertRaises(TypeError, rand.seed, b("foo"), None)
74 L{OpenSSL.rand.seed} adds entropy to the PRNG.
76 rand.seed(b('milk shake'))
79 def test_status_wrong_args(self):
81 L{OpenSSL.rand.status} raises L{TypeError} when called with any
84 self.assertRaises(TypeError, rand.status, None)
87 def test_status(self):
89 L{OpenSSL.rand.status} returns C{True} if the PRNG has sufficient
90 entropy, C{False} otherwise.
92 # It's hard to know what it is actually going to return. Different
93 # OpenSSL random engines decide differently whether they have enough
95 self.assertTrue(rand.status() in (1, 2))
98 def test_egd_wrong_args(self):
100 L{OpenSSL.rand.egd} raises L{TypeError} when called with the wrong
101 number of arguments or with arguments not of type C{str} and C{int}.
103 self.assertRaises(TypeError, rand.egd)
104 self.assertRaises(TypeError, rand.egd, None)
105 self.assertRaises(TypeError, rand.egd, "foo", None)
106 self.assertRaises(TypeError, rand.egd, None, 3)
107 self.assertRaises(TypeError, rand.egd, "foo", 3, None)
110 def test_egd_missing(self):
112 L{OpenSSL.rand.egd} returns C{0} or C{-1} if the EGD socket passed
113 to it does not exist.
115 result = rand.egd(self.mktemp())
119 "%r not in %r" % (result, expected))
122 def test_cleanup_wrong_args(self):
124 L{OpenSSL.rand.cleanup} raises L{TypeError} when called with any
127 self.assertRaises(TypeError, rand.cleanup, None)
130 def test_cleanup(self):
132 L{OpenSSL.rand.cleanup} releases the memory used by the PRNG and returns
135 self.assertIdentical(rand.cleanup(), None)
138 def test_load_file_wrong_args(self):
140 L{OpenSSL.rand.load_file} raises L{TypeError} when called the wrong
141 number of arguments or arguments not of type C{str} and C{int}.
143 self.assertRaises(TypeError, rand.load_file)
144 self.assertRaises(TypeError, rand.load_file, "foo", None)
145 self.assertRaises(TypeError, rand.load_file, None, 1)
146 self.assertRaises(TypeError, rand.load_file, "foo", 1, None)
149 def test_write_file_wrong_args(self):
151 L{OpenSSL.rand.write_file} raises L{TypeError} when called with the
152 wrong number of arguments or a non-C{str} argument.
154 self.assertRaises(TypeError, rand.write_file)
155 self.assertRaises(TypeError, rand.write_file, None)
156 self.assertRaises(TypeError, rand.write_file, "foo", None)
159 def test_files(self):
161 Test reading and writing of files via rand functions.
163 # Write random bytes to a file
164 tmpfile = self.mktemp()
165 # Make sure it exists (so cleanup definitely succeeds)
166 fObj = open(tmpfile, 'w')
169 rand.write_file(tmpfile)
170 # Verify length of written file
171 size = os.stat(tmpfile)[stat.ST_SIZE]
172 self.assertEquals(size, 1024)
173 # Read random bytes from file
174 rand.load_file(tmpfile)
175 rand.load_file(tmpfile, 4) # specify a length
181 if __name__ == '__main__':