Debian Codegraph @

tests/zlib.test

raw
1 # The file tests the tclZlib.c file.
2 #
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1996-1998 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 Scriptics Corporation.
9 #
10 # See the file "license.terms" for information on usage and redistribution of
11 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
13 if {"::tcltest" ni [namespace children]} {
14 package require tcltest 2.5
15 namespace import -force ::tcltest::*
16 }
17
18 testConstraint zlib [llength [info commands zlib]]
19 testConstraint recentZlib 0
20 catch {
21 # Work around a bug in some versions of zlib; known to manifest on at
22 # least Mac OS X Mountain Lion...
23 testConstraint recentZlib \
24 [package vsatisfies [zlib::pkgconfig get zlibVersion] 1.2.6]
25 }
26
27 test zlib-1.1 {zlib basics} -constraints zlib -returnCodes error -body {
28 zlib
29 } -result {wrong # args: should be "zlib command arg ?...?"}
30 test zlib-1.2 {zlib basics} -constraints zlib -returnCodes error -body {
31 zlib ? {}
32 } -result {bad command "?": must be adler32, compress, crc32, decompress, deflate, gunzip, gzip, inflate, push, or stream}
33 test zlib-1.3 {zlib basics} -constraints zlib -body {
34 zlib::pkgconfig list
35 } -result zlibVersion
36 test zlib-1.4 {zlib basics} -constraints zlib -body {
37 package present zlib
38 } -result 2.0.1
39
40 test zlib-2.1 {zlib compress/decompress} zlib {
41 zlib decompress [zlib compress abcdefghijklm]
42 } abcdefghijklm
43
44 test zlib-3.1 {zlib deflate/inflate} zlib {
45 zlib inflate [zlib deflate abcdefghijklm]
46 } abcdefghijklm
47
48 test zlib-4.1 {zlib gzip/gunzip} zlib {
49 zlib gunzip [zlib gzip abcdefghijklm]
50 } abcdefghijklm
51 test zlib-4.2 {zlib gzip/gunzip} zlib {
52 set s [string repeat abcdef 5]
53 list [zlib gunzip [zlib gzip $s -header {comment gorp}] -header head] \
54 [dict get $head comment] [dict get $head size]
55 } {abcdefabcdefabcdefabcdefabcdef gorp 30}
56
57 test zlib-5.1 {zlib adler32} zlib {
58 format %x [expr {[zlib adler32 abcdeabcdeabcdeabcdeabcdeabcde] & 0xffffffff}]
59 } b3b50b9b
60 test zlib-5.2 {zlib adler32} zlib {
61 format %x [expr {[zlib adler32 abcdeabcdeabcdeabcdeabcdeabcde 42] & 0xffffffff}]
62 } b8830bc4
63 test zlib-5.3 {zlib adler32} -constraints zlib -returnCodes error -body {
64 zlib adler32 abcdeabcdeabcdeabcdeabcdeabcde 42 x
65 } -result {wrong # args: should be "zlib adler32 data ?startValue?"}
66
67 test zlib-6.1 {zlib crc32} zlib {
68 format %x [expr {[zlib crc32 abcdeabcdeabcdeabcdeabcdeabcde] & 0xffffffff}]
69 } 6f73e901
70 test zlib-6.2 {zlib crc32} zlib {
71 format %x [expr {[zlib crc32 abcdeabcdeabcdeabcdeabcdeabcde 42] & 0xffffffff}]
72 } ce1c4914
73 test zlib-6.3 {zlib crc32} -constraints zlib -returnCodes error -body {
74 zlib crc32 abcdeabcdeabcdeabcdeabcdeabcde 42 x
75 } -result {wrong # args: should be "zlib crc32 data ?startValue?"}
76 test zlib-6.4 {zlib crc32: bug 2662434} -constraints zlib -body {
77 zlib crc32 "dabale arroz a la zorra el abad"
78 } -result 3842832571
79
80 test zlib-7.0 {zlib stream} -constraints zlib -returnCodes error -setup {
81 set s [zlib stream compress]
82 } -body {
83 $s ?
84 } -cleanup {
85 $s close
86 } -result {bad option "?": must be add, checksum, close, eof, finalize, flush, fullflush, get, header, put, or reset}
87 test zlib-7.1 {zlib stream} zlib {
88 set s [zlib stream compress]
89 $s put -finalize abcdeEDCBA
90 set data [$s get]
91 set result [list [$s get] [format %x [$s checksum]]]
92 $s close
93 lappend result [zlib decompress $data]
94 } {{} 136f033f abcdeEDCBA}
95 test zlib-7.2 {zlib stream} zlib {
96 set s [zlib stream decompress]
97 $s put -finalize [zlib compress abcdeEDCBA]
98 set data [$s get]
99 set result [list [$s get] [format %x [$s checksum]]]
100 $s close
101 lappend result $data
102 } {{} 136f033f abcdeEDCBA}
103 test zlib-7.3 {zlib stream} zlib {
104 set s [zlib stream deflate]
105 $s put -finalize abcdeEDCBA
106 set data [$s get]
107 set result [list [$s get] [format %x [$s checksum]]]
108 $s close
109 lappend result [zlib inflate $data]
110 } {{} 1 abcdeEDCBA}
111 test zlib-7.4 {zlib stream} zlib {
112 set s [zlib stream inflate]
113 $s put -finalize [zlib deflate abcdeEDCBA]
114 set data [$s get]
115 set result [list [$s get] [format %x [$s checksum]]]
116 $s close
117 lappend result $data
118 } {{} 1 abcdeEDCBA}
119 test zlib-7.5 {zlib stream} zlib {
120 set s [zlib stream gzip]
121 $s put -finalize abcdeEDCBA..
122 set data [$s get]
123 set result [list [$s get] [format %x [$s checksum]]]
124 $s close
125 lappend result [zlib gunzip $data]
126 } {{} 69f34b6a abcdeEDCBA..}
127 test zlib-7.6 {zlib stream} zlib {
128 set s [zlib stream gunzip]
129 $s put -finalize [zlib gzip abcdeEDCBA..]
130 set data [$s get]
131 set result [list [$s get] [format %x [$s checksum]]]
132 $s close
133 lappend result $data
134 } {{} 69f34b6a abcdeEDCBA..}
135 test zlib-7.7 {zlib stream: Bug 25842c161} -constraints zlib -body {
136 set s [zlib stream deflate]
137 $s put {}
138 } -cleanup {
139 catch {$s close}
140 } -result ""
141 # Also causes Tk Bug 10f2e7872b
142 test zlib-7.8 {zlib stream: Bug b26e38a3e4} -constraints zlib -setup {
143 expr {srand(12345)}
144 set randdata {}
145 for {set i 0} {$i<6001} {incr i} {
146 append randdata [binary format c [expr {int(256*rand())}]]
147 }
148 } -body {
149 set strm [zlib stream compress]
150 for {set i 1} {$i<3000} {incr i} {
151 $strm put $randdata
152 }
153 $strm put -finalize $randdata
154 set data [$strm get]
155 list [string length $data] [string length [zlib decompress $data]]
156 } -cleanup {
157 catch {$strm close}
158 unset -nocomplain randdata data
159 } -result {120185 18003000}
160 test zlib-7.9 {zlib stream finalize (bug 25842c161)} -constraints zlib -setup {
161 set z1 [zlib stream gzip]
162 set z2 [zlib stream gzip]
163 } -body {
164 $z1 put ABCDEedbca..
165 $z1 finalize
166 zlib gunzip [$z1 get]
167 } -cleanup {
168 $z1 close
169 } -result ABCDEedbca..
170 test zlib-7.10 {zlib stream finalize (bug 25842c161)} -constraints zlib -setup {
171 set z2 [zlib stream gzip]
172 } -body {
173 $z2 put -finalize ABCDEedbca..
174 zlib gunzip [$z2 get]
175 } -cleanup {
176 $z2 close
177 } -result ABCDEedbca..
178 test zlib-7.11 {zlib stream put -finalize (bug 25842c161)} -constraints zlib -setup {
179 set c [zlib stream gzip]
180 set d [zlib stream gunzip]
181 } -body {
182 $c put abcdeEDCBA..
183 $c finalize
184 $d put [$c get]
185 $d finalize
186 $d get
187 } -cleanup {
188 $c close
189 $d close
190 } -result abcdeEDCBA..
191 test zlib-7.12 {zlib stream put; zlib stream finalize (bug 25842c161)} -constraints zlib -setup {
192 set c [zlib stream gzip]
193 set d [zlib stream gunzip]
194 } -body {
195 $c put -finalize abcdeEDCBA..
196 $d put -finalize [$c get]
197 $d get
198 } -cleanup {
199 $c close
200 $d close
201 } -result abcdeEDCBA..
202
203 test zlib-8.1 {zlib transformation} -constraints zlib -setup {
204 set file [makeFile {} test.gz]
205 } -body {
206 set f [zlib push gzip [open $file w] -header {comment gorp}]
207 puts $f "ok"
208 close $f
209 set f [zlib push gunzip [open $file]]
210 list [gets $f] [dict get [chan configure $f -header] comment]
211 } -cleanup {
212 close $f
213 removeFile $file
214 } -result {ok gorp}
215 test zlib-8.2 {zlib transformation} -constraints zlib -setup {
216 set file [makeFile {} test.z]
217 } -body {
218 set f [zlib push compress [open $file w]]
219 puts $f "ok"
220 close $f
221 set f [zlib push decompress [open $file]]
222 gets $f
223 } -cleanup {
224 close $f
225 removeFile $file
226 } -result ok
227 test zlib-8.3 {zlib transformation and fileevent} -constraints zlib -setup {
228 set srv [socket -myaddr localhost -server {apply {{c a p} {
229 fconfigure $c -translation binary -buffering none -blocking 0
230 puts -nonewline $c [zlib gzip [string repeat a 81920]]
231 close $c
232 }}} 0]
233 set port [lindex [fconfigure $srv -sockname] 2]
234 set file [makeFile {} test.gz]
235 set fout [open $file wb]
236 } -body {
237 set sin [socket localhost $port]
238 try {
239 fconfigure $sin -translation binary
240 zlib push gunzip $sin
241 after 1000 {set total timeout}
242 fcopy $sin $fout -command {apply {{c {e {}}} {
243 set ::total [expr {$e eq {} ? $c : $e}]
244 }}}
245 vwait total
246 after cancel {set total timeout}
247 } finally {
248 close $sin
249 }
250 append total --> [file size $file]
251 } -cleanup {
252 close $fout
253 close $srv
254 removeFile $file
255 } -result 81920-->81920
256 test zlib-8.4 {transformation and flushing: Bug 3517696} -setup {
257 set file [makeFile {} test.z]
258 set fd [open $file w]
259 } -constraints zlib -body {
260 zlib push compress $fd
261 puts $fd "qwertyuiop"
262 fconfigure $fd -flush sync
263 puts $fd "qwertyuiop"
264 } -cleanup {
265 catch {close $fd}
266 removeFile $file
267 } -result {}
268 test zlib-8.5 {transformation and flushing and fileevents: Bug 3525907} -setup {
269 foreach {r w} [chan pipe] break
270 } -constraints zlib -body {
271 set ::res {}
272 fconfigure $w -buffering none
273 zlib push compress $w
274 puts -nonewline $w qwertyuiop
275 chan configure $w -flush sync
276 after 500 {puts -nonewline $w asdfghjkl;close $w}
277 fconfigure $r -blocking 0 -buffering none
278 zlib push decompress $r
279 fileevent $r readable {set msg [read $r];lappend ::res $msg;if {[eof $r]} {set ::done 1}}
280 after 250 {lappend ::res MIDDLE}
281 vwait ::done
282 set ::res
283 } -cleanup {
284 catch {close $r}
285 } -result {qwertyuiop MIDDLE asdfghjkl {}}
286 test zlib-8.6 {transformation and fconfigure} -setup {
287 set file [makeFile {} test.z]
288 set fd [open $file wb]
289 } -constraints zlib -body {
290 list [fconfigure $fd] [zlib push compress $fd; fconfigure $fd] \
291 [chan pop $fd; fconfigure $fd]
292 } -cleanup {
293 catch {close $fd}
294 removeFile $file
295 } -result {{-blocking 1 -buffering full -buffersize 4096 -encoding binary -eofchar {} -translation lf} {-blocking 1 -buffering full -buffersize 4096 -encoding binary -eofchar {} -translation lf -checksum 1 -dictionary {}} {-blocking 1 -buffering full -buffersize 4096 -encoding binary -eofchar {} -translation lf}}
296 test zlib-8.7 {transformation and fconfigure} -setup {
297 set file [makeFile {} test.gz]
298 set fd [open $file wb]
299 } -constraints zlib -body {
300 list [fconfigure $fd] [zlib push gzip $fd; fconfigure $fd] \
301 [chan pop $fd; fconfigure $fd]
302 } -cleanup {
303 catch {close $fd}
304 removeFile $file
305 } -result {{-blocking 1 -buffering full -buffersize 4096 -encoding binary -eofchar {} -translation lf} {-blocking 1 -buffering full -buffersize 4096 -encoding binary -eofchar {} -translation lf -checksum 0} {-blocking 1 -buffering full -buffersize 4096 -encoding binary -eofchar {} -translation lf}}
306 # Input is headers from fetching SPDY draft
307 # Dictionary is that which is proposed _in_ SPDY draft
308 set spdyHeaders "HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nX-Robots-Tag: noarchive\r\nLast-Modified: Tue, 05 Jun 2012 02:43:25 GMT\r\nETag: \"1338864205129|#public|0|en|||0\"\r\nExpires: Tue, 05 Jun 2012 16:17:11 GMT\r\nDate: Tue, 05 Jun 2012 16:17:06 GMT\r\nCache-Control: public, max-age=5\r\nX-Content-Type-Options: nosniff\r\nX-XSS-Protection: 1; mode=block\r\nServer: GSE\r\n"
309 set spdyDict "optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchif-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser-agent100101200201202203204205206300301302303304305306307400401402403404405406407408409410411412413414415416417500501502503504505accept-rangesageetaglocationproxy-authenticatepublicretry-afterservervarywarningwww-authenticateallowcontent-basecontent-encodingcache-controlconnectiondatetrailertransfer-encodingupgradeviawarningcontent-languagecontent-lengthcontent-locationcontent-md5content-rangecontent-typeetagexpireslast-modifiedset-cookieMondayTuesdayWednesdayThursdayFridaySaturdaySundayJanFebMarAprMayJunJulAugSepOctNovDecchunkedtext/htmlimage/pngimage/jpgimage/gifapplication/xmlapplication/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1.1statusversionurl"
310 test zlib-8.8 {transformation and fconfigure} -setup {
311 lassign [chan pipe] inSide outSide
312 } -constraints zlib -body {
313 zlib push compress $outSide -dictionary $spdyDict
314 fconfigure $outSide -blocking 1 -translation binary -buffering none
315 fconfigure $inSide -blocking 1 -translation binary
316 puts -nonewline $outSide $spdyHeaders
317 chan pop $outSide
318 chan close $outSide
319 set compressed [read $inSide]
320 catch {zlib decompress $compressed} err opt
321 list [string length [zlib decompress [zlib compress $spdyHeaders]]] \
322 $err [dict get $opt -errorcode] [zlib adler32 $spdyDict]
323 } -cleanup {
324 catch {close $outSide}
325 catch {close $inSide}
326 } -result {358 {need dictionary} {TCL ZLIB NEED_DICT 2381337010} 2381337010}
327 test zlib-8.9 {transformation and fconfigure} -setup {
328 lassign [chan pipe] inSide outSide
329 set strm [zlib stream decompress]
330 } -constraints zlib -body {
331 zlib push compress $outSide -dictionary $spdyDict
332 fconfigure $outSide -blocking 1 -translation binary -buffering none
333 fconfigure $inSide -blocking 1 -translation binary
334 puts -nonewline $outSide $spdyHeaders
335 set result [fconfigure $outSide -checksum]
336 chan pop $outSide
337 chan close $outSide
338 $strm put -dictionary $spdyDict [read $inSide]
339 lappend result [string length $spdyHeaders] [string length [$strm get]]
340 } -cleanup {
341 catch {close $outSide}
342 catch {close $inSide}
343 catch {$strm close}
344 } -result {3064818174 358 358}
345 test zlib-8.10 {transformation and fconfigure} -setup {
346 lassign [chan pipe] inSide outSide
347 } -constraints {zlib recentZlib} -body {
348 zlib push deflate $outSide -dictionary $spdyDict
349 fconfigure $outSide -blocking 1 -translation binary -buffering none
350 fconfigure $inSide -blocking 1 -translation binary
351 puts -nonewline $outSide $spdyHeaders
352 chan pop $outSide
353 chan close $outSide
354 set compressed [read $inSide]
355 catch {
356 zlib inflate $compressed
357 throw UNREACHABLE "should be unreachable"
358 } err opt
359 list [string length [zlib deflate $spdyHeaders]] \
360 [string length $compressed] \
361 $err [dict get $opt -errorcode]
362 } -cleanup {
363 catch {close $outSide}
364 catch {close $inSide}
365 } -result {254 212 {data error} {TCL ZLIB DATA}}
366 test zlib-8.11 {transformation and fconfigure} -setup {
367 lassign [chan pipe] inSide outSide
368 set strm [zlib stream inflate]
369 } -constraints zlib -body {
370 zlib push deflate $outSide -dictionary $spdyDict
371 fconfigure $outSide -blocking 1 -translation binary -buffering none
372 fconfigure $inSide -blocking 1 -translation binary
373 puts -nonewline $outSide $spdyHeaders
374 chan pop $outSide
375 chan close $outSide
376 $strm put -dictionary $spdyDict [read $inSide]
377 list [string length $spdyHeaders] [string length [$strm get]]
378 } -cleanup {
379 catch {close $outSide}
380 catch {close $inSide}
381 catch {$strm close}
382 } -result {358 358}
383 test zlib-8.12 {transformation and fconfigure} -setup {
384 lassign [chan pipe] inSide outSide
385 set strm [zlib stream compress]
386 } -constraints zlib -body {
387 $strm put -dictionary $spdyDict -finalize $spdyHeaders
388 zlib push decompress $inSide
389 fconfigure $outSide -blocking 1 -translation binary
390 fconfigure $inSide -translation binary -dictionary $spdyDict
391 puts -nonewline $outSide [$strm get]
392 close $outSide
393 list [string length $spdyHeaders] [string length [read $inSide]] \
394 [fconfigure $inSide -checksum]
395 } -cleanup {
396 catch {close $outSide}
397 catch {close $inSide}
398 catch {$strm close}
399 } -result {358 358 3064818174}
400 test zlib-8.13 {transformation and fconfigure} -setup {
401 lassign [chan pipe] inSide outSide
402 set strm [zlib stream compress]
403 } -constraints zlib -body {
404 $strm put -dictionary $spdyDict -finalize $spdyHeaders
405 zlib push decompress $inSide -dictionary $spdyDict
406 fconfigure $outSide -blocking 1 -translation binary
407 fconfigure $inSide -translation binary
408 puts -nonewline $outSide [$strm get]
409 close $outSide
410 list [string length $spdyHeaders] [string length [read $inSide]] \
411 [fconfigure $inSide -checksum]
412 } -cleanup {
413 catch {close $outSide}
414 catch {close $inSide}
415 catch {$strm close}
416 } -result {358 358 3064818174}
417 test zlib-8.14 {transformation and fconfigure} -setup {
418 lassign [chan pipe] inSide outSide
419 set strm [zlib stream deflate]
420 } -constraints zlib -body {
421 $strm put -finalize -dictionary $spdyDict $spdyHeaders
422 zlib push inflate $inSide
423 fconfigure $outSide -blocking 1 -buffering none -translation binary
424 fconfigure $inSide -translation binary -dictionary $spdyDict
425 puts -nonewline $outSide [$strm get]
426 close $outSide
427 list [string length $spdyHeaders] [string length [read $inSide]]
428 } -cleanup {
429 catch {close $outSide}
430 catch {close $inSide}
431 catch {$strm close}
432 } -result {358 358}
433 test zlib-8.15 {transformation and fconfigure} -setup {
434 lassign [chan pipe] inSide outSide
435 set strm [zlib stream deflate]
436 } -constraints zlib -body {
437 $strm put -finalize -dictionary $spdyDict $spdyHeaders
438 zlib push inflate $inSide -dictionary $spdyDict
439 fconfigure $outSide -blocking 1 -buffering none -translation binary
440 fconfigure $inSide -translation binary
441 puts -nonewline $outSide [$strm get]
442 close $outSide
443 list [string length $spdyHeaders] [string length [read $inSide]]
444 } -cleanup {
445 catch {close $outSide}
446 catch {close $inSide}
447 catch {$strm close}
448 } -result {358 358}
449 test zlib-8.16 {Bug 3603553: buffer transfer with large writes} -setup {
450 # Actual data isn't very important; needs to be substantially larger than
451 # the internal buffer (32kB) and incompressible.
452 set largeData {}
453 for {set i 0;expr {srand(1)}} {$i < 100000} {incr i} {
454 append largeData [lindex "a b c d e f g h i j k l m n o p" \
455 [expr {int(16*rand())}]]
456 }
457 set file [makeFile {} test.gz]
458 } -constraints zlib -body {
459 set f [open $file wb]
460 fconfigure $f -buffering none
461 zlib push gzip $f
462 puts -nonewline $f $largeData
463 close $f
464 expr {[file size $file]<57648}
465 } -cleanup {
466 removeFile $file
467 } -result 1
468 test zlib-8.17 {Bug dd260aaf: fconfigure} -setup {
469 lassign [chan pipe] inSide outSide
470 } -constraints zlib -body {
471 zlib push inflate $inSide
472 zlib push deflate $outSide
473 list [chan configure $inSide -dictionary] [chan configure $outSide -dictionary]
474 } -cleanup {
475 catch {close $inSide}
476 catch {close $outSide}
477 } -result {{} {}}
478 test zlib-8.18 {Bug dd260aaf: fconfigure} -setup {
479 lassign [chan pipe] inSide outSide
480 } -constraints zlib -body {
481 zlib push inflate $inSide -dictionary "one two"
482 zlib push deflate $outSide -dictionary "one two"
483 list [chan configure $inSide -dictionary] [chan configure $outSide -dictionary]
484 } -cleanup {
485 catch {close $inSide}
486 catch {close $outSide}
487 } -result {{one two} {one two}}
488 test zlib-8.19 {zlib transformation, bug f9eafc3886} -constraints zlib -setup {
489 set file [makeFile {} test.gz]
490 } -body {
491 set f [zlib push gzip [open $file w] -header [list comment [string repeat A 500]]]
492 } -cleanup {
493 catch {close $f}
494 removeFile $file
495 } -returnCodes 1 -result {Comment too large for zip}
496 test zlib-8.20 {zlib transformation, bug f9eafc3886} -constraints zlib -setup {
497 set file [makeFile {} test.gz]
498 } -body {
499 set f [zlib push gzip [open $file w] -header [list filename [string repeat A 5000]]]
500 } -cleanup {
501 catch {close $f}
502 removeFile $file
503 } -returnCodes 1 -result {Filename too large for zip}
504 test zlib-8.21 {zlib transformation, bug f9eafc3886} -constraints zlib -setup {
505 set file [makeFile {} test.gz]
506 } -body {
507 set f [zlib push gzip [open $file w] -header [list comment \u100]]
508 } -cleanup {
509 catch {close $f}
510 removeFile $file
511 } -returnCodes 1 -result {Comment contains characters > 0xFF}
512 test zlib-8.22 {zlib transformation, bug f9eafc3886} -constraints zlib -setup {
513 set file [makeFile {} test.gz]
514 } -body {
515 set f [zlib push gzip [open $file w] -header [list filename \u100]]
516 } -cleanup {
517 catch {close $f}
518 removeFile $file
519 } -returnCodes 1 -result {Filename contains characters > 0xFF}
520
521 test zlib-9.1 "check fcopy with push" -constraints zlib -setup {
522 set sfile [makeFile {} testsrc.gz]
523 set file [makeFile {} test.gz]
524 set f [open $sfile wb]
525 puts -nonewline $f [zlib gzip [string repeat a 81920]]
526 close $f
527 } -body {
528 set fin [zlib push gunzip [open $sfile rb]]
529 set fout [open $file wb]
530 set total [fcopy $fin $fout]
531 close $fin ; close $fout
532 list copied $total size [file size $file]
533 } -cleanup {
534 removeFile $file
535 removeFile $sfile
536 } -result {copied 81920 size 81920}
537 test zlib-9.2 "socket fcopy with push" -constraints zlib -setup {
538 set srv [socket -myaddr localhost -server {apply {{c a p} {
539 chan configure $c -translation binary -buffering none -blocking 0
540 puts -nonewline $c [zlib gzip [string repeat a 81920]]
541 close $c
542 set ::total -1
543 }}} 0]
544 set file [makeFile {} test.gz]
545 } -body {
546 lassign [chan configure $srv -sockname] addr name port
547 set sin [socket $addr $port]
548 chan configure $sin -translation binary
549 zlib push gunzip $sin
550 after 1000 {set ::total timeout}
551 vwait ::total
552 after cancel {set ::total timeout}
553 if {$::total != -1} {error "unexpected value $::total of ::total"}
554 set total [fcopy $sin [set fout [open $file wb]]]
555 close $sin
556 close $fout
557 list read $total size [file size $file]
558 } -cleanup {
559 close $srv
560 removeFile $file
561 } -result {read 81920 size 81920}
562 test zlib-9.3 "socket fcopy bg (identity)" -constraints {tempNotWin zlib} -setup {
563 set srv [socket -myaddr localhost -server {apply {{c a p} {
564 #puts "connection from $a:$p on $c"
565 chan configure $c -translation binary -buffering none -blocking 0
566 puts -nonewline $c [string repeat a 81920]
567 close $c
568 }}} 0]
569 set file [makeFile {} test.gz]
570 } -body {
571 lassign [chan configure $srv -sockname] addr name port
572 #puts "listening for connections on $addr $port"
573 set sin [socket localhost $port]
574 chan configure $sin -translation binary
575 update
576 set fout [open $file wb]
577 after 1000 {set ::total timeout}
578 fcopy $sin $fout -command {apply {{c {e {}}} {
579 set ::total [expr {$e eq {} ? $c : $e}]
580 }}}
581 vwait ::total
582 after cancel {set ::total timeout}
583 close $sin; close $fout
584 list read $::total size [file size $file]
585 } -cleanup {
586 close $srv
587 removeFile $file
588 } -returnCodes {ok error} -result {read 81920 size 81920}
589 test zlib-9.4 "socket fcopy bg (gzip)" -constraints zlib -setup {
590 set srv [socket -myaddr localhost -server {apply {{c a p} {
591 chan configure $c -translation binary -buffering none -blocking 0
592 puts -nonewline $c [zlib gzip [string repeat a 81920]]
593 close $c
594 }}} 0]
595 set file [makeFile {} test.gz]
596 } -body {
597 lassign [chan configure $srv -sockname] addr name port
598 set sin [socket $addr $port]
599 chan configure $sin -translation binary
600 zlib push gunzip $sin
601 update
602 set fout [open $file wb]
603 after 1000 {set ::total timeout}
604 fcopy $sin $fout -command {apply {{c {e {}}} {
605 set ::total [expr {$e eq {} ? $c : $e}]
606 }}}
607 vwait ::total
608 after cancel {set ::total timeout}
609 close $sin; close $fout
610 list read $::total size [file size $file]
611 } -cleanup {
612 close $srv
613 removeFile $file
614 } -result {read 81920 size 81920}
615 test zlib-9.5 "socket fcopy incremental (gzip)" -constraints zlib -setup {
616 set srv [socket -myaddr localhost -server {apply {{c a p} {
617 chan configure $c -translation binary -buffering none -blocking 0
618 puts -nonewline $c [zlib gzip [string repeat a 81920]]
619 close $c
620 }}} 0]
621 proc zlib95copy {i o t c {e {}}} {
622 incr t $c
623 if {$e ne {}} {
624 set ::total [list error $e]
625 } elseif {[eof $i]} {
626 set ::total [list eof $t]
627 } else {
628 fcopy $i $o -size 8192 -command [list zlib95copy $i $o $t]
629 }
630 }
631 set file [makeFile {} test.gz]
632 } -body {
633 lassign [chan configure $srv -sockname] addr name port
634 set sin [socket $addr $port]
635 chan configure $sin -translation binary
636 zlib push gunzip $sin
637 update
638 set fout [open $file wb]
639 after 1000 {set ::total timeout}
640 fcopy $sin $fout -size 8192 -command [list zlib95copy $sin $fout 0]
641 vwait ::total
642 after cancel {set ::total timeout}
643 close $sin; close $fout
644 list $::total size [file size $file]
645 } -cleanup {
646 close $srv
647 rename zlib95copy {}
648 removeFile $file
649 } -result {{eof 81920} size 81920}
650 test zlib-9.6 "bug #2818131 (gzip)" -constraints zlib -setup {
651 set srv [socket -myaddr localhost -server {apply {{c a p} {
652 chan configure $c -translation binary -buffering none -blocking 0
653 zlib push gzip $c
654 puts -nonewline $c [string repeat hello 100]
655 close $c
656 }}} 0]
657 } -body {
658 lassign [chan configure $srv -sockname] addr name port
659 after 1000 {set ::total timeout}
660 set s [socket $addr $port]
661 chan configure $s -translation binary
662 zlib push gunzip $s
663 chan event $s readable [list apply {{s} {
664 set d [read $s]
665 if {[eof $s]} {
666 chan event $s readable {}
667 set ::total [list eof [string length $d]]
668 }
669 }} $s]
670 vwait ::total
671 after cancel {set ::total timeout}
672 close $s
673 set ::total
674 } -cleanup {
675 close $srv
676 unset -nocomplain total
677 } -result {eof 500}
678 test zlib-9.7 "bug #2818131 (compress)" -constraints zlib -setup {
679 set srv [socket -myaddr localhost -server {apply {{c a p} {
680 chan configure $c -translation binary -buffering none -blocking 0
681 zlib push compress $c
682 puts -nonewline $c [string repeat hello 100]
683 close $c
684 }}} 0]
685 } -body {
686 lassign [chan configure $srv -sockname] addr name port
687 after 1000 {set ::total timeout}
688 set s [socket $addr $port]
689 chan configure $s -translation binary
690 zlib push decompress $s
691 chan event $s readable [list apply {{s} {
692 set d [read $s]
693 if {[eof $s]} {
694 chan event $s readable {}
695 set ::total [list eof [string length $d]]
696 }
697 }} $s]
698 vwait ::total
699 after cancel {set ::total timeout}
700 close $s
701 set ::total
702 } -cleanup {
703 close $srv
704 unset -nocomplain total
705 } -result {eof 500}
706 test zlib-9.8 "bug #2818131 (deflate)" -constraints zlib -setup {
707 set srv [socket -myaddr localhost -server {apply {{c a p} {
708 chan configure $c -translation binary -buffering none -blocking 0
709 zlib push deflate $c
710 puts -nonewline $c [string repeat hello 100]
711 close $c
712 }}} 0]
713 } -body {
714 lassign [chan configure $srv -sockname] addr name port
715 after 1000 {set ::total timeout}
716 set s [socket $addr $port]
717 chan configure $s -translation binary
718 zlib push inflate $s
719 chan event $s readable [list apply {{s} {
720 set d [read $s]
721 if {[eof $s]} {
722 chan event $s readable {}
723 set ::total [list eof [string length $d]]
724 }
725 }} $s]
726 vwait ::total
727 after cancel {set ::total timeout}
728 close $s
729 set ::total
730 } -cleanup {
731 unset -nocomplain total
732 close $srv
733 } -result {eof 500}
734 test zlib-9.9 "bug #2818131 (gzip mismatch)" -constraints zlib -setup {
735 proc bgerror {s} {set ::total [list error $s]}
736 set srv [socket -myaddr localhost -server {apply {{c a p} {
737 chan configure $c -translation binary -buffering none -blocking 0
738 zlib push gzip $c
739 puts -nonewline $c [string repeat hello 100]
740 close $c
741 }}} 0]
742 } -body {
743 lassign [chan configure $srv -sockname] addr name port
744 after 1000 {set ::total timeout}
745 set s [socket $addr $port]
746 try {
747 chan configure $s -translation binary
748 zlib push inflate $s
749 chan event $s readable [list apply {{s} {
750 set d [read $s]
751 if {[eof $s]} {
752 chan event $s readable {}
753 set ::total [list eof [string length $d]]
754 }
755 }} $s]
756 vwait ::total
757 } finally {
758 after cancel {set ::total timeout}
759 close $s
760 }
761 set ::total
762 } -cleanup {
763 unset -nocomplain total
764 close $srv
765 rename bgerror {}
766 } -result {error {invalid block type}}
767 test zlib-9.10 "bug #2818131 (compress mismatch)" -constraints zlib -setup {
768 proc bgerror {s} {set ::total [list error $s]}
769 set srv [socket -myaddr localhost -server {apply {{c a p} {
770 chan configure $c -translation binary -buffering none -blocking 0
771 zlib push compress $c
772 puts -nonewline $c [string repeat hello 100]
773 close $c
774 }}} 0]
775 } -body {
776 lassign [chan configure $srv -sockname] addr name port
777 after 1000 {set ::total timeout}
778 set s [socket $addr $port]
779 try {
780 chan configure $s -translation binary
781 zlib push inflate $s
782 chan event $s readable [list apply {{s} {
783 set d [read $s]
784 if {[eof $s]} {
785 chan event $s readable {}
786 set ::total [list eof [string length $d]]
787 }
788 }} $s]
789 vwait ::total
790 } finally {
791 after cancel {set ::total timeout}
792 close $s
793 }
794 set ::total
795 } -cleanup {
796 unset -nocomplain total
797 close $srv
798 rename bgerror {}
799 } -result {error {invalid stored block lengths}}
800 test zlib-9.11 "bug #2818131 (deflate mismatch)" -constraints zlib -setup {
801 proc bgerror {s} {set ::total [list error $s]}
802 set srv [socket -myaddr localhost -server {apply {{c a p} {
803 chan configure $c -translation binary -buffering none -blocking 0
804 zlib push deflate $c
805 puts -nonewline $c [string repeat hello 100]
806 close $c
807 }}} 0]
808 } -body {
809 lassign [chan configure $srv -sockname] addr name port
810 after 1000 {set ::total timeout}
811 set s [socket $addr $port]
812 try {
813 chan configure $s -translation binary
814 zlib push gunzip $s
815 chan event $s readable [list apply {{s} {
816 set d [read $s]
817 if {[eof $s]} {
818 chan event $s readable {}
819 set ::total [list eof [string length $d]]
820 }
821 }} $s]
822 vwait ::total
823 } finally {
824 after cancel {set ::total timeout}
825 close $s
826 }
827 set ::total
828 } -cleanup {
829 unset -nocomplain total
830 close $srv
831 rename bgerror {}
832 } -result {error {incorrect header check}}
833
834 test zlib-10.0 "bug #2818131 (close with null interp)" -constraints {
835 zlib
836 } -setup {
837 proc bgerror {s} {set ::total [list error $s]}
838 set srv [socket -myaddr localhost -server {apply {{c a p} {
839 chan configure $c -translation binary
840 zlib push inflate $c
841 chan event $c readable [list apply {{c} {
842 set d [read $c]
843 if {[eof $c]} {
844 chan event $c readable {}
845 close $c
846 set ::total [list eof [string length $d]]
847 }
848 }} $c]
849 }}} 0]
850 } -body {
851 lassign [chan configure $srv -sockname] addr name port
852 after 1000 {set ::total timeout}
853 set s [socket $addr $port]
854 chan configure $s -translation binary -buffering none -blocking 0
855 zlib push gzip $s
856 chan event $s xyzzy [list apply {{s} {
857 if {[gets $s line] < 0} {
858 chan close $s
859 }
860 }} $s]
861 after idle [list apply {{s} {
862 puts $s test
863 chan close $s
864 after 100 {set ::total done}
865 }} $s]
866 vwait ::total
867 after cancel {set ::total timeout}
868 after cancel {set ::total done}
869 set ::total
870 } -cleanup {
871 close $srv
872 rename bgerror {}
873 } -returnCodes error \
874 -result {bad event name "xyzzy": must be readable or writable}
875 test zlib-10.1 "bug #2818131 (mismatch read)" -constraints {
876 zlib
877 } -setup {
878 proc bgerror {s} {set ::total [list error $s]}
879 proc zlibRead {c} {
880 set d [read $c]
881 if {[eof $c]} {
882 chan event $c readable {}
883 close $c
884 set ::total [list eof [string length $d]]
885 }
886 }
887 set srv [socket -myaddr localhost -server {apply {{c a p} {
888 chan configure $c -translation binary
889 zlib push inflate $c
890 chan event $c readable [list zlibRead $c]
891 }}} 0]
892 } -body {
893 lassign [chan configure $srv -sockname] addr name port
894 after 1000 {set ::total timeout}
895 set s [socket $addr $port]
896 chan configure $s -translation binary -buffering none -blocking 0
897 zlib push gzip $s
898 chan event $s readable [list zlibRead $s]
899 after idle [list apply {{s} {
900 puts $s test
901 chan close $s
902 after 100 {set ::total done}
903 }} $s]
904 vwait ::total
905 after cancel {set ::total timeout}
906 after cancel {set ::total done}
907 set ::total
908 } -cleanup {
909 close $srv
910 rename bgerror {}
911 rename zlibRead {}
912 } -result {error {invalid block type}}
913 test zlib-10.2 "bug #2818131 (mismatch gets)" -constraints {
914 zlib
915 } -setup {
916 proc bgerror {s} {set ::total [list error $s]}
917 proc zlibRead {c} {
918 if {[gets $c line] < 0} {
919 close $c
920 set ::total [list error -1]
921 } elseif {[eof $c]} {
922 chan event $c readable {}
923 close $c
924 set ::total [list eof 0]
925 }
926 }
927 set srv [socket -myaddr localhost -server {apply {{c a p} {
928 chan configure $c -translation binary
929 zlib push inflate $c
930 chan event $c readable [list zlibRead $c]
931 }}} 0]
932 } -body {
933 lassign [chan configure $srv -sockname] addr name port
934 after 1000 {set ::total timeout}
935 set s [socket $addr $port]
936 chan configure $s -translation binary -buffering none -blocking 0
937 zlib push gzip $s
938 chan event $s readable [list zlibRead $s]
939 after idle [list apply {{s} {
940 puts $s test
941 chan close $s
942 after 100 {set ::total done}
943 }} $s]
944 vwait ::total
945 after cancel {set ::total timeout}
946 after cancel {set ::total done}
947 set ::total
948 } -cleanup {
949 close $srv
950 rename bgerror {}
951 rename zlibRead {}
952 } -result {error {invalid block type}}
953
954 test zlib-11.1 "Bug #3390073: mis-applied gzip filtering" -setup {
955 set file [makeFile {} test.input]
956 } -constraints zlib -body {
957 set f [open $file wb]
958 puts -nonewline [zlib push gzip $f] [string repeat "hello" 1000]
959 close $f
960 set f [open $file rb]
961 set d [read $f]
962 close $f
963 set d [zlib gunzip $d]
964 list [regexp -all "hello" $d] [string length [regsub -all "hello" $d {}]]
965 } -cleanup {
966 removeFile $file
967 } -result {1000 0}
968 test zlib-11.2 "Bug #3390073: mis-applied gzip filtering" -setup {
969 set file [makeFile {} test.input]
970 } -constraints zlib -body {
971 set f [open $file wb]
972 puts -nonewline [zlib push gzip $f -header {filename /foo/bar}] \
973 [string repeat "hello" 1000]
974 close $f
975 set f [open $file rb]
976 set d [read $f]
977 close $f
978 set d [zlib gunzip $d -header h]
979 list [regexp -all "hello" $d] [dict get $h filename] \
980 [string length [regsub -all "hello" $d {}]]
981 } -cleanup {
982 removeFile $file
983 } -result {1000 /foo/bar 0}
984 test zlib-11.3 {Bug 3595576 variant} -setup {
985 set file [makeFile {} test.input]
986 } -constraints zlib -body {
987 set f [open $file wb]
988 puts -nonewline [zlib push gzip $f -header {filename /foo/bar}] \
989 [string repeat "hello" 1000]
990 close $f
991 set f [open $file rb]
992 set d [read $f]
993 close $f
994 zlib gunzip $d -header noSuchNs::foo
995 } -cleanup {
996 removeFile $file
997 } -returnCodes error -result {can't set "noSuchNs::foo": parent namespace doesn't exist}
998
999 test zlib-12.1 {Tk Bug 9eb55debc5} -constraints zlib -setup {
1000 set stream [zlib stream compress]
1001 } -body {
1002 for {set opts {};set y 0} {$y < 60} {incr y} {
1003 for {set line {};set x 0} {$x < 100} {incr x} {
1004 append line [binary format ccc $x $y 128]
1005 }
1006 if {$y == 59} {
1007 set opts -finalize
1008 }
1009 $stream put {*}$opts $line
1010 }
1011 set data [$stream get]
1012 list [string length $data] [string length [zlib decompress $data]]
1013 } -cleanup {
1014 $stream close
1015 } -result {12026 18000}
1016 test zlib-12.2 {Patrick Dunnigan's issue} -constraints zlib -setup {
1017 set filesrc [makeFile {} test.input]
1018 set filedst [makeFile {} test.output]
1019 set f [open $filesrc "wb"]
1020 for {set i 0} {$i < 10000} {incr i} {
1021 puts -nonewline $f "x"
1022 }
1023 close $f
1024 } -body {
1025 set fin [open $filesrc "rb"]
1026 set fout [open $filedst "wb"]
1027 set header [dict create filename "test.input" time 0]
1028 try {
1029 fcopy $fin [zlib push gzip $fout -header $header]
1030 } finally {
1031 close $fin
1032 close $fout
1033 }
1034 file size $filedst
1035 } -cleanup {
1036 removeFile $filesrc
1037 removeFile $filedst
1038 } -result 56
1039
1040 set zlibbinf ""
1041 proc _zlibbinf {} {
1042 # inlined zlib.bin file creator:
1043 variable zlibbinf
1044 if {$zlibbinf eq ""} {
1045 set zlibbinf [makeFile {} test-zlib-13.bin]
1046 set f [open $zlibbinf wb]
1047 puts -nonewline $f [zlib decompress [binary decode base64 {
1048 eJx7e+6s1+EAgYaLjK3ratptGmOck0vT/y/ZujHAd0qJelDBXfUPJ3tfrtLbpX+wOOFHmtn03/tizm
1049 /+tXROXU3d203b79p5X6/0cvUyFzTsqOj4sa9r8SrZI5zT7265e2Xzq595Fb9LbpgffVy7cZaJ/d15
1050 4U9L7LLM2vdqut8+aSU/r6q9Ltv6+T9mBhTgIK97bH33m/O1C1eBwf9FDKNgaIDaj9wA+5hToA==
1051 }]]
1052 close $f
1053 }
1054 return $zlibbinf
1055 }
1056 test zlib-13.1 {Ticket [8af92dfb66] - zlib stream mis-expansion} -constraints zlib -setup {
1057 set pathin [_zlibbinf]
1058 set chanin [open $pathin rb]
1059 set pathout [makeFile {} test-zlib-13.deflated]
1060 set chanout [open $pathout wb]
1061 zlib push inflate $chanin
1062 fcopy $chanin $chanout
1063 close $chanin
1064 close $chanout
1065 } -body {
1066 file size $pathout
1067 } -cleanup {
1068 removeFile $pathout
1069 unset chanin pathin chanout pathout
1070 } -result 458752
1071
1072 test zlib-13.2 {Ticket [f70ce1fead] - zlib multi-stream expansion} -constraints zlib -setup {
1073 # Start from the basic asset
1074 set pathin [_zlibbinf]
1075 set chanin [open $pathin rb]
1076 # Create a multi-stream by copying the asset twice into it.
1077 set pathout [makeFile {} test-zlib-13.multi]
1078 set chanout [open $pathout wb]
1079 fcopy $chanin $chanout
1080 seek $chanin 0 start
1081 fcopy $chanin $chanout
1082 close $chanin
1083 close $chanout
1084 # The multi-stream file shall be our input
1085 set pathin $pathout
1086 set chanin [open $pathin rb]
1087 # And our destinations
1088 set pathout1 [makeFile {} test-zlib-13.multi-1]
1089 set pathout2 [makeFile {} test-zlib-13.multi-2]
1090 } -body {
1091 # Decode first stream
1092 set chanout [open $pathout1 wb]
1093 zlib push inflate $chanin
1094 fcopy $chanin $chanout
1095 chan pop $chanin
1096 close $chanout
1097 # Decode second stream
1098 set chanout [open $pathout2 wb]
1099 zlib push inflate $chanin
1100 fcopy $chanin $chanout
1101 chan pop $chanin
1102 close $chanout
1103 #
1104 list [file size $pathout1] [file size $pathout2]
1105 } -cleanup {
1106 close $chanin
1107 removeFile $pathout
1108 removeFile $pathout1
1109 removeFile $pathout2
1110 unset chanin pathin chanout pathout pathout1 pathout2
1111 } -result {458752 458752}
1112
1113 if {$zlibbinf ne ""} {
1114 removeFile $zlibbinf
1115 }
1116 unset zlibbinf
1117 rename _zlibbinf {}
1118
1119 test zlib-14.1 {Bug 9ee9f4d7be: compression header added to source channel} -setup {
1120 set data hello
1121 set src [file tempfile]
1122 puts -nonewline $src $data
1123 flush $src
1124 chan configure $src -translation binary
1125 set dst [file tempfile]
1126 chan configure $dst -translation binary
1127 set result {}
1128 } -constraints knownBug -body {
1129 for {set i 0} {$i < 3} {incr i} {
1130 # Determine size of src channel
1131 seek $src 0 end
1132 set size [chan tell $src]
1133 seek $src 0 start
1134 # Determine size of content in src channel
1135 set data [read $src]
1136 set size2 [string length $data]
1137 seek $src 0 start
1138 # Copy src over to dst, keep dst empty
1139 zlib push deflate $src -level 6
1140 chan truncate $dst 0
1141 chan copy $src $dst
1142 set size3 [chan tell $dst]
1143 chan pop $src
1144 # Show sizes
1145 lappend result $size $size2 ->$size3
1146 }
1147 return $result
1148 } -cleanup {
1149 chan close $src
1150 chan close $dst
1151 } -result {5 5 ->5 5 5 ->5 5 5 ->5}
1152
1153 test zlib-15.1 {Bug cfdf80a2efc6 - negative checksums} -setup {
1154 set compressor [zlib stream gzip -header {comment "A zlib demo"}]
1155 $compressor put abcd
1156 $compressor finalize
1157 } -body {
1158 $compressor checksum
1159 } -cleanup {
1160 $compressor close
1161 } -result 3984772369
1162
1163
1164 ::tcltest::cleanupTests
1165 return
1166
1167 # Local Variables:
1168 # mode: tcl
1169 # End: