blob: c428c615fdd870312ef1ce698dfcddae7274ed89 [file] [log] [blame]
Barry Warsaw04f357c2002-07-23 19:04:11 +00001from test import test_support
2import unittest
Marc-André Lemburga37171d2001-06-19 20:09:28 +00003import codecs
4import StringIO
5
6class UTF16Test(unittest.TestCase):
7
8 spamle = '\xff\xfes\x00p\x00a\x00m\x00s\x00p\x00a\x00m\x00'
9 spambe = '\xfe\xff\x00s\x00p\x00a\x00m\x00s\x00p\x00a\x00m'
10
11 def test_only_one_bom(self):
12 _,_,reader,writer = codecs.lookup("utf-16")
13 # encode some stream
14 s = StringIO.StringIO()
15 f = writer(s)
16 f.write(u"spam")
17 f.write(u"spam")
18 d = s.getvalue()
19 # check whether there is exactly one BOM in it
20 self.assert_(d == self.spamle or d == self.spambe)
21 # try to read it back
22 s = StringIO.StringIO(d)
23 f = reader(s)
24 self.assertEquals(f.read(), u"spamspam")
25
Walter Dörwald8709a422002-09-03 13:53:40 +000026class EscapeDecodeTest(unittest.TestCase):
27 def test_empty_escape_decode(self):
28 self.assertEquals(codecs.escape_decode(""), ("", 0))
29
Marc-André Lemburg29273c82003-02-04 19:35:03 +000030class RecodingTest(unittest.TestCase):
31 def test_recoding(self):
32 f = StringIO.StringIO()
33 f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
34 f2.write(u"a")
35 f2.close()
36 # Python used to crash on this at exit because of a refcount
37 # bug in _codecsmodule.c
Fred Drake2e2be372001-09-20 21:33:42 +000038
Martin v. Löwis2548c732003-04-18 10:39:54 +000039# From RFC 3492
40punycode_testcases = [
41 # A Arabic (Egyptian):
42 (u"\u0644\u064A\u0647\u0645\u0627\u0628\u062A\u0643\u0644"
43 u"\u0645\u0648\u0634\u0639\u0631\u0628\u064A\u061F",
44 "egbpdaj6bu4bxfgehfvwxn"),
45 # B Chinese (simplified):
46 (u"\u4ED6\u4EEC\u4E3A\u4EC0\u4E48\u4E0D\u8BF4\u4E2D\u6587",
47 "ihqwcrb4cv8a8dqg056pqjye"),
48 # C Chinese (traditional):
49 (u"\u4ED6\u5011\u7232\u4EC0\u9EBD\u4E0D\u8AAA\u4E2D\u6587",
50 "ihqwctvzc91f659drss3x8bo0yb"),
51 # D Czech: Pro<ccaron>prost<ecaron>nemluv<iacute><ccaron>esky
52 (u"\u0050\u0072\u006F\u010D\u0070\u0072\u006F\u0073\u0074"
53 u"\u011B\u006E\u0065\u006D\u006C\u0075\u0076\u00ED\u010D"
54 u"\u0065\u0073\u006B\u0079",
55 "Proprostnemluvesky-uyb24dma41a"),
56 # E Hebrew:
57 (u"\u05DC\u05DE\u05D4\u05D4\u05DD\u05E4\u05E9\u05D5\u05D8"
58 u"\u05DC\u05D0\u05DE\u05D3\u05D1\u05E8\u05D9\u05DD\u05E2"
59 u"\u05D1\u05E8\u05D9\u05EA",
60 "4dbcagdahymbxekheh6e0a7fei0b"),
61 # F Hindi (Devanagari):
62 (u"\u092F\u0939\u0932\u094B\u0917\u0939\u093F\u0928\u094D"
63 u"\u0926\u0940\u0915\u094D\u092F\u094B\u0902\u0928\u0939"
64 u"\u0940\u0902\u092C\u094B\u0932\u0938\u0915\u0924\u0947"
65 u"\u0939\u0948\u0902",
66 "i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd"),
67
68 #(G) Japanese (kanji and hiragana):
69 (u"\u306A\u305C\u307F\u3093\u306A\u65E5\u672C\u8A9E\u3092"
70 u"\u8A71\u3057\u3066\u304F\u308C\u306A\u3044\u306E\u304B",
71 "n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa"),
72
73 # (H) Korean (Hangul syllables):
74 (u"\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774"
75 u"\uD55C\uAD6D\uC5B4\uB97C\uC774\uD574\uD55C\uB2E4\uBA74"
76 u"\uC5BC\uB9C8\uB098\uC88B\uC744\uAE4C",
77 "989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5j"
78 "psd879ccm6fea98c"),
79
80 # (I) Russian (Cyrillic):
81 (u"\u043F\u043E\u0447\u0435\u043C\u0443\u0436\u0435\u043E"
82 u"\u043D\u0438\u043D\u0435\u0433\u043E\u0432\u043E\u0440"
83 u"\u044F\u0442\u043F\u043E\u0440\u0443\u0441\u0441\u043A"
84 u"\u0438",
85 "b1abfaaepdrnnbgefbaDotcwatmq2g4l"),
86
87 # (J) Spanish: Porqu<eacute>nopuedensimplementehablarenEspa<ntilde>ol
88 (u"\u0050\u006F\u0072\u0071\u0075\u00E9\u006E\u006F\u0070"
89 u"\u0075\u0065\u0064\u0065\u006E\u0073\u0069\u006D\u0070"
90 u"\u006C\u0065\u006D\u0065\u006E\u0074\u0065\u0068\u0061"
91 u"\u0062\u006C\u0061\u0072\u0065\u006E\u0045\u0073\u0070"
92 u"\u0061\u00F1\u006F\u006C",
93 "PorqunopuedensimplementehablarenEspaol-fmd56a"),
94
95 # (K) Vietnamese:
96 # T<adotbelow>isaoh<odotbelow>kh<ocirc>ngth<ecirchookabove>ch\
97 # <ihookabove>n<oacute>iti<ecircacute>ngVi<ecircdotbelow>t
98 (u"\u0054\u1EA1\u0069\u0073\u0061\u006F\u0068\u1ECD\u006B"
99 u"\u0068\u00F4\u006E\u0067\u0074\u0068\u1EC3\u0063\u0068"
100 u"\u1EC9\u006E\u00F3\u0069\u0074\u0069\u1EBF\u006E\u0067"
101 u"\u0056\u0069\u1EC7\u0074",
102 "TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g"),
103
104
105 #(L) 3<nen>B<gumi><kinpachi><sensei>
106 (u"\u0033\u5E74\u0042\u7D44\u91D1\u516B\u5148\u751F",
107 "3B-ww4c5e180e575a65lsy2b"),
Tim Peters0eadaac2003-04-24 16:02:54 +0000108
Martin v. Löwis2548c732003-04-18 10:39:54 +0000109 # (M) <amuro><namie>-with-SUPER-MONKEYS
110 (u"\u5B89\u5BA4\u5948\u7F8E\u6075\u002D\u0077\u0069\u0074"
111 u"\u0068\u002D\u0053\u0055\u0050\u0045\u0052\u002D\u004D"
112 u"\u004F\u004E\u004B\u0045\u0059\u0053",
113 "-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n"),
114
115 # (N) Hello-Another-Way-<sorezore><no><basho>
116 (u"\u0048\u0065\u006C\u006C\u006F\u002D\u0041\u006E\u006F"
117 u"\u0074\u0068\u0065\u0072\u002D\u0057\u0061\u0079\u002D"
118 u"\u305D\u308C\u305E\u308C\u306E\u5834\u6240",
119 "Hello-Another-Way--fc4qua05auwb3674vfr0b"),
120
121 # (O) <hitotsu><yane><no><shita>2
122 (u"\u3072\u3068\u3064\u5C4B\u6839\u306E\u4E0B\u0032",
123 "2-u9tlzr9756bt3uc0v"),
124
125 # (P) Maji<de>Koi<suru>5<byou><mae>
126 (u"\u004D\u0061\u006A\u0069\u3067\u004B\u006F\u0069\u3059"
127 u"\u308B\u0035\u79D2\u524D",
128 "MajiKoi5-783gue6qz075azm5e"),
129
130 # (Q) <pafii>de<runba>
131 (u"\u30D1\u30D5\u30A3\u30FC\u0064\u0065\u30EB\u30F3\u30D0",
132 "de-jg4avhby1noc0d"),
133
134 # (R) <sono><supiido><de>
135 (u"\u305D\u306E\u30B9\u30D4\u30FC\u30C9\u3067",
136 "d9juau41awczczp"),
137
138 # (S) -> $1.00 <-
139 (u"\u002D\u003E\u0020\u0024\u0031\u002E\u0030\u0030\u0020"
140 u"\u003C\u002D",
141 "-> $1.00 <--")
142 ]
143
144for i in punycode_testcases:
145 if len(i)!=2:
146 print repr(i)
147
148class PunycodeTest(unittest.TestCase):
149 def test_encode(self):
150 for uni, puny in punycode_testcases:
151 # Need to convert both strings to lower case, since
152 # some of the extended encodings use upper case, but our
153 # code produces only lower case. Converting just puny to
154 # lower is also insufficient, since some of the input characters
155 # are upper case.
156 self.assertEquals(uni.encode("punycode").lower(), puny.lower())
157
158 def test_decode(self):
159 for uni, puny in punycode_testcases:
160 self.assertEquals(uni, puny.decode("punycode"))
161
162# From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html
163nameprep_tests = [
164 # 3.1 Map to nothing.
165 ('foo\xc2\xad\xcd\x8f\xe1\xa0\x86\xe1\xa0\x8bbar'
166 '\xe2\x80\x8b\xe2\x81\xa0baz\xef\xb8\x80\xef\xb8\x88\xef'
167 '\xb8\x8f\xef\xbb\xbf',
168 'foobarbaz'),
169 # 3.2 Case folding ASCII U+0043 U+0041 U+0046 U+0045.
170 ('CAFE',
171 'cafe'),
172 # 3.3 Case folding 8bit U+00DF (german sharp s).
173 # The original test case is bogus; it says \xc3\xdf
174 ('\xc3\x9f',
175 'ss'),
176 # 3.4 Case folding U+0130 (turkish capital I with dot).
177 ('\xc4\xb0',
178 'i\xcc\x87'),
179 # 3.5 Case folding multibyte U+0143 U+037A.
180 ('\xc5\x83\xcd\xba',
181 '\xc5\x84 \xce\xb9'),
182 # 3.6 Case folding U+2121 U+33C6 U+1D7BB.
183 # XXX: skip this as it fails in UCS-2 mode
184 #('\xe2\x84\xa1\xe3\x8f\x86\xf0\x9d\x9e\xbb',
185 # 'telc\xe2\x88\x95kg\xcf\x83'),
186 (None, None),
187 # 3.7 Normalization of U+006a U+030c U+00A0 U+00AA.
188 ('j\xcc\x8c\xc2\xa0\xc2\xaa',
189 '\xc7\xb0 a'),
190 # 3.8 Case folding U+1FB7 and normalization.
191 ('\xe1\xbe\xb7',
192 '\xe1\xbe\xb6\xce\xb9'),
193 # 3.9 Self-reverting case folding U+01F0 and normalization.
194 # The original test case is bogus, it says `\xc7\xf0'
195 ('\xc7\xb0',
196 '\xc7\xb0'),
197 # 3.10 Self-reverting case folding U+0390 and normalization.
198 ('\xce\x90',
199 '\xce\x90'),
200 # 3.11 Self-reverting case folding U+03B0 and normalization.
201 ('\xce\xb0',
202 '\xce\xb0'),
203 # 3.12 Self-reverting case folding U+1E96 and normalization.
204 ('\xe1\xba\x96',
205 '\xe1\xba\x96'),
206 # 3.13 Self-reverting case folding U+1F56 and normalization.
207 ('\xe1\xbd\x96',
208 '\xe1\xbd\x96'),
209 # 3.14 ASCII space character U+0020.
210 (' ',
211 ' '),
212 # 3.15 Non-ASCII 8bit space character U+00A0.
213 ('\xc2\xa0',
214 ' '),
215 # 3.16 Non-ASCII multibyte space character U+1680.
216 ('\xe1\x9a\x80',
217 None),
218 # 3.17 Non-ASCII multibyte space character U+2000.
219 ('\xe2\x80\x80',
220 ' '),
221 # 3.18 Zero Width Space U+200b.
222 ('\xe2\x80\x8b',
223 ''),
224 # 3.19 Non-ASCII multibyte space character U+3000.
225 ('\xe3\x80\x80',
226 ' '),
227 # 3.20 ASCII control characters U+0010 U+007F.
228 ('\x10\x7f',
229 '\x10\x7f'),
230 # 3.21 Non-ASCII 8bit control character U+0085.
231 ('\xc2\x85',
232 None),
233 # 3.22 Non-ASCII multibyte control character U+180E.
234 ('\xe1\xa0\x8e',
235 None),
236 # 3.23 Zero Width No-Break Space U+FEFF.
237 ('\xef\xbb\xbf',
238 ''),
239 # 3.24 Non-ASCII control character U+1D175.
240 ('\xf0\x9d\x85\xb5',
241 None),
242 # 3.25 Plane 0 private use character U+F123.
243 ('\xef\x84\xa3',
244 None),
245 # 3.26 Plane 15 private use character U+F1234.
246 ('\xf3\xb1\x88\xb4',
247 None),
248 # 3.27 Plane 16 private use character U+10F234.
249 ('\xf4\x8f\x88\xb4',
250 None),
251 # 3.28 Non-character code point U+8FFFE.
252 ('\xf2\x8f\xbf\xbe',
253 None),
254 # 3.29 Non-character code point U+10FFFF.
255 ('\xf4\x8f\xbf\xbf',
256 None),
257 # 3.30 Surrogate code U+DF42.
258 ('\xed\xbd\x82',
259 None),
260 # 3.31 Non-plain text character U+FFFD.
261 ('\xef\xbf\xbd',
262 None),
263 # 3.32 Ideographic description character U+2FF5.
264 ('\xe2\xbf\xb5',
265 None),
266 # 3.33 Display property character U+0341.
Tim Peters0eadaac2003-04-24 16:02:54 +0000267 ('\xcd\x81',
Martin v. Löwis2548c732003-04-18 10:39:54 +0000268 '\xcc\x81'),
269 # 3.34 Left-to-right mark U+200E.
270 ('\xe2\x80\x8e',
271 None),
272 # 3.35 Deprecated U+202A.
273 ('\xe2\x80\xaa',
274 None),
275 # 3.36 Language tagging character U+E0001.
276 ('\xf3\xa0\x80\x81',
277 None),
278 # 3.37 Language tagging character U+E0042.
279 ('\xf3\xa0\x81\x82',
280 None),
281 # 3.38 Bidi: RandALCat character U+05BE and LCat characters.
282 ('foo\xd6\xbebar',
283 None),
284 # 3.39 Bidi: RandALCat character U+FD50 and LCat characters.
285 ('foo\xef\xb5\x90bar',
286 None),
287 # 3.40 Bidi: RandALCat character U+FB38 and LCat characters.
288 ('foo\xef\xb9\xb6bar',
289 'foo \xd9\x8ebar'),
290 # 3.41 Bidi: RandALCat without trailing RandALCat U+0627 U+0031.
291 ('\xd8\xa71',
292 None),
293 # 3.42 Bidi: RandALCat character U+0627 U+0031 U+0628.
294 ('\xd8\xa71\xd8\xa8',
295 '\xd8\xa71\xd8\xa8'),
296 # 3.43 Unassigned code point U+E0002.
Martin v. Löwisb5c4b7b2003-04-18 20:21:00 +0000297 # Skip this test as we allow unassigned
298 #('\xf3\xa0\x80\x82',
299 # None),
300 (None, None),
Martin v. Löwis2548c732003-04-18 10:39:54 +0000301 # 3.44 Larger test (shrinking).
302 # Original test case reads \xc3\xdf
303 ('X\xc2\xad\xc3\x9f\xc4\xb0\xe2\x84\xa1j\xcc\x8c\xc2\xa0\xc2'
304 '\xaa\xce\xb0\xe2\x80\x80',
305 'xssi\xcc\x87tel\xc7\xb0 a\xce\xb0 '),
306 # 3.45 Larger test (expanding).
307 # Original test case reads \xc3\x9f
308 ('X\xc3\x9f\xe3\x8c\x96\xc4\xb0\xe2\x84\xa1\xe2\x92\x9f\xe3\x8c'
309 '\x80',
310 'xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3'
311 '\x83\x88\xe3\x83\xabi\xcc\x87tel\x28d\x29\xe3\x82'
312 '\xa2\xe3\x83\x91\xe3\x83\xbc\xe3\x83\x88')
313 ]
314
315
316class NameprepTest(unittest.TestCase):
317 def test_nameprep(self):
318 from encodings.idna import nameprep
319 for pos, (orig, prepped) in enumerate(nameprep_tests):
320 if orig is None:
321 # Skipped
322 continue
323 # The Unicode strings are given in UTF-8
324 orig = unicode(orig, "utf-8")
325 if prepped is None:
326 # Input contains prohibited characters
327 self.assertRaises(UnicodeError, nameprep, orig)
328 else:
329 prepped = unicode(prepped, "utf-8")
330 try:
331 self.assertEquals(nameprep(orig), prepped)
332 except Exception,e:
333 raise test_support.TestFailed("Test 3.%d: %s" % (pos+1, str(e)))
334
Martin v. Löwisa1dde132004-03-24 16:48:24 +0000335class CodecTest(unittest.TestCase):
336 def test_builtin(self):
337 self.assertEquals(unicode("python.org", "idna"), u"python.org")
338
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000339class CodecsModuleTest(unittest.TestCase):
340
341 def test_decode(self):
342 self.assertEquals(codecs.decode('\xe4\xf6\xfc', 'latin-1'),
343 u'\xe4\xf6\xfc')
344 def test_encode(self):
345 self.assertEquals(codecs.encode(u'\xe4\xf6\xfc', 'latin-1'),
346 '\xe4\xf6\xfc')
347
Fred Drake2e2be372001-09-20 21:33:42 +0000348def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000349 test_support.run_unittest(
350 UTF16Test,
351 EscapeDecodeTest,
352 RecodingTest,
353 PunycodeTest,
Martin v. Löwisa1dde132004-03-24 16:48:24 +0000354 NameprepTest,
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000355 CodecTest,
356 CodecsModuleTest
Walter Dörwald21d3a322003-05-01 17:45:56 +0000357 )
Fred Drake2e2be372001-09-20 21:33:42 +0000358
359
360if __name__ == "__main__":
361 test_main()