1 | n/a | import unittest |
---|
2 | n/a | from email import _encoded_words as _ew |
---|
3 | n/a | from email import errors |
---|
4 | n/a | from test.test_email import TestEmailBase |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class TestDecodeQ(TestEmailBase): |
---|
8 | n/a | |
---|
9 | n/a | def _test(self, source, ex_result, ex_defects=[]): |
---|
10 | n/a | result, defects = _ew.decode_q(source) |
---|
11 | n/a | self.assertEqual(result, ex_result) |
---|
12 | n/a | self.assertDefectsEqual(defects, ex_defects) |
---|
13 | n/a | |
---|
14 | n/a | def test_no_encoded(self): |
---|
15 | n/a | self._test(b'foobar', b'foobar') |
---|
16 | n/a | |
---|
17 | n/a | def test_spaces(self): |
---|
18 | n/a | self._test(b'foo=20bar=20', b'foo bar ') |
---|
19 | n/a | self._test(b'foo_bar_', b'foo bar ') |
---|
20 | n/a | |
---|
21 | n/a | def test_run_of_encoded(self): |
---|
22 | n/a | self._test(b'foo=20=20=21=2Cbar', b'foo !,bar') |
---|
23 | n/a | |
---|
24 | n/a | |
---|
25 | n/a | class TestDecodeB(TestEmailBase): |
---|
26 | n/a | |
---|
27 | n/a | def _test(self, source, ex_result, ex_defects=[]): |
---|
28 | n/a | result, defects = _ew.decode_b(source) |
---|
29 | n/a | self.assertEqual(result, ex_result) |
---|
30 | n/a | self.assertDefectsEqual(defects, ex_defects) |
---|
31 | n/a | |
---|
32 | n/a | def test_simple(self): |
---|
33 | n/a | self._test(b'Zm9v', b'foo') |
---|
34 | n/a | |
---|
35 | n/a | def test_missing_padding(self): |
---|
36 | n/a | self._test(b'dmk', b'vi', [errors.InvalidBase64PaddingDefect]) |
---|
37 | n/a | |
---|
38 | n/a | def test_invalid_character(self): |
---|
39 | n/a | self._test(b'dm\x01k===', b'vi', [errors.InvalidBase64CharactersDefect]) |
---|
40 | n/a | |
---|
41 | n/a | def test_invalid_character_and_bad_padding(self): |
---|
42 | n/a | self._test(b'dm\x01k', b'vi', [errors.InvalidBase64CharactersDefect, |
---|
43 | n/a | errors.InvalidBase64PaddingDefect]) |
---|
44 | n/a | |
---|
45 | n/a | |
---|
46 | n/a | class TestDecode(TestEmailBase): |
---|
47 | n/a | |
---|
48 | n/a | def test_wrong_format_input_raises(self): |
---|
49 | n/a | with self.assertRaises(ValueError): |
---|
50 | n/a | _ew.decode('=?badone?=') |
---|
51 | n/a | with self.assertRaises(ValueError): |
---|
52 | n/a | _ew.decode('=?') |
---|
53 | n/a | with self.assertRaises(ValueError): |
---|
54 | n/a | _ew.decode('') |
---|
55 | n/a | |
---|
56 | n/a | def _test(self, source, result, charset='us-ascii', lang='', defects=[]): |
---|
57 | n/a | res, char, l, d = _ew.decode(source) |
---|
58 | n/a | self.assertEqual(res, result) |
---|
59 | n/a | self.assertEqual(char, charset) |
---|
60 | n/a | self.assertEqual(l, lang) |
---|
61 | n/a | self.assertDefectsEqual(d, defects) |
---|
62 | n/a | |
---|
63 | n/a | def test_simple_q(self): |
---|
64 | n/a | self._test('=?us-ascii?q?foo?=', 'foo') |
---|
65 | n/a | |
---|
66 | n/a | def test_simple_b(self): |
---|
67 | n/a | self._test('=?us-ascii?b?dmk=?=', 'vi') |
---|
68 | n/a | |
---|
69 | n/a | def test_q_case_ignored(self): |
---|
70 | n/a | self._test('=?us-ascii?Q?foo?=', 'foo') |
---|
71 | n/a | |
---|
72 | n/a | def test_b_case_ignored(self): |
---|
73 | n/a | self._test('=?us-ascii?B?dmk=?=', 'vi') |
---|
74 | n/a | |
---|
75 | n/a | def test_non_trivial_q(self): |
---|
76 | n/a | self._test('=?latin-1?q?=20F=fcr=20Elise=20?=', ' Für Elise ', 'latin-1') |
---|
77 | n/a | |
---|
78 | n/a | def test_q_escaped_bytes_preserved(self): |
---|
79 | n/a | self._test(b'=?us-ascii?q?=20\xACfoo?='.decode('us-ascii', |
---|
80 | n/a | 'surrogateescape'), |
---|
81 | n/a | ' \uDCACfoo', |
---|
82 | n/a | defects = [errors.UndecodableBytesDefect]) |
---|
83 | n/a | |
---|
84 | n/a | def test_b_undecodable_bytes_ignored_with_defect(self): |
---|
85 | n/a | self._test(b'=?us-ascii?b?dm\xACk?='.decode('us-ascii', |
---|
86 | n/a | 'surrogateescape'), |
---|
87 | n/a | 'vi', |
---|
88 | n/a | defects = [ |
---|
89 | n/a | errors.InvalidBase64CharactersDefect, |
---|
90 | n/a | errors.InvalidBase64PaddingDefect]) |
---|
91 | n/a | |
---|
92 | n/a | def test_b_invalid_bytes_ignored_with_defect(self): |
---|
93 | n/a | self._test('=?us-ascii?b?dm\x01k===?=', |
---|
94 | n/a | 'vi', |
---|
95 | n/a | defects = [errors.InvalidBase64CharactersDefect]) |
---|
96 | n/a | |
---|
97 | n/a | def test_b_invalid_bytes_incorrect_padding(self): |
---|
98 | n/a | self._test('=?us-ascii?b?dm\x01k?=', |
---|
99 | n/a | 'vi', |
---|
100 | n/a | defects = [ |
---|
101 | n/a | errors.InvalidBase64CharactersDefect, |
---|
102 | n/a | errors.InvalidBase64PaddingDefect]) |
---|
103 | n/a | |
---|
104 | n/a | def test_b_padding_defect(self): |
---|
105 | n/a | self._test('=?us-ascii?b?dmk?=', |
---|
106 | n/a | 'vi', |
---|
107 | n/a | defects = [errors.InvalidBase64PaddingDefect]) |
---|
108 | n/a | |
---|
109 | n/a | def test_nonnull_lang(self): |
---|
110 | n/a | self._test('=?us-ascii*jive?q?test?=', 'test', lang='jive') |
---|
111 | n/a | |
---|
112 | n/a | def test_unknown_8bit_charset(self): |
---|
113 | n/a | self._test('=?unknown-8bit?q?foo=ACbar?=', |
---|
114 | n/a | b'foo\xacbar'.decode('ascii', 'surrogateescape'), |
---|
115 | n/a | charset = 'unknown-8bit', |
---|
116 | n/a | defects = []) |
---|
117 | n/a | |
---|
118 | n/a | def test_unknown_charset(self): |
---|
119 | n/a | self._test('=?foobar?q?foo=ACbar?=', |
---|
120 | n/a | b'foo\xacbar'.decode('ascii', 'surrogateescape'), |
---|
121 | n/a | charset = 'foobar', |
---|
122 | n/a | # XXX Should this be a new Defect instead? |
---|
123 | n/a | defects = [errors.CharsetError]) |
---|
124 | n/a | |
---|
125 | n/a | def test_q_nonascii(self): |
---|
126 | n/a | self._test('=?utf-8?q?=C3=89ric?=', |
---|
127 | n/a | 'Ãric', |
---|
128 | n/a | charset='utf-8') |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | class TestEncodeQ(TestEmailBase): |
---|
132 | n/a | |
---|
133 | n/a | def _test(self, src, expected): |
---|
134 | n/a | self.assertEqual(_ew.encode_q(src), expected) |
---|
135 | n/a | |
---|
136 | n/a | def test_all_safe(self): |
---|
137 | n/a | self._test(b'foobar', 'foobar') |
---|
138 | n/a | |
---|
139 | n/a | def test_spaces(self): |
---|
140 | n/a | self._test(b'foo bar ', 'foo_bar_') |
---|
141 | n/a | |
---|
142 | n/a | def test_run_of_encodables(self): |
---|
143 | n/a | self._test(b'foo ,,bar', 'foo__=2C=2Cbar') |
---|
144 | n/a | |
---|
145 | n/a | |
---|
146 | n/a | class TestEncodeB(TestEmailBase): |
---|
147 | n/a | |
---|
148 | n/a | def test_simple(self): |
---|
149 | n/a | self.assertEqual(_ew.encode_b(b'foo'), 'Zm9v') |
---|
150 | n/a | |
---|
151 | n/a | def test_padding(self): |
---|
152 | n/a | self.assertEqual(_ew.encode_b(b'vi'), 'dmk=') |
---|
153 | n/a | |
---|
154 | n/a | |
---|
155 | n/a | class TestEncode(TestEmailBase): |
---|
156 | n/a | |
---|
157 | n/a | def test_q(self): |
---|
158 | n/a | self.assertEqual(_ew.encode('foo', 'utf-8', 'q'), '=?utf-8?q?foo?=') |
---|
159 | n/a | |
---|
160 | n/a | def test_b(self): |
---|
161 | n/a | self.assertEqual(_ew.encode('foo', 'utf-8', 'b'), '=?utf-8?b?Zm9v?=') |
---|
162 | n/a | |
---|
163 | n/a | def test_auto_q(self): |
---|
164 | n/a | self.assertEqual(_ew.encode('foo', 'utf-8'), '=?utf-8?q?foo?=') |
---|
165 | n/a | |
---|
166 | n/a | def test_auto_q_if_short_mostly_safe(self): |
---|
167 | n/a | self.assertEqual(_ew.encode('vi.', 'utf-8'), '=?utf-8?q?vi=2E?=') |
---|
168 | n/a | |
---|
169 | n/a | def test_auto_b_if_enough_unsafe(self): |
---|
170 | n/a | self.assertEqual(_ew.encode('.....', 'utf-8'), '=?utf-8?b?Li4uLi4=?=') |
---|
171 | n/a | |
---|
172 | n/a | def test_auto_b_if_long_unsafe(self): |
---|
173 | n/a | self.assertEqual(_ew.encode('vi.vi.vi.vi.vi.', 'utf-8'), |
---|
174 | n/a | '=?utf-8?b?dmkudmkudmkudmkudmku?=') |
---|
175 | n/a | |
---|
176 | n/a | def test_auto_q_if_long_mostly_safe(self): |
---|
177 | n/a | self.assertEqual(_ew.encode('vi vi vi.vi ', 'utf-8'), |
---|
178 | n/a | '=?utf-8?q?vi_vi_vi=2Evi_?=') |
---|
179 | n/a | |
---|
180 | n/a | def test_utf8_default(self): |
---|
181 | n/a | self.assertEqual(_ew.encode('foo'), '=?utf-8?q?foo?=') |
---|
182 | n/a | |
---|
183 | n/a | def test_lang(self): |
---|
184 | n/a | self.assertEqual(_ew.encode('foo', lang='jive'), '=?utf-8*jive?q?foo?=') |
---|
185 | n/a | |
---|
186 | n/a | def test_unknown_8bit(self): |
---|
187 | n/a | self.assertEqual(_ew.encode('foo\uDCACbar', charset='unknown-8bit'), |
---|
188 | n/a | '=?unknown-8bit?q?foo=ACbar?=') |
---|
189 | n/a | |
---|
190 | n/a | |
---|
191 | n/a | if __name__ == '__main__': |
---|
192 | n/a | unittest.main() |
---|