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 | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame^] | 14 | # $Id: tester.tcl,v 1.69 2006/10/04 11:55:50 drh Exp $ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 15 | |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 16 | # Make sure tclsqlite3 was compiled correctly. Abort now with an |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 17 | # error message if not. |
| 18 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 19 | if {[sqlite3 -tcl-uses-utf]} { |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 20 | if {"\u1234"=="u1234"} { |
| 21 | puts stderr "***** BUILD PROBLEM *****" |
| 22 | puts stderr "$argv0 was linked against an older version" |
| 23 | puts stderr "of TCL that does not support Unicode, but uses a header" |
| 24 | puts stderr "file (\"tcl.h\") from a new TCL version that does support" |
| 25 | puts stderr "Unicode. This combination causes internal errors." |
| 26 | puts stderr "Recompile using a TCL library and header file that match" |
| 27 | puts stderr "and try again.\n**************************" |
| 28 | exit 1 |
| 29 | } |
| 30 | } else { |
| 31 | if {"\u1234"!="u1234"} { |
| 32 | puts stderr "***** BUILD PROBLEM *****" |
| 33 | puts stderr "$argv0 was linked against an newer version" |
| 34 | puts stderr "of TCL that supports Unicode, but uses a header file" |
| 35 | puts stderr "(\"tcl.h\") from a old TCL version that does not support" |
| 36 | puts stderr "Unicode. This combination causes internal errors." |
| 37 | puts stderr "Recompile using a TCL library and header file that match" |
| 38 | puts stderr "and try again.\n**************************" |
| 39 | exit 1 |
| 40 | } |
| 41 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 42 | |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 43 | set tcl_precision 15 |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 44 | set sqlite_pending_byte 0x0010000 |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 45 | |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 46 | # Use the pager codec if it is available |
| 47 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 48 | if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} { |
| 49 | rename sqlite3 sqlite_orig |
| 50 | proc sqlite3 {args} { |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 51 | if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} { |
| 52 | lappend args -key {xyzzy} |
| 53 | } |
| 54 | uplevel 1 sqlite_orig $args |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 59 | # Create a test database |
| 60 | # |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 61 | catch {db close} |
| 62 | file delete -force test.db |
| 63 | file delete -force test.db-journal |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 64 | sqlite3 db ./test.db |
| 65 | set ::DB [sqlite3_connection_pointer db] |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 66 | if {[info exists ::SETUP_SQL]} { |
| 67 | db eval $::SETUP_SQL |
| 68 | } |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 69 | |
| 70 | # Abort early if this script has been run before. |
| 71 | # |
| 72 | if {[info exists nTest]} return |
| 73 | |
| 74 | # Set the test counters to zero |
| 75 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 76 | set nErr 0 |
| 77 | set nTest 0 |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 78 | set skip_test 0 |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 79 | set failList {} |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 80 | set maxErr 1000 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 81 | |
| 82 | # Invoke the do_test procedure to run a single test |
| 83 | # |
| 84 | proc do_test {name cmd expected} { |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 85 | global argv nErr nTest skip_test maxErr |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 86 | set ::sqlite_malloc_id $name |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 87 | if {$skip_test} { |
| 88 | set skip_test 0 |
| 89 | return |
| 90 | } |
| 91 | if {[llength $argv]==0} { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 92 | set go 1 |
| 93 | } else { |
| 94 | set go 0 |
| 95 | foreach pattern $argv { |
| 96 | if {[string match $pattern $name]} { |
| 97 | set go 1 |
| 98 | break |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | if {!$go} return |
| 103 | incr nTest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 104 | puts -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 105 | flush stdout |
| 106 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 107 | puts "\nError: $result" |
| 108 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 109 | lappend ::failList $name |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 110 | if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 111 | } elseif {[string compare $result $expected]} { |
| 112 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
| 113 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 114 | lappend ::failList $name |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 115 | if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 116 | } else { |
| 117 | puts " Ok" |
| 118 | } |
| 119 | } |
| 120 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 121 | # The procedure uses the special "sqlite_malloc_stat" command |
drh | c89b91b | 2005-01-03 01:32:59 +0000 | [diff] [blame] | 122 | # (which is only available if SQLite is compiled with -DSQLITE_DEBUG=1) |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 123 | # to see how many malloc()s have not been free()ed. The number |
| 124 | # of surplus malloc()s is stored in the global variable $::Leak. |
| 125 | # If the value in $::Leak grows, it may mean there is a memory leak |
| 126 | # in the library. |
| 127 | # |
| 128 | proc memleak_check {} { |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 129 | if {[info command sqlite_malloc_stat]!=""} { |
| 130 | set r [sqlite_malloc_stat] |
| 131 | set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}] |
| 132 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 133 | } |
| 134 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 135 | # Run this routine last |
| 136 | # |
| 137 | proc finish_test {} { |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 138 | finalize_testing |
| 139 | } |
| 140 | proc finalize_testing {} { |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 141 | global nTest nErr sqlite_open_file_count |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 142 | if {$nErr==0} memleak_check |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 143 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 144 | catch {db close} |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 145 | catch {db2 close} |
| 146 | catch {db3 close} |
| 147 | |
danielk1977 | 97cb2e9 | 2005-12-09 14:39:04 +0000 | [diff] [blame] | 148 | catch { |
| 149 | pp_check_for_leaks |
| 150 | } |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 151 | sqlite3 db {} |
danielk1977 | cbb8496 | 2006-01-17 16:10:13 +0000 | [diff] [blame] | 152 | # sqlite3_clear_tsd_memdebug |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 153 | db close |
| 154 | if {$::sqlite3_tsd_count} { |
| 155 | puts "Thread-specific data leak: $::sqlite3_tsd_count instances" |
| 156 | incr nErr |
| 157 | } else { |
| 158 | puts "Thread-specific data deallocated properly" |
| 159 | } |
| 160 | incr nTest |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 161 | puts "$nErr errors out of $nTest tests" |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 162 | puts "Failures on these tests: $::failList" |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 163 | if {$nErr>0 && ![working_64bit_int]} { |
| 164 | puts "******************************************************************" |
| 165 | puts "N.B.: The version of TCL that you used to build this test harness" |
| 166 | puts "is defective in that it does not support 64-bit integers. Some or" |
| 167 | puts "all of the test failures above might be a result from this defect" |
| 168 | puts "in your TCL build." |
| 169 | puts "******************************************************************" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 170 | } |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 171 | if {$sqlite_open_file_count} { |
| 172 | puts "$sqlite_open_file_count files were left open" |
| 173 | incr nErr |
| 174 | } |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame^] | 175 | foreach f [glob -nocomplain test.db-*-journal] { |
| 176 | file delete -force $f |
| 177 | } |
| 178 | foreach f [glob -nocomplain test.db-mj*] { |
| 179 | file delete -force $f |
| 180 | } |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 181 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 182 | } |
| 183 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 184 | # A procedure to execute SQL |
| 185 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 186 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 187 | # puts "SQL = $sql" |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 188 | uplevel [list $db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 189 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 190 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 191 | # Execute SQL and catch exceptions. |
| 192 | # |
| 193 | proc catchsql {sql {db db}} { |
| 194 | # puts "SQL = $sql" |
| 195 | set r [catch {$db eval $sql} msg] |
| 196 | lappend r $msg |
| 197 | return $r |
| 198 | } |
| 199 | |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 200 | # Do an VDBE code dump on the SQL given |
| 201 | # |
| 202 | proc explain {sql {db db}} { |
| 203 | puts "" |
| 204 | puts "addr opcode p1 p2 p3 " |
| 205 | puts "---- ------------ ------ ------ ---------------" |
| 206 | $db eval "explain $sql" {} { |
| 207 | puts [format {%-4d %-12.12s %-6d %-6d %s} $addr $opcode $p1 $p2 $p3] |
| 208 | } |
| 209 | } |
| 210 | |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 211 | # Another procedure to execute SQL. This one includes the field |
| 212 | # names in the returned list. |
| 213 | # |
| 214 | proc execsql2 {sql} { |
| 215 | set result {} |
| 216 | db eval $sql data { |
| 217 | foreach f $data(*) { |
| 218 | lappend result $f $data($f) |
| 219 | } |
| 220 | } |
| 221 | return $result |
| 222 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 223 | |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 224 | # Use the non-callback API to execute multiple SQL statements |
| 225 | # |
| 226 | proc stepsql {dbptr sql} { |
| 227 | set sql [string trim $sql] |
| 228 | set r 0 |
| 229 | while {[string length $sql]>0} { |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 230 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 231 | return [list 1 $vm] |
| 232 | } |
| 233 | set sql [string trim $sqltail] |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 234 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 235 | # foreach v $VAL {lappend r $v} |
| 236 | # } |
| 237 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 238 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 239 | lappend r [sqlite3_column_text $vm $i] |
| 240 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 241 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 242 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 243 | return [list 1 $errmsg] |
| 244 | } |
| 245 | } |
| 246 | return $r |
| 247 | } |
| 248 | |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 249 | # Delete a file or directory |
| 250 | # |
| 251 | proc forcedelete {filename} { |
| 252 | if {[catch {file delete -force $filename}]} { |
| 253 | exec rm -rf $filename |
| 254 | } |
| 255 | } |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 256 | |
| 257 | # Do an integrity check of the entire database |
| 258 | # |
| 259 | proc integrity_check {name} { |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 260 | ifcapable integrityck { |
| 261 | do_test $name { |
| 262 | execsql {PRAGMA integrity_check} |
| 263 | } {ok} |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | # Evaluate a boolean expression of capabilities. If true, execute the |
| 268 | # code. Omit the code if false. |
| 269 | # |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 270 | proc ifcapable {expr code {else ""} {elsecode ""}} { |
drh | 5436dc2 | 2004-11-14 04:04:17 +0000 | [diff] [blame] | 271 | regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 272 | if ($e2) { |
| 273 | set c [catch {uplevel 1 $code} r] |
| 274 | } else { |
| 275 | set c [catch {uplevel 1 $elsecode} r] |
| 276 | } |
| 277 | return -code $c $r |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 278 | } |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 279 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 280 | # This proc execs a seperate process that crashes midway through executing |
| 281 | # the SQL script $sql on database test.db. |
| 282 | # |
| 283 | # The crash occurs during a sync() of file $crashfile. When the crash |
| 284 | # occurs a random subset of all unsynced writes made by the process are |
| 285 | # written into the files on disk. Argument $crashdelay indicates the |
| 286 | # number of file syncs to wait before crashing. |
| 287 | # |
| 288 | # The return value is a list of two elements. The first element is a |
| 289 | # boolean, indicating whether or not the process actually crashed or |
| 290 | # reported some other error. The second element in the returned list is the |
| 291 | # error message. This is "child process exited abnormally" if the crash |
| 292 | # occured. |
| 293 | # |
| 294 | proc crashsql {crashdelay crashfile sql} { |
| 295 | if {$::tcl_platform(platform)!="unix"} { |
| 296 | error "crashsql should only be used on unix" |
| 297 | } |
| 298 | set cfile [file join [pwd] $crashfile] |
| 299 | |
| 300 | set f [open crash.tcl w] |
| 301 | puts $f "sqlite3_crashparams $crashdelay $cfile" |
| 302 | puts $f "sqlite3 db test.db" |
| 303 | puts $f "db eval {pragma cache_size = 10}" |
| 304 | puts $f "db eval {" |
| 305 | puts $f "$sql" |
| 306 | puts $f "}" |
| 307 | close $f |
| 308 | |
| 309 | set r [catch { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 310 | exec [info nameofexec] crash.tcl >@stdout |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 311 | } msg] |
| 312 | lappend r $msg |
| 313 | } |
| 314 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 315 | # Usage: do_ioerr_test <test number> <options...> |
| 316 | # |
| 317 | # This proc is used to implement test cases that check that IO errors |
| 318 | # are correctly handled. The first argument, <test number>, is an integer |
| 319 | # used to name the tests executed by this proc. Options are as follows: |
| 320 | # |
| 321 | # -tclprep TCL script to run to prepare test. |
| 322 | # -sqlprep SQL script to run to prepare test. |
| 323 | # -tclbody TCL script to run with IO error simulation. |
| 324 | # -sqlbody TCL script to run with IO error simulation. |
| 325 | # -exclude List of 'N' values not to test. |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 326 | # -erc Use extended result codes |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 327 | # -start Value of 'N' to begin with (default 1) |
| 328 | # |
| 329 | # -cksum Boolean. If true, test that the database does |
| 330 | # not change during the execution of the test case. |
| 331 | # |
| 332 | proc do_ioerr_test {testname args} { |
| 333 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 334 | set ::ioerropts(-start) 1 |
| 335 | set ::ioerropts(-cksum) 0 |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 336 | set ::ioerropts(-erc) 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 337 | array set ::ioerropts $args |
| 338 | |
| 339 | set ::go 1 |
| 340 | for {set n $::ioerropts(-start)} {$::go} {incr n} { |
| 341 | |
| 342 | # Skip this IO error if it was specified with the "-exclude" option. |
| 343 | if {[info exists ::ioerropts(-exclude)]} { |
| 344 | if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue |
| 345 | } |
| 346 | |
| 347 | # Delete the files test.db and test2.db, then execute the TCL and |
| 348 | # SQL (in that order) to prepare for the test case. |
| 349 | do_test $testname.$n.1 { |
| 350 | set ::sqlite_io_error_pending 0 |
| 351 | catch {db close} |
| 352 | catch {file delete -force test.db} |
| 353 | catch {file delete -force test.db-journal} |
| 354 | catch {file delete -force test2.db} |
| 355 | catch {file delete -force test2.db-journal} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 356 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 357 | sqlite3_extended_result_codes $::DB $::ioerropts(-erc) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 358 | if {[info exists ::ioerropts(-tclprep)]} { |
| 359 | eval $::ioerropts(-tclprep) |
| 360 | } |
| 361 | if {[info exists ::ioerropts(-sqlprep)]} { |
| 362 | execsql $::ioerropts(-sqlprep) |
| 363 | } |
| 364 | expr 0 |
| 365 | } {0} |
| 366 | |
| 367 | # Read the 'checksum' of the database. |
| 368 | if {$::ioerropts(-cksum)} { |
| 369 | set checksum [cksum] |
| 370 | } |
| 371 | |
| 372 | # Set the Nth IO error to fail. |
| 373 | do_test $testname.$n.2 [subst { |
| 374 | set ::sqlite_io_error_pending $n |
| 375 | }] $n |
| 376 | |
| 377 | # Create a single TCL script from the TCL and SQL specified |
| 378 | # as the body of the test. |
| 379 | set ::ioerrorbody {} |
| 380 | if {[info exists ::ioerropts(-tclbody)]} { |
| 381 | append ::ioerrorbody "$::ioerropts(-tclbody)\n" |
| 382 | } |
| 383 | if {[info exists ::ioerropts(-sqlbody)]} { |
| 384 | append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" |
| 385 | } |
| 386 | |
| 387 | # Execute the TCL Script created in the above block. If |
| 388 | # there are at least N IO operations performed by SQLite as |
| 389 | # a result of the script, the Nth will fail. |
| 390 | do_test $testname.$n.3 { |
| 391 | set r [catch $::ioerrorbody msg] |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 392 | # puts rc=[sqlite3_errcode $::DB] |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 393 | set rc [sqlite3_errcode $::DB] |
| 394 | if {$::ioerropts(-erc)} { |
| 395 | # In extended result mode, all IOERRs are qualified |
| 396 | if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { |
| 397 | return $rc |
| 398 | } |
| 399 | } else { |
| 400 | # Not in extended result mode, no errors are qualified |
| 401 | if {[regexp {\+\d} $rc]} { |
| 402 | return $rc |
| 403 | } |
| 404 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 405 | set ::go [expr {$::sqlite_io_error_pending<=0}] |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 406 | set s [expr $::sqlite_io_error_hit==0] |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 407 | set ::sqlite_io_error_hit 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 408 | # puts "$::sqlite_io_error_pending $r $msg" |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 409 | # puts "r=$r s=$s go=$::go msg=\"$msg\"" |
| 410 | expr { ($s && !$r && !$::go) || (!$s && $r && $::go) } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 411 | # expr {$::sqlite_io_error_pending>0 || $r!=0} |
| 412 | } {1} |
| 413 | |
| 414 | # If an IO error occured, then the checksum of the database should |
| 415 | # be the same as before the script that caused the IO error was run. |
| 416 | if {$::go && $::ioerropts(-cksum)} { |
| 417 | do_test $testname.$n.4 { |
| 418 | catch {db close} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 419 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 420 | cksum |
| 421 | } $checksum |
| 422 | } |
| 423 | |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 424 | set ::sqlite_io_error_pending 0 |
danielk1977 | 105afed | 2005-05-26 15:20:53 +0000 | [diff] [blame] | 425 | if {[info exists ::ioerropts(-cleanup)]} { |
| 426 | catch $::ioerropts(-cleanup) |
| 427 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 428 | } |
| 429 | set ::sqlite_io_error_pending 0 |
| 430 | unset ::ioerropts |
| 431 | } |
| 432 | |
| 433 | # Return a checksum based on the contents of database 'db'. |
| 434 | # |
| 435 | proc cksum {{db db}} { |
| 436 | set txt [$db eval { |
| 437 | SELECT name, type, sql FROM sqlite_master order by name |
| 438 | }]\n |
| 439 | foreach tbl [$db eval { |
| 440 | SELECT name FROM sqlite_master WHERE type='table' order by name |
| 441 | }] { |
| 442 | append txt [$db eval "SELECT * FROM $tbl"]\n |
| 443 | } |
| 444 | foreach prag {default_synchronous default_cache_size} { |
| 445 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 446 | } |
| 447 | set cksum [string length $txt]-[md5 $txt] |
| 448 | # puts $cksum-[file size test.db] |
| 449 | return $cksum |
| 450 | } |
| 451 | |
| 452 | # Copy file $from into $to. This is used because some versions of |
| 453 | # TCL for windows (notably the 8.4.1 binary package shipped with the |
| 454 | # current mingw release) have a broken "file copy" command. |
| 455 | # |
| 456 | proc copy_file {from to} { |
| 457 | if {$::tcl_platform(platform)=="unix"} { |
| 458 | file copy -force $from $to |
| 459 | } else { |
| 460 | set f [open $from] |
| 461 | fconfigure $f -translation binary |
| 462 | set t [open $to w] |
| 463 | fconfigure $t -translation binary |
| 464 | puts -nonewline $t [read $f [file size $from]] |
| 465 | close $t |
| 466 | close $f |
| 467 | } |
| 468 | } |
| 469 | |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 470 | # This command checks for outstanding calls to sqliteMalloc() from within |
| 471 | # the current thread. A list is returned with one entry for each outstanding |
| 472 | # malloc. Each list entry is itself a list of 5 items, as follows: |
| 473 | # |
| 474 | # { <number-bytes> <file-name> <line-number> <test-case> <stack-dump> } |
| 475 | # |
| 476 | proc check_for_leaks {} { |
| 477 | set ret [list] |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 478 | set cnt 0 |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 479 | foreach alloc [sqlite_malloc_outstanding] { |
| 480 | foreach {nBytes file iLine userstring backtrace} $alloc {} |
| 481 | set stack [list] |
| 482 | set skip 0 |
| 483 | |
| 484 | # The first command in this block will probably fail on windows. This |
| 485 | # means there will be no stack dump available. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 486 | if {$cnt < 25 && $backtrace!=""} { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 487 | catch { |
| 488 | set stuff [eval "exec addr2line -e ./testfixture -f $backtrace"] |
| 489 | foreach {func line} $stuff { |
| 490 | if {$func != "??" || $line != "??:0"} { |
| 491 | regexp {.*/(.*)} $line dummy line |
| 492 | lappend stack "${func}() $line" |
| 493 | } else { |
| 494 | if {[lindex $stack end] != "..."} { |
| 495 | lappend stack "..." |
| 496 | } |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 500 | incr cnt |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if {!$skip} { |
| 504 | lappend ret [list $nBytes $file $iLine $userstring $stack] |
| 505 | } |
| 506 | } |
| 507 | return $ret |
| 508 | } |
| 509 | |
| 510 | # Pretty print a report based on the return value of [check_for_leaks] to |
| 511 | # stdout. |
| 512 | proc pp_check_for_leaks {} { |
| 513 | set l [check_for_leaks] |
| 514 | set n 0 |
| 515 | foreach leak $l { |
| 516 | foreach {nBytes file iLine userstring stack} $leak {} |
| 517 | puts "$nBytes bytes leaked at $file:$iLine ($userstring)" |
| 518 | foreach frame $stack { |
| 519 | puts " $frame" |
| 520 | } |
| 521 | incr n $nBytes |
| 522 | } |
| 523 | puts "Memory leaked: $n bytes in [llength $l] allocations" |
| 524 | puts "" |
| 525 | } |
| 526 | |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 527 | # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set |
| 528 | # to non-zero, then set the global variable $AUTOVACUUM to 1. |
| 529 | set AUTOVACUUM $sqlite_options(default_autovacuum) |