Skip to content

gh-107862: Add roundtrip hypothesis tests to test_binascii #107863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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"
Expand All @@ -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))
Expand All @@ -52,9 +61,7 @@ def test_returned_value(self):
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)

Expand Down Expand Up @@ -222,6 +229,15 @@ def test_uu(self):
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)
Expand Down Expand Up @@ -259,6 +275,12 @@ def test_hex(self):
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if unhexlify should also take a sep and bytes_per_sep argument

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
Expand Down Expand Up @@ -373,6 +395,21 @@ def test_qp(self):
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'')
Expand Down Expand Up @@ -428,6 +465,15 @@ def test_b2a_base64_newline(self):
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):
Expand Down