Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 1 | # Copyright (C) Frederick Dean 2009, All rights reserved |
| 2 | |
| 3 | """ |
| 4 | Unit tests for L{OpenSSL.rand}. |
| 5 | """ |
| 6 | |
| 7 | from unittest import main |
Jean-Paul Calderone | e379d73 | 2010-07-30 17:06:48 -0400 | [diff] [blame^] | 8 | import os |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 9 | import stat |
| 10 | |
| 11 | from OpenSSL.test.util import TestCase |
| 12 | from OpenSSL import rand |
| 13 | |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 14 | |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 15 | class RandTests(TestCase): |
Jean-Paul Calderone | e379d73 | 2010-07-30 17:06:48 -0400 | [diff] [blame^] | 16 | 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 Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 26 | 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 Calderone | e379d73 | 2010-07-30 17:06:48 -0400 | [diff] [blame^] | 36 | b3 = rand.bytes(num_bytes=0) |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 37 | self.assertEqual(len(b3), 0) |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 38 | exc = self.assertRaises(ValueError, rand.bytes, -1) |
Jean-Paul Calderone | d2ead82 | 2009-07-16 19:03:30 -0400 | [diff] [blame] | 39 | self.assertEqual(str(exc), "num_bytes must not be negative") |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 40 | |
| 41 | |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 42 | 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 Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 53 | def test_add(self): |
| 54 | """ |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 55 | L{OpenSSL.rand.add} adds entropy to the PRNG. |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 56 | """ |
| 57 | rand.add('hamburger', 3) |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 58 | |
| 59 | |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 60 | 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 Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 70 | def test_seed(self): |
| 71 | """ |
| 72 | L{OpenSSL.rand.seed} adds entropy to the PRNG. |
| 73 | """ |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 74 | rand.seed('milk shake') |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 75 | |
| 76 | |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 77 | 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 Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 85 | 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 Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 94 | |
| 95 | |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 96 | 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 Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 153 | def test_files(self): |
| 154 | """ |
| 155 | Test reading and writing of files via rand functions. |
| 156 | """ |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 157 | # Write random bytes to a file |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 158 | tmpfile = self.mktemp() |
Jean-Paul Calderone | b534ff4 | 2009-07-16 13:44:56 -0400 | [diff] [blame] | 159 | # 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 Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 167 | # Read random bytes from file |
Jean-Paul Calderone | b534ff4 | 2009-07-16 13:44:56 -0400 | [diff] [blame] | 168 | rand.load_file(tmpfile) |
| 169 | rand.load_file(tmpfile, 4) # specify a length |
| 170 | finally: |
| 171 | # Cleanup |
| 172 | os.unlink(tmpfile) |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 173 | |
| 174 | |
| 175 | if __name__ == '__main__': |
| 176 | main() |