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