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 13set testdir [file dirname $argv0] 14source $testdir/tester.tcl 15set testprefix zipfile2 16 17ifcapable !vtab { 18 finish_test; return 19} 20if {[catch {load_static_extension db zipfile} error]} { 21 puts "Skipping zipfile2 tests, hit load error: $error" 22 finish_test; return 23} 24 25proc blobliteral {str} { 26 set concat [string map {" " "" "\n" ""} $str] 27 return "X'$concat'" 28} 29 30proc blob {str} { 31 binary decode hex $str 32} 33 34proc findall {needle haystack} { 35 set L [list] 36 set start 0 37 while { [set idx [string first $needle $haystack $start]]>=0 } { 38 lappend L $idx 39 set start [expr $idx+1] 40 } 41 set L 42} 43 44do_execsql_test 1.0 { 45 CREATE VIRTUAL TABLE aaa USING zipfile('testzip'); 46 CREATE VIRTUAL TABLE bbb USING zipfile("testzip"); 47 CREATE VIRTUAL TABLE ccc USING zipfile(`testzip`); 48 CREATE VIRTUAL TABLE ddd USING zipfile([testzip]); 49 CREATE VIRTUAL TABLE eee USING zipfile(testzip); 50 CREATE VIRTUAL TABLE fff USING zipfile('test''zip'); 51} 52 53do_test 2.0 { 54 forcedelete testdir 55 file mkdir testdir 56 execsql { CREATE VIRTUAL TABLE hhh USING zipfile('testdir') } 57 catchsql { SELECT * FROM hhh } 58} {1 {error in fread()}} 59 60 61set archive { 62 504B0304140000080000D4A52BEC09F3B6E0110000001100000005000900612E 63 747874555405000140420F00636F6E74656E7473206F6620612E747874504B03 64 04140000080000D4A52BECD98916A7110000001100000005000900622E747874 65 555405000140420F00636F6E74656E7473206F6620622E747874504B01021E03 66 140000080000D4A52BEC09F3B6E0110000001100000005000900000000000000 67 0000A48100000000612E747874555405000140420F00504B01021E0314000008 68 0000D4A52BECD98916A71100000011000000050009000000000000000000A481 69 3D000000622E747874555405000140420F00504B050600000000020002007800 70 00007A0000000000 71} 72 73do_execsql_test 3.1 { 74 WITH contents(name,mtime,data) AS ( 75 VALUES('a.txt', 1000000, 'contents of a.txt') UNION ALL 76 VALUES('b.txt', 1000000, 'contents of b.txt') 77 ) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents; 78} [blobliteral $archive] 79 80set blob [blob $archive] 81do_execsql_test 3.2 { 82 SELECT name,mtime,data FROM zipfile($blob) 83} { 84 a.txt 1000000 {contents of a.txt} 85 b.txt 1000000 {contents of b.txt} 86} 87 88# Corrupt each of the 0x50 0x4B (ascii "PK") headers in the file 89# Test that in each case this causes an error. 90# 91set L [findall 504B $archive] 92for {set i 0} {$i < [llength $L]} {incr i} { 93 set idx [lindex $L $i] 94 set a [string replace $archive $idx [expr $idx+3] 0000] 95 set blob [blob $a] 96 do_catchsql_test 3.3.$i { 97 SELECT name,mtime,data FROM zipfile($blob) 98 } {/1 .*/} 99} 100 101set L [findall 5554 $archive] 102for {set i 0} {$i < [llength $L]} {incr i} { 103 set idx [lindex $L $i] 104 set a [string replace $archive $idx [expr $idx+3] 1234] 105 set blob [blob $a] 106 do_execsql_test 3.4.$i { 107 SELECT name,data FROM zipfile($blob) 108 } { 109 a.txt {contents of a.txt} 110 b.txt {contents of b.txt} 111 } 112} 113 114for {set i 0} {$i < [llength $L]} {incr i} { 115 set idx [lindex $L $i] 116 set a [string replace $archive [expr $idx+8] [expr $idx+9] 00] 117 set blob [blob $a] 118 do_execsql_test 3.5.$i { 119 SELECT name,data FROM zipfile($blob) 120 } { 121 a.txt {contents of a.txt} 122 b.txt {contents of b.txt} 123 } 124} 125 126if 0 { 127set blob [db one { 128 WITH contents(name,mtime,data) AS ( 129 VALUES('a.txt', 1000000, 'aaaaaaaaaaaaaaaaaaaaaaa') 130 ) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents; 131}] 132set blob [string range $blob 2 end] 133set blob [string range $blob 0 end-1] 134while {[string length $blob]>0} { 135 puts [string range $blob 0 63] 136 set blob [string range $blob 64 end] 137} 138exit 139} 140 141set archive2 { 142 504B0304140000080800D4A52BEC08F54C6E050000001700000005000900612E 143 747874555405000140420F004B4CC40A00504B01021E03140000080800D4A52B 144 EC08F54C6E0500000017000000050009000000000000000000A4810000000061 145 2E747874555405000140420F00504B050600000000010001003C000000310000 146 000000 147} 148set blob [blob $archive2] 149do_execsql_test 4.0 { 150 SELECT name,mtime,data,method FROM zipfile($blob) 151} { 152 a.txt 1000000 aaaaaaaaaaaaaaaaaaaaaaa 8 153} 154 155breakpoint 156set L [findall 17000000 $archive2] 157set a $archive2 158foreach i $L { set a [string replace $a $i [expr $i+7] 16000000] } 159set blob [blob $a] 160do_catchsql_test 4.1 { 161 SELECT name,mtime,data,method FROM zipfile($blob) 162} {1 {SQL logic error}} 163 164 165 166finish_test 167 168