Debian Codegraph @

tests/encoding.test

raw
1 # This file contains a collection of tests for tclEncoding.c
2 # Sourcing this file into Tcl runs the tests and generates output for errors.
3 # No output means no errors were found.
4 #
5 # Copyright (c) 1997 Sun Microsystems, Inc.
6 # Copyright (c) 1998-1999 Scriptics Corporation.
7 #
8 # See the file "license.terms" for information on usage and redistribution of
9 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
10
11 if {"::tcltest" ni [namespace children]} {
12 package require tcltest 2.5
13 namespace import -force ::tcltest::*
14 }
15
16 namespace eval ::tcl::test::encoding {
17 variable x
18
19 catch {
20 ::tcltest::loadTestedCommands
21 package require -exact Tcltest [info patchlevel]
22 }
23
24 proc toutf {args} {
25 variable x
26 lappend x "toutf $args"
27 }
28 proc fromutf {args} {
29 variable x
30 lappend x "fromutf $args"
31 }
32
33 proc runtests {} {
34 variable x
35
36 # Some tests require the testencoding command
37 testConstraint testencoding [llength [info commands testencoding]]
38 testConstraint testbytestring [llength [info commands testbytestring]]
39 testConstraint teststringbytes [llength [info commands teststringbytes]]
40 testConstraint ucs2 [expr {[format %c 0x010000] eq "\uFFFD"}]
41 testConstraint fullutf [expr {[format %c 0x010000] ne "\uFFFD"}]
42 testConstraint exec [llength [info commands exec]]
43 testConstraint testgetdefenc [llength [info commands testgetdefenc]]
44
45 # TclInitEncodingSubsystem is tested by the rest of this file
46 # TclFinalizeEncodingSubsystem is not currently tested
47
48 test encoding-1.1 {Tcl_GetEncoding: system encoding} -setup {
49 set old [encoding system]
50 } -constraints {testencoding} -body {
51 testencoding create foo [namespace origin toutf] [namespace origin fromutf]
52 encoding system foo
53 set x {}
54 encoding convertto abcd
55 return $x
56 } -cleanup {
57 encoding system $old
58 testencoding delete foo
59 } -result {{fromutf }}
60 test encoding-1.2 {Tcl_GetEncoding: existing encoding} {testencoding} {
61 testencoding create foo [namespace origin toutf] [namespace origin fromutf]
62 set x {}
63 encoding convertto foo abcd
64 testencoding delete foo
65 return $x
66 } {{fromutf }}
67 test encoding-1.3 {Tcl_GetEncoding: load encoding} {
68 list [encoding convertto jis0208 \u4E4E] \
69 [encoding convertfrom jis0208 8C]
70 } "8C \u4E4E"
71
72 test encoding-2.1 {Tcl_FreeEncoding: refcount == 0} {
73 encoding convertto jis0208 \u4E4E
74 } {8C}
75 test encoding-2.2 {Tcl_FreeEncoding: refcount != 0} -setup {
76 set system [encoding system]
77 set path [encoding dirs]
78 } -constraints {testencoding} -body {
79 encoding system shiftjis ;# incr ref count
80 encoding dirs [list [pwd]]
81 set x [encoding convertto shiftjis \u4E4E] ;# old one found
82 encoding system iso8859-1
83 llength shiftjis ;# Shimmer away any cache of Tcl_Encoding
84 lappend x [catch {encoding convertto shiftjis \u4E4E} msg] $msg
85 } -cleanup {
86 encoding system iso8859-1
87 encoding dirs $path
88 encoding system $system
89 } -result "\x8C\xC1 1 {unknown encoding \"shiftjis\"}"
90
91 test encoding-3.1 {Tcl_GetEncodingName, NULL} -setup {
92 set old [encoding system]
93 } -body {
94 encoding system shiftjis
95 encoding system
96 } -cleanup {
97 encoding system $old
98 } -result {shiftjis}
99 test encoding-3.2 {Tcl_GetEncodingName, non-null} -setup {
100 set old [fconfigure stdout -encoding]
101 } -body {
102 fconfigure stdout -encoding jis0208
103 fconfigure stdout -encoding
104 } -cleanup {
105 fconfigure stdout -encoding $old
106 } -result {jis0208}
107
108 test encoding-4.1 {Tcl_GetEncodingNames} -constraints {testencoding} -setup {
109 cd [makeDirectory tmp]
110 makeDirectory [file join tmp encoding]
111 set path [encoding dirs]
112 encoding dirs {}
113 catch {unset encodings}
114 catch {unset x}
115 } -body {
116 foreach encoding [encoding names] {
117 set encodings($encoding) 1
118 }
119 makeFile {} [file join tmp encoding junk.enc]
120 makeFile {} [file join tmp encoding junk2.enc]
121 encoding dirs [list [file join [pwd] encoding]]
122 foreach encoding [encoding names] {
123 if {![info exists encodings($encoding)]} {
124 lappend x $encoding
125 }
126 }
127 lsort $x
128 } -cleanup {
129 encoding dirs $path
130 cd [workingDirectory]
131 removeFile [file join tmp encoding junk2.enc]
132 removeFile [file join tmp encoding junk.enc]
133 removeDirectory [file join tmp encoding]
134 removeDirectory tmp
135 } -result {junk junk2}
136
137 test encoding-5.1 {Tcl_SetSystemEncoding} -setup {
138 set old [encoding system]
139 } -body {
140 encoding system jis0208
141 encoding convertto \u4E4E
142 } -cleanup {
143 encoding system iso8859-1
144 encoding system $old
145 } -result {8C}
146 test encoding-5.2 {Tcl_SetSystemEncoding: test ref count} {
147 set old [encoding system]
148 encoding system $old
149 string compare $old [encoding system]
150 } {0}
151
152 test encoding-6.1 {Tcl_CreateEncoding: new} {testencoding} {
153 testencoding create foo [namespace code {toutf 1}] \
154 [namespace code {fromutf 2}]
155 set x {}
156 encoding convertfrom foo abcd
157 encoding convertto foo abcd
158 testencoding delete foo
159 return $x
160 } {{toutf 1} {fromutf 2}}
161 test encoding-6.2 {Tcl_CreateEncoding: replace encoding} {testencoding} {
162 testencoding create foo [namespace code {toutf a}] \
163 [namespace code {fromutf b}]
164 set x {}
165 encoding convertfrom foo abcd
166 encoding convertto foo abcd
167 testencoding delete foo
168 return $x
169 } {{toutf a} {fromutf b}}
170
171 test encoding-7.1 {Tcl_ExternalToUtfDString: small buffer} {
172 encoding convertfrom jis0208 8c8c8c8c
173 } "\u543E\u543E\u543E\u543E"
174 test encoding-7.2 {Tcl_UtfToExternalDString: big buffer} {
175 set a 8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C8C
176 append a $a
177 append a $a
178 append a $a
179 append a $a
180 set x [encoding convertfrom jis0208 $a]
181 list [string length $x] [string index $x 0]
182 } "512 \u4E4E"
183
184 test encoding-8.1 {Tcl_ExternalToUtf} {
185 set f [open [file join [temporaryDirectory] dummy] w]
186 fconfigure $f -translation binary
187 puts -nonewline $f "ab\x8C\xC1g"
188 close $f
189 set f [open [file join [temporaryDirectory] dummy] r]
190 fconfigure $f -translation lf -encoding shiftjis
191 set x [read $f]
192 close $f
193 file delete [file join [temporaryDirectory] dummy]
194 return $x
195 } "ab\u4E4Eg"
196
197 test encoding-9.1 {Tcl_UtfToExternalDString: small buffer} {
198 encoding convertto jis0208 "\u543E\u543E\u543E\u543E"
199 } {8c8c8c8c}
200 test encoding-9.2 {Tcl_UtfToExternalDString: big buffer} {
201 set a \u4E4E\u4E4E\u4E4E\u4E4E\u4E4E\u4E4E\u4E4E\u4E4E
202 append a $a
203 append a $a
204 append a $a
205 append a $a
206 append a $a
207 append a $a
208 set x [encoding convertto jis0208 $a]
209 list [string length $x] [string range $x 0 1]
210 } "1024 8C"
211
212 test encoding-10.1 {Tcl_UtfToExternal} {
213 set f [open [file join [temporaryDirectory] dummy] w]
214 fconfigure $f -translation lf -encoding shiftjis
215 puts -nonewline $f "ab\u4E4Eg"
216 close $f
217 set f [open [file join [temporaryDirectory] dummy] r]
218 fconfigure $f -translation binary
219 set x [read $f]
220 close $f
221 file delete [file join [temporaryDirectory] dummy]
222 return $x
223 } "ab\x8C\xC1g"
224
225 proc viewable {str} {
226 set res ""
227 foreach c [split $str {}] {
228 if {[string is print $c] && [string is ascii $c]} {
229 append res $c
230 } else {
231 append res "\\u[format %4.4X [scan $c %c]]"
232 }
233 }
234 return "$str ($res)"
235 }
236
237 test encoding-11.1 {LoadEncodingFile: unknown encoding} {testencoding} {
238 set system [encoding system]
239 set path [encoding dirs]
240 encoding system iso8859-1
241 encoding dirs {}
242 llength jis0208 ;# Shimmer any cached Tcl_Encoding in shared literal
243 set x [list [catch {encoding convertto jis0208 \u4E4E} msg] $msg]
244 encoding dirs $path
245 encoding system $system
246 lappend x [encoding convertto jis0208 \u4E4E]
247 } {1 {unknown encoding "jis0208"} 8C}
248 test encoding-11.2 {LoadEncodingFile: single-byte} {
249 encoding convertfrom jis0201 \xA1
250 } \uFF61
251 test encoding-11.3 {LoadEncodingFile: double-byte} {
252 encoding convertfrom jis0208 8C
253 } \u4E4E
254 test encoding-11.4 {LoadEncodingFile: multi-byte} {
255 encoding convertfrom shiftjis \x8C\xC1
256 } \u4E4E
257 test encoding-11.5 {LoadEncodingFile: escape file} {
258 viewable [encoding convertto iso2022 \u4E4E]
259 } [viewable "\x1B\$B8C\x1B(B"]
260 test encoding-11.5.1 {LoadEncodingFile: escape file} {
261 viewable [encoding convertto iso2022-jp \u4E4E]
262 } [viewable "\x1B\$B8C\x1B(B"]
263 test encoding-11.6 {LoadEncodingFile: invalid file} -constraints {testencoding} -setup {
264 set system [encoding system]
265 set path [encoding dirs]
266 encoding system iso8859-1
267 } -body {
268 cd [temporaryDirectory]
269 encoding dirs [file join tmp encoding]
270 makeDirectory tmp
271 makeDirectory [file join tmp encoding]
272 set f [open [file join tmp encoding splat.enc] w]
273 fconfigure $f -translation binary
274 puts $f "abcdefghijklmnop"
275 close $f
276 encoding convertto splat \u4E4E
277 } -returnCodes error -cleanup {
278 file delete [file join [temporaryDirectory] tmp encoding splat.enc]
279 removeDirectory [file join tmp encoding]
280 removeDirectory tmp
281 cd [workingDirectory]
282 encoding dirs $path
283 encoding system $system
284 } -result {invalid encoding file "splat"}
285
286 # OpenEncodingFile is fully tested by the rest of the tests in this file.
287
288 test encoding-12.1 {LoadTableEncoding: normal encoding} {
289 set x [encoding convertto iso8859-3 \u0120]
290 append x [encoding convertto iso8859-3 \xD5]
291 append x [encoding convertfrom iso8859-3 \xD5]
292 } \xD5?\u0120
293 test encoding-12.2 {LoadTableEncoding: single-byte encoding} {
294 set x [encoding convertto iso8859-3 ab\u0120g]
295 append x [encoding convertfrom iso8859-3 ab\xD5g]
296 } ab\xD5gab\u0120g
297 test encoding-12.3 {LoadTableEncoding: multi-byte encoding} {
298 set x [encoding convertto shiftjis ab\u4E4Eg]
299 append x [encoding convertfrom shiftjis ab\x8C\xC1g]
300 } ab\x8C\xC1gab\u4E4Eg
301 test encoding-12.4 {LoadTableEncoding: double-byte encoding} {
302 set x [encoding convertto jis0208 \u4E4E\u03B1]
303 append x [encoding convertfrom jis0208 8C&A]
304 } 8C&A\u4E4E\u03B1
305 test encoding-12.5 {LoadTableEncoding: symbol encoding} {
306 set x [encoding convertto symbol \u03B3]
307 append x [encoding convertto symbol g]
308 append x [encoding convertfrom symbol g]
309 } gg\u03B3
310 test encoding-12.6 {LoadTableEncoding: overflow in char value} ucs2 {
311 encoding convertto iso8859-3 \U010000
312 } ?
313 test encoding-12.7 {cp864 [ecafd8611d]} {
314 scan [encoding convertfrom cp864 \xA7] %c
315 } 8364
316 test encoding-12.8 {cp165 [ecafd8611d]} {
317 scan [encoding convertfrom cp165 \xA7] %c
318 } 65160
319
320 test encoding-13.1 {LoadEscapeTable} {
321 viewable [set x [encoding convertto iso2022 ab\u4E4E\u68D9g]]
322 } [viewable "ab\x1B\$B8C\x1B\$\(DD%\x1B(Bg"]
323
324 test encoding-14.1 {BinaryProc} {
325 encoding convertto identity \x12\x34\x56\xff\x69
326 } "\x12\x34\x56\xc3\xbf\x69"
327
328 test encoding-15.1 {UtfToUtfProc} {
329 encoding convertto utf-8 \xA3
330 } "\xC2\xA3"
331 test encoding-15.2 {UtfToUtfProc null character output} testbytestring {
332 binary scan [testbytestring [encoding convertto utf-8 \x00]] H* z
333 set z
334 } 00
335 test encoding-15.3 {UtfToUtfProc null character input} teststringbytes {
336 set y [encoding convertfrom utf-8 [encoding convertto utf-8 \x00]]
337 binary scan [teststringbytes $y] H* z
338 set z
339 } c080
340 test encoding-15.4 {UtfToUtfProc emoji character input} -body {
341 set x \xED\xA0\xBD\xED\xB8\x82
342 set y [encoding convertfrom utf-8 \xED\xA0\xBD\xED\xB8\x82]
343 list [string length $x] $y
344 } -result "6 \uD83D\uDE02"
345 test encoding-15.5 {UtfToUtfProc emoji character input} {
346 set x \xF0\x9F\x98\x82
347 set y [encoding convertfrom utf-8 \xF0\x9F\x98\x82]
348 list [string length $x] $y
349 } "4 \uD83D\uDE02"
350 test encoding-15.6 {UtfToUtfProc emoji character output} {
351 set x \uDE02\uD83D\uDE02\uD83D
352 set y [encoding convertto utf-8 \uDE02\uD83D\uDE02\uD83D]
353 binary scan $y H* z
354 list [string length $y] $z
355 } {10 edb882f09f9882eda0bd}
356 test encoding-15.7 {UtfToUtfProc emoji character output} {
357 set x \uDE02\uD83D\uD83D
358 set y [encoding convertto utf-8 \uDE02\uD83D\uD83D]
359 binary scan $y H* z
360 list [string length $x] [string length $y] $z
361 } {3 9 edb882eda0bdeda0bd}
362 test encoding-15.8 {UtfToUtfProc emoji character output} {
363 set x \uDE02\uD83D\xE9
364 set y [encoding convertto utf-8 \uDE02\uD83D\xE9]
365 binary scan $y H* z
366 list [string length $x] [string length $y] $z
367 } {3 8 edb882eda0bdc3a9}
368 test encoding-15.9 {UtfToUtfProc emoji character output} {
369 set x \uDE02\uD83DX
370 set y [encoding convertto utf-8 \uDE02\uD83DX]
371 binary scan $y H* z
372 list [string length $x] [string length $y] $z
373 } {3 7 edb882eda0bd58}
374 test encoding-15.10 {UtfToUtfProc high surrogate character output} {
375 set x \uDE02\xE9
376 set y [encoding convertto utf-8 \uDE02\xE9]
377 binary scan $y H* z
378 list [string length $x] [string length $y] $z
379 } {2 5 edb882c3a9}
380 test encoding-15.11 {UtfToUtfProc low surrogate character output} {
381 set x \uDA02\xE9
382 set y [encoding convertto utf-8 \uDA02\xE9]
383 binary scan $y H* z
384 list [string length $x] [string length $y] $z
385 } {2 5 eda882c3a9}
386 test encoding-15.12 {UtfToUtfProc high surrogate character output} {
387 set x \uDE02Y
388 set y [encoding convertto utf-8 \uDE02Y]
389 binary scan $y H* z
390 list [string length $x] [string length $y] $z
391 } {2 4 edb88259}
392 test encoding-15.13 {UtfToUtfProc low surrogate character output} {
393 set x \uDA02Y
394 set y [encoding convertto utf-8 \uDA02Y]
395 binary scan $y H* z
396 list [string length $x] [string length $y] $z
397 } {2 4 eda88259}
398 test encoding-15.14 {UtfToUtfProc high surrogate character output} {
399 set x \uDE02
400 set y [encoding convertto utf-8 \uDE02]
401 binary scan $y H* z
402 list [string length $x] [string length $y] $z
403 } {1 3 edb882}
404 test encoding-15.15 {UtfToUtfProc low surrogate character output} {
405 set x \uDA02
406 set y [encoding convertto utf-8 \uDA02]
407 binary scan $y H* z
408 list [string length $x] [string length $y] $z
409 } {1 3 eda882}
410 test encoding-15.16 {UtfToUtfProc: Invalid 4-byte UTF-8, see [ed29806ba]} {
411 set x \xF0\xA0\xA1\xC2
412 set y [encoding convertfrom utf-8 \xF0\xA0\xA1\xC2]
413 list [string length $x] $y
414 } "4 \xF0\xA0\xA1\xC2"
415
416 test encoding-16.1 {UnicodeToUtfProc} -body {
417 set val [encoding convertfrom unicode NN]
418 list $val [format %X [scan $val %c]]
419 } -result "\u4E4E 4E4E"
420 test encoding-16.2 {UnicodeToUtfProc} -constraints fullutf -body {
421 set val [encoding convertfrom unicode "\xD8\xD8\xDC\xDC"]
422 list $val [format %x [scan $val %c]]
423 } -result "\U460DC 460dc"
424 test encoding-16.3 {UnicodeToUtfProc} -body {
425 set val [encoding convertfrom unicode "\xDC\xDC"]
426 list $val [format %X [scan $val %c]]
427 } -result "\uDCDC DCDC"
428 test encoding-16.4 {UnicodeToUtfProc, bug [d19fe0a5b]} -body {
429 encoding convertfrom unicode "\x41\x41\x41"
430 } -result \u4141\uFFFD
431 test encoding-16.5 {UnicodeToUtfProc, bug [d19fe0a5b]} -constraints ucs2 -body {
432 encoding convertfrom unicode "\xD8\xD8"
433 } -result \uD8D8
434
435 test encoding-17.1 {UtfToUnicodeProc} -constraints fullutf -body {
436 encoding convertto unicode "\U460DC"
437 } -result "\xD8\xD8\xDC\xDC"
438 test encoding-17.2 {UtfToUnicodeProc} -body {
439 encoding convertto unicode "\uDCDC"
440 } -result "\xDC\xDC"
441 test encoding-17.3 {UtfToUnicodeProc} -body {
442 encoding convertto unicode "\uD8D8"
443 } -result "\xD8\xD8"
444
445 test encoding-18.1 {TableToUtfProc on invalid input} -body {
446 list [catch {encoding convertto jis0208 \\} res] $res
447 } -result {0 !)}
448
449 test encoding-19.1 {TableFromUtfProc} {
450 } {}
451
452 test encoding-20.1 {TableFreefProc} {
453 } {}
454
455 test encoding-21.1 {EscapeToUtfProc} {
456 } {}
457
458 test encoding-22.1 {EscapeFromUtfProc} {
459 } {}
460
461 set iso2022encData "\x1B\$B;d\$I\$b\$G\$O!\"%A%C%W\$49XF~;~\$K\$4EPO?\$\$\$?\$@\$\$\$?\$4=;=j\$r%-%c%C%7%e%\"%&%H\$N:]\$N\x1B(B
462 \x1B\$B>.@Z<jAwIU@h\$H\$7\$F;HMQ\$7\$F\$*\$j\$^\$9!#62\$lF~\$j\$^\$9\$,!\"@5\$7\$\$=;=j\$r\$4EPO?\$7\$J\$*\x1B(B
463 \x1B\$B\$*4j\$\$\$\$\$?\$7\$^\$9!#\$^\$?!\"BgJQ62=L\$G\$9\$,!\"=;=jJQ99\$N\$\"\$H!\"F|K\\8l%5!<%S%9It!J\x1B(B
464 casino_japanese@___.com \x1B\$B!K\$^\$G\$4=;=jJQ99:Q\$NO\"Mm\$r\$\$\$?\$@\$1\$J\$\$\$G\x1B(B
465 \x1B\$B\$7\$g\$&\$+!)\x1B(B"
466
467 set iso2022uniData [encoding convertfrom iso2022-jp $iso2022encData]
468 set iso2022uniData2 "\u79c1\u3069\u3082\u3067\u306f\u3001\u30c1\u30c3\u30d7\u3054\u8cfc\u5165\u6642\u306b\u3054\u767b\u9332\u3044\u305f\u3060\u3044\u305f\u3054\u4f4f\u6240\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u30a2\u30a6\u30c8\u306e\u969b\u306e
469 \u5c0f\u5207\u624b\u9001\u4ed8\u5148\u3068\u3057\u3066\u4f7f\u7528\u3057\u3066\u304a\u308a\u307e\u3059\u3002\u6050\u308c\u5165\u308a\u307e\u3059\u304c\u3001\u6b63\u3057\u3044\u4f4f\u6240\u3092\u3054\u767b\u9332\u3057\u306a\u304a
470 \u304a\u9858\u3044\u3044\u305f\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u5927\u5909\u6050\u7e2e\u3067\u3059\u304c\u3001\u4f4f\u6240\u5909\u66f4\u306e\u3042\u3068\u3001\u65e5\u672c\u8a9e\u30b5\u30fc\u30d3\u30b9\u90e8\uff08
471 \u0063\u0061\u0073\u0069\u006e\u006f\u005f\u006a\u0061\u0070\u0061\u006e\u0065\u0073\u0065\u0040\u005f\u005f\u005f\u002e\u0063\u006f\u006d\u0020\uff09\u307e\u3067\u3054\u4f4f\u6240\u5909\u66f4\u6e08\u306e\u9023\u7d61\u3092\u3044\u305f\u3060\u3051\u306a\u3044\u3067
472 \u3057\u3087\u3046\u304b\uff1f"
473
474 cd [temporaryDirectory]
475 set fid [open iso2022.txt w]
476 fconfigure $fid -encoding binary
477 puts -nonewline $fid $iso2022encData
478 close $fid
479
480 test encoding-23.1 {iso2022-jp escape encoding test} {
481 string equal $iso2022uniData $iso2022uniData2
482 } 1
483 test encoding-23.2 {iso2022-jp escape encoding test} {
484 # This checks that 'gets' isn't resetting the encoding inappropriately.
485 # [Bug #523988]
486 set fid [open iso2022.txt r]
487 fconfigure $fid -encoding iso2022-jp
488 set out ""
489 set count 0
490 while {[set num [gets $fid line]] >= 0} {
491 if {$count} {
492 incr count 1 ; # account for newline
493 append out \n
494 }
495 append out $line
496 incr count $num
497 }
498 close $fid
499 if {[string compare $iso2022uniData $out]} {
500 return -code error "iso2022-jp read in doesn't match original"
501 }
502 list $count $out
503 } [list [string length $iso2022uniData] $iso2022uniData]
504 test encoding-23.3 {iso2022-jp escape encoding test} {
505 # read $fis <size> reads size in chars, not raw bytes.
506 set fid [open iso2022.txt r]
507 fconfigure $fid -encoding iso2022-jp
508 set data [read $fid 50]
509 close $fid
510 return $data
511 } [string range $iso2022uniData 0 49] ; # 0 .. 49 inclusive == 50
512 cd [workingDirectory]
513
514 # Code to make the next few tests more intelligible; the code being tested
515 # should be in the body of the test!
516 proc runInSubprocess {contents {filename iso2022.tcl}} {
517 set theFile [makeFile $contents $filename]
518 try {
519 exec [interpreter] $theFile
520 } finally {
521 removeFile $theFile
522 }
523 }
524
525 test encoding-24.1 {EscapeFreeProc on open channels} exec {
526 runInSubprocess {
527 set f [open [file join [file dirname [info script]] iso2022.txt]]
528 fconfigure $f -encoding iso2022-jp
529 gets $f
530 }
531 } {}
532 test encoding-24.2 {EscapeFreeProc on open channels} {exec} {
533 # Bug #524674 output
534 viewable [runInSubprocess {
535 encoding system cp1252; # Bug #2891556 crash revelator
536 fconfigure stdout -encoding iso2022-jp
537 puts ab\u4E4E\u68D9g
538 set env(TCL_FINALIZE_ON_EXIT) 1
539 exit
540 }]
541 } "ab\x1B\$B8C\x1B\$(DD%\x1B(Bg (ab\\u001B\$B8C\\u001B\$(DD%\\u001B(Bg)"
542 test encoding-24.3 {EscapeFreeProc on open channels} {stdio} {
543 # Bug #219314 - if we don't free escape encodings correctly on channel
544 # closure, we go boom
545 set file [makeFile {
546 encoding system iso2022-jp
547 set a \u4E4E\u4E5E\u4E5F; # 3 Japanese Kanji letters
548 puts $a
549 } iso2022.tcl]
550 set f [open "|[list [interpreter] $file]"]
551 fconfigure $f -encoding iso2022-jp
552 set count [gets $f line]
553 close $f
554 removeFile iso2022.tcl
555 list $count [viewable $line]
556 } [list 3 "\u4E4E\u4E5E\u4E5F (\\u4E4E\\u4E5E\\u4E5F)"]
557
558 test encoding-24.4 {Parse valid or invalid utf-8} {
559 string length [encoding convertfrom utf-8 "\xC0\x80"]
560 } 1
561 test encoding-24.5 {Parse valid or invalid utf-8} {
562 string length [encoding convertfrom utf-8 "\xC0\x81"]
563 } 2
564 test encoding-24.6 {Parse valid or invalid utf-8} {
565 string length [encoding convertfrom utf-8 "\xC1\xBF"]
566 } 2
567 test encoding-24.7 {Parse valid or invalid utf-8} {
568 string length [encoding convertfrom utf-8 "\xC2\x80"]
569 } 1
570 test encoding-24.8 {Parse valid or invalid utf-8} {
571 string length [encoding convertfrom utf-8 "\xE0\x80\x80"]
572 } 3
573 test encoding-24.9 {Parse valid or invalid utf-8} {
574 string length [encoding convertfrom utf-8 "\xE0\x9F\xBF"]
575 } 3
576 test encoding-24.10 {Parse valid or invalid utf-8} {
577 string length [encoding convertfrom utf-8 "\xE0\xA0\x80"]
578 } 1
579 test encoding-24.11 {Parse valid or invalid utf-8} {
580 string length [encoding convertfrom utf-8 "\xEF\xBF\xBF"]
581 } 1
582
583 file delete [file join [temporaryDirectory] iso2022.txt]
584
585 #
586 # Begin jajp encoding round-trip conformity tests
587 #
588 proc foreach-jisx0208 {varName command} {
589 upvar 1 $varName code
590 foreach range {
591 {2121 217E}
592 {2221 222E}
593 {223A 2241}
594 {224A 2250}
595 {225C 226A}
596 {2272 2279}
597 {227E 227E}
598 {2330 2339}
599 {2421 2473}
600 {2521 2576}
601 {2821 2821}
602 {282C 282C}
603 {2837 2837}
604
605 {30 21 4E 7E}
606 {4F21 4F53}
607
608 {50 21 73 7E}
609 {7421 7426}
610 } {
611 if {[llength $range] == 2} {
612 # for adhoc range. simple {first last}. inclusive.
613 scan $range %x%x first last
614 for {set i $first} {$i <= $last} {incr i} {
615 set code $i
616 uplevel 1 $command
617 }
618 } elseif {[llength $range] == 4} {
619 # for uniform range.
620 scan $range %x%x%x%x h0 l0 hend lend
621 for {set hi $h0} {$hi <= $hend} {incr hi} {
622 for {set lo $l0} {$lo <= $lend} {incr lo} {
623 set code [expr {$hi << 8 | ($lo & 0xff)}]
624 uplevel 1 $command
625 }
626 }
627 } else {
628 error "really?"
629 }
630 }
631 }
632 proc gen-jisx0208-euc-jp {code} {
633 binary format cc \
634 [expr {($code >> 8) | 0x80}] [expr {($code & 0xFF) | 0x80}]
635 }
636 proc gen-jisx0208-iso2022-jp {code} {
637 binary format a3cca3 \
638 "\x1B\$B" [expr {$code >> 8}] [expr {$code & 0xFF}] "\x1B(B"
639 }
640 proc gen-jisx0208-cp932 {code} {
641 set c1 [expr {($code >> 8) | 0x80}]
642 set c2 [expr {($code & 0xff)| 0x80}]
643 if {$c1 % 2} {
644 set c1 [expr {($c1 >> 1) + ($c1 < 0xDF ? 0x31 : 0x71)}]
645 incr c2 [expr {- (0x60 + ($c2 < 0xE0))}]
646 } else {
647 set c1 [expr {($c1 >> 1) + ($c1 < 0xDF ? 0x30 : 0x70)}]
648 incr c2 -2
649 }
650 binary format cc $c1 $c2
651 }
652 proc channel-diff {fa fb} {
653 set diff {}
654 while {[gets $fa la] >= 0 && [gets $fb lb] >= 0} {
655 if {[string compare $la $lb] == 0} continue
656 # lappend diff $la $lb
657
658 # For more readable (easy to analyze) output.
659 set code [lindex $la 0]
660 binary scan [lindex $la 1] H* expected
661 binary scan [lindex $lb 1] H* got
662 lappend diff [list $code $expected $got]
663 }
664 return $diff
665 }
666
667 # Create char tables.
668 cd [temporaryDirectory]
669 foreach enc {cp932 euc-jp iso2022-jp} {
670 set f [open $enc.chars w]
671 fconfigure $f -encoding binary
672 foreach-jisx0208 code {
673 puts $f [format "%04X %s" $code [gen-jisx0208-$enc $code]]
674 }
675 close $f
676 }
677 # shiftjis == cp932 for jisx0208.
678 file copy -force cp932.chars shiftjis.chars
679
680 set NUM 0
681 foreach from {cp932 shiftjis euc-jp iso2022-jp} {
682 foreach to {cp932 shiftjis euc-jp iso2022-jp} {
683 test encoding-25.[incr NUM] "jisx0208 $from => $to" -setup {
684 cd [temporaryDirectory]
685 } -body {
686 set f [open $from.chars]
687 fconfigure $f -encoding $from
688 set out [open $from.$to.tcltestout w]
689 fconfigure $out -encoding $to
690 puts -nonewline $out [read $f]
691 close $out
692 close $f
693 # then compare $to.chars <=> $from.to.tcltestout as binary.
694 set fa [open $to.chars rb]
695 set fb [open $from.$to.tcltestout rb]
696 channel-diff $fa $fb
697 # Difference should be empty.
698 } -cleanup {
699 close $fa
700 close $fb
701 } -result {}
702 }
703 }
704
705 test encoding-26.0 {Tcl_GetDefaultEncodingDir} -constraints {
706 testgetdefenc
707 } -setup {
708 set origDir [testgetdefenc]
709 testsetdefenc slappy
710 } -body {
711 testgetdefenc
712 } -cleanup {
713 testsetdefenc $origDir
714 } -result slappy
715
716 file delete {*}[glob -directory [temporaryDirectory] *.chars *.tcltestout]
717 # ===> Cut here <===
718
719 # EscapeFreeProc, GetTableEncoding, unilen are fully tested by the rest of
720 # this file.
721
722
723 test encoding-27.1 {encoding dirs basic behavior} -returnCodes error -body {
724 encoding dirs ? ?
725 } -result {wrong # args: should be "encoding dirs ?dirList?"}
726 test encoding-27.2 {encoding dirs basic behavior} -returnCodes error -body {
727 encoding dirs "\{not a list"
728 } -result "expected directory list but got \"\{not a list\""
729
730 }
731
732
733 test encoding-28.0 {all encodings load} -body {
734 set string hello
735 foreach name [encoding names] {
736 incr count
737 encoding convertto $name $string
738
739 # discard the cached internal representation of Tcl_Encoding
740 # Unfortunately, without this, encoding 2-1 fails.
741 llength $name
742 }
743 return $count
744 } -result 86
745
746 runtests
747
748 test encoding-bug-183a1adcc0-1 {Bug [183a1adcc0] Buffer overflow Tcl_UtfToExternal} -constraints {
749 testencoding
750 } -body {
751 # Note - buffers are initialized to \xff
752 list [catch {testencoding Tcl_UtfToExternal unicode A {start end} {} 1} result] $result
753 } -result [list 0 [list nospace {} \xff]]
754
755 test encoding-bug-183a1adcc0-2 {Bug [183a1adcc0] Buffer overflow Tcl_UtfToExternal} -constraints {
756 testencoding
757 } -body {
758 # Note - buffers are initialized to \xff
759 list [catch {testencoding Tcl_UtfToExternal unicode A {start end} {} 0} result] $result
760 } -result [list 0 [list nospace {} {}]]
761
762 test encoding-bug-183a1adcc0-3 {Bug [183a1adcc0] Buffer overflow Tcl_UtfToExternal} -constraints {
763 testencoding
764 } -body {
765 # Note - buffers are initialized to \xff
766 list [catch {testencoding Tcl_UtfToExternal unicode A {start end} {} 2} result] $result
767 } -result [list 0 [list nospace {} \x00\x00]]
768
769 test encoding-bug-183a1adcc0-4 {Bug [183a1adcc0] Buffer overflow Tcl_UtfToExternal} -constraints {
770 testencoding
771 } -body {
772 # Note - buffers are initialized to \xff
773 list [catch {testencoding Tcl_UtfToExternal unicode A {start end} {} 3} result] $result
774 } -result [list 0 [list nospace {} \x00\x00\xff]]
775
776 test encoding-bug-183a1adcc0-5 {Bug [183a1adcc0] Buffer overflow Tcl_UtfToExternal} -constraints {
777 testencoding ucs2
778 } -body {
779 # Note - buffers are initialized to \xff
780 list [catch {testencoding Tcl_UtfToExternal unicode A {start end} {} 4} result] $result
781 } -result [list 0 [list ok {} [expr {$::tcl_platform(byteOrder) eq "littleEndian" ? "\x41\x00" : "\x00\x41"}]\x00\x00]]
782
783 }
784
785 # cleanup
786 namespace delete ::tcl::test::encoding
787 ::tcltest::cleanupTests
788 return
789
790 # Local Variables:
791 # mode: tcl
792 # End: