summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2023-09-01 21:04:07 (GMT)
committerGitHub <noreply@github.com>2023-09-01 21:04:07 (GMT)
commit3b73f9f00e44d6b709fa27f83d64d7355e1891ca (patch)
tree2ee1e9c97941a5c606a4c0ce7e8fdef4d4cd1c04
parenta1cbace91b624681dd7bb8eb82ba187bda55d785 (diff)
downloadcpython-3b73f9f00e44d6b709fa27f83d64d7355e1891ca.zip
cpython-3b73f9f00e44d6b709fa27f83d64d7355e1891ca.tar.gz
cpython-3b73f9f00e44d6b709fa27f83d64d7355e1891ca.tar.bz2
gh-107862: Add roundtrip `hypothesis` tests to `test_binascii` (#107863)
-rw-r--r--Lib/test/test_binascii.py52
1 files changed, 49 insertions, 3 deletions
diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py
index a2d7d02..eb831b1 100644
--- a/Lib/test/test_binascii.py
+++ b/Lib/test/test_binascii.py
@@ -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):