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