ยปCore Development>Code coverage>Lib/test/test_binascii.py

Python code coverage for Lib/test/test_binascii.py

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