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