Debian Codegraph @

test/zipfile2.test

raw
1 # 2018 January 30
2 #
3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
5 #
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
9 #
10 #***********************************************************************
11 #
12
13 if {$tcl_version<8.6} {
14 puts "Requires TCL 8.6 or later"
15 return
16 }
17
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
20 set testprefix zipfile2
21
22 ifcapable !vtab {
23 finish_test; return
24 }
25 if {[catch {load_static_extension db zipfile} error]} {
26 puts "Skipping zipfile2 tests, hit load error: $error"
27 finish_test; return
28 }
29
30 proc blobliteral {str} {
31 set concat [string map {" " "" "\n" ""} $str]
32 return "X'$concat'"
33 }
34
35 proc blob {str} {
36 binary decode hex $str
37 }
38
39 proc findall {needle haystack} {
40 set L [list]
41 set start 0
42 while { [set idx [string first $needle $haystack $start]]>=0 } {
43 lappend L $idx
44 set start [expr $idx+1]
45 }
46 set L
47 }
48
49 do_execsql_test 1.0 {
50 CREATE VIRTUAL TABLE aaa USING zipfile('testzip');
51 CREATE VIRTUAL TABLE bbb USING zipfile("testzip");
52 CREATE VIRTUAL TABLE ccc USING zipfile(`testzip`);
53 CREATE VIRTUAL TABLE ddd USING zipfile([testzip]);
54 CREATE VIRTUAL TABLE eee USING zipfile(testzip);
55 CREATE VIRTUAL TABLE fff USING zipfile('test''zip');
56 }
57
58 do_test 2.0 {
59 forcedelete testdir
60 file mkdir testdir
61 execsql { CREATE VIRTUAL TABLE hhh USING zipfile('testdir') }
62 lindex [catchsql {
63 SELECT * FROM hhh;
64 INSERT INTO hhh(name, data) VALUES('1.txt', 'file data');
65 }] 0
66 } 1
67
68
69 set archive {
70 504B0304140000080000D4A52BEC09F3B6E0110000001100000005000900612E
71 747874555405000140420F00636F6E74656E7473206F6620612E747874504B03
72 04140000080000D4A52BECD98916A7110000001100000005000900622E747874
73 555405000140420F00636F6E74656E7473206F6620622E747874504B01021E03
74 140000080000D4A52BEC09F3B6E0110000001100000005000900000000000000
75 0000A48100000000612E747874555405000140420F00504B01021E0314000008
76 0000D4A52BECD98916A71100000011000000050009000000000000000000A481
77 3D000000622E747874555405000140420F00504B050600000000020002007800
78 00007A0000000000
79 }
80
81 if 0 {
82 # This test is broken - the archive generated is slightly different
83 # depending on the zlib version used.
84 do_execsql_test 3.1 {
85 WITH contents(name,mtime,data) AS (
86 VALUES('a.txt', 1000000, 'contents of a.txt') UNION ALL
87 VALUES('b.txt', 1000000, 'contents of b.txt')
88 ) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents;
89 } [blobliteral $archive]
90 }
91
92
93 set blob [blob $archive]
94 do_execsql_test 3.2 {
95 SELECT name,mtime,data FROM zipfile($blob)
96 } {
97 a.txt 1000000 {contents of a.txt}
98 b.txt 1000000 {contents of b.txt}
99 }
100
101 # Corrupt each of the 0x50 0x4B (ascii "PK") headers in the file
102 # Test that in each case this causes an error.
103 #
104 set L [findall 504B $archive]
105 for {set i 0} {$i < [llength $L]} {incr i} {
106 set idx [lindex $L $i]
107 set a [string replace $archive $idx [expr $idx+3] 0000]
108 set blob [blob $a]
109 do_catchsql_test 3.3.$i {
110 SELECT name,mtime,data FROM zipfile($blob)
111 } {/1 .*/}
112 }
113
114 # Change the "extra info id" for all extended-timestamp fields.
115 set L [findall 5554 $archive]
116 for {set i 0} {$i < [llength $L]} {incr i} {
117 set idx [lindex $L $i]
118 set a [string replace $archive $idx [expr $idx+3] 1234]
119 set blob [blob $a]
120 do_execsql_test 3.4.$i {
121 SELECT name,data FROM zipfile($blob)
122 } {
123 a.txt {contents of a.txt}
124 b.txt {contents of b.txt}
125 }
126 }
127
128 for {set i 0} {$i < [llength $L]} {incr i} {
129 set idx [lindex $L $i]
130 set a [string replace $archive [expr $idx+8] [expr $idx+9] 00]
131 set blob [blob $a]
132 do_execsql_test 3.5.$i {
133 SELECT name,data FROM zipfile($blob)
134 } {
135 a.txt {contents of a.txt}
136 b.txt {contents of b.txt}
137 }
138 }
139
140 # set blob [db one {
141 # WITH contents(name,mtime,data) AS (
142 # VALUES('a.txt', 1000000, 'aaaaaaaaaaaaaaaaaaaaaaa')
143 # ) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents;
144 # }]
145 # set blob [string range $blob 2 end]
146 # set blob [string range $blob 0 end-1]
147 # while {[string length $blob]>0} {
148 # puts [string range $blob 0 63]
149 # set blob [string range $blob 64 end]
150 # }
151 # exit
152
153 set archive2 {
154 504B0304140000080800D4A52BEC08F54C6E050000001700000005000900612E
155 747874555405000140420F004B4CC40A00504B01021E03140000080800D4A52B
156 EC08F54C6E0500000017000000050009000000000000000000A4810000000061
157 2E747874555405000140420F00504B050600000000010001003C000000310000
158 000000
159 }
160 set blob [blob $archive2]
161 do_execsql_test 4.0 {
162 SELECT name,mtime,data,method FROM zipfile($blob)
163 } {
164 a.txt 1000000 aaaaaaaaaaaaaaaaaaaaaaa 8
165 }
166
167 set L [findall 17000000 $archive2]
168 set a $archive2
169 foreach i $L { set a [string replace $a $i [expr $i+7] 16000000] }
170 set blob [blob $a]
171 do_catchsql_test 4.1 {
172 SELECT name,mtime,data,method FROM zipfile($blob)
173 } {1 {inflate() failed (0)}}
174
175 # Check the response to an unknown compression method (set data to NULL).
176 set blob [blob [string map {0800 0900} $archive2]]
177 do_execsql_test 4.2 {
178 SELECT name,mtime,data IS NULL,method FROM zipfile($blob)
179 } {a.txt 1000000 1 9}
180
181 # Corrupt the EOCDS signature bytes in various ways.
182 foreach {tn sub} {
183 1 {504B0500}
184 2 {504B0006}
185 3 {50000506}
186 4 {004B0506}
187 } {
188 set blob [blob [string map [list 504B0506 $sub] $archive2]]
189 do_catchsql_test 4.3.$tn {
190 SELECT * FROM zipfile($blob)
191 } {1 {cannot find end of central directory record}}
192 }
193
194 #-------------------------------------------------------------------------
195 # Test that a zero-length file with a '/' at the end is treated as
196 # a directory (data IS NULL). Even if the mode doesn't indicate
197 # that it is a directory.
198
199 do_test 5.0 {
200 set blob [db one {
201 WITH c(n, d) AS (
202 SELECT 'notadir', ''
203 )
204 SELECT zipfile(n, d) FROM c
205 }]
206
207 set hex [binary encode hex $blob]
208 set hex [string map {6e6f7461646972 6e6f746164692f} $hex]
209 set blob2 [binary decode hex $hex]
210
211 execsql { SELECT name, data IS NULL FROM zipfile($blob2) }
212 } {notadi/ 1}
213
214 #-------------------------------------------------------------------------
215 # Test that duplicate entries may not be created using UPDATE
216 # statements.
217 #
218 forcedelete test.zip
219 do_execsql_test 6.0 {
220 CREATE VIRTUAL TABLE temp.zip USING zipfile('test.zip');
221 INSERT INTO temp.zip (name,data) VALUES ('test1','test');
222 INSERT INTO temp.zip (name,data) VALUES ('test2','test');
223 }
224 do_catchsql_test 6.1 {
225 UPDATE temp.zip SET name='test1' WHERE name='test2'
226 } {1 {duplicate name: "test1"}}
227
228 forcedelete test.zip
229 do_catchsql_test 6.2 {
230 DROP TABLE zip;
231 CREATE VIRTUAL TABLE temp.zip USING zipfile('test.zip');
232 INSERT INTO temp.zip (name,data) VALUES ('test','test');
233 UPDATE temp.zip set name=name||'new' where name='test';
234 INSERT INTO temp.zip (name,data) VALUES ('test','test');
235 UPDATE temp.zip set name=name||'new' where name='test';
236 } {1 {duplicate name: "testnew"}}
237
238 forcedelete test.zip
239 do_execsql_test 6.3 {
240 INSERT INTO temp.zip (name,data) VALUES ('test1','test');
241 INSERT INTO temp.zip (name,data) VALUES ('test2','test');
242 UPDATE OR REPLACE zip SET name='test2' WHERE name='test1';
243 SELECT name FROM zip;
244 } {test2}
245
246 #-------------------------------------------------------------------------
247 # Test a crafted corrupt file.
248 #
249 proc make_corrupt_file {fname} {
250 set central_dir_offset 1000
251 set lfh_offset 200
252 set nFile 60000
253 set nExtra 60000
254 set nComment 0
255
256 # Build local file header at lfh_offset
257 set lfh ""
258 append lfh [binary format isssssiiiss 0x04034b50 20 0 0 0 0 0 0 0 1 0]
259 append lfh "A"
260
261 # Build central directory structure (CDS) at central_dir_offset
262 set cds ""
263 append cds [binary format issssssiiisssssii 0x02014b50 0 20 0 0 0 0 0 0 0 $nFile $nExtra $nComment 0 0 0 $lfh_offset]
264
265 # Payload following CDS: filename + extra + comment
266 set payload ""
267 append payload [string repeat "B" $nFile]
268 append payload [string repeat "C" $nExtra]
269
270 # EOCD at end
271 set cdsize [expr {[string length $cds] + [string length $payload]}]
272 set eocd ""
273 append eocd [binary format issssiis 0x06054b50 0 0 1 1 $cdsize $central_dir_offset 0]
274
275 # Assemble file
276 set buf [string repeat "X" $lfh_offset]
277 append buf $lfh
278
279 set buflen [string length $buf]
280 if {$central_dir_offset > $buflen} {
281 append buf [string repeat "\x00" [expr {$central_dir_offset - $buflen}]]
282 }
283
284 append buf $cds
285 append buf $payload
286 append buf $eocd
287
288 # Write to file
289 set f [open $fname wb]
290 fconfigure $f -translation binary
291 puts -nonewline $f $buf
292 close $f
293 }
294
295 forcedelete test.zip
296 make_corrupt_file test.zip
297 do_execsql_test 7.0 {
298 DROP TABLE IF EXISTS t1;
299 CREATE VIRTUAL TABLE t1 USING zipfile('test.zip');
300 }
301 do_execsql_test 7.1 {
302 SELECT length(name) FROM t1;
303 } {60000}
304
305
306 # https://sqlite.org/forum/forumpost/721a05d2c5
307 #
308 if {[catch { load_static_extension db fileio }]==0} {
309 forcedelete test.zip
310 set fd [open test.zip wb]
311 fconfigure $fd -translation binary
312 puts -nonewline $fd [db one {SELECT X'504b0506000000000100010030000000160000000000504b01021400140000000000000000000000000000000000000000000100010000000000000000000000000000006100'}]
313 close $fd
314
315 do_catchsql_test 8.0 {
316 SELECT name,sz FROM zipfile(readfile('test.zip'));
317 } {1 {failed to read LFH at offset 0}}
318 }
319
320 finish_test