55import array
66import re
77from test .support import bigmemtest , _1G , _4G
8+ from test .support .hypothesis_helper import hypothesis
89
910
1011# Note: "*_hex" functions are aliases for "(un)hexlify"
@@ -27,6 +28,14 @@ class BinASCIITest(unittest.TestCase):
2728 def setUp (self ):
2829 self .data = self .type2test (self .rawdata )
2930
31+ def assertConversion (self , original , converted , restored , ** kwargs ):
32+ self .assertIsInstance (original , bytes )
33+ self .assertIsInstance (converted , bytes )
34+ self .assertIsInstance (restored , bytes )
35+ if converted :
36+ self .assertLess (max (converted ), 128 )
37+ self .assertEqual (original , restored , msg = f'{ self .type2test = } { kwargs = } ' )
38+
3039 def test_exceptions (self ):
3140 # Check module exceptions
3241 self .assertTrue (issubclass (binascii .Error , Exception ))
@@ -52,9 +61,7 @@ def test_returned_value(self):
5261 self .fail ("{}/{} conversion raises {!r}" .format (fb , fa , err ))
5362 self .assertEqual (res , raw , "{}/{} conversion: "
5463 "{!r} != {!r}" .format (fb , fa , res , raw ))
55- self .assertIsInstance (res , bytes )
56- self .assertIsInstance (a , bytes )
57- self .assertLess (max (a ), 128 )
64+ self .assertConversion (raw , a , res )
5865 self .assertIsInstance (binascii .crc_hqx (raw , 0 ), int )
5966 self .assertIsInstance (binascii .crc32 (raw ), int )
6067
@@ -222,6 +229,15 @@ def test_uu(self):
222229 with self .assertRaises (TypeError ):
223230 binascii .b2a_uu (b"" , True )
224231
232+ @hypothesis .given (
233+ binary = hypothesis .strategies .binary (),
234+ backtick = hypothesis .strategies .booleans (),
235+ )
236+ def test_hex_roundtrip (self , binary , backtick ):
237+ converted = binascii .b2a_uu (self .type2test (binary ), backtick = backtick )
238+ restored = binascii .a2b_uu (self .type2test (converted ))
239+ self .assertConversion (binary , converted , restored , backtick = backtick )
240+
225241 def test_crc_hqx (self ):
226242 crc = binascii .crc_hqx (self .type2test (b"Test the CRC-32 of" ), 0 )
227243 crc = binascii .crc_hqx (self .type2test (b" this string." ), crc )
@@ -259,6 +275,12 @@ def test_hex(self):
259275 self .assertEqual (binascii .hexlify (self .type2test (s )), t )
260276 self .assertEqual (binascii .unhexlify (self .type2test (t )), u )
261277
278+ @hypothesis .given (binary = hypothesis .strategies .binary ())
279+ def test_hex_roundtrip (self , binary ):
280+ converted = binascii .hexlify (self .type2test (binary ))
281+ restored = binascii .unhexlify (self .type2test (converted ))
282+ self .assertConversion (binary , converted , restored )
283+
262284 def test_hex_separator (self ):
263285 """Test that hexlify and b2a_hex are binary versions of bytes.hex."""
264286 # Logic of separators is tested in test_bytes.py. This checks that
@@ -373,6 +395,21 @@ def test_qp(self):
373395 self .assertEqual (b2a_qp (type2test (b'a.\n ' )), b'a.\n ' )
374396 self .assertEqual (b2a_qp (type2test (b'.a' )[:- 1 ]), b'=2E' )
375397
398+ @hypothesis .given (
399+ binary = hypothesis .strategies .binary (),
400+ quotetabs = hypothesis .strategies .booleans (),
401+ istext = hypothesis .strategies .booleans (),
402+ header = hypothesis .strategies .booleans (),
403+ )
404+ def test_b2a_qp_a2b_qp_round_trip (self , binary , quotetabs , istext , header ):
405+ converted = binascii .b2a_qp (
406+ self .type2test (binary ),
407+ quotetabs = quotetabs , istext = istext , header = header ,
408+ )
409+ restored = binascii .a2b_qp (self .type2test (converted ), header = header )
410+ self .assertConversion (binary , converted , restored ,
411+ quotetabs = quotetabs , istext = istext , header = header )
412+
376413 def test_empty_string (self ):
377414 # A test for SF bug #1022953. Make sure SystemError is not raised.
378415 empty = self .type2test (b'' )
@@ -428,6 +465,15 @@ def test_b2a_base64_newline(self):
428465 self .assertEqual (binascii .b2a_base64 (b , newline = False ),
429466 b'aGVsbG8=' )
430467
468+ @hypothesis .given (
469+ binary = hypothesis .strategies .binary (),
470+ newline = hypothesis .strategies .booleans (),
471+ )
472+ def test_base64_roundtrip (self , binary , newline ):
473+ converted = binascii .b2a_base64 (self .type2test (binary ), newline = newline )
474+ restored = binascii .a2b_base64 (self .type2test (converted ))
475+ self .assertConversion (binary , converted , restored , newline = newline )
476+
431477
432478class ArrayBinASCIITest (BinASCIITest ):
433479 def type2test (self , s ):
0 commit comments