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