blob: 92511fac902fc10c9c7161be6a0bc266ca66951c [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test import support
Barry Warsaw70697632001-06-19 19:08:13 +00002import unittest
3
Martin v. Löwis48f4cf92007-07-28 17:58:14 +00004import sys, os, io, subprocess
Walter Dörwaldb1975432005-12-14 23:32:22 +00005import quopri
Barry Warsaw70697632001-06-19 19:08:13 +00006
7
Tim Peters87cc0c32001-07-21 01:41:30 +00008
Martin v. Löwisc582bfc2007-07-28 17:52:25 +00009ENCSAMPLE = b"""\
Barry Warsaw70697632001-06-19 19:08:13 +000010Here's a bunch of special=20
11
12=A1=A2=A3=A4=A5=A6=A7=A8=A9
13=AA=AB=AC=AD=AE=AF=B0=B1=B2=B3
14=B4=B5=B6=B7=B8=B9=BA=BB=BC=BD=BE
15=BF=C0=C1=C2=C3=C4=C5=C6
16=C7=C8=C9=CA=CB=CC=CD=CE=CF
17=D0=D1=D2=D3=D4=D5=D6=D7
18=D8=D9=DA=DB=DC=DD=DE=DF
19=E0=E1=E2=E3=E4=E5=E6=E7
20=E8=E9=EA=EB=EC=ED=EE=EF
21=F0=F1=F2=F3=F4=F5=F6=F7
22=F8=F9=FA=FB=FC=FD=FE=FF
23
24characters... have fun!
25"""
26
27# First line ends with a space
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000028DECSAMPLE = b"Here's a bunch of special \n" + \
29b"""\
Barry Warsaw70697632001-06-19 19:08:13 +000030
Tim Peterse8613652001-08-03 20:40:18 +000031\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9
32\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3
33\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe
34\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6
35\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf
36\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7
37\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf
38\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7
39\xe8\xe9\xea\xeb\xec\xed\xee\xef
40\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7
41\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff
Barry Warsaw70697632001-06-19 19:08:13 +000042
43characters... have fun!
44"""
45
46
Walter Dörwaldb1975432005-12-14 23:32:22 +000047def withpythonimplementation(testfunc):
48 def newtest(self):
49 # Test default implementation
50 testfunc(self)
51 # Test Python implementation
52 if quopri.b2a_qp is not None or quopri.a2b_qp is not None:
53 oldencode = quopri.b2a_qp
54 olddecode = quopri.a2b_qp
55 try:
56 quopri.b2a_qp = None
57 quopri.a2b_qp = None
58 testfunc(self)
59 finally:
60 quopri.b2a_qp = oldencode
61 quopri.a2b_qp = olddecode
62 newtest.__name__ = testfunc.__name__
63 return newtest
Tim Peters87cc0c32001-07-21 01:41:30 +000064
Barry Warsaw70697632001-06-19 19:08:13 +000065class QuopriTestCase(unittest.TestCase):
66 # Each entry is a tuple of (plaintext, encoded string). These strings are
67 # used in the "quotetabs=0" tests.
68 STRINGS = (
69 # Some normal strings
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000070 (b'hello', b'hello'),
71 (b'''hello
Barry Warsaw70697632001-06-19 19:08:13 +000072 there
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000073 world''', b'''hello
Barry Warsaw70697632001-06-19 19:08:13 +000074 there
75 world'''),
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000076 (b'''hello
Barry Warsaw70697632001-06-19 19:08:13 +000077 there
78 world
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000079''', b'''hello
Barry Warsaw70697632001-06-19 19:08:13 +000080 there
81 world
82'''),
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000083 (b'\201\202\203', b'=81=82=83'),
Barry Warsaw70697632001-06-19 19:08:13 +000084 # Add some trailing MUST QUOTE strings
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000085 (b'hello ', b'hello=20'),
86 (b'hello\t', b'hello=09'),
Barry Warsaw7599a3f2001-06-19 22:48:42 +000087 # Some long lines. First, a single line of 108 characters
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000088 (b'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\xd8\xd9\xda\xdb\xdc\xdd\xde\xdfxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
89 b'''xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=D8=D9=DA=DB=DC=DD=DE=DFx=
Barry Warsaw7599a3f2001-06-19 22:48:42 +000090xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'''),
91 # A line of exactly 76 characters, no soft line break should be needed
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000092 (b'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy',
93 b'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'),
Barry Warsaw7599a3f2001-06-19 22:48:42 +000094 # A line of 77 characters, forcing a soft line break at position 75,
95 # and a second line of exactly 2 characters (because the soft line
96 # break `=' sign counts against the line length limit).
Martin v. Löwisc582bfc2007-07-28 17:52:25 +000097 (b'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz',
98 b'''zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz=
Barry Warsaw7599a3f2001-06-19 22:48:42 +000099zz'''),
100 # A line of 151 characters, forcing a soft line break at position 75,
101 # with a second line of exactly 76 characters and no trailing =
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000102 (b'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz',
103 b'''zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz=
Barry Warsaw7599a3f2001-06-19 22:48:42 +0000104zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'''),
105 # A string containing a hard line break, but which the first line is
106 # 151 characters and the second line is exactly 76 characters. This
107 # should leave us with three lines, the first which has a soft line
108 # break, and which the second and third do not.
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000109 (b'''yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
Barry Warsaw7599a3f2001-06-19 22:48:42 +0000110zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz''',
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000111 b'''yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy=
Barry Warsaw7599a3f2001-06-19 22:48:42 +0000112yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
113zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'''),
114 # Now some really complex stuff ;)
Barry Warsaw70697632001-06-19 19:08:13 +0000115 (DECSAMPLE, ENCSAMPLE),
116 )
117
118 # These are used in the "quotetabs=1" tests.
119 ESTRINGS = (
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000120 (b'hello world', b'hello=20world'),
121 (b'hello\tworld', b'hello=09world'),
Barry Warsaw70697632001-06-19 19:08:13 +0000122 )
Tim Peters87cc0c32001-07-21 01:41:30 +0000123
Martin v. Löwis16dc7f42001-09-30 20:32:11 +0000124 # These are used in the "header=1" tests.
125 HSTRINGS = (
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000126 (b'hello world', b'hello_world'),
127 (b'hello_world', b'hello=5Fworld'),
Martin v. Löwis16dc7f42001-09-30 20:32:11 +0000128 )
129
Walter Dörwaldb1975432005-12-14 23:32:22 +0000130 @withpythonimplementation
Barry Warsaw70697632001-06-19 19:08:13 +0000131 def test_encodestring(self):
132 for p, e in self.STRINGS:
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000133 self.assertEqual(quopri.encodestring(p), e)
Tim Peters87cc0c32001-07-21 01:41:30 +0000134
Walter Dörwaldb1975432005-12-14 23:32:22 +0000135 @withpythonimplementation
Barry Warsaw70697632001-06-19 19:08:13 +0000136 def test_decodestring(self):
137 for p, e in self.STRINGS:
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000138 self.assertEqual(quopri.decodestring(e), p)
Tim Peters87cc0c32001-07-21 01:41:30 +0000139
Walter Dörwaldb1975432005-12-14 23:32:22 +0000140 @withpythonimplementation
Guido van Rossume173c072015-04-11 17:44:01 -0400141 def test_decodestring_double_equals(self):
142 # Issue 21511 - Ensure that byte string is compared to byte string
143 # instead of int byte value
144 decoded_value, encoded_value = (b"123=four", b"123==four")
145 self.assertEqual(quopri.decodestring(encoded_value), decoded_value)
146
147 @withpythonimplementation
Barry Warsaw70697632001-06-19 19:08:13 +0000148 def test_idempotent_string(self):
149 for p, e in self.STRINGS:
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000150 self.assertEqual(quopri.decodestring(quopri.encodestring(e)), e)
Barry Warsaw70697632001-06-19 19:08:13 +0000151
Walter Dörwaldb1975432005-12-14 23:32:22 +0000152 @withpythonimplementation
Barry Warsaw70697632001-06-19 19:08:13 +0000153 def test_encode(self):
154 for p, e in self.STRINGS:
Martin v. Löwis48f4cf92007-07-28 17:58:14 +0000155 infp = io.BytesIO(p)
156 outfp = io.BytesIO()
Walter Dörwaldb1975432005-12-14 23:32:22 +0000157 quopri.encode(infp, outfp, quotetabs=False)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000158 self.assertEqual(outfp.getvalue(), e)
Barry Warsaw70697632001-06-19 19:08:13 +0000159
Walter Dörwaldb1975432005-12-14 23:32:22 +0000160 @withpythonimplementation
Barry Warsaw70697632001-06-19 19:08:13 +0000161 def test_decode(self):
162 for p, e in self.STRINGS:
Martin v. Löwis48f4cf92007-07-28 17:58:14 +0000163 infp = io.BytesIO(e)
164 outfp = io.BytesIO()
Walter Dörwaldb1975432005-12-14 23:32:22 +0000165 quopri.decode(infp, outfp)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000166 self.assertEqual(outfp.getvalue(), p)
Barry Warsaw70697632001-06-19 19:08:13 +0000167
Walter Dörwaldb1975432005-12-14 23:32:22 +0000168 @withpythonimplementation
Barry Warsaw70697632001-06-19 19:08:13 +0000169 def test_embedded_ws(self):
170 for p, e in self.ESTRINGS:
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000171 self.assertEqual(quopri.encodestring(p, quotetabs=True), e)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000172 self.assertEqual(quopri.decodestring(e), p)
Barry Warsaw70697632001-06-19 19:08:13 +0000173
Walter Dörwaldb1975432005-12-14 23:32:22 +0000174 @withpythonimplementation
Martin v. Löwis16dc7f42001-09-30 20:32:11 +0000175 def test_encode_header(self):
176 for p, e in self.HSTRINGS:
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000177 self.assertEqual(quopri.encodestring(p, header=True), e)
Martin v. Löwis16dc7f42001-09-30 20:32:11 +0000178
Walter Dörwaldb1975432005-12-14 23:32:22 +0000179 @withpythonimplementation
Martin v. Löwis16dc7f42001-09-30 20:32:11 +0000180 def test_decode_header(self):
181 for p, e in self.HSTRINGS:
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000182 self.assertEqual(quopri.decodestring(e, header=True), p)
Tim Peters87cc0c32001-07-21 01:41:30 +0000183
Walter Dörwald3b287702005-12-15 20:17:20 +0000184 def test_scriptencode(self):
Tim Peters536cf992005-12-25 23:18:31 +0000185 (p, e) = self.STRINGS[-1]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000186 process = subprocess.Popen([sys.executable, "-mquopri"],
187 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtinab747a72010-11-05 15:40:27 +0000188 self.addCleanup(process.stdout.close)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189 cout, cerr = process.communicate(p)
190 # On Windows, Python will output the result to stdout using
191 # CRLF, as the mode of stdout is text mode. To compare this
192 # with the expected result, we need to do a line-by-line comparison.
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000193 cout = cout.decode('latin-1').splitlines()
194 e = e.decode('latin-1').splitlines()
195 assert len(cout)==len(e)
196 for i in range(len(cout)):
197 self.assertEqual(cout[i], e[i])
198 self.assertEqual(cout, e)
Walter Dörwald3b287702005-12-15 20:17:20 +0000199
200 def test_scriptdecode(self):
Tim Peters536cf992005-12-25 23:18:31 +0000201 (p, e) = self.STRINGS[-1]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202 process = subprocess.Popen([sys.executable, "-mquopri", "-d"],
203 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Brian Curtinab747a72010-11-05 15:40:27 +0000204 self.addCleanup(process.stdout.close)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205 cout, cerr = process.communicate(e)
Martin v. Löwisc582bfc2007-07-28 17:52:25 +0000206 cout = cout.decode('latin-1')
207 p = p.decode('latin-1')
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000208 self.assertEqual(cout.splitlines(), p.splitlines())
Walter Dörwald3b287702005-12-15 20:17:20 +0000209
Fred Drake2e2be372001-09-20 21:33:42 +0000210def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000211 support.run_unittest(QuopriTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000212
213
214if __name__ == "__main__":
215 test_main()