5
5
import array
6
6
import re
7
7
from test .support import bigmemtest , _1G , _4G
8
+ from test .support .hypothesis_helper import hypothesis
8
9
9
10
10
11
# Note: "*_hex" functions are aliases for "(un)hexlify"
@@ -27,6 +28,14 @@ class BinASCIITest(unittest.TestCase):
27
28
def setUp (self ):
28
29
self .data = self .type2test (self .rawdata )
29
30
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
+
30
39
def test_exceptions (self ):
31
40
# Check module exceptions
32
41
self .assertTrue (issubclass (binascii .Error , Exception ))
@@ -52,9 +61,7 @@ def test_returned_value(self):
52
61
self .fail ("{}/{} conversion raises {!r}" .format (fb , fa , err ))
53
62
self .assertEqual (res , raw , "{}/{} conversion: "
54
63
"{!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 )
58
65
self .assertIsInstance (binascii .crc_hqx (raw , 0 ), int )
59
66
self .assertIsInstance (binascii .crc32 (raw ), int )
60
67
@@ -222,6 +229,15 @@ def test_uu(self):
222
229
with self .assertRaises (TypeError ):
223
230
binascii .b2a_uu (b"" , True )
224
231
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
+
225
241
def test_crc_hqx (self ):
226
242
crc = binascii .crc_hqx (self .type2test (b"Test the CRC-32 of" ), 0 )
227
243
crc = binascii .crc_hqx (self .type2test (b" this string." ), crc )
@@ -259,6 +275,12 @@ def test_hex(self):
259
275
self .assertEqual (binascii .hexlify (self .type2test (s )), t )
260
276
self .assertEqual (binascii .unhexlify (self .type2test (t )), u )
261
277
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
+
262
284
def test_hex_separator (self ):
263
285
"""Test that hexlify and b2a_hex are binary versions of bytes.hex."""
264
286
# Logic of separators is tested in test_bytes.py. This checks that
@@ -373,6 +395,21 @@ def test_qp(self):
373
395
self .assertEqual (b2a_qp (type2test (b'a.\n ' )), b'a.\n ' )
374
396
self .assertEqual (b2a_qp (type2test (b'.a' )[:- 1 ]), b'=2E' )
375
397
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
+
376
413
def test_empty_string (self ):
377
414
# A test for SF bug #1022953. Make sure SystemError is not raised.
378
415
empty = self .type2test (b'' )
@@ -428,6 +465,15 @@ def test_b2a_base64_newline(self):
428
465
self .assertEqual (binascii .b2a_base64 (b , newline = False ),
429
466
b'aGVsbG8=' )
430
467
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
+
431
477
432
478
class ArrayBinASCIITest (BinASCIITest ):
433
479
def type2test (self , s ):
0 commit comments