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 | bb77b75 | 2009-04-09 01:23:49 +0000 | [diff] [blame] | 14 | # $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 15 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 16 | #------------------------------------------------------------------------- |
| 17 | # The commands provided by the code in this file to help with creating |
| 18 | # test cases are as follows: |
drh | 8359d8c | 2008-03-29 11:00:54 +0000 | [diff] [blame] | 19 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 20 | # Commands to manipulate the db and the file-system at a high level: |
drh | 8359d8c | 2008-03-29 11:00:54 +0000 | [diff] [blame] | 21 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 22 | # copy_file FROM TO |
| 23 | # drop_all_table ?DB? |
| 24 | # forcedelete FILENAME |
| 25 | # |
| 26 | # Test the capability of the SQLite version built into the interpreter to |
| 27 | # determine if a specific test can be run: |
| 28 | # |
| 29 | # ifcapable EXPR |
| 30 | # |
| 31 | # Calulate checksums based on database contents: |
| 32 | # |
| 33 | # dbcksum DB DBNAME |
| 34 | # allcksum ?DB? |
| 35 | # cksum ?DB? |
| 36 | # |
| 37 | # Commands to execute/explain SQL statements: |
| 38 | # |
| 39 | # stepsql DB SQL |
| 40 | # execsql2 SQL |
| 41 | # explain_no_trace SQL |
| 42 | # explain SQL ?DB? |
| 43 | # catchsql SQL ?DB? |
| 44 | # execsql SQL ?DB? |
| 45 | # |
| 46 | # Commands to run test cases: |
| 47 | # |
| 48 | # do_ioerr_test TESTNAME ARGS... |
| 49 | # crashsql ARGS... |
| 50 | # integrity_check TESTNAME ?DB? |
| 51 | # do_test TESTNAME SCRIPT EXPECTED |
| 52 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 53 | # Commands providing a lower level interface to the global test counters: |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 54 | # |
| 55 | # set_test_counter COUNTER ?VALUE? |
| 56 | # omit_test TESTNAME REASON |
| 57 | # fail_test TESTNAME |
| 58 | # incr_ntest |
| 59 | # |
| 60 | # Command run at the end of each test file: |
| 61 | # |
| 62 | # finish_test |
| 63 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 64 | # Commands to help create test files that run with the "WAL" and other |
| 65 | # permutations (see file permutations.test): |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 66 | # |
| 67 | # wal_is_wal_mode |
| 68 | # wal_set_journal_mode ?DB? |
| 69 | # wal_check_journal_mode TESTNAME?DB? |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 70 | # permutation |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 71 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 72 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 73 | # Set the precision of FP arithmatic used by the interpreter. And |
| 74 | # configure SQLite to take database file locks on the page that begins |
| 75 | # 64KB into the database file instead of the one 1GB in. This means |
| 76 | # the code that handles that special case can be tested without creating |
| 77 | # very large database files. |
| 78 | # |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 79 | set tcl_precision 15 |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 80 | sqlite3_test_control_pending_byte 0x0010000 |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 81 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 82 | |
| 83 | # If the pager codec is available, create a wrapper for the [sqlite3] |
| 84 | # command that appends "-key {xyzzy}" to the command line. i.e. this: |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 85 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 86 | # sqlite3 db test.db |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 87 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 88 | # becomes |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 89 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 90 | # sqlite3 db test.db -key {xyzzy} |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 91 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 92 | if {[info command sqlite_orig]==""} { |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 93 | rename sqlite3 sqlite_orig |
| 94 | proc sqlite3 {args} { |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 95 | if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 96 | # This command is opening a new database connection. |
| 97 | # |
| 98 | if {[info exists ::G(perm:sqlite3_args)]} { |
| 99 | set args [concat $args $::G(perm:sqlite3_args)] |
| 100 | } |
| 101 | if {[sqlite_orig -has-codec]} { |
| 102 | lappend args -key {xyzzy} |
| 103 | } |
| 104 | |
| 105 | uplevel 1 sqlite_orig $args |
| 106 | |
| 107 | if {[info exists ::G(perm:presql)]} { |
| 108 | [lindex $args 0] eval $::G(perm:presql) |
| 109 | } |
| 110 | } else { |
| 111 | # This command is not opening a new database connection. Pass the |
| 112 | # arguments through to the C implemenation as the are. |
| 113 | # |
| 114 | uplevel 1 sqlite_orig $args |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 115 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 119 | # The following block only runs the first time this file is sourced. It |
| 120 | # does not run in slave interpreters (since the ::cmdlinearg array is |
| 121 | # populated before the test script is run in slave interpreters). |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 122 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 123 | if {[info exists cmdlinearg]==0} { |
| 124 | |
| 125 | # Parse any options specified in the $argv array. This script accepts the |
| 126 | # following options: |
| 127 | # |
| 128 | # --pause |
| 129 | # --soft-heap-limit=NN |
| 130 | # --maxerror=NN |
| 131 | # --malloctrace=N |
| 132 | # --backtrace=N |
| 133 | # --binarylog=N |
| 134 | # |
| 135 | set cmdlinearg(soft-heap-limit) 0 |
| 136 | set cmdlinearg(maxerror) 1000 |
| 137 | set cmdlinearg(malloctrace) 0 |
| 138 | set cmdlinearg(backtrace) 10 |
| 139 | set cmdlinearg(binarylog) 0 |
| 140 | |
| 141 | set leftover [list] |
| 142 | foreach a $argv { |
| 143 | switch -regexp -- $a { |
| 144 | {^-+pause$} { |
| 145 | # Wait for user input before continuing. This is to give the user an |
| 146 | # opportunity to connect profiling tools to the process. |
| 147 | puts -nonewline "Press RETURN to begin..." |
| 148 | flush stdout |
| 149 | gets stdin |
| 150 | } |
| 151 | {^-+soft-heap-limit=.+$} { |
| 152 | foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break |
| 153 | } |
| 154 | {^-+maxerror=.+$} { |
| 155 | foreach {dummy cmdlinearg(maxerror)} [split $a =] break |
| 156 | } |
| 157 | {^-+malloctrace=.+$} { |
| 158 | foreach {dummy cmdlinearg(malloctrace)} [split $a =] break |
| 159 | if {$cmdlinearg(malloctrace)} { |
| 160 | sqlite3_memdebug_log start |
| 161 | } |
| 162 | } |
| 163 | {^-+backtrace=.+$} { |
| 164 | foreach {dummy cmdlinearg(backtrace)} [split $a =] break |
| 165 | } |
| 166 | sqlite3_memdebug_backtrace $value |
| 167 | {^-+binarylog=.+$} { |
| 168 | foreach {dummy cmdlinearg(binarylog)} [split $a =] break |
| 169 | } |
| 170 | default { |
| 171 | lappend leftover $a |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | set argv $leftover |
| 176 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 177 | # Install the malloc layer used to inject OOM errors. And the 'automatic' |
| 178 | # extensions. This only needs to be done once for the process. |
| 179 | # |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 180 | sqlite3_shutdown |
| 181 | install_malloc_faultsim 1 |
| 182 | sqlite3_initialize |
drh | bb77b75 | 2009-04-09 01:23:49 +0000 | [diff] [blame] | 183 | autoinstall_test_functions |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 184 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 185 | # If the --binarylog option was specified, create the logging VFS. This |
| 186 | # call installs the new VFS as the default for all SQLite connections. |
| 187 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 188 | if {$cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 189 | vfslog new binarylog {} vfslog.bin |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | # Set the backtrace depth, if malloc tracing is enabled. |
| 193 | # |
| 194 | if {$cmdlinearg(malloctrace)} { |
| 195 | sqlite3_memdebug_backtrace $cmdlinearg(backtrace) |
danielk1977 | 185eac9 | 2008-07-12 15:55:54 +0000 | [diff] [blame] | 196 | } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 197 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 198 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 199 | # Update the soft-heap-limit each time this script is run. In that |
| 200 | # way if an individual test file changes the soft-heap-limit, it |
| 201 | # will be reset at the start of the next test file. |
| 202 | # |
| 203 | sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) |
| 204 | |
| 205 | # Create a test database |
| 206 | # |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 207 | proc reset_db {} { |
| 208 | catch {db close} |
| 209 | file delete -force test.db |
| 210 | file delete -force test.db-journal |
dan | d3f8f94 | 2010-04-13 11:35:01 +0000 | [diff] [blame] | 211 | file delete -force test.db-wal |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 212 | sqlite3 db ./test.db |
| 213 | set ::DB [sqlite3_connection_pointer db] |
| 214 | if {[info exists ::SETUP_SQL]} { |
| 215 | db eval $::SETUP_SQL |
| 216 | } |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 217 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 218 | reset_db |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 219 | |
| 220 | # Abort early if this script has been run before. |
| 221 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 222 | if {[info exists TC(count)]} return |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 223 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 224 | # Initialize the test counters and set up commands to access them. |
| 225 | # Or, if this is a slave interpreter, set up aliases to write the |
| 226 | # counters in the parent interpreter. |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 227 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 228 | if {0==[info exists ::SLAVE]} { |
| 229 | set TC(errors) 0 |
| 230 | set TC(count) 0 |
| 231 | set TC(fail_list) [list] |
| 232 | set TC(omit_list) [list] |
| 233 | |
| 234 | proc set_test_counter {counter args} { |
| 235 | if {[llength $args]} { |
| 236 | set ::TC($counter) [lindex $args 0] |
| 237 | } |
| 238 | set ::TC($counter) |
| 239 | } |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 240 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 241 | |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 242 | # Record the fact that a sequence of tests were omitted. |
| 243 | # |
| 244 | proc omit_test {name reason} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 245 | set omitList [set_test_counter omit_list] |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 246 | lappend omitList [list $name $reason] |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 247 | set_test_counter omit_list $omitList |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 248 | } |
| 249 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 250 | # Record the fact that a test failed. |
| 251 | # |
| 252 | proc fail_test {name} { |
| 253 | set f [set_test_counter fail_list] |
| 254 | lappend f $name |
| 255 | set_test_counter fail_list $f |
| 256 | set_test_counter errors [expr [set_test_counter errors] + 1] |
| 257 | |
| 258 | set nFail [set_test_counter errors] |
| 259 | if {$nFail>=$::cmdlinearg(maxerror)} { |
| 260 | puts "*** Giving up..." |
| 261 | finalize_testing |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | # Increment the number of tests run |
| 266 | # |
| 267 | proc incr_ntest {} { |
| 268 | set_test_counter count [expr [set_test_counter count] + 1] |
| 269 | } |
| 270 | |
| 271 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 272 | # Invoke the do_test procedure to run a single test |
| 273 | # |
| 274 | proc do_test {name cmd expected} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 275 | |
| 276 | global argv cmdlinearg |
| 277 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 278 | sqlite3_memdebug_settitle $name |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 279 | |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 280 | if {[llength $argv]==0} { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 281 | set go 1 |
| 282 | } else { |
| 283 | set go 0 |
| 284 | foreach pattern $argv { |
| 285 | if {[string match $pattern $name]} { |
| 286 | set go 1 |
| 287 | break |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | if {!$go} return |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 292 | |
| 293 | if {[info exists ::G(perm:name)]} { |
| 294 | set name "$::G(perm:name)-$name" |
| 295 | } |
| 296 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 297 | incr_ntest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 298 | puts -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 299 | flush stdout |
| 300 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 301 | puts "\nError: $result" |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 302 | fail_test $name |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 303 | } elseif {[string compare $result $expected]} { |
| 304 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 305 | fail_test $name |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 306 | } else { |
| 307 | puts " Ok" |
| 308 | } |
drh | 6558db8 | 2007-04-13 03:23:21 +0000 | [diff] [blame] | 309 | flush stdout |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 310 | } |
| 311 | |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 312 | # Run an SQL script. |
| 313 | # Return the number of microseconds per statement. |
| 314 | # |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 315 | proc speed_trial {name numstmt units sql} { |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 316 | puts -nonewline [format {%-21.21s } $name...] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 317 | flush stdout |
| 318 | set speed [time {sqlite3_exec_nr db $sql}] |
| 319 | set tm [lindex $speed 0] |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 320 | if {$tm == 0} { |
| 321 | set rate [format %20s "many"] |
| 322 | } else { |
| 323 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 324 | } |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 325 | set u2 $units/s |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 326 | puts [format {%12d uS %s %s} $tm $rate $u2] |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 327 | global total_time |
| 328 | set total_time [expr {$total_time+$tm}] |
| 329 | } |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 330 | proc speed_trial_tcl {name numstmt units script} { |
| 331 | puts -nonewline [format {%-21.21s } $name...] |
| 332 | flush stdout |
| 333 | set speed [time {eval $script}] |
| 334 | set tm [lindex $speed 0] |
| 335 | if {$tm == 0} { |
| 336 | set rate [format %20s "many"] |
| 337 | } else { |
| 338 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 339 | } |
| 340 | set u2 $units/s |
| 341 | puts [format {%12d uS %s %s} $tm $rate $u2] |
| 342 | global total_time |
| 343 | set total_time [expr {$total_time+$tm}] |
| 344 | } |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 345 | proc speed_trial_init {name} { |
| 346 | global total_time |
| 347 | set total_time 0 |
| 348 | } |
| 349 | proc speed_trial_summary {name} { |
| 350 | global total_time |
| 351 | puts [format {%-21.21s %12d uS TOTAL} $name $total_time] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 352 | } |
| 353 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 354 | # Run this routine last |
| 355 | # |
| 356 | proc finish_test {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 357 | catch {db close} |
| 358 | catch {db2 close} |
| 359 | catch {db3 close} |
| 360 | if {0==[info exists ::SLAVE]} { finalize_testing } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 361 | } |
| 362 | proc finalize_testing {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 363 | global sqlite_open_file_count |
| 364 | |
| 365 | set omitList [set_test_counter omit_list] |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 366 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 367 | catch {db close} |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 368 | catch {db2 close} |
| 369 | catch {db3 close} |
| 370 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 371 | vfs_unlink_test |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 372 | sqlite3 db {} |
danielk1977 | cbb8496 | 2006-01-17 16:10:13 +0000 | [diff] [blame] | 373 | # sqlite3_clear_tsd_memdebug |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 374 | db close |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 375 | sqlite3_reset_auto_extension |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 376 | |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 377 | sqlite3_soft_heap_limit 0 |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 378 | set nTest [incr_ntest] |
| 379 | set nErr [set_test_counter errors] |
| 380 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 381 | puts "$nErr errors out of $nTest tests" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 382 | if {$nErr>0} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 383 | puts "Failures on these tests: [set_test_counter fail_list]" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 384 | } |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 385 | run_thread_tests 1 |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 386 | if {[llength $omitList]>0} { |
| 387 | puts "Omitted test cases:" |
| 388 | set prec {} |
| 389 | foreach {rec} [lsort $omitList] { |
| 390 | if {$rec==$prec} continue |
| 391 | set prec $rec |
| 392 | puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]] |
| 393 | } |
| 394 | } |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 395 | if {$nErr>0 && ![working_64bit_int]} { |
| 396 | puts "******************************************************************" |
| 397 | puts "N.B.: The version of TCL that you used to build this test harness" |
| 398 | puts "is defective in that it does not support 64-bit integers. Some or" |
| 399 | puts "all of the test failures above might be a result from this defect" |
| 400 | puts "in your TCL build." |
| 401 | puts "******************************************************************" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 402 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 403 | if {$::cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 404 | vfslog finalize binarylog |
danielk1977 | 374177e | 2008-05-08 15:58:06 +0000 | [diff] [blame] | 405 | } |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 406 | if {$sqlite_open_file_count} { |
| 407 | puts "$sqlite_open_file_count files were left open" |
| 408 | incr nErr |
| 409 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 410 | if {[sqlite3_memory_used]>0} { |
| 411 | puts "Unfreed memory: [sqlite3_memory_used] bytes" |
| 412 | incr nErr |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 413 | ifcapable memdebug||mem5||(mem3&&debug) { |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 414 | puts "Writing unfreed memory log to \"./memleak.txt\"" |
| 415 | sqlite3_memdebug_dump ./memleak.txt |
| 416 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 417 | } else { |
| 418 | puts "All memory allocations freed - no leaks" |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 419 | ifcapable memdebug||mem5 { |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 420 | sqlite3_memdebug_dump ./memusage.txt |
| 421 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 422 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 423 | show_memstats |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 424 | puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes" |
| 425 | puts "Current memory usage: [sqlite3_memory_highwater] bytes" |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 426 | if {[info commands sqlite3_memdebug_malloc_count] ne ""} { |
| 427 | puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls" |
| 428 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 429 | if {$::cmdlinearg(malloctrace)} { |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 430 | puts "Writing mallocs.sql..." |
| 431 | memdebug_log_sql |
| 432 | sqlite3_memdebug_log stop |
| 433 | sqlite3_memdebug_log clear |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 434 | |
| 435 | if {[sqlite3_memory_used]>0} { |
| 436 | puts "Writing leaks.sql..." |
| 437 | sqlite3_memdebug_log sync |
| 438 | memdebug_log_sql leaks.sql |
| 439 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 440 | } |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 441 | foreach f [glob -nocomplain test.db-*-journal] { |
| 442 | file delete -force $f |
| 443 | } |
| 444 | foreach f [glob -nocomplain test.db-mj*] { |
| 445 | file delete -force $f |
| 446 | } |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 447 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 448 | } |
| 449 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 450 | # Display memory statistics for analysis and debugging purposes. |
| 451 | # |
| 452 | proc show_memstats {} { |
| 453 | set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 454 | set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] |
| 455 | set val [format {now %10d max %10d max-size %10d} \ |
| 456 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 457 | puts "Memory used: $val" |
| 458 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 459 | set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0] |
| 460 | set val [format {now %10d max %10d max-size %10d} \ |
| 461 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 462 | puts "Page-cache used: $val" |
| 463 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] |
| 464 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 465 | puts "Page-cache overflow: $val" |
| 466 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] |
| 467 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 468 | puts "Scratch memory used: $val" |
| 469 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 470 | set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0] |
| 471 | set val [format {now %10d max %10d max-size %10d} \ |
| 472 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 473 | puts "Scratch overflow: $val" |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 474 | ifcapable yytrackmaxstackdepth { |
| 475 | set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0] |
| 476 | set val [format { max %10d} [lindex $x 2]] |
| 477 | puts "Parser stack depth: $val" |
| 478 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 479 | } |
| 480 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 481 | # A procedure to execute SQL |
| 482 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 483 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 484 | # puts "SQL = $sql" |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 485 | uplevel [list $db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 486 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 487 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 488 | # Execute SQL and catch exceptions. |
| 489 | # |
| 490 | proc catchsql {sql {db db}} { |
| 491 | # puts "SQL = $sql" |
dan | 81fa6dc | 2009-11-28 12:40:32 +0000 | [diff] [blame] | 492 | set r [catch [list uplevel [list $db eval $sql]] msg] |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 493 | lappend r $msg |
| 494 | return $r |
| 495 | } |
| 496 | |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 497 | # Do an VDBE code dump on the SQL given |
| 498 | # |
| 499 | proc explain {sql {db db}} { |
| 500 | puts "" |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 501 | puts "addr opcode p1 p2 p3 p4 p5 #" |
| 502 | puts "---- ------------ ------ ------ ------ --------------- -- -" |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 503 | $db eval "explain $sql" {} { |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 504 | puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \ |
| 505 | $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment |
| 506 | ] |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 510 | # Show the VDBE program for an SQL statement but omit the Trace |
| 511 | # opcode at the beginning. This procedure can be used to prove |
| 512 | # that different SQL statements generate exactly the same VDBE code. |
| 513 | # |
| 514 | proc explain_no_trace {sql} { |
| 515 | set tr [db eval "EXPLAIN $sql"] |
| 516 | return [lrange $tr 7 end] |
| 517 | } |
| 518 | |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 519 | # Another procedure to execute SQL. This one includes the field |
| 520 | # names in the returned list. |
| 521 | # |
| 522 | proc execsql2 {sql} { |
| 523 | set result {} |
| 524 | db eval $sql data { |
| 525 | foreach f $data(*) { |
| 526 | lappend result $f $data($f) |
| 527 | } |
| 528 | } |
| 529 | return $result |
| 530 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 531 | |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 532 | # Use the non-callback API to execute multiple SQL statements |
| 533 | # |
| 534 | proc stepsql {dbptr sql} { |
| 535 | set sql [string trim $sql] |
| 536 | set r 0 |
| 537 | while {[string length $sql]>0} { |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 538 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 539 | return [list 1 $vm] |
| 540 | } |
| 541 | set sql [string trim $sqltail] |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 542 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 543 | # foreach v $VAL {lappend r $v} |
| 544 | # } |
| 545 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 546 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 547 | lappend r [sqlite3_column_text $vm $i] |
| 548 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 549 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 550 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 551 | return [list 1 $errmsg] |
| 552 | } |
| 553 | } |
| 554 | return $r |
| 555 | } |
| 556 | |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 557 | # Delete a file or directory |
| 558 | # |
| 559 | proc forcedelete {filename} { |
| 560 | if {[catch {file delete -force $filename}]} { |
| 561 | exec rm -rf $filename |
| 562 | } |
| 563 | } |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 564 | |
| 565 | # Do an integrity check of the entire database |
| 566 | # |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 567 | proc integrity_check {name {db db}} { |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 568 | ifcapable integrityck { |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 569 | do_test $name [list execsql {PRAGMA integrity_check} $db] {ok} |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 573 | proc fix_ifcapable_expr {expr} { |
| 574 | set ret "" |
| 575 | set state 0 |
| 576 | for {set i 0} {$i < [string length $expr]} {incr i} { |
| 577 | set char [string range $expr $i $i] |
| 578 | set newstate [expr {[string is alnum $char] || $char eq "_"}] |
| 579 | if {$newstate && !$state} { |
| 580 | append ret {$::sqlite_options(} |
| 581 | } |
| 582 | if {!$newstate && $state} { |
| 583 | append ret ) |
| 584 | } |
| 585 | append ret $char |
| 586 | set state $newstate |
| 587 | } |
| 588 | if {$state} {append ret )} |
| 589 | return $ret |
| 590 | } |
| 591 | |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 592 | # Evaluate a boolean expression of capabilities. If true, execute the |
| 593 | # code. Omit the code if false. |
| 594 | # |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 595 | proc ifcapable {expr code {else ""} {elsecode ""}} { |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 596 | #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 |
| 597 | set e2 [fix_ifcapable_expr $expr] |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 598 | if ($e2) { |
| 599 | set c [catch {uplevel 1 $code} r] |
| 600 | } else { |
| 601 | set c [catch {uplevel 1 $elsecode} r] |
| 602 | } |
| 603 | return -code $c $r |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 604 | } |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 605 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 606 | # This proc execs a seperate process that crashes midway through executing |
| 607 | # the SQL script $sql on database test.db. |
| 608 | # |
| 609 | # The crash occurs during a sync() of file $crashfile. When the crash |
| 610 | # occurs a random subset of all unsynced writes made by the process are |
| 611 | # written into the files on disk. Argument $crashdelay indicates the |
| 612 | # number of file syncs to wait before crashing. |
| 613 | # |
| 614 | # The return value is a list of two elements. The first element is a |
| 615 | # boolean, indicating whether or not the process actually crashed or |
| 616 | # reported some other error. The second element in the returned list is the |
| 617 | # error message. This is "child process exited abnormally" if the crash |
| 618 | # occured. |
| 619 | # |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 620 | # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 621 | # |
| 622 | proc crashsql {args} { |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 623 | if {$::tcl_platform(platform)!="unix"} { |
| 624 | error "crashsql should only be used on unix" |
| 625 | } |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 626 | |
| 627 | set blocksize "" |
| 628 | set crashdelay 1 |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 629 | set prngseed 0 |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 630 | set tclbody {} |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 631 | set crashfile "" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 632 | set dc "" |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 633 | set sql [lindex $args end] |
| 634 | |
| 635 | for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { |
| 636 | set z [lindex $args $ii] |
| 637 | set n [string length $z] |
| 638 | set z2 [lindex $args [expr $ii+1]] |
| 639 | |
| 640 | if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \ |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 641 | elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 642 | elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 643 | elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 644 | elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 645 | elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 646 | else { error "Unrecognized option: $z" } |
| 647 | } |
| 648 | |
| 649 | if {$crashfile eq ""} { |
| 650 | error "Compulsory option -file missing" |
| 651 | } |
| 652 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 653 | set cfile [file join [pwd] $crashfile] |
| 654 | |
| 655 | set f [open crash.tcl w] |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 656 | puts $f "sqlite3_crash_enable 1" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 657 | puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 658 | puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 659 | puts $f "sqlite3 db test.db -vfs crash" |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 660 | |
| 661 | # This block sets the cache size of the main database to 10 |
| 662 | # pages. This is done in case the build is configured to omit |
| 663 | # "PRAGMA cache_size". |
| 664 | puts $f {db eval {SELECT * FROM sqlite_master;}} |
| 665 | puts $f {set bt [btree_from_db db]} |
| 666 | puts $f {btree_set_cache_size $bt 10} |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 667 | if {$prngseed} { |
| 668 | set seed [expr {$prngseed%10007+1}] |
| 669 | # puts seed=$seed |
| 670 | puts $f "db eval {SELECT randomblob($seed)}" |
| 671 | } |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 672 | |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 673 | if {[string length $tclbody]>0} { |
| 674 | puts $f $tclbody |
| 675 | } |
| 676 | if {[string length $sql]>0} { |
| 677 | puts $f "db eval {" |
| 678 | puts $f "$sql" |
| 679 | puts $f "}" |
| 680 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 681 | close $f |
| 682 | |
| 683 | set r [catch { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 684 | exec [info nameofexec] crash.tcl >@stdout |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 685 | } msg] |
| 686 | lappend r $msg |
| 687 | } |
| 688 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 689 | # Usage: do_ioerr_test <test number> <options...> |
| 690 | # |
| 691 | # This proc is used to implement test cases that check that IO errors |
| 692 | # are correctly handled. The first argument, <test number>, is an integer |
| 693 | # used to name the tests executed by this proc. Options are as follows: |
| 694 | # |
| 695 | # -tclprep TCL script to run to prepare test. |
| 696 | # -sqlprep SQL script to run to prepare test. |
| 697 | # -tclbody TCL script to run with IO error simulation. |
| 698 | # -sqlbody TCL script to run with IO error simulation. |
| 699 | # -exclude List of 'N' values not to test. |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 700 | # -erc Use extended result codes |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 701 | # -persist Make simulated I/O errors persistent |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 702 | # -start Value of 'N' to begin with (default 1) |
| 703 | # |
| 704 | # -cksum Boolean. If true, test that the database does |
| 705 | # not change during the execution of the test case. |
| 706 | # |
| 707 | proc do_ioerr_test {testname args} { |
| 708 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 709 | set ::ioerropts(-start) 1 |
| 710 | set ::ioerropts(-cksum) 0 |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 711 | set ::ioerropts(-erc) 0 |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 712 | set ::ioerropts(-count) 100000000 |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 713 | set ::ioerropts(-persist) 1 |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 714 | set ::ioerropts(-ckrefcount) 0 |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 715 | set ::ioerropts(-restoreprng) 1 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 716 | array set ::ioerropts $args |
| 717 | |
danielk1977 | 47cd39c | 2008-05-12 12:41:15 +0000 | [diff] [blame] | 718 | # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are |
| 719 | # a couple of obscure IO errors that do not return them. |
| 720 | set ::ioerropts(-erc) 0 |
| 721 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 722 | set ::go 1 |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 723 | #reset_prng_state |
| 724 | save_prng_state |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 725 | for {set n $::ioerropts(-start)} {$::go} {incr n} { |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 726 | set ::TN $n |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 727 | incr ::ioerropts(-count) -1 |
| 728 | if {$::ioerropts(-count)<0} break |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 729 | |
| 730 | # Skip this IO error if it was specified with the "-exclude" option. |
| 731 | if {[info exists ::ioerropts(-exclude)]} { |
| 732 | if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue |
| 733 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 734 | if {$::ioerropts(-restoreprng)} { |
| 735 | restore_prng_state |
| 736 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 737 | |
| 738 | # Delete the files test.db and test2.db, then execute the TCL and |
| 739 | # SQL (in that order) to prepare for the test case. |
| 740 | do_test $testname.$n.1 { |
| 741 | set ::sqlite_io_error_pending 0 |
| 742 | catch {db close} |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 743 | catch {db2 close} |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 744 | catch {file delete -force test.db} |
| 745 | catch {file delete -force test.db-journal} |
| 746 | catch {file delete -force test2.db} |
| 747 | catch {file delete -force test2.db-journal} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 748 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 749 | sqlite3_extended_result_codes $::DB $::ioerropts(-erc) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 750 | if {[info exists ::ioerropts(-tclprep)]} { |
| 751 | eval $::ioerropts(-tclprep) |
| 752 | } |
| 753 | if {[info exists ::ioerropts(-sqlprep)]} { |
| 754 | execsql $::ioerropts(-sqlprep) |
| 755 | } |
| 756 | expr 0 |
| 757 | } {0} |
| 758 | |
| 759 | # Read the 'checksum' of the database. |
| 760 | if {$::ioerropts(-cksum)} { |
| 761 | set checksum [cksum] |
| 762 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 763 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 764 | # Set the Nth IO error to fail. |
| 765 | do_test $testname.$n.2 [subst { |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 766 | set ::sqlite_io_error_persist $::ioerropts(-persist) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 767 | set ::sqlite_io_error_pending $n |
| 768 | }] $n |
| 769 | |
| 770 | # Create a single TCL script from the TCL and SQL specified |
| 771 | # as the body of the test. |
| 772 | set ::ioerrorbody {} |
| 773 | if {[info exists ::ioerropts(-tclbody)]} { |
| 774 | append ::ioerrorbody "$::ioerropts(-tclbody)\n" |
| 775 | } |
| 776 | if {[info exists ::ioerropts(-sqlbody)]} { |
| 777 | append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" |
| 778 | } |
| 779 | |
| 780 | # Execute the TCL Script created in the above block. If |
| 781 | # there are at least N IO operations performed by SQLite as |
| 782 | # a result of the script, the Nth will fail. |
| 783 | do_test $testname.$n.3 { |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 784 | set ::sqlite_io_error_hit 0 |
| 785 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 786 | set r [catch $::ioerrorbody msg] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 787 | set ::errseen $r |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 788 | set rc [sqlite3_errcode $::DB] |
| 789 | if {$::ioerropts(-erc)} { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 790 | # If we are in extended result code mode, make sure all of the |
| 791 | # IOERRs we get back really do have their extended code values. |
| 792 | # If an extended result code is returned, the sqlite3_errcode |
| 793 | # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn |
| 794 | # where nnnn is a number |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 795 | if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { |
| 796 | return $rc |
| 797 | } |
| 798 | } else { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 799 | # If we are not in extended result code mode, make sure no |
| 800 | # extended error codes are returned. |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 801 | if {[regexp {\+\d} $rc]} { |
| 802 | return $rc |
| 803 | } |
| 804 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 805 | # The test repeats as long as $::go is non-zero. $::go starts out |
| 806 | # as 1. When a test runs to completion without hitting an I/O |
| 807 | # error, that means there is no point in continuing with this test |
| 808 | # case so set $::go to zero. |
| 809 | # |
| 810 | if {$::sqlite_io_error_pending>0} { |
| 811 | set ::go 0 |
| 812 | set q 0 |
| 813 | set ::sqlite_io_error_pending 0 |
| 814 | } else { |
| 815 | set q 1 |
| 816 | } |
| 817 | |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 818 | set s [expr $::sqlite_io_error_hit==0] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 819 | if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} { |
| 820 | set r 1 |
| 821 | } |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 822 | set ::sqlite_io_error_hit 0 |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 823 | |
| 824 | # One of two things must have happened. either |
| 825 | # 1. We never hit the IO error and the SQL returned OK |
| 826 | # 2. An IO error was hit and the SQL failed |
| 827 | # |
dan | d41a29a | 2010-05-06 15:56:28 +0000 | [diff] [blame] | 828 | #puts "s=$s r=$r q=$q" |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 829 | expr { ($s && !$r && !$q) || (!$s && $r && $q) } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 830 | } {1} |
| 831 | |
danielk1977 | 80daec6 | 2008-05-12 10:57:02 +0000 | [diff] [blame] | 832 | set ::sqlite_io_error_hit 0 |
| 833 | set ::sqlite_io_error_pending 0 |
| 834 | |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 835 | # Check that no page references were leaked. There should be |
| 836 | # a single reference if there is still an active transaction, |
| 837 | # or zero otherwise. |
| 838 | # |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 839 | # UPDATE: If the IO error occurs after a 'BEGIN' but before any |
| 840 | # locks are established on database files (i.e. if the error |
| 841 | # occurs while attempting to detect a hot-journal file), then |
| 842 | # there may 0 page references and an active transaction according |
| 843 | # to [sqlite3_get_autocommit]. |
| 844 | # |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 845 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} { |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 846 | do_test $testname.$n.4 { |
| 847 | set bt [btree_from_db db] |
| 848 | db_enter db |
| 849 | array set stats [btree_pager_stats $bt] |
| 850 | db_leave db |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 851 | set nRef $stats(ref) |
| 852 | expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)} |
| 853 | } {1} |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 854 | } |
| 855 | |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 856 | # If there is an open database handle and no open transaction, |
| 857 | # and the pager is not running in exclusive-locking mode, |
| 858 | # check that the pager is in "unlocked" state. Theoretically, |
| 859 | # if a call to xUnlock() failed due to an IO error the underlying |
| 860 | # file may still be locked. |
| 861 | # |
| 862 | ifcapable pragma { |
| 863 | if { [info commands db] ne "" |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 864 | && $::ioerropts(-ckrefcount) |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 865 | && [db one {pragma locking_mode}] eq "normal" |
| 866 | && [sqlite3_get_autocommit db] |
| 867 | } { |
| 868 | do_test $testname.$n.5 { |
| 869 | set bt [btree_from_db db] |
| 870 | db_enter db |
| 871 | array set stats [btree_pager_stats $bt] |
| 872 | db_leave db |
| 873 | set stats(state) |
| 874 | } 0 |
| 875 | } |
| 876 | } |
| 877 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 878 | # If an IO error occured, then the checksum of the database should |
| 879 | # be the same as before the script that caused the IO error was run. |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 880 | # |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 881 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} { |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 882 | do_test $testname.$n.6 { |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 883 | catch {db close} |
danielk1977 | a961339 | 2008-07-08 12:07:32 +0000 | [diff] [blame] | 884 | catch {db2 close} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 885 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 886 | cksum |
| 887 | } $checksum |
| 888 | } |
| 889 | |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 890 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 891 | set ::sqlite_io_error_pending 0 |
danielk1977 | 105afed | 2005-05-26 15:20:53 +0000 | [diff] [blame] | 892 | if {[info exists ::ioerropts(-cleanup)]} { |
| 893 | catch $::ioerropts(-cleanup) |
| 894 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 895 | } |
| 896 | set ::sqlite_io_error_pending 0 |
drh | 6d54da0 | 2007-03-25 19:08:46 +0000 | [diff] [blame] | 897 | set ::sqlite_io_error_persist 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 898 | unset ::ioerropts |
| 899 | } |
| 900 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 901 | # Return a checksum based on the contents of the main database associated |
| 902 | # with connection $db |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 903 | # |
| 904 | proc cksum {{db db}} { |
| 905 | set txt [$db eval { |
| 906 | SELECT name, type, sql FROM sqlite_master order by name |
| 907 | }]\n |
| 908 | foreach tbl [$db eval { |
| 909 | SELECT name FROM sqlite_master WHERE type='table' order by name |
| 910 | }] { |
| 911 | append txt [$db eval "SELECT * FROM $tbl"]\n |
| 912 | } |
| 913 | foreach prag {default_synchronous default_cache_size} { |
| 914 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 915 | } |
| 916 | set cksum [string length $txt]-[md5 $txt] |
| 917 | # puts $cksum-[file size test.db] |
| 918 | return $cksum |
| 919 | } |
| 920 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 921 | # Generate a checksum based on the contents of the main and temp tables |
| 922 | # database $db. If the checksum of two databases is the same, and the |
| 923 | # integrity-check passes for both, the two databases are identical. |
| 924 | # |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 925 | proc allcksum {{db db}} { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 926 | set ret [list] |
| 927 | ifcapable tempdb { |
| 928 | set sql { |
| 929 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 930 | SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION |
| 931 | SELECT 'sqlite_master' UNION |
| 932 | SELECT 'sqlite_temp_master' ORDER BY 1 |
| 933 | } |
| 934 | } else { |
| 935 | set sql { |
| 936 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 937 | SELECT 'sqlite_master' ORDER BY 1 |
| 938 | } |
| 939 | } |
| 940 | set tbllist [$db eval $sql] |
| 941 | set txt {} |
| 942 | foreach tbl $tbllist { |
| 943 | append txt [$db eval "SELECT * FROM $tbl"] |
| 944 | } |
| 945 | foreach prag {default_cache_size} { |
| 946 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 947 | } |
| 948 | # puts txt=$txt |
| 949 | return [md5 $txt] |
| 950 | } |
| 951 | |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 952 | # Generate a checksum based on the contents of a single database with |
| 953 | # a database connection. The name of the database is $dbname. |
| 954 | # Examples of $dbname are "temp" or "main". |
| 955 | # |
| 956 | proc dbcksum {db dbname} { |
| 957 | if {$dbname=="temp"} { |
| 958 | set master sqlite_temp_master |
| 959 | } else { |
| 960 | set master $dbname.sqlite_master |
| 961 | } |
| 962 | set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] |
| 963 | set txt [$db eval "SELECT * FROM $master"]\n |
| 964 | foreach tab $alltab { |
| 965 | append txt [$db eval "SELECT * FROM $dbname.$tab"]\n |
| 966 | } |
| 967 | return [md5 $txt] |
| 968 | } |
| 969 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 970 | proc memdebug_log_sql {{filename mallocs.sql}} { |
| 971 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 972 | set data [sqlite3_memdebug_log dump] |
| 973 | set nFrame [expr [llength [lindex $data 0]]-2] |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 974 | if {$nFrame < 0} { return "" } |
| 975 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 976 | set database temp |
| 977 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 978 | set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 979 | |
| 980 | set sql "" |
| 981 | foreach e $data { |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 982 | set nCall [lindex $e 0] |
| 983 | set nByte [lindex $e 1] |
| 984 | set lStack [lrange $e 2 end] |
| 985 | append sql "INSERT INTO ${database}.malloc VALUES" |
| 986 | append sql "('test', $nCall, $nByte, '$lStack');\n" |
| 987 | foreach f $lStack { |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 988 | set frames($f) 1 |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 993 | set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 994 | |
| 995 | foreach f [array names frames] { |
| 996 | set addr [format %x $f] |
| 997 | set cmd "addr2line -e [info nameofexec] $addr" |
| 998 | set line [eval exec $cmd] |
| 999 | append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1000 | |
| 1001 | set file [lindex [split $line :] 0] |
| 1002 | set files($file) 1 |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1005 | foreach f [array names files] { |
| 1006 | set contents "" |
| 1007 | catch { |
| 1008 | set fd [open $f] |
| 1009 | set contents [read $fd] |
| 1010 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1011 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1012 | set contents [string map {' ''} $contents] |
| 1013 | append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1014 | } |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1015 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1016 | set fd [open $filename w] |
| 1017 | puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" |
| 1018 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1019 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1020 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1021 | # Copy file $from into $to. This is used because some versions of |
| 1022 | # TCL for windows (notably the 8.4.1 binary package shipped with the |
| 1023 | # current mingw release) have a broken "file copy" command. |
| 1024 | # |
| 1025 | proc copy_file {from to} { |
| 1026 | if {$::tcl_platform(platform)=="unix"} { |
| 1027 | file copy -force $from $to |
| 1028 | } else { |
| 1029 | set f [open $from] |
| 1030 | fconfigure $f -translation binary |
| 1031 | set t [open $to w] |
| 1032 | fconfigure $t -translation binary |
| 1033 | puts -nonewline $t [read $f [file size $from]] |
| 1034 | close $t |
| 1035 | close $f |
| 1036 | } |
| 1037 | } |
| 1038 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1039 | # Drop all tables in database [db] |
| 1040 | proc drop_all_tables {{db db}} { |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1041 | ifcapable trigger&&foreignkey { |
| 1042 | set pk [$db one "PRAGMA foreign_keys"] |
| 1043 | $db eval "PRAGMA foreign_keys = OFF" |
| 1044 | } |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1045 | foreach {idx name file} [db eval {PRAGMA database_list}] { |
| 1046 | if {$idx==1} { |
| 1047 | set master sqlite_temp_master |
| 1048 | } else { |
| 1049 | set master $name.sqlite_master |
| 1050 | } |
| 1051 | foreach {t type} [$db eval " |
| 1052 | SELECT name, type FROM $master |
| 1053 | WHERE type IN('table', 'view') AND name NOT like 'sqlite_%' |
| 1054 | "] { |
| 1055 | $db eval "DROP $type $t" |
| 1056 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1057 | } |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1058 | ifcapable trigger&&foreignkey { |
| 1059 | $db eval "PRAGMA foreign_keys = $pk" |
| 1060 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1063 | #------------------------------------------------------------------------- |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 1064 | # If a test script is executed with global variable $::G(perm:name) set to |
| 1065 | # "wal", then the tests are run in WAL mode. Otherwise, they should be run |
| 1066 | # in rollback mode. The following Tcl procs are used to make this less |
| 1067 | # intrusive: |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1068 | # |
| 1069 | # wal_set_journal_mode ?DB? |
| 1070 | # |
| 1071 | # If running a WAL test, execute "PRAGMA journal_mode = wal" using |
| 1072 | # connection handle DB. Otherwise, this command is a no-op. |
| 1073 | # |
| 1074 | # wal_check_journal_mode TESTNAME ?DB? |
| 1075 | # |
| 1076 | # If running a WAL test, execute a tests case that fails if the main |
| 1077 | # database for connection handle DB is not currently a WAL database. |
| 1078 | # Otherwise (if not running a WAL permutation) this is a no-op. |
| 1079 | # |
| 1080 | # wal_is_wal_mode |
| 1081 | # |
| 1082 | # Returns true if this test should be run in WAL mode. False otherwise. |
| 1083 | # |
| 1084 | proc wal_is_wal_mode {} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 1085 | expr {[permutation] eq "wal"} |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1086 | } |
| 1087 | proc wal_set_journal_mode {{db db}} { |
| 1088 | if { [wal_is_wal_mode] } { |
| 1089 | $db eval "PRAGMA journal_mode = WAL" |
| 1090 | } |
| 1091 | } |
| 1092 | proc wal_check_journal_mode {testname {db db}} { |
| 1093 | if { [wal_is_wal_mode] } { |
| 1094 | $db eval { SELECT * FROM sqlite_master } |
| 1095 | do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal} |
| 1096 | } |
| 1097 | } |
| 1098 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 1099 | proc permutation {} { |
| 1100 | set perm "" |
| 1101 | catch {set perm $::G(perm:name)} |
| 1102 | set perm |
| 1103 | } |
| 1104 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1105 | #------------------------------------------------------------------------- |
| 1106 | # |
| 1107 | proc slave_test_script {script} { |
| 1108 | |
| 1109 | # Create the interpreter used to run the test script. |
| 1110 | interp create tinterp |
| 1111 | |
| 1112 | # Populate some global variables that tester.tcl expects to see. |
| 1113 | foreach {var value} [list \ |
| 1114 | ::argv0 $::argv0 \ |
| 1115 | ::argv {} \ |
| 1116 | ::SLAVE 1 \ |
| 1117 | ] { |
| 1118 | interp eval tinterp [list set $var $value] |
| 1119 | } |
| 1120 | |
| 1121 | # The alias used to access the global test counters. |
| 1122 | tinterp alias set_test_counter set_test_counter |
| 1123 | |
| 1124 | # Set up the ::cmdlinearg array in the slave. |
| 1125 | interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]] |
| 1126 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 1127 | # Set up the ::G array in the slave. |
| 1128 | interp eval tinterp [list array set ::G [array get ::G]] |
| 1129 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1130 | # Load the various test interfaces implemented in C. |
| 1131 | load_testfixture_extensions tinterp |
| 1132 | |
| 1133 | # Run the test script. |
| 1134 | interp eval tinterp $script |
| 1135 | |
| 1136 | # Delete the interpreter used to run the test script. |
| 1137 | interp delete tinterp |
| 1138 | } |
| 1139 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 1140 | proc slave_test_file {zFile} { |
| 1141 | |
| 1142 | set ::sqlite_open_file_count 0 |
| 1143 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1144 | set time [time { |
| 1145 | slave_test_script [list source $zFile] |
| 1146 | }] |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame^] | 1147 | |
| 1148 | set tail [file tail $zFile] |
| 1149 | if {$::sqlite_open_file_count>0} { |
| 1150 | puts "$tail did not close all files: $::sqlite_open_file_count" |
| 1151 | fail_test $tail |
| 1152 | set ::sqlite_open_file_count 0 |
| 1153 | exit |
| 1154 | } |
| 1155 | puts "time $tail [lrange $time 0 1]" |
| 1156 | |
| 1157 | show_memstats |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1160 | |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 1161 | # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set |
| 1162 | # to non-zero, then set the global variable $AUTOVACUUM to 1. |
| 1163 | set AUTOVACUUM $sqlite_options(default_autovacuum) |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 1164 | |
| 1165 | source $testdir/thread_common.tcl |