summaryrefslogtreecommitdiffstats
path: root/tests/zlib.test
blob: b8d6e66d5af78cec750e0a60f75c348b73d14489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# The file tests the tclZlib.c file.
#
# This file contains a collection of tests for one or more of the Tcl built-in
# commands. Sourcing this file into Tcl runs the tests and generates output
# for errors. No output means no errors were found.
#
# Copyright (c) 1996-1998 by Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: zlib.test,v 1.9 2009/07/05 21:46:27 patthoyts Exp $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest 2.1
    namespace import -force ::tcltest::*
}

testConstraint zlib [llength [info commands zlib]]

test zlib-1.1 {zlib basics} -constraints zlib -returnCodes error -body {
    zlib
} -result {wrong # args: should be "zlib command arg ?...?"}
test zlib-1.2 {zlib basics} -constraints zlib -returnCodes error -body {
    zlib ? {}
} -result {bad command "?": must be adler32, compress, crc32, decompress, deflate, gunzip, gzip, inflate, push, or stream}

test zlib-2.1 {zlib compress/decompress} zlib {
    zlib decompress [zlib compress abcdefghijklm]
} abcdefghijklm

test zlib-3.1 {zlib deflate/inflate} zlib {
    zlib inflate [zlib deflate abcdefghijklm]
} abcdefghijklm

test zlib-4.1 {zlib gzip/gunzip} zlib {
    zlib gunzip [zlib gzip abcdefghijklm]
} abcdefghijklm
test zlib-4.2 {zlib gzip/gunzip} zlib {
    set s [string repeat abcdef 5]
    list [zlib gunzip [zlib gzip $s -header {comment gorp}] -header head] \
	[dict get $head comment] [dict get $head size]
} {abcdefabcdefabcdefabcdefabcdef gorp 30}

test zlib-5.1 {zlib adler32} zlib {
    format %x [expr {[zlib adler32 abcdeabcdeabcdeabcdeabcdeabcde] & 0xffffffff}]
} b3b50b9b
test zlib-5.2 {zlib adler32} zlib {
    format %x [expr {[zlib adler32 abcdeabcdeabcdeabcdeabcdeabcde 42] & 0xffffffff}]
} b8830bc4
test zlib-5.3 {zlib adler32} -constraints zlib -returnCodes error -body {
    zlib adler32 abcdeabcdeabcdeabcdeabcdeabcde 42 x
} -result {wrong # args: should be "zlib adler32 data ?startValue?"}

test zlib-6.1 {zlib crc32} zlib {
    format %x [expr {[zlib crc32 abcdeabcdeabcdeabcdeabcdeabcde] & 0xffffffff}]
} 6f73e901
test zlib-6.2 {zlib crc32} zlib {
    format %x [expr {[zlib crc32 abcdeabcdeabcdeabcdeabcdeabcde 42] & 0xffffffff}]
} ce1c4914
test zlib-6.3 {zlib crc32} -constraints zlib -returnCodes error -body {
    zlib crc32 abcdeabcdeabcdeabcdeabcdeabcde 42 x
} -result {wrong # args: should be "zlib crc32 data ?startValue?"}
test zlib-6.4 {zlib crc32: bug 2662434} -constraints zlib -body {
    zlib crc32 "dabale arroz a la zorra el abad"
} -result 3842832571

test zlib-7.0 {zlib stream} -constraints zlib -returnCodes error -setup {
    set s [zlib stream compress]
} -body {
    $s ?
} -cleanup {
    $s close
} -result {bad option "?": must be add, checksum, close, eof, finalize, flush, fullflush, get, put, or reset}
test zlib-7.1 {zlib stream} zlib {
    set s [zlib stream compress]
    $s put -finalize abcdeEDCBA
    set data [$s get]
    set result [list [$s get] [format %x [$s checksum]]]
    $s close
    lappend result [zlib decompress $data]
} {{} 136f033f abcdeEDCBA}
test zlib-7.2 {zlib stream} zlib {
    set s [zlib stream decompress]
    $s put -finalize [zlib compress abcdeEDCBA]
    set data [$s get]
    set result [list [$s get] [format %x [$s checksum]]]
    $s close
    lappend result $data
} {{} 136f033f abcdeEDCBA}
test zlib-7.3 {zlib stream} zlib {
    set s [zlib stream deflate]
    $s put -finalize abcdeEDCBA
    set data [$s get]
    set result [list [$s get] [format %x [$s checksum]]]
    $s close
    lappend result [zlib inflate $data]
} {{} 1 abcdeEDCBA}
test zlib-7.4 {zlib stream} zlib {
    set s [zlib stream inflate]
    $s put -finalize [zlib deflate abcdeEDCBA]
    set data [$s get]
    set result [list [$s get] [format %x [$s checksum]]]
    $s close
    lappend result $data
} {{} 1 abcdeEDCBA}

test zlib-8.1 {zlib transformation} -constraints zlib -setup {
    set file [makeFile {} test.gz]
} -body {
    set f [zlib push gzip [open $file w] -header {comment gorp}]
    puts $f "ok"
    close $f
    set f [zlib push gunzip [open $file]]
    list [gets $f] [dict get [chan configure $f -header] comment]
} -cleanup {
    close $f
    removeFile $file
} -result {ok gorp}
test zlib-8.2 {zlib transformation} -constraints zlib -setup {
    set file [makeFile {} test.z]
} -body {
    set f [zlib push compress [open $file w]]
    puts $f "ok"
    close $f
    set f [zlib push decompress [open $file]]
    gets $f
} -cleanup {
    close $f
    removeFile $file
} -result ok
test zlib-8.3 {zlib transformation and fileevent} -constraints zlib -setup {
    set srv [socket -myaddr localhost -server {apply {{c a p} {
        fconfigure $c -translation binary
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    set port [lindex [fconfigure $srv -sockname] 2]
    set file [makeFile {} test.gz]
    set fout [open $file wb]
} -body {
    set sin [socket localhost $port]
    try {
	fconfigure $sin -translation binary
	zlib push gunzip $sin
	after 1000 {set total timeout}
	fcopy $sin $fout -command {apply {{c {e {}}} {
	    set ::total [expr {$e eq {} ? $c : $e}]
	}}}
	vwait total
    } finally {
	close $sin
    }
    append total --> [file size $file]
} -cleanup {
    close $fout
    close $srv
    removeFile $file
} -result 81920-->81920
test zlib-9.1 "check fcopy with push" -constraints zlib -setup {
    set sfile [makeFile {} testsrc.gz]
    set file [makeFile {} test.gz]
    set f [open $sfile wb]
    puts -nonewline $f [zlib gzip [string repeat a 81920]]
    close $f
} -body {
    set fin [zlib push gunzip [open $sfile rb]]
    set fout [open $file wb]
    set total [fcopy $fin $fout]
    close $fin ; close $fout
    list copied $total size [file size $file]
} -cleanup {
    removeFile $file
    removeFile $sfile
} -returnCodes {ok} -result {copied 81920 size 81920}
test zlib-9.2 "socket fcopy with push" -constraints zlib -setup {
    set srv [socket -server {apply {{c a p} {
        chan configure $c -encoding binary -translation binary
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    set sin [socket $addr $port]
    chan configure $sin -translation binary
    zlib push gunzip $sin
    update
    set total [fcopy $sin [set fout [open $file wb]]]
    close $sin
    close $fout
    list read $total size [file size $file]
} -cleanup {
    close $srv
    removeFile $file
} -returnCodes {ok error} -result {read 81920 size 81920}
test zlib-9.3 "socket fcopy bg (identity)" -constraints zlib -setup {
    set srv [socket -server {apply {{c a p} {
        chan configure $c -encoding binary -translation binary
        puts -nonewline $c [string repeat a 81920]
        close $c
    }}} 0]
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    set sin [socket $addr $port]
    chan configure $sin -translation binary
    update
    set fout [open $file wb]
    after 1000 {set ::total timeout}
    fcopy $sin $fout -command {apply {{c {e {}}} {
        set ::total [expr {$e eq {} ? $c : $e}]
    }}}
    vwait ::total
    close $sin; close $fout
    list read $::total size [file size $file]
} -cleanup {
    close $srv
} -returnCodes {ok error} -result {read 81920 size 81920}
test zlib-9.4 "socket fcopy bg (gzip)" -constraints zlib -setup {
    set srv [socket -server {apply {{c a p} {
        chan configure $c -encoding binary -translation binary
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    set sin [socket $addr $port]
    chan configure $sin -translation binary
    zlib push gunzip $sin
    update
    set fout [open $file wb]
    after 1000 {set ::total timeout}
    fcopy $sin $fout -command {apply {{c {e {}}} {
        set ::total [expr {$e eq {} ? $c : $e}]
    }}}
    vwait ::total
    close $sin; close $fout
    list read $::total size [file size $file]
} -cleanup {
    close $srv
    removeFile $file
} -result {read 81920 size 81920}
test zlib-9.5 "socket fcopy incremental (gzip)" -constraints zlib -setup {
    set srv [socket -server {apply {{c a p} {
        chan configure $c -encoding binary -translation binary
        puts -nonewline $c [zlib gzip [string repeat a 81920]]
        close $c
    }}} 0]
    proc zlib95copy {i o t c {e {}}} {
        incr t $c
        if {$e ne {}} {
            set ::total [list error $e]
        } elseif {[eof $i]} {
            set ::total [list eof $t]
        } else {
            fcopy $i $o -size 8192 -command [list zlib95copy $i $o $t]
        }
    }
    set file [makeFile {} test.gz]
} -body {
    lassign [chan configure $srv -sockname] addr name port
    set sin [socket $addr $port]
    chan configure $sin -translation binary
    zlib push gunzip $sin
    update
    set fout [open $file wb]
    after 1000 {set ::total timeout}
    fcopy $sin $fout -size 8192 -command [list zlib95copy $sin $fout 0]
    vwait ::total
    close $sin; close $fout
    list $::total size [file size $file]
} -cleanup {
    close $srv
    rename zlib95copy {}
    removeFile $file
} -result {{eof 81920} size 81920}

::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End: