summaryrefslogtreecommitdiffstats
path: root/tests/zipfs.test
blob: f661e503aa2c36c56fa9f29ccff060056df0748b (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
# 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 © 1996-1998 Sun Microsystems, Inc.
# Copyright © 1998-1999 Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {"::tcltest" ni [namespace children]} {
    package require tcltest 2.5
    namespace import -force ::tcltest::*
}
source [file join [file dirname [info script]] tcltests.tcl]

testConstraint zipfs [expr {[llength [info commands zipfs]]}]
testConstraint zipfslib 1


set ziproot [zipfs root]
set CWD [pwd]
set tmpdir  [file join $CWD tmp]
file mkdir $tmpdir

test zipfs-0.0 {zipfs basics} -constraints zipfs -body {
    package require tcl::zipfs
} -result {2.0}
test zipfs-0.1 {zipfs basics} -constraints zipfs -body {
    expr {${ziproot} in [file volumes]}
} -result 1

if {![string match ${ziproot}* $tcl_library]} {
    ###
    # "make test" does not map tcl_library from the dynamic library on Unix
    #
    # Hack the environment to pretend we did pull tcl_library from a zip
    # archive
    ###
    set tclzip [file join $CWD libtcl[info patchlevel].zip]
    testConstraint zipfslib [file isfile $tclzip]
    if {[testConstraint zipfslib]} {
        zipfs mount $tclzip /lib/tcl
        set ::tcl_library ${ziproot}lib/tcl/tcl_library
    }
}

test zipfs-0.2 {zipfs basics} -constraints zipfslib -body {
    string match ${ziproot}* $tcl_library
} -result 1
test zipfs-0.3 {zipfs basics: glob} -constraints zipfslib -setup {
    set pwd [pwd]
} -body {
    cd $tcl_library
    expr { [file join . http] in [glob -dir . http*] }
} -cleanup {
    cd $pwd
} -result 1
test zipfs-0.4 {zipfs basics: glob} -constraints zipfslib -setup {
    set pwd [pwd]
} -body {
    cd $tcl_library
    expr { [file join $tcl_library http] in [glob -dir [pwd] http*] }
} -cleanup {
    cd $pwd
} -result 1
test zipfs-0.5 {zipfs basics: glob} -constraints zipfslib -body {
    expr { [file join $tcl_library http] in [glob -dir $tcl_library http*] }
} -result 1
test zipfs-0.6 {zipfs basics: glob} -constraints zipfslib -body {
    expr { [file join $tcl_library http] in [glob [file join $tcl_library http*]] }
} -result 1
test zipfs-0.7 {zipfs basics: glob} -constraints zipfslib -body {
    expr { "http" in [glob -tails -dir $tcl_library http*] }
} -result 1
test zipfs-0.8 {zipfs basics: glob} -constraints zipfslib -body {
    expr { "http" in [glob -nocomplain -tails -types d -dir $tcl_library http*] }
} -result 1
test zipfs-0.9 {zipfs basics: glob} -constraints zipfslib -body {
    glob -nocomplain -tails -types f -dir $tcl_library http*
} -result {}
test zipfs-0.10 {zipfs basics: join} -constraints {zipfs zipfslib} -body {
    file join ${ziproot} bar baz
} -result "${ziproot}bar/baz"
test zipfs-0.11 {zipfs basics: join} -constraints {zipfs zipfslib} -body {
    file normalize ${ziproot}
} -result "${ziproot}"
test zipfs-0.12 {zipfs basics: join} -constraints {zipfs zipfslib} -body {
    file normalize ${ziproot}//bar/baz//qux/../
} -result "${ziproot}bar/baz"

file mkdir tmp
test zipfs-2.1 {zipfs mkzip empty archive} -constraints zipfs -returnCodes error -body {
    zipfs mkzip [file join $tmpdir empty.zip] $tcl_library/xxxx
} -result {empty archive}
###
# The next series of tests operate within a zipfile created a temporary
# directory.
###
set zipfile [file join $tmpdir abc.zip]
if {[file exists $zipfile]} {
   file delete $zipfile
}
test zipfs-2.2 {zipfs mkzip} -constraints zipfs -body {
    cd $tcl_library/encoding
    zipfs mkzip $zipfile .
    zipfs mount $zipfile ${ziproot}abc
    zipfs list -glob ${ziproot}abc/cp850.*
} -cleanup {
    cd $CWD
} -result "${ziproot}abc/cp850.enc"
testConstraint zipfsenc [zipfs exists ${ziproot}abc/cp850.enc]
test zipfs-2.3 {zipfs info} -constraints {zipfs zipfsenc} -body {
    set r [zipfs info ${ziproot}abc/cp850.enc]
    lrange $r 0 2
} -result [list $zipfile 1090 527] ;# NOTE: Only the first 3 results are stable
test zipfs-2.4 {zipfs data} -constraints {zipfs zipfsenc} -body {
    set zipfd [open ${ziproot}/abc/cp850.enc]	;# FIXME: leave open - see later test
    read $zipfd
} -result {# Encoding file: cp850, single-byte
S
003F 0 1
00
0000000100020003000400050006000700080009000A000B000C000D000E000F
0010001100120013001400150016001700180019001A001B001C001D001E001F
0020002100220023002400250026002700280029002A002B002C002D002E002F
0030003100320033003400350036003700380039003A003B003C003D003E003F
0040004100420043004400450046004700480049004A004B004C004D004E004F
0050005100520053005400550056005700580059005A005B005C005D005E005F
0060006100620063006400650066006700680069006A006B006C006D006E006F
0070007100720073007400750076007700780079007A007B007C007D007E007F
00C700FC00E900E200E400E000E500E700EA00EB00E800EF00EE00EC00C400C5
00C900E600C600F400F600F200FB00F900FF00D600DC00F800A300D800D70192
00E100ED00F300FA00F100D100AA00BA00BF00AE00AC00BD00BC00A100AB00BB
2591259225932502252400C100C200C000A9256325512557255D00A200A52510
25142534252C251C2500253C00E300C3255A25542569256625602550256C00A4
00F000D000CA00CB00C8013100CD00CE00CF2518250C2588258400A600CC2580
00D300DF00D400D200F500D500B500FE00DE00DA00DB00D900FD00DD00AF00B4
00AD00B1201700BE00B600A700F700B800B000A800B700B900B300B225A000A0
} ;# FIXME: result depends on content of encodings dir
test zipfs-2.5 {zipfs exists} -constraints {zipfs zipfsenc} -body {
    zipfs exists ${ziproot}abc/cp850.enc
} -result 1
test zipfs-2.6 {zipfs unmount while busy} -constraints {zipfs zipfsenc} -body {
    zipfs unmount /abc
} -returnCodes error -result {filesystem is busy}
test zipfs-2.7 {zipfs unmount} -constraints {zipfs zipfsenc} -body {
    close $zipfd
    zipfs unmount /abc
    zipfs exists /abc/cp850.enc
} -result 0
###
# Repeat the tests for a buffer mounted archive
###
test zipfs-2.8 {zipfs mkzip} -constraints zipfs -body {
    cd $tcl_library/encoding
    zipfs mkzip $zipfile .
    set fin [open $zipfile r]
    fconfigure $fin -translation binary
    set dat [read $fin]
    close $fin
    zipfs mount_data $dat def
    zipfs list -glob ${ziproot}def/cp850.*
} -cleanup {
    cd $CWD
} -result "${ziproot}def/cp850.enc"
testConstraint zipfsencbuf [zipfs exists ${ziproot}def/cp850.enc]
test zipfs-2.9 {zipfs info} -constraints {zipfs zipfsencbuf} -body {
    set r [zipfs info ${ziproot}def/cp850.enc]
    lrange $r 0 2
} -result [list {Memory Buffer} 1090 527] ;# NOTE: Only the first 3 results are stable
test zipfs-2.10 {zipfs data} -constraints {zipfs zipfsencbuf} -body {
    set zipfd [open ${ziproot}/def/cp850.enc]	;# FIXME: leave open - see later test
    read $zipfd
} -result {# Encoding file: cp850, single-byte
S
003F 0 1
00
0000000100020003000400050006000700080009000A000B000C000D000E000F
0010001100120013001400150016001700180019001A001B001C001D001E001F
0020002100220023002400250026002700280029002A002B002C002D002E002F
0030003100320033003400350036003700380039003A003B003C003D003E003F
0040004100420043004400450046004700480049004A004B004C004D004E004F
0050005100520053005400550056005700580059005A005B005C005D005E005F
0060006100620063006400650066006700680069006A006B006C006D006E006F
0070007100720073007400750076007700780079007A007B007C007D007E007F
00C700FC00E900E200E400E000E500E700EA00EB00E800EF00EE00EC00C400C5
00C900E600C600F400F600F200FB00F900FF00D600DC00F800A300D800D70192
00E100ED00F300FA00F100D100AA00BA00BF00AE00AC00BD00BC00A100AB00BB
2591259225932502252400C100C200C000A9256325512557255D00A200A52510
25142534252C251C2500253C00E300C3255A25542569256625602550256C00A4
00F000D000CA00CB00C8013100CD00CE00CF2518250C2588258400A600CC2580
00D300DF00D400D200F500D500B500FE00DE00DA00DB00D900FD00DD00AF00B4
00AD00B1201700BE00B600A700F700B800B000A800B700B900B300B225A000A0
} ;# FIXME: result depends on content of encodings dir
test zipfs-2.11 {zipfs exists} -constraints {zipfs zipfsencbuf} -body {
    zipfs exists ${ziproot}def/cp850.enc
} -result 1
test zipfs-2.12 {zipfs unmount while busy} -constraints {zipfs zipfsencbuf} -body {
    zipfs unmount /def
} -returnCodes error -result {filesystem is busy}
test zipfs-2.13 {zipfs unmount} -constraints {zipfs zipfsencbuf} -body {
    close $zipfd
    zipfs unmount /def
    zipfs exists /def/cp850.enc
} -result 0

catch {file delete -force $tmpdir}

test zipfs-3.1 {zipfs in child interpreters} -constraints zipfs -setup {
    set interp [interp create]
} -body {
    interp eval $interp {
	zipfs ?
    }
} -returnCodes error -cleanup {
    interp delete $interp
} -result {unknown or ambiguous subcommand "?": must be canonical, exists, find, info, list, lmkimg, lmkzip, mkimg, mkkey, mkzip, mount, mount_data, root, or unmount}
test zipfs-3.2 {zipfs in child interpreters} -constraints zipfs -setup {
    set interp [interp create]
} -body {
    interp eval $interp {
	zipfs mkzip
    }
} -returnCodes error -cleanup {
    interp delete $interp
} -result {wrong # args: should be "zipfs mkzip outfile indir ?strip? ?password?"}
test zipfs-3.3 {zipfs in child interpreters} -constraints zipfs -setup {
    set safe [interp create -safe]
} -body {
    interp eval $safe {
	zipfs ?
    }
} -returnCodes error -cleanup {
    interp delete $safe
} -result {unknown or ambiguous subcommand "?": must be canonical, exists, find, info, list, lmkimg, lmkzip, mkimg, mkkey, mkzip, mount, mount_data, root, or unmount}
test zipfs-3.4 {zipfs in child interpreters} -constraints zipfs -setup {
    set safe [interp create -safe]
} -body {
    interp eval $safe {
	zipfs mkzip
    }
} -returnCodes error -cleanup {
    interp delete $safe
} -result {not allowed to invoke subcommand mkzip of zipfs}

test zipfs-4.1 {zipfs lmkimg} -constraints zipfs -setup {
    set baseImage [makeFile "return sourceWorking\n\x1A" base]
    set targetImage [makeFile "" target]
    set addFile [makeFile "return mountWorking" add.data]
    file delete $targetImage
} -body {
    zipfs lmkimg $targetImage [list $addFile test/add.tcl] {} $baseImage
    zipfs mount $targetImage ziptest
    try {
	list [source $targetImage] [source ${ziproot}ziptest/test/add.tcl]
    } finally {
	zipfs unmount ziptest
    }
} -cleanup {
    removeFile $baseImage
    removeFile $targetImage
    removeFile $addFile
} -result {sourceWorking mountWorking}
test zipfs-4.2 {zipfs lmkimg: making an image from an image} -constraints zipfs -setup {
    set baseImage [makeFile "return sourceWorking\n\x1A" base_image.tcl]
    set midImage [makeFile "" mid_image.tcl]
    set targetImage [makeFile "" target_image.tcl]
    set addFile [makeFile "return mountWorking" add.data]
    file delete $midImage $targetImage
} -body {
    zipfs lmkimg $midImage [list $addFile test/ko.tcl] {} $baseImage
    zipfs lmkimg $targetImage [list $addFile test/ok.tcl] {} $midImage
    zipfs mount $targetImage ziptest
    try {
	list [glob -tails -directory ${ziproot}/ziptest/test *.tcl] \
	    [if {[file size $midImage] == [file size $targetImage]} {
		string cat equal
	    } else {
		list mid=[file size $midImage] target=[file size $targetImage]
	    }]
    } finally {
	zipfs unmount ziptest
    }
} -cleanup {
    removeFile $baseImage
    removeFile $midImage
    removeFile $targetImage
    removeFile $addFile
} -result {ok.tcl equal}
test zipfs-4.3 {zipfs lmkimg: stripping password} -constraints zipfs -setup {
    set baseImage [makeFile "return sourceWorking\n\x1A" base_image.tcl]
    set midImage [makeFile "" mid_image.tcl]
    set targetImage [makeFile "" target_image.tcl]
    set addFile [makeFile "return mountWorking" add.data]
    file delete $midImage $targetImage
} -body {
    set pass gorp
    zipfs lmkimg $midImage [list $addFile test/add.tcl] $pass $baseImage
    zipfs lmkimg $targetImage [list $addFile test/ok.tcl] {} $midImage
    zipfs mount $targetImage ziptest
    try {
	glob -tails -directory ${ziproot}/ziptest/test *.tcl
    } finally {
	zipfs unmount ziptest
    }
} -cleanup {
    removeFile $baseImage
    removeFile $midImage
    removeFile $targetImage
    removeFile $addFile
} -result {ok.tcl}
test zipfs-4.4 {zipfs lmkimg: final password} -constraints zipfs -setup {
    set baseImage [makeFile "return sourceWorking\n\x1A" base_image.tcl]
    set midImage [makeFile "" mid_image.tcl]
    set targetImage [makeFile "" target_image.tcl]
    set addFile [makeFile "return mountWorking" add.data]
    file delete $midImage $targetImage
} -body {
    set pass gorp
    zipfs lmkimg $midImage [list $addFile test/add.tcl] {} $baseImage
    zipfs lmkimg $targetImage [list $addFile test/ok.tcl] $pass $midImage
    zipfs mount $targetImage ziptest
    try {
	glob -tails -directory ${ziproot}/ziptest/test *.tcl
    } finally {
	zipfs unmount ziptest
    }
} -cleanup {
    removeFile $baseImage
    removeFile $midImage
    removeFile $targetImage
    removeFile $addFile
} -result {ok.tcl}
test zipfs-4.5 {zipfs lmkimg: making image from mounted} -constraints zipfs -setup {
    set baseImage [makeFile "return sourceWorking\n\x1A" base_image.tcl]
    set midImage [makeFile "" mid_image.tcl]
    set targetImage [makeFile "" target_image.tcl]
    set addFile [makeFile "return mountWorking" add.data]
    file delete $midImage $targetImage
} -body {
    zipfs lmkimg $midImage [list $addFile test/add.tcl] {} $baseImage
    zipfs mount $midImage ziptest
    set f [glob -directory ${ziproot}/ziptest/test *.tcl]
    zipfs lmkimg $targetImage [list $f test/ok.tcl] {} $midImage
    zipfs unmount ziptest
    zipfs mount $targetImage ziptest
    list $f [glob -directory ${ziproot}/ziptest/test *.tcl]
} -cleanup {
    zipfs unmount ziptest
    removeFile $baseImage
    removeFile $midImage
    removeFile $targetImage
    removeFile $addFile
} -result [list ${ziproot}/ziptest/test/add.tcl ${ziproot}/ziptest/test/ok.tcl]

test zipfs-5.1 {zipfs mount_data: short data} -constraints zipfs -body {
    zipfs mount_data {} gorp
} -returnCodes error -result {archive directory end signature not found}
test zipfs-5.2 {zipfs mount_data: short data} -constraints zipfs -body {
    zipfs mount_data gorpGORPgorp gorp
} -returnCodes error -result {archive directory end signature not found}
test zipfs-5.3 {zipfs mount_data: short data} -constraints zipfs -body {
    set data PK\x03\x04.....................................
    append data PK\x01\x02.....................................
    append data PK\x05\x06.....................................
    zipfs mount_data $data gorp
} -returnCodes error -result {archive directory truncated}
test zipfs-5.4 {zipfs mount_data: bad arg count} -constraints zipfs -body {
    zipfs mount_data {} gorp foobar
} -returnCodes error -result {wrong # args: should be "zipfs mount_data ?data? ?mountpoint?"}

test zipfs-6.1 {zipfs mkkey} -constraints zipfs -body {
    binary scan [zipfs mkkey gorp] cu* x
    return $x
} -result {224 226 111 103 4 80 75 90 90}


#
# Additional tests for more coverage. Some of the ones above may be duplicated.

namespace eval test_ns_zipfs {
    namespace import ::tcltest::test
    namespace path ::tcltests
    variable zipTestDir [file normalize [file join [file dirname [info script]] zipfiles]]
    variable defaultMountPoint [file join [zipfs root] testmount]

    proc readbin {path} {
        set fd [open $path rb]
        set data [read $fd]
        close $fd
        return $data
    }

    # Wrapper to ease transition if Tcl changes order of argument to zipfs mount
    # or the zipfs prefix
    proc mount [list zippath [list mountpoint $defaultMountPoint]] {
        zipfs mount $zippath $mountpoint
    }

    # Make full path to zip file
    proc zippath {zippath} {
        variable zipTestDir
        if {[file pathtype $zippath] eq "absolute"} {
            return $zippath
        } else {
            return [file join $zipTestDir $zippath]
        }
    }

    # list of paths -> list of paths under [zipfs root]
    proc zipfspaths {args} {
        return [lmap path $args {file join [zipfs root] $path}]
    }

    proc cleanup {} {
        dict for {mount -} [zipfs mount] {
            if {[string match //zipfs:/test* $mount]} {
                zipfs unmount $mount
            }
        }
        zipfs unmount [zipfs root]
    }

    proc mounttarget {mountpoint} {
        return [dict getdef [zipfs mount] $mountpoint ""]
    }

    #
    # zipfs root - only arg count check since do not want to assume
    # what it resolves to
    testnumargs "zipfs root" "" ""

    #
    # zipfs mount

    proc testbadmount {id zippath messagePattern args} {
        variable defaultMountPoint
        set zippath [zippath $zippath]
        test zipfs-mount-$id $id -body {
            list [catch {mount $zippath} message] \
                [string match $messagePattern $message] \
                [mounttarget $defaultMountPoint]
        } -cleanup {
            # In case mount succeeded when it should not
            cleanup
        } -result {1 1 {}} {*}$args

        if {![file exists $zippath]} {
            return
        }
        set data [readbin $zippath]
        test zipfs-mount_data-$id $id -body {
            list [catch {zipfs mount_data $data $defaultMountPoint} message] \
                [string match $messagePattern $message] \
                [mounttarget $defaultMountPoint]
        } -cleanup {
            # In case mount succeeded when it should not
            cleanup
        } -result {1 1 {}} {*}$args
    }

    # Generates tests for file, file on root, memory buffer cases for an archive
    proc testmount {id zippath checkPath mountpoint args} {
        set zippath [zippath $zippath]
        test zipfs-mount-$id "zipfs mount $id" -body {
            mount $zippath $mountpoint
            set canon [zipfs canonical $mountpoint]
            list [file exists [file join $canon $checkPath]] \
                [mounttarget $canon]
        } -cleanup {
            zipfs unmount $mountpoint
        } -result [list 1 $zippath] {*}$args

        # Mount memory buffer
        test zipfs-mount_data-$id "zipfs mount_data $id" -body {
            zipfs mount_data [readbin $zippath] $mountpoint
            set canon [zipfs canonical $mountpoint]
            list [file exists [file join $canon $checkPath]] \
                [mounttarget $canon]
        } -cleanup {
            cleanup
        } -result [list 1 {Memory Buffer}] {*}$args

    }

    testnumargs "zipfs mount" "" "?zipfile? ?mountpoint? ?password?"

    # Not supported zip files
    testbadmount non-existent-file    nosuchfile.zip "couldn't open*nosuchfile.zip*no such file or directory"
    testbadmount not-zipfile          [file normalize [info script]]      "archive directory end signature not found"
    testbadmount zip64-unsupported    zip64.zip      "wrong header signature"

    # Inconsistent metadata
    testbadmount bad-directory-offset incons-cdoffset.zip          "archive directory truncated"
    testbadmount bad-directory-magic  incons-central-magic-bad.zip "wrong header signature"
    testbadmount bad-local-magic      incons-local-magic-bad.zip   "Failed to find local header"
    testbadmount bad-file-count-high  incons-file-count-high.zip   "truncated directory"
    testbadmount bad-file-count-low   incons-file-count-low.zip    "short file count"

    testmount basic             test.zip           testdir/test2 $defaultMountPoint
    testmount basic-on-default  test.zip           testdir/test2 ""
    testmount basic-on-root     test.zip           testdir/test2 [zipfs root]
    testmount basic-on-slash    test.zip           testdir/test2 /
    testmount basic-on-relative test.zip           testdir/test2 testmount
    testmount basic-on-absolute test.zip           testdir/test2 /testmount
    testmount zip-at-end        junk-at-start.zip  testdir/test2 $defaultMountPoint
    testmount zip-at-start      junk-at-end.zip    testdir/test2 $defaultMountPoint
    testmount zip-in-zip [file join [zipfs root] test2 test.zip] testdir/test2 $defaultMountPoint -setup {
        mount [zippath test-zip-in-zip.zip] [file join [zipfs root] test2]
    } -cleanup {
        zipfs unmount $mountpoint
        zipfs unmount [file join [zipfs root] test2]
    }
    testmount relative-mount-point test.zip testdir/test2 ""

    test zipfs-mount-busy-1 "Attempt to mount on existing mount point" -setup {
        mount [zippath test.zip]
    } -cleanup {
        cleanup
    } -body {
        zipfs mount [zippath testfile-cp437.zip] $defaultMountPoint
    } -result "[zippath test.zip] is already mounted on $defaultMountPoint" -returnCodes error

    test zipfs-mount-no-args-1 "mount - get mount list" -setup {
        mount [zippath test.zip]
    } -cleanup {
        cleanup
    } -body {
        set mounts [zipfs mount]
        lsearch -inline -stride 2 $mounts $defaultMountPoint
    } -result [list $defaultMountPoint [zippath test.zip]]

    test zipfs-mount-one-arg-1 "mount - get mount target - absolute path" -setup {
        mount [zippath test.zip]
    } -cleanup {
        cleanup
    } -body {
        zipfs mount $defaultMountPoint
    } -result [zippath test.zip]

    test zipfs-mount-one-arg-2 "mount - get mount target - relative path" -setup {
        file copy [zippath test.zip] test.zip
        mount ./test.zip
    } -cleanup {
        cleanup
        file delete ./test.zip
    } -body {
        zipfs mount $defaultMountPoint
    } -result [file normalize ./test.zip]

    test zipfs-mount-password-1 "mount - verify plaintext readable without password" -body {
        zipfs mount [zippath test-password.zip] $defaultMountPoint
        readbin [file join $defaultMountPoint plain.txt]
    } -cleanup {
        cleanup
    } -result plaintext

    test zipfs-mount-password-2 "mount - verify uncompressed cipher unreadable without password" -body {
        zipfs mount [zippath test-password.zip] $defaultMountPoint
        set chans [lsort [chan names]]; # Want to ensure open does not leave dangling channel
        set result [list ]
        lappend result [catch {open [file join $defaultMountPoint cipher.bin]} message]
        lappend result $message
        lappend result [string equal $chans [lsort [chan names]]]
    } -cleanup {
        cleanup
    } -result {1 {decryption failed} 1}

    test zipfs-mount-password-3 "mount - verify compressed cipher unreadable without password" -body {
        zipfs mount [zippath test-password.zip] $defaultMountPoint
        set chans [lsort [chan names]]; # Want to ensure open does not leave dangling channel
        set result [list ]
        lappend result [catch {open [file join $defaultMountPoint cipher-deflate.bin]} message]
        lappend result $message
        lappend result [string equal $chans [lsort [chan names]]]
    } -cleanup {
        cleanup
    } -result {1 {decryption failed} 1}

    test zipfs-mount-nested-1 "mount - nested mount on non-existing path" -setup {
        mount [zippath test.zip]
    } -cleanup {
        cleanup
    } -body {
        set newmount [file join $defaultMountPoint newdir]
        mount [zippath test-overlay.zip] $newmount
        list \
            [lsort [glob -tails -dir $defaultMountPoint *]] \
            [lsort [glob -tails -dir $newmount *]] \
            [readbin [file join $newmount test2]]
    } -result {{newdir test testdir} {test2 test3} test2-overlay}

    test zipfs-mount-nested-2 "mount - nested mount on existing path" -setup {
        mount [zippath test.zip]
    } -cleanup {
        cleanup
    } -body {
        set newmount [file join $defaultMountPoint testdir]
        mount [zippath test-overlay.zip] $newmount
        # Note - file from existing mount is preserved (testdir/test2)
        # Not clear this is desired but defined as such by the
        # current implementation
        list \
            [lsort [glob -tails -dir $defaultMountPoint *]] \
            [lsort [glob -tails -dir $newmount *]] \
            [readbin [file join $newmount test2]]
    } -result [list {test testdir} {test2 test3} test\n]

    #
    # unmount - only special cases. Normal case already tested as part of other tests

    testnumargs "zipfs unmount" "mountpoint" ""

    test zipfs-unmount-1 "Unmount bogus mount" -body {
        zipfs unmount [file join [zipfs root] nosuchmount]
    } -result ""

    test zipfs-unmount-2 "Unmount mount with open files" -setup {
        mount [zippath test.zip]
        set fd [open [file join $defaultMountPoint test]]
    } -cleanup {
        close $fd
        cleanup
    } -body {
        zipfs unmount $defaultMountPoint
    } -result {filesystem is busy} -returnCodes error

    test zipfs-unmount-3 "Unmount mount with current directory" -setup {
        mount [zippath test.zip]
    } -cleanup {
        cleanup
    } -body {
        set cwd [pwd]
        cd [file join $defaultMountPoint testdir]
        list [pwd] [zipfs unmount $defaultMountPoint] [string equal [pwd] $cwd]
    } -result [list [file join $defaultMountPoint testdir] {} 1]

    test zipfs-unmount-nested-1 "unmount parent of nested mount on new directory should not affect nested mount" -setup {
        mount [zippath test.zip]
        set newmount [file join [zipfs root] test newdir]
        mount [zippath test-overlay.zip] $newmount
    } -cleanup {
        cleanup
    } -body {
        zipfs unmount $defaultMountPoint
        list \
            [zipfs mount $defaultMountPoint] \
            [lsort [glob -tails -dir $newmount *]] \
            [readbin [file join $newmount test2]]
    } -result {{} {test2 test3} test2-overlay}

    test zipfs-unmount-nested-2 "unmount parent of nested mount on existing directory should not affect nested mount" -setup {
        mount [zippath test.zip]
        set newmount [file join [zipfs root] test testdir]
        mount [zippath test-overlay.zip] $newmount
    } -constraints bug-4ae42446ab -cleanup {
        cleanup
    } -body {
        # KNOWN BUG. The test2 file is also present in parent mount.
        # After the unmount, the test2 in the nested mount is not
        # made available.
        zipfs unmount $defaultMountPoint
        list \
            [zipfs mount $defaultMountPoint] \
            [lsort [glob -tails -dir $newmount *]] \
            [readbin [file join $newmount test2]]
    } -result {{} {test2 test3} test2-overlay}

    #
    # zipfs list
    testnumargs "zipfs list" "" "?(-glob|-regexp)? ?pattern?"

    # Generates zipfs list tests for file, memory buffer cases for an archive
    proc testzipfslist {id cmdargs mounts resultpaths args} {
        set resultpaths [lmap path $resultpaths {
            file join [zipfs root] $path
        }]
        set resultpaths [lsort $resultpaths]
        test zipfs-list-$id "zipfs list $id" -body {
            lsort [zipfs list {*}$cmdargs]
        } -setup {
            foreach {zippath mountpoint} $mounts {
                zipfs mount [zippath $zippath] [file join [zipfs root] $mountpoint]
            }
        } -cleanup {
            cleanup
        } -result $resultpaths {*}$args

        # Mount memory buffer
        test zipfs-list-memory-$id "zipfs list memory $id" -body {
            lsort [zipfs list {*}$cmdargs]
        } -setup {
            foreach {zippath mountpoint} $mounts {
                zipfs mount_data [readbin [zippath $zippath]] [file join [zipfs root] $mountpoint]
            }
        } -cleanup {
            cleanup
        } -result $resultpaths {*}$args
    }
    # Some tests have !zipfslib constraint because otherwise they dump the entire Tcl library which is mounted on root
    testzipfslist no-mounts "" {} {} -constraints !zipfslib
    testzipfslist no-pattern "" {test.zip testmountA} {testmountA testmountA/test testmountA/testdir testmountA/testdir/test2} -constraints !zipfslib
    testzipfslist no-pattern-mount-on-empty "" {test.zip {}} {{} test testdir testdir/test2} -constraints !zipfslib
    testzipfslist no-pattern-mount-on-root "" [list test.zip [zipfs root]] {{} test testdir testdir/test2} -constraints !zipfslib
    testzipfslist no-pattern-mount-on-slash "" [list test.zip /] {{} test testdir testdir/test2} -constraints !zipfslib
    testzipfslist no-pattern-mount-on-level3 "" [list test.zip testmt/a/b] {{} testmt testmt/a testmt/a/b testmt/a/b/test testmt/a/b/testdir testmt/a/b/testdir/test2} -constraints {bug-02acab5aea !zipfslib}
    testzipfslist no-pattern-multiple "" {test.zip testmountA test.zip testmountB/subdir} {
        testmountA testmountA/test testmountA/testdir testmountA/testdir/test2
        testmountB/subdir testmountB/subdir/test testmountB/subdir/testdir testmountB/subdir/testdir/test2
    } -constraints !zipfslib
    testzipfslist glob [list "*testmount*2*"] {test.zip testmountA test.zip testmountB/subdir} {
        testmountA/testdir/test2
        testmountB/subdir/testdir/test2
    }
    testzipfslist opt-glob [list -glob "*testmount*2*"] {test.zip testmountA test.zip testmountB/subdir} {
        testmountA/testdir/test2
        testmountB/subdir/testdir/test2
    }
    testzipfslist opt-regexp [list -regexp "testmount.*(A|2)"] {test.zip testmountA test.zip testmountB/subdir} {
        testmountA testmountA/test testmountA/testdir testmountA/testdir/test2
        testmountB/subdir/testdir/test2
    }

    #
    # zipfs exists
    testnumargs "zipfs exists" "filename" ""

    # Generates tests for zipfs exists
    proc testzipfsexists [list id path result [list mountpoint $defaultMountPoint] args] {
        test zipfs-exists-$id "zipfs exists $id" -body {
            zipfs exists $path
        } -setup {
            mount [zippath test.zip] $mountpoint
        } -cleanup {
            zipfs unmount $mountpoint
            cleanup
        } -result $result {*}$args
    }
    testzipfsexists native-file      [info nameofexecutable]                   0
    testzipfsexists nonexistent-file [file join $defaultMountPoint nosuchfile] 0
    testzipfsexists file             [file join $defaultMountPoint test]       1
    testzipfsexists dir              [file join $defaultMountPoint testdir]    1
    testzipfsexists mountpoint       $defaultMountPoint                        1
    testzipfsexists root             [zipfs root]                              1 \
        $defaultMountPoint -constraints bug-02acab5aea
    testzipfsexists level3           [file join $defaultMountPoint a b]        1 \
        [file join $defaultMountPoint a b c] -constraints bug-02acab5aea

    #
    # zipfs find
    testnumargs "zipfs find" "directoryName" ""
    # Generates zipfs find tests for file, memory buffer cases for an archive
    proc testzipfsfind {id findtarget mounts resultpaths args} {
        set setup {
            foreach {zippath mountpoint} $mounts {
                zipfs mount [zippath $zippath] [file join [zipfs root] $mountpoint]
            }
        }
        set memory_setup {
            foreach {zippath mountpoint} $mounts {
                zipfs mount_data [readbin [zippath $zippath]] [file join [zipfs root] $mountpoint]
            }
        }
        if {[dict exists $args -setup]} {
            append setup \n[dict get $args -setup]
            append memory_setup \n[dict get $args -setup]
            dict unset args -setup
        }
        set cleanup cleanup
        if {[dict exists $args -cleanup]} {
            set cleanup "[dict get $args -cleanup]\n$cleanup"
            dict unset args -cleanup
        }
        set resultpaths [lsort $resultpaths]
        test zipfs-find-$id "zipfs find $id" -body {
            lsort [zipfs find $findtarget]
        } -setup $setup -cleanup $cleanup -result $resultpaths {*}$args

        # Mount memory buffer
        test zipfs-find-memory-$id "zipfs find memory $id" -body {
            lsort [zipfs find $findtarget]
        } -setup $memory_setup -cleanup $cleanup -result $resultpaths {*}$args
    }

    testzipfsfind nonexistingmount [file join [zipfs root] nosuchmount] {
        test.zip testmountA test.zip testmountB/subdir
    } {}

    testzipfsfind absolute-path    [file join [zipfs root] testmountA] {
        test.zip testmountA test.zip testmountB/subdir
    } [zipfspaths testmountA/test testmountA/testdir testmountA/testdir/test2]

    testzipfsfind relative-path   testdir {
        test.zip testmountA test.zip testmountB/subdir
    } { testdir/test2 } -setup {
        set cwd [pwd]
        cd [file join [zipfs root] testmountA]
    } -cleanup {
        cd $cwd
    }

    # bug-6183f535c8
    testzipfsfind root-path   [zipfs root] {
        test.zip {} test.zip testmountB/subdir
    } [zipfspaths test testdir testdir/test2] -constraints !zipfslib

    testzipfsfind level3 [file join [zipfs root] testmt a] {
        test.zip testmt/a/b
    } [zipfspaths testmt/a/b testmt/a/b/test testmt/a/b/testdir testmt/a/b/testdir/test2] \
        -constraints bug-02acab5aea

    testzipfsfind level3-root [zipfs root] {
        test.zip testmt/a/b
    } [zipfspaths testmt testmt/a testmt/a/b testmt/a/b/test testmt/a/b/testdir testmt/a/b/testdir/test2] \
        -constraints bug-02acab5aea

    test zipfs-find-native-absolute "zipfs find on native file system" -setup {
        set dir [makeDirectory zipfs-native-absolute]
        set subdir [file join $dir subdir]
        file mkdir $subdir
        set file [file join $subdir native]
        close [open $file w]
    } -cleanup {
        removeDirectory zipfs-native-absolute
    } -body {
        string equal [zipfs find $dir] [list $subdir $file]
    } -result 1

    test zipfs-find-native-relative "zipfs find relative on native file system" -setup {
        set dir [makeDirectory zipfs-native-relative]
        set subdir [file join $dir subdir]
        file mkdir $subdir
        set file [file join $subdir native]
        close [open $file w]
        set cwd [pwd]
    } -cleanup {
        cd $cwd
        removeDirectory zipfs-native-relative
    } -body {
        cd [file dirname $dir]
        # string equal [zipfs find [file tail $subdir]] [list subdir subdir/native]
        zipfs find [file tail $dir]
    } -result {zipfs-native-relative/subdir zipfs-native-relative/subdir/native}

    #
    # zipfs info
    testnumargs "zipfs info" "filename" ""

    test zipfs-info-native-nosuchfile "zipfs info on non-existent native path" -body {
        zipfs info nosuchfile
    } -result {path "nosuchfile" not found in any zipfs volume} -returnCodes error

    test zipfs-info-native-file "zipfs info on native path" -body {
        zipfs info [info nameofexecutable]
    } -result "path \"[info nameofexecutable]\" not found in any zipfs volume" -returnCodes error

    test zipfs-info-nosuchfile "zipfs info non-existent path in mounted archive" -setup {
        mount [zippath test.zip]
    } -cleanup {
        cleanup
    } -body {
        zipfs info [file join $defaultMountPoint nosuchfile]
    } -result "path \"[file join $defaultMountPoint nosuchfile]\" not found in any zipfs volume" -returnCodes error

    test zipfs-info-file "zipfs info file within mounted archive" -setup {
        mount [zippath testdeflated2.zip]
    } -cleanup {
        cleanup
    } -body {
        zipfs info [file join $defaultMountPoint abac-repeat.txt]
    } -result [list [zippath testdeflated2.zip] 60 17 108]

    test zipfs-info-dir "zipfs info dir within mounted archive" -setup {
        mount [zippath test.zip]
    } -cleanup {
        cleanup
    } -body {
        zipfs info [file join $defaultMountPoint testdir]
    } -result [list [zippath test.zip] 0 0 119]

    test zipfs-info-mountpoint "zipfs info on mount point - verify correct offset of zip content" -setup {
        # zip starts at offset 4
        mount [zippath junk-at-start.zip]
    } -cleanup {
        cleanup
    } -body {
        zipfs info $defaultMountPoint
    } -result [list [zippath junk-at-start.zip] 0 0 4]

    test zipfs-info-level3 "zipfs info on mount point - verify correct offset of zip content" -setup {
        # zip starts at offset 4
        mount [zippath junk-at-start.zip] /testmt/a/b
    } -cleanup {
        cleanup
    } -body {
        zipfs info [file join [zipfs root] testmt a]
    } -result {{} 0 0 0} -constraints bug-02acab5aea

    #
    # zipfs canonical -
    # TODO - semantics are very unclear. Can produce nonsensical paths like
    # //zipfs:/n/zipfs:/m/test. Minimal sanity tests for now.
    test zipfs-canonical-minargs {zipfs canonical min args} -body {
        zipfs canonical
    } -returnCodes error -result {wrong # args: should be "zipfs canonical ?mountpoint? filename ?inZipfs?"}
    test zipfs-canonical-maxargs {zipfs canonical max args} -body {
        zipfs canonical a b c d
    } -returnCodes error -result {wrong # args: should be "zipfs canonical ?mountpoint? filename ?inZipfs?"}
    proc testzipfscanonical {id cmdargs result args} {
        test zipfs-canonical-$id "zipfs canonical $id" \
            -body [list zipfs canonical {*}$cmdargs] \
            -result $result {*}$args
    }
    testzipfscanonical basic-relative           PATH [file join [zipfs root] PATH]
    testzipfscanonical basic-absolute           /PATH [file join [zipfs root] PATH]
    testzipfscanonical mountpoint-relative      {MT PATH} [file join [zipfs root] MT PATH]
    testzipfscanonical mountpoint-absolute      {MT /PATH} [file join [zipfs root] PATH]
    testzipfscanonical mountpoint-trailslash-relative      {MT/ PATH} [file join [zipfs root] MT PATH]
    testzipfscanonical mountpoint-trailslash-absolute      {MT/ /PATH} [file join [zipfs root] PATH]
    testzipfscanonical mountpoint-root-relative [list [zipfs root] PATH] [file join [zipfs root] PATH]
    testzipfscanonical mountpoint-root-absolute [list [zipfs root] /PATH] [file join [zipfs root] PATH]
    testzipfscanonical mountpoint-empty-relative      {{} PATH} [file join [zipfs root] PATH]

    testzipfscanonical driveletter X: [zipfs root] -constraints win
    testzipfscanonical drivepath X:/foo/bar [file join [zipfs root] foo bar] -constraints win
    # (backslashes need additional escaping passed to testzipfscanonical)
    testzipfscanonical backslashes X:\\\\foo\\\\bar [file join [zipfs root] foo bar] -constraints win
    testzipfscanonical backslashes-1 X:/foo\\\\bar [file join [zipfs root] foo bar] -constraints win

    #
    # Read/uncompress
    proc testzipfsread {id zippath result {filename abac-repeat.txt} {openopts {}} args} {
        variable defaultMountPoint
        set zippath [zippath $zippath]
        test zipfs-read-$id "zipfs read $id" -setup {
            unset -nocomplain fd
            zipfs mount $zippath $defaultMountPoint
        } -cleanup {
            # In case open succeeded when it should not
            if {[info exists fd]} {
                close $fd
            }
            cleanup
        } -body {
            set fd [open [file join $defaultMountPoint $filename] {*}$openopts]
            gets $fd
        } -result $result {*}$args

        set data [readbin $zippath]
        test zipfs-read-memory-$id "zipfs read in-memory $id" -setup {
            unset -nocomplain fd
            zipfs mount_data $data $defaultMountPoint
        } -cleanup {
            # In case open succeeded when it should not
            if {[info exists fd]} {
                close $fd
            }
            cleanup
        } -body {
            set fd [open [file join $defaultMountPoint $filename] {*}$openopts]
            gets $fd
        } -result $result {*}$args

    }
    testzipfsread stored  test.zip          test           test
    testzipfsread stored  teststored.zip    aaaaaaaaaaaaaa
    testzipfsread deflate testdeflated2.zip aaaaaaaaaaaaaa
    # Test open modes - see bug [4645658689]
    testzipfsread stored-rw  teststored.zip    aaaaaaaaaaaaaa abac-repeat.txt r+
    testzipfsread deflate-rw testdeflated2.zip aaaaaaaaaaaaaa abac-repeat.txt r+
    testzipfsread stored-wr  teststored.zip    {} abac-repeat.txt w+ -constraints bug-00018ec7a0
    testzipfsread deflate-wr testdeflated2.zip {} abac-repeat.txt w+ -constraints bug-00018ec7a0
    testzipfsread stored-ar  teststored.zip    {} abac-repeat.txt a+
    testzipfsread deflate-ar testdeflated2.zip {} abac-repeat.txt a+

    testzipfsread nosuch  test.zip          "file not found \"//zipfs:/testmount/nosuchfile\": no such file or directory" nosuchfile {} -returnCodes error
    testzipfsread deflate-error broken.zip  {decompression error} deflatezliberror {} -returnCodes error
    testzipfsread bzip2   testbzip2.zip     {unsupported compression method} abac-repeat.txt {} -returnCodes error
    testzipfsread lzma    testfile-lzma.zip {unsupported compression method} abac-repeat.txt {} -returnCodes error
    testzipfsread xz      testfile-xz.zip   {unsupported compression method} abac-repeat.txt {} -returnCodes error
    testzipfsread zstd    testfile-zstd.zip {unsupported compression method} abac-repeat.txt {} -returnCodes error

    test zipfs-read-unwritable "Writes not allowed on file opened for read" -setup {
        mount [zippath test.zip]
    } -cleanup {
        close $fd
        cleanup
    } -body {
        set fd [open [file join $defaultMountPoint test]]
        puts $fd blah
    } -result {channel "*" wasn't opened for writing} -match glob -returnCodes error

    #
    # Write
    proc testzipfswrite {id zippath result filename mode args} {
        variable defaultMountPoint
        set zippath [zippath $zippath]
        set path [file join $defaultMountPoint $filename]
        set body {
            set fd [open $path $mode]
            fconfigure $fd -translation binary
            puts -nonewline $fd "xyz"
            close $fd
            set fd [open $path]
            fconfigure $fd -translation binary
            read $fd
        }
        test zipfs-write-$id "zipfs write $id" -setup {
            unset -nocomplain fd
            zipfs mount $zippath $defaultMountPoint
        } -cleanup {
            # In case open succeeded when it should not
            if {[info exists fd]} {
                close $fd
            }
            cleanup
        } -body $body -result $result {*}$args

        set data [readbin $zippath]
        test zipfs-write-memory-$id "zipfs write in-memory $id" -setup {
            unset -nocomplain fd
            zipfs mount_data $data $defaultMountPoint
        } -cleanup {
            # In case open succeeded when it should not
            if {[info exists fd]} {
                close $fd
            }
            cleanup
        } -body $body -result $result {*}$args

    }
    testzipfswrite create-w test.zip  "file not found \"//zipfs:/testmount/newfile\": no such file or directory" newfile w -returnCodes error
    testzipfswrite create-wr test.zip "file not found \"//zipfs:/testmount/newfile\": no such file or directory" newfile w+ -returnCodes error
    testzipfswrite create-a test.zip  "append mode not supported: permission denied" newfile a -returnCodes error
    testzipfswrite create-ar test.zip "file not found \"//zipfs:/testmount/newfile\": no such file or directory" newfile a+ -returnCodes error
    testzipfswrite store-w teststored.zip      "xyz" abac-repeat.txt w
    testzipfswrite deflate-w testdeflated2.zip "xyz" abac-repeat.txt w
    testzipfswrite store-wr teststored.zip     "xyz" abac-repeat.txt w+
    testzipfswrite deflate-wr testdeflated2.zip "xyz" abac-repeat.txt w+
    testzipfswrite stored-a teststored.zip     "append mode not supported: permission denied" abac-repeat.txt a -returnCodes error
    testzipfswrite deflate-a testdeflated2.zip "append mode not supported: permission denied" abac-repeat.txt a -returnCodes error
    testzipfswrite store-ar teststored.zip     "aaaaaaaaaaaaaa\nbbbbbbbbbbbbbb\naaaaaaaaaaaaaa\ncccccccccccccc\nxyz" abac-repeat.txt a+
    testzipfswrite deflate-ar testdeflated2.zip "aaaaaaaaaaaaaa\nbbbbbbbbbbbbbb\naaaaaaaaaaaaaa\ncccccccccccccc\nxyz" abac-repeat.txt a+

    test zipfs-write-unreadable "Reads not allowed on file opened for write" -setup {
        mount [zippath test.zip]
    } -cleanup {
        close $fd
        cleanup
    } -body {
        set fd [open [file join $defaultMountPoint test] w]
        read $fd
    } -result {channel "*" wasn't opened for reading} -match glob -returnCodes error

    test zipfs-write-persist "Writes persist ONLY while mounted" -setup {
        mount [zippath test.zip]
    } -cleanup {
        cleanup
    } -body {
        set path [file join $defaultMountPoint test]
        set fd [open $path w]
        puts -nonewline $fd newtext
        close $fd
        set fd [open $path]
        set result [list [read $fd]]
        close $fd
        zipfs unmount $defaultMountPoint
        mount [zippath test.zip]
        set fd [open $path]
        lappend result [read $fd]
        close $fd
        set result
    } -result [list newtext test\n]

    test zipfs-write-size-limit-0 "Writes have a size limit" -setup {
        set origlimit $::tcl::zipfs::wrmax
        mount [zippath test.zip]
    } -cleanup {
        close $fd
        set ::tcl::zipfs::wrmax $origlimit
        cleanup
    } -body {
        set ::tcl::zipfs::wrmax 10
        set fd [open [file join $defaultMountPoint test] w]
        puts -nonewline $fd [string repeat x 11]
    } -result {} -returnCodes error -constraints bug-d5d03207ca

    test zipfs-write-size-limit-1 "Writes disallowed" -setup {
        set origlimit $::tcl::zipfs::wrmax
        mount [zippath test.zip]
    } -cleanup {
        set ::tcl::zipfs::wrmax $origlimit
        cleanup
    } -body {
        set ::tcl::zipfs::wrmax -1
        open [file join $defaultMountPoint test] w
    } -result {write access not supported: permission denied} -returnCodes error

    #
    # read/seek/write
    proc testzipfsrw {id zippath expected filename mode args} {
        variable defaultMountPoint
        set zippath [zippath $zippath]
        set path [file join $defaultMountPoint $filename]
        set body {
            set result ""
            set fd [open $path $mode]
            fconfigure $fd -translation binary
            append result [gets $fd],
            set pos [tell $fd]
            append result $pos,
            puts -nonewline $fd "xyz"
            append result [gets $fd],
            seek $fd $pos
            append result [gets $fd],
            seek $fd -6 end
            append result [read $fd]
        }
        test zipfs-readwrite-$id "zipfs read/seek/write $id" -setup {
            unset -nocomplain fd
            zipfs mount $zippath $defaultMountPoint
        } -cleanup {
            # In case open succeeded when it should not
            if {[info exists fd]} {
                close $fd
            }
            cleanup
        } -body $body -result $expected {*}$args

        set data [readbin $zippath]
        test zipfs-readwrite-memory-$id "zipfs read/seek/write in-memory $id" -setup {
            unset -nocomplain fd
            zipfs mount_data $data $defaultMountPoint
        } -cleanup {
            # In case open succeeded when it should not
            if {[info exists fd]} {
                close $fd
            }
            cleanup
        } -body $body -result $expected {*}$args

    }
    testzipfsrw store-r+ teststored.zip      "aaaaaaaaaaaaaa,15,bbbbbbbbbbb,xyzbbbbbbbbbbb,ccccc\n" abac-repeat.txt r+
    testzipfsrw store-w+ teststored.zip      ",0,,xyz,yz" abac-repeat.txt w+ -constraints bug-00018ec7a0
    testzipfsrw store-a+ teststored.zip      ",60,,xyz,cc\nxyz" abac-repeat.txt a+

    #
    # Password protected
    proc testpassword {id filename password result args} {
        variable defaultMountPoint
        set zippath [zippath test-password.zip]
        test zipfs-password-read-$id "zipfs password read $id" -setup {
            unset -nocomplain fd
            if {$password ne ""} {
                zipfs mount $zippath $defaultMountPoint $password
            } else {
                zipfs mount $zippath $defaultMountPoint
            }
        } -cleanup {
            # In case open succeeded when it should not
            if {[info exists fd]} {
                close $fd
            }
            cleanup
        } -body {
            set fd [open [file join $defaultMountPoint $filename]]
            gets $fd
        } -result $result {*}$args
    }
    # The bug bug-bbe7c6ff9e only manifests on macos
    testConstraint bug-bbe7c6ff9e [expr {$::tcl_platform(os) ne "Darwin"}]
    testpassword plain plain.txt password plaintext
    testpassword plain-nopassword plain.txt "" plaintext
    testpassword plain-badpassword plain.txt xxx plaintext
    testpassword cipher cipher.bin password ciphertext -constraints bug-bbe7c6ff9e
    testpassword cipher-nopassword cipher.bin {} "decryption failed" -returnCodes error
    testpassword cipher-badpassword cipher.bin xxx "invalid CRC" -returnCodes error
    testpassword cipher-deflate cipher-deflate.bin password [lseq 100] -constraints bug-bbe7c6ff9e
    testpassword cipher-deflate-nopassword cipher-deflate.bin {} "decryption failed" -returnCodes error
    testpassword cipher-deflate-badpassword cipher-deflate.bin xxx "decompression error" -returnCodes error

    #
    # CRC errors
    proc testcrc {id zippath filename result args} {
        variable defaultMountPoint
        set zippath [zippath $zippath]
        test zipfs-crc-$id "zipfs crc $id" -setup {
            unset -nocomplain fd
            zipfs mount $zippath $defaultMountPoint
        } -cleanup {
            # In case mount succeeded when it should not
            if {[info exists fd]} {
                close $fd
            }
            cleanup
        } -body {
            set fd [open [file join $defaultMountPoint $filename]]
        } -result $result -returnCodes error {*}$args

        # Mount memory buffer
        test zipfs-crc-memory-$id "zipfs crc memory $id" -setup {
            zipfs mount_data [readbin [zippath $zippath]] $defaultMountPoint
        } -cleanup {
            cleanup
        } -body {
            set fd [open [file join $defaultMountPoint $filename]]
        } -result $result -returnCodes error {*}$args
    }
    testcrc local incons-local-crc.zip a "invalid CRC"
    testcrc store-crc broken.zip storedcrcerror "invalid CRC"
    testcrc deflate-crc broken.zip deflatecrcerror "invalid CRC"
    test zipfs-crc-false-positives {
        Verify no false positives in CRC checking
    } -constraints zipfslib -body {
        # Just loop ensuring no crc failures
        foreach f [zipfs list] {
            if {[file isfile $f]} {
                close [open $f]
                incr count
            }
        }
        expr {$count > 0}
    } -result 1

    #
    # file stat
    proc fixupstat {stat} {
        foreach key {atime ctime mtime} {
            # ZIP files have no TZ info so zipfs uses mktime which is localtime
            set time [dict get $stat $key]
            if {$time ne "0"} {
                dict set stat $key [clock scan [dict get $stat $key] -format "%Y-%m-%d %H:%M:%S"]
            }
        }
        if {$::tcl_platform(platform) ne "windows"} {
            dict set stat blksize 0
            dict set stat blocks 0
        }
        return [lsort -stride 2 $stat]
    }
    test zipfs-file-stat-nosuchfile "Read stat of nonexistent file" -setup {
        mount [zippath test.zip]
    } -cleanup cleanup -body {
        file stat [file join $defaultMountPoint nosuchfile]
    } -result "could not read \"[file join $defaultMountPoint nosuchfile]\": *" -match glob -returnCodes error

    test zipfs-file-stat-nosuchmount "Read stat of nonexistent mount" -body {
        file stat [file join $defaultMountPoint nosuchfile]
    } -result "could not read \"[file join $defaultMountPoint nosuchfile]\": no such file or directory" -returnCodes error

    test zipfs-file-stat-file "Read stat of file" -setup {
        mount [zippath test.zip]
    } -cleanup cleanup -body {
        lsort -stride 2 [file stat [file join $defaultMountPoint test]]
    } -result [fixupstat {atime {2003-10-06 15:46:42} ctime {2003-10-06 15:46:42} dev 0 gid 0 ino 0 mode 33133 mtime {2003-10-06 15:46:42} nlink 0 size
        5 type file uid 0}]

    test zipfs-file-stat-dir "Read stat of dir" -setup {
        mount [zippath test.zip]
    } -cleanup cleanup -body {
        lsort -stride 2 [file stat [file join $defaultMountPoint testdir]]
    } -result [fixupstat {atime {2005-01-11 19:03:54} ctime {2005-01-11 19:03:54} dev 0 gid 0 ino 0 mode 16749 mtime {2005-01-11 19:03:54} nlink 0 size 0 type directory uid 0}]

    test zipfs-file-stat-mount "Read stat of mount point" -setup {
        mount [zippath test.zip]
    } -cleanup cleanup -body {
        lsort -stride 2 [file stat $defaultMountPoint]
    } -result [fixupstat {atime 0 ctime 0 dev 0 gid 0 ino 0 mode 16749 mtime 0 nlink 0 size 0 type directory uid 0}]

    test zipfs-file-stat-root-mount "Read stat of root" -setup {
        mount [zippath test.zip] [zipfs root]
    } -cleanup cleanup -body {
        lsort -stride 2 [file stat [zipfs root]]
    } -result [fixupstat {atime 0 ctime 0 dev 0 gid 0 ino 0 mode 16749 mtime 0 nlink 0 size 0 type directory uid 0}]

    test zipfs-file-stat-root-subdir-mount "Read stat of root when mount is subdir" -constraints {
        bug-02acab5aea
    } -setup {
        mount [zippath test.zip]
    } -cleanup cleanup -body {
        lsort -stride 2 [file stat [zipfs root]]
    } -result [fixupstat {atime 0 ctime 0 dev 0 gid 0 ino 0 mode 16749 mtime 0 nlink 0 size 0 type directory uid 0}]

    test zipfs-file-stat-level3 "Stat on a directory that is intermediary in a mount point" -constraints {
        bug-02acab5aea
    } -setup {
        mount [zippath test.zip] [file join $defaultMountPoint mt2]
    } -cleanup cleanup -body {
        lsort -stride 2 [file stat $defaultMountPoint]
    }

    #
    # glob of zipfs file
    proc testzipfsglob {id mountpoint pat result {globopt {}} args} {
        test zipfs-glob-$id "zipfs glob $id" -setup {
            mount [zippath test.zip] $mountpoint
        } -cleanup {
            cleanup
        } -body {
            lsort [glob {*}$globopt $pat]
        } -result $result {*}$args
    }
    # Bug 14db54d81e
    testzipfsglob root-dir [zipfs root] * {//zipfs:/test //zipfs:/testdir} [list -dir [zipfs root]] -constraints !zipfslib
    testzipfsglob root [zipfs root] [file join [zipfs root] *] {//zipfs:/test //zipfs:/testdir} {} -constraints !zipfslib
    testzipfsglob pattern $defaultMountPoint [file join $defaultMountPoint testdir t*] \
        [file join $defaultMountPoint testdir test2]
    testzipfsglob files $defaultMountPoint [file join $defaultMountPoint t*] \
        [list [file join $defaultMountPoint test]] {-type f}
    testzipfsglob dirs $defaultMountPoint [file join $defaultMountPoint t*] \
        [list [file join $defaultMountPoint testdir]] {-type d}
    testzipfsglob no-match $defaultMountPoint [file join $defaultMountPoint testdir x*] \
        {no files matched glob pattern "//zipfs:/testmount/testdir/x*"} {} -returnCodes error
    testzipfsglob no-match-nocomplain $defaultMountPoint [file join $defaultMountPoint testdir x*] {} {-nocomplain}
    testzipfsglob mountpoint $defaultMountPoint [file join [zipfs root] *] \
        [list $defaultMountPoint] {} -constraints !zipfslib

    #
    # file attributes
    proc testzipfsfileattr [list id path result [list mountpoint $defaultMountPoint] args] {
        test zipfs-file-attrs-$id "zipfs file attrs $id" -setup {
            mount [zippath test.zip] $mountpoint
        } -cleanup cleanup -body {
            lsort -stride 2 [file attributes $path]
        } -result $result {*}$args
    }
    testzipfsfileattr noent [file join $defaultMountPoint nosuchfile] \
        {file not found: no such file or directory} $defaultMountPoint -returnCodes error
    testzipfsfileattr file [file join $defaultMountPoint test] \
        [list -archive [zippath test.zip] -compsize 5 -crc [expr 0x3BB935C6] -mount $defaultMountPoint -offset 55 -permissions 0o555 -uncompsize 5]
    testzipfsfileattr dir [file join $defaultMountPoint testdir] \
        [list -archive [zippath test.zip] -compsize 0 -crc 0 -mount $defaultMountPoint -offset 119 -permissions 0o555 -uncompsize 0]
    testzipfsfileattr root [zipfs root] {} {} -constraints bug-4af110a6a1
    testzipfsfileattr mountpoint $defaultMountPoint \
        [list -archive [zippath test.zip] -compsize 0 -crc 0 -mount $defaultMountPoint -offset 0 -permissions 0o555 -uncompsize 0]
    testzipfsfileattr mezzo [file join $defaultMountPoint a b] {} [file join $defaultMountPoint a b c] -constraints bug-4af110a6a1


    #
    # TODO - file copy, file rename etc.

    #
    # file normalize
    proc testzipfsnormalize {id path result {dir {}}} {
        if {$dir eq ""} {
            test zipfs-file-normalize-$id "zipfs file normalize $id" -body {
                file normalize $path
            } -result $result
        } else {
            test zipfs-file-normalize-$id "zipfs file normalize $id" -setup {
                set cwd [pwd]
                mount [zippath test.zip] [zipfs root]
                cd $dir
            } -cleanup {
                cd $cwd
                cleanup
            } -body {
                file normalize $path
            } -result $result
        }
    }
    # The parsing requires all these cases for various code paths
    # in particular, root, one below root and more than one below root
    testzipfsnormalize dot-1  [zipfs root] [zipfs root]
    testzipfsnormalize dot-2  [file join [zipfs root] .]        [zipfs root]
    testzipfsnormalize dot-3  [file join [zipfs root] . .]      [zipfs root]
    testzipfsnormalize dot-4  [file join [zipfs root] a .]      [file join [zipfs root] a]
    testzipfsnormalize dot-5  [file join [zipfs root] a . . .]  [file join [zipfs root] a]
    testzipfsnormalize dot-6  [file join [zipfs root] a b .]    [file join [zipfs root] a b]
    testzipfsnormalize dot-7  [file join [zipfs root] a b . .]  [file join [zipfs root] a b]

    testzipfsnormalize dotdot-1  [file join [zipfs root] ..]              [zipfs root]
    testzipfsnormalize dotdot-2  [file join [zipfs root] .. ..]           [zipfs root]
    testzipfsnormalize dotdot-3  [file join [zipfs root] a ..]            [zipfs root]
    testzipfsnormalize dotdot-4  [file join [zipfs root] a .. .. ..]      [zipfs root]
    testzipfsnormalize dotdot-5  [file join [zipfs root] a b ..]          [file join [zipfs root] a]
    testzipfsnormalize dotdot-6  [file join [zipfs root] a b ..]          [file join [zipfs root] a]
    testzipfsnormalize dotdot-7  [file join [zipfs root] a b .. ..]       [zipfs root]
    testzipfsnormalize dotdot-8  [file join [zipfs root] a b .. .. .. ..] [zipfs root]

    testzipfsnormalize relative-1  a               [file join [zipfs root] a]         [zipfs root]
    testzipfsnormalize relative-2  .               [zipfs root]                       [zipfs root]
    testzipfsnormalize relative-3  ./              [zipfs root]                       [zipfs root]
    testzipfsnormalize relative-4  ./a             [file join [zipfs root] a]         [zipfs root]
    testzipfsnormalize relative-5  ../             [file join [zipfs root]]           [zipfs root]
    testzipfsnormalize relative-6  ../a            [file join [zipfs root] a]         [zipfs root]
    testzipfsnormalize relative-7  ../a/           [file join [zipfs root] a]         [zipfs root]
    testzipfsnormalize relative-8  ../..           [zipfs root]                       [zipfs root]
    testzipfsnormalize relative-9  dir/a           [file join [zipfs root] dir a]     [zipfs root]
    testzipfsnormalize relative-10  dir/dirb/..    [file join [zipfs root] dir]       [zipfs root]
    testzipfsnormalize relative-11  dir/../a       [file join [zipfs root] a]         [zipfs root]
    testzipfsnormalize relative-12  dir/../a/      [file join [zipfs root] a]         [zipfs root]
    testzipfsnormalize relative-13  dir/../../../a [file join [zipfs root] a]         [zipfs root]
    testzipfsnormalize relative-14  a              [file join [zipfs root] testdir a] [file join [zipfs root] testdir]

    # TODO - mkkey, mkimg, mkzip, lmkimg, lmkzip
    testnumargs "zipfs mkkey" "password" "" -constraints zipfs
    testnumargs "zipfs mkimg" "outfile indir" "?strip? ?password? ?infile?"
    testnumargs "zipfs lmkimg" "outfile inlist" "?password? ?infile?"
    testnumargs "zipfs mkzip" "outfile indir" "?strip? ?password?"
    testnumargs "zipfs lmkzip" "outfile inlist" "?password?"

    #
    # Bug regressions

    test bug-6ed3447a7e "Crash opening file in streamed archive" -setup {
        mount [zippath streamed.zip]
    } -cleanup {
        cleanup
    } -body {
        set fd [open [file join $defaultMountPoint -]]
        list [catch {read $fd} message] [close $fd] $message
        close $fd
    } -result {file size error (may be zip64)} -returnCodes error
}


::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End: