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

Python code coverage for Lib/test/test_quopri.py

#countcontent
1n/aimport unittest
2n/a
3n/aimport sys, io, subprocess
4n/aimport quopri
5n/a
6n/a
7n/a
8n/aENCSAMPLE = b"""\
9n/aHere's a bunch of special=20
10n/a
11n/a=A1=A2=A3=A4=A5=A6=A7=A8=A9
12n/a=AA=AB=AC=AD=AE=AF=B0=B1=B2=B3
13n/a=B4=B5=B6=B7=B8=B9=BA=BB=BC=BD=BE
14n/a=BF=C0=C1=C2=C3=C4=C5=C6
15n/a=C7=C8=C9=CA=CB=CC=CD=CE=CF
16n/a=D0=D1=D2=D3=D4=D5=D6=D7
17n/a=D8=D9=DA=DB=DC=DD=DE=DF
18n/a=E0=E1=E2=E3=E4=E5=E6=E7
19n/a=E8=E9=EA=EB=EC=ED=EE=EF
20n/a=F0=F1=F2=F3=F4=F5=F6=F7
21n/a=F8=F9=FA=FB=FC=FD=FE=FF
22n/a
23n/acharacters... have fun!
24n/a"""
25n/a
26n/a# First line ends with a space
27n/aDECSAMPLE = b"Here's a bunch of special \n" + \
28n/ab"""\
29n/a
30n/a\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9
31n/a\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3
32n/a\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe
33n/a\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6
34n/a\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf
35n/a\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7
36n/a\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf
37n/a\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7
38n/a\xe8\xe9\xea\xeb\xec\xed\xee\xef
39n/a\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7
40n/a\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff
41n/a
42n/acharacters... have fun!
43n/a"""
44n/a
45n/a
46n/adef withpythonimplementation(testfunc):
47n/a def newtest(self):
48n/a # Test default implementation
49n/a testfunc(self)
50n/a # Test Python implementation
51n/a if quopri.b2a_qp is not None or quopri.a2b_qp is not None:
52n/a oldencode = quopri.b2a_qp
53n/a olddecode = quopri.a2b_qp
54n/a try:
55n/a quopri.b2a_qp = None
56n/a quopri.a2b_qp = None
57n/a testfunc(self)
58n/a finally:
59n/a quopri.b2a_qp = oldencode
60n/a quopri.a2b_qp = olddecode
61n/a newtest.__name__ = testfunc.__name__
62n/a return newtest
63n/a
64n/aclass QuopriTestCase(unittest.TestCase):
65n/a # Each entry is a tuple of (plaintext, encoded string). These strings are
66n/a # used in the "quotetabs=0" tests.
67n/a STRINGS = (
68n/a # Some normal strings
69n/a (b'hello', b'hello'),
70n/a (b'''hello
71n/a there
72n/a world''', b'''hello
73n/a there
74n/a world'''),
75n/a (b'''hello
76n/a there
77n/a world
78n/a''', b'''hello
79n/a there
80n/a world
81n/a'''),
82n/a (b'\201\202\203', b'=81=82=83'),
83n/a # Add some trailing MUST QUOTE strings
84n/a (b'hello ', b'hello=20'),
85n/a (b'hello\t', b'hello=09'),
86n/a # Some long lines. First, a single line of 108 characters
87n/a (b'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\xd8\xd9\xda\xdb\xdc\xdd\xde\xdfxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
88n/a b'''xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=D8=D9=DA=DB=DC=DD=DE=DFx=
89n/axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'''),
90n/a # A line of exactly 76 characters, no soft line break should be needed
91n/a (b'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy',
92n/a b'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'),
93n/a # A line of 77 characters, forcing a soft line break at position 75,
94n/a # and a second line of exactly 2 characters (because the soft line
95n/a # break `=' sign counts against the line length limit).
96n/a (b'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz',
97n/a b'''zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz=
98n/azz'''),
99n/a # A line of 151 characters, forcing a soft line break at position 75,
100n/a # with a second line of exactly 76 characters and no trailing =
101n/a (b'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz',
102n/a b'''zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz=
103n/azzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'''),
104n/a # A string containing a hard line break, but which the first line is
105n/a # 151 characters and the second line is exactly 76 characters. This
106n/a # should leave us with three lines, the first which has a soft line
107n/a # break, and which the second and third do not.
108n/a (b'''yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
109n/azzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz''',
110n/a b'''yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy=
111n/ayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
112n/azzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'''),
113n/a # Now some really complex stuff ;)
114n/a (DECSAMPLE, ENCSAMPLE),
115n/a )
116n/a
117n/a # These are used in the "quotetabs=1" tests.
118n/a ESTRINGS = (
119n/a (b'hello world', b'hello=20world'),
120n/a (b'hello\tworld', b'hello=09world'),
121n/a )
122n/a
123n/a # These are used in the "header=1" tests.
124n/a HSTRINGS = (
125n/a (b'hello world', b'hello_world'),
126n/a (b'hello_world', b'hello=5Fworld'),
127n/a )
128n/a
129n/a @withpythonimplementation
130n/a def test_encodestring(self):
131n/a for p, e in self.STRINGS:
132n/a self.assertEqual(quopri.encodestring(p), e)
133n/a
134n/a @withpythonimplementation
135n/a def test_decodestring(self):
136n/a for p, e in self.STRINGS:
137n/a self.assertEqual(quopri.decodestring(e), p)
138n/a
139n/a @withpythonimplementation
140n/a def test_decodestring_double_equals(self):
141n/a # Issue 21511 - Ensure that byte string is compared to byte string
142n/a # instead of int byte value
143n/a decoded_value, encoded_value = (b"123=four", b"123==four")
144n/a self.assertEqual(quopri.decodestring(encoded_value), decoded_value)
145n/a
146n/a @withpythonimplementation
147n/a def test_idempotent_string(self):
148n/a for p, e in self.STRINGS:
149n/a self.assertEqual(quopri.decodestring(quopri.encodestring(e)), e)
150n/a
151n/a @withpythonimplementation
152n/a def test_encode(self):
153n/a for p, e in self.STRINGS:
154n/a infp = io.BytesIO(p)
155n/a outfp = io.BytesIO()
156n/a quopri.encode(infp, outfp, quotetabs=False)
157n/a self.assertEqual(outfp.getvalue(), e)
158n/a
159n/a @withpythonimplementation
160n/a def test_decode(self):
161n/a for p, e in self.STRINGS:
162n/a infp = io.BytesIO(e)
163n/a outfp = io.BytesIO()
164n/a quopri.decode(infp, outfp)
165n/a self.assertEqual(outfp.getvalue(), p)
166n/a
167n/a @withpythonimplementation
168n/a def test_embedded_ws(self):
169n/a for p, e in self.ESTRINGS:
170n/a self.assertEqual(quopri.encodestring(p, quotetabs=True), e)
171n/a self.assertEqual(quopri.decodestring(e), p)
172n/a
173n/a @withpythonimplementation
174n/a def test_encode_header(self):
175n/a for p, e in self.HSTRINGS:
176n/a self.assertEqual(quopri.encodestring(p, header=True), e)
177n/a
178n/a @withpythonimplementation
179n/a def test_decode_header(self):
180n/a for p, e in self.HSTRINGS:
181n/a self.assertEqual(quopri.decodestring(e, header=True), p)
182n/a
183n/a def test_scriptencode(self):
184n/a (p, e) = self.STRINGS[-1]
185n/a process = subprocess.Popen([sys.executable, "-mquopri"],
186n/a stdin=subprocess.PIPE, stdout=subprocess.PIPE)
187n/a self.addCleanup(process.stdout.close)
188n/a cout, cerr = process.communicate(p)
189n/a # On Windows, Python will output the result to stdout using
190n/a # CRLF, as the mode of stdout is text mode. To compare this
191n/a # with the expected result, we need to do a line-by-line comparison.
192n/a cout = cout.decode('latin-1').splitlines()
193n/a e = e.decode('latin-1').splitlines()
194n/a assert len(cout)==len(e)
195n/a for i in range(len(cout)):
196n/a self.assertEqual(cout[i], e[i])
197n/a self.assertEqual(cout, e)
198n/a
199n/a def test_scriptdecode(self):
200n/a (p, e) = self.STRINGS[-1]
201n/a process = subprocess.Popen([sys.executable, "-mquopri", "-d"],
202n/a stdin=subprocess.PIPE, stdout=subprocess.PIPE)
203n/a self.addCleanup(process.stdout.close)
204n/a cout, cerr = process.communicate(e)
205n/a cout = cout.decode('latin-1')
206n/a p = p.decode('latin-1')
207n/a self.assertEqual(cout.splitlines(), p.splitlines())
208n/a
209n/aif __name__ == "__main__":
210n/a unittest.main()