Debian Codegraph @

Lib/test/test_email/test_asian_codecs.py

raw
1 # Copyright (C) 2002 Python Software Foundation
2 # Contact: [email protected]
3 # email package unit tests for (optional) Asian codecs
4
5 import unittest
6
7 from test.test_email import TestEmailBase
8 from email.charset import Charset
9 from email.header import Header, decode_header
10 from email.message import Message
11
12 # We're compatible with Python 2.3, but it doesn't have the built-in Asian
13 # codecs, so we have to skip all these tests.
14 try:
15 str(b'foo', 'euc-jp')
16 except LookupError:
17 raise unittest.SkipTest
18
19
20
21 class TestEmailAsianCodecs(TestEmailBase):
22 def test_japanese_codecs(self):
23 eq = self.ndiffAssertEqual
24 jcode = "euc-jp"
25 gcode = "iso-8859-1"
26 j = Charset(jcode)
27 g = Charset(gcode)
28 h = Header("Hello World!")
29 jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc'
30 b'\xa5\xeb\xa5\xc9\xa1\xaa', jcode)
31 ghello = str(b'Gr\xfc\xdf Gott!', gcode)
32 h.append(jhello, j)
33 h.append(ghello, g)
34 # BAW: This used to -- and maybe should -- fold the two iso-8859-1
35 # chunks into a single encoded word. However it doesn't violate the
36 # standard to have them as two encoded chunks and maybe it's
37 # reasonable <wink> for each .append() call to result in a separate
38 # encoded word.
39 eq(h.encode(), """\
40 Hello World! =?iso-2022-jp?b?GyRCJU8lbSE8JW8hPCVrJUkhKhsoQg==?=
41 =?iso-8859-1?q?Gr=FC=DF_Gott!?=""")
42 eq(decode_header(h.encode()),
43 [(b'Hello World! ', None),
44 (b'\x1b$B%O%m!<%o!<%k%I!*\x1b(B', 'iso-2022-jp'),
45 (b'Gr\xfc\xdf Gott!', gcode)])
46 subject_bytes = (b'test-ja \xa4\xd8\xc5\xea\xb9\xc6\xa4\xb5'
47 b'\xa4\xec\xa4\xbf\xa5\xe1\xa1\xbc\xa5\xeb\xa4\xcf\xbb\xca\xb2'
48 b'\xf1\xbc\xd4\xa4\xce\xbe\xb5\xc7\xa7\xa4\xf2\xc2\xd4\xa4\xc3'
49 b'\xa4\xc6\xa4\xa4\xa4\xde\xa4\xb9')
50 subject = str(subject_bytes, jcode)
51 h = Header(subject, j, header_name="Subject")
52 # test a very long header
53 enc = h.encode()
54 # TK: splitting point may differ by codec design and/or Header encoding
55 eq(enc , """\
56 =?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKGyhC?=
57 =?iso-2022-jp?b?GyRCMnE8VCROPjVHJyRyQlQkQyRGJCQkXiQ5GyhC?=""")
58 # TK: full decode comparison
59 eq(str(h).encode(jcode), subject_bytes)
60
61 h = Header("Japanese")
62 s = '\u65e5\u672c\u8a9e' # 日本語
63 h.append(s, Charset('euc-jp'))
64 h.append(s, Charset('iso-2022-jp'))
65 h.append(s, Charset('shift_jis'))
66 eq(h.encode(), """\
67 Japanese =?iso-2022-jp?b?GyRCRnxLXDhsGyhC?= =?iso-2022-jp?b?GyRCRnxLXDhsGyhC?=
68 =?iso-2022-jp?b?GyRCRnxLXDhsGyhC?=""")
69 eq(decode_header(h.encode()),
70 [(b'Japanese ', None),
71 (b'\x1b$BF|K\\8l\x1b(B\x1b$BF|K\\8l\x1b(B\x1b$BF|K\\8l\x1b(B', 'iso-2022-jp'),
72 ])
73
74 def test_chinese_codecs(self):
75 eq = self.ndiffAssertEqual
76 h = Header("Chinese")
77 s = '\u4e2d\u6587' # 中文
78 h.append(s, Charset('gb2312'))
79 h.append(s, Charset('gbk'))
80 h.append(s, Charset('gb18030'))
81 h.append(s, Charset('hz'))
82 h.append(s, Charset('big5'))
83 h.append(s, Charset('big5hkscs'))
84 eq(h.encode(), """\
85 Chinese =?gb2312?b?1tDOxA==?= =?gbk?b?1tDOxA==?= =?gb18030?b?1tDOxA==?=
86 =?hz?b?fntWUE5Efn0=?= =?big5?b?pKSk5Q==?= =?big5hkscs?b?pKSk5Q==?=""")
87 eq(decode_header(h.encode()),
88 [(b'Chinese ', None),
89 (b'\xd6\xd0\xce\xc4', 'gb2312'),
90 (b'\xd6\xd0\xce\xc4', 'gbk'),
91 (b'\xd6\xd0\xce\xc4', 'gb18030'),
92 (b'~{VPND~}', 'hz'),
93 (b'\xa4\xa4\xa4\xe5', 'big5'),
94 (b'\xa4\xa4\xa4\xe5', 'big5hkscs'),
95 ])
96
97 def test_korean_codecs(self):
98 eq = self.ndiffAssertEqual
99 h = Header("Korean")
100 s = '\ud55c\uad6d\uc5b4' # 한국어
101 h.append(s, Charset('euc-kr'))
102 h.append(s, Charset('ks_c_5601-1987'))
103 h.append(s, Charset('cp949'))
104 h.append(s, Charset('iso-2022-kr'))
105 h.append(s, Charset('johab'))
106 eq(h.encode(), """\
107 Korean =?euc-kr?b?x9Gxub7u?= =?ks_c_5601-1987?b?x9Gxub7uIMfRsbm+7g==?=
108 =?iso-2022-kr?b?GyQpQw5HUTE5Pm4P?= =?johab?b?0GWKgrTh?=""")
109 eq(decode_header(h.encode()),
110 [(b'Korean ', None),
111 (b'\xc7\xd1\xb1\xb9\xbe\xee', 'euc-kr'),
112 (b'\xc7\xd1\xb1\xb9\xbe\xee \xc7\xd1\xb1\xb9\xbe\xee', 'ks_c_5601-1987'),
113 (b'\x1b$)C\x0eGQ19>n\x0f', 'iso-2022-kr'),
114 (b'\xd0e\x8a\x82\xb4\xe1', 'johab'),
115 ])
116
117 def test_payload_encoding_utf8(self):
118 jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc'
119 b'\xa5\xeb\xa5\xc9\xa1\xaa', 'euc-jp')
120 msg = Message()
121 msg.set_payload(jhello, 'utf-8')
122 ustr = msg.get_payload(decode=True).decode(msg.get_content_charset())
123 self.assertEqual(jhello, ustr)
124
125 def test_payload_encoding(self):
126 jcode = 'euc-jp'
127 jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc'
128 b'\xa5\xeb\xa5\xc9\xa1\xaa', jcode)
129 msg = Message()
130 msg.set_payload(jhello, jcode)
131 ustr = msg.get_payload(decode=True).decode(msg.get_content_charset())
132 self.assertEqual(jhello, ustr)
133
134
135
136 if __name__ == '__main__':
137 unittest.main()