Initial import to Tizen
[profile/ivi/python-pyOpenSSL.git] / OpenSSL / test / test_rand.py
1 # Copyright (c) Frederick Dean
2 # See LICENSE for details.
3
4 """
5 Unit tests for L{OpenSSL.rand}.
6 """
7
8 from unittest import main
9 import os
10 import stat
11
12 from OpenSSL.test.util import TestCase, b
13 from OpenSSL import rand
14
15
16 class RandTests(TestCase):
17     def test_bytes_wrong_args(self):
18         """
19         L{OpenSSL.rand.bytes} raises L{TypeError} if called with the wrong
20         number of arguments or with a non-C{int} argument.
21         """
22         self.assertRaises(TypeError, rand.bytes)
23         self.assertRaises(TypeError, rand.bytes, None)
24         self.assertRaises(TypeError, rand.bytes, 3, None)
25
26     # XXX Test failure of the malloc() in rand_bytes.
27
28     def test_bytes(self):
29         """
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.
33         """
34         b1 = rand.bytes(50)
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")
42
43
44     def test_add_wrong_args(self):
45         """
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}.
48         """
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)
53
54
55     def test_add(self):
56         """
57         L{OpenSSL.rand.add} adds entropy to the PRNG.
58         """
59         rand.add(b('hamburger'), 3)
60
61
62     def test_seed_wrong_args(self):
63         """
64         When called with the wrong number of arguments, or with a non-C{str}
65         argument, L{OpenSSL.rand.seed} raises L{TypeError}.
66         """
67         self.assertRaises(TypeError, rand.seed)
68         self.assertRaises(TypeError, rand.seed, None)
69         self.assertRaises(TypeError, rand.seed, b("foo"), None)
70
71
72     def test_seed(self):
73         """
74         L{OpenSSL.rand.seed} adds entropy to the PRNG.
75         """
76         rand.seed(b('milk shake'))
77
78
79     def test_status_wrong_args(self):
80         """
81         L{OpenSSL.rand.status} raises L{TypeError} when called with any
82         arguments.
83         """
84         self.assertRaises(TypeError, rand.status, None)
85
86
87     def test_status(self):
88         """
89         L{OpenSSL.rand.status} returns C{True} if the PRNG has sufficient
90         entropy, C{False} otherwise.
91         """
92         # It's hard to know what it is actually going to return.  Different
93         # OpenSSL random engines decide differently whether they have enough
94         # entropy or not.
95         self.assertTrue(rand.status() in (1, 2))
96
97
98     def test_egd_wrong_args(self):
99         """
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}.
102         """
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)
108
109
110     def test_egd_missing(self):
111         """
112         L{OpenSSL.rand.egd} returns C{0} or C{-1} if the EGD socket passed
113         to it does not exist.
114         """
115         result = rand.egd(self.mktemp())
116         expected = (-1, 0)
117         self.assertTrue(
118             result in expected,
119             "%r not in %r" % (result, expected))
120
121
122     def test_cleanup_wrong_args(self):
123         """
124         L{OpenSSL.rand.cleanup} raises L{TypeError} when called with any
125         arguments.
126         """
127         self.assertRaises(TypeError, rand.cleanup, None)
128
129
130     def test_cleanup(self):
131         """
132         L{OpenSSL.rand.cleanup} releases the memory used by the PRNG and returns
133         C{None}.
134         """
135         self.assertIdentical(rand.cleanup(), None)
136
137
138     def test_load_file_wrong_args(self):
139         """
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}.
142         """
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)
147
148
149     def test_write_file_wrong_args(self):
150         """
151         L{OpenSSL.rand.write_file} raises L{TypeError} when called with the
152         wrong number of arguments or a non-C{str} argument.
153         """
154         self.assertRaises(TypeError, rand.write_file)
155         self.assertRaises(TypeError, rand.write_file, None)
156         self.assertRaises(TypeError, rand.write_file, "foo", None)
157
158
159     def test_files(self):
160         """
161         Test reading and writing of files via rand functions.
162         """
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')
167         fObj.close()
168         try:
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
176         finally:
177             # Cleanup
178             os.unlink(tmpfile)
179
180
181 if __name__ == '__main__':
182     main()