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