sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 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 | # The EOCD record stores the number of entries as a 16-bit value, and |
| 321 | # zipfile does not support zip64. Check that attempts to create an |
| 322 | # archive with more than 65535 entries fail with an error, instead of |
| 323 | # writing a wrapped entry count that causes later writes to discard |
| 324 | # entries. |
| 325 | # |
| 326 | if {[catch { load_static_extension db fileio }]==0} { |
| 327 | do_catchsql_test 9.0 { |
| 328 | WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<65536) |
| 329 | SELECT length(zipfile('f'||i, 'x')) FROM c; |
| 330 | } {1 {too many entries for zipfile(); max: 65535}} |
| 331 | |
| 332 | forcedelete test.zip |
| 333 | do_execsql_test 9.1 { |
| 334 | WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<65535) |
| 335 | SELECT writefile('test.zip', zipfile('f'||i, 'x'))>0 FROM c; |
| 336 | SELECT count(*) FROM zipfile('test.zip'); |
| 337 | } {1 65535} |
| 338 | |
| 339 | do_execsql_test 9.2 { |
| 340 | CREATE VIRTUAL TABLE temp.zz USING zipfile('test.zip'); |
| 341 | SELECT count(*) FROM zz; |
| 342 | } {65535} |
| 343 | |
| 344 | do_catchsql_test 9.3 { |
| 345 | INSERT INTO zz(name, data) VALUES('g', 'y'); |
| 346 | } {1 {too many entries; max: 65535}} |
| 347 | do_execsql_test 9.3.1 { |
| 348 | SELECT count(*) FROM zipfile(readfile('test.zip')); |
| 349 | } {65535} |
| 350 | |
| 351 | # Operations that do not increase the number of entries still work. |
| 352 | do_execsql_test 9.4 { |
| 353 | INSERT OR REPLACE INTO zz(name, data) VALUES('f1', 'y'); |
| 354 | INSERT OR IGNORE INTO zz(name, data) VALUES('f2', 'z'); |
| 355 | UPDATE zz SET name='renamed' WHERE name='f3'; |
| 356 | SELECT count(*) FROM zipfile(readfile('test.zip')); |
| 357 | SELECT name, data FROM zz WHERE name IN ('f1', 'f2', 'renamed'); |
| 358 | } {65535 f2 x renamed x f1 y} |
| 359 | |
| 360 | # After a DELETE, there is room for exactly one more entry. |
| 361 | do_execsql_test 9.5 { |
| 362 | DELETE FROM zz WHERE name='f4'; |
| 363 | INSERT INTO zz(name, data) VALUES('g', 'y'); |
| 364 | SELECT count(*) FROM zipfile(readfile('test.zip')); |
| 365 | } {65535} |
| 366 | do_catchsql_test 9.6 { |
| 367 | INSERT INTO zz(name, data) VALUES('h', 'y'); |
| 368 | } {1 {too many entries; max: 65535}} |
| 369 | |
| 370 | do_execsql_test 9.7 { |
| 371 | DROP TABLE temp.zz; |
| 372 | SELECT count(*) FROM zipfile('test.zip'); |
| 373 | } {65535} |
| 374 | } |
| 375 | |
| 376 | #------------------------------------------------------------------------- |
| 377 | # Bug 7faad02e21dced4d. The LEVEL constraint on fsdir() must give the |
| 378 | # same results as if it were not passed to xBestIndex, even for values |
| 379 | # that are out of range, negative, NULL or non-integer. |
| 380 | # |
| 381 | if {[catch { load_static_extension db fileio }]==0} { |
| 382 | forcedelete test_fsdir |
| 383 | file mkdir test_fsdir/a/b/c |
| 384 | foreach f {test_fsdir/x test_fsdir/a/y test_fsdir/a/b/z test_fsdir/a/b/c/w} { |
| 385 | close [open $f w] |
| 386 | } |
| 387 | |
| 388 | set i 0 |
| 389 | foreach {w res} { |
| 390 | {level<=2147483647} 8 |
| 391 | {level<=4294967296} 8 |
| 392 | {level<=4294967297} 8 |
| 393 | {level<0} 0 |
| 394 | {level<=-1} 0 |
| 395 | {level<=0} 0 |
| 396 | {level<1} 0 |
| 397 | {level<=1} 1 |
| 398 | {level<2} 1 |
| 399 | {level<=2} 3 |
| 400 | {level<2.5} 3 |
| 401 | {level<=2.5} 3 |
| 402 | {level<=NULL} 0 |
| 403 | {level<NULL} 0 |
| 404 | {level=NULL} 0 |
| 405 | {level<'x'} 8 |
| 406 | {level<x'00'} 8 |
| 407 | {level=3} 2 |
| 408 | {level=3.0} 2 |
| 409 | {level=3.5} 0 |
| 410 | {level=4294967299} 0 |
| 411 | } { |
| 412 | incr i |
| 413 | do_execsql_test 10.$i.1 " |
| 414 | SELECT count(*) FROM fsdir('test_fsdir') WHERE $w |
| 415 | " $res |
| 416 | do_execsql_test 10.$i.2 " |
| 417 | SELECT count(*) FROM fsdir('test_fsdir') WHERE +$w |
| 418 | " $res |
| 419 | } |
| 420 | |
| 421 | # Check that the LEVEL constraint still limits the depth of the |
| 422 | # search. Make directory test_fsdir/a/b/c (itself at level 4) unreadable. |
| 423 | # Then queries that need the entries within it (level 5) fail, and |
| 424 | # those that do not succeed. |
| 425 | # |
| 426 | if {$::tcl_platform(platform)=="unix"} { |
| 427 | file attributes test_fsdir/a/b/c -permissions 0000 |
| 428 | if {[catch {glob -nocomplain test_fsdir/a/b/c/*}]} { |
| 429 | do_execsql_test 10.100 { |
| 430 | SELECT count(*) FROM fsdir('test_fsdir') WHERE level<=4 |
| 431 | } {7} |
| 432 | do_execsql_test 10.101 { |
| 433 | SELECT count(*) FROM fsdir('test_fsdir') WHERE level<5 |
| 434 | } {7} |
| 435 | do_execsql_test 10.102 { |
| 436 | SELECT count(*) FROM fsdir('test_fsdir') WHERE level<4.5 |
| 437 | } {7} |
| 438 | do_execsql_test 10.103 { |
| 439 | SELECT count(*) FROM fsdir('test_fsdir') WHERE level=4 |
| 440 | } {2} |
| 441 | do_catchsql_test 10.104 { |
| 442 | SELECT count(*) FROM fsdir('test_fsdir') WHERE level<5.5 |
| 443 | } {1 {cannot read directory: test_fsdir/a/b/c}} |
| 444 | do_catchsql_test 10.105 { |
| 445 | SELECT count(*) FROM fsdir('test_fsdir') WHERE level<=5 |
| 446 | } {1 {cannot read directory: test_fsdir/a/b/c}} |
| 447 | } |
| 448 | file attributes test_fsdir/a/b/c -permissions 0755 |
| 449 | } |
| 450 | forcedelete test_fsdir |
| 451 | } |
| 452 | |
| 453 | finish_test |