Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 1 | """Test the binascii C module.""" |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 2 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 3 | import unittest |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 4 | import binascii |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 5 | import array |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 6 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 7 | # Note: "*_hex" functions are aliases for "(un)hexlify" |
| 8 | b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu', |
| 9 | 'hexlify', 'rlecode_hqx'] |
| 10 | a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu', |
| 11 | 'unhexlify', 'rledecode_hqx'] |
| 12 | all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx'] |
| 13 | |
| 14 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 15 | class BinASCIITest(unittest.TestCase): |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 16 | |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 17 | type2test = bytes |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 18 | # Create binary test data |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 19 | rawdata = b"The quick brown fox jumps over the lazy dog.\r\n" |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 20 | # Be slow so we don't depend on other modules |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 21 | rawdata += bytes(range(256)) |
| 22 | rawdata += b"\r\nHello world.\n" |
| 23 | |
| 24 | def setUp(self): |
| 25 | self.data = self.type2test(self.rawdata) |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 26 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 27 | def test_exceptions(self): |
| 28 | # Check module exceptions |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 29 | self.assertTrue(issubclass(binascii.Error, Exception)) |
| 30 | self.assertTrue(issubclass(binascii.Incomplete, Exception)) |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 31 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 32 | def test_functions(self): |
| 33 | # Check presence of all functions |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 34 | for name in all_functions: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 35 | self.assertTrue(hasattr(getattr(binascii, name), '__call__')) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 36 | self.assertRaises(TypeError, getattr(binascii, name)) |
| 37 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 38 | def test_returned_value(self): |
| 39 | # Limit to the minimum of all limits (b2a_uu) |
| 40 | MAX_ALL = 45 |
| 41 | raw = self.rawdata[:MAX_ALL] |
| 42 | for fa, fb in zip(a2b_functions, b2a_functions): |
| 43 | a2b = getattr(binascii, fa) |
| 44 | b2a = getattr(binascii, fb) |
| 45 | try: |
| 46 | a = b2a(self.type2test(raw)) |
| 47 | res = a2b(self.type2test(a)) |
| 48 | except Exception as err: |
| 49 | self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) |
| 50 | if fb == 'b2a_hqx': |
| 51 | # b2a_hqx returns a tuple |
| 52 | res, _ = res |
| 53 | self.assertEqual(res, raw, "{}/{} conversion: " |
| 54 | "{!r} != {!r}".format(fb, fa, res, raw)) |
| 55 | self.assertIsInstance(res, bytes) |
| 56 | self.assertIsInstance(a, bytes) |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 57 | self.assertLess(max(a), 128) |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 58 | self.assertIsInstance(binascii.crc_hqx(raw, 0), int) |
| 59 | self.assertIsInstance(binascii.crc32(raw), int) |
| 60 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 61 | def test_base64valid(self): |
| 62 | # Test base64 with valid data |
| 63 | MAX_BASE64 = 57 |
| 64 | lines = [] |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 65 | for i in range(0, len(self.rawdata), MAX_BASE64): |
| 66 | b = self.type2test(self.rawdata[i:i+MAX_BASE64]) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 67 | a = binascii.b2a_base64(b) |
| 68 | lines.append(a) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 69 | res = bytes() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 70 | for line in lines: |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 71 | a = self.type2test(line) |
| 72 | b = binascii.a2b_base64(a) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 73 | res += b |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 74 | self.assertEqual(res, self.rawdata) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 75 | |
| 76 | def test_base64invalid(self): |
| 77 | # Test base64 with random invalid characters sprinkled throughout |
| 78 | # (This requires a new version of binascii.) |
| 79 | MAX_BASE64 = 57 |
| 80 | lines = [] |
| 81 | for i in range(0, len(self.data), MAX_BASE64): |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 82 | b = self.type2test(self.rawdata[i:i+MAX_BASE64]) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 83 | a = binascii.b2a_base64(b) |
| 84 | lines.append(a) |
| 85 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 86 | fillers = bytearray() |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 87 | valid = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 88 | for i in range(256): |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 89 | if i not in valid: |
| 90 | fillers.append(i) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 91 | def addnoise(line): |
| 92 | noise = fillers |
| 93 | ratio = len(line) // len(noise) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 94 | res = bytearray() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 95 | while line and noise: |
| 96 | if len(line) // len(noise) > ratio: |
| 97 | c, line = line[0], line[1:] |
| 98 | else: |
| 99 | c, noise = noise[0], noise[1:] |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 100 | res.append(c) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 101 | return res + noise + line |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 102 | res = bytearray() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 103 | for line in map(addnoise, lines): |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 104 | a = self.type2test(line) |
| 105 | b = binascii.a2b_base64(a) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 106 | res += b |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 107 | self.assertEqual(res, self.rawdata) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 108 | |
| 109 | # Test base64 with just invalid characters, which should return |
| 110 | # empty strings. TBD: shouldn't it raise an exception instead ? |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 111 | self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'') |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 112 | |
| 113 | def test_uu(self): |
| 114 | MAX_UU = 45 |
Xiang Zhang | 13f1f42 | 2017-05-03 11:16:21 +0800 | [diff] [blame] | 115 | for backtick in (True, False): |
| 116 | lines = [] |
| 117 | for i in range(0, len(self.data), MAX_UU): |
| 118 | b = self.type2test(self.rawdata[i:i+MAX_UU]) |
| 119 | a = binascii.b2a_uu(b, backtick=backtick) |
| 120 | lines.append(a) |
| 121 | res = bytes() |
| 122 | for line in lines: |
| 123 | a = self.type2test(line) |
| 124 | b = binascii.a2b_uu(a) |
| 125 | res += b |
| 126 | self.assertEqual(res, self.rawdata) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 128 | self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31) |
| 129 | self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32) |
| 130 | self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31) |
| 131 | self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") |
| 132 | self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 133 | self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!") |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | 2843aa8 | 2010-01-15 00:27:43 +0000 | [diff] [blame] | 135 | # Issue #7701 (crash on a pydebug build) |
| 136 | self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n') |
| 137 | |
Xiang Zhang | 13f1f42 | 2017-05-03 11:16:21 +0800 | [diff] [blame] | 138 | self.assertEqual(binascii.b2a_uu(b''), b' \n') |
| 139 | self.assertEqual(binascii.b2a_uu(b'', backtick=True), b'`\n') |
| 140 | self.assertEqual(binascii.a2b_uu(b' \n'), b'') |
| 141 | self.assertEqual(binascii.a2b_uu(b'`\n'), b'') |
| 142 | self.assertEqual(binascii.b2a_uu(b'\x00Cat'), b'$ $-A= \n') |
| 143 | self.assertEqual(binascii.b2a_uu(b'\x00Cat', backtick=True), |
| 144 | b'$`$-A=```\n') |
| 145 | self.assertEqual(binascii.a2b_uu(b'$`$-A=```\n'), |
| 146 | binascii.a2b_uu(b'$ $-A= \n')) |
| 147 | with self.assertRaises(TypeError): |
| 148 | binascii.b2a_uu(b"", True) |
| 149 | |
Serhiy Storchaka | 2ef7c47 | 2015-04-20 09:26:49 +0300 | [diff] [blame] | 150 | def test_crc_hqx(self): |
| 151 | crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0) |
| 152 | crc = binascii.crc_hqx(self.type2test(b" this string."), crc) |
| 153 | self.assertEqual(crc, 14290) |
| 154 | |
| 155 | self.assertRaises(TypeError, binascii.crc_hqx) |
| 156 | self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b'')) |
| 157 | |
| 158 | for crc in 0, 1, 0x1234, 0x12345, 0x12345678, -1: |
| 159 | self.assertEqual(binascii.crc_hqx(self.type2test(b''), crc), |
| 160 | crc & 0xffff) |
| 161 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 162 | def test_crc32(self): |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 163 | crc = binascii.crc32(self.type2test(b"Test the CRC-32 of")) |
| 164 | crc = binascii.crc32(self.type2test(b" this string."), crc) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 165 | self.assertEqual(crc, 1571220330) |
| 166 | |
| 167 | self.assertRaises(TypeError, binascii.crc32) |
| 168 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 169 | def test_hqx(self): |
| 170 | # Perform binhex4 style RLE-compression |
| 171 | # Then calculate the hexbin4 binary-to-ASCII translation |
| 172 | rle = binascii.rlecode_hqx(self.data) |
| 173 | a = binascii.b2a_hqx(self.type2test(rle)) |
Victor Stinner | 1bfe930 | 2015-10-14 15:02:35 +0200 | [diff] [blame] | 174 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 175 | b, _ = binascii.a2b_hqx(self.type2test(a)) |
| 176 | res = binascii.rledecode_hqx(b) |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 177 | self.assertEqual(res, self.rawdata) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 178 | |
Victor Stinner | 1bfe930 | 2015-10-14 15:02:35 +0200 | [diff] [blame] | 179 | def test_rle(self): |
| 180 | # test repetition with a repetition longer than the limit of 255 |
| 181 | data = (b'a' * 100 + b'b' + b'c' * 300) |
| 182 | |
| 183 | encoded = binascii.rlecode_hqx(data) |
| 184 | self.assertEqual(encoded, |
| 185 | (b'a\x90d' # 'a' * 100 |
| 186 | b'b' # 'b' |
| 187 | b'c\x90\xff' # 'c' * 255 |
| 188 | b'c\x90-')) # 'c' * 45 |
| 189 | |
| 190 | decoded = binascii.rledecode_hqx(encoded) |
| 191 | self.assertEqual(decoded, data) |
| 192 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 193 | def test_hex(self): |
| 194 | # test hexlification |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 195 | s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 196 | t = binascii.b2a_hex(self.type2test(s)) |
| 197 | u = binascii.a2b_hex(self.type2test(t)) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 198 | self.assertEqual(s, u) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 199 | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1]) |
| 200 | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q') |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 201 | |
Zachary Ware | b176d40 | 2015-01-20 13:59:46 -0600 | [diff] [blame] | 202 | # Confirm that b2a_hex == hexlify and a2b_hex == unhexlify |
| 203 | self.assertEqual(binascii.hexlify(self.type2test(s)), t) |
| 204 | self.assertEqual(binascii.unhexlify(self.type2test(t)), u) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 205 | |
| 206 | def test_qp(self): |
Serhiy Storchaka | e6265e9 | 2016-09-14 16:34:37 +0300 | [diff] [blame] | 207 | type2test = self.type2test |
| 208 | a2b_qp = binascii.a2b_qp |
| 209 | b2a_qp = binascii.b2a_qp |
| 210 | |
| 211 | a2b_qp(data=b"", header=False) # Keyword arguments allowed |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 212 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 213 | # A test for SF bug 534347 (segfaults without the proper fix) |
| 214 | try: |
Serhiy Storchaka | e6265e9 | 2016-09-14 16:34:37 +0300 | [diff] [blame] | 215 | a2b_qp(b"", **{1:1}) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 216 | except TypeError: |
| 217 | pass |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 218 | else: |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 219 | self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError") |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 220 | |
Serhiy Storchaka | e6265e9 | 2016-09-14 16:34:37 +0300 | [diff] [blame] | 221 | self.assertEqual(a2b_qp(type2test(b"=")), b"") |
| 222 | self.assertEqual(a2b_qp(type2test(b"= ")), b"= ") |
| 223 | self.assertEqual(a2b_qp(type2test(b"==")), b"=") |
| 224 | self.assertEqual(a2b_qp(type2test(b"=\nAB")), b"AB") |
| 225 | self.assertEqual(a2b_qp(type2test(b"=\r\nAB")), b"AB") |
| 226 | self.assertEqual(a2b_qp(type2test(b"=\rAB")), b"") # ? |
| 227 | self.assertEqual(a2b_qp(type2test(b"=\rAB\nCD")), b"CD") # ? |
| 228 | self.assertEqual(a2b_qp(type2test(b"=AB")), b"\xab") |
| 229 | self.assertEqual(a2b_qp(type2test(b"=ab")), b"\xab") |
| 230 | self.assertEqual(a2b_qp(type2test(b"=AX")), b"=AX") |
| 231 | self.assertEqual(a2b_qp(type2test(b"=XA")), b"=XA") |
| 232 | self.assertEqual(a2b_qp(type2test(b"=AB")[:-1]), b"=A") |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 233 | |
Serhiy Storchaka | e6265e9 | 2016-09-14 16:34:37 +0300 | [diff] [blame] | 234 | self.assertEqual(a2b_qp(type2test(b'_')), b'_') |
| 235 | self.assertEqual(a2b_qp(type2test(b'_'), header=True), b' ') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 236 | |
Serhiy Storchaka | e6265e9 | 2016-09-14 16:34:37 +0300 | [diff] [blame] | 237 | self.assertRaises(TypeError, b2a_qp, foo="bar") |
| 238 | self.assertEqual(a2b_qp(type2test(b"=00\r\n=00")), b"\x00\r\n\x00") |
| 239 | self.assertEqual(b2a_qp(type2test(b"\xff\r\n\xff\n\xff")), |
| 240 | b"=FF\r\n=FF\r\n=FF") |
| 241 | self.assertEqual(b2a_qp(type2test(b"0"*75+b"\xff\r\n\xff\r\n\xff")), |
| 242 | b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF") |
| 243 | |
| 244 | self.assertEqual(b2a_qp(type2test(b'\x7f')), b'=7F') |
| 245 | self.assertEqual(b2a_qp(type2test(b'=')), b'=3D') |
| 246 | |
| 247 | self.assertEqual(b2a_qp(type2test(b'_')), b'_') |
| 248 | self.assertEqual(b2a_qp(type2test(b'_'), header=True), b'=5F') |
| 249 | self.assertEqual(b2a_qp(type2test(b'x y'), header=True), b'x_y') |
| 250 | self.assertEqual(b2a_qp(type2test(b'x '), header=True), b'x=20') |
| 251 | self.assertEqual(b2a_qp(type2test(b'x y'), header=True, quotetabs=True), |
| 252 | b'x=20y') |
| 253 | self.assertEqual(b2a_qp(type2test(b'x\ty'), header=True), b'x\ty') |
| 254 | |
| 255 | self.assertEqual(b2a_qp(type2test(b' ')), b'=20') |
| 256 | self.assertEqual(b2a_qp(type2test(b'\t')), b'=09') |
| 257 | self.assertEqual(b2a_qp(type2test(b' x')), b' x') |
| 258 | self.assertEqual(b2a_qp(type2test(b'\tx')), b'\tx') |
| 259 | self.assertEqual(b2a_qp(type2test(b' x')[:-1]), b'=20') |
| 260 | self.assertEqual(b2a_qp(type2test(b'\tx')[:-1]), b'=09') |
| 261 | self.assertEqual(b2a_qp(type2test(b'\0')), b'=00') |
| 262 | |
| 263 | self.assertEqual(b2a_qp(type2test(b'\0\n')), b'=00\n') |
| 264 | self.assertEqual(b2a_qp(type2test(b'\0\n'), quotetabs=True), b'=00\n') |
| 265 | |
| 266 | self.assertEqual(b2a_qp(type2test(b'x y\tz')), b'x y\tz') |
| 267 | self.assertEqual(b2a_qp(type2test(b'x y\tz'), quotetabs=True), |
| 268 | b'x=20y=09z') |
| 269 | self.assertEqual(b2a_qp(type2test(b'x y\tz'), istext=False), |
| 270 | b'x y\tz') |
| 271 | self.assertEqual(b2a_qp(type2test(b'x \ny\t\n')), |
| 272 | b'x=20\ny=09\n') |
| 273 | self.assertEqual(b2a_qp(type2test(b'x \ny\t\n'), quotetabs=True), |
| 274 | b'x=20\ny=09\n') |
| 275 | self.assertEqual(b2a_qp(type2test(b'x \ny\t\n'), istext=False), |
| 276 | b'x =0Ay\t=0A') |
| 277 | self.assertEqual(b2a_qp(type2test(b'x \ry\t\r')), |
| 278 | b'x \ry\t\r') |
| 279 | self.assertEqual(b2a_qp(type2test(b'x \ry\t\r'), quotetabs=True), |
| 280 | b'x=20\ry=09\r') |
| 281 | self.assertEqual(b2a_qp(type2test(b'x \ry\t\r'), istext=False), |
| 282 | b'x =0Dy\t=0D') |
| 283 | self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n')), |
| 284 | b'x=20\r\ny=09\r\n') |
| 285 | self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n'), quotetabs=True), |
| 286 | b'x=20\r\ny=09\r\n') |
| 287 | self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n'), istext=False), |
| 288 | b'x =0D=0Ay\t=0D=0A') |
| 289 | |
| 290 | self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1]), b'x \r') |
| 291 | self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1]), b'x\t\r') |
| 292 | self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1], quotetabs=True), |
| 293 | b'x=20\r') |
| 294 | self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1], quotetabs=True), |
| 295 | b'x=09\r') |
| 296 | self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1], istext=False), |
| 297 | b'x =0D') |
| 298 | self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1], istext=False), |
| 299 | b'x\t=0D') |
| 300 | |
| 301 | self.assertEqual(b2a_qp(type2test(b'.')), b'=2E') |
| 302 | self.assertEqual(b2a_qp(type2test(b'.\n')), b'=2E\n') |
| 303 | self.assertEqual(b2a_qp(type2test(b'.\r')), b'=2E\r') |
| 304 | self.assertEqual(b2a_qp(type2test(b'.\0')), b'=2E=00') |
| 305 | self.assertEqual(b2a_qp(type2test(b'a.\n')), b'a.\n') |
| 306 | self.assertEqual(b2a_qp(type2test(b'.a')[:-1]), b'=2E') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 307 | |
Raymond Hettinger | 658717e | 2004-09-06 22:58:37 +0000 | [diff] [blame] | 308 | def test_empty_string(self): |
| 309 | # A test for SF bug #1022953. Make sure SystemError is not raised. |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 310 | empty = self.type2test(b'') |
| 311 | for func in all_functions: |
| 312 | if func == 'crc_hqx': |
| 313 | # crc_hqx needs 2 arguments |
| 314 | binascii.crc_hqx(empty, 0) |
| 315 | continue |
| 316 | f = getattr(binascii, func) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 317 | try: |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 318 | f(empty) |
| 319 | except Exception as err: |
| 320 | self.fail("{}({!r}) raises {!r}".format(func, empty, err)) |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | 0831676 | 2011-12-20 13:58:41 +0100 | [diff] [blame] | 322 | def test_unicode_b2a(self): |
| 323 | # Unicode strings are not accepted by b2a_* functions. |
| 324 | for func in set(all_functions) - set(a2b_functions) | {'rledecode_hqx'}: |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 325 | try: |
| 326 | self.assertRaises(TypeError, getattr(binascii, func), "test") |
| 327 | except Exception as err: |
| 328 | self.fail('{}("test") raises {!r}'.format(func, err)) |
| 329 | # crc_hqx needs 2 arguments |
| 330 | self.assertRaises(TypeError, binascii.crc_hqx, "test", 0) |
Raymond Hettinger | 658717e | 2004-09-06 22:58:37 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | 0831676 | 2011-12-20 13:58:41 +0100 | [diff] [blame] | 332 | def test_unicode_a2b(self): |
| 333 | # Unicode strings are accepted by a2b_* functions. |
| 334 | MAX_ALL = 45 |
| 335 | raw = self.rawdata[:MAX_ALL] |
| 336 | for fa, fb in zip(a2b_functions, b2a_functions): |
| 337 | if fa == 'rledecode_hqx': |
| 338 | # Takes non-ASCII data |
| 339 | continue |
| 340 | a2b = getattr(binascii, fa) |
| 341 | b2a = getattr(binascii, fb) |
| 342 | try: |
| 343 | a = b2a(self.type2test(raw)) |
| 344 | binary_res = a2b(a) |
| 345 | a = a.decode('ascii') |
| 346 | res = a2b(a) |
| 347 | except Exception as err: |
| 348 | self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) |
| 349 | if fb == 'b2a_hqx': |
| 350 | # b2a_hqx returns a tuple |
| 351 | res, _ = res |
| 352 | binary_res, _ = binary_res |
| 353 | self.assertEqual(res, raw, "{}/{} conversion: " |
| 354 | "{!r} != {!r}".format(fb, fa, res, raw)) |
| 355 | self.assertEqual(res, binary_res) |
| 356 | self.assertIsInstance(res, bytes) |
| 357 | # non-ASCII string |
| 358 | self.assertRaises(ValueError, a2b, "\x80") |
| 359 | |
Victor Stinner | e84c976 | 2015-10-11 11:01:02 +0200 | [diff] [blame] | 360 | def test_b2a_base64_newline(self): |
| 361 | # Issue #25357: test newline parameter |
| 362 | b = self.type2test(b'hello') |
| 363 | self.assertEqual(binascii.b2a_base64(b), |
| 364 | b'aGVsbG8=\n') |
| 365 | self.assertEqual(binascii.b2a_base64(b, newline=True), |
| 366 | b'aGVsbG8=\n') |
| 367 | self.assertEqual(binascii.b2a_base64(b, newline=False), |
| 368 | b'aGVsbG8=') |
| 369 | |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 370 | |
| 371 | class ArrayBinASCIITest(BinASCIITest): |
| 372 | def type2test(self, s): |
| 373 | return array.array('B', list(s)) |
| 374 | |
| 375 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 376 | class BytearrayBinASCIITest(BinASCIITest): |
| 377 | type2test = bytearray |
| 378 | |
| 379 | |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 380 | class MemoryviewBinASCIITest(BinASCIITest): |
| 381 | type2test = memoryview |
| 382 | |
| 383 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 384 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 385 | unittest.main() |