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