mirror of https://github.com/python/cpython
gh-107862: Add roundtrip `hypothesis` tests to `test_binascii` (#107863)
This commit is contained in:
parent
a1cbace91b
commit
3b73f9f00e
|
@ -5,6 +5,7 @@ import binascii
|
|||
import array
|
||||
import re
|
||||
from test.support import bigmemtest, _1G, _4G
|
||||
from test.support.hypothesis_helper import hypothesis
|
||||
|
||||
|
||||
# Note: "*_hex" functions are aliases for "(un)hexlify"
|
||||
|
@ -27,6 +28,14 @@ class BinASCIITest(unittest.TestCase):
|
|||
def setUp(self):
|
||||
self.data = self.type2test(self.rawdata)
|
||||
|
||||
def assertConversion(self, original, converted, restored, **kwargs):
|
||||
self.assertIsInstance(original, bytes)
|
||||
self.assertIsInstance(converted, bytes)
|
||||
self.assertIsInstance(restored, bytes)
|
||||
if converted:
|
||||
self.assertLess(max(converted), 128)
|
||||
self.assertEqual(original, restored, msg=f'{self.type2test=} {kwargs=}')
|
||||
|
||||
def test_exceptions(self):
|
||||
# Check module exceptions
|
||||
self.assertTrue(issubclass(binascii.Error, Exception))
|
||||
|
@ -52,9 +61,7 @@ class BinASCIITest(unittest.TestCase):
|
|||
self.fail("{}/{} conversion raises {!r}".format(fb, fa, err))
|
||||
self.assertEqual(res, raw, "{}/{} conversion: "
|
||||
"{!r} != {!r}".format(fb, fa, res, raw))
|
||||
self.assertIsInstance(res, bytes)
|
||||
self.assertIsInstance(a, bytes)
|
||||
self.assertLess(max(a), 128)
|
||||
self.assertConversion(raw, a, res)
|
||||
self.assertIsInstance(binascii.crc_hqx(raw, 0), int)
|
||||
self.assertIsInstance(binascii.crc32(raw), int)
|
||||
|
||||
|
@ -222,6 +229,15 @@ class BinASCIITest(unittest.TestCase):
|
|||
with self.assertRaises(TypeError):
|
||||
binascii.b2a_uu(b"", True)
|
||||
|
||||
@hypothesis.given(
|
||||
binary=hypothesis.strategies.binary(),
|
||||
backtick=hypothesis.strategies.booleans(),
|
||||
)
|
||||
def test_hex_roundtrip(self, binary, backtick):
|
||||
converted = binascii.b2a_uu(self.type2test(binary), backtick=backtick)
|
||||
restored = binascii.a2b_uu(self.type2test(converted))
|
||||
self.assertConversion(binary, converted, restored, backtick=backtick)
|
||||
|
||||
def test_crc_hqx(self):
|
||||
crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0)
|
||||
crc = binascii.crc_hqx(self.type2test(b" this string."), crc)
|
||||
|
@ -259,6 +275,12 @@ class BinASCIITest(unittest.TestCase):
|
|||
self.assertEqual(binascii.hexlify(self.type2test(s)), t)
|
||||
self.assertEqual(binascii.unhexlify(self.type2test(t)), u)
|
||||
|
||||
@hypothesis.given(binary=hypothesis.strategies.binary())
|
||||
def test_hex_roundtrip(self, binary):
|
||||
converted = binascii.hexlify(self.type2test(binary))
|
||||
restored = binascii.unhexlify(self.type2test(converted))
|
||||
self.assertConversion(binary, converted, restored)
|
||||
|
||||
def test_hex_separator(self):
|
||||
"""Test that hexlify and b2a_hex are binary versions of bytes.hex."""
|
||||
# Logic of separators is tested in test_bytes.py. This checks that
|
||||
|
@ -373,6 +395,21 @@ class BinASCIITest(unittest.TestCase):
|
|||
self.assertEqual(b2a_qp(type2test(b'a.\n')), b'a.\n')
|
||||
self.assertEqual(b2a_qp(type2test(b'.a')[:-1]), b'=2E')
|
||||
|
||||
@hypothesis.given(
|
||||
binary=hypothesis.strategies.binary(),
|
||||
quotetabs=hypothesis.strategies.booleans(),
|
||||
istext=hypothesis.strategies.booleans(),
|
||||
header=hypothesis.strategies.booleans(),
|
||||
)
|
||||
def test_b2a_qp_a2b_qp_round_trip(self, binary, quotetabs, istext, header):
|
||||
converted = binascii.b2a_qp(
|
||||
self.type2test(binary),
|
||||
quotetabs=quotetabs, istext=istext, header=header,
|
||||
)
|
||||
restored = binascii.a2b_qp(self.type2test(converted), header=header)
|
||||
self.assertConversion(binary, converted, restored,
|
||||
quotetabs=quotetabs, istext=istext, header=header)
|
||||
|
||||
def test_empty_string(self):
|
||||
# A test for SF bug #1022953. Make sure SystemError is not raised.
|
||||
empty = self.type2test(b'')
|
||||
|
@ -428,6 +465,15 @@ class BinASCIITest(unittest.TestCase):
|
|||
self.assertEqual(binascii.b2a_base64(b, newline=False),
|
||||
b'aGVsbG8=')
|
||||
|
||||
@hypothesis.given(
|
||||
binary=hypothesis.strategies.binary(),
|
||||
newline=hypothesis.strategies.booleans(),
|
||||
)
|
||||
def test_base64_roundtrip(self, binary, newline):
|
||||
converted = binascii.b2a_base64(self.type2test(binary), newline=newline)
|
||||
restored = binascii.a2b_base64(self.type2test(converted))
|
||||
self.assertConversion(binary, converted, restored, newline=newline)
|
||||
|
||||
|
||||
class ArrayBinASCIITest(BinASCIITest):
|
||||
def type2test(self, s):
|
||||
|
|
Loading…
Reference in New Issue