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 | #------------------------------------------------------------------------- |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 17 | # The commands provided by the code in this file to help with creating |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 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 | # |
mistachkin | c548465 | 2012-03-05 22:52:33 +0000 | [diff] [blame] | 22 | # is_relative_file |
mistachkin | 4a41f34 | 2012-03-06 03:00:49 +0000 | [diff] [blame] | 23 | # test_pwd |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 24 | # get_pwd |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 25 | # copy_file FROM TO |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 26 | # delete_file FILENAME |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 27 | # drop_all_tables ?DB? |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 28 | # forcecopy FROM TO |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 29 | # forcedelete FILENAME |
| 30 | # |
| 31 | # Test the capability of the SQLite version built into the interpreter to |
| 32 | # determine if a specific test can be run: |
| 33 | # |
mistachkin | c60941f | 2012-09-13 01:51:02 +0000 | [diff] [blame] | 34 | # capable EXPR |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 35 | # ifcapable EXPR |
| 36 | # |
| 37 | # Calulate checksums based on database contents: |
| 38 | # |
| 39 | # dbcksum DB DBNAME |
| 40 | # allcksum ?DB? |
| 41 | # cksum ?DB? |
| 42 | # |
| 43 | # Commands to execute/explain SQL statements: |
| 44 | # |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 45 | # memdbsql SQL |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 46 | # stepsql DB SQL |
| 47 | # execsql2 SQL |
| 48 | # explain_no_trace SQL |
| 49 | # explain SQL ?DB? |
| 50 | # catchsql SQL ?DB? |
| 51 | # execsql SQL ?DB? |
| 52 | # |
| 53 | # Commands to run test cases: |
| 54 | # |
| 55 | # do_ioerr_test TESTNAME ARGS... |
| 56 | # crashsql ARGS... |
| 57 | # integrity_check TESTNAME ?DB? |
drh | 433dccf | 2013-02-09 15:37:11 +0000 | [diff] [blame] | 58 | # verify_ex_errcode TESTNAME EXPECTED ?DB? |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 59 | # do_test TESTNAME SCRIPT EXPECTED |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 60 | # do_execsql_test TESTNAME SQL EXPECTED |
| 61 | # do_catchsql_test TESTNAME SQL EXPECTED |
drh | 3c2aeae | 2014-01-24 14:37:44 +0000 | [diff] [blame] | 62 | # do_timed_execsql_test TESTNAME SQL EXPECTED |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 63 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 64 | # Commands providing a lower level interface to the global test counters: |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 65 | # |
| 66 | # set_test_counter COUNTER ?VALUE? |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 67 | # omit_test TESTNAME REASON ?APPEND? |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 68 | # fail_test TESTNAME |
| 69 | # incr_ntest |
| 70 | # |
| 71 | # Command run at the end of each test file: |
| 72 | # |
| 73 | # finish_test |
| 74 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 75 | # Commands to help create test files that run with the "WAL" and other |
| 76 | # permutations (see file permutations.test): |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 77 | # |
| 78 | # wal_is_wal_mode |
| 79 | # wal_set_journal_mode ?DB? |
| 80 | # wal_check_journal_mode TESTNAME?DB? |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 81 | # permutation |
dan | cb79e51 | 2010-08-06 13:50:07 +0000 | [diff] [blame] | 82 | # presql |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 83 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 84 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 85 | # Set the precision of FP arithmatic used by the interpreter. And |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 86 | # configure SQLite to take database file locks on the page that begins |
| 87 | # 64KB into the database file instead of the one 1GB in. This means |
| 88 | # the code that handles that special case can be tested without creating |
| 89 | # very large database files. |
| 90 | # |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 91 | set tcl_precision 15 |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 92 | sqlite3_test_control_pending_byte 0x0010000 |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 93 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 94 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 95 | # If the pager codec is available, create a wrapper for the [sqlite3] |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 96 | # command that appends "-key {xyzzy}" to the command line. i.e. this: |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 97 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 98 | # sqlite3 db test.db |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 99 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 100 | # becomes |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 101 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 102 | # sqlite3 db test.db -key {xyzzy} |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 103 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 104 | if {[info command sqlite_orig]==""} { |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 105 | rename sqlite3 sqlite_orig |
| 106 | proc sqlite3 {args} { |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 107 | if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 108 | # This command is opening a new database connection. |
| 109 | # |
| 110 | if {[info exists ::G(perm:sqlite3_args)]} { |
| 111 | set args [concat $args $::G(perm:sqlite3_args)] |
| 112 | } |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 113 | if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 114 | lappend args -key {xyzzy} |
| 115 | } |
| 116 | |
dan | 3ccd20a | 2010-06-09 19:01:02 +0000 | [diff] [blame] | 117 | set res [uplevel 1 sqlite_orig $args] |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 118 | if {[info exists ::G(perm:presql)]} { |
| 119 | [lindex $args 0] eval $::G(perm:presql) |
| 120 | } |
dan | 1ce1b4a | 2010-12-07 14:32:28 +0000 | [diff] [blame] | 121 | if {[info exists ::G(perm:dbconfig)]} { |
| 122 | set ::dbhandle [lindex $args 0] |
| 123 | uplevel #0 $::G(perm:dbconfig) |
| 124 | } |
dan | 3ccd20a | 2010-06-09 19:01:02 +0000 | [diff] [blame] | 125 | set res |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 126 | } else { |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 127 | # This command is not opening a new database connection. Pass the |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 128 | # arguments through to the C implementation as the are. |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 129 | # |
| 130 | uplevel 1 sqlite_orig $args |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 131 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 135 | proc getFileRetries {} { |
| 136 | if {![info exists ::G(file-retries)]} { |
| 137 | # |
| 138 | # NOTE: Return the default number of retries for [file] operations. A |
| 139 | # value of zero or less here means "disabled". |
| 140 | # |
mistachkin | c60941f | 2012-09-13 01:51:02 +0000 | [diff] [blame] | 141 | return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}] |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 142 | } |
| 143 | return $::G(file-retries) |
| 144 | } |
| 145 | |
| 146 | proc getFileRetryDelay {} { |
| 147 | if {![info exists ::G(file-retry-delay)]} { |
| 148 | # |
| 149 | # NOTE: Return the default number of milliseconds to wait when retrying |
| 150 | # failed [file] operations. A value of zero or less means "do not |
| 151 | # wait". |
| 152 | # |
| 153 | return 100; # TODO: Good default? |
| 154 | } |
| 155 | return $::G(file-retry-delay) |
| 156 | } |
| 157 | |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 158 | # Return the string representing the name of the current directory. On |
| 159 | # Windows, the result is "normalized" to whatever our parent command shell |
| 160 | # is using to prevent case-mismatch issues. |
| 161 | # |
| 162 | proc get_pwd {} { |
| 163 | if {$::tcl_platform(platform) eq "windows"} { |
mistachkin | 533b8f6 | 2012-03-08 20:28:31 +0000 | [diff] [blame] | 164 | # |
| 165 | # NOTE: Cannot use [file normalize] here because it would alter the |
| 166 | # case of the result to what Tcl considers canonical, which would |
| 167 | # defeat the purpose of this procedure. |
| 168 | # |
| 169 | return [string map [list \\ /] \ |
| 170 | [string trim [exec -- $::env(ComSpec) /c echo %CD%]]] |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 171 | } else { |
| 172 | return [pwd] |
| 173 | } |
| 174 | } |
| 175 | |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 176 | # Copy file $from into $to. This is used because some versions of |
| 177 | # TCL for windows (notably the 8.4.1 binary package shipped with the |
| 178 | # current mingw release) have a broken "file copy" command. |
| 179 | # |
| 180 | proc copy_file {from to} { |
| 181 | do_copy_file false $from $to |
| 182 | } |
| 183 | |
| 184 | proc forcecopy {from to} { |
| 185 | do_copy_file true $from $to |
| 186 | } |
| 187 | |
| 188 | proc do_copy_file {force from to} { |
| 189 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 190 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 191 | |
| 192 | # On windows, sometimes even a [file copy -force] can fail. The cause is |
| 193 | # usually "tag-alongs" - programs like anti-virus software, automatic backup |
| 194 | # tools and various explorer extensions that keep a file open a little longer |
| 195 | # than we expect, causing the delete to fail. |
| 196 | # |
| 197 | # The solution is to wait a short amount of time before retrying the copy. |
| 198 | # |
| 199 | if {$nRetry > 0} { |
| 200 | for {set i 0} {$i<$nRetry} {incr i} { |
| 201 | set rc [catch { |
| 202 | if {$force} { |
| 203 | file copy -force $from $to |
| 204 | } else { |
| 205 | file copy $from $to |
| 206 | } |
| 207 | } msg] |
| 208 | if {$rc==0} break |
| 209 | if {$nDelay > 0} { after $nDelay } |
| 210 | } |
| 211 | if {$rc} { error $msg } |
| 212 | } else { |
| 213 | if {$force} { |
| 214 | file copy -force $from $to |
| 215 | } else { |
| 216 | file copy $from $to |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
mistachkin | c548465 | 2012-03-05 22:52:33 +0000 | [diff] [blame] | 221 | # Check if a file name is relative |
| 222 | # |
| 223 | proc is_relative_file { file } { |
| 224 | return [expr {[file pathtype $file] != "absolute"}] |
| 225 | } |
| 226 | |
mistachkin | 4a41f34 | 2012-03-06 03:00:49 +0000 | [diff] [blame] | 227 | # If the VFS supports using the current directory, returns [pwd]; |
| 228 | # otherwise, it returns only the provided suffix string (which is |
| 229 | # empty by default). |
| 230 | # |
| 231 | proc test_pwd { args } { |
| 232 | if {[llength $args] > 0} { |
| 233 | set suffix1 [lindex $args 0] |
| 234 | if {[llength $args] > 1} { |
| 235 | set suffix2 [lindex $args 1] |
| 236 | } else { |
| 237 | set suffix2 $suffix1 |
| 238 | } |
| 239 | } else { |
| 240 | set suffix1 ""; set suffix2 "" |
| 241 | } |
| 242 | ifcapable curdir { |
mistachkin | 6aa18c9 | 2012-03-08 20:22:42 +0000 | [diff] [blame] | 243 | return "[get_pwd]$suffix1" |
mistachkin | 4a41f34 | 2012-03-06 03:00:49 +0000 | [diff] [blame] | 244 | } else { |
| 245 | return $suffix2 |
| 246 | } |
| 247 | } |
| 248 | |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 249 | # Delete a file or directory |
| 250 | # |
| 251 | proc delete_file {args} { |
| 252 | do_delete_file false {*}$args |
| 253 | } |
| 254 | |
| 255 | proc forcedelete {args} { |
| 256 | do_delete_file true {*}$args |
| 257 | } |
| 258 | |
| 259 | proc do_delete_file {force args} { |
| 260 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 261 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 262 | |
| 263 | foreach filename $args { |
| 264 | # On windows, sometimes even a [file delete -force] can fail just after |
| 265 | # a file is closed. The cause is usually "tag-alongs" - programs like |
| 266 | # anti-virus software, automatic backup tools and various explorer |
| 267 | # extensions that keep a file open a little longer than we expect, causing |
| 268 | # the delete to fail. |
| 269 | # |
| 270 | # The solution is to wait a short amount of time before retrying the |
| 271 | # delete. |
| 272 | # |
| 273 | if {$nRetry > 0} { |
| 274 | for {set i 0} {$i<$nRetry} {incr i} { |
| 275 | set rc [catch { |
| 276 | if {$force} { |
| 277 | file delete -force $filename |
| 278 | } else { |
| 279 | file delete $filename |
| 280 | } |
| 281 | } msg] |
| 282 | if {$rc==0} break |
| 283 | if {$nDelay > 0} { after $nDelay } |
| 284 | } |
| 285 | if {$rc} { error $msg } |
| 286 | } else { |
| 287 | if {$force} { |
| 288 | file delete -force $filename |
| 289 | } else { |
| 290 | file delete $filename |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 296 | if {$::tcl_platform(platform) eq "windows"} { |
| 297 | proc do_remove_win32_dir {args} { |
| 298 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 299 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 300 | |
| 301 | foreach dirName $args { |
| 302 | # On windows, sometimes even a [remove_win32_dir] can fail just after |
| 303 | # a directory is emptied. The cause is usually "tag-alongs" - programs |
| 304 | # like anti-virus software, automatic backup tools and various explorer |
| 305 | # extensions that keep a file open a little longer than we expect, |
| 306 | # causing the delete to fail. |
| 307 | # |
| 308 | # The solution is to wait a short amount of time before retrying the |
| 309 | # removal. |
| 310 | # |
| 311 | if {$nRetry > 0} { |
| 312 | for {set i 0} {$i < $nRetry} {incr i} { |
| 313 | set rc [catch { |
| 314 | remove_win32_dir $dirName |
| 315 | } msg] |
| 316 | if {$rc == 0} break |
| 317 | if {$nDelay > 0} { after $nDelay } |
| 318 | } |
| 319 | if {$rc} { error $msg } |
| 320 | } else { |
| 321 | remove_win32_dir $dirName |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | proc do_delete_win32_file {args} { |
| 327 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 328 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 329 | |
| 330 | foreach fileName $args { |
| 331 | # On windows, sometimes even a [delete_win32_file] can fail just after |
| 332 | # a file is closed. The cause is usually "tag-alongs" - programs like |
| 333 | # anti-virus software, automatic backup tools and various explorer |
| 334 | # extensions that keep a file open a little longer than we expect, |
| 335 | # causing the delete to fail. |
| 336 | # |
| 337 | # The solution is to wait a short amount of time before retrying the |
| 338 | # delete. |
| 339 | # |
| 340 | if {$nRetry > 0} { |
| 341 | for {set i 0} {$i < $nRetry} {incr i} { |
| 342 | set rc [catch { |
| 343 | delete_win32_file $fileName |
| 344 | } msg] |
| 345 | if {$rc == 0} break |
| 346 | if {$nDelay > 0} { after $nDelay } |
| 347 | } |
| 348 | if {$rc} { error $msg } |
| 349 | } else { |
| 350 | delete_win32_file $fileName |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
dan | cb35460 | 2010-07-08 09:44:42 +0000 | [diff] [blame] | 356 | proc execpresql {handle args} { |
| 357 | trace remove execution $handle enter [list execpresql $handle] |
| 358 | if {[info exists ::G(perm:presql)]} { |
| 359 | $handle eval $::G(perm:presql) |
| 360 | } |
| 361 | } |
| 362 | |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 363 | # This command should be called after loading tester.tcl from within |
| 364 | # all test scripts that are incompatible with encryption codecs. |
| 365 | # |
| 366 | proc do_not_use_codec {} { |
| 367 | set ::do_not_use_codec 1 |
| 368 | reset_db |
| 369 | } |
| 370 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 371 | # The following block only runs the first time this file is sourced. It |
| 372 | # does not run in slave interpreters (since the ::cmdlinearg array is |
| 373 | # populated before the test script is run in slave interpreters). |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 374 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 375 | if {[info exists cmdlinearg]==0} { |
| 376 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 377 | # Parse any options specified in the $argv array. This script accepts the |
| 378 | # following options: |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 379 | # |
| 380 | # --pause |
| 381 | # --soft-heap-limit=NN |
| 382 | # --maxerror=NN |
| 383 | # --malloctrace=N |
| 384 | # --backtrace=N |
| 385 | # --binarylog=N |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 386 | # --soak=N |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 387 | # --file-retries=N |
| 388 | # --file-retry-delay=N |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 389 | # --start=[$permutation:]$testfile |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 390 | # --match=$pattern |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 391 | # |
| 392 | set cmdlinearg(soft-heap-limit) 0 |
| 393 | set cmdlinearg(maxerror) 1000 |
| 394 | set cmdlinearg(malloctrace) 0 |
| 395 | set cmdlinearg(backtrace) 10 |
| 396 | set cmdlinearg(binarylog) 0 |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 397 | set cmdlinearg(soak) 0 |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 398 | set cmdlinearg(file-retries) 0 |
| 399 | set cmdlinearg(file-retry-delay) 0 |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 400 | set cmdlinearg(start) "" |
| 401 | set cmdlinearg(match) "" |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 402 | |
| 403 | set leftover [list] |
| 404 | foreach a $argv { |
| 405 | switch -regexp -- $a { |
| 406 | {^-+pause$} { |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 407 | # Wait for user input before continuing. This is to give the user an |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 408 | # opportunity to connect profiling tools to the process. |
| 409 | puts -nonewline "Press RETURN to begin..." |
| 410 | flush stdout |
| 411 | gets stdin |
| 412 | } |
| 413 | {^-+soft-heap-limit=.+$} { |
| 414 | foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break |
| 415 | } |
| 416 | {^-+maxerror=.+$} { |
| 417 | foreach {dummy cmdlinearg(maxerror)} [split $a =] break |
| 418 | } |
| 419 | {^-+malloctrace=.+$} { |
| 420 | foreach {dummy cmdlinearg(malloctrace)} [split $a =] break |
| 421 | if {$cmdlinearg(malloctrace)} { |
| 422 | sqlite3_memdebug_log start |
| 423 | } |
| 424 | } |
| 425 | {^-+backtrace=.+$} { |
| 426 | foreach {dummy cmdlinearg(backtrace)} [split $a =] break |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 427 | sqlite3_memdebug_backtrace $value |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 428 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 429 | {^-+binarylog=.+$} { |
| 430 | foreach {dummy cmdlinearg(binarylog)} [split $a =] break |
| 431 | } |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 432 | {^-+soak=.+$} { |
| 433 | foreach {dummy cmdlinearg(soak)} [split $a =] break |
| 434 | set ::G(issoak) $cmdlinearg(soak) |
| 435 | } |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 436 | {^-+file-retries=.+$} { |
| 437 | foreach {dummy cmdlinearg(file-retries)} [split $a =] break |
| 438 | set ::G(file-retries) $cmdlinearg(file-retries) |
| 439 | } |
| 440 | {^-+file-retry-delay=.+$} { |
| 441 | foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break |
| 442 | set ::G(file-retry-delay) $cmdlinearg(file-retry-delay) |
| 443 | } |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 444 | {^-+start=.+$} { |
| 445 | foreach {dummy cmdlinearg(start)} [split $a =] break |
| 446 | |
| 447 | set ::G(start:file) $cmdlinearg(start) |
| 448 | if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} { |
| 449 | set ::G(start:permutation) ${s.perm} |
| 450 | set ::G(start:file) ${s.file} |
| 451 | } |
| 452 | if {$::G(start:file) == ""} {unset ::G(start:file)} |
| 453 | } |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 454 | {^-+match=.+$} { |
| 455 | foreach {dummy cmdlinearg(match)} [split $a =] break |
| 456 | |
| 457 | set ::G(match) $cmdlinearg(match) |
| 458 | if {$::G(match) == ""} {unset ::G(match)} |
| 459 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 460 | default { |
| 461 | lappend leftover $a |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | set argv $leftover |
| 466 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 467 | # Install the malloc layer used to inject OOM errors. And the 'automatic' |
| 468 | # extensions. This only needs to be done once for the process. |
| 469 | # |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 470 | sqlite3_shutdown |
| 471 | install_malloc_faultsim 1 |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 472 | sqlite3_initialize |
drh | bb77b75 | 2009-04-09 01:23:49 +0000 | [diff] [blame] | 473 | autoinstall_test_functions |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 474 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 475 | # If the --binarylog option was specified, create the logging VFS. This |
| 476 | # call installs the new VFS as the default for all SQLite connections. |
| 477 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 478 | if {$cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 479 | vfslog new binarylog {} vfslog.bin |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | # Set the backtrace depth, if malloc tracing is enabled. |
| 483 | # |
| 484 | if {$cmdlinearg(malloctrace)} { |
| 485 | sqlite3_memdebug_backtrace $cmdlinearg(backtrace) |
danielk1977 | 185eac9 | 2008-07-12 15:55:54 +0000 | [diff] [blame] | 486 | } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 487 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 488 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 489 | # Update the soft-heap-limit each time this script is run. In that |
| 490 | # way if an individual test file changes the soft-heap-limit, it |
| 491 | # will be reset at the start of the next test file. |
| 492 | # |
| 493 | sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) |
| 494 | |
| 495 | # Create a test database |
| 496 | # |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 497 | proc reset_db {} { |
| 498 | catch {db close} |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 499 | forcedelete test.db |
| 500 | forcedelete test.db-journal |
| 501 | forcedelete test.db-wal |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 502 | sqlite3 db ./test.db |
| 503 | set ::DB [sqlite3_connection_pointer db] |
| 504 | if {[info exists ::SETUP_SQL]} { |
| 505 | db eval $::SETUP_SQL |
| 506 | } |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 507 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 508 | reset_db |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 509 | |
| 510 | # Abort early if this script has been run before. |
| 511 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 512 | if {[info exists TC(count)]} return |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 513 | |
drh | ce2198c | 2010-09-10 13:22:59 +0000 | [diff] [blame] | 514 | # Make sure memory statistics are enabled. |
| 515 | # |
| 516 | sqlite3_config_memstatus 1 |
| 517 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 518 | # Initialize the test counters and set up commands to access them. |
| 519 | # Or, if this is a slave interpreter, set up aliases to write the |
| 520 | # counters in the parent interpreter. |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 521 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 522 | if {0==[info exists ::SLAVE]} { |
| 523 | set TC(errors) 0 |
| 524 | set TC(count) 0 |
| 525 | set TC(fail_list) [list] |
| 526 | set TC(omit_list) [list] |
drh | cca17c3 | 2013-04-19 12:32:52 +0000 | [diff] [blame] | 527 | set TC(warn_list) [list] |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 528 | |
| 529 | proc set_test_counter {counter args} { |
| 530 | if {[llength $args]} { |
| 531 | set ::TC($counter) [lindex $args 0] |
| 532 | } |
| 533 | set ::TC($counter) |
| 534 | } |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 535 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 536 | |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 537 | # Record the fact that a sequence of tests were omitted. |
| 538 | # |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 539 | proc omit_test {name reason {append 1}} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 540 | set omitList [set_test_counter omit_list] |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 541 | if {$append} { |
| 542 | lappend omitList [list $name $reason] |
| 543 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 544 | set_test_counter omit_list $omitList |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 545 | } |
| 546 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 547 | # Record the fact that a test failed. |
| 548 | # |
| 549 | proc fail_test {name} { |
| 550 | set f [set_test_counter fail_list] |
| 551 | lappend f $name |
| 552 | set_test_counter fail_list $f |
| 553 | set_test_counter errors [expr [set_test_counter errors] + 1] |
| 554 | |
| 555 | set nFail [set_test_counter errors] |
| 556 | if {$nFail>=$::cmdlinearg(maxerror)} { |
| 557 | puts "*** Giving up..." |
| 558 | finalize_testing |
| 559 | } |
| 560 | } |
| 561 | |
drh | cca17c3 | 2013-04-19 12:32:52 +0000 | [diff] [blame] | 562 | # Remember a warning message to be displayed at the conclusion of all testing |
| 563 | # |
| 564 | proc warning {msg {append 1}} { |
| 565 | puts "Warning: $msg" |
| 566 | set warnList [set_test_counter warn_list] |
| 567 | if {$append} { |
| 568 | lappend warnList $msg |
| 569 | } |
| 570 | set_test_counter warn_list $warnList |
| 571 | } |
| 572 | |
| 573 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 574 | # Increment the number of tests run |
| 575 | # |
| 576 | proc incr_ntest {} { |
| 577 | set_test_counter count [expr [set_test_counter count] + 1] |
| 578 | } |
| 579 | |
| 580 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 581 | # Invoke the do_test procedure to run a single test |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 582 | # |
| 583 | proc do_test {name cmd expected} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 584 | global argv cmdlinearg |
| 585 | |
dan | 8c40800 | 2010-11-01 17:38:24 +0000 | [diff] [blame] | 586 | fix_testname name |
| 587 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 588 | sqlite3_memdebug_settitle $name |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 589 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 590 | # if {[llength $argv]==0} { |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 591 | # set go 1 |
| 592 | # } else { |
| 593 | # set go 0 |
| 594 | # foreach pattern $argv { |
| 595 | # if {[string match $pattern $name]} { |
| 596 | # set go 1 |
| 597 | # break |
| 598 | # } |
| 599 | # } |
| 600 | # } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 601 | |
dan | d506de0 | 2010-07-03 13:59:01 +0000 | [diff] [blame] | 602 | if {[info exists ::G(perm:prefix)]} { |
| 603 | set name "$::G(perm:prefix)$name" |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 604 | } |
| 605 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 606 | incr_ntest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 607 | puts -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 608 | flush stdout |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 609 | |
| 610 | if {![info exists ::G(match)] || [string match $::G(match) $name]} { |
| 611 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 612 | puts "\nError: $result" |
| 613 | fail_test $name |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 614 | } else { |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 615 | if {[regexp {^~?/.*/$} $expected]} { |
drh | 70bdcc7 | 2013-05-30 19:28:34 +0000 | [diff] [blame] | 616 | # "expected" is of the form "/PATTERN/" then the result if correct if |
| 617 | # regular expression PATTERN matches the result. "~/PATTERN/" means |
| 618 | # the regular expression must not match. |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 619 | if {[string index $expected 0]=="~"} { |
drh | c2b23e7 | 2013-11-13 15:32:15 +0000 | [diff] [blame] | 620 | set re [string range $expected 2 end-1] |
| 621 | if {[string index $re 0]=="*"} { |
| 622 | # If the regular expression begins with * then treat it as a glob instead |
| 623 | set ok [string match $re $result] |
| 624 | } else { |
| 625 | set re [string map {# {[-0-9.]+}} $re] |
| 626 | set ok [regexp $re $result] |
| 627 | } |
| 628 | set ok [expr {!$ok}] |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 629 | } else { |
drh | c2b23e7 | 2013-11-13 15:32:15 +0000 | [diff] [blame] | 630 | set re [string range $expected 1 end-1] |
| 631 | if {[string index $re 0]=="*"} { |
| 632 | # If the regular expression begins with * then treat it as a glob instead |
| 633 | set ok [string match $re $result] |
| 634 | } else { |
| 635 | set re [string map {# {[-0-9.]+}} $re] |
| 636 | set ok [regexp $re $result] |
| 637 | } |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 638 | } |
drh | 70bdcc7 | 2013-05-30 19:28:34 +0000 | [diff] [blame] | 639 | } elseif {[regexp {^~?\*.*\*$} $expected]} { |
| 640 | # "expected" is of the form "*GLOB*" then the result if correct if |
| 641 | # glob pattern GLOB matches the result. "~/GLOB/" means |
| 642 | # the glob must not match. |
| 643 | if {[string index $expected 0]=="~"} { |
| 644 | set e [string range $expected 1 end] |
| 645 | set ok [expr {![string match $e $result]}] |
| 646 | } else { |
| 647 | set ok [string match $expected $result] |
| 648 | } |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 649 | } else { |
| 650 | set ok [expr {[string compare $result $expected]==0}] |
| 651 | } |
| 652 | if {!$ok} { |
mistachkin | c60941f | 2012-09-13 01:51:02 +0000 | [diff] [blame] | 653 | # if {![info exists ::testprefix] || $::testprefix eq ""} { |
| 654 | # error "no test prefix" |
| 655 | # } |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 656 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
| 657 | fail_test $name |
| 658 | } else { |
| 659 | puts " Ok" |
| 660 | } |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 661 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 662 | } else { |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 663 | puts " Omitted" |
| 664 | omit_test $name "pattern mismatch" 0 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 665 | } |
drh | 6558db8 | 2007-04-13 03:23:21 +0000 | [diff] [blame] | 666 | flush stdout |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 667 | } |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 668 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 669 | proc dumpbytes {s} { |
| 670 | set r "" |
| 671 | for {set i 0} {$i < [string length $s]} {incr i} { |
| 672 | if {$i > 0} {append r " "} |
| 673 | append r [format %02X [scan [string index $s $i] %c]] |
| 674 | } |
| 675 | return $r |
| 676 | } |
| 677 | |
drh | 8df9185 | 2012-04-24 12:46:05 +0000 | [diff] [blame] | 678 | proc catchcmd {db {cmd ""}} { |
| 679 | global CLI |
| 680 | set out [open cmds.txt w] |
| 681 | puts $out $cmd |
| 682 | close $out |
| 683 | set line "exec $CLI $db < cmds.txt" |
| 684 | set rc [catch { eval $line } msg] |
| 685 | list $rc $msg |
| 686 | } |
| 687 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 688 | proc catchcmdex {db {cmd ""}} { |
| 689 | global CLI |
| 690 | set out [open cmds.txt w] |
| 691 | fconfigure $out -encoding binary -translation binary |
| 692 | puts -nonewline $out $cmd |
| 693 | close $out |
| 694 | set line "exec -keepnewline -- $CLI $db < cmds.txt" |
| 695 | set chans [list stdin stdout stderr] |
| 696 | foreach chan $chans { |
| 697 | catch { |
| 698 | set modes($chan) [fconfigure $chan] |
| 699 | fconfigure $chan -encoding binary -translation binary -buffering none |
| 700 | } |
| 701 | } |
| 702 | set rc [catch { eval $line } msg] |
| 703 | foreach chan $chans { |
| 704 | catch { |
| 705 | eval fconfigure [list $chan] $modes($chan) |
| 706 | } |
| 707 | } |
| 708 | # puts [dumpbytes $msg] |
| 709 | list $rc $msg |
| 710 | } |
| 711 | |
shaneh | 5550517 | 2011-06-21 19:30:19 +0000 | [diff] [blame] | 712 | proc filepath_normalize {p} { |
| 713 | # test cases should be written to assume "unix"-like file paths |
shaneh | 285a18f | 2011-06-21 19:39:59 +0000 | [diff] [blame] | 714 | if {$::tcl_platform(platform)!="unix"} { |
shaneh | 5550517 | 2011-06-21 19:30:19 +0000 | [diff] [blame] | 715 | # lreverse*2 as a hack to remove any unneeded {} after the string map |
| 716 | lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]] |
shaneh | c489640 | 2011-06-21 19:38:16 +0000 | [diff] [blame] | 717 | } { |
| 718 | set p |
shaneh | 5550517 | 2011-06-21 19:30:19 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | proc do_filepath_test {name cmd expected} { |
| 722 | uplevel [list do_test $name [ |
| 723 | subst -nocommands { filepath_normalize [ $cmd ] } |
| 724 | ] [filepath_normalize $expected]] |
| 725 | } |
| 726 | |
dan | 33f5379 | 2011-05-05 19:44:22 +0000 | [diff] [blame] | 727 | proc realnum_normalize {r} { |
shaneh | 4f529e8 | 2011-06-20 17:41:41 +0000 | [diff] [blame] | 728 | # different TCL versions display floating point values differently. |
| 729 | string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}] |
dan | 33f5379 | 2011-05-05 19:44:22 +0000 | [diff] [blame] | 730 | } |
| 731 | proc do_realnum_test {name cmd expected} { |
| 732 | uplevel [list do_test $name [ |
| 733 | subst -nocommands { realnum_normalize [ $cmd ] } |
| 734 | ] [realnum_normalize $expected]] |
| 735 | } |
| 736 | |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 737 | proc fix_testname {varname} { |
| 738 | upvar $varname testname |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 739 | if {[info exists ::testprefix] |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 740 | && [string is digit [string range $testname 0 0]] |
| 741 | } { |
| 742 | set testname "${::testprefix}-$testname" |
| 743 | } |
| 744 | } |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 745 | |
dan | 57f7f4b | 2010-10-01 19:04:37 +0000 | [diff] [blame] | 746 | proc do_execsql_test {testname sql {result {}}} { |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 747 | fix_testname testname |
dan | 99ebad9 | 2011-06-13 09:11:01 +0000 | [diff] [blame] | 748 | uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]] |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 749 | } |
| 750 | proc do_catchsql_test {testname sql result} { |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 751 | fix_testname testname |
dan | 99ebad9 | 2011-06-13 09:11:01 +0000 | [diff] [blame] | 752 | uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result] |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 753 | } |
drh | 3c2aeae | 2014-01-24 14:37:44 +0000 | [diff] [blame] | 754 | proc do_timed_execsql_test {testname sql {result {}}} { |
| 755 | fix_testname testname |
| 756 | uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\ |
| 757 | [list [list {*}$result]] |
| 758 | } |
dan | 3985479 | 2010-11-15 16:12:58 +0000 | [diff] [blame] | 759 | proc do_eqp_test {name sql res} { |
| 760 | uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res] |
| 761 | } |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 762 | |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 763 | #------------------------------------------------------------------------- |
| 764 | # Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST |
| 765 | # |
| 766 | # Where switches are: |
| 767 | # |
| 768 | # -errorformat FMTSTRING |
| 769 | # -count |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 770 | # -query SQL |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 771 | # -tclquery TCL |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 772 | # -repair TCL |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 773 | # |
| 774 | proc do_select_tests {prefix args} { |
| 775 | |
| 776 | set testlist [lindex $args end] |
| 777 | set switches [lrange $args 0 end-1] |
| 778 | |
| 779 | set errfmt "" |
| 780 | set countonly 0 |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 781 | set tclquery "" |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 782 | set repair "" |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 783 | |
| 784 | for {set i 0} {$i < [llength $switches]} {incr i} { |
| 785 | set s [lindex $switches $i] |
| 786 | set n [string length $s] |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 787 | if {$n>=2 && [string equal -length $n $s "-query"]} { |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 788 | set tclquery [list execsql [lindex $switches [incr i]]] |
| 789 | } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} { |
| 790 | set tclquery [lindex $switches [incr i]] |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 791 | } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} { |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 792 | set errfmt [lindex $switches [incr i]] |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 793 | } elseif {$n>=2 && [string equal -length $n $s "-repair"]} { |
| 794 | set repair [lindex $switches [incr i]] |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 795 | } elseif {$n>=2 && [string equal -length $n $s "-count"]} { |
| 796 | set countonly 1 |
| 797 | } else { |
| 798 | error "unknown switch: $s" |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | if {$countonly && $errfmt!=""} { |
| 803 | error "Cannot use -count and -errorformat together" |
| 804 | } |
| 805 | set nTestlist [llength $testlist] |
| 806 | if {$nTestlist%3 || $nTestlist==0 } { |
| 807 | error "SELECT test list contains [llength $testlist] elements" |
| 808 | } |
| 809 | |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 810 | eval $repair |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 811 | foreach {tn sql res} $testlist { |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 812 | if {$tclquery != ""} { |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 813 | execsql $sql |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 814 | uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]] |
| 815 | } elseif {$countonly} { |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 816 | set nRow 0 |
| 817 | db eval $sql {incr nRow} |
| 818 | uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res] |
| 819 | } elseif {$errfmt==""} { |
| 820 | uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]] |
| 821 | } else { |
| 822 | set res [list 1 [string trim [format $errfmt {*}$res]]] |
| 823 | uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res] |
| 824 | } |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 825 | eval $repair |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 826 | } |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 827 | |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 828 | } |
| 829 | |
dan | b04757a | 2010-09-21 16:59:16 +0000 | [diff] [blame] | 830 | proc delete_all_data {} { |
| 831 | db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} { |
| 832 | db eval "DELETE FROM '[string map {' ''} $t]'" |
| 833 | } |
| 834 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 835 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 836 | # Run an SQL script. |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 837 | # Return the number of microseconds per statement. |
| 838 | # |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 839 | proc speed_trial {name numstmt units sql} { |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 840 | puts -nonewline [format {%-21.21s } $name...] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 841 | flush stdout |
| 842 | set speed [time {sqlite3_exec_nr db $sql}] |
| 843 | set tm [lindex $speed 0] |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 844 | if {$tm == 0} { |
| 845 | set rate [format %20s "many"] |
| 846 | } else { |
| 847 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 848 | } |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 849 | set u2 $units/s |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 850 | puts [format {%12d uS %s %s} $tm $rate $u2] |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 851 | global total_time |
| 852 | set total_time [expr {$total_time+$tm}] |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 853 | lappend ::speed_trial_times $name $tm |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 854 | } |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 855 | proc speed_trial_tcl {name numstmt units script} { |
| 856 | puts -nonewline [format {%-21.21s } $name...] |
| 857 | flush stdout |
| 858 | set speed [time {eval $script}] |
| 859 | set tm [lindex $speed 0] |
| 860 | if {$tm == 0} { |
| 861 | set rate [format %20s "many"] |
| 862 | } else { |
| 863 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 864 | } |
| 865 | set u2 $units/s |
| 866 | puts [format {%12d uS %s %s} $tm $rate $u2] |
| 867 | global total_time |
| 868 | set total_time [expr {$total_time+$tm}] |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 869 | lappend ::speed_trial_times $name $tm |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 870 | } |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 871 | proc speed_trial_init {name} { |
| 872 | global total_time |
| 873 | set total_time 0 |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 874 | set ::speed_trial_times [list] |
drh | bb810a9 | 2010-07-03 12:00:53 +0000 | [diff] [blame] | 875 | sqlite3 versdb :memory: |
| 876 | set vers [versdb one {SELECT sqlite_source_id()}] |
| 877 | versdb close |
| 878 | puts "SQLite $vers" |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 879 | } |
| 880 | proc speed_trial_summary {name} { |
| 881 | global total_time |
| 882 | puts [format {%-21.21s %12d uS TOTAL} $name $total_time] |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 883 | |
| 884 | if { 0 } { |
| 885 | sqlite3 versdb :memory: |
| 886 | set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0] |
| 887 | versdb close |
| 888 | puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);" |
| 889 | foreach {test us} $::speed_trial_times { |
| 890 | puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);" |
| 891 | } |
| 892 | } |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 893 | } |
| 894 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 895 | # Run this routine last |
| 896 | # |
| 897 | proc finish_test {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 898 | catch {db close} |
dan | e855983 | 2014-07-31 17:35:40 +0000 | [diff] [blame] | 899 | catch {db1 close} |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 900 | catch {db2 close} |
| 901 | catch {db3 close} |
| 902 | if {0==[info exists ::SLAVE]} { finalize_testing } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 903 | } |
| 904 | proc finalize_testing {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 905 | global sqlite_open_file_count |
| 906 | |
| 907 | set omitList [set_test_counter omit_list] |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 908 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 909 | catch {db close} |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 910 | catch {db2 close} |
| 911 | catch {db3 close} |
| 912 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 913 | vfs_unlink_test |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 914 | sqlite3 db {} |
danielk1977 | cbb8496 | 2006-01-17 16:10:13 +0000 | [diff] [blame] | 915 | # sqlite3_clear_tsd_memdebug |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 916 | db close |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 917 | sqlite3_reset_auto_extension |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 918 | |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 919 | sqlite3_soft_heap_limit 0 |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 920 | set nTest [incr_ntest] |
| 921 | set nErr [set_test_counter errors] |
| 922 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 923 | set nKnown 0 |
| 924 | if {[file readable known-problems.txt]} { |
| 925 | set fd [open known-problems.txt] |
| 926 | set content [read $fd] |
| 927 | close $fd |
| 928 | foreach x $content {set known_error($x) 1} |
| 929 | foreach x [set_test_counter fail_list] { |
| 930 | if {[info exists known_error($x)]} {incr nKnown} |
| 931 | } |
| 932 | } |
| 933 | if {$nKnown>0} { |
| 934 | puts "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\ |
| 935 | out of $nTest tests" |
| 936 | } else { |
| 937 | puts "$nErr errors out of $nTest tests" |
| 938 | } |
| 939 | if {$nErr>$nKnown} { |
| 940 | puts -nonewline "Failures on these tests:" |
| 941 | foreach x [set_test_counter fail_list] { |
| 942 | if {![info exists known_error($x)]} {puts -nonewline " $x"} |
| 943 | } |
| 944 | puts "" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 945 | } |
drh | cca17c3 | 2013-04-19 12:32:52 +0000 | [diff] [blame] | 946 | foreach warning [set_test_counter warn_list] { |
| 947 | puts "Warning: $warning" |
| 948 | } |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 949 | run_thread_tests 1 |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 950 | if {[llength $omitList]>0} { |
| 951 | puts "Omitted test cases:" |
| 952 | set prec {} |
| 953 | foreach {rec} [lsort $omitList] { |
| 954 | if {$rec==$prec} continue |
| 955 | set prec $rec |
| 956 | puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]] |
| 957 | } |
| 958 | } |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 959 | if {$nErr>0 && ![working_64bit_int]} { |
| 960 | puts "******************************************************************" |
| 961 | puts "N.B.: The version of TCL that you used to build this test harness" |
| 962 | puts "is defective in that it does not support 64-bit integers. Some or" |
| 963 | puts "all of the test failures above might be a result from this defect" |
| 964 | puts "in your TCL build." |
| 965 | puts "******************************************************************" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 966 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 967 | if {$::cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 968 | vfslog finalize binarylog |
danielk1977 | 374177e | 2008-05-08 15:58:06 +0000 | [diff] [blame] | 969 | } |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 970 | if {$sqlite_open_file_count} { |
| 971 | puts "$sqlite_open_file_count files were left open" |
| 972 | incr nErr |
| 973 | } |
drh | eafc43b | 2010-07-26 18:43:40 +0000 | [diff] [blame] | 974 | if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 || |
| 975 | [sqlite3_memory_used]>0} { |
| 976 | puts "Unfreed memory: [sqlite3_memory_used] bytes in\ |
| 977 | [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 978 | incr nErr |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 979 | ifcapable memdebug||mem5||(mem3&&debug) { |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 980 | puts "Writing unfreed memory log to \"./memleak.txt\"" |
| 981 | sqlite3_memdebug_dump ./memleak.txt |
| 982 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 983 | } else { |
| 984 | puts "All memory allocations freed - no leaks" |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 985 | ifcapable memdebug||mem5 { |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 986 | sqlite3_memdebug_dump ./memusage.txt |
| 987 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 988 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 989 | show_memstats |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 990 | puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes" |
| 991 | puts "Current memory usage: [sqlite3_memory_highwater] bytes" |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 992 | if {[info commands sqlite3_memdebug_malloc_count] ne ""} { |
| 993 | puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls" |
| 994 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 995 | if {$::cmdlinearg(malloctrace)} { |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 996 | puts "Writing mallocs.sql..." |
| 997 | memdebug_log_sql |
| 998 | sqlite3_memdebug_log stop |
| 999 | sqlite3_memdebug_log clear |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 1000 | |
| 1001 | if {[sqlite3_memory_used]>0} { |
| 1002 | puts "Writing leaks.sql..." |
| 1003 | sqlite3_memdebug_log sync |
| 1004 | memdebug_log_sql leaks.sql |
| 1005 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1006 | } |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 1007 | foreach f [glob -nocomplain test.db-*-journal] { |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 1008 | forcedelete $f |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 1009 | } |
| 1010 | foreach f [glob -nocomplain test.db-mj*] { |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 1011 | forcedelete $f |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 1012 | } |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 1013 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1016 | # Display memory statistics for analysis and debugging purposes. |
| 1017 | # |
| 1018 | proc show_memstats {} { |
| 1019 | set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 1020 | set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] |
| 1021 | set val [format {now %10d max %10d max-size %10d} \ |
| 1022 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1023 | puts "Memory used: $val" |
drh | eafc43b | 2010-07-26 18:43:40 +0000 | [diff] [blame] | 1024 | set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] |
| 1025 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 1026 | puts "Allocation count: $val" |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1027 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 1028 | set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0] |
| 1029 | set val [format {now %10d max %10d max-size %10d} \ |
| 1030 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1031 | puts "Page-cache used: $val" |
| 1032 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] |
| 1033 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 1034 | puts "Page-cache overflow: $val" |
| 1035 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] |
| 1036 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 1037 | puts "Scratch memory used: $val" |
| 1038 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 1039 | set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0] |
| 1040 | set val [format {now %10d max %10d max-size %10d} \ |
| 1041 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1042 | puts "Scratch overflow: $val" |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 1043 | ifcapable yytrackmaxstackdepth { |
| 1044 | set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0] |
| 1045 | set val [format { max %10d} [lindex $x 2]] |
| 1046 | puts "Parser stack depth: $val" |
| 1047 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1050 | # A procedure to execute SQL |
| 1051 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1052 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 1053 | # puts "SQL = $sql" |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 1054 | uplevel [list $db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1055 | } |
drh | 3c2aeae | 2014-01-24 14:37:44 +0000 | [diff] [blame] | 1056 | proc execsql_timed {sql {db db}} { |
| 1057 | set tm [time { |
| 1058 | set x [uplevel [list $db eval $sql]] |
| 1059 | } 1] |
| 1060 | set tm [lindex $tm 0] |
| 1061 | puts -nonewline " ([expr {$tm*0.001}]ms) " |
| 1062 | set x |
| 1063 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 1064 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1065 | # Execute SQL and catch exceptions. |
| 1066 | # |
| 1067 | proc catchsql {sql {db db}} { |
| 1068 | # puts "SQL = $sql" |
dan | 81fa6dc | 2009-11-28 12:40:32 +0000 | [diff] [blame] | 1069 | set r [catch [list uplevel [list $db eval $sql]] msg] |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1070 | lappend r $msg |
| 1071 | return $r |
| 1072 | } |
| 1073 | |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 1074 | # Do an VDBE code dump on the SQL given |
| 1075 | # |
| 1076 | proc explain {sql {db db}} { |
| 1077 | puts "" |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 1078 | puts "addr opcode p1 p2 p3 p4 p5 #" |
| 1079 | puts "---- ------------ ------ ------ ------ --------------- -- -" |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 1080 | $db eval "explain $sql" {} { |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 1081 | puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \ |
| 1082 | $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment |
| 1083 | ] |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1087 | proc explain_i {sql {db db}} { |
| 1088 | puts "" |
| 1089 | puts "addr opcode p1 p2 p3 p4 p5 #" |
| 1090 | puts "---- ------------ ------ ------ ------ ---------------- -- -" |
| 1091 | |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1092 | |
dan | 5c663c3 | 2013-11-06 14:52:40 +0000 | [diff] [blame] | 1093 | # Set up colors for the different opcodes. Scheme is as follows: |
| 1094 | # |
| 1095 | # Red: Opcodes that write to a b-tree. |
| 1096 | # Blue: Opcodes that reposition or seek a cursor. |
| 1097 | # Green: The ResultRow opcode. |
| 1098 | # |
dan | dea63f2 | 2014-03-03 16:48:47 +0000 | [diff] [blame] | 1099 | if { [catch {fconfigure stdout -mode}]==0 } { |
| 1100 | set R "\033\[31;1m" ;# Red fg |
| 1101 | set G "\033\[32;1m" ;# Green fg |
| 1102 | set B "\033\[34;1m" ;# Red fg |
| 1103 | set D "\033\[39;0m" ;# Default fg |
| 1104 | } else { |
| 1105 | set R "" |
| 1106 | set G "" |
| 1107 | set B "" |
| 1108 | set D "" |
| 1109 | } |
dan | 5c663c3 | 2013-11-06 14:52:40 +0000 | [diff] [blame] | 1110 | foreach opcode { |
| 1111 | Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind |
dan | e3ab729 | 2013-11-12 12:30:09 +0000 | [diff] [blame] | 1112 | NoConflict Next Prev VNext VPrev VFilter |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1113 | SorterSort SorterNext |
dan | 5c663c3 | 2013-11-06 14:52:40 +0000 | [diff] [blame] | 1114 | } { |
| 1115 | set color($opcode) $B |
| 1116 | } |
| 1117 | foreach opcode {ResultRow} { |
| 1118 | set color($opcode) $G |
| 1119 | } |
| 1120 | foreach opcode {IdxInsert Insert Delete IdxDelete} { |
| 1121 | set color($opcode) $R |
| 1122 | } |
| 1123 | |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 1124 | set bSeenGoto 0 |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1125 | $db eval "explain $sql" {} { |
| 1126 | set x($addr) 0 |
| 1127 | set op($addr) $opcode |
| 1128 | |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 1129 | if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} { |
| 1130 | set linebreak($p2) 1 |
| 1131 | set bSeenGoto 1 |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
dan | e3ab729 | 2013-11-12 12:30:09 +0000 | [diff] [blame] | 1134 | if {$opcode=="Next" || $opcode=="Prev" |
| 1135 | || $opcode=="VNext" || $opcode=="VPrev" |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1136 | || $opcode=="SorterNext" |
dan | e3ab729 | 2013-11-12 12:30:09 +0000 | [diff] [blame] | 1137 | } { |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1138 | for {set i $p2} {$i<$addr} {incr i} { |
| 1139 | incr x($i) 2 |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} { |
| 1144 | for {set i [expr $p2+1]} {$i<$addr} {incr i} { |
| 1145 | incr x($i) 2 |
| 1146 | } |
| 1147 | } |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 1148 | |
| 1149 | if {$opcode == "Halt" && $comment == "End of coroutine"} { |
| 1150 | set linebreak([expr $addr+1]) 1 |
| 1151 | } |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | $db eval "explain $sql" {} { |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 1155 | if {[info exists linebreak($addr)]} { |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1156 | puts "" |
| 1157 | } |
| 1158 | set I [string repeat " " $x($addr)] |
dan | 5c663c3 | 2013-11-06 14:52:40 +0000 | [diff] [blame] | 1159 | |
| 1160 | set col "" |
| 1161 | catch { set col $color($opcode) } |
| 1162 | |
| 1163 | puts [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \ |
| 1164 | $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1165 | ] |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1166 | } |
| 1167 | puts "---- ------------ ------ ------ ------ ---------------- -- -" |
| 1168 | } |
| 1169 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1170 | # Show the VDBE program for an SQL statement but omit the Trace |
| 1171 | # opcode at the beginning. This procedure can be used to prove |
| 1172 | # that different SQL statements generate exactly the same VDBE code. |
| 1173 | # |
| 1174 | proc explain_no_trace {sql} { |
| 1175 | set tr [db eval "EXPLAIN $sql"] |
| 1176 | return [lrange $tr 7 end] |
| 1177 | } |
| 1178 | |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 1179 | # Another procedure to execute SQL. This one includes the field |
| 1180 | # names in the returned list. |
| 1181 | # |
| 1182 | proc execsql2 {sql} { |
| 1183 | set result {} |
| 1184 | db eval $sql data { |
| 1185 | foreach f $data(*) { |
| 1186 | lappend result $f $data($f) |
| 1187 | } |
| 1188 | } |
| 1189 | return $result |
| 1190 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 1191 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1192 | # Use a temporary in-memory database to execute SQL statements |
| 1193 | # |
| 1194 | proc memdbsql {sql} { |
| 1195 | sqlite3 memdb :memory: |
| 1196 | set result [memdb eval $sql] |
| 1197 | memdb close |
| 1198 | return $result |
| 1199 | } |
| 1200 | |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 1201 | # Use the non-callback API to execute multiple SQL statements |
| 1202 | # |
| 1203 | proc stepsql {dbptr sql} { |
| 1204 | set sql [string trim $sql] |
| 1205 | set r 0 |
| 1206 | while {[string length $sql]>0} { |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1207 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 1208 | return [list 1 $vm] |
| 1209 | } |
| 1210 | set sql [string trim $sqltail] |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1211 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 1212 | # foreach v $VAL {lappend r $v} |
| 1213 | # } |
| 1214 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 1215 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 1216 | lappend r [sqlite3_column_text $vm $i] |
| 1217 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 1218 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1219 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 1220 | return [list 1 $errmsg] |
| 1221 | } |
| 1222 | } |
| 1223 | return $r |
| 1224 | } |
| 1225 | |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 1226 | # Do an integrity check of the entire database |
| 1227 | # |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 1228 | proc integrity_check {name {db db}} { |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 1229 | ifcapable integrityck { |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 1230 | do_test $name [list execsql {PRAGMA integrity_check} $db] {ok} |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
drh | 433dccf | 2013-02-09 15:37:11 +0000 | [diff] [blame] | 1234 | # Check the extended error code |
| 1235 | # |
| 1236 | proc verify_ex_errcode {name expected {db db}} { |
| 1237 | do_test $name [list sqlite3_extended_errcode $db] $expected |
| 1238 | } |
| 1239 | |
dan | c6055c7 | 2011-04-28 18:46:46 +0000 | [diff] [blame] | 1240 | |
| 1241 | # Return true if the SQL statement passed as the second argument uses a |
| 1242 | # statement transaction. |
| 1243 | # |
| 1244 | proc sql_uses_stmt {db sql} { |
| 1245 | set stmt [sqlite3_prepare $db $sql -1 dummy] |
| 1246 | set uses [uses_stmt_journal $stmt] |
| 1247 | sqlite3_finalize $stmt |
| 1248 | return $uses |
| 1249 | } |
| 1250 | |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 1251 | proc fix_ifcapable_expr {expr} { |
| 1252 | set ret "" |
| 1253 | set state 0 |
| 1254 | for {set i 0} {$i < [string length $expr]} {incr i} { |
| 1255 | set char [string range $expr $i $i] |
| 1256 | set newstate [expr {[string is alnum $char] || $char eq "_"}] |
| 1257 | if {$newstate && !$state} { |
| 1258 | append ret {$::sqlite_options(} |
| 1259 | } |
| 1260 | if {!$newstate && $state} { |
| 1261 | append ret ) |
| 1262 | } |
| 1263 | append ret $char |
| 1264 | set state $newstate |
| 1265 | } |
| 1266 | if {$state} {append ret )} |
| 1267 | return $ret |
| 1268 | } |
| 1269 | |
mistachkin | c60941f | 2012-09-13 01:51:02 +0000 | [diff] [blame] | 1270 | # Returns non-zero if the capabilities are present; zero otherwise. |
| 1271 | # |
| 1272 | proc capable {expr} { |
| 1273 | set e [fix_ifcapable_expr $expr]; return [expr ($e)] |
| 1274 | } |
| 1275 | |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 1276 | # Evaluate a boolean expression of capabilities. If true, execute the |
| 1277 | # code. Omit the code if false. |
| 1278 | # |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 1279 | proc ifcapable {expr code {else ""} {elsecode ""}} { |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 1280 | #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 |
| 1281 | set e2 [fix_ifcapable_expr $expr] |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 1282 | if ($e2) { |
| 1283 | set c [catch {uplevel 1 $code} r] |
| 1284 | } else { |
| 1285 | set c [catch {uplevel 1 $elsecode} r] |
| 1286 | } |
| 1287 | return -code $c $r |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 1288 | } |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 1289 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1290 | # This proc execs a seperate process that crashes midway through executing |
| 1291 | # the SQL script $sql on database test.db. |
| 1292 | # |
| 1293 | # The crash occurs during a sync() of file $crashfile. When the crash |
| 1294 | # occurs a random subset of all unsynced writes made by the process are |
| 1295 | # written into the files on disk. Argument $crashdelay indicates the |
| 1296 | # number of file syncs to wait before crashing. |
| 1297 | # |
| 1298 | # The return value is a list of two elements. The first element is a |
| 1299 | # boolean, indicating whether or not the process actually crashed or |
| 1300 | # reported some other error. The second element in the returned list is the |
| 1301 | # error message. This is "child process exited abnormally" if the crash |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 1302 | # occurred. |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1303 | # |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 1304 | # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1305 | # |
| 1306 | proc crashsql {args} { |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1307 | |
| 1308 | set blocksize "" |
| 1309 | set crashdelay 1 |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 1310 | set prngseed 0 |
dan | 999cd08 | 2013-12-09 20:42:03 +0000 | [diff] [blame] | 1311 | set opendb { sqlite3 db test.db -vfs crash } |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 1312 | set tclbody {} |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1313 | set crashfile "" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 1314 | set dc "" |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1315 | set sql [lindex $args end] |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1316 | |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1317 | for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { |
| 1318 | set z [lindex $args $ii] |
| 1319 | set n [string length $z] |
| 1320 | set z2 [lindex $args [expr $ii+1]] |
| 1321 | |
| 1322 | if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \ |
dan | 999cd08 | 2013-12-09 20:42:03 +0000 | [diff] [blame] | 1323 | elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \ |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 1324 | elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1325 | elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 1326 | elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 1327 | elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 1328 | elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1329 | else { error "Unrecognized option: $z" } |
| 1330 | } |
| 1331 | |
| 1332 | if {$crashfile eq ""} { |
| 1333 | error "Compulsory option -file missing" |
| 1334 | } |
| 1335 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1336 | # $crashfile gets compared to the native filename in |
shaneh | f2c0882 | 2010-07-08 16:30:44 +0000 | [diff] [blame] | 1337 | # cfSync(), which can be different then what TCL uses by |
| 1338 | # default, so here we force it to the "nativename" format. |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 1339 | set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]] |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1340 | |
| 1341 | set f [open crash.tcl w] |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 1342 | puts $f "sqlite3_crash_enable 1" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 1343 | puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 1344 | puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" |
dan | 999cd08 | 2013-12-09 20:42:03 +0000 | [diff] [blame] | 1345 | puts $f $opendb |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 1346 | |
| 1347 | # This block sets the cache size of the main database to 10 |
| 1348 | # pages. This is done in case the build is configured to omit |
| 1349 | # "PRAGMA cache_size". |
| 1350 | puts $f {db eval {SELECT * FROM sqlite_master;}} |
| 1351 | puts $f {set bt [btree_from_db db]} |
| 1352 | puts $f {btree_set_cache_size $bt 10} |
dan | 999cd08 | 2013-12-09 20:42:03 +0000 | [diff] [blame] | 1353 | |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 1354 | if {$prngseed} { |
| 1355 | set seed [expr {$prngseed%10007+1}] |
| 1356 | # puts seed=$seed |
| 1357 | puts $f "db eval {SELECT randomblob($seed)}" |
| 1358 | } |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 1359 | |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 1360 | if {[string length $tclbody]>0} { |
| 1361 | puts $f $tclbody |
| 1362 | } |
| 1363 | if {[string length $sql]>0} { |
| 1364 | puts $f "db eval {" |
| 1365 | puts $f "$sql" |
| 1366 | puts $f "}" |
| 1367 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1368 | close $f |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1369 | set r [catch { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1370 | exec [info nameofexec] crash.tcl >@stdout |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1371 | } msg] |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1372 | |
shaneh | f2c0882 | 2010-07-08 16:30:44 +0000 | [diff] [blame] | 1373 | # Windows/ActiveState TCL returns a slightly different |
| 1374 | # error message. We map that to the expected message |
| 1375 | # so that we don't have to change all of the test |
| 1376 | # cases. |
| 1377 | if {$::tcl_platform(platform)=="windows"} { |
| 1378 | if {$msg=="child killed: unknown signal"} { |
| 1379 | set msg "child process exited abnormally" |
| 1380 | } |
| 1381 | } |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1382 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1383 | lappend r $msg |
| 1384 | } |
| 1385 | |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1386 | proc run_ioerr_prep {} { |
| 1387 | set ::sqlite_io_error_pending 0 |
| 1388 | catch {db close} |
| 1389 | catch {db2 close} |
| 1390 | catch {forcedelete test.db} |
| 1391 | catch {forcedelete test.db-journal} |
| 1392 | catch {forcedelete test2.db} |
| 1393 | catch {forcedelete test2.db-journal} |
| 1394 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
| 1395 | sqlite3_extended_result_codes $::DB $::ioerropts(-erc) |
| 1396 | if {[info exists ::ioerropts(-tclprep)]} { |
| 1397 | eval $::ioerropts(-tclprep) |
| 1398 | } |
| 1399 | if {[info exists ::ioerropts(-sqlprep)]} { |
| 1400 | execsql $::ioerropts(-sqlprep) |
| 1401 | } |
| 1402 | expr 0 |
| 1403 | } |
| 1404 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1405 | # Usage: do_ioerr_test <test number> <options...> |
| 1406 | # |
| 1407 | # This proc is used to implement test cases that check that IO errors |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1408 | # are correctly handled. The first argument, <test number>, is an integer |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1409 | # used to name the tests executed by this proc. Options are as follows: |
| 1410 | # |
| 1411 | # -tclprep TCL script to run to prepare test. |
| 1412 | # -sqlprep SQL script to run to prepare test. |
| 1413 | # -tclbody TCL script to run with IO error simulation. |
| 1414 | # -sqlbody TCL script to run with IO error simulation. |
| 1415 | # -exclude List of 'N' values not to test. |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1416 | # -erc Use extended result codes |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 1417 | # -persist Make simulated I/O errors persistent |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1418 | # -start Value of 'N' to begin with (default 1) |
| 1419 | # |
| 1420 | # -cksum Boolean. If true, test that the database does |
| 1421 | # not change during the execution of the test case. |
| 1422 | # |
| 1423 | proc do_ioerr_test {testname args} { |
| 1424 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1425 | set ::ioerropts(-start) 1 |
| 1426 | set ::ioerropts(-cksum) 0 |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1427 | set ::ioerropts(-erc) 0 |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 1428 | set ::ioerropts(-count) 100000000 |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 1429 | set ::ioerropts(-persist) 1 |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1430 | set ::ioerropts(-ckrefcount) 0 |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1431 | set ::ioerropts(-restoreprng) 1 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1432 | array set ::ioerropts $args |
| 1433 | |
danielk1977 | 47cd39c | 2008-05-12 12:41:15 +0000 | [diff] [blame] | 1434 | # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are |
| 1435 | # a couple of obscure IO errors that do not return them. |
| 1436 | set ::ioerropts(-erc) 0 |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1437 | |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1438 | # Create a single TCL script from the TCL and SQL specified |
| 1439 | # as the body of the test. |
| 1440 | set ::ioerrorbody {} |
| 1441 | if {[info exists ::ioerropts(-tclbody)]} { |
| 1442 | append ::ioerrorbody "$::ioerropts(-tclbody)\n" |
| 1443 | } |
| 1444 | if {[info exists ::ioerropts(-sqlbody)]} { |
| 1445 | append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" |
| 1446 | } |
| 1447 | |
| 1448 | save_prng_state |
| 1449 | if {$::ioerropts(-cksum)} { |
| 1450 | run_ioerr_prep |
| 1451 | eval $::ioerrorbody |
| 1452 | set ::goodcksum [cksum] |
| 1453 | } |
danielk1977 | 47cd39c | 2008-05-12 12:41:15 +0000 | [diff] [blame] | 1454 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1455 | set ::go 1 |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1456 | #reset_prng_state |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 1457 | for {set n $::ioerropts(-start)} {$::go} {incr n} { |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 1458 | set ::TN $n |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 1459 | incr ::ioerropts(-count) -1 |
| 1460 | if {$::ioerropts(-count)<0} break |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1461 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1462 | # Skip this IO error if it was specified with the "-exclude" option. |
| 1463 | if {[info exists ::ioerropts(-exclude)]} { |
| 1464 | if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue |
| 1465 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1466 | if {$::ioerropts(-restoreprng)} { |
| 1467 | restore_prng_state |
| 1468 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1469 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1470 | # Delete the files test.db and test2.db, then execute the TCL and |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1471 | # SQL (in that order) to prepare for the test case. |
| 1472 | do_test $testname.$n.1 { |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1473 | run_ioerr_prep |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1474 | } {0} |
| 1475 | |
| 1476 | # Read the 'checksum' of the database. |
| 1477 | if {$::ioerropts(-cksum)} { |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1478 | set ::checksum [cksum] |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1479 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1480 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1481 | # Set the Nth IO error to fail. |
| 1482 | do_test $testname.$n.2 [subst { |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 1483 | set ::sqlite_io_error_persist $::ioerropts(-persist) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1484 | set ::sqlite_io_error_pending $n |
| 1485 | }] $n |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1486 | |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1487 | # Execute the TCL script created for the body of this test. If |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1488 | # at least N IO operations performed by SQLite as a result of |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1489 | # the script, the Nth will fail. |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1490 | do_test $testname.$n.3 { |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1491 | set ::sqlite_io_error_hit 0 |
| 1492 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1493 | set r [catch $::ioerrorbody msg] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1494 | set ::errseen $r |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 1495 | set rc [sqlite3_errcode $::DB] |
| 1496 | if {$::ioerropts(-erc)} { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 1497 | # If we are in extended result code mode, make sure all of the |
| 1498 | # IOERRs we get back really do have their extended code values. |
| 1499 | # If an extended result code is returned, the sqlite3_errcode |
| 1500 | # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn |
| 1501 | # where nnnn is a number |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 1502 | if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { |
| 1503 | return $rc |
| 1504 | } |
| 1505 | } else { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 1506 | # If we are not in extended result code mode, make sure no |
| 1507 | # extended error codes are returned. |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 1508 | if {[regexp {\+\d} $rc]} { |
| 1509 | return $rc |
| 1510 | } |
| 1511 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1512 | # The test repeats as long as $::go is non-zero. $::go starts out |
| 1513 | # as 1. When a test runs to completion without hitting an I/O |
| 1514 | # error, that means there is no point in continuing with this test |
| 1515 | # case so set $::go to zero. |
| 1516 | # |
| 1517 | if {$::sqlite_io_error_pending>0} { |
| 1518 | set ::go 0 |
| 1519 | set q 0 |
| 1520 | set ::sqlite_io_error_pending 0 |
| 1521 | } else { |
| 1522 | set q 1 |
| 1523 | } |
| 1524 | |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 1525 | set s [expr $::sqlite_io_error_hit==0] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1526 | if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} { |
| 1527 | set r 1 |
| 1528 | } |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 1529 | set ::sqlite_io_error_hit 0 |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 1530 | |
| 1531 | # One of two things must have happened. either |
| 1532 | # 1. We never hit the IO error and the SQL returned OK |
| 1533 | # 2. An IO error was hit and the SQL failed |
| 1534 | # |
dan | d41a29a | 2010-05-06 15:56:28 +0000 | [diff] [blame] | 1535 | #puts "s=$s r=$r q=$q" |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1536 | expr { ($s && !$r && !$q) || (!$s && $r && $q) } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1537 | } {1} |
| 1538 | |
danielk1977 | 80daec6 | 2008-05-12 10:57:02 +0000 | [diff] [blame] | 1539 | set ::sqlite_io_error_hit 0 |
| 1540 | set ::sqlite_io_error_pending 0 |
| 1541 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1542 | # Check that no page references were leaked. There should be |
| 1543 | # a single reference if there is still an active transaction, |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1544 | # or zero otherwise. |
| 1545 | # |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 1546 | # UPDATE: If the IO error occurs after a 'BEGIN' but before any |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1547 | # locks are established on database files (i.e. if the error |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 1548 | # occurs while attempting to detect a hot-journal file), then |
| 1549 | # there may 0 page references and an active transaction according |
| 1550 | # to [sqlite3_get_autocommit]. |
| 1551 | # |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1552 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} { |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1553 | do_test $testname.$n.4 { |
| 1554 | set bt [btree_from_db db] |
| 1555 | db_enter db |
| 1556 | array set stats [btree_pager_stats $bt] |
| 1557 | db_leave db |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 1558 | set nRef $stats(ref) |
| 1559 | expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)} |
| 1560 | } {1} |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1563 | # If there is an open database handle and no open transaction, |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1564 | # and the pager is not running in exclusive-locking mode, |
| 1565 | # check that the pager is in "unlocked" state. Theoretically, |
| 1566 | # if a call to xUnlock() failed due to an IO error the underlying |
| 1567 | # file may still be locked. |
| 1568 | # |
| 1569 | ifcapable pragma { |
| 1570 | if { [info commands db] ne "" |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 1571 | && $::ioerropts(-ckrefcount) |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1572 | && [db one {pragma locking_mode}] eq "normal" |
| 1573 | && [sqlite3_get_autocommit db] |
| 1574 | } { |
| 1575 | do_test $testname.$n.5 { |
| 1576 | set bt [btree_from_db db] |
| 1577 | db_enter db |
| 1578 | array set stats [btree_pager_stats $bt] |
| 1579 | db_leave db |
| 1580 | set stats(state) |
| 1581 | } 0 |
| 1582 | } |
| 1583 | } |
| 1584 | |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 1585 | # If an IO error occurred, then the checksum of the database should |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1586 | # 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] | 1587 | # |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1588 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} { |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 1589 | do_test $testname.$n.6 { |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1590 | catch {db close} |
danielk1977 | a961339 | 2008-07-08 12:07:32 +0000 | [diff] [blame] | 1591 | catch {db2 close} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 1592 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1593 | set nowcksum [cksum] |
| 1594 | set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}] |
| 1595 | if {$res==0} { |
| 1596 | puts "now=$nowcksum" |
| 1597 | puts "the=$::checksum" |
| 1598 | puts "fwd=$::goodcksum" |
| 1599 | } |
| 1600 | set res |
| 1601 | } 1 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1604 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 1605 | set ::sqlite_io_error_pending 0 |
danielk1977 | 105afed | 2005-05-26 15:20:53 +0000 | [diff] [blame] | 1606 | if {[info exists ::ioerropts(-cleanup)]} { |
| 1607 | catch $::ioerropts(-cleanup) |
| 1608 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1609 | } |
| 1610 | set ::sqlite_io_error_pending 0 |
drh | 6d54da0 | 2007-03-25 19:08:46 +0000 | [diff] [blame] | 1611 | set ::sqlite_io_error_persist 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1612 | unset ::ioerropts |
| 1613 | } |
| 1614 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1615 | # Return a checksum based on the contents of the main database associated |
| 1616 | # with connection $db |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1617 | # |
| 1618 | proc cksum {{db db}} { |
| 1619 | set txt [$db eval { |
| 1620 | SELECT name, type, sql FROM sqlite_master order by name |
| 1621 | }]\n |
| 1622 | foreach tbl [$db eval { |
| 1623 | SELECT name FROM sqlite_master WHERE type='table' order by name |
| 1624 | }] { |
| 1625 | append txt [$db eval "SELECT * FROM $tbl"]\n |
| 1626 | } |
| 1627 | foreach prag {default_synchronous default_cache_size} { |
| 1628 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 1629 | } |
| 1630 | set cksum [string length $txt]-[md5 $txt] |
| 1631 | # puts $cksum-[file size test.db] |
| 1632 | return $cksum |
| 1633 | } |
| 1634 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1635 | # Generate a checksum based on the contents of the main and temp tables |
| 1636 | # database $db. If the checksum of two databases is the same, and the |
| 1637 | # integrity-check passes for both, the two databases are identical. |
| 1638 | # |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 1639 | proc allcksum {{db db}} { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1640 | set ret [list] |
| 1641 | ifcapable tempdb { |
| 1642 | set sql { |
| 1643 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 1644 | SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION |
| 1645 | SELECT 'sqlite_master' UNION |
| 1646 | SELECT 'sqlite_temp_master' ORDER BY 1 |
| 1647 | } |
| 1648 | } else { |
| 1649 | set sql { |
| 1650 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 1651 | SELECT 'sqlite_master' ORDER BY 1 |
| 1652 | } |
| 1653 | } |
| 1654 | set tbllist [$db eval $sql] |
| 1655 | set txt {} |
| 1656 | foreach tbl $tbllist { |
| 1657 | append txt [$db eval "SELECT * FROM $tbl"] |
| 1658 | } |
| 1659 | foreach prag {default_cache_size} { |
| 1660 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 1661 | } |
| 1662 | # puts txt=$txt |
| 1663 | return [md5 $txt] |
| 1664 | } |
| 1665 | |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 1666 | # Generate a checksum based on the contents of a single database with |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1667 | # a database connection. The name of the database is $dbname. |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 1668 | # Examples of $dbname are "temp" or "main". |
| 1669 | # |
| 1670 | proc dbcksum {db dbname} { |
| 1671 | if {$dbname=="temp"} { |
| 1672 | set master sqlite_temp_master |
| 1673 | } else { |
| 1674 | set master $dbname.sqlite_master |
| 1675 | } |
| 1676 | set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] |
| 1677 | set txt [$db eval "SELECT * FROM $master"]\n |
| 1678 | foreach tab $alltab { |
| 1679 | append txt [$db eval "SELECT * FROM $dbname.$tab"]\n |
| 1680 | } |
| 1681 | return [md5 $txt] |
| 1682 | } |
| 1683 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1684 | proc memdebug_log_sql {{filename mallocs.sql}} { |
| 1685 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1686 | set data [sqlite3_memdebug_log dump] |
| 1687 | set nFrame [expr [llength [lindex $data 0]]-2] |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1688 | if {$nFrame < 0} { return "" } |
| 1689 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1690 | set database temp |
| 1691 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1692 | set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1693 | |
| 1694 | set sql "" |
| 1695 | foreach e $data { |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1696 | set nCall [lindex $e 0] |
| 1697 | set nByte [lindex $e 1] |
| 1698 | set lStack [lrange $e 2 end] |
| 1699 | append sql "INSERT INTO ${database}.malloc VALUES" |
| 1700 | append sql "('test', $nCall, $nByte, '$lStack');\n" |
| 1701 | foreach f $lStack { |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1702 | set frames($f) 1 |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1707 | set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1708 | |
| 1709 | foreach f [array names frames] { |
| 1710 | set addr [format %x $f] |
| 1711 | set cmd "addr2line -e [info nameofexec] $addr" |
| 1712 | set line [eval exec $cmd] |
| 1713 | append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1714 | |
| 1715 | set file [lindex [split $line :] 0] |
| 1716 | set files($file) 1 |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1719 | foreach f [array names files] { |
| 1720 | set contents "" |
| 1721 | catch { |
| 1722 | set fd [open $f] |
| 1723 | set contents [read $fd] |
| 1724 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1725 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1726 | set contents [string map {' ''} $contents] |
| 1727 | append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1728 | } |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1729 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1730 | set fd [open $filename w] |
| 1731 | puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" |
| 1732 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1733 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1734 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1735 | # Drop all tables in database [db] |
| 1736 | proc drop_all_tables {{db db}} { |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1737 | ifcapable trigger&&foreignkey { |
| 1738 | set pk [$db one "PRAGMA foreign_keys"] |
| 1739 | $db eval "PRAGMA foreign_keys = OFF" |
| 1740 | } |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1741 | foreach {idx name file} [db eval {PRAGMA database_list}] { |
| 1742 | if {$idx==1} { |
| 1743 | set master sqlite_temp_master |
| 1744 | } else { |
| 1745 | set master $name.sqlite_master |
| 1746 | } |
| 1747 | foreach {t type} [$db eval " |
| 1748 | SELECT name, type FROM $master |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 1749 | WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X' |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1750 | "] { |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 1751 | $db eval "DROP $type \"$t\"" |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1752 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1753 | } |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1754 | ifcapable trigger&&foreignkey { |
| 1755 | $db eval "PRAGMA foreign_keys = $pk" |
| 1756 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1759 | #------------------------------------------------------------------------- |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1760 | # If a test script is executed with global variable $::G(perm:name) set to |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1761 | # "wal", then the tests are run in WAL mode. Otherwise, they should be run |
| 1762 | # in rollback mode. The following Tcl procs are used to make this less |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1763 | # intrusive: |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1764 | # |
| 1765 | # wal_set_journal_mode ?DB? |
| 1766 | # |
| 1767 | # If running a WAL test, execute "PRAGMA journal_mode = wal" using |
| 1768 | # connection handle DB. Otherwise, this command is a no-op. |
| 1769 | # |
| 1770 | # wal_check_journal_mode TESTNAME ?DB? |
| 1771 | # |
| 1772 | # If running a WAL test, execute a tests case that fails if the main |
| 1773 | # database for connection handle DB is not currently a WAL database. |
| 1774 | # Otherwise (if not running a WAL permutation) this is a no-op. |
| 1775 | # |
| 1776 | # wal_is_wal_mode |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1777 | # |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1778 | # Returns true if this test should be run in WAL mode. False otherwise. |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1779 | # |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1780 | proc wal_is_wal_mode {} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1781 | expr {[permutation] eq "wal"} |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1782 | } |
| 1783 | proc wal_set_journal_mode {{db db}} { |
| 1784 | if { [wal_is_wal_mode] } { |
| 1785 | $db eval "PRAGMA journal_mode = WAL" |
| 1786 | } |
| 1787 | } |
| 1788 | proc wal_check_journal_mode {testname {db db}} { |
| 1789 | if { [wal_is_wal_mode] } { |
| 1790 | $db eval { SELECT * FROM sqlite_master } |
| 1791 | do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal} |
| 1792 | } |
| 1793 | } |
| 1794 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1795 | proc permutation {} { |
| 1796 | set perm "" |
| 1797 | catch {set perm $::G(perm:name)} |
| 1798 | set perm |
| 1799 | } |
dan | cb79e51 | 2010-08-06 13:50:07 +0000 | [diff] [blame] | 1800 | proc presql {} { |
| 1801 | set presql "" |
| 1802 | catch {set presql $::G(perm:presql)} |
| 1803 | set presql |
| 1804 | } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1805 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1806 | #------------------------------------------------------------------------- |
| 1807 | # |
| 1808 | proc slave_test_script {script} { |
| 1809 | |
| 1810 | # Create the interpreter used to run the test script. |
| 1811 | interp create tinterp |
| 1812 | |
| 1813 | # Populate some global variables that tester.tcl expects to see. |
| 1814 | foreach {var value} [list \ |
| 1815 | ::argv0 $::argv0 \ |
| 1816 | ::argv {} \ |
| 1817 | ::SLAVE 1 \ |
| 1818 | ] { |
| 1819 | interp eval tinterp [list set $var $value] |
| 1820 | } |
| 1821 | |
| 1822 | # The alias used to access the global test counters. |
| 1823 | tinterp alias set_test_counter set_test_counter |
| 1824 | |
| 1825 | # Set up the ::cmdlinearg array in the slave. |
| 1826 | interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]] |
| 1827 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1828 | # Set up the ::G array in the slave. |
| 1829 | interp eval tinterp [list array set ::G [array get ::G]] |
| 1830 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1831 | # Load the various test interfaces implemented in C. |
| 1832 | load_testfixture_extensions tinterp |
| 1833 | |
| 1834 | # Run the test script. |
| 1835 | interp eval tinterp $script |
| 1836 | |
dan | ea5542d | 2010-07-06 11:26:15 +0000 | [diff] [blame] | 1837 | # Check if the interpreter call [run_thread_tests] |
| 1838 | if { [interp eval tinterp {info exists ::run_thread_tests_called}] } { |
| 1839 | set ::run_thread_tests_called 1 |
| 1840 | } |
| 1841 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1842 | # Delete the interpreter used to run the test script. |
| 1843 | interp delete tinterp |
| 1844 | } |
| 1845 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1846 | proc slave_test_file {zFile} { |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1847 | set tail [file tail $zFile] |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1848 | |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 1849 | if {[info exists ::G(start:permutation)]} { |
| 1850 | if {[permutation] != $::G(start:permutation)} return |
| 1851 | unset ::G(start:permutation) |
| 1852 | } |
| 1853 | if {[info exists ::G(start:file)]} { |
| 1854 | if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return |
| 1855 | unset ::G(start:file) |
| 1856 | } |
| 1857 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1858 | # Remember the value of the shared-cache setting. So that it is possible |
| 1859 | # to check afterwards that it was not modified by the test script. |
| 1860 | # |
| 1861 | ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] } |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1862 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1863 | # Run the test script in a slave interpreter. |
| 1864 | # |
dan | ea5542d | 2010-07-06 11:26:15 +0000 | [diff] [blame] | 1865 | unset -nocomplain ::run_thread_tests_called |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1866 | reset_prng_state |
| 1867 | set ::sqlite_open_file_count 0 |
| 1868 | set time [time { slave_test_script [list source $zFile] }] |
| 1869 | set ms [expr [lindex $time 0] / 1000] |
| 1870 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1871 | # Test that all files opened by the test script were closed. Omit this |
| 1872 | # if the test script has "thread" in its name. The open file counter |
| 1873 | # is not thread-safe. |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1874 | # |
dan | ea5542d | 2010-07-06 11:26:15 +0000 | [diff] [blame] | 1875 | if {[info exists ::run_thread_tests_called]==0} { |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1876 | do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0} |
| 1877 | } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1878 | set ::sqlite_open_file_count 0 |
| 1879 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1880 | # Test that the global "shared-cache" setting was not altered by |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1881 | # the test script. |
| 1882 | # |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1883 | ifcapable shared_cache { |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1884 | set res [expr {[sqlite3_enable_shared_cache] == $scs}] |
| 1885 | do_test ${tail}-sharedcachesetting [list set {} $res] 1 |
| 1886 | } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1887 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1888 | # Add some info to the output. |
| 1889 | # |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1890 | puts "Time: $tail $ms ms" |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1891 | show_memstats |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
dan | 59257dc | 2010-08-04 11:34:31 +0000 | [diff] [blame] | 1894 | # Open a new connection on database test.db and execute the SQL script |
| 1895 | # supplied as an argument. Before returning, close the new conection and |
| 1896 | # restore the 4 byte fields starting at header offsets 28, 92 and 96 |
| 1897 | # to the values they held before the SQL was executed. This simulates |
| 1898 | # a write by a pre-3.7.0 client. |
| 1899 | # |
| 1900 | proc sql36231 {sql} { |
| 1901 | set B [hexio_read test.db 92 8] |
| 1902 | set A [hexio_read test.db 28 4] |
| 1903 | sqlite3 db36231 test.db |
| 1904 | catch { db36231 func a_string a_string } |
| 1905 | execsql $sql db36231 |
| 1906 | db36231 close |
| 1907 | hexio_write test.db 28 $A |
| 1908 | hexio_write test.db 92 $B |
| 1909 | return "" |
| 1910 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1911 | |
dan | ccc7ff4 | 2010-11-29 12:06:45 +0000 | [diff] [blame] | 1912 | proc db_save {} { |
| 1913 | foreach f [glob -nocomplain sv_test.db*] { forcedelete $f } |
| 1914 | foreach f [glob -nocomplain test.db*] { |
| 1915 | set f2 "sv_$f" |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 1916 | forcecopy $f $f2 |
dan | ccc7ff4 | 2010-11-29 12:06:45 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | proc db_save_and_close {} { |
| 1920 | db_save |
| 1921 | catch { db close } |
| 1922 | return "" |
| 1923 | } |
| 1924 | proc db_restore {} { |
| 1925 | foreach f [glob -nocomplain test.db*] { forcedelete $f } |
| 1926 | foreach f2 [glob -nocomplain sv_test.db*] { |
| 1927 | set f [string range $f2 3 end] |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 1928 | forcecopy $f2 $f |
dan | ccc7ff4 | 2010-11-29 12:06:45 +0000 | [diff] [blame] | 1929 | } |
| 1930 | } |
| 1931 | proc db_restore_and_reopen {{dbfile test.db}} { |
| 1932 | catch { db close } |
| 1933 | db_restore |
| 1934 | sqlite3 db $dbfile |
| 1935 | } |
| 1936 | proc db_delete_and_reopen {{file test.db}} { |
| 1937 | catch { db close } |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 1938 | foreach f [glob -nocomplain test.db*] { forcedelete $f } |
dan | ccc7ff4 | 2010-11-29 12:06:45 +0000 | [diff] [blame] | 1939 | sqlite3 db $file |
| 1940 | } |
| 1941 | |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 1942 | # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set |
| 1943 | # to non-zero, then set the global variable $AUTOVACUUM to 1. |
| 1944 | set AUTOVACUUM $sqlite_options(default_autovacuum) |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 1945 | |
dan | 64b95bb | 2012-05-12 05:30:29 +0000 | [diff] [blame] | 1946 | # Make sure the FTS enhanced query syntax is disabled. |
| 1947 | set sqlite_fts3_enable_parentheses 0 |
| 1948 | |
drh | 09fe614 | 2013-11-29 15:06:27 +0000 | [diff] [blame] | 1949 | # During testing, assume that all database files are well-formed. The |
| 1950 | # few test cases that deliberately corrupt database files should rescind |
| 1951 | # this setting by invoking "database_can_be_corrupt" |
| 1952 | # |
| 1953 | database_never_corrupt |
| 1954 | |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 1955 | source $testdir/thread_common.tcl |
dan | ddf80eb | 2010-10-25 12:47:43 +0000 | [diff] [blame] | 1956 | source $testdir/malloc_common.tcl |