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 |
| 8 | import os |
| 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): |
| 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 Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame^] | 28 | exc = self.assertRaises(ValueError, rand.bytes, -1) |
| 29 | self.assertEqual(exc.message, "num_bytes must not be negative") |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 30 | |
| 31 | |
| 32 | def test_add(self): |
| 33 | """ |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame^] | 34 | L{OpenSSL.rand.add} adds entropy to the PRNG. |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 35 | """ |
| 36 | rand.add('hamburger', 3) |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame^] | 37 | |
| 38 | |
| 39 | def test_seed(self): |
| 40 | """ |
| 41 | L{OpenSSL.rand.seed} adds entropy to the PRNG. |
| 42 | """ |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 43 | rand.seed('milk shake') |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame^] | 44 | |
| 45 | |
| 46 | def test_status(self): |
| 47 | """ |
| 48 | L{OpenSSL.rand.status} returns C{True} if the PRNG has sufficient |
| 49 | entropy, C{False} otherwise. |
| 50 | """ |
| 51 | # It's hard to know what it is actually going to return. Different |
| 52 | # OpenSSL random engines decide differently whether they have enough |
| 53 | # entropy or not. |
| 54 | self.assertTrue(rand.status() in (1, 2)) |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def test_files(self): |
| 58 | """ |
| 59 | Test reading and writing of files via rand functions. |
| 60 | """ |
| 61 | # Write random bytes to a file |
| 62 | tmpfile = self.mktemp() |
| 63 | rand.write_file(tmpfile) |
| 64 | # Verify length of written file |
| 65 | size = os.stat(tmpfile)[stat.ST_SIZE] |
| 66 | self.assertEquals(size, 1024) |
| 67 | # Read random bytes from file |
| 68 | rand.load_file(tmpfile) |
| 69 | rand.load_file(tmpfile, 4) # specify a length |
| 70 | # Cleanup |
| 71 | os.unlink(tmpfile) |
| 72 | |
| 73 | |
| 74 | if __name__ == '__main__': |
| 75 | main() |