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
|
# 2018 January 30
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
package require Tcl 8.6
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix zipfile2
ifcapable !vtab {
finish_test; return
}
if {[catch {load_static_extension db zipfile} error]} {
puts "Skipping zipfile2 tests, hit load error: $error"
finish_test; return
}
proc blobliteral {str} {
set concat [string map {" " "" "\n" ""} $str]
return "X'$concat'"
}
proc blob {str} {
binary decode hex $str
}
proc findall {needle haystack} {
set L [list]
set start 0
while { [set idx [string first $needle $haystack $start]]>=0 } {
lappend L $idx
set start [expr $idx+1]
}
set L
}
do_execsql_test 1.0 {
CREATE VIRTUAL TABLE aaa USING zipfile('testzip');
CREATE VIRTUAL TABLE bbb USING zipfile("testzip");
CREATE VIRTUAL TABLE ccc USING zipfile(`testzip`);
CREATE VIRTUAL TABLE ddd USING zipfile([testzip]);
CREATE VIRTUAL TABLE eee USING zipfile(testzip);
CREATE VIRTUAL TABLE fff USING zipfile('test''zip');
}
do_test 2.0 {
forcedelete testdir
file mkdir testdir
execsql { CREATE VIRTUAL TABLE hhh USING zipfile('testdir') }
lindex [catchsql {
SELECT * FROM hhh;
INSERT INTO hhh(name, data) VALUES('1.txt', 'file data');
}] 0
} 1
set archive {
504B0304140000080000D4A52BEC09F3B6E0110000001100000005000900612E
747874555405000140420F00636F6E74656E7473206F6620612E747874504B03
04140000080000D4A52BECD98916A7110000001100000005000900622E747874
555405000140420F00636F6E74656E7473206F6620622E747874504B01021E03
140000080000D4A52BEC09F3B6E0110000001100000005000900000000000000
0000A48100000000612E747874555405000140420F00504B01021E0314000008
0000D4A52BECD98916A71100000011000000050009000000000000000000A481
3D000000622E747874555405000140420F00504B050600000000020002007800
00007A0000000000
}
if 0 {
# This test is broken - the archive generated is slightly different
# depending on the zlib version used.
do_execsql_test 3.1 {
WITH contents(name,mtime,data) AS (
VALUES('a.txt', 1000000, 'contents of a.txt') UNION ALL
VALUES('b.txt', 1000000, 'contents of b.txt')
) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents;
} [blobliteral $archive]
}
set blob [blob $archive]
do_execsql_test 3.2 {
SELECT name,mtime,data FROM zipfile($blob)
} {
a.txt 1000000 {contents of a.txt}
b.txt 1000000 {contents of b.txt}
}
# Corrupt each of the 0x50 0x4B (ascii "PK") headers in the file
# Test that in each case this causes an error.
#
set L [findall 504B $archive]
for {set i 0} {$i < [llength $L]} {incr i} {
set idx [lindex $L $i]
set a [string replace $archive $idx [expr $idx+3] 0000]
set blob [blob $a]
do_catchsql_test 3.3.$i {
SELECT name,mtime,data FROM zipfile($blob)
} {/1 .*/}
}
# Change the "extra info id" for all extended-timestamp fields.
set L [findall 5554 $archive]
for {set i 0} {$i < [llength $L]} {incr i} {
set idx [lindex $L $i]
set a [string replace $archive $idx [expr $idx+3] 1234]
set blob [blob $a]
do_execsql_test 3.4.$i {
SELECT name,data FROM zipfile($blob)
} {
a.txt {contents of a.txt}
b.txt {contents of b.txt}
}
}
for {set i 0} {$i < [llength $L]} {incr i} {
set idx [lindex $L $i]
set a [string replace $archive [expr $idx+8] [expr $idx+9] 00]
set blob [blob $a]
do_execsql_test 3.5.$i {
SELECT name,data FROM zipfile($blob)
} {
a.txt {contents of a.txt}
b.txt {contents of b.txt}
}
}
# set blob [db one {
# WITH contents(name,mtime,data) AS (
# VALUES('a.txt', 1000000, 'aaaaaaaaaaaaaaaaaaaaaaa')
# ) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents;
# }]
# set blob [string range $blob 2 end]
# set blob [string range $blob 0 end-1]
# while {[string length $blob]>0} {
# puts [string range $blob 0 63]
# set blob [string range $blob 64 end]
# }
# exit
set archive2 {
504B0304140000080800D4A52BEC08F54C6E050000001700000005000900612E
747874555405000140420F004B4CC40A00504B01021E03140000080800D4A52B
EC08F54C6E0500000017000000050009000000000000000000A4810000000061
2E747874555405000140420F00504B050600000000010001003C000000310000
000000
}
set blob [blob $archive2]
do_execsql_test 4.0 {
SELECT name,mtime,data,method FROM zipfile($blob)
} {
a.txt 1000000 aaaaaaaaaaaaaaaaaaaaaaa 8
}
set L [findall 17000000 $archive2]
set a $archive2
foreach i $L { set a [string replace $a $i [expr $i+7] 16000000] }
set blob [blob $a]
do_catchsql_test 4.1 {
SELECT name,mtime,data,method FROM zipfile($blob)
} {1 {inflate() failed (0)}}
# Check the response to an unknown compression method (set data to NULL).
set blob [blob [string map {0800 0900} $archive2]]
do_execsql_test 4.2 {
SELECT name,mtime,data IS NULL,method FROM zipfile($blob)
} {a.txt 1000000 1 9}
# Corrupt the EOCDS signature bytes in various ways.
foreach {tn sub} {
1 {504B0500}
2 {504B0006}
3 {50000506}
4 {004B0506}
} {
set blob [blob [string map [list 504B0506 $sub] $archive2]]
do_catchsql_test 4.3.$tn {
SELECT * FROM zipfile($blob)
} {1 {cannot find end of central directory record}}
}
#-------------------------------------------------------------------------
# Test that a zero-length file with a '/' at the end is treated as
# a directory (data IS NULL). Even if the mode doesn't indicate
# that it is a directory.
do_test 5.0 {
set blob [db one {
WITH c(n, d) AS (
SELECT 'notadir', ''
)
SELECT zipfile(n, d) FROM c
}]
set hex [binary encode hex $blob]
set hex [string map {6e6f7461646972 6e6f746164692f} $hex]
set blob2 [binary decode hex $hex]
execsql { SELECT name, data IS NULL FROM zipfile($blob2) }
} {notadi/ 1}
#-------------------------------------------------------------------------
# Test that duplicate entries may not be created using UPDATE
# statements.
#
forcedelete test.zip
do_execsql_test 6.0 {
CREATE VIRTUAL TABLE temp.zip USING zipfile('test.zip');
INSERT INTO temp.zip (name,data) VALUES ('test1','test');
INSERT INTO temp.zip (name,data) VALUES ('test2','test');
}
do_catchsql_test 6.1 {
UPDATE temp.zip SET name='test1' WHERE name='test2'
} {1 {duplicate name: "test1"}}
forcedelete test.zip
do_catchsql_test 6.2 {
DROP TABLE zip;
CREATE VIRTUAL TABLE temp.zip USING zipfile('test.zip');
INSERT INTO temp.zip (name,data) VALUES ('test','test');
UPDATE temp.zip set name=name||'new' where name='test';
INSERT INTO temp.zip (name,data) VALUES ('test','test');
UPDATE temp.zip set name=name||'new' where name='test';
} {1 {duplicate name: "testnew"}}
forcedelete test.zip
do_execsql_test 6.3 {
INSERT INTO temp.zip (name,data) VALUES ('test1','test');
INSERT INTO temp.zip (name,data) VALUES ('test2','test');
UPDATE OR REPLACE zip SET name='test2' WHERE name='test1';
SELECT name FROM zip;
} {test2}
finish_test
|