blob: 41a4f274a88b25e69295692dcfa3d8d2b2a03b94 [file] [log] [blame]
Jean-Paul Calderone8671c852011-03-02 19:26:20 -05001# Copyright (c) Frederick Dean
2# See LICENSE for details.
Rick Dean433dc642009-07-07 13:11:55 -05003
4"""
Alex Chan6b69c552016-10-24 16:34:41 +01005Unit tests for `OpenSSL.rand`.
Rick Dean433dc642009-07-07 13:11:55 -05006"""
7
Jean-Paul Calderonee379d732010-07-30 17:06:48 -04008import os
Rick Dean433dc642009-07-07 13:11:55 -05009import stat
Jean-Paul Calderonec7cd3e62013-12-31 13:54:39 -050010import sys
Rick Dean433dc642009-07-07 13:11:55 -050011
Hynek Schlawack80d005f2015-10-20 18:34:13 +020012import pytest
13
Rick Dean433dc642009-07-07 13:11:55 -050014from OpenSSL import rand
15
Alex Chan6b69c552016-10-24 16:34:41 +010016from .util import NON_ASCII
Hynek Schlawackf0e66852015-10-16 20:18:38 +020017
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040018
Alex Chan6b69c552016-10-24 16:34:41 +010019class TestRand(object):
Jean-Paul Calderonee379d732010-07-30 17:06:48 -040020
Alex Chan6b69c552016-10-24 16:34:41 +010021 @pytest.mark.parametrize('args', [
lymanZerga117f3914b2017-01-30 14:58:45 +080022 (None,),
23 (b"foo",),
Alex Chan6b69c552016-10-24 16:34:41 +010024 ])
25 def test_bytes_wrong_args(self, args):
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050026 """
Alex Chan6b69c552016-10-24 16:34:41 +010027 `OpenSSL.rand.bytes` raises `TypeError` if called with a non-`int`
28 argument.
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050029 """
Alex Chan6b69c552016-10-24 16:34:41 +010030 with pytest.raises(TypeError):
31 rand.bytes(*args)
32
33 def test_insufficient_memory(self):
34 """
Alex Gaynorb3460c62017-07-06 22:40:40 -040035 `OpenSSL.rand.bytes` raises `MemoryError` or `OverflowError` if more
36 bytes are requested than will fit in memory.
Alex Chan6b69c552016-10-24 16:34:41 +010037 """
Alex Gaynorb3460c62017-07-06 22:40:40 -040038 with pytest.raises((MemoryError, OverflowError)):
39 rand.bytes(sys.maxsize + 1)
Jean-Paul Calderonec86bb7d2013-12-29 10:25:59 -050040
Rick Dean433dc642009-07-07 13:11:55 -050041 def test_bytes(self):
42 """
Alex Chan6b69c552016-10-24 16:34:41 +010043 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 Dean433dc642009-07-07 13:11:55 -050046 """
47 b1 = rand.bytes(50)
Alex Chan6b69c552016-10-24 16:34:41 +010048 assert len(b1) == 50
Rick Dean433dc642009-07-07 13:11:55 -050049 b2 = rand.bytes(num_bytes=50) # parameter by name
Alex Chan6b69c552016-10-24 16:34:41 +010050 assert b1 != b2 # Hip, Hip, Horay! FIPS complaince
Jean-Paul Calderonee379d732010-07-30 17:06:48 -040051 b3 = rand.bytes(num_bytes=0)
Alex Chan6b69c552016-10-24 16:34:41 +010052 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 Dean433dc642009-07-07 13:11:55 -050056
Alex Chan6b69c552016-10-24 16:34:41 +010057 @pytest.mark.parametrize('args', [
58 (b"foo", None),
59 (None, 3),
60 ])
61 def test_add_wrong_args(self, args):
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040062 """
Alex Chan6b69c552016-10-24 16:34:41 +010063 `OpenSSL.rand.add` raises `TypeError` if called with arguments not of
64 type `str` and `int`.
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040065 """
Alex Chan6b69c552016-10-24 16:34:41 +010066 with pytest.raises(TypeError):
67 rand.add(*args)
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040068
Rick Dean433dc642009-07-07 13:11:55 -050069 def test_add(self):
70 """
Alex Chan6b69c552016-10-24 16:34:41 +010071 `OpenSSL.rand.add` adds entropy to the PRNG.
Rick Dean433dc642009-07-07 13:11:55 -050072 """
Alex Gaynore7f51982016-09-11 11:48:14 -040073 rand.add(b'hamburger', 3)
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040074
Alex Chan6b69c552016-10-24 16:34:41 +010075 @pytest.mark.parametrize('args', [
lymanZerga117f3914b2017-01-30 14:58:45 +080076 (None,),
77 (42,),
Alex Chan6b69c552016-10-24 16:34:41 +010078 ])
79 def test_seed_wrong_args(self, args):
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040080 """
Alex Chan6b69c552016-10-24 16:34:41 +010081 `OpenSSL.rand.seed` raises `TypeError` if called with
82 a non-`str` argument.
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040083 """
Alex Chan6b69c552016-10-24 16:34:41 +010084 with pytest.raises(TypeError):
85 rand.seed(*args)
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -040086
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040087 def test_seed(self):
88 """
Alex Chan6b69c552016-10-24 16:34:41 +010089 `OpenSSL.rand.seed` adds entropy to the PRNG.
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040090 """
Alex Gaynore7f51982016-09-11 11:48:14 -040091 rand.seed(b'milk shake')
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040092
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040093 def test_status(self):
94 """
Alex Chan6b69c552016-10-24 16:34:41 +010095 `OpenSSL.rand.status` returns `1` if the PRNG has sufficient entropy,
96 `0` otherwise.
Jean-Paul Calderonebc2bb812009-07-16 13:31:20 -040097 """
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 Chan6b69c552016-10-24 16:34:41 +0100101 assert rand.status() in (0, 1)
Rick Dean433dc642009-07-07 13:11:55 -0500102
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400103 def test_cleanup(self):
104 """
Alex Chan6b69c552016-10-24 16:34:41 +0100105 `OpenSSL.rand.cleanup` releases the memory used by the PRNG and
106 returns `None`.
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400107 """
Alex Chan6b69c552016-10-24 16:34:41 +0100108 assert rand.cleanup() is None
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400109
Alex Chan6b69c552016-10-24 16:34:41 +0100110 @pytest.mark.parametrize('args', [
111 ("foo", None),
112 (None, 1),
113 ])
114 def test_load_file_wrong_args(self, args):
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400115 """
Alex Chan6b69c552016-10-24 16:34:41 +0100116 `OpenSSL.rand.load_file` raises `TypeError` when with arguments
117 not of type `str` and `int`.
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400118 """
Alex Chan6b69c552016-10-24 16:34:41 +0100119 with pytest.raises(TypeError):
120 rand.load_file(*args)
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400121
Alex Chan6b69c552016-10-24 16:34:41 +0100122 @pytest.mark.parametrize('args', [
123 None,
124 1,
125 ])
126 def test_write_file_wrong_args(self, args):
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400127 """
Alex Chan6b69c552016-10-24 16:34:41 +0100128 `OpenSSL.rand.write_file` raises `TypeError` when called with
129 a non-`str` argument.
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400130 """
Alex Chan6b69c552016-10-24 16:34:41 +0100131 with pytest.raises(TypeError):
132 rand.write_file(*args)
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400133
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400134 def _read_write_test(self, path):
Rick Dean433dc642009-07-07 13:11:55 -0500135 """
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400136 Verify that ``rand.write_file`` and ``rand.load_file`` can be used.
Rick Dean433dc642009-07-07 13:11:55 -0500137 """
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400138 # Create the file so cleanup is more straightforward
139 with open(path, "w"):
140 pass
141
Jean-Paul Calderoneb534ff42009-07-16 13:44:56 -0400142 try:
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400143 # Write random bytes to a file
144 rand.write_file(path)
145
Jean-Paul Calderoneb534ff42009-07-16 13:44:56 -0400146 # Verify length of written file
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400147 size = os.stat(path)[stat.ST_SIZE]
Alex Chan6b69c552016-10-24 16:34:41 +0100148 assert size == 1024
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400149
Jean-Paul Calderoneee0a1f02010-07-30 17:04:24 -0400150 # Read random bytes from file
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400151 rand.load_file(path)
152 rand.load_file(path, 4) # specify a length
Jean-Paul Calderoneb534ff42009-07-16 13:44:56 -0400153 finally:
154 # Cleanup
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400155 os.unlink(path)
156
Alex Chan6b69c552016-10-24 16:34:41 +0100157 def test_bytes_paths(self, tmpfile):
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400158 """
159 Random data can be saved and loaded to files with paths specified as
160 bytes.
161 """
Alex Chan6b69c552016-10-24 16:34:41 +0100162 path = tmpfile
Jean-Paul Calderone210c0f32015-04-12 09:20:31 -0400163 path += NON_ASCII.encode(sys.getfilesystemencoding())
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400164 self._read_write_test(path)
165
Alex Chan6b69c552016-10-24 16:34:41 +0100166 def test_unicode_paths(self, tmpfile):
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400167 """
168 Random data can be saved and loaded to files with paths specified as
169 unicode.
170 """
Alex Chan6b69c552016-10-24 16:34:41 +0100171 path = tmpfile.decode('utf-8') + NON_ASCII
Jean-Paul Calderonecbb68cc2015-04-11 12:41:30 -0400172 self._read_write_test(path)