| 1 |
"""Test the binascii C module.""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
import binascii |
| 5 |
import array |
| 6 |
import re |
| 7 |
from test.support import bigmemtest, _1G, _4G |
| 8 |
from test.support.hypothesis_helper import hypothesis |
| 9 |
|
| 10 |
|
| 11 |
# Note: "*_hex" functions are aliases for "(un)hexlify" |
| 12 |
b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_qp', 'b2a_uu', |
| 13 |
'hexlify'] |
| 14 |
a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_qp', 'a2b_uu', |
| 15 |
'unhexlify'] |
| 16 |
all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx'] |
| 17 |
|
| 18 |
|
| 19 |
class BinASCIITest(unittest.TestCase): |
| 20 |
|
| 21 |
type2test = bytes |
| 22 |
# Create binary test data |
| 23 |
rawdata = b"The quick brown fox jumps over the lazy dog.\r\n" |
| 24 |
# Be slow so we don't depend on other modules |
| 25 |
rawdata += bytes(range(256)) |
| 26 |
rawdata += b"\r\nHello world.\n" |
| 27 |
|
| 28 |
def setUp(self): |
| 29 |
self.data = self.type2test(self.rawdata) |
| 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 |
|
| 39 |
def test_exceptions(self): |
| 40 |
# Check module exceptions |
| 41 |
self.assertIsSubclass(binascii.Error, Exception) |
| 42 |
self.assertIsSubclass(binascii.Incomplete, Exception) |
| 43 |
|
| 44 |
def test_functions(self): |
| 45 |
# Check presence of all functions |
| 46 |
for name in all_functions: |
| 47 |
self.assertHasAttr(getattr(binascii, name), '__call__') |
| 48 |
self.assertRaises(TypeError, getattr(binascii, name)) |
| 49 |
|
| 50 |
def test_returned_value(self): |
| 51 |
# Limit to the minimum of all limits (b2a_uu) |
| 52 |
MAX_ALL = 45 |
| 53 |
raw = self.rawdata[:MAX_ALL] |
| 54 |
for fa, fb in zip(a2b_functions, b2a_functions): |
| 55 |
a2b = getattr(binascii, fa) |
| 56 |
b2a = getattr(binascii, fb) |
| 57 |
try: |
| 58 |
a = b2a(self.type2test(raw)) |
| 59 |
res = a2b(self.type2test(a)) |
| 60 |
except Exception as err: |
| 61 |
self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) |
| 62 |
self.assertEqual(res, raw, "{}/{} conversion: " |
| 63 |
"{!r} != {!r}".format(fb, fa, res, raw)) |
| 64 |
self.assertConversion(raw, a, res) |
| 65 |
self.assertIsInstance(binascii.crc_hqx(raw, 0), int) |
| 66 |
self.assertIsInstance(binascii.crc32(raw), int) |
| 67 |
|
| 68 |
def test_base64valid(self): |
| 69 |
# Test base64 with valid data |
| 70 |
MAX_BASE64 = 57 |
| 71 |
lines = [] |
| 72 |
for i in range(0, len(self.rawdata), MAX_BASE64): |
| 73 |
b = self.type2test(self.rawdata[i:i+MAX_BASE64]) |
| 74 |
a = binascii.b2a_base64(b) |
| 75 |
lines.append(a) |
| 76 |
res = bytes() |
| 77 |
for line in lines: |
| 78 |
a = self.type2test(line) |
| 79 |
b = binascii.a2b_base64(a) |
| 80 |
res += b |
| 81 |
self.assertEqual(res, self.rawdata) |
| 82 |
|
| 83 |
def test_base64invalid(self): |
| 84 |
# Test base64 with random invalid characters sprinkled throughout |
| 85 |
# (This requires a new version of binascii.) |
| 86 |
MAX_BASE64 = 57 |
| 87 |
lines = [] |
| 88 |
for i in range(0, len(self.data), MAX_BASE64): |
| 89 |
b = self.type2test(self.rawdata[i:i+MAX_BASE64]) |
| 90 |
a = binascii.b2a_base64(b) |
| 91 |
lines.append(a) |
| 92 |
|
| 93 |
fillers = bytearray() |
| 94 |
valid = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" |
| 95 |
for i in range(256): |
| 96 |
if i not in valid: |
| 97 |
fillers.append(i) |
| 98 |
def addnoise(line): |
| 99 |
noise = fillers |
| 100 |
ratio = len(line) // len(noise) |
| 101 |
res = bytearray() |
| 102 |
while line and noise: |
| 103 |
if len(line) // len(noise) > ratio: |
| 104 |
c, line = line[0], line[1:] |
| 105 |
else: |
| 106 |
c, noise = noise[0], noise[1:] |
| 107 |
res.append(c) |
| 108 |
return res + noise + line |
| 109 |
res = bytearray() |
| 110 |
for line in map(addnoise, lines): |
| 111 |
a = self.type2test(line) |
| 112 |
b = binascii.a2b_base64(a) |
| 113 |
res += b |
| 114 |
self.assertEqual(res, self.rawdata) |
| 115 |
|
| 116 |
# Test base64 with just invalid characters, which should return |
| 117 |
# empty strings. TBD: shouldn't it raise an exception instead ? |
| 118 |
self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'') |
| 119 |
|
| 120 |
def test_base64_strict_mode(self): |
| 121 |
# Test base64 with strict mode on |
| 122 |
def _assertRegexTemplate(assert_regex: str, data: bytes, non_strict_mode_expected_result: bytes): |
| 123 |
with self.assertRaisesRegex(binascii.Error, assert_regex): |
| 124 |
binascii.a2b_base64(self.type2test(data), strict_mode=True) |
| 125 |
self.assertEqual(binascii.a2b_base64(self.type2test(data), strict_mode=False), |
| 126 |
non_strict_mode_expected_result) |
| 127 |
self.assertEqual(binascii.a2b_base64(self.type2test(data)), |
| 128 |
non_strict_mode_expected_result) |
| 129 |
|
| 130 |
def assertExcessData(data, non_strict_mode_expected_result: bytes): |
| 131 |
_assertRegexTemplate(r'(?i)Excess data', data, non_strict_mode_expected_result) |
| 132 |
|
| 133 |
def assertNonBase64Data(data, non_strict_mode_expected_result: bytes): |
| 134 |
_assertRegexTemplate(r'(?i)Only base64 data', data, non_strict_mode_expected_result) |
| 135 |
|
| 136 |
def assertLeadingPadding(data, non_strict_mode_expected_result: bytes): |
| 137 |
_assertRegexTemplate(r'(?i)Leading padding', data, non_strict_mode_expected_result) |
| 138 |
|
| 139 |
def assertDiscontinuousPadding(data, non_strict_mode_expected_result: bytes): |
| 140 |
_assertRegexTemplate(r'(?i)Discontinuous padding', data, non_strict_mode_expected_result) |
| 141 |
|
| 142 |
def assertExcessPadding(data, non_strict_mode_expected_result: bytes): |
| 143 |
_assertRegexTemplate(r'(?i)Excess padding', data, non_strict_mode_expected_result) |
| 144 |
|
| 145 |
# Test excess data exceptions |
| 146 |
assertExcessPadding(b'ab===', b'i') |
| 147 |
assertExcessPadding(b'ab====', b'i') |
| 148 |
assertNonBase64Data(b'ab==:', b'i') |
| 149 |
assertExcessData(b'abc=a', b'i\xb7\x1a') |
| 150 |
assertNonBase64Data(b'abc=:', b'i\xb7') |
| 151 |
assertNonBase64Data(b'ab==\n', b'i') |
| 152 |
assertExcessPadding(b'abc==', b'i\xb7') |
| 153 |
assertExcessPadding(b'abc===', b'i\xb7') |
| 154 |
assertExcessPadding(b'abc====', b'i\xb7') |
| 155 |
assertExcessPadding(b'abc=====', b'i\xb7') |
| 156 |
|
| 157 |
# Test non-base64 data exceptions |
| 158 |
assertNonBase64Data(b'\nab==', b'i') |
| 159 |
assertNonBase64Data(b'ab:(){:|:&};:==', b'i') |
| 160 |
assertNonBase64Data(b'a\nb==', b'i') |
| 161 |
assertNonBase64Data(b'a\x00b==', b'i') |
| 162 |
|
| 163 |
# Test malformed padding |
| 164 |
assertLeadingPadding(b'=', b'') |
| 165 |
assertLeadingPadding(b'==', b'') |
| 166 |
assertLeadingPadding(b'===', b'') |
| 167 |
assertLeadingPadding(b'====', b'') |
| 168 |
assertLeadingPadding(b'=====', b'') |
| 169 |
assertDiscontinuousPadding(b'ab=c=', b'i\xb7') |
| 170 |
assertDiscontinuousPadding(b'ab=ab==', b'i\xb6\x9b') |
| 171 |
assertExcessPadding(b'abcd=', b'i\xb7\x1d') |
| 172 |
assertExcessPadding(b'abcd==', b'i\xb7\x1d') |
| 173 |
assertExcessPadding(b'abcd===', b'i\xb7\x1d') |
| 174 |
assertExcessPadding(b'abcd====', b'i\xb7\x1d') |
| 175 |
assertExcessPadding(b'abcd=====', b'i\xb7\x1d') |
| 176 |
|
| 177 |
def test_base64_excess_data(self): |
| 178 |
# Test excess data exceptions |
| 179 |
def assertExcessData(data, expected): |
| 180 |
assert_regex = r'(?i)Excess data' |
| 181 |
data = self.type2test(data) |
| 182 |
with self.assertRaisesRegex(binascii.Error, assert_regex): |
| 183 |
binascii.a2b_base64(data, strict_mode=True) |
| 184 |
self.assertEqual(binascii.a2b_base64(data, strict_mode=False), |
| 185 |
expected) |
| 186 |
self.assertEqual(binascii.a2b_base64(data), expected) |
| 187 |
|
| 188 |
assertExcessData(b'ab==c=', b'i\xb7') |
| 189 |
assertExcessData(b'ab==cd', b'i\xb7\x1d') |
| 190 |
assertExcessData(b'abc=d', b'i\xb7\x1d') |
| 191 |
|
| 192 |
def test_base64errors(self): |
| 193 |
# Test base64 with invalid padding |
| 194 |
def assertIncorrectPadding(data): |
| 195 |
with self.assertRaisesRegex(binascii.Error, r'(?i)Incorrect padding'): |
| 196 |
binascii.a2b_base64(self.type2test(data)) |
| 197 |
|
| 198 |
assertIncorrectPadding(b'ab') |
| 199 |
assertIncorrectPadding(b'ab=') |
| 200 |
assertIncorrectPadding(b'abc') |
| 201 |
assertIncorrectPadding(b'abcdef') |
| 202 |
assertIncorrectPadding(b'abcdef=') |
| 203 |
assertIncorrectPadding(b'abcdefg') |
| 204 |
assertIncorrectPadding(b'a=b=') |
| 205 |
assertIncorrectPadding(b'a\nb=') |
| 206 |
|
| 207 |
# Test base64 with invalid number of valid characters (1 mod 4) |
| 208 |
def assertInvalidLength(data): |
| 209 |
n_data_chars = len(re.sub(br'[^A-Za-z0-9/+]', br'', data)) |
| 210 |
expected_errmsg_re = \ |
| 211 |
r'(?i)Invalid.+number of data characters.+' + str(n_data_chars) |
| 212 |
with self.assertRaisesRegex(binascii.Error, expected_errmsg_re): |
| 213 |
binascii.a2b_base64(self.type2test(data)) |
| 214 |
|
| 215 |
assertInvalidLength(b'a') |
| 216 |
assertInvalidLength(b'a=') |
| 217 |
assertInvalidLength(b'a==') |
| 218 |
assertInvalidLength(b'a===') |
| 219 |
assertInvalidLength(b'a' * 5) |
| 220 |
assertInvalidLength(b'a' * (4 * 87 + 1)) |
| 221 |
assertInvalidLength(b'A\tB\nC ??DE') # only 5 valid characters |
| 222 |
|
| 223 |
def test_uu(self): |
| 224 |
MAX_UU = 45 |
| 225 |
for backtick in (True, False): |
| 226 |
lines = [] |
| 227 |
for i in range(0, len(self.data), MAX_UU): |
| 228 |
b = self.type2test(self.rawdata[i:i+MAX_UU]) |
| 229 |
a = binascii.b2a_uu(b, backtick=backtick) |
| 230 |
lines.append(a) |
| 231 |
res = bytes() |
| 232 |
for line in lines: |
| 233 |
a = self.type2test(line) |
| 234 |
b = binascii.a2b_uu(a) |
| 235 |
res += b |
| 236 |
self.assertEqual(res, self.rawdata) |
| 237 |
|
| 238 |
self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31) |
| 239 |
self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32) |
| 240 |
self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31) |
| 241 |
self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") |
| 242 |
self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") |
| 243 |
self.assertRaises(binascii.Error, binascii.a2b_uu, |
| 244 |
self.type2test(b"")) |
| 245 |
self.assertRaises(binascii.Error, binascii.a2b_uu, |
| 246 |
self.type2test(b"#86)C")[:0]) |
| 247 |
self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!") |
| 248 |
|
| 249 |
# Issue #7701 (crash on a pydebug build) |
| 250 |
self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n') |
| 251 |
|
| 252 |
self.assertEqual(binascii.b2a_uu(b''), b' \n') |
| 253 |
self.assertEqual(binascii.b2a_uu(b'', backtick=True), b'`\n') |
| 254 |
self.assertEqual(binascii.a2b_uu(b' \n'), b'') |
| 255 |
self.assertEqual(binascii.a2b_uu(b'`\n'), b'') |
| 256 |
self.assertEqual(binascii.b2a_uu(b'\x00Cat'), b'$ $-A= \n') |
| 257 |
self.assertEqual(binascii.b2a_uu(b'\x00Cat', backtick=True), |
| 258 |
b'$`$-A=```\n') |
| 259 |
self.assertEqual(binascii.a2b_uu(b'$`$-A=```\n'), |
| 260 |
binascii.a2b_uu(b'$ $-A= \n')) |
| 261 |
with self.assertRaises(TypeError): |
| 262 |
binascii.b2a_uu(b"", True) |
| 263 |
|
| 264 |
@hypothesis.given( |
| 265 |
binary=hypothesis.strategies.binary(max_size=45), |
| 266 |
backtick=hypothesis.strategies.booleans(), |
| 267 |
) |
| 268 |
def test_b2a_roundtrip(self, binary, backtick): |
| 269 |
converted = binascii.b2a_uu(self.type2test(binary), backtick=backtick) |
| 270 |
restored = binascii.a2b_uu(self.type2test(converted)) |
| 271 |
self.assertConversion(binary, converted, restored, backtick=backtick) |
| 272 |
|
| 273 |
def test_crc_hqx(self): |
| 274 |
crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0) |
| 275 |
crc = binascii.crc_hqx(self.type2test(b" this string."), crc) |
| 276 |
self.assertEqual(crc, 14290) |
| 277 |
|
| 278 |
self.assertRaises(TypeError, binascii.crc_hqx) |
| 279 |
self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b'')) |
| 280 |
|
| 281 |
for crc in 0, 1, 0x1234, 0x12345, 0x12345678, -1: |
| 282 |
self.assertEqual(binascii.crc_hqx(self.type2test(b''), crc), |
| 283 |
crc & 0xffff) |
| 284 |
|
| 285 |
def test_crc32(self): |
| 286 |
crc = binascii.crc32(self.type2test(b"Test the CRC-32 of")) |
| 287 |
crc = binascii.crc32(self.type2test(b" this string."), crc) |
| 288 |
self.assertEqual(crc, 1571220330) |
| 289 |
|
| 290 |
self.assertRaises(TypeError, binascii.crc32) |
| 291 |
|
| 292 |
def test_hex(self): |
| 293 |
# test hexlification |
| 294 |
s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' |
| 295 |
t = binascii.b2a_hex(self.type2test(s)) |
| 296 |
u = binascii.a2b_hex(self.type2test(t)) |
| 297 |
self.assertEqual(s, u) |
| 298 |
self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1]) |
| 299 |
self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q') |
| 300 |
self.assertRaises(binascii.Error, binascii.a2b_hex, bytes([255, 255])) |
| 301 |
self.assertRaises(binascii.Error, binascii.a2b_hex, b'0G') |
| 302 |
self.assertRaises(binascii.Error, binascii.a2b_hex, b'0g') |
| 303 |
self.assertRaises(binascii.Error, binascii.a2b_hex, b'G0') |
| 304 |
self.assertRaises(binascii.Error, binascii.a2b_hex, b'g0') |
| 305 |
|
| 306 |
# Confirm that b2a_hex == hexlify and a2b_hex == unhexlify |
| 307 |
self.assertEqual(binascii.hexlify(self.type2test(s)), t) |
| 308 |
self.assertEqual(binascii.unhexlify(self.type2test(t)), u) |
| 309 |
|
| 310 |
@hypothesis.given(binary=hypothesis.strategies.binary()) |
| 311 |
def test_hex_roundtrip(self, binary): |
| 312 |
converted = binascii.hexlify(self.type2test(binary)) |
| 313 |
restored = binascii.unhexlify(self.type2test(converted)) |
| 314 |
self.assertConversion(binary, converted, restored) |
| 315 |
|
| 316 |
def test_hex_separator(self): |
| 317 |
"""Test that hexlify and b2a_hex are binary versions of bytes.hex.""" |
| 318 |
# Logic of separators is tested in test_bytes.py. This checks that |
| 319 |
# arg parsing works and exercises the direct to bytes object code |
| 320 |
# path within pystrhex.c. |
| 321 |
s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' |
| 322 |
self.assertEqual(binascii.hexlify(self.type2test(s)), s.hex().encode('ascii')) |
| 323 |
expected8 = s.hex('.', 8).encode('ascii') |
| 324 |
self.assertEqual(binascii.hexlify(self.type2test(s), '.', 8), expected8) |
| 325 |
expected1 = s.hex(':').encode('ascii') |
| 326 |
self.assertEqual(binascii.b2a_hex(self.type2test(s), ':'), expected1) |
| 327 |
|
| 328 |
def test_qp(self): |
| 329 |
type2test = self.type2test |
| 330 |
a2b_qp = binascii.a2b_qp |
| 331 |
b2a_qp = binascii.b2a_qp |
| 332 |
|
| 333 |
a2b_qp(data=b"", header=False) # Keyword arguments allowed |
| 334 |
|
| 335 |
# A test for SF bug 534347 (segfaults without the proper fix) |
| 336 |
try: |
| 337 |
a2b_qp(b"", **{1:1}) |
| 338 |
except TypeError: |
| 339 |
pass |
| 340 |
else: |
| 341 |
self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError") |
| 342 |
|
| 343 |
self.assertEqual(a2b_qp(type2test(b"=")), b"") |
| 344 |
self.assertEqual(a2b_qp(type2test(b"= ")), b"= ") |
| 345 |
self.assertEqual(a2b_qp(type2test(b"==")), b"=") |
| 346 |
self.assertEqual(a2b_qp(type2test(b"=\nAB")), b"AB") |
| 347 |
self.assertEqual(a2b_qp(type2test(b"=\r\nAB")), b"AB") |
| 348 |
self.assertEqual(a2b_qp(type2test(b"=\rAB")), b"") # ? |
| 349 |
self.assertEqual(a2b_qp(type2test(b"=\rAB\nCD")), b"CD") # ? |
| 350 |
self.assertEqual(a2b_qp(type2test(b"=AB")), b"\xab") |
| 351 |
self.assertEqual(a2b_qp(type2test(b"=ab")), b"\xab") |
| 352 |
self.assertEqual(a2b_qp(type2test(b"=AX")), b"=AX") |
| 353 |
self.assertEqual(a2b_qp(type2test(b"=XA")), b"=XA") |
| 354 |
self.assertEqual(a2b_qp(type2test(b"=AB")[:-1]), b"=A") |
| 355 |
|
| 356 |
self.assertEqual(a2b_qp(type2test(b'_')), b'_') |
| 357 |
self.assertEqual(a2b_qp(type2test(b'_'), header=True), b' ') |
| 358 |
|
| 359 |
self.assertRaises(TypeError, b2a_qp, foo="bar") |
| 360 |
self.assertEqual(a2b_qp(type2test(b"=00\r\n=00")), b"\x00\r\n\x00") |
| 361 |
self.assertEqual(b2a_qp(type2test(b"\xff\r\n\xff\n\xff")), |
| 362 |
b"=FF\r\n=FF\r\n=FF") |
| 363 |
self.assertEqual(b2a_qp(type2test(b"0"*75+b"\xff\r\n\xff\r\n\xff")), |
| 364 |
b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF") |
| 365 |
|
| 366 |
self.assertEqual(b2a_qp(type2test(b'\x7f')), b'=7F') |
| 367 |
self.assertEqual(b2a_qp(type2test(b'=')), b'=3D') |
| 368 |
|
| 369 |
self.assertEqual(b2a_qp(type2test(b'_')), b'_') |
| 370 |
self.assertEqual(b2a_qp(type2test(b'_'), header=True), b'=5F') |
| 371 |
self.assertEqual(b2a_qp(type2test(b'x y'), header=True), b'x_y') |
| 372 |
self.assertEqual(b2a_qp(type2test(b'x '), header=True), b'x=20') |
| 373 |
self.assertEqual(b2a_qp(type2test(b'x y'), header=True, quotetabs=True), |
| 374 |
b'x=20y') |
| 375 |
self.assertEqual(b2a_qp(type2test(b'x\ty'), header=True), b'x\ty') |
| 376 |
|
| 377 |
self.assertEqual(b2a_qp(type2test(b' ')), b'=20') |
| 378 |
self.assertEqual(b2a_qp(type2test(b'\t')), b'=09') |
| 379 |
self.assertEqual(b2a_qp(type2test(b' x')), b' x') |
| 380 |
self.assertEqual(b2a_qp(type2test(b'\tx')), b'\tx') |
| 381 |
self.assertEqual(b2a_qp(type2test(b' x')[:-1]), b'=20') |
| 382 |
self.assertEqual(b2a_qp(type2test(b'\tx')[:-1]), b'=09') |
| 383 |
self.assertEqual(b2a_qp(type2test(b'\0')), b'=00') |
| 384 |
|
| 385 |
self.assertEqual(b2a_qp(type2test(b'\0\n')), b'=00\n') |
| 386 |
self.assertEqual(b2a_qp(type2test(b'\0\n'), quotetabs=True), b'=00\n') |
| 387 |
|
| 388 |
self.assertEqual(b2a_qp(type2test(b'x y\tz')), b'x y\tz') |
| 389 |
self.assertEqual(b2a_qp(type2test(b'x y\tz'), quotetabs=True), |
| 390 |
b'x=20y=09z') |
| 391 |
self.assertEqual(b2a_qp(type2test(b'x y\tz'), istext=False), |
| 392 |
b'x y\tz') |
| 393 |
self.assertEqual(b2a_qp(type2test(b'x \ny\t\n')), |
| 394 |
b'x=20\ny=09\n') |
| 395 |
self.assertEqual(b2a_qp(type2test(b'x \ny\t\n'), quotetabs=True), |
| 396 |
b'x=20\ny=09\n') |
| 397 |
self.assertEqual(b2a_qp(type2test(b'x \ny\t\n'), istext=False), |
| 398 |
b'x =0Ay\t=0A') |
| 399 |
self.assertEqual(b2a_qp(type2test(b'x \ry\t\r')), |
| 400 |
b'x \ry\t\r') |
| 401 |
self.assertEqual(b2a_qp(type2test(b'x \ry\t\r'), quotetabs=True), |
| 402 |
b'x=20\ry=09\r') |
| 403 |
self.assertEqual(b2a_qp(type2test(b'x \ry\t\r'), istext=False), |
| 404 |
b'x =0Dy\t=0D') |
| 405 |
self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n')), |
| 406 |
b'x=20\r\ny=09\r\n') |
| 407 |
self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n'), quotetabs=True), |
| 408 |
b'x=20\r\ny=09\r\n') |
| 409 |
self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n'), istext=False), |
| 410 |
b'x =0D=0Ay\t=0D=0A') |
| 411 |
|
| 412 |
self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1]), b'x \r') |
| 413 |
self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1]), b'x\t\r') |
| 414 |
self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1], quotetabs=True), |
| 415 |
b'x=20\r') |
| 416 |
self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1], quotetabs=True), |
| 417 |
b'x=09\r') |
| 418 |
self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1], istext=False), |
| 419 |
b'x =0D') |
| 420 |
self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1], istext=False), |
| 421 |
b'x\t=0D') |
| 422 |
|
| 423 |
self.assertEqual(b2a_qp(type2test(b'.')), b'=2E') |
| 424 |
self.assertEqual(b2a_qp(type2test(b'.\n')), b'=2E\n') |
| 425 |
self.assertEqual(b2a_qp(type2test(b'.\r')), b'=2E\r') |
| 426 |
self.assertEqual(b2a_qp(type2test(b'.\0')), b'=2E=00') |
| 427 |
self.assertEqual(b2a_qp(type2test(b'a.\n')), b'a.\n') |
| 428 |
self.assertEqual(b2a_qp(type2test(b'.a')[:-1]), b'=2E') |
| 429 |
|
| 430 |
@hypothesis.given( |
| 431 |
binary=hypothesis.strategies.binary(), |
| 432 |
quotetabs=hypothesis.strategies.booleans(), |
| 433 |
istext=hypothesis.strategies.booleans(), |
| 434 |
header=hypothesis.strategies.booleans(), |
| 435 |
) |
| 436 |
def test_b2a_qp_a2b_qp_round_trip(self, binary, quotetabs, istext, header): |
| 437 |
converted = binascii.b2a_qp( |
| 438 |
self.type2test(binary), |
| 439 |
quotetabs=quotetabs, istext=istext, header=header, |
| 440 |
) |
| 441 |
restored = binascii.a2b_qp(self.type2test(converted), header=header) |
| 442 |
self.assertConversion(binary, converted, restored, |
| 443 |
quotetabs=quotetabs, istext=istext, header=header) |
| 444 |
|
| 445 |
def test_empty_string(self): |
| 446 |
# A test for SF bug #1022953. Make sure SystemError is not raised. |
| 447 |
empty = self.type2test(b'') |
| 448 |
for func in all_functions: |
| 449 |
if func == 'crc_hqx': |
| 450 |
# crc_hqx needs 2 arguments |
| 451 |
binascii.crc_hqx(empty, 0) |
| 452 |
continue |
| 453 |
f = getattr(binascii, func) |
| 454 |
if func == 'a2b_uu': |
| 455 |
self.assertRaises(binascii.Error, f, empty) |
| 456 |
continue |
| 457 |
try: |
| 458 |
f(empty) |
| 459 |
except Exception as err: |
| 460 |
self.fail("{}({!r}) raises {!r}".format(func, empty, err)) |
| 461 |
|
| 462 |
def test_unicode_b2a(self): |
| 463 |
# Unicode strings are not accepted by b2a_* functions. |
| 464 |
for func in set(all_functions) - set(a2b_functions): |
| 465 |
try: |
| 466 |
self.assertRaises(TypeError, getattr(binascii, func), "test") |
| 467 |
except Exception as err: |
| 468 |
self.fail('{}("test") raises {!r}'.format(func, err)) |
| 469 |
# crc_hqx needs 2 arguments |
| 470 |
self.assertRaises(TypeError, binascii.crc_hqx, "test", 0) |
| 471 |
|
| 472 |
def test_unicode_a2b(self): |
| 473 |
# Unicode strings are accepted by a2b_* functions. |
| 474 |
MAX_ALL = 45 |
| 475 |
raw = self.rawdata[:MAX_ALL] |
| 476 |
for fa, fb in zip(a2b_functions, b2a_functions): |
| 477 |
a2b = getattr(binascii, fa) |
| 478 |
b2a = getattr(binascii, fb) |
| 479 |
try: |
| 480 |
a = b2a(self.type2test(raw)) |
| 481 |
binary_res = a2b(a) |
| 482 |
a = a.decode('ascii') |
| 483 |
res = a2b(a) |
| 484 |
except Exception as err: |
| 485 |
self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) |
| 486 |
self.assertEqual(res, raw, "{}/{} conversion: " |
| 487 |
"{!r} != {!r}".format(fb, fa, res, raw)) |
| 488 |
self.assertEqual(res, binary_res) |
| 489 |
self.assertIsInstance(res, bytes) |
| 490 |
# non-ASCII string |
| 491 |
self.assertRaises(ValueError, a2b, "\x80") |
| 492 |
|
| 493 |
def test_b2a_base64_newline(self): |
| 494 |
# Issue #25357: test newline parameter |
| 495 |
b = self.type2test(b'hello') |
| 496 |
self.assertEqual(binascii.b2a_base64(b), |
| 497 |
b'aGVsbG8=\n') |
| 498 |
self.assertEqual(binascii.b2a_base64(b, newline=True), |
| 499 |
b'aGVsbG8=\n') |
| 500 |
self.assertEqual(binascii.b2a_base64(b, newline=False), |
| 501 |
b'aGVsbG8=') |
| 502 |
|
| 503 |
@hypothesis.given( |
| 504 |
binary=hypothesis.strategies.binary(), |
| 505 |
newline=hypothesis.strategies.booleans(), |
| 506 |
) |
| 507 |
def test_base64_roundtrip(self, binary, newline): |
| 508 |
converted = binascii.b2a_base64(self.type2test(binary), newline=newline) |
| 509 |
restored = binascii.a2b_base64(self.type2test(converted)) |
| 510 |
self.assertConversion(binary, converted, restored, newline=newline) |
| 511 |
|
| 512 |
def test_c_contiguity(self): |
| 513 |
m = memoryview(bytearray(b'noncontig')) |
| 514 |
noncontig_writable = m[::-2] |
| 515 |
with self.assertRaises(BufferError): |
| 516 |
binascii.b2a_hex(noncontig_writable) |
| 517 |
|
| 518 |
|
| 519 |
class ArrayBinASCIITest(BinASCIITest): |
| 520 |
def type2test(self, s): |
| 521 |
return array.array('B', list(s)) |
| 522 |
|
| 523 |
|
| 524 |
class BytearrayBinASCIITest(BinASCIITest): |
| 525 |
type2test = bytearray |
| 526 |
|
| 527 |
|
| 528 |
class MemoryviewBinASCIITest(BinASCIITest): |
| 529 |
type2test = memoryview |
| 530 |
|
| 531 |
class ChecksumBigBufferTestCase(unittest.TestCase): |
| 532 |
"""bpo-38256 - check that inputs >=4 GiB are handled correctly.""" |
| 533 |
|
| 534 |
@bigmemtest(size=_4G + 4, memuse=1, dry_run=False) |
| 535 |
def test_big_buffer(self, size): |
| 536 |
data = b"nyan" * (_1G + 1) |
| 537 |
self.assertEqual(binascii.crc32(data), 1044521549) |
| 538 |
|
| 539 |
|
| 540 |
if __name__ == "__main__": |
| 541 |
unittest.main() |