Jean-Paul Calderone | 8671c85 | 2011-03-02 19:26:20 -0500 | [diff] [blame] | 1 | # Copyright (c) Frederick Dean |
| 2 | # See LICENSE for details. |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 3 | |
| 4 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 5 | Unit tests for `OpenSSL.rand`. |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 6 | """ |
| 7 | |
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 |
Jean-Paul Calderone | c7cd3e6 | 2013-12-31 13:54:39 -0500 | [diff] [blame] | 10 | import sys |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 11 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 12 | import pytest |
| 13 | |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 14 | from OpenSSL import rand |
| 15 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 16 | from .util import NON_ASCII |
Hynek Schlawack | f0e6685 | 2015-10-16 20:18:38 +0200 | [diff] [blame] | 17 | |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 18 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 19 | class TestRand(object): |
Jean-Paul Calderone | e379d73 | 2010-07-30 17:06:48 -0400 | [diff] [blame] | 20 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 21 | @pytest.mark.parametrize('args', [ |
lymanZerga11 | 7f3914b | 2017-01-30 14:58:45 +0800 | [diff] [blame] | 22 | (None,), |
| 23 | (b"foo",), |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 24 | ]) |
| 25 | def test_bytes_wrong_args(self, args): |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 26 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 27 | `OpenSSL.rand.bytes` raises `TypeError` if called with a non-`int` |
| 28 | argument. |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 29 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 30 | with pytest.raises(TypeError): |
| 31 | rand.bytes(*args) |
| 32 | |
| 33 | def test_insufficient_memory(self): |
| 34 | """ |
Alex Gaynor | b3460c6 | 2017-07-06 22:40:40 -0400 | [diff] [blame] | 35 | `OpenSSL.rand.bytes` raises `MemoryError` or `OverflowError` if more |
| 36 | bytes are requested than will fit in memory. |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 37 | """ |
Alex Gaynor | b3460c6 | 2017-07-06 22:40:40 -0400 | [diff] [blame] | 38 | with pytest.raises((MemoryError, OverflowError)): |
| 39 | rand.bytes(sys.maxsize + 1) |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 40 | |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 41 | def test_bytes(self): |
| 42 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 43 | Verify that we can obtain bytes from rand_bytes() and that they are |
| 44 | different each time. Test the parameter of rand_bytes() for |
| 45 | bad values. |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 46 | """ |
| 47 | b1 = rand.bytes(50) |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 48 | assert len(b1) == 50 |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 49 | b2 = rand.bytes(num_bytes=50) # parameter by name |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 50 | assert b1 != b2 # Hip, Hip, Horay! FIPS complaince |
Jean-Paul Calderone | e379d73 | 2010-07-30 17:06:48 -0400 | [diff] [blame] | 51 | b3 = rand.bytes(num_bytes=0) |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 52 | assert len(b3) == 0 |
| 53 | with pytest.raises(ValueError) as exc: |
| 54 | rand.bytes(-1) |
| 55 | assert str(exc.value) == "num_bytes must not be negative" |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 56 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 57 | @pytest.mark.parametrize('args', [ |
| 58 | (b"foo", None), |
| 59 | (None, 3), |
| 60 | ]) |
| 61 | def test_add_wrong_args(self, args): |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 62 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 63 | `OpenSSL.rand.add` raises `TypeError` if called with arguments not of |
| 64 | type `str` and `int`. |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 65 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 66 | with pytest.raises(TypeError): |
| 67 | rand.add(*args) |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 68 | |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 69 | def test_add(self): |
| 70 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 71 | `OpenSSL.rand.add` adds entropy to the PRNG. |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 72 | """ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 73 | rand.add(b'hamburger', 3) |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 74 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 75 | @pytest.mark.parametrize('args', [ |
lymanZerga11 | 7f3914b | 2017-01-30 14:58:45 +0800 | [diff] [blame] | 76 | (None,), |
| 77 | (42,), |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 78 | ]) |
| 79 | def test_seed_wrong_args(self, args): |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 80 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 81 | `OpenSSL.rand.seed` raises `TypeError` if called with |
| 82 | a non-`str` argument. |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 83 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 84 | with pytest.raises(TypeError): |
| 85 | rand.seed(*args) |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 86 | |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 87 | def test_seed(self): |
| 88 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 89 | `OpenSSL.rand.seed` adds entropy to the PRNG. |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 90 | """ |
Alex Gaynor | e7f5198 | 2016-09-11 11:48:14 -0400 | [diff] [blame] | 91 | rand.seed(b'milk shake') |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 92 | |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 93 | def test_status(self): |
| 94 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 95 | `OpenSSL.rand.status` returns `1` if the PRNG has sufficient entropy, |
| 96 | `0` otherwise. |
Jean-Paul Calderone | bc2bb81 | 2009-07-16 13:31:20 -0400 | [diff] [blame] | 97 | """ |
| 98 | # It's hard to know what it is actually going to return. Different |
| 99 | # OpenSSL random engines decide differently whether they have enough |
| 100 | # entropy or not. |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 101 | assert rand.status() in (0, 1) |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 102 | |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 103 | def test_cleanup(self): |
| 104 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 105 | `OpenSSL.rand.cleanup` releases the memory used by the PRNG and |
| 106 | returns `None`. |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 107 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 108 | assert rand.cleanup() is None |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 109 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 110 | @pytest.mark.parametrize('args', [ |
| 111 | ("foo", None), |
| 112 | (None, 1), |
| 113 | ]) |
| 114 | def test_load_file_wrong_args(self, args): |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 115 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 116 | `OpenSSL.rand.load_file` raises `TypeError` when with arguments |
| 117 | not of type `str` and `int`. |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 118 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 119 | with pytest.raises(TypeError): |
| 120 | rand.load_file(*args) |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 121 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 122 | @pytest.mark.parametrize('args', [ |
| 123 | None, |
| 124 | 1, |
| 125 | ]) |
| 126 | def test_write_file_wrong_args(self, args): |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 127 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 128 | `OpenSSL.rand.write_file` raises `TypeError` when called with |
| 129 | a non-`str` argument. |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 130 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 131 | with pytest.raises(TypeError): |
| 132 | rand.write_file(*args) |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 133 | |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 134 | def _read_write_test(self, path): |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 135 | """ |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 136 | Verify that ``rand.write_file`` and ``rand.load_file`` can be used. |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame] | 137 | """ |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 138 | # Create the file so cleanup is more straightforward |
| 139 | with open(path, "w"): |
| 140 | pass |
| 141 | |
Jean-Paul Calderone | b534ff4 | 2009-07-16 13:44:56 -0400 | [diff] [blame] | 142 | try: |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 143 | # Write random bytes to a file |
| 144 | rand.write_file(path) |
| 145 | |
Jean-Paul Calderone | b534ff4 | 2009-07-16 13:44:56 -0400 | [diff] [blame] | 146 | # Verify length of written file |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 147 | size = os.stat(path)[stat.ST_SIZE] |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 148 | assert size == 1024 |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 149 | |
Jean-Paul Calderone | ee0a1f0 | 2010-07-30 17:04:24 -0400 | [diff] [blame] | 150 | # Read random bytes from file |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 151 | rand.load_file(path) |
| 152 | rand.load_file(path, 4) # specify a length |
Jean-Paul Calderone | b534ff4 | 2009-07-16 13:44:56 -0400 | [diff] [blame] | 153 | finally: |
| 154 | # Cleanup |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 155 | os.unlink(path) |
| 156 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 157 | def test_bytes_paths(self, tmpfile): |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 158 | """ |
| 159 | Random data can be saved and loaded to files with paths specified as |
| 160 | bytes. |
| 161 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 162 | path = tmpfile |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 163 | path += NON_ASCII.encode(sys.getfilesystemencoding()) |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 164 | self._read_write_test(path) |
| 165 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 166 | def test_unicode_paths(self, tmpfile): |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 167 | """ |
| 168 | Random data can be saved and loaded to files with paths specified as |
| 169 | unicode. |
| 170 | """ |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 171 | path = tmpfile.decode('utf-8') + NON_ASCII |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 172 | self._read_write_test(path) |