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 © 1996-1998 Sun Microsystems, Inc.
8# Copyright © 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
13if {"::tcltest" ni [namespace children]} {
14    package require tcltest 2.5
15    namespace import -force ::tcltest::*
16}
17
18testConstraint zipfs [expr {
19    [llength [info commands zlib]] && [regexp tcltest [info nameofexecutable]]
20}]
21testConstraint zipfslib 1
22
23set ziproot [zipfs root]
24set CWD [pwd]
25set tmpdir  [file join $CWD tmp]
26file mkdir $tmpdir
27
28test zipfs-0.0 {zipfs basics} -constraints zipfs -body {
29    package require tcl::zipfs
30} -result {2.0}
31test zipfs-0.1 {zipfs basics} -constraints zipfs -body {
32    expr {${ziproot} in [file volumes]}
33} -result 1
34
35if {![string match ${ziproot}* $tcl_library]} {
36    ###
37    # "make test" does not map tcl_library from the dynamic library on Unix
38    #
39    # Hack the environment to pretend we did pull tcl_library from a zip
40    # archive
41    ###
42    set tclzip [file join $CWD libtcl[info patchlevel].zip]
43    testConstraint zipfslib [file isfile $tclzip]
44    if {[testConstraint zipfslib]} {
45        zipfs mount /lib/tcl $tclzip
46        set ::tcl_library ${ziproot}lib/tcl/tcl_library
47    }
48}
49
50test zipfs-0.2 {zipfs basics} -constraints zipfslib -body {
51    string match ${ziproot}* $tcl_library
52} -result 1
53test zipfs-0.3 {zipfs basics: glob} -constraints zipfslib -setup {
54    set pwd [pwd]
55} -body {
56    cd $tcl_library
57    expr { [file join . http] in [glob -dir . http*] }
58} -cleanup {
59    cd $pwd
60} -result 1
61test zipfs-0.4 {zipfs basics: glob} -constraints zipfslib -setup {
62    set pwd [pwd]
63} -body {
64    cd $tcl_library
65    expr { [file join $tcl_library http] in [glob -dir [pwd] http*] }
66} -cleanup {
67    cd $pwd
68} -result 1
69test zipfs-0.5 {zipfs basics: glob} -constraints zipfslib -body {
70    expr { [file join $tcl_library http] in [glob -dir $tcl_library http*] }
71} -result 1
72test zipfs-0.6 {zipfs basics: glob} -constraints zipfslib -body {
73    expr { [file join $tcl_library http] in [glob [file join $tcl_library http*]] }
74} -result 1
75test zipfs-0.7 {zipfs basics: glob} -constraints zipfslib -body {
76    expr { "http" in [glob -tails -dir $tcl_library http*] }
77} -result 1
78test zipfs-0.8 {zipfs basics: glob} -constraints zipfslib -body {
79    expr { "http" in [glob -nocomplain -tails -types d -dir $tcl_library http*] }
80} -result 1
81test zipfs-0.9 {zipfs basics: glob} -constraints zipfslib -body {
82    glob -nocomplain -tails -types f -dir $tcl_library http*
83} -result {}
84test zipfs-0.10 {zipfs basics: join} -constraints {zipfs zipfslib} -body {
85    file join [zipfs root] bar baz
86} -result "[zipfs root]bar/baz"
87test zipfs-0.11 {zipfs basics: join} -constraints {zipfs zipfslib} -body {
88    file normalize [zipfs root]
89} -result "[zipfs root]"
90test zipfs-0.12 {zipfs basics: join} -constraints {zipfs zipfslib} -body {
91    file normalize [zipfs root]//bar/baz//qux/../
92} -result "[zipfs root]bar/baz"
93
94test zipfs-1.3 {zipfs errors} -constraints zipfs -returnCodes error -body {
95    zipfs mount a b c d e f
96} -result {wrong # args: should be "zipfs mount ?mountpoint? ?zipfile? ?password?"}
97test zipfs-1.4 {zipfs errors} -constraints zipfs -returnCodes error -body {
98    zipfs unmount a b c d e f
99} -result {wrong # args: should be "zipfs unmount zipfile"}
100test zipfs-1.5 {zipfs errors} -constraints zipfs -returnCodes error -body {
101    zipfs mkkey a b c d e f
102} -result {wrong # args: should be "zipfs mkkey password"}
103test zipfs-1.6 {zipfs errors} -constraints zipfs -returnCodes error -body {
104    zipfs mkimg a b c d e f
105} -result {wrong # args: should be "zipfs mkimg outfile indir ?strip? ?password? ?infile?"}
106test zipfs-1.7 {zipfs errors} -constraints zipfs -returnCodes error -body {
107    zipfs mkzip a b c d e f
108} -result {wrong # args: should be "zipfs mkzip outfile indir ?strip? ?password?"}
109test zipfs-1.8 {zipfs errors} -constraints zipfs -returnCodes error -body {
110    zipfs exists a b c d e f
111} -result {wrong # args: should be "zipfs exists filename"}
112test zipfs-1.9 {zipfs errors} -constraints zipfs -returnCodes error -body {
113    zipfs info a b c d e f
114} -result {wrong # args: should be "zipfs info filename"}
115test zipfs-1.10 {zipfs errors} -constraints zipfs -returnCodes error -body {
116    zipfs list a b c d e f
117} -result {wrong # args: should be "zipfs list ?(-glob|-regexp)? ?pattern?"}
118
119file mkdir tmp
120test zipfs-2.1 {zipfs mkzip empty archive} -constraints zipfs -returnCodes error -body {
121    zipfs mkzip [file join $tmpdir empty.zip] $tcl_library/xxxx
122} -result {empty archive}
123###
124# The next series of tests operate within a zipfile created a temporary
125# directory.
126###
127set zipfile [file join $tmpdir abc.zip]
128if {[file exists $zipfile]} {
129   file delete $zipfile
130}
131test zipfs-2.2 {zipfs mkzip} -constraints zipfs -body {
132    cd $tcl_library/encoding
133    zipfs mkzip $zipfile .
134    zipfs mount ${ziproot}abc $zipfile
135    zipfs list -glob ${ziproot}abc/cp850.*
136} -cleanup {
137    cd $CWD
138} -result "[zipfs root]abc/cp850.enc"
139testConstraint zipfsenc [zipfs exists /abc/cp850.enc]
140test zipfs-2.3 {zipfs info} -constraints {zipfs zipfsenc} -body {
141    set r [zipfs info ${ziproot}abc/cp850.enc]
142    lrange $r 0 2
143} -result [list $zipfile 1090 527] ;# NOTE: Only the first 3 results are stable
144test zipfs-2.4 {zipfs data} -constraints {zipfs zipfsenc} -body {
145    set zipfd [open ${ziproot}/abc/cp850.enc]	;# FIXME: leave open - see later test
146    read $zipfd
147} -result {# Encoding file: cp850, single-byte
148S
149003F 0 1
15000
1510000000100020003000400050006000700080009000A000B000C000D000E000F
1520010001100120013001400150016001700180019001A001B001C001D001E001F
1530020002100220023002400250026002700280029002A002B002C002D002E002F
1540030003100320033003400350036003700380039003A003B003C003D003E003F
1550040004100420043004400450046004700480049004A004B004C004D004E004F
1560050005100520053005400550056005700580059005A005B005C005D005E005F
1570060006100620063006400650066006700680069006A006B006C006D006E006F
1580070007100720073007400750076007700780079007A007B007C007D007E007F
15900C700FC00E900E200E400E000E500E700EA00EB00E800EF00EE00EC00C400C5
16000C900E600C600F400F600F200FB00F900FF00D600DC00F800A300D800D70192
16100E100ED00F300FA00F100D100AA00BA00BF00AE00AC00BD00BC00A100AB00BB
1622591259225932502252400C100C200C000A9256325512557255D00A200A52510
16325142534252C251C2500253C00E300C3255A25542569256625602550256C00A4
16400F000D000CA00CB00C8013100CD00CE00CF2518250C2588258400A600CC2580
16500D300DF00D400D200F500D500B500FE00DE00DA00DB00D900FD00DD00AF00B4
16600AD00B1201700BE00B600A700F700B800B000A800B700B900B300B225A000A0
167} ;# FIXME: result depends on content of encodings dir
168test zipfs-2.5 {zipfs exists} -constraints {zipfs zipfsenc} -body {
169    zipfs exists /abc/cp850.enc
170} -result 1
171test zipfs-2.6 {zipfs unmount while busy} -constraints {zipfs zipfsenc} -body {
172    zipfs unmount /abc
173} -returnCodes error -result {filesystem is busy}
174test zipfs-2.7 {zipfs unmount} -constraints {zipfs zipfsenc} -body {
175    close $zipfd
176    zipfs unmount /abc
177    zipfs exists /abc/cp850.enc
178} -result 0
179###
180# Repeat the tests for a buffer mounted archive
181###
182test zipfs-2.8 {zipfs mkzip} -constraints zipfs -body {
183    cd $tcl_library/encoding
184    zipfs mkzip $zipfile .
185    set fin [open $zipfile r]
186    fconfigure $fin -translation binary
187    set dat [read $fin]
188    close $fin
189    zipfs mount_data def $dat
190    zipfs list -glob ${ziproot}def/cp850.*
191} -cleanup {
192    cd $CWD
193} -result "[zipfs root]def/cp850.enc"
194testConstraint zipfsencbuf [zipfs exists /def/cp850.enc]
195test zipfs-2.9 {zipfs info} -constraints {zipfs zipfsencbuf} -body {
196    set r [zipfs info ${ziproot}def/cp850.enc]
197    lrange $r 0 2
198} -result [list {Memory Buffer} 1090 527] ;# NOTE: Only the first 3 results are stable
199test zipfs-2.10 {zipfs data} -constraints {zipfs zipfsencbuf} -body {
200    set zipfd [open ${ziproot}/def/cp850.enc]	;# FIXME: leave open - see later test
201    read $zipfd
202} -result {# Encoding file: cp850, single-byte
203S
204003F 0 1
20500
2060000000100020003000400050006000700080009000A000B000C000D000E000F
2070010001100120013001400150016001700180019001A001B001C001D001E001F
2080020002100220023002400250026002700280029002A002B002C002D002E002F
2090030003100320033003400350036003700380039003A003B003C003D003E003F
2100040004100420043004400450046004700480049004A004B004C004D004E004F
2110050005100520053005400550056005700580059005A005B005C005D005E005F
2120060006100620063006400650066006700680069006A006B006C006D006E006F
2130070007100720073007400750076007700780079007A007B007C007D007E007F
21400C700FC00E900E200E400E000E500E700EA00EB00E800EF00EE00EC00C400C5
21500C900E600C600F400F600F200FB00F900FF00D600DC00F800A300D800D70192
21600E100ED00F300FA00F100D100AA00BA00BF00AE00AC00BD00BC00A100AB00BB
2172591259225932502252400C100C200C000A9256325512557255D00A200A52510
21825142534252C251C2500253C00E300C3255A25542569256625602550256C00A4
21900F000D000CA00CB00C8013100CD00CE00CF2518250C2588258400A600CC2580
22000D300DF00D400D200F500D500B500FE00DE00DA00DB00D900FD00DD00AF00B4
22100AD00B1201700BE00B600A700F700B800B000A800B700B900B300B225A000A0
222} ;# FIXME: result depends on content of encodings dir
223test zipfs-2.11 {zipfs exists} -constraints {zipfs zipfsencbuf} -body {
224    zipfs exists /def/cp850.enc
225} -result 1
226test zipfs-2.12 {zipfs unmount while busy} -constraints {zipfs zipfsencbuf} -body {
227    zipfs unmount /def
228} -returnCodes error -result {filesystem is busy}
229test zipfs-2.13 {zipfs unmount} -constraints {zipfs zipfsencbuf} -body {
230    close $zipfd
231    zipfs unmount /def
232    zipfs exists /def/cp850.enc
233} -result 0
234
235catch {file delete -force $tmpdir}
236
237test zipfs-3.1 {zipfs in child interpreters} -constraints zipfs -setup {
238    set interp [interp create]
239} -body {
240    interp eval $interp {
241	zipfs ?
242    }
243} -returnCodes error -cleanup {
244    interp delete $interp
245} -result {unknown or ambiguous subcommand "?": must be canonical, exists, find, info, list, lmkimg, lmkzip, mkimg, mkkey, mkzip, mount, mount_data, root, or unmount}
246test zipfs-3.2 {zipfs in child interpreters} -constraints zipfs -setup {
247    set interp [interp create]
248} -body {
249    interp eval $interp {
250	zipfs mkzip
251    }
252} -returnCodes error -cleanup {
253    interp delete $interp
254} -result {wrong # args: should be "zipfs mkzip outfile indir ?strip? ?password?"}
255test zipfs-3.3 {zipfs in child interpreters} -constraints zipfs -setup {
256    set safe [interp create -safe]
257} -body {
258    interp eval $safe {
259	zipfs ?
260    }
261} -returnCodes error -cleanup {
262    interp delete $safe
263} -result {unknown or ambiguous subcommand "?": must be canonical, exists, find, info, list, lmkimg, lmkzip, mkimg, mkkey, mkzip, mount, mount_data, root, or unmount}
264test zipfs-3.4 {zipfs in child interpreters} -constraints zipfs -setup {
265    set safe [interp create -safe]
266} -body {
267    interp eval $safe {
268	zipfs mkzip
269    }
270} -returnCodes error -cleanup {
271    interp delete $safe
272} -result {not allowed to invoke subcommand mkzip of zipfs}
273
274test zipfs-4.1 {zipfs lmkimg} -constraints zipfs -setup {
275    set baseImage [makeFile "return sourceWorking\n\x1A" base]
276    set targetImage [makeFile "" target]
277    set addFile [makeFile "return mountWorking" add.data]
278    file delete $targetImage
279} -body {
280    zipfs lmkimg $targetImage [list $addFile test/add.tcl] {} $baseImage
281    zipfs mount ziptest $targetImage
282    try {
283	list [source $targetImage] [source //zipfs:/ziptest/test/add.tcl]
284    } finally {
285	zipfs unmount ziptest
286    }
287} -cleanup {
288    removeFile $baseImage
289    removeFile $targetImage
290    removeFile $addFile
291} -result {sourceWorking mountWorking}
292test zipfs-4.2 {zipfs lmkimg: making an image from an image} -constraints zipfs -setup {
293    set baseImage [makeFile "return sourceWorking\n\x1A" base_image.tcl]
294    set midImage [makeFile "" mid_image.tcl]
295    set targetImage [makeFile "" target_image.tcl]
296    set addFile [makeFile "return mountWorking" add.data]
297    file delete $midImage $targetImage
298} -body {
299    zipfs lmkimg $midImage [list $addFile test/ko.tcl] {} $baseImage
300    zipfs lmkimg $targetImage [list $addFile test/ok.tcl] {} $midImage
301    zipfs mount ziptest $targetImage
302    try {
303	list [glob -tails -directory //zipfs://ziptest/test *.tcl] \
304	    [if {[file size $midImage] == [file size $targetImage]} {
305		string cat equal
306	    } else {
307		list mid=[file size $midImage] target=[file size $targetImage]
308	    }]
309    } finally {
310	zipfs unmount ziptest
311    }
312} -cleanup {
313    removeFile $baseImage
314    removeFile $midImage
315    removeFile $targetImage
316    removeFile $addFile
317} -result {ok.tcl equal}
318test zipfs-4.3 {zipfs lmkimg: stripping password} -constraints zipfs -setup {
319    set baseImage [makeFile "return sourceWorking\n\x1A" base_image.tcl]
320    set midImage [makeFile "" mid_image.tcl]
321    set targetImage [makeFile "" target_image.tcl]
322    set addFile [makeFile "return mountWorking" add.data]
323    file delete $midImage $targetImage
324} -body {
325    set pass gorp
326    zipfs lmkimg $midImage [list $addFile test/add.tcl] $pass $baseImage
327    zipfs lmkimg $targetImage [list $addFile test/ok.tcl] {} $midImage
328    zipfs mount ziptest $targetImage
329    try {
330	glob -tails -directory //zipfs://ziptest/test *.tcl
331    } finally {
332	zipfs unmount ziptest
333    }
334} -cleanup {
335    removeFile $baseImage
336    removeFile $midImage
337    removeFile $targetImage
338    removeFile $addFile
339} -result {ok.tcl}
340test zipfs-4.4 {zipfs lmkimg: final password} -constraints zipfs -setup {
341    set baseImage [makeFile "return sourceWorking\n\x1A" base_image.tcl]
342    set midImage [makeFile "" mid_image.tcl]
343    set targetImage [makeFile "" target_image.tcl]
344    set addFile [makeFile "return mountWorking" add.data]
345    file delete $midImage $targetImage
346} -body {
347    set pass gorp
348    zipfs lmkimg $midImage [list $addFile test/add.tcl] {} $baseImage
349    zipfs lmkimg $targetImage [list $addFile test/ok.tcl] $pass $midImage
350    zipfs mount ziptest $targetImage
351    try {
352	glob -tails -directory //zipfs://ziptest/test *.tcl
353    } finally {
354	zipfs unmount ziptest
355    }
356} -cleanup {
357    removeFile $baseImage
358    removeFile $midImage
359    removeFile $targetImage
360    removeFile $addFile
361} -result {ok.tcl}
362test zipfs-4.5 {zipfs lmkimg: making image from mounted} -constraints zipfs -setup {
363    set baseImage [makeFile "return sourceWorking\n\x1A" base_image.tcl]
364    set midImage [makeFile "" mid_image.tcl]
365    set targetImage [makeFile "" target_image.tcl]
366    set addFile [makeFile "return mountWorking" add.data]
367    file delete $midImage $targetImage
368} -body {
369    zipfs lmkimg $midImage [list $addFile test/add.tcl] {} $baseImage
370    zipfs mount ziptest $midImage
371    set f [glob -directory //zipfs://ziptest/test *.tcl]
372    zipfs lmkimg $targetImage [list $f test/ok.tcl] {} $midImage
373    zipfs unmount ziptest
374    zipfs mount ziptest $targetImage
375    list $f [glob -directory //zipfs://ziptest/test *.tcl]
376} -cleanup {
377    zipfs unmount ziptest
378    removeFile $baseImage
379    removeFile $midImage
380    removeFile $targetImage
381    removeFile $addFile
382} -result {//zipfs://ziptest/test/add.tcl //zipfs://ziptest/test/ok.tcl}
383
384test zipfs-5.1 {zipfs mount_data: short data} -constraints zipfs -body {
385    zipfs mount_data gorp {}
386} -returnCodes error -result {bad zip data}
387test zipfs-5.2 {zipfs mount_data: short data} -constraints zipfs -body {
388    zipfs mount_data gorp gorpGORPgorp
389} -returnCodes error -result {bad zip data}
390test zipfs-5.3 {zipfs mount_data: short data} -constraints zipfs -body {
391    set data PK\x03\x04.....................................
392    append data PK\x01\x02.....................................
393    append data PK\x05\x06.....................................
394    zipfs mount_data gorp $data
395} -returnCodes error -result {bad zip data}
396test zipfs-5.4 {zipfs mount_data: bad arg count} -constraints zipfs -body {
397    zipfs mount_data gorp {} foobar
398} -returnCodes error -result {wrong # args: should be "zipfs mount_data ?mountpoint? ?data?"}
399
400test zipfs-6.1 {zipfs mkkey} -constraints zipfs -body {
401    binary scan [zipfs mkkey gorp] cu* x
402    return $x
403} -result {224 226 111 103 4 80 75 90 90}
404
405::tcltest::cleanupTests
406return
407
408# Local Variables:
409# mode: tcl
410# End:
411