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