Skip to content

Commit

Permalink
Add tests for BN_rand and BN_rand_range
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxxy committed Feb 23, 2018
1 parent 69b529b commit c4981ad
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/hazmat/backends/test_openssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ def test_bn_to_int(self):
bn = backend._int_to_bn(0)
assert backend._bn_to_int(bn) == 0

def test_bn_rand(self):
size_bits = 8
rand_bn_ptr = backend._lib.BN_new()
assert rand_bn_ptr != backend._ffi.NULL
rand_bn_ptr = backend._ffi.gc(rand_bn_ptr, backend._lib.BN_free)

res = backend._lib.BN_rand(rand_bn_ptr, size_bits, -1, 0)
assert res == 1

# Value should fit in one byte when serialized
rand_val = backend._bn_to_int(rand_bn_ptr)
int.to_bytes(rand_val, 1, 'big')

def test_bn_rand_range(self):
value = 100
bn_range_ptr = backend._int_to_bn(value)

bn_rand_ptr = backend._lib.BN_new()
assert bn_rand_ptr != backend._ffi.NULL
bn_rand_ptr = backend._ffi.gc(bn_rand_ptr, backend._lib.BN_free)

res = backend._lib.BN_rand_range(bn_rand_ptr, bn_range_ptr)
assert res == 1

rand_val = backend._bn_to_int(rand_bn_ptr)
assert rand_val < value and rand_val >= 0

class TestOpenSSLRandomEngine(object):
def setup(self):
Expand Down

0 comments on commit c4981ad

Please sign in to comment.