drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements some common TCL routines used for regression |
| 12 | # testing the SQLite library |
| 13 | # |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame^] | 14 | # $Id: tester.tcl,v 1.102 2008/01/19 20:11:26 drh Exp $ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 15 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 16 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 17 | set tcl_precision 15 |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 18 | set sqlite_pending_byte 0x0010000 |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 19 | |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 20 | # |
| 21 | # Check the command-line arguments for a default soft-heap-limit. |
| 22 | # Store this default value in the global variable ::soft_limit and |
| 23 | # update the soft-heap-limit each time this script is run. In that |
| 24 | # way if an individual test file changes the soft-heap-limit, it |
| 25 | # will be reset at the start of the next test file. |
| 26 | # |
| 27 | if {![info exists soft_limit]} { |
| 28 | set soft_limit 0 |
| 29 | for {set i 0} {$i<[llength $argv]} {incr i} { |
| 30 | if {[regexp {^--soft-heap-limit=(.+)$} [lindex $argv $i] all value]} { |
| 31 | if {$value!="off"} { |
| 32 | set soft_limit $value |
| 33 | } |
| 34 | set argv [lreplace $argv $i $i] |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | sqlite3_soft_heap_limit $soft_limit |
| 39 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 40 | # |
| 41 | # Check the command-line arguments to set the memory debugger |
| 42 | # backtrace depth. |
| 43 | # |
| 44 | # See the sqlite3_memdebug_backtrace() function in mem2.c or |
| 45 | # test_malloc.c for additional information. |
| 46 | # |
| 47 | for {set i 0} {$i<[llength $argv]} {incr i} { |
| 48 | if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} { |
| 49 | sqlite3_memdebug_backtrace $value |
| 50 | set argv [lreplace $argv $i $i] |
| 51 | } |
| 52 | } |
| 53 | |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 54 | # |
| 55 | # Check the command-line arguments to set the maximum number of |
| 56 | # errors tolerated before halting. |
| 57 | # |
| 58 | if {![info exists maxErr]} { |
| 59 | set maxErr 1000 |
| 60 | } |
| 61 | for {set i 0} {$i<[llength $argv]} {incr i} { |
| 62 | if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} { |
| 63 | set argv [lreplace $argv $i $i] |
| 64 | } |
| 65 | } |
| 66 | #puts "Max error = $maxErr" |
| 67 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 68 | |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 69 | # Use the pager codec if it is available |
| 70 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 71 | if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} { |
| 72 | rename sqlite3 sqlite_orig |
| 73 | proc sqlite3 {args} { |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 74 | if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} { |
| 75 | lappend args -key {xyzzy} |
| 76 | } |
| 77 | uplevel 1 sqlite_orig $args |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 82 | # Create a test database |
| 83 | # |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 84 | catch {db close} |
| 85 | file delete -force test.db |
| 86 | file delete -force test.db-journal |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 87 | sqlite3 db ./test.db |
| 88 | set ::DB [sqlite3_connection_pointer db] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 89 | if {[info exists ::SETUP_SQL]} { |
| 90 | db eval $::SETUP_SQL |
| 91 | } |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 92 | |
| 93 | # Abort early if this script has been run before. |
| 94 | # |
| 95 | if {[info exists nTest]} return |
| 96 | |
| 97 | # Set the test counters to zero |
| 98 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 99 | set nErr 0 |
| 100 | set nTest 0 |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 101 | set skip_test 0 |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 102 | set failList {} |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 103 | if {![info exists speedTest]} { |
| 104 | set speedTest 0 |
| 105 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 106 | |
| 107 | # Invoke the do_test procedure to run a single test |
| 108 | # |
| 109 | proc do_test {name cmd expected} { |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 110 | global argv nErr nTest skip_test maxErr |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 111 | sqlite3_memdebug_settitle $name |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 112 | if {$skip_test} { |
| 113 | set skip_test 0 |
| 114 | return |
| 115 | } |
| 116 | if {[llength $argv]==0} { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 117 | set go 1 |
| 118 | } else { |
| 119 | set go 0 |
| 120 | foreach pattern $argv { |
| 121 | if {[string match $pattern $name]} { |
| 122 | set go 1 |
| 123 | break |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | if {!$go} return |
| 128 | incr nTest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 129 | puts -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 130 | flush stdout |
| 131 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 132 | puts "\nError: $result" |
| 133 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 134 | lappend ::failList $name |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 135 | if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 136 | } elseif {[string compare $result $expected]} { |
| 137 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
| 138 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 139 | lappend ::failList $name |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 140 | if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 141 | } else { |
| 142 | puts " Ok" |
| 143 | } |
drh | 6558db8 | 2007-04-13 03:23:21 +0000 | [diff] [blame] | 144 | flush stdout |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 145 | } |
| 146 | |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 147 | # Run an SQL script. |
| 148 | # Return the number of microseconds per statement. |
| 149 | # |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 150 | proc speed_trial {name numstmt units sql} { |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 151 | puts -nonewline [format {%-21.21s } $name...] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 152 | flush stdout |
| 153 | set speed [time {sqlite3_exec_nr db $sql}] |
| 154 | set tm [lindex $speed 0] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 155 | set rate [expr {1000000.0*$numstmt/$tm}] |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 156 | set u2 $units/s |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 157 | puts [format {%12d uS %20.5f %s} $tm $rate $u2] |
| 158 | global total_time |
| 159 | set total_time [expr {$total_time+$tm}] |
| 160 | } |
| 161 | proc speed_trial_init {name} { |
| 162 | global total_time |
| 163 | set total_time 0 |
| 164 | } |
| 165 | proc speed_trial_summary {name} { |
| 166 | global total_time |
| 167 | puts [format {%-21.21s %12d uS TOTAL} $name $total_time] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 168 | } |
| 169 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 170 | # Run this routine last |
| 171 | # |
| 172 | proc finish_test {} { |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 173 | finalize_testing |
| 174 | } |
| 175 | proc finalize_testing {} { |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 176 | global nTest nErr sqlite_open_file_count |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 177 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 178 | catch {db close} |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 179 | catch {db2 close} |
| 180 | catch {db3 close} |
| 181 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 182 | vfs_unlink_test |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 183 | sqlite3 db {} |
danielk1977 | cbb8496 | 2006-01-17 16:10:13 +0000 | [diff] [blame] | 184 | # sqlite3_clear_tsd_memdebug |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 185 | db close |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 186 | set heaplimit [sqlite3_soft_heap_limit] |
| 187 | if {$heaplimit!=$::soft_limit} { |
| 188 | puts "soft-heap-limit changed by this script\ |
| 189 | from $::soft_limit to $heaplimit" |
| 190 | } elseif {$heaplimit!="" && $heaplimit>0} { |
| 191 | puts "soft-heap-limit set to $heaplimit" |
| 192 | } |
| 193 | sqlite3_soft_heap_limit 0 |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 194 | incr nTest |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 195 | puts "$nErr errors out of $nTest tests" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 196 | if {$nErr>0} { |
| 197 | puts "Failures on these tests: $::failList" |
| 198 | } |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 199 | if {$nErr>0 && ![working_64bit_int]} { |
| 200 | puts "******************************************************************" |
| 201 | puts "N.B.: The version of TCL that you used to build this test harness" |
| 202 | puts "is defective in that it does not support 64-bit integers. Some or" |
| 203 | puts "all of the test failures above might be a result from this defect" |
| 204 | puts "in your TCL build." |
| 205 | puts "******************************************************************" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 206 | } |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 207 | if {$sqlite_open_file_count} { |
| 208 | puts "$sqlite_open_file_count files were left open" |
| 209 | incr nErr |
| 210 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 211 | if {[sqlite3_memory_used]>0} { |
| 212 | puts "Unfreed memory: [sqlite3_memory_used] bytes" |
| 213 | incr nErr |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 214 | ifcapable memdebug||(mem3&&debug) { |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 215 | puts "Writing unfreed memory log to \"./memleak.txt\"" |
| 216 | sqlite3_memdebug_dump ./memleak.txt |
| 217 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 218 | } else { |
| 219 | puts "All memory allocations freed - no leaks" |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 220 | ifcapable memdebug { |
| 221 | sqlite3_memdebug_dump ./memusage.txt |
| 222 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 223 | } |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame^] | 224 | puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes" |
| 225 | puts "Current memory usage: [sqlite3_memory_highwater] bytes" |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 226 | foreach f [glob -nocomplain test.db-*-journal] { |
| 227 | file delete -force $f |
| 228 | } |
| 229 | foreach f [glob -nocomplain test.db-mj*] { |
| 230 | file delete -force $f |
| 231 | } |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 232 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 233 | } |
| 234 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 235 | # A procedure to execute SQL |
| 236 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 237 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 238 | # puts "SQL = $sql" |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 239 | uplevel [list $db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 240 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 241 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 242 | # Execute SQL and catch exceptions. |
| 243 | # |
| 244 | proc catchsql {sql {db db}} { |
| 245 | # puts "SQL = $sql" |
| 246 | set r [catch {$db eval $sql} msg] |
| 247 | lappend r $msg |
| 248 | return $r |
| 249 | } |
| 250 | |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 251 | # Do an VDBE code dump on the SQL given |
| 252 | # |
| 253 | proc explain {sql {db db}} { |
| 254 | puts "" |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 255 | puts "addr opcode p1 p2 p3 p4 p5 #" |
| 256 | puts "---- ------------ ------ ------ ------ --------------- -- -" |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 257 | $db eval "explain $sql" {} { |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 258 | puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \ |
| 259 | $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment |
| 260 | ] |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 264 | # Another procedure to execute SQL. This one includes the field |
| 265 | # names in the returned list. |
| 266 | # |
| 267 | proc execsql2 {sql} { |
| 268 | set result {} |
| 269 | db eval $sql data { |
| 270 | foreach f $data(*) { |
| 271 | lappend result $f $data($f) |
| 272 | } |
| 273 | } |
| 274 | return $result |
| 275 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 276 | |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 277 | # Use the non-callback API to execute multiple SQL statements |
| 278 | # |
| 279 | proc stepsql {dbptr sql} { |
| 280 | set sql [string trim $sql] |
| 281 | set r 0 |
| 282 | while {[string length $sql]>0} { |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 283 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 284 | return [list 1 $vm] |
| 285 | } |
| 286 | set sql [string trim $sqltail] |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 287 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 288 | # foreach v $VAL {lappend r $v} |
| 289 | # } |
| 290 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 291 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 292 | lappend r [sqlite3_column_text $vm $i] |
| 293 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 294 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 295 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 296 | return [list 1 $errmsg] |
| 297 | } |
| 298 | } |
| 299 | return $r |
| 300 | } |
| 301 | |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 302 | # Delete a file or directory |
| 303 | # |
| 304 | proc forcedelete {filename} { |
| 305 | if {[catch {file delete -force $filename}]} { |
| 306 | exec rm -rf $filename |
| 307 | } |
| 308 | } |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 309 | |
| 310 | # Do an integrity check of the entire database |
| 311 | # |
| 312 | proc integrity_check {name} { |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 313 | ifcapable integrityck { |
| 314 | do_test $name { |
| 315 | execsql {PRAGMA integrity_check} |
| 316 | } {ok} |
| 317 | } |
| 318 | } |
| 319 | |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 320 | proc fix_ifcapable_expr {expr} { |
| 321 | set ret "" |
| 322 | set state 0 |
| 323 | for {set i 0} {$i < [string length $expr]} {incr i} { |
| 324 | set char [string range $expr $i $i] |
| 325 | set newstate [expr {[string is alnum $char] || $char eq "_"}] |
| 326 | if {$newstate && !$state} { |
| 327 | append ret {$::sqlite_options(} |
| 328 | } |
| 329 | if {!$newstate && $state} { |
| 330 | append ret ) |
| 331 | } |
| 332 | append ret $char |
| 333 | set state $newstate |
| 334 | } |
| 335 | if {$state} {append ret )} |
| 336 | return $ret |
| 337 | } |
| 338 | |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 339 | # Evaluate a boolean expression of capabilities. If true, execute the |
| 340 | # code. Omit the code if false. |
| 341 | # |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 342 | proc ifcapable {expr code {else ""} {elsecode ""}} { |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 343 | #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 |
| 344 | set e2 [fix_ifcapable_expr $expr] |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 345 | if ($e2) { |
| 346 | set c [catch {uplevel 1 $code} r] |
| 347 | } else { |
| 348 | set c [catch {uplevel 1 $elsecode} r] |
| 349 | } |
| 350 | return -code $c $r |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 351 | } |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 352 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 353 | # This proc execs a seperate process that crashes midway through executing |
| 354 | # the SQL script $sql on database test.db. |
| 355 | # |
| 356 | # The crash occurs during a sync() of file $crashfile. When the crash |
| 357 | # occurs a random subset of all unsynced writes made by the process are |
| 358 | # written into the files on disk. Argument $crashdelay indicates the |
| 359 | # number of file syncs to wait before crashing. |
| 360 | # |
| 361 | # The return value is a list of two elements. The first element is a |
| 362 | # boolean, indicating whether or not the process actually crashed or |
| 363 | # reported some other error. The second element in the returned list is the |
| 364 | # error message. This is "child process exited abnormally" if the crash |
| 365 | # occured. |
| 366 | # |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 367 | # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 368 | # |
| 369 | proc crashsql {args} { |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 370 | if {$::tcl_platform(platform)!="unix"} { |
| 371 | error "crashsql should only be used on unix" |
| 372 | } |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 373 | |
| 374 | set blocksize "" |
| 375 | set crashdelay 1 |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 376 | set prngseed 0 |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 377 | set tclbody {} |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 378 | set crashfile "" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 379 | set dc "" |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 380 | set sql [lindex $args end] |
| 381 | |
| 382 | for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { |
| 383 | set z [lindex $args $ii] |
| 384 | set n [string length $z] |
| 385 | set z2 [lindex $args [expr $ii+1]] |
| 386 | |
| 387 | if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \ |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 388 | elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 389 | elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 390 | elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 391 | elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 392 | elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 393 | else { error "Unrecognized option: $z" } |
| 394 | } |
| 395 | |
| 396 | if {$crashfile eq ""} { |
| 397 | error "Compulsory option -file missing" |
| 398 | } |
| 399 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 400 | set cfile [file join [pwd] $crashfile] |
| 401 | |
| 402 | set f [open crash.tcl w] |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 403 | puts $f "sqlite3_crash_enable 1" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 404 | puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 405 | puts $f "set sqlite_pending_byte $::sqlite_pending_byte" |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 406 | puts $f "sqlite3 db test.db -vfs crash" |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 407 | |
| 408 | # This block sets the cache size of the main database to 10 |
| 409 | # pages. This is done in case the build is configured to omit |
| 410 | # "PRAGMA cache_size". |
| 411 | puts $f {db eval {SELECT * FROM sqlite_master;}} |
| 412 | puts $f {set bt [btree_from_db db]} |
| 413 | puts $f {btree_set_cache_size $bt 10} |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 414 | if {$prngseed} { |
| 415 | set seed [expr {$prngseed%10007+1}] |
| 416 | # puts seed=$seed |
| 417 | puts $f "db eval {SELECT randomblob($seed)}" |
| 418 | } |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 419 | |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 420 | if {[string length $tclbody]>0} { |
| 421 | puts $f $tclbody |
| 422 | } |
| 423 | if {[string length $sql]>0} { |
| 424 | puts $f "db eval {" |
| 425 | puts $f "$sql" |
| 426 | puts $f "}" |
| 427 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 428 | close $f |
| 429 | |
| 430 | set r [catch { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 431 | exec [info nameofexec] crash.tcl >@stdout |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 432 | } msg] |
| 433 | lappend r $msg |
| 434 | } |
| 435 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 436 | # Usage: do_ioerr_test <test number> <options...> |
| 437 | # |
| 438 | # This proc is used to implement test cases that check that IO errors |
| 439 | # are correctly handled. The first argument, <test number>, is an integer |
| 440 | # used to name the tests executed by this proc. Options are as follows: |
| 441 | # |
| 442 | # -tclprep TCL script to run to prepare test. |
| 443 | # -sqlprep SQL script to run to prepare test. |
| 444 | # -tclbody TCL script to run with IO error simulation. |
| 445 | # -sqlbody TCL script to run with IO error simulation. |
| 446 | # -exclude List of 'N' values not to test. |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 447 | # -erc Use extended result codes |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 448 | # -persist Make simulated I/O errors persistent |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 449 | # -start Value of 'N' to begin with (default 1) |
| 450 | # |
| 451 | # -cksum Boolean. If true, test that the database does |
| 452 | # not change during the execution of the test case. |
| 453 | # |
| 454 | proc do_ioerr_test {testname args} { |
| 455 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 456 | set ::ioerropts(-start) 1 |
| 457 | set ::ioerropts(-cksum) 0 |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 458 | set ::ioerropts(-erc) 0 |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 459 | set ::ioerropts(-count) 100000000 |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 460 | set ::ioerropts(-persist) 1 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 461 | array set ::ioerropts $args |
| 462 | |
| 463 | set ::go 1 |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 464 | #reset_prng_state |
| 465 | save_prng_state |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 466 | for {set n $::ioerropts(-start)} {$::go} {incr n} { |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 467 | set ::TN $n |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 468 | incr ::ioerropts(-count) -1 |
| 469 | if {$::ioerropts(-count)<0} break |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 470 | |
| 471 | # Skip this IO error if it was specified with the "-exclude" option. |
| 472 | if {[info exists ::ioerropts(-exclude)]} { |
| 473 | if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue |
| 474 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 475 | restore_prng_state |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 476 | |
| 477 | # Delete the files test.db and test2.db, then execute the TCL and |
| 478 | # SQL (in that order) to prepare for the test case. |
| 479 | do_test $testname.$n.1 { |
| 480 | set ::sqlite_io_error_pending 0 |
| 481 | catch {db close} |
| 482 | catch {file delete -force test.db} |
| 483 | catch {file delete -force test.db-journal} |
| 484 | catch {file delete -force test2.db} |
| 485 | catch {file delete -force test2.db-journal} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 486 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 487 | sqlite3_extended_result_codes $::DB $::ioerropts(-erc) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 488 | if {[info exists ::ioerropts(-tclprep)]} { |
| 489 | eval $::ioerropts(-tclprep) |
| 490 | } |
| 491 | if {[info exists ::ioerropts(-sqlprep)]} { |
| 492 | execsql $::ioerropts(-sqlprep) |
| 493 | } |
| 494 | expr 0 |
| 495 | } {0} |
| 496 | |
| 497 | # Read the 'checksum' of the database. |
| 498 | if {$::ioerropts(-cksum)} { |
| 499 | set checksum [cksum] |
| 500 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 501 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 502 | # Set the Nth IO error to fail. |
| 503 | do_test $testname.$n.2 [subst { |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 504 | set ::sqlite_io_error_persist $::ioerropts(-persist) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 505 | set ::sqlite_io_error_pending $n |
| 506 | }] $n |
| 507 | |
| 508 | # Create a single TCL script from the TCL and SQL specified |
| 509 | # as the body of the test. |
| 510 | set ::ioerrorbody {} |
| 511 | if {[info exists ::ioerropts(-tclbody)]} { |
| 512 | append ::ioerrorbody "$::ioerropts(-tclbody)\n" |
| 513 | } |
| 514 | if {[info exists ::ioerropts(-sqlbody)]} { |
| 515 | append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" |
| 516 | } |
| 517 | |
| 518 | # Execute the TCL Script created in the above block. If |
| 519 | # there are at least N IO operations performed by SQLite as |
| 520 | # a result of the script, the Nth will fail. |
| 521 | do_test $testname.$n.3 { |
| 522 | set r [catch $::ioerrorbody msg] |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 523 | set rc [sqlite3_errcode $::DB] |
| 524 | if {$::ioerropts(-erc)} { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 525 | # If we are in extended result code mode, make sure all of the |
| 526 | # IOERRs we get back really do have their extended code values. |
| 527 | # If an extended result code is returned, the sqlite3_errcode |
| 528 | # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn |
| 529 | # where nnnn is a number |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 530 | if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { |
| 531 | return $rc |
| 532 | } |
| 533 | } else { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 534 | # If we are not in extended result code mode, make sure no |
| 535 | # extended error codes are returned. |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 536 | if {[regexp {\+\d} $rc]} { |
| 537 | return $rc |
| 538 | } |
| 539 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 540 | # The test repeats as long as $::go is non-zero. $::go starts out |
| 541 | # as 1. When a test runs to completion without hitting an I/O |
| 542 | # error, that means there is no point in continuing with this test |
| 543 | # case so set $::go to zero. |
| 544 | # |
| 545 | if {$::sqlite_io_error_pending>0} { |
| 546 | set ::go 0 |
| 547 | set q 0 |
| 548 | set ::sqlite_io_error_pending 0 |
| 549 | } else { |
| 550 | set q 1 |
| 551 | } |
| 552 | |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 553 | set s [expr $::sqlite_io_error_hit==0] |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 554 | set ::sqlite_io_error_hit 0 |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 555 | |
| 556 | # One of two things must have happened. either |
| 557 | # 1. We never hit the IO error and the SQL returned OK |
| 558 | # 2. An IO error was hit and the SQL failed |
| 559 | # |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 560 | expr { ($s && !$r && !$q) || (!$s && $r && $q) } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 561 | } {1} |
| 562 | |
| 563 | # If an IO error occured, then the checksum of the database should |
| 564 | # be the same as before the script that caused the IO error was run. |
| 565 | if {$::go && $::ioerropts(-cksum)} { |
| 566 | do_test $testname.$n.4 { |
| 567 | catch {db close} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 568 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 569 | cksum |
| 570 | } $checksum |
| 571 | } |
| 572 | |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 573 | set ::sqlite_io_error_pending 0 |
danielk1977 | 105afed | 2005-05-26 15:20:53 +0000 | [diff] [blame] | 574 | if {[info exists ::ioerropts(-cleanup)]} { |
| 575 | catch $::ioerropts(-cleanup) |
| 576 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 577 | } |
| 578 | set ::sqlite_io_error_pending 0 |
drh | 6d54da0 | 2007-03-25 19:08:46 +0000 | [diff] [blame] | 579 | set ::sqlite_io_error_persist 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 580 | unset ::ioerropts |
| 581 | } |
| 582 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 583 | # Return a checksum based on the contents of the main database associated |
| 584 | # with connection $db |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 585 | # |
| 586 | proc cksum {{db db}} { |
| 587 | set txt [$db eval { |
| 588 | SELECT name, type, sql FROM sqlite_master order by name |
| 589 | }]\n |
| 590 | foreach tbl [$db eval { |
| 591 | SELECT name FROM sqlite_master WHERE type='table' order by name |
| 592 | }] { |
| 593 | append txt [$db eval "SELECT * FROM $tbl"]\n |
| 594 | } |
| 595 | foreach prag {default_synchronous default_cache_size} { |
| 596 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 597 | } |
| 598 | set cksum [string length $txt]-[md5 $txt] |
| 599 | # puts $cksum-[file size test.db] |
| 600 | return $cksum |
| 601 | } |
| 602 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 603 | # Generate a checksum based on the contents of the main and temp tables |
| 604 | # database $db. If the checksum of two databases is the same, and the |
| 605 | # integrity-check passes for both, the two databases are identical. |
| 606 | # |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 607 | proc allcksum {{db db}} { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 608 | set ret [list] |
| 609 | ifcapable tempdb { |
| 610 | set sql { |
| 611 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 612 | SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION |
| 613 | SELECT 'sqlite_master' UNION |
| 614 | SELECT 'sqlite_temp_master' ORDER BY 1 |
| 615 | } |
| 616 | } else { |
| 617 | set sql { |
| 618 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 619 | SELECT 'sqlite_master' ORDER BY 1 |
| 620 | } |
| 621 | } |
| 622 | set tbllist [$db eval $sql] |
| 623 | set txt {} |
| 624 | foreach tbl $tbllist { |
| 625 | append txt [$db eval "SELECT * FROM $tbl"] |
| 626 | } |
| 627 | foreach prag {default_cache_size} { |
| 628 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 629 | } |
| 630 | # puts txt=$txt |
| 631 | return [md5 $txt] |
| 632 | } |
| 633 | |
| 634 | |
| 635 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 636 | # Copy file $from into $to. This is used because some versions of |
| 637 | # TCL for windows (notably the 8.4.1 binary package shipped with the |
| 638 | # current mingw release) have a broken "file copy" command. |
| 639 | # |
| 640 | proc copy_file {from to} { |
| 641 | if {$::tcl_platform(platform)=="unix"} { |
| 642 | file copy -force $from $to |
| 643 | } else { |
| 644 | set f [open $from] |
| 645 | fconfigure $f -translation binary |
| 646 | set t [open $to w] |
| 647 | fconfigure $t -translation binary |
| 648 | puts -nonewline $t [read $f [file size $from]] |
| 649 | close $t |
| 650 | close $f |
| 651 | } |
| 652 | } |
| 653 | |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 654 | # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set |
| 655 | # to non-zero, then set the global variable $AUTOVACUUM to 1. |
| 656 | set AUTOVACUUM $sqlite_options(default_autovacuum) |