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