From e041df73bb7bf424bcf571f25e3459359c4f36ed Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Thu, 9 May 2019 03:19:29 -0400 Subject: [PATCH] Add tests for overflows converting ints to bytes --- tests/basics/array_overflow.py | 35 ++++++++++++++++++++++++ tests/basics/int_bytes.py | 12 ++++++++ tests/basics/int_bytes_intbig.py | 12 ++++++++ tests/basics/struct_overflow.py | 47 ++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 tests/basics/array_overflow.py create mode 100644 tests/basics/struct_overflow.py diff --git a/tests/basics/array_overflow.py b/tests/basics/array_overflow.py new file mode 100644 index 0000000000..ba3eb7f060 --- /dev/null +++ b/tests/basics/array_overflow.py @@ -0,0 +1,35 @@ +import skip_if +skip_if.no_bigint() + +try: + from array import array +except ImportError: + print("SKIP") + raise SystemExit + +def test_array_overflow(typecode, val): + try: + print(array(typecode, [val])) + except OverflowError: + print('OverflowError') + +# small int -1 +test_array_overflow('Q', -1) +test_array_overflow('L', -1) +test_array_overflow('I', -1) +test_array_overflow('H', -1) +test_array_overflow('B', -1) + +# big int -1 +test_array_overflow('Q', -2**64 // 2**64) +test_array_overflow('L', -2**64 // 2**64) +test_array_overflow('I', -2**64 // 2**64) +test_array_overflow('H', -2**64 // 2**64) +test_array_overflow('B', -2**64 // 2**64) + +# big int 2**63 +test_array_overflow('q', 2**63) +test_array_overflow('l', 2**63) +test_array_overflow('i', 2**63) +test_array_overflow('h', 2**63) +test_array_overflow('b', 2**63) diff --git a/tests/basics/int_bytes.py b/tests/basics/int_bytes.py index 059c16d3fa..8c0c9a82f8 100644 --- a/tests/basics/int_bytes.py +++ b/tests/basics/int_bytes.py @@ -17,3 +17,15 @@ try: (1).to_bytes(-1, "little") except ValueError: print("ValueError") + +# too small buffer should raise an error +try: + (256).to_bytes(1, "little") +except OverflowError: + print("OverflowError") + +# negative numbers should raise an error +try: + (-256).to_bytes(2, "little") +except OverflowError: + print("OverflowError") diff --git a/tests/basics/int_bytes_intbig.py b/tests/basics/int_bytes_intbig.py index ce6fec40ae..b19c8ae53e 100644 --- a/tests/basics/int_bytes_intbig.py +++ b/tests/basics/int_bytes_intbig.py @@ -15,3 +15,15 @@ print(ib.to_bytes(20, "big")) # check that extra zero bytes don't change the internal int value print(int.from_bytes(b + bytes(10), "little") == int.from_bytes(b, "little")) + +# too small buffer should raise an error +try: + (2**64).to_bytes(8, "little") +except OverflowError: + print("OverflowError") + +# negative numbers should raise an error +try: + (-2**64).to_bytes(9, "little") +except OverflowError: + print("OverflowError") diff --git a/tests/basics/struct_overflow.py b/tests/basics/struct_overflow.py new file mode 100644 index 0000000000..aee0c6b9db --- /dev/null +++ b/tests/basics/struct_overflow.py @@ -0,0 +1,47 @@ +import skip_if +skip_if.no_bigint() + +try: + import ustruct as struct +except: + try: + import struct + except ImportError: + print("SKIP") + raise SystemExit + +def test_struct_overflow(typecode, val): + try: + print(struct.pack(typecode, val)) + except OverflowError: + print('OverflowError') + except struct.error: + print('OverflowError') + +# small int -1 +test_struct_overflow('>Q', -1) +test_struct_overflow('>L', -1) +test_struct_overflow('>I', -1) +test_struct_overflow('>H', -1) +test_struct_overflow('>B', -1) + +# big int -1 +test_struct_overflow('>Q', -2**64 // 2**64) +test_struct_overflow('>L', -2**64 // 2**64) +test_struct_overflow('>I', -2**64 // 2**64) +test_struct_overflow('>H', -2**64 // 2**64) +test_struct_overflow('>B', -2**64 // 2**64) + +# possibly small ints +test_struct_overflow('>q', 2**63) +test_struct_overflow('>l', 2**31) +test_struct_overflow('>i', 2**31) +test_struct_overflow('>h', 2**15) +test_struct_overflow('>b', 2**7) + +# definitely big ints +test_struct_overflow('>q', 2**64 // 2**1) +test_struct_overflow('>l', 2**64 // 2**33) +test_struct_overflow('>i', 2**64 // 2**33) +test_struct_overflow('>h', 2**64 // 2**49) +test_struct_overflow('>b', 2**64 // 2**57)