blob: ecc5b978b2f8918abeed7883fc2081dd14349f96 [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
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040014
Rick Dean433dc642009-07-07 13:11:55 -050015class RandTests(TestCase):
16 def test_bytes(self):
17 """
18 Verify that we can obtain bytes from rand_bytes() and
19 that they are different each time. Test the parameter
20 of rand_bytes() for bad values.
21 """
22 b1 = rand.bytes(50)
23 self.assertEqual(len(b1), 50)
24 b2 = rand.bytes(num_bytes=50) # parameter by name
25 self.assertNotEqual(b1, b2) # Hip, Hip, Horay! FIPS complaince
26 b3 = rand.bytes(num_bytes=0)
27 self.assertEqual(len(b3), 0)
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040028 exc = self.assertRaises(ValueError, rand.bytes, -1)
Jean-Paul Calderoned2ead822009-07-16 19:03:30 -040029 self.assertEqual(str(exc), "num_bytes must not be negative")
Rick Dean433dc642009-07-07 13:11:55 -050030
31
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040032 def test_add_wrong_args(self):
33 """
34 When called with the wrong number of arguments, or with arguments not of
35 type C{str} and C{int}, L{OpenSSL.rand.add} raises L{TypeError}.
36 """
37 self.assertRaises(TypeError, rand.add)
38 self.assertRaises(TypeError, rand.add, "foo", None)
39 self.assertRaises(TypeError, rand.add, None, 3)
40 self.assertRaises(TypeError, rand.add, "foo", 3, None)
41
42
Rick Dean433dc642009-07-07 13:11:55 -050043 def test_add(self):
44 """
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040045 L{OpenSSL.rand.add} adds entropy to the PRNG.
Rick Dean433dc642009-07-07 13:11:55 -050046 """
47 rand.add('hamburger', 3)
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040048
49
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040050 def test_seed_wrong_args(self):
51 """
52 When called with the wrong number of arguments, or with a non-C{str}
53 argument, L{OpenSSL.rand.seed} raises L{TypeError}.
54 """
55 self.assertRaises(TypeError, rand.seed)
56 self.assertRaises(TypeError, rand.seed, None)
57 self.assertRaises(TypeError, rand.seed, "foo", None)
58
59
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040060 def test_seed(self):
61 """
62 L{OpenSSL.rand.seed} adds entropy to the PRNG.
63 """
Rick Dean433dc642009-07-07 13:11:55 -050064 rand.seed('milk shake')
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040065
66
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040067 def test_status_wrong_args(self):
68 """
69 L{OpenSSL.rand.status} raises L{TypeError} when called with any
70 arguments.
71 """
72 self.assertRaises(TypeError, rand.status, None)
73
74
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040075 def test_status(self):
76 """
77 L{OpenSSL.rand.status} returns C{True} if the PRNG has sufficient
78 entropy, C{False} otherwise.
79 """
80 # It's hard to know what it is actually going to return. Different
81 # OpenSSL random engines decide differently whether they have enough
82 # entropy or not.
83 self.assertTrue(rand.status() in (1, 2))
Rick Dean433dc642009-07-07 13:11:55 -050084
85
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040086 def test_egd_wrong_args(self):
87 """
88 L{OpenSSL.rand.egd} raises L{TypeError} when called with the wrong
89 number of arguments or with arguments not of type C{str} and C{int}.
90 """
91 self.assertRaises(TypeError, rand.egd)
92 self.assertRaises(TypeError, rand.egd, None)
93 self.assertRaises(TypeError, rand.egd, "foo", None)
94 self.assertRaises(TypeError, rand.egd, None, 3)
95 self.assertRaises(TypeError, rand.egd, "foo", 3, None)
96
97
98 def test_egd_missing(self):
99 """
100 L{OpenSSL.rand.egd} returns C{0} if the EGD socket passed to it does not
101 exist.
102 """
103 self.assertEquals(rand.egd(self.mktemp()), 0)
104
105
106 def test_cleanup_wrong_args(self):
107 """
108 L{OpenSSL.rand.cleanup} raises L{TypeError} when called with any
109 arguments.
110 """
111 self.assertRaises(TypeError, rand.cleanup, None)
112
113
114 def test_cleanup(self):
115 """
116 L{OpenSSL.rand.cleanup} releases the memory used by the PRNG and returns
117 C{None}.
118 """
119 self.assertIdentical(rand.cleanup(), None)
120
121
122 def test_load_file_wrong_args(self):
123 """
124 L{OpenSSL.rand.load_file} raises L{TypeError} when called the wrong
125 number of arguments or arguments not of type C{str} and C{int}.
126 """
127 self.assertRaises(TypeError, rand.load_file)
128 self.assertRaises(TypeError, rand.load_file, "foo", None)
129 self.assertRaises(TypeError, rand.load_file, None, 1)
130 self.assertRaises(TypeError, rand.load_file, "foo", 1, None)
131
132
133 def test_write_file_wrong_args(self):
134 """
135 L{OpenSSL.rand.write_file} raises L{TypeError} when called with the
136 wrong number of arguments or a non-C{str} argument.
137 """
138 self.assertRaises(TypeError, rand.write_file)
139 self.assertRaises(TypeError, rand.write_file, None)
140 self.assertRaises(TypeError, rand.write_file, "foo", None)
141
142
Rick Dean433dc642009-07-07 13:11:55 -0500143 def test_files(self):
144 """
145 Test reading and writing of files via rand functions.
146 """
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400147 # Write random bytes to a file
Rick Dean433dc642009-07-07 13:11:55 -0500148 tmpfile = self.mktemp()
Jean-Paul Calderoneb534ff42009-07-16 13:44:56 -0400149 # Make sure it exists (so cleanup definitely succeeds)
150 fObj = file(tmpfile, 'w')
151 fObj.close()
152 try:
153 rand.write_file(tmpfile)
154 # Verify length of written file
155 size = os.stat(tmpfile)[stat.ST_SIZE]
156 self.assertEquals(size, 1024)
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400157 # Read random bytes from file
Jean-Paul Calderoneb534ff42009-07-16 13:44:56 -0400158 rand.load_file(tmpfile)
159 rand.load_file(tmpfile, 4) # specify a length
160 finally:
161 # Cleanup
162 os.unlink(tmpfile)
Rick Dean433dc642009-07-07 13:11:55 -0500163
164
165if __name__ == '__main__':
166 main()